blob: fe212f4ebcf68555300a7c1a109cf038d31fba0a [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"
John McCall8b0666c2010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
Douglas Gregordb0b9f12011-08-04 15:30:47 +000019#include "clang/Sema/Scope.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor7861a802009-11-03 01:35:08 +000021#include "llvm/Support/ErrorHandling.h"
22
Chris Lattner29375652006-12-04 18:06:35 +000023using namespace clang;
24
Richard Smith55858492011-04-14 21:45:45 +000025static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
26 switch (Kind) {
27 case tok::kw_template: return 0;
28 case tok::kw_const_cast: return 1;
29 case tok::kw_dynamic_cast: return 2;
30 case tok::kw_reinterpret_cast: return 3;
31 case tok::kw_static_cast: return 4;
32 default:
David Blaikie83d382b2011-09-23 05:06:16 +000033 llvm_unreachable("Unknown type for digraph error message.");
Richard Smith55858492011-04-14 21:45:45 +000034 }
35}
36
37// Are the two tokens adjacent in the same source file?
38static bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
39 SourceManager &SM = PP.getSourceManager();
40 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000041 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smith55858492011-04-14 21:45:45 +000042 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
43}
44
45// Suggest fixit for "<::" after a cast.
46static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
47 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
48 // Pull '<:' and ':' off token stream.
49 if (!AtDigraph)
50 PP.Lex(DigraphToken);
51 PP.Lex(ColonToken);
52
53 SourceRange Range;
54 Range.setBegin(DigraphToken.getLocation());
55 Range.setEnd(ColonToken.getLocation());
56 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
57 << SelectDigraphErrorMessage(Kind)
58 << FixItHint::CreateReplacement(Range, "< ::");
59
60 // Update token information to reflect their change in token type.
61 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000062 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smith55858492011-04-14 21:45:45 +000063 ColonToken.setLength(2);
64 DigraphToken.setKind(tok::less);
65 DigraphToken.setLength(1);
66
67 // Push new tokens back to token stream.
68 PP.EnterToken(ColonToken);
69 if (!AtDigraph)
70 PP.EnterToken(DigraphToken);
71}
72
Richard Trieu01fc0012011-09-19 19:01:00 +000073// Check for '<::' which should be '< ::' instead of '[:' when following
74// a template name.
75void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
76 bool EnteringContext,
77 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieu02e25db2011-09-20 20:03:50 +000078 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu01fc0012011-09-19 19:01:00 +000079 return;
80
81 Token SecondToken = GetLookAheadToken(2);
82 if (!SecondToken.is(tok::colon) || !AreTokensAdjacent(PP, Next, SecondToken))
83 return;
84
85 TemplateTy Template;
86 UnqualifiedId TemplateName;
87 TemplateName.setIdentifier(&II, Tok.getLocation());
88 bool MemberOfUnknownSpecialization;
89 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
90 TemplateName, ObjectType, EnteringContext,
91 Template, MemberOfUnknownSpecialization))
92 return;
93
94 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
95 /*AtDigraph*/false);
96}
97
Mike Stump11289f42009-09-09 15:08:12 +000098/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +000099///
100/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000101/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000102/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000103///
104/// '::'[opt] nested-name-specifier
105/// '::'
106///
107/// nested-name-specifier:
108/// type-name '::'
109/// namespace-name '::'
110/// nested-name-specifier identifier '::'
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000111/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000112///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000113///
Mike Stump11289f42009-09-09 15:08:12 +0000114/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000115/// nested-name-specifier (or empty)
116///
Mike Stump11289f42009-09-09 15:08:12 +0000117/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000118/// the "." or "->" of a member access expression, this parameter provides the
119/// type of the object whose members are being accessed.
120///
121/// \param EnteringContext whether we will be entering into the context of
122/// the nested-name-specifier after parsing it.
123///
Douglas Gregore610ada2010-02-24 18:44:31 +0000124/// \param MayBePseudoDestructor When non-NULL, points to a flag that
125/// indicates whether this nested-name-specifier may be part of a
126/// pseudo-destructor name. In this case, the flag will be set false
127/// if we don't actually end up parsing a destructor name. Moreorover,
128/// if we do end up determining that we are parsing a destructor name,
129/// the last component of the nested-name-specifier is not parsed as
130/// part of the scope specifier.
131
Douglas Gregor90d554e2010-02-21 18:36:56 +0000132/// member access expression, e.g., the \p T:: in \p p->T::m.
133///
John McCall1f476a12010-02-26 08:45:28 +0000134/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000135bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000136 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000137 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000138 bool *MayBePseudoDestructor,
139 bool IsTypename) {
Argyrios Kyrtzidisace521a2008-11-26 21:41:52 +0000140 assert(getLang().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000141 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000142
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000143 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor869ad452011-02-24 17:54:50 +0000144 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
145 Tok.getAnnotationRange(),
146 SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000147 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +0000148 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000149 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000150
Douglas Gregor7f741122009-02-25 19:37:18 +0000151 bool HasScopeSpecifier = false;
152
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000153 if (Tok.is(tok::coloncolon)) {
154 // ::new and ::delete aren't nested-name-specifiers.
155 tok::TokenKind NextKind = NextToken().getKind();
156 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
157 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattner45ddec32009-01-05 00:13:00 +0000159 // '::' - Global scope qualifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000160 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
161 return true;
162
Douglas Gregor7f741122009-02-25 19:37:18 +0000163 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000164 }
165
Douglas Gregore610ada2010-02-24 18:44:31 +0000166 bool CheckForDestructor = false;
167 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
168 CheckForDestructor = true;
169 *MayBePseudoDestructor = false;
170 }
171
David Blaikie15a430a2011-12-04 05:04:18 +0000172 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
173 DeclSpec DS(AttrFactory);
174 SourceLocation DeclLoc = Tok.getLocation();
175 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
176 if (Tok.isNot(tok::coloncolon)) {
177 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
178 return false;
179 }
180
181 SourceLocation CCLoc = ConsumeToken();
182 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
183 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
184
185 HasScopeSpecifier = true;
186 }
187
Douglas Gregor7f741122009-02-25 19:37:18 +0000188 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000189 if (HasScopeSpecifier) {
190 // C++ [basic.lookup.classref]p5:
191 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000192 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000193 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000194 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000195 // the class-name-or-namespace-name is looked up in global scope as a
196 // class-name or namespace-name.
197 //
198 // To implement this, we clear out the object type as soon as we've
199 // seen a leading '::' or part of a nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000200 ObjectType = ParsedType();
Douglas Gregor2436e712009-09-17 21:32:03 +0000201
202 if (Tok.is(tok::code_completion)) {
203 // Code completion for a nested-name-specifier, where the code
204 // code completion token follows the '::'.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000205 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidis7d94c922011-04-23 01:04:12 +0000206 // Include code completion token into the range of the scope otherwise
207 // when we try to annotate the scope tokens the dangling code completion
208 // token will cause assertion in
209 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000210 SS.setEndLoc(Tok.getLocation());
211 cutOffParsing();
212 return true;
Douglas Gregor2436e712009-09-17 21:32:03 +0000213 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000214 }
Mike Stump11289f42009-09-09 15:08:12 +0000215
Douglas Gregor7f741122009-02-25 19:37:18 +0000216 // nested-name-specifier:
Chris Lattner0eed3a62009-06-26 03:47:46 +0000217 // nested-name-specifier 'template'[opt] simple-template-id '::'
218
219 // Parse the optional 'template' keyword, then make sure we have
220 // 'identifier <' after it.
221 if (Tok.is(tok::kw_template)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000222 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000223 // nested-name-specifier, since they aren't allowed to start with
224 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000225 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000226 break;
227
Douglas Gregor120635b2009-11-11 16:39:34 +0000228 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000229 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000230
231 UnqualifiedId TemplateName;
232 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000233 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000234 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000235 ConsumeToken();
236 } else if (Tok.is(tok::kw_operator)) {
237 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000238 TemplateName)) {
239 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000240 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000241 }
Douglas Gregor71395fa2009-11-04 00:56:37 +0000242
Alexis Hunted0530f2009-11-28 08:58:14 +0000243 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
244 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000245 Diag(TemplateName.getSourceRange().getBegin(),
246 diag::err_id_after_template_in_nested_name_spec)
247 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000248 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000249 break;
250 }
251 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000252 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000253 break;
254 }
Mike Stump11289f42009-09-09 15:08:12 +0000255
Douglas Gregor120635b2009-11-11 16:39:34 +0000256 // If the next token is not '<', we have a qualified-id that refers
257 // to a template name, such as T::template apply, but is not a
258 // template-id.
259 if (Tok.isNot(tok::less)) {
260 TPA.Revert();
261 break;
262 }
263
264 // Commit to parsing the template-id.
265 TPA.Commit();
Douglas Gregorbb119652010-06-16 23:00:59 +0000266 TemplateTy Template;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000267 if (TemplateNameKind TNK
268 = Actions.ActOnDependentTemplateName(getCurScope(),
269 SS, TemplateKWLoc, TemplateName,
270 ObjectType, EnteringContext,
271 Template)) {
272 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
273 TemplateName, false))
Douglas Gregorbb119652010-06-16 23:00:59 +0000274 return true;
275 } else
John McCall1f476a12010-02-26 08:45:28 +0000276 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000277
Chris Lattner0eed3a62009-06-26 03:47:46 +0000278 continue;
279 }
Mike Stump11289f42009-09-09 15:08:12 +0000280
Douglas Gregor7f741122009-02-25 19:37:18 +0000281 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000282 // We have
Douglas Gregor7f741122009-02-25 19:37:18 +0000283 //
284 // simple-template-id '::'
285 //
286 // So we need to check whether the simple-template-id is of the
Douglas Gregorb67535d2009-03-31 00:43:58 +0000287 // right kind (it should name a type or be dependent), and then
288 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000289 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000290 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
291 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000292 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000293 }
294
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000295 // Consume the template-id token.
296 ConsumeToken();
297
298 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
299 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000300
David Blaikie8c045bc2011-11-07 03:30:03 +0000301 HasScopeSpecifier = true;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000302
303 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
304 TemplateId->getTemplateArgs(),
305 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;
Francois Pichet0706d202011-09-17 17:15:52 +0000418 if (getLang().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() {
580 assert(getLang().CPlusPlus0x
581 && Tok.is(tok::l_square)
582 && "Not at the start of a possible lambda expression.");
583
584 const Token Next = NextToken(), After = GetLookAheadToken(2);
585
586 // If lookahead indicates this is a lambda...
587 if (Next.is(tok::r_square) || // []
588 Next.is(tok::equal) || // [=
589 (Next.is(tok::amp) && // [&] or [&,
590 (After.is(tok::r_square) ||
591 After.is(tok::comma))) ||
592 (Next.is(tok::identifier) && // [identifier]
593 After.is(tok::r_square))) {
594 return ParseLambdaExpression();
595 }
596
Eli 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.
618llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
619 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) {
643 if (Tok.isNot(tok::comma))
644 return DiagResult(diag::err_expected_comma_or_rsquare);
645 ConsumeToken();
646 }
647
648 first = false;
649
650 // Parse capture.
651 LambdaCaptureKind Kind = LCK_ByCopy;
652 SourceLocation Loc;
653 IdentifierInfo* Id = 0;
654
655 if (Tok.is(tok::kw_this)) {
656 Kind = LCK_This;
657 Loc = ConsumeToken();
658 } else {
659 if (Tok.is(tok::amp)) {
660 Kind = LCK_ByRef;
661 ConsumeToken();
662 }
663
664 if (Tok.is(tok::identifier)) {
665 Id = Tok.getIdentifierInfo();
666 Loc = ConsumeToken();
667 } else if (Tok.is(tok::kw_this)) {
668 // FIXME: If we want to suggest a fixit here, will need to return more
669 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
670 // Clear()ed to prevent emission in case of tentative parsing?
671 return DiagResult(diag::err_this_captured_by_reference);
672 } else {
673 return DiagResult(diag::err_expected_capture);
674 }
675 }
676
677 Intro.addCapture(Kind, Loc, Id);
678 }
679
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000680 T.consumeClose();
681 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000682
683 return DiagResult();
684}
685
686/// TryParseLambdaExpression - Tentatively parse a lambda introducer.
687///
688/// Returns true if it hit something unexpected.
689bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
690 TentativeParsingAction PA(*this);
691
692 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
693
694 if (DiagID) {
695 PA.Revert();
696 return true;
697 }
698
699 PA.Commit();
700 return false;
701}
702
703/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
704/// expression.
705ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
706 LambdaIntroducer &Intro) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000707 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
708 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
709
710 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
711 "lambda expression parsing");
712
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000713 // Parse lambda-declarator[opt].
714 DeclSpec DS(AttrFactory);
Eli Friedman36d12942012-01-04 04:41:38 +0000715 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000716
717 if (Tok.is(tok::l_paren)) {
718 ParseScope PrototypeScope(this,
719 Scope::FunctionPrototypeScope |
720 Scope::DeclScope);
721
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000722 SourceLocation DeclLoc, DeclEndLoc;
723 BalancedDelimiterTracker T(*this, tok::l_paren);
724 T.consumeOpen();
725 DeclLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000726
727 // Parse parameter-declaration-clause.
728 ParsedAttributes Attr(AttrFactory);
729 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
730 SourceLocation EllipsisLoc;
731
732 if (Tok.isNot(tok::r_paren))
733 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
734
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000735 T.consumeClose();
736 DeclEndLoc = T.getCloseLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000737
738 // Parse 'mutable'[opt].
739 SourceLocation MutableLoc;
740 if (Tok.is(tok::kw_mutable)) {
741 MutableLoc = ConsumeToken();
742 DeclEndLoc = MutableLoc;
743 }
744
745 // Parse exception-specification[opt].
746 ExceptionSpecificationType ESpecType = EST_None;
747 SourceRange ESpecRange;
748 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
749 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
750 ExprResult NoexceptExpr;
751 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
752 DynamicExceptions,
753 DynamicExceptionRanges,
754 NoexceptExpr);
755
756 if (ESpecType != EST_None)
757 DeclEndLoc = ESpecRange.getEnd();
758
759 // Parse attribute-specifier[opt].
760 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
761
762 // Parse trailing-return-type[opt].
763 ParsedType TrailingReturnType;
764 if (Tok.is(tok::arrow)) {
765 SourceRange Range;
766 TrailingReturnType = ParseTrailingReturnType(Range).get();
767 if (Range.getEnd().isValid())
768 DeclEndLoc = Range.getEnd();
769 }
770
771 PrototypeScope.Exit();
772
773 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
774 /*isVariadic=*/EllipsisLoc.isValid(),
775 EllipsisLoc,
776 ParamInfo.data(), ParamInfo.size(),
777 DS.getTypeQualifiers(),
778 /*RefQualifierIsLValueRef=*/true,
779 /*RefQualifierLoc=*/SourceLocation(),
Douglas Gregore248eea2011-10-19 06:04:55 +0000780 /*ConstQualifierLoc=*/SourceLocation(),
781 /*VolatileQualifierLoc=*/SourceLocation(),
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000782 MutableLoc,
783 ESpecType, ESpecRange.getBegin(),
784 DynamicExceptions.data(),
785 DynamicExceptionRanges.data(),
786 DynamicExceptions.size(),
787 NoexceptExpr.isUsable() ?
788 NoexceptExpr.get() : 0,
789 DeclLoc, DeclEndLoc, D,
790 TrailingReturnType),
791 Attr, DeclEndLoc);
792 }
793
Eli Friedman4817cf72012-01-06 03:05:34 +0000794 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
795 // it.
796 ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
797 Scope::BreakScope | Scope::ContinueScope |
798 Scope::DeclScope);
799
Eli Friedman71c80552012-01-05 03:35:19 +0000800 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
801
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000802 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +0000803 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000804 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000805 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
806 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000807 }
808
Eli Friedmanc7c97142012-01-04 02:40:39 +0000809 StmtResult Stmt(ParseCompoundStatementBody());
810 BodyScope.Exit();
811
Eli Friedman898caf82012-01-04 02:46:53 +0000812 if (!Stmt.isInvalid())
813 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(),
814 getCurScope());
Eli Friedmanc7c97142012-01-04 02:40:39 +0000815
Eli Friedman898caf82012-01-04 02:46:53 +0000816 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
817 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000818}
819
Chris Lattner29375652006-12-04 18:06:35 +0000820/// ParseCXXCasts - This handles the various ways to cast expressions to another
821/// type.
822///
823/// postfix-expression: [C++ 5.2p1]
824/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
825/// 'static_cast' '<' type-name '>' '(' expression ')'
826/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
827/// 'const_cast' '<' type-name '>' '(' expression ')'
828///
John McCalldadc5752010-08-24 06:29:42 +0000829ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +0000830 tok::TokenKind Kind = Tok.getKind();
831 const char *CastName = 0; // For error messages
832
833 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +0000834 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +0000835 case tok::kw_const_cast: CastName = "const_cast"; break;
836 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
837 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
838 case tok::kw_static_cast: CastName = "static_cast"; break;
839 }
840
841 SourceLocation OpLoc = ConsumeToken();
842 SourceLocation LAngleBracketLoc = Tok.getLocation();
843
Richard Smith55858492011-04-14 21:45:45 +0000844 // Check for "<::" which is parsed as "[:". If found, fix token stream,
845 // diagnose error, suggest fix, and recover parsing.
846 Token Next = NextToken();
847 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
848 AreTokensAdjacent(PP, Tok, Next))
849 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
850
Chris Lattner29375652006-12-04 18:06:35 +0000851 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +0000852 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000853
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000854 // Parse the common declaration-specifiers piece.
855 DeclSpec DS(AttrFactory);
856 ParseSpecifierQualifierList(DS);
857
858 // Parse the abstract-declarator, if present.
859 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
860 ParseDeclarator(DeclaratorInfo);
861
Chris Lattner29375652006-12-04 18:06:35 +0000862 SourceLocation RAngleBracketLoc = Tok.getLocation();
863
Chris Lattner6d29c102008-11-18 07:48:38 +0000864 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +0000865 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +0000866
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000867 SourceLocation LParenLoc, RParenLoc;
868 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +0000869
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000870 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000871 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000872
John McCalldadc5752010-08-24 06:29:42 +0000873 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +0000874
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000875 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000876 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +0000877
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000878 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +0000879 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000880 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +0000881 RAngleBracketLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000882 T.getOpenLocation(), Result.take(),
883 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +0000884
Sebastian Redld65cea82008-12-11 22:51:44 +0000885 return move(Result);
Chris Lattner29375652006-12-04 18:06:35 +0000886}
Bill Wendling4073ed52007-02-13 01:51:42 +0000887
Sebastian Redlc4704762008-11-11 11:37:55 +0000888/// ParseCXXTypeid - This handles the C++ typeid expression.
889///
890/// postfix-expression: [C++ 5.2p1]
891/// 'typeid' '(' expression ')'
892/// 'typeid' '(' type-id ')'
893///
John McCalldadc5752010-08-24 06:29:42 +0000894ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +0000895 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
896
897 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000898 SourceLocation LParenLoc, RParenLoc;
899 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +0000900
901 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000902 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +0000903 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000904 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +0000905
John McCalldadc5752010-08-24 06:29:42 +0000906 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +0000907
908 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +0000909 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +0000910
911 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000912 T.consumeClose();
913 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +0000914 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +0000915 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +0000916
917 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +0000918 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +0000919 } else {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000920 // C++0x [expr.typeid]p3:
Mike Stump11289f42009-09-09 15:08:12 +0000921 // When typeid is applied to an expression other than an lvalue of a
922 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000923 // operand (Clause 5).
924 //
Mike Stump11289f42009-09-09 15:08:12 +0000925 // Note that we can't tell whether the expression is an lvalue of a
Eli Friedman456f0182012-01-20 01:26:23 +0000926 // polymorphic class type until after we've parsed the expression; we
927 // speculatively assume the subexpression is unevaluated, and fix it up
928 // later.
929 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redlc4704762008-11-11 11:37:55 +0000930 Result = ParseExpression();
931
932 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000933 if (Result.isInvalid())
Sebastian Redlc4704762008-11-11 11:37:55 +0000934 SkipUntil(tok::r_paren);
935 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000936 T.consumeClose();
937 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +0000938 if (RParenLoc.isInvalid())
939 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +0000940
Sebastian Redlc4704762008-11-11 11:37:55 +0000941 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000942 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +0000943 }
944 }
945
Sebastian Redld65cea82008-12-11 22:51:44 +0000946 return move(Result);
Sebastian Redlc4704762008-11-11 11:37:55 +0000947}
948
Francois Pichet9f4f2072010-09-08 12:20:18 +0000949/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
950///
951/// '__uuidof' '(' expression ')'
952/// '__uuidof' '(' type-id ')'
953///
954ExprResult Parser::ParseCXXUuidof() {
955 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
956
957 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000958 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +0000959
960 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000961 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +0000962 return ExprError();
963
964 ExprResult Result;
965
966 if (isTypeIdInParens()) {
967 TypeResult Ty = ParseTypeName();
968
969 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000970 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +0000971
972 if (Ty.isInvalid())
973 return ExprError();
974
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000975 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
976 Ty.get().getAsOpaquePtr(),
977 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +0000978 } else {
979 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
980 Result = ParseExpression();
981
982 // Match the ')'.
983 if (Result.isInvalid())
984 SkipUntil(tok::r_paren);
985 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000986 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +0000987
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000988 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
989 /*isType=*/false,
990 Result.release(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +0000991 }
992 }
993
994 return move(Result);
995}
996
Douglas Gregore610ada2010-02-24 18:44:31 +0000997/// \brief Parse a C++ pseudo-destructor expression after the base,
998/// . or -> operator, and nested-name-specifier have already been
999/// parsed.
1000///
1001/// postfix-expression: [C++ 5.2]
1002/// postfix-expression . pseudo-destructor-name
1003/// postfix-expression -> pseudo-destructor-name
1004///
1005/// pseudo-destructor-name:
1006/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1007/// ::[opt] nested-name-specifier template simple-template-id ::
1008/// ~type-name
1009/// ::[opt] nested-name-specifier[opt] ~type-name
1010///
John McCalldadc5752010-08-24 06:29:42 +00001011ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +00001012Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1013 tok::TokenKind OpKind,
1014 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001015 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001016 // We're parsing either a pseudo-destructor-name or a dependent
1017 // member access that has the same form as a
1018 // pseudo-destructor-name. We parse both in the same way and let
1019 // the action model sort them out.
1020 //
1021 // Note that the ::[opt] nested-name-specifier[opt] has already
1022 // been parsed, and if there was a simple-template-id, it has
1023 // been coalesced into a template-id annotation token.
1024 UnqualifiedId FirstTypeName;
1025 SourceLocation CCLoc;
1026 if (Tok.is(tok::identifier)) {
1027 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1028 ConsumeToken();
1029 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1030 CCLoc = ConsumeToken();
1031 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001032 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1033 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001034 FirstTypeName.setTemplateId(
1035 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1036 ConsumeToken();
1037 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1038 CCLoc = ConsumeToken();
1039 } else {
1040 FirstTypeName.setIdentifier(0, SourceLocation());
1041 }
1042
1043 // Parse the tilde.
1044 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1045 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001046
1047 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1048 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001049 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-12-16 16:03:09 +00001050 if (DS.getTypeSpecType() == TST_error)
1051 return ExprError();
1052 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1053 OpKind, TildeLoc, DS,
1054 Tok.is(tok::l_paren));
1055 }
1056
Douglas Gregore610ada2010-02-24 18:44:31 +00001057 if (!Tok.is(tok::identifier)) {
1058 Diag(Tok, diag::err_destructor_tilde_identifier);
1059 return ExprError();
1060 }
1061
1062 // Parse the second type.
1063 UnqualifiedId SecondTypeName;
1064 IdentifierInfo *Name = Tok.getIdentifierInfo();
1065 SourceLocation NameLoc = ConsumeToken();
1066 SecondTypeName.setIdentifier(Name, NameLoc);
1067
1068 // If there is a '<', the second type name is a template-id. Parse
1069 // it as such.
1070 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001071 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1072 Name, NameLoc,
1073 false, ObjectType, SecondTypeName,
1074 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001075 return ExprError();
1076
John McCallb268a282010-08-23 23:25:46 +00001077 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1078 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001079 SS, FirstTypeName, CCLoc,
1080 TildeLoc, SecondTypeName,
1081 Tok.is(tok::l_paren));
1082}
1083
Bill Wendling4073ed52007-02-13 01:51:42 +00001084/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1085///
1086/// boolean-literal: [C++ 2.13.5]
1087/// 'true'
1088/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001089ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001090 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001091 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001092}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001093
1094/// ParseThrowExpression - This handles the C++ throw expression.
1095///
1096/// throw-expression: [C++ 15]
1097/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001098ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001099 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001100 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001101
Chris Lattner65dd8432008-04-06 06:02:23 +00001102 // If the current token isn't the start of an assignment-expression,
1103 // then the expression is not present. This handles things like:
1104 // "C ? throw : (void)42", which is crazy but legal.
1105 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1106 case tok::semi:
1107 case tok::r_paren:
1108 case tok::r_square:
1109 case tok::r_brace:
1110 case tok::colon:
1111 case tok::comma:
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001112 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001113
Chris Lattner65dd8432008-04-06 06:02:23 +00001114 default:
John McCalldadc5752010-08-24 06:29:42 +00001115 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redld65cea82008-12-11 22:51:44 +00001116 if (Expr.isInvalid()) return move(Expr);
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001117 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001118 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001119}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001120
1121/// ParseCXXThis - This handles the C++ 'this' pointer.
1122///
1123/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1124/// a non-lvalue expression whose value is the address of the object for which
1125/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001126ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001127 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1128 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001129 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001130}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001131
1132/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1133/// Can be interpreted either as function-style casting ("int(x)")
1134/// or class type construction ("ClassType(x,y,z)")
1135/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001136/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001137///
1138/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001139/// simple-type-specifier '(' expression-list[opt] ')'
1140/// [C++0x] simple-type-specifier braced-init-list
1141/// typename-specifier '(' expression-list[opt] ')'
1142/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001143///
John McCalldadc5752010-08-24 06:29:42 +00001144ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001145Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001146 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001147 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001148
Sebastian Redl3da34892011-06-05 12:23:16 +00001149 assert((Tok.is(tok::l_paren) ||
1150 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1151 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001152
Sebastian Redl3da34892011-06-05 12:23:16 +00001153 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001154 ExprResult Init = ParseBraceInitializer();
1155 if (Init.isInvalid())
1156 return Init;
1157 Expr *InitList = Init.take();
1158 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1159 MultiExprArg(&InitList, 1),
1160 SourceLocation());
Sebastian Redl3da34892011-06-05 12:23:16 +00001161 } else {
1162 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1163
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001164 BalancedDelimiterTracker T(*this, tok::l_paren);
1165 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001166
1167 ExprVector Exprs(Actions);
1168 CommaLocsTy CommaLocs;
1169
1170 if (Tok.isNot(tok::r_paren)) {
1171 if (ParseExpressionList(Exprs, CommaLocs)) {
1172 SkipUntil(tok::r_paren);
1173 return ExprError();
1174 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001175 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001176
1177 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001178 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001179
1180 // TypeRep could be null, if it references an invalid typedef.
1181 if (!TypeRep)
1182 return ExprError();
1183
1184 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1185 "Unexpected number of commas!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001186 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
1187 move_arg(Exprs),
1188 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001189 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001190}
1191
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001192/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001193///
1194/// condition:
1195/// expression
1196/// type-specifier-seq declarator '=' assignment-expression
1197/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1198/// '=' assignment-expression
1199///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001200/// \param ExprResult if the condition was parsed as an expression, the
1201/// parsed expression.
1202///
1203/// \param DeclResult if the condition was parsed as a declaration, the
1204/// parsed declaration.
1205///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001206/// \param Loc The location of the start of the statement that requires this
1207/// condition, e.g., the "for" in a for loop.
1208///
1209/// \param ConvertToBoolean Whether the condition expression should be
1210/// converted to a boolean value.
1211///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001212/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001213bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1214 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001215 SourceLocation Loc,
1216 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001217 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001218 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001219 cutOffParsing();
1220 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001221 }
1222
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001223 if (!isCXXConditionDeclaration()) {
Douglas Gregore60e41a2010-05-06 17:25:47 +00001224 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001225 ExprOut = ParseExpression(); // expression
1226 DeclOut = 0;
1227 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001228 return true;
1229
1230 // If required, convert to a boolean value.
1231 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001232 ExprOut
1233 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1234 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001235 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001236
1237 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001238 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001239 ParseSpecifierQualifierList(DS);
1240
1241 // declarator
1242 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1243 ParseDeclarator(DeclaratorInfo);
1244
1245 // simple-asm-expr[opt]
1246 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001247 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001248 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001249 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001250 SkipUntil(tok::semi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001251 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001252 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001253 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001254 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001255 }
1256
1257 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001258 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001259
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001260 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001261 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001262 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001263 DeclOut = Dcl.get();
1264 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001265
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001266 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001267 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001268 if (isTokenEqualOrEqualTypo()) {
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001269 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001270 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001271 if (!AssignExpr.isInvalid())
Richard Smith30482bc2011-02-20 03:19:35 +00001272 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1273 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001274 } else {
1275 // FIXME: C++0x allows a braced-init-list
1276 Diag(Tok, diag::err_expected_equal_after_declarator);
1277 }
1278
Douglas Gregore60e41a2010-05-06 17:25:47 +00001279 // FIXME: Build a reference to this declaration? Convert it to bool?
1280 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001281
1282 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001283
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001284 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001285}
1286
Douglas Gregor8d4de672010-04-21 22:36:40 +00001287/// \brief Determine whether the current token starts a C++
1288/// simple-type-specifier.
1289bool Parser::isCXXSimpleTypeSpecifier() const {
1290 switch (Tok.getKind()) {
1291 case tok::annot_typename:
1292 case tok::kw_short:
1293 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00001294 case tok::kw___int64:
Douglas Gregor8d4de672010-04-21 22:36:40 +00001295 case tok::kw_signed:
1296 case tok::kw_unsigned:
1297 case tok::kw_void:
1298 case tok::kw_char:
1299 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001300 case tok::kw_half:
Douglas Gregor8d4de672010-04-21 22:36:40 +00001301 case tok::kw_float:
1302 case tok::kw_double:
1303 case tok::kw_wchar_t:
1304 case tok::kw_char16_t:
1305 case tok::kw_char32_t:
1306 case tok::kw_bool:
Douglas Gregor19b7acf2011-04-27 05:41:15 +00001307 case tok::kw_decltype:
Douglas Gregor8d4de672010-04-21 22:36:40 +00001308 case tok::kw_typeof:
Alexis Hunt4a257072011-05-19 05:37:45 +00001309 case tok::kw___underlying_type:
Douglas Gregor8d4de672010-04-21 22:36:40 +00001310 return true;
1311
1312 default:
1313 break;
1314 }
1315
1316 return false;
1317}
1318
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001319/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1320/// This should only be called when the current token is known to be part of
1321/// simple-type-specifier.
1322///
1323/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001324/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001325/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1326/// char
1327/// wchar_t
1328/// bool
1329/// short
1330/// int
1331/// long
1332/// signed
1333/// unsigned
1334/// float
1335/// double
1336/// void
1337/// [GNU] typeof-specifier
1338/// [C++0x] auto [TODO]
1339///
1340/// type-name:
1341/// class-name
1342/// enum-name
1343/// typedef-name
1344///
1345void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1346 DS.SetRangeStart(Tok.getLocation());
1347 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001348 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001349 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001350
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001351 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001352 case tok::identifier: // foo::bar
1353 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001354 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001355 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001356 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001357
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001358 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001359 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001360 if (getTypeAnnotation(Tok))
1361 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1362 getTypeAnnotation(Tok));
1363 else
1364 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001365
1366 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1367 ConsumeToken();
1368
1369 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1370 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1371 // Objective-C interface. If we don't have Objective-C or a '<', this is
1372 // just a normal reference to a typedef name.
1373 if (Tok.is(tok::less) && getLang().ObjC1)
1374 ParseObjCProtocolQualifiers(DS);
1375
1376 DS.Finish(Diags, PP);
1377 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001378 }
Mike Stump11289f42009-09-09 15:08:12 +00001379
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001380 // builtin types
1381 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001382 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001383 break;
1384 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +00001385 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001386 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001387 case tok::kw___int64:
1388 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1389 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001390 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001391 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001392 break;
1393 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001394 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001395 break;
1396 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001397 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001398 break;
1399 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001400 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001401 break;
1402 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001403 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001404 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001405 case tok::kw_half:
1406 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1407 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001408 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001409 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001410 break;
1411 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001412 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001413 break;
1414 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001415 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001416 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001417 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001418 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001419 break;
1420 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001421 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001422 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001423 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +00001424 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001425 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001426 case tok::annot_decltype:
1427 case tok::kw_decltype:
1428 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1429 return DS.Finish(Diags, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001430
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001431 // GNU typeof support.
1432 case tok::kw_typeof:
1433 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +00001434 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001435 return;
1436 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001437 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001438 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1439 else
1440 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001441 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +00001442 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001443}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001444
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001445/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1446/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1447/// e.g., "const short int". Note that the DeclSpec is *not* finished
1448/// by parsing the type-specifier-seq, because these sequences are
1449/// typically followed by some form of declarator. Returns true and
1450/// emits diagnostics if this is not a type-specifier-seq, false
1451/// otherwise.
1452///
1453/// type-specifier-seq: [C++ 8.1]
1454/// type-specifier type-specifier-seq[opt]
1455///
1456bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1457 DS.SetRangeStart(Tok.getLocation());
1458 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001459 unsigned DiagID;
1460 bool isInvalid = 0;
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001461
1462 // Parse one or more of the type specifiers.
Sebastian Redl2b372722010-02-03 21:21:43 +00001463 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1464 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky07e97c52010-11-03 17:52:57 +00001465 Diag(Tok, diag::err_expected_type);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001466 return true;
1467 }
Mike Stump11289f42009-09-09 15:08:12 +00001468
Sebastian Redl2b372722010-02-03 21:21:43 +00001469 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1470 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1471 {}
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001472
Douglas Gregor40d732f2010-02-24 23:13:13 +00001473 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001474 return false;
1475}
1476
Douglas Gregor7861a802009-11-03 01:35:08 +00001477/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1478/// some form.
1479///
1480/// This routine is invoked when a '<' is encountered after an identifier or
1481/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1482/// whether the unqualified-id is actually a template-id. This routine will
1483/// then parse the template arguments and form the appropriate template-id to
1484/// return to the caller.
1485///
1486/// \param SS the nested-name-specifier that precedes this template-id, if
1487/// we're actually parsing a qualified-id.
1488///
1489/// \param Name for constructor and destructor names, this is the actual
1490/// identifier that may be a template-name.
1491///
1492/// \param NameLoc the location of the class-name in a constructor or
1493/// destructor.
1494///
1495/// \param EnteringContext whether we're entering the scope of the
1496/// nested-name-specifier.
1497///
Douglas Gregor127ea592009-11-03 21:24:04 +00001498/// \param ObjectType if this unqualified-id occurs within a member access
1499/// expression, the type of the base object whose member is being accessed.
1500///
Douglas Gregor7861a802009-11-03 01:35:08 +00001501/// \param Id as input, describes the template-name or operator-function-id
1502/// that precedes the '<'. If template arguments were parsed successfully,
1503/// will be updated with the template-id.
1504///
Douglas Gregore610ada2010-02-24 18:44:31 +00001505/// \param AssumeTemplateId When true, this routine will assume that the name
1506/// refers to a template without performing name lookup to verify.
1507///
Douglas Gregor7861a802009-11-03 01:35:08 +00001508/// \returns true if a parse error occurred, false otherwise.
1509bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001510 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001511 IdentifierInfo *Name,
1512 SourceLocation NameLoc,
1513 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001514 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001515 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001516 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001517 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1518 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001519
1520 TemplateTy Template;
1521 TemplateNameKind TNK = TNK_Non_template;
1522 switch (Id.getKind()) {
1523 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001524 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001525 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001526 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001527 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001528 Id, ObjectType, EnteringContext,
1529 Template);
1530 if (TNK == TNK_Non_template)
1531 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001532 } else {
1533 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001534 TNK = Actions.isTemplateName(getCurScope(), SS,
1535 TemplateKWLoc.isValid(), Id,
1536 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001537 MemberOfUnknownSpecialization);
1538
1539 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1540 ObjectType && IsTemplateArgumentList()) {
1541 // We have something like t->getAs<T>(), where getAs is a
1542 // member of an unknown specialization. However, this will only
1543 // parse correctly as a template, so suggest the keyword 'template'
1544 // before 'getAs' and treat this as a dependent template name.
1545 std::string Name;
1546 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1547 Name = Id.Identifier->getName();
1548 else {
1549 Name = "operator ";
1550 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1551 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1552 else
1553 Name += Id.Identifier->getName();
1554 }
1555 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1556 << Name
1557 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00001558 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1559 SS, TemplateKWLoc, Id,
1560 ObjectType, EnteringContext,
1561 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001562 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001563 return true;
1564 }
1565 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001566 break;
1567
Douglas Gregor3cf81312009-11-03 23:16:33 +00001568 case UnqualifiedId::IK_ConstructorName: {
1569 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001570 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001571 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001572 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1573 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001574 EnteringContext, Template,
1575 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001576 break;
1577 }
1578
Douglas Gregor3cf81312009-11-03 23:16:33 +00001579 case UnqualifiedId::IK_DestructorName: {
1580 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001581 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001582 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001583 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001584 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1585 SS, TemplateKWLoc, TemplateName,
1586 ObjectType, EnteringContext,
1587 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001588 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001589 return true;
1590 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001591 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1592 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001593 EnteringContext, Template,
1594 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001595
John McCallba7bf592010-08-24 05:47:05 +00001596 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001597 Diag(NameLoc, diag::err_destructor_template_id)
1598 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001599 return true;
1600 }
1601 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001602 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001603 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001604
1605 default:
1606 return false;
1607 }
1608
1609 if (TNK == TNK_Non_template)
1610 return false;
1611
1612 // Parse the enclosed template argument list.
1613 SourceLocation LAngleLoc, RAngleLoc;
1614 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001615 if (Tok.is(tok::less) &&
1616 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001617 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001618 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001619 RAngleLoc))
1620 return true;
1621
1622 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001623 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1624 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001625 // Form a parsed representation of the template-id to be stored in the
1626 // UnqualifiedId.
1627 TemplateIdAnnotation *TemplateId
1628 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1629
1630 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1631 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001632 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001633 TemplateId->TemplateNameLoc = Id.StartLocation;
1634 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001635 TemplateId->Name = 0;
1636 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1637 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001638 }
1639
Douglas Gregore7c20652011-03-02 00:47:37 +00001640 TemplateId->SS = SS;
John McCall3e56fd42010-08-23 07:28:44 +00001641 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001642 TemplateId->Kind = TNK;
1643 TemplateId->LAngleLoc = LAngleLoc;
1644 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001645 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001646 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001647 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001648 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001649
1650 Id.setTemplateId(TemplateId);
1651 return false;
1652 }
1653
1654 // Bundle the template arguments together.
1655 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor7861a802009-11-03 01:35:08 +00001656 TemplateArgs.size());
Abramo Bagnara4244b432012-01-27 08:46:19 +00001657
Douglas Gregor7861a802009-11-03 01:35:08 +00001658 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001659 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001660 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1661 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00001662 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1663 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00001664 if (Type.isInvalid())
1665 return true;
1666
1667 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1668 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1669 else
1670 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1671
1672 return false;
1673}
1674
Douglas Gregor71395fa2009-11-04 00:56:37 +00001675/// \brief Parse an operator-function-id or conversion-function-id as part
1676/// of a C++ unqualified-id.
1677///
1678/// This routine is responsible only for parsing the operator-function-id or
1679/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001680///
1681/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001682/// operator-function-id: [C++ 13.5]
1683/// 'operator' operator
1684///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001685/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001686/// new delete new[] delete[]
1687/// + - * / % ^ & | ~
1688/// ! = < > += -= *= /= %=
1689/// ^= &= |= << >> >>= <<= == !=
1690/// <= >= && || ++ -- , ->* ->
1691/// () []
1692///
1693/// conversion-function-id: [C++ 12.3.2]
1694/// operator conversion-type-id
1695///
1696/// conversion-type-id:
1697/// type-specifier-seq conversion-declarator[opt]
1698///
1699/// conversion-declarator:
1700/// ptr-operator conversion-declarator[opt]
1701/// \endcode
1702///
1703/// \param The nested-name-specifier that preceded this unqualified-id. If
1704/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1705///
1706/// \param EnteringContext whether we are entering the scope of the
1707/// nested-name-specifier.
1708///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001709/// \param ObjectType if this unqualified-id occurs within a member access
1710/// expression, the type of the base object whose member is being accessed.
1711///
1712/// \param Result on a successful parse, contains the parsed unqualified-id.
1713///
1714/// \returns true if parsing fails, false otherwise.
1715bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001716 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001717 UnqualifiedId &Result) {
1718 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1719
1720 // Consume the 'operator' keyword.
1721 SourceLocation KeywordLoc = ConsumeToken();
1722
1723 // Determine what kind of operator name we have.
1724 unsigned SymbolIdx = 0;
1725 SourceLocation SymbolLocations[3];
1726 OverloadedOperatorKind Op = OO_None;
1727 switch (Tok.getKind()) {
1728 case tok::kw_new:
1729 case tok::kw_delete: {
1730 bool isNew = Tok.getKind() == tok::kw_new;
1731 // Consume the 'new' or 'delete'.
1732 SymbolLocations[SymbolIdx++] = ConsumeToken();
1733 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001734 // Consume the '[' and ']'.
1735 BalancedDelimiterTracker T(*this, tok::l_square);
1736 T.consumeOpen();
1737 T.consumeClose();
1738 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001739 return true;
1740
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001741 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1742 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001743 Op = isNew? OO_Array_New : OO_Array_Delete;
1744 } else {
1745 Op = isNew? OO_New : OO_Delete;
1746 }
1747 break;
1748 }
1749
1750#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1751 case tok::Token: \
1752 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1753 Op = OO_##Name; \
1754 break;
1755#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1756#include "clang/Basic/OperatorKinds.def"
1757
1758 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001759 // Consume the '(' and ')'.
1760 BalancedDelimiterTracker T(*this, tok::l_paren);
1761 T.consumeOpen();
1762 T.consumeClose();
1763 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001764 return true;
1765
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001766 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1767 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001768 Op = OO_Call;
1769 break;
1770 }
1771
1772 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001773 // Consume the '[' and ']'.
1774 BalancedDelimiterTracker T(*this, tok::l_square);
1775 T.consumeOpen();
1776 T.consumeClose();
1777 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001778 return true;
1779
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001780 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1781 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001782 Op = OO_Subscript;
1783 break;
1784 }
1785
1786 case tok::code_completion: {
1787 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001788 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001789 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001790 // Don't try to parse any further.
1791 return true;
1792 }
1793
1794 default:
1795 break;
1796 }
1797
1798 if (Op != OO_None) {
1799 // We have parsed an operator-function-id.
1800 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1801 return false;
1802 }
Alexis Hunt34458502009-11-28 04:44:28 +00001803
1804 // Parse a literal-operator-id.
1805 //
1806 // literal-operator-id: [C++0x 13.5.8]
1807 // operator "" identifier
1808
1809 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001810 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00001811 if (Tok.getLength() != 2)
1812 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1813 ConsumeStringToken();
1814
1815 if (Tok.isNot(tok::identifier)) {
1816 Diag(Tok.getLocation(), diag::err_expected_ident);
1817 return true;
1818 }
1819
1820 IdentifierInfo *II = Tok.getIdentifierInfo();
1821 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Alexis Hunt3d221f22009-11-29 07:34:05 +00001822 return false;
Alexis Hunt34458502009-11-28 04:44:28 +00001823 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00001824
1825 // Parse a conversion-function-id.
1826 //
1827 // conversion-function-id: [C++ 12.3.2]
1828 // operator conversion-type-id
1829 //
1830 // conversion-type-id:
1831 // type-specifier-seq conversion-declarator[opt]
1832 //
1833 // conversion-declarator:
1834 // ptr-operator conversion-declarator[opt]
1835
1836 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00001837 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00001838 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00001839 return true;
1840
1841 // Parse the conversion-declarator, which is merely a sequence of
1842 // ptr-operators.
1843 Declarator D(DS, Declarator::TypeNameContext);
1844 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1845
1846 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00001847 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00001848 if (Ty.isInvalid())
1849 return true;
1850
1851 // Note that this is a conversion-function-id.
1852 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1853 D.getSourceRange().getEnd());
1854 return false;
1855}
1856
1857/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1858/// name of an entity.
1859///
1860/// \code
1861/// unqualified-id: [C++ expr.prim.general]
1862/// identifier
1863/// operator-function-id
1864/// conversion-function-id
1865/// [C++0x] literal-operator-id [TODO]
1866/// ~ class-name
1867/// template-id
1868///
1869/// \endcode
1870///
1871/// \param The nested-name-specifier that preceded this unqualified-id. If
1872/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1873///
1874/// \param EnteringContext whether we are entering the scope of the
1875/// nested-name-specifier.
1876///
Douglas Gregor7861a802009-11-03 01:35:08 +00001877/// \param AllowDestructorName whether we allow parsing of a destructor name.
1878///
1879/// \param AllowConstructorName whether we allow parsing a constructor name.
1880///
Douglas Gregor127ea592009-11-03 21:24:04 +00001881/// \param ObjectType if this unqualified-id occurs within a member access
1882/// expression, the type of the base object whose member is being accessed.
1883///
Douglas Gregor7861a802009-11-03 01:35:08 +00001884/// \param Result on a successful parse, contains the parsed unqualified-id.
1885///
1886/// \returns true if parsing fails, false otherwise.
1887bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1888 bool AllowDestructorName,
1889 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00001890 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001891 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001892 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001893
1894 // Handle 'A::template B'. This is for template-ids which have not
1895 // already been annotated by ParseOptionalCXXScopeSpecifier().
1896 bool TemplateSpecified = false;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001897 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1898 (ObjectType || SS.isSet())) {
1899 TemplateSpecified = true;
1900 TemplateKWLoc = ConsumeToken();
1901 }
1902
Douglas Gregor7861a802009-11-03 01:35:08 +00001903 // unqualified-id:
1904 // identifier
1905 // template-id (when it hasn't already been annotated)
1906 if (Tok.is(tok::identifier)) {
1907 // Consume the identifier.
1908 IdentifierInfo *Id = Tok.getIdentifierInfo();
1909 SourceLocation IdLoc = ConsumeToken();
1910
Douglas Gregor411e5ac2010-01-11 23:29:10 +00001911 if (!getLang().CPlusPlus) {
1912 // If we're not in C++, only identifiers matter. Record the
1913 // identifier and return.
1914 Result.setIdentifier(Id, IdLoc);
1915 return false;
1916 }
1917
Douglas Gregor7861a802009-11-03 01:35:08 +00001918 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001919 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001920 // We have parsed a constructor name.
Abramo Bagnara4244b432012-01-27 08:46:19 +00001921 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
1922 &SS, false, false,
1923 ParsedType(),
1924 /*IsCtorOrDtorName=*/true,
1925 /*NonTrivialTypeSourceInfo=*/true);
1926 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00001927 } else {
1928 // We have parsed an identifier.
1929 Result.setIdentifier(Id, IdLoc);
1930 }
1931
1932 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00001933 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00001934 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
1935 EnteringContext, ObjectType,
1936 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00001937
1938 return false;
1939 }
1940
1941 // unqualified-id:
1942 // template-id (already parsed and annotated)
1943 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001944 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001945
1946 // If the template-name names the current class, then this is a constructor
1947 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001948 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001949 if (SS.isSet()) {
1950 // C++ [class.qual]p2 specifies that a qualified template-name
1951 // is taken as the constructor name where a constructor can be
1952 // declared. Thus, the template arguments are extraneous, so
1953 // complain about them and remove them entirely.
1954 Diag(TemplateId->TemplateNameLoc,
1955 diag::err_out_of_line_constructor_template_id)
1956 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00001957 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001958 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnara4244b432012-01-27 08:46:19 +00001959 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
1960 TemplateId->TemplateNameLoc,
1961 getCurScope(),
1962 &SS, false, false,
1963 ParsedType(),
1964 /*IsCtorOrDtorName=*/true,
1965 /*NontrivialTypeSourceInfo=*/true);
1966 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001967 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001968 ConsumeToken();
1969 return false;
1970 }
1971
1972 Result.setConstructorTemplateId(TemplateId);
1973 ConsumeToken();
1974 return false;
1975 }
1976
Douglas Gregor7861a802009-11-03 01:35:08 +00001977 // We have already parsed a template-id; consume the annotation token as
1978 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001979 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001980 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00001981 ConsumeToken();
1982 return false;
1983 }
1984
1985 // unqualified-id:
1986 // operator-function-id
1987 // conversion-function-id
1988 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00001989 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00001990 return true;
1991
Alexis Hunted0530f2009-11-28 08:58:14 +00001992 // If we have an operator-function-id or a literal-operator-id and the next
1993 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00001994 //
1995 // template-id:
1996 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00001997 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1998 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00001999 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002000 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2001 0, SourceLocation(),
2002 EnteringContext, ObjectType,
2003 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002004
Douglas Gregor7861a802009-11-03 01:35:08 +00002005 return false;
2006 }
2007
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002008 if (getLang().CPlusPlus &&
2009 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002010 // C++ [expr.unary.op]p10:
2011 // There is an ambiguity in the unary-expression ~X(), where X is a
2012 // class-name. The ambiguity is resolved in favor of treating ~ as a
2013 // unary complement rather than treating ~X as referring to a destructor.
2014
2015 // Parse the '~'.
2016 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002017
2018 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2019 DeclSpec DS(AttrFactory);
2020 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2021 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2022 Result.setDestructorName(TildeLoc, Type, EndLoc);
2023 return false;
2024 }
2025 return true;
2026 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002027
2028 // Parse the class-name.
2029 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002030 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002031 return true;
2032 }
2033
2034 // Parse the class-name (or template-name in a simple-template-id).
2035 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2036 SourceLocation ClassNameLoc = ConsumeToken();
2037
Douglas Gregorb22ee882010-05-05 05:58:24 +00002038 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00002039 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002040 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2041 ClassName, ClassNameLoc,
2042 EnteringContext, ObjectType,
2043 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002044 }
2045
Douglas Gregor7861a802009-11-03 01:35:08 +00002046 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002047 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2048 ClassNameLoc, getCurScope(),
2049 SS, ObjectType,
2050 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002051 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002052 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002053
Douglas Gregor7861a802009-11-03 01:35:08 +00002054 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002055 return false;
2056 }
2057
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002058 Diag(Tok, diag::err_expected_unqualified_id)
2059 << getLang().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002060 return true;
2061}
2062
Sebastian Redlbd150f42008-11-21 19:14:01 +00002063/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2064/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002065///
Chris Lattner109faf22009-01-04 21:25:24 +00002066/// This method is called to parse the new expression after the optional :: has
2067/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2068/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002069///
2070/// new-expression:
2071/// '::'[opt] 'new' new-placement[opt] new-type-id
2072/// new-initializer[opt]
2073/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2074/// new-initializer[opt]
2075///
2076/// new-placement:
2077/// '(' expression-list ')'
2078///
Sebastian Redl351bb782008-12-02 14:43:59 +00002079/// new-type-id:
2080/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002081/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002082///
2083/// new-declarator:
2084/// ptr-operator new-declarator[opt]
2085/// direct-new-declarator
2086///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002087/// new-initializer:
2088/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002089/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002090///
John McCalldadc5752010-08-24 06:29:42 +00002091ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002092Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2093 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2094 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002095
2096 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2097 // second form of new-expression. It can't be a new-type-id.
2098
Sebastian Redl511ed552008-11-25 22:21:31 +00002099 ExprVector PlacementArgs(Actions);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002100 SourceLocation PlacementLParen, PlacementRParen;
2101
Douglas Gregorf2753b32010-07-13 15:54:32 +00002102 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002103 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002104 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002105 if (Tok.is(tok::l_paren)) {
2106 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002107 BalancedDelimiterTracker T(*this, tok::l_paren);
2108 T.consumeOpen();
2109 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002110 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2111 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002112 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002113 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002114
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002115 T.consumeClose();
2116 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002117 if (PlacementRParen.isInvalid()) {
2118 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002119 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002120 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002121
Sebastian Redl351bb782008-12-02 14:43:59 +00002122 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002123 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002124 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002125 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002126 } else {
2127 // We still need the type.
2128 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002129 BalancedDelimiterTracker T(*this, tok::l_paren);
2130 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002131 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002132 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002133 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002134 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002135 T.consumeClose();
2136 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002137 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002138 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002139 if (ParseCXXTypeSpecifierSeq(DS))
2140 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002141 else {
2142 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002143 ParseDeclaratorInternal(DeclaratorInfo,
2144 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002145 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002146 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002147 }
2148 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002149 // A new-type-id is a simplified type-id, where essentially the
2150 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002151 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002152 if (ParseCXXTypeSpecifierSeq(DS))
2153 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002154 else {
2155 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002156 ParseDeclaratorInternal(DeclaratorInfo,
2157 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002158 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002159 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002160 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002161 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002162 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002163 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002164
Sebastian Redl511ed552008-11-25 22:21:31 +00002165 ExprVector ConstructorArgs(Actions);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002166 SourceLocation ConstructorLParen, ConstructorRParen;
2167
2168 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002169 BalancedDelimiterTracker T(*this, tok::l_paren);
2170 T.consumeOpen();
2171 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002172 if (Tok.isNot(tok::r_paren)) {
2173 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002174 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2175 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002176 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002177 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002178 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002179 T.consumeClose();
2180 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002181 if (ConstructorRParen.isInvalid()) {
2182 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002183 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002184 }
Richard Smith2db96522011-10-15 03:38:41 +00002185 } else if (Tok.is(tok::l_brace) && getLang().CPlusPlus0x) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002186 Diag(Tok.getLocation(),
2187 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl82ace982012-02-11 23:51:08 +00002188 ExprResult InitList = ParseBraceInitializer();
2189 if (InitList.isInvalid())
2190 return InitList;
2191 ConstructorArgs.push_back(InitList.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002192 }
2193
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002194 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2195 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregorf2753b32010-07-13 15:54:32 +00002196 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002197 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002198}
2199
Sebastian Redlbd150f42008-11-21 19:14:01 +00002200/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2201/// passed to ParseDeclaratorInternal.
2202///
2203/// direct-new-declarator:
2204/// '[' expression ']'
2205/// direct-new-declarator '[' constant-expression ']'
2206///
Chris Lattner109faf22009-01-04 21:25:24 +00002207void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002208 // Parse the array dimensions.
2209 bool first = true;
2210 while (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002211 BalancedDelimiterTracker T(*this, tok::l_square);
2212 T.consumeOpen();
2213
John McCalldadc5752010-08-24 06:29:42 +00002214 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002215 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002216 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002217 // Recover
2218 SkipUntil(tok::r_square);
2219 return;
2220 }
2221 first = false;
2222
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002223 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002224
2225 ParsedAttributes attrs(AttrFactory);
2226 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002227 /*static=*/false, /*star=*/false,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002228 Size.release(),
2229 T.getOpenLocation(),
2230 T.getCloseLocation()),
2231 attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002232
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002233 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002234 return;
2235 }
2236}
2237
2238/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2239/// This ambiguity appears in the syntax of the C++ new operator.
2240///
2241/// new-expression:
2242/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2243/// new-initializer[opt]
2244///
2245/// new-placement:
2246/// '(' expression-list ')'
2247///
John McCall37ad5512010-08-23 06:44:23 +00002248bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002249 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002250 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002251 // The '(' was already consumed.
2252 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002253 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002254 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002255 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002256 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002257 }
2258
2259 // It's not a type, it has to be an expression list.
2260 // Discard the comma locations - ActOnCXXNew has enough parameters.
2261 CommaLocsTy CommaLocs;
2262 return ParseExpressionList(PlacementArgs, CommaLocs);
2263}
2264
2265/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2266/// to free memory allocated by new.
2267///
Chris Lattner109faf22009-01-04 21:25:24 +00002268/// This method is called to parse the 'delete' expression after the optional
2269/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2270/// and "Start" is its location. Otherwise, "Start" is the location of the
2271/// 'delete' token.
2272///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002273/// delete-expression:
2274/// '::'[opt] 'delete' cast-expression
2275/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002276ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002277Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2278 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2279 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002280
2281 // Array delete?
2282 bool ArrayDelete = false;
2283 if (Tok.is(tok::l_square)) {
2284 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002285 BalancedDelimiterTracker T(*this, tok::l_square);
2286
2287 T.consumeOpen();
2288 T.consumeClose();
2289 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002290 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002291 }
2292
John McCalldadc5752010-08-24 06:29:42 +00002293 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002294 if (Operand.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002295 return move(Operand);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002296
John McCallb268a282010-08-23 23:25:46 +00002297 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002298}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002299
Mike Stump11289f42009-09-09 15:08:12 +00002300static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002301 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002302 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002303 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002304 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002305 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002306 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Alexis Huntf479f1b2011-05-09 18:22:59 +00002307 case tok::kw___has_trivial_constructor:
2308 return UTT_HasTrivialDefaultConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002309 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002310 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2311 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2312 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley65497cc2011-04-27 23:09:49 +00002313 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2314 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002315 case tok::kw___is_class: return UTT_IsClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002316 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2317 case tok::kw___is_compound: return UTT_IsCompound;
2318 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002319 case tok::kw___is_empty: return UTT_IsEmpty;
2320 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregordca70af2011-12-03 18:14:24 +00002321 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley65497cc2011-04-27 23:09:49 +00002322 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2323 case tok::kw___is_function: return UTT_IsFunction;
2324 case tok::kw___is_fundamental: return UTT_IsFundamental;
2325 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley65497cc2011-04-27 23:09:49 +00002326 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2327 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2328 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2329 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2330 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth79803482011-04-23 10:47:20 +00002331 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00002332 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002333 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley65497cc2011-04-27 23:09:49 +00002334 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002335 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley65497cc2011-04-27 23:09:49 +00002336 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley65497cc2011-04-27 23:09:49 +00002337 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2338 case tok::kw___is_scalar: return UTT_IsScalar;
2339 case tok::kw___is_signed: return UTT_IsSigned;
2340 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2341 case tok::kw___is_trivial: return UTT_IsTrivial;
Alexis Huntd9a5cc12011-05-13 00:31:07 +00002342 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002343 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley65497cc2011-04-27 23:09:49 +00002344 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2345 case tok::kw___is_void: return UTT_IsVoid;
2346 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002347 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002348}
2349
2350static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2351 switch(kind) {
Francois Pichet347c4c72010-12-07 00:55:57 +00002352 default: llvm_unreachable("Not a known binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00002353 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley65497cc2011-04-27 23:09:49 +00002354 case tok::kw___is_convertible: return BTT_IsConvertible;
2355 case tok::kw___is_same: return BTT_IsSame;
Francois Pichet34b21132010-12-08 22:35:30 +00002356 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor8006e762011-01-27 20:28:01 +00002357 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002358 }
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002359}
2360
John Wiegley6242b6a2011-04-28 00:16:57 +00002361static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2362 switch(kind) {
2363 default: llvm_unreachable("Not a known binary type trait");
2364 case tok::kw___array_rank: return ATT_ArrayRank;
2365 case tok::kw___array_extent: return ATT_ArrayExtent;
2366 }
2367}
2368
John Wiegleyf9f65842011-04-25 06:54:41 +00002369static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2370 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002371 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002372 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2373 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2374 }
2375}
2376
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002377/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2378/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2379/// templates.
2380///
2381/// primary-expression:
2382/// [GNU] unary-type-trait '(' type-id ')'
2383///
John McCalldadc5752010-08-24 06:29:42 +00002384ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002385 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2386 SourceLocation Loc = ConsumeToken();
2387
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002388 BalancedDelimiterTracker T(*this, tok::l_paren);
2389 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002390 return ExprError();
2391
2392 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2393 // there will be cryptic errors about mismatched parentheses and missing
2394 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00002395 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002396
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002397 T.consumeClose();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002398
Douglas Gregor220cac52009-02-18 17:45:20 +00002399 if (Ty.isInvalid())
2400 return ExprError();
2401
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002402 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002403}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002404
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002405/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2406/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2407/// templates.
2408///
2409/// primary-expression:
2410/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2411///
2412ExprResult Parser::ParseBinaryTypeTrait() {
2413 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2414 SourceLocation Loc = ConsumeToken();
2415
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002416 BalancedDelimiterTracker T(*this, tok::l_paren);
2417 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002418 return ExprError();
2419
2420 TypeResult LhsTy = ParseTypeName();
2421 if (LhsTy.isInvalid()) {
2422 SkipUntil(tok::r_paren);
2423 return ExprError();
2424 }
2425
2426 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2427 SkipUntil(tok::r_paren);
2428 return ExprError();
2429 }
2430
2431 TypeResult RhsTy = ParseTypeName();
2432 if (RhsTy.isInvalid()) {
2433 SkipUntil(tok::r_paren);
2434 return ExprError();
2435 }
2436
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002437 T.consumeClose();
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002438
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002439 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2440 T.getCloseLocation());
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002441}
2442
John Wiegley6242b6a2011-04-28 00:16:57 +00002443/// ParseArrayTypeTrait - Parse the built-in array type-trait
2444/// pseudo-functions.
2445///
2446/// primary-expression:
2447/// [Embarcadero] '__array_rank' '(' type-id ')'
2448/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2449///
2450ExprResult Parser::ParseArrayTypeTrait() {
2451 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2452 SourceLocation Loc = ConsumeToken();
2453
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002454 BalancedDelimiterTracker T(*this, tok::l_paren);
2455 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley6242b6a2011-04-28 00:16:57 +00002456 return ExprError();
2457
2458 TypeResult Ty = ParseTypeName();
2459 if (Ty.isInvalid()) {
2460 SkipUntil(tok::comma);
2461 SkipUntil(tok::r_paren);
2462 return ExprError();
2463 }
2464
2465 switch (ATT) {
2466 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002467 T.consumeClose();
2468 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2469 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002470 }
2471 case ATT_ArrayExtent: {
2472 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2473 SkipUntil(tok::r_paren);
2474 return ExprError();
2475 }
2476
2477 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002478 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00002479
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002480 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2481 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002482 }
John Wiegley6242b6a2011-04-28 00:16:57 +00002483 }
David Blaikiee4d798f2012-01-20 21:50:17 +00002484 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00002485}
2486
John Wiegleyf9f65842011-04-25 06:54:41 +00002487/// ParseExpressionTrait - Parse built-in expression-trait
2488/// pseudo-functions like __is_lvalue_expr( xxx ).
2489///
2490/// primary-expression:
2491/// [Embarcadero] expression-trait '(' expression ')'
2492///
2493ExprResult Parser::ParseExpressionTrait() {
2494 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2495 SourceLocation Loc = ConsumeToken();
2496
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002497 BalancedDelimiterTracker T(*this, tok::l_paren);
2498 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegleyf9f65842011-04-25 06:54:41 +00002499 return ExprError();
2500
2501 ExprResult Expr = ParseExpression();
2502
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002503 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00002504
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002505 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2506 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00002507}
2508
2509
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002510/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2511/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2512/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002513ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002514Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002515 ParsedType &CastTy,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002516 BalancedDelimiterTracker &Tracker) {
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002517 assert(getLang().CPlusPlus && "Should only be called for C++!");
2518 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2519 assert(isTypeIdInParens() && "Not a type-id!");
2520
John McCalldadc5752010-08-24 06:29:42 +00002521 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002522 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002523
2524 // We need to disambiguate a very ugly part of the C++ syntax:
2525 //
2526 // (T())x; - type-id
2527 // (T())*x; - type-id
2528 // (T())/x; - expression
2529 // (T()); - expression
2530 //
2531 // The bad news is that we cannot use the specialized tentative parser, since
2532 // it can only verify that the thing inside the parens can be parsed as
2533 // type-id, it is not useful for determining the context past the parens.
2534 //
2535 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002536 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002537 //
2538 // It uses a scheme similar to parsing inline methods. The parenthesized
2539 // tokens are cached, the context that follows is determined (possibly by
2540 // parsing a cast-expression), and then we re-introduce the cached tokens
2541 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002542
Mike Stump11289f42009-09-09 15:08:12 +00002543 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002544 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002545
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002546 // Store the tokens of the parentheses. We will parse them after we determine
2547 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002548 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002549 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002550 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002551 return ExprError();
2552 }
2553
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002554 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002555 ParseAs = CompoundLiteral;
2556 } else {
2557 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002558 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2559 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2560 NotCastExpr = true;
2561 } else {
2562 // Try parsing the cast-expression that may follow.
2563 // If it is not a cast-expression, NotCastExpr will be true and no token
2564 // will be consumed.
2565 Result = ParseCastExpression(false/*isUnaryExpression*/,
2566 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002567 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002568 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002569 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002570 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002571
2572 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2573 // an expression.
2574 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002575 }
2576
Mike Stump11289f42009-09-09 15:08:12 +00002577 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002578 Toks.push_back(Tok);
2579 // Re-enter the stored parenthesized tokens into the token stream, so we may
2580 // parse them now.
2581 PP.EnterTokenStream(Toks.data(), Toks.size(),
2582 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2583 // Drop the current token and bring the first cached one. It's the same token
2584 // as when we entered this function.
2585 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002586
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002587 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002588 // Parse the type declarator.
2589 DeclSpec DS(AttrFactory);
2590 ParseSpecifierQualifierList(DS);
2591 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2592 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002593
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002594 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002595 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002596
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002597 if (ParseAs == CompoundLiteral) {
2598 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002599 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002600 return ParseCompoundLiteralExpression(Ty.get(),
2601 Tracker.getOpenLocation(),
2602 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002603 }
Mike Stump11289f42009-09-09 15:08:12 +00002604
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002605 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2606 assert(ParseAs == CastExpr);
2607
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002608 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002609 return ExprError();
2610
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002611 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002612 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002613 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2614 DeclaratorInfo, CastTy,
2615 Tracker.getCloseLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002616 return move(Result);
2617 }
Mike Stump11289f42009-09-09 15:08:12 +00002618
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002619 // Not a compound literal, and not followed by a cast-expression.
2620 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002621
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002622 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002623 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002624 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002625 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2626 Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002627
2628 // Match the ')'.
2629 if (Result.isInvalid()) {
2630 SkipUntil(tok::r_paren);
2631 return ExprError();
2632 }
Mike Stump11289f42009-09-09 15:08:12 +00002633
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002634 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002635 return move(Result);
2636}