blob: 8bc9a796fe1194074a3a4fb8cb13bb30c241ddae [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer5f016e22007-07-11 17:01:13 +000014#include "clang/Parse/Parser.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000015#include "RAIIObjectsForParser.h"
Eli Friedmandc3b7232012-01-04 02:40:39 +000016#include "clang/Basic/PrettyStackTrace.h"
Richard Smith33762772012-03-08 23:06:02 +000017#include "clang/Lex/LiteralSupport.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
20#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000022#include "llvm/Support/ErrorHandling.h"
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Richard Smithea698b32011-04-14 21:45:45 +000026static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
27 switch (Kind) {
28 case tok::kw_template: return 0;
29 case tok::kw_const_cast: return 1;
30 case tok::kw_dynamic_cast: return 2;
31 case tok::kw_reinterpret_cast: return 3;
32 case tok::kw_static_cast: return 4;
33 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000034 llvm_unreachable("Unknown type for digraph error message.");
Richard Smithea698b32011-04-14 21:45:45 +000035 }
36}
37
38// Are the two tokens adjacent in the same source file?
Richard Smith19a27022012-06-18 06:11:04 +000039bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smithea698b32011-04-14 21:45:45 +000040 SourceManager &SM = PP.getSourceManager();
41 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000042 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smithea698b32011-04-14 21:45:45 +000043 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
44}
45
46// Suggest fixit for "<::" after a cast.
47static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
48 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
49 // Pull '<:' and ':' off token stream.
50 if (!AtDigraph)
51 PP.Lex(DigraphToken);
52 PP.Lex(ColonToken);
53
54 SourceRange Range;
55 Range.setBegin(DigraphToken.getLocation());
56 Range.setEnd(ColonToken.getLocation());
57 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
58 << SelectDigraphErrorMessage(Kind)
59 << FixItHint::CreateReplacement(Range, "< ::");
60
61 // Update token information to reflect their change in token type.
62 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000063 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smithea698b32011-04-14 21:45:45 +000064 ColonToken.setLength(2);
65 DigraphToken.setKind(tok::less);
66 DigraphToken.setLength(1);
67
68 // Push new tokens back to token stream.
69 PP.EnterToken(ColonToken);
70 if (!AtDigraph)
71 PP.EnterToken(DigraphToken);
72}
73
Richard Trieu950be712011-09-19 19:01:00 +000074// Check for '<::' which should be '< ::' instead of '[:' when following
75// a template name.
76void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
77 bool EnteringContext,
78 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieuc11030e2011-09-20 20:03:50 +000079 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu950be712011-09-19 19:01:00 +000080 return;
81
82 Token SecondToken = GetLookAheadToken(2);
Richard Smith19a27022012-06-18 06:11:04 +000083 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu950be712011-09-19 19:01:00 +000084 return;
85
86 TemplateTy Template;
87 UnqualifiedId TemplateName;
88 TemplateName.setIdentifier(&II, Tok.getLocation());
89 bool MemberOfUnknownSpecialization;
90 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
91 TemplateName, ObjectType, EnteringContext,
92 Template, MemberOfUnknownSpecialization))
93 return;
94
95 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
96 /*AtDigraph*/false);
97}
98
Richard Trieu919b9552012-11-02 01:08:58 +000099/// \brief Emits an error for a left parentheses after a double colon.
100///
101/// When a '(' is found after a '::', emit an error. Attempt to fix the token
Nico Weberbba91b82012-11-29 05:29:23 +0000102/// stream by removing the '(', and the matching ')' if found.
Richard Trieu919b9552012-11-02 01:08:58 +0000103void Parser::CheckForLParenAfterColonColon() {
104 if (!Tok.is(tok::l_paren))
105 return;
106
107 SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
108 Token Tok1 = getCurToken();
109 if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
110 return;
111
112 if (Tok1.is(tok::identifier)) {
113 Token Tok2 = GetLookAheadToken(1);
114 if (Tok2.is(tok::r_paren)) {
115 ConsumeToken();
116 PP.EnterToken(Tok1);
117 r_parenLoc = ConsumeParen();
118 }
119 } else if (Tok1.is(tok::star)) {
120 Token Tok2 = GetLookAheadToken(1);
121 if (Tok2.is(tok::identifier)) {
122 Token Tok3 = GetLookAheadToken(2);
123 if (Tok3.is(tok::r_paren)) {
124 ConsumeToken();
125 ConsumeToken();
126 PP.EnterToken(Tok2);
127 PP.EnterToken(Tok1);
128 r_parenLoc = ConsumeParen();
129 }
130 }
131 }
132
133 Diag(l_parenLoc, diag::err_paren_after_colon_colon)
134 << FixItHint::CreateRemoval(l_parenLoc)
135 << FixItHint::CreateRemoval(r_parenLoc);
136}
137
Mike Stump1eb44332009-09-09 15:08:12 +0000138/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000139///
140/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000141/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000142/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000143///
144/// '::'[opt] nested-name-specifier
145/// '::'
146///
147/// nested-name-specifier:
148/// type-name '::'
149/// namespace-name '::'
150/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000151/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000152///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000153///
Mike Stump1eb44332009-09-09 15:08:12 +0000154/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000155/// nested-name-specifier (or empty)
156///
Mike Stump1eb44332009-09-09 15:08:12 +0000157/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000158/// the "." or "->" of a member access expression, this parameter provides the
159/// type of the object whose members are being accessed.
160///
161/// \param EnteringContext whether we will be entering into the context of
162/// the nested-name-specifier after parsing it.
163///
Douglas Gregord4dca082010-02-24 18:44:31 +0000164/// \param MayBePseudoDestructor When non-NULL, points to a flag that
165/// indicates whether this nested-name-specifier may be part of a
166/// pseudo-destructor name. In this case, the flag will be set false
167/// if we don't actually end up parsing a destructor name. Moreorover,
168/// if we do end up determining that we are parsing a destructor name,
169/// the last component of the nested-name-specifier is not parsed as
170/// part of the scope specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000171///
172/// \param IsTypename If \c true, this nested-name-specifier is known to be
173/// part of a type name. This is used to improve error recovery.
174///
175/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
176/// filled in with the leading identifier in the last component of the
177/// nested-name-specifier, if any.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000178///
John McCall9ba61662010-02-26 08:45:28 +0000179/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000180bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000181 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000182 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000183 bool *MayBePseudoDestructor,
Richard Smith2db075b2013-03-26 01:15:19 +0000184 bool IsTypename,
185 IdentifierInfo **LastII) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000186 assert(getLangOpts().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000187 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000189 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith2db075b2013-03-26 01:15:19 +0000190 assert(!LastII && "want last identifier but have already annotated scope");
Douglas Gregorc34348a2011-02-24 17:54:50 +0000191 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
192 Tok.getAnnotationRange(),
193 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000194 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000195 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000196 }
Chris Lattnere607e802009-01-04 21:14:15 +0000197
Richard Smith2db075b2013-03-26 01:15:19 +0000198 if (LastII)
199 *LastII = 0;
200
Douglas Gregor39a8de12009-02-25 19:37:18 +0000201 bool HasScopeSpecifier = false;
202
Chris Lattner5b454732009-01-05 03:55:46 +0000203 if (Tok.is(tok::coloncolon)) {
204 // ::new and ::delete aren't nested-name-specifiers.
205 tok::TokenKind NextKind = NextToken().getKind();
206 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
207 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattner55a7cef2009-01-05 00:13:00 +0000209 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000210 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
211 return true;
Richard Trieu919b9552012-11-02 01:08:58 +0000212
213 CheckForLParenAfterColonColon();
214
Douglas Gregor39a8de12009-02-25 19:37:18 +0000215 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000216 }
217
Douglas Gregord4dca082010-02-24 18:44:31 +0000218 bool CheckForDestructor = false;
219 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
220 CheckForDestructor = true;
221 *MayBePseudoDestructor = false;
222 }
223
David Blaikie42d6d0c2011-12-04 05:04:18 +0000224 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
225 DeclSpec DS(AttrFactory);
226 SourceLocation DeclLoc = Tok.getLocation();
227 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
228 if (Tok.isNot(tok::coloncolon)) {
229 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
230 return false;
231 }
232
233 SourceLocation CCLoc = ConsumeToken();
234 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
235 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
236
237 HasScopeSpecifier = true;
238 }
239
Douglas Gregor39a8de12009-02-25 19:37:18 +0000240 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000241 if (HasScopeSpecifier) {
242 // C++ [basic.lookup.classref]p5:
243 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000244 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000245 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000246 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000247 // the class-name-or-namespace-name is looked up in global scope as a
248 // class-name or namespace-name.
249 //
250 // To implement this, we clear out the object type as soon as we've
251 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000252 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000253
254 if (Tok.is(tok::code_completion)) {
255 // Code completion for a nested-name-specifier, where the code
256 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000257 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000258 // Include code completion token into the range of the scope otherwise
259 // when we try to annotate the scope tokens the dangling code completion
260 // token will cause assertion in
261 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000262 SS.setEndLoc(Tok.getLocation());
263 cutOffParsing();
264 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000265 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Douglas Gregor39a8de12009-02-25 19:37:18 +0000268 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000269 // nested-name-specifier 'template'[opt] simple-template-id '::'
270
271 // Parse the optional 'template' keyword, then make sure we have
272 // 'identifier <' after it.
273 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000274 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000275 // nested-name-specifier, since they aren't allowed to start with
276 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000277 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000278 break;
279
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000280 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000281 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000282
283 UnqualifiedId TemplateName;
284 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000285 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000286 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000287 ConsumeToken();
288 } else if (Tok.is(tok::kw_operator)) {
289 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000290 TemplateName)) {
291 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000292 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000293 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000294
Sean Hunte6252d12009-11-28 08:58:14 +0000295 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
296 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000297 Diag(TemplateName.getSourceRange().getBegin(),
298 diag::err_id_after_template_in_nested_name_spec)
299 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000300 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000301 break;
302 }
303 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000304 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000305 break;
306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000308 // If the next token is not '<', we have a qualified-id that refers
309 // to a template name, such as T::template apply, but is not a
310 // template-id.
311 if (Tok.isNot(tok::less)) {
312 TPA.Revert();
313 break;
314 }
315
316 // Commit to parsing the template-id.
317 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000318 TemplateTy Template;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000319 if (TemplateNameKind TNK
320 = Actions.ActOnDependentTemplateName(getCurScope(),
321 SS, TemplateKWLoc, TemplateName,
322 ObjectType, EnteringContext,
323 Template)) {
324 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
325 TemplateName, false))
Douglas Gregord6ab2322010-06-16 23:00:59 +0000326 return true;
327 } else
John McCall9ba61662010-02-26 08:45:28 +0000328 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner77cf72a2009-06-26 03:47:46 +0000330 continue;
331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Douglas Gregor39a8de12009-02-25 19:37:18 +0000333 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000334 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000335 //
336 // simple-template-id '::'
337 //
338 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000339 // right kind (it should name a type or be dependent), and then
340 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000341 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000342 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
343 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000344 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000345 }
346
Richard Smith2db075b2013-03-26 01:15:19 +0000347 if (LastII)
348 *LastII = TemplateId->Name;
349
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000350 // Consume the template-id token.
351 ConsumeToken();
352
353 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
354 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000355
David Blaikie6796fc12011-11-07 03:30:03 +0000356 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000357
Benjamin Kramer5354e772012-08-23 23:38:35 +0000358 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000359 TemplateId->NumArgs);
360
361 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000362 SS,
363 TemplateId->TemplateKWLoc,
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000364 TemplateId->Template,
365 TemplateId->TemplateNameLoc,
366 TemplateId->LAngleLoc,
367 TemplateArgsPtr,
368 TemplateId->RAngleLoc,
369 CCLoc,
370 EnteringContext)) {
371 SourceLocation StartLoc
372 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
373 : TemplateId->TemplateNameLoc;
374 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000375 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000376
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000377 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000378 }
379
Chris Lattner5c7f7862009-06-26 03:52:38 +0000380
381 // The rest of the nested-name-specifier possibilities start with
382 // tok::identifier.
383 if (Tok.isNot(tok::identifier))
384 break;
385
386 IdentifierInfo &II = *Tok.getIdentifierInfo();
387
388 // nested-name-specifier:
389 // type-name '::'
390 // namespace-name '::'
391 // nested-name-specifier identifier '::'
392 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000393
394 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
395 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000396 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000397 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
398 Tok.getLocation(),
399 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000400 EnteringContext) &&
401 // If the token after the colon isn't an identifier, it's still an
402 // error, but they probably meant something else strange so don't
403 // recover like this.
404 PP.LookAhead(1).is(tok::identifier)) {
405 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000406 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000407
408 // Recover as if the user wrote '::'.
409 Next.setKind(tok::coloncolon);
410 }
Chris Lattner46646492009-12-07 01:36:53 +0000411 }
412
Chris Lattner5c7f7862009-06-26 03:52:38 +0000413 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000414 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000415 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000416 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000417 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000418 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000419 }
420
Richard Smith2db075b2013-03-26 01:15:19 +0000421 if (LastII)
422 *LastII = &II;
423
Chris Lattner5c7f7862009-06-26 03:52:38 +0000424 // We have an identifier followed by a '::'. Lookup this name
425 // as the name in a nested-name-specifier.
426 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000427 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
428 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000429 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Richard Trieu919b9552012-11-02 01:08:58 +0000431 CheckForLParenAfterColonColon();
432
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000433 HasScopeSpecifier = true;
434 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
435 ObjectType, EnteringContext, SS))
436 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
437
Chris Lattner5c7f7862009-06-26 03:52:38 +0000438 continue;
439 }
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Richard Trieu950be712011-09-19 19:01:00 +0000441 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000442
Chris Lattner5c7f7862009-06-26 03:52:38 +0000443 // nested-name-specifier:
444 // type-name '<'
445 if (Next.is(tok::less)) {
446 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000447 UnqualifiedId TemplateName;
448 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000449 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000450 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000451 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000452 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000453 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000454 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000455 Template,
456 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000457 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000458 // with a template-id annotation. We do not permit the
459 // template-id to be translated into a type annotation,
460 // because some clients (e.g., the parsing of class template
461 // specializations) still want to see the original template-id
462 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000463 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000464 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
465 TemplateName, false))
John McCall9ba61662010-02-26 08:45:28 +0000466 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000467 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000468 }
469
470 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000471 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000472 // We have something like t::getAs<T>, where getAs is a
473 // member of an unknown specialization. However, this will only
474 // parse correctly as a template, so suggest the keyword 'template'
475 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000476 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikie4e4d0842012-03-11 07:00:24 +0000477 if (getLangOpts().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000478 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000479
480 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000481 << II.getName()
482 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
483
Douglas Gregord6ab2322010-06-16 23:00:59 +0000484 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000485 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000486 SS, SourceLocation(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000487 TemplateName, ObjectType,
488 EnteringContext, Template)) {
489 // Consume the identifier.
490 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000491 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
492 TemplateName, false))
493 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000494 }
495 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000496 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000497
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000498 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000499 }
500 }
501
Douglas Gregor39a8de12009-02-25 19:37:18 +0000502 // We don't have any tokens that form the beginning of a
503 // nested-name-specifier, so we're done.
504 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregord4dca082010-02-24 18:44:31 +0000507 // Even if we didn't see any pieces of a nested-name-specifier, we
508 // still check whether there is a tilde in this position, which
509 // indicates a potential pseudo-destructor.
510 if (CheckForDestructor && Tok.is(tok::tilde))
511 *MayBePseudoDestructor = true;
512
John McCall9ba61662010-02-26 08:45:28 +0000513 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000514}
515
516/// ParseCXXIdExpression - Handle id-expression.
517///
518/// id-expression:
519/// unqualified-id
520/// qualified-id
521///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000522/// qualified-id:
523/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
524/// '::' identifier
525/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000526/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000527///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000528/// NOTE: The standard specifies that, for qualified-id, the parser does not
529/// expect:
530///
531/// '::' conversion-function-id
532/// '::' '~' class-name
533///
534/// This may cause a slight inconsistency on diagnostics:
535///
536/// class C {};
537/// namespace A {}
538/// void f() {
539/// :: A :: ~ C(); // Some Sema error about using destructor with a
540/// // namespace.
541/// :: ~ C(); // Some Parser error like 'unexpected ~'.
542/// }
543///
544/// We simplify the parser a bit and make it work like:
545///
546/// qualified-id:
547/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
548/// '::' unqualified-id
549///
550/// That way Sema can handle and report similar errors for namespaces and the
551/// global scope.
552///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000553/// The isAddressOfOperand parameter indicates that this id-expression is a
554/// direct operand of the address-of operator. This is, besides member contexts,
555/// the only place where a qualified-id naming a non-static class member may
556/// appear.
557///
John McCall60d7b3a2010-08-24 06:29:42 +0000558ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000559 // qualified-id:
560 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
561 // '::' unqualified-id
562 //
563 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000564 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000565
566 SourceLocation TemplateKWLoc;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000567 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000568 if (ParseUnqualifiedId(SS,
569 /*EnteringContext=*/false,
570 /*AllowDestructorName=*/false,
571 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000572 /*ObjectType=*/ ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000573 TemplateKWLoc,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000574 Name))
575 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000576
577 // This is only the direct operand of an & operator if it is not
578 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000579 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
580 isAddressOfOperand = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000581
582 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
583 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000584}
585
Richard Smith0a664b82013-05-09 21:36:41 +0000586/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000587///
588/// lambda-expression:
589/// lambda-introducer lambda-declarator[opt] compound-statement
590///
591/// lambda-introducer:
592/// '[' lambda-capture[opt] ']'
593///
594/// lambda-capture:
595/// capture-default
596/// capture-list
597/// capture-default ',' capture-list
598///
599/// capture-default:
600/// '&'
601/// '='
602///
603/// capture-list:
604/// capture
605/// capture-list ',' capture
606///
607/// capture:
Richard Smith0a664b82013-05-09 21:36:41 +0000608/// simple-capture
609/// init-capture [C++1y]
610///
611/// simple-capture:
Douglas Gregorae7902c2011-08-04 15:30:47 +0000612/// identifier
613/// '&' identifier
614/// 'this'
615///
Richard Smith0a664b82013-05-09 21:36:41 +0000616/// init-capture: [C++1y]
617/// identifier initializer
618/// '&' identifier initializer
619///
Douglas Gregorae7902c2011-08-04 15:30:47 +0000620/// lambda-declarator:
621/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
622/// 'mutable'[opt] exception-specification[opt]
623/// trailing-return-type[opt]
624///
625ExprResult Parser::ParseLambdaExpression() {
626 // Parse lambda-introducer.
627 LambdaIntroducer Intro;
628
David Blaikiedc84cd52013-02-20 22:23:23 +0000629 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000630 if (DiagID) {
631 Diag(Tok, DiagID.getValue());
632 SkipUntil(tok::r_square);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000633 SkipUntil(tok::l_brace);
634 SkipUntil(tok::r_brace);
635 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000636 }
637
638 return ParseLambdaExpressionAfterIntroducer(Intro);
639}
640
641/// TryParseLambdaExpression - Use lookahead and potentially tentative
642/// parsing to determine if we are looking at a C++0x lambda expression, and parse
643/// it if we are.
644///
645/// If we are not looking at a lambda expression, returns ExprError().
646ExprResult Parser::TryParseLambdaExpression() {
Richard Smith80ad52f2013-01-02 11:42:31 +0000647 assert(getLangOpts().CPlusPlus11
Douglas Gregorae7902c2011-08-04 15:30:47 +0000648 && Tok.is(tok::l_square)
649 && "Not at the start of a possible lambda expression.");
650
651 const Token Next = NextToken(), After = GetLookAheadToken(2);
652
653 // If lookahead indicates this is a lambda...
654 if (Next.is(tok::r_square) || // []
655 Next.is(tok::equal) || // [=
656 (Next.is(tok::amp) && // [&] or [&,
657 (After.is(tok::r_square) ||
658 After.is(tok::comma))) ||
659 (Next.is(tok::identifier) && // [identifier]
660 After.is(tok::r_square))) {
661 return ParseLambdaExpression();
662 }
663
Eli Friedmandc3b7232012-01-04 02:40:39 +0000664 // If lookahead indicates an ObjC message send...
665 // [identifier identifier
Douglas Gregorae7902c2011-08-04 15:30:47 +0000666 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000667 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000668 }
669
Eli Friedmandc3b7232012-01-04 02:40:39 +0000670 // Here, we're stuck: lambda introducers and Objective-C message sends are
671 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
672 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
673 // writing two routines to parse a lambda introducer, just try to parse
674 // a lambda introducer first, and fall back if that fails.
675 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregorae7902c2011-08-04 15:30:47 +0000676 LambdaIntroducer Intro;
677 if (TryParseLambdaIntroducer(Intro))
Eli Friedmandc3b7232012-01-04 02:40:39 +0000678 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000679 return ParseLambdaExpressionAfterIntroducer(Intro);
680}
681
682/// ParseLambdaExpression - Parse a lambda introducer.
683///
684/// Returns a DiagnosticID if it hit something unexpected.
David Blaikiedc84cd52013-02-20 22:23:23 +0000685Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
686 typedef Optional<unsigned> DiagResult;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000687
688 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000689 BalancedDelimiterTracker T(*this, tok::l_square);
690 T.consumeOpen();
691
692 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000693
694 bool first = true;
695
696 // Parse capture-default.
697 if (Tok.is(tok::amp) &&
698 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
699 Intro.Default = LCD_ByRef;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000700 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000701 first = false;
702 } else if (Tok.is(tok::equal)) {
703 Intro.Default = LCD_ByCopy;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000704 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000705 first = false;
706 }
707
708 while (Tok.isNot(tok::r_square)) {
709 if (!first) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000710 if (Tok.isNot(tok::comma)) {
Douglas Gregor437fbc52012-07-31 00:50:07 +0000711 // Provide a completion for a lambda introducer here. Except
712 // in Objective-C, where this is Almost Surely meant to be a message
713 // send. In that case, fail here and let the ObjC message
714 // expression parser perform the completion.
Douglas Gregord48ab062012-07-31 15:27:48 +0000715 if (Tok.is(tok::code_completion) &&
716 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
717 !Intro.Captures.empty())) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000718 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
719 /*AfterAmpersand=*/false);
720 ConsumeCodeCompletionToken();
721 break;
722 }
723
Douglas Gregorae7902c2011-08-04 15:30:47 +0000724 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000725 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000726 ConsumeToken();
727 }
728
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000729 if (Tok.is(tok::code_completion)) {
730 // If we're in Objective-C++ and we have a bare '[', then this is more
731 // likely to be a message receiver.
David Blaikie4e4d0842012-03-11 07:00:24 +0000732 if (getLangOpts().ObjC1 && first)
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000733 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
734 else
735 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
736 /*AfterAmpersand=*/false);
737 ConsumeCodeCompletionToken();
738 break;
739 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000740
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000741 first = false;
742
Douglas Gregorae7902c2011-08-04 15:30:47 +0000743 // Parse capture.
744 LambdaCaptureKind Kind = LCK_ByCopy;
745 SourceLocation Loc;
746 IdentifierInfo* Id = 0;
Douglas Gregora7365242012-02-14 19:27:52 +0000747 SourceLocation EllipsisLoc;
Richard Smith0a664b82013-05-09 21:36:41 +0000748 ExprResult Init;
Douglas Gregora7365242012-02-14 19:27:52 +0000749
Douglas Gregorae7902c2011-08-04 15:30:47 +0000750 if (Tok.is(tok::kw_this)) {
751 Kind = LCK_This;
752 Loc = ConsumeToken();
753 } else {
754 if (Tok.is(tok::amp)) {
755 Kind = LCK_ByRef;
756 ConsumeToken();
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000757
758 if (Tok.is(tok::code_completion)) {
759 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
760 /*AfterAmpersand=*/true);
761 ConsumeCodeCompletionToken();
762 break;
763 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000764 }
765
766 if (Tok.is(tok::identifier)) {
767 Id = Tok.getIdentifierInfo();
768 Loc = ConsumeToken();
769 } else if (Tok.is(tok::kw_this)) {
770 // FIXME: If we want to suggest a fixit here, will need to return more
771 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
772 // Clear()ed to prevent emission in case of tentative parsing?
773 return DiagResult(diag::err_this_captured_by_reference);
774 } else {
775 return DiagResult(diag::err_expected_capture);
776 }
Richard Smith0a664b82013-05-09 21:36:41 +0000777
778 if (Tok.is(tok::l_paren)) {
779 BalancedDelimiterTracker Parens(*this, tok::l_paren);
780 Parens.consumeOpen();
781
782 ExprVector Exprs;
783 CommaLocsTy Commas;
784 if (ParseExpressionList(Exprs, Commas)) {
785 Parens.skipToEnd();
786 Init = ExprError();
787 } else {
788 Parens.consumeClose();
789 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
790 Parens.getCloseLocation(),
791 Exprs);
792 }
793 } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
794 if (Tok.is(tok::equal))
795 ConsumeToken();
796
797 Init = ParseInitializer();
Richard Smith0d8e9642013-05-16 06:20:58 +0000798 } else if (Tok.is(tok::ellipsis))
799 EllipsisLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000800 }
801
Richard Smith0a664b82013-05-09 21:36:41 +0000802 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000803 }
804
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000805 T.consumeClose();
806 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000807
808 return DiagResult();
809}
810
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000811/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000812///
813/// Returns true if it hit something unexpected.
814bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
815 TentativeParsingAction PA(*this);
816
David Blaikiedc84cd52013-02-20 22:23:23 +0000817 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000818
819 if (DiagID) {
820 PA.Revert();
821 return true;
822 }
823
824 PA.Commit();
825 return false;
826}
827
828/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
829/// expression.
830ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
831 LambdaIntroducer &Intro) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000832 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
833 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
834
835 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
836 "lambda expression parsing");
837
Richard Smith0a664b82013-05-09 21:36:41 +0000838 // FIXME: Call into Actions to add any init-capture declarations to the
839 // scope while parsing the lambda-declarator and compound-statement.
840
Douglas Gregorae7902c2011-08-04 15:30:47 +0000841 // Parse lambda-declarator[opt].
842 DeclSpec DS(AttrFactory);
Eli Friedmanf88c4002012-01-04 04:41:38 +0000843 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000844
845 if (Tok.is(tok::l_paren)) {
846 ParseScope PrototypeScope(this,
847 Scope::FunctionPrototypeScope |
Richard Smith3a2b7a12013-01-28 22:42:45 +0000848 Scope::FunctionDeclarationScope |
Douglas Gregorae7902c2011-08-04 15:30:47 +0000849 Scope::DeclScope);
850
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000851 SourceLocation DeclEndLoc;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000852 BalancedDelimiterTracker T(*this, tok::l_paren);
853 T.consumeOpen();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000854 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000855
856 // Parse parameter-declaration-clause.
857 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000858 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000859 SourceLocation EllipsisLoc;
860
861 if (Tok.isNot(tok::r_paren))
862 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
863
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000864 T.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000865 SourceLocation RParenLoc = T.getCloseLocation();
866 DeclEndLoc = RParenLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000867
868 // Parse 'mutable'[opt].
869 SourceLocation MutableLoc;
870 if (Tok.is(tok::kw_mutable)) {
871 MutableLoc = ConsumeToken();
872 DeclEndLoc = MutableLoc;
873 }
874
875 // Parse exception-specification[opt].
876 ExceptionSpecificationType ESpecType = EST_None;
877 SourceRange ESpecRange;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000878 SmallVector<ParsedType, 2> DynamicExceptions;
879 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000880 ExprResult NoexceptExpr;
Richard Smitha058fd42012-05-02 22:22:32 +0000881 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +0000882 DynamicExceptions,
883 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +0000884 NoexceptExpr);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000885
886 if (ESpecType != EST_None)
887 DeclEndLoc = ESpecRange.getEnd();
888
889 // Parse attribute-specifier[opt].
Richard Smith4e24f0f2013-01-02 12:01:23 +0000890 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000891
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000892 SourceLocation FunLocalRangeEnd = DeclEndLoc;
893
Douglas Gregorae7902c2011-08-04 15:30:47 +0000894 // Parse trailing-return-type[opt].
Richard Smith54655be2012-06-12 01:51:59 +0000895 TypeResult TrailingReturnType;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000896 if (Tok.is(tok::arrow)) {
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000897 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000898 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +0000899 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000900 if (Range.getEnd().isValid())
901 DeclEndLoc = Range.getEnd();
902 }
903
904 PrototypeScope.Exit();
905
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000906 SourceLocation NoLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000907 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000908 /*isAmbiguous=*/false,
909 LParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000910 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000911 EllipsisLoc, RParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000912 DS.getTypeQualifiers(),
913 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000914 /*RefQualifierLoc=*/NoLoc,
915 /*ConstQualifierLoc=*/NoLoc,
916 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000917 MutableLoc,
918 ESpecType, ESpecRange.getBegin(),
919 DynamicExceptions.data(),
920 DynamicExceptionRanges.data(),
921 DynamicExceptions.size(),
922 NoexceptExpr.isUsable() ?
923 NoexceptExpr.get() : 0,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000924 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000925 TrailingReturnType),
926 Attr, DeclEndLoc);
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000927 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
928 // It's common to forget that one needs '()' before 'mutable' or the
929 // result type. Deal with this.
930 Diag(Tok, diag::err_lambda_missing_parens)
931 << Tok.is(tok::arrow)
932 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
933 SourceLocation DeclLoc = Tok.getLocation();
934 SourceLocation DeclEndLoc = DeclLoc;
935
936 // Parse 'mutable', if it's there.
937 SourceLocation MutableLoc;
938 if (Tok.is(tok::kw_mutable)) {
939 MutableLoc = ConsumeToken();
940 DeclEndLoc = MutableLoc;
941 }
942
943 // Parse the return type, if there is one.
Richard Smith54655be2012-06-12 01:51:59 +0000944 TypeResult TrailingReturnType;
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000945 if (Tok.is(tok::arrow)) {
946 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +0000947 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000948 if (Range.getEnd().isValid())
949 DeclEndLoc = Range.getEnd();
950 }
951
952 ParsedAttributes Attr(AttrFactory);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000953 SourceLocation NoLoc;
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000954 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000955 /*isAmbiguous=*/false,
956 /*LParenLoc=*/NoLoc,
957 /*Params=*/0,
958 /*NumParams=*/0,
959 /*EllipsisLoc=*/NoLoc,
960 /*RParenLoc=*/NoLoc,
961 /*TypeQuals=*/0,
962 /*RefQualifierIsLValueRef=*/true,
963 /*RefQualifierLoc=*/NoLoc,
964 /*ConstQualifierLoc=*/NoLoc,
965 /*VolatileQualifierLoc=*/NoLoc,
966 MutableLoc,
967 EST_None,
968 /*ESpecLoc=*/NoLoc,
969 /*Exceptions=*/0,
970 /*ExceptionRanges=*/0,
971 /*NumExceptions=*/0,
972 /*NoexceptExpr=*/0,
973 DeclLoc, DeclEndLoc, D,
974 TrailingReturnType),
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000975 Attr, DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000976 }
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000977
Douglas Gregorae7902c2011-08-04 15:30:47 +0000978
Eli Friedman906a7e12012-01-06 03:05:34 +0000979 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
980 // it.
Douglas Gregorfccfb622012-02-21 22:51:27 +0000981 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorfccfb622012-02-21 22:51:27 +0000982 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman906a7e12012-01-06 03:05:34 +0000983
Eli Friedmanec9ea722012-01-05 03:35:19 +0000984 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
985
Douglas Gregorae7902c2011-08-04 15:30:47 +0000986 // Parse compound-statement.
Eli Friedmandc3b7232012-01-04 02:40:39 +0000987 if (!Tok.is(tok::l_brace)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +0000988 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000989 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
990 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000991 }
992
Eli Friedmandc3b7232012-01-04 02:40:39 +0000993 StmtResult Stmt(ParseCompoundStatementBody());
994 BodyScope.Exit();
995
Eli Friedmandeeab902012-01-04 02:46:53 +0000996 if (!Stmt.isInvalid())
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000997 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmandc3b7232012-01-04 02:40:39 +0000998
Eli Friedmandeeab902012-01-04 02:46:53 +0000999 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1000 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001001}
1002
Reid Spencer5f016e22007-07-11 17:01:13 +00001003/// ParseCXXCasts - This handles the various ways to cast expressions to another
1004/// type.
1005///
1006/// postfix-expression: [C++ 5.2p1]
1007/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1008/// 'static_cast' '<' type-name '>' '(' expression ')'
1009/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1010/// 'const_cast' '<' type-name '>' '(' expression ')'
1011///
John McCall60d7b3a2010-08-24 06:29:42 +00001012ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 tok::TokenKind Kind = Tok.getKind();
1014 const char *CastName = 0; // For error messages
1015
1016 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +00001017 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 case tok::kw_const_cast: CastName = "const_cast"; break;
1019 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1020 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1021 case tok::kw_static_cast: CastName = "static_cast"; break;
1022 }
1023
1024 SourceLocation OpLoc = ConsumeToken();
1025 SourceLocation LAngleBracketLoc = Tok.getLocation();
1026
Richard Smithea698b32011-04-14 21:45:45 +00001027 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1028 // diagnose error, suggest fix, and recover parsing.
Richard Smith78fe3e02012-08-20 17:37:52 +00001029 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1030 Token Next = NextToken();
1031 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1032 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1033 }
Richard Smithea698b32011-04-14 21:45:45 +00001034
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001036 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001037
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001038 // Parse the common declaration-specifiers piece.
1039 DeclSpec DS(AttrFactory);
1040 ParseSpecifierQualifierList(DS);
1041
1042 // Parse the abstract-declarator, if present.
1043 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1044 ParseDeclarator(DeclaratorInfo);
1045
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 SourceLocation RAngleBracketLoc = Tok.getLocation();
1047
Chris Lattner1ab3b962008-11-18 07:48:38 +00001048 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001049 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +00001050
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001051 SourceLocation LParenLoc, RParenLoc;
1052 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001053
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001054 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001055 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001056
John McCall60d7b3a2010-08-24 06:29:42 +00001057 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001059 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001060 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001061
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001062 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +00001063 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001064 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +00001065 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001066 T.getOpenLocation(), Result.take(),
1067 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001068
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001069 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00001070}
1071
Sebastian Redlc42e1182008-11-11 11:37:55 +00001072/// ParseCXXTypeid - This handles the C++ typeid expression.
1073///
1074/// postfix-expression: [C++ 5.2p1]
1075/// 'typeid' '(' expression ')'
1076/// 'typeid' '(' type-id ')'
1077///
John McCall60d7b3a2010-08-24 06:29:42 +00001078ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +00001079 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1080
1081 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001082 SourceLocation LParenLoc, RParenLoc;
1083 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001084
1085 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001086 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001087 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001088 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001089
John McCall60d7b3a2010-08-24 06:29:42 +00001090 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001091
Richard Smith05766812012-08-18 00:55:03 +00001092 // C++0x [expr.typeid]p3:
1093 // When typeid is applied to an expression other than an lvalue of a
1094 // polymorphic class type [...] The expression is an unevaluated
1095 // operand (Clause 5).
1096 //
1097 // Note that we can't tell whether the expression is an lvalue of a
1098 // polymorphic class type until after we've parsed the expression; we
1099 // speculatively assume the subexpression is unevaluated, and fix it up
1100 // later.
1101 //
1102 // We enter the unevaluated context before trying to determine whether we
1103 // have a type-id, because the tentative parse logic will try to resolve
1104 // names, and must treat them as unevaluated.
Eli Friedman80bfa3d2012-09-26 04:34:21 +00001105 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1106 Sema::ReuseLambdaContextDecl);
Richard Smith05766812012-08-18 00:55:03 +00001107
Sebastian Redlc42e1182008-11-11 11:37:55 +00001108 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001109 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001110
1111 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001112 T.consumeClose();
1113 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001114 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001115 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001116
1117 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +00001118 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001119 } else {
1120 Result = ParseExpression();
1121
1122 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001123 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +00001124 SkipUntil(tok::r_paren);
1125 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001126 T.consumeClose();
1127 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001128 if (RParenLoc.isInvalid())
1129 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001130
Sebastian Redlc42e1182008-11-11 11:37:55 +00001131 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001132 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001133 }
1134 }
1135
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001136 return Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001137}
1138
Francois Pichet01b7c302010-09-08 12:20:18 +00001139/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1140///
1141/// '__uuidof' '(' expression ')'
1142/// '__uuidof' '(' type-id ')'
1143///
1144ExprResult Parser::ParseCXXUuidof() {
1145 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1146
1147 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001148 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +00001149
1150 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001151 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +00001152 return ExprError();
1153
1154 ExprResult Result;
1155
1156 if (isTypeIdInParens()) {
1157 TypeResult Ty = ParseTypeName();
1158
1159 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001160 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001161
1162 if (Ty.isInvalid())
1163 return ExprError();
1164
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001165 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1166 Ty.get().getAsOpaquePtr(),
1167 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001168 } else {
1169 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1170 Result = ParseExpression();
1171
1172 // Match the ')'.
1173 if (Result.isInvalid())
1174 SkipUntil(tok::r_paren);
1175 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001176 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001177
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001178 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1179 /*isType=*/false,
1180 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001181 }
1182 }
1183
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001184 return Result;
Francois Pichet01b7c302010-09-08 12:20:18 +00001185}
1186
Douglas Gregord4dca082010-02-24 18:44:31 +00001187/// \brief Parse a C++ pseudo-destructor expression after the base,
1188/// . or -> operator, and nested-name-specifier have already been
1189/// parsed.
1190///
1191/// postfix-expression: [C++ 5.2]
1192/// postfix-expression . pseudo-destructor-name
1193/// postfix-expression -> pseudo-destructor-name
1194///
1195/// pseudo-destructor-name:
1196/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1197/// ::[opt] nested-name-specifier template simple-template-id ::
1198/// ~type-name
1199/// ::[opt] nested-name-specifier[opt] ~type-name
1200///
John McCall60d7b3a2010-08-24 06:29:42 +00001201ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +00001202Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1203 tok::TokenKind OpKind,
1204 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +00001205 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +00001206 // We're parsing either a pseudo-destructor-name or a dependent
1207 // member access that has the same form as a
1208 // pseudo-destructor-name. We parse both in the same way and let
1209 // the action model sort them out.
1210 //
1211 // Note that the ::[opt] nested-name-specifier[opt] has already
1212 // been parsed, and if there was a simple-template-id, it has
1213 // been coalesced into a template-id annotation token.
1214 UnqualifiedId FirstTypeName;
1215 SourceLocation CCLoc;
1216 if (Tok.is(tok::identifier)) {
1217 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1218 ConsumeToken();
1219 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1220 CCLoc = ConsumeToken();
1221 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001222 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1223 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregord4dca082010-02-24 18:44:31 +00001224 FirstTypeName.setTemplateId(
1225 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1226 ConsumeToken();
1227 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1228 CCLoc = ConsumeToken();
1229 } else {
1230 FirstTypeName.setIdentifier(0, SourceLocation());
1231 }
1232
1233 // Parse the tilde.
1234 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1235 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001236
1237 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1238 DeclSpec DS(AttrFactory);
Benjamin Kramer85c60db2011-12-18 12:18:02 +00001239 ParseDecltypeSpecifier(DS);
David Blaikie91ec7892011-12-16 16:03:09 +00001240 if (DS.getTypeSpecType() == TST_error)
1241 return ExprError();
1242 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1243 OpKind, TildeLoc, DS,
1244 Tok.is(tok::l_paren));
1245 }
1246
Douglas Gregord4dca082010-02-24 18:44:31 +00001247 if (!Tok.is(tok::identifier)) {
1248 Diag(Tok, diag::err_destructor_tilde_identifier);
1249 return ExprError();
1250 }
1251
1252 // Parse the second type.
1253 UnqualifiedId SecondTypeName;
1254 IdentifierInfo *Name = Tok.getIdentifierInfo();
1255 SourceLocation NameLoc = ConsumeToken();
1256 SecondTypeName.setIdentifier(Name, NameLoc);
1257
1258 // If there is a '<', the second type name is a template-id. Parse
1259 // it as such.
1260 if (Tok.is(tok::less) &&
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001261 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1262 Name, NameLoc,
1263 false, ObjectType, SecondTypeName,
1264 /*AssumeTemplateName=*/true))
Douglas Gregord4dca082010-02-24 18:44:31 +00001265 return ExprError();
1266
John McCall9ae2f072010-08-23 23:25:46 +00001267 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1268 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001269 SS, FirstTypeName, CCLoc,
1270 TildeLoc, SecondTypeName,
1271 Tok.is(tok::l_paren));
1272}
1273
Reid Spencer5f016e22007-07-11 17:01:13 +00001274/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1275///
1276/// boolean-literal: [C++ 2.13.5]
1277/// 'true'
1278/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001279ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001281 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001282}
Chris Lattner50dd2892008-02-26 00:51:44 +00001283
1284/// ParseThrowExpression - This handles the C++ throw expression.
1285///
1286/// throw-expression: [C++ 15]
1287/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001288ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001289 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001290 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001291
Chris Lattner2a2819a2008-04-06 06:02:23 +00001292 // If the current token isn't the start of an assignment-expression,
1293 // then the expression is not present. This handles things like:
1294 // "C ? throw : (void)42", which is crazy but legal.
1295 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1296 case tok::semi:
1297 case tok::r_paren:
1298 case tok::r_square:
1299 case tok::r_brace:
1300 case tok::colon:
1301 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001302 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001303
Chris Lattner2a2819a2008-04-06 06:02:23 +00001304 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001305 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001306 if (Expr.isInvalid()) return Expr;
Douglas Gregorbca01b42011-07-06 22:04:06 +00001307 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001308 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001309}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001310
1311/// ParseCXXThis - This handles the C++ 'this' pointer.
1312///
1313/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1314/// a non-lvalue expression whose value is the address of the object for which
1315/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001316ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001317 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1318 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001319 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001320}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001321
1322/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1323/// Can be interpreted either as function-style casting ("int(x)")
1324/// or class type construction ("ClassType(x,y,z)")
1325/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001326/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001327///
1328/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001329/// simple-type-specifier '(' expression-list[opt] ')'
1330/// [C++0x] simple-type-specifier braced-init-list
1331/// typename-specifier '(' expression-list[opt] ')'
1332/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001333///
John McCall60d7b3a2010-08-24 06:29:42 +00001334ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001335Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001336 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001337 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001338
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001339 assert((Tok.is(tok::l_paren) ||
Richard Smith80ad52f2013-01-02 11:42:31 +00001340 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001341 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001342
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001343 if (Tok.is(tok::l_brace)) {
Sebastian Redl6dc00f62012-02-12 18:41:05 +00001344 ExprResult Init = ParseBraceInitializer();
1345 if (Init.isInvalid())
1346 return Init;
1347 Expr *InitList = Init.take();
1348 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1349 MultiExprArg(&InitList, 1),
1350 SourceLocation());
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001351 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001352 BalancedDelimiterTracker T(*this, tok::l_paren);
1353 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001354
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001355 ExprVector Exprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001356 CommaLocsTy CommaLocs;
1357
1358 if (Tok.isNot(tok::r_paren)) {
1359 if (ParseExpressionList(Exprs, CommaLocs)) {
1360 SkipUntil(tok::r_paren);
1361 return ExprError();
1362 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001363 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001364
1365 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001366 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001367
1368 // TypeRep could be null, if it references an invalid typedef.
1369 if (!TypeRep)
1370 return ExprError();
1371
1372 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1373 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001374 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001375 Exprs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001376 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001377 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001378}
1379
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001380/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001381///
1382/// condition:
1383/// expression
1384/// type-specifier-seq declarator '=' assignment-expression
Richard Smith0635aa72012-02-22 06:49:09 +00001385/// [C++11] type-specifier-seq declarator '=' initializer-clause
1386/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001387/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1388/// '=' assignment-expression
1389///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001390/// \param ExprOut if the condition was parsed as an expression, the parsed
1391/// expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001392///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001393/// \param DeclOut if the condition was parsed as a declaration, the parsed
1394/// declaration.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001395///
Douglas Gregor586596f2010-05-06 17:25:47 +00001396/// \param Loc The location of the start of the statement that requires this
1397/// condition, e.g., the "for" in a for loop.
1398///
1399/// \param ConvertToBoolean Whether the condition expression should be
1400/// converted to a boolean value.
1401///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001402/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001403bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1404 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001405 SourceLocation Loc,
1406 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001407 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001408 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001409 cutOffParsing();
1410 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001411 }
1412
Sean Hunt2edf0a22012-06-23 05:07:58 +00001413 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001414 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001415
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001416 if (!isCXXConditionDeclaration()) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001417 ProhibitAttributes(attrs);
1418
Douglas Gregor586596f2010-05-06 17:25:47 +00001419 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001420 ExprOut = ParseExpression(); // expression
1421 DeclOut = 0;
1422 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001423 return true;
1424
1425 // If required, convert to a boolean value.
1426 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001427 ExprOut
1428 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1429 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001430 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001431
1432 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001433 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +00001434 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001435 ParseSpecifierQualifierList(DS);
1436
1437 // declarator
1438 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1439 ParseDeclarator(DeclaratorInfo);
1440
1441 // simple-asm-expr[opt]
1442 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001443 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001444 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001445 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001446 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001447 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001448 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001449 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001450 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001451 }
1452
1453 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001454 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001455
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001456 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001457 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001458 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001459 DeclOut = Dcl.get();
1460 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001461
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001462 // '=' assignment-expression
Richard Trieud6c7c672012-01-18 22:54:52 +00001463 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith0635aa72012-02-22 06:49:09 +00001464 bool CopyInitialization = isTokenEqualOrEqualTypo();
1465 if (CopyInitialization)
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001466 ConsumeToken();
Richard Smith0635aa72012-02-22 06:49:09 +00001467
1468 ExprResult InitExpr = ExprError();
Richard Smith80ad52f2013-01-02 11:42:31 +00001469 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith0635aa72012-02-22 06:49:09 +00001470 Diag(Tok.getLocation(),
1471 diag::warn_cxx98_compat_generalized_initializer_lists);
1472 InitExpr = ParseBraceInitializer();
1473 } else if (CopyInitialization) {
1474 InitExpr = ParseAssignmentExpression();
1475 } else if (Tok.is(tok::l_paren)) {
1476 // This was probably an attempt to initialize the variable.
1477 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1478 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1479 RParen = ConsumeParen();
1480 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1481 diag::err_expected_init_in_condition_lparen)
1482 << SourceRange(LParen, RParen);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001483 } else {
Richard Smith0635aa72012-02-22 06:49:09 +00001484 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1485 diag::err_expected_init_in_condition);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001486 }
Richard Smith0635aa72012-02-22 06:49:09 +00001487
1488 if (!InitExpr.isInvalid())
1489 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smitha2c36462013-04-26 16:15:35 +00001490 DS.containsPlaceholderType());
Richard Smithdc7a4f52013-04-30 13:56:41 +00001491 else
1492 Actions.ActOnInitializerError(DeclOut);
Richard Smith0635aa72012-02-22 06:49:09 +00001493
Douglas Gregor586596f2010-05-06 17:25:47 +00001494 // FIXME: Build a reference to this declaration? Convert it to bool?
1495 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001496
1497 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001498
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001499 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001500}
1501
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001502/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1503/// This should only be called when the current token is known to be part of
1504/// simple-type-specifier.
1505///
1506/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001507/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001508/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1509/// char
1510/// wchar_t
1511/// bool
1512/// short
1513/// int
1514/// long
1515/// signed
1516/// unsigned
1517/// float
1518/// double
1519/// void
1520/// [GNU] typeof-specifier
1521/// [C++0x] auto [TODO]
1522///
1523/// type-name:
1524/// class-name
1525/// enum-name
1526/// typedef-name
1527///
1528void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1529 DS.SetRangeStart(Tok.getLocation());
1530 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001531 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001532 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001534 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001535 case tok::identifier: // foo::bar
1536 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001537 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001538 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001539 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001540
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001541 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001542 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001543 if (getTypeAnnotation(Tok))
1544 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1545 getTypeAnnotation(Tok));
1546 else
1547 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001548
1549 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1550 ConsumeToken();
1551
1552 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1553 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1554 // Objective-C interface. If we don't have Objective-C or a '<', this is
1555 // just a normal reference to a typedef name.
David Blaikie4e4d0842012-03-11 07:00:24 +00001556 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001557 ParseObjCProtocolQualifiers(DS);
1558
1559 DS.Finish(Diags, PP);
1560 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001563 // builtin types
1564 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001565 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001566 break;
1567 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001568 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001569 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001570 case tok::kw___int64:
1571 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1572 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001573 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001574 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001575 break;
1576 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001577 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001578 break;
1579 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001580 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001581 break;
1582 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001583 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001584 break;
1585 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001586 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001587 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00001588 case tok::kw___int128:
1589 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1590 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001591 case tok::kw_half:
1592 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1593 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001594 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001595 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001596 break;
1597 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001598 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001599 break;
1600 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001601 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001602 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001603 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001604 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001605 break;
1606 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001607 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001608 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001609 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001610 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001611 break;
David Blaikie5e089fe2012-01-24 05:47:35 +00001612 case tok::annot_decltype:
1613 case tok::kw_decltype:
1614 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1615 return DS.Finish(Diags, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001617 // GNU typeof support.
1618 case tok::kw_typeof:
1619 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001620 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001621 return;
1622 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001623 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001624 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1625 else
1626 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001627 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001628 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001629}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001630
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001631/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1632/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1633/// e.g., "const short int". Note that the DeclSpec is *not* finished
1634/// by parsing the type-specifier-seq, because these sequences are
1635/// typically followed by some form of declarator. Returns true and
1636/// emits diagnostics if this is not a type-specifier-seq, false
1637/// otherwise.
1638///
1639/// type-specifier-seq: [C++ 8.1]
1640/// type-specifier type-specifier-seq[opt]
1641///
1642bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smith69730c12012-03-12 07:56:15 +00001643 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor396a9f22010-02-24 23:13:13 +00001644 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001645 return false;
1646}
1647
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001648/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1649/// some form.
1650///
1651/// This routine is invoked when a '<' is encountered after an identifier or
1652/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1653/// whether the unqualified-id is actually a template-id. This routine will
1654/// then parse the template arguments and form the appropriate template-id to
1655/// return to the caller.
1656///
1657/// \param SS the nested-name-specifier that precedes this template-id, if
1658/// we're actually parsing a qualified-id.
1659///
1660/// \param Name for constructor and destructor names, this is the actual
1661/// identifier that may be a template-name.
1662///
1663/// \param NameLoc the location of the class-name in a constructor or
1664/// destructor.
1665///
1666/// \param EnteringContext whether we're entering the scope of the
1667/// nested-name-specifier.
1668///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001669/// \param ObjectType if this unqualified-id occurs within a member access
1670/// expression, the type of the base object whose member is being accessed.
1671///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001672/// \param Id as input, describes the template-name or operator-function-id
1673/// that precedes the '<'. If template arguments were parsed successfully,
1674/// will be updated with the template-id.
1675///
Douglas Gregord4dca082010-02-24 18:44:31 +00001676/// \param AssumeTemplateId When true, this routine will assume that the name
1677/// refers to a template without performing name lookup to verify.
1678///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001679/// \returns true if a parse error occurred, false otherwise.
1680bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001681 SourceLocation TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001682 IdentifierInfo *Name,
1683 SourceLocation NameLoc,
1684 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001685 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001686 UnqualifiedId &Id,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001687 bool AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001688 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1689 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001690
1691 TemplateTy Template;
1692 TemplateNameKind TNK = TNK_Non_template;
1693 switch (Id.getKind()) {
1694 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001695 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001696 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001697 if (AssumeTemplateId) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001698 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001699 Id, ObjectType, EnteringContext,
1700 Template);
1701 if (TNK == TNK_Non_template)
1702 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001703 } else {
1704 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001705 TNK = Actions.isTemplateName(getCurScope(), SS,
1706 TemplateKWLoc.isValid(), Id,
1707 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001708 MemberOfUnknownSpecialization);
1709
1710 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1711 ObjectType && IsTemplateArgumentList()) {
1712 // We have something like t->getAs<T>(), where getAs is a
1713 // member of an unknown specialization. However, this will only
1714 // parse correctly as a template, so suggest the keyword 'template'
1715 // before 'getAs' and treat this as a dependent template name.
1716 std::string Name;
1717 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1718 Name = Id.Identifier->getName();
1719 else {
1720 Name = "operator ";
1721 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1722 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1723 else
1724 Name += Id.Identifier->getName();
1725 }
1726 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1727 << Name
1728 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001729 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1730 SS, TemplateKWLoc, Id,
1731 ObjectType, EnteringContext,
1732 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001733 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001734 return true;
1735 }
1736 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001737 break;
1738
Douglas Gregor014e88d2009-11-03 23:16:33 +00001739 case UnqualifiedId::IK_ConstructorName: {
1740 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001741 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001742 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001743 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1744 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001745 EnteringContext, Template,
1746 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001747 break;
1748 }
1749
Douglas Gregor014e88d2009-11-03 23:16:33 +00001750 case UnqualifiedId::IK_DestructorName: {
1751 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001752 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001753 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001754 if (ObjectType) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001755 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1756 SS, TemplateKWLoc, TemplateName,
1757 ObjectType, EnteringContext,
1758 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001759 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001760 return true;
1761 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001762 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1763 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001764 EnteringContext, Template,
1765 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001766
John McCallb3d87482010-08-24 05:47:05 +00001767 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001768 Diag(NameLoc, diag::err_destructor_template_id)
1769 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001770 return true;
1771 }
1772 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001773 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001774 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001775
1776 default:
1777 return false;
1778 }
1779
1780 if (TNK == TNK_Non_template)
1781 return false;
1782
1783 // Parse the enclosed template argument list.
1784 SourceLocation LAngleLoc, RAngleLoc;
1785 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001786 if (Tok.is(tok::less) &&
1787 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001788 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001789 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001790 RAngleLoc))
1791 return true;
1792
1793 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001794 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1795 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001796 // Form a parsed representation of the template-id to be stored in the
1797 // UnqualifiedId.
1798 TemplateIdAnnotation *TemplateId
Benjamin Kramer13bb7012012-04-14 12:14:03 +00001799 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001800
1801 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1802 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001803 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001804 TemplateId->TemplateNameLoc = Id.StartLocation;
1805 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001806 TemplateId->Name = 0;
1807 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1808 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001809 }
1810
Douglas Gregor059101f2011-03-02 00:47:37 +00001811 TemplateId->SS = SS;
Benjamin Kramer2b28bf12012-02-19 23:37:39 +00001812 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall2b5289b2010-08-23 07:28:44 +00001813 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001814 TemplateId->Kind = TNK;
1815 TemplateId->LAngleLoc = LAngleLoc;
1816 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001817 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001818 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001819 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001820 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001821
1822 Id.setTemplateId(TemplateId);
1823 return false;
1824 }
1825
1826 // Bundle the template arguments together.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001827 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001828
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001829 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001830 TypeResult Type
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001831 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1832 Template, NameLoc,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001833 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1834 /*IsCtorOrDtorName=*/true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001835 if (Type.isInvalid())
1836 return true;
1837
1838 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1839 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1840 else
1841 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1842
1843 return false;
1844}
1845
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001846/// \brief Parse an operator-function-id or conversion-function-id as part
1847/// of a C++ unqualified-id.
1848///
1849/// This routine is responsible only for parsing the operator-function-id or
1850/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001851///
1852/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001853/// operator-function-id: [C++ 13.5]
1854/// 'operator' operator
1855///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001856/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001857/// new delete new[] delete[]
1858/// + - * / % ^ & | ~
1859/// ! = < > += -= *= /= %=
1860/// ^= &= |= << >> >>= <<= == !=
1861/// <= >= && || ++ -- , ->* ->
1862/// () []
1863///
1864/// conversion-function-id: [C++ 12.3.2]
1865/// operator conversion-type-id
1866///
1867/// conversion-type-id:
1868/// type-specifier-seq conversion-declarator[opt]
1869///
1870/// conversion-declarator:
1871/// ptr-operator conversion-declarator[opt]
1872/// \endcode
1873///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001874/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001875/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1876///
1877/// \param EnteringContext whether we are entering the scope of the
1878/// nested-name-specifier.
1879///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001880/// \param ObjectType if this unqualified-id occurs within a member access
1881/// expression, the type of the base object whose member is being accessed.
1882///
1883/// \param Result on a successful parse, contains the parsed unqualified-id.
1884///
1885/// \returns true if parsing fails, false otherwise.
1886bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001887 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001888 UnqualifiedId &Result) {
1889 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1890
1891 // Consume the 'operator' keyword.
1892 SourceLocation KeywordLoc = ConsumeToken();
1893
1894 // Determine what kind of operator name we have.
1895 unsigned SymbolIdx = 0;
1896 SourceLocation SymbolLocations[3];
1897 OverloadedOperatorKind Op = OO_None;
1898 switch (Tok.getKind()) {
1899 case tok::kw_new:
1900 case tok::kw_delete: {
1901 bool isNew = Tok.getKind() == tok::kw_new;
1902 // Consume the 'new' or 'delete'.
1903 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith6ee326a2012-04-10 01:32:12 +00001904 // Check for array new/delete.
1905 if (Tok.is(tok::l_square) &&
Richard Smith80ad52f2013-01-02 11:42:31 +00001906 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001907 // Consume the '[' and ']'.
1908 BalancedDelimiterTracker T(*this, tok::l_square);
1909 T.consumeOpen();
1910 T.consumeClose();
1911 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001912 return true;
1913
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001914 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1915 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001916 Op = isNew? OO_Array_New : OO_Array_Delete;
1917 } else {
1918 Op = isNew? OO_New : OO_Delete;
1919 }
1920 break;
1921 }
1922
1923#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1924 case tok::Token: \
1925 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1926 Op = OO_##Name; \
1927 break;
1928#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1929#include "clang/Basic/OperatorKinds.def"
1930
1931 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001932 // Consume the '(' and ')'.
1933 BalancedDelimiterTracker T(*this, tok::l_paren);
1934 T.consumeOpen();
1935 T.consumeClose();
1936 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001937 return true;
1938
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001939 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1940 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001941 Op = OO_Call;
1942 break;
1943 }
1944
1945 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001946 // Consume the '[' and ']'.
1947 BalancedDelimiterTracker T(*this, tok::l_square);
1948 T.consumeOpen();
1949 T.consumeClose();
1950 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001951 return true;
1952
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001953 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1954 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001955 Op = OO_Subscript;
1956 break;
1957 }
1958
1959 case tok::code_completion: {
1960 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001961 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001962 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001963 // Don't try to parse any further.
1964 return true;
1965 }
1966
1967 default:
1968 break;
1969 }
1970
1971 if (Op != OO_None) {
1972 // We have parsed an operator-function-id.
1973 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1974 return false;
1975 }
Sean Hunt0486d742009-11-28 04:44:28 +00001976
1977 // Parse a literal-operator-id.
1978 //
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001979 // literal-operator-id: C++11 [over.literal]
1980 // operator string-literal identifier
1981 // operator user-defined-string-literal
Sean Hunt0486d742009-11-28 04:44:28 +00001982
Richard Smith80ad52f2013-01-02 11:42:31 +00001983 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith7fe62082011-10-15 05:09:34 +00001984 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001985
Richard Smith33762772012-03-08 23:06:02 +00001986 SourceLocation DiagLoc;
1987 unsigned DiagId = 0;
1988
1989 // We're past translation phase 6, so perform string literal concatenation
1990 // before checking for "".
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001991 SmallVector<Token, 4> Toks;
1992 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith33762772012-03-08 23:06:02 +00001993 while (isTokenStringLiteral()) {
1994 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001995 // C++11 [over.literal]p1:
1996 // The string-literal or user-defined-string-literal in a
1997 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith33762772012-03-08 23:06:02 +00001998 DiagLoc = Tok.getLocation();
1999 DiagId = diag::err_literal_operator_string_prefix;
2000 }
2001 Toks.push_back(Tok);
2002 TokLocs.push_back(ConsumeStringToken());
2003 }
2004
2005 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
2006 if (Literal.hadError)
2007 return true;
2008
2009 // Grab the literal operator's suffix, which will be either the next token
2010 // or a ud-suffix from the string literal.
2011 IdentifierInfo *II = 0;
2012 SourceLocation SuffixLoc;
2013 if (!Literal.getUDSuffix().empty()) {
2014 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2015 SuffixLoc =
2016 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2017 Literal.getUDSuffixOffset(),
David Blaikie4e4d0842012-03-11 07:00:24 +00002018 PP.getSourceManager(), getLangOpts());
Richard Smith33762772012-03-08 23:06:02 +00002019 } else if (Tok.is(tok::identifier)) {
2020 II = Tok.getIdentifierInfo();
2021 SuffixLoc = ConsumeToken();
2022 TokLocs.push_back(SuffixLoc);
2023 } else {
Sean Hunt0486d742009-11-28 04:44:28 +00002024 Diag(Tok.getLocation(), diag::err_expected_ident);
2025 return true;
2026 }
2027
Richard Smith33762772012-03-08 23:06:02 +00002028 // The string literal must be empty.
2029 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002030 // C++11 [over.literal]p1:
2031 // The string-literal or user-defined-string-literal in a
2032 // literal-operator-id shall [...] contain no characters
2033 // other than the implicit terminating '\0'.
Richard Smith33762772012-03-08 23:06:02 +00002034 DiagLoc = TokLocs.front();
2035 DiagId = diag::err_literal_operator_string_not_empty;
2036 }
2037
2038 if (DiagId) {
2039 // This isn't a valid literal-operator-id, but we think we know
2040 // what the user meant. Tell them what they should have written.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002041 SmallString<32> Str;
Richard Smith33762772012-03-08 23:06:02 +00002042 Str += "\"\" ";
2043 Str += II->getName();
2044 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2045 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2046 }
2047
2048 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Sean Hunt3e518bd2009-11-29 07:34:05 +00002049 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00002050 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002051
2052 // Parse a conversion-function-id.
2053 //
2054 // conversion-function-id: [C++ 12.3.2]
2055 // operator conversion-type-id
2056 //
2057 // conversion-type-id:
2058 // type-specifier-seq conversion-declarator[opt]
2059 //
2060 // conversion-declarator:
2061 // ptr-operator conversion-declarator[opt]
2062
2063 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00002064 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00002065 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002066 return true;
2067
2068 // Parse the conversion-declarator, which is merely a sequence of
2069 // ptr-operators.
Richard Smith14f78f42013-05-04 01:26:46 +00002070 Declarator D(DS, Declarator::ConversionIdContext);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002071 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2072
2073 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00002074 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002075 if (Ty.isInvalid())
2076 return true;
2077
2078 // Note that this is a conversion-function-id.
2079 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2080 D.getSourceRange().getEnd());
2081 return false;
2082}
2083
2084/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2085/// name of an entity.
2086///
2087/// \code
2088/// unqualified-id: [C++ expr.prim.general]
2089/// identifier
2090/// operator-function-id
2091/// conversion-function-id
2092/// [C++0x] literal-operator-id [TODO]
2093/// ~ class-name
2094/// template-id
2095///
2096/// \endcode
2097///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00002098/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002099/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2100///
2101/// \param EnteringContext whether we are entering the scope of the
2102/// nested-name-specifier.
2103///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002104/// \param AllowDestructorName whether we allow parsing of a destructor name.
2105///
2106/// \param AllowConstructorName whether we allow parsing a constructor name.
2107///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00002108/// \param ObjectType if this unqualified-id occurs within a member access
2109/// expression, the type of the base object whose member is being accessed.
2110///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002111/// \param Result on a successful parse, contains the parsed unqualified-id.
2112///
2113/// \returns true if parsing fails, false otherwise.
2114bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2115 bool AllowDestructorName,
2116 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002117 ParsedType ObjectType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002118 SourceLocation& TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002119 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00002120
2121 // Handle 'A::template B'. This is for template-ids which have not
2122 // already been annotated by ParseOptionalCXXScopeSpecifier().
2123 bool TemplateSpecified = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002124 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002125 (ObjectType || SS.isSet())) {
2126 TemplateSpecified = true;
2127 TemplateKWLoc = ConsumeToken();
2128 }
2129
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002130 // unqualified-id:
2131 // identifier
2132 // template-id (when it hasn't already been annotated)
2133 if (Tok.is(tok::identifier)) {
2134 // Consume the identifier.
2135 IdentifierInfo *Id = Tok.getIdentifierInfo();
2136 SourceLocation IdLoc = ConsumeToken();
2137
David Blaikie4e4d0842012-03-11 07:00:24 +00002138 if (!getLangOpts().CPlusPlus) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002139 // If we're not in C++, only identifiers matter. Record the
2140 // identifier and return.
2141 Result.setIdentifier(Id, IdLoc);
2142 return false;
2143 }
2144
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002145 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002146 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002147 // We have parsed a constructor name.
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002148 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2149 &SS, false, false,
2150 ParsedType(),
2151 /*IsCtorOrDtorName=*/true,
2152 /*NonTrivialTypeSourceInfo=*/true);
2153 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002154 } else {
2155 // We have parsed an identifier.
2156 Result.setIdentifier(Id, IdLoc);
2157 }
2158
2159 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00002160 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002161 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2162 EnteringContext, ObjectType,
2163 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002164
2165 return false;
2166 }
2167
2168 // unqualified-id:
2169 // template-id (already parsed and annotated)
2170 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002171 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002172
2173 // If the template-name names the current class, then this is a constructor
2174 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002175 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002176 if (SS.isSet()) {
2177 // C++ [class.qual]p2 specifies that a qualified template-name
2178 // is taken as the constructor name where a constructor can be
2179 // declared. Thus, the template arguments are extraneous, so
2180 // complain about them and remove them entirely.
2181 Diag(TemplateId->TemplateNameLoc,
2182 diag::err_out_of_line_constructor_template_id)
2183 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00002184 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002185 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002186 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2187 TemplateId->TemplateNameLoc,
2188 getCurScope(),
2189 &SS, false, false,
2190 ParsedType(),
2191 /*IsCtorOrDtorName=*/true,
2192 /*NontrivialTypeSourceInfo=*/true);
2193 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002194 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002195 ConsumeToken();
2196 return false;
2197 }
2198
2199 Result.setConstructorTemplateId(TemplateId);
2200 ConsumeToken();
2201 return false;
2202 }
2203
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002204 // We have already parsed a template-id; consume the annotation token as
2205 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002206 Result.setTemplateId(TemplateId);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002207 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002208 ConsumeToken();
2209 return false;
2210 }
2211
2212 // unqualified-id:
2213 // operator-function-id
2214 // conversion-function-id
2215 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002216 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002217 return true;
2218
Sean Hunte6252d12009-11-28 08:58:14 +00002219 // If we have an operator-function-id or a literal-operator-id and the next
2220 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002221 //
2222 // template-id:
2223 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00002224 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2225 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002226 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002227 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2228 0, SourceLocation(),
2229 EnteringContext, ObjectType,
2230 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002231
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002232 return false;
2233 }
2234
David Blaikie4e4d0842012-03-11 07:00:24 +00002235 if (getLangOpts().CPlusPlus &&
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002236 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002237 // C++ [expr.unary.op]p10:
2238 // There is an ambiguity in the unary-expression ~X(), where X is a
2239 // class-name. The ambiguity is resolved in favor of treating ~ as a
2240 // unary complement rather than treating ~X as referring to a destructor.
2241
2242 // Parse the '~'.
2243 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00002244
2245 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2246 DeclSpec DS(AttrFactory);
2247 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2248 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2249 Result.setDestructorName(TildeLoc, Type, EndLoc);
2250 return false;
2251 }
2252 return true;
2253 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002254
2255 // Parse the class-name.
2256 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00002257 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002258 return true;
2259 }
2260
2261 // Parse the class-name (or template-name in a simple-template-id).
2262 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2263 SourceLocation ClassNameLoc = ConsumeToken();
2264
Douglas Gregor0278e122010-05-05 05:58:24 +00002265 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002266 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002267 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2268 ClassName, ClassNameLoc,
2269 EnteringContext, ObjectType,
2270 Result, TemplateSpecified);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002271 }
2272
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002273 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002274 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2275 ClassNameLoc, getCurScope(),
2276 SS, ObjectType,
2277 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002278 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002279 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002280
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002281 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002282 return false;
2283 }
2284
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002285 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikie4e4d0842012-03-11 07:00:24 +00002286 << getLangOpts().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002287 return true;
2288}
2289
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002290/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2291/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002292///
Chris Lattner59232d32009-01-04 21:25:24 +00002293/// This method is called to parse the new expression after the optional :: has
2294/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2295/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002296///
2297/// new-expression:
2298/// '::'[opt] 'new' new-placement[opt] new-type-id
2299/// new-initializer[opt]
2300/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2301/// new-initializer[opt]
2302///
2303/// new-placement:
2304/// '(' expression-list ')'
2305///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002306/// new-type-id:
2307/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002308/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002309///
2310/// new-declarator:
2311/// ptr-operator new-declarator[opt]
2312/// direct-new-declarator
2313///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002314/// new-initializer:
2315/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002316/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002317///
John McCall60d7b3a2010-08-24 06:29:42 +00002318ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002319Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2320 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2321 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002322
2323 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2324 // second form of new-expression. It can't be a new-type-id.
2325
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002326 ExprVector PlacementArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002327 SourceLocation PlacementLParen, PlacementRParen;
2328
Douglas Gregor4bd40312010-07-13 15:54:32 +00002329 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002330 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002331 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002332 if (Tok.is(tok::l_paren)) {
2333 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002334 BalancedDelimiterTracker T(*this, tok::l_paren);
2335 T.consumeOpen();
2336 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002337 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2338 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002339 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002340 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002341
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002342 T.consumeClose();
2343 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002344 if (PlacementRParen.isInvalid()) {
2345 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002346 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002347 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002348
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002349 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002350 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002351 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002352 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002353 } else {
2354 // We still need the type.
2355 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002356 BalancedDelimiterTracker T(*this, tok::l_paren);
2357 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002358 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002359 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002360 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002361 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002362 T.consumeClose();
2363 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002364 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002365 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002366 if (ParseCXXTypeSpecifierSeq(DS))
2367 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002368 else {
2369 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002370 ParseDeclaratorInternal(DeclaratorInfo,
2371 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002372 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002373 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002374 }
2375 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002376 // A new-type-id is a simplified type-id, where essentially the
2377 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002378 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002379 if (ParseCXXTypeSpecifierSeq(DS))
2380 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002381 else {
2382 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002383 ParseDeclaratorInternal(DeclaratorInfo,
2384 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002385 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002386 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002387 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002388 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002389 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002390 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002391
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002392 ExprResult Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002393
2394 if (Tok.is(tok::l_paren)) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002395 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002396 ExprVector ConstructorArgs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002397 BalancedDelimiterTracker T(*this, tok::l_paren);
2398 T.consumeOpen();
2399 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002400 if (Tok.isNot(tok::r_paren)) {
2401 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002402 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2403 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002404 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002405 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002406 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002407 T.consumeClose();
2408 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002409 if (ConstructorRParen.isInvalid()) {
2410 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002411 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002412 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002413 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2414 ConstructorRParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002415 ConstructorArgs);
Richard Smith80ad52f2013-01-02 11:42:31 +00002416 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00002417 Diag(Tok.getLocation(),
2418 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002419 Initializer = ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002420 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002421 if (Initializer.isInvalid())
2422 return Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002423
Sebastian Redlf53597f2009-03-15 17:47:39 +00002424 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002425 PlacementArgs, PlacementRParen,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002426 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002427}
2428
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002429/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2430/// passed to ParseDeclaratorInternal.
2431///
2432/// direct-new-declarator:
2433/// '[' expression ']'
2434/// direct-new-declarator '[' constant-expression ']'
2435///
Chris Lattner59232d32009-01-04 21:25:24 +00002436void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002437 // Parse the array dimensions.
2438 bool first = true;
2439 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00002440 // An array-size expression can't start with a lambda.
2441 if (CheckProhibitedCXX11Attribute())
2442 continue;
2443
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002444 BalancedDelimiterTracker T(*this, tok::l_square);
2445 T.consumeOpen();
2446
John McCall60d7b3a2010-08-24 06:29:42 +00002447 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002448 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002449 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002450 // Recover
2451 SkipUntil(tok::r_square);
2452 return;
2453 }
2454 first = false;
2455
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002456 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002457
Bill Wendlingad017fa2012-12-20 19:22:21 +00002458 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith6ee326a2012-04-10 01:32:12 +00002459 ParsedAttributes Attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002460 MaybeParseCXX11Attributes(Attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00002461
John McCall0b7e6782011-03-24 11:26:52 +00002462 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002463 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002464 Size.release(),
2465 T.getOpenLocation(),
2466 T.getCloseLocation()),
Richard Smith6ee326a2012-04-10 01:32:12 +00002467 Attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002468
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002469 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002470 return;
2471 }
2472}
2473
2474/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2475/// This ambiguity appears in the syntax of the C++ new operator.
2476///
2477/// new-expression:
2478/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2479/// new-initializer[opt]
2480///
2481/// new-placement:
2482/// '(' expression-list ')'
2483///
John McCallca0408f2010-08-23 06:44:23 +00002484bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002485 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002486 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002487 // The '(' was already consumed.
2488 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002489 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002490 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002491 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002492 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002493 }
2494
2495 // It's not a type, it has to be an expression list.
2496 // Discard the comma locations - ActOnCXXNew has enough parameters.
2497 CommaLocsTy CommaLocs;
2498 return ParseExpressionList(PlacementArgs, CommaLocs);
2499}
2500
2501/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2502/// to free memory allocated by new.
2503///
Chris Lattner59232d32009-01-04 21:25:24 +00002504/// This method is called to parse the 'delete' expression after the optional
2505/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2506/// and "Start" is its location. Otherwise, "Start" is the location of the
2507/// 'delete' token.
2508///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002509/// delete-expression:
2510/// '::'[opt] 'delete' cast-expression
2511/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002512ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002513Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2514 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2515 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002516
2517 // Array delete?
2518 bool ArrayDelete = false;
Richard Smith6ee326a2012-04-10 01:32:12 +00002519 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith950435c2012-08-09 19:01:51 +00002520 // C++11 [expr.delete]p1:
2521 // Whenever the delete keyword is followed by empty square brackets, it
2522 // shall be interpreted as [array delete].
2523 // [Footnote: A lambda expression with a lambda-introducer that consists
2524 // of empty square brackets can follow the delete keyword if
2525 // the lambda expression is enclosed in parentheses.]
2526 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2527 // lambda-introducer.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002528 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002529 BalancedDelimiterTracker T(*this, tok::l_square);
2530
2531 T.consumeOpen();
2532 T.consumeClose();
2533 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002534 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002535 }
2536
John McCall60d7b3a2010-08-24 06:29:42 +00002537 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002538 if (Operand.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002539 return Operand;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002540
John McCall9ae2f072010-08-23 23:25:46 +00002541 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002542}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002543
Mike Stump1eb44332009-09-09 15:08:12 +00002544static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002545 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002546 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002547 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Joao Matos9ef98752013-03-27 01:34:16 +00002548 case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002549 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002550 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002551 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Joao Matos9ef98752013-03-27 01:34:16 +00002552 case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002553 case tok::kw___has_trivial_constructor:
2554 return UTT_HasTrivialDefaultConstructor;
Joao Matos9ef98752013-03-27 01:34:16 +00002555 case tok::kw___has_trivial_move_constructor:
2556 return UTT_HasTrivialMoveConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002557 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002558 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2559 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2560 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002561 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2562 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002563 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002564 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2565 case tok::kw___is_compound: return UTT_IsCompound;
2566 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002567 case tok::kw___is_empty: return UTT_IsEmpty;
2568 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002569 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002570 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2571 case tok::kw___is_function: return UTT_IsFunction;
2572 case tok::kw___is_fundamental: return UTT_IsFundamental;
2573 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallea30e2f2012-09-25 07:32:49 +00002574 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002575 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2576 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2577 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2578 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2579 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002580 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002581 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002582 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002583 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002584 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002585 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002586 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2587 case tok::kw___is_scalar: return UTT_IsScalar;
2588 case tok::kw___is_signed: return UTT_IsSigned;
2589 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2590 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002591 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002592 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002593 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2594 case tok::kw___is_void: return UTT_IsVoid;
2595 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002596 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002597}
2598
2599static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2600 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002601 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002602 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002603 case tok::kw___is_convertible: return BTT_IsConvertible;
2604 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002605 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002606 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002607 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002608 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002609}
2610
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002611static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2612 switch (kind) {
2613 default: llvm_unreachable("Not a known type trait");
2614 case tok::kw___is_trivially_constructible:
2615 return TT_IsTriviallyConstructible;
2616 }
2617}
2618
John Wiegley21ff2e52011-04-28 00:16:57 +00002619static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2620 switch(kind) {
2621 default: llvm_unreachable("Not a known binary type trait");
2622 case tok::kw___array_rank: return ATT_ArrayRank;
2623 case tok::kw___array_extent: return ATT_ArrayExtent;
2624 }
2625}
2626
John Wiegley55262202011-04-25 06:54:41 +00002627static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2628 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002629 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002630 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2631 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2632 }
2633}
2634
Sebastian Redl64b45f72009-01-05 20:52:13 +00002635/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2636/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2637/// templates.
2638///
2639/// primary-expression:
2640/// [GNU] unary-type-trait '(' type-id ')'
2641///
John McCall60d7b3a2010-08-24 06:29:42 +00002642ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002643 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2644 SourceLocation Loc = ConsumeToken();
2645
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002646 BalancedDelimiterTracker T(*this, tok::l_paren);
2647 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002648 return ExprError();
2649
2650 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2651 // there will be cryptic errors about mismatched parentheses and missing
2652 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002653 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002654
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002655 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002656
Douglas Gregor809070a2009-02-18 17:45:20 +00002657 if (Ty.isInvalid())
2658 return ExprError();
2659
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002660 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002661}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002662
Francois Pichet6ad6f282010-12-07 00:08:36 +00002663/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2664/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2665/// templates.
2666///
2667/// primary-expression:
2668/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2669///
2670ExprResult Parser::ParseBinaryTypeTrait() {
2671 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2672 SourceLocation Loc = ConsumeToken();
2673
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002674 BalancedDelimiterTracker T(*this, tok::l_paren);
2675 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002676 return ExprError();
2677
2678 TypeResult LhsTy = ParseTypeName();
2679 if (LhsTy.isInvalid()) {
2680 SkipUntil(tok::r_paren);
2681 return ExprError();
2682 }
2683
2684 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2685 SkipUntil(tok::r_paren);
2686 return ExprError();
2687 }
2688
2689 TypeResult RhsTy = ParseTypeName();
2690 if (RhsTy.isInvalid()) {
2691 SkipUntil(tok::r_paren);
2692 return ExprError();
2693 }
2694
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002695 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002696
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002697 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2698 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002699}
2700
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002701/// \brief Parse the built-in type-trait pseudo-functions that allow
2702/// implementation of the TR1/C++11 type traits templates.
2703///
2704/// primary-expression:
2705/// type-trait '(' type-id-seq ')'
2706///
2707/// type-id-seq:
2708/// type-id ...[opt] type-id-seq[opt]
2709///
2710ExprResult Parser::ParseTypeTrait() {
2711 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2712 SourceLocation Loc = ConsumeToken();
2713
2714 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2715 if (Parens.expectAndConsume(diag::err_expected_lparen))
2716 return ExprError();
2717
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002718 SmallVector<ParsedType, 2> Args;
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002719 do {
2720 // Parse the next type.
2721 TypeResult Ty = ParseTypeName();
2722 if (Ty.isInvalid()) {
2723 Parens.skipToEnd();
2724 return ExprError();
2725 }
2726
2727 // Parse the ellipsis, if present.
2728 if (Tok.is(tok::ellipsis)) {
2729 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2730 if (Ty.isInvalid()) {
2731 Parens.skipToEnd();
2732 return ExprError();
2733 }
2734 }
2735
2736 // Add this type to the list of arguments.
2737 Args.push_back(Ty.get());
2738
2739 if (Tok.is(tok::comma)) {
2740 ConsumeToken();
2741 continue;
2742 }
2743
2744 break;
2745 } while (true);
2746
2747 if (Parens.consumeClose())
2748 return ExprError();
2749
2750 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2751}
2752
John Wiegley21ff2e52011-04-28 00:16:57 +00002753/// ParseArrayTypeTrait - Parse the built-in array type-trait
2754/// pseudo-functions.
2755///
2756/// primary-expression:
2757/// [Embarcadero] '__array_rank' '(' type-id ')'
2758/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2759///
2760ExprResult Parser::ParseArrayTypeTrait() {
2761 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2762 SourceLocation Loc = ConsumeToken();
2763
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002764 BalancedDelimiterTracker T(*this, tok::l_paren);
2765 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002766 return ExprError();
2767
2768 TypeResult Ty = ParseTypeName();
2769 if (Ty.isInvalid()) {
2770 SkipUntil(tok::comma);
2771 SkipUntil(tok::r_paren);
2772 return ExprError();
2773 }
2774
2775 switch (ATT) {
2776 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002777 T.consumeClose();
2778 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2779 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002780 }
2781 case ATT_ArrayExtent: {
2782 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2783 SkipUntil(tok::r_paren);
2784 return ExprError();
2785 }
2786
2787 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002788 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002789
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002790 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2791 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002792 }
John Wiegley21ff2e52011-04-28 00:16:57 +00002793 }
David Blaikie30263482012-01-20 21:50:17 +00002794 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley21ff2e52011-04-28 00:16:57 +00002795}
2796
John Wiegley55262202011-04-25 06:54:41 +00002797/// ParseExpressionTrait - Parse built-in expression-trait
2798/// pseudo-functions like __is_lvalue_expr( xxx ).
2799///
2800/// primary-expression:
2801/// [Embarcadero] expression-trait '(' expression ')'
2802///
2803ExprResult Parser::ParseExpressionTrait() {
2804 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2805 SourceLocation Loc = ConsumeToken();
2806
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002807 BalancedDelimiterTracker T(*this, tok::l_paren);
2808 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002809 return ExprError();
2810
2811 ExprResult Expr = ParseExpression();
2812
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002813 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002814
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002815 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2816 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002817}
2818
2819
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002820/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2821/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2822/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002823ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002824Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002825 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002826 BalancedDelimiterTracker &Tracker) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002827 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002828 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2829 assert(isTypeIdInParens() && "Not a type-id!");
2830
John McCall60d7b3a2010-08-24 06:29:42 +00002831 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002832 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002833
2834 // We need to disambiguate a very ugly part of the C++ syntax:
2835 //
2836 // (T())x; - type-id
2837 // (T())*x; - type-id
2838 // (T())/x; - expression
2839 // (T()); - expression
2840 //
2841 // The bad news is that we cannot use the specialized tentative parser, since
2842 // it can only verify that the thing inside the parens can be parsed as
2843 // type-id, it is not useful for determining the context past the parens.
2844 //
2845 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002846 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002847 //
2848 // It uses a scheme similar to parsing inline methods. The parenthesized
2849 // tokens are cached, the context that follows is determined (possibly by
2850 // parsing a cast-expression), and then we re-introduce the cached tokens
2851 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002852
Mike Stump1eb44332009-09-09 15:08:12 +00002853 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002854 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002855
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002856 // Store the tokens of the parentheses. We will parse them after we determine
2857 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002858 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002859 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002860 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002861 return ExprError();
2862 }
2863
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002864 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002865 ParseAs = CompoundLiteral;
2866 } else {
2867 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002868 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2869 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2870 NotCastExpr = true;
2871 } else {
2872 // Try parsing the cast-expression that may follow.
2873 // If it is not a cast-expression, NotCastExpr will be true and no token
2874 // will be consumed.
2875 Result = ParseCastExpression(false/*isUnaryExpression*/,
2876 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002877 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002878 // type-id has priority.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002879 IsTypeCast);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002880 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002881
2882 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2883 // an expression.
2884 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002885 }
2886
Mike Stump1eb44332009-09-09 15:08:12 +00002887 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002888 Toks.push_back(Tok);
2889 // Re-enter the stored parenthesized tokens into the token stream, so we may
2890 // parse them now.
2891 PP.EnterTokenStream(Toks.data(), Toks.size(),
2892 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2893 // Drop the current token and bring the first cached one. It's the same token
2894 // as when we entered this function.
2895 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002896
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002897 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002898 // Parse the type declarator.
2899 DeclSpec DS(AttrFactory);
2900 ParseSpecifierQualifierList(DS);
2901 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2902 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002903
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002904 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002905 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002906
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002907 if (ParseAs == CompoundLiteral) {
2908 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002909 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002910 return ParseCompoundLiteralExpression(Ty.get(),
2911 Tracker.getOpenLocation(),
2912 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002913 }
Mike Stump1eb44332009-09-09 15:08:12 +00002914
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002915 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2916 assert(ParseAs == CastExpr);
2917
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002918 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002919 return ExprError();
2920
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002921 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002922 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002923 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2924 DeclaratorInfo, CastTy,
2925 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002926 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002927 }
Mike Stump1eb44332009-09-09 15:08:12 +00002928
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002929 // Not a compound literal, and not followed by a cast-expression.
2930 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002931
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002932 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002933 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002934 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002935 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2936 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002937
2938 // Match the ')'.
2939 if (Result.isInvalid()) {
2940 SkipUntil(tok::r_paren);
2941 return ExprError();
2942 }
Mike Stump1eb44332009-09-09 15:08:12 +00002943
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002944 Tracker.consumeClose();
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002945 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002946}