blob: 94b5dbad49f598187864be31055102ce49cd687f [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
605 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
606 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.
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000661llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro){
Douglas Gregorae7902c2011-08-04 15:30:47 +0000662 typedef llvm::Optional<unsigned> DiagResult;
663
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
772 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
773
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 |
800 Scope::DeclScope);
801
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000802 SourceLocation DeclEndLoc;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000803 BalancedDelimiterTracker T(*this, tok::l_paren);
804 T.consumeOpen();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000805 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000806
807 // Parse parameter-declaration-clause.
808 ParsedAttributes Attr(AttrFactory);
809 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
810 SourceLocation EllipsisLoc;
811
812 if (Tok.isNot(tok::r_paren))
813 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
814
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000815 T.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000816 SourceLocation RParenLoc = T.getCloseLocation();
817 DeclEndLoc = RParenLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000818
819 // Parse 'mutable'[opt].
820 SourceLocation MutableLoc;
821 if (Tok.is(tok::kw_mutable)) {
822 MutableLoc = ConsumeToken();
823 DeclEndLoc = MutableLoc;
824 }
825
826 // Parse exception-specification[opt].
827 ExceptionSpecificationType ESpecType = EST_None;
828 SourceRange ESpecRange;
829 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
830 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
831 ExprResult NoexceptExpr;
Richard Smitha058fd42012-05-02 22:22:32 +0000832 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +0000833 DynamicExceptions,
834 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +0000835 NoexceptExpr);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000836
837 if (ESpecType != EST_None)
838 DeclEndLoc = ESpecRange.getEnd();
839
840 // Parse attribute-specifier[opt].
Richard Smith4e24f0f2013-01-02 12:01:23 +0000841 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000842
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000843 SourceLocation FunLocalRangeEnd = DeclEndLoc;
844
Douglas Gregorae7902c2011-08-04 15:30:47 +0000845 // Parse trailing-return-type[opt].
Richard Smith54655be2012-06-12 01:51:59 +0000846 TypeResult TrailingReturnType;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000847 if (Tok.is(tok::arrow)) {
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000848 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000849 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +0000850 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000851 if (Range.getEnd().isValid())
852 DeclEndLoc = Range.getEnd();
853 }
854
855 PrototypeScope.Exit();
856
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000857 SourceLocation NoLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000858 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000859 /*isAmbiguous=*/false,
860 LParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000861 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000862 EllipsisLoc, RParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000863 DS.getTypeQualifiers(),
864 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000865 /*RefQualifierLoc=*/NoLoc,
866 /*ConstQualifierLoc=*/NoLoc,
867 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000868 MutableLoc,
869 ESpecType, ESpecRange.getBegin(),
870 DynamicExceptions.data(),
871 DynamicExceptionRanges.data(),
872 DynamicExceptions.size(),
873 NoexceptExpr.isUsable() ?
874 NoexceptExpr.get() : 0,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000875 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000876 TrailingReturnType),
877 Attr, DeclEndLoc);
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000878 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
879 // It's common to forget that one needs '()' before 'mutable' or the
880 // result type. Deal with this.
881 Diag(Tok, diag::err_lambda_missing_parens)
882 << Tok.is(tok::arrow)
883 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
884 SourceLocation DeclLoc = Tok.getLocation();
885 SourceLocation DeclEndLoc = DeclLoc;
886
887 // Parse 'mutable', if it's there.
888 SourceLocation MutableLoc;
889 if (Tok.is(tok::kw_mutable)) {
890 MutableLoc = ConsumeToken();
891 DeclEndLoc = MutableLoc;
892 }
893
894 // Parse the return type, if there is one.
Richard Smith54655be2012-06-12 01:51:59 +0000895 TypeResult TrailingReturnType;
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000896 if (Tok.is(tok::arrow)) {
897 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +0000898 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000899 if (Range.getEnd().isValid())
900 DeclEndLoc = Range.getEnd();
901 }
902
903 ParsedAttributes Attr(AttrFactory);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000904 SourceLocation NoLoc;
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000905 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000906 /*isAmbiguous=*/false,
907 /*LParenLoc=*/NoLoc,
908 /*Params=*/0,
909 /*NumParams=*/0,
910 /*EllipsisLoc=*/NoLoc,
911 /*RParenLoc=*/NoLoc,
912 /*TypeQuals=*/0,
913 /*RefQualifierIsLValueRef=*/true,
914 /*RefQualifierLoc=*/NoLoc,
915 /*ConstQualifierLoc=*/NoLoc,
916 /*VolatileQualifierLoc=*/NoLoc,
917 MutableLoc,
918 EST_None,
919 /*ESpecLoc=*/NoLoc,
920 /*Exceptions=*/0,
921 /*ExceptionRanges=*/0,
922 /*NumExceptions=*/0,
923 /*NoexceptExpr=*/0,
924 DeclLoc, DeclEndLoc, D,
925 TrailingReturnType),
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000926 Attr, DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000927 }
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000928
Douglas Gregorae7902c2011-08-04 15:30:47 +0000929
Eli Friedman906a7e12012-01-06 03:05:34 +0000930 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
931 // it.
Douglas Gregorfccfb622012-02-21 22:51:27 +0000932 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorfccfb622012-02-21 22:51:27 +0000933 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman906a7e12012-01-06 03:05:34 +0000934
Eli Friedmanec9ea722012-01-05 03:35:19 +0000935 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
936
Douglas Gregorae7902c2011-08-04 15:30:47 +0000937 // Parse compound-statement.
Eli Friedmandc3b7232012-01-04 02:40:39 +0000938 if (!Tok.is(tok::l_brace)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +0000939 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000940 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
941 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000942 }
943
Eli Friedmandc3b7232012-01-04 02:40:39 +0000944 StmtResult Stmt(ParseCompoundStatementBody());
945 BodyScope.Exit();
946
Eli Friedmandeeab902012-01-04 02:46:53 +0000947 if (!Stmt.isInvalid())
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000948 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmandc3b7232012-01-04 02:40:39 +0000949
Eli Friedmandeeab902012-01-04 02:46:53 +0000950 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
951 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000952}
953
Reid Spencer5f016e22007-07-11 17:01:13 +0000954/// ParseCXXCasts - This handles the various ways to cast expressions to another
955/// type.
956///
957/// postfix-expression: [C++ 5.2p1]
958/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
959/// 'static_cast' '<' type-name '>' '(' expression ')'
960/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
961/// 'const_cast' '<' type-name '>' '(' expression ')'
962///
John McCall60d7b3a2010-08-24 06:29:42 +0000963ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 tok::TokenKind Kind = Tok.getKind();
965 const char *CastName = 0; // For error messages
966
967 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000968 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 case tok::kw_const_cast: CastName = "const_cast"; break;
970 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
971 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
972 case tok::kw_static_cast: CastName = "static_cast"; break;
973 }
974
975 SourceLocation OpLoc = ConsumeToken();
976 SourceLocation LAngleBracketLoc = Tok.getLocation();
977
Richard Smithea698b32011-04-14 21:45:45 +0000978 // Check for "<::" which is parsed as "[:". If found, fix token stream,
979 // diagnose error, suggest fix, and recover parsing.
Richard Smith78fe3e02012-08-20 17:37:52 +0000980 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
981 Token Next = NextToken();
982 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
983 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
984 }
Richard Smithea698b32011-04-14 21:45:45 +0000985
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000987 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000988
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000989 // Parse the common declaration-specifiers piece.
990 DeclSpec DS(AttrFactory);
991 ParseSpecifierQualifierList(DS);
992
993 // Parse the abstract-declarator, if present.
994 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
995 ParseDeclarator(DeclaratorInfo);
996
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 SourceLocation RAngleBracketLoc = Tok.getLocation();
998
Chris Lattner1ab3b962008-11-18 07:48:38 +0000999 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001000 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +00001001
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001002 SourceLocation LParenLoc, RParenLoc;
1003 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001004
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001005 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001006 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001007
John McCall60d7b3a2010-08-24 06:29:42 +00001008 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001010 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001011 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001012
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001013 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +00001014 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001015 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +00001016 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001017 T.getOpenLocation(), Result.take(),
1018 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001019
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001020 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00001021}
1022
Sebastian Redlc42e1182008-11-11 11:37:55 +00001023/// ParseCXXTypeid - This handles the C++ typeid expression.
1024///
1025/// postfix-expression: [C++ 5.2p1]
1026/// 'typeid' '(' expression ')'
1027/// 'typeid' '(' type-id ')'
1028///
John McCall60d7b3a2010-08-24 06:29:42 +00001029ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +00001030 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1031
1032 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001033 SourceLocation LParenLoc, RParenLoc;
1034 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001035
1036 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001037 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001038 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001039 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001040
John McCall60d7b3a2010-08-24 06:29:42 +00001041 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001042
Richard Smith05766812012-08-18 00:55:03 +00001043 // C++0x [expr.typeid]p3:
1044 // When typeid is applied to an expression other than an lvalue of a
1045 // polymorphic class type [...] The expression is an unevaluated
1046 // operand (Clause 5).
1047 //
1048 // Note that we can't tell whether the expression is an lvalue of a
1049 // polymorphic class type until after we've parsed the expression; we
1050 // speculatively assume the subexpression is unevaluated, and fix it up
1051 // later.
1052 //
1053 // We enter the unevaluated context before trying to determine whether we
1054 // have a type-id, because the tentative parse logic will try to resolve
1055 // names, and must treat them as unevaluated.
Eli Friedman80bfa3d2012-09-26 04:34:21 +00001056 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1057 Sema::ReuseLambdaContextDecl);
Richard Smith05766812012-08-18 00:55:03 +00001058
Sebastian Redlc42e1182008-11-11 11:37:55 +00001059 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001060 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001061
1062 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001063 T.consumeClose();
1064 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001065 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001066 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001067
1068 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +00001069 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001070 } else {
1071 Result = ParseExpression();
1072
1073 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001074 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +00001075 SkipUntil(tok::r_paren);
1076 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001077 T.consumeClose();
1078 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001079 if (RParenLoc.isInvalid())
1080 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001081
Sebastian Redlc42e1182008-11-11 11:37:55 +00001082 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001083 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001084 }
1085 }
1086
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001087 return Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001088}
1089
Francois Pichet01b7c302010-09-08 12:20:18 +00001090/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1091///
1092/// '__uuidof' '(' expression ')'
1093/// '__uuidof' '(' type-id ')'
1094///
1095ExprResult Parser::ParseCXXUuidof() {
1096 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1097
1098 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001099 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +00001100
1101 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001102 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +00001103 return ExprError();
1104
1105 ExprResult Result;
1106
1107 if (isTypeIdInParens()) {
1108 TypeResult Ty = ParseTypeName();
1109
1110 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001111 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001112
1113 if (Ty.isInvalid())
1114 return ExprError();
1115
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001116 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1117 Ty.get().getAsOpaquePtr(),
1118 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001119 } else {
1120 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1121 Result = ParseExpression();
1122
1123 // Match the ')'.
1124 if (Result.isInvalid())
1125 SkipUntil(tok::r_paren);
1126 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001127 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001128
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001129 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1130 /*isType=*/false,
1131 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001132 }
1133 }
1134
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001135 return Result;
Francois Pichet01b7c302010-09-08 12:20:18 +00001136}
1137
Douglas Gregord4dca082010-02-24 18:44:31 +00001138/// \brief Parse a C++ pseudo-destructor expression after the base,
1139/// . or -> operator, and nested-name-specifier have already been
1140/// parsed.
1141///
1142/// postfix-expression: [C++ 5.2]
1143/// postfix-expression . pseudo-destructor-name
1144/// postfix-expression -> pseudo-destructor-name
1145///
1146/// pseudo-destructor-name:
1147/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1148/// ::[opt] nested-name-specifier template simple-template-id ::
1149/// ~type-name
1150/// ::[opt] nested-name-specifier[opt] ~type-name
1151///
John McCall60d7b3a2010-08-24 06:29:42 +00001152ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +00001153Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1154 tok::TokenKind OpKind,
1155 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +00001156 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +00001157 // We're parsing either a pseudo-destructor-name or a dependent
1158 // member access that has the same form as a
1159 // pseudo-destructor-name. We parse both in the same way and let
1160 // the action model sort them out.
1161 //
1162 // Note that the ::[opt] nested-name-specifier[opt] has already
1163 // been parsed, and if there was a simple-template-id, it has
1164 // been coalesced into a template-id annotation token.
1165 UnqualifiedId FirstTypeName;
1166 SourceLocation CCLoc;
1167 if (Tok.is(tok::identifier)) {
1168 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1169 ConsumeToken();
1170 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1171 CCLoc = ConsumeToken();
1172 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001173 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1174 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregord4dca082010-02-24 18:44:31 +00001175 FirstTypeName.setTemplateId(
1176 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1177 ConsumeToken();
1178 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1179 CCLoc = ConsumeToken();
1180 } else {
1181 FirstTypeName.setIdentifier(0, SourceLocation());
1182 }
1183
1184 // Parse the tilde.
1185 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1186 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001187
1188 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1189 DeclSpec DS(AttrFactory);
Benjamin Kramer85c60db2011-12-18 12:18:02 +00001190 ParseDecltypeSpecifier(DS);
David Blaikie91ec7892011-12-16 16:03:09 +00001191 if (DS.getTypeSpecType() == TST_error)
1192 return ExprError();
1193 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1194 OpKind, TildeLoc, DS,
1195 Tok.is(tok::l_paren));
1196 }
1197
Douglas Gregord4dca082010-02-24 18:44:31 +00001198 if (!Tok.is(tok::identifier)) {
1199 Diag(Tok, diag::err_destructor_tilde_identifier);
1200 return ExprError();
1201 }
1202
1203 // Parse the second type.
1204 UnqualifiedId SecondTypeName;
1205 IdentifierInfo *Name = Tok.getIdentifierInfo();
1206 SourceLocation NameLoc = ConsumeToken();
1207 SecondTypeName.setIdentifier(Name, NameLoc);
1208
1209 // If there is a '<', the second type name is a template-id. Parse
1210 // it as such.
1211 if (Tok.is(tok::less) &&
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001212 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1213 Name, NameLoc,
1214 false, ObjectType, SecondTypeName,
1215 /*AssumeTemplateName=*/true))
Douglas Gregord4dca082010-02-24 18:44:31 +00001216 return ExprError();
1217
John McCall9ae2f072010-08-23 23:25:46 +00001218 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1219 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001220 SS, FirstTypeName, CCLoc,
1221 TildeLoc, SecondTypeName,
1222 Tok.is(tok::l_paren));
1223}
1224
Reid Spencer5f016e22007-07-11 17:01:13 +00001225/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1226///
1227/// boolean-literal: [C++ 2.13.5]
1228/// 'true'
1229/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001230ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001232 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001233}
Chris Lattner50dd2892008-02-26 00:51:44 +00001234
1235/// ParseThrowExpression - This handles the C++ throw expression.
1236///
1237/// throw-expression: [C++ 15]
1238/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001239ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001240 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001241 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001242
Chris Lattner2a2819a2008-04-06 06:02:23 +00001243 // If the current token isn't the start of an assignment-expression,
1244 // then the expression is not present. This handles things like:
1245 // "C ? throw : (void)42", which is crazy but legal.
1246 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1247 case tok::semi:
1248 case tok::r_paren:
1249 case tok::r_square:
1250 case tok::r_brace:
1251 case tok::colon:
1252 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001253 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001254
Chris Lattner2a2819a2008-04-06 06:02:23 +00001255 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001256 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001257 if (Expr.isInvalid()) return Expr;
Douglas Gregorbca01b42011-07-06 22:04:06 +00001258 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001259 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001260}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001261
1262/// ParseCXXThis - This handles the C++ 'this' pointer.
1263///
1264/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1265/// a non-lvalue expression whose value is the address of the object for which
1266/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001267ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001268 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1269 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001270 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001271}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001272
1273/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1274/// Can be interpreted either as function-style casting ("int(x)")
1275/// or class type construction ("ClassType(x,y,z)")
1276/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001277/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001278///
1279/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001280/// simple-type-specifier '(' expression-list[opt] ')'
1281/// [C++0x] simple-type-specifier braced-init-list
1282/// typename-specifier '(' expression-list[opt] ')'
1283/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001284///
John McCall60d7b3a2010-08-24 06:29:42 +00001285ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001286Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001287 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001288 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001289
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001290 assert((Tok.is(tok::l_paren) ||
Richard Smith80ad52f2013-01-02 11:42:31 +00001291 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001292 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001293
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001294 if (Tok.is(tok::l_brace)) {
Sebastian Redl6dc00f62012-02-12 18:41:05 +00001295 ExprResult Init = ParseBraceInitializer();
1296 if (Init.isInvalid())
1297 return Init;
1298 Expr *InitList = Init.take();
1299 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1300 MultiExprArg(&InitList, 1),
1301 SourceLocation());
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001302 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001303 BalancedDelimiterTracker T(*this, tok::l_paren);
1304 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001305
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001306 ExprVector Exprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001307 CommaLocsTy CommaLocs;
1308
1309 if (Tok.isNot(tok::r_paren)) {
1310 if (ParseExpressionList(Exprs, CommaLocs)) {
1311 SkipUntil(tok::r_paren);
1312 return ExprError();
1313 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001314 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001315
1316 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001317 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001318
1319 // TypeRep could be null, if it references an invalid typedef.
1320 if (!TypeRep)
1321 return ExprError();
1322
1323 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1324 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001325 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001326 Exprs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001327 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001328 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001329}
1330
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001331/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001332///
1333/// condition:
1334/// expression
1335/// type-specifier-seq declarator '=' assignment-expression
Richard Smith0635aa72012-02-22 06:49:09 +00001336/// [C++11] type-specifier-seq declarator '=' initializer-clause
1337/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001338/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1339/// '=' assignment-expression
1340///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001341/// \param ExprOut if the condition was parsed as an expression, the parsed
1342/// expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001343///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001344/// \param DeclOut if the condition was parsed as a declaration, the parsed
1345/// declaration.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001346///
Douglas Gregor586596f2010-05-06 17:25:47 +00001347/// \param Loc The location of the start of the statement that requires this
1348/// condition, e.g., the "for" in a for loop.
1349///
1350/// \param ConvertToBoolean Whether the condition expression should be
1351/// converted to a boolean value.
1352///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001353/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001354bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1355 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001356 SourceLocation Loc,
1357 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001358 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001359 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001360 cutOffParsing();
1361 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001362 }
1363
Sean Hunt2edf0a22012-06-23 05:07:58 +00001364 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001365 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001366
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001367 if (!isCXXConditionDeclaration()) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001368 ProhibitAttributes(attrs);
1369
Douglas Gregor586596f2010-05-06 17:25:47 +00001370 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001371 ExprOut = ParseExpression(); // expression
1372 DeclOut = 0;
1373 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001374 return true;
1375
1376 // If required, convert to a boolean value.
1377 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001378 ExprOut
1379 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1380 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001381 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001382
1383 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001384 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001385 ParseSpecifierQualifierList(DS);
1386
1387 // declarator
1388 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1389 ParseDeclarator(DeclaratorInfo);
1390
1391 // simple-asm-expr[opt]
1392 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001393 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001394 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001395 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001396 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001397 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001398 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001399 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001400 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001401 }
1402
1403 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001404 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001405
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001406 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001407 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001408 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001409 DeclOut = Dcl.get();
1410 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001411
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001412 // '=' assignment-expression
Richard Trieud6c7c672012-01-18 22:54:52 +00001413 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith0635aa72012-02-22 06:49:09 +00001414 bool CopyInitialization = isTokenEqualOrEqualTypo();
1415 if (CopyInitialization)
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001416 ConsumeToken();
Richard Smith0635aa72012-02-22 06:49:09 +00001417
1418 ExprResult InitExpr = ExprError();
Richard Smith80ad52f2013-01-02 11:42:31 +00001419 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith0635aa72012-02-22 06:49:09 +00001420 Diag(Tok.getLocation(),
1421 diag::warn_cxx98_compat_generalized_initializer_lists);
1422 InitExpr = ParseBraceInitializer();
1423 } else if (CopyInitialization) {
1424 InitExpr = ParseAssignmentExpression();
1425 } else if (Tok.is(tok::l_paren)) {
1426 // This was probably an attempt to initialize the variable.
1427 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1428 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1429 RParen = ConsumeParen();
1430 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1431 diag::err_expected_init_in_condition_lparen)
1432 << SourceRange(LParen, RParen);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001433 } else {
Richard Smith0635aa72012-02-22 06:49:09 +00001434 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1435 diag::err_expected_init_in_condition);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001436 }
Richard Smith0635aa72012-02-22 06:49:09 +00001437
1438 if (!InitExpr.isInvalid())
1439 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
1440 DS.getTypeSpecType() == DeclSpec::TST_auto);
1441
Douglas Gregor586596f2010-05-06 17:25:47 +00001442 // FIXME: Build a reference to this declaration? Convert it to bool?
1443 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001444
1445 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001446
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001447 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001448}
1449
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001450/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1451/// This should only be called when the current token is known to be part of
1452/// simple-type-specifier.
1453///
1454/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001455/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001456/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1457/// char
1458/// wchar_t
1459/// bool
1460/// short
1461/// int
1462/// long
1463/// signed
1464/// unsigned
1465/// float
1466/// double
1467/// void
1468/// [GNU] typeof-specifier
1469/// [C++0x] auto [TODO]
1470///
1471/// type-name:
1472/// class-name
1473/// enum-name
1474/// typedef-name
1475///
1476void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1477 DS.SetRangeStart(Tok.getLocation());
1478 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001479 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001480 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001482 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001483 case tok::identifier: // foo::bar
1484 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001485 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001486 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001487 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001488
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001489 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001490 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001491 if (getTypeAnnotation(Tok))
1492 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1493 getTypeAnnotation(Tok));
1494 else
1495 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001496
1497 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1498 ConsumeToken();
1499
1500 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1501 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1502 // Objective-C interface. If we don't have Objective-C or a '<', this is
1503 // just a normal reference to a typedef name.
David Blaikie4e4d0842012-03-11 07:00:24 +00001504 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001505 ParseObjCProtocolQualifiers(DS);
1506
1507 DS.Finish(Diags, PP);
1508 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001511 // builtin types
1512 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001513 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001514 break;
1515 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001516 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001517 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001518 case tok::kw___int64:
1519 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1520 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001521 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001522 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001523 break;
1524 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001525 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001526 break;
1527 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001528 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001529 break;
1530 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001531 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001532 break;
1533 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001534 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001535 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00001536 case tok::kw___int128:
1537 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1538 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001539 case tok::kw_half:
1540 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1541 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001542 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001543 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001544 break;
1545 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001546 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001547 break;
1548 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001549 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001550 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001551 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001552 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001553 break;
1554 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001555 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001556 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001557 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001558 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001559 break;
David Blaikie5e089fe2012-01-24 05:47:35 +00001560 case tok::annot_decltype:
1561 case tok::kw_decltype:
1562 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1563 return DS.Finish(Diags, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001565 // GNU typeof support.
1566 case tok::kw_typeof:
1567 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001568 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001569 return;
1570 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001571 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001572 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1573 else
1574 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001575 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001576 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001577}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001578
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001579/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1580/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1581/// e.g., "const short int". Note that the DeclSpec is *not* finished
1582/// by parsing the type-specifier-seq, because these sequences are
1583/// typically followed by some form of declarator. Returns true and
1584/// emits diagnostics if this is not a type-specifier-seq, false
1585/// otherwise.
1586///
1587/// type-specifier-seq: [C++ 8.1]
1588/// type-specifier type-specifier-seq[opt]
1589///
1590bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smith69730c12012-03-12 07:56:15 +00001591 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor396a9f22010-02-24 23:13:13 +00001592 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001593 return false;
1594}
1595
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001596/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1597/// some form.
1598///
1599/// This routine is invoked when a '<' is encountered after an identifier or
1600/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1601/// whether the unqualified-id is actually a template-id. This routine will
1602/// then parse the template arguments and form the appropriate template-id to
1603/// return to the caller.
1604///
1605/// \param SS the nested-name-specifier that precedes this template-id, if
1606/// we're actually parsing a qualified-id.
1607///
1608/// \param Name for constructor and destructor names, this is the actual
1609/// identifier that may be a template-name.
1610///
1611/// \param NameLoc the location of the class-name in a constructor or
1612/// destructor.
1613///
1614/// \param EnteringContext whether we're entering the scope of the
1615/// nested-name-specifier.
1616///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001617/// \param ObjectType if this unqualified-id occurs within a member access
1618/// expression, the type of the base object whose member is being accessed.
1619///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001620/// \param Id as input, describes the template-name or operator-function-id
1621/// that precedes the '<'. If template arguments were parsed successfully,
1622/// will be updated with the template-id.
1623///
Douglas Gregord4dca082010-02-24 18:44:31 +00001624/// \param AssumeTemplateId When true, this routine will assume that the name
1625/// refers to a template without performing name lookup to verify.
1626///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001627/// \returns true if a parse error occurred, false otherwise.
1628bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001629 SourceLocation TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001630 IdentifierInfo *Name,
1631 SourceLocation NameLoc,
1632 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001633 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001634 UnqualifiedId &Id,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001635 bool AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001636 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1637 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001638
1639 TemplateTy Template;
1640 TemplateNameKind TNK = TNK_Non_template;
1641 switch (Id.getKind()) {
1642 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001643 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001644 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001645 if (AssumeTemplateId) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001646 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001647 Id, ObjectType, EnteringContext,
1648 Template);
1649 if (TNK == TNK_Non_template)
1650 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001651 } else {
1652 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001653 TNK = Actions.isTemplateName(getCurScope(), SS,
1654 TemplateKWLoc.isValid(), Id,
1655 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001656 MemberOfUnknownSpecialization);
1657
1658 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1659 ObjectType && IsTemplateArgumentList()) {
1660 // We have something like t->getAs<T>(), where getAs is a
1661 // member of an unknown specialization. However, this will only
1662 // parse correctly as a template, so suggest the keyword 'template'
1663 // before 'getAs' and treat this as a dependent template name.
1664 std::string Name;
1665 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1666 Name = Id.Identifier->getName();
1667 else {
1668 Name = "operator ";
1669 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1670 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1671 else
1672 Name += Id.Identifier->getName();
1673 }
1674 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1675 << Name
1676 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001677 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1678 SS, TemplateKWLoc, Id,
1679 ObjectType, EnteringContext,
1680 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001681 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001682 return true;
1683 }
1684 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001685 break;
1686
Douglas Gregor014e88d2009-11-03 23:16:33 +00001687 case UnqualifiedId::IK_ConstructorName: {
1688 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001689 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001690 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001691 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1692 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001693 EnteringContext, Template,
1694 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001695 break;
1696 }
1697
Douglas Gregor014e88d2009-11-03 23:16:33 +00001698 case UnqualifiedId::IK_DestructorName: {
1699 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001700 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001701 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001702 if (ObjectType) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001703 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1704 SS, TemplateKWLoc, TemplateName,
1705 ObjectType, EnteringContext,
1706 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001707 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001708 return true;
1709 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001710 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1711 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001712 EnteringContext, Template,
1713 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001714
John McCallb3d87482010-08-24 05:47:05 +00001715 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001716 Diag(NameLoc, diag::err_destructor_template_id)
1717 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001718 return true;
1719 }
1720 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001721 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001722 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001723
1724 default:
1725 return false;
1726 }
1727
1728 if (TNK == TNK_Non_template)
1729 return false;
1730
1731 // Parse the enclosed template argument list.
1732 SourceLocation LAngleLoc, RAngleLoc;
1733 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001734 if (Tok.is(tok::less) &&
1735 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001736 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001737 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001738 RAngleLoc))
1739 return true;
1740
1741 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001742 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1743 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001744 // Form a parsed representation of the template-id to be stored in the
1745 // UnqualifiedId.
1746 TemplateIdAnnotation *TemplateId
Benjamin Kramer13bb7012012-04-14 12:14:03 +00001747 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001748
1749 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1750 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001751 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001752 TemplateId->TemplateNameLoc = Id.StartLocation;
1753 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001754 TemplateId->Name = 0;
1755 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1756 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001757 }
1758
Douglas Gregor059101f2011-03-02 00:47:37 +00001759 TemplateId->SS = SS;
Benjamin Kramer2b28bf12012-02-19 23:37:39 +00001760 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall2b5289b2010-08-23 07:28:44 +00001761 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001762 TemplateId->Kind = TNK;
1763 TemplateId->LAngleLoc = LAngleLoc;
1764 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001765 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001766 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001767 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001768 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001769
1770 Id.setTemplateId(TemplateId);
1771 return false;
1772 }
1773
1774 // Bundle the template arguments together.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001775 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001776
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001777 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001778 TypeResult Type
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001779 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1780 Template, NameLoc,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001781 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1782 /*IsCtorOrDtorName=*/true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001783 if (Type.isInvalid())
1784 return true;
1785
1786 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1787 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1788 else
1789 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1790
1791 return false;
1792}
1793
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001794/// \brief Parse an operator-function-id or conversion-function-id as part
1795/// of a C++ unqualified-id.
1796///
1797/// This routine is responsible only for parsing the operator-function-id or
1798/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001799///
1800/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001801/// operator-function-id: [C++ 13.5]
1802/// 'operator' operator
1803///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001804/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001805/// new delete new[] delete[]
1806/// + - * / % ^ & | ~
1807/// ! = < > += -= *= /= %=
1808/// ^= &= |= << >> >>= <<= == !=
1809/// <= >= && || ++ -- , ->* ->
1810/// () []
1811///
1812/// conversion-function-id: [C++ 12.3.2]
1813/// operator conversion-type-id
1814///
1815/// conversion-type-id:
1816/// type-specifier-seq conversion-declarator[opt]
1817///
1818/// conversion-declarator:
1819/// ptr-operator conversion-declarator[opt]
1820/// \endcode
1821///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001822/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001823/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1824///
1825/// \param EnteringContext whether we are entering the scope of the
1826/// nested-name-specifier.
1827///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001828/// \param ObjectType if this unqualified-id occurs within a member access
1829/// expression, the type of the base object whose member is being accessed.
1830///
1831/// \param Result on a successful parse, contains the parsed unqualified-id.
1832///
1833/// \returns true if parsing fails, false otherwise.
1834bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001835 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001836 UnqualifiedId &Result) {
1837 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1838
1839 // Consume the 'operator' keyword.
1840 SourceLocation KeywordLoc = ConsumeToken();
1841
1842 // Determine what kind of operator name we have.
1843 unsigned SymbolIdx = 0;
1844 SourceLocation SymbolLocations[3];
1845 OverloadedOperatorKind Op = OO_None;
1846 switch (Tok.getKind()) {
1847 case tok::kw_new:
1848 case tok::kw_delete: {
1849 bool isNew = Tok.getKind() == tok::kw_new;
1850 // Consume the 'new' or 'delete'.
1851 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith6ee326a2012-04-10 01:32:12 +00001852 // Check for array new/delete.
1853 if (Tok.is(tok::l_square) &&
Richard Smith80ad52f2013-01-02 11:42:31 +00001854 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001855 // Consume the '[' and ']'.
1856 BalancedDelimiterTracker T(*this, tok::l_square);
1857 T.consumeOpen();
1858 T.consumeClose();
1859 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001860 return true;
1861
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001862 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1863 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001864 Op = isNew? OO_Array_New : OO_Array_Delete;
1865 } else {
1866 Op = isNew? OO_New : OO_Delete;
1867 }
1868 break;
1869 }
1870
1871#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1872 case tok::Token: \
1873 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1874 Op = OO_##Name; \
1875 break;
1876#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1877#include "clang/Basic/OperatorKinds.def"
1878
1879 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001880 // Consume the '(' and ')'.
1881 BalancedDelimiterTracker T(*this, tok::l_paren);
1882 T.consumeOpen();
1883 T.consumeClose();
1884 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001885 return true;
1886
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001887 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1888 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001889 Op = OO_Call;
1890 break;
1891 }
1892
1893 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001894 // Consume the '[' and ']'.
1895 BalancedDelimiterTracker T(*this, tok::l_square);
1896 T.consumeOpen();
1897 T.consumeClose();
1898 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001899 return true;
1900
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001901 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1902 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001903 Op = OO_Subscript;
1904 break;
1905 }
1906
1907 case tok::code_completion: {
1908 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001909 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001910 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001911 // Don't try to parse any further.
1912 return true;
1913 }
1914
1915 default:
1916 break;
1917 }
1918
1919 if (Op != OO_None) {
1920 // We have parsed an operator-function-id.
1921 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1922 return false;
1923 }
Sean Hunt0486d742009-11-28 04:44:28 +00001924
1925 // Parse a literal-operator-id.
1926 //
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001927 // literal-operator-id: C++11 [over.literal]
1928 // operator string-literal identifier
1929 // operator user-defined-string-literal
Sean Hunt0486d742009-11-28 04:44:28 +00001930
Richard Smith80ad52f2013-01-02 11:42:31 +00001931 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith7fe62082011-10-15 05:09:34 +00001932 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001933
Richard Smith33762772012-03-08 23:06:02 +00001934 SourceLocation DiagLoc;
1935 unsigned DiagId = 0;
1936
1937 // We're past translation phase 6, so perform string literal concatenation
1938 // before checking for "".
1939 llvm::SmallVector<Token, 4> Toks;
1940 llvm::SmallVector<SourceLocation, 4> TokLocs;
1941 while (isTokenStringLiteral()) {
1942 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001943 // C++11 [over.literal]p1:
1944 // The string-literal or user-defined-string-literal in a
1945 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith33762772012-03-08 23:06:02 +00001946 DiagLoc = Tok.getLocation();
1947 DiagId = diag::err_literal_operator_string_prefix;
1948 }
1949 Toks.push_back(Tok);
1950 TokLocs.push_back(ConsumeStringToken());
1951 }
1952
1953 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
1954 if (Literal.hadError)
1955 return true;
1956
1957 // Grab the literal operator's suffix, which will be either the next token
1958 // or a ud-suffix from the string literal.
1959 IdentifierInfo *II = 0;
1960 SourceLocation SuffixLoc;
1961 if (!Literal.getUDSuffix().empty()) {
1962 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
1963 SuffixLoc =
1964 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
1965 Literal.getUDSuffixOffset(),
David Blaikie4e4d0842012-03-11 07:00:24 +00001966 PP.getSourceManager(), getLangOpts());
Richard Smith33762772012-03-08 23:06:02 +00001967 } else if (Tok.is(tok::identifier)) {
1968 II = Tok.getIdentifierInfo();
1969 SuffixLoc = ConsumeToken();
1970 TokLocs.push_back(SuffixLoc);
1971 } else {
Sean Hunt0486d742009-11-28 04:44:28 +00001972 Diag(Tok.getLocation(), diag::err_expected_ident);
1973 return true;
1974 }
1975
Richard Smith33762772012-03-08 23:06:02 +00001976 // The string literal must be empty.
1977 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001978 // C++11 [over.literal]p1:
1979 // The string-literal or user-defined-string-literal in a
1980 // literal-operator-id shall [...] contain no characters
1981 // other than the implicit terminating '\0'.
Richard Smith33762772012-03-08 23:06:02 +00001982 DiagLoc = TokLocs.front();
1983 DiagId = diag::err_literal_operator_string_not_empty;
1984 }
1985
1986 if (DiagId) {
1987 // This isn't a valid literal-operator-id, but we think we know
1988 // what the user meant. Tell them what they should have written.
1989 llvm::SmallString<32> Str;
1990 Str += "\"\" ";
1991 Str += II->getName();
1992 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
1993 SourceRange(TokLocs.front(), TokLocs.back()), Str);
1994 }
1995
1996 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Sean Hunt3e518bd2009-11-29 07:34:05 +00001997 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001998 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001999
2000 // Parse a conversion-function-id.
2001 //
2002 // conversion-function-id: [C++ 12.3.2]
2003 // operator conversion-type-id
2004 //
2005 // conversion-type-id:
2006 // type-specifier-seq conversion-declarator[opt]
2007 //
2008 // conversion-declarator:
2009 // ptr-operator conversion-declarator[opt]
2010
2011 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00002012 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00002013 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002014 return true;
2015
2016 // Parse the conversion-declarator, which is merely a sequence of
2017 // ptr-operators.
2018 Declarator D(DS, Declarator::TypeNameContext);
2019 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2020
2021 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00002022 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002023 if (Ty.isInvalid())
2024 return true;
2025
2026 // Note that this is a conversion-function-id.
2027 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2028 D.getSourceRange().getEnd());
2029 return false;
2030}
2031
2032/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2033/// name of an entity.
2034///
2035/// \code
2036/// unqualified-id: [C++ expr.prim.general]
2037/// identifier
2038/// operator-function-id
2039/// conversion-function-id
2040/// [C++0x] literal-operator-id [TODO]
2041/// ~ class-name
2042/// template-id
2043///
2044/// \endcode
2045///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00002046/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002047/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2048///
2049/// \param EnteringContext whether we are entering the scope of the
2050/// nested-name-specifier.
2051///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002052/// \param AllowDestructorName whether we allow parsing of a destructor name.
2053///
2054/// \param AllowConstructorName whether we allow parsing a constructor name.
2055///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00002056/// \param ObjectType if this unqualified-id occurs within a member access
2057/// expression, the type of the base object whose member is being accessed.
2058///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002059/// \param Result on a successful parse, contains the parsed unqualified-id.
2060///
2061/// \returns true if parsing fails, false otherwise.
2062bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2063 bool AllowDestructorName,
2064 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002065 ParsedType ObjectType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002066 SourceLocation& TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002067 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00002068
2069 // Handle 'A::template B'. This is for template-ids which have not
2070 // already been annotated by ParseOptionalCXXScopeSpecifier().
2071 bool TemplateSpecified = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002072 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002073 (ObjectType || SS.isSet())) {
2074 TemplateSpecified = true;
2075 TemplateKWLoc = ConsumeToken();
2076 }
2077
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002078 // unqualified-id:
2079 // identifier
2080 // template-id (when it hasn't already been annotated)
2081 if (Tok.is(tok::identifier)) {
2082 // Consume the identifier.
2083 IdentifierInfo *Id = Tok.getIdentifierInfo();
2084 SourceLocation IdLoc = ConsumeToken();
2085
David Blaikie4e4d0842012-03-11 07:00:24 +00002086 if (!getLangOpts().CPlusPlus) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002087 // If we're not in C++, only identifiers matter. Record the
2088 // identifier and return.
2089 Result.setIdentifier(Id, IdLoc);
2090 return false;
2091 }
2092
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002093 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002094 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002095 // We have parsed a constructor name.
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002096 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2097 &SS, false, false,
2098 ParsedType(),
2099 /*IsCtorOrDtorName=*/true,
2100 /*NonTrivialTypeSourceInfo=*/true);
2101 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002102 } else {
2103 // We have parsed an identifier.
2104 Result.setIdentifier(Id, IdLoc);
2105 }
2106
2107 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00002108 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002109 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2110 EnteringContext, ObjectType,
2111 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002112
2113 return false;
2114 }
2115
2116 // unqualified-id:
2117 // template-id (already parsed and annotated)
2118 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002119 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002120
2121 // If the template-name names the current class, then this is a constructor
2122 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002123 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002124 if (SS.isSet()) {
2125 // C++ [class.qual]p2 specifies that a qualified template-name
2126 // is taken as the constructor name where a constructor can be
2127 // declared. Thus, the template arguments are extraneous, so
2128 // complain about them and remove them entirely.
2129 Diag(TemplateId->TemplateNameLoc,
2130 diag::err_out_of_line_constructor_template_id)
2131 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00002132 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002133 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002134 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2135 TemplateId->TemplateNameLoc,
2136 getCurScope(),
2137 &SS, false, false,
2138 ParsedType(),
2139 /*IsCtorOrDtorName=*/true,
2140 /*NontrivialTypeSourceInfo=*/true);
2141 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002142 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002143 ConsumeToken();
2144 return false;
2145 }
2146
2147 Result.setConstructorTemplateId(TemplateId);
2148 ConsumeToken();
2149 return false;
2150 }
2151
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002152 // We have already parsed a template-id; consume the annotation token as
2153 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002154 Result.setTemplateId(TemplateId);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002155 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002156 ConsumeToken();
2157 return false;
2158 }
2159
2160 // unqualified-id:
2161 // operator-function-id
2162 // conversion-function-id
2163 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002164 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002165 return true;
2166
Sean Hunte6252d12009-11-28 08:58:14 +00002167 // If we have an operator-function-id or a literal-operator-id and the next
2168 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002169 //
2170 // template-id:
2171 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00002172 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2173 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002174 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002175 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2176 0, SourceLocation(),
2177 EnteringContext, ObjectType,
2178 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002179
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002180 return false;
2181 }
2182
David Blaikie4e4d0842012-03-11 07:00:24 +00002183 if (getLangOpts().CPlusPlus &&
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002184 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002185 // C++ [expr.unary.op]p10:
2186 // There is an ambiguity in the unary-expression ~X(), where X is a
2187 // class-name. The ambiguity is resolved in favor of treating ~ as a
2188 // unary complement rather than treating ~X as referring to a destructor.
2189
2190 // Parse the '~'.
2191 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00002192
2193 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2194 DeclSpec DS(AttrFactory);
2195 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2196 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2197 Result.setDestructorName(TildeLoc, Type, EndLoc);
2198 return false;
2199 }
2200 return true;
2201 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002202
2203 // Parse the class-name.
2204 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00002205 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002206 return true;
2207 }
2208
2209 // Parse the class-name (or template-name in a simple-template-id).
2210 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2211 SourceLocation ClassNameLoc = ConsumeToken();
2212
Douglas Gregor0278e122010-05-05 05:58:24 +00002213 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002214 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002215 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2216 ClassName, ClassNameLoc,
2217 EnteringContext, ObjectType,
2218 Result, TemplateSpecified);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002219 }
2220
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002221 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002222 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2223 ClassNameLoc, getCurScope(),
2224 SS, ObjectType,
2225 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002226 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002227 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002228
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002229 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002230 return false;
2231 }
2232
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002233 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikie4e4d0842012-03-11 07:00:24 +00002234 << getLangOpts().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002235 return true;
2236}
2237
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002238/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2239/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002240///
Chris Lattner59232d32009-01-04 21:25:24 +00002241/// This method is called to parse the new expression after the optional :: has
2242/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2243/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002244///
2245/// new-expression:
2246/// '::'[opt] 'new' new-placement[opt] new-type-id
2247/// new-initializer[opt]
2248/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2249/// new-initializer[opt]
2250///
2251/// new-placement:
2252/// '(' expression-list ')'
2253///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002254/// new-type-id:
2255/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002256/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002257///
2258/// new-declarator:
2259/// ptr-operator new-declarator[opt]
2260/// direct-new-declarator
2261///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002262/// new-initializer:
2263/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002264/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002265///
John McCall60d7b3a2010-08-24 06:29:42 +00002266ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002267Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2268 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2269 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002270
2271 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2272 // second form of new-expression. It can't be a new-type-id.
2273
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002274 ExprVector PlacementArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002275 SourceLocation PlacementLParen, PlacementRParen;
2276
Douglas Gregor4bd40312010-07-13 15:54:32 +00002277 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002278 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002279 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002280 if (Tok.is(tok::l_paren)) {
2281 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002282 BalancedDelimiterTracker T(*this, tok::l_paren);
2283 T.consumeOpen();
2284 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002285 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2286 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002287 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002288 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002289
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002290 T.consumeClose();
2291 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002292 if (PlacementRParen.isInvalid()) {
2293 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002294 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002295 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002296
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002297 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002298 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002299 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002300 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002301 } else {
2302 // We still need the type.
2303 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002304 BalancedDelimiterTracker T(*this, tok::l_paren);
2305 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002306 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002307 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002308 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002309 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002310 T.consumeClose();
2311 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002312 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002313 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002314 if (ParseCXXTypeSpecifierSeq(DS))
2315 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002316 else {
2317 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002318 ParseDeclaratorInternal(DeclaratorInfo,
2319 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002320 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002321 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002322 }
2323 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002324 // A new-type-id is a simplified type-id, where essentially the
2325 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002326 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002327 if (ParseCXXTypeSpecifierSeq(DS))
2328 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002329 else {
2330 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002331 ParseDeclaratorInternal(DeclaratorInfo,
2332 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002333 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002334 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002335 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002336 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002337 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002338 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002339
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002340 ExprResult Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002341
2342 if (Tok.is(tok::l_paren)) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002343 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002344 ExprVector ConstructorArgs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002345 BalancedDelimiterTracker T(*this, tok::l_paren);
2346 T.consumeOpen();
2347 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002348 if (Tok.isNot(tok::r_paren)) {
2349 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002350 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2351 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002352 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002353 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002354 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002355 T.consumeClose();
2356 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002357 if (ConstructorRParen.isInvalid()) {
2358 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002359 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002360 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002361 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2362 ConstructorRParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002363 ConstructorArgs);
Richard Smith80ad52f2013-01-02 11:42:31 +00002364 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00002365 Diag(Tok.getLocation(),
2366 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002367 Initializer = ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002368 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002369 if (Initializer.isInvalid())
2370 return Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002371
Sebastian Redlf53597f2009-03-15 17:47:39 +00002372 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002373 PlacementArgs, PlacementRParen,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002374 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002375}
2376
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002377/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2378/// passed to ParseDeclaratorInternal.
2379///
2380/// direct-new-declarator:
2381/// '[' expression ']'
2382/// direct-new-declarator '[' constant-expression ']'
2383///
Chris Lattner59232d32009-01-04 21:25:24 +00002384void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002385 // Parse the array dimensions.
2386 bool first = true;
2387 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00002388 // An array-size expression can't start with a lambda.
2389 if (CheckProhibitedCXX11Attribute())
2390 continue;
2391
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002392 BalancedDelimiterTracker T(*this, tok::l_square);
2393 T.consumeOpen();
2394
John McCall60d7b3a2010-08-24 06:29:42 +00002395 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002396 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002397 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002398 // Recover
2399 SkipUntil(tok::r_square);
2400 return;
2401 }
2402 first = false;
2403
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002404 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002405
Bill Wendlingad017fa2012-12-20 19:22:21 +00002406 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith6ee326a2012-04-10 01:32:12 +00002407 ParsedAttributes Attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002408 MaybeParseCXX11Attributes(Attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00002409
John McCall0b7e6782011-03-24 11:26:52 +00002410 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002411 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002412 Size.release(),
2413 T.getOpenLocation(),
2414 T.getCloseLocation()),
Richard Smith6ee326a2012-04-10 01:32:12 +00002415 Attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002416
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002417 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002418 return;
2419 }
2420}
2421
2422/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2423/// This ambiguity appears in the syntax of the C++ new operator.
2424///
2425/// new-expression:
2426/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2427/// new-initializer[opt]
2428///
2429/// new-placement:
2430/// '(' expression-list ')'
2431///
John McCallca0408f2010-08-23 06:44:23 +00002432bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002433 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002434 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002435 // The '(' was already consumed.
2436 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002437 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002438 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002439 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002440 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002441 }
2442
2443 // It's not a type, it has to be an expression list.
2444 // Discard the comma locations - ActOnCXXNew has enough parameters.
2445 CommaLocsTy CommaLocs;
2446 return ParseExpressionList(PlacementArgs, CommaLocs);
2447}
2448
2449/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2450/// to free memory allocated by new.
2451///
Chris Lattner59232d32009-01-04 21:25:24 +00002452/// This method is called to parse the 'delete' expression after the optional
2453/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2454/// and "Start" is its location. Otherwise, "Start" is the location of the
2455/// 'delete' token.
2456///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002457/// delete-expression:
2458/// '::'[opt] 'delete' cast-expression
2459/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002460ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002461Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2462 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2463 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002464
2465 // Array delete?
2466 bool ArrayDelete = false;
Richard Smith6ee326a2012-04-10 01:32:12 +00002467 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith950435c2012-08-09 19:01:51 +00002468 // C++11 [expr.delete]p1:
2469 // Whenever the delete keyword is followed by empty square brackets, it
2470 // shall be interpreted as [array delete].
2471 // [Footnote: A lambda expression with a lambda-introducer that consists
2472 // of empty square brackets can follow the delete keyword if
2473 // the lambda expression is enclosed in parentheses.]
2474 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2475 // lambda-introducer.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002476 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002477 BalancedDelimiterTracker T(*this, tok::l_square);
2478
2479 T.consumeOpen();
2480 T.consumeClose();
2481 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002482 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002483 }
2484
John McCall60d7b3a2010-08-24 06:29:42 +00002485 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002486 if (Operand.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002487 return Operand;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002488
John McCall9ae2f072010-08-23 23:25:46 +00002489 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002490}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002491
Mike Stump1eb44332009-09-09 15:08:12 +00002492static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002493 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002494 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002495 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002496 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002497 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002498 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002499 case tok::kw___has_trivial_constructor:
2500 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002501 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002502 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2503 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2504 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002505 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2506 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002507 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002508 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2509 case tok::kw___is_compound: return UTT_IsCompound;
2510 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002511 case tok::kw___is_empty: return UTT_IsEmpty;
2512 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002513 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002514 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2515 case tok::kw___is_function: return UTT_IsFunction;
2516 case tok::kw___is_fundamental: return UTT_IsFundamental;
2517 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallea30e2f2012-09-25 07:32:49 +00002518 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002519 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2520 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2521 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2522 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2523 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002524 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002525 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002526 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002527 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002528 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002529 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002530 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2531 case tok::kw___is_scalar: return UTT_IsScalar;
2532 case tok::kw___is_signed: return UTT_IsSigned;
2533 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2534 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002535 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002536 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002537 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2538 case tok::kw___is_void: return UTT_IsVoid;
2539 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002540 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002541}
2542
2543static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2544 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002545 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002546 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002547 case tok::kw___is_convertible: return BTT_IsConvertible;
2548 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002549 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002550 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002551 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002552 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002553}
2554
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002555static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2556 switch (kind) {
2557 default: llvm_unreachable("Not a known type trait");
2558 case tok::kw___is_trivially_constructible:
2559 return TT_IsTriviallyConstructible;
2560 }
2561}
2562
John Wiegley21ff2e52011-04-28 00:16:57 +00002563static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2564 switch(kind) {
2565 default: llvm_unreachable("Not a known binary type trait");
2566 case tok::kw___array_rank: return ATT_ArrayRank;
2567 case tok::kw___array_extent: return ATT_ArrayExtent;
2568 }
2569}
2570
John Wiegley55262202011-04-25 06:54:41 +00002571static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2572 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002573 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002574 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2575 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2576 }
2577}
2578
Sebastian Redl64b45f72009-01-05 20:52:13 +00002579/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2580/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2581/// templates.
2582///
2583/// primary-expression:
2584/// [GNU] unary-type-trait '(' type-id ')'
2585///
John McCall60d7b3a2010-08-24 06:29:42 +00002586ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002587 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2588 SourceLocation Loc = ConsumeToken();
2589
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002590 BalancedDelimiterTracker T(*this, tok::l_paren);
2591 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002592 return ExprError();
2593
2594 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2595 // there will be cryptic errors about mismatched parentheses and missing
2596 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002597 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002598
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002599 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002600
Douglas Gregor809070a2009-02-18 17:45:20 +00002601 if (Ty.isInvalid())
2602 return ExprError();
2603
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002604 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002605}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002606
Francois Pichet6ad6f282010-12-07 00:08:36 +00002607/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2608/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2609/// templates.
2610///
2611/// primary-expression:
2612/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2613///
2614ExprResult Parser::ParseBinaryTypeTrait() {
2615 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2616 SourceLocation Loc = ConsumeToken();
2617
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002618 BalancedDelimiterTracker T(*this, tok::l_paren);
2619 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002620 return ExprError();
2621
2622 TypeResult LhsTy = ParseTypeName();
2623 if (LhsTy.isInvalid()) {
2624 SkipUntil(tok::r_paren);
2625 return ExprError();
2626 }
2627
2628 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2629 SkipUntil(tok::r_paren);
2630 return ExprError();
2631 }
2632
2633 TypeResult RhsTy = ParseTypeName();
2634 if (RhsTy.isInvalid()) {
2635 SkipUntil(tok::r_paren);
2636 return ExprError();
2637 }
2638
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002639 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002640
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002641 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2642 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002643}
2644
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002645/// \brief Parse the built-in type-trait pseudo-functions that allow
2646/// implementation of the TR1/C++11 type traits templates.
2647///
2648/// primary-expression:
2649/// type-trait '(' type-id-seq ')'
2650///
2651/// type-id-seq:
2652/// type-id ...[opt] type-id-seq[opt]
2653///
2654ExprResult Parser::ParseTypeTrait() {
2655 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2656 SourceLocation Loc = ConsumeToken();
2657
2658 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2659 if (Parens.expectAndConsume(diag::err_expected_lparen))
2660 return ExprError();
2661
2662 llvm::SmallVector<ParsedType, 2> Args;
2663 do {
2664 // Parse the next type.
2665 TypeResult Ty = ParseTypeName();
2666 if (Ty.isInvalid()) {
2667 Parens.skipToEnd();
2668 return ExprError();
2669 }
2670
2671 // Parse the ellipsis, if present.
2672 if (Tok.is(tok::ellipsis)) {
2673 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2674 if (Ty.isInvalid()) {
2675 Parens.skipToEnd();
2676 return ExprError();
2677 }
2678 }
2679
2680 // Add this type to the list of arguments.
2681 Args.push_back(Ty.get());
2682
2683 if (Tok.is(tok::comma)) {
2684 ConsumeToken();
2685 continue;
2686 }
2687
2688 break;
2689 } while (true);
2690
2691 if (Parens.consumeClose())
2692 return ExprError();
2693
2694 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2695}
2696
John Wiegley21ff2e52011-04-28 00:16:57 +00002697/// ParseArrayTypeTrait - Parse the built-in array type-trait
2698/// pseudo-functions.
2699///
2700/// primary-expression:
2701/// [Embarcadero] '__array_rank' '(' type-id ')'
2702/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2703///
2704ExprResult Parser::ParseArrayTypeTrait() {
2705 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2706 SourceLocation Loc = ConsumeToken();
2707
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002708 BalancedDelimiterTracker T(*this, tok::l_paren);
2709 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002710 return ExprError();
2711
2712 TypeResult Ty = ParseTypeName();
2713 if (Ty.isInvalid()) {
2714 SkipUntil(tok::comma);
2715 SkipUntil(tok::r_paren);
2716 return ExprError();
2717 }
2718
2719 switch (ATT) {
2720 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002721 T.consumeClose();
2722 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2723 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002724 }
2725 case ATT_ArrayExtent: {
2726 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2727 SkipUntil(tok::r_paren);
2728 return ExprError();
2729 }
2730
2731 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002732 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002733
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002734 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2735 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002736 }
John Wiegley21ff2e52011-04-28 00:16:57 +00002737 }
David Blaikie30263482012-01-20 21:50:17 +00002738 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley21ff2e52011-04-28 00:16:57 +00002739}
2740
John Wiegley55262202011-04-25 06:54:41 +00002741/// ParseExpressionTrait - Parse built-in expression-trait
2742/// pseudo-functions like __is_lvalue_expr( xxx ).
2743///
2744/// primary-expression:
2745/// [Embarcadero] expression-trait '(' expression ')'
2746///
2747ExprResult Parser::ParseExpressionTrait() {
2748 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2749 SourceLocation Loc = ConsumeToken();
2750
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002751 BalancedDelimiterTracker T(*this, tok::l_paren);
2752 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002753 return ExprError();
2754
2755 ExprResult Expr = ParseExpression();
2756
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002757 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002758
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002759 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2760 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002761}
2762
2763
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002764/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2765/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2766/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002767ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002768Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002769 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002770 BalancedDelimiterTracker &Tracker) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002771 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002772 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2773 assert(isTypeIdInParens() && "Not a type-id!");
2774
John McCall60d7b3a2010-08-24 06:29:42 +00002775 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002776 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002777
2778 // We need to disambiguate a very ugly part of the C++ syntax:
2779 //
2780 // (T())x; - type-id
2781 // (T())*x; - type-id
2782 // (T())/x; - expression
2783 // (T()); - expression
2784 //
2785 // The bad news is that we cannot use the specialized tentative parser, since
2786 // it can only verify that the thing inside the parens can be parsed as
2787 // type-id, it is not useful for determining the context past the parens.
2788 //
2789 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002790 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002791 //
2792 // It uses a scheme similar to parsing inline methods. The parenthesized
2793 // tokens are cached, the context that follows is determined (possibly by
2794 // parsing a cast-expression), and then we re-introduce the cached tokens
2795 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002796
Mike Stump1eb44332009-09-09 15:08:12 +00002797 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002798 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002799
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002800 // Store the tokens of the parentheses. We will parse them after we determine
2801 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002802 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002803 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002804 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002805 return ExprError();
2806 }
2807
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002808 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002809 ParseAs = CompoundLiteral;
2810 } else {
2811 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002812 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2813 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2814 NotCastExpr = true;
2815 } else {
2816 // Try parsing the cast-expression that may follow.
2817 // If it is not a cast-expression, NotCastExpr will be true and no token
2818 // will be consumed.
2819 Result = ParseCastExpression(false/*isUnaryExpression*/,
2820 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002821 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002822 // type-id has priority.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002823 IsTypeCast);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002824 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002825
2826 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2827 // an expression.
2828 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002829 }
2830
Mike Stump1eb44332009-09-09 15:08:12 +00002831 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002832 Toks.push_back(Tok);
2833 // Re-enter the stored parenthesized tokens into the token stream, so we may
2834 // parse them now.
2835 PP.EnterTokenStream(Toks.data(), Toks.size(),
2836 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2837 // Drop the current token and bring the first cached one. It's the same token
2838 // as when we entered this function.
2839 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002840
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002841 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002842 // Parse the type declarator.
2843 DeclSpec DS(AttrFactory);
2844 ParseSpecifierQualifierList(DS);
2845 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2846 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002847
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002848 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002849 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002850
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002851 if (ParseAs == CompoundLiteral) {
2852 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002853 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002854 return ParseCompoundLiteralExpression(Ty.get(),
2855 Tracker.getOpenLocation(),
2856 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002857 }
Mike Stump1eb44332009-09-09 15:08:12 +00002858
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002859 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2860 assert(ParseAs == CastExpr);
2861
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002862 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002863 return ExprError();
2864
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002865 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002866 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002867 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2868 DeclaratorInfo, CastTy,
2869 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002870 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002871 }
Mike Stump1eb44332009-09-09 15:08:12 +00002872
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002873 // Not a compound literal, and not followed by a cast-expression.
2874 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002875
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002876 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002877 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002878 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002879 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2880 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002881
2882 // Match the ')'.
2883 if (Result.isInvalid()) {
2884 SkipUntil(tok::r_paren);
2885 return ExprError();
2886 }
Mike Stump1eb44332009-09-09 15:08:12 +00002887
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002888 Tracker.consumeClose();
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002889 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002890}