blob: 613b0bec5d98ed822cf34c789bfd6b3405bd4b8d [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner29375652006-12-04 18:06:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner29375652006-12-04 18:06:35 +000014#include "clang/Parse/Parser.h"
Douglas Gregor94a32472011-01-11 00:33:19 +000015#include "RAIIObjectsForParser.h"
Eli Friedmanc7c97142012-01-04 02:40:39 +000016#include "clang/Basic/PrettyStackTrace.h"
Richard Smith7d182a72012-03-08 23:06:02 +000017#include "clang/Lex/LiteralSupport.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
20#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
Douglas Gregor7861a802009-11-03 01:35:08 +000022#include "llvm/Support/ErrorHandling.h"
23
Chris Lattner29375652006-12-04 18:06:35 +000024using namespace clang;
25
Richard Smith55858492011-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 Blaikie83d382b2011-09-23 05:06:16 +000034 llvm_unreachable("Unknown type for digraph error message.");
Richard Smith55858492011-04-14 21:45:45 +000035 }
36}
37
38// Are the two tokens adjacent in the same source file?
Richard Smith7b3f3222012-06-18 06:11:04 +000039bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smith55858492011-04-14 21:45:45 +000040 SourceManager &SM = PP.getSourceManager();
41 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000042 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smith55858492011-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 Kyrtzidise6e67de2011-09-19 20:40:19 +000063 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smith55858492011-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 Trieu01fc0012011-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 Trieu02e25db2011-09-20 20:03:50 +000079 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu01fc0012011-09-19 19:01:00 +000080 return;
81
82 Token SecondToken = GetLookAheadToken(2);
Richard Smith7b3f3222012-06-18 06:11:04 +000083 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu01fc0012011-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 Trieu1f3ea7b2012-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 Weber6be9b252012-11-29 05:29:23 +0000102/// stream by removing the '(', and the matching ')' if found.
Richard Trieu1f3ea7b2012-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 Stump11289f42009-09-09 15:08:12 +0000138/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000139///
140/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000141/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000142/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-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 Gregorb7bfe792009-09-02 22:59:36 +0000151/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000152///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000153///
Mike Stump11289f42009-09-09 15:08:12 +0000154/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000155/// nested-name-specifier (or empty)
156///
Mike Stump11289f42009-09-09 15:08:12 +0000157/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-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 Gregore610ada2010-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 Gregor90d554e2010-02-21 18:36:56 +0000172/// member access expression, e.g., the \p T:: in \p p->T::m.
173///
John McCall1f476a12010-02-26 08:45:28 +0000174/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000175bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000176 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000177 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000178 bool *MayBePseudoDestructor,
179 bool IsTypename) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000180 assert(getLangOpts().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000181 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000182
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000183 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor869ad452011-02-24 17:54:50 +0000184 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
185 Tok.getAnnotationRange(),
186 SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000187 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +0000188 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000189 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000190
Douglas Gregor7f741122009-02-25 19:37:18 +0000191 bool HasScopeSpecifier = false;
192
Chris Lattner8a7d10d2009-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 Stump11289f42009-09-09 15:08:12 +0000198
Chris Lattner45ddec32009-01-05 00:13:00 +0000199 // '::' - Global scope qualifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000200 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
201 return true;
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000202
203 CheckForLParenAfterColonColon();
204
Douglas Gregor7f741122009-02-25 19:37:18 +0000205 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000206 }
207
Douglas Gregore610ada2010-02-24 18:44:31 +0000208 bool CheckForDestructor = false;
209 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
210 CheckForDestructor = true;
211 *MayBePseudoDestructor = false;
212 }
213
David Blaikie15a430a2011-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 Gregor7f741122009-02-25 19:37:18 +0000230 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000231 if (HasScopeSpecifier) {
232 // C++ [basic.lookup.classref]p5:
233 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000234 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000235 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000236 //
Douglas Gregorb7bfe792009-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 McCallba7bf592010-08-24 05:47:05 +0000242 ObjectType = ParsedType();
Douglas Gregor2436e712009-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 Gregor0be31a22010-07-02 17:43:08 +0000247 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidis7d94c922011-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 Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000252 SS.setEndLoc(Tok.getLocation());
253 cutOffParsing();
254 return true;
Douglas Gregor2436e712009-09-17 21:32:03 +0000255 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000256 }
Mike Stump11289f42009-09-09 15:08:12 +0000257
Douglas Gregor7f741122009-02-25 19:37:18 +0000258 // nested-name-specifier:
Chris Lattner0eed3a62009-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 Gregorb7bfe792009-09-02 22:59:36 +0000264 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000265 // nested-name-specifier, since they aren't allowed to start with
266 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000267 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000268 break;
269
Douglas Gregor120635b2009-11-11 16:39:34 +0000270 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000271 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000272
273 UnqualifiedId TemplateName;
274 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000275 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000276 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000277 ConsumeToken();
278 } else if (Tok.is(tok::kw_operator)) {
279 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000280 TemplateName)) {
281 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000282 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000283 }
Douglas Gregor71395fa2009-11-04 00:56:37 +0000284
Alexis Hunted0530f2009-11-28 08:58:14 +0000285 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
286 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000287 Diag(TemplateName.getSourceRange().getBegin(),
288 diag::err_id_after_template_in_nested_name_spec)
289 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000290 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000291 break;
292 }
293 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000294 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000295 break;
296 }
Mike Stump11289f42009-09-09 15:08:12 +0000297
Douglas Gregor120635b2009-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 Gregorbb119652010-06-16 23:00:59 +0000308 TemplateTy Template;
Abramo Bagnara7945c982012-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 Gregorbb119652010-06-16 23:00:59 +0000316 return true;
317 } else
John McCall1f476a12010-02-26 08:45:28 +0000318 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000319
Chris Lattner0eed3a62009-06-26 03:47:46 +0000320 continue;
321 }
Mike Stump11289f42009-09-09 15:08:12 +0000322
Douglas Gregor7f741122009-02-25 19:37:18 +0000323 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000324 // We have
Douglas Gregor7f741122009-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 Gregorb67535d2009-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 Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000331 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000332 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
333 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000334 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000335 }
336
Douglas Gregor8b6070b2011-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 Stump11289f42009-09-09 15:08:12 +0000342
David Blaikie8c045bc2011-11-07 03:30:03 +0000343 HasScopeSpecifier = true;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000344
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000345 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000346 TemplateId->NumArgs);
347
348 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000349 SS,
350 TemplateId->TemplateKWLoc,
Douglas Gregor8b6070b2011-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 Lattner704edfb2009-06-26 03:45:46 +0000362 }
Argyrios Kyrtzidis13935672011-05-03 18:45:38 +0000363
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000364 continue;
Douglas Gregor7f741122009-02-25 19:37:18 +0000365 }
366
Chris Lattnere2355f72009-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 Lattner1c428032009-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 Gregor90d554e2010-02-21 18:36:56 +0000383 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000384 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
385 Tok.getLocation(),
386 Next.getLocation(), ObjectType,
Douglas Gregor90d554e2010-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 Gregora771f462010-03-31 17:46:05 +0000393 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000394
395 // Recover as if the user wrote '::'.
396 Next.setKind(tok::coloncolon);
397 }
Chris Lattner1c428032009-12-07 01:36:53 +0000398 }
399
Chris Lattnere2355f72009-06-26 03:52:38 +0000400 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000401 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000402 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000403 II, ObjectType)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000404 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000405 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000406 }
407
Chris Lattnere2355f72009-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 Lattner1c428032009-12-07 01:36:53 +0000411 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
412 "NextToken() not working properly!");
Chris Lattnere2355f72009-06-26 03:52:38 +0000413 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000414
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000415 CheckForLParenAfterColonColon();
416
Douglas Gregor90c99722011-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 Lattnere2355f72009-06-26 03:52:38 +0000422 continue;
423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
Richard Trieu01fc0012011-09-19 19:01:00 +0000425 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smith55858492011-04-14 21:45:45 +0000426
Chris Lattnere2355f72009-06-26 03:52:38 +0000427 // nested-name-specifier:
428 // type-name '<'
429 if (Next.is(tok::less)) {
430 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000431 UnqualifiedId TemplateName;
432 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000433 bool MemberOfUnknownSpecialization;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000434 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000435 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000436 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000437 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000438 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000439 Template,
440 MemberOfUnknownSpecialization)) {
David Blaikie8c045bc2011-11-07 03:30:03 +0000441 // We have found a template name, so annotate this token
Chris Lattnere2355f72009-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 Gregor71395fa2009-11-04 00:56:37 +0000447 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000448 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
449 TemplateName, false))
John McCall1f476a12010-02-26 08:45:28 +0000450 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000451 continue;
Douglas Gregor20c38a72010-05-21 23:43:39 +0000452 }
453
454 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000455 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregor20c38a72010-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 Pichet4e7a2c02011-03-27 19:41:34 +0000460 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000461 if (getLangOpts().MicrosoftExt)
Francois Pichet93921652011-04-22 08:25:24 +0000462 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000463
464 Diag(Tok.getLocation(), DiagID)
Douglas Gregor20c38a72010-05-21 23:43:39 +0000465 << II.getName()
466 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
467
Douglas Gregorbb119652010-06-16 23:00:59 +0000468 if (TemplateNameKind TNK
Douglas Gregor0be31a22010-07-02 17:43:08 +0000469 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000470 SS, SourceLocation(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000471 TemplateName, ObjectType,
472 EnteringContext, Template)) {
473 // Consume the identifier.
474 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000475 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
476 TemplateName, false))
477 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000478 }
479 else
Douglas Gregor20c38a72010-05-21 23:43:39 +0000480 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000481
Douglas Gregor20c38a72010-05-21 23:43:39 +0000482 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000483 }
484 }
485
Douglas Gregor7f741122009-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 Kyrtzidis32a03792008-11-08 16:45:02 +0000489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Douglas Gregore610ada2010-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 McCall1f476a12010-02-26 08:45:28 +0000497 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000498}
499
500/// ParseCXXIdExpression - Handle id-expression.
501///
502/// id-expression:
503/// unqualified-id
504/// qualified-id
505///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000506/// qualified-id:
507/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
508/// '::' identifier
509/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000510/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000511///
Argyrios Kyrtzidis32a03792008-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 Redl3d3f75a2009-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 McCalldadc5752010-08-24 06:29:42 +0000542ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-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 Gregordf593fb2011-11-07 17:33:42 +0000548 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000549
550 SourceLocation TemplateKWLoc;
Douglas Gregora121b752009-11-03 16:56:39 +0000551 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000552 if (ParseUnqualifiedId(SS,
553 /*EnteringContext=*/false,
554 /*AllowDestructorName=*/false,
555 /*AllowConstructorName=*/false,
John McCallba7bf592010-08-24 05:47:05 +0000556 /*ObjectType=*/ ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000557 TemplateKWLoc,
Douglas Gregora121b752009-11-03 16:56:39 +0000558 Name))
559 return ExprError();
John McCalla9ee3252009-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 McCall8d08b9b2010-08-27 09:08:28 +0000563 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
564 isAddressOfOperand = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000565
566 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
567 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000568}
569
Douglas Gregordb0b9f12011-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 Friedmanc7c97142012-01-04 02:40:39 +0000609 SkipUntil(tok::l_brace);
610 SkipUntil(tok::r_brace);
611 return ExprError();
Douglas Gregordb0b9f12011-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 Smith2bf7fdb2013-01-02 11:42:31 +0000623 assert(getLangOpts().CPlusPlus11
Douglas Gregordb0b9f12011-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 Friedmanc7c97142012-01-04 02:40:39 +0000640 // If lookahead indicates an ObjC message send...
641 // [identifier identifier
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000642 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000643 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000644 }
645
Eli Friedmanc7c97142012-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 Gregordb0b9f12011-08-04 15:30:47 +0000652 LambdaIntroducer Intro;
653 if (TryParseLambdaIntroducer(Intro))
Eli Friedmanc7c97142012-01-04 02:40:39 +0000654 return ExprEmpty();
Douglas Gregordb0b9f12011-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 Gregord8c61782012-02-15 15:34:24 +0000661llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro){
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000662 typedef llvm::Optional<unsigned> DiagResult;
663
664 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000665 BalancedDelimiterTracker T(*this, tok::l_square);
666 T.consumeOpen();
667
668 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregordb0b9f12011-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 Gregora1bffa22012-02-10 17:46:20 +0000676 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000677 first = false;
678 } else if (Tok.is(tok::equal)) {
679 Intro.Default = LCD_ByCopy;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000680 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000681 first = false;
682 }
683
684 while (Tok.isNot(tok::r_square)) {
685 if (!first) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000686 if (Tok.isNot(tok::comma)) {
Douglas Gregor721b14d2012-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 Gregor2d8db8f2012-07-31 15:27:48 +0000691 if (Tok.is(tok::code_completion) &&
692 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
693 !Intro.Captures.empty())) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000694 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
695 /*AfterAmpersand=*/false);
696 ConsumeCodeCompletionToken();
697 break;
698 }
699
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000700 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregord8c61782012-02-15 15:34:24 +0000701 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000702 ConsumeToken();
703 }
704
Douglas Gregord8c61782012-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 Blaikiebbafb8a2012-03-11 07:00:24 +0000708 if (getLangOpts().ObjC1 && first)
Douglas Gregord8c61782012-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 Gregordb0b9f12011-08-04 15:30:47 +0000716
Douglas Gregord8c61782012-02-15 15:34:24 +0000717 first = false;
718
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000719 // Parse capture.
720 LambdaCaptureKind Kind = LCK_ByCopy;
721 SourceLocation Loc;
722 IdentifierInfo* Id = 0;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000723 SourceLocation EllipsisLoc;
724
Douglas Gregordb0b9f12011-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 Gregord8c61782012-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 Gregordb0b9f12011-08-04 15:30:47 +0000739 }
740
741 if (Tok.is(tok::identifier)) {
742 Id = Tok.getIdentifierInfo();
743 Loc = ConsumeToken();
Douglas Gregor3e308b12012-02-14 19:27:52 +0000744
745 if (Tok.is(tok::ellipsis))
746 EllipsisLoc = ConsumeToken();
Douglas Gregordb0b9f12011-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 Gregor3e308b12012-02-14 19:27:52 +0000757 Intro.addCapture(Kind, Loc, Id, EllipsisLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000758 }
759
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000760 T.consumeClose();
761 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000762
763 return DiagResult();
764}
765
Douglas Gregord8c61782012-02-15 15:34:24 +0000766/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregordb0b9f12011-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 Friedmanc7c97142012-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 Gregordb0b9f12011-08-04 15:30:47 +0000793 // Parse lambda-declarator[opt].
794 DeclSpec DS(AttrFactory);
Eli Friedman36d12942012-01-04 04:41:38 +0000795 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000796
797 if (Tok.is(tok::l_paren)) {
798 ParseScope PrototypeScope(this,
799 Scope::FunctionPrototypeScope |
Richard Smithe233fbf2013-01-28 22:42:45 +0000800 Scope::FunctionDeclarationScope |
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000801 Scope::DeclScope);
802
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000803 SourceLocation DeclEndLoc;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000804 BalancedDelimiterTracker T(*this, tok::l_paren);
805 T.consumeOpen();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000806 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000807
808 // Parse parameter-declaration-clause.
809 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000810 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000811 SourceLocation EllipsisLoc;
812
813 if (Tok.isNot(tok::r_paren))
814 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
815
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000816 T.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000817 SourceLocation RParenLoc = T.getCloseLocation();
818 DeclEndLoc = RParenLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000819
820 // Parse 'mutable'[opt].
821 SourceLocation MutableLoc;
822 if (Tok.is(tok::kw_mutable)) {
823 MutableLoc = ConsumeToken();
824 DeclEndLoc = MutableLoc;
825 }
826
827 // Parse exception-specification[opt].
828 ExceptionSpecificationType ESpecType = EST_None;
829 SourceRange ESpecRange;
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000830 SmallVector<ParsedType, 2> DynamicExceptions;
831 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000832 ExprResult NoexceptExpr;
Richard Smith2331bbf2012-05-02 22:22:32 +0000833 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +0000834 DynamicExceptions,
835 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +0000836 NoexceptExpr);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000837
838 if (ESpecType != EST_None)
839 DeclEndLoc = ESpecRange.getEnd();
840
841 // Parse attribute-specifier[opt].
Richard Smith89645bc2013-01-02 12:01:23 +0000842 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000843
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000844 SourceLocation FunLocalRangeEnd = DeclEndLoc;
845
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000846 // Parse trailing-return-type[opt].
Richard Smith700537c2012-06-12 01:51:59 +0000847 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000848 if (Tok.is(tok::arrow)) {
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000849 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000850 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000851 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000852 if (Range.getEnd().isValid())
853 DeclEndLoc = Range.getEnd();
854 }
855
856 PrototypeScope.Exit();
857
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000858 SourceLocation NoLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000859 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000860 /*isAmbiguous=*/false,
861 LParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000862 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000863 EllipsisLoc, RParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000864 DS.getTypeQualifiers(),
865 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000866 /*RefQualifierLoc=*/NoLoc,
867 /*ConstQualifierLoc=*/NoLoc,
868 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000869 MutableLoc,
870 ESpecType, ESpecRange.getBegin(),
871 DynamicExceptions.data(),
872 DynamicExceptionRanges.data(),
873 DynamicExceptions.size(),
874 NoexceptExpr.isUsable() ?
875 NoexceptExpr.get() : 0,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000876 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000877 TrailingReturnType),
878 Attr, DeclEndLoc);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000879 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
880 // It's common to forget that one needs '()' before 'mutable' or the
881 // result type. Deal with this.
882 Diag(Tok, diag::err_lambda_missing_parens)
883 << Tok.is(tok::arrow)
884 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
885 SourceLocation DeclLoc = Tok.getLocation();
886 SourceLocation DeclEndLoc = DeclLoc;
887
888 // Parse 'mutable', if it's there.
889 SourceLocation MutableLoc;
890 if (Tok.is(tok::kw_mutable)) {
891 MutableLoc = ConsumeToken();
892 DeclEndLoc = MutableLoc;
893 }
894
895 // Parse the return type, if there is one.
Richard Smith700537c2012-06-12 01:51:59 +0000896 TypeResult TrailingReturnType;
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000897 if (Tok.is(tok::arrow)) {
898 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000899 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000900 if (Range.getEnd().isValid())
901 DeclEndLoc = Range.getEnd();
902 }
903
904 ParsedAttributes Attr(AttrFactory);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000905 SourceLocation NoLoc;
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000906 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000907 /*isAmbiguous=*/false,
908 /*LParenLoc=*/NoLoc,
909 /*Params=*/0,
910 /*NumParams=*/0,
911 /*EllipsisLoc=*/NoLoc,
912 /*RParenLoc=*/NoLoc,
913 /*TypeQuals=*/0,
914 /*RefQualifierIsLValueRef=*/true,
915 /*RefQualifierLoc=*/NoLoc,
916 /*ConstQualifierLoc=*/NoLoc,
917 /*VolatileQualifierLoc=*/NoLoc,
918 MutableLoc,
919 EST_None,
920 /*ESpecLoc=*/NoLoc,
921 /*Exceptions=*/0,
922 /*ExceptionRanges=*/0,
923 /*NumExceptions=*/0,
924 /*NoexceptExpr=*/0,
925 DeclLoc, DeclEndLoc, D,
926 TrailingReturnType),
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000927 Attr, DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000928 }
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000929
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000930
Eli Friedman4817cf72012-01-06 03:05:34 +0000931 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
932 // it.
Douglas Gregorb8389972012-02-21 22:51:27 +0000933 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorb8389972012-02-21 22:51:27 +0000934 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +0000935
Eli Friedman71c80552012-01-05 03:35:19 +0000936 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
937
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000938 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +0000939 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000940 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000941 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
942 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000943 }
944
Eli Friedmanc7c97142012-01-04 02:40:39 +0000945 StmtResult Stmt(ParseCompoundStatementBody());
946 BodyScope.Exit();
947
Eli Friedman898caf82012-01-04 02:46:53 +0000948 if (!Stmt.isInvalid())
Douglas Gregor63798542012-02-20 19:44:39 +0000949 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmanc7c97142012-01-04 02:40:39 +0000950
Eli Friedman898caf82012-01-04 02:46:53 +0000951 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
952 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000953}
954
Chris Lattner29375652006-12-04 18:06:35 +0000955/// ParseCXXCasts - This handles the various ways to cast expressions to another
956/// type.
957///
958/// postfix-expression: [C++ 5.2p1]
959/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
960/// 'static_cast' '<' type-name '>' '(' expression ')'
961/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
962/// 'const_cast' '<' type-name '>' '(' expression ')'
963///
John McCalldadc5752010-08-24 06:29:42 +0000964ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +0000965 tok::TokenKind Kind = Tok.getKind();
966 const char *CastName = 0; // For error messages
967
968 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +0000969 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +0000970 case tok::kw_const_cast: CastName = "const_cast"; break;
971 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
972 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
973 case tok::kw_static_cast: CastName = "static_cast"; break;
974 }
975
976 SourceLocation OpLoc = ConsumeToken();
977 SourceLocation LAngleBracketLoc = Tok.getLocation();
978
Richard Smith55858492011-04-14 21:45:45 +0000979 // Check for "<::" which is parsed as "[:". If found, fix token stream,
980 // diagnose error, suggest fix, and recover parsing.
Richard Smith62e66302012-08-20 17:37:52 +0000981 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
982 Token Next = NextToken();
983 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
984 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
985 }
Richard Smith55858492011-04-14 21:45:45 +0000986
Chris Lattner29375652006-12-04 18:06:35 +0000987 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +0000988 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000989
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000990 // Parse the common declaration-specifiers piece.
991 DeclSpec DS(AttrFactory);
992 ParseSpecifierQualifierList(DS);
993
994 // Parse the abstract-declarator, if present.
995 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
996 ParseDeclarator(DeclaratorInfo);
997
Chris Lattner29375652006-12-04 18:06:35 +0000998 SourceLocation RAngleBracketLoc = Tok.getLocation();
999
Chris Lattner6d29c102008-11-18 07:48:38 +00001000 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +00001001 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +00001002
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001003 SourceLocation LParenLoc, RParenLoc;
1004 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +00001005
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001006 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001007 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001008
John McCalldadc5752010-08-24 06:29:42 +00001009 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +00001010
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001011 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001012 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +00001013
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001014 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +00001015 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001016 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +00001017 RAngleBracketLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001018 T.getOpenLocation(), Result.take(),
1019 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +00001020
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001021 return Result;
Chris Lattner29375652006-12-04 18:06:35 +00001022}
Bill Wendling4073ed52007-02-13 01:51:42 +00001023
Sebastian Redlc4704762008-11-11 11:37:55 +00001024/// ParseCXXTypeid - This handles the C++ typeid expression.
1025///
1026/// postfix-expression: [C++ 5.2p1]
1027/// 'typeid' '(' expression ')'
1028/// 'typeid' '(' type-id ')'
1029///
John McCalldadc5752010-08-24 06:29:42 +00001030ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +00001031 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1032
1033 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001034 SourceLocation LParenLoc, RParenLoc;
1035 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +00001036
1037 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001038 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +00001039 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001040 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +00001041
John McCalldadc5752010-08-24 06:29:42 +00001042 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001043
Richard Smith4f605af2012-08-18 00:55:03 +00001044 // C++0x [expr.typeid]p3:
1045 // When typeid is applied to an expression other than an lvalue of a
1046 // polymorphic class type [...] The expression is an unevaluated
1047 // operand (Clause 5).
1048 //
1049 // Note that we can't tell whether the expression is an lvalue of a
1050 // polymorphic class type until after we've parsed the expression; we
1051 // speculatively assume the subexpression is unevaluated, and fix it up
1052 // later.
1053 //
1054 // We enter the unevaluated context before trying to determine whether we
1055 // have a type-id, because the tentative parse logic will try to resolve
1056 // names, and must treat them as unevaluated.
Eli Friedman15681d62012-09-26 04:34:21 +00001057 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1058 Sema::ReuseLambdaContextDecl);
Richard Smith4f605af2012-08-18 00:55:03 +00001059
Sebastian Redlc4704762008-11-11 11:37:55 +00001060 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00001061 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +00001062
1063 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001064 T.consumeClose();
1065 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001066 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001067 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +00001068
1069 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001070 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001071 } else {
1072 Result = ParseExpression();
1073
1074 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001075 if (Result.isInvalid())
Sebastian Redlc4704762008-11-11 11:37:55 +00001076 SkipUntil(tok::r_paren);
1077 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001078 T.consumeClose();
1079 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001080 if (RParenLoc.isInvalid())
1081 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001082
Sebastian Redlc4704762008-11-11 11:37:55 +00001083 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001084 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001085 }
1086 }
1087
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001088 return Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001089}
1090
Francois Pichet9f4f2072010-09-08 12:20:18 +00001091/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1092///
1093/// '__uuidof' '(' expression ')'
1094/// '__uuidof' '(' type-id ')'
1095///
1096ExprResult Parser::ParseCXXUuidof() {
1097 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1098
1099 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001100 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001101
1102 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001103 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +00001104 return ExprError();
1105
1106 ExprResult Result;
1107
1108 if (isTypeIdInParens()) {
1109 TypeResult Ty = ParseTypeName();
1110
1111 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001112 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001113
1114 if (Ty.isInvalid())
1115 return ExprError();
1116
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001117 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1118 Ty.get().getAsOpaquePtr(),
1119 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001120 } else {
1121 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1122 Result = ParseExpression();
1123
1124 // Match the ')'.
1125 if (Result.isInvalid())
1126 SkipUntil(tok::r_paren);
1127 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001128 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001129
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001130 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1131 /*isType=*/false,
1132 Result.release(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001133 }
1134 }
1135
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001136 return Result;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001137}
1138
Douglas Gregore610ada2010-02-24 18:44:31 +00001139/// \brief Parse a C++ pseudo-destructor expression after the base,
1140/// . or -> operator, and nested-name-specifier have already been
1141/// parsed.
1142///
1143/// postfix-expression: [C++ 5.2]
1144/// postfix-expression . pseudo-destructor-name
1145/// postfix-expression -> pseudo-destructor-name
1146///
1147/// pseudo-destructor-name:
1148/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1149/// ::[opt] nested-name-specifier template simple-template-id ::
1150/// ~type-name
1151/// ::[opt] nested-name-specifier[opt] ~type-name
1152///
John McCalldadc5752010-08-24 06:29:42 +00001153ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +00001154Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1155 tok::TokenKind OpKind,
1156 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001157 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001158 // We're parsing either a pseudo-destructor-name or a dependent
1159 // member access that has the same form as a
1160 // pseudo-destructor-name. We parse both in the same way and let
1161 // the action model sort them out.
1162 //
1163 // Note that the ::[opt] nested-name-specifier[opt] has already
1164 // been parsed, and if there was a simple-template-id, it has
1165 // been coalesced into a template-id annotation token.
1166 UnqualifiedId FirstTypeName;
1167 SourceLocation CCLoc;
1168 if (Tok.is(tok::identifier)) {
1169 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1170 ConsumeToken();
1171 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1172 CCLoc = ConsumeToken();
1173 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001174 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1175 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001176 FirstTypeName.setTemplateId(
1177 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1178 ConsumeToken();
1179 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1180 CCLoc = ConsumeToken();
1181 } else {
1182 FirstTypeName.setIdentifier(0, SourceLocation());
1183 }
1184
1185 // Parse the tilde.
1186 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1187 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001188
1189 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1190 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001191 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-12-16 16:03:09 +00001192 if (DS.getTypeSpecType() == TST_error)
1193 return ExprError();
1194 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1195 OpKind, TildeLoc, DS,
1196 Tok.is(tok::l_paren));
1197 }
1198
Douglas Gregore610ada2010-02-24 18:44:31 +00001199 if (!Tok.is(tok::identifier)) {
1200 Diag(Tok, diag::err_destructor_tilde_identifier);
1201 return ExprError();
1202 }
1203
1204 // Parse the second type.
1205 UnqualifiedId SecondTypeName;
1206 IdentifierInfo *Name = Tok.getIdentifierInfo();
1207 SourceLocation NameLoc = ConsumeToken();
1208 SecondTypeName.setIdentifier(Name, NameLoc);
1209
1210 // If there is a '<', the second type name is a template-id. Parse
1211 // it as such.
1212 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001213 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1214 Name, NameLoc,
1215 false, ObjectType, SecondTypeName,
1216 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001217 return ExprError();
1218
John McCallb268a282010-08-23 23:25:46 +00001219 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1220 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001221 SS, FirstTypeName, CCLoc,
1222 TildeLoc, SecondTypeName,
1223 Tok.is(tok::l_paren));
1224}
1225
Bill Wendling4073ed52007-02-13 01:51:42 +00001226/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1227///
1228/// boolean-literal: [C++ 2.13.5]
1229/// 'true'
1230/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001231ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001232 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001233 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001234}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001235
1236/// ParseThrowExpression - This handles the C++ throw expression.
1237///
1238/// throw-expression: [C++ 15]
1239/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001240ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001241 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001242 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001243
Chris Lattner65dd8432008-04-06 06:02:23 +00001244 // If the current token isn't the start of an assignment-expression,
1245 // then the expression is not present. This handles things like:
1246 // "C ? throw : (void)42", which is crazy but legal.
1247 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1248 case tok::semi:
1249 case tok::r_paren:
1250 case tok::r_square:
1251 case tok::r_brace:
1252 case tok::colon:
1253 case tok::comma:
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001254 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001255
Chris Lattner65dd8432008-04-06 06:02:23 +00001256 default:
John McCalldadc5752010-08-24 06:29:42 +00001257 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001258 if (Expr.isInvalid()) return Expr;
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001259 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001260 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001261}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001262
1263/// ParseCXXThis - This handles the C++ 'this' pointer.
1264///
1265/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1266/// a non-lvalue expression whose value is the address of the object for which
1267/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001268ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001269 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1270 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001271 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001272}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001273
1274/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1275/// Can be interpreted either as function-style casting ("int(x)")
1276/// or class type construction ("ClassType(x,y,z)")
1277/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001278/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001279///
1280/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001281/// simple-type-specifier '(' expression-list[opt] ')'
1282/// [C++0x] simple-type-specifier braced-init-list
1283/// typename-specifier '(' expression-list[opt] ')'
1284/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001285///
John McCalldadc5752010-08-24 06:29:42 +00001286ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001287Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001288 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001289 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001290
Sebastian Redl3da34892011-06-05 12:23:16 +00001291 assert((Tok.is(tok::l_paren) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001292 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001293 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001294
Sebastian Redl3da34892011-06-05 12:23:16 +00001295 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001296 ExprResult Init = ParseBraceInitializer();
1297 if (Init.isInvalid())
1298 return Init;
1299 Expr *InitList = Init.take();
1300 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1301 MultiExprArg(&InitList, 1),
1302 SourceLocation());
Sebastian Redl3da34892011-06-05 12:23:16 +00001303 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001304 BalancedDelimiterTracker T(*this, tok::l_paren);
1305 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001306
Benjamin Kramerf0623432012-08-23 22:51:59 +00001307 ExprVector Exprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00001308 CommaLocsTy CommaLocs;
1309
1310 if (Tok.isNot(tok::r_paren)) {
1311 if (ParseExpressionList(Exprs, CommaLocs)) {
1312 SkipUntil(tok::r_paren);
1313 return ExprError();
1314 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001315 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001316
1317 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001318 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001319
1320 // TypeRep could be null, if it references an invalid typedef.
1321 if (!TypeRep)
1322 return ExprError();
1323
1324 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1325 "Unexpected number of commas!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001326 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001327 Exprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001328 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001329 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001330}
1331
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001332/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001333///
1334/// condition:
1335/// expression
1336/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-02-22 06:49:09 +00001337/// [C++11] type-specifier-seq declarator '=' initializer-clause
1338/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001339/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1340/// '=' assignment-expression
1341///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001342/// \param ExprOut if the condition was parsed as an expression, the parsed
1343/// expression.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001344///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001345/// \param DeclOut if the condition was parsed as a declaration, the parsed
1346/// declaration.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001347///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001348/// \param Loc The location of the start of the statement that requires this
1349/// condition, e.g., the "for" in a for loop.
1350///
1351/// \param ConvertToBoolean Whether the condition expression should be
1352/// converted to a boolean value.
1353///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001354/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001355bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1356 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001357 SourceLocation Loc,
1358 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001359 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001360 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001361 cutOffParsing();
1362 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001363 }
1364
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001365 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001366 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001367
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001368 if (!isCXXConditionDeclaration()) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001369 ProhibitAttributes(attrs);
1370
Douglas Gregore60e41a2010-05-06 17:25:47 +00001371 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001372 ExprOut = ParseExpression(); // expression
1373 DeclOut = 0;
1374 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001375 return true;
1376
1377 // If required, convert to a boolean value.
1378 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001379 ExprOut
1380 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1381 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001382 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001383
1384 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001385 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001386 ParseSpecifierQualifierList(DS);
1387
1388 // declarator
1389 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1390 ParseDeclarator(DeclaratorInfo);
1391
1392 // simple-asm-expr[opt]
1393 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001394 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001395 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001396 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001397 SkipUntil(tok::semi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001398 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001399 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001400 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001401 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001402 }
1403
1404 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001405 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001406
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001407 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001408 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001409 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001410 DeclOut = Dcl.get();
1411 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001412
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001413 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001414 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001415 bool CopyInitialization = isTokenEqualOrEqualTypo();
1416 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001417 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001418
1419 ExprResult InitExpr = ExprError();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001420 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-02-22 06:49:09 +00001421 Diag(Tok.getLocation(),
1422 diag::warn_cxx98_compat_generalized_initializer_lists);
1423 InitExpr = ParseBraceInitializer();
1424 } else if (CopyInitialization) {
1425 InitExpr = ParseAssignmentExpression();
1426 } else if (Tok.is(tok::l_paren)) {
1427 // This was probably an attempt to initialize the variable.
1428 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1429 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1430 RParen = ConsumeParen();
1431 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1432 diag::err_expected_init_in_condition_lparen)
1433 << SourceRange(LParen, RParen);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001434 } else {
Richard Smith2a15b742012-02-22 06:49:09 +00001435 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1436 diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001437 }
Richard Smith2a15b742012-02-22 06:49:09 +00001438
1439 if (!InitExpr.isInvalid())
1440 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
1441 DS.getTypeSpecType() == DeclSpec::TST_auto);
1442
Douglas Gregore60e41a2010-05-06 17:25:47 +00001443 // FIXME: Build a reference to this declaration? Convert it to bool?
1444 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001445
1446 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001447
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001448 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001449}
1450
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001451/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1452/// This should only be called when the current token is known to be part of
1453/// simple-type-specifier.
1454///
1455/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001456/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001457/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1458/// char
1459/// wchar_t
1460/// bool
1461/// short
1462/// int
1463/// long
1464/// signed
1465/// unsigned
1466/// float
1467/// double
1468/// void
1469/// [GNU] typeof-specifier
1470/// [C++0x] auto [TODO]
1471///
1472/// type-name:
1473/// class-name
1474/// enum-name
1475/// typedef-name
1476///
1477void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1478 DS.SetRangeStart(Tok.getLocation());
1479 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001480 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001481 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001482
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001483 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001484 case tok::identifier: // foo::bar
1485 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001486 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001487 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001488 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001489
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001490 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001491 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001492 if (getTypeAnnotation(Tok))
1493 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1494 getTypeAnnotation(Tok));
1495 else
1496 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001497
1498 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1499 ConsumeToken();
1500
1501 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1502 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1503 // Objective-C interface. If we don't have Objective-C or a '<', this is
1504 // just a normal reference to a typedef name.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001505 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001506 ParseObjCProtocolQualifiers(DS);
1507
1508 DS.Finish(Diags, PP);
1509 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001510 }
Mike Stump11289f42009-09-09 15:08:12 +00001511
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001512 // builtin types
1513 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001514 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001515 break;
1516 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +00001517 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001518 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001519 case tok::kw___int64:
1520 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1521 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001522 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001523 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001524 break;
1525 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001526 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001527 break;
1528 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001529 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001530 break;
1531 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001532 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001533 break;
1534 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001535 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001536 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00001537 case tok::kw___int128:
1538 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1539 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001540 case tok::kw_half:
1541 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1542 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001543 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001544 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001545 break;
1546 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001547 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001548 break;
1549 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001550 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001551 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001552 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001553 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001554 break;
1555 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001556 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001557 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001558 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +00001559 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001560 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001561 case tok::annot_decltype:
1562 case tok::kw_decltype:
1563 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1564 return DS.Finish(Diags, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001565
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001566 // GNU typeof support.
1567 case tok::kw_typeof:
1568 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +00001569 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001570 return;
1571 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001572 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001573 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1574 else
1575 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001576 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +00001577 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001578}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001579
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001580/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1581/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1582/// e.g., "const short int". Note that the DeclSpec is *not* finished
1583/// by parsing the type-specifier-seq, because these sequences are
1584/// typically followed by some form of declarator. Returns true and
1585/// emits diagnostics if this is not a type-specifier-seq, false
1586/// otherwise.
1587///
1588/// type-specifier-seq: [C++ 8.1]
1589/// type-specifier type-specifier-seq[opt]
1590///
1591bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smithc5b05522012-03-12 07:56:15 +00001592 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor40d732f2010-02-24 23:13:13 +00001593 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001594 return false;
1595}
1596
Douglas Gregor7861a802009-11-03 01:35:08 +00001597/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1598/// some form.
1599///
1600/// This routine is invoked when a '<' is encountered after an identifier or
1601/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1602/// whether the unqualified-id is actually a template-id. This routine will
1603/// then parse the template arguments and form the appropriate template-id to
1604/// return to the caller.
1605///
1606/// \param SS the nested-name-specifier that precedes this template-id, if
1607/// we're actually parsing a qualified-id.
1608///
1609/// \param Name for constructor and destructor names, this is the actual
1610/// identifier that may be a template-name.
1611///
1612/// \param NameLoc the location of the class-name in a constructor or
1613/// destructor.
1614///
1615/// \param EnteringContext whether we're entering the scope of the
1616/// nested-name-specifier.
1617///
Douglas Gregor127ea592009-11-03 21:24:04 +00001618/// \param ObjectType if this unqualified-id occurs within a member access
1619/// expression, the type of the base object whose member is being accessed.
1620///
Douglas Gregor7861a802009-11-03 01:35:08 +00001621/// \param Id as input, describes the template-name or operator-function-id
1622/// that precedes the '<'. If template arguments were parsed successfully,
1623/// will be updated with the template-id.
1624///
Douglas Gregore610ada2010-02-24 18:44:31 +00001625/// \param AssumeTemplateId When true, this routine will assume that the name
1626/// refers to a template without performing name lookup to verify.
1627///
Douglas Gregor7861a802009-11-03 01:35:08 +00001628/// \returns true if a parse error occurred, false otherwise.
1629bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001630 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001631 IdentifierInfo *Name,
1632 SourceLocation NameLoc,
1633 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001634 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001635 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001636 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001637 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1638 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001639
1640 TemplateTy Template;
1641 TemplateNameKind TNK = TNK_Non_template;
1642 switch (Id.getKind()) {
1643 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001644 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001645 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001646 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001647 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001648 Id, ObjectType, EnteringContext,
1649 Template);
1650 if (TNK == TNK_Non_template)
1651 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001652 } else {
1653 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001654 TNK = Actions.isTemplateName(getCurScope(), SS,
1655 TemplateKWLoc.isValid(), Id,
1656 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001657 MemberOfUnknownSpecialization);
1658
1659 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1660 ObjectType && IsTemplateArgumentList()) {
1661 // We have something like t->getAs<T>(), where getAs is a
1662 // member of an unknown specialization. However, this will only
1663 // parse correctly as a template, so suggest the keyword 'template'
1664 // before 'getAs' and treat this as a dependent template name.
1665 std::string Name;
1666 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1667 Name = Id.Identifier->getName();
1668 else {
1669 Name = "operator ";
1670 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1671 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1672 else
1673 Name += Id.Identifier->getName();
1674 }
1675 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1676 << Name
1677 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00001678 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1679 SS, TemplateKWLoc, Id,
1680 ObjectType, EnteringContext,
1681 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001682 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001683 return true;
1684 }
1685 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001686 break;
1687
Douglas Gregor3cf81312009-11-03 23:16:33 +00001688 case UnqualifiedId::IK_ConstructorName: {
1689 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001690 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001691 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001692 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1693 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001694 EnteringContext, Template,
1695 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001696 break;
1697 }
1698
Douglas Gregor3cf81312009-11-03 23:16:33 +00001699 case UnqualifiedId::IK_DestructorName: {
1700 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001701 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001702 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001703 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001704 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1705 SS, TemplateKWLoc, TemplateName,
1706 ObjectType, EnteringContext,
1707 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001708 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001709 return true;
1710 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001711 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1712 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001713 EnteringContext, Template,
1714 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001715
John McCallba7bf592010-08-24 05:47:05 +00001716 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001717 Diag(NameLoc, diag::err_destructor_template_id)
1718 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001719 return true;
1720 }
1721 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001722 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001723 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001724
1725 default:
1726 return false;
1727 }
1728
1729 if (TNK == TNK_Non_template)
1730 return false;
1731
1732 // Parse the enclosed template argument list.
1733 SourceLocation LAngleLoc, RAngleLoc;
1734 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001735 if (Tok.is(tok::less) &&
1736 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001737 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001738 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001739 RAngleLoc))
1740 return true;
1741
1742 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001743 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1744 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001745 // Form a parsed representation of the template-id to be stored in the
1746 // UnqualifiedId.
1747 TemplateIdAnnotation *TemplateId
Benjamin Kramer1e6b6062012-04-14 12:14:03 +00001748 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor7861a802009-11-03 01:35:08 +00001749
1750 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1751 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001752 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001753 TemplateId->TemplateNameLoc = Id.StartLocation;
1754 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001755 TemplateId->Name = 0;
1756 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1757 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001758 }
1759
Douglas Gregore7c20652011-03-02 00:47:37 +00001760 TemplateId->SS = SS;
Benjamin Kramer807c2db2012-02-19 23:37:39 +00001761 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall3e56fd42010-08-23 07:28:44 +00001762 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001763 TemplateId->Kind = TNK;
1764 TemplateId->LAngleLoc = LAngleLoc;
1765 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001766 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001767 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001768 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001769 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001770
1771 Id.setTemplateId(TemplateId);
1772 return false;
1773 }
1774
1775 // Bundle the template arguments together.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001776 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnara4244b432012-01-27 08:46:19 +00001777
Douglas Gregor7861a802009-11-03 01:35:08 +00001778 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001779 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001780 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1781 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00001782 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1783 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00001784 if (Type.isInvalid())
1785 return true;
1786
1787 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1788 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1789 else
1790 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1791
1792 return false;
1793}
1794
Douglas Gregor71395fa2009-11-04 00:56:37 +00001795/// \brief Parse an operator-function-id or conversion-function-id as part
1796/// of a C++ unqualified-id.
1797///
1798/// This routine is responsible only for parsing the operator-function-id or
1799/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001800///
1801/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001802/// operator-function-id: [C++ 13.5]
1803/// 'operator' operator
1804///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001805/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001806/// new delete new[] delete[]
1807/// + - * / % ^ & | ~
1808/// ! = < > += -= *= /= %=
1809/// ^= &= |= << >> >>= <<= == !=
1810/// <= >= && || ++ -- , ->* ->
1811/// () []
1812///
1813/// conversion-function-id: [C++ 12.3.2]
1814/// operator conversion-type-id
1815///
1816/// conversion-type-id:
1817/// type-specifier-seq conversion-declarator[opt]
1818///
1819/// conversion-declarator:
1820/// ptr-operator conversion-declarator[opt]
1821/// \endcode
1822///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001823/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor7861a802009-11-03 01:35:08 +00001824/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1825///
1826/// \param EnteringContext whether we are entering the scope of the
1827/// nested-name-specifier.
1828///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001829/// \param ObjectType if this unqualified-id occurs within a member access
1830/// expression, the type of the base object whose member is being accessed.
1831///
1832/// \param Result on a successful parse, contains the parsed unqualified-id.
1833///
1834/// \returns true if parsing fails, false otherwise.
1835bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001836 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001837 UnqualifiedId &Result) {
1838 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1839
1840 // Consume the 'operator' keyword.
1841 SourceLocation KeywordLoc = ConsumeToken();
1842
1843 // Determine what kind of operator name we have.
1844 unsigned SymbolIdx = 0;
1845 SourceLocation SymbolLocations[3];
1846 OverloadedOperatorKind Op = OO_None;
1847 switch (Tok.getKind()) {
1848 case tok::kw_new:
1849 case tok::kw_delete: {
1850 bool isNew = Tok.getKind() == tok::kw_new;
1851 // Consume the 'new' or 'delete'.
1852 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001853 // Check for array new/delete.
1854 if (Tok.is(tok::l_square) &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001855 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001856 // Consume the '[' and ']'.
1857 BalancedDelimiterTracker T(*this, tok::l_square);
1858 T.consumeOpen();
1859 T.consumeClose();
1860 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001861 return true;
1862
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001863 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1864 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001865 Op = isNew? OO_Array_New : OO_Array_Delete;
1866 } else {
1867 Op = isNew? OO_New : OO_Delete;
1868 }
1869 break;
1870 }
1871
1872#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1873 case tok::Token: \
1874 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1875 Op = OO_##Name; \
1876 break;
1877#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1878#include "clang/Basic/OperatorKinds.def"
1879
1880 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001881 // Consume the '(' and ')'.
1882 BalancedDelimiterTracker T(*this, tok::l_paren);
1883 T.consumeOpen();
1884 T.consumeClose();
1885 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001886 return true;
1887
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001888 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1889 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001890 Op = OO_Call;
1891 break;
1892 }
1893
1894 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001895 // Consume the '[' and ']'.
1896 BalancedDelimiterTracker T(*this, tok::l_square);
1897 T.consumeOpen();
1898 T.consumeClose();
1899 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001900 return true;
1901
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001902 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1903 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001904 Op = OO_Subscript;
1905 break;
1906 }
1907
1908 case tok::code_completion: {
1909 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001910 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001911 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001912 // Don't try to parse any further.
1913 return true;
1914 }
1915
1916 default:
1917 break;
1918 }
1919
1920 if (Op != OO_None) {
1921 // We have parsed an operator-function-id.
1922 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1923 return false;
1924 }
Alexis Hunt34458502009-11-28 04:44:28 +00001925
1926 // Parse a literal-operator-id.
1927 //
Richard Smith6f212062012-10-20 08:41:10 +00001928 // literal-operator-id: C++11 [over.literal]
1929 // operator string-literal identifier
1930 // operator user-defined-string-literal
Alexis Hunt34458502009-11-28 04:44:28 +00001931
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001932 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001933 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00001934
Richard Smith7d182a72012-03-08 23:06:02 +00001935 SourceLocation DiagLoc;
1936 unsigned DiagId = 0;
1937
1938 // We're past translation phase 6, so perform string literal concatenation
1939 // before checking for "".
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001940 SmallVector<Token, 4> Toks;
1941 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith7d182a72012-03-08 23:06:02 +00001942 while (isTokenStringLiteral()) {
1943 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smith6f212062012-10-20 08:41:10 +00001944 // C++11 [over.literal]p1:
1945 // The string-literal or user-defined-string-literal in a
1946 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith7d182a72012-03-08 23:06:02 +00001947 DiagLoc = Tok.getLocation();
1948 DiagId = diag::err_literal_operator_string_prefix;
1949 }
1950 Toks.push_back(Tok);
1951 TokLocs.push_back(ConsumeStringToken());
1952 }
1953
1954 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
1955 if (Literal.hadError)
1956 return true;
1957
1958 // Grab the literal operator's suffix, which will be either the next token
1959 // or a ud-suffix from the string literal.
1960 IdentifierInfo *II = 0;
1961 SourceLocation SuffixLoc;
1962 if (!Literal.getUDSuffix().empty()) {
1963 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
1964 SuffixLoc =
1965 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
1966 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00001967 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00001968 } else if (Tok.is(tok::identifier)) {
1969 II = Tok.getIdentifierInfo();
1970 SuffixLoc = ConsumeToken();
1971 TokLocs.push_back(SuffixLoc);
1972 } else {
Alexis Hunt34458502009-11-28 04:44:28 +00001973 Diag(Tok.getLocation(), diag::err_expected_ident);
1974 return true;
1975 }
1976
Richard Smith7d182a72012-03-08 23:06:02 +00001977 // The string literal must be empty.
1978 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smith6f212062012-10-20 08:41:10 +00001979 // C++11 [over.literal]p1:
1980 // The string-literal or user-defined-string-literal in a
1981 // literal-operator-id shall [...] contain no characters
1982 // other than the implicit terminating '\0'.
Richard Smith7d182a72012-03-08 23:06:02 +00001983 DiagLoc = TokLocs.front();
1984 DiagId = diag::err_literal_operator_string_not_empty;
1985 }
1986
1987 if (DiagId) {
1988 // This isn't a valid literal-operator-id, but we think we know
1989 // what the user meant. Tell them what they should have written.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001990 SmallString<32> Str;
Richard Smith7d182a72012-03-08 23:06:02 +00001991 Str += "\"\" ";
1992 Str += II->getName();
1993 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
1994 SourceRange(TokLocs.front(), TokLocs.back()), Str);
1995 }
1996
1997 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Alexis Hunt3d221f22009-11-29 07:34:05 +00001998 return false;
Alexis Hunt34458502009-11-28 04:44:28 +00001999 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00002000
2001 // Parse a conversion-function-id.
2002 //
2003 // conversion-function-id: [C++ 12.3.2]
2004 // operator conversion-type-id
2005 //
2006 // conversion-type-id:
2007 // type-specifier-seq conversion-declarator[opt]
2008 //
2009 // conversion-declarator:
2010 // ptr-operator conversion-declarator[opt]
2011
2012 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00002013 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00002014 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00002015 return true;
2016
2017 // Parse the conversion-declarator, which is merely a sequence of
2018 // ptr-operators.
2019 Declarator D(DS, Declarator::TypeNameContext);
2020 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2021
2022 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00002023 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002024 if (Ty.isInvalid())
2025 return true;
2026
2027 // Note that this is a conversion-function-id.
2028 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2029 D.getSourceRange().getEnd());
2030 return false;
2031}
2032
2033/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2034/// name of an entity.
2035///
2036/// \code
2037/// unqualified-id: [C++ expr.prim.general]
2038/// identifier
2039/// operator-function-id
2040/// conversion-function-id
2041/// [C++0x] literal-operator-id [TODO]
2042/// ~ class-name
2043/// template-id
2044///
2045/// \endcode
2046///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002047/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor71395fa2009-11-04 00:56:37 +00002048/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2049///
2050/// \param EnteringContext whether we are entering the scope of the
2051/// nested-name-specifier.
2052///
Douglas Gregor7861a802009-11-03 01:35:08 +00002053/// \param AllowDestructorName whether we allow parsing of a destructor name.
2054///
2055/// \param AllowConstructorName whether we allow parsing a constructor name.
2056///
Douglas Gregor127ea592009-11-03 21:24:04 +00002057/// \param ObjectType if this unqualified-id occurs within a member access
2058/// expression, the type of the base object whose member is being accessed.
2059///
Douglas Gregor7861a802009-11-03 01:35:08 +00002060/// \param Result on a successful parse, contains the parsed unqualified-id.
2061///
2062/// \returns true if parsing fails, false otherwise.
2063bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2064 bool AllowDestructorName,
2065 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002066 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002067 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002068 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002069
2070 // Handle 'A::template B'. This is for template-ids which have not
2071 // already been annotated by ParseOptionalCXXScopeSpecifier().
2072 bool TemplateSpecified = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002073 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002074 (ObjectType || SS.isSet())) {
2075 TemplateSpecified = true;
2076 TemplateKWLoc = ConsumeToken();
2077 }
2078
Douglas Gregor7861a802009-11-03 01:35:08 +00002079 // unqualified-id:
2080 // identifier
2081 // template-id (when it hasn't already been annotated)
2082 if (Tok.is(tok::identifier)) {
2083 // Consume the identifier.
2084 IdentifierInfo *Id = Tok.getIdentifierInfo();
2085 SourceLocation IdLoc = ConsumeToken();
2086
David Blaikiebbafb8a2012-03-11 07:00:24 +00002087 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002088 // If we're not in C++, only identifiers matter. Record the
2089 // identifier and return.
2090 Result.setIdentifier(Id, IdLoc);
2091 return false;
2092 }
2093
Douglas Gregor7861a802009-11-03 01:35:08 +00002094 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002095 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002096 // We have parsed a constructor name.
Abramo Bagnara4244b432012-01-27 08:46:19 +00002097 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2098 &SS, false, false,
2099 ParsedType(),
2100 /*IsCtorOrDtorName=*/true,
2101 /*NonTrivialTypeSourceInfo=*/true);
2102 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002103 } else {
2104 // We have parsed an identifier.
2105 Result.setIdentifier(Id, IdLoc);
2106 }
2107
2108 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00002109 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002110 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2111 EnteringContext, ObjectType,
2112 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002113
2114 return false;
2115 }
2116
2117 // unqualified-id:
2118 // template-id (already parsed and annotated)
2119 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002120 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002121
2122 // If the template-name names the current class, then this is a constructor
2123 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002124 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002125 if (SS.isSet()) {
2126 // C++ [class.qual]p2 specifies that a qualified template-name
2127 // is taken as the constructor name where a constructor can be
2128 // declared. Thus, the template arguments are extraneous, so
2129 // complain about them and remove them entirely.
2130 Diag(TemplateId->TemplateNameLoc,
2131 diag::err_out_of_line_constructor_template_id)
2132 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002133 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002134 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnara4244b432012-01-27 08:46:19 +00002135 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2136 TemplateId->TemplateNameLoc,
2137 getCurScope(),
2138 &SS, false, false,
2139 ParsedType(),
2140 /*IsCtorOrDtorName=*/true,
2141 /*NontrivialTypeSourceInfo=*/true);
2142 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002143 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002144 ConsumeToken();
2145 return false;
2146 }
2147
2148 Result.setConstructorTemplateId(TemplateId);
2149 ConsumeToken();
2150 return false;
2151 }
2152
Douglas Gregor7861a802009-11-03 01:35:08 +00002153 // We have already parsed a template-id; consume the annotation token as
2154 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002155 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002156 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00002157 ConsumeToken();
2158 return false;
2159 }
2160
2161 // unqualified-id:
2162 // operator-function-id
2163 // conversion-function-id
2164 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002165 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002166 return true;
2167
Alexis Hunted0530f2009-11-28 08:58:14 +00002168 // If we have an operator-function-id or a literal-operator-id and the next
2169 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00002170 //
2171 // template-id:
2172 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00002173 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2174 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002175 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002176 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2177 0, SourceLocation(),
2178 EnteringContext, ObjectType,
2179 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002180
Douglas Gregor7861a802009-11-03 01:35:08 +00002181 return false;
2182 }
2183
David Blaikiebbafb8a2012-03-11 07:00:24 +00002184 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002185 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002186 // C++ [expr.unary.op]p10:
2187 // There is an ambiguity in the unary-expression ~X(), where X is a
2188 // class-name. The ambiguity is resolved in favor of treating ~ as a
2189 // unary complement rather than treating ~X as referring to a destructor.
2190
2191 // Parse the '~'.
2192 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002193
2194 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2195 DeclSpec DS(AttrFactory);
2196 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2197 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2198 Result.setDestructorName(TildeLoc, Type, EndLoc);
2199 return false;
2200 }
2201 return true;
2202 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002203
2204 // Parse the class-name.
2205 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002206 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002207 return true;
2208 }
2209
2210 // Parse the class-name (or template-name in a simple-template-id).
2211 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2212 SourceLocation ClassNameLoc = ConsumeToken();
2213
Douglas Gregorb22ee882010-05-05 05:58:24 +00002214 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00002215 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002216 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2217 ClassName, ClassNameLoc,
2218 EnteringContext, ObjectType,
2219 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002220 }
2221
Douglas Gregor7861a802009-11-03 01:35:08 +00002222 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002223 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2224 ClassNameLoc, getCurScope(),
2225 SS, ObjectType,
2226 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002227 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002228 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002229
Douglas Gregor7861a802009-11-03 01:35:08 +00002230 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002231 return false;
2232 }
2233
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002234 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002235 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002236 return true;
2237}
2238
Sebastian Redlbd150f42008-11-21 19:14:01 +00002239/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2240/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002241///
Chris Lattner109faf22009-01-04 21:25:24 +00002242/// This method is called to parse the new expression after the optional :: has
2243/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2244/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002245///
2246/// new-expression:
2247/// '::'[opt] 'new' new-placement[opt] new-type-id
2248/// new-initializer[opt]
2249/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2250/// new-initializer[opt]
2251///
2252/// new-placement:
2253/// '(' expression-list ')'
2254///
Sebastian Redl351bb782008-12-02 14:43:59 +00002255/// new-type-id:
2256/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002257/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002258///
2259/// new-declarator:
2260/// ptr-operator new-declarator[opt]
2261/// direct-new-declarator
2262///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002263/// new-initializer:
2264/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002265/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002266///
John McCalldadc5752010-08-24 06:29:42 +00002267ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002268Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2269 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2270 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002271
2272 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2273 // second form of new-expression. It can't be a new-type-id.
2274
Benjamin Kramerf0623432012-08-23 22:51:59 +00002275 ExprVector PlacementArgs;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002276 SourceLocation PlacementLParen, PlacementRParen;
2277
Douglas Gregorf2753b32010-07-13 15:54:32 +00002278 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002279 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002280 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002281 if (Tok.is(tok::l_paren)) {
2282 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002283 BalancedDelimiterTracker T(*this, tok::l_paren);
2284 T.consumeOpen();
2285 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002286 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2287 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002288 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002289 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002290
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002291 T.consumeClose();
2292 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002293 if (PlacementRParen.isInvalid()) {
2294 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002295 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002296 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002297
Sebastian Redl351bb782008-12-02 14:43:59 +00002298 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002299 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002300 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002301 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002302 } else {
2303 // We still need the type.
2304 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002305 BalancedDelimiterTracker T(*this, tok::l_paren);
2306 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002307 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002308 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002309 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002310 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002311 T.consumeClose();
2312 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002313 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002314 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002315 if (ParseCXXTypeSpecifierSeq(DS))
2316 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002317 else {
2318 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002319 ParseDeclaratorInternal(DeclaratorInfo,
2320 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002321 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002322 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002323 }
2324 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002325 // A new-type-id is a simplified type-id, where essentially the
2326 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002327 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002328 if (ParseCXXTypeSpecifierSeq(DS))
2329 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002330 else {
2331 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002332 ParseDeclaratorInternal(DeclaratorInfo,
2333 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002334 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002335 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002336 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002337 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002338 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002339 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002340
Sebastian Redl6047f072012-02-16 12:22:20 +00002341 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002342
2343 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002344 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002345 ExprVector ConstructorArgs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002346 BalancedDelimiterTracker T(*this, tok::l_paren);
2347 T.consumeOpen();
2348 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002349 if (Tok.isNot(tok::r_paren)) {
2350 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002351 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2352 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002353 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002354 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002355 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002356 T.consumeClose();
2357 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002358 if (ConstructorRParen.isInvalid()) {
2359 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002360 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002361 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002362 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2363 ConstructorRParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002364 ConstructorArgs);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002365 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002366 Diag(Tok.getLocation(),
2367 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002368 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002369 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002370 if (Initializer.isInvalid())
2371 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002372
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002373 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002374 PlacementArgs, PlacementRParen,
Sebastian Redl6047f072012-02-16 12:22:20 +00002375 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002376}
2377
Sebastian Redlbd150f42008-11-21 19:14:01 +00002378/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2379/// passed to ParseDeclaratorInternal.
2380///
2381/// direct-new-declarator:
2382/// '[' expression ']'
2383/// direct-new-declarator '[' constant-expression ']'
2384///
Chris Lattner109faf22009-01-04 21:25:24 +00002385void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002386 // Parse the array dimensions.
2387 bool first = true;
2388 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002389 // An array-size expression can't start with a lambda.
2390 if (CheckProhibitedCXX11Attribute())
2391 continue;
2392
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002393 BalancedDelimiterTracker T(*this, tok::l_square);
2394 T.consumeOpen();
2395
John McCalldadc5752010-08-24 06:29:42 +00002396 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002397 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002398 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002399 // Recover
2400 SkipUntil(tok::r_square);
2401 return;
2402 }
2403 first = false;
2404
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002405 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002406
Bill Wendling44426052012-12-20 19:22:21 +00002407 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002408 ParsedAttributes Attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002409 MaybeParseCXX11Attributes(Attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002410
John McCall084e83d2011-03-24 11:26:52 +00002411 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002412 /*static=*/false, /*star=*/false,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002413 Size.release(),
2414 T.getOpenLocation(),
2415 T.getCloseLocation()),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002416 Attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002417
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002418 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002419 return;
2420 }
2421}
2422
2423/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2424/// This ambiguity appears in the syntax of the C++ new operator.
2425///
2426/// new-expression:
2427/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2428/// new-initializer[opt]
2429///
2430/// new-placement:
2431/// '(' expression-list ')'
2432///
John McCall37ad5512010-08-23 06:44:23 +00002433bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002434 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002435 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002436 // The '(' was already consumed.
2437 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002438 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002439 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002440 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002441 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002442 }
2443
2444 // It's not a type, it has to be an expression list.
2445 // Discard the comma locations - ActOnCXXNew has enough parameters.
2446 CommaLocsTy CommaLocs;
2447 return ParseExpressionList(PlacementArgs, CommaLocs);
2448}
2449
2450/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2451/// to free memory allocated by new.
2452///
Chris Lattner109faf22009-01-04 21:25:24 +00002453/// This method is called to parse the 'delete' expression after the optional
2454/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2455/// and "Start" is its location. Otherwise, "Start" is the location of the
2456/// 'delete' token.
2457///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002458/// delete-expression:
2459/// '::'[opt] 'delete' cast-expression
2460/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002461ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002462Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2463 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2464 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002465
2466 // Array delete?
2467 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002468 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith10c60722012-08-09 19:01:51 +00002469 // C++11 [expr.delete]p1:
2470 // Whenever the delete keyword is followed by empty square brackets, it
2471 // shall be interpreted as [array delete].
2472 // [Footnote: A lambda expression with a lambda-introducer that consists
2473 // of empty square brackets can follow the delete keyword if
2474 // the lambda expression is enclosed in parentheses.]
2475 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2476 // lambda-introducer.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002477 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002478 BalancedDelimiterTracker T(*this, tok::l_square);
2479
2480 T.consumeOpen();
2481 T.consumeClose();
2482 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002483 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002484 }
2485
John McCalldadc5752010-08-24 06:29:42 +00002486 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002487 if (Operand.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002488 return Operand;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002489
John McCallb268a282010-08-23 23:25:46 +00002490 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002491}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002492
Mike Stump11289f42009-09-09 15:08:12 +00002493static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002494 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002495 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002496 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002497 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002498 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002499 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Alexis Huntf479f1b2011-05-09 18:22:59 +00002500 case tok::kw___has_trivial_constructor:
2501 return UTT_HasTrivialDefaultConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002502 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002503 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2504 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2505 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley65497cc2011-04-27 23:09:49 +00002506 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2507 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002508 case tok::kw___is_class: return UTT_IsClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002509 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2510 case tok::kw___is_compound: return UTT_IsCompound;
2511 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002512 case tok::kw___is_empty: return UTT_IsEmpty;
2513 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregordca70af2011-12-03 18:14:24 +00002514 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley65497cc2011-04-27 23:09:49 +00002515 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2516 case tok::kw___is_function: return UTT_IsFunction;
2517 case tok::kw___is_fundamental: return UTT_IsFundamental;
2518 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallbf4a7d72012-09-25 07:32:49 +00002519 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002520 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2521 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2522 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2523 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2524 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth79803482011-04-23 10:47:20 +00002525 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00002526 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002527 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley65497cc2011-04-27 23:09:49 +00002528 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002529 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley65497cc2011-04-27 23:09:49 +00002530 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley65497cc2011-04-27 23:09:49 +00002531 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2532 case tok::kw___is_scalar: return UTT_IsScalar;
2533 case tok::kw___is_signed: return UTT_IsSigned;
2534 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2535 case tok::kw___is_trivial: return UTT_IsTrivial;
Alexis Huntd9a5cc12011-05-13 00:31:07 +00002536 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002537 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley65497cc2011-04-27 23:09:49 +00002538 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2539 case tok::kw___is_void: return UTT_IsVoid;
2540 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002541 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002542}
2543
2544static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2545 switch(kind) {
Francois Pichet347c4c72010-12-07 00:55:57 +00002546 default: llvm_unreachable("Not a known binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00002547 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley65497cc2011-04-27 23:09:49 +00002548 case tok::kw___is_convertible: return BTT_IsConvertible;
2549 case tok::kw___is_same: return BTT_IsSame;
Francois Pichet34b21132010-12-08 22:35:30 +00002550 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor8006e762011-01-27 20:28:01 +00002551 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor1be329d2012-02-23 07:33:15 +00002552 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002553 }
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002554}
2555
Douglas Gregor29c42f22012-02-24 07:38:34 +00002556static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2557 switch (kind) {
2558 default: llvm_unreachable("Not a known type trait");
2559 case tok::kw___is_trivially_constructible:
2560 return TT_IsTriviallyConstructible;
2561 }
2562}
2563
John Wiegley6242b6a2011-04-28 00:16:57 +00002564static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2565 switch(kind) {
2566 default: llvm_unreachable("Not a known binary type trait");
2567 case tok::kw___array_rank: return ATT_ArrayRank;
2568 case tok::kw___array_extent: return ATT_ArrayExtent;
2569 }
2570}
2571
John Wiegleyf9f65842011-04-25 06:54:41 +00002572static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2573 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002574 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002575 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2576 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2577 }
2578}
2579
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002580/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2581/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2582/// templates.
2583///
2584/// primary-expression:
2585/// [GNU] unary-type-trait '(' type-id ')'
2586///
John McCalldadc5752010-08-24 06:29:42 +00002587ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002588 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2589 SourceLocation Loc = ConsumeToken();
2590
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002591 BalancedDelimiterTracker T(*this, tok::l_paren);
2592 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002593 return ExprError();
2594
2595 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2596 // there will be cryptic errors about mismatched parentheses and missing
2597 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00002598 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002599
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002600 T.consumeClose();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002601
Douglas Gregor220cac52009-02-18 17:45:20 +00002602 if (Ty.isInvalid())
2603 return ExprError();
2604
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002605 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002606}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002607
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002608/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2609/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2610/// templates.
2611///
2612/// primary-expression:
2613/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2614///
2615ExprResult Parser::ParseBinaryTypeTrait() {
2616 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2617 SourceLocation Loc = ConsumeToken();
2618
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002619 BalancedDelimiterTracker T(*this, tok::l_paren);
2620 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002621 return ExprError();
2622
2623 TypeResult LhsTy = ParseTypeName();
2624 if (LhsTy.isInvalid()) {
2625 SkipUntil(tok::r_paren);
2626 return ExprError();
2627 }
2628
2629 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2630 SkipUntil(tok::r_paren);
2631 return ExprError();
2632 }
2633
2634 TypeResult RhsTy = ParseTypeName();
2635 if (RhsTy.isInvalid()) {
2636 SkipUntil(tok::r_paren);
2637 return ExprError();
2638 }
2639
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002640 T.consumeClose();
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002641
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002642 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2643 T.getCloseLocation());
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002644}
2645
Douglas Gregor29c42f22012-02-24 07:38:34 +00002646/// \brief Parse the built-in type-trait pseudo-functions that allow
2647/// implementation of the TR1/C++11 type traits templates.
2648///
2649/// primary-expression:
2650/// type-trait '(' type-id-seq ')'
2651///
2652/// type-id-seq:
2653/// type-id ...[opt] type-id-seq[opt]
2654///
2655ExprResult Parser::ParseTypeTrait() {
2656 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2657 SourceLocation Loc = ConsumeToken();
2658
2659 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2660 if (Parens.expectAndConsume(diag::err_expected_lparen))
2661 return ExprError();
2662
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002663 SmallVector<ParsedType, 2> Args;
Douglas Gregor29c42f22012-02-24 07:38:34 +00002664 do {
2665 // Parse the next type.
2666 TypeResult Ty = ParseTypeName();
2667 if (Ty.isInvalid()) {
2668 Parens.skipToEnd();
2669 return ExprError();
2670 }
2671
2672 // Parse the ellipsis, if present.
2673 if (Tok.is(tok::ellipsis)) {
2674 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2675 if (Ty.isInvalid()) {
2676 Parens.skipToEnd();
2677 return ExprError();
2678 }
2679 }
2680
2681 // Add this type to the list of arguments.
2682 Args.push_back(Ty.get());
2683
2684 if (Tok.is(tok::comma)) {
2685 ConsumeToken();
2686 continue;
2687 }
2688
2689 break;
2690 } while (true);
2691
2692 if (Parens.consumeClose())
2693 return ExprError();
2694
2695 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2696}
2697
John Wiegley6242b6a2011-04-28 00:16:57 +00002698/// ParseArrayTypeTrait - Parse the built-in array type-trait
2699/// pseudo-functions.
2700///
2701/// primary-expression:
2702/// [Embarcadero] '__array_rank' '(' type-id ')'
2703/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2704///
2705ExprResult Parser::ParseArrayTypeTrait() {
2706 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2707 SourceLocation Loc = ConsumeToken();
2708
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002709 BalancedDelimiterTracker T(*this, tok::l_paren);
2710 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley6242b6a2011-04-28 00:16:57 +00002711 return ExprError();
2712
2713 TypeResult Ty = ParseTypeName();
2714 if (Ty.isInvalid()) {
2715 SkipUntil(tok::comma);
2716 SkipUntil(tok::r_paren);
2717 return ExprError();
2718 }
2719
2720 switch (ATT) {
2721 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002722 T.consumeClose();
2723 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2724 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002725 }
2726 case ATT_ArrayExtent: {
2727 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2728 SkipUntil(tok::r_paren);
2729 return ExprError();
2730 }
2731
2732 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002733 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00002734
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002735 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2736 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002737 }
John Wiegley6242b6a2011-04-28 00:16:57 +00002738 }
David Blaikiee4d798f2012-01-20 21:50:17 +00002739 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00002740}
2741
John Wiegleyf9f65842011-04-25 06:54:41 +00002742/// ParseExpressionTrait - Parse built-in expression-trait
2743/// pseudo-functions like __is_lvalue_expr( xxx ).
2744///
2745/// primary-expression:
2746/// [Embarcadero] expression-trait '(' expression ')'
2747///
2748ExprResult Parser::ParseExpressionTrait() {
2749 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2750 SourceLocation Loc = ConsumeToken();
2751
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002752 BalancedDelimiterTracker T(*this, tok::l_paren);
2753 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegleyf9f65842011-04-25 06:54:41 +00002754 return ExprError();
2755
2756 ExprResult Expr = ParseExpression();
2757
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002758 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00002759
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002760 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2761 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00002762}
2763
2764
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002765/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2766/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2767/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002768ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002769Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002770 ParsedType &CastTy,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002771 BalancedDelimiterTracker &Tracker) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002772 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002773 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2774 assert(isTypeIdInParens() && "Not a type-id!");
2775
John McCalldadc5752010-08-24 06:29:42 +00002776 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002777 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002778
2779 // We need to disambiguate a very ugly part of the C++ syntax:
2780 //
2781 // (T())x; - type-id
2782 // (T())*x; - type-id
2783 // (T())/x; - expression
2784 // (T()); - expression
2785 //
2786 // The bad news is that we cannot use the specialized tentative parser, since
2787 // it can only verify that the thing inside the parens can be parsed as
2788 // type-id, it is not useful for determining the context past the parens.
2789 //
2790 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002791 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002792 //
2793 // It uses a scheme similar to parsing inline methods. The parenthesized
2794 // tokens are cached, the context that follows is determined (possibly by
2795 // parsing a cast-expression), and then we re-introduce the cached tokens
2796 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002797
Mike Stump11289f42009-09-09 15:08:12 +00002798 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002799 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002800
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002801 // Store the tokens of the parentheses. We will parse them after we determine
2802 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002803 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002804 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002805 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002806 return ExprError();
2807 }
2808
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002809 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002810 ParseAs = CompoundLiteral;
2811 } else {
2812 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002813 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2814 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2815 NotCastExpr = true;
2816 } else {
2817 // Try parsing the cast-expression that may follow.
2818 // If it is not a cast-expression, NotCastExpr will be true and no token
2819 // will be consumed.
2820 Result = ParseCastExpression(false/*isUnaryExpression*/,
2821 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002822 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002823 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002824 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002825 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002826
2827 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2828 // an expression.
2829 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002830 }
2831
Mike Stump11289f42009-09-09 15:08:12 +00002832 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002833 Toks.push_back(Tok);
2834 // Re-enter the stored parenthesized tokens into the token stream, so we may
2835 // parse them now.
2836 PP.EnterTokenStream(Toks.data(), Toks.size(),
2837 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2838 // Drop the current token and bring the first cached one. It's the same token
2839 // as when we entered this function.
2840 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002841
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002842 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002843 // Parse the type declarator.
2844 DeclSpec DS(AttrFactory);
2845 ParseSpecifierQualifierList(DS);
2846 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2847 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002848
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002849 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002850 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002851
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002852 if (ParseAs == CompoundLiteral) {
2853 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002854 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002855 return ParseCompoundLiteralExpression(Ty.get(),
2856 Tracker.getOpenLocation(),
2857 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002858 }
Mike Stump11289f42009-09-09 15:08:12 +00002859
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002860 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2861 assert(ParseAs == CastExpr);
2862
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002863 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002864 return ExprError();
2865
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002866 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002867 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002868 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2869 DeclaratorInfo, CastTy,
2870 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002871 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002872 }
Mike Stump11289f42009-09-09 15:08:12 +00002873
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002874 // Not a compound literal, and not followed by a cast-expression.
2875 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002876
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002877 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002878 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002879 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002880 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2881 Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002882
2883 // Match the ')'.
2884 if (Result.isInvalid()) {
2885 SkipUntil(tok::r_paren);
2886 return ExprError();
2887 }
Mike Stump11289f42009-09-09 15:08:12 +00002888
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002889 Tracker.consumeClose();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002890 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002891}