blob: 41bf1b623f68a7baaf57c1bbd4b3ba233ca28f6f [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner29375652006-12-04 18:06:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner29375652006-12-04 18:06:35 +000015#include "clang/Parse/Parser.h"
Douglas Gregor94a32472011-01-11 00:33:19 +000016#include "RAIIObjectsForParser.h"
Eli Friedmanc7c97142012-01-04 02:40:39 +000017#include "clang/Basic/PrettyStackTrace.h"
Richard Smith7d182a72012-03-08 23:06:02 +000018#include "clang/Lex/LiteralSupport.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
Douglas Gregordb0b9f12011-08-04 15:30:47 +000020#include "clang/Sema/Scope.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor7861a802009-11-03 01:35:08 +000022#include "llvm/Support/ErrorHandling.h"
23
Chris Lattner29375652006-12-04 18:06:35 +000024using namespace clang;
25
Richard Smith55858492011-04-14 21:45:45 +000026static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
27 switch (Kind) {
28 case tok::kw_template: return 0;
29 case tok::kw_const_cast: return 1;
30 case tok::kw_dynamic_cast: return 2;
31 case tok::kw_reinterpret_cast: return 3;
32 case tok::kw_static_cast: return 4;
33 default:
David Blaikie83d382b2011-09-23 05:06:16 +000034 llvm_unreachable("Unknown type for digraph error message.");
Richard Smith55858492011-04-14 21:45:45 +000035 }
36}
37
38// Are the two tokens adjacent in the same source file?
Richard Smith7b3f3222012-06-18 06:11:04 +000039bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smith55858492011-04-14 21:45:45 +000040 SourceManager &SM = PP.getSourceManager();
41 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000042 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smith55858492011-04-14 21:45:45 +000043 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
44}
45
46// Suggest fixit for "<::" after a cast.
47static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
48 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
49 // Pull '<:' and ':' off token stream.
50 if (!AtDigraph)
51 PP.Lex(DigraphToken);
52 PP.Lex(ColonToken);
53
54 SourceRange Range;
55 Range.setBegin(DigraphToken.getLocation());
56 Range.setEnd(ColonToken.getLocation());
57 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
58 << SelectDigraphErrorMessage(Kind)
59 << FixItHint::CreateReplacement(Range, "< ::");
60
61 // Update token information to reflect their change in token type.
62 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000063 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smith55858492011-04-14 21:45:45 +000064 ColonToken.setLength(2);
65 DigraphToken.setKind(tok::less);
66 DigraphToken.setLength(1);
67
68 // Push new tokens back to token stream.
69 PP.EnterToken(ColonToken);
70 if (!AtDigraph)
71 PP.EnterToken(DigraphToken);
72}
73
Richard Trieu01fc0012011-09-19 19:01:00 +000074// Check for '<::' which should be '< ::' instead of '[:' when following
75// a template name.
76void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
77 bool EnteringContext,
78 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieu02e25db2011-09-20 20:03:50 +000079 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu01fc0012011-09-19 19:01:00 +000080 return;
81
82 Token SecondToken = GetLookAheadToken(2);
Richard Smith7b3f3222012-06-18 06:11:04 +000083 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu01fc0012011-09-19 19:01:00 +000084 return;
85
86 TemplateTy Template;
87 UnqualifiedId TemplateName;
88 TemplateName.setIdentifier(&II, Tok.getLocation());
89 bool MemberOfUnknownSpecialization;
90 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
91 TemplateName, ObjectType, EnteringContext,
92 Template, MemberOfUnknownSpecialization))
93 return;
94
95 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
96 /*AtDigraph*/false);
97}
98
Mike Stump11289f42009-09-09 15:08:12 +000099/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000100///
101/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000102/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000103/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000104///
105/// '::'[opt] nested-name-specifier
106/// '::'
107///
108/// nested-name-specifier:
109/// type-name '::'
110/// namespace-name '::'
111/// nested-name-specifier identifier '::'
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000112/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000113///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000114///
Mike Stump11289f42009-09-09 15:08:12 +0000115/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000116/// nested-name-specifier (or empty)
117///
Mike Stump11289f42009-09-09 15:08:12 +0000118/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000119/// the "." or "->" of a member access expression, this parameter provides the
120/// type of the object whose members are being accessed.
121///
122/// \param EnteringContext whether we will be entering into the context of
123/// the nested-name-specifier after parsing it.
124///
Douglas Gregore610ada2010-02-24 18:44:31 +0000125/// \param MayBePseudoDestructor When non-NULL, points to a flag that
126/// indicates whether this nested-name-specifier may be part of a
127/// pseudo-destructor name. In this case, the flag will be set false
128/// if we don't actually end up parsing a destructor name. Moreorover,
129/// if we do end up determining that we are parsing a destructor name,
130/// the last component of the nested-name-specifier is not parsed as
131/// part of the scope specifier.
132
Douglas Gregor90d554e2010-02-21 18:36:56 +0000133/// member access expression, e.g., the \p T:: in \p p->T::m.
134///
John McCall1f476a12010-02-26 08:45:28 +0000135/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000136bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000137 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000138 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000139 bool *MayBePseudoDestructor,
140 bool IsTypename) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000141 assert(getLangOpts().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000142 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000143
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000144 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor869ad452011-02-24 17:54:50 +0000145 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
146 Tok.getAnnotationRange(),
147 SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000148 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +0000149 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000150 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000151
Douglas Gregor7f741122009-02-25 19:37:18 +0000152 bool HasScopeSpecifier = false;
153
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000154 if (Tok.is(tok::coloncolon)) {
155 // ::new and ::delete aren't nested-name-specifiers.
156 tok::TokenKind NextKind = NextToken().getKind();
157 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
158 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattner45ddec32009-01-05 00:13:00 +0000160 // '::' - Global scope qualifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000161 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
162 return true;
163
Douglas Gregor7f741122009-02-25 19:37:18 +0000164 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000165 }
166
Douglas Gregore610ada2010-02-24 18:44:31 +0000167 bool CheckForDestructor = false;
168 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
169 CheckForDestructor = true;
170 *MayBePseudoDestructor = false;
171 }
172
David Blaikie15a430a2011-12-04 05:04:18 +0000173 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
174 DeclSpec DS(AttrFactory);
175 SourceLocation DeclLoc = Tok.getLocation();
176 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
177 if (Tok.isNot(tok::coloncolon)) {
178 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
179 return false;
180 }
181
182 SourceLocation CCLoc = ConsumeToken();
183 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
184 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
185
186 HasScopeSpecifier = true;
187 }
188
Douglas Gregor7f741122009-02-25 19:37:18 +0000189 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000190 if (HasScopeSpecifier) {
191 // C++ [basic.lookup.classref]p5:
192 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000193 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000194 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000195 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000196 // the class-name-or-namespace-name is looked up in global scope as a
197 // class-name or namespace-name.
198 //
199 // To implement this, we clear out the object type as soon as we've
200 // seen a leading '::' or part of a nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000201 ObjectType = ParsedType();
Douglas Gregor2436e712009-09-17 21:32:03 +0000202
203 if (Tok.is(tok::code_completion)) {
204 // Code completion for a nested-name-specifier, where the code
205 // code completion token follows the '::'.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000206 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidis7d94c922011-04-23 01:04:12 +0000207 // Include code completion token into the range of the scope otherwise
208 // when we try to annotate the scope tokens the dangling code completion
209 // token will cause assertion in
210 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000211 SS.setEndLoc(Tok.getLocation());
212 cutOffParsing();
213 return true;
Douglas Gregor2436e712009-09-17 21:32:03 +0000214 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000215 }
Mike Stump11289f42009-09-09 15:08:12 +0000216
Douglas Gregor7f741122009-02-25 19:37:18 +0000217 // nested-name-specifier:
Chris Lattner0eed3a62009-06-26 03:47:46 +0000218 // nested-name-specifier 'template'[opt] simple-template-id '::'
219
220 // Parse the optional 'template' keyword, then make sure we have
221 // 'identifier <' after it.
222 if (Tok.is(tok::kw_template)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000223 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000224 // nested-name-specifier, since they aren't allowed to start with
225 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000226 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000227 break;
228
Douglas Gregor120635b2009-11-11 16:39:34 +0000229 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000230 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000231
232 UnqualifiedId TemplateName;
233 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000234 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000235 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000236 ConsumeToken();
237 } else if (Tok.is(tok::kw_operator)) {
238 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000239 TemplateName)) {
240 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000241 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000242 }
Douglas Gregor71395fa2009-11-04 00:56:37 +0000243
Alexis Hunted0530f2009-11-28 08:58:14 +0000244 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
245 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000246 Diag(TemplateName.getSourceRange().getBegin(),
247 diag::err_id_after_template_in_nested_name_spec)
248 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000249 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000250 break;
251 }
252 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000253 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000254 break;
255 }
Mike Stump11289f42009-09-09 15:08:12 +0000256
Douglas Gregor120635b2009-11-11 16:39:34 +0000257 // If the next token is not '<', we have a qualified-id that refers
258 // to a template name, such as T::template apply, but is not a
259 // template-id.
260 if (Tok.isNot(tok::less)) {
261 TPA.Revert();
262 break;
263 }
264
265 // Commit to parsing the template-id.
266 TPA.Commit();
Douglas Gregorbb119652010-06-16 23:00:59 +0000267 TemplateTy Template;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000268 if (TemplateNameKind TNK
269 = Actions.ActOnDependentTemplateName(getCurScope(),
270 SS, TemplateKWLoc, TemplateName,
271 ObjectType, EnteringContext,
272 Template)) {
273 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
274 TemplateName, false))
Douglas Gregorbb119652010-06-16 23:00:59 +0000275 return true;
276 } else
John McCall1f476a12010-02-26 08:45:28 +0000277 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000278
Chris Lattner0eed3a62009-06-26 03:47:46 +0000279 continue;
280 }
Mike Stump11289f42009-09-09 15:08:12 +0000281
Douglas Gregor7f741122009-02-25 19:37:18 +0000282 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000283 // We have
Douglas Gregor7f741122009-02-25 19:37:18 +0000284 //
285 // simple-template-id '::'
286 //
287 // So we need to check whether the simple-template-id is of the
Douglas Gregorb67535d2009-03-31 00:43:58 +0000288 // right kind (it should name a type or be dependent), and then
289 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000290 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000291 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
292 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000293 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000294 }
295
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000296 // Consume the template-id token.
297 ConsumeToken();
298
299 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
300 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000301
David Blaikie8c045bc2011-11-07 03:30:03 +0000302 HasScopeSpecifier = true;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000303
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000304 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000305 TemplateId->NumArgs);
306
307 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000308 SS,
309 TemplateId->TemplateKWLoc,
Douglas Gregor8b6070b2011-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 Lattner704edfb2009-06-26 03:45:46 +0000321 }
Argyrios Kyrtzidis13935672011-05-03 18:45:38 +0000322
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000323 continue;
Douglas Gregor7f741122009-02-25 19:37:18 +0000324 }
325
Chris Lattnere2355f72009-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 Lattner1c428032009-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 Gregor90d554e2010-02-21 18:36:56 +0000342 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000343 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
344 Tok.getLocation(),
345 Next.getLocation(), ObjectType,
Douglas Gregor90d554e2010-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 Gregora771f462010-03-31 17:46:05 +0000352 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000353
354 // Recover as if the user wrote '::'.
355 Next.setKind(tok::coloncolon);
356 }
Chris Lattner1c428032009-12-07 01:36:53 +0000357 }
358
Chris Lattnere2355f72009-06-26 03:52:38 +0000359 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000360 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000361 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000362 II, ObjectType)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000363 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000364 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000365 }
366
Chris Lattnere2355f72009-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 Lattner1c428032009-12-07 01:36:53 +0000370 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
371 "NextToken() not working properly!");
Chris Lattnere2355f72009-06-26 03:52:38 +0000372 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000373
Douglas Gregor90c99722011-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 Lattnere2355f72009-06-26 03:52:38 +0000379 continue;
380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Richard Trieu01fc0012011-09-19 19:01:00 +0000382 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smith55858492011-04-14 21:45:45 +0000383
Chris Lattnere2355f72009-06-26 03:52:38 +0000384 // nested-name-specifier:
385 // type-name '<'
386 if (Next.is(tok::less)) {
387 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000388 UnqualifiedId TemplateName;
389 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000390 bool MemberOfUnknownSpecialization;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000391 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000392 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000393 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000394 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000395 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000396 Template,
397 MemberOfUnknownSpecialization)) {
David Blaikie8c045bc2011-11-07 03:30:03 +0000398 // We have found a template name, so annotate this token
Chris Lattnere2355f72009-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 Gregor71395fa2009-11-04 00:56:37 +0000404 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000405 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
406 TemplateName, false))
John McCall1f476a12010-02-26 08:45:28 +0000407 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000408 continue;
Douglas Gregor20c38a72010-05-21 23:43:39 +0000409 }
410
411 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000412 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregor20c38a72010-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 Pichet4e7a2c02011-03-27 19:41:34 +0000417 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000418 if (getLangOpts().MicrosoftExt)
Francois Pichet93921652011-04-22 08:25:24 +0000419 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000420
421 Diag(Tok.getLocation(), DiagID)
Douglas Gregor20c38a72010-05-21 23:43:39 +0000422 << II.getName()
423 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
424
Douglas Gregorbb119652010-06-16 23:00:59 +0000425 if (TemplateNameKind TNK
Douglas Gregor0be31a22010-07-02 17:43:08 +0000426 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000427 SS, SourceLocation(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000428 TemplateName, ObjectType,
429 EnteringContext, Template)) {
430 // Consume the identifier.
431 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000432 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
433 TemplateName, false))
434 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000435 }
436 else
Douglas Gregor20c38a72010-05-21 23:43:39 +0000437 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000438
Douglas Gregor20c38a72010-05-21 23:43:39 +0000439 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000440 }
441 }
442
Douglas Gregor7f741122009-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 Kyrtzidis32a03792008-11-08 16:45:02 +0000446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Douglas Gregore610ada2010-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 McCall1f476a12010-02-26 08:45:28 +0000454 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000455}
456
457/// ParseCXXIdExpression - Handle id-expression.
458///
459/// id-expression:
460/// unqualified-id
461/// qualified-id
462///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000463/// qualified-id:
464/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
465/// '::' identifier
466/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000467/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000468///
Argyrios Kyrtzidis32a03792008-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 Redl3d3f75a2009-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 McCalldadc5752010-08-24 06:29:42 +0000499ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-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 Gregordf593fb2011-11-07 17:33:42 +0000505 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000506
507 SourceLocation TemplateKWLoc;
Douglas Gregora121b752009-11-03 16:56:39 +0000508 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000509 if (ParseUnqualifiedId(SS,
510 /*EnteringContext=*/false,
511 /*AllowDestructorName=*/false,
512 /*AllowConstructorName=*/false,
John McCallba7bf592010-08-24 05:47:05 +0000513 /*ObjectType=*/ ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000514 TemplateKWLoc,
Douglas Gregora121b752009-11-03 16:56:39 +0000515 Name))
516 return ExprError();
John McCalla9ee3252009-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 McCall8d08b9b2010-08-27 09:08:28 +0000520 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
521 isAddressOfOperand = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000522
523 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
524 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000525}
526
Douglas Gregordb0b9f12011-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 Friedmanc7c97142012-01-04 02:40:39 +0000566 SkipUntil(tok::l_brace);
567 SkipUntil(tok::r_brace);
568 return ExprError();
Douglas Gregordb0b9f12011-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() {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000580 assert(getLangOpts().CPlusPlus0x
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000581 && 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 Friedmanc7c97142012-01-04 02:40:39 +0000597 // If lookahead indicates an ObjC message send...
598 // [identifier identifier
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000599 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000600 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000601 }
602
Eli Friedmanc7c97142012-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 Gregordb0b9f12011-08-04 15:30:47 +0000609 LambdaIntroducer Intro;
610 if (TryParseLambdaIntroducer(Intro))
Eli Friedmanc7c97142012-01-04 02:40:39 +0000611 return ExprEmpty();
Douglas Gregordb0b9f12011-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 Gregord8c61782012-02-15 15:34:24 +0000618llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro){
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000619 typedef llvm::Optional<unsigned> DiagResult;
620
621 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000622 BalancedDelimiterTracker T(*this, tok::l_square);
623 T.consumeOpen();
624
625 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregordb0b9f12011-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 Gregora1bffa22012-02-10 17:46:20 +0000633 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000634 first = false;
635 } else if (Tok.is(tok::equal)) {
636 Intro.Default = LCD_ByCopy;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000637 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000638 first = false;
639 }
640
641 while (Tok.isNot(tok::r_square)) {
642 if (!first) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000643 if (Tok.isNot(tok::comma)) {
Douglas Gregor721b14d2012-07-31 00:50:07 +0000644 // Provide a completion for a lambda introducer here. Except
645 // in Objective-C, where this is Almost Surely meant to be a message
646 // send. In that case, fail here and let the ObjC message
647 // expression parser perform the completion.
Douglas Gregor2d8db8f2012-07-31 15:27:48 +0000648 if (Tok.is(tok::code_completion) &&
649 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
650 !Intro.Captures.empty())) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000651 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
652 /*AfterAmpersand=*/false);
653 ConsumeCodeCompletionToken();
654 break;
655 }
656
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000657 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregord8c61782012-02-15 15:34:24 +0000658 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000659 ConsumeToken();
660 }
661
Douglas Gregord8c61782012-02-15 15:34:24 +0000662 if (Tok.is(tok::code_completion)) {
663 // If we're in Objective-C++ and we have a bare '[', then this is more
664 // likely to be a message receiver.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000665 if (getLangOpts().ObjC1 && first)
Douglas Gregord8c61782012-02-15 15:34:24 +0000666 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
667 else
668 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
669 /*AfterAmpersand=*/false);
670 ConsumeCodeCompletionToken();
671 break;
672 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000673
Douglas Gregord8c61782012-02-15 15:34:24 +0000674 first = false;
675
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000676 // Parse capture.
677 LambdaCaptureKind Kind = LCK_ByCopy;
678 SourceLocation Loc;
679 IdentifierInfo* Id = 0;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000680 SourceLocation EllipsisLoc;
681
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000682 if (Tok.is(tok::kw_this)) {
683 Kind = LCK_This;
684 Loc = ConsumeToken();
685 } else {
686 if (Tok.is(tok::amp)) {
687 Kind = LCK_ByRef;
688 ConsumeToken();
Douglas Gregord8c61782012-02-15 15:34:24 +0000689
690 if (Tok.is(tok::code_completion)) {
691 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
692 /*AfterAmpersand=*/true);
693 ConsumeCodeCompletionToken();
694 break;
695 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000696 }
697
698 if (Tok.is(tok::identifier)) {
699 Id = Tok.getIdentifierInfo();
700 Loc = ConsumeToken();
Douglas Gregor3e308b12012-02-14 19:27:52 +0000701
702 if (Tok.is(tok::ellipsis))
703 EllipsisLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000704 } else if (Tok.is(tok::kw_this)) {
705 // FIXME: If we want to suggest a fixit here, will need to return more
706 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
707 // Clear()ed to prevent emission in case of tentative parsing?
708 return DiagResult(diag::err_this_captured_by_reference);
709 } else {
710 return DiagResult(diag::err_expected_capture);
711 }
712 }
713
Douglas Gregor3e308b12012-02-14 19:27:52 +0000714 Intro.addCapture(Kind, Loc, Id, EllipsisLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000715 }
716
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000717 T.consumeClose();
718 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000719
720 return DiagResult();
721}
722
Douglas Gregord8c61782012-02-15 15:34:24 +0000723/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000724///
725/// Returns true if it hit something unexpected.
726bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
727 TentativeParsingAction PA(*this);
728
729 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
730
731 if (DiagID) {
732 PA.Revert();
733 return true;
734 }
735
736 PA.Commit();
737 return false;
738}
739
740/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
741/// expression.
742ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
743 LambdaIntroducer &Intro) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000744 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
745 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
746
747 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
748 "lambda expression parsing");
749
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000750 // Parse lambda-declarator[opt].
751 DeclSpec DS(AttrFactory);
Eli Friedman36d12942012-01-04 04:41:38 +0000752 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000753
754 if (Tok.is(tok::l_paren)) {
755 ParseScope PrototypeScope(this,
756 Scope::FunctionPrototypeScope |
757 Scope::DeclScope);
758
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000759 SourceLocation DeclLoc, DeclEndLoc;
760 BalancedDelimiterTracker T(*this, tok::l_paren);
761 T.consumeOpen();
762 DeclLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000763
764 // Parse parameter-declaration-clause.
765 ParsedAttributes Attr(AttrFactory);
766 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
767 SourceLocation EllipsisLoc;
768
769 if (Tok.isNot(tok::r_paren))
770 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
771
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000772 T.consumeClose();
773 DeclEndLoc = T.getCloseLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000774
775 // Parse 'mutable'[opt].
776 SourceLocation MutableLoc;
777 if (Tok.is(tok::kw_mutable)) {
778 MutableLoc = ConsumeToken();
779 DeclEndLoc = MutableLoc;
780 }
781
782 // Parse exception-specification[opt].
783 ExceptionSpecificationType ESpecType = EST_None;
784 SourceRange ESpecRange;
785 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
786 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
787 ExprResult NoexceptExpr;
Richard Smith2331bbf2012-05-02 22:22:32 +0000788 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +0000789 DynamicExceptions,
790 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +0000791 NoexceptExpr);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000792
793 if (ESpecType != EST_None)
794 DeclEndLoc = ESpecRange.getEnd();
795
796 // Parse attribute-specifier[opt].
797 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
798
799 // Parse trailing-return-type[opt].
Richard Smith700537c2012-06-12 01:51:59 +0000800 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000801 if (Tok.is(tok::arrow)) {
802 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000803 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000804 if (Range.getEnd().isValid())
805 DeclEndLoc = Range.getEnd();
806 }
807
808 PrototypeScope.Exit();
809
810 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
811 /*isVariadic=*/EllipsisLoc.isValid(),
Richard Smith943c4402012-07-30 21:30:52 +0000812 /*isAmbiguous=*/false, EllipsisLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000813 ParamInfo.data(), ParamInfo.size(),
814 DS.getTypeQualifiers(),
815 /*RefQualifierIsLValueRef=*/true,
816 /*RefQualifierLoc=*/SourceLocation(),
Douglas Gregore248eea2011-10-19 06:04:55 +0000817 /*ConstQualifierLoc=*/SourceLocation(),
818 /*VolatileQualifierLoc=*/SourceLocation(),
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000819 MutableLoc,
820 ESpecType, ESpecRange.getBegin(),
821 DynamicExceptions.data(),
822 DynamicExceptionRanges.data(),
823 DynamicExceptions.size(),
824 NoexceptExpr.isUsable() ?
825 NoexceptExpr.get() : 0,
826 DeclLoc, DeclEndLoc, D,
827 TrailingReturnType),
828 Attr, DeclEndLoc);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000829 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
830 // It's common to forget that one needs '()' before 'mutable' or the
831 // result type. Deal with this.
832 Diag(Tok, diag::err_lambda_missing_parens)
833 << Tok.is(tok::arrow)
834 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
835 SourceLocation DeclLoc = Tok.getLocation();
836 SourceLocation DeclEndLoc = DeclLoc;
837
838 // Parse 'mutable', if it's there.
839 SourceLocation MutableLoc;
840 if (Tok.is(tok::kw_mutable)) {
841 MutableLoc = ConsumeToken();
842 DeclEndLoc = MutableLoc;
843 }
844
845 // Parse the return type, if there is one.
Richard Smith700537c2012-06-12 01:51:59 +0000846 TypeResult TrailingReturnType;
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000847 if (Tok.is(tok::arrow)) {
848 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000849 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000850 if (Range.getEnd().isValid())
851 DeclEndLoc = Range.getEnd();
852 }
853
854 ParsedAttributes Attr(AttrFactory);
855 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
856 /*isVariadic=*/false,
Richard Smith943c4402012-07-30 21:30:52 +0000857 /*isAmbiguous=*/false,
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000858 /*EllipsisLoc=*/SourceLocation(),
859 /*Params=*/0, /*NumParams=*/0,
860 /*TypeQuals=*/0,
861 /*RefQualifierIsLValueRef=*/true,
862 /*RefQualifierLoc=*/SourceLocation(),
863 /*ConstQualifierLoc=*/SourceLocation(),
864 /*VolatileQualifierLoc=*/SourceLocation(),
865 MutableLoc,
866 EST_None,
867 /*ESpecLoc=*/SourceLocation(),
868 /*Exceptions=*/0,
869 /*ExceptionRanges=*/0,
870 /*NumExceptions=*/0,
871 /*NoexceptExpr=*/0,
872 DeclLoc, DeclEndLoc, D,
873 TrailingReturnType),
874 Attr, DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000875 }
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000876
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000877
Eli Friedman4817cf72012-01-06 03:05:34 +0000878 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
879 // it.
Douglas Gregorb8389972012-02-21 22:51:27 +0000880 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorb8389972012-02-21 22:51:27 +0000881 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +0000882
Eli Friedman71c80552012-01-05 03:35:19 +0000883 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
884
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000885 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +0000886 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000887 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000888 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
889 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000890 }
891
Eli Friedmanc7c97142012-01-04 02:40:39 +0000892 StmtResult Stmt(ParseCompoundStatementBody());
893 BodyScope.Exit();
894
Eli Friedman898caf82012-01-04 02:46:53 +0000895 if (!Stmt.isInvalid())
Douglas Gregor63798542012-02-20 19:44:39 +0000896 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmanc7c97142012-01-04 02:40:39 +0000897
Eli Friedman898caf82012-01-04 02:46:53 +0000898 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
899 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000900}
901
Chris Lattner29375652006-12-04 18:06:35 +0000902/// ParseCXXCasts - This handles the various ways to cast expressions to another
903/// type.
904///
905/// postfix-expression: [C++ 5.2p1]
906/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
907/// 'static_cast' '<' type-name '>' '(' expression ')'
908/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
909/// 'const_cast' '<' type-name '>' '(' expression ')'
910///
John McCalldadc5752010-08-24 06:29:42 +0000911ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +0000912 tok::TokenKind Kind = Tok.getKind();
913 const char *CastName = 0; // For error messages
914
915 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +0000916 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +0000917 case tok::kw_const_cast: CastName = "const_cast"; break;
918 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
919 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
920 case tok::kw_static_cast: CastName = "static_cast"; break;
921 }
922
923 SourceLocation OpLoc = ConsumeToken();
924 SourceLocation LAngleBracketLoc = Tok.getLocation();
925
Richard Smith55858492011-04-14 21:45:45 +0000926 // Check for "<::" which is parsed as "[:". If found, fix token stream,
927 // diagnose error, suggest fix, and recover parsing.
Richard Smith62e66302012-08-20 17:37:52 +0000928 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
929 Token Next = NextToken();
930 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
931 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
932 }
Richard Smith55858492011-04-14 21:45:45 +0000933
Chris Lattner29375652006-12-04 18:06:35 +0000934 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +0000935 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000936
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000937 // Parse the common declaration-specifiers piece.
938 DeclSpec DS(AttrFactory);
939 ParseSpecifierQualifierList(DS);
940
941 // Parse the abstract-declarator, if present.
942 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
943 ParseDeclarator(DeclaratorInfo);
944
Chris Lattner29375652006-12-04 18:06:35 +0000945 SourceLocation RAngleBracketLoc = Tok.getLocation();
946
Chris Lattner6d29c102008-11-18 07:48:38 +0000947 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +0000948 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +0000949
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000950 SourceLocation LParenLoc, RParenLoc;
951 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +0000952
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000953 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000954 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000955
John McCalldadc5752010-08-24 06:29:42 +0000956 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +0000957
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000958 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000959 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +0000960
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000961 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +0000962 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000963 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +0000964 RAngleBracketLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000965 T.getOpenLocation(), Result.take(),
966 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +0000967
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000968 return Result;
Chris Lattner29375652006-12-04 18:06:35 +0000969}
Bill Wendling4073ed52007-02-13 01:51:42 +0000970
Sebastian Redlc4704762008-11-11 11:37:55 +0000971/// ParseCXXTypeid - This handles the C++ typeid expression.
972///
973/// postfix-expression: [C++ 5.2p1]
974/// 'typeid' '(' expression ')'
975/// 'typeid' '(' type-id ')'
976///
John McCalldadc5752010-08-24 06:29:42 +0000977ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +0000978 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
979
980 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000981 SourceLocation LParenLoc, RParenLoc;
982 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +0000983
984 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000985 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +0000986 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000987 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +0000988
John McCalldadc5752010-08-24 06:29:42 +0000989 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +0000990
Richard Smith4f605af2012-08-18 00:55:03 +0000991 // C++0x [expr.typeid]p3:
992 // When typeid is applied to an expression other than an lvalue of a
993 // polymorphic class type [...] The expression is an unevaluated
994 // operand (Clause 5).
995 //
996 // Note that we can't tell whether the expression is an lvalue of a
997 // polymorphic class type until after we've parsed the expression; we
998 // speculatively assume the subexpression is unevaluated, and fix it up
999 // later.
1000 //
1001 // We enter the unevaluated context before trying to determine whether we
1002 // have a type-id, because the tentative parse logic will try to resolve
1003 // names, and must treat them as unevaluated.
Eli Friedman15681d62012-09-26 04:34:21 +00001004 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1005 Sema::ReuseLambdaContextDecl);
Richard Smith4f605af2012-08-18 00:55:03 +00001006
Sebastian Redlc4704762008-11-11 11:37:55 +00001007 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00001008 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +00001009
1010 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001011 T.consumeClose();
1012 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001013 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001014 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +00001015
1016 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001017 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001018 } else {
1019 Result = ParseExpression();
1020
1021 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001022 if (Result.isInvalid())
Sebastian Redlc4704762008-11-11 11:37:55 +00001023 SkipUntil(tok::r_paren);
1024 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001025 T.consumeClose();
1026 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001027 if (RParenLoc.isInvalid())
1028 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001029
Sebastian Redlc4704762008-11-11 11:37:55 +00001030 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001031 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001032 }
1033 }
1034
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001035 return Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001036}
1037
Francois Pichet9f4f2072010-09-08 12:20:18 +00001038/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1039///
1040/// '__uuidof' '(' expression ')'
1041/// '__uuidof' '(' type-id ')'
1042///
1043ExprResult Parser::ParseCXXUuidof() {
1044 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1045
1046 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001047 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001048
1049 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001050 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +00001051 return ExprError();
1052
1053 ExprResult Result;
1054
1055 if (isTypeIdInParens()) {
1056 TypeResult Ty = ParseTypeName();
1057
1058 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001059 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001060
1061 if (Ty.isInvalid())
1062 return ExprError();
1063
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001064 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1065 Ty.get().getAsOpaquePtr(),
1066 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001067 } else {
1068 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1069 Result = ParseExpression();
1070
1071 // Match the ')'.
1072 if (Result.isInvalid())
1073 SkipUntil(tok::r_paren);
1074 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001075 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001076
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001077 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1078 /*isType=*/false,
1079 Result.release(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001080 }
1081 }
1082
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001083 return Result;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001084}
1085
Douglas Gregore610ada2010-02-24 18:44:31 +00001086/// \brief Parse a C++ pseudo-destructor expression after the base,
1087/// . or -> operator, and nested-name-specifier have already been
1088/// parsed.
1089///
1090/// postfix-expression: [C++ 5.2]
1091/// postfix-expression . pseudo-destructor-name
1092/// postfix-expression -> pseudo-destructor-name
1093///
1094/// pseudo-destructor-name:
1095/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1096/// ::[opt] nested-name-specifier template simple-template-id ::
1097/// ~type-name
1098/// ::[opt] nested-name-specifier[opt] ~type-name
1099///
John McCalldadc5752010-08-24 06:29:42 +00001100ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +00001101Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1102 tok::TokenKind OpKind,
1103 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001104 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001105 // We're parsing either a pseudo-destructor-name or a dependent
1106 // member access that has the same form as a
1107 // pseudo-destructor-name. We parse both in the same way and let
1108 // the action model sort them out.
1109 //
1110 // Note that the ::[opt] nested-name-specifier[opt] has already
1111 // been parsed, and if there was a simple-template-id, it has
1112 // been coalesced into a template-id annotation token.
1113 UnqualifiedId FirstTypeName;
1114 SourceLocation CCLoc;
1115 if (Tok.is(tok::identifier)) {
1116 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1117 ConsumeToken();
1118 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1119 CCLoc = ConsumeToken();
1120 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001121 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1122 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001123 FirstTypeName.setTemplateId(
1124 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1125 ConsumeToken();
1126 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1127 CCLoc = ConsumeToken();
1128 } else {
1129 FirstTypeName.setIdentifier(0, SourceLocation());
1130 }
1131
1132 // Parse the tilde.
1133 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1134 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001135
1136 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1137 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001138 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-12-16 16:03:09 +00001139 if (DS.getTypeSpecType() == TST_error)
1140 return ExprError();
1141 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1142 OpKind, TildeLoc, DS,
1143 Tok.is(tok::l_paren));
1144 }
1145
Douglas Gregore610ada2010-02-24 18:44:31 +00001146 if (!Tok.is(tok::identifier)) {
1147 Diag(Tok, diag::err_destructor_tilde_identifier);
1148 return ExprError();
1149 }
1150
1151 // Parse the second type.
1152 UnqualifiedId SecondTypeName;
1153 IdentifierInfo *Name = Tok.getIdentifierInfo();
1154 SourceLocation NameLoc = ConsumeToken();
1155 SecondTypeName.setIdentifier(Name, NameLoc);
1156
1157 // If there is a '<', the second type name is a template-id. Parse
1158 // it as such.
1159 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001160 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1161 Name, NameLoc,
1162 false, ObjectType, SecondTypeName,
1163 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001164 return ExprError();
1165
John McCallb268a282010-08-23 23:25:46 +00001166 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1167 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001168 SS, FirstTypeName, CCLoc,
1169 TildeLoc, SecondTypeName,
1170 Tok.is(tok::l_paren));
1171}
1172
Bill Wendling4073ed52007-02-13 01:51:42 +00001173/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1174///
1175/// boolean-literal: [C++ 2.13.5]
1176/// 'true'
1177/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001178ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001179 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001180 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001181}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001182
1183/// ParseThrowExpression - This handles the C++ throw expression.
1184///
1185/// throw-expression: [C++ 15]
1186/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001187ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001188 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001189 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001190
Chris Lattner65dd8432008-04-06 06:02:23 +00001191 // If the current token isn't the start of an assignment-expression,
1192 // then the expression is not present. This handles things like:
1193 // "C ? throw : (void)42", which is crazy but legal.
1194 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1195 case tok::semi:
1196 case tok::r_paren:
1197 case tok::r_square:
1198 case tok::r_brace:
1199 case tok::colon:
1200 case tok::comma:
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001201 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001202
Chris Lattner65dd8432008-04-06 06:02:23 +00001203 default:
John McCalldadc5752010-08-24 06:29:42 +00001204 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001205 if (Expr.isInvalid()) return Expr;
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001206 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001207 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001208}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001209
1210/// ParseCXXThis - This handles the C++ 'this' pointer.
1211///
1212/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1213/// a non-lvalue expression whose value is the address of the object for which
1214/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001215ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001216 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1217 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001218 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001219}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001220
1221/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1222/// Can be interpreted either as function-style casting ("int(x)")
1223/// or class type construction ("ClassType(x,y,z)")
1224/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001225/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001226///
1227/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001228/// simple-type-specifier '(' expression-list[opt] ')'
1229/// [C++0x] simple-type-specifier braced-init-list
1230/// typename-specifier '(' expression-list[opt] ')'
1231/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001232///
John McCalldadc5752010-08-24 06:29:42 +00001233ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001234Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001235 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001236 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001237
Sebastian Redl3da34892011-06-05 12:23:16 +00001238 assert((Tok.is(tok::l_paren) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001239 (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001240 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001241
Sebastian Redl3da34892011-06-05 12:23:16 +00001242 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001243 ExprResult Init = ParseBraceInitializer();
1244 if (Init.isInvalid())
1245 return Init;
1246 Expr *InitList = Init.take();
1247 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1248 MultiExprArg(&InitList, 1),
1249 SourceLocation());
Sebastian Redl3da34892011-06-05 12:23:16 +00001250 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001251 BalancedDelimiterTracker T(*this, tok::l_paren);
1252 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001253
Benjamin Kramerf0623432012-08-23 22:51:59 +00001254 ExprVector Exprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00001255 CommaLocsTy CommaLocs;
1256
1257 if (Tok.isNot(tok::r_paren)) {
1258 if (ParseExpressionList(Exprs, CommaLocs)) {
1259 SkipUntil(tok::r_paren);
1260 return ExprError();
1261 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001262 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001263
1264 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001265 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001266
1267 // TypeRep could be null, if it references an invalid typedef.
1268 if (!TypeRep)
1269 return ExprError();
1270
1271 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1272 "Unexpected number of commas!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001273 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001274 Exprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001275 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001276 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001277}
1278
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001279/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001280///
1281/// condition:
1282/// expression
1283/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-02-22 06:49:09 +00001284/// [C++11] type-specifier-seq declarator '=' initializer-clause
1285/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001286/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1287/// '=' assignment-expression
1288///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001289/// \param ExprOut if the condition was parsed as an expression, the parsed
1290/// expression.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001291///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001292/// \param DeclOut if the condition was parsed as a declaration, the parsed
1293/// declaration.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001294///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001295/// \param Loc The location of the start of the statement that requires this
1296/// condition, e.g., the "for" in a for loop.
1297///
1298/// \param ConvertToBoolean Whether the condition expression should be
1299/// converted to a boolean value.
1300///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001301/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001302bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1303 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001304 SourceLocation Loc,
1305 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001306 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001307 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001308 cutOffParsing();
1309 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001310 }
1311
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001312 ParsedAttributesWithRange attrs(AttrFactory);
1313 MaybeParseCXX0XAttributes(attrs);
1314
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001315 if (!isCXXConditionDeclaration()) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001316 ProhibitAttributes(attrs);
1317
Douglas Gregore60e41a2010-05-06 17:25:47 +00001318 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001319 ExprOut = ParseExpression(); // expression
1320 DeclOut = 0;
1321 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001322 return true;
1323
1324 // If required, convert to a boolean value.
1325 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001326 ExprOut
1327 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1328 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001329 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001330
1331 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001332 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001333 ParseSpecifierQualifierList(DS);
1334
1335 // declarator
1336 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1337 ParseDeclarator(DeclaratorInfo);
1338
1339 // simple-asm-expr[opt]
1340 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001341 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001342 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001343 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001344 SkipUntil(tok::semi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001345 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001346 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001347 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001348 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001349 }
1350
1351 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001352 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001353
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001354 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001355 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001356 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001357 DeclOut = Dcl.get();
1358 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001359
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001360 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001361 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001362 bool CopyInitialization = isTokenEqualOrEqualTypo();
1363 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001364 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001365
1366 ExprResult InitExpr = ExprError();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001367 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-02-22 06:49:09 +00001368 Diag(Tok.getLocation(),
1369 diag::warn_cxx98_compat_generalized_initializer_lists);
1370 InitExpr = ParseBraceInitializer();
1371 } else if (CopyInitialization) {
1372 InitExpr = ParseAssignmentExpression();
1373 } else if (Tok.is(tok::l_paren)) {
1374 // This was probably an attempt to initialize the variable.
1375 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1376 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1377 RParen = ConsumeParen();
1378 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1379 diag::err_expected_init_in_condition_lparen)
1380 << SourceRange(LParen, RParen);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001381 } else {
Richard Smith2a15b742012-02-22 06:49:09 +00001382 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1383 diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001384 }
Richard Smith2a15b742012-02-22 06:49:09 +00001385
1386 if (!InitExpr.isInvalid())
1387 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
1388 DS.getTypeSpecType() == DeclSpec::TST_auto);
1389
Douglas Gregore60e41a2010-05-06 17:25:47 +00001390 // FIXME: Build a reference to this declaration? Convert it to bool?
1391 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001392
1393 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001394
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001395 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001396}
1397
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001398/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1399/// This should only be called when the current token is known to be part of
1400/// simple-type-specifier.
1401///
1402/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001403/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001404/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1405/// char
1406/// wchar_t
1407/// bool
1408/// short
1409/// int
1410/// long
1411/// signed
1412/// unsigned
1413/// float
1414/// double
1415/// void
1416/// [GNU] typeof-specifier
1417/// [C++0x] auto [TODO]
1418///
1419/// type-name:
1420/// class-name
1421/// enum-name
1422/// typedef-name
1423///
1424void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1425 DS.SetRangeStart(Tok.getLocation());
1426 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001427 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001428 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001429
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001430 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001431 case tok::identifier: // foo::bar
1432 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001433 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001434 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001435 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001436
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001437 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001438 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001439 if (getTypeAnnotation(Tok))
1440 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1441 getTypeAnnotation(Tok));
1442 else
1443 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001444
1445 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1446 ConsumeToken();
1447
1448 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1449 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1450 // Objective-C interface. If we don't have Objective-C or a '<', this is
1451 // just a normal reference to a typedef name.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001452 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001453 ParseObjCProtocolQualifiers(DS);
1454
1455 DS.Finish(Diags, PP);
1456 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001457 }
Mike Stump11289f42009-09-09 15:08:12 +00001458
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001459 // builtin types
1460 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001461 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001462 break;
1463 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +00001464 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001465 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001466 case tok::kw___int64:
1467 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1468 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001469 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001470 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001471 break;
1472 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001473 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001474 break;
1475 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001476 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001477 break;
1478 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001479 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001480 break;
1481 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001482 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001483 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00001484 case tok::kw___int128:
1485 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1486 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001487 case tok::kw_half:
1488 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1489 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001490 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001491 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001492 break;
1493 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001494 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001495 break;
1496 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001497 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001498 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001499 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001500 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001501 break;
1502 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001503 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001504 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001505 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +00001506 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001507 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001508 case tok::annot_decltype:
1509 case tok::kw_decltype:
1510 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1511 return DS.Finish(Diags, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001512
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001513 // GNU typeof support.
1514 case tok::kw_typeof:
1515 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +00001516 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001517 return;
1518 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001519 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001520 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1521 else
1522 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001523 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +00001524 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001525}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001526
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001527/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1528/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1529/// e.g., "const short int". Note that the DeclSpec is *not* finished
1530/// by parsing the type-specifier-seq, because these sequences are
1531/// typically followed by some form of declarator. Returns true and
1532/// emits diagnostics if this is not a type-specifier-seq, false
1533/// otherwise.
1534///
1535/// type-specifier-seq: [C++ 8.1]
1536/// type-specifier type-specifier-seq[opt]
1537///
1538bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smithc5b05522012-03-12 07:56:15 +00001539 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor40d732f2010-02-24 23:13:13 +00001540 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001541 return false;
1542}
1543
Douglas Gregor7861a802009-11-03 01:35:08 +00001544/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1545/// some form.
1546///
1547/// This routine is invoked when a '<' is encountered after an identifier or
1548/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1549/// whether the unqualified-id is actually a template-id. This routine will
1550/// then parse the template arguments and form the appropriate template-id to
1551/// return to the caller.
1552///
1553/// \param SS the nested-name-specifier that precedes this template-id, if
1554/// we're actually parsing a qualified-id.
1555///
1556/// \param Name for constructor and destructor names, this is the actual
1557/// identifier that may be a template-name.
1558///
1559/// \param NameLoc the location of the class-name in a constructor or
1560/// destructor.
1561///
1562/// \param EnteringContext whether we're entering the scope of the
1563/// nested-name-specifier.
1564///
Douglas Gregor127ea592009-11-03 21:24:04 +00001565/// \param ObjectType if this unqualified-id occurs within a member access
1566/// expression, the type of the base object whose member is being accessed.
1567///
Douglas Gregor7861a802009-11-03 01:35:08 +00001568/// \param Id as input, describes the template-name or operator-function-id
1569/// that precedes the '<'. If template arguments were parsed successfully,
1570/// will be updated with the template-id.
1571///
Douglas Gregore610ada2010-02-24 18:44:31 +00001572/// \param AssumeTemplateId When true, this routine will assume that the name
1573/// refers to a template without performing name lookup to verify.
1574///
Douglas Gregor7861a802009-11-03 01:35:08 +00001575/// \returns true if a parse error occurred, false otherwise.
1576bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001577 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001578 IdentifierInfo *Name,
1579 SourceLocation NameLoc,
1580 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001581 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001582 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001583 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001584 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1585 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001586
1587 TemplateTy Template;
1588 TemplateNameKind TNK = TNK_Non_template;
1589 switch (Id.getKind()) {
1590 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001591 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001592 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001593 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001594 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001595 Id, ObjectType, EnteringContext,
1596 Template);
1597 if (TNK == TNK_Non_template)
1598 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001599 } else {
1600 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001601 TNK = Actions.isTemplateName(getCurScope(), SS,
1602 TemplateKWLoc.isValid(), Id,
1603 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001604 MemberOfUnknownSpecialization);
1605
1606 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1607 ObjectType && IsTemplateArgumentList()) {
1608 // We have something like t->getAs<T>(), where getAs is a
1609 // member of an unknown specialization. However, this will only
1610 // parse correctly as a template, so suggest the keyword 'template'
1611 // before 'getAs' and treat this as a dependent template name.
1612 std::string Name;
1613 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1614 Name = Id.Identifier->getName();
1615 else {
1616 Name = "operator ";
1617 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1618 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1619 else
1620 Name += Id.Identifier->getName();
1621 }
1622 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1623 << Name
1624 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00001625 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1626 SS, TemplateKWLoc, Id,
1627 ObjectType, EnteringContext,
1628 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001629 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001630 return true;
1631 }
1632 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001633 break;
1634
Douglas Gregor3cf81312009-11-03 23:16:33 +00001635 case UnqualifiedId::IK_ConstructorName: {
1636 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001637 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001638 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001639 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1640 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001641 EnteringContext, Template,
1642 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001643 break;
1644 }
1645
Douglas Gregor3cf81312009-11-03 23:16:33 +00001646 case UnqualifiedId::IK_DestructorName: {
1647 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001648 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001649 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001650 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001651 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1652 SS, TemplateKWLoc, TemplateName,
1653 ObjectType, EnteringContext,
1654 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001655 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001656 return true;
1657 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001658 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1659 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001660 EnteringContext, Template,
1661 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001662
John McCallba7bf592010-08-24 05:47:05 +00001663 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001664 Diag(NameLoc, diag::err_destructor_template_id)
1665 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001666 return true;
1667 }
1668 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001669 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001670 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001671
1672 default:
1673 return false;
1674 }
1675
1676 if (TNK == TNK_Non_template)
1677 return false;
1678
1679 // Parse the enclosed template argument list.
1680 SourceLocation LAngleLoc, RAngleLoc;
1681 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001682 if (Tok.is(tok::less) &&
1683 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001684 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001685 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001686 RAngleLoc))
1687 return true;
1688
1689 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001690 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1691 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001692 // Form a parsed representation of the template-id to be stored in the
1693 // UnqualifiedId.
1694 TemplateIdAnnotation *TemplateId
Benjamin Kramer1e6b6062012-04-14 12:14:03 +00001695 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor7861a802009-11-03 01:35:08 +00001696
1697 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1698 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001699 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001700 TemplateId->TemplateNameLoc = Id.StartLocation;
1701 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001702 TemplateId->Name = 0;
1703 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1704 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001705 }
1706
Douglas Gregore7c20652011-03-02 00:47:37 +00001707 TemplateId->SS = SS;
Benjamin Kramer807c2db2012-02-19 23:37:39 +00001708 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall3e56fd42010-08-23 07:28:44 +00001709 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001710 TemplateId->Kind = TNK;
1711 TemplateId->LAngleLoc = LAngleLoc;
1712 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001713 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001714 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001715 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001716 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001717
1718 Id.setTemplateId(TemplateId);
1719 return false;
1720 }
1721
1722 // Bundle the template arguments together.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001723 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnara4244b432012-01-27 08:46:19 +00001724
Douglas Gregor7861a802009-11-03 01:35:08 +00001725 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001726 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001727 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1728 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00001729 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1730 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00001731 if (Type.isInvalid())
1732 return true;
1733
1734 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1735 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1736 else
1737 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1738
1739 return false;
1740}
1741
Douglas Gregor71395fa2009-11-04 00:56:37 +00001742/// \brief Parse an operator-function-id or conversion-function-id as part
1743/// of a C++ unqualified-id.
1744///
1745/// This routine is responsible only for parsing the operator-function-id or
1746/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001747///
1748/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001749/// operator-function-id: [C++ 13.5]
1750/// 'operator' operator
1751///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001752/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001753/// new delete new[] delete[]
1754/// + - * / % ^ & | ~
1755/// ! = < > += -= *= /= %=
1756/// ^= &= |= << >> >>= <<= == !=
1757/// <= >= && || ++ -- , ->* ->
1758/// () []
1759///
1760/// conversion-function-id: [C++ 12.3.2]
1761/// operator conversion-type-id
1762///
1763/// conversion-type-id:
1764/// type-specifier-seq conversion-declarator[opt]
1765///
1766/// conversion-declarator:
1767/// ptr-operator conversion-declarator[opt]
1768/// \endcode
1769///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001770/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor7861a802009-11-03 01:35:08 +00001771/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1772///
1773/// \param EnteringContext whether we are entering the scope of the
1774/// nested-name-specifier.
1775///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001776/// \param ObjectType if this unqualified-id occurs within a member access
1777/// expression, the type of the base object whose member is being accessed.
1778///
1779/// \param Result on a successful parse, contains the parsed unqualified-id.
1780///
1781/// \returns true if parsing fails, false otherwise.
1782bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001783 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001784 UnqualifiedId &Result) {
1785 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1786
1787 // Consume the 'operator' keyword.
1788 SourceLocation KeywordLoc = ConsumeToken();
1789
1790 // Determine what kind of operator name we have.
1791 unsigned SymbolIdx = 0;
1792 SourceLocation SymbolLocations[3];
1793 OverloadedOperatorKind Op = OO_None;
1794 switch (Tok.getKind()) {
1795 case tok::kw_new:
1796 case tok::kw_delete: {
1797 bool isNew = Tok.getKind() == tok::kw_new;
1798 // Consume the 'new' or 'delete'.
1799 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001800 // Check for array new/delete.
1801 if (Tok.is(tok::l_square) &&
1802 (!getLangOpts().CPlusPlus0x || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001803 // Consume the '[' and ']'.
1804 BalancedDelimiterTracker T(*this, tok::l_square);
1805 T.consumeOpen();
1806 T.consumeClose();
1807 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001808 return true;
1809
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001810 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1811 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001812 Op = isNew? OO_Array_New : OO_Array_Delete;
1813 } else {
1814 Op = isNew? OO_New : OO_Delete;
1815 }
1816 break;
1817 }
1818
1819#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1820 case tok::Token: \
1821 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1822 Op = OO_##Name; \
1823 break;
1824#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1825#include "clang/Basic/OperatorKinds.def"
1826
1827 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001828 // Consume the '(' and ')'.
1829 BalancedDelimiterTracker T(*this, tok::l_paren);
1830 T.consumeOpen();
1831 T.consumeClose();
1832 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001833 return true;
1834
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001835 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1836 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001837 Op = OO_Call;
1838 break;
1839 }
1840
1841 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001842 // Consume the '[' and ']'.
1843 BalancedDelimiterTracker T(*this, tok::l_square);
1844 T.consumeOpen();
1845 T.consumeClose();
1846 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001847 return true;
1848
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001849 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1850 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001851 Op = OO_Subscript;
1852 break;
1853 }
1854
1855 case tok::code_completion: {
1856 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001857 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001858 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001859 // Don't try to parse any further.
1860 return true;
1861 }
1862
1863 default:
1864 break;
1865 }
1866
1867 if (Op != OO_None) {
1868 // We have parsed an operator-function-id.
1869 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1870 return false;
1871 }
Alexis Hunt34458502009-11-28 04:44:28 +00001872
1873 // Parse a literal-operator-id.
1874 //
1875 // literal-operator-id: [C++0x 13.5.8]
1876 // operator "" identifier
1877
David Blaikiebbafb8a2012-03-11 07:00:24 +00001878 if (getLangOpts().CPlusPlus0x && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001879 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00001880
Richard Smith7d182a72012-03-08 23:06:02 +00001881 SourceLocation DiagLoc;
1882 unsigned DiagId = 0;
1883
1884 // We're past translation phase 6, so perform string literal concatenation
1885 // before checking for "".
1886 llvm::SmallVector<Token, 4> Toks;
1887 llvm::SmallVector<SourceLocation, 4> TokLocs;
1888 while (isTokenStringLiteral()) {
1889 if (!Tok.is(tok::string_literal) && !DiagId) {
1890 DiagLoc = Tok.getLocation();
1891 DiagId = diag::err_literal_operator_string_prefix;
1892 }
1893 Toks.push_back(Tok);
1894 TokLocs.push_back(ConsumeStringToken());
1895 }
1896
1897 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
1898 if (Literal.hadError)
1899 return true;
1900
1901 // Grab the literal operator's suffix, which will be either the next token
1902 // or a ud-suffix from the string literal.
1903 IdentifierInfo *II = 0;
1904 SourceLocation SuffixLoc;
1905 if (!Literal.getUDSuffix().empty()) {
1906 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
1907 SuffixLoc =
1908 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
1909 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00001910 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00001911 // This form is not permitted by the standard (yet).
1912 DiagLoc = SuffixLoc;
1913 DiagId = diag::err_literal_operator_missing_space;
1914 } else if (Tok.is(tok::identifier)) {
1915 II = Tok.getIdentifierInfo();
1916 SuffixLoc = ConsumeToken();
1917 TokLocs.push_back(SuffixLoc);
1918 } else {
Alexis Hunt34458502009-11-28 04:44:28 +00001919 Diag(Tok.getLocation(), diag::err_expected_ident);
1920 return true;
1921 }
1922
Richard Smith7d182a72012-03-08 23:06:02 +00001923 // The string literal must be empty.
1924 if (!Literal.GetString().empty() || Literal.Pascal) {
1925 DiagLoc = TokLocs.front();
1926 DiagId = diag::err_literal_operator_string_not_empty;
1927 }
1928
1929 if (DiagId) {
1930 // This isn't a valid literal-operator-id, but we think we know
1931 // what the user meant. Tell them what they should have written.
1932 llvm::SmallString<32> Str;
1933 Str += "\"\" ";
1934 Str += II->getName();
1935 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
1936 SourceRange(TokLocs.front(), TokLocs.back()), Str);
1937 }
1938
1939 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Alexis Hunt3d221f22009-11-29 07:34:05 +00001940 return false;
Alexis Hunt34458502009-11-28 04:44:28 +00001941 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00001942
1943 // Parse a conversion-function-id.
1944 //
1945 // conversion-function-id: [C++ 12.3.2]
1946 // operator conversion-type-id
1947 //
1948 // conversion-type-id:
1949 // type-specifier-seq conversion-declarator[opt]
1950 //
1951 // conversion-declarator:
1952 // ptr-operator conversion-declarator[opt]
1953
1954 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00001955 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00001956 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00001957 return true;
1958
1959 // Parse the conversion-declarator, which is merely a sequence of
1960 // ptr-operators.
1961 Declarator D(DS, Declarator::TypeNameContext);
1962 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1963
1964 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00001965 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00001966 if (Ty.isInvalid())
1967 return true;
1968
1969 // Note that this is a conversion-function-id.
1970 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1971 D.getSourceRange().getEnd());
1972 return false;
1973}
1974
1975/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1976/// name of an entity.
1977///
1978/// \code
1979/// unqualified-id: [C++ expr.prim.general]
1980/// identifier
1981/// operator-function-id
1982/// conversion-function-id
1983/// [C++0x] literal-operator-id [TODO]
1984/// ~ class-name
1985/// template-id
1986///
1987/// \endcode
1988///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001989/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor71395fa2009-11-04 00:56:37 +00001990/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1991///
1992/// \param EnteringContext whether we are entering the scope of the
1993/// nested-name-specifier.
1994///
Douglas Gregor7861a802009-11-03 01:35:08 +00001995/// \param AllowDestructorName whether we allow parsing of a destructor name.
1996///
1997/// \param AllowConstructorName whether we allow parsing a constructor name.
1998///
Douglas Gregor127ea592009-11-03 21:24:04 +00001999/// \param ObjectType if this unqualified-id occurs within a member access
2000/// expression, the type of the base object whose member is being accessed.
2001///
Douglas Gregor7861a802009-11-03 01:35:08 +00002002/// \param Result on a successful parse, contains the parsed unqualified-id.
2003///
2004/// \returns true if parsing fails, false otherwise.
2005bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2006 bool AllowDestructorName,
2007 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002008 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002009 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002010 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002011
2012 // Handle 'A::template B'. This is for template-ids which have not
2013 // already been annotated by ParseOptionalCXXScopeSpecifier().
2014 bool TemplateSpecified = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002015 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002016 (ObjectType || SS.isSet())) {
2017 TemplateSpecified = true;
2018 TemplateKWLoc = ConsumeToken();
2019 }
2020
Douglas Gregor7861a802009-11-03 01:35:08 +00002021 // unqualified-id:
2022 // identifier
2023 // template-id (when it hasn't already been annotated)
2024 if (Tok.is(tok::identifier)) {
2025 // Consume the identifier.
2026 IdentifierInfo *Id = Tok.getIdentifierInfo();
2027 SourceLocation IdLoc = ConsumeToken();
2028
David Blaikiebbafb8a2012-03-11 07:00:24 +00002029 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002030 // If we're not in C++, only identifiers matter. Record the
2031 // identifier and return.
2032 Result.setIdentifier(Id, IdLoc);
2033 return false;
2034 }
2035
Douglas Gregor7861a802009-11-03 01:35:08 +00002036 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002037 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002038 // We have parsed a constructor name.
Abramo Bagnara4244b432012-01-27 08:46:19 +00002039 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2040 &SS, false, false,
2041 ParsedType(),
2042 /*IsCtorOrDtorName=*/true,
2043 /*NonTrivialTypeSourceInfo=*/true);
2044 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002045 } else {
2046 // We have parsed an identifier.
2047 Result.setIdentifier(Id, IdLoc);
2048 }
2049
2050 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00002051 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002052 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2053 EnteringContext, ObjectType,
2054 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002055
2056 return false;
2057 }
2058
2059 // unqualified-id:
2060 // template-id (already parsed and annotated)
2061 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002062 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002063
2064 // If the template-name names the current class, then this is a constructor
2065 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002066 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002067 if (SS.isSet()) {
2068 // C++ [class.qual]p2 specifies that a qualified template-name
2069 // is taken as the constructor name where a constructor can be
2070 // declared. Thus, the template arguments are extraneous, so
2071 // complain about them and remove them entirely.
2072 Diag(TemplateId->TemplateNameLoc,
2073 diag::err_out_of_line_constructor_template_id)
2074 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002075 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002076 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnara4244b432012-01-27 08:46:19 +00002077 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2078 TemplateId->TemplateNameLoc,
2079 getCurScope(),
2080 &SS, false, false,
2081 ParsedType(),
2082 /*IsCtorOrDtorName=*/true,
2083 /*NontrivialTypeSourceInfo=*/true);
2084 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002085 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002086 ConsumeToken();
2087 return false;
2088 }
2089
2090 Result.setConstructorTemplateId(TemplateId);
2091 ConsumeToken();
2092 return false;
2093 }
2094
Douglas Gregor7861a802009-11-03 01:35:08 +00002095 // We have already parsed a template-id; consume the annotation token as
2096 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002097 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002098 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00002099 ConsumeToken();
2100 return false;
2101 }
2102
2103 // unqualified-id:
2104 // operator-function-id
2105 // conversion-function-id
2106 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002107 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002108 return true;
2109
Alexis Hunted0530f2009-11-28 08:58:14 +00002110 // If we have an operator-function-id or a literal-operator-id and the next
2111 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00002112 //
2113 // template-id:
2114 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00002115 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2116 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002117 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002118 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2119 0, SourceLocation(),
2120 EnteringContext, ObjectType,
2121 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002122
Douglas Gregor7861a802009-11-03 01:35:08 +00002123 return false;
2124 }
2125
David Blaikiebbafb8a2012-03-11 07:00:24 +00002126 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002127 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002128 // C++ [expr.unary.op]p10:
2129 // There is an ambiguity in the unary-expression ~X(), where X is a
2130 // class-name. The ambiguity is resolved in favor of treating ~ as a
2131 // unary complement rather than treating ~X as referring to a destructor.
2132
2133 // Parse the '~'.
2134 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002135
2136 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2137 DeclSpec DS(AttrFactory);
2138 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2139 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2140 Result.setDestructorName(TildeLoc, Type, EndLoc);
2141 return false;
2142 }
2143 return true;
2144 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002145
2146 // Parse the class-name.
2147 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002148 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002149 return true;
2150 }
2151
2152 // Parse the class-name (or template-name in a simple-template-id).
2153 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2154 SourceLocation ClassNameLoc = ConsumeToken();
2155
Douglas Gregorb22ee882010-05-05 05:58:24 +00002156 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00002157 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002158 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2159 ClassName, ClassNameLoc,
2160 EnteringContext, ObjectType,
2161 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002162 }
2163
Douglas Gregor7861a802009-11-03 01:35:08 +00002164 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002165 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2166 ClassNameLoc, getCurScope(),
2167 SS, ObjectType,
2168 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002169 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002170 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002171
Douglas Gregor7861a802009-11-03 01:35:08 +00002172 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002173 return false;
2174 }
2175
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002176 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002177 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002178 return true;
2179}
2180
Sebastian Redlbd150f42008-11-21 19:14:01 +00002181/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2182/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002183///
Chris Lattner109faf22009-01-04 21:25:24 +00002184/// This method is called to parse the new expression after the optional :: has
2185/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2186/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002187///
2188/// new-expression:
2189/// '::'[opt] 'new' new-placement[opt] new-type-id
2190/// new-initializer[opt]
2191/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2192/// new-initializer[opt]
2193///
2194/// new-placement:
2195/// '(' expression-list ')'
2196///
Sebastian Redl351bb782008-12-02 14:43:59 +00002197/// new-type-id:
2198/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002199/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002200///
2201/// new-declarator:
2202/// ptr-operator new-declarator[opt]
2203/// direct-new-declarator
2204///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002205/// new-initializer:
2206/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002207/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002208///
John McCalldadc5752010-08-24 06:29:42 +00002209ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002210Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2211 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2212 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002213
2214 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2215 // second form of new-expression. It can't be a new-type-id.
2216
Benjamin Kramerf0623432012-08-23 22:51:59 +00002217 ExprVector PlacementArgs;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002218 SourceLocation PlacementLParen, PlacementRParen;
2219
Douglas Gregorf2753b32010-07-13 15:54:32 +00002220 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002221 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002222 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002223 if (Tok.is(tok::l_paren)) {
2224 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002225 BalancedDelimiterTracker T(*this, tok::l_paren);
2226 T.consumeOpen();
2227 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002228 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2229 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002230 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002231 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002232
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002233 T.consumeClose();
2234 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002235 if (PlacementRParen.isInvalid()) {
2236 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002237 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002238 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002239
Sebastian Redl351bb782008-12-02 14:43:59 +00002240 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002241 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002242 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002243 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002244 } else {
2245 // We still need the type.
2246 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002247 BalancedDelimiterTracker T(*this, tok::l_paren);
2248 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002249 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002250 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002251 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002252 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002253 T.consumeClose();
2254 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002255 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002256 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002257 if (ParseCXXTypeSpecifierSeq(DS))
2258 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002259 else {
2260 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002261 ParseDeclaratorInternal(DeclaratorInfo,
2262 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002263 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002264 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002265 }
2266 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002267 // A new-type-id is a simplified type-id, where essentially the
2268 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002269 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002270 if (ParseCXXTypeSpecifierSeq(DS))
2271 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002272 else {
2273 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002274 ParseDeclaratorInternal(DeclaratorInfo,
2275 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002276 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002277 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002278 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002279 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002280 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002281 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002282
Sebastian Redl6047f072012-02-16 12:22:20 +00002283 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002284
2285 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002286 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002287 ExprVector ConstructorArgs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002288 BalancedDelimiterTracker T(*this, tok::l_paren);
2289 T.consumeOpen();
2290 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002291 if (Tok.isNot(tok::r_paren)) {
2292 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002293 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2294 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002295 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002296 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002297 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002298 T.consumeClose();
2299 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002300 if (ConstructorRParen.isInvalid()) {
2301 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002302 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002303 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002304 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2305 ConstructorRParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002306 ConstructorArgs);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002307 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus0x) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002308 Diag(Tok.getLocation(),
2309 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002310 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002311 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002312 if (Initializer.isInvalid())
2313 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002314
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002315 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002316 PlacementArgs, PlacementRParen,
Sebastian Redl6047f072012-02-16 12:22:20 +00002317 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002318}
2319
Sebastian Redlbd150f42008-11-21 19:14:01 +00002320/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2321/// passed to ParseDeclaratorInternal.
2322///
2323/// direct-new-declarator:
2324/// '[' expression ']'
2325/// direct-new-declarator '[' constant-expression ']'
2326///
Chris Lattner109faf22009-01-04 21:25:24 +00002327void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002328 // Parse the array dimensions.
2329 bool first = true;
2330 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002331 // An array-size expression can't start with a lambda.
2332 if (CheckProhibitedCXX11Attribute())
2333 continue;
2334
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002335 BalancedDelimiterTracker T(*this, tok::l_square);
2336 T.consumeOpen();
2337
John McCalldadc5752010-08-24 06:29:42 +00002338 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002339 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002340 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002341 // Recover
2342 SkipUntil(tok::r_square);
2343 return;
2344 }
2345 first = false;
2346
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002347 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002348
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002349 // Attributes here appertain to the array type. C++11 [expr.new]p5.
2350 ParsedAttributes Attrs(AttrFactory);
2351 MaybeParseCXX0XAttributes(Attrs);
2352
John McCall084e83d2011-03-24 11:26:52 +00002353 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002354 /*static=*/false, /*star=*/false,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002355 Size.release(),
2356 T.getOpenLocation(),
2357 T.getCloseLocation()),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002358 Attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002359
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002360 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002361 return;
2362 }
2363}
2364
2365/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2366/// This ambiguity appears in the syntax of the C++ new operator.
2367///
2368/// new-expression:
2369/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2370/// new-initializer[opt]
2371///
2372/// new-placement:
2373/// '(' expression-list ')'
2374///
John McCall37ad5512010-08-23 06:44:23 +00002375bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002376 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002377 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002378 // The '(' was already consumed.
2379 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002380 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002381 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002382 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002383 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002384 }
2385
2386 // It's not a type, it has to be an expression list.
2387 // Discard the comma locations - ActOnCXXNew has enough parameters.
2388 CommaLocsTy CommaLocs;
2389 return ParseExpressionList(PlacementArgs, CommaLocs);
2390}
2391
2392/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2393/// to free memory allocated by new.
2394///
Chris Lattner109faf22009-01-04 21:25:24 +00002395/// This method is called to parse the 'delete' expression after the optional
2396/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2397/// and "Start" is its location. Otherwise, "Start" is the location of the
2398/// 'delete' token.
2399///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002400/// delete-expression:
2401/// '::'[opt] 'delete' cast-expression
2402/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002403ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002404Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2405 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2406 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002407
2408 // Array delete?
2409 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002410 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith10c60722012-08-09 19:01:51 +00002411 // C++11 [expr.delete]p1:
2412 // Whenever the delete keyword is followed by empty square brackets, it
2413 // shall be interpreted as [array delete].
2414 // [Footnote: A lambda expression with a lambda-introducer that consists
2415 // of empty square brackets can follow the delete keyword if
2416 // the lambda expression is enclosed in parentheses.]
2417 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2418 // lambda-introducer.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002419 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002420 BalancedDelimiterTracker T(*this, tok::l_square);
2421
2422 T.consumeOpen();
2423 T.consumeClose();
2424 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002425 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002426 }
2427
John McCalldadc5752010-08-24 06:29:42 +00002428 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002429 if (Operand.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002430 return Operand;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002431
John McCallb268a282010-08-23 23:25:46 +00002432 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002433}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002434
Mike Stump11289f42009-09-09 15:08:12 +00002435static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002436 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002437 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002438 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002439 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002440 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002441 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Alexis Huntf479f1b2011-05-09 18:22:59 +00002442 case tok::kw___has_trivial_constructor:
2443 return UTT_HasTrivialDefaultConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002444 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002445 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2446 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2447 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley65497cc2011-04-27 23:09:49 +00002448 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2449 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002450 case tok::kw___is_class: return UTT_IsClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002451 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2452 case tok::kw___is_compound: return UTT_IsCompound;
2453 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002454 case tok::kw___is_empty: return UTT_IsEmpty;
2455 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregordca70af2011-12-03 18:14:24 +00002456 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley65497cc2011-04-27 23:09:49 +00002457 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2458 case tok::kw___is_function: return UTT_IsFunction;
2459 case tok::kw___is_fundamental: return UTT_IsFundamental;
2460 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallbf4a7d72012-09-25 07:32:49 +00002461 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002462 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2463 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2464 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2465 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2466 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth79803482011-04-23 10:47:20 +00002467 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00002468 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002469 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley65497cc2011-04-27 23:09:49 +00002470 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002471 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley65497cc2011-04-27 23:09:49 +00002472 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley65497cc2011-04-27 23:09:49 +00002473 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2474 case tok::kw___is_scalar: return UTT_IsScalar;
2475 case tok::kw___is_signed: return UTT_IsSigned;
2476 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2477 case tok::kw___is_trivial: return UTT_IsTrivial;
Alexis Huntd9a5cc12011-05-13 00:31:07 +00002478 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002479 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley65497cc2011-04-27 23:09:49 +00002480 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2481 case tok::kw___is_void: return UTT_IsVoid;
2482 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002483 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002484}
2485
2486static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2487 switch(kind) {
Francois Pichet347c4c72010-12-07 00:55:57 +00002488 default: llvm_unreachable("Not a known binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00002489 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley65497cc2011-04-27 23:09:49 +00002490 case tok::kw___is_convertible: return BTT_IsConvertible;
2491 case tok::kw___is_same: return BTT_IsSame;
Francois Pichet34b21132010-12-08 22:35:30 +00002492 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor8006e762011-01-27 20:28:01 +00002493 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor1be329d2012-02-23 07:33:15 +00002494 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002495 }
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002496}
2497
Douglas Gregor29c42f22012-02-24 07:38:34 +00002498static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2499 switch (kind) {
2500 default: llvm_unreachable("Not a known type trait");
2501 case tok::kw___is_trivially_constructible:
2502 return TT_IsTriviallyConstructible;
2503 }
2504}
2505
John Wiegley6242b6a2011-04-28 00:16:57 +00002506static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2507 switch(kind) {
2508 default: llvm_unreachable("Not a known binary type trait");
2509 case tok::kw___array_rank: return ATT_ArrayRank;
2510 case tok::kw___array_extent: return ATT_ArrayExtent;
2511 }
2512}
2513
John Wiegleyf9f65842011-04-25 06:54:41 +00002514static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2515 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002516 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002517 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2518 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2519 }
2520}
2521
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002522/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2523/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2524/// templates.
2525///
2526/// primary-expression:
2527/// [GNU] unary-type-trait '(' type-id ')'
2528///
John McCalldadc5752010-08-24 06:29:42 +00002529ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002530 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2531 SourceLocation Loc = ConsumeToken();
2532
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002533 BalancedDelimiterTracker T(*this, tok::l_paren);
2534 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002535 return ExprError();
2536
2537 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2538 // there will be cryptic errors about mismatched parentheses and missing
2539 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00002540 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002541
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002542 T.consumeClose();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002543
Douglas Gregor220cac52009-02-18 17:45:20 +00002544 if (Ty.isInvalid())
2545 return ExprError();
2546
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002547 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002548}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002549
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002550/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2551/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2552/// templates.
2553///
2554/// primary-expression:
2555/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2556///
2557ExprResult Parser::ParseBinaryTypeTrait() {
2558 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2559 SourceLocation Loc = ConsumeToken();
2560
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002561 BalancedDelimiterTracker T(*this, tok::l_paren);
2562 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002563 return ExprError();
2564
2565 TypeResult LhsTy = ParseTypeName();
2566 if (LhsTy.isInvalid()) {
2567 SkipUntil(tok::r_paren);
2568 return ExprError();
2569 }
2570
2571 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2572 SkipUntil(tok::r_paren);
2573 return ExprError();
2574 }
2575
2576 TypeResult RhsTy = ParseTypeName();
2577 if (RhsTy.isInvalid()) {
2578 SkipUntil(tok::r_paren);
2579 return ExprError();
2580 }
2581
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002582 T.consumeClose();
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002583
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002584 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2585 T.getCloseLocation());
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002586}
2587
Douglas Gregor29c42f22012-02-24 07:38:34 +00002588/// \brief Parse the built-in type-trait pseudo-functions that allow
2589/// implementation of the TR1/C++11 type traits templates.
2590///
2591/// primary-expression:
2592/// type-trait '(' type-id-seq ')'
2593///
2594/// type-id-seq:
2595/// type-id ...[opt] type-id-seq[opt]
2596///
2597ExprResult Parser::ParseTypeTrait() {
2598 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2599 SourceLocation Loc = ConsumeToken();
2600
2601 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2602 if (Parens.expectAndConsume(diag::err_expected_lparen))
2603 return ExprError();
2604
2605 llvm::SmallVector<ParsedType, 2> Args;
2606 do {
2607 // Parse the next type.
2608 TypeResult Ty = ParseTypeName();
2609 if (Ty.isInvalid()) {
2610 Parens.skipToEnd();
2611 return ExprError();
2612 }
2613
2614 // Parse the ellipsis, if present.
2615 if (Tok.is(tok::ellipsis)) {
2616 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2617 if (Ty.isInvalid()) {
2618 Parens.skipToEnd();
2619 return ExprError();
2620 }
2621 }
2622
2623 // Add this type to the list of arguments.
2624 Args.push_back(Ty.get());
2625
2626 if (Tok.is(tok::comma)) {
2627 ConsumeToken();
2628 continue;
2629 }
2630
2631 break;
2632 } while (true);
2633
2634 if (Parens.consumeClose())
2635 return ExprError();
2636
2637 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2638}
2639
John Wiegley6242b6a2011-04-28 00:16:57 +00002640/// ParseArrayTypeTrait - Parse the built-in array type-trait
2641/// pseudo-functions.
2642///
2643/// primary-expression:
2644/// [Embarcadero] '__array_rank' '(' type-id ')'
2645/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2646///
2647ExprResult Parser::ParseArrayTypeTrait() {
2648 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2649 SourceLocation Loc = ConsumeToken();
2650
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002651 BalancedDelimiterTracker T(*this, tok::l_paren);
2652 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley6242b6a2011-04-28 00:16:57 +00002653 return ExprError();
2654
2655 TypeResult Ty = ParseTypeName();
2656 if (Ty.isInvalid()) {
2657 SkipUntil(tok::comma);
2658 SkipUntil(tok::r_paren);
2659 return ExprError();
2660 }
2661
2662 switch (ATT) {
2663 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002664 T.consumeClose();
2665 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2666 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002667 }
2668 case ATT_ArrayExtent: {
2669 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2670 SkipUntil(tok::r_paren);
2671 return ExprError();
2672 }
2673
2674 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002675 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00002676
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002677 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2678 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002679 }
John Wiegley6242b6a2011-04-28 00:16:57 +00002680 }
David Blaikiee4d798f2012-01-20 21:50:17 +00002681 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00002682}
2683
John Wiegleyf9f65842011-04-25 06:54:41 +00002684/// ParseExpressionTrait - Parse built-in expression-trait
2685/// pseudo-functions like __is_lvalue_expr( xxx ).
2686///
2687/// primary-expression:
2688/// [Embarcadero] expression-trait '(' expression ')'
2689///
2690ExprResult Parser::ParseExpressionTrait() {
2691 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2692 SourceLocation Loc = ConsumeToken();
2693
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002694 BalancedDelimiterTracker T(*this, tok::l_paren);
2695 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegleyf9f65842011-04-25 06:54:41 +00002696 return ExprError();
2697
2698 ExprResult Expr = ParseExpression();
2699
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002700 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00002701
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002702 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2703 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00002704}
2705
2706
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002707/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2708/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2709/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002710ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002711Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002712 ParsedType &CastTy,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002713 BalancedDelimiterTracker &Tracker) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002714 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002715 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2716 assert(isTypeIdInParens() && "Not a type-id!");
2717
John McCalldadc5752010-08-24 06:29:42 +00002718 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002719 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002720
2721 // We need to disambiguate a very ugly part of the C++ syntax:
2722 //
2723 // (T())x; - type-id
2724 // (T())*x; - type-id
2725 // (T())/x; - expression
2726 // (T()); - expression
2727 //
2728 // The bad news is that we cannot use the specialized tentative parser, since
2729 // it can only verify that the thing inside the parens can be parsed as
2730 // type-id, it is not useful for determining the context past the parens.
2731 //
2732 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002733 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002734 //
2735 // It uses a scheme similar to parsing inline methods. The parenthesized
2736 // tokens are cached, the context that follows is determined (possibly by
2737 // parsing a cast-expression), and then we re-introduce the cached tokens
2738 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002739
Mike Stump11289f42009-09-09 15:08:12 +00002740 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002741 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002742
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002743 // Store the tokens of the parentheses. We will parse them after we determine
2744 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002745 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002746 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002747 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002748 return ExprError();
2749 }
2750
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002751 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002752 ParseAs = CompoundLiteral;
2753 } else {
2754 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002755 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2756 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2757 NotCastExpr = true;
2758 } else {
2759 // Try parsing the cast-expression that may follow.
2760 // If it is not a cast-expression, NotCastExpr will be true and no token
2761 // will be consumed.
2762 Result = ParseCastExpression(false/*isUnaryExpression*/,
2763 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002764 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002765 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002766 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002767 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002768
2769 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2770 // an expression.
2771 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002772 }
2773
Mike Stump11289f42009-09-09 15:08:12 +00002774 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002775 Toks.push_back(Tok);
2776 // Re-enter the stored parenthesized tokens into the token stream, so we may
2777 // parse them now.
2778 PP.EnterTokenStream(Toks.data(), Toks.size(),
2779 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2780 // Drop the current token and bring the first cached one. It's the same token
2781 // as when we entered this function.
2782 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002783
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002784 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002785 // Parse the type declarator.
2786 DeclSpec DS(AttrFactory);
2787 ParseSpecifierQualifierList(DS);
2788 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2789 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002790
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002791 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002792 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002793
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002794 if (ParseAs == CompoundLiteral) {
2795 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002796 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002797 return ParseCompoundLiteralExpression(Ty.get(),
2798 Tracker.getOpenLocation(),
2799 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002800 }
Mike Stump11289f42009-09-09 15:08:12 +00002801
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002802 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2803 assert(ParseAs == CastExpr);
2804
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002805 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002806 return ExprError();
2807
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002808 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002809 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002810 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2811 DeclaratorInfo, CastTy,
2812 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002813 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002814 }
Mike Stump11289f42009-09-09 15:08:12 +00002815
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002816 // Not a compound literal, and not followed by a cast-expression.
2817 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002818
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002819 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002820 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002821 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002822 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2823 Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002824
2825 // Match the ')'.
2826 if (Result.isInvalid()) {
2827 SkipUntil(tok::r_paren);
2828 return ExprError();
2829 }
Mike Stump11289f42009-09-09 15:08:12 +00002830
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002831 Tracker.consumeClose();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002832 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002833}