blob: e478ef4db3e56ccdb444ff7ae53e0eaecec36a0b [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
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000759 SourceLocation DeclEndLoc;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000760 BalancedDelimiterTracker T(*this, tok::l_paren);
761 T.consumeOpen();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000762 SourceLocation LParenLoc = 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();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000773 SourceLocation RParenLoc = T.getCloseLocation();
774 DeclEndLoc = RParenLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000775
776 // Parse 'mutable'[opt].
777 SourceLocation MutableLoc;
778 if (Tok.is(tok::kw_mutable)) {
779 MutableLoc = ConsumeToken();
780 DeclEndLoc = MutableLoc;
781 }
782
783 // Parse exception-specification[opt].
784 ExceptionSpecificationType ESpecType = EST_None;
785 SourceRange ESpecRange;
786 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
787 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
788 ExprResult NoexceptExpr;
Richard Smith2331bbf2012-05-02 22:22:32 +0000789 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +0000790 DynamicExceptions,
791 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +0000792 NoexceptExpr);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000793
794 if (ESpecType != EST_None)
795 DeclEndLoc = ESpecRange.getEnd();
796
797 // Parse attribute-specifier[opt].
798 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
799
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000800 SourceLocation FunLocalRangeEnd = DeclEndLoc;
801
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000802 // Parse trailing-return-type[opt].
Richard Smith700537c2012-06-12 01:51:59 +0000803 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000804 if (Tok.is(tok::arrow)) {
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000805 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000806 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000807 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000808 if (Range.getEnd().isValid())
809 DeclEndLoc = Range.getEnd();
810 }
811
812 PrototypeScope.Exit();
813
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000814 SourceLocation NoLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000815 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000816 /*isAmbiguous=*/false,
817 LParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000818 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000819 EllipsisLoc, RParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000820 DS.getTypeQualifiers(),
821 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000822 /*RefQualifierLoc=*/NoLoc,
823 /*ConstQualifierLoc=*/NoLoc,
824 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000825 MutableLoc,
826 ESpecType, ESpecRange.getBegin(),
827 DynamicExceptions.data(),
828 DynamicExceptionRanges.data(),
829 DynamicExceptions.size(),
830 NoexceptExpr.isUsable() ?
831 NoexceptExpr.get() : 0,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000832 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000833 TrailingReturnType),
834 Attr, DeclEndLoc);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000835 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
836 // It's common to forget that one needs '()' before 'mutable' or the
837 // result type. Deal with this.
838 Diag(Tok, diag::err_lambda_missing_parens)
839 << Tok.is(tok::arrow)
840 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
841 SourceLocation DeclLoc = Tok.getLocation();
842 SourceLocation DeclEndLoc = DeclLoc;
843
844 // Parse 'mutable', if it's there.
845 SourceLocation MutableLoc;
846 if (Tok.is(tok::kw_mutable)) {
847 MutableLoc = ConsumeToken();
848 DeclEndLoc = MutableLoc;
849 }
850
851 // Parse the return type, if there is one.
Richard Smith700537c2012-06-12 01:51:59 +0000852 TypeResult TrailingReturnType;
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000853 if (Tok.is(tok::arrow)) {
854 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000855 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000856 if (Range.getEnd().isValid())
857 DeclEndLoc = Range.getEnd();
858 }
859
860 ParsedAttributes Attr(AttrFactory);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000861 SourceLocation NoLoc;
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000862 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000863 /*isAmbiguous=*/false,
864 /*LParenLoc=*/NoLoc,
865 /*Params=*/0,
866 /*NumParams=*/0,
867 /*EllipsisLoc=*/NoLoc,
868 /*RParenLoc=*/NoLoc,
869 /*TypeQuals=*/0,
870 /*RefQualifierIsLValueRef=*/true,
871 /*RefQualifierLoc=*/NoLoc,
872 /*ConstQualifierLoc=*/NoLoc,
873 /*VolatileQualifierLoc=*/NoLoc,
874 MutableLoc,
875 EST_None,
876 /*ESpecLoc=*/NoLoc,
877 /*Exceptions=*/0,
878 /*ExceptionRanges=*/0,
879 /*NumExceptions=*/0,
880 /*NoexceptExpr=*/0,
881 DeclLoc, DeclEndLoc, D,
882 TrailingReturnType),
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000883 Attr, DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000884 }
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000885
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000886
Eli Friedman4817cf72012-01-06 03:05:34 +0000887 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
888 // it.
Douglas Gregorb8389972012-02-21 22:51:27 +0000889 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorb8389972012-02-21 22:51:27 +0000890 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +0000891
Eli Friedman71c80552012-01-05 03:35:19 +0000892 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
893
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000894 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +0000895 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000896 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000897 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
898 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000899 }
900
Eli Friedmanc7c97142012-01-04 02:40:39 +0000901 StmtResult Stmt(ParseCompoundStatementBody());
902 BodyScope.Exit();
903
Eli Friedman898caf82012-01-04 02:46:53 +0000904 if (!Stmt.isInvalid())
Douglas Gregor63798542012-02-20 19:44:39 +0000905 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmanc7c97142012-01-04 02:40:39 +0000906
Eli Friedman898caf82012-01-04 02:46:53 +0000907 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
908 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000909}
910
Chris Lattner29375652006-12-04 18:06:35 +0000911/// ParseCXXCasts - This handles the various ways to cast expressions to another
912/// type.
913///
914/// postfix-expression: [C++ 5.2p1]
915/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
916/// 'static_cast' '<' type-name '>' '(' expression ')'
917/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
918/// 'const_cast' '<' type-name '>' '(' expression ')'
919///
John McCalldadc5752010-08-24 06:29:42 +0000920ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +0000921 tok::TokenKind Kind = Tok.getKind();
922 const char *CastName = 0; // For error messages
923
924 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +0000925 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +0000926 case tok::kw_const_cast: CastName = "const_cast"; break;
927 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
928 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
929 case tok::kw_static_cast: CastName = "static_cast"; break;
930 }
931
932 SourceLocation OpLoc = ConsumeToken();
933 SourceLocation LAngleBracketLoc = Tok.getLocation();
934
Richard Smith55858492011-04-14 21:45:45 +0000935 // Check for "<::" which is parsed as "[:". If found, fix token stream,
936 // diagnose error, suggest fix, and recover parsing.
Richard Smith62e66302012-08-20 17:37:52 +0000937 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
938 Token Next = NextToken();
939 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
940 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
941 }
Richard Smith55858492011-04-14 21:45:45 +0000942
Chris Lattner29375652006-12-04 18:06:35 +0000943 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +0000944 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000945
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000946 // Parse the common declaration-specifiers piece.
947 DeclSpec DS(AttrFactory);
948 ParseSpecifierQualifierList(DS);
949
950 // Parse the abstract-declarator, if present.
951 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
952 ParseDeclarator(DeclaratorInfo);
953
Chris Lattner29375652006-12-04 18:06:35 +0000954 SourceLocation RAngleBracketLoc = Tok.getLocation();
955
Chris Lattner6d29c102008-11-18 07:48:38 +0000956 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +0000957 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +0000958
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000959 SourceLocation LParenLoc, RParenLoc;
960 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +0000961
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000962 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000963 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000964
John McCalldadc5752010-08-24 06:29:42 +0000965 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +0000966
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000967 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000968 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +0000969
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000970 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +0000971 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000972 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +0000973 RAngleBracketLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000974 T.getOpenLocation(), Result.take(),
975 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +0000976
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000977 return Result;
Chris Lattner29375652006-12-04 18:06:35 +0000978}
Bill Wendling4073ed52007-02-13 01:51:42 +0000979
Sebastian Redlc4704762008-11-11 11:37:55 +0000980/// ParseCXXTypeid - This handles the C++ typeid expression.
981///
982/// postfix-expression: [C++ 5.2p1]
983/// 'typeid' '(' expression ')'
984/// 'typeid' '(' type-id ')'
985///
John McCalldadc5752010-08-24 06:29:42 +0000986ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +0000987 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
988
989 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000990 SourceLocation LParenLoc, RParenLoc;
991 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +0000992
993 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000994 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +0000995 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000996 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +0000997
John McCalldadc5752010-08-24 06:29:42 +0000998 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +0000999
Richard Smith4f605af2012-08-18 00:55:03 +00001000 // C++0x [expr.typeid]p3:
1001 // When typeid is applied to an expression other than an lvalue of a
1002 // polymorphic class type [...] The expression is an unevaluated
1003 // operand (Clause 5).
1004 //
1005 // Note that we can't tell whether the expression is an lvalue of a
1006 // polymorphic class type until after we've parsed the expression; we
1007 // speculatively assume the subexpression is unevaluated, and fix it up
1008 // later.
1009 //
1010 // We enter the unevaluated context before trying to determine whether we
1011 // have a type-id, because the tentative parse logic will try to resolve
1012 // names, and must treat them as unevaluated.
Eli Friedman15681d62012-09-26 04:34:21 +00001013 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1014 Sema::ReuseLambdaContextDecl);
Richard Smith4f605af2012-08-18 00:55:03 +00001015
Sebastian Redlc4704762008-11-11 11:37:55 +00001016 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00001017 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +00001018
1019 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001020 T.consumeClose();
1021 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001022 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001023 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +00001024
1025 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001026 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001027 } else {
1028 Result = ParseExpression();
1029
1030 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001031 if (Result.isInvalid())
Sebastian Redlc4704762008-11-11 11:37:55 +00001032 SkipUntil(tok::r_paren);
1033 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001034 T.consumeClose();
1035 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001036 if (RParenLoc.isInvalid())
1037 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001038
Sebastian Redlc4704762008-11-11 11:37:55 +00001039 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001040 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001041 }
1042 }
1043
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001044 return Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001045}
1046
Francois Pichet9f4f2072010-09-08 12:20:18 +00001047/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1048///
1049/// '__uuidof' '(' expression ')'
1050/// '__uuidof' '(' type-id ')'
1051///
1052ExprResult Parser::ParseCXXUuidof() {
1053 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1054
1055 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001056 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001057
1058 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001059 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +00001060 return ExprError();
1061
1062 ExprResult Result;
1063
1064 if (isTypeIdInParens()) {
1065 TypeResult Ty = ParseTypeName();
1066
1067 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001068 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001069
1070 if (Ty.isInvalid())
1071 return ExprError();
1072
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001073 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1074 Ty.get().getAsOpaquePtr(),
1075 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001076 } else {
1077 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1078 Result = ParseExpression();
1079
1080 // Match the ')'.
1081 if (Result.isInvalid())
1082 SkipUntil(tok::r_paren);
1083 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001084 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001085
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001086 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1087 /*isType=*/false,
1088 Result.release(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001089 }
1090 }
1091
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001092 return Result;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001093}
1094
Douglas Gregore610ada2010-02-24 18:44:31 +00001095/// \brief Parse a C++ pseudo-destructor expression after the base,
1096/// . or -> operator, and nested-name-specifier have already been
1097/// parsed.
1098///
1099/// postfix-expression: [C++ 5.2]
1100/// postfix-expression . pseudo-destructor-name
1101/// postfix-expression -> pseudo-destructor-name
1102///
1103/// pseudo-destructor-name:
1104/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1105/// ::[opt] nested-name-specifier template simple-template-id ::
1106/// ~type-name
1107/// ::[opt] nested-name-specifier[opt] ~type-name
1108///
John McCalldadc5752010-08-24 06:29:42 +00001109ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +00001110Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1111 tok::TokenKind OpKind,
1112 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001113 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001114 // We're parsing either a pseudo-destructor-name or a dependent
1115 // member access that has the same form as a
1116 // pseudo-destructor-name. We parse both in the same way and let
1117 // the action model sort them out.
1118 //
1119 // Note that the ::[opt] nested-name-specifier[opt] has already
1120 // been parsed, and if there was a simple-template-id, it has
1121 // been coalesced into a template-id annotation token.
1122 UnqualifiedId FirstTypeName;
1123 SourceLocation CCLoc;
1124 if (Tok.is(tok::identifier)) {
1125 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1126 ConsumeToken();
1127 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1128 CCLoc = ConsumeToken();
1129 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001130 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1131 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001132 FirstTypeName.setTemplateId(
1133 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1134 ConsumeToken();
1135 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1136 CCLoc = ConsumeToken();
1137 } else {
1138 FirstTypeName.setIdentifier(0, SourceLocation());
1139 }
1140
1141 // Parse the tilde.
1142 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1143 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001144
1145 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1146 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001147 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-12-16 16:03:09 +00001148 if (DS.getTypeSpecType() == TST_error)
1149 return ExprError();
1150 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1151 OpKind, TildeLoc, DS,
1152 Tok.is(tok::l_paren));
1153 }
1154
Douglas Gregore610ada2010-02-24 18:44:31 +00001155 if (!Tok.is(tok::identifier)) {
1156 Diag(Tok, diag::err_destructor_tilde_identifier);
1157 return ExprError();
1158 }
1159
1160 // Parse the second type.
1161 UnqualifiedId SecondTypeName;
1162 IdentifierInfo *Name = Tok.getIdentifierInfo();
1163 SourceLocation NameLoc = ConsumeToken();
1164 SecondTypeName.setIdentifier(Name, NameLoc);
1165
1166 // If there is a '<', the second type name is a template-id. Parse
1167 // it as such.
1168 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001169 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1170 Name, NameLoc,
1171 false, ObjectType, SecondTypeName,
1172 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001173 return ExprError();
1174
John McCallb268a282010-08-23 23:25:46 +00001175 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1176 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001177 SS, FirstTypeName, CCLoc,
1178 TildeLoc, SecondTypeName,
1179 Tok.is(tok::l_paren));
1180}
1181
Bill Wendling4073ed52007-02-13 01:51:42 +00001182/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1183///
1184/// boolean-literal: [C++ 2.13.5]
1185/// 'true'
1186/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001187ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001188 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001189 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001190}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001191
1192/// ParseThrowExpression - This handles the C++ throw expression.
1193///
1194/// throw-expression: [C++ 15]
1195/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001196ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001197 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001198 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001199
Chris Lattner65dd8432008-04-06 06:02:23 +00001200 // If the current token isn't the start of an assignment-expression,
1201 // then the expression is not present. This handles things like:
1202 // "C ? throw : (void)42", which is crazy but legal.
1203 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1204 case tok::semi:
1205 case tok::r_paren:
1206 case tok::r_square:
1207 case tok::r_brace:
1208 case tok::colon:
1209 case tok::comma:
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001210 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001211
Chris Lattner65dd8432008-04-06 06:02:23 +00001212 default:
John McCalldadc5752010-08-24 06:29:42 +00001213 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001214 if (Expr.isInvalid()) return Expr;
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001215 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001216 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001217}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001218
1219/// ParseCXXThis - This handles the C++ 'this' pointer.
1220///
1221/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1222/// a non-lvalue expression whose value is the address of the object for which
1223/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001224ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001225 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1226 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001227 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001228}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001229
1230/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1231/// Can be interpreted either as function-style casting ("int(x)")
1232/// or class type construction ("ClassType(x,y,z)")
1233/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001234/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001235///
1236/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001237/// simple-type-specifier '(' expression-list[opt] ')'
1238/// [C++0x] simple-type-specifier braced-init-list
1239/// typename-specifier '(' expression-list[opt] ')'
1240/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001241///
John McCalldadc5752010-08-24 06:29:42 +00001242ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001243Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001244 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001245 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001246
Sebastian Redl3da34892011-06-05 12:23:16 +00001247 assert((Tok.is(tok::l_paren) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001248 (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001249 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001250
Sebastian Redl3da34892011-06-05 12:23:16 +00001251 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001252 ExprResult Init = ParseBraceInitializer();
1253 if (Init.isInvalid())
1254 return Init;
1255 Expr *InitList = Init.take();
1256 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1257 MultiExprArg(&InitList, 1),
1258 SourceLocation());
Sebastian Redl3da34892011-06-05 12:23:16 +00001259 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001260 BalancedDelimiterTracker T(*this, tok::l_paren);
1261 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001262
Benjamin Kramerf0623432012-08-23 22:51:59 +00001263 ExprVector Exprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00001264 CommaLocsTy CommaLocs;
1265
1266 if (Tok.isNot(tok::r_paren)) {
1267 if (ParseExpressionList(Exprs, CommaLocs)) {
1268 SkipUntil(tok::r_paren);
1269 return ExprError();
1270 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001271 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001272
1273 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001274 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001275
1276 // TypeRep could be null, if it references an invalid typedef.
1277 if (!TypeRep)
1278 return ExprError();
1279
1280 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1281 "Unexpected number of commas!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001282 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001283 Exprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001284 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001285 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001286}
1287
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001288/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001289///
1290/// condition:
1291/// expression
1292/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-02-22 06:49:09 +00001293/// [C++11] type-specifier-seq declarator '=' initializer-clause
1294/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001295/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1296/// '=' assignment-expression
1297///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001298/// \param ExprOut if the condition was parsed as an expression, the parsed
1299/// expression.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001300///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001301/// \param DeclOut if the condition was parsed as a declaration, the parsed
1302/// declaration.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001303///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001304/// \param Loc The location of the start of the statement that requires this
1305/// condition, e.g., the "for" in a for loop.
1306///
1307/// \param ConvertToBoolean Whether the condition expression should be
1308/// converted to a boolean value.
1309///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001310/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001311bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1312 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001313 SourceLocation Loc,
1314 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001315 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001316 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001317 cutOffParsing();
1318 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001319 }
1320
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001321 ParsedAttributesWithRange attrs(AttrFactory);
1322 MaybeParseCXX0XAttributes(attrs);
1323
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001324 if (!isCXXConditionDeclaration()) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001325 ProhibitAttributes(attrs);
1326
Douglas Gregore60e41a2010-05-06 17:25:47 +00001327 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001328 ExprOut = ParseExpression(); // expression
1329 DeclOut = 0;
1330 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001331 return true;
1332
1333 // If required, convert to a boolean value.
1334 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001335 ExprOut
1336 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1337 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001338 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001339
1340 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001341 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001342 ParseSpecifierQualifierList(DS);
1343
1344 // declarator
1345 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1346 ParseDeclarator(DeclaratorInfo);
1347
1348 // simple-asm-expr[opt]
1349 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001350 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001351 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001352 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001353 SkipUntil(tok::semi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001354 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001355 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001356 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001357 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001358 }
1359
1360 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001361 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001362
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001363 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001364 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001365 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001366 DeclOut = Dcl.get();
1367 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001368
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001369 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001370 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001371 bool CopyInitialization = isTokenEqualOrEqualTypo();
1372 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001373 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001374
1375 ExprResult InitExpr = ExprError();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001376 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-02-22 06:49:09 +00001377 Diag(Tok.getLocation(),
1378 diag::warn_cxx98_compat_generalized_initializer_lists);
1379 InitExpr = ParseBraceInitializer();
1380 } else if (CopyInitialization) {
1381 InitExpr = ParseAssignmentExpression();
1382 } else if (Tok.is(tok::l_paren)) {
1383 // This was probably an attempt to initialize the variable.
1384 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1385 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1386 RParen = ConsumeParen();
1387 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1388 diag::err_expected_init_in_condition_lparen)
1389 << SourceRange(LParen, RParen);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001390 } else {
Richard Smith2a15b742012-02-22 06:49:09 +00001391 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1392 diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001393 }
Richard Smith2a15b742012-02-22 06:49:09 +00001394
1395 if (!InitExpr.isInvalid())
1396 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
1397 DS.getTypeSpecType() == DeclSpec::TST_auto);
1398
Douglas Gregore60e41a2010-05-06 17:25:47 +00001399 // FIXME: Build a reference to this declaration? Convert it to bool?
1400 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001401
1402 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001403
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001404 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001405}
1406
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001407/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1408/// This should only be called when the current token is known to be part of
1409/// simple-type-specifier.
1410///
1411/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001412/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001413/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1414/// char
1415/// wchar_t
1416/// bool
1417/// short
1418/// int
1419/// long
1420/// signed
1421/// unsigned
1422/// float
1423/// double
1424/// void
1425/// [GNU] typeof-specifier
1426/// [C++0x] auto [TODO]
1427///
1428/// type-name:
1429/// class-name
1430/// enum-name
1431/// typedef-name
1432///
1433void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1434 DS.SetRangeStart(Tok.getLocation());
1435 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001436 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001437 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001438
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001439 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001440 case tok::identifier: // foo::bar
1441 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001442 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001443 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001444 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001445
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001446 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001447 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001448 if (getTypeAnnotation(Tok))
1449 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1450 getTypeAnnotation(Tok));
1451 else
1452 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001453
1454 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1455 ConsumeToken();
1456
1457 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1458 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1459 // Objective-C interface. If we don't have Objective-C or a '<', this is
1460 // just a normal reference to a typedef name.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001461 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001462 ParseObjCProtocolQualifiers(DS);
1463
1464 DS.Finish(Diags, PP);
1465 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001466 }
Mike Stump11289f42009-09-09 15:08:12 +00001467
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001468 // builtin types
1469 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001470 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001471 break;
1472 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +00001473 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001474 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001475 case tok::kw___int64:
1476 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1477 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001478 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001479 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001480 break;
1481 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001482 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001483 break;
1484 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001485 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001486 break;
1487 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001488 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001489 break;
1490 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001491 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001492 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00001493 case tok::kw___int128:
1494 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1495 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001496 case tok::kw_half:
1497 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1498 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001499 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001500 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001501 break;
1502 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001503 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001504 break;
1505 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001506 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001507 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001508 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001509 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001510 break;
1511 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001512 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001513 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001514 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +00001515 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001516 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001517 case tok::annot_decltype:
1518 case tok::kw_decltype:
1519 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1520 return DS.Finish(Diags, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001521
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001522 // GNU typeof support.
1523 case tok::kw_typeof:
1524 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +00001525 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001526 return;
1527 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001528 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001529 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1530 else
1531 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001532 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +00001533 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001534}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001535
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001536/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1537/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1538/// e.g., "const short int". Note that the DeclSpec is *not* finished
1539/// by parsing the type-specifier-seq, because these sequences are
1540/// typically followed by some form of declarator. Returns true and
1541/// emits diagnostics if this is not a type-specifier-seq, false
1542/// otherwise.
1543///
1544/// type-specifier-seq: [C++ 8.1]
1545/// type-specifier type-specifier-seq[opt]
1546///
1547bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smithc5b05522012-03-12 07:56:15 +00001548 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor40d732f2010-02-24 23:13:13 +00001549 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001550 return false;
1551}
1552
Douglas Gregor7861a802009-11-03 01:35:08 +00001553/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1554/// some form.
1555///
1556/// This routine is invoked when a '<' is encountered after an identifier or
1557/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1558/// whether the unqualified-id is actually a template-id. This routine will
1559/// then parse the template arguments and form the appropriate template-id to
1560/// return to the caller.
1561///
1562/// \param SS the nested-name-specifier that precedes this template-id, if
1563/// we're actually parsing a qualified-id.
1564///
1565/// \param Name for constructor and destructor names, this is the actual
1566/// identifier that may be a template-name.
1567///
1568/// \param NameLoc the location of the class-name in a constructor or
1569/// destructor.
1570///
1571/// \param EnteringContext whether we're entering the scope of the
1572/// nested-name-specifier.
1573///
Douglas Gregor127ea592009-11-03 21:24:04 +00001574/// \param ObjectType if this unqualified-id occurs within a member access
1575/// expression, the type of the base object whose member is being accessed.
1576///
Douglas Gregor7861a802009-11-03 01:35:08 +00001577/// \param Id as input, describes the template-name or operator-function-id
1578/// that precedes the '<'. If template arguments were parsed successfully,
1579/// will be updated with the template-id.
1580///
Douglas Gregore610ada2010-02-24 18:44:31 +00001581/// \param AssumeTemplateId When true, this routine will assume that the name
1582/// refers to a template without performing name lookup to verify.
1583///
Douglas Gregor7861a802009-11-03 01:35:08 +00001584/// \returns true if a parse error occurred, false otherwise.
1585bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001586 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001587 IdentifierInfo *Name,
1588 SourceLocation NameLoc,
1589 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001590 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001591 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001592 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001593 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1594 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001595
1596 TemplateTy Template;
1597 TemplateNameKind TNK = TNK_Non_template;
1598 switch (Id.getKind()) {
1599 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001600 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001601 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001602 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001603 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001604 Id, ObjectType, EnteringContext,
1605 Template);
1606 if (TNK == TNK_Non_template)
1607 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001608 } else {
1609 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001610 TNK = Actions.isTemplateName(getCurScope(), SS,
1611 TemplateKWLoc.isValid(), Id,
1612 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001613 MemberOfUnknownSpecialization);
1614
1615 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1616 ObjectType && IsTemplateArgumentList()) {
1617 // We have something like t->getAs<T>(), where getAs is a
1618 // member of an unknown specialization. However, this will only
1619 // parse correctly as a template, so suggest the keyword 'template'
1620 // before 'getAs' and treat this as a dependent template name.
1621 std::string Name;
1622 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1623 Name = Id.Identifier->getName();
1624 else {
1625 Name = "operator ";
1626 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1627 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1628 else
1629 Name += Id.Identifier->getName();
1630 }
1631 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1632 << Name
1633 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00001634 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1635 SS, TemplateKWLoc, Id,
1636 ObjectType, EnteringContext,
1637 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001638 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001639 return true;
1640 }
1641 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001642 break;
1643
Douglas Gregor3cf81312009-11-03 23:16:33 +00001644 case UnqualifiedId::IK_ConstructorName: {
1645 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001646 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001647 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001648 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1649 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001650 EnteringContext, Template,
1651 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001652 break;
1653 }
1654
Douglas Gregor3cf81312009-11-03 23:16:33 +00001655 case UnqualifiedId::IK_DestructorName: {
1656 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001657 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001658 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001659 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001660 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1661 SS, TemplateKWLoc, TemplateName,
1662 ObjectType, EnteringContext,
1663 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001664 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001665 return true;
1666 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001667 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1668 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001669 EnteringContext, Template,
1670 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001671
John McCallba7bf592010-08-24 05:47:05 +00001672 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001673 Diag(NameLoc, diag::err_destructor_template_id)
1674 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001675 return true;
1676 }
1677 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001678 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001679 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001680
1681 default:
1682 return false;
1683 }
1684
1685 if (TNK == TNK_Non_template)
1686 return false;
1687
1688 // Parse the enclosed template argument list.
1689 SourceLocation LAngleLoc, RAngleLoc;
1690 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001691 if (Tok.is(tok::less) &&
1692 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001693 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001694 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001695 RAngleLoc))
1696 return true;
1697
1698 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001699 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1700 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001701 // Form a parsed representation of the template-id to be stored in the
1702 // UnqualifiedId.
1703 TemplateIdAnnotation *TemplateId
Benjamin Kramer1e6b6062012-04-14 12:14:03 +00001704 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor7861a802009-11-03 01:35:08 +00001705
1706 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1707 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001708 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001709 TemplateId->TemplateNameLoc = Id.StartLocation;
1710 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001711 TemplateId->Name = 0;
1712 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1713 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001714 }
1715
Douglas Gregore7c20652011-03-02 00:47:37 +00001716 TemplateId->SS = SS;
Benjamin Kramer807c2db2012-02-19 23:37:39 +00001717 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall3e56fd42010-08-23 07:28:44 +00001718 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001719 TemplateId->Kind = TNK;
1720 TemplateId->LAngleLoc = LAngleLoc;
1721 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001722 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001723 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001724 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001725 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001726
1727 Id.setTemplateId(TemplateId);
1728 return false;
1729 }
1730
1731 // Bundle the template arguments together.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001732 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnara4244b432012-01-27 08:46:19 +00001733
Douglas Gregor7861a802009-11-03 01:35:08 +00001734 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001735 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001736 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1737 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00001738 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1739 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00001740 if (Type.isInvalid())
1741 return true;
1742
1743 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1744 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1745 else
1746 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1747
1748 return false;
1749}
1750
Douglas Gregor71395fa2009-11-04 00:56:37 +00001751/// \brief Parse an operator-function-id or conversion-function-id as part
1752/// of a C++ unqualified-id.
1753///
1754/// This routine is responsible only for parsing the operator-function-id or
1755/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001756///
1757/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001758/// operator-function-id: [C++ 13.5]
1759/// 'operator' operator
1760///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001761/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001762/// new delete new[] delete[]
1763/// + - * / % ^ & | ~
1764/// ! = < > += -= *= /= %=
1765/// ^= &= |= << >> >>= <<= == !=
1766/// <= >= && || ++ -- , ->* ->
1767/// () []
1768///
1769/// conversion-function-id: [C++ 12.3.2]
1770/// operator conversion-type-id
1771///
1772/// conversion-type-id:
1773/// type-specifier-seq conversion-declarator[opt]
1774///
1775/// conversion-declarator:
1776/// ptr-operator conversion-declarator[opt]
1777/// \endcode
1778///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001779/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor7861a802009-11-03 01:35:08 +00001780/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1781///
1782/// \param EnteringContext whether we are entering the scope of the
1783/// nested-name-specifier.
1784///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001785/// \param ObjectType if this unqualified-id occurs within a member access
1786/// expression, the type of the base object whose member is being accessed.
1787///
1788/// \param Result on a successful parse, contains the parsed unqualified-id.
1789///
1790/// \returns true if parsing fails, false otherwise.
1791bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001792 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001793 UnqualifiedId &Result) {
1794 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1795
1796 // Consume the 'operator' keyword.
1797 SourceLocation KeywordLoc = ConsumeToken();
1798
1799 // Determine what kind of operator name we have.
1800 unsigned SymbolIdx = 0;
1801 SourceLocation SymbolLocations[3];
1802 OverloadedOperatorKind Op = OO_None;
1803 switch (Tok.getKind()) {
1804 case tok::kw_new:
1805 case tok::kw_delete: {
1806 bool isNew = Tok.getKind() == tok::kw_new;
1807 // Consume the 'new' or 'delete'.
1808 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001809 // Check for array new/delete.
1810 if (Tok.is(tok::l_square) &&
1811 (!getLangOpts().CPlusPlus0x || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001812 // Consume the '[' and ']'.
1813 BalancedDelimiterTracker T(*this, tok::l_square);
1814 T.consumeOpen();
1815 T.consumeClose();
1816 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001817 return true;
1818
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001819 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1820 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001821 Op = isNew? OO_Array_New : OO_Array_Delete;
1822 } else {
1823 Op = isNew? OO_New : OO_Delete;
1824 }
1825 break;
1826 }
1827
1828#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1829 case tok::Token: \
1830 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1831 Op = OO_##Name; \
1832 break;
1833#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1834#include "clang/Basic/OperatorKinds.def"
1835
1836 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001837 // Consume the '(' and ')'.
1838 BalancedDelimiterTracker T(*this, tok::l_paren);
1839 T.consumeOpen();
1840 T.consumeClose();
1841 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001842 return true;
1843
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001844 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1845 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001846 Op = OO_Call;
1847 break;
1848 }
1849
1850 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001851 // Consume the '[' and ']'.
1852 BalancedDelimiterTracker T(*this, tok::l_square);
1853 T.consumeOpen();
1854 T.consumeClose();
1855 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001856 return true;
1857
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001858 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1859 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001860 Op = OO_Subscript;
1861 break;
1862 }
1863
1864 case tok::code_completion: {
1865 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001866 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001867 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001868 // Don't try to parse any further.
1869 return true;
1870 }
1871
1872 default:
1873 break;
1874 }
1875
1876 if (Op != OO_None) {
1877 // We have parsed an operator-function-id.
1878 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1879 return false;
1880 }
Alexis Hunt34458502009-11-28 04:44:28 +00001881
1882 // Parse a literal-operator-id.
1883 //
1884 // literal-operator-id: [C++0x 13.5.8]
1885 // operator "" identifier
1886
David Blaikiebbafb8a2012-03-11 07:00:24 +00001887 if (getLangOpts().CPlusPlus0x && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001888 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00001889
Richard Smith7d182a72012-03-08 23:06:02 +00001890 SourceLocation DiagLoc;
1891 unsigned DiagId = 0;
1892
1893 // We're past translation phase 6, so perform string literal concatenation
1894 // before checking for "".
1895 llvm::SmallVector<Token, 4> Toks;
1896 llvm::SmallVector<SourceLocation, 4> TokLocs;
1897 while (isTokenStringLiteral()) {
1898 if (!Tok.is(tok::string_literal) && !DiagId) {
1899 DiagLoc = Tok.getLocation();
1900 DiagId = diag::err_literal_operator_string_prefix;
1901 }
1902 Toks.push_back(Tok);
1903 TokLocs.push_back(ConsumeStringToken());
1904 }
1905
1906 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
1907 if (Literal.hadError)
1908 return true;
1909
1910 // Grab the literal operator's suffix, which will be either the next token
1911 // or a ud-suffix from the string literal.
1912 IdentifierInfo *II = 0;
1913 SourceLocation SuffixLoc;
1914 if (!Literal.getUDSuffix().empty()) {
1915 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
1916 SuffixLoc =
1917 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
1918 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00001919 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00001920 // This form is not permitted by the standard (yet).
1921 DiagLoc = SuffixLoc;
1922 DiagId = diag::err_literal_operator_missing_space;
1923 } else if (Tok.is(tok::identifier)) {
1924 II = Tok.getIdentifierInfo();
1925 SuffixLoc = ConsumeToken();
1926 TokLocs.push_back(SuffixLoc);
1927 } else {
Alexis Hunt34458502009-11-28 04:44:28 +00001928 Diag(Tok.getLocation(), diag::err_expected_ident);
1929 return true;
1930 }
1931
Richard Smith7d182a72012-03-08 23:06:02 +00001932 // The string literal must be empty.
1933 if (!Literal.GetString().empty() || Literal.Pascal) {
1934 DiagLoc = TokLocs.front();
1935 DiagId = diag::err_literal_operator_string_not_empty;
1936 }
1937
1938 if (DiagId) {
1939 // This isn't a valid literal-operator-id, but we think we know
1940 // what the user meant. Tell them what they should have written.
1941 llvm::SmallString<32> Str;
1942 Str += "\"\" ";
1943 Str += II->getName();
1944 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
1945 SourceRange(TokLocs.front(), TokLocs.back()), Str);
1946 }
1947
1948 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Alexis Hunt3d221f22009-11-29 07:34:05 +00001949 return false;
Alexis Hunt34458502009-11-28 04:44:28 +00001950 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00001951
1952 // Parse a conversion-function-id.
1953 //
1954 // conversion-function-id: [C++ 12.3.2]
1955 // operator conversion-type-id
1956 //
1957 // conversion-type-id:
1958 // type-specifier-seq conversion-declarator[opt]
1959 //
1960 // conversion-declarator:
1961 // ptr-operator conversion-declarator[opt]
1962
1963 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00001964 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00001965 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00001966 return true;
1967
1968 // Parse the conversion-declarator, which is merely a sequence of
1969 // ptr-operators.
1970 Declarator D(DS, Declarator::TypeNameContext);
1971 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1972
1973 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00001974 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00001975 if (Ty.isInvalid())
1976 return true;
1977
1978 // Note that this is a conversion-function-id.
1979 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1980 D.getSourceRange().getEnd());
1981 return false;
1982}
1983
1984/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1985/// name of an entity.
1986///
1987/// \code
1988/// unqualified-id: [C++ expr.prim.general]
1989/// identifier
1990/// operator-function-id
1991/// conversion-function-id
1992/// [C++0x] literal-operator-id [TODO]
1993/// ~ class-name
1994/// template-id
1995///
1996/// \endcode
1997///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001998/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor71395fa2009-11-04 00:56:37 +00001999/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2000///
2001/// \param EnteringContext whether we are entering the scope of the
2002/// nested-name-specifier.
2003///
Douglas Gregor7861a802009-11-03 01:35:08 +00002004/// \param AllowDestructorName whether we allow parsing of a destructor name.
2005///
2006/// \param AllowConstructorName whether we allow parsing a constructor name.
2007///
Douglas Gregor127ea592009-11-03 21:24:04 +00002008/// \param ObjectType if this unqualified-id occurs within a member access
2009/// expression, the type of the base object whose member is being accessed.
2010///
Douglas Gregor7861a802009-11-03 01:35:08 +00002011/// \param Result on a successful parse, contains the parsed unqualified-id.
2012///
2013/// \returns true if parsing fails, false otherwise.
2014bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2015 bool AllowDestructorName,
2016 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002017 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002018 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002019 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002020
2021 // Handle 'A::template B'. This is for template-ids which have not
2022 // already been annotated by ParseOptionalCXXScopeSpecifier().
2023 bool TemplateSpecified = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002024 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002025 (ObjectType || SS.isSet())) {
2026 TemplateSpecified = true;
2027 TemplateKWLoc = ConsumeToken();
2028 }
2029
Douglas Gregor7861a802009-11-03 01:35:08 +00002030 // unqualified-id:
2031 // identifier
2032 // template-id (when it hasn't already been annotated)
2033 if (Tok.is(tok::identifier)) {
2034 // Consume the identifier.
2035 IdentifierInfo *Id = Tok.getIdentifierInfo();
2036 SourceLocation IdLoc = ConsumeToken();
2037
David Blaikiebbafb8a2012-03-11 07:00:24 +00002038 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002039 // If we're not in C++, only identifiers matter. Record the
2040 // identifier and return.
2041 Result.setIdentifier(Id, IdLoc);
2042 return false;
2043 }
2044
Douglas Gregor7861a802009-11-03 01:35:08 +00002045 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002046 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002047 // We have parsed a constructor name.
Abramo Bagnara4244b432012-01-27 08:46:19 +00002048 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2049 &SS, false, false,
2050 ParsedType(),
2051 /*IsCtorOrDtorName=*/true,
2052 /*NonTrivialTypeSourceInfo=*/true);
2053 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002054 } else {
2055 // We have parsed an identifier.
2056 Result.setIdentifier(Id, IdLoc);
2057 }
2058
2059 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00002060 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002061 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2062 EnteringContext, ObjectType,
2063 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002064
2065 return false;
2066 }
2067
2068 // unqualified-id:
2069 // template-id (already parsed and annotated)
2070 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002071 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002072
2073 // If the template-name names the current class, then this is a constructor
2074 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002075 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002076 if (SS.isSet()) {
2077 // C++ [class.qual]p2 specifies that a qualified template-name
2078 // is taken as the constructor name where a constructor can be
2079 // declared. Thus, the template arguments are extraneous, so
2080 // complain about them and remove them entirely.
2081 Diag(TemplateId->TemplateNameLoc,
2082 diag::err_out_of_line_constructor_template_id)
2083 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002084 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002085 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnara4244b432012-01-27 08:46:19 +00002086 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2087 TemplateId->TemplateNameLoc,
2088 getCurScope(),
2089 &SS, false, false,
2090 ParsedType(),
2091 /*IsCtorOrDtorName=*/true,
2092 /*NontrivialTypeSourceInfo=*/true);
2093 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002094 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002095 ConsumeToken();
2096 return false;
2097 }
2098
2099 Result.setConstructorTemplateId(TemplateId);
2100 ConsumeToken();
2101 return false;
2102 }
2103
Douglas Gregor7861a802009-11-03 01:35:08 +00002104 // We have already parsed a template-id; consume the annotation token as
2105 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002106 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002107 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00002108 ConsumeToken();
2109 return false;
2110 }
2111
2112 // unqualified-id:
2113 // operator-function-id
2114 // conversion-function-id
2115 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002116 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002117 return true;
2118
Alexis Hunted0530f2009-11-28 08:58:14 +00002119 // If we have an operator-function-id or a literal-operator-id and the next
2120 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00002121 //
2122 // template-id:
2123 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00002124 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2125 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002126 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002127 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2128 0, SourceLocation(),
2129 EnteringContext, ObjectType,
2130 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002131
Douglas Gregor7861a802009-11-03 01:35:08 +00002132 return false;
2133 }
2134
David Blaikiebbafb8a2012-03-11 07:00:24 +00002135 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002136 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002137 // C++ [expr.unary.op]p10:
2138 // There is an ambiguity in the unary-expression ~X(), where X is a
2139 // class-name. The ambiguity is resolved in favor of treating ~ as a
2140 // unary complement rather than treating ~X as referring to a destructor.
2141
2142 // Parse the '~'.
2143 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002144
2145 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2146 DeclSpec DS(AttrFactory);
2147 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2148 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2149 Result.setDestructorName(TildeLoc, Type, EndLoc);
2150 return false;
2151 }
2152 return true;
2153 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002154
2155 // Parse the class-name.
2156 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002157 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002158 return true;
2159 }
2160
2161 // Parse the class-name (or template-name in a simple-template-id).
2162 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2163 SourceLocation ClassNameLoc = ConsumeToken();
2164
Douglas Gregorb22ee882010-05-05 05:58:24 +00002165 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00002166 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002167 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2168 ClassName, ClassNameLoc,
2169 EnteringContext, ObjectType,
2170 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002171 }
2172
Douglas Gregor7861a802009-11-03 01:35:08 +00002173 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002174 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2175 ClassNameLoc, getCurScope(),
2176 SS, ObjectType,
2177 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002178 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002179 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002180
Douglas Gregor7861a802009-11-03 01:35:08 +00002181 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002182 return false;
2183 }
2184
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002185 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002186 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002187 return true;
2188}
2189
Sebastian Redlbd150f42008-11-21 19:14:01 +00002190/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2191/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002192///
Chris Lattner109faf22009-01-04 21:25:24 +00002193/// This method is called to parse the new expression after the optional :: has
2194/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2195/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002196///
2197/// new-expression:
2198/// '::'[opt] 'new' new-placement[opt] new-type-id
2199/// new-initializer[opt]
2200/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2201/// new-initializer[opt]
2202///
2203/// new-placement:
2204/// '(' expression-list ')'
2205///
Sebastian Redl351bb782008-12-02 14:43:59 +00002206/// new-type-id:
2207/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002208/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002209///
2210/// new-declarator:
2211/// ptr-operator new-declarator[opt]
2212/// direct-new-declarator
2213///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002214/// new-initializer:
2215/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002216/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002217///
John McCalldadc5752010-08-24 06:29:42 +00002218ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002219Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2220 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2221 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002222
2223 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2224 // second form of new-expression. It can't be a new-type-id.
2225
Benjamin Kramerf0623432012-08-23 22:51:59 +00002226 ExprVector PlacementArgs;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002227 SourceLocation PlacementLParen, PlacementRParen;
2228
Douglas Gregorf2753b32010-07-13 15:54:32 +00002229 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002230 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002231 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002232 if (Tok.is(tok::l_paren)) {
2233 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002234 BalancedDelimiterTracker T(*this, tok::l_paren);
2235 T.consumeOpen();
2236 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002237 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2238 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002239 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002240 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002241
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002242 T.consumeClose();
2243 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002244 if (PlacementRParen.isInvalid()) {
2245 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002246 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002247 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002248
Sebastian Redl351bb782008-12-02 14:43:59 +00002249 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002250 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002251 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002252 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002253 } else {
2254 // We still need the type.
2255 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002256 BalancedDelimiterTracker T(*this, tok::l_paren);
2257 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002258 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002259 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002260 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002261 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002262 T.consumeClose();
2263 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002264 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002265 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002266 if (ParseCXXTypeSpecifierSeq(DS))
2267 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002268 else {
2269 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002270 ParseDeclaratorInternal(DeclaratorInfo,
2271 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002272 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002273 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002274 }
2275 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002276 // A new-type-id is a simplified type-id, where essentially the
2277 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002278 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002279 if (ParseCXXTypeSpecifierSeq(DS))
2280 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002281 else {
2282 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002283 ParseDeclaratorInternal(DeclaratorInfo,
2284 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002285 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002286 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002287 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002288 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002289 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002290 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002291
Sebastian Redl6047f072012-02-16 12:22:20 +00002292 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002293
2294 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002295 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002296 ExprVector ConstructorArgs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002297 BalancedDelimiterTracker T(*this, tok::l_paren);
2298 T.consumeOpen();
2299 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002300 if (Tok.isNot(tok::r_paren)) {
2301 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002302 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2303 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002304 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002305 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002306 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002307 T.consumeClose();
2308 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002309 if (ConstructorRParen.isInvalid()) {
2310 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002311 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002312 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002313 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2314 ConstructorRParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002315 ConstructorArgs);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002316 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus0x) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002317 Diag(Tok.getLocation(),
2318 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002319 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002320 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002321 if (Initializer.isInvalid())
2322 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002323
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002324 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002325 PlacementArgs, PlacementRParen,
Sebastian Redl6047f072012-02-16 12:22:20 +00002326 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002327}
2328
Sebastian Redlbd150f42008-11-21 19:14:01 +00002329/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2330/// passed to ParseDeclaratorInternal.
2331///
2332/// direct-new-declarator:
2333/// '[' expression ']'
2334/// direct-new-declarator '[' constant-expression ']'
2335///
Chris Lattner109faf22009-01-04 21:25:24 +00002336void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002337 // Parse the array dimensions.
2338 bool first = true;
2339 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002340 // An array-size expression can't start with a lambda.
2341 if (CheckProhibitedCXX11Attribute())
2342 continue;
2343
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002344 BalancedDelimiterTracker T(*this, tok::l_square);
2345 T.consumeOpen();
2346
John McCalldadc5752010-08-24 06:29:42 +00002347 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002348 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002349 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002350 // Recover
2351 SkipUntil(tok::r_square);
2352 return;
2353 }
2354 first = false;
2355
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002356 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002357
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002358 // Attributes here appertain to the array type. C++11 [expr.new]p5.
2359 ParsedAttributes Attrs(AttrFactory);
2360 MaybeParseCXX0XAttributes(Attrs);
2361
John McCall084e83d2011-03-24 11:26:52 +00002362 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002363 /*static=*/false, /*star=*/false,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002364 Size.release(),
2365 T.getOpenLocation(),
2366 T.getCloseLocation()),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002367 Attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002368
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002369 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002370 return;
2371 }
2372}
2373
2374/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2375/// This ambiguity appears in the syntax of the C++ new operator.
2376///
2377/// new-expression:
2378/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2379/// new-initializer[opt]
2380///
2381/// new-placement:
2382/// '(' expression-list ')'
2383///
John McCall37ad5512010-08-23 06:44:23 +00002384bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002385 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002386 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002387 // The '(' was already consumed.
2388 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002389 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002390 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002391 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002392 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002393 }
2394
2395 // It's not a type, it has to be an expression list.
2396 // Discard the comma locations - ActOnCXXNew has enough parameters.
2397 CommaLocsTy CommaLocs;
2398 return ParseExpressionList(PlacementArgs, CommaLocs);
2399}
2400
2401/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2402/// to free memory allocated by new.
2403///
Chris Lattner109faf22009-01-04 21:25:24 +00002404/// This method is called to parse the 'delete' expression after the optional
2405/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2406/// and "Start" is its location. Otherwise, "Start" is the location of the
2407/// 'delete' token.
2408///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002409/// delete-expression:
2410/// '::'[opt] 'delete' cast-expression
2411/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002412ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002413Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2414 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2415 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002416
2417 // Array delete?
2418 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002419 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith10c60722012-08-09 19:01:51 +00002420 // C++11 [expr.delete]p1:
2421 // Whenever the delete keyword is followed by empty square brackets, it
2422 // shall be interpreted as [array delete].
2423 // [Footnote: A lambda expression with a lambda-introducer that consists
2424 // of empty square brackets can follow the delete keyword if
2425 // the lambda expression is enclosed in parentheses.]
2426 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2427 // lambda-introducer.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002428 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002429 BalancedDelimiterTracker T(*this, tok::l_square);
2430
2431 T.consumeOpen();
2432 T.consumeClose();
2433 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002434 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002435 }
2436
John McCalldadc5752010-08-24 06:29:42 +00002437 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002438 if (Operand.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002439 return Operand;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002440
John McCallb268a282010-08-23 23:25:46 +00002441 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002442}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002443
Mike Stump11289f42009-09-09 15:08:12 +00002444static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002445 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002446 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002447 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002448 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002449 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002450 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Alexis Huntf479f1b2011-05-09 18:22:59 +00002451 case tok::kw___has_trivial_constructor:
2452 return UTT_HasTrivialDefaultConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002453 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002454 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2455 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2456 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley65497cc2011-04-27 23:09:49 +00002457 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2458 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002459 case tok::kw___is_class: return UTT_IsClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002460 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2461 case tok::kw___is_compound: return UTT_IsCompound;
2462 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002463 case tok::kw___is_empty: return UTT_IsEmpty;
2464 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregordca70af2011-12-03 18:14:24 +00002465 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley65497cc2011-04-27 23:09:49 +00002466 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2467 case tok::kw___is_function: return UTT_IsFunction;
2468 case tok::kw___is_fundamental: return UTT_IsFundamental;
2469 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallbf4a7d72012-09-25 07:32:49 +00002470 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002471 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2472 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2473 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2474 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2475 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth79803482011-04-23 10:47:20 +00002476 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00002477 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002478 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley65497cc2011-04-27 23:09:49 +00002479 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002480 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley65497cc2011-04-27 23:09:49 +00002481 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley65497cc2011-04-27 23:09:49 +00002482 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2483 case tok::kw___is_scalar: return UTT_IsScalar;
2484 case tok::kw___is_signed: return UTT_IsSigned;
2485 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2486 case tok::kw___is_trivial: return UTT_IsTrivial;
Alexis Huntd9a5cc12011-05-13 00:31:07 +00002487 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002488 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley65497cc2011-04-27 23:09:49 +00002489 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2490 case tok::kw___is_void: return UTT_IsVoid;
2491 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002492 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002493}
2494
2495static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2496 switch(kind) {
Francois Pichet347c4c72010-12-07 00:55:57 +00002497 default: llvm_unreachable("Not a known binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00002498 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley65497cc2011-04-27 23:09:49 +00002499 case tok::kw___is_convertible: return BTT_IsConvertible;
2500 case tok::kw___is_same: return BTT_IsSame;
Francois Pichet34b21132010-12-08 22:35:30 +00002501 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor8006e762011-01-27 20:28:01 +00002502 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor1be329d2012-02-23 07:33:15 +00002503 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002504 }
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002505}
2506
Douglas Gregor29c42f22012-02-24 07:38:34 +00002507static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2508 switch (kind) {
2509 default: llvm_unreachable("Not a known type trait");
2510 case tok::kw___is_trivially_constructible:
2511 return TT_IsTriviallyConstructible;
2512 }
2513}
2514
John Wiegley6242b6a2011-04-28 00:16:57 +00002515static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2516 switch(kind) {
2517 default: llvm_unreachable("Not a known binary type trait");
2518 case tok::kw___array_rank: return ATT_ArrayRank;
2519 case tok::kw___array_extent: return ATT_ArrayExtent;
2520 }
2521}
2522
John Wiegleyf9f65842011-04-25 06:54:41 +00002523static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2524 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002525 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002526 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2527 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2528 }
2529}
2530
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002531/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2532/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2533/// templates.
2534///
2535/// primary-expression:
2536/// [GNU] unary-type-trait '(' type-id ')'
2537///
John McCalldadc5752010-08-24 06:29:42 +00002538ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002539 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2540 SourceLocation Loc = ConsumeToken();
2541
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002542 BalancedDelimiterTracker T(*this, tok::l_paren);
2543 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002544 return ExprError();
2545
2546 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2547 // there will be cryptic errors about mismatched parentheses and missing
2548 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00002549 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002550
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002551 T.consumeClose();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002552
Douglas Gregor220cac52009-02-18 17:45:20 +00002553 if (Ty.isInvalid())
2554 return ExprError();
2555
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002556 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002557}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002558
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002559/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2560/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2561/// templates.
2562///
2563/// primary-expression:
2564/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2565///
2566ExprResult Parser::ParseBinaryTypeTrait() {
2567 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2568 SourceLocation Loc = ConsumeToken();
2569
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002570 BalancedDelimiterTracker T(*this, tok::l_paren);
2571 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002572 return ExprError();
2573
2574 TypeResult LhsTy = ParseTypeName();
2575 if (LhsTy.isInvalid()) {
2576 SkipUntil(tok::r_paren);
2577 return ExprError();
2578 }
2579
2580 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2581 SkipUntil(tok::r_paren);
2582 return ExprError();
2583 }
2584
2585 TypeResult RhsTy = ParseTypeName();
2586 if (RhsTy.isInvalid()) {
2587 SkipUntil(tok::r_paren);
2588 return ExprError();
2589 }
2590
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002591 T.consumeClose();
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002592
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002593 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2594 T.getCloseLocation());
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002595}
2596
Douglas Gregor29c42f22012-02-24 07:38:34 +00002597/// \brief Parse the built-in type-trait pseudo-functions that allow
2598/// implementation of the TR1/C++11 type traits templates.
2599///
2600/// primary-expression:
2601/// type-trait '(' type-id-seq ')'
2602///
2603/// type-id-seq:
2604/// type-id ...[opt] type-id-seq[opt]
2605///
2606ExprResult Parser::ParseTypeTrait() {
2607 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2608 SourceLocation Loc = ConsumeToken();
2609
2610 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2611 if (Parens.expectAndConsume(diag::err_expected_lparen))
2612 return ExprError();
2613
2614 llvm::SmallVector<ParsedType, 2> Args;
2615 do {
2616 // Parse the next type.
2617 TypeResult Ty = ParseTypeName();
2618 if (Ty.isInvalid()) {
2619 Parens.skipToEnd();
2620 return ExprError();
2621 }
2622
2623 // Parse the ellipsis, if present.
2624 if (Tok.is(tok::ellipsis)) {
2625 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2626 if (Ty.isInvalid()) {
2627 Parens.skipToEnd();
2628 return ExprError();
2629 }
2630 }
2631
2632 // Add this type to the list of arguments.
2633 Args.push_back(Ty.get());
2634
2635 if (Tok.is(tok::comma)) {
2636 ConsumeToken();
2637 continue;
2638 }
2639
2640 break;
2641 } while (true);
2642
2643 if (Parens.consumeClose())
2644 return ExprError();
2645
2646 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2647}
2648
John Wiegley6242b6a2011-04-28 00:16:57 +00002649/// ParseArrayTypeTrait - Parse the built-in array type-trait
2650/// pseudo-functions.
2651///
2652/// primary-expression:
2653/// [Embarcadero] '__array_rank' '(' type-id ')'
2654/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2655///
2656ExprResult Parser::ParseArrayTypeTrait() {
2657 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2658 SourceLocation Loc = ConsumeToken();
2659
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002660 BalancedDelimiterTracker T(*this, tok::l_paren);
2661 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley6242b6a2011-04-28 00:16:57 +00002662 return ExprError();
2663
2664 TypeResult Ty = ParseTypeName();
2665 if (Ty.isInvalid()) {
2666 SkipUntil(tok::comma);
2667 SkipUntil(tok::r_paren);
2668 return ExprError();
2669 }
2670
2671 switch (ATT) {
2672 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002673 T.consumeClose();
2674 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2675 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002676 }
2677 case ATT_ArrayExtent: {
2678 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2679 SkipUntil(tok::r_paren);
2680 return ExprError();
2681 }
2682
2683 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002684 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00002685
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002686 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2687 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002688 }
John Wiegley6242b6a2011-04-28 00:16:57 +00002689 }
David Blaikiee4d798f2012-01-20 21:50:17 +00002690 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00002691}
2692
John Wiegleyf9f65842011-04-25 06:54:41 +00002693/// ParseExpressionTrait - Parse built-in expression-trait
2694/// pseudo-functions like __is_lvalue_expr( xxx ).
2695///
2696/// primary-expression:
2697/// [Embarcadero] expression-trait '(' expression ')'
2698///
2699ExprResult Parser::ParseExpressionTrait() {
2700 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2701 SourceLocation Loc = ConsumeToken();
2702
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002703 BalancedDelimiterTracker T(*this, tok::l_paren);
2704 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegleyf9f65842011-04-25 06:54:41 +00002705 return ExprError();
2706
2707 ExprResult Expr = ParseExpression();
2708
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002709 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00002710
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002711 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2712 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00002713}
2714
2715
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002716/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2717/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2718/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002719ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002720Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002721 ParsedType &CastTy,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002722 BalancedDelimiterTracker &Tracker) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002723 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002724 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2725 assert(isTypeIdInParens() && "Not a type-id!");
2726
John McCalldadc5752010-08-24 06:29:42 +00002727 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002728 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002729
2730 // We need to disambiguate a very ugly part of the C++ syntax:
2731 //
2732 // (T())x; - type-id
2733 // (T())*x; - type-id
2734 // (T())/x; - expression
2735 // (T()); - expression
2736 //
2737 // The bad news is that we cannot use the specialized tentative parser, since
2738 // it can only verify that the thing inside the parens can be parsed as
2739 // type-id, it is not useful for determining the context past the parens.
2740 //
2741 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002742 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002743 //
2744 // It uses a scheme similar to parsing inline methods. The parenthesized
2745 // tokens are cached, the context that follows is determined (possibly by
2746 // parsing a cast-expression), and then we re-introduce the cached tokens
2747 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002748
Mike Stump11289f42009-09-09 15:08:12 +00002749 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002750 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002751
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002752 // Store the tokens of the parentheses. We will parse them after we determine
2753 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002754 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002755 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002756 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002757 return ExprError();
2758 }
2759
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002760 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002761 ParseAs = CompoundLiteral;
2762 } else {
2763 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002764 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2765 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2766 NotCastExpr = true;
2767 } else {
2768 // Try parsing the cast-expression that may follow.
2769 // If it is not a cast-expression, NotCastExpr will be true and no token
2770 // will be consumed.
2771 Result = ParseCastExpression(false/*isUnaryExpression*/,
2772 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002773 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002774 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002775 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002776 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002777
2778 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2779 // an expression.
2780 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002781 }
2782
Mike Stump11289f42009-09-09 15:08:12 +00002783 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002784 Toks.push_back(Tok);
2785 // Re-enter the stored parenthesized tokens into the token stream, so we may
2786 // parse them now.
2787 PP.EnterTokenStream(Toks.data(), Toks.size(),
2788 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2789 // Drop the current token and bring the first cached one. It's the same token
2790 // as when we entered this function.
2791 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002792
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002793 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002794 // Parse the type declarator.
2795 DeclSpec DS(AttrFactory);
2796 ParseSpecifierQualifierList(DS);
2797 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2798 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002799
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002800 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002801 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002802
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002803 if (ParseAs == CompoundLiteral) {
2804 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002805 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002806 return ParseCompoundLiteralExpression(Ty.get(),
2807 Tracker.getOpenLocation(),
2808 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002809 }
Mike Stump11289f42009-09-09 15:08:12 +00002810
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002811 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2812 assert(ParseAs == CastExpr);
2813
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002814 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002815 return ExprError();
2816
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002817 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002818 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002819 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2820 DeclaratorInfo, CastTy,
2821 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002822 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002823 }
Mike Stump11289f42009-09-09 15:08:12 +00002824
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002825 // Not a compound literal, and not followed by a cast-expression.
2826 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002827
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002828 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002829 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002830 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002831 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2832 Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002833
2834 // Match the ')'.
2835 if (Result.isInvalid()) {
2836 SkipUntil(tok::r_paren);
2837 return ExprError();
2838 }
Mike Stump11289f42009-09-09 15:08:12 +00002839
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002840 Tracker.consumeClose();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002841 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002842}