blob: 592a3cc160bcd15a959a812da634abb638e0f23a [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 Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner29375652006-12-04 18:06:35 +000015#include "clang/Parse/Parser.h"
Douglas Gregor94a32472011-01-11 00:33:19 +000016#include "RAIIObjectsForParser.h"
Eli Friedmanc7c97142012-01-04 02:40:39 +000017#include "clang/Basic/PrettyStackTrace.h"
Richard Smith7d182a72012-03-08 23:06:02 +000018#include "clang/Lex/LiteralSupport.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
Douglas Gregordb0b9f12011-08-04 15:30:47 +000020#include "clang/Sema/Scope.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.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
Mike Stump11289f42009-09-09 15:08:12 +000099/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000100///
101/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000102/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000103/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000104///
105/// '::'[opt] nested-name-specifier
106/// '::'
107///
108/// nested-name-specifier:
109/// type-name '::'
110/// namespace-name '::'
111/// nested-name-specifier identifier '::'
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000112/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000113///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000114///
Mike Stump11289f42009-09-09 15:08:12 +0000115/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000116/// nested-name-specifier (or empty)
117///
Mike Stump11289f42009-09-09 15:08:12 +0000118/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000119/// the "." or "->" of a member access expression, this parameter provides the
120/// type of the object whose members are being accessed.
121///
122/// \param EnteringContext whether we will be entering into the context of
123/// the nested-name-specifier after parsing it.
124///
Douglas Gregore610ada2010-02-24 18:44:31 +0000125/// \param MayBePseudoDestructor When non-NULL, points to a flag that
126/// indicates whether this nested-name-specifier may be part of a
127/// pseudo-destructor name. In this case, the flag will be set false
128/// if we don't actually end up parsing a destructor name. Moreorover,
129/// if we do end up determining that we are parsing a destructor name,
130/// the last component of the nested-name-specifier is not parsed as
131/// part of the scope specifier.
132
Douglas Gregor90d554e2010-02-21 18:36:56 +0000133/// member access expression, e.g., the \p T:: in \p p->T::m.
134///
John McCall1f476a12010-02-26 08:45:28 +0000135/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000136bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000137 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000138 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000139 bool *MayBePseudoDestructor,
140 bool IsTypename) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000141 assert(getLangOpts().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000142 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000143
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000144 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor869ad452011-02-24 17:54:50 +0000145 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
146 Tok.getAnnotationRange(),
147 SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000148 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +0000149 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000150 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000151
Douglas Gregor7f741122009-02-25 19:37:18 +0000152 bool HasScopeSpecifier = false;
153
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000154 if (Tok.is(tok::coloncolon)) {
155 // ::new and ::delete aren't nested-name-specifiers.
156 tok::TokenKind NextKind = NextToken().getKind();
157 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
158 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattner45ddec32009-01-05 00:13:00 +0000160 // '::' - Global scope qualifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000161 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
162 return true;
163
Douglas Gregor7f741122009-02-25 19:37:18 +0000164 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000165 }
166
Douglas Gregore610ada2010-02-24 18:44:31 +0000167 bool CheckForDestructor = false;
168 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
169 CheckForDestructor = true;
170 *MayBePseudoDestructor = false;
171 }
172
David Blaikie15a430a2011-12-04 05:04:18 +0000173 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
174 DeclSpec DS(AttrFactory);
175 SourceLocation DeclLoc = Tok.getLocation();
176 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
177 if (Tok.isNot(tok::coloncolon)) {
178 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
179 return false;
180 }
181
182 SourceLocation CCLoc = ConsumeToken();
183 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
184 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
185
186 HasScopeSpecifier = true;
187 }
188
Douglas Gregor7f741122009-02-25 19:37:18 +0000189 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000190 if (HasScopeSpecifier) {
191 // C++ [basic.lookup.classref]p5:
192 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000193 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000194 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000195 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000196 // the class-name-or-namespace-name is looked up in global scope as a
197 // class-name or namespace-name.
198 //
199 // To implement this, we clear out the object type as soon as we've
200 // seen a leading '::' or part of a nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000201 ObjectType = ParsedType();
Douglas Gregor2436e712009-09-17 21:32:03 +0000202
203 if (Tok.is(tok::code_completion)) {
204 // Code completion for a nested-name-specifier, where the code
205 // code completion token follows the '::'.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000206 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidis7d94c922011-04-23 01:04:12 +0000207 // Include code completion token into the range of the scope otherwise
208 // when we try to annotate the scope tokens the dangling code completion
209 // token will cause assertion in
210 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000211 SS.setEndLoc(Tok.getLocation());
212 cutOffParsing();
213 return true;
Douglas Gregor2436e712009-09-17 21:32:03 +0000214 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000215 }
Mike Stump11289f42009-09-09 15:08:12 +0000216
Douglas Gregor7f741122009-02-25 19:37:18 +0000217 // nested-name-specifier:
Chris Lattner0eed3a62009-06-26 03:47:46 +0000218 // nested-name-specifier 'template'[opt] simple-template-id '::'
219
220 // Parse the optional 'template' keyword, then make sure we have
221 // 'identifier <' after it.
222 if (Tok.is(tok::kw_template)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000223 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000224 // nested-name-specifier, since they aren't allowed to start with
225 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000226 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000227 break;
228
Douglas Gregor120635b2009-11-11 16:39:34 +0000229 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000230 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000231
232 UnqualifiedId TemplateName;
233 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000234 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000235 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000236 ConsumeToken();
237 } else if (Tok.is(tok::kw_operator)) {
238 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000239 TemplateName)) {
240 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000241 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000242 }
Douglas Gregor71395fa2009-11-04 00:56:37 +0000243
Alexis Hunted0530f2009-11-28 08:58:14 +0000244 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
245 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000246 Diag(TemplateName.getSourceRange().getBegin(),
247 diag::err_id_after_template_in_nested_name_spec)
248 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000249 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000250 break;
251 }
252 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000253 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000254 break;
255 }
Mike Stump11289f42009-09-09 15:08:12 +0000256
Douglas Gregor120635b2009-11-11 16:39:34 +0000257 // If the next token is not '<', we have a qualified-id that refers
258 // to a template name, such as T::template apply, but is not a
259 // template-id.
260 if (Tok.isNot(tok::less)) {
261 TPA.Revert();
262 break;
263 }
264
265 // Commit to parsing the template-id.
266 TPA.Commit();
Douglas Gregorbb119652010-06-16 23:00:59 +0000267 TemplateTy Template;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000268 if (TemplateNameKind TNK
269 = Actions.ActOnDependentTemplateName(getCurScope(),
270 SS, TemplateKWLoc, TemplateName,
271 ObjectType, EnteringContext,
272 Template)) {
273 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
274 TemplateName, false))
Douglas Gregorbb119652010-06-16 23:00:59 +0000275 return true;
276 } else
John McCall1f476a12010-02-26 08:45:28 +0000277 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000278
Chris Lattner0eed3a62009-06-26 03:47:46 +0000279 continue;
280 }
Mike Stump11289f42009-09-09 15:08:12 +0000281
Douglas Gregor7f741122009-02-25 19:37:18 +0000282 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000283 // We have
Douglas Gregor7f741122009-02-25 19:37:18 +0000284 //
285 // simple-template-id '::'
286 //
287 // So we need to check whether the simple-template-id is of the
Douglas Gregorb67535d2009-03-31 00:43:58 +0000288 // right kind (it should name a type or be dependent), and then
289 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000290 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000291 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
292 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000293 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000294 }
295
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000296 // Consume the template-id token.
297 ConsumeToken();
298
299 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
300 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000301
David Blaikie8c045bc2011-11-07 03:30:03 +0000302 HasScopeSpecifier = true;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000303
304 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
305 TemplateId->getTemplateArgs(),
306 TemplateId->NumArgs);
307
308 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000309 SS,
310 TemplateId->TemplateKWLoc,
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000311 TemplateId->Template,
312 TemplateId->TemplateNameLoc,
313 TemplateId->LAngleLoc,
314 TemplateArgsPtr,
315 TemplateId->RAngleLoc,
316 CCLoc,
317 EnteringContext)) {
318 SourceLocation StartLoc
319 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
320 : TemplateId->TemplateNameLoc;
321 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner704edfb2009-06-26 03:45:46 +0000322 }
Argyrios Kyrtzidis13935672011-05-03 18:45:38 +0000323
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000324 continue;
Douglas Gregor7f741122009-02-25 19:37:18 +0000325 }
326
Chris Lattnere2355f72009-06-26 03:52:38 +0000327
328 // The rest of the nested-name-specifier possibilities start with
329 // tok::identifier.
330 if (Tok.isNot(tok::identifier))
331 break;
332
333 IdentifierInfo &II = *Tok.getIdentifierInfo();
334
335 // nested-name-specifier:
336 // type-name '::'
337 // namespace-name '::'
338 // nested-name-specifier identifier '::'
339 Token Next = NextToken();
Chris Lattner1c428032009-12-07 01:36:53 +0000340
341 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
342 // and emit a fixit hint for it.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000343 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000344 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
345 Tok.getLocation(),
346 Next.getLocation(), ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000347 EnteringContext) &&
348 // If the token after the colon isn't an identifier, it's still an
349 // error, but they probably meant something else strange so don't
350 // recover like this.
351 PP.LookAhead(1).is(tok::identifier)) {
352 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregora771f462010-03-31 17:46:05 +0000353 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000354
355 // Recover as if the user wrote '::'.
356 Next.setKind(tok::coloncolon);
357 }
Chris Lattner1c428032009-12-07 01:36:53 +0000358 }
359
Chris Lattnere2355f72009-06-26 03:52:38 +0000360 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000361 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000362 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000363 II, ObjectType)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000364 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000365 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000366 }
367
Chris Lattnere2355f72009-06-26 03:52:38 +0000368 // We have an identifier followed by a '::'. Lookup this name
369 // as the name in a nested-name-specifier.
370 SourceLocation IdLoc = ConsumeToken();
Chris Lattner1c428032009-12-07 01:36:53 +0000371 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
372 "NextToken() not working properly!");
Chris Lattnere2355f72009-06-26 03:52:38 +0000373 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000374
Douglas Gregor90c99722011-02-24 00:17:56 +0000375 HasScopeSpecifier = true;
376 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
377 ObjectType, EnteringContext, SS))
378 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
379
Chris Lattnere2355f72009-06-26 03:52:38 +0000380 continue;
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Richard Trieu01fc0012011-09-19 19:01:00 +0000383 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smith55858492011-04-14 21:45:45 +0000384
Chris Lattnere2355f72009-06-26 03:52:38 +0000385 // nested-name-specifier:
386 // type-name '<'
387 if (Next.is(tok::less)) {
388 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000389 UnqualifiedId TemplateName;
390 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000391 bool MemberOfUnknownSpecialization;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000392 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000393 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000394 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000395 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000396 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000397 Template,
398 MemberOfUnknownSpecialization)) {
David Blaikie8c045bc2011-11-07 03:30:03 +0000399 // We have found a template name, so annotate this token
Chris Lattnere2355f72009-06-26 03:52:38 +0000400 // with a template-id annotation. We do not permit the
401 // template-id to be translated into a type annotation,
402 // because some clients (e.g., the parsing of class template
403 // specializations) still want to see the original template-id
404 // token.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000405 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000406 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
407 TemplateName, false))
John McCall1f476a12010-02-26 08:45:28 +0000408 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000409 continue;
Douglas Gregor20c38a72010-05-21 23:43:39 +0000410 }
411
412 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000413 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregor20c38a72010-05-21 23:43:39 +0000414 // We have something like t::getAs<T>, where getAs is a
415 // member of an unknown specialization. However, this will only
416 // parse correctly as a template, so suggest the keyword 'template'
417 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000418 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000419 if (getLangOpts().MicrosoftExt)
Francois Pichet93921652011-04-22 08:25:24 +0000420 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000421
422 Diag(Tok.getLocation(), DiagID)
Douglas Gregor20c38a72010-05-21 23:43:39 +0000423 << II.getName()
424 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
425
Douglas Gregorbb119652010-06-16 23:00:59 +0000426 if (TemplateNameKind TNK
Douglas Gregor0be31a22010-07-02 17:43:08 +0000427 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000428 SS, SourceLocation(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000429 TemplateName, ObjectType,
430 EnteringContext, Template)) {
431 // Consume the identifier.
432 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000433 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
434 TemplateName, false))
435 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000436 }
437 else
Douglas Gregor20c38a72010-05-21 23:43:39 +0000438 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000439
Douglas Gregor20c38a72010-05-21 23:43:39 +0000440 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000441 }
442 }
443
Douglas Gregor7f741122009-02-25 19:37:18 +0000444 // We don't have any tokens that form the beginning of a
445 // nested-name-specifier, so we're done.
446 break;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000447 }
Mike Stump11289f42009-09-09 15:08:12 +0000448
Douglas Gregore610ada2010-02-24 18:44:31 +0000449 // Even if we didn't see any pieces of a nested-name-specifier, we
450 // still check whether there is a tilde in this position, which
451 // indicates a potential pseudo-destructor.
452 if (CheckForDestructor && Tok.is(tok::tilde))
453 *MayBePseudoDestructor = true;
454
John McCall1f476a12010-02-26 08:45:28 +0000455 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000456}
457
458/// ParseCXXIdExpression - Handle id-expression.
459///
460/// id-expression:
461/// unqualified-id
462/// qualified-id
463///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000464/// qualified-id:
465/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
466/// '::' identifier
467/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000468/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000469///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000470/// NOTE: The standard specifies that, for qualified-id, the parser does not
471/// expect:
472///
473/// '::' conversion-function-id
474/// '::' '~' class-name
475///
476/// This may cause a slight inconsistency on diagnostics:
477///
478/// class C {};
479/// namespace A {}
480/// void f() {
481/// :: A :: ~ C(); // Some Sema error about using destructor with a
482/// // namespace.
483/// :: ~ C(); // Some Parser error like 'unexpected ~'.
484/// }
485///
486/// We simplify the parser a bit and make it work like:
487///
488/// qualified-id:
489/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
490/// '::' unqualified-id
491///
492/// That way Sema can handle and report similar errors for namespaces and the
493/// global scope.
494///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000495/// The isAddressOfOperand parameter indicates that this id-expression is a
496/// direct operand of the address-of operator. This is, besides member contexts,
497/// the only place where a qualified-id naming a non-static class member may
498/// appear.
499///
John McCalldadc5752010-08-24 06:29:42 +0000500ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000501 // qualified-id:
502 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
503 // '::' unqualified-id
504 //
505 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +0000506 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000507
508 SourceLocation TemplateKWLoc;
Douglas Gregora121b752009-11-03 16:56:39 +0000509 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000510 if (ParseUnqualifiedId(SS,
511 /*EnteringContext=*/false,
512 /*AllowDestructorName=*/false,
513 /*AllowConstructorName=*/false,
John McCallba7bf592010-08-24 05:47:05 +0000514 /*ObjectType=*/ ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000515 TemplateKWLoc,
Douglas Gregora121b752009-11-03 16:56:39 +0000516 Name))
517 return ExprError();
John McCalla9ee3252009-11-22 02:49:43 +0000518
519 // This is only the direct operand of an & operator if it is not
520 // followed by a postfix-expression suffix.
John McCall8d08b9b2010-08-27 09:08:28 +0000521 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
522 isAddressOfOperand = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000523
524 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
525 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000526}
527
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000528/// ParseLambdaExpression - Parse a C++0x lambda expression.
529///
530/// lambda-expression:
531/// lambda-introducer lambda-declarator[opt] compound-statement
532///
533/// lambda-introducer:
534/// '[' lambda-capture[opt] ']'
535///
536/// lambda-capture:
537/// capture-default
538/// capture-list
539/// capture-default ',' capture-list
540///
541/// capture-default:
542/// '&'
543/// '='
544///
545/// capture-list:
546/// capture
547/// capture-list ',' capture
548///
549/// capture:
550/// identifier
551/// '&' identifier
552/// 'this'
553///
554/// lambda-declarator:
555/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
556/// 'mutable'[opt] exception-specification[opt]
557/// trailing-return-type[opt]
558///
559ExprResult Parser::ParseLambdaExpression() {
560 // Parse lambda-introducer.
561 LambdaIntroducer Intro;
562
563 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
564 if (DiagID) {
565 Diag(Tok, DiagID.getValue());
566 SkipUntil(tok::r_square);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000567 SkipUntil(tok::l_brace);
568 SkipUntil(tok::r_brace);
569 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000570 }
571
572 return ParseLambdaExpressionAfterIntroducer(Intro);
573}
574
575/// TryParseLambdaExpression - Use lookahead and potentially tentative
576/// parsing to determine if we are looking at a C++0x lambda expression, and parse
577/// it if we are.
578///
579/// If we are not looking at a lambda expression, returns ExprError().
580ExprResult Parser::TryParseLambdaExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000581 assert(getLangOpts().CPlusPlus0x
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000582 && Tok.is(tok::l_square)
583 && "Not at the start of a possible lambda expression.");
584
585 const Token Next = NextToken(), After = GetLookAheadToken(2);
586
587 // If lookahead indicates this is a lambda...
588 if (Next.is(tok::r_square) || // []
589 Next.is(tok::equal) || // [=
590 (Next.is(tok::amp) && // [&] or [&,
591 (After.is(tok::r_square) ||
592 After.is(tok::comma))) ||
593 (Next.is(tok::identifier) && // [identifier]
594 After.is(tok::r_square))) {
595 return ParseLambdaExpression();
596 }
597
Eli Friedmanc7c97142012-01-04 02:40:39 +0000598 // If lookahead indicates an ObjC message send...
599 // [identifier identifier
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000600 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000601 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000602 }
603
Eli Friedmanc7c97142012-01-04 02:40:39 +0000604 // Here, we're stuck: lambda introducers and Objective-C message sends are
605 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
606 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
607 // writing two routines to parse a lambda introducer, just try to parse
608 // a lambda introducer first, and fall back if that fails.
609 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000610 LambdaIntroducer Intro;
611 if (TryParseLambdaIntroducer(Intro))
Eli Friedmanc7c97142012-01-04 02:40:39 +0000612 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000613 return ParseLambdaExpressionAfterIntroducer(Intro);
614}
615
616/// ParseLambdaExpression - Parse a lambda introducer.
617///
618/// Returns a DiagnosticID if it hit something unexpected.
Douglas Gregord8c61782012-02-15 15:34:24 +0000619llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro){
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000620 typedef llvm::Optional<unsigned> DiagResult;
621
622 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000623 BalancedDelimiterTracker T(*this, tok::l_square);
624 T.consumeOpen();
625
626 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000627
628 bool first = true;
629
630 // Parse capture-default.
631 if (Tok.is(tok::amp) &&
632 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
633 Intro.Default = LCD_ByRef;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000634 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000635 first = false;
636 } else if (Tok.is(tok::equal)) {
637 Intro.Default = LCD_ByCopy;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000638 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000639 first = false;
640 }
641
642 while (Tok.isNot(tok::r_square)) {
643 if (!first) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000644 if (Tok.isNot(tok::comma)) {
Douglas Gregor721b14d2012-07-31 00:50:07 +0000645 // Provide a completion for a lambda introducer here. Except
646 // in Objective-C, where this is Almost Surely meant to be a message
647 // send. In that case, fail here and let the ObjC message
648 // expression parser perform the completion.
Douglas Gregor2d8db8f2012-07-31 15:27:48 +0000649 if (Tok.is(tok::code_completion) &&
650 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
651 !Intro.Captures.empty())) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000652 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
653 /*AfterAmpersand=*/false);
654 ConsumeCodeCompletionToken();
655 break;
656 }
657
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000658 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregord8c61782012-02-15 15:34:24 +0000659 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000660 ConsumeToken();
661 }
662
Douglas Gregord8c61782012-02-15 15:34:24 +0000663 if (Tok.is(tok::code_completion)) {
664 // If we're in Objective-C++ and we have a bare '[', then this is more
665 // likely to be a message receiver.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000666 if (getLangOpts().ObjC1 && first)
Douglas Gregord8c61782012-02-15 15:34:24 +0000667 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
668 else
669 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
670 /*AfterAmpersand=*/false);
671 ConsumeCodeCompletionToken();
672 break;
673 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000674
Douglas Gregord8c61782012-02-15 15:34:24 +0000675 first = false;
676
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000677 // Parse capture.
678 LambdaCaptureKind Kind = LCK_ByCopy;
679 SourceLocation Loc;
680 IdentifierInfo* Id = 0;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000681 SourceLocation EllipsisLoc;
682
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000683 if (Tok.is(tok::kw_this)) {
684 Kind = LCK_This;
685 Loc = ConsumeToken();
686 } else {
687 if (Tok.is(tok::amp)) {
688 Kind = LCK_ByRef;
689 ConsumeToken();
Douglas Gregord8c61782012-02-15 15:34:24 +0000690
691 if (Tok.is(tok::code_completion)) {
692 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
693 /*AfterAmpersand=*/true);
694 ConsumeCodeCompletionToken();
695 break;
696 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000697 }
698
699 if (Tok.is(tok::identifier)) {
700 Id = Tok.getIdentifierInfo();
701 Loc = ConsumeToken();
Douglas Gregor3e308b12012-02-14 19:27:52 +0000702
703 if (Tok.is(tok::ellipsis))
704 EllipsisLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000705 } else if (Tok.is(tok::kw_this)) {
706 // FIXME: If we want to suggest a fixit here, will need to return more
707 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
708 // Clear()ed to prevent emission in case of tentative parsing?
709 return DiagResult(diag::err_this_captured_by_reference);
710 } else {
711 return DiagResult(diag::err_expected_capture);
712 }
713 }
714
Douglas Gregor3e308b12012-02-14 19:27:52 +0000715 Intro.addCapture(Kind, Loc, Id, EllipsisLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000716 }
717
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000718 T.consumeClose();
719 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000720
721 return DiagResult();
722}
723
Douglas Gregord8c61782012-02-15 15:34:24 +0000724/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000725///
726/// Returns true if it hit something unexpected.
727bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
728 TentativeParsingAction PA(*this);
729
730 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
731
732 if (DiagID) {
733 PA.Revert();
734 return true;
735 }
736
737 PA.Commit();
738 return false;
739}
740
741/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
742/// expression.
743ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
744 LambdaIntroducer &Intro) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000745 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
746 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
747
748 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
749 "lambda expression parsing");
750
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000751 // Parse lambda-declarator[opt].
752 DeclSpec DS(AttrFactory);
Eli Friedman36d12942012-01-04 04:41:38 +0000753 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000754
755 if (Tok.is(tok::l_paren)) {
756 ParseScope PrototypeScope(this,
757 Scope::FunctionPrototypeScope |
758 Scope::DeclScope);
759
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000760 SourceLocation DeclLoc, DeclEndLoc;
761 BalancedDelimiterTracker T(*this, tok::l_paren);
762 T.consumeOpen();
763 DeclLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000764
765 // Parse parameter-declaration-clause.
766 ParsedAttributes Attr(AttrFactory);
767 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
768 SourceLocation EllipsisLoc;
769
770 if (Tok.isNot(tok::r_paren))
771 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
772
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000773 T.consumeClose();
774 DeclEndLoc = T.getCloseLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000775
776 // Parse 'mutable'[opt].
777 SourceLocation MutableLoc;
778 if (Tok.is(tok::kw_mutable)) {
779 MutableLoc = ConsumeToken();
780 DeclEndLoc = MutableLoc;
781 }
782
783 // Parse exception-specification[opt].
784 ExceptionSpecificationType ESpecType = EST_None;
785 SourceRange ESpecRange;
786 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
787 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
788 ExprResult NoexceptExpr;
Richard Smith2331bbf2012-05-02 22:22:32 +0000789 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +0000790 DynamicExceptions,
791 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +0000792 NoexceptExpr);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000793
794 if (ESpecType != EST_None)
795 DeclEndLoc = ESpecRange.getEnd();
796
797 // Parse attribute-specifier[opt].
798 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
799
800 // Parse trailing-return-type[opt].
Richard Smith700537c2012-06-12 01:51:59 +0000801 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000802 if (Tok.is(tok::arrow)) {
803 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000804 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000805 if (Range.getEnd().isValid())
806 DeclEndLoc = Range.getEnd();
807 }
808
809 PrototypeScope.Exit();
810
811 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
812 /*isVariadic=*/EllipsisLoc.isValid(),
Richard Smith943c4402012-07-30 21:30:52 +0000813 /*isAmbiguous=*/false, EllipsisLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000814 ParamInfo.data(), ParamInfo.size(),
815 DS.getTypeQualifiers(),
816 /*RefQualifierIsLValueRef=*/true,
817 /*RefQualifierLoc=*/SourceLocation(),
Douglas Gregore248eea2011-10-19 06:04:55 +0000818 /*ConstQualifierLoc=*/SourceLocation(),
819 /*VolatileQualifierLoc=*/SourceLocation(),
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000820 MutableLoc,
821 ESpecType, ESpecRange.getBegin(),
822 DynamicExceptions.data(),
823 DynamicExceptionRanges.data(),
824 DynamicExceptions.size(),
825 NoexceptExpr.isUsable() ?
826 NoexceptExpr.get() : 0,
827 DeclLoc, DeclEndLoc, D,
828 TrailingReturnType),
829 Attr, DeclEndLoc);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000830 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
831 // It's common to forget that one needs '()' before 'mutable' or the
832 // result type. Deal with this.
833 Diag(Tok, diag::err_lambda_missing_parens)
834 << Tok.is(tok::arrow)
835 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
836 SourceLocation DeclLoc = Tok.getLocation();
837 SourceLocation DeclEndLoc = DeclLoc;
838
839 // Parse 'mutable', if it's there.
840 SourceLocation MutableLoc;
841 if (Tok.is(tok::kw_mutable)) {
842 MutableLoc = ConsumeToken();
843 DeclEndLoc = MutableLoc;
844 }
845
846 // Parse the return type, if there is one.
Richard Smith700537c2012-06-12 01:51:59 +0000847 TypeResult TrailingReturnType;
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000848 if (Tok.is(tok::arrow)) {
849 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000850 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000851 if (Range.getEnd().isValid())
852 DeclEndLoc = Range.getEnd();
853 }
854
855 ParsedAttributes Attr(AttrFactory);
856 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
857 /*isVariadic=*/false,
Richard Smith943c4402012-07-30 21:30:52 +0000858 /*isAmbiguous=*/false,
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000859 /*EllipsisLoc=*/SourceLocation(),
860 /*Params=*/0, /*NumParams=*/0,
861 /*TypeQuals=*/0,
862 /*RefQualifierIsLValueRef=*/true,
863 /*RefQualifierLoc=*/SourceLocation(),
864 /*ConstQualifierLoc=*/SourceLocation(),
865 /*VolatileQualifierLoc=*/SourceLocation(),
866 MutableLoc,
867 EST_None,
868 /*ESpecLoc=*/SourceLocation(),
869 /*Exceptions=*/0,
870 /*ExceptionRanges=*/0,
871 /*NumExceptions=*/0,
872 /*NoexceptExpr=*/0,
873 DeclLoc, DeclEndLoc, D,
874 TrailingReturnType),
875 Attr, DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000876 }
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000877
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000878
Eli Friedman4817cf72012-01-06 03:05:34 +0000879 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
880 // it.
Douglas Gregorb8389972012-02-21 22:51:27 +0000881 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorb8389972012-02-21 22:51:27 +0000882 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +0000883
Eli Friedman71c80552012-01-05 03:35:19 +0000884 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
885
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000886 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +0000887 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000888 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000889 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
890 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000891 }
892
Eli Friedmanc7c97142012-01-04 02:40:39 +0000893 StmtResult Stmt(ParseCompoundStatementBody());
894 BodyScope.Exit();
895
Eli Friedman898caf82012-01-04 02:46:53 +0000896 if (!Stmt.isInvalid())
Douglas Gregor63798542012-02-20 19:44:39 +0000897 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmanc7c97142012-01-04 02:40:39 +0000898
Eli Friedman898caf82012-01-04 02:46:53 +0000899 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
900 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000901}
902
Chris Lattner29375652006-12-04 18:06:35 +0000903/// ParseCXXCasts - This handles the various ways to cast expressions to another
904/// type.
905///
906/// postfix-expression: [C++ 5.2p1]
907/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
908/// 'static_cast' '<' type-name '>' '(' expression ')'
909/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
910/// 'const_cast' '<' type-name '>' '(' expression ')'
911///
John McCalldadc5752010-08-24 06:29:42 +0000912ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +0000913 tok::TokenKind Kind = Tok.getKind();
914 const char *CastName = 0; // For error messages
915
916 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +0000917 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +0000918 case tok::kw_const_cast: CastName = "const_cast"; break;
919 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
920 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
921 case tok::kw_static_cast: CastName = "static_cast"; break;
922 }
923
924 SourceLocation OpLoc = ConsumeToken();
925 SourceLocation LAngleBracketLoc = Tok.getLocation();
926
Richard Smith55858492011-04-14 21:45:45 +0000927 // Check for "<::" which is parsed as "[:". If found, fix token stream,
928 // diagnose error, suggest fix, and recover parsing.
929 Token Next = NextToken();
930 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
Richard Smith7b3f3222012-06-18 06:11:04 +0000931 areTokensAdjacent(Tok, Next))
Richard Smith55858492011-04-14 21:45:45 +0000932 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
933
Chris Lattner29375652006-12-04 18:06:35 +0000934 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +0000935 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000936
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000937 // Parse the common declaration-specifiers piece.
938 DeclSpec DS(AttrFactory);
939 ParseSpecifierQualifierList(DS);
940
941 // Parse the abstract-declarator, if present.
942 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
943 ParseDeclarator(DeclaratorInfo);
944
Chris Lattner29375652006-12-04 18:06:35 +0000945 SourceLocation RAngleBracketLoc = Tok.getLocation();
946
Chris Lattner6d29c102008-11-18 07:48:38 +0000947 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +0000948 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +0000949
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000950 SourceLocation LParenLoc, RParenLoc;
951 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +0000952
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000953 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000954 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000955
John McCalldadc5752010-08-24 06:29:42 +0000956 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +0000957
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000958 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000959 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +0000960
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000961 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +0000962 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000963 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +0000964 RAngleBracketLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000965 T.getOpenLocation(), Result.take(),
966 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +0000967
Sebastian Redld65cea82008-12-11 22:51:44 +0000968 return move(Result);
Chris Lattner29375652006-12-04 18:06:35 +0000969}
Bill Wendling4073ed52007-02-13 01:51:42 +0000970
Sebastian Redlc4704762008-11-11 11:37:55 +0000971/// ParseCXXTypeid - This handles the C++ typeid expression.
972///
973/// postfix-expression: [C++ 5.2p1]
974/// 'typeid' '(' expression ')'
975/// 'typeid' '(' type-id ')'
976///
John McCalldadc5752010-08-24 06:29:42 +0000977ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +0000978 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
979
980 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000981 SourceLocation LParenLoc, RParenLoc;
982 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +0000983
984 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000985 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +0000986 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000987 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +0000988
John McCalldadc5752010-08-24 06:29:42 +0000989 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +0000990
991 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +0000992 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +0000993
994 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000995 T.consumeClose();
996 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +0000997 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +0000998 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +0000999
1000 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001001 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001002 } else {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001003 // C++0x [expr.typeid]p3:
Mike Stump11289f42009-09-09 15:08:12 +00001004 // When typeid is applied to an expression other than an lvalue of a
1005 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001006 // operand (Clause 5).
1007 //
Mike Stump11289f42009-09-09 15:08:12 +00001008 // Note that we can't tell whether the expression is an lvalue of a
Eli Friedman456f0182012-01-20 01:26:23 +00001009 // polymorphic class type until after we've parsed the expression; we
1010 // speculatively assume the subexpression is unevaluated, and fix it up
1011 // later.
1012 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redlc4704762008-11-11 11:37:55 +00001013 Result = ParseExpression();
1014
1015 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001016 if (Result.isInvalid())
Sebastian Redlc4704762008-11-11 11:37:55 +00001017 SkipUntil(tok::r_paren);
1018 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001019 T.consumeClose();
1020 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001021 if (RParenLoc.isInvalid())
1022 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001023
Sebastian Redlc4704762008-11-11 11:37:55 +00001024 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001025 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001026 }
1027 }
1028
Sebastian Redld65cea82008-12-11 22:51:44 +00001029 return move(Result);
Sebastian Redlc4704762008-11-11 11:37:55 +00001030}
1031
Francois Pichet9f4f2072010-09-08 12:20:18 +00001032/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1033///
1034/// '__uuidof' '(' expression ')'
1035/// '__uuidof' '(' type-id ')'
1036///
1037ExprResult Parser::ParseCXXUuidof() {
1038 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1039
1040 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001041 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001042
1043 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001044 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +00001045 return ExprError();
1046
1047 ExprResult Result;
1048
1049 if (isTypeIdInParens()) {
1050 TypeResult Ty = ParseTypeName();
1051
1052 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001053 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001054
1055 if (Ty.isInvalid())
1056 return ExprError();
1057
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001058 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1059 Ty.get().getAsOpaquePtr(),
1060 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001061 } else {
1062 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1063 Result = ParseExpression();
1064
1065 // Match the ')'.
1066 if (Result.isInvalid())
1067 SkipUntil(tok::r_paren);
1068 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001069 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001070
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001071 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1072 /*isType=*/false,
1073 Result.release(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001074 }
1075 }
1076
1077 return move(Result);
1078}
1079
Douglas Gregore610ada2010-02-24 18:44:31 +00001080/// \brief Parse a C++ pseudo-destructor expression after the base,
1081/// . or -> operator, and nested-name-specifier have already been
1082/// parsed.
1083///
1084/// postfix-expression: [C++ 5.2]
1085/// postfix-expression . pseudo-destructor-name
1086/// postfix-expression -> pseudo-destructor-name
1087///
1088/// pseudo-destructor-name:
1089/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1090/// ::[opt] nested-name-specifier template simple-template-id ::
1091/// ~type-name
1092/// ::[opt] nested-name-specifier[opt] ~type-name
1093///
John McCalldadc5752010-08-24 06:29:42 +00001094ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +00001095Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1096 tok::TokenKind OpKind,
1097 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001098 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001099 // We're parsing either a pseudo-destructor-name or a dependent
1100 // member access that has the same form as a
1101 // pseudo-destructor-name. We parse both in the same way and let
1102 // the action model sort them out.
1103 //
1104 // Note that the ::[opt] nested-name-specifier[opt] has already
1105 // been parsed, and if there was a simple-template-id, it has
1106 // been coalesced into a template-id annotation token.
1107 UnqualifiedId FirstTypeName;
1108 SourceLocation CCLoc;
1109 if (Tok.is(tok::identifier)) {
1110 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1111 ConsumeToken();
1112 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1113 CCLoc = ConsumeToken();
1114 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001115 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1116 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001117 FirstTypeName.setTemplateId(
1118 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1119 ConsumeToken();
1120 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1121 CCLoc = ConsumeToken();
1122 } else {
1123 FirstTypeName.setIdentifier(0, SourceLocation());
1124 }
1125
1126 // Parse the tilde.
1127 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1128 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001129
1130 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1131 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001132 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-12-16 16:03:09 +00001133 if (DS.getTypeSpecType() == TST_error)
1134 return ExprError();
1135 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1136 OpKind, TildeLoc, DS,
1137 Tok.is(tok::l_paren));
1138 }
1139
Douglas Gregore610ada2010-02-24 18:44:31 +00001140 if (!Tok.is(tok::identifier)) {
1141 Diag(Tok, diag::err_destructor_tilde_identifier);
1142 return ExprError();
1143 }
1144
1145 // Parse the second type.
1146 UnqualifiedId SecondTypeName;
1147 IdentifierInfo *Name = Tok.getIdentifierInfo();
1148 SourceLocation NameLoc = ConsumeToken();
1149 SecondTypeName.setIdentifier(Name, NameLoc);
1150
1151 // If there is a '<', the second type name is a template-id. Parse
1152 // it as such.
1153 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001154 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1155 Name, NameLoc,
1156 false, ObjectType, SecondTypeName,
1157 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001158 return ExprError();
1159
John McCallb268a282010-08-23 23:25:46 +00001160 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1161 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001162 SS, FirstTypeName, CCLoc,
1163 TildeLoc, SecondTypeName,
1164 Tok.is(tok::l_paren));
1165}
1166
Bill Wendling4073ed52007-02-13 01:51:42 +00001167/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1168///
1169/// boolean-literal: [C++ 2.13.5]
1170/// 'true'
1171/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001172ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001173 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001174 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001175}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001176
1177/// ParseThrowExpression - This handles the C++ throw expression.
1178///
1179/// throw-expression: [C++ 15]
1180/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001181ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001182 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001183 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001184
Chris Lattner65dd8432008-04-06 06:02:23 +00001185 // If the current token isn't the start of an assignment-expression,
1186 // then the expression is not present. This handles things like:
1187 // "C ? throw : (void)42", which is crazy but legal.
1188 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1189 case tok::semi:
1190 case tok::r_paren:
1191 case tok::r_square:
1192 case tok::r_brace:
1193 case tok::colon:
1194 case tok::comma:
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001195 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001196
Chris Lattner65dd8432008-04-06 06:02:23 +00001197 default:
John McCalldadc5752010-08-24 06:29:42 +00001198 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redld65cea82008-12-11 22:51:44 +00001199 if (Expr.isInvalid()) return move(Expr);
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001200 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001201 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001202}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001203
1204/// ParseCXXThis - This handles the C++ 'this' pointer.
1205///
1206/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1207/// a non-lvalue expression whose value is the address of the object for which
1208/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001209ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001210 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1211 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001212 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001213}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001214
1215/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1216/// Can be interpreted either as function-style casting ("int(x)")
1217/// or class type construction ("ClassType(x,y,z)")
1218/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001219/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001220///
1221/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001222/// simple-type-specifier '(' expression-list[opt] ')'
1223/// [C++0x] simple-type-specifier braced-init-list
1224/// typename-specifier '(' expression-list[opt] ')'
1225/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001226///
John McCalldadc5752010-08-24 06:29:42 +00001227ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001228Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001229 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001230 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001231
Sebastian Redl3da34892011-06-05 12:23:16 +00001232 assert((Tok.is(tok::l_paren) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001233 (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001234 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001235
Sebastian Redl3da34892011-06-05 12:23:16 +00001236 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001237 ExprResult Init = ParseBraceInitializer();
1238 if (Init.isInvalid())
1239 return Init;
1240 Expr *InitList = Init.take();
1241 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1242 MultiExprArg(&InitList, 1),
1243 SourceLocation());
Sebastian Redl3da34892011-06-05 12:23:16 +00001244 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001245 BalancedDelimiterTracker T(*this, tok::l_paren);
1246 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001247
1248 ExprVector Exprs(Actions);
1249 CommaLocsTy CommaLocs;
1250
1251 if (Tok.isNot(tok::r_paren)) {
1252 if (ParseExpressionList(Exprs, CommaLocs)) {
1253 SkipUntil(tok::r_paren);
1254 return ExprError();
1255 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001256 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001257
1258 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001259 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001260
1261 // TypeRep could be null, if it references an invalid typedef.
1262 if (!TypeRep)
1263 return ExprError();
1264
1265 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1266 "Unexpected number of commas!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001267 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
1268 move_arg(Exprs),
1269 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001270 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001271}
1272
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001273/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001274///
1275/// condition:
1276/// expression
1277/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-02-22 06:49:09 +00001278/// [C++11] type-specifier-seq declarator '=' initializer-clause
1279/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001280/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1281/// '=' assignment-expression
1282///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001283/// \param ExprResult if the condition was parsed as an expression, the
1284/// parsed expression.
1285///
1286/// \param DeclResult if the condition was parsed as a declaration, the
1287/// parsed declaration.
1288///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001289/// \param Loc The location of the start of the statement that requires this
1290/// condition, e.g., the "for" in a for loop.
1291///
1292/// \param ConvertToBoolean Whether the condition expression should be
1293/// converted to a boolean value.
1294///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001295/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001296bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1297 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001298 SourceLocation Loc,
1299 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001300 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001301 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001302 cutOffParsing();
1303 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001304 }
1305
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001306 ParsedAttributesWithRange attrs(AttrFactory);
1307 MaybeParseCXX0XAttributes(attrs);
1308
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001309 if (!isCXXConditionDeclaration()) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001310 ProhibitAttributes(attrs);
1311
Douglas Gregore60e41a2010-05-06 17:25:47 +00001312 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001313 ExprOut = ParseExpression(); // expression
1314 DeclOut = 0;
1315 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001316 return true;
1317
1318 // If required, convert to a boolean value.
1319 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001320 ExprOut
1321 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1322 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001323 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001324
1325 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001326 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001327 ParseSpecifierQualifierList(DS);
1328
1329 // declarator
1330 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1331 ParseDeclarator(DeclaratorInfo);
1332
1333 // simple-asm-expr[opt]
1334 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001335 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001336 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001337 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001338 SkipUntil(tok::semi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001339 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001340 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001341 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001342 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001343 }
1344
1345 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001346 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001347
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001348 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001349 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001350 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001351 DeclOut = Dcl.get();
1352 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001353
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001354 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001355 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001356 bool CopyInitialization = isTokenEqualOrEqualTypo();
1357 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001358 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001359
1360 ExprResult InitExpr = ExprError();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001361 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-02-22 06:49:09 +00001362 Diag(Tok.getLocation(),
1363 diag::warn_cxx98_compat_generalized_initializer_lists);
1364 InitExpr = ParseBraceInitializer();
1365 } else if (CopyInitialization) {
1366 InitExpr = ParseAssignmentExpression();
1367 } else if (Tok.is(tok::l_paren)) {
1368 // This was probably an attempt to initialize the variable.
1369 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1370 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1371 RParen = ConsumeParen();
1372 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1373 diag::err_expected_init_in_condition_lparen)
1374 << SourceRange(LParen, RParen);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001375 } else {
Richard Smith2a15b742012-02-22 06:49:09 +00001376 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1377 diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001378 }
Richard Smith2a15b742012-02-22 06:49:09 +00001379
1380 if (!InitExpr.isInvalid())
1381 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
1382 DS.getTypeSpecType() == DeclSpec::TST_auto);
1383
Douglas Gregore60e41a2010-05-06 17:25:47 +00001384 // FIXME: Build a reference to this declaration? Convert it to bool?
1385 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001386
1387 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001388
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001389 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001390}
1391
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001392/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1393/// This should only be called when the current token is known to be part of
1394/// simple-type-specifier.
1395///
1396/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001397/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001398/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1399/// char
1400/// wchar_t
1401/// bool
1402/// short
1403/// int
1404/// long
1405/// signed
1406/// unsigned
1407/// float
1408/// double
1409/// void
1410/// [GNU] typeof-specifier
1411/// [C++0x] auto [TODO]
1412///
1413/// type-name:
1414/// class-name
1415/// enum-name
1416/// typedef-name
1417///
1418void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1419 DS.SetRangeStart(Tok.getLocation());
1420 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001421 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001422 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001423
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001424 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001425 case tok::identifier: // foo::bar
1426 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001427 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001428 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001429 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001430
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001431 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001432 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001433 if (getTypeAnnotation(Tok))
1434 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1435 getTypeAnnotation(Tok));
1436 else
1437 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001438
1439 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1440 ConsumeToken();
1441
1442 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1443 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1444 // Objective-C interface. If we don't have Objective-C or a '<', this is
1445 // just a normal reference to a typedef name.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001446 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001447 ParseObjCProtocolQualifiers(DS);
1448
1449 DS.Finish(Diags, PP);
1450 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001451 }
Mike Stump11289f42009-09-09 15:08:12 +00001452
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001453 // builtin types
1454 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001455 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001456 break;
1457 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +00001458 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001459 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001460 case tok::kw___int64:
1461 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1462 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001463 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001464 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001465 break;
1466 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001467 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001468 break;
1469 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001470 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001471 break;
1472 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001473 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001474 break;
1475 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001476 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001477 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00001478 case tok::kw___int128:
1479 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1480 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001481 case tok::kw_half:
1482 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1483 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001484 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001485 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001486 break;
1487 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001488 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001489 break;
1490 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001491 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001492 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001493 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001494 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001495 break;
1496 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001497 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001498 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001499 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +00001500 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001501 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001502 case tok::annot_decltype:
1503 case tok::kw_decltype:
1504 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1505 return DS.Finish(Diags, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001506
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001507 // GNU typeof support.
1508 case tok::kw_typeof:
1509 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +00001510 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001511 return;
1512 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001513 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001514 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1515 else
1516 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001517 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +00001518 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001519}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001520
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001521/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1522/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1523/// e.g., "const short int". Note that the DeclSpec is *not* finished
1524/// by parsing the type-specifier-seq, because these sequences are
1525/// typically followed by some form of declarator. Returns true and
1526/// emits diagnostics if this is not a type-specifier-seq, false
1527/// otherwise.
1528///
1529/// type-specifier-seq: [C++ 8.1]
1530/// type-specifier type-specifier-seq[opt]
1531///
1532bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smithc5b05522012-03-12 07:56:15 +00001533 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor40d732f2010-02-24 23:13:13 +00001534 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001535 return false;
1536}
1537
Douglas Gregor7861a802009-11-03 01:35:08 +00001538/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1539/// some form.
1540///
1541/// This routine is invoked when a '<' is encountered after an identifier or
1542/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1543/// whether the unqualified-id is actually a template-id. This routine will
1544/// then parse the template arguments and form the appropriate template-id to
1545/// return to the caller.
1546///
1547/// \param SS the nested-name-specifier that precedes this template-id, if
1548/// we're actually parsing a qualified-id.
1549///
1550/// \param Name for constructor and destructor names, this is the actual
1551/// identifier that may be a template-name.
1552///
1553/// \param NameLoc the location of the class-name in a constructor or
1554/// destructor.
1555///
1556/// \param EnteringContext whether we're entering the scope of the
1557/// nested-name-specifier.
1558///
Douglas Gregor127ea592009-11-03 21:24:04 +00001559/// \param ObjectType if this unqualified-id occurs within a member access
1560/// expression, the type of the base object whose member is being accessed.
1561///
Douglas Gregor7861a802009-11-03 01:35:08 +00001562/// \param Id as input, describes the template-name or operator-function-id
1563/// that precedes the '<'. If template arguments were parsed successfully,
1564/// will be updated with the template-id.
1565///
Douglas Gregore610ada2010-02-24 18:44:31 +00001566/// \param AssumeTemplateId When true, this routine will assume that the name
1567/// refers to a template without performing name lookup to verify.
1568///
Douglas Gregor7861a802009-11-03 01:35:08 +00001569/// \returns true if a parse error occurred, false otherwise.
1570bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001571 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001572 IdentifierInfo *Name,
1573 SourceLocation NameLoc,
1574 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001575 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001576 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001577 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001578 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1579 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001580
1581 TemplateTy Template;
1582 TemplateNameKind TNK = TNK_Non_template;
1583 switch (Id.getKind()) {
1584 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001585 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001586 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001587 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001588 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001589 Id, ObjectType, EnteringContext,
1590 Template);
1591 if (TNK == TNK_Non_template)
1592 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001593 } else {
1594 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001595 TNK = Actions.isTemplateName(getCurScope(), SS,
1596 TemplateKWLoc.isValid(), Id,
1597 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001598 MemberOfUnknownSpecialization);
1599
1600 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1601 ObjectType && IsTemplateArgumentList()) {
1602 // We have something like t->getAs<T>(), where getAs is a
1603 // member of an unknown specialization. However, this will only
1604 // parse correctly as a template, so suggest the keyword 'template'
1605 // before 'getAs' and treat this as a dependent template name.
1606 std::string Name;
1607 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1608 Name = Id.Identifier->getName();
1609 else {
1610 Name = "operator ";
1611 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1612 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1613 else
1614 Name += Id.Identifier->getName();
1615 }
1616 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1617 << Name
1618 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00001619 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1620 SS, TemplateKWLoc, Id,
1621 ObjectType, EnteringContext,
1622 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001623 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001624 return true;
1625 }
1626 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001627 break;
1628
Douglas Gregor3cf81312009-11-03 23:16:33 +00001629 case UnqualifiedId::IK_ConstructorName: {
1630 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001631 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001632 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001633 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1634 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001635 EnteringContext, Template,
1636 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001637 break;
1638 }
1639
Douglas Gregor3cf81312009-11-03 23:16:33 +00001640 case UnqualifiedId::IK_DestructorName: {
1641 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001642 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001643 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001644 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001645 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1646 SS, TemplateKWLoc, TemplateName,
1647 ObjectType, EnteringContext,
1648 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001649 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001650 return true;
1651 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001652 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1653 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001654 EnteringContext, Template,
1655 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001656
John McCallba7bf592010-08-24 05:47:05 +00001657 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001658 Diag(NameLoc, diag::err_destructor_template_id)
1659 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001660 return true;
1661 }
1662 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001663 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001664 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001665
1666 default:
1667 return false;
1668 }
1669
1670 if (TNK == TNK_Non_template)
1671 return false;
1672
1673 // Parse the enclosed template argument list.
1674 SourceLocation LAngleLoc, RAngleLoc;
1675 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001676 if (Tok.is(tok::less) &&
1677 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001678 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001679 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001680 RAngleLoc))
1681 return true;
1682
1683 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001684 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1685 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001686 // Form a parsed representation of the template-id to be stored in the
1687 // UnqualifiedId.
1688 TemplateIdAnnotation *TemplateId
Benjamin Kramer1e6b6062012-04-14 12:14:03 +00001689 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor7861a802009-11-03 01:35:08 +00001690
1691 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1692 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001693 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001694 TemplateId->TemplateNameLoc = Id.StartLocation;
1695 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001696 TemplateId->Name = 0;
1697 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1698 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001699 }
1700
Douglas Gregore7c20652011-03-02 00:47:37 +00001701 TemplateId->SS = SS;
Benjamin Kramer807c2db2012-02-19 23:37:39 +00001702 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall3e56fd42010-08-23 07:28:44 +00001703 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001704 TemplateId->Kind = TNK;
1705 TemplateId->LAngleLoc = LAngleLoc;
1706 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001707 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001708 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001709 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001710 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001711
1712 Id.setTemplateId(TemplateId);
1713 return false;
1714 }
1715
1716 // Bundle the template arguments together.
1717 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor7861a802009-11-03 01:35:08 +00001718 TemplateArgs.size());
Abramo Bagnara4244b432012-01-27 08:46:19 +00001719
Douglas Gregor7861a802009-11-03 01:35:08 +00001720 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001721 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001722 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1723 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00001724 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1725 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00001726 if (Type.isInvalid())
1727 return true;
1728
1729 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1730 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1731 else
1732 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1733
1734 return false;
1735}
1736
Douglas Gregor71395fa2009-11-04 00:56:37 +00001737/// \brief Parse an operator-function-id or conversion-function-id as part
1738/// of a C++ unqualified-id.
1739///
1740/// This routine is responsible only for parsing the operator-function-id or
1741/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001742///
1743/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001744/// operator-function-id: [C++ 13.5]
1745/// 'operator' operator
1746///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001747/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001748/// new delete new[] delete[]
1749/// + - * / % ^ & | ~
1750/// ! = < > += -= *= /= %=
1751/// ^= &= |= << >> >>= <<= == !=
1752/// <= >= && || ++ -- , ->* ->
1753/// () []
1754///
1755/// conversion-function-id: [C++ 12.3.2]
1756/// operator conversion-type-id
1757///
1758/// conversion-type-id:
1759/// type-specifier-seq conversion-declarator[opt]
1760///
1761/// conversion-declarator:
1762/// ptr-operator conversion-declarator[opt]
1763/// \endcode
1764///
1765/// \param The nested-name-specifier that preceded this unqualified-id. If
1766/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1767///
1768/// \param EnteringContext whether we are entering the scope of the
1769/// nested-name-specifier.
1770///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001771/// \param ObjectType if this unqualified-id occurs within a member access
1772/// expression, the type of the base object whose member is being accessed.
1773///
1774/// \param Result on a successful parse, contains the parsed unqualified-id.
1775///
1776/// \returns true if parsing fails, false otherwise.
1777bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001778 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001779 UnqualifiedId &Result) {
1780 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1781
1782 // Consume the 'operator' keyword.
1783 SourceLocation KeywordLoc = ConsumeToken();
1784
1785 // Determine what kind of operator name we have.
1786 unsigned SymbolIdx = 0;
1787 SourceLocation SymbolLocations[3];
1788 OverloadedOperatorKind Op = OO_None;
1789 switch (Tok.getKind()) {
1790 case tok::kw_new:
1791 case tok::kw_delete: {
1792 bool isNew = Tok.getKind() == tok::kw_new;
1793 // Consume the 'new' or 'delete'.
1794 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001795 // Check for array new/delete.
1796 if (Tok.is(tok::l_square) &&
1797 (!getLangOpts().CPlusPlus0x || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001798 // Consume the '[' and ']'.
1799 BalancedDelimiterTracker T(*this, tok::l_square);
1800 T.consumeOpen();
1801 T.consumeClose();
1802 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001803 return true;
1804
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001805 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1806 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001807 Op = isNew? OO_Array_New : OO_Array_Delete;
1808 } else {
1809 Op = isNew? OO_New : OO_Delete;
1810 }
1811 break;
1812 }
1813
1814#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1815 case tok::Token: \
1816 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1817 Op = OO_##Name; \
1818 break;
1819#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1820#include "clang/Basic/OperatorKinds.def"
1821
1822 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001823 // Consume the '(' and ')'.
1824 BalancedDelimiterTracker T(*this, tok::l_paren);
1825 T.consumeOpen();
1826 T.consumeClose();
1827 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001828 return true;
1829
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001830 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1831 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001832 Op = OO_Call;
1833 break;
1834 }
1835
1836 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001837 // Consume the '[' and ']'.
1838 BalancedDelimiterTracker T(*this, tok::l_square);
1839 T.consumeOpen();
1840 T.consumeClose();
1841 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001842 return true;
1843
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001844 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1845 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001846 Op = OO_Subscript;
1847 break;
1848 }
1849
1850 case tok::code_completion: {
1851 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001852 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001853 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001854 // Don't try to parse any further.
1855 return true;
1856 }
1857
1858 default:
1859 break;
1860 }
1861
1862 if (Op != OO_None) {
1863 // We have parsed an operator-function-id.
1864 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1865 return false;
1866 }
Alexis Hunt34458502009-11-28 04:44:28 +00001867
1868 // Parse a literal-operator-id.
1869 //
1870 // literal-operator-id: [C++0x 13.5.8]
1871 // operator "" identifier
1872
David Blaikiebbafb8a2012-03-11 07:00:24 +00001873 if (getLangOpts().CPlusPlus0x && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001874 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00001875
Richard Smith7d182a72012-03-08 23:06:02 +00001876 SourceLocation DiagLoc;
1877 unsigned DiagId = 0;
1878
1879 // We're past translation phase 6, so perform string literal concatenation
1880 // before checking for "".
1881 llvm::SmallVector<Token, 4> Toks;
1882 llvm::SmallVector<SourceLocation, 4> TokLocs;
1883 while (isTokenStringLiteral()) {
1884 if (!Tok.is(tok::string_literal) && !DiagId) {
1885 DiagLoc = Tok.getLocation();
1886 DiagId = diag::err_literal_operator_string_prefix;
1887 }
1888 Toks.push_back(Tok);
1889 TokLocs.push_back(ConsumeStringToken());
1890 }
1891
1892 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
1893 if (Literal.hadError)
1894 return true;
1895
1896 // Grab the literal operator's suffix, which will be either the next token
1897 // or a ud-suffix from the string literal.
1898 IdentifierInfo *II = 0;
1899 SourceLocation SuffixLoc;
1900 if (!Literal.getUDSuffix().empty()) {
1901 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
1902 SuffixLoc =
1903 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
1904 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00001905 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00001906 // This form is not permitted by the standard (yet).
1907 DiagLoc = SuffixLoc;
1908 DiagId = diag::err_literal_operator_missing_space;
1909 } else if (Tok.is(tok::identifier)) {
1910 II = Tok.getIdentifierInfo();
1911 SuffixLoc = ConsumeToken();
1912 TokLocs.push_back(SuffixLoc);
1913 } else {
Alexis Hunt34458502009-11-28 04:44:28 +00001914 Diag(Tok.getLocation(), diag::err_expected_ident);
1915 return true;
1916 }
1917
Richard Smith7d182a72012-03-08 23:06:02 +00001918 // The string literal must be empty.
1919 if (!Literal.GetString().empty() || Literal.Pascal) {
1920 DiagLoc = TokLocs.front();
1921 DiagId = diag::err_literal_operator_string_not_empty;
1922 }
1923
1924 if (DiagId) {
1925 // This isn't a valid literal-operator-id, but we think we know
1926 // what the user meant. Tell them what they should have written.
1927 llvm::SmallString<32> Str;
1928 Str += "\"\" ";
1929 Str += II->getName();
1930 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
1931 SourceRange(TokLocs.front(), TokLocs.back()), Str);
1932 }
1933
1934 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Alexis Hunt3d221f22009-11-29 07:34:05 +00001935 return false;
Alexis Hunt34458502009-11-28 04:44:28 +00001936 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00001937
1938 // Parse a conversion-function-id.
1939 //
1940 // conversion-function-id: [C++ 12.3.2]
1941 // operator conversion-type-id
1942 //
1943 // conversion-type-id:
1944 // type-specifier-seq conversion-declarator[opt]
1945 //
1946 // conversion-declarator:
1947 // ptr-operator conversion-declarator[opt]
1948
1949 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00001950 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00001951 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00001952 return true;
1953
1954 // Parse the conversion-declarator, which is merely a sequence of
1955 // ptr-operators.
1956 Declarator D(DS, Declarator::TypeNameContext);
1957 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1958
1959 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00001960 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00001961 if (Ty.isInvalid())
1962 return true;
1963
1964 // Note that this is a conversion-function-id.
1965 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1966 D.getSourceRange().getEnd());
1967 return false;
1968}
1969
1970/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1971/// name of an entity.
1972///
1973/// \code
1974/// unqualified-id: [C++ expr.prim.general]
1975/// identifier
1976/// operator-function-id
1977/// conversion-function-id
1978/// [C++0x] literal-operator-id [TODO]
1979/// ~ class-name
1980/// template-id
1981///
1982/// \endcode
1983///
1984/// \param The nested-name-specifier that preceded this unqualified-id. If
1985/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1986///
1987/// \param EnteringContext whether we are entering the scope of the
1988/// nested-name-specifier.
1989///
Douglas Gregor7861a802009-11-03 01:35:08 +00001990/// \param AllowDestructorName whether we allow parsing of a destructor name.
1991///
1992/// \param AllowConstructorName whether we allow parsing a constructor name.
1993///
Douglas Gregor127ea592009-11-03 21:24:04 +00001994/// \param ObjectType if this unqualified-id occurs within a member access
1995/// expression, the type of the base object whose member is being accessed.
1996///
Douglas Gregor7861a802009-11-03 01:35:08 +00001997/// \param Result on a successful parse, contains the parsed unqualified-id.
1998///
1999/// \returns true if parsing fails, false otherwise.
2000bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2001 bool AllowDestructorName,
2002 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002003 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002004 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002005 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002006
2007 // Handle 'A::template B'. This is for template-ids which have not
2008 // already been annotated by ParseOptionalCXXScopeSpecifier().
2009 bool TemplateSpecified = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002010 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002011 (ObjectType || SS.isSet())) {
2012 TemplateSpecified = true;
2013 TemplateKWLoc = ConsumeToken();
2014 }
2015
Douglas Gregor7861a802009-11-03 01:35:08 +00002016 // unqualified-id:
2017 // identifier
2018 // template-id (when it hasn't already been annotated)
2019 if (Tok.is(tok::identifier)) {
2020 // Consume the identifier.
2021 IdentifierInfo *Id = Tok.getIdentifierInfo();
2022 SourceLocation IdLoc = ConsumeToken();
2023
David Blaikiebbafb8a2012-03-11 07:00:24 +00002024 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002025 // If we're not in C++, only identifiers matter. Record the
2026 // identifier and return.
2027 Result.setIdentifier(Id, IdLoc);
2028 return false;
2029 }
2030
Douglas Gregor7861a802009-11-03 01:35:08 +00002031 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002032 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002033 // We have parsed a constructor name.
Abramo Bagnara4244b432012-01-27 08:46:19 +00002034 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2035 &SS, false, false,
2036 ParsedType(),
2037 /*IsCtorOrDtorName=*/true,
2038 /*NonTrivialTypeSourceInfo=*/true);
2039 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002040 } else {
2041 // We have parsed an identifier.
2042 Result.setIdentifier(Id, IdLoc);
2043 }
2044
2045 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00002046 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002047 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2048 EnteringContext, ObjectType,
2049 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002050
2051 return false;
2052 }
2053
2054 // unqualified-id:
2055 // template-id (already parsed and annotated)
2056 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002057 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002058
2059 // If the template-name names the current class, then this is a constructor
2060 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002061 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002062 if (SS.isSet()) {
2063 // C++ [class.qual]p2 specifies that a qualified template-name
2064 // is taken as the constructor name where a constructor can be
2065 // declared. Thus, the template arguments are extraneous, so
2066 // complain about them and remove them entirely.
2067 Diag(TemplateId->TemplateNameLoc,
2068 diag::err_out_of_line_constructor_template_id)
2069 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002070 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002071 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnara4244b432012-01-27 08:46:19 +00002072 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2073 TemplateId->TemplateNameLoc,
2074 getCurScope(),
2075 &SS, false, false,
2076 ParsedType(),
2077 /*IsCtorOrDtorName=*/true,
2078 /*NontrivialTypeSourceInfo=*/true);
2079 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002080 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002081 ConsumeToken();
2082 return false;
2083 }
2084
2085 Result.setConstructorTemplateId(TemplateId);
2086 ConsumeToken();
2087 return false;
2088 }
2089
Douglas Gregor7861a802009-11-03 01:35:08 +00002090 // We have already parsed a template-id; consume the annotation token as
2091 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002092 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002093 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00002094 ConsumeToken();
2095 return false;
2096 }
2097
2098 // unqualified-id:
2099 // operator-function-id
2100 // conversion-function-id
2101 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002102 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002103 return true;
2104
Alexis Hunted0530f2009-11-28 08:58:14 +00002105 // If we have an operator-function-id or a literal-operator-id and the next
2106 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00002107 //
2108 // template-id:
2109 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00002110 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2111 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002112 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002113 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2114 0, SourceLocation(),
2115 EnteringContext, ObjectType,
2116 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002117
Douglas Gregor7861a802009-11-03 01:35:08 +00002118 return false;
2119 }
2120
David Blaikiebbafb8a2012-03-11 07:00:24 +00002121 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002122 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002123 // C++ [expr.unary.op]p10:
2124 // There is an ambiguity in the unary-expression ~X(), where X is a
2125 // class-name. The ambiguity is resolved in favor of treating ~ as a
2126 // unary complement rather than treating ~X as referring to a destructor.
2127
2128 // Parse the '~'.
2129 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002130
2131 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2132 DeclSpec DS(AttrFactory);
2133 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2134 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2135 Result.setDestructorName(TildeLoc, Type, EndLoc);
2136 return false;
2137 }
2138 return true;
2139 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002140
2141 // Parse the class-name.
2142 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002143 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002144 return true;
2145 }
2146
2147 // Parse the class-name (or template-name in a simple-template-id).
2148 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2149 SourceLocation ClassNameLoc = ConsumeToken();
2150
Douglas Gregorb22ee882010-05-05 05:58:24 +00002151 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00002152 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002153 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2154 ClassName, ClassNameLoc,
2155 EnteringContext, ObjectType,
2156 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002157 }
2158
Douglas Gregor7861a802009-11-03 01:35:08 +00002159 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002160 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2161 ClassNameLoc, getCurScope(),
2162 SS, ObjectType,
2163 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002164 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002165 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002166
Douglas Gregor7861a802009-11-03 01:35:08 +00002167 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002168 return false;
2169 }
2170
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002171 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002172 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002173 return true;
2174}
2175
Sebastian Redlbd150f42008-11-21 19:14:01 +00002176/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2177/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002178///
Chris Lattner109faf22009-01-04 21:25:24 +00002179/// This method is called to parse the new expression after the optional :: has
2180/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2181/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002182///
2183/// new-expression:
2184/// '::'[opt] 'new' new-placement[opt] new-type-id
2185/// new-initializer[opt]
2186/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2187/// new-initializer[opt]
2188///
2189/// new-placement:
2190/// '(' expression-list ')'
2191///
Sebastian Redl351bb782008-12-02 14:43:59 +00002192/// new-type-id:
2193/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002194/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002195///
2196/// new-declarator:
2197/// ptr-operator new-declarator[opt]
2198/// direct-new-declarator
2199///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002200/// new-initializer:
2201/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002202/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002203///
John McCalldadc5752010-08-24 06:29:42 +00002204ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002205Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2206 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2207 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002208
2209 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2210 // second form of new-expression. It can't be a new-type-id.
2211
Sebastian Redl511ed552008-11-25 22:21:31 +00002212 ExprVector PlacementArgs(Actions);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002213 SourceLocation PlacementLParen, PlacementRParen;
2214
Douglas Gregorf2753b32010-07-13 15:54:32 +00002215 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002216 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002217 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002218 if (Tok.is(tok::l_paren)) {
2219 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002220 BalancedDelimiterTracker T(*this, tok::l_paren);
2221 T.consumeOpen();
2222 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002223 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2224 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002225 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002226 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002227
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002228 T.consumeClose();
2229 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002230 if (PlacementRParen.isInvalid()) {
2231 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002232 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002233 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002234
Sebastian Redl351bb782008-12-02 14:43:59 +00002235 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002236 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002237 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002238 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002239 } else {
2240 // We still need the type.
2241 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002242 BalancedDelimiterTracker T(*this, tok::l_paren);
2243 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002244 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002245 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002246 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002247 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002248 T.consumeClose();
2249 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002250 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002251 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002252 if (ParseCXXTypeSpecifierSeq(DS))
2253 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002254 else {
2255 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002256 ParseDeclaratorInternal(DeclaratorInfo,
2257 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002258 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002259 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002260 }
2261 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002262 // A new-type-id is a simplified type-id, where essentially the
2263 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002264 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002265 if (ParseCXXTypeSpecifierSeq(DS))
2266 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002267 else {
2268 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002269 ParseDeclaratorInternal(DeclaratorInfo,
2270 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002271 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002272 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002273 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002274 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002275 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002276 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002277
Sebastian Redl6047f072012-02-16 12:22:20 +00002278 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002279
2280 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002281 SourceLocation ConstructorLParen, ConstructorRParen;
2282 ExprVector ConstructorArgs(Actions);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002283 BalancedDelimiterTracker T(*this, tok::l_paren);
2284 T.consumeOpen();
2285 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002286 if (Tok.isNot(tok::r_paren)) {
2287 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002288 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2289 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002290 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002291 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002292 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002293 T.consumeClose();
2294 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002295 if (ConstructorRParen.isInvalid()) {
2296 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002297 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002298 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002299 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2300 ConstructorRParen,
2301 move_arg(ConstructorArgs));
David Blaikiebbafb8a2012-03-11 07:00:24 +00002302 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus0x) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002303 Diag(Tok.getLocation(),
2304 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002305 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002306 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002307 if (Initializer.isInvalid())
2308 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002309
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002310 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2311 move_arg(PlacementArgs), PlacementRParen,
Sebastian Redl6047f072012-02-16 12:22:20 +00002312 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002313}
2314
Sebastian Redlbd150f42008-11-21 19:14:01 +00002315/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2316/// passed to ParseDeclaratorInternal.
2317///
2318/// direct-new-declarator:
2319/// '[' expression ']'
2320/// direct-new-declarator '[' constant-expression ']'
2321///
Chris Lattner109faf22009-01-04 21:25:24 +00002322void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002323 // Parse the array dimensions.
2324 bool first = true;
2325 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002326 // An array-size expression can't start with a lambda.
2327 if (CheckProhibitedCXX11Attribute())
2328 continue;
2329
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002330 BalancedDelimiterTracker T(*this, tok::l_square);
2331 T.consumeOpen();
2332
John McCalldadc5752010-08-24 06:29:42 +00002333 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002334 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002335 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002336 // Recover
2337 SkipUntil(tok::r_square);
2338 return;
2339 }
2340 first = false;
2341
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002342 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002343
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002344 // Attributes here appertain to the array type. C++11 [expr.new]p5.
2345 ParsedAttributes Attrs(AttrFactory);
2346 MaybeParseCXX0XAttributes(Attrs);
2347
John McCall084e83d2011-03-24 11:26:52 +00002348 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002349 /*static=*/false, /*star=*/false,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002350 Size.release(),
2351 T.getOpenLocation(),
2352 T.getCloseLocation()),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002353 Attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002354
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002355 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002356 return;
2357 }
2358}
2359
2360/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2361/// This ambiguity appears in the syntax of the C++ new operator.
2362///
2363/// new-expression:
2364/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2365/// new-initializer[opt]
2366///
2367/// new-placement:
2368/// '(' expression-list ')'
2369///
John McCall37ad5512010-08-23 06:44:23 +00002370bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002371 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002372 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002373 // The '(' was already consumed.
2374 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002375 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002376 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002377 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002378 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002379 }
2380
2381 // It's not a type, it has to be an expression list.
2382 // Discard the comma locations - ActOnCXXNew has enough parameters.
2383 CommaLocsTy CommaLocs;
2384 return ParseExpressionList(PlacementArgs, CommaLocs);
2385}
2386
2387/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2388/// to free memory allocated by new.
2389///
Chris Lattner109faf22009-01-04 21:25:24 +00002390/// This method is called to parse the 'delete' expression after the optional
2391/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2392/// and "Start" is its location. Otherwise, "Start" is the location of the
2393/// 'delete' token.
2394///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002395/// delete-expression:
2396/// '::'[opt] 'delete' cast-expression
2397/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002398ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002399Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2400 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2401 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002402
2403 // Array delete?
2404 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002405 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
2406 // FIXME: This could be the start of a lambda-expression. We should
2407 // disambiguate this, but that will require arbitrary lookahead if
2408 // the next token is '(':
2409 // delete [](int*){ /* ... */
Sebastian Redlbd150f42008-11-21 19:14:01 +00002410 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002411 BalancedDelimiterTracker T(*this, tok::l_square);
2412
2413 T.consumeOpen();
2414 T.consumeClose();
2415 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002416 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002417 }
2418
John McCalldadc5752010-08-24 06:29:42 +00002419 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002420 if (Operand.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002421 return move(Operand);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002422
John McCallb268a282010-08-23 23:25:46 +00002423 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002424}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002425
Mike Stump11289f42009-09-09 15:08:12 +00002426static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002427 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002428 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002429 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002430 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002431 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002432 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Alexis Huntf479f1b2011-05-09 18:22:59 +00002433 case tok::kw___has_trivial_constructor:
2434 return UTT_HasTrivialDefaultConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002435 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002436 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2437 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2438 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley65497cc2011-04-27 23:09:49 +00002439 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2440 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002441 case tok::kw___is_class: return UTT_IsClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002442 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2443 case tok::kw___is_compound: return UTT_IsCompound;
2444 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002445 case tok::kw___is_empty: return UTT_IsEmpty;
2446 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregordca70af2011-12-03 18:14:24 +00002447 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley65497cc2011-04-27 23:09:49 +00002448 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2449 case tok::kw___is_function: return UTT_IsFunction;
2450 case tok::kw___is_fundamental: return UTT_IsFundamental;
2451 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley65497cc2011-04-27 23:09:49 +00002452 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2453 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2454 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2455 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2456 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth79803482011-04-23 10:47:20 +00002457 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00002458 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002459 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley65497cc2011-04-27 23:09:49 +00002460 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002461 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley65497cc2011-04-27 23:09:49 +00002462 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley65497cc2011-04-27 23:09:49 +00002463 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2464 case tok::kw___is_scalar: return UTT_IsScalar;
2465 case tok::kw___is_signed: return UTT_IsSigned;
2466 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2467 case tok::kw___is_trivial: return UTT_IsTrivial;
Alexis Huntd9a5cc12011-05-13 00:31:07 +00002468 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002469 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley65497cc2011-04-27 23:09:49 +00002470 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2471 case tok::kw___is_void: return UTT_IsVoid;
2472 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002473 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002474}
2475
2476static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2477 switch(kind) {
Francois Pichet347c4c72010-12-07 00:55:57 +00002478 default: llvm_unreachable("Not a known binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00002479 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley65497cc2011-04-27 23:09:49 +00002480 case tok::kw___is_convertible: return BTT_IsConvertible;
2481 case tok::kw___is_same: return BTT_IsSame;
Francois Pichet34b21132010-12-08 22:35:30 +00002482 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor8006e762011-01-27 20:28:01 +00002483 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor1be329d2012-02-23 07:33:15 +00002484 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002485 }
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002486}
2487
Douglas Gregor29c42f22012-02-24 07:38:34 +00002488static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2489 switch (kind) {
2490 default: llvm_unreachable("Not a known type trait");
2491 case tok::kw___is_trivially_constructible:
2492 return TT_IsTriviallyConstructible;
2493 }
2494}
2495
John Wiegley6242b6a2011-04-28 00:16:57 +00002496static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2497 switch(kind) {
2498 default: llvm_unreachable("Not a known binary type trait");
2499 case tok::kw___array_rank: return ATT_ArrayRank;
2500 case tok::kw___array_extent: return ATT_ArrayExtent;
2501 }
2502}
2503
John Wiegleyf9f65842011-04-25 06:54:41 +00002504static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2505 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002506 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002507 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2508 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2509 }
2510}
2511
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002512/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2513/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2514/// templates.
2515///
2516/// primary-expression:
2517/// [GNU] unary-type-trait '(' type-id ')'
2518///
John McCalldadc5752010-08-24 06:29:42 +00002519ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002520 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2521 SourceLocation Loc = ConsumeToken();
2522
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002523 BalancedDelimiterTracker T(*this, tok::l_paren);
2524 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002525 return ExprError();
2526
2527 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2528 // there will be cryptic errors about mismatched parentheses and missing
2529 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00002530 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002531
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002532 T.consumeClose();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002533
Douglas Gregor220cac52009-02-18 17:45:20 +00002534 if (Ty.isInvalid())
2535 return ExprError();
2536
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002537 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002538}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002539
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002540/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2541/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2542/// templates.
2543///
2544/// primary-expression:
2545/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2546///
2547ExprResult Parser::ParseBinaryTypeTrait() {
2548 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2549 SourceLocation Loc = ConsumeToken();
2550
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002551 BalancedDelimiterTracker T(*this, tok::l_paren);
2552 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002553 return ExprError();
2554
2555 TypeResult LhsTy = ParseTypeName();
2556 if (LhsTy.isInvalid()) {
2557 SkipUntil(tok::r_paren);
2558 return ExprError();
2559 }
2560
2561 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2562 SkipUntil(tok::r_paren);
2563 return ExprError();
2564 }
2565
2566 TypeResult RhsTy = ParseTypeName();
2567 if (RhsTy.isInvalid()) {
2568 SkipUntil(tok::r_paren);
2569 return ExprError();
2570 }
2571
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002572 T.consumeClose();
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002573
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002574 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2575 T.getCloseLocation());
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002576}
2577
Douglas Gregor29c42f22012-02-24 07:38:34 +00002578/// \brief Parse the built-in type-trait pseudo-functions that allow
2579/// implementation of the TR1/C++11 type traits templates.
2580///
2581/// primary-expression:
2582/// type-trait '(' type-id-seq ')'
2583///
2584/// type-id-seq:
2585/// type-id ...[opt] type-id-seq[opt]
2586///
2587ExprResult Parser::ParseTypeTrait() {
2588 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2589 SourceLocation Loc = ConsumeToken();
2590
2591 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2592 if (Parens.expectAndConsume(diag::err_expected_lparen))
2593 return ExprError();
2594
2595 llvm::SmallVector<ParsedType, 2> Args;
2596 do {
2597 // Parse the next type.
2598 TypeResult Ty = ParseTypeName();
2599 if (Ty.isInvalid()) {
2600 Parens.skipToEnd();
2601 return ExprError();
2602 }
2603
2604 // Parse the ellipsis, if present.
2605 if (Tok.is(tok::ellipsis)) {
2606 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2607 if (Ty.isInvalid()) {
2608 Parens.skipToEnd();
2609 return ExprError();
2610 }
2611 }
2612
2613 // Add this type to the list of arguments.
2614 Args.push_back(Ty.get());
2615
2616 if (Tok.is(tok::comma)) {
2617 ConsumeToken();
2618 continue;
2619 }
2620
2621 break;
2622 } while (true);
2623
2624 if (Parens.consumeClose())
2625 return ExprError();
2626
2627 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2628}
2629
John Wiegley6242b6a2011-04-28 00:16:57 +00002630/// ParseArrayTypeTrait - Parse the built-in array type-trait
2631/// pseudo-functions.
2632///
2633/// primary-expression:
2634/// [Embarcadero] '__array_rank' '(' type-id ')'
2635/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2636///
2637ExprResult Parser::ParseArrayTypeTrait() {
2638 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2639 SourceLocation Loc = ConsumeToken();
2640
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002641 BalancedDelimiterTracker T(*this, tok::l_paren);
2642 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley6242b6a2011-04-28 00:16:57 +00002643 return ExprError();
2644
2645 TypeResult Ty = ParseTypeName();
2646 if (Ty.isInvalid()) {
2647 SkipUntil(tok::comma);
2648 SkipUntil(tok::r_paren);
2649 return ExprError();
2650 }
2651
2652 switch (ATT) {
2653 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002654 T.consumeClose();
2655 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2656 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002657 }
2658 case ATT_ArrayExtent: {
2659 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2660 SkipUntil(tok::r_paren);
2661 return ExprError();
2662 }
2663
2664 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002665 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00002666
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002667 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2668 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002669 }
John Wiegley6242b6a2011-04-28 00:16:57 +00002670 }
David Blaikiee4d798f2012-01-20 21:50:17 +00002671 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00002672}
2673
John Wiegleyf9f65842011-04-25 06:54:41 +00002674/// ParseExpressionTrait - Parse built-in expression-trait
2675/// pseudo-functions like __is_lvalue_expr( xxx ).
2676///
2677/// primary-expression:
2678/// [Embarcadero] expression-trait '(' expression ')'
2679///
2680ExprResult Parser::ParseExpressionTrait() {
2681 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2682 SourceLocation Loc = ConsumeToken();
2683
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002684 BalancedDelimiterTracker T(*this, tok::l_paren);
2685 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegleyf9f65842011-04-25 06:54:41 +00002686 return ExprError();
2687
2688 ExprResult Expr = ParseExpression();
2689
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002690 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00002691
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002692 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2693 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00002694}
2695
2696
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002697/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2698/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2699/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002700ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002701Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002702 ParsedType &CastTy,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002703 BalancedDelimiterTracker &Tracker) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002704 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002705 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2706 assert(isTypeIdInParens() && "Not a type-id!");
2707
John McCalldadc5752010-08-24 06:29:42 +00002708 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002709 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002710
2711 // We need to disambiguate a very ugly part of the C++ syntax:
2712 //
2713 // (T())x; - type-id
2714 // (T())*x; - type-id
2715 // (T())/x; - expression
2716 // (T()); - expression
2717 //
2718 // The bad news is that we cannot use the specialized tentative parser, since
2719 // it can only verify that the thing inside the parens can be parsed as
2720 // type-id, it is not useful for determining the context past the parens.
2721 //
2722 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002723 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002724 //
2725 // It uses a scheme similar to parsing inline methods. The parenthesized
2726 // tokens are cached, the context that follows is determined (possibly by
2727 // parsing a cast-expression), and then we re-introduce the cached tokens
2728 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002729
Mike Stump11289f42009-09-09 15:08:12 +00002730 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002731 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002732
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002733 // Store the tokens of the parentheses. We will parse them after we determine
2734 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002735 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002736 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002737 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002738 return ExprError();
2739 }
2740
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002741 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002742 ParseAs = CompoundLiteral;
2743 } else {
2744 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002745 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2746 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2747 NotCastExpr = true;
2748 } else {
2749 // Try parsing the cast-expression that may follow.
2750 // If it is not a cast-expression, NotCastExpr will be true and no token
2751 // will be consumed.
2752 Result = ParseCastExpression(false/*isUnaryExpression*/,
2753 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002754 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002755 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002756 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002757 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002758
2759 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2760 // an expression.
2761 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002762 }
2763
Mike Stump11289f42009-09-09 15:08:12 +00002764 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002765 Toks.push_back(Tok);
2766 // Re-enter the stored parenthesized tokens into the token stream, so we may
2767 // parse them now.
2768 PP.EnterTokenStream(Toks.data(), Toks.size(),
2769 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2770 // Drop the current token and bring the first cached one. It's the same token
2771 // as when we entered this function.
2772 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002773
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002774 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002775 // Parse the type declarator.
2776 DeclSpec DS(AttrFactory);
2777 ParseSpecifierQualifierList(DS);
2778 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2779 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002780
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002781 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002782 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002783
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002784 if (ParseAs == CompoundLiteral) {
2785 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002786 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002787 return ParseCompoundLiteralExpression(Ty.get(),
2788 Tracker.getOpenLocation(),
2789 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002790 }
Mike Stump11289f42009-09-09 15:08:12 +00002791
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002792 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2793 assert(ParseAs == CastExpr);
2794
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002795 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002796 return ExprError();
2797
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002798 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002799 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002800 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2801 DeclaratorInfo, CastTy,
2802 Tracker.getCloseLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002803 return move(Result);
2804 }
Mike Stump11289f42009-09-09 15:08:12 +00002805
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002806 // Not a compound literal, and not followed by a cast-expression.
2807 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002808
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002809 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002810 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002811 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002812 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2813 Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002814
2815 // Match the ')'.
2816 if (Result.isInvalid()) {
2817 SkipUntil(tok::r_paren);
2818 return ExprError();
2819 }
Mike Stump11289f42009-09-09 15:08:12 +00002820
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002821 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002822 return move(Result);
2823}