blob: f72e68e2a1ba5bbac683f2032770c046fb769433 [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.
171
Douglas Gregorb10cd042010-02-21 18:36:56 +0000172/// member access expression, e.g., the \p T:: in \p p->T::m.
173///
John McCall9ba61662010-02-26 08:45:28 +0000174/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000175bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000176 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000177 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000178 bool *MayBePseudoDestructor,
179 bool IsTypename) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000180 assert(getLangOpts().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000181 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000183 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +0000184 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
185 Tok.getAnnotationRange(),
186 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000187 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000188 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000189 }
Chris Lattnere607e802009-01-04 21:14:15 +0000190
Douglas Gregor39a8de12009-02-25 19:37:18 +0000191 bool HasScopeSpecifier = false;
192
Chris Lattner5b454732009-01-05 03:55:46 +0000193 if (Tok.is(tok::coloncolon)) {
194 // ::new and ::delete aren't nested-name-specifiers.
195 tok::TokenKind NextKind = NextToken().getKind();
196 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
197 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Chris Lattner55a7cef2009-01-05 00:13:00 +0000199 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000200 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
201 return true;
Richard Trieu919b9552012-11-02 01:08:58 +0000202
203 CheckForLParenAfterColonColon();
204
Douglas Gregor39a8de12009-02-25 19:37:18 +0000205 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000206 }
207
Douglas Gregord4dca082010-02-24 18:44:31 +0000208 bool CheckForDestructor = false;
209 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
210 CheckForDestructor = true;
211 *MayBePseudoDestructor = false;
212 }
213
David Blaikie42d6d0c2011-12-04 05:04:18 +0000214 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
215 DeclSpec DS(AttrFactory);
216 SourceLocation DeclLoc = Tok.getLocation();
217 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
218 if (Tok.isNot(tok::coloncolon)) {
219 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
220 return false;
221 }
222
223 SourceLocation CCLoc = ConsumeToken();
224 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
225 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
226
227 HasScopeSpecifier = true;
228 }
229
Douglas Gregor39a8de12009-02-25 19:37:18 +0000230 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000231 if (HasScopeSpecifier) {
232 // C++ [basic.lookup.classref]p5:
233 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000234 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000235 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000236 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000237 // the class-name-or-namespace-name is looked up in global scope as a
238 // class-name or namespace-name.
239 //
240 // To implement this, we clear out the object type as soon as we've
241 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000242 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000243
244 if (Tok.is(tok::code_completion)) {
245 // Code completion for a nested-name-specifier, where the code
246 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000247 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000248 // Include code completion token into the range of the scope otherwise
249 // when we try to annotate the scope tokens the dangling code completion
250 // token will cause assertion in
251 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000252 SS.setEndLoc(Tok.getLocation());
253 cutOffParsing();
254 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000255 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Douglas Gregor39a8de12009-02-25 19:37:18 +0000258 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000259 // nested-name-specifier 'template'[opt] simple-template-id '::'
260
261 // Parse the optional 'template' keyword, then make sure we have
262 // 'identifier <' after it.
263 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000264 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000265 // nested-name-specifier, since they aren't allowed to start with
266 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000267 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000268 break;
269
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000270 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000271 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000272
273 UnqualifiedId TemplateName;
274 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000275 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000276 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000277 ConsumeToken();
278 } else if (Tok.is(tok::kw_operator)) {
279 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000280 TemplateName)) {
281 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000282 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000283 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000284
Sean Hunte6252d12009-11-28 08:58:14 +0000285 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
286 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000287 Diag(TemplateName.getSourceRange().getBegin(),
288 diag::err_id_after_template_in_nested_name_spec)
289 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000290 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000291 break;
292 }
293 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000294 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000295 break;
296 }
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000298 // If the next token is not '<', we have a qualified-id that refers
299 // to a template name, such as T::template apply, but is not a
300 // template-id.
301 if (Tok.isNot(tok::less)) {
302 TPA.Revert();
303 break;
304 }
305
306 // Commit to parsing the template-id.
307 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000308 TemplateTy Template;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000309 if (TemplateNameKind TNK
310 = Actions.ActOnDependentTemplateName(getCurScope(),
311 SS, TemplateKWLoc, TemplateName,
312 ObjectType, EnteringContext,
313 Template)) {
314 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
315 TemplateName, false))
Douglas Gregord6ab2322010-06-16 23:00:59 +0000316 return true;
317 } else
John McCall9ba61662010-02-26 08:45:28 +0000318 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Chris Lattner77cf72a2009-06-26 03:47:46 +0000320 continue;
321 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Douglas Gregor39a8de12009-02-25 19:37:18 +0000323 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000324 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000325 //
326 // simple-template-id '::'
327 //
328 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000329 // right kind (it should name a type or be dependent), and then
330 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000331 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000332 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
333 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000334 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000335 }
336
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000337 // Consume the template-id token.
338 ConsumeToken();
339
340 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
341 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000342
David Blaikie6796fc12011-11-07 03:30:03 +0000343 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000344
Benjamin Kramer5354e772012-08-23 23:38:35 +0000345 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000346 TemplateId->NumArgs);
347
348 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000349 SS,
350 TemplateId->TemplateKWLoc,
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000351 TemplateId->Template,
352 TemplateId->TemplateNameLoc,
353 TemplateId->LAngleLoc,
354 TemplateArgsPtr,
355 TemplateId->RAngleLoc,
356 CCLoc,
357 EnteringContext)) {
358 SourceLocation StartLoc
359 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
360 : TemplateId->TemplateNameLoc;
361 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000362 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000363
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000364 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000365 }
366
Chris Lattner5c7f7862009-06-26 03:52:38 +0000367
368 // The rest of the nested-name-specifier possibilities start with
369 // tok::identifier.
370 if (Tok.isNot(tok::identifier))
371 break;
372
373 IdentifierInfo &II = *Tok.getIdentifierInfo();
374
375 // nested-name-specifier:
376 // type-name '::'
377 // namespace-name '::'
378 // nested-name-specifier identifier '::'
379 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000380
381 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
382 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000383 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000384 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
385 Tok.getLocation(),
386 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000387 EnteringContext) &&
388 // If the token after the colon isn't an identifier, it's still an
389 // error, but they probably meant something else strange so don't
390 // recover like this.
391 PP.LookAhead(1).is(tok::identifier)) {
392 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000393 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000394
395 // Recover as if the user wrote '::'.
396 Next.setKind(tok::coloncolon);
397 }
Chris Lattner46646492009-12-07 01:36:53 +0000398 }
399
Chris Lattner5c7f7862009-06-26 03:52:38 +0000400 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000401 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000402 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000403 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000404 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000405 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000406 }
407
Chris Lattner5c7f7862009-06-26 03:52:38 +0000408 // We have an identifier followed by a '::'. Lookup this name
409 // as the name in a nested-name-specifier.
410 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000411 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
412 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000413 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Richard Trieu919b9552012-11-02 01:08:58 +0000415 CheckForLParenAfterColonColon();
416
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000417 HasScopeSpecifier = true;
418 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
419 ObjectType, EnteringContext, SS))
420 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
421
Chris Lattner5c7f7862009-06-26 03:52:38 +0000422 continue;
423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Richard Trieu950be712011-09-19 19:01:00 +0000425 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000426
Chris Lattner5c7f7862009-06-26 03:52:38 +0000427 // nested-name-specifier:
428 // type-name '<'
429 if (Next.is(tok::less)) {
430 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000431 UnqualifiedId TemplateName;
432 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000433 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000434 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000435 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000436 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000437 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000438 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000439 Template,
440 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000441 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000442 // with a template-id annotation. We do not permit the
443 // template-id to be translated into a type annotation,
444 // because some clients (e.g., the parsing of class template
445 // specializations) still want to see the original template-id
446 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000447 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000448 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
449 TemplateName, false))
John McCall9ba61662010-02-26 08:45:28 +0000450 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000451 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000452 }
453
454 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000455 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000456 // We have something like t::getAs<T>, where getAs is a
457 // member of an unknown specialization. However, this will only
458 // parse correctly as a template, so suggest the keyword 'template'
459 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000460 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikie4e4d0842012-03-11 07:00:24 +0000461 if (getLangOpts().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000462 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000463
464 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000465 << II.getName()
466 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
467
Douglas Gregord6ab2322010-06-16 23:00:59 +0000468 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000469 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000470 SS, SourceLocation(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000471 TemplateName, ObjectType,
472 EnteringContext, Template)) {
473 // Consume the identifier.
474 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000475 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
476 TemplateName, false))
477 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000478 }
479 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000480 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000481
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000482 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000483 }
484 }
485
Douglas Gregor39a8de12009-02-25 19:37:18 +0000486 // We don't have any tokens that form the beginning of a
487 // nested-name-specifier, so we're done.
488 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Douglas Gregord4dca082010-02-24 18:44:31 +0000491 // Even if we didn't see any pieces of a nested-name-specifier, we
492 // still check whether there is a tilde in this position, which
493 // indicates a potential pseudo-destructor.
494 if (CheckForDestructor && Tok.is(tok::tilde))
495 *MayBePseudoDestructor = true;
496
John McCall9ba61662010-02-26 08:45:28 +0000497 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000498}
499
500/// ParseCXXIdExpression - Handle id-expression.
501///
502/// id-expression:
503/// unqualified-id
504/// qualified-id
505///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000506/// qualified-id:
507/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
508/// '::' identifier
509/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000510/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000511///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000512/// NOTE: The standard specifies that, for qualified-id, the parser does not
513/// expect:
514///
515/// '::' conversion-function-id
516/// '::' '~' class-name
517///
518/// This may cause a slight inconsistency on diagnostics:
519///
520/// class C {};
521/// namespace A {}
522/// void f() {
523/// :: A :: ~ C(); // Some Sema error about using destructor with a
524/// // namespace.
525/// :: ~ C(); // Some Parser error like 'unexpected ~'.
526/// }
527///
528/// We simplify the parser a bit and make it work like:
529///
530/// qualified-id:
531/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
532/// '::' unqualified-id
533///
534/// That way Sema can handle and report similar errors for namespaces and the
535/// global scope.
536///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000537/// The isAddressOfOperand parameter indicates that this id-expression is a
538/// direct operand of the address-of operator. This is, besides member contexts,
539/// the only place where a qualified-id naming a non-static class member may
540/// appear.
541///
John McCall60d7b3a2010-08-24 06:29:42 +0000542ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000543 // qualified-id:
544 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
545 // '::' unqualified-id
546 //
547 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000548 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000549
550 SourceLocation TemplateKWLoc;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000551 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000552 if (ParseUnqualifiedId(SS,
553 /*EnteringContext=*/false,
554 /*AllowDestructorName=*/false,
555 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000556 /*ObjectType=*/ ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000557 TemplateKWLoc,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000558 Name))
559 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000560
561 // This is only the direct operand of an & operator if it is not
562 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000563 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
564 isAddressOfOperand = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000565
566 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
567 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000568}
569
Douglas Gregorae7902c2011-08-04 15:30:47 +0000570/// ParseLambdaExpression - Parse a C++0x lambda expression.
571///
572/// lambda-expression:
573/// lambda-introducer lambda-declarator[opt] compound-statement
574///
575/// lambda-introducer:
576/// '[' lambda-capture[opt] ']'
577///
578/// lambda-capture:
579/// capture-default
580/// capture-list
581/// capture-default ',' capture-list
582///
583/// capture-default:
584/// '&'
585/// '='
586///
587/// capture-list:
588/// capture
589/// capture-list ',' capture
590///
591/// capture:
592/// identifier
593/// '&' identifier
594/// 'this'
595///
596/// lambda-declarator:
597/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
598/// 'mutable'[opt] exception-specification[opt]
599/// trailing-return-type[opt]
600///
601ExprResult Parser::ParseLambdaExpression() {
602 // Parse lambda-introducer.
603 LambdaIntroducer Intro;
604
David Blaikiedc84cd52013-02-20 22:23:23 +0000605 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000606 if (DiagID) {
607 Diag(Tok, DiagID.getValue());
608 SkipUntil(tok::r_square);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000609 SkipUntil(tok::l_brace);
610 SkipUntil(tok::r_brace);
611 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000612 }
613
614 return ParseLambdaExpressionAfterIntroducer(Intro);
615}
616
617/// TryParseLambdaExpression - Use lookahead and potentially tentative
618/// parsing to determine if we are looking at a C++0x lambda expression, and parse
619/// it if we are.
620///
621/// If we are not looking at a lambda expression, returns ExprError().
622ExprResult Parser::TryParseLambdaExpression() {
Richard Smith80ad52f2013-01-02 11:42:31 +0000623 assert(getLangOpts().CPlusPlus11
Douglas Gregorae7902c2011-08-04 15:30:47 +0000624 && Tok.is(tok::l_square)
625 && "Not at the start of a possible lambda expression.");
626
627 const Token Next = NextToken(), After = GetLookAheadToken(2);
628
629 // If lookahead indicates this is a lambda...
630 if (Next.is(tok::r_square) || // []
631 Next.is(tok::equal) || // [=
632 (Next.is(tok::amp) && // [&] or [&,
633 (After.is(tok::r_square) ||
634 After.is(tok::comma))) ||
635 (Next.is(tok::identifier) && // [identifier]
636 After.is(tok::r_square))) {
637 return ParseLambdaExpression();
638 }
639
Eli Friedmandc3b7232012-01-04 02:40:39 +0000640 // If lookahead indicates an ObjC message send...
641 // [identifier identifier
Douglas Gregorae7902c2011-08-04 15:30:47 +0000642 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000643 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000644 }
645
Eli Friedmandc3b7232012-01-04 02:40:39 +0000646 // Here, we're stuck: lambda introducers and Objective-C message sends are
647 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
648 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
649 // writing two routines to parse a lambda introducer, just try to parse
650 // a lambda introducer first, and fall back if that fails.
651 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregorae7902c2011-08-04 15:30:47 +0000652 LambdaIntroducer Intro;
653 if (TryParseLambdaIntroducer(Intro))
Eli Friedmandc3b7232012-01-04 02:40:39 +0000654 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000655 return ParseLambdaExpressionAfterIntroducer(Intro);
656}
657
658/// ParseLambdaExpression - Parse a lambda introducer.
659///
660/// Returns a DiagnosticID if it hit something unexpected.
David Blaikiedc84cd52013-02-20 22:23:23 +0000661Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
662 typedef Optional<unsigned> DiagResult;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000663
664 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000665 BalancedDelimiterTracker T(*this, tok::l_square);
666 T.consumeOpen();
667
668 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000669
670 bool first = true;
671
672 // Parse capture-default.
673 if (Tok.is(tok::amp) &&
674 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
675 Intro.Default = LCD_ByRef;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000676 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000677 first = false;
678 } else if (Tok.is(tok::equal)) {
679 Intro.Default = LCD_ByCopy;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000680 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000681 first = false;
682 }
683
684 while (Tok.isNot(tok::r_square)) {
685 if (!first) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000686 if (Tok.isNot(tok::comma)) {
Douglas Gregor437fbc52012-07-31 00:50:07 +0000687 // Provide a completion for a lambda introducer here. Except
688 // in Objective-C, where this is Almost Surely meant to be a message
689 // send. In that case, fail here and let the ObjC message
690 // expression parser perform the completion.
Douglas Gregord48ab062012-07-31 15:27:48 +0000691 if (Tok.is(tok::code_completion) &&
692 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
693 !Intro.Captures.empty())) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000694 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
695 /*AfterAmpersand=*/false);
696 ConsumeCodeCompletionToken();
697 break;
698 }
699
Douglas Gregorae7902c2011-08-04 15:30:47 +0000700 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000701 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000702 ConsumeToken();
703 }
704
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000705 if (Tok.is(tok::code_completion)) {
706 // If we're in Objective-C++ and we have a bare '[', then this is more
707 // likely to be a message receiver.
David Blaikie4e4d0842012-03-11 07:00:24 +0000708 if (getLangOpts().ObjC1 && first)
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000709 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
710 else
711 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
712 /*AfterAmpersand=*/false);
713 ConsumeCodeCompletionToken();
714 break;
715 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000716
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000717 first = false;
718
Douglas Gregorae7902c2011-08-04 15:30:47 +0000719 // Parse capture.
720 LambdaCaptureKind Kind = LCK_ByCopy;
721 SourceLocation Loc;
722 IdentifierInfo* Id = 0;
Douglas Gregora7365242012-02-14 19:27:52 +0000723 SourceLocation EllipsisLoc;
724
Douglas Gregorae7902c2011-08-04 15:30:47 +0000725 if (Tok.is(tok::kw_this)) {
726 Kind = LCK_This;
727 Loc = ConsumeToken();
728 } else {
729 if (Tok.is(tok::amp)) {
730 Kind = LCK_ByRef;
731 ConsumeToken();
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000732
733 if (Tok.is(tok::code_completion)) {
734 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
735 /*AfterAmpersand=*/true);
736 ConsumeCodeCompletionToken();
737 break;
738 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000739 }
740
741 if (Tok.is(tok::identifier)) {
742 Id = Tok.getIdentifierInfo();
743 Loc = ConsumeToken();
Douglas Gregora7365242012-02-14 19:27:52 +0000744
745 if (Tok.is(tok::ellipsis))
746 EllipsisLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000747 } else if (Tok.is(tok::kw_this)) {
748 // FIXME: If we want to suggest a fixit here, will need to return more
749 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
750 // Clear()ed to prevent emission in case of tentative parsing?
751 return DiagResult(diag::err_this_captured_by_reference);
752 } else {
753 return DiagResult(diag::err_expected_capture);
754 }
755 }
756
Douglas Gregora7365242012-02-14 19:27:52 +0000757 Intro.addCapture(Kind, Loc, Id, EllipsisLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000758 }
759
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000760 T.consumeClose();
761 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000762
763 return DiagResult();
764}
765
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000766/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000767///
768/// Returns true if it hit something unexpected.
769bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
770 TentativeParsingAction PA(*this);
771
David Blaikiedc84cd52013-02-20 22:23:23 +0000772 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000773
774 if (DiagID) {
775 PA.Revert();
776 return true;
777 }
778
779 PA.Commit();
780 return false;
781}
782
783/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
784/// expression.
785ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
786 LambdaIntroducer &Intro) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000787 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
788 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
789
790 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
791 "lambda expression parsing");
792
Douglas Gregorae7902c2011-08-04 15:30:47 +0000793 // Parse lambda-declarator[opt].
794 DeclSpec DS(AttrFactory);
Eli Friedmanf88c4002012-01-04 04:41:38 +0000795 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000796
797 if (Tok.is(tok::l_paren)) {
798 ParseScope PrototypeScope(this,
799 Scope::FunctionPrototypeScope |
Richard Smith3a2b7a12013-01-28 22:42:45 +0000800 Scope::FunctionDeclarationScope |
Douglas Gregorae7902c2011-08-04 15:30:47 +0000801 Scope::DeclScope);
802
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000803 SourceLocation DeclEndLoc;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000804 BalancedDelimiterTracker T(*this, tok::l_paren);
805 T.consumeOpen();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000806 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000807
808 // Parse parameter-declaration-clause.
809 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000810 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000811 SourceLocation EllipsisLoc;
812
813 if (Tok.isNot(tok::r_paren))
814 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
815
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000816 T.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000817 SourceLocation RParenLoc = T.getCloseLocation();
818 DeclEndLoc = RParenLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000819
820 // Parse 'mutable'[opt].
821 SourceLocation MutableLoc;
822 if (Tok.is(tok::kw_mutable)) {
823 MutableLoc = ConsumeToken();
824 DeclEndLoc = MutableLoc;
825 }
826
827 // Parse exception-specification[opt].
828 ExceptionSpecificationType ESpecType = EST_None;
829 SourceRange ESpecRange;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000830 SmallVector<ParsedType, 2> DynamicExceptions;
831 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000832 ExprResult NoexceptExpr;
Richard Smitha058fd42012-05-02 22:22:32 +0000833 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +0000834 DynamicExceptions,
835 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +0000836 NoexceptExpr);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000837
838 if (ESpecType != EST_None)
839 DeclEndLoc = ESpecRange.getEnd();
840
841 // Parse attribute-specifier[opt].
Richard Smith4e24f0f2013-01-02 12:01:23 +0000842 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000843
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000844 SourceLocation FunLocalRangeEnd = DeclEndLoc;
845
Douglas Gregorae7902c2011-08-04 15:30:47 +0000846 // Parse trailing-return-type[opt].
Richard Smith54655be2012-06-12 01:51:59 +0000847 TypeResult TrailingReturnType;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000848 if (Tok.is(tok::arrow)) {
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000849 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000850 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +0000851 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000852 if (Range.getEnd().isValid())
853 DeclEndLoc = Range.getEnd();
854 }
855
856 PrototypeScope.Exit();
857
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000858 SourceLocation NoLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000859 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000860 /*isAmbiguous=*/false,
861 LParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000862 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000863 EllipsisLoc, RParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000864 DS.getTypeQualifiers(),
865 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000866 /*RefQualifierLoc=*/NoLoc,
867 /*ConstQualifierLoc=*/NoLoc,
868 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000869 MutableLoc,
870 ESpecType, ESpecRange.getBegin(),
871 DynamicExceptions.data(),
872 DynamicExceptionRanges.data(),
873 DynamicExceptions.size(),
874 NoexceptExpr.isUsable() ?
875 NoexceptExpr.get() : 0,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000876 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000877 TrailingReturnType),
878 Attr, DeclEndLoc);
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000879 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
880 // It's common to forget that one needs '()' before 'mutable' or the
881 // result type. Deal with this.
882 Diag(Tok, diag::err_lambda_missing_parens)
883 << Tok.is(tok::arrow)
884 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
885 SourceLocation DeclLoc = Tok.getLocation();
886 SourceLocation DeclEndLoc = DeclLoc;
887
888 // Parse 'mutable', if it's there.
889 SourceLocation MutableLoc;
890 if (Tok.is(tok::kw_mutable)) {
891 MutableLoc = ConsumeToken();
892 DeclEndLoc = MutableLoc;
893 }
894
895 // Parse the return type, if there is one.
Richard Smith54655be2012-06-12 01:51:59 +0000896 TypeResult TrailingReturnType;
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000897 if (Tok.is(tok::arrow)) {
898 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +0000899 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000900 if (Range.getEnd().isValid())
901 DeclEndLoc = Range.getEnd();
902 }
903
904 ParsedAttributes Attr(AttrFactory);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000905 SourceLocation NoLoc;
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000906 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000907 /*isAmbiguous=*/false,
908 /*LParenLoc=*/NoLoc,
909 /*Params=*/0,
910 /*NumParams=*/0,
911 /*EllipsisLoc=*/NoLoc,
912 /*RParenLoc=*/NoLoc,
913 /*TypeQuals=*/0,
914 /*RefQualifierIsLValueRef=*/true,
915 /*RefQualifierLoc=*/NoLoc,
916 /*ConstQualifierLoc=*/NoLoc,
917 /*VolatileQualifierLoc=*/NoLoc,
918 MutableLoc,
919 EST_None,
920 /*ESpecLoc=*/NoLoc,
921 /*Exceptions=*/0,
922 /*ExceptionRanges=*/0,
923 /*NumExceptions=*/0,
924 /*NoexceptExpr=*/0,
925 DeclLoc, DeclEndLoc, D,
926 TrailingReturnType),
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000927 Attr, DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000928 }
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000929
Douglas Gregorae7902c2011-08-04 15:30:47 +0000930
Eli Friedman906a7e12012-01-06 03:05:34 +0000931 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
932 // it.
Douglas Gregorfccfb622012-02-21 22:51:27 +0000933 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorfccfb622012-02-21 22:51:27 +0000934 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman906a7e12012-01-06 03:05:34 +0000935
Eli Friedmanec9ea722012-01-05 03:35:19 +0000936 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
937
Douglas Gregorae7902c2011-08-04 15:30:47 +0000938 // Parse compound-statement.
Eli Friedmandc3b7232012-01-04 02:40:39 +0000939 if (!Tok.is(tok::l_brace)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +0000940 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000941 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
942 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000943 }
944
Eli Friedmandc3b7232012-01-04 02:40:39 +0000945 StmtResult Stmt(ParseCompoundStatementBody());
946 BodyScope.Exit();
947
Eli Friedmandeeab902012-01-04 02:46:53 +0000948 if (!Stmt.isInvalid())
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000949 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmandc3b7232012-01-04 02:40:39 +0000950
Eli Friedmandeeab902012-01-04 02:46:53 +0000951 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
952 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000953}
954
Reid Spencer5f016e22007-07-11 17:01:13 +0000955/// ParseCXXCasts - This handles the various ways to cast expressions to another
956/// type.
957///
958/// postfix-expression: [C++ 5.2p1]
959/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
960/// 'static_cast' '<' type-name '>' '(' expression ')'
961/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
962/// 'const_cast' '<' type-name '>' '(' expression ')'
963///
John McCall60d7b3a2010-08-24 06:29:42 +0000964ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 tok::TokenKind Kind = Tok.getKind();
966 const char *CastName = 0; // For error messages
967
968 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000969 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 case tok::kw_const_cast: CastName = "const_cast"; break;
971 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
972 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
973 case tok::kw_static_cast: CastName = "static_cast"; break;
974 }
975
976 SourceLocation OpLoc = ConsumeToken();
977 SourceLocation LAngleBracketLoc = Tok.getLocation();
978
Richard Smithea698b32011-04-14 21:45:45 +0000979 // Check for "<::" which is parsed as "[:". If found, fix token stream,
980 // diagnose error, suggest fix, and recover parsing.
Richard Smith78fe3e02012-08-20 17:37:52 +0000981 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
982 Token Next = NextToken();
983 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
984 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
985 }
Richard Smithea698b32011-04-14 21:45:45 +0000986
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000988 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000989
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000990 // Parse the common declaration-specifiers piece.
991 DeclSpec DS(AttrFactory);
992 ParseSpecifierQualifierList(DS);
993
994 // Parse the abstract-declarator, if present.
995 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
996 ParseDeclarator(DeclaratorInfo);
997
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 SourceLocation RAngleBracketLoc = Tok.getLocation();
999
Chris Lattner1ab3b962008-11-18 07:48:38 +00001000 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001001 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +00001002
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001003 SourceLocation LParenLoc, RParenLoc;
1004 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001005
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001006 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001007 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001008
John McCall60d7b3a2010-08-24 06:29:42 +00001009 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001011 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001012 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001013
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001014 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +00001015 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001016 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +00001017 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001018 T.getOpenLocation(), Result.take(),
1019 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001020
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001021 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00001022}
1023
Sebastian Redlc42e1182008-11-11 11:37:55 +00001024/// ParseCXXTypeid - This handles the C++ typeid expression.
1025///
1026/// postfix-expression: [C++ 5.2p1]
1027/// 'typeid' '(' expression ')'
1028/// 'typeid' '(' type-id ')'
1029///
John McCall60d7b3a2010-08-24 06:29:42 +00001030ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +00001031 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1032
1033 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001034 SourceLocation LParenLoc, RParenLoc;
1035 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001036
1037 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001038 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001039 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001040 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001041
John McCall60d7b3a2010-08-24 06:29:42 +00001042 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001043
Richard Smith05766812012-08-18 00:55:03 +00001044 // C++0x [expr.typeid]p3:
1045 // When typeid is applied to an expression other than an lvalue of a
1046 // polymorphic class type [...] The expression is an unevaluated
1047 // operand (Clause 5).
1048 //
1049 // Note that we can't tell whether the expression is an lvalue of a
1050 // polymorphic class type until after we've parsed the expression; we
1051 // speculatively assume the subexpression is unevaluated, and fix it up
1052 // later.
1053 //
1054 // We enter the unevaluated context before trying to determine whether we
1055 // have a type-id, because the tentative parse logic will try to resolve
1056 // names, and must treat them as unevaluated.
Eli Friedman80bfa3d2012-09-26 04:34:21 +00001057 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1058 Sema::ReuseLambdaContextDecl);
Richard Smith05766812012-08-18 00:55:03 +00001059
Sebastian Redlc42e1182008-11-11 11:37:55 +00001060 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001061 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001062
1063 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001064 T.consumeClose();
1065 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001066 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001067 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001068
1069 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +00001070 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001071 } else {
1072 Result = ParseExpression();
1073
1074 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001075 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +00001076 SkipUntil(tok::r_paren);
1077 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001078 T.consumeClose();
1079 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001080 if (RParenLoc.isInvalid())
1081 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001082
Sebastian Redlc42e1182008-11-11 11:37:55 +00001083 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001084 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001085 }
1086 }
1087
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001088 return Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001089}
1090
Francois Pichet01b7c302010-09-08 12:20:18 +00001091/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1092///
1093/// '__uuidof' '(' expression ')'
1094/// '__uuidof' '(' type-id ')'
1095///
1096ExprResult Parser::ParseCXXUuidof() {
1097 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1098
1099 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001100 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +00001101
1102 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001103 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +00001104 return ExprError();
1105
1106 ExprResult Result;
1107
1108 if (isTypeIdInParens()) {
1109 TypeResult Ty = ParseTypeName();
1110
1111 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001112 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001113
1114 if (Ty.isInvalid())
1115 return ExprError();
1116
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001117 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1118 Ty.get().getAsOpaquePtr(),
1119 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001120 } else {
1121 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1122 Result = ParseExpression();
1123
1124 // Match the ')'.
1125 if (Result.isInvalid())
1126 SkipUntil(tok::r_paren);
1127 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001128 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001129
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001130 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1131 /*isType=*/false,
1132 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001133 }
1134 }
1135
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001136 return Result;
Francois Pichet01b7c302010-09-08 12:20:18 +00001137}
1138
Douglas Gregord4dca082010-02-24 18:44:31 +00001139/// \brief Parse a C++ pseudo-destructor expression after the base,
1140/// . or -> operator, and nested-name-specifier have already been
1141/// parsed.
1142///
1143/// postfix-expression: [C++ 5.2]
1144/// postfix-expression . pseudo-destructor-name
1145/// postfix-expression -> pseudo-destructor-name
1146///
1147/// pseudo-destructor-name:
1148/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1149/// ::[opt] nested-name-specifier template simple-template-id ::
1150/// ~type-name
1151/// ::[opt] nested-name-specifier[opt] ~type-name
1152///
John McCall60d7b3a2010-08-24 06:29:42 +00001153ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +00001154Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1155 tok::TokenKind OpKind,
1156 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +00001157 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +00001158 // We're parsing either a pseudo-destructor-name or a dependent
1159 // member access that has the same form as a
1160 // pseudo-destructor-name. We parse both in the same way and let
1161 // the action model sort them out.
1162 //
1163 // Note that the ::[opt] nested-name-specifier[opt] has already
1164 // been parsed, and if there was a simple-template-id, it has
1165 // been coalesced into a template-id annotation token.
1166 UnqualifiedId FirstTypeName;
1167 SourceLocation CCLoc;
1168 if (Tok.is(tok::identifier)) {
1169 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1170 ConsumeToken();
1171 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1172 CCLoc = ConsumeToken();
1173 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001174 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1175 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregord4dca082010-02-24 18:44:31 +00001176 FirstTypeName.setTemplateId(
1177 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1178 ConsumeToken();
1179 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1180 CCLoc = ConsumeToken();
1181 } else {
1182 FirstTypeName.setIdentifier(0, SourceLocation());
1183 }
1184
1185 // Parse the tilde.
1186 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1187 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001188
1189 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1190 DeclSpec DS(AttrFactory);
Benjamin Kramer85c60db2011-12-18 12:18:02 +00001191 ParseDecltypeSpecifier(DS);
David Blaikie91ec7892011-12-16 16:03:09 +00001192 if (DS.getTypeSpecType() == TST_error)
1193 return ExprError();
1194 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1195 OpKind, TildeLoc, DS,
1196 Tok.is(tok::l_paren));
1197 }
1198
Douglas Gregord4dca082010-02-24 18:44:31 +00001199 if (!Tok.is(tok::identifier)) {
1200 Diag(Tok, diag::err_destructor_tilde_identifier);
1201 return ExprError();
1202 }
1203
1204 // Parse the second type.
1205 UnqualifiedId SecondTypeName;
1206 IdentifierInfo *Name = Tok.getIdentifierInfo();
1207 SourceLocation NameLoc = ConsumeToken();
1208 SecondTypeName.setIdentifier(Name, NameLoc);
1209
1210 // If there is a '<', the second type name is a template-id. Parse
1211 // it as such.
1212 if (Tok.is(tok::less) &&
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001213 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1214 Name, NameLoc,
1215 false, ObjectType, SecondTypeName,
1216 /*AssumeTemplateName=*/true))
Douglas Gregord4dca082010-02-24 18:44:31 +00001217 return ExprError();
1218
John McCall9ae2f072010-08-23 23:25:46 +00001219 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1220 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001221 SS, FirstTypeName, CCLoc,
1222 TildeLoc, SecondTypeName,
1223 Tok.is(tok::l_paren));
1224}
1225
Reid Spencer5f016e22007-07-11 17:01:13 +00001226/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1227///
1228/// boolean-literal: [C++ 2.13.5]
1229/// 'true'
1230/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001231ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001233 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001234}
Chris Lattner50dd2892008-02-26 00:51:44 +00001235
1236/// ParseThrowExpression - This handles the C++ throw expression.
1237///
1238/// throw-expression: [C++ 15]
1239/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001240ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001241 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001242 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001243
Chris Lattner2a2819a2008-04-06 06:02:23 +00001244 // If the current token isn't the start of an assignment-expression,
1245 // then the expression is not present. This handles things like:
1246 // "C ? throw : (void)42", which is crazy but legal.
1247 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1248 case tok::semi:
1249 case tok::r_paren:
1250 case tok::r_square:
1251 case tok::r_brace:
1252 case tok::colon:
1253 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001254 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001255
Chris Lattner2a2819a2008-04-06 06:02:23 +00001256 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001257 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001258 if (Expr.isInvalid()) return Expr;
Douglas Gregorbca01b42011-07-06 22:04:06 +00001259 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001260 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001261}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001262
1263/// ParseCXXThis - This handles the C++ 'this' pointer.
1264///
1265/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1266/// a non-lvalue expression whose value is the address of the object for which
1267/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001268ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001269 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1270 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001271 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001272}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001273
1274/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1275/// Can be interpreted either as function-style casting ("int(x)")
1276/// or class type construction ("ClassType(x,y,z)")
1277/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001278/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001279///
1280/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001281/// simple-type-specifier '(' expression-list[opt] ')'
1282/// [C++0x] simple-type-specifier braced-init-list
1283/// typename-specifier '(' expression-list[opt] ')'
1284/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001285///
John McCall60d7b3a2010-08-24 06:29:42 +00001286ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001287Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001288 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001289 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001290
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001291 assert((Tok.is(tok::l_paren) ||
Richard Smith80ad52f2013-01-02 11:42:31 +00001292 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001293 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001294
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001295 if (Tok.is(tok::l_brace)) {
Sebastian Redl6dc00f62012-02-12 18:41:05 +00001296 ExprResult Init = ParseBraceInitializer();
1297 if (Init.isInvalid())
1298 return Init;
1299 Expr *InitList = Init.take();
1300 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1301 MultiExprArg(&InitList, 1),
1302 SourceLocation());
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001303 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001304 BalancedDelimiterTracker T(*this, tok::l_paren);
1305 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001306
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001307 ExprVector Exprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001308 CommaLocsTy CommaLocs;
1309
1310 if (Tok.isNot(tok::r_paren)) {
1311 if (ParseExpressionList(Exprs, CommaLocs)) {
1312 SkipUntil(tok::r_paren);
1313 return ExprError();
1314 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001315 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001316
1317 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001318 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001319
1320 // TypeRep could be null, if it references an invalid typedef.
1321 if (!TypeRep)
1322 return ExprError();
1323
1324 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1325 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001326 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001327 Exprs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001328 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001329 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001330}
1331
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001332/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001333///
1334/// condition:
1335/// expression
1336/// type-specifier-seq declarator '=' assignment-expression
Richard Smith0635aa72012-02-22 06:49:09 +00001337/// [C++11] type-specifier-seq declarator '=' initializer-clause
1338/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001339/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1340/// '=' assignment-expression
1341///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001342/// \param ExprOut if the condition was parsed as an expression, the parsed
1343/// expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001344///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001345/// \param DeclOut if the condition was parsed as a declaration, the parsed
1346/// declaration.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001347///
Douglas Gregor586596f2010-05-06 17:25:47 +00001348/// \param Loc The location of the start of the statement that requires this
1349/// condition, e.g., the "for" in a for loop.
1350///
1351/// \param ConvertToBoolean Whether the condition expression should be
1352/// converted to a boolean value.
1353///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001354/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001355bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1356 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001357 SourceLocation Loc,
1358 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001359 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001360 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001361 cutOffParsing();
1362 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001363 }
1364
Sean Hunt2edf0a22012-06-23 05:07:58 +00001365 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001366 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001367
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001368 if (!isCXXConditionDeclaration()) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001369 ProhibitAttributes(attrs);
1370
Douglas Gregor586596f2010-05-06 17:25:47 +00001371 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001372 ExprOut = ParseExpression(); // expression
1373 DeclOut = 0;
1374 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001375 return true;
1376
1377 // If required, convert to a boolean value.
1378 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001379 ExprOut
1380 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1381 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001382 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001383
1384 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001385 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +00001386 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001387 ParseSpecifierQualifierList(DS);
1388
1389 // declarator
1390 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1391 ParseDeclarator(DeclaratorInfo);
1392
1393 // simple-asm-expr[opt]
1394 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001395 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001396 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001397 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001398 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001399 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001400 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001401 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001402 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001403 }
1404
1405 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001406 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001407
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001408 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001409 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001410 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001411 DeclOut = Dcl.get();
1412 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001413
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001414 // '=' assignment-expression
Richard Trieud6c7c672012-01-18 22:54:52 +00001415 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith0635aa72012-02-22 06:49:09 +00001416 bool CopyInitialization = isTokenEqualOrEqualTypo();
1417 if (CopyInitialization)
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001418 ConsumeToken();
Richard Smith0635aa72012-02-22 06:49:09 +00001419
1420 ExprResult InitExpr = ExprError();
Richard Smith80ad52f2013-01-02 11:42:31 +00001421 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith0635aa72012-02-22 06:49:09 +00001422 Diag(Tok.getLocation(),
1423 diag::warn_cxx98_compat_generalized_initializer_lists);
1424 InitExpr = ParseBraceInitializer();
1425 } else if (CopyInitialization) {
1426 InitExpr = ParseAssignmentExpression();
1427 } else if (Tok.is(tok::l_paren)) {
1428 // This was probably an attempt to initialize the variable.
1429 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1430 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1431 RParen = ConsumeParen();
1432 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1433 diag::err_expected_init_in_condition_lparen)
1434 << SourceRange(LParen, RParen);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001435 } else {
Richard Smith0635aa72012-02-22 06:49:09 +00001436 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1437 diag::err_expected_init_in_condition);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001438 }
Richard Smith0635aa72012-02-22 06:49:09 +00001439
1440 if (!InitExpr.isInvalid())
1441 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
1442 DS.getTypeSpecType() == DeclSpec::TST_auto);
1443
Douglas Gregor586596f2010-05-06 17:25:47 +00001444 // FIXME: Build a reference to this declaration? Convert it to bool?
1445 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001446
1447 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001448
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001449 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001450}
1451
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001452/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1453/// This should only be called when the current token is known to be part of
1454/// simple-type-specifier.
1455///
1456/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001457/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001458/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1459/// char
1460/// wchar_t
1461/// bool
1462/// short
1463/// int
1464/// long
1465/// signed
1466/// unsigned
1467/// float
1468/// double
1469/// void
1470/// [GNU] typeof-specifier
1471/// [C++0x] auto [TODO]
1472///
1473/// type-name:
1474/// class-name
1475/// enum-name
1476/// typedef-name
1477///
1478void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1479 DS.SetRangeStart(Tok.getLocation());
1480 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001481 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001482 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001484 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001485 case tok::identifier: // foo::bar
1486 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001487 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001488 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001489 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001490
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001491 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001492 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001493 if (getTypeAnnotation(Tok))
1494 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1495 getTypeAnnotation(Tok));
1496 else
1497 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001498
1499 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1500 ConsumeToken();
1501
1502 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1503 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1504 // Objective-C interface. If we don't have Objective-C or a '<', this is
1505 // just a normal reference to a typedef name.
David Blaikie4e4d0842012-03-11 07:00:24 +00001506 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001507 ParseObjCProtocolQualifiers(DS);
1508
1509 DS.Finish(Diags, PP);
1510 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001511 }
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001513 // builtin types
1514 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001515 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001516 break;
1517 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001518 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001519 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001520 case tok::kw___int64:
1521 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1522 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001523 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001524 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001525 break;
1526 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001527 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001528 break;
1529 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001530 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001531 break;
1532 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001533 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001534 break;
1535 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001536 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001537 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00001538 case tok::kw___int128:
1539 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1540 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001541 case tok::kw_half:
1542 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1543 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001544 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001545 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001546 break;
1547 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001548 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001549 break;
1550 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001551 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001552 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001553 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001554 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001555 break;
1556 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001557 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001558 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001559 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001560 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001561 break;
David Blaikie5e089fe2012-01-24 05:47:35 +00001562 case tok::annot_decltype:
1563 case tok::kw_decltype:
1564 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1565 return DS.Finish(Diags, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001567 // GNU typeof support.
1568 case tok::kw_typeof:
1569 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001570 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001571 return;
1572 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001573 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001574 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1575 else
1576 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001577 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001578 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001579}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001580
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001581/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1582/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1583/// e.g., "const short int". Note that the DeclSpec is *not* finished
1584/// by parsing the type-specifier-seq, because these sequences are
1585/// typically followed by some form of declarator. Returns true and
1586/// emits diagnostics if this is not a type-specifier-seq, false
1587/// otherwise.
1588///
1589/// type-specifier-seq: [C++ 8.1]
1590/// type-specifier type-specifier-seq[opt]
1591///
1592bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smith69730c12012-03-12 07:56:15 +00001593 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor396a9f22010-02-24 23:13:13 +00001594 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001595 return false;
1596}
1597
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001598/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1599/// some form.
1600///
1601/// This routine is invoked when a '<' is encountered after an identifier or
1602/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1603/// whether the unqualified-id is actually a template-id. This routine will
1604/// then parse the template arguments and form the appropriate template-id to
1605/// return to the caller.
1606///
1607/// \param SS the nested-name-specifier that precedes this template-id, if
1608/// we're actually parsing a qualified-id.
1609///
1610/// \param Name for constructor and destructor names, this is the actual
1611/// identifier that may be a template-name.
1612///
1613/// \param NameLoc the location of the class-name in a constructor or
1614/// destructor.
1615///
1616/// \param EnteringContext whether we're entering the scope of the
1617/// nested-name-specifier.
1618///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001619/// \param ObjectType if this unqualified-id occurs within a member access
1620/// expression, the type of the base object whose member is being accessed.
1621///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001622/// \param Id as input, describes the template-name or operator-function-id
1623/// that precedes the '<'. If template arguments were parsed successfully,
1624/// will be updated with the template-id.
1625///
Douglas Gregord4dca082010-02-24 18:44:31 +00001626/// \param AssumeTemplateId When true, this routine will assume that the name
1627/// refers to a template without performing name lookup to verify.
1628///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001629/// \returns true if a parse error occurred, false otherwise.
1630bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001631 SourceLocation TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001632 IdentifierInfo *Name,
1633 SourceLocation NameLoc,
1634 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001635 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001636 UnqualifiedId &Id,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001637 bool AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001638 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1639 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001640
1641 TemplateTy Template;
1642 TemplateNameKind TNK = TNK_Non_template;
1643 switch (Id.getKind()) {
1644 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001645 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001646 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001647 if (AssumeTemplateId) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001648 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001649 Id, ObjectType, EnteringContext,
1650 Template);
1651 if (TNK == TNK_Non_template)
1652 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001653 } else {
1654 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001655 TNK = Actions.isTemplateName(getCurScope(), SS,
1656 TemplateKWLoc.isValid(), Id,
1657 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001658 MemberOfUnknownSpecialization);
1659
1660 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1661 ObjectType && IsTemplateArgumentList()) {
1662 // We have something like t->getAs<T>(), where getAs is a
1663 // member of an unknown specialization. However, this will only
1664 // parse correctly as a template, so suggest the keyword 'template'
1665 // before 'getAs' and treat this as a dependent template name.
1666 std::string Name;
1667 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1668 Name = Id.Identifier->getName();
1669 else {
1670 Name = "operator ";
1671 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1672 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1673 else
1674 Name += Id.Identifier->getName();
1675 }
1676 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1677 << Name
1678 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001679 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1680 SS, TemplateKWLoc, Id,
1681 ObjectType, EnteringContext,
1682 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001683 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001684 return true;
1685 }
1686 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001687 break;
1688
Douglas Gregor014e88d2009-11-03 23:16:33 +00001689 case UnqualifiedId::IK_ConstructorName: {
1690 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001691 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001692 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001693 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1694 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001695 EnteringContext, Template,
1696 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001697 break;
1698 }
1699
Douglas Gregor014e88d2009-11-03 23:16:33 +00001700 case UnqualifiedId::IK_DestructorName: {
1701 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001702 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001703 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001704 if (ObjectType) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001705 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1706 SS, TemplateKWLoc, TemplateName,
1707 ObjectType, EnteringContext,
1708 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001709 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001710 return true;
1711 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001712 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1713 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001714 EnteringContext, Template,
1715 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001716
John McCallb3d87482010-08-24 05:47:05 +00001717 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001718 Diag(NameLoc, diag::err_destructor_template_id)
1719 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001720 return true;
1721 }
1722 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001723 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001724 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001725
1726 default:
1727 return false;
1728 }
1729
1730 if (TNK == TNK_Non_template)
1731 return false;
1732
1733 // Parse the enclosed template argument list.
1734 SourceLocation LAngleLoc, RAngleLoc;
1735 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001736 if (Tok.is(tok::less) &&
1737 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001738 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001739 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001740 RAngleLoc))
1741 return true;
1742
1743 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001744 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1745 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001746 // Form a parsed representation of the template-id to be stored in the
1747 // UnqualifiedId.
1748 TemplateIdAnnotation *TemplateId
Benjamin Kramer13bb7012012-04-14 12:14:03 +00001749 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001750
1751 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1752 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001753 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001754 TemplateId->TemplateNameLoc = Id.StartLocation;
1755 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001756 TemplateId->Name = 0;
1757 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1758 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001759 }
1760
Douglas Gregor059101f2011-03-02 00:47:37 +00001761 TemplateId->SS = SS;
Benjamin Kramer2b28bf12012-02-19 23:37:39 +00001762 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall2b5289b2010-08-23 07:28:44 +00001763 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001764 TemplateId->Kind = TNK;
1765 TemplateId->LAngleLoc = LAngleLoc;
1766 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001767 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001768 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001769 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001770 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001771
1772 Id.setTemplateId(TemplateId);
1773 return false;
1774 }
1775
1776 // Bundle the template arguments together.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001777 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001778
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001779 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001780 TypeResult Type
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001781 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1782 Template, NameLoc,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001783 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1784 /*IsCtorOrDtorName=*/true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001785 if (Type.isInvalid())
1786 return true;
1787
1788 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1789 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1790 else
1791 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1792
1793 return false;
1794}
1795
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001796/// \brief Parse an operator-function-id or conversion-function-id as part
1797/// of a C++ unqualified-id.
1798///
1799/// This routine is responsible only for parsing the operator-function-id or
1800/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001801///
1802/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001803/// operator-function-id: [C++ 13.5]
1804/// 'operator' operator
1805///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001806/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001807/// new delete new[] delete[]
1808/// + - * / % ^ & | ~
1809/// ! = < > += -= *= /= %=
1810/// ^= &= |= << >> >>= <<= == !=
1811/// <= >= && || ++ -- , ->* ->
1812/// () []
1813///
1814/// conversion-function-id: [C++ 12.3.2]
1815/// operator conversion-type-id
1816///
1817/// conversion-type-id:
1818/// type-specifier-seq conversion-declarator[opt]
1819///
1820/// conversion-declarator:
1821/// ptr-operator conversion-declarator[opt]
1822/// \endcode
1823///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001824/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001825/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1826///
1827/// \param EnteringContext whether we are entering the scope of the
1828/// nested-name-specifier.
1829///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001830/// \param ObjectType if this unqualified-id occurs within a member access
1831/// expression, the type of the base object whose member is being accessed.
1832///
1833/// \param Result on a successful parse, contains the parsed unqualified-id.
1834///
1835/// \returns true if parsing fails, false otherwise.
1836bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001837 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001838 UnqualifiedId &Result) {
1839 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1840
1841 // Consume the 'operator' keyword.
1842 SourceLocation KeywordLoc = ConsumeToken();
1843
1844 // Determine what kind of operator name we have.
1845 unsigned SymbolIdx = 0;
1846 SourceLocation SymbolLocations[3];
1847 OverloadedOperatorKind Op = OO_None;
1848 switch (Tok.getKind()) {
1849 case tok::kw_new:
1850 case tok::kw_delete: {
1851 bool isNew = Tok.getKind() == tok::kw_new;
1852 // Consume the 'new' or 'delete'.
1853 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith6ee326a2012-04-10 01:32:12 +00001854 // Check for array new/delete.
1855 if (Tok.is(tok::l_square) &&
Richard Smith80ad52f2013-01-02 11:42:31 +00001856 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001857 // Consume the '[' and ']'.
1858 BalancedDelimiterTracker T(*this, tok::l_square);
1859 T.consumeOpen();
1860 T.consumeClose();
1861 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001862 return true;
1863
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001864 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1865 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001866 Op = isNew? OO_Array_New : OO_Array_Delete;
1867 } else {
1868 Op = isNew? OO_New : OO_Delete;
1869 }
1870 break;
1871 }
1872
1873#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1874 case tok::Token: \
1875 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1876 Op = OO_##Name; \
1877 break;
1878#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1879#include "clang/Basic/OperatorKinds.def"
1880
1881 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001882 // Consume the '(' and ')'.
1883 BalancedDelimiterTracker T(*this, tok::l_paren);
1884 T.consumeOpen();
1885 T.consumeClose();
1886 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001887 return true;
1888
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001889 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1890 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001891 Op = OO_Call;
1892 break;
1893 }
1894
1895 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001896 // Consume the '[' and ']'.
1897 BalancedDelimiterTracker T(*this, tok::l_square);
1898 T.consumeOpen();
1899 T.consumeClose();
1900 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001901 return true;
1902
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001903 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1904 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001905 Op = OO_Subscript;
1906 break;
1907 }
1908
1909 case tok::code_completion: {
1910 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001911 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001912 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001913 // Don't try to parse any further.
1914 return true;
1915 }
1916
1917 default:
1918 break;
1919 }
1920
1921 if (Op != OO_None) {
1922 // We have parsed an operator-function-id.
1923 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1924 return false;
1925 }
Sean Hunt0486d742009-11-28 04:44:28 +00001926
1927 // Parse a literal-operator-id.
1928 //
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001929 // literal-operator-id: C++11 [over.literal]
1930 // operator string-literal identifier
1931 // operator user-defined-string-literal
Sean Hunt0486d742009-11-28 04:44:28 +00001932
Richard Smith80ad52f2013-01-02 11:42:31 +00001933 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith7fe62082011-10-15 05:09:34 +00001934 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001935
Richard Smith33762772012-03-08 23:06:02 +00001936 SourceLocation DiagLoc;
1937 unsigned DiagId = 0;
1938
1939 // We're past translation phase 6, so perform string literal concatenation
1940 // before checking for "".
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001941 SmallVector<Token, 4> Toks;
1942 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith33762772012-03-08 23:06:02 +00001943 while (isTokenStringLiteral()) {
1944 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001945 // C++11 [over.literal]p1:
1946 // The string-literal or user-defined-string-literal in a
1947 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith33762772012-03-08 23:06:02 +00001948 DiagLoc = Tok.getLocation();
1949 DiagId = diag::err_literal_operator_string_prefix;
1950 }
1951 Toks.push_back(Tok);
1952 TokLocs.push_back(ConsumeStringToken());
1953 }
1954
1955 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
1956 if (Literal.hadError)
1957 return true;
1958
1959 // Grab the literal operator's suffix, which will be either the next token
1960 // or a ud-suffix from the string literal.
1961 IdentifierInfo *II = 0;
1962 SourceLocation SuffixLoc;
1963 if (!Literal.getUDSuffix().empty()) {
1964 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
1965 SuffixLoc =
1966 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
1967 Literal.getUDSuffixOffset(),
David Blaikie4e4d0842012-03-11 07:00:24 +00001968 PP.getSourceManager(), getLangOpts());
Richard Smith33762772012-03-08 23:06:02 +00001969 } else if (Tok.is(tok::identifier)) {
1970 II = Tok.getIdentifierInfo();
1971 SuffixLoc = ConsumeToken();
1972 TokLocs.push_back(SuffixLoc);
1973 } else {
Sean Hunt0486d742009-11-28 04:44:28 +00001974 Diag(Tok.getLocation(), diag::err_expected_ident);
1975 return true;
1976 }
1977
Richard Smith33762772012-03-08 23:06:02 +00001978 // The string literal must be empty.
1979 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001980 // C++11 [over.literal]p1:
1981 // The string-literal or user-defined-string-literal in a
1982 // literal-operator-id shall [...] contain no characters
1983 // other than the implicit terminating '\0'.
Richard Smith33762772012-03-08 23:06:02 +00001984 DiagLoc = TokLocs.front();
1985 DiagId = diag::err_literal_operator_string_not_empty;
1986 }
1987
1988 if (DiagId) {
1989 // This isn't a valid literal-operator-id, but we think we know
1990 // what the user meant. Tell them what they should have written.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001991 SmallString<32> Str;
Richard Smith33762772012-03-08 23:06:02 +00001992 Str += "\"\" ";
1993 Str += II->getName();
1994 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
1995 SourceRange(TokLocs.front(), TokLocs.back()), Str);
1996 }
1997
1998 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Sean Hunt3e518bd2009-11-29 07:34:05 +00001999 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00002000 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002001
2002 // Parse a conversion-function-id.
2003 //
2004 // conversion-function-id: [C++ 12.3.2]
2005 // operator conversion-type-id
2006 //
2007 // conversion-type-id:
2008 // type-specifier-seq conversion-declarator[opt]
2009 //
2010 // conversion-declarator:
2011 // ptr-operator conversion-declarator[opt]
2012
2013 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00002014 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00002015 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002016 return true;
2017
2018 // Parse the conversion-declarator, which is merely a sequence of
2019 // ptr-operators.
2020 Declarator D(DS, Declarator::TypeNameContext);
2021 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2022
2023 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00002024 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002025 if (Ty.isInvalid())
2026 return true;
2027
2028 // Note that this is a conversion-function-id.
2029 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2030 D.getSourceRange().getEnd());
2031 return false;
2032}
2033
2034/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2035/// name of an entity.
2036///
2037/// \code
2038/// unqualified-id: [C++ expr.prim.general]
2039/// identifier
2040/// operator-function-id
2041/// conversion-function-id
2042/// [C++0x] literal-operator-id [TODO]
2043/// ~ class-name
2044/// template-id
2045///
2046/// \endcode
2047///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00002048/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002049/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2050///
2051/// \param EnteringContext whether we are entering the scope of the
2052/// nested-name-specifier.
2053///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002054/// \param AllowDestructorName whether we allow parsing of a destructor name.
2055///
2056/// \param AllowConstructorName whether we allow parsing a constructor name.
2057///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00002058/// \param ObjectType if this unqualified-id occurs within a member access
2059/// expression, the type of the base object whose member is being accessed.
2060///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002061/// \param Result on a successful parse, contains the parsed unqualified-id.
2062///
2063/// \returns true if parsing fails, false otherwise.
2064bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2065 bool AllowDestructorName,
2066 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002067 ParsedType ObjectType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002068 SourceLocation& TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002069 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00002070
2071 // Handle 'A::template B'. This is for template-ids which have not
2072 // already been annotated by ParseOptionalCXXScopeSpecifier().
2073 bool TemplateSpecified = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002074 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002075 (ObjectType || SS.isSet())) {
2076 TemplateSpecified = true;
2077 TemplateKWLoc = ConsumeToken();
2078 }
2079
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002080 // unqualified-id:
2081 // identifier
2082 // template-id (when it hasn't already been annotated)
2083 if (Tok.is(tok::identifier)) {
2084 // Consume the identifier.
2085 IdentifierInfo *Id = Tok.getIdentifierInfo();
2086 SourceLocation IdLoc = ConsumeToken();
2087
David Blaikie4e4d0842012-03-11 07:00:24 +00002088 if (!getLangOpts().CPlusPlus) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002089 // If we're not in C++, only identifiers matter. Record the
2090 // identifier and return.
2091 Result.setIdentifier(Id, IdLoc);
2092 return false;
2093 }
2094
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002095 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002096 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002097 // We have parsed a constructor name.
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002098 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2099 &SS, false, false,
2100 ParsedType(),
2101 /*IsCtorOrDtorName=*/true,
2102 /*NonTrivialTypeSourceInfo=*/true);
2103 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002104 } else {
2105 // We have parsed an identifier.
2106 Result.setIdentifier(Id, IdLoc);
2107 }
2108
2109 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00002110 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002111 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2112 EnteringContext, ObjectType,
2113 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002114
2115 return false;
2116 }
2117
2118 // unqualified-id:
2119 // template-id (already parsed and annotated)
2120 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002121 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002122
2123 // If the template-name names the current class, then this is a constructor
2124 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002125 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002126 if (SS.isSet()) {
2127 // C++ [class.qual]p2 specifies that a qualified template-name
2128 // is taken as the constructor name where a constructor can be
2129 // declared. Thus, the template arguments are extraneous, so
2130 // complain about them and remove them entirely.
2131 Diag(TemplateId->TemplateNameLoc,
2132 diag::err_out_of_line_constructor_template_id)
2133 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00002134 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002135 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002136 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2137 TemplateId->TemplateNameLoc,
2138 getCurScope(),
2139 &SS, false, false,
2140 ParsedType(),
2141 /*IsCtorOrDtorName=*/true,
2142 /*NontrivialTypeSourceInfo=*/true);
2143 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002144 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002145 ConsumeToken();
2146 return false;
2147 }
2148
2149 Result.setConstructorTemplateId(TemplateId);
2150 ConsumeToken();
2151 return false;
2152 }
2153
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002154 // We have already parsed a template-id; consume the annotation token as
2155 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002156 Result.setTemplateId(TemplateId);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002157 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002158 ConsumeToken();
2159 return false;
2160 }
2161
2162 // unqualified-id:
2163 // operator-function-id
2164 // conversion-function-id
2165 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002166 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002167 return true;
2168
Sean Hunte6252d12009-11-28 08:58:14 +00002169 // If we have an operator-function-id or a literal-operator-id and the next
2170 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002171 //
2172 // template-id:
2173 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00002174 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2175 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002176 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002177 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2178 0, SourceLocation(),
2179 EnteringContext, ObjectType,
2180 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002181
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002182 return false;
2183 }
2184
David Blaikie4e4d0842012-03-11 07:00:24 +00002185 if (getLangOpts().CPlusPlus &&
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002186 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002187 // C++ [expr.unary.op]p10:
2188 // There is an ambiguity in the unary-expression ~X(), where X is a
2189 // class-name. The ambiguity is resolved in favor of treating ~ as a
2190 // unary complement rather than treating ~X as referring to a destructor.
2191
2192 // Parse the '~'.
2193 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00002194
2195 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2196 DeclSpec DS(AttrFactory);
2197 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2198 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2199 Result.setDestructorName(TildeLoc, Type, EndLoc);
2200 return false;
2201 }
2202 return true;
2203 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002204
2205 // Parse the class-name.
2206 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00002207 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002208 return true;
2209 }
2210
2211 // Parse the class-name (or template-name in a simple-template-id).
2212 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2213 SourceLocation ClassNameLoc = ConsumeToken();
2214
Douglas Gregor0278e122010-05-05 05:58:24 +00002215 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002216 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002217 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2218 ClassName, ClassNameLoc,
2219 EnteringContext, ObjectType,
2220 Result, TemplateSpecified);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002221 }
2222
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002223 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002224 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2225 ClassNameLoc, getCurScope(),
2226 SS, ObjectType,
2227 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002228 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002229 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002230
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002231 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002232 return false;
2233 }
2234
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002235 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikie4e4d0842012-03-11 07:00:24 +00002236 << getLangOpts().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002237 return true;
2238}
2239
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002240/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2241/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002242///
Chris Lattner59232d32009-01-04 21:25:24 +00002243/// This method is called to parse the new expression after the optional :: has
2244/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2245/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002246///
2247/// new-expression:
2248/// '::'[opt] 'new' new-placement[opt] new-type-id
2249/// new-initializer[opt]
2250/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2251/// new-initializer[opt]
2252///
2253/// new-placement:
2254/// '(' expression-list ')'
2255///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002256/// new-type-id:
2257/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002258/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002259///
2260/// new-declarator:
2261/// ptr-operator new-declarator[opt]
2262/// direct-new-declarator
2263///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002264/// new-initializer:
2265/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002266/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002267///
John McCall60d7b3a2010-08-24 06:29:42 +00002268ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002269Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2270 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2271 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002272
2273 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2274 // second form of new-expression. It can't be a new-type-id.
2275
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002276 ExprVector PlacementArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002277 SourceLocation PlacementLParen, PlacementRParen;
2278
Douglas Gregor4bd40312010-07-13 15:54:32 +00002279 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002280 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002281 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002282 if (Tok.is(tok::l_paren)) {
2283 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002284 BalancedDelimiterTracker T(*this, tok::l_paren);
2285 T.consumeOpen();
2286 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002287 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2288 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002289 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002290 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002291
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002292 T.consumeClose();
2293 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002294 if (PlacementRParen.isInvalid()) {
2295 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002296 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002297 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002298
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002299 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002300 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002301 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002302 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002303 } else {
2304 // We still need the type.
2305 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002306 BalancedDelimiterTracker T(*this, tok::l_paren);
2307 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002308 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002309 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002310 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002311 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002312 T.consumeClose();
2313 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002314 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002315 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002316 if (ParseCXXTypeSpecifierSeq(DS))
2317 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002318 else {
2319 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002320 ParseDeclaratorInternal(DeclaratorInfo,
2321 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002322 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002323 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002324 }
2325 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002326 // A new-type-id is a simplified type-id, where essentially the
2327 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002328 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002329 if (ParseCXXTypeSpecifierSeq(DS))
2330 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002331 else {
2332 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002333 ParseDeclaratorInternal(DeclaratorInfo,
2334 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002335 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002336 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002337 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002338 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
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002342 ExprResult Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002343
2344 if (Tok.is(tok::l_paren)) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002345 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002346 ExprVector ConstructorArgs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002347 BalancedDelimiterTracker T(*this, tok::l_paren);
2348 T.consumeOpen();
2349 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002350 if (Tok.isNot(tok::r_paren)) {
2351 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002352 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2353 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002354 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002355 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002356 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002357 T.consumeClose();
2358 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002359 if (ConstructorRParen.isInvalid()) {
2360 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002361 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002362 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002363 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2364 ConstructorRParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002365 ConstructorArgs);
Richard Smith80ad52f2013-01-02 11:42:31 +00002366 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00002367 Diag(Tok.getLocation(),
2368 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002369 Initializer = ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002370 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002371 if (Initializer.isInvalid())
2372 return Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002373
Sebastian Redlf53597f2009-03-15 17:47:39 +00002374 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002375 PlacementArgs, PlacementRParen,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002376 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002377}
2378
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002379/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2380/// passed to ParseDeclaratorInternal.
2381///
2382/// direct-new-declarator:
2383/// '[' expression ']'
2384/// direct-new-declarator '[' constant-expression ']'
2385///
Chris Lattner59232d32009-01-04 21:25:24 +00002386void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002387 // Parse the array dimensions.
2388 bool first = true;
2389 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00002390 // An array-size expression can't start with a lambda.
2391 if (CheckProhibitedCXX11Attribute())
2392 continue;
2393
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002394 BalancedDelimiterTracker T(*this, tok::l_square);
2395 T.consumeOpen();
2396
John McCall60d7b3a2010-08-24 06:29:42 +00002397 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002398 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002399 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002400 // Recover
2401 SkipUntil(tok::r_square);
2402 return;
2403 }
2404 first = false;
2405
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002406 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002407
Bill Wendlingad017fa2012-12-20 19:22:21 +00002408 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith6ee326a2012-04-10 01:32:12 +00002409 ParsedAttributes Attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002410 MaybeParseCXX11Attributes(Attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00002411
John McCall0b7e6782011-03-24 11:26:52 +00002412 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002413 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002414 Size.release(),
2415 T.getOpenLocation(),
2416 T.getCloseLocation()),
Richard Smith6ee326a2012-04-10 01:32:12 +00002417 Attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002418
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002419 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002420 return;
2421 }
2422}
2423
2424/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2425/// This ambiguity appears in the syntax of the C++ new operator.
2426///
2427/// new-expression:
2428/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2429/// new-initializer[opt]
2430///
2431/// new-placement:
2432/// '(' expression-list ')'
2433///
John McCallca0408f2010-08-23 06:44:23 +00002434bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002435 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002436 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002437 // The '(' was already consumed.
2438 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002439 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002440 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002441 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002442 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002443 }
2444
2445 // It's not a type, it has to be an expression list.
2446 // Discard the comma locations - ActOnCXXNew has enough parameters.
2447 CommaLocsTy CommaLocs;
2448 return ParseExpressionList(PlacementArgs, CommaLocs);
2449}
2450
2451/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2452/// to free memory allocated by new.
2453///
Chris Lattner59232d32009-01-04 21:25:24 +00002454/// This method is called to parse the 'delete' expression after the optional
2455/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2456/// and "Start" is its location. Otherwise, "Start" is the location of the
2457/// 'delete' token.
2458///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002459/// delete-expression:
2460/// '::'[opt] 'delete' cast-expression
2461/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002462ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002463Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2464 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2465 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002466
2467 // Array delete?
2468 bool ArrayDelete = false;
Richard Smith6ee326a2012-04-10 01:32:12 +00002469 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith950435c2012-08-09 19:01:51 +00002470 // C++11 [expr.delete]p1:
2471 // Whenever the delete keyword is followed by empty square brackets, it
2472 // shall be interpreted as [array delete].
2473 // [Footnote: A lambda expression with a lambda-introducer that consists
2474 // of empty square brackets can follow the delete keyword if
2475 // the lambda expression is enclosed in parentheses.]
2476 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2477 // lambda-introducer.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002478 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002479 BalancedDelimiterTracker T(*this, tok::l_square);
2480
2481 T.consumeOpen();
2482 T.consumeClose();
2483 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002484 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002485 }
2486
John McCall60d7b3a2010-08-24 06:29:42 +00002487 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002488 if (Operand.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002489 return Operand;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002490
John McCall9ae2f072010-08-23 23:25:46 +00002491 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002492}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002493
Mike Stump1eb44332009-09-09 15:08:12 +00002494static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002495 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002496 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002497 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002498 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002499 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002500 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002501 case tok::kw___has_trivial_constructor:
2502 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002503 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002504 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2505 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2506 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002507 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2508 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002509 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002510 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2511 case tok::kw___is_compound: return UTT_IsCompound;
2512 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002513 case tok::kw___is_empty: return UTT_IsEmpty;
2514 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002515 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002516 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2517 case tok::kw___is_function: return UTT_IsFunction;
2518 case tok::kw___is_fundamental: return UTT_IsFundamental;
2519 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallea30e2f2012-09-25 07:32:49 +00002520 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002521 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2522 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2523 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2524 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2525 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002526 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002527 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002528 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002529 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002530 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002531 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002532 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2533 case tok::kw___is_scalar: return UTT_IsScalar;
2534 case tok::kw___is_signed: return UTT_IsSigned;
2535 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2536 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002537 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002538 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002539 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2540 case tok::kw___is_void: return UTT_IsVoid;
2541 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002542 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002543}
2544
2545static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2546 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002547 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002548 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002549 case tok::kw___is_convertible: return BTT_IsConvertible;
2550 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002551 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002552 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002553 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002554 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002555}
2556
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002557static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2558 switch (kind) {
2559 default: llvm_unreachable("Not a known type trait");
2560 case tok::kw___is_trivially_constructible:
2561 return TT_IsTriviallyConstructible;
2562 }
2563}
2564
John Wiegley21ff2e52011-04-28 00:16:57 +00002565static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2566 switch(kind) {
2567 default: llvm_unreachable("Not a known binary type trait");
2568 case tok::kw___array_rank: return ATT_ArrayRank;
2569 case tok::kw___array_extent: return ATT_ArrayExtent;
2570 }
2571}
2572
John Wiegley55262202011-04-25 06:54:41 +00002573static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2574 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002575 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002576 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2577 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2578 }
2579}
2580
Sebastian Redl64b45f72009-01-05 20:52:13 +00002581/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2582/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2583/// templates.
2584///
2585/// primary-expression:
2586/// [GNU] unary-type-trait '(' type-id ')'
2587///
John McCall60d7b3a2010-08-24 06:29:42 +00002588ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002589 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2590 SourceLocation Loc = ConsumeToken();
2591
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002592 BalancedDelimiterTracker T(*this, tok::l_paren);
2593 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002594 return ExprError();
2595
2596 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2597 // there will be cryptic errors about mismatched parentheses and missing
2598 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002599 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002600
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002601 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002602
Douglas Gregor809070a2009-02-18 17:45:20 +00002603 if (Ty.isInvalid())
2604 return ExprError();
2605
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002606 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002607}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002608
Francois Pichet6ad6f282010-12-07 00:08:36 +00002609/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2610/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2611/// templates.
2612///
2613/// primary-expression:
2614/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2615///
2616ExprResult Parser::ParseBinaryTypeTrait() {
2617 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2618 SourceLocation Loc = ConsumeToken();
2619
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002620 BalancedDelimiterTracker T(*this, tok::l_paren);
2621 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002622 return ExprError();
2623
2624 TypeResult LhsTy = ParseTypeName();
2625 if (LhsTy.isInvalid()) {
2626 SkipUntil(tok::r_paren);
2627 return ExprError();
2628 }
2629
2630 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2631 SkipUntil(tok::r_paren);
2632 return ExprError();
2633 }
2634
2635 TypeResult RhsTy = ParseTypeName();
2636 if (RhsTy.isInvalid()) {
2637 SkipUntil(tok::r_paren);
2638 return ExprError();
2639 }
2640
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002641 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002642
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002643 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2644 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002645}
2646
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002647/// \brief Parse the built-in type-trait pseudo-functions that allow
2648/// implementation of the TR1/C++11 type traits templates.
2649///
2650/// primary-expression:
2651/// type-trait '(' type-id-seq ')'
2652///
2653/// type-id-seq:
2654/// type-id ...[opt] type-id-seq[opt]
2655///
2656ExprResult Parser::ParseTypeTrait() {
2657 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2658 SourceLocation Loc = ConsumeToken();
2659
2660 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2661 if (Parens.expectAndConsume(diag::err_expected_lparen))
2662 return ExprError();
2663
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002664 SmallVector<ParsedType, 2> Args;
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002665 do {
2666 // Parse the next type.
2667 TypeResult Ty = ParseTypeName();
2668 if (Ty.isInvalid()) {
2669 Parens.skipToEnd();
2670 return ExprError();
2671 }
2672
2673 // Parse the ellipsis, if present.
2674 if (Tok.is(tok::ellipsis)) {
2675 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2676 if (Ty.isInvalid()) {
2677 Parens.skipToEnd();
2678 return ExprError();
2679 }
2680 }
2681
2682 // Add this type to the list of arguments.
2683 Args.push_back(Ty.get());
2684
2685 if (Tok.is(tok::comma)) {
2686 ConsumeToken();
2687 continue;
2688 }
2689
2690 break;
2691 } while (true);
2692
2693 if (Parens.consumeClose())
2694 return ExprError();
2695
2696 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2697}
2698
John Wiegley21ff2e52011-04-28 00:16:57 +00002699/// ParseArrayTypeTrait - Parse the built-in array type-trait
2700/// pseudo-functions.
2701///
2702/// primary-expression:
2703/// [Embarcadero] '__array_rank' '(' type-id ')'
2704/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2705///
2706ExprResult Parser::ParseArrayTypeTrait() {
2707 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2708 SourceLocation Loc = ConsumeToken();
2709
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002710 BalancedDelimiterTracker T(*this, tok::l_paren);
2711 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002712 return ExprError();
2713
2714 TypeResult Ty = ParseTypeName();
2715 if (Ty.isInvalid()) {
2716 SkipUntil(tok::comma);
2717 SkipUntil(tok::r_paren);
2718 return ExprError();
2719 }
2720
2721 switch (ATT) {
2722 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002723 T.consumeClose();
2724 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2725 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002726 }
2727 case ATT_ArrayExtent: {
2728 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2729 SkipUntil(tok::r_paren);
2730 return ExprError();
2731 }
2732
2733 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002734 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002735
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002736 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2737 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002738 }
John Wiegley21ff2e52011-04-28 00:16:57 +00002739 }
David Blaikie30263482012-01-20 21:50:17 +00002740 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley21ff2e52011-04-28 00:16:57 +00002741}
2742
John Wiegley55262202011-04-25 06:54:41 +00002743/// ParseExpressionTrait - Parse built-in expression-trait
2744/// pseudo-functions like __is_lvalue_expr( xxx ).
2745///
2746/// primary-expression:
2747/// [Embarcadero] expression-trait '(' expression ')'
2748///
2749ExprResult Parser::ParseExpressionTrait() {
2750 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2751 SourceLocation Loc = ConsumeToken();
2752
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002753 BalancedDelimiterTracker T(*this, tok::l_paren);
2754 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002755 return ExprError();
2756
2757 ExprResult Expr = ParseExpression();
2758
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002759 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002760
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002761 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2762 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002763}
2764
2765
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002766/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2767/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2768/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002769ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002770Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002771 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002772 BalancedDelimiterTracker &Tracker) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002773 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002774 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2775 assert(isTypeIdInParens() && "Not a type-id!");
2776
John McCall60d7b3a2010-08-24 06:29:42 +00002777 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002778 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002779
2780 // We need to disambiguate a very ugly part of the C++ syntax:
2781 //
2782 // (T())x; - type-id
2783 // (T())*x; - type-id
2784 // (T())/x; - expression
2785 // (T()); - expression
2786 //
2787 // The bad news is that we cannot use the specialized tentative parser, since
2788 // it can only verify that the thing inside the parens can be parsed as
2789 // type-id, it is not useful for determining the context past the parens.
2790 //
2791 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002792 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002793 //
2794 // It uses a scheme similar to parsing inline methods. The parenthesized
2795 // tokens are cached, the context that follows is determined (possibly by
2796 // parsing a cast-expression), and then we re-introduce the cached tokens
2797 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002798
Mike Stump1eb44332009-09-09 15:08:12 +00002799 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002800 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002801
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002802 // Store the tokens of the parentheses. We will parse them after we determine
2803 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002804 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002805 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002806 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002807 return ExprError();
2808 }
2809
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002810 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002811 ParseAs = CompoundLiteral;
2812 } else {
2813 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002814 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2815 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2816 NotCastExpr = true;
2817 } else {
2818 // Try parsing the cast-expression that may follow.
2819 // If it is not a cast-expression, NotCastExpr will be true and no token
2820 // will be consumed.
2821 Result = ParseCastExpression(false/*isUnaryExpression*/,
2822 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002823 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002824 // type-id has priority.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002825 IsTypeCast);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002826 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002827
2828 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2829 // an expression.
2830 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002831 }
2832
Mike Stump1eb44332009-09-09 15:08:12 +00002833 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002834 Toks.push_back(Tok);
2835 // Re-enter the stored parenthesized tokens into the token stream, so we may
2836 // parse them now.
2837 PP.EnterTokenStream(Toks.data(), Toks.size(),
2838 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2839 // Drop the current token and bring the first cached one. It's the same token
2840 // as when we entered this function.
2841 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002842
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002843 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002844 // Parse the type declarator.
2845 DeclSpec DS(AttrFactory);
2846 ParseSpecifierQualifierList(DS);
2847 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2848 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002849
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002850 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002851 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002852
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002853 if (ParseAs == CompoundLiteral) {
2854 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002855 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002856 return ParseCompoundLiteralExpression(Ty.get(),
2857 Tracker.getOpenLocation(),
2858 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002859 }
Mike Stump1eb44332009-09-09 15:08:12 +00002860
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002861 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2862 assert(ParseAs == CastExpr);
2863
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002864 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002865 return ExprError();
2866
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002867 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002868 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002869 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2870 DeclaratorInfo, CastTy,
2871 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002872 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002873 }
Mike Stump1eb44332009-09-09 15:08:12 +00002874
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002875 // Not a compound literal, and not followed by a cast-expression.
2876 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002877
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002878 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002879 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002880 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002881 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2882 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002883
2884 // Match the ')'.
2885 if (Result.isInvalid()) {
2886 SkipUntil(tok::r_paren);
2887 return ExprError();
2888 }
Mike Stump1eb44332009-09-09 15:08:12 +00002889
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002890 Tracker.consumeClose();
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002891 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002892}