blob: 19bc22020cf38adc46e1df1fc547140bdc3b8263 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000016#include "RAIIObjectsForParser.h"
Eli Friedmandc3b7232012-01-04 02:40:39 +000017#include "clang/Basic/PrettyStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
Douglas Gregorae7902c2011-08-04 15:30:47 +000019#include "clang/Sema/Scope.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000021#include "llvm/Support/ErrorHandling.h"
22
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Richard Smithea698b32011-04-14 21:45:45 +000025static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
26 switch (Kind) {
27 case tok::kw_template: return 0;
28 case tok::kw_const_cast: return 1;
29 case tok::kw_dynamic_cast: return 2;
30 case tok::kw_reinterpret_cast: return 3;
31 case tok::kw_static_cast: return 4;
32 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000033 llvm_unreachable("Unknown type for digraph error message.");
Richard Smithea698b32011-04-14 21:45:45 +000034 }
35}
36
37// Are the two tokens adjacent in the same source file?
38static bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
39 SourceManager &SM = PP.getSourceManager();
40 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000041 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smithea698b32011-04-14 21:45:45 +000042 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
43}
44
45// Suggest fixit for "<::" after a cast.
46static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
47 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
48 // Pull '<:' and ':' off token stream.
49 if (!AtDigraph)
50 PP.Lex(DigraphToken);
51 PP.Lex(ColonToken);
52
53 SourceRange Range;
54 Range.setBegin(DigraphToken.getLocation());
55 Range.setEnd(ColonToken.getLocation());
56 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
57 << SelectDigraphErrorMessage(Kind)
58 << FixItHint::CreateReplacement(Range, "< ::");
59
60 // Update token information to reflect their change in token type.
61 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000062 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smithea698b32011-04-14 21:45:45 +000063 ColonToken.setLength(2);
64 DigraphToken.setKind(tok::less);
65 DigraphToken.setLength(1);
66
67 // Push new tokens back to token stream.
68 PP.EnterToken(ColonToken);
69 if (!AtDigraph)
70 PP.EnterToken(DigraphToken);
71}
72
Richard Trieu950be712011-09-19 19:01:00 +000073// Check for '<::' which should be '< ::' instead of '[:' when following
74// a template name.
75void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
76 bool EnteringContext,
77 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieuc11030e2011-09-20 20:03:50 +000078 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu950be712011-09-19 19:01:00 +000079 return;
80
81 Token SecondToken = GetLookAheadToken(2);
82 if (!SecondToken.is(tok::colon) || !AreTokensAdjacent(PP, Next, SecondToken))
83 return;
84
85 TemplateTy Template;
86 UnqualifiedId TemplateName;
87 TemplateName.setIdentifier(&II, Tok.getLocation());
88 bool MemberOfUnknownSpecialization;
89 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
90 TemplateName, ObjectType, EnteringContext,
91 Template, MemberOfUnknownSpecialization))
92 return;
93
94 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
95 /*AtDigraph*/false);
96}
97
Mike Stump1eb44332009-09-09 15:08:12 +000098/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000099///
100/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000101/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000102/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000103///
104/// '::'[opt] nested-name-specifier
105/// '::'
106///
107/// nested-name-specifier:
108/// type-name '::'
109/// namespace-name '::'
110/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000111/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000112///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000113///
Mike Stump1eb44332009-09-09 15:08:12 +0000114/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000115/// nested-name-specifier (or empty)
116///
Mike Stump1eb44332009-09-09 15:08:12 +0000117/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000118/// the "." or "->" of a member access expression, this parameter provides the
119/// type of the object whose members are being accessed.
120///
121/// \param EnteringContext whether we will be entering into the context of
122/// the nested-name-specifier after parsing it.
123///
Douglas Gregord4dca082010-02-24 18:44:31 +0000124/// \param MayBePseudoDestructor When non-NULL, points to a flag that
125/// indicates whether this nested-name-specifier may be part of a
126/// pseudo-destructor name. In this case, the flag will be set false
127/// if we don't actually end up parsing a destructor name. Moreorover,
128/// if we do end up determining that we are parsing a destructor name,
129/// the last component of the nested-name-specifier is not parsed as
130/// part of the scope specifier.
131
Douglas Gregorb10cd042010-02-21 18:36:56 +0000132/// member access expression, e.g., the \p T:: in \p p->T::m.
133///
John McCall9ba61662010-02-26 08:45:28 +0000134/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000135bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000136 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000137 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000138 bool *MayBePseudoDestructor,
139 bool IsTypename) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000140 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000141 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000143 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +0000144 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
145 Tok.getAnnotationRange(),
146 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000147 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000148 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000149 }
Chris Lattnere607e802009-01-04 21:14:15 +0000150
Douglas Gregor39a8de12009-02-25 19:37:18 +0000151 bool HasScopeSpecifier = false;
152
Chris Lattner5b454732009-01-05 03:55:46 +0000153 if (Tok.is(tok::coloncolon)) {
154 // ::new and ::delete aren't nested-name-specifiers.
155 tok::TokenKind NextKind = NextToken().getKind();
156 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
157 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattner55a7cef2009-01-05 00:13:00 +0000159 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000160 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
161 return true;
162
Douglas Gregor39a8de12009-02-25 19:37:18 +0000163 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000164 }
165
Douglas Gregord4dca082010-02-24 18:44:31 +0000166 bool CheckForDestructor = false;
167 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
168 CheckForDestructor = true;
169 *MayBePseudoDestructor = false;
170 }
171
David Blaikie42d6d0c2011-12-04 05:04:18 +0000172 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
173 DeclSpec DS(AttrFactory);
174 SourceLocation DeclLoc = Tok.getLocation();
175 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
176 if (Tok.isNot(tok::coloncolon)) {
177 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
178 return false;
179 }
180
181 SourceLocation CCLoc = ConsumeToken();
182 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
183 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
184
185 HasScopeSpecifier = true;
186 }
187
Douglas Gregor39a8de12009-02-25 19:37:18 +0000188 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000189 if (HasScopeSpecifier) {
190 // C++ [basic.lookup.classref]p5:
191 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000192 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000193 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000194 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000195 // the class-name-or-namespace-name is looked up in global scope as a
196 // class-name or namespace-name.
197 //
198 // To implement this, we clear out the object type as soon as we've
199 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000200 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000201
202 if (Tok.is(tok::code_completion)) {
203 // Code completion for a nested-name-specifier, where the code
204 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000205 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000206 // Include code completion token into the range of the scope otherwise
207 // when we try to annotate the scope tokens the dangling code completion
208 // token will cause assertion in
209 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000210 SS.setEndLoc(Tok.getLocation());
211 cutOffParsing();
212 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000213 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Douglas Gregor39a8de12009-02-25 19:37:18 +0000216 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000217 // nested-name-specifier 'template'[opt] simple-template-id '::'
218
219 // Parse the optional 'template' keyword, then make sure we have
220 // 'identifier <' after it.
221 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000222 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000223 // nested-name-specifier, since they aren't allowed to start with
224 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000225 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000226 break;
227
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000228 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000229 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000230
231 UnqualifiedId TemplateName;
232 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000233 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000234 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000235 ConsumeToken();
236 } else if (Tok.is(tok::kw_operator)) {
237 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000238 TemplateName)) {
239 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000240 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000241 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000242
Sean Hunte6252d12009-11-28 08:58:14 +0000243 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
244 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000245 Diag(TemplateName.getSourceRange().getBegin(),
246 diag::err_id_after_template_in_nested_name_spec)
247 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000248 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000249 break;
250 }
251 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000252 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000253 break;
254 }
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000256 // If the next token is not '<', we have a qualified-id that refers
257 // to a template name, such as T::template apply, but is not a
258 // template-id.
259 if (Tok.isNot(tok::less)) {
260 TPA.Revert();
261 break;
262 }
263
264 // Commit to parsing the template-id.
265 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000266 TemplateTy Template;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000267 if (TemplateNameKind TNK
268 = Actions.ActOnDependentTemplateName(getCurScope(),
269 SS, TemplateKWLoc, TemplateName,
270 ObjectType, EnteringContext,
271 Template)) {
272 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
273 TemplateName, false))
Douglas Gregord6ab2322010-06-16 23:00:59 +0000274 return true;
275 } else
John McCall9ba61662010-02-26 08:45:28 +0000276 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner77cf72a2009-06-26 03:47:46 +0000278 continue;
279 }
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Douglas Gregor39a8de12009-02-25 19:37:18 +0000281 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000282 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000283 //
284 // simple-template-id '::'
285 //
286 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000287 // right kind (it should name a type or be dependent), and then
288 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000289 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000290 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
291 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000292 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000293 }
294
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000295 // Consume the template-id token.
296 ConsumeToken();
297
298 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
299 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000300
David Blaikie6796fc12011-11-07 03:30:03 +0000301 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000302
303 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
304 TemplateId->getTemplateArgs(),
305 TemplateId->NumArgs);
306
307 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000308 SS,
309 TemplateId->TemplateKWLoc,
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000310 TemplateId->Template,
311 TemplateId->TemplateNameLoc,
312 TemplateId->LAngleLoc,
313 TemplateArgsPtr,
314 TemplateId->RAngleLoc,
315 CCLoc,
316 EnteringContext)) {
317 SourceLocation StartLoc
318 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
319 : TemplateId->TemplateNameLoc;
320 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000321 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000322
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000323 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000324 }
325
Chris Lattner5c7f7862009-06-26 03:52:38 +0000326
327 // The rest of the nested-name-specifier possibilities start with
328 // tok::identifier.
329 if (Tok.isNot(tok::identifier))
330 break;
331
332 IdentifierInfo &II = *Tok.getIdentifierInfo();
333
334 // nested-name-specifier:
335 // type-name '::'
336 // namespace-name '::'
337 // nested-name-specifier identifier '::'
338 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000339
340 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
341 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000342 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000343 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
344 Tok.getLocation(),
345 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000346 EnteringContext) &&
347 // If the token after the colon isn't an identifier, it's still an
348 // error, but they probably meant something else strange so don't
349 // recover like this.
350 PP.LookAhead(1).is(tok::identifier)) {
351 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000352 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000353
354 // Recover as if the user wrote '::'.
355 Next.setKind(tok::coloncolon);
356 }
Chris Lattner46646492009-12-07 01:36:53 +0000357 }
358
Chris Lattner5c7f7862009-06-26 03:52:38 +0000359 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000360 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000361 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000362 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000363 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000364 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000365 }
366
Chris Lattner5c7f7862009-06-26 03:52:38 +0000367 // We have an identifier followed by a '::'. Lookup this name
368 // as the name in a nested-name-specifier.
369 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000370 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
371 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000372 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000374 HasScopeSpecifier = true;
375 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
376 ObjectType, EnteringContext, SS))
377 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
378
Chris Lattner5c7f7862009-06-26 03:52:38 +0000379 continue;
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Richard Trieu950be712011-09-19 19:01:00 +0000382 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000383
Chris Lattner5c7f7862009-06-26 03:52:38 +0000384 // nested-name-specifier:
385 // type-name '<'
386 if (Next.is(tok::less)) {
387 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000388 UnqualifiedId TemplateName;
389 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000390 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000391 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000392 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000393 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000394 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000395 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000396 Template,
397 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000398 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000399 // with a template-id annotation. We do not permit the
400 // template-id to be translated into a type annotation,
401 // because some clients (e.g., the parsing of class template
402 // specializations) still want to see the original template-id
403 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000404 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000405 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
406 TemplateName, false))
John McCall9ba61662010-02-26 08:45:28 +0000407 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000408 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000409 }
410
411 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000412 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000413 // We have something like t::getAs<T>, where getAs is a
414 // member of an unknown specialization. However, this will only
415 // parse correctly as a template, so suggest the keyword 'template'
416 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000417 unsigned DiagID = diag::err_missing_dependent_template_keyword;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000418 if (getLang().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000419 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000420
421 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000422 << II.getName()
423 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
424
Douglas Gregord6ab2322010-06-16 23:00:59 +0000425 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000426 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000427 SS, SourceLocation(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000428 TemplateName, ObjectType,
429 EnteringContext, Template)) {
430 // Consume the identifier.
431 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000432 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
433 TemplateName, false))
434 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000435 }
436 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000437 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000438
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000439 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000440 }
441 }
442
Douglas Gregor39a8de12009-02-25 19:37:18 +0000443 // We don't have any tokens that form the beginning of a
444 // nested-name-specifier, so we're done.
445 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Douglas Gregord4dca082010-02-24 18:44:31 +0000448 // Even if we didn't see any pieces of a nested-name-specifier, we
449 // still check whether there is a tilde in this position, which
450 // indicates a potential pseudo-destructor.
451 if (CheckForDestructor && Tok.is(tok::tilde))
452 *MayBePseudoDestructor = true;
453
John McCall9ba61662010-02-26 08:45:28 +0000454 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000455}
456
457/// ParseCXXIdExpression - Handle id-expression.
458///
459/// id-expression:
460/// unqualified-id
461/// qualified-id
462///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000463/// qualified-id:
464/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
465/// '::' identifier
466/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000467/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000468///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000469/// NOTE: The standard specifies that, for qualified-id, the parser does not
470/// expect:
471///
472/// '::' conversion-function-id
473/// '::' '~' class-name
474///
475/// This may cause a slight inconsistency on diagnostics:
476///
477/// class C {};
478/// namespace A {}
479/// void f() {
480/// :: A :: ~ C(); // Some Sema error about using destructor with a
481/// // namespace.
482/// :: ~ C(); // Some Parser error like 'unexpected ~'.
483/// }
484///
485/// We simplify the parser a bit and make it work like:
486///
487/// qualified-id:
488/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
489/// '::' unqualified-id
490///
491/// That way Sema can handle and report similar errors for namespaces and the
492/// global scope.
493///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000494/// The isAddressOfOperand parameter indicates that this id-expression is a
495/// direct operand of the address-of operator. This is, besides member contexts,
496/// the only place where a qualified-id naming a non-static class member may
497/// appear.
498///
John McCall60d7b3a2010-08-24 06:29:42 +0000499ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000500 // qualified-id:
501 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
502 // '::' unqualified-id
503 //
504 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000505 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000506
507 SourceLocation TemplateKWLoc;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000508 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000509 if (ParseUnqualifiedId(SS,
510 /*EnteringContext=*/false,
511 /*AllowDestructorName=*/false,
512 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000513 /*ObjectType=*/ ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000514 TemplateKWLoc,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000515 Name))
516 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000517
518 // This is only the direct operand of an & operator if it is not
519 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000520 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
521 isAddressOfOperand = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000522
523 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
524 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000525}
526
Douglas Gregorae7902c2011-08-04 15:30:47 +0000527/// ParseLambdaExpression - Parse a C++0x lambda expression.
528///
529/// lambda-expression:
530/// lambda-introducer lambda-declarator[opt] compound-statement
531///
532/// lambda-introducer:
533/// '[' lambda-capture[opt] ']'
534///
535/// lambda-capture:
536/// capture-default
537/// capture-list
538/// capture-default ',' capture-list
539///
540/// capture-default:
541/// '&'
542/// '='
543///
544/// capture-list:
545/// capture
546/// capture-list ',' capture
547///
548/// capture:
549/// identifier
550/// '&' identifier
551/// 'this'
552///
553/// lambda-declarator:
554/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
555/// 'mutable'[opt] exception-specification[opt]
556/// trailing-return-type[opt]
557///
558ExprResult Parser::ParseLambdaExpression() {
559 // Parse lambda-introducer.
560 LambdaIntroducer Intro;
561
562 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
563 if (DiagID) {
564 Diag(Tok, DiagID.getValue());
565 SkipUntil(tok::r_square);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000566 SkipUntil(tok::l_brace);
567 SkipUntil(tok::r_brace);
568 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000569 }
570
571 return ParseLambdaExpressionAfterIntroducer(Intro);
572}
573
574/// TryParseLambdaExpression - Use lookahead and potentially tentative
575/// parsing to determine if we are looking at a C++0x lambda expression, and parse
576/// it if we are.
577///
578/// If we are not looking at a lambda expression, returns ExprError().
579ExprResult Parser::TryParseLambdaExpression() {
580 assert(getLang().CPlusPlus0x
581 && Tok.is(tok::l_square)
582 && "Not at the start of a possible lambda expression.");
583
584 const Token Next = NextToken(), After = GetLookAheadToken(2);
585
586 // If lookahead indicates this is a lambda...
587 if (Next.is(tok::r_square) || // []
588 Next.is(tok::equal) || // [=
589 (Next.is(tok::amp) && // [&] or [&,
590 (After.is(tok::r_square) ||
591 After.is(tok::comma))) ||
592 (Next.is(tok::identifier) && // [identifier]
593 After.is(tok::r_square))) {
594 return ParseLambdaExpression();
595 }
596
Eli Friedmandc3b7232012-01-04 02:40:39 +0000597 // If lookahead indicates an ObjC message send...
598 // [identifier identifier
Douglas Gregorae7902c2011-08-04 15:30:47 +0000599 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000600 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000601 }
602
Eli Friedmandc3b7232012-01-04 02:40:39 +0000603 // Here, we're stuck: lambda introducers and Objective-C message sends are
604 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
605 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
606 // writing two routines to parse a lambda introducer, just try to parse
607 // a lambda introducer first, and fall back if that fails.
608 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregorae7902c2011-08-04 15:30:47 +0000609 LambdaIntroducer Intro;
610 if (TryParseLambdaIntroducer(Intro))
Eli Friedmandc3b7232012-01-04 02:40:39 +0000611 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000612 return ParseLambdaExpressionAfterIntroducer(Intro);
613}
614
615/// ParseLambdaExpression - Parse a lambda introducer.
616///
617/// Returns a DiagnosticID if it hit something unexpected.
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000618llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro){
Douglas Gregorae7902c2011-08-04 15:30:47 +0000619 typedef llvm::Optional<unsigned> DiagResult;
620
621 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000622 BalancedDelimiterTracker T(*this, tok::l_square);
623 T.consumeOpen();
624
625 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000626
627 bool first = true;
628
629 // Parse capture-default.
630 if (Tok.is(tok::amp) &&
631 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
632 Intro.Default = LCD_ByRef;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000633 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000634 first = false;
635 } else if (Tok.is(tok::equal)) {
636 Intro.Default = LCD_ByCopy;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000637 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000638 first = false;
639 }
640
641 while (Tok.isNot(tok::r_square)) {
642 if (!first) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000643 if (Tok.isNot(tok::comma)) {
644 if (Tok.is(tok::code_completion)) {
645 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
646 /*AfterAmpersand=*/false);
647 ConsumeCodeCompletionToken();
648 break;
649 }
650
Douglas Gregorae7902c2011-08-04 15:30:47 +0000651 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000652 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000653 ConsumeToken();
654 }
655
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000656 if (Tok.is(tok::code_completion)) {
657 // If we're in Objective-C++ and we have a bare '[', then this is more
658 // likely to be a message receiver.
659 if (getLang().ObjC1 && first)
660 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
661 else
662 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
663 /*AfterAmpersand=*/false);
664 ConsumeCodeCompletionToken();
665 break;
666 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000667
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000668 first = false;
669
Douglas Gregorae7902c2011-08-04 15:30:47 +0000670 // Parse capture.
671 LambdaCaptureKind Kind = LCK_ByCopy;
672 SourceLocation Loc;
673 IdentifierInfo* Id = 0;
Douglas Gregora7365242012-02-14 19:27:52 +0000674 SourceLocation EllipsisLoc;
675
Douglas Gregorae7902c2011-08-04 15:30:47 +0000676 if (Tok.is(tok::kw_this)) {
677 Kind = LCK_This;
678 Loc = ConsumeToken();
679 } else {
680 if (Tok.is(tok::amp)) {
681 Kind = LCK_ByRef;
682 ConsumeToken();
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000683
684 if (Tok.is(tok::code_completion)) {
685 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
686 /*AfterAmpersand=*/true);
687 ConsumeCodeCompletionToken();
688 break;
689 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000690 }
691
692 if (Tok.is(tok::identifier)) {
693 Id = Tok.getIdentifierInfo();
694 Loc = ConsumeToken();
Douglas Gregora7365242012-02-14 19:27:52 +0000695
696 if (Tok.is(tok::ellipsis))
697 EllipsisLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000698 } else if (Tok.is(tok::kw_this)) {
699 // FIXME: If we want to suggest a fixit here, will need to return more
700 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
701 // Clear()ed to prevent emission in case of tentative parsing?
702 return DiagResult(diag::err_this_captured_by_reference);
703 } else {
704 return DiagResult(diag::err_expected_capture);
705 }
706 }
707
Douglas Gregora7365242012-02-14 19:27:52 +0000708 Intro.addCapture(Kind, Loc, Id, EllipsisLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000709 }
710
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000711 T.consumeClose();
712 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000713
714 return DiagResult();
715}
716
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000717/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000718///
719/// Returns true if it hit something unexpected.
720bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
721 TentativeParsingAction PA(*this);
722
723 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
724
725 if (DiagID) {
726 PA.Revert();
727 return true;
728 }
729
730 PA.Commit();
731 return false;
732}
733
734/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
735/// expression.
736ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
737 LambdaIntroducer &Intro) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000738 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
739 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
740
741 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
742 "lambda expression parsing");
743
Douglas Gregorae7902c2011-08-04 15:30:47 +0000744 // Parse lambda-declarator[opt].
745 DeclSpec DS(AttrFactory);
Eli Friedmanf88c4002012-01-04 04:41:38 +0000746 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000747
748 if (Tok.is(tok::l_paren)) {
749 ParseScope PrototypeScope(this,
750 Scope::FunctionPrototypeScope |
751 Scope::DeclScope);
752
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000753 SourceLocation DeclLoc, DeclEndLoc;
754 BalancedDelimiterTracker T(*this, tok::l_paren);
755 T.consumeOpen();
756 DeclLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000757
758 // Parse parameter-declaration-clause.
759 ParsedAttributes Attr(AttrFactory);
760 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
761 SourceLocation EllipsisLoc;
762
763 if (Tok.isNot(tok::r_paren))
764 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
765
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000766 T.consumeClose();
767 DeclEndLoc = T.getCloseLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000768
769 // Parse 'mutable'[opt].
770 SourceLocation MutableLoc;
771 if (Tok.is(tok::kw_mutable)) {
772 MutableLoc = ConsumeToken();
773 DeclEndLoc = MutableLoc;
774 }
775
776 // Parse exception-specification[opt].
777 ExceptionSpecificationType ESpecType = EST_None;
778 SourceRange ESpecRange;
779 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
780 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
781 ExprResult NoexceptExpr;
782 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
783 DynamicExceptions,
784 DynamicExceptionRanges,
785 NoexceptExpr);
786
787 if (ESpecType != EST_None)
788 DeclEndLoc = ESpecRange.getEnd();
789
790 // Parse attribute-specifier[opt].
791 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
792
793 // Parse trailing-return-type[opt].
794 ParsedType TrailingReturnType;
795 if (Tok.is(tok::arrow)) {
796 SourceRange Range;
797 TrailingReturnType = ParseTrailingReturnType(Range).get();
798 if (Range.getEnd().isValid())
799 DeclEndLoc = Range.getEnd();
800 }
801
802 PrototypeScope.Exit();
803
804 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
805 /*isVariadic=*/EllipsisLoc.isValid(),
806 EllipsisLoc,
807 ParamInfo.data(), ParamInfo.size(),
808 DS.getTypeQualifiers(),
809 /*RefQualifierIsLValueRef=*/true,
810 /*RefQualifierLoc=*/SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +0000811 /*ConstQualifierLoc=*/SourceLocation(),
812 /*VolatileQualifierLoc=*/SourceLocation(),
Douglas Gregorae7902c2011-08-04 15:30:47 +0000813 MutableLoc,
814 ESpecType, ESpecRange.getBegin(),
815 DynamicExceptions.data(),
816 DynamicExceptionRanges.data(),
817 DynamicExceptions.size(),
818 NoexceptExpr.isUsable() ?
819 NoexceptExpr.get() : 0,
820 DeclLoc, DeclEndLoc, D,
821 TrailingReturnType),
822 Attr, DeclEndLoc);
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000823 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
824 // It's common to forget that one needs '()' before 'mutable' or the
825 // result type. Deal with this.
826 Diag(Tok, diag::err_lambda_missing_parens)
827 << Tok.is(tok::arrow)
828 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
829 SourceLocation DeclLoc = Tok.getLocation();
830 SourceLocation DeclEndLoc = DeclLoc;
831
832 // Parse 'mutable', if it's there.
833 SourceLocation MutableLoc;
834 if (Tok.is(tok::kw_mutable)) {
835 MutableLoc = ConsumeToken();
836 DeclEndLoc = MutableLoc;
837 }
838
839 // Parse the return type, if there is one.
840 ParsedType TrailingReturnType;
841 if (Tok.is(tok::arrow)) {
842 SourceRange Range;
843 TrailingReturnType = ParseTrailingReturnType(Range).get();
844 if (Range.getEnd().isValid())
845 DeclEndLoc = Range.getEnd();
846 }
847
848 ParsedAttributes Attr(AttrFactory);
849 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
850 /*isVariadic=*/false,
851 /*EllipsisLoc=*/SourceLocation(),
852 /*Params=*/0, /*NumParams=*/0,
853 /*TypeQuals=*/0,
854 /*RefQualifierIsLValueRef=*/true,
855 /*RefQualifierLoc=*/SourceLocation(),
856 /*ConstQualifierLoc=*/SourceLocation(),
857 /*VolatileQualifierLoc=*/SourceLocation(),
858 MutableLoc,
859 EST_None,
860 /*ESpecLoc=*/SourceLocation(),
861 /*Exceptions=*/0,
862 /*ExceptionRanges=*/0,
863 /*NumExceptions=*/0,
864 /*NoexceptExpr=*/0,
865 DeclLoc, DeclEndLoc, D,
866 TrailingReturnType),
867 Attr, DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000868 }
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000869
Douglas Gregorae7902c2011-08-04 15:30:47 +0000870
Eli Friedman906a7e12012-01-06 03:05:34 +0000871 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
872 // it.
Douglas Gregorfccfb622012-02-21 22:51:27 +0000873 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
874 if (getCurScope()->getFlags() & Scope::ThisScope)
875 ScopeFlags |= Scope::ThisScope;
876 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman906a7e12012-01-06 03:05:34 +0000877
Eli Friedmanec9ea722012-01-05 03:35:19 +0000878 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
879
Douglas Gregorae7902c2011-08-04 15:30:47 +0000880 // Parse compound-statement.
Eli Friedmandc3b7232012-01-04 02:40:39 +0000881 if (!Tok.is(tok::l_brace)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +0000882 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000883 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
884 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000885 }
886
Eli Friedmandc3b7232012-01-04 02:40:39 +0000887 StmtResult Stmt(ParseCompoundStatementBody());
888 BodyScope.Exit();
889
Eli Friedmandeeab902012-01-04 02:46:53 +0000890 if (!Stmt.isInvalid())
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000891 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmandc3b7232012-01-04 02:40:39 +0000892
Eli Friedmandeeab902012-01-04 02:46:53 +0000893 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
894 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000895}
896
Reid Spencer5f016e22007-07-11 17:01:13 +0000897/// ParseCXXCasts - This handles the various ways to cast expressions to another
898/// type.
899///
900/// postfix-expression: [C++ 5.2p1]
901/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
902/// 'static_cast' '<' type-name '>' '(' expression ')'
903/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
904/// 'const_cast' '<' type-name '>' '(' expression ')'
905///
John McCall60d7b3a2010-08-24 06:29:42 +0000906ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 tok::TokenKind Kind = Tok.getKind();
908 const char *CastName = 0; // For error messages
909
910 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000911 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 case tok::kw_const_cast: CastName = "const_cast"; break;
913 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
914 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
915 case tok::kw_static_cast: CastName = "static_cast"; break;
916 }
917
918 SourceLocation OpLoc = ConsumeToken();
919 SourceLocation LAngleBracketLoc = Tok.getLocation();
920
Richard Smithea698b32011-04-14 21:45:45 +0000921 // Check for "<::" which is parsed as "[:". If found, fix token stream,
922 // diagnose error, suggest fix, and recover parsing.
923 Token Next = NextToken();
924 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
925 AreTokensAdjacent(PP, Tok, Next))
926 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
927
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000929 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000930
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000931 // Parse the common declaration-specifiers piece.
932 DeclSpec DS(AttrFactory);
933 ParseSpecifierQualifierList(DS);
934
935 // Parse the abstract-declarator, if present.
936 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
937 ParseDeclarator(DeclaratorInfo);
938
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 SourceLocation RAngleBracketLoc = Tok.getLocation();
940
Chris Lattner1ab3b962008-11-18 07:48:38 +0000941 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000942 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000943
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000944 SourceLocation LParenLoc, RParenLoc;
945 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +0000946
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000947 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000948 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000949
John McCall60d7b3a2010-08-24 06:29:42 +0000950 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000952 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000953 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +0000954
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000955 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +0000956 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000957 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +0000958 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000959 T.getOpenLocation(), Result.take(),
960 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000961
Sebastian Redl20df9b72008-12-11 22:51:44 +0000962 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000963}
964
Sebastian Redlc42e1182008-11-11 11:37:55 +0000965/// ParseCXXTypeid - This handles the C++ typeid expression.
966///
967/// postfix-expression: [C++ 5.2p1]
968/// 'typeid' '(' expression ')'
969/// 'typeid' '(' type-id ')'
970///
John McCall60d7b3a2010-08-24 06:29:42 +0000971ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000972 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
973
974 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000975 SourceLocation LParenLoc, RParenLoc;
976 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000977
978 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000979 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000980 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000981 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000982
John McCall60d7b3a2010-08-24 06:29:42 +0000983 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000984
985 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000986 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000987
988 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000989 T.consumeClose();
990 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000991 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000992 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000993
994 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000995 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000996 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000997 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000998 // When typeid is applied to an expression other than an lvalue of a
999 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +00001000 // operand (Clause 5).
1001 //
Mike Stump1eb44332009-09-09 15:08:12 +00001002 // Note that we can't tell whether the expression is an lvalue of a
Eli Friedmanef331b72012-01-20 01:26:23 +00001003 // polymorphic class type until after we've parsed the expression; we
1004 // speculatively assume the subexpression is unevaluated, and fix it up
1005 // later.
1006 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001007 Result = ParseExpression();
1008
1009 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001010 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +00001011 SkipUntil(tok::r_paren);
1012 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001013 T.consumeClose();
1014 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001015 if (RParenLoc.isInvalid())
1016 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001017
Sebastian Redlc42e1182008-11-11 11:37:55 +00001018 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001019 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001020 }
1021 }
1022
Sebastian Redl20df9b72008-12-11 22:51:44 +00001023 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001024}
1025
Francois Pichet01b7c302010-09-08 12:20:18 +00001026/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1027///
1028/// '__uuidof' '(' expression ')'
1029/// '__uuidof' '(' type-id ')'
1030///
1031ExprResult Parser::ParseCXXUuidof() {
1032 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1033
1034 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001035 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +00001036
1037 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001038 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +00001039 return ExprError();
1040
1041 ExprResult Result;
1042
1043 if (isTypeIdInParens()) {
1044 TypeResult Ty = ParseTypeName();
1045
1046 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001047 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001048
1049 if (Ty.isInvalid())
1050 return ExprError();
1051
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001052 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1053 Ty.get().getAsOpaquePtr(),
1054 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001055 } else {
1056 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1057 Result = ParseExpression();
1058
1059 // Match the ')'.
1060 if (Result.isInvalid())
1061 SkipUntil(tok::r_paren);
1062 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001063 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001064
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001065 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1066 /*isType=*/false,
1067 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001068 }
1069 }
1070
1071 return move(Result);
1072}
1073
Douglas Gregord4dca082010-02-24 18:44:31 +00001074/// \brief Parse a C++ pseudo-destructor expression after the base,
1075/// . or -> operator, and nested-name-specifier have already been
1076/// parsed.
1077///
1078/// postfix-expression: [C++ 5.2]
1079/// postfix-expression . pseudo-destructor-name
1080/// postfix-expression -> pseudo-destructor-name
1081///
1082/// pseudo-destructor-name:
1083/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1084/// ::[opt] nested-name-specifier template simple-template-id ::
1085/// ~type-name
1086/// ::[opt] nested-name-specifier[opt] ~type-name
1087///
John McCall60d7b3a2010-08-24 06:29:42 +00001088ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +00001089Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1090 tok::TokenKind OpKind,
1091 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +00001092 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +00001093 // We're parsing either a pseudo-destructor-name or a dependent
1094 // member access that has the same form as a
1095 // pseudo-destructor-name. We parse both in the same way and let
1096 // the action model sort them out.
1097 //
1098 // Note that the ::[opt] nested-name-specifier[opt] has already
1099 // been parsed, and if there was a simple-template-id, it has
1100 // been coalesced into a template-id annotation token.
1101 UnqualifiedId FirstTypeName;
1102 SourceLocation CCLoc;
1103 if (Tok.is(tok::identifier)) {
1104 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1105 ConsumeToken();
1106 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1107 CCLoc = ConsumeToken();
1108 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001109 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1110 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregord4dca082010-02-24 18:44:31 +00001111 FirstTypeName.setTemplateId(
1112 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1113 ConsumeToken();
1114 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1115 CCLoc = ConsumeToken();
1116 } else {
1117 FirstTypeName.setIdentifier(0, SourceLocation());
1118 }
1119
1120 // Parse the tilde.
1121 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1122 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001123
1124 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1125 DeclSpec DS(AttrFactory);
Benjamin Kramer85c60db2011-12-18 12:18:02 +00001126 ParseDecltypeSpecifier(DS);
David Blaikie91ec7892011-12-16 16:03:09 +00001127 if (DS.getTypeSpecType() == TST_error)
1128 return ExprError();
1129 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1130 OpKind, TildeLoc, DS,
1131 Tok.is(tok::l_paren));
1132 }
1133
Douglas Gregord4dca082010-02-24 18:44:31 +00001134 if (!Tok.is(tok::identifier)) {
1135 Diag(Tok, diag::err_destructor_tilde_identifier);
1136 return ExprError();
1137 }
1138
1139 // Parse the second type.
1140 UnqualifiedId SecondTypeName;
1141 IdentifierInfo *Name = Tok.getIdentifierInfo();
1142 SourceLocation NameLoc = ConsumeToken();
1143 SecondTypeName.setIdentifier(Name, NameLoc);
1144
1145 // If there is a '<', the second type name is a template-id. Parse
1146 // it as such.
1147 if (Tok.is(tok::less) &&
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001148 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1149 Name, NameLoc,
1150 false, ObjectType, SecondTypeName,
1151 /*AssumeTemplateName=*/true))
Douglas Gregord4dca082010-02-24 18:44:31 +00001152 return ExprError();
1153
John McCall9ae2f072010-08-23 23:25:46 +00001154 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1155 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001156 SS, FirstTypeName, CCLoc,
1157 TildeLoc, SecondTypeName,
1158 Tok.is(tok::l_paren));
1159}
1160
Reid Spencer5f016e22007-07-11 17:01:13 +00001161/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1162///
1163/// boolean-literal: [C++ 2.13.5]
1164/// 'true'
1165/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001166ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001168 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001169}
Chris Lattner50dd2892008-02-26 00:51:44 +00001170
1171/// ParseThrowExpression - This handles the C++ throw expression.
1172///
1173/// throw-expression: [C++ 15]
1174/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001175ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001176 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001177 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001178
Chris Lattner2a2819a2008-04-06 06:02:23 +00001179 // If the current token isn't the start of an assignment-expression,
1180 // then the expression is not present. This handles things like:
1181 // "C ? throw : (void)42", which is crazy but legal.
1182 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1183 case tok::semi:
1184 case tok::r_paren:
1185 case tok::r_square:
1186 case tok::r_brace:
1187 case tok::colon:
1188 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001189 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001190
Chris Lattner2a2819a2008-04-06 06:02:23 +00001191 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001192 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +00001193 if (Expr.isInvalid()) return move(Expr);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001194 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001195 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001196}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001197
1198/// ParseCXXThis - This handles the C++ 'this' pointer.
1199///
1200/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1201/// a non-lvalue expression whose value is the address of the object for which
1202/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001203ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001204 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1205 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001206 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001207}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001208
1209/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1210/// Can be interpreted either as function-style casting ("int(x)")
1211/// or class type construction ("ClassType(x,y,z)")
1212/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001213/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001214///
1215/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001216/// simple-type-specifier '(' expression-list[opt] ')'
1217/// [C++0x] simple-type-specifier braced-init-list
1218/// typename-specifier '(' expression-list[opt] ')'
1219/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001220///
John McCall60d7b3a2010-08-24 06:29:42 +00001221ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001222Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001223 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001224 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001225
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001226 assert((Tok.is(tok::l_paren) ||
1227 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1228 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001229
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001230 if (Tok.is(tok::l_brace)) {
Sebastian Redl6dc00f62012-02-12 18:41:05 +00001231 ExprResult Init = ParseBraceInitializer();
1232 if (Init.isInvalid())
1233 return Init;
1234 Expr *InitList = Init.take();
1235 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1236 MultiExprArg(&InitList, 1),
1237 SourceLocation());
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001238 } else {
1239 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1240
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001241 BalancedDelimiterTracker T(*this, tok::l_paren);
1242 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001243
1244 ExprVector Exprs(Actions);
1245 CommaLocsTy CommaLocs;
1246
1247 if (Tok.isNot(tok::r_paren)) {
1248 if (ParseExpressionList(Exprs, CommaLocs)) {
1249 SkipUntil(tok::r_paren);
1250 return ExprError();
1251 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001252 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001253
1254 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001255 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001256
1257 // TypeRep could be null, if it references an invalid typedef.
1258 if (!TypeRep)
1259 return ExprError();
1260
1261 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1262 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001263 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
1264 move_arg(Exprs),
1265 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001266 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001267}
1268
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001269/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001270///
1271/// condition:
1272/// expression
1273/// type-specifier-seq declarator '=' assignment-expression
Richard Smith0635aa72012-02-22 06:49:09 +00001274/// [C++11] type-specifier-seq declarator '=' initializer-clause
1275/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001276/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1277/// '=' assignment-expression
1278///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001279/// \param ExprResult if the condition was parsed as an expression, the
1280/// parsed expression.
1281///
1282/// \param DeclResult if the condition was parsed as a declaration, the
1283/// parsed declaration.
1284///
Douglas Gregor586596f2010-05-06 17:25:47 +00001285/// \param Loc The location of the start of the statement that requires this
1286/// condition, e.g., the "for" in a for loop.
1287///
1288/// \param ConvertToBoolean Whether the condition expression should be
1289/// converted to a boolean value.
1290///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001291/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001292bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1293 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001294 SourceLocation Loc,
1295 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001296 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001297 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001298 cutOffParsing();
1299 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001300 }
1301
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001302 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001303 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001304 ExprOut = ParseExpression(); // expression
1305 DeclOut = 0;
1306 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001307 return true;
1308
1309 // If required, convert to a boolean value.
1310 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001311 ExprOut
1312 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1313 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001314 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001315
1316 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001317 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001318 ParseSpecifierQualifierList(DS);
1319
1320 // declarator
1321 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1322 ParseDeclarator(DeclaratorInfo);
1323
1324 // simple-asm-expr[opt]
1325 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001326 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001327 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001328 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001329 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001330 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001331 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001332 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001333 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001334 }
1335
1336 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001337 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001338
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001339 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001340 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001341 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001342 DeclOut = Dcl.get();
1343 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001344
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001345 // '=' assignment-expression
Richard Trieud6c7c672012-01-18 22:54:52 +00001346 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith0635aa72012-02-22 06:49:09 +00001347 bool CopyInitialization = isTokenEqualOrEqualTypo();
1348 if (CopyInitialization)
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001349 ConsumeToken();
Richard Smith0635aa72012-02-22 06:49:09 +00001350
1351 ExprResult InitExpr = ExprError();
1352 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1353 Diag(Tok.getLocation(),
1354 diag::warn_cxx98_compat_generalized_initializer_lists);
1355 InitExpr = ParseBraceInitializer();
1356 } else if (CopyInitialization) {
1357 InitExpr = ParseAssignmentExpression();
1358 } else if (Tok.is(tok::l_paren)) {
1359 // This was probably an attempt to initialize the variable.
1360 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1361 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1362 RParen = ConsumeParen();
1363 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1364 diag::err_expected_init_in_condition_lparen)
1365 << SourceRange(LParen, RParen);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001366 } else {
Richard Smith0635aa72012-02-22 06:49:09 +00001367 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1368 diag::err_expected_init_in_condition);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001369 }
Richard Smith0635aa72012-02-22 06:49:09 +00001370
1371 if (!InitExpr.isInvalid())
1372 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
1373 DS.getTypeSpecType() == DeclSpec::TST_auto);
1374
Douglas Gregor586596f2010-05-06 17:25:47 +00001375 // FIXME: Build a reference to this declaration? Convert it to bool?
1376 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001377
1378 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001379
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001380 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001381}
1382
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001383/// \brief Determine whether the current token starts a C++
1384/// simple-type-specifier.
1385bool Parser::isCXXSimpleTypeSpecifier() const {
1386 switch (Tok.getKind()) {
1387 case tok::annot_typename:
1388 case tok::kw_short:
1389 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001390 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001391 case tok::kw_signed:
1392 case tok::kw_unsigned:
1393 case tok::kw_void:
1394 case tok::kw_char:
1395 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001396 case tok::kw_half:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001397 case tok::kw_float:
1398 case tok::kw_double:
1399 case tok::kw_wchar_t:
1400 case tok::kw_char16_t:
1401 case tok::kw_char32_t:
1402 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +00001403 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001404 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +00001405 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001406 return true;
1407
1408 default:
1409 break;
1410 }
1411
1412 return false;
1413}
1414
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001415/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1416/// This should only be called when the current token is known to be part of
1417/// simple-type-specifier.
1418///
1419/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001420/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001421/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1422/// char
1423/// wchar_t
1424/// bool
1425/// short
1426/// int
1427/// long
1428/// signed
1429/// unsigned
1430/// float
1431/// double
1432/// void
1433/// [GNU] typeof-specifier
1434/// [C++0x] auto [TODO]
1435///
1436/// type-name:
1437/// class-name
1438/// enum-name
1439/// typedef-name
1440///
1441void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1442 DS.SetRangeStart(Tok.getLocation());
1443 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001444 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001445 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001447 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001448 case tok::identifier: // foo::bar
1449 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001450 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001451 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001452 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001453
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001454 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001455 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001456 if (getTypeAnnotation(Tok))
1457 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1458 getTypeAnnotation(Tok));
1459 else
1460 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001461
1462 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1463 ConsumeToken();
1464
1465 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1466 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1467 // Objective-C interface. If we don't have Objective-C or a '<', this is
1468 // just a normal reference to a typedef name.
1469 if (Tok.is(tok::less) && getLang().ObjC1)
1470 ParseObjCProtocolQualifiers(DS);
1471
1472 DS.Finish(Diags, PP);
1473 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001474 }
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001476 // builtin types
1477 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001478 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001479 break;
1480 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001481 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001482 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001483 case tok::kw___int64:
1484 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1485 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001486 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001487 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001488 break;
1489 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001490 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001491 break;
1492 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001493 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001494 break;
1495 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001496 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001497 break;
1498 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001499 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001500 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001501 case tok::kw_half:
1502 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1503 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001504 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001505 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001506 break;
1507 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001508 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001509 break;
1510 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001511 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001512 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001513 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001514 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001515 break;
1516 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001517 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001518 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001519 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001520 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001521 break;
David Blaikie5e089fe2012-01-24 05:47:35 +00001522 case tok::annot_decltype:
1523 case tok::kw_decltype:
1524 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1525 return DS.Finish(Diags, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001527 // GNU typeof support.
1528 case tok::kw_typeof:
1529 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001530 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001531 return;
1532 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001533 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001534 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1535 else
1536 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001537 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001538 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001539}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001540
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001541/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1542/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1543/// e.g., "const short int". Note that the DeclSpec is *not* finished
1544/// by parsing the type-specifier-seq, because these sequences are
1545/// typically followed by some form of declarator. Returns true and
1546/// emits diagnostics if this is not a type-specifier-seq, false
1547/// otherwise.
1548///
1549/// type-specifier-seq: [C++ 8.1]
1550/// type-specifier type-specifier-seq[opt]
1551///
1552bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1553 DS.SetRangeStart(Tok.getLocation());
1554 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001555 unsigned DiagID;
1556 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001557
1558 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001559 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1560 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001561 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001562 return true;
1563 }
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Sebastian Redld9bafa72010-02-03 21:21:43 +00001565 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1566 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1567 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001568
Douglas Gregor396a9f22010-02-24 23:13:13 +00001569 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001570 return false;
1571}
1572
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001573/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1574/// some form.
1575///
1576/// This routine is invoked when a '<' is encountered after an identifier or
1577/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1578/// whether the unqualified-id is actually a template-id. This routine will
1579/// then parse the template arguments and form the appropriate template-id to
1580/// return to the caller.
1581///
1582/// \param SS the nested-name-specifier that precedes this template-id, if
1583/// we're actually parsing a qualified-id.
1584///
1585/// \param Name for constructor and destructor names, this is the actual
1586/// identifier that may be a template-name.
1587///
1588/// \param NameLoc the location of the class-name in a constructor or
1589/// destructor.
1590///
1591/// \param EnteringContext whether we're entering the scope of the
1592/// nested-name-specifier.
1593///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001594/// \param ObjectType if this unqualified-id occurs within a member access
1595/// expression, the type of the base object whose member is being accessed.
1596///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001597/// \param Id as input, describes the template-name or operator-function-id
1598/// that precedes the '<'. If template arguments were parsed successfully,
1599/// will be updated with the template-id.
1600///
Douglas Gregord4dca082010-02-24 18:44:31 +00001601/// \param AssumeTemplateId When true, this routine will assume that the name
1602/// refers to a template without performing name lookup to verify.
1603///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001604/// \returns true if a parse error occurred, false otherwise.
1605bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001606 SourceLocation TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001607 IdentifierInfo *Name,
1608 SourceLocation NameLoc,
1609 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001610 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001611 UnqualifiedId &Id,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001612 bool AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001613 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1614 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001615
1616 TemplateTy Template;
1617 TemplateNameKind TNK = TNK_Non_template;
1618 switch (Id.getKind()) {
1619 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001620 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001621 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001622 if (AssumeTemplateId) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001623 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001624 Id, ObjectType, EnteringContext,
1625 Template);
1626 if (TNK == TNK_Non_template)
1627 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001628 } else {
1629 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001630 TNK = Actions.isTemplateName(getCurScope(), SS,
1631 TemplateKWLoc.isValid(), Id,
1632 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001633 MemberOfUnknownSpecialization);
1634
1635 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1636 ObjectType && IsTemplateArgumentList()) {
1637 // We have something like t->getAs<T>(), where getAs is a
1638 // member of an unknown specialization. However, this will only
1639 // parse correctly as a template, so suggest the keyword 'template'
1640 // before 'getAs' and treat this as a dependent template name.
1641 std::string Name;
1642 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1643 Name = Id.Identifier->getName();
1644 else {
1645 Name = "operator ";
1646 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1647 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1648 else
1649 Name += Id.Identifier->getName();
1650 }
1651 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1652 << Name
1653 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001654 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1655 SS, TemplateKWLoc, Id,
1656 ObjectType, EnteringContext,
1657 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001658 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001659 return true;
1660 }
1661 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001662 break;
1663
Douglas Gregor014e88d2009-11-03 23:16:33 +00001664 case UnqualifiedId::IK_ConstructorName: {
1665 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001666 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001667 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001668 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1669 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001670 EnteringContext, Template,
1671 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001672 break;
1673 }
1674
Douglas Gregor014e88d2009-11-03 23:16:33 +00001675 case UnqualifiedId::IK_DestructorName: {
1676 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001677 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001678 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001679 if (ObjectType) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001680 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1681 SS, TemplateKWLoc, TemplateName,
1682 ObjectType, EnteringContext,
1683 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001684 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001685 return true;
1686 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001687 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1688 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001689 EnteringContext, Template,
1690 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001691
John McCallb3d87482010-08-24 05:47:05 +00001692 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001693 Diag(NameLoc, diag::err_destructor_template_id)
1694 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001695 return true;
1696 }
1697 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001698 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001699 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001700
1701 default:
1702 return false;
1703 }
1704
1705 if (TNK == TNK_Non_template)
1706 return false;
1707
1708 // Parse the enclosed template argument list.
1709 SourceLocation LAngleLoc, RAngleLoc;
1710 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001711 if (Tok.is(tok::less) &&
1712 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001713 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001714 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001715 RAngleLoc))
1716 return true;
1717
1718 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001719 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1720 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001721 // Form a parsed representation of the template-id to be stored in the
1722 // UnqualifiedId.
1723 TemplateIdAnnotation *TemplateId
1724 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1725
1726 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1727 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001728 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001729 TemplateId->TemplateNameLoc = Id.StartLocation;
1730 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001731 TemplateId->Name = 0;
1732 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1733 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001734 }
1735
Douglas Gregor059101f2011-03-02 00:47:37 +00001736 TemplateId->SS = SS;
Benjamin Kramer2b28bf12012-02-19 23:37:39 +00001737 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall2b5289b2010-08-23 07:28:44 +00001738 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001739 TemplateId->Kind = TNK;
1740 TemplateId->LAngleLoc = LAngleLoc;
1741 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001742 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001743 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001744 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001745 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001746
1747 Id.setTemplateId(TemplateId);
1748 return false;
1749 }
1750
1751 // Bundle the template arguments together.
1752 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001753 TemplateArgs.size());
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001754
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001755 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001756 TypeResult Type
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001757 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1758 Template, NameLoc,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001759 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1760 /*IsCtorOrDtorName=*/true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001761 if (Type.isInvalid())
1762 return true;
1763
1764 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1765 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1766 else
1767 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1768
1769 return false;
1770}
1771
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001772/// \brief Parse an operator-function-id or conversion-function-id as part
1773/// of a C++ unqualified-id.
1774///
1775/// This routine is responsible only for parsing the operator-function-id or
1776/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001777///
1778/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001779/// operator-function-id: [C++ 13.5]
1780/// 'operator' operator
1781///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001782/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001783/// new delete new[] delete[]
1784/// + - * / % ^ & | ~
1785/// ! = < > += -= *= /= %=
1786/// ^= &= |= << >> >>= <<= == !=
1787/// <= >= && || ++ -- , ->* ->
1788/// () []
1789///
1790/// conversion-function-id: [C++ 12.3.2]
1791/// operator conversion-type-id
1792///
1793/// conversion-type-id:
1794/// type-specifier-seq conversion-declarator[opt]
1795///
1796/// conversion-declarator:
1797/// ptr-operator conversion-declarator[opt]
1798/// \endcode
1799///
1800/// \param The nested-name-specifier that preceded this unqualified-id. If
1801/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1802///
1803/// \param EnteringContext whether we are entering the scope of the
1804/// nested-name-specifier.
1805///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001806/// \param ObjectType if this unqualified-id occurs within a member access
1807/// expression, the type of the base object whose member is being accessed.
1808///
1809/// \param Result on a successful parse, contains the parsed unqualified-id.
1810///
1811/// \returns true if parsing fails, false otherwise.
1812bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001813 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001814 UnqualifiedId &Result) {
1815 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1816
1817 // Consume the 'operator' keyword.
1818 SourceLocation KeywordLoc = ConsumeToken();
1819
1820 // Determine what kind of operator name we have.
1821 unsigned SymbolIdx = 0;
1822 SourceLocation SymbolLocations[3];
1823 OverloadedOperatorKind Op = OO_None;
1824 switch (Tok.getKind()) {
1825 case tok::kw_new:
1826 case tok::kw_delete: {
1827 bool isNew = Tok.getKind() == tok::kw_new;
1828 // Consume the 'new' or 'delete'.
1829 SymbolLocations[SymbolIdx++] = ConsumeToken();
1830 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001831 // Consume the '[' and ']'.
1832 BalancedDelimiterTracker T(*this, tok::l_square);
1833 T.consumeOpen();
1834 T.consumeClose();
1835 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001836 return true;
1837
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001838 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1839 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001840 Op = isNew? OO_Array_New : OO_Array_Delete;
1841 } else {
1842 Op = isNew? OO_New : OO_Delete;
1843 }
1844 break;
1845 }
1846
1847#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1848 case tok::Token: \
1849 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1850 Op = OO_##Name; \
1851 break;
1852#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1853#include "clang/Basic/OperatorKinds.def"
1854
1855 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001856 // Consume the '(' and ')'.
1857 BalancedDelimiterTracker T(*this, tok::l_paren);
1858 T.consumeOpen();
1859 T.consumeClose();
1860 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001861 return true;
1862
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001863 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1864 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001865 Op = OO_Call;
1866 break;
1867 }
1868
1869 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001870 // Consume the '[' and ']'.
1871 BalancedDelimiterTracker T(*this, tok::l_square);
1872 T.consumeOpen();
1873 T.consumeClose();
1874 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001875 return true;
1876
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001877 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1878 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001879 Op = OO_Subscript;
1880 break;
1881 }
1882
1883 case tok::code_completion: {
1884 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001885 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001886 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001887 // Don't try to parse any further.
1888 return true;
1889 }
1890
1891 default:
1892 break;
1893 }
1894
1895 if (Op != OO_None) {
1896 // We have parsed an operator-function-id.
1897 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1898 return false;
1899 }
Sean Hunt0486d742009-11-28 04:44:28 +00001900
1901 // Parse a literal-operator-id.
1902 //
1903 // literal-operator-id: [C++0x 13.5.8]
1904 // operator "" identifier
1905
1906 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
Richard Smith7fe62082011-10-15 05:09:34 +00001907 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001908 if (Tok.getLength() != 2)
1909 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1910 ConsumeStringToken();
1911
1912 if (Tok.isNot(tok::identifier)) {
1913 Diag(Tok.getLocation(), diag::err_expected_ident);
1914 return true;
1915 }
1916
1917 IdentifierInfo *II = Tok.getIdentifierInfo();
1918 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001919 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001920 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001921
1922 // Parse a conversion-function-id.
1923 //
1924 // conversion-function-id: [C++ 12.3.2]
1925 // operator conversion-type-id
1926 //
1927 // conversion-type-id:
1928 // type-specifier-seq conversion-declarator[opt]
1929 //
1930 // conversion-declarator:
1931 // ptr-operator conversion-declarator[opt]
1932
1933 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001934 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001935 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001936 return true;
1937
1938 // Parse the conversion-declarator, which is merely a sequence of
1939 // ptr-operators.
1940 Declarator D(DS, Declarator::TypeNameContext);
1941 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1942
1943 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001944 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001945 if (Ty.isInvalid())
1946 return true;
1947
1948 // Note that this is a conversion-function-id.
1949 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1950 D.getSourceRange().getEnd());
1951 return false;
1952}
1953
1954/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1955/// name of an entity.
1956///
1957/// \code
1958/// unqualified-id: [C++ expr.prim.general]
1959/// identifier
1960/// operator-function-id
1961/// conversion-function-id
1962/// [C++0x] literal-operator-id [TODO]
1963/// ~ class-name
1964/// template-id
1965///
1966/// \endcode
1967///
1968/// \param The nested-name-specifier that preceded this unqualified-id. If
1969/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1970///
1971/// \param EnteringContext whether we are entering the scope of the
1972/// nested-name-specifier.
1973///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001974/// \param AllowDestructorName whether we allow parsing of a destructor name.
1975///
1976/// \param AllowConstructorName whether we allow parsing a constructor name.
1977///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001978/// \param ObjectType if this unqualified-id occurs within a member access
1979/// expression, the type of the base object whose member is being accessed.
1980///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001981/// \param Result on a successful parse, contains the parsed unqualified-id.
1982///
1983/// \returns true if parsing fails, false otherwise.
1984bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1985 bool AllowDestructorName,
1986 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001987 ParsedType ObjectType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001988 SourceLocation& TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001989 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001990
1991 // Handle 'A::template B'. This is for template-ids which have not
1992 // already been annotated by ParseOptionalCXXScopeSpecifier().
1993 bool TemplateSpecified = false;
Douglas Gregor0278e122010-05-05 05:58:24 +00001994 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1995 (ObjectType || SS.isSet())) {
1996 TemplateSpecified = true;
1997 TemplateKWLoc = ConsumeToken();
1998 }
1999
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002000 // unqualified-id:
2001 // identifier
2002 // template-id (when it hasn't already been annotated)
2003 if (Tok.is(tok::identifier)) {
2004 // Consume the identifier.
2005 IdentifierInfo *Id = Tok.getIdentifierInfo();
2006 SourceLocation IdLoc = ConsumeToken();
2007
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002008 if (!getLang().CPlusPlus) {
2009 // If we're not in C++, only identifiers matter. Record the
2010 // identifier and return.
2011 Result.setIdentifier(Id, IdLoc);
2012 return false;
2013 }
2014
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002015 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002016 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002017 // We have parsed a constructor name.
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002018 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2019 &SS, false, false,
2020 ParsedType(),
2021 /*IsCtorOrDtorName=*/true,
2022 /*NonTrivialTypeSourceInfo=*/true);
2023 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002024 } else {
2025 // We have parsed an identifier.
2026 Result.setIdentifier(Id, IdLoc);
2027 }
2028
2029 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00002030 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002031 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2032 EnteringContext, ObjectType,
2033 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002034
2035 return false;
2036 }
2037
2038 // unqualified-id:
2039 // template-id (already parsed and annotated)
2040 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002041 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002042
2043 // If the template-name names the current class, then this is a constructor
2044 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002045 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002046 if (SS.isSet()) {
2047 // C++ [class.qual]p2 specifies that a qualified template-name
2048 // is taken as the constructor name where a constructor can be
2049 // declared. Thus, the template arguments are extraneous, so
2050 // complain about them and remove them entirely.
2051 Diag(TemplateId->TemplateNameLoc,
2052 diag::err_out_of_line_constructor_template_id)
2053 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00002054 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002055 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002056 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2057 TemplateId->TemplateNameLoc,
2058 getCurScope(),
2059 &SS, false, false,
2060 ParsedType(),
2061 /*IsCtorOrDtorName=*/true,
2062 /*NontrivialTypeSourceInfo=*/true);
2063 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002064 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002065 ConsumeToken();
2066 return false;
2067 }
2068
2069 Result.setConstructorTemplateId(TemplateId);
2070 ConsumeToken();
2071 return false;
2072 }
2073
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002074 // We have already parsed a template-id; consume the annotation token as
2075 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002076 Result.setTemplateId(TemplateId);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002077 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002078 ConsumeToken();
2079 return false;
2080 }
2081
2082 // unqualified-id:
2083 // operator-function-id
2084 // conversion-function-id
2085 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002086 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002087 return true;
2088
Sean Hunte6252d12009-11-28 08:58:14 +00002089 // If we have an operator-function-id or a literal-operator-id and the next
2090 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002091 //
2092 // template-id:
2093 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00002094 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2095 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002096 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002097 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2098 0, SourceLocation(),
2099 EnteringContext, ObjectType,
2100 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002101
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002102 return false;
2103 }
2104
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002105 if (getLang().CPlusPlus &&
2106 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002107 // C++ [expr.unary.op]p10:
2108 // There is an ambiguity in the unary-expression ~X(), where X is a
2109 // class-name. The ambiguity is resolved in favor of treating ~ as a
2110 // unary complement rather than treating ~X as referring to a destructor.
2111
2112 // Parse the '~'.
2113 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00002114
2115 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2116 DeclSpec DS(AttrFactory);
2117 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2118 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2119 Result.setDestructorName(TildeLoc, Type, EndLoc);
2120 return false;
2121 }
2122 return true;
2123 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002124
2125 // Parse the class-name.
2126 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00002127 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002128 return true;
2129 }
2130
2131 // Parse the class-name (or template-name in a simple-template-id).
2132 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2133 SourceLocation ClassNameLoc = ConsumeToken();
2134
Douglas Gregor0278e122010-05-05 05:58:24 +00002135 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002136 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002137 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2138 ClassName, ClassNameLoc,
2139 EnteringContext, ObjectType,
2140 Result, TemplateSpecified);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002141 }
2142
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002143 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002144 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2145 ClassNameLoc, getCurScope(),
2146 SS, ObjectType,
2147 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002148 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002149 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002150
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002151 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002152 return false;
2153 }
2154
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002155 Diag(Tok, diag::err_expected_unqualified_id)
2156 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002157 return true;
2158}
2159
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002160/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2161/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002162///
Chris Lattner59232d32009-01-04 21:25:24 +00002163/// This method is called to parse the new expression after the optional :: has
2164/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2165/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002166///
2167/// new-expression:
2168/// '::'[opt] 'new' new-placement[opt] new-type-id
2169/// new-initializer[opt]
2170/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2171/// new-initializer[opt]
2172///
2173/// new-placement:
2174/// '(' expression-list ')'
2175///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002176/// new-type-id:
2177/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002178/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002179///
2180/// new-declarator:
2181/// ptr-operator new-declarator[opt]
2182/// direct-new-declarator
2183///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002184/// new-initializer:
2185/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002186/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002187///
John McCall60d7b3a2010-08-24 06:29:42 +00002188ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002189Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2190 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2191 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002192
2193 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2194 // second form of new-expression. It can't be a new-type-id.
2195
Sebastian Redla55e52c2008-11-25 22:21:31 +00002196 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002197 SourceLocation PlacementLParen, PlacementRParen;
2198
Douglas Gregor4bd40312010-07-13 15:54:32 +00002199 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002200 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002201 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002202 if (Tok.is(tok::l_paren)) {
2203 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002204 BalancedDelimiterTracker T(*this, tok::l_paren);
2205 T.consumeOpen();
2206 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002207 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2208 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002209 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002210 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002211
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002212 T.consumeClose();
2213 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002214 if (PlacementRParen.isInvalid()) {
2215 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002216 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002217 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002218
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002219 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002220 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002221 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002222 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002223 } else {
2224 // We still need the type.
2225 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002226 BalancedDelimiterTracker T(*this, tok::l_paren);
2227 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002228 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002229 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002230 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002231 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002232 T.consumeClose();
2233 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002234 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002235 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002236 if (ParseCXXTypeSpecifierSeq(DS))
2237 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002238 else {
2239 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002240 ParseDeclaratorInternal(DeclaratorInfo,
2241 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002242 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002243 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002244 }
2245 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002246 // A new-type-id is a simplified type-id, where essentially the
2247 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002248 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002249 if (ParseCXXTypeSpecifierSeq(DS))
2250 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002251 else {
2252 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002253 ParseDeclaratorInternal(DeclaratorInfo,
2254 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002255 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002256 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002257 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002258 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002259 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002260 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002261
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002262 ExprResult Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002263
2264 if (Tok.is(tok::l_paren)) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002265 SourceLocation ConstructorLParen, ConstructorRParen;
2266 ExprVector ConstructorArgs(Actions);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002267 BalancedDelimiterTracker T(*this, tok::l_paren);
2268 T.consumeOpen();
2269 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002270 if (Tok.isNot(tok::r_paren)) {
2271 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002272 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2273 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002274 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002275 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002276 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002277 T.consumeClose();
2278 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002279 if (ConstructorRParen.isInvalid()) {
2280 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002281 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002282 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002283 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2284 ConstructorRParen,
2285 move_arg(ConstructorArgs));
Richard Smith29e3a312011-10-15 03:38:41 +00002286 } else if (Tok.is(tok::l_brace) && getLang().CPlusPlus0x) {
Richard Smith7fe62082011-10-15 05:09:34 +00002287 Diag(Tok.getLocation(),
2288 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002289 Initializer = ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002290 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002291 if (Initializer.isInvalid())
2292 return Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002293
Sebastian Redlf53597f2009-03-15 17:47:39 +00002294 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2295 move_arg(PlacementArgs), PlacementRParen,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002296 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002297}
2298
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002299/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2300/// passed to ParseDeclaratorInternal.
2301///
2302/// direct-new-declarator:
2303/// '[' expression ']'
2304/// direct-new-declarator '[' constant-expression ']'
2305///
Chris Lattner59232d32009-01-04 21:25:24 +00002306void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002307 // Parse the array dimensions.
2308 bool first = true;
2309 while (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002310 BalancedDelimiterTracker T(*this, tok::l_square);
2311 T.consumeOpen();
2312
John McCall60d7b3a2010-08-24 06:29:42 +00002313 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002314 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002315 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002316 // Recover
2317 SkipUntil(tok::r_square);
2318 return;
2319 }
2320 first = false;
2321
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002322 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002323
2324 ParsedAttributes attrs(AttrFactory);
2325 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002326 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002327 Size.release(),
2328 T.getOpenLocation(),
2329 T.getCloseLocation()),
2330 attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002331
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002332 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002333 return;
2334 }
2335}
2336
2337/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2338/// This ambiguity appears in the syntax of the C++ new operator.
2339///
2340/// new-expression:
2341/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2342/// new-initializer[opt]
2343///
2344/// new-placement:
2345/// '(' expression-list ')'
2346///
John McCallca0408f2010-08-23 06:44:23 +00002347bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002348 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002349 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002350 // The '(' was already consumed.
2351 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002352 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002353 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002354 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002355 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002356 }
2357
2358 // It's not a type, it has to be an expression list.
2359 // Discard the comma locations - ActOnCXXNew has enough parameters.
2360 CommaLocsTy CommaLocs;
2361 return ParseExpressionList(PlacementArgs, CommaLocs);
2362}
2363
2364/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2365/// to free memory allocated by new.
2366///
Chris Lattner59232d32009-01-04 21:25:24 +00002367/// This method is called to parse the 'delete' expression after the optional
2368/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2369/// and "Start" is its location. Otherwise, "Start" is the location of the
2370/// 'delete' token.
2371///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002372/// delete-expression:
2373/// '::'[opt] 'delete' cast-expression
2374/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002375ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002376Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2377 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2378 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002379
2380 // Array delete?
2381 bool ArrayDelete = false;
2382 if (Tok.is(tok::l_square)) {
2383 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002384 BalancedDelimiterTracker T(*this, tok::l_square);
2385
2386 T.consumeOpen();
2387 T.consumeClose();
2388 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002389 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002390 }
2391
John McCall60d7b3a2010-08-24 06:29:42 +00002392 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002393 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002394 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002395
John McCall9ae2f072010-08-23 23:25:46 +00002396 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002397}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002398
Mike Stump1eb44332009-09-09 15:08:12 +00002399static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002400 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002401 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002402 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002403 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002404 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002405 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002406 case tok::kw___has_trivial_constructor:
2407 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002408 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002409 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2410 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2411 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002412 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2413 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002414 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002415 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2416 case tok::kw___is_compound: return UTT_IsCompound;
2417 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002418 case tok::kw___is_empty: return UTT_IsEmpty;
2419 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002420 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002421 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2422 case tok::kw___is_function: return UTT_IsFunction;
2423 case tok::kw___is_fundamental: return UTT_IsFundamental;
2424 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00002425 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2426 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2427 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2428 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2429 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002430 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002431 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002432 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002433 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002434 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002435 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002436 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2437 case tok::kw___is_scalar: return UTT_IsScalar;
2438 case tok::kw___is_signed: return UTT_IsSigned;
2439 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2440 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002441 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002442 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002443 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2444 case tok::kw___is_void: return UTT_IsVoid;
2445 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002446 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002447}
2448
2449static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2450 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002451 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002452 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002453 case tok::kw___is_convertible: return BTT_IsConvertible;
2454 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002455 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002456 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002457 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002458 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002459}
2460
John Wiegley21ff2e52011-04-28 00:16:57 +00002461static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2462 switch(kind) {
2463 default: llvm_unreachable("Not a known binary type trait");
2464 case tok::kw___array_rank: return ATT_ArrayRank;
2465 case tok::kw___array_extent: return ATT_ArrayExtent;
2466 }
2467}
2468
John Wiegley55262202011-04-25 06:54:41 +00002469static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2470 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002471 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002472 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2473 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2474 }
2475}
2476
Sebastian Redl64b45f72009-01-05 20:52:13 +00002477/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2478/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2479/// templates.
2480///
2481/// primary-expression:
2482/// [GNU] unary-type-trait '(' type-id ')'
2483///
John McCall60d7b3a2010-08-24 06:29:42 +00002484ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002485 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2486 SourceLocation Loc = ConsumeToken();
2487
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002488 BalancedDelimiterTracker T(*this, tok::l_paren);
2489 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002490 return ExprError();
2491
2492 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2493 // there will be cryptic errors about mismatched parentheses and missing
2494 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002495 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002496
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002497 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002498
Douglas Gregor809070a2009-02-18 17:45:20 +00002499 if (Ty.isInvalid())
2500 return ExprError();
2501
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002502 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002503}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002504
Francois Pichet6ad6f282010-12-07 00:08:36 +00002505/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2506/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2507/// templates.
2508///
2509/// primary-expression:
2510/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2511///
2512ExprResult Parser::ParseBinaryTypeTrait() {
2513 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2514 SourceLocation Loc = ConsumeToken();
2515
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002516 BalancedDelimiterTracker T(*this, tok::l_paren);
2517 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002518 return ExprError();
2519
2520 TypeResult LhsTy = ParseTypeName();
2521 if (LhsTy.isInvalid()) {
2522 SkipUntil(tok::r_paren);
2523 return ExprError();
2524 }
2525
2526 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2527 SkipUntil(tok::r_paren);
2528 return ExprError();
2529 }
2530
2531 TypeResult RhsTy = ParseTypeName();
2532 if (RhsTy.isInvalid()) {
2533 SkipUntil(tok::r_paren);
2534 return ExprError();
2535 }
2536
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002537 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002538
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002539 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2540 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002541}
2542
John Wiegley21ff2e52011-04-28 00:16:57 +00002543/// ParseArrayTypeTrait - Parse the built-in array type-trait
2544/// pseudo-functions.
2545///
2546/// primary-expression:
2547/// [Embarcadero] '__array_rank' '(' type-id ')'
2548/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2549///
2550ExprResult Parser::ParseArrayTypeTrait() {
2551 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2552 SourceLocation Loc = ConsumeToken();
2553
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002554 BalancedDelimiterTracker T(*this, tok::l_paren);
2555 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002556 return ExprError();
2557
2558 TypeResult Ty = ParseTypeName();
2559 if (Ty.isInvalid()) {
2560 SkipUntil(tok::comma);
2561 SkipUntil(tok::r_paren);
2562 return ExprError();
2563 }
2564
2565 switch (ATT) {
2566 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002567 T.consumeClose();
2568 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2569 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002570 }
2571 case ATT_ArrayExtent: {
2572 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2573 SkipUntil(tok::r_paren);
2574 return ExprError();
2575 }
2576
2577 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002578 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002579
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002580 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2581 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002582 }
John Wiegley21ff2e52011-04-28 00:16:57 +00002583 }
David Blaikie30263482012-01-20 21:50:17 +00002584 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley21ff2e52011-04-28 00:16:57 +00002585}
2586
John Wiegley55262202011-04-25 06:54:41 +00002587/// ParseExpressionTrait - Parse built-in expression-trait
2588/// pseudo-functions like __is_lvalue_expr( xxx ).
2589///
2590/// primary-expression:
2591/// [Embarcadero] expression-trait '(' expression ')'
2592///
2593ExprResult Parser::ParseExpressionTrait() {
2594 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2595 SourceLocation Loc = ConsumeToken();
2596
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002597 BalancedDelimiterTracker T(*this, tok::l_paren);
2598 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002599 return ExprError();
2600
2601 ExprResult Expr = ParseExpression();
2602
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002603 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002604
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002605 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2606 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002607}
2608
2609
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002610/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2611/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2612/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002613ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002614Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002615 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002616 BalancedDelimiterTracker &Tracker) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002617 assert(getLang().CPlusPlus && "Should only be called for C++!");
2618 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2619 assert(isTypeIdInParens() && "Not a type-id!");
2620
John McCall60d7b3a2010-08-24 06:29:42 +00002621 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002622 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002623
2624 // We need to disambiguate a very ugly part of the C++ syntax:
2625 //
2626 // (T())x; - type-id
2627 // (T())*x; - type-id
2628 // (T())/x; - expression
2629 // (T()); - expression
2630 //
2631 // The bad news is that we cannot use the specialized tentative parser, since
2632 // it can only verify that the thing inside the parens can be parsed as
2633 // type-id, it is not useful for determining the context past the parens.
2634 //
2635 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002636 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002637 //
2638 // It uses a scheme similar to parsing inline methods. The parenthesized
2639 // tokens are cached, the context that follows is determined (possibly by
2640 // parsing a cast-expression), and then we re-introduce the cached tokens
2641 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002642
Mike Stump1eb44332009-09-09 15:08:12 +00002643 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002644 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002645
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002646 // Store the tokens of the parentheses. We will parse them after we determine
2647 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002648 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002649 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002650 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002651 return ExprError();
2652 }
2653
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002654 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002655 ParseAs = CompoundLiteral;
2656 } else {
2657 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002658 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2659 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2660 NotCastExpr = true;
2661 } else {
2662 // Try parsing the cast-expression that may follow.
2663 // If it is not a cast-expression, NotCastExpr will be true and no token
2664 // will be consumed.
2665 Result = ParseCastExpression(false/*isUnaryExpression*/,
2666 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002667 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002668 // type-id has priority.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002669 IsTypeCast);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002670 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002671
2672 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2673 // an expression.
2674 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002675 }
2676
Mike Stump1eb44332009-09-09 15:08:12 +00002677 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002678 Toks.push_back(Tok);
2679 // Re-enter the stored parenthesized tokens into the token stream, so we may
2680 // parse them now.
2681 PP.EnterTokenStream(Toks.data(), Toks.size(),
2682 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2683 // Drop the current token and bring the first cached one. It's the same token
2684 // as when we entered this function.
2685 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002686
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002687 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002688 // Parse the type declarator.
2689 DeclSpec DS(AttrFactory);
2690 ParseSpecifierQualifierList(DS);
2691 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2692 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002693
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002694 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002695 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002696
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002697 if (ParseAs == CompoundLiteral) {
2698 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002699 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002700 return ParseCompoundLiteralExpression(Ty.get(),
2701 Tracker.getOpenLocation(),
2702 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002703 }
Mike Stump1eb44332009-09-09 15:08:12 +00002704
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002705 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2706 assert(ParseAs == CastExpr);
2707
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002708 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002709 return ExprError();
2710
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002711 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002712 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002713 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2714 DeclaratorInfo, CastTy,
2715 Tracker.getCloseLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002716 return move(Result);
2717 }
Mike Stump1eb44332009-09-09 15:08:12 +00002718
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002719 // Not a compound literal, and not followed by a cast-expression.
2720 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002721
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002722 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002723 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002724 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002725 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2726 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002727
2728 // Match the ')'.
2729 if (Result.isInvalid()) {
2730 SkipUntil(tok::r_paren);
2731 return ExprError();
2732 }
Mike Stump1eb44332009-09-09 15:08:12 +00002733
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002734 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002735 return move(Result);
2736}