blob: 9704b98d48eef798347f44ec8abaa79afc2e7c10 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer5f016e22007-07-11 17:01:13 +000014#include "clang/Parse/Parser.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000015#include "RAIIObjectsForParser.h"
Eli Friedmandc3b7232012-01-04 02:40:39 +000016#include "clang/Basic/PrettyStackTrace.h"
Richard Smith33762772012-03-08 23:06:02 +000017#include "clang/Lex/LiteralSupport.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
20#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000022#include "llvm/Support/ErrorHandling.h"
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Richard Smithea698b32011-04-14 21:45:45 +000026static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
27 switch (Kind) {
28 case tok::kw_template: return 0;
29 case tok::kw_const_cast: return 1;
30 case tok::kw_dynamic_cast: return 2;
31 case tok::kw_reinterpret_cast: return 3;
32 case tok::kw_static_cast: return 4;
33 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000034 llvm_unreachable("Unknown type for digraph error message.");
Richard Smithea698b32011-04-14 21:45:45 +000035 }
36}
37
38// Are the two tokens adjacent in the same source file?
Richard Smith19a27022012-06-18 06:11:04 +000039bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smithea698b32011-04-14 21:45:45 +000040 SourceManager &SM = PP.getSourceManager();
41 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000042 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smithea698b32011-04-14 21:45:45 +000043 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
44}
45
46// Suggest fixit for "<::" after a cast.
47static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
48 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
49 // Pull '<:' and ':' off token stream.
50 if (!AtDigraph)
51 PP.Lex(DigraphToken);
52 PP.Lex(ColonToken);
53
54 SourceRange Range;
55 Range.setBegin(DigraphToken.getLocation());
56 Range.setEnd(ColonToken.getLocation());
57 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
58 << SelectDigraphErrorMessage(Kind)
59 << FixItHint::CreateReplacement(Range, "< ::");
60
61 // Update token information to reflect their change in token type.
62 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000063 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smithea698b32011-04-14 21:45:45 +000064 ColonToken.setLength(2);
65 DigraphToken.setKind(tok::less);
66 DigraphToken.setLength(1);
67
68 // Push new tokens back to token stream.
69 PP.EnterToken(ColonToken);
70 if (!AtDigraph)
71 PP.EnterToken(DigraphToken);
72}
73
Richard Trieu950be712011-09-19 19:01:00 +000074// Check for '<::' which should be '< ::' instead of '[:' when following
75// a template name.
76void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
77 bool EnteringContext,
78 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieuc11030e2011-09-20 20:03:50 +000079 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu950be712011-09-19 19:01:00 +000080 return;
81
82 Token SecondToken = GetLookAheadToken(2);
Richard Smith19a27022012-06-18 06:11:04 +000083 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu950be712011-09-19 19:01:00 +000084 return;
85
86 TemplateTy Template;
87 UnqualifiedId TemplateName;
88 TemplateName.setIdentifier(&II, Tok.getLocation());
89 bool MemberOfUnknownSpecialization;
90 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
91 TemplateName, ObjectType, EnteringContext,
92 Template, MemberOfUnknownSpecialization))
93 return;
94
95 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
96 /*AtDigraph*/false);
97}
98
Richard Trieu919b9552012-11-02 01:08:58 +000099/// \brief Emits an error for a left parentheses after a double colon.
100///
101/// When a '(' is found after a '::', emit an error. Attempt to fix the token
Nico Weberbba91b82012-11-29 05:29:23 +0000102/// stream by removing the '(', and the matching ')' if found.
Richard Trieu919b9552012-11-02 01:08:58 +0000103void Parser::CheckForLParenAfterColonColon() {
104 if (!Tok.is(tok::l_paren))
105 return;
106
107 SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
108 Token Tok1 = getCurToken();
109 if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
110 return;
111
112 if (Tok1.is(tok::identifier)) {
113 Token Tok2 = GetLookAheadToken(1);
114 if (Tok2.is(tok::r_paren)) {
115 ConsumeToken();
116 PP.EnterToken(Tok1);
117 r_parenLoc = ConsumeParen();
118 }
119 } else if (Tok1.is(tok::star)) {
120 Token Tok2 = GetLookAheadToken(1);
121 if (Tok2.is(tok::identifier)) {
122 Token Tok3 = GetLookAheadToken(2);
123 if (Tok3.is(tok::r_paren)) {
124 ConsumeToken();
125 ConsumeToken();
126 PP.EnterToken(Tok2);
127 PP.EnterToken(Tok1);
128 r_parenLoc = ConsumeParen();
129 }
130 }
131 }
132
133 Diag(l_parenLoc, diag::err_paren_after_colon_colon)
134 << FixItHint::CreateRemoval(l_parenLoc)
135 << FixItHint::CreateRemoval(r_parenLoc);
136}
137
Mike Stump1eb44332009-09-09 15:08:12 +0000138/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000139///
140/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000141/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000142/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000143///
144/// '::'[opt] nested-name-specifier
145/// '::'
146///
147/// nested-name-specifier:
148/// type-name '::'
149/// namespace-name '::'
150/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000151/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000152///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000153///
Mike Stump1eb44332009-09-09 15:08:12 +0000154/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000155/// nested-name-specifier (or empty)
156///
Mike Stump1eb44332009-09-09 15:08:12 +0000157/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000158/// the "." or "->" of a member access expression, this parameter provides the
159/// type of the object whose members are being accessed.
160///
161/// \param EnteringContext whether we will be entering into the context of
162/// the nested-name-specifier after parsing it.
163///
Douglas Gregord4dca082010-02-24 18:44:31 +0000164/// \param MayBePseudoDestructor When non-NULL, points to a flag that
165/// indicates whether this nested-name-specifier may be part of a
166/// pseudo-destructor name. In this case, the flag will be set false
167/// if we don't actually end up parsing a destructor name. Moreorover,
168/// if we do end up determining that we are parsing a destructor name,
169/// the last component of the nested-name-specifier is not parsed as
170/// part of the scope specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000171///
172/// \param IsTypename If \c true, this nested-name-specifier is known to be
173/// part of a type name. This is used to improve error recovery.
174///
175/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
176/// filled in with the leading identifier in the last component of the
177/// nested-name-specifier, if any.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000178///
John McCall9ba61662010-02-26 08:45:28 +0000179/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000180bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000181 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000182 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000183 bool *MayBePseudoDestructor,
Richard Smith2db075b2013-03-26 01:15:19 +0000184 bool IsTypename,
185 IdentifierInfo **LastII) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000186 assert(getLangOpts().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000187 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000189 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith2db075b2013-03-26 01:15:19 +0000190 assert(!LastII && "want last identifier but have already annotated scope");
Douglas Gregorc34348a2011-02-24 17:54:50 +0000191 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
192 Tok.getAnnotationRange(),
193 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000194 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000195 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000196 }
Chris Lattnere607e802009-01-04 21:14:15 +0000197
Richard Smith2db075b2013-03-26 01:15:19 +0000198 if (LastII)
199 *LastII = 0;
200
Douglas Gregor39a8de12009-02-25 19:37:18 +0000201 bool HasScopeSpecifier = false;
202
Chris Lattner5b454732009-01-05 03:55:46 +0000203 if (Tok.is(tok::coloncolon)) {
204 // ::new and ::delete aren't nested-name-specifiers.
205 tok::TokenKind NextKind = NextToken().getKind();
206 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
207 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattner55a7cef2009-01-05 00:13:00 +0000209 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000210 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
211 return true;
Richard Trieu919b9552012-11-02 01:08:58 +0000212
213 CheckForLParenAfterColonColon();
214
Douglas Gregor39a8de12009-02-25 19:37:18 +0000215 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000216 }
217
Douglas Gregord4dca082010-02-24 18:44:31 +0000218 bool CheckForDestructor = false;
219 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
220 CheckForDestructor = true;
221 *MayBePseudoDestructor = false;
222 }
223
David Blaikie42d6d0c2011-12-04 05:04:18 +0000224 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
225 DeclSpec DS(AttrFactory);
226 SourceLocation DeclLoc = Tok.getLocation();
227 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
228 if (Tok.isNot(tok::coloncolon)) {
229 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
230 return false;
231 }
232
233 SourceLocation CCLoc = ConsumeToken();
234 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
235 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
236
237 HasScopeSpecifier = true;
238 }
239
Douglas Gregor39a8de12009-02-25 19:37:18 +0000240 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000241 if (HasScopeSpecifier) {
242 // C++ [basic.lookup.classref]p5:
243 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000244 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000245 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000246 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000247 // the class-name-or-namespace-name is looked up in global scope as a
248 // class-name or namespace-name.
249 //
250 // To implement this, we clear out the object type as soon as we've
251 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000252 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000253
254 if (Tok.is(tok::code_completion)) {
255 // Code completion for a nested-name-specifier, where the code
256 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000257 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000258 // Include code completion token into the range of the scope otherwise
259 // when we try to annotate the scope tokens the dangling code completion
260 // token will cause assertion in
261 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000262 SS.setEndLoc(Tok.getLocation());
263 cutOffParsing();
264 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000265 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Douglas Gregor39a8de12009-02-25 19:37:18 +0000268 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000269 // nested-name-specifier 'template'[opt] simple-template-id '::'
270
271 // Parse the optional 'template' keyword, then make sure we have
272 // 'identifier <' after it.
273 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000274 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000275 // nested-name-specifier, since they aren't allowed to start with
276 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000277 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000278 break;
279
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000280 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000281 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000282
283 UnqualifiedId TemplateName;
284 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000285 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000286 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000287 ConsumeToken();
288 } else if (Tok.is(tok::kw_operator)) {
289 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000290 TemplateName)) {
291 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000292 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000293 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000294
Sean Hunte6252d12009-11-28 08:58:14 +0000295 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
296 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000297 Diag(TemplateName.getSourceRange().getBegin(),
298 diag::err_id_after_template_in_nested_name_spec)
299 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000300 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000301 break;
302 }
303 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000304 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000305 break;
306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000308 // If the next token is not '<', we have a qualified-id that refers
309 // to a template name, such as T::template apply, but is not a
310 // template-id.
311 if (Tok.isNot(tok::less)) {
312 TPA.Revert();
313 break;
314 }
315
316 // Commit to parsing the template-id.
317 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000318 TemplateTy Template;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000319 if (TemplateNameKind TNK
320 = Actions.ActOnDependentTemplateName(getCurScope(),
321 SS, TemplateKWLoc, TemplateName,
322 ObjectType, EnteringContext,
323 Template)) {
324 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
325 TemplateName, false))
Douglas Gregord6ab2322010-06-16 23:00:59 +0000326 return true;
327 } else
John McCall9ba61662010-02-26 08:45:28 +0000328 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner77cf72a2009-06-26 03:47:46 +0000330 continue;
331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Douglas Gregor39a8de12009-02-25 19:37:18 +0000333 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000334 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000335 //
336 // simple-template-id '::'
337 //
338 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000339 // right kind (it should name a type or be dependent), and then
340 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000341 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000342 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
343 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000344 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000345 }
346
Richard Smith2db075b2013-03-26 01:15:19 +0000347 if (LastII)
348 *LastII = TemplateId->Name;
349
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000350 // Consume the template-id token.
351 ConsumeToken();
352
353 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
354 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000355
David Blaikie6796fc12011-11-07 03:30:03 +0000356 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000357
Benjamin Kramer5354e772012-08-23 23:38:35 +0000358 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000359 TemplateId->NumArgs);
360
361 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000362 SS,
363 TemplateId->TemplateKWLoc,
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000364 TemplateId->Template,
365 TemplateId->TemplateNameLoc,
366 TemplateId->LAngleLoc,
367 TemplateArgsPtr,
368 TemplateId->RAngleLoc,
369 CCLoc,
370 EnteringContext)) {
371 SourceLocation StartLoc
372 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
373 : TemplateId->TemplateNameLoc;
374 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000375 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000376
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000377 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000378 }
379
Chris Lattner5c7f7862009-06-26 03:52:38 +0000380
381 // The rest of the nested-name-specifier possibilities start with
382 // tok::identifier.
383 if (Tok.isNot(tok::identifier))
384 break;
385
386 IdentifierInfo &II = *Tok.getIdentifierInfo();
387
388 // nested-name-specifier:
389 // type-name '::'
390 // namespace-name '::'
391 // nested-name-specifier identifier '::'
392 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000393
394 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
395 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000396 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000397 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
398 Tok.getLocation(),
399 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000400 EnteringContext) &&
401 // If the token after the colon isn't an identifier, it's still an
402 // error, but they probably meant something else strange so don't
403 // recover like this.
404 PP.LookAhead(1).is(tok::identifier)) {
405 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000406 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000407
408 // Recover as if the user wrote '::'.
409 Next.setKind(tok::coloncolon);
410 }
Chris Lattner46646492009-12-07 01:36:53 +0000411 }
412
Chris Lattner5c7f7862009-06-26 03:52:38 +0000413 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000414 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000415 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000416 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000417 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000418 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000419 }
420
Richard Smith2db075b2013-03-26 01:15:19 +0000421 if (LastII)
422 *LastII = &II;
423
Chris Lattner5c7f7862009-06-26 03:52:38 +0000424 // We have an identifier followed by a '::'. Lookup this name
425 // as the name in a nested-name-specifier.
426 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000427 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
428 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000429 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Richard Trieu919b9552012-11-02 01:08:58 +0000431 CheckForLParenAfterColonColon();
432
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000433 HasScopeSpecifier = true;
434 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
435 ObjectType, EnteringContext, SS))
436 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
437
Chris Lattner5c7f7862009-06-26 03:52:38 +0000438 continue;
439 }
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Richard Trieu950be712011-09-19 19:01:00 +0000441 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000442
Chris Lattner5c7f7862009-06-26 03:52:38 +0000443 // nested-name-specifier:
444 // type-name '<'
445 if (Next.is(tok::less)) {
446 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000447 UnqualifiedId TemplateName;
448 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000449 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000450 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000451 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000452 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000453 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000454 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000455 Template,
456 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000457 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000458 // with a template-id annotation. We do not permit the
459 // template-id to be translated into a type annotation,
460 // because some clients (e.g., the parsing of class template
461 // specializations) still want to see the original template-id
462 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000463 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000464 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
465 TemplateName, false))
John McCall9ba61662010-02-26 08:45:28 +0000466 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000467 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000468 }
469
470 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000471 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000472 // We have something like t::getAs<T>, where getAs is a
473 // member of an unknown specialization. However, this will only
474 // parse correctly as a template, so suggest the keyword 'template'
475 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000476 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikie4e4d0842012-03-11 07:00:24 +0000477 if (getLangOpts().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000478 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000479
480 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000481 << II.getName()
482 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
483
Douglas Gregord6ab2322010-06-16 23:00:59 +0000484 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000485 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000486 SS, SourceLocation(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000487 TemplateName, ObjectType,
488 EnteringContext, Template)) {
489 // Consume the identifier.
490 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000491 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
492 TemplateName, false))
493 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000494 }
495 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000496 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000497
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000498 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000499 }
500 }
501
Douglas Gregor39a8de12009-02-25 19:37:18 +0000502 // We don't have any tokens that form the beginning of a
503 // nested-name-specifier, so we're done.
504 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregord4dca082010-02-24 18:44:31 +0000507 // Even if we didn't see any pieces of a nested-name-specifier, we
508 // still check whether there is a tilde in this position, which
509 // indicates a potential pseudo-destructor.
510 if (CheckForDestructor && Tok.is(tok::tilde))
511 *MayBePseudoDestructor = true;
512
John McCall9ba61662010-02-26 08:45:28 +0000513 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000514}
515
516/// ParseCXXIdExpression - Handle id-expression.
517///
518/// id-expression:
519/// unqualified-id
520/// qualified-id
521///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000522/// qualified-id:
523/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
524/// '::' identifier
525/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000526/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000527///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000528/// NOTE: The standard specifies that, for qualified-id, the parser does not
529/// expect:
530///
531/// '::' conversion-function-id
532/// '::' '~' class-name
533///
534/// This may cause a slight inconsistency on diagnostics:
535///
536/// class C {};
537/// namespace A {}
538/// void f() {
539/// :: A :: ~ C(); // Some Sema error about using destructor with a
540/// // namespace.
541/// :: ~ C(); // Some Parser error like 'unexpected ~'.
542/// }
543///
544/// We simplify the parser a bit and make it work like:
545///
546/// qualified-id:
547/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
548/// '::' unqualified-id
549///
550/// That way Sema can handle and report similar errors for namespaces and the
551/// global scope.
552///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000553/// The isAddressOfOperand parameter indicates that this id-expression is a
554/// direct operand of the address-of operator. This is, besides member contexts,
555/// the only place where a qualified-id naming a non-static class member may
556/// appear.
557///
John McCall60d7b3a2010-08-24 06:29:42 +0000558ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000559 // qualified-id:
560 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
561 // '::' unqualified-id
562 //
563 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000564 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000565
566 SourceLocation TemplateKWLoc;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000567 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000568 if (ParseUnqualifiedId(SS,
569 /*EnteringContext=*/false,
570 /*AllowDestructorName=*/false,
571 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000572 /*ObjectType=*/ ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000573 TemplateKWLoc,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000574 Name))
575 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000576
577 // This is only the direct operand of an & operator if it is not
578 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000579 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
580 isAddressOfOperand = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000581
582 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
583 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000584}
585
Richard Smith0a664b82013-05-09 21:36:41 +0000586/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000587///
588/// lambda-expression:
589/// lambda-introducer lambda-declarator[opt] compound-statement
590///
591/// lambda-introducer:
592/// '[' lambda-capture[opt] ']'
593///
594/// lambda-capture:
595/// capture-default
596/// capture-list
597/// capture-default ',' capture-list
598///
599/// capture-default:
600/// '&'
601/// '='
602///
603/// capture-list:
604/// capture
605/// capture-list ',' capture
606///
607/// capture:
Richard Smith0a664b82013-05-09 21:36:41 +0000608/// simple-capture
609/// init-capture [C++1y]
610///
611/// simple-capture:
Douglas Gregorae7902c2011-08-04 15:30:47 +0000612/// identifier
613/// '&' identifier
614/// 'this'
615///
Richard Smith0a664b82013-05-09 21:36:41 +0000616/// init-capture: [C++1y]
617/// identifier initializer
618/// '&' identifier initializer
619///
Douglas Gregorae7902c2011-08-04 15:30:47 +0000620/// lambda-declarator:
621/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
622/// 'mutable'[opt] exception-specification[opt]
623/// trailing-return-type[opt]
624///
625ExprResult Parser::ParseLambdaExpression() {
626 // Parse lambda-introducer.
627 LambdaIntroducer Intro;
628
David Blaikiedc84cd52013-02-20 22:23:23 +0000629 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000630 if (DiagID) {
631 Diag(Tok, DiagID.getValue());
632 SkipUntil(tok::r_square);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000633 SkipUntil(tok::l_brace);
634 SkipUntil(tok::r_brace);
635 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000636 }
637
638 return ParseLambdaExpressionAfterIntroducer(Intro);
639}
640
641/// TryParseLambdaExpression - Use lookahead and potentially tentative
642/// parsing to determine if we are looking at a C++0x lambda expression, and parse
643/// it if we are.
644///
645/// If we are not looking at a lambda expression, returns ExprError().
646ExprResult Parser::TryParseLambdaExpression() {
Richard Smith80ad52f2013-01-02 11:42:31 +0000647 assert(getLangOpts().CPlusPlus11
Douglas Gregorae7902c2011-08-04 15:30:47 +0000648 && Tok.is(tok::l_square)
649 && "Not at the start of a possible lambda expression.");
650
651 const Token Next = NextToken(), After = GetLookAheadToken(2);
652
653 // If lookahead indicates this is a lambda...
654 if (Next.is(tok::r_square) || // []
655 Next.is(tok::equal) || // [=
656 (Next.is(tok::amp) && // [&] or [&,
657 (After.is(tok::r_square) ||
658 After.is(tok::comma))) ||
659 (Next.is(tok::identifier) && // [identifier]
660 After.is(tok::r_square))) {
661 return ParseLambdaExpression();
662 }
663
Eli Friedmandc3b7232012-01-04 02:40:39 +0000664 // If lookahead indicates an ObjC message send...
665 // [identifier identifier
Douglas Gregorae7902c2011-08-04 15:30:47 +0000666 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000667 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000668 }
669
Eli Friedmandc3b7232012-01-04 02:40:39 +0000670 // Here, we're stuck: lambda introducers and Objective-C message sends are
671 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
672 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
673 // writing two routines to parse a lambda introducer, just try to parse
674 // a lambda introducer first, and fall back if that fails.
675 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregorae7902c2011-08-04 15:30:47 +0000676 LambdaIntroducer Intro;
677 if (TryParseLambdaIntroducer(Intro))
Eli Friedmandc3b7232012-01-04 02:40:39 +0000678 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000679 return ParseLambdaExpressionAfterIntroducer(Intro);
680}
681
Richard Smith440d4562013-05-21 22:21:19 +0000682/// \brief Parse a lambda introducer.
683/// \param Intro A LambdaIntroducer filled in with information about the
684/// contents of the lambda-introducer.
685/// \param SkippedInits If non-null, we are disambiguating between an Obj-C
686/// message send and a lambda expression. In this mode, we will
687/// sometimes skip the initializers for init-captures and not fully
688/// populate \p Intro. This flag will be set to \c true if we do so.
689/// \return A DiagnosticID if it hit something unexpected. The location for
690/// for the diagnostic is that of the current token.
691Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
692 bool *SkippedInits) {
David Blaikiedc84cd52013-02-20 22:23:23 +0000693 typedef Optional<unsigned> DiagResult;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000694
695 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000696 BalancedDelimiterTracker T(*this, tok::l_square);
697 T.consumeOpen();
698
699 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000700
701 bool first = true;
702
703 // Parse capture-default.
704 if (Tok.is(tok::amp) &&
705 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
706 Intro.Default = LCD_ByRef;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000707 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000708 first = false;
709 } else if (Tok.is(tok::equal)) {
710 Intro.Default = LCD_ByCopy;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000711 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000712 first = false;
713 }
714
715 while (Tok.isNot(tok::r_square)) {
716 if (!first) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000717 if (Tok.isNot(tok::comma)) {
Douglas Gregor437fbc52012-07-31 00:50:07 +0000718 // Provide a completion for a lambda introducer here. Except
719 // in Objective-C, where this is Almost Surely meant to be a message
720 // send. In that case, fail here and let the ObjC message
721 // expression parser perform the completion.
Douglas Gregord48ab062012-07-31 15:27:48 +0000722 if (Tok.is(tok::code_completion) &&
723 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
724 !Intro.Captures.empty())) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000725 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
726 /*AfterAmpersand=*/false);
727 ConsumeCodeCompletionToken();
728 break;
729 }
730
Douglas Gregorae7902c2011-08-04 15:30:47 +0000731 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000732 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000733 ConsumeToken();
734 }
735
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000736 if (Tok.is(tok::code_completion)) {
737 // If we're in Objective-C++ and we have a bare '[', then this is more
738 // likely to be a message receiver.
David Blaikie4e4d0842012-03-11 07:00:24 +0000739 if (getLangOpts().ObjC1 && first)
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000740 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
741 else
742 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
743 /*AfterAmpersand=*/false);
744 ConsumeCodeCompletionToken();
745 break;
746 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000747
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000748 first = false;
749
Douglas Gregorae7902c2011-08-04 15:30:47 +0000750 // Parse capture.
751 LambdaCaptureKind Kind = LCK_ByCopy;
752 SourceLocation Loc;
753 IdentifierInfo* Id = 0;
Douglas Gregora7365242012-02-14 19:27:52 +0000754 SourceLocation EllipsisLoc;
Richard Smith0a664b82013-05-09 21:36:41 +0000755 ExprResult Init;
Douglas Gregora7365242012-02-14 19:27:52 +0000756
Douglas Gregorae7902c2011-08-04 15:30:47 +0000757 if (Tok.is(tok::kw_this)) {
758 Kind = LCK_This;
759 Loc = ConsumeToken();
760 } else {
761 if (Tok.is(tok::amp)) {
762 Kind = LCK_ByRef;
763 ConsumeToken();
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000764
765 if (Tok.is(tok::code_completion)) {
766 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
767 /*AfterAmpersand=*/true);
768 ConsumeCodeCompletionToken();
769 break;
770 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000771 }
772
773 if (Tok.is(tok::identifier)) {
774 Id = Tok.getIdentifierInfo();
775 Loc = ConsumeToken();
776 } else if (Tok.is(tok::kw_this)) {
777 // FIXME: If we want to suggest a fixit here, will need to return more
778 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
779 // Clear()ed to prevent emission in case of tentative parsing?
780 return DiagResult(diag::err_this_captured_by_reference);
781 } else {
782 return DiagResult(diag::err_expected_capture);
783 }
Richard Smith0a664b82013-05-09 21:36:41 +0000784
785 if (Tok.is(tok::l_paren)) {
786 BalancedDelimiterTracker Parens(*this, tok::l_paren);
787 Parens.consumeOpen();
788
789 ExprVector Exprs;
790 CommaLocsTy Commas;
Richard Smith440d4562013-05-21 22:21:19 +0000791 if (SkippedInits) {
792 Parens.skipToEnd();
793 *SkippedInits = true;
794 } else if (ParseExpressionList(Exprs, Commas)) {
Richard Smith0a664b82013-05-09 21:36:41 +0000795 Parens.skipToEnd();
796 Init = ExprError();
797 } else {
798 Parens.consumeClose();
799 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
800 Parens.getCloseLocation(),
801 Exprs);
802 }
803 } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
804 if (Tok.is(tok::equal))
805 ConsumeToken();
806
Richard Smith440d4562013-05-21 22:21:19 +0000807 if (!SkippedInits)
808 Init = ParseInitializer();
809 else if (Tok.is(tok::l_brace)) {
810 BalancedDelimiterTracker Braces(*this, tok::l_brace);
811 Braces.consumeOpen();
812 Braces.skipToEnd();
813 *SkippedInits = true;
814 } else {
815 // We're disambiguating this:
816 //
817 // [..., x = expr
818 //
819 // We need to find the end of the following expression in order to
820 // determine whether this is an Obj-C message send's receiver, or a
821 // lambda init-capture.
822 //
823 // Parse the expression to find where it ends, and annotate it back
824 // onto the tokens. We would have parsed this expression the same way
825 // in either case: both the RHS of an init-capture and the RHS of an
826 // assignment expression are parsed as an initializer-clause, and in
827 // neither case can anything be added to the scope between the '[' and
828 // here.
829 //
830 // FIXME: This is horrible. Adding a mechanism to skip an expression
831 // would be much cleaner.
832 // FIXME: If there is a ',' before the next ']' or ':', we can skip to
833 // that instead. (And if we see a ':' with no matching '?', we can
834 // classify this as an Obj-C message send.)
835 SourceLocation StartLoc = Tok.getLocation();
836 InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
837 Init = ParseInitializer();
838
839 if (Tok.getLocation() != StartLoc) {
840 // Back out the lexing of the token after the initializer.
841 PP.RevertCachedTokens(1);
842
843 // Replace the consumed tokens with an appropriate annotation.
844 Tok.setLocation(StartLoc);
845 Tok.setKind(tok::annot_primary_expr);
846 setExprAnnotation(Tok, Init);
847 Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
848 PP.AnnotateCachedTokens(Tok);
849
850 // Consume the annotated initializer.
851 ConsumeToken();
852 }
853 }
Richard Smith0d8e9642013-05-16 06:20:58 +0000854 } else if (Tok.is(tok::ellipsis))
855 EllipsisLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000856 }
857
Richard Smith0a664b82013-05-09 21:36:41 +0000858 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000859 }
860
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000861 T.consumeClose();
862 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000863
864 return DiagResult();
865}
866
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000867/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000868///
869/// Returns true if it hit something unexpected.
870bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
871 TentativeParsingAction PA(*this);
872
Richard Smith440d4562013-05-21 22:21:19 +0000873 bool SkippedInits = false;
874 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000875
876 if (DiagID) {
877 PA.Revert();
878 return true;
879 }
880
Richard Smith440d4562013-05-21 22:21:19 +0000881 if (SkippedInits) {
882 // Parse it again, but this time parse the init-captures too.
883 PA.Revert();
884 Intro = LambdaIntroducer();
885 DiagID = ParseLambdaIntroducer(Intro);
886 assert(!DiagID && "parsing lambda-introducer failed on reparse");
887 return false;
888 }
889
Douglas Gregorae7902c2011-08-04 15:30:47 +0000890 PA.Commit();
891 return false;
892}
893
894/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
895/// expression.
896ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
897 LambdaIntroducer &Intro) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000898 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
899 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
900
901 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
902 "lambda expression parsing");
903
Richard Smith0a664b82013-05-09 21:36:41 +0000904 // FIXME: Call into Actions to add any init-capture declarations to the
905 // scope while parsing the lambda-declarator and compound-statement.
906
Douglas Gregorae7902c2011-08-04 15:30:47 +0000907 // Parse lambda-declarator[opt].
908 DeclSpec DS(AttrFactory);
Eli Friedmanf88c4002012-01-04 04:41:38 +0000909 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000910
911 if (Tok.is(tok::l_paren)) {
912 ParseScope PrototypeScope(this,
913 Scope::FunctionPrototypeScope |
Richard Smith3a2b7a12013-01-28 22:42:45 +0000914 Scope::FunctionDeclarationScope |
Douglas Gregorae7902c2011-08-04 15:30:47 +0000915 Scope::DeclScope);
916
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000917 SourceLocation DeclEndLoc;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000918 BalancedDelimiterTracker T(*this, tok::l_paren);
919 T.consumeOpen();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000920 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000921
922 // Parse parameter-declaration-clause.
923 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000924 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000925 SourceLocation EllipsisLoc;
926
927 if (Tok.isNot(tok::r_paren))
928 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
929
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000930 T.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000931 SourceLocation RParenLoc = T.getCloseLocation();
932 DeclEndLoc = RParenLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000933
934 // Parse 'mutable'[opt].
935 SourceLocation MutableLoc;
936 if (Tok.is(tok::kw_mutable)) {
937 MutableLoc = ConsumeToken();
938 DeclEndLoc = MutableLoc;
939 }
940
941 // Parse exception-specification[opt].
942 ExceptionSpecificationType ESpecType = EST_None;
943 SourceRange ESpecRange;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000944 SmallVector<ParsedType, 2> DynamicExceptions;
945 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000946 ExprResult NoexceptExpr;
Richard Smitha058fd42012-05-02 22:22:32 +0000947 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +0000948 DynamicExceptions,
949 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +0000950 NoexceptExpr);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000951
952 if (ESpecType != EST_None)
953 DeclEndLoc = ESpecRange.getEnd();
954
955 // Parse attribute-specifier[opt].
Richard Smith4e24f0f2013-01-02 12:01:23 +0000956 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000957
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000958 SourceLocation FunLocalRangeEnd = DeclEndLoc;
959
Douglas Gregorae7902c2011-08-04 15:30:47 +0000960 // Parse trailing-return-type[opt].
Richard Smith54655be2012-06-12 01:51:59 +0000961 TypeResult TrailingReturnType;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000962 if (Tok.is(tok::arrow)) {
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000963 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000964 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +0000965 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000966 if (Range.getEnd().isValid())
967 DeclEndLoc = Range.getEnd();
968 }
969
970 PrototypeScope.Exit();
971
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000972 SourceLocation NoLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000973 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000974 /*isAmbiguous=*/false,
975 LParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000976 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000977 EllipsisLoc, RParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000978 DS.getTypeQualifiers(),
979 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000980 /*RefQualifierLoc=*/NoLoc,
981 /*ConstQualifierLoc=*/NoLoc,
982 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000983 MutableLoc,
984 ESpecType, ESpecRange.getBegin(),
985 DynamicExceptions.data(),
986 DynamicExceptionRanges.data(),
987 DynamicExceptions.size(),
988 NoexceptExpr.isUsable() ?
989 NoexceptExpr.get() : 0,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000990 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000991 TrailingReturnType),
992 Attr, DeclEndLoc);
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000993 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
994 // It's common to forget that one needs '()' before 'mutable' or the
995 // result type. Deal with this.
996 Diag(Tok, diag::err_lambda_missing_parens)
997 << Tok.is(tok::arrow)
998 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
999 SourceLocation DeclLoc = Tok.getLocation();
1000 SourceLocation DeclEndLoc = DeclLoc;
1001
1002 // Parse 'mutable', if it's there.
1003 SourceLocation MutableLoc;
1004 if (Tok.is(tok::kw_mutable)) {
1005 MutableLoc = ConsumeToken();
1006 DeclEndLoc = MutableLoc;
1007 }
1008
1009 // Parse the return type, if there is one.
Richard Smith54655be2012-06-12 01:51:59 +00001010 TypeResult TrailingReturnType;
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001011 if (Tok.is(tok::arrow)) {
1012 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00001013 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001014 if (Range.getEnd().isValid())
1015 DeclEndLoc = Range.getEnd();
1016 }
1017
1018 ParsedAttributes Attr(AttrFactory);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001019 SourceLocation NoLoc;
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001020 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001021 /*isAmbiguous=*/false,
1022 /*LParenLoc=*/NoLoc,
1023 /*Params=*/0,
1024 /*NumParams=*/0,
1025 /*EllipsisLoc=*/NoLoc,
1026 /*RParenLoc=*/NoLoc,
1027 /*TypeQuals=*/0,
1028 /*RefQualifierIsLValueRef=*/true,
1029 /*RefQualifierLoc=*/NoLoc,
1030 /*ConstQualifierLoc=*/NoLoc,
1031 /*VolatileQualifierLoc=*/NoLoc,
1032 MutableLoc,
1033 EST_None,
1034 /*ESpecLoc=*/NoLoc,
1035 /*Exceptions=*/0,
1036 /*ExceptionRanges=*/0,
1037 /*NumExceptions=*/0,
1038 /*NoexceptExpr=*/0,
1039 DeclLoc, DeclEndLoc, D,
1040 TrailingReturnType),
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001041 Attr, DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001042 }
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001043
Douglas Gregorae7902c2011-08-04 15:30:47 +00001044
Eli Friedman906a7e12012-01-06 03:05:34 +00001045 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1046 // it.
Douglas Gregorfccfb622012-02-21 22:51:27 +00001047 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorfccfb622012-02-21 22:51:27 +00001048 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman906a7e12012-01-06 03:05:34 +00001049
Eli Friedmanec9ea722012-01-05 03:35:19 +00001050 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1051
Douglas Gregorae7902c2011-08-04 15:30:47 +00001052 // Parse compound-statement.
Eli Friedmandc3b7232012-01-04 02:40:39 +00001053 if (!Tok.is(tok::l_brace)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +00001054 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmandc3b7232012-01-04 02:40:39 +00001055 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1056 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001057 }
1058
Eli Friedmandc3b7232012-01-04 02:40:39 +00001059 StmtResult Stmt(ParseCompoundStatementBody());
1060 BodyScope.Exit();
1061
Eli Friedmandeeab902012-01-04 02:46:53 +00001062 if (!Stmt.isInvalid())
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001063 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmandc3b7232012-01-04 02:40:39 +00001064
Eli Friedmandeeab902012-01-04 02:46:53 +00001065 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1066 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001067}
1068
Reid Spencer5f016e22007-07-11 17:01:13 +00001069/// ParseCXXCasts - This handles the various ways to cast expressions to another
1070/// type.
1071///
1072/// postfix-expression: [C++ 5.2p1]
1073/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1074/// 'static_cast' '<' type-name '>' '(' expression ')'
1075/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1076/// 'const_cast' '<' type-name '>' '(' expression ')'
1077///
John McCall60d7b3a2010-08-24 06:29:42 +00001078ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 tok::TokenKind Kind = Tok.getKind();
1080 const char *CastName = 0; // For error messages
1081
1082 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +00001083 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 case tok::kw_const_cast: CastName = "const_cast"; break;
1085 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1086 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1087 case tok::kw_static_cast: CastName = "static_cast"; break;
1088 }
1089
1090 SourceLocation OpLoc = ConsumeToken();
1091 SourceLocation LAngleBracketLoc = Tok.getLocation();
1092
Richard Smithea698b32011-04-14 21:45:45 +00001093 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1094 // diagnose error, suggest fix, and recover parsing.
Richard Smith78fe3e02012-08-20 17:37:52 +00001095 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1096 Token Next = NextToken();
1097 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1098 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1099 }
Richard Smithea698b32011-04-14 21:45:45 +00001100
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001102 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001103
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001104 // Parse the common declaration-specifiers piece.
1105 DeclSpec DS(AttrFactory);
1106 ParseSpecifierQualifierList(DS);
1107
1108 // Parse the abstract-declarator, if present.
1109 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1110 ParseDeclarator(DeclaratorInfo);
1111
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 SourceLocation RAngleBracketLoc = Tok.getLocation();
1113
Chris Lattner1ab3b962008-11-18 07:48:38 +00001114 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001115 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +00001116
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001117 SourceLocation LParenLoc, RParenLoc;
1118 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001119
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001120 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001121 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001122
John McCall60d7b3a2010-08-24 06:29:42 +00001123 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001125 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001126 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001127
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001128 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +00001129 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001130 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +00001131 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001132 T.getOpenLocation(), Result.take(),
1133 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001134
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001135 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00001136}
1137
Sebastian Redlc42e1182008-11-11 11:37:55 +00001138/// ParseCXXTypeid - This handles the C++ typeid expression.
1139///
1140/// postfix-expression: [C++ 5.2p1]
1141/// 'typeid' '(' expression ')'
1142/// 'typeid' '(' type-id ')'
1143///
John McCall60d7b3a2010-08-24 06:29:42 +00001144ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +00001145 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1146
1147 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001148 SourceLocation LParenLoc, RParenLoc;
1149 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001150
1151 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001152 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001153 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001154 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001155
John McCall60d7b3a2010-08-24 06:29:42 +00001156 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001157
Richard Smith05766812012-08-18 00:55:03 +00001158 // C++0x [expr.typeid]p3:
1159 // When typeid is applied to an expression other than an lvalue of a
1160 // polymorphic class type [...] The expression is an unevaluated
1161 // operand (Clause 5).
1162 //
1163 // Note that we can't tell whether the expression is an lvalue of a
1164 // polymorphic class type until after we've parsed the expression; we
1165 // speculatively assume the subexpression is unevaluated, and fix it up
1166 // later.
1167 //
1168 // We enter the unevaluated context before trying to determine whether we
1169 // have a type-id, because the tentative parse logic will try to resolve
1170 // names, and must treat them as unevaluated.
Eli Friedman80bfa3d2012-09-26 04:34:21 +00001171 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1172 Sema::ReuseLambdaContextDecl);
Richard Smith05766812012-08-18 00:55:03 +00001173
Sebastian Redlc42e1182008-11-11 11:37:55 +00001174 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001175 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001176
1177 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001178 T.consumeClose();
1179 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001180 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001181 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001182
1183 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +00001184 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001185 } else {
1186 Result = ParseExpression();
1187
1188 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001189 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +00001190 SkipUntil(tok::r_paren);
1191 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001192 T.consumeClose();
1193 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001194 if (RParenLoc.isInvalid())
1195 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001196
Sebastian Redlc42e1182008-11-11 11:37:55 +00001197 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001198 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001199 }
1200 }
1201
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001202 return Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001203}
1204
Francois Pichet01b7c302010-09-08 12:20:18 +00001205/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1206///
1207/// '__uuidof' '(' expression ')'
1208/// '__uuidof' '(' type-id ')'
1209///
1210ExprResult Parser::ParseCXXUuidof() {
1211 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1212
1213 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001214 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +00001215
1216 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001217 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +00001218 return ExprError();
1219
1220 ExprResult Result;
1221
1222 if (isTypeIdInParens()) {
1223 TypeResult Ty = ParseTypeName();
1224
1225 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001226 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001227
1228 if (Ty.isInvalid())
1229 return ExprError();
1230
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001231 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1232 Ty.get().getAsOpaquePtr(),
1233 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001234 } else {
1235 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1236 Result = ParseExpression();
1237
1238 // Match the ')'.
1239 if (Result.isInvalid())
1240 SkipUntil(tok::r_paren);
1241 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001242 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001243
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001244 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1245 /*isType=*/false,
1246 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001247 }
1248 }
1249
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001250 return Result;
Francois Pichet01b7c302010-09-08 12:20:18 +00001251}
1252
Douglas Gregord4dca082010-02-24 18:44:31 +00001253/// \brief Parse a C++ pseudo-destructor expression after the base,
1254/// . or -> operator, and nested-name-specifier have already been
1255/// parsed.
1256///
1257/// postfix-expression: [C++ 5.2]
1258/// postfix-expression . pseudo-destructor-name
1259/// postfix-expression -> pseudo-destructor-name
1260///
1261/// pseudo-destructor-name:
1262/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1263/// ::[opt] nested-name-specifier template simple-template-id ::
1264/// ~type-name
1265/// ::[opt] nested-name-specifier[opt] ~type-name
1266///
John McCall60d7b3a2010-08-24 06:29:42 +00001267ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +00001268Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1269 tok::TokenKind OpKind,
1270 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +00001271 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +00001272 // We're parsing either a pseudo-destructor-name or a dependent
1273 // member access that has the same form as a
1274 // pseudo-destructor-name. We parse both in the same way and let
1275 // the action model sort them out.
1276 //
1277 // Note that the ::[opt] nested-name-specifier[opt] has already
1278 // been parsed, and if there was a simple-template-id, it has
1279 // been coalesced into a template-id annotation token.
1280 UnqualifiedId FirstTypeName;
1281 SourceLocation CCLoc;
1282 if (Tok.is(tok::identifier)) {
1283 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1284 ConsumeToken();
1285 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1286 CCLoc = ConsumeToken();
1287 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001288 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1289 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregord4dca082010-02-24 18:44:31 +00001290 FirstTypeName.setTemplateId(
1291 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1292 ConsumeToken();
1293 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1294 CCLoc = ConsumeToken();
1295 } else {
1296 FirstTypeName.setIdentifier(0, SourceLocation());
1297 }
1298
1299 // Parse the tilde.
1300 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1301 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001302
1303 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1304 DeclSpec DS(AttrFactory);
Benjamin Kramer85c60db2011-12-18 12:18:02 +00001305 ParseDecltypeSpecifier(DS);
David Blaikie91ec7892011-12-16 16:03:09 +00001306 if (DS.getTypeSpecType() == TST_error)
1307 return ExprError();
1308 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1309 OpKind, TildeLoc, DS,
1310 Tok.is(tok::l_paren));
1311 }
1312
Douglas Gregord4dca082010-02-24 18:44:31 +00001313 if (!Tok.is(tok::identifier)) {
1314 Diag(Tok, diag::err_destructor_tilde_identifier);
1315 return ExprError();
1316 }
1317
1318 // Parse the second type.
1319 UnqualifiedId SecondTypeName;
1320 IdentifierInfo *Name = Tok.getIdentifierInfo();
1321 SourceLocation NameLoc = ConsumeToken();
1322 SecondTypeName.setIdentifier(Name, NameLoc);
1323
1324 // If there is a '<', the second type name is a template-id. Parse
1325 // it as such.
1326 if (Tok.is(tok::less) &&
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001327 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1328 Name, NameLoc,
1329 false, ObjectType, SecondTypeName,
1330 /*AssumeTemplateName=*/true))
Douglas Gregord4dca082010-02-24 18:44:31 +00001331 return ExprError();
1332
John McCall9ae2f072010-08-23 23:25:46 +00001333 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1334 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001335 SS, FirstTypeName, CCLoc,
1336 TildeLoc, SecondTypeName,
1337 Tok.is(tok::l_paren));
1338}
1339
Reid Spencer5f016e22007-07-11 17:01:13 +00001340/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1341///
1342/// boolean-literal: [C++ 2.13.5]
1343/// 'true'
1344/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001345ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001346 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001347 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001348}
Chris Lattner50dd2892008-02-26 00:51:44 +00001349
1350/// ParseThrowExpression - This handles the C++ throw expression.
1351///
1352/// throw-expression: [C++ 15]
1353/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001354ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001355 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001356 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001357
Chris Lattner2a2819a2008-04-06 06:02:23 +00001358 // If the current token isn't the start of an assignment-expression,
1359 // then the expression is not present. This handles things like:
1360 // "C ? throw : (void)42", which is crazy but legal.
1361 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1362 case tok::semi:
1363 case tok::r_paren:
1364 case tok::r_square:
1365 case tok::r_brace:
1366 case tok::colon:
1367 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001368 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001369
Chris Lattner2a2819a2008-04-06 06:02:23 +00001370 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001371 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001372 if (Expr.isInvalid()) return Expr;
Douglas Gregorbca01b42011-07-06 22:04:06 +00001373 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001374 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001375}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001376
1377/// ParseCXXThis - This handles the C++ 'this' pointer.
1378///
1379/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1380/// a non-lvalue expression whose value is the address of the object for which
1381/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001382ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001383 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1384 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001385 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001386}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001387
1388/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1389/// Can be interpreted either as function-style casting ("int(x)")
1390/// or class type construction ("ClassType(x,y,z)")
1391/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001392/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001393///
1394/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001395/// simple-type-specifier '(' expression-list[opt] ')'
1396/// [C++0x] simple-type-specifier braced-init-list
1397/// typename-specifier '(' expression-list[opt] ')'
1398/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001399///
John McCall60d7b3a2010-08-24 06:29:42 +00001400ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001401Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001402 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001403 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001404
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001405 assert((Tok.is(tok::l_paren) ||
Richard Smith80ad52f2013-01-02 11:42:31 +00001406 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001407 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001408
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001409 if (Tok.is(tok::l_brace)) {
Sebastian Redl6dc00f62012-02-12 18:41:05 +00001410 ExprResult Init = ParseBraceInitializer();
1411 if (Init.isInvalid())
1412 return Init;
1413 Expr *InitList = Init.take();
1414 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1415 MultiExprArg(&InitList, 1),
1416 SourceLocation());
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001417 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001418 BalancedDelimiterTracker T(*this, tok::l_paren);
1419 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001420
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001421 ExprVector Exprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001422 CommaLocsTy CommaLocs;
1423
1424 if (Tok.isNot(tok::r_paren)) {
1425 if (ParseExpressionList(Exprs, CommaLocs)) {
1426 SkipUntil(tok::r_paren);
1427 return ExprError();
1428 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001429 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001430
1431 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001432 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001433
1434 // TypeRep could be null, if it references an invalid typedef.
1435 if (!TypeRep)
1436 return ExprError();
1437
1438 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1439 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001440 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001441 Exprs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001442 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001443 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001444}
1445
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001446/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001447///
1448/// condition:
1449/// expression
1450/// type-specifier-seq declarator '=' assignment-expression
Richard Smith0635aa72012-02-22 06:49:09 +00001451/// [C++11] type-specifier-seq declarator '=' initializer-clause
1452/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001453/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1454/// '=' assignment-expression
1455///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001456/// \param ExprOut if the condition was parsed as an expression, the parsed
1457/// expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001458///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001459/// \param DeclOut if the condition was parsed as a declaration, the parsed
1460/// declaration.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001461///
Douglas Gregor586596f2010-05-06 17:25:47 +00001462/// \param Loc The location of the start of the statement that requires this
1463/// condition, e.g., the "for" in a for loop.
1464///
1465/// \param ConvertToBoolean Whether the condition expression should be
1466/// converted to a boolean value.
1467///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001468/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001469bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1470 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001471 SourceLocation Loc,
1472 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001473 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001474 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001475 cutOffParsing();
1476 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001477 }
1478
Sean Hunt2edf0a22012-06-23 05:07:58 +00001479 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001480 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001481
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001482 if (!isCXXConditionDeclaration()) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001483 ProhibitAttributes(attrs);
1484
Douglas Gregor586596f2010-05-06 17:25:47 +00001485 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001486 ExprOut = ParseExpression(); // expression
1487 DeclOut = 0;
1488 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001489 return true;
1490
1491 // If required, convert to a boolean value.
1492 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001493 ExprOut
1494 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1495 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001496 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001497
1498 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001499 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +00001500 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001501 ParseSpecifierQualifierList(DS);
1502
1503 // declarator
1504 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1505 ParseDeclarator(DeclaratorInfo);
1506
1507 // simple-asm-expr[opt]
1508 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001509 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001510 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001511 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001512 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001513 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001514 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001515 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001516 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001517 }
1518
1519 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001520 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001521
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001522 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001523 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001524 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001525 DeclOut = Dcl.get();
1526 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001527
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001528 // '=' assignment-expression
Richard Trieud6c7c672012-01-18 22:54:52 +00001529 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith0635aa72012-02-22 06:49:09 +00001530 bool CopyInitialization = isTokenEqualOrEqualTypo();
1531 if (CopyInitialization)
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001532 ConsumeToken();
Richard Smith0635aa72012-02-22 06:49:09 +00001533
1534 ExprResult InitExpr = ExprError();
Richard Smith80ad52f2013-01-02 11:42:31 +00001535 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith0635aa72012-02-22 06:49:09 +00001536 Diag(Tok.getLocation(),
1537 diag::warn_cxx98_compat_generalized_initializer_lists);
1538 InitExpr = ParseBraceInitializer();
1539 } else if (CopyInitialization) {
1540 InitExpr = ParseAssignmentExpression();
1541 } else if (Tok.is(tok::l_paren)) {
1542 // This was probably an attempt to initialize the variable.
1543 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1544 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1545 RParen = ConsumeParen();
1546 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1547 diag::err_expected_init_in_condition_lparen)
1548 << SourceRange(LParen, RParen);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001549 } else {
Richard Smith0635aa72012-02-22 06:49:09 +00001550 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1551 diag::err_expected_init_in_condition);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001552 }
Richard Smith0635aa72012-02-22 06:49:09 +00001553
1554 if (!InitExpr.isInvalid())
1555 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smitha2c36462013-04-26 16:15:35 +00001556 DS.containsPlaceholderType());
Richard Smithdc7a4f52013-04-30 13:56:41 +00001557 else
1558 Actions.ActOnInitializerError(DeclOut);
Richard Smith0635aa72012-02-22 06:49:09 +00001559
Douglas Gregor586596f2010-05-06 17:25:47 +00001560 // FIXME: Build a reference to this declaration? Convert it to bool?
1561 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001562
1563 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001564
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001565 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001566}
1567
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001568/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1569/// This should only be called when the current token is known to be part of
1570/// simple-type-specifier.
1571///
1572/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001573/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001574/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1575/// char
1576/// wchar_t
1577/// bool
1578/// short
1579/// int
1580/// long
1581/// signed
1582/// unsigned
1583/// float
1584/// double
1585/// void
1586/// [GNU] typeof-specifier
1587/// [C++0x] auto [TODO]
1588///
1589/// type-name:
1590/// class-name
1591/// enum-name
1592/// typedef-name
1593///
1594void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1595 DS.SetRangeStart(Tok.getLocation());
1596 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001597 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001598 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001600 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001601 case tok::identifier: // foo::bar
1602 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001603 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001604 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001605 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001606
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001607 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001608 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001609 if (getTypeAnnotation(Tok))
1610 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1611 getTypeAnnotation(Tok));
1612 else
1613 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001614
1615 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1616 ConsumeToken();
1617
1618 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1619 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1620 // Objective-C interface. If we don't have Objective-C or a '<', this is
1621 // just a normal reference to a typedef name.
David Blaikie4e4d0842012-03-11 07:00:24 +00001622 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001623 ParseObjCProtocolQualifiers(DS);
1624
1625 DS.Finish(Diags, PP);
1626 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001627 }
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001629 // builtin types
1630 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001631 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001632 break;
1633 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001634 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001635 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001636 case tok::kw___int64:
1637 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1638 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001639 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001640 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001641 break;
1642 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001643 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001644 break;
1645 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001646 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001647 break;
1648 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001649 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001650 break;
1651 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001652 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001653 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00001654 case tok::kw___int128:
1655 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1656 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001657 case tok::kw_half:
1658 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1659 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001660 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001661 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001662 break;
1663 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001664 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001665 break;
1666 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001667 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001668 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001669 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001670 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001671 break;
1672 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001673 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001674 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001675 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001676 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001677 break;
David Blaikie5e089fe2012-01-24 05:47:35 +00001678 case tok::annot_decltype:
1679 case tok::kw_decltype:
1680 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1681 return DS.Finish(Diags, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001683 // GNU typeof support.
1684 case tok::kw_typeof:
1685 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001686 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001687 return;
1688 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001689 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001690 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1691 else
1692 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001693 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001694 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001695}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001696
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001697/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1698/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1699/// e.g., "const short int". Note that the DeclSpec is *not* finished
1700/// by parsing the type-specifier-seq, because these sequences are
1701/// typically followed by some form of declarator. Returns true and
1702/// emits diagnostics if this is not a type-specifier-seq, false
1703/// otherwise.
1704///
1705/// type-specifier-seq: [C++ 8.1]
1706/// type-specifier type-specifier-seq[opt]
1707///
1708bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smith69730c12012-03-12 07:56:15 +00001709 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor396a9f22010-02-24 23:13:13 +00001710 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001711 return false;
1712}
1713
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001714/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1715/// some form.
1716///
1717/// This routine is invoked when a '<' is encountered after an identifier or
1718/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1719/// whether the unqualified-id is actually a template-id. This routine will
1720/// then parse the template arguments and form the appropriate template-id to
1721/// return to the caller.
1722///
1723/// \param SS the nested-name-specifier that precedes this template-id, if
1724/// we're actually parsing a qualified-id.
1725///
1726/// \param Name for constructor and destructor names, this is the actual
1727/// identifier that may be a template-name.
1728///
1729/// \param NameLoc the location of the class-name in a constructor or
1730/// destructor.
1731///
1732/// \param EnteringContext whether we're entering the scope of the
1733/// nested-name-specifier.
1734///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001735/// \param ObjectType if this unqualified-id occurs within a member access
1736/// expression, the type of the base object whose member is being accessed.
1737///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001738/// \param Id as input, describes the template-name or operator-function-id
1739/// that precedes the '<'. If template arguments were parsed successfully,
1740/// will be updated with the template-id.
1741///
Douglas Gregord4dca082010-02-24 18:44:31 +00001742/// \param AssumeTemplateId When true, this routine will assume that the name
1743/// refers to a template without performing name lookup to verify.
1744///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001745/// \returns true if a parse error occurred, false otherwise.
1746bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001747 SourceLocation TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001748 IdentifierInfo *Name,
1749 SourceLocation NameLoc,
1750 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001751 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001752 UnqualifiedId &Id,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001753 bool AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001754 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1755 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001756
1757 TemplateTy Template;
1758 TemplateNameKind TNK = TNK_Non_template;
1759 switch (Id.getKind()) {
1760 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001761 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001762 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001763 if (AssumeTemplateId) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001764 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001765 Id, ObjectType, EnteringContext,
1766 Template);
1767 if (TNK == TNK_Non_template)
1768 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001769 } else {
1770 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001771 TNK = Actions.isTemplateName(getCurScope(), SS,
1772 TemplateKWLoc.isValid(), Id,
1773 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001774 MemberOfUnknownSpecialization);
1775
1776 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1777 ObjectType && IsTemplateArgumentList()) {
1778 // We have something like t->getAs<T>(), where getAs is a
1779 // member of an unknown specialization. However, this will only
1780 // parse correctly as a template, so suggest the keyword 'template'
1781 // before 'getAs' and treat this as a dependent template name.
1782 std::string Name;
1783 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1784 Name = Id.Identifier->getName();
1785 else {
1786 Name = "operator ";
1787 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1788 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1789 else
1790 Name += Id.Identifier->getName();
1791 }
1792 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1793 << Name
1794 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001795 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1796 SS, TemplateKWLoc, Id,
1797 ObjectType, EnteringContext,
1798 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001799 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001800 return true;
1801 }
1802 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001803 break;
1804
Douglas Gregor014e88d2009-11-03 23:16:33 +00001805 case UnqualifiedId::IK_ConstructorName: {
1806 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001807 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001808 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001809 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1810 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001811 EnteringContext, Template,
1812 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001813 break;
1814 }
1815
Douglas Gregor014e88d2009-11-03 23:16:33 +00001816 case UnqualifiedId::IK_DestructorName: {
1817 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001818 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001819 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001820 if (ObjectType) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001821 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1822 SS, TemplateKWLoc, TemplateName,
1823 ObjectType, EnteringContext,
1824 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001825 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001826 return true;
1827 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001828 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1829 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001830 EnteringContext, Template,
1831 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001832
John McCallb3d87482010-08-24 05:47:05 +00001833 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001834 Diag(NameLoc, diag::err_destructor_template_id)
1835 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001836 return true;
1837 }
1838 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001839 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001840 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001841
1842 default:
1843 return false;
1844 }
1845
1846 if (TNK == TNK_Non_template)
1847 return false;
1848
1849 // Parse the enclosed template argument list.
1850 SourceLocation LAngleLoc, RAngleLoc;
1851 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001852 if (Tok.is(tok::less) &&
1853 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001854 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001855 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001856 RAngleLoc))
1857 return true;
1858
1859 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001860 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1861 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001862 // Form a parsed representation of the template-id to be stored in the
1863 // UnqualifiedId.
1864 TemplateIdAnnotation *TemplateId
Benjamin Kramer13bb7012012-04-14 12:14:03 +00001865 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001866
1867 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1868 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001869 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001870 TemplateId->TemplateNameLoc = Id.StartLocation;
1871 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001872 TemplateId->Name = 0;
1873 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1874 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001875 }
1876
Douglas Gregor059101f2011-03-02 00:47:37 +00001877 TemplateId->SS = SS;
Benjamin Kramer2b28bf12012-02-19 23:37:39 +00001878 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall2b5289b2010-08-23 07:28:44 +00001879 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001880 TemplateId->Kind = TNK;
1881 TemplateId->LAngleLoc = LAngleLoc;
1882 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001883 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001884 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001885 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001886 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001887
1888 Id.setTemplateId(TemplateId);
1889 return false;
1890 }
1891
1892 // Bundle the template arguments together.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001893 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001894
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001895 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001896 TypeResult Type
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001897 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1898 Template, NameLoc,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001899 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1900 /*IsCtorOrDtorName=*/true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001901 if (Type.isInvalid())
1902 return true;
1903
1904 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1905 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1906 else
1907 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1908
1909 return false;
1910}
1911
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001912/// \brief Parse an operator-function-id or conversion-function-id as part
1913/// of a C++ unqualified-id.
1914///
1915/// This routine is responsible only for parsing the operator-function-id or
1916/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001917///
1918/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001919/// operator-function-id: [C++ 13.5]
1920/// 'operator' operator
1921///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001922/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001923/// new delete new[] delete[]
1924/// + - * / % ^ & | ~
1925/// ! = < > += -= *= /= %=
1926/// ^= &= |= << >> >>= <<= == !=
1927/// <= >= && || ++ -- , ->* ->
1928/// () []
1929///
1930/// conversion-function-id: [C++ 12.3.2]
1931/// operator conversion-type-id
1932///
1933/// conversion-type-id:
1934/// type-specifier-seq conversion-declarator[opt]
1935///
1936/// conversion-declarator:
1937/// ptr-operator conversion-declarator[opt]
1938/// \endcode
1939///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001940/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001941/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1942///
1943/// \param EnteringContext whether we are entering the scope of the
1944/// nested-name-specifier.
1945///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001946/// \param ObjectType if this unqualified-id occurs within a member access
1947/// expression, the type of the base object whose member is being accessed.
1948///
1949/// \param Result on a successful parse, contains the parsed unqualified-id.
1950///
1951/// \returns true if parsing fails, false otherwise.
1952bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001953 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001954 UnqualifiedId &Result) {
1955 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1956
1957 // Consume the 'operator' keyword.
1958 SourceLocation KeywordLoc = ConsumeToken();
1959
1960 // Determine what kind of operator name we have.
1961 unsigned SymbolIdx = 0;
1962 SourceLocation SymbolLocations[3];
1963 OverloadedOperatorKind Op = OO_None;
1964 switch (Tok.getKind()) {
1965 case tok::kw_new:
1966 case tok::kw_delete: {
1967 bool isNew = Tok.getKind() == tok::kw_new;
1968 // Consume the 'new' or 'delete'.
1969 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith6ee326a2012-04-10 01:32:12 +00001970 // Check for array new/delete.
1971 if (Tok.is(tok::l_square) &&
Richard Smith80ad52f2013-01-02 11:42:31 +00001972 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001973 // Consume the '[' and ']'.
1974 BalancedDelimiterTracker T(*this, tok::l_square);
1975 T.consumeOpen();
1976 T.consumeClose();
1977 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001978 return true;
1979
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001980 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1981 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001982 Op = isNew? OO_Array_New : OO_Array_Delete;
1983 } else {
1984 Op = isNew? OO_New : OO_Delete;
1985 }
1986 break;
1987 }
1988
1989#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1990 case tok::Token: \
1991 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1992 Op = OO_##Name; \
1993 break;
1994#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1995#include "clang/Basic/OperatorKinds.def"
1996
1997 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001998 // Consume the '(' and ')'.
1999 BalancedDelimiterTracker T(*this, tok::l_paren);
2000 T.consumeOpen();
2001 T.consumeClose();
2002 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002003 return true;
2004
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002005 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2006 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002007 Op = OO_Call;
2008 break;
2009 }
2010
2011 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002012 // Consume the '[' and ']'.
2013 BalancedDelimiterTracker T(*this, tok::l_square);
2014 T.consumeOpen();
2015 T.consumeClose();
2016 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002017 return true;
2018
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002019 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2020 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002021 Op = OO_Subscript;
2022 break;
2023 }
2024
2025 case tok::code_completion: {
2026 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002027 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002028 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002029 // Don't try to parse any further.
2030 return true;
2031 }
2032
2033 default:
2034 break;
2035 }
2036
2037 if (Op != OO_None) {
2038 // We have parsed an operator-function-id.
2039 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2040 return false;
2041 }
Sean Hunt0486d742009-11-28 04:44:28 +00002042
2043 // Parse a literal-operator-id.
2044 //
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002045 // literal-operator-id: C++11 [over.literal]
2046 // operator string-literal identifier
2047 // operator user-defined-string-literal
Sean Hunt0486d742009-11-28 04:44:28 +00002048
Richard Smith80ad52f2013-01-02 11:42:31 +00002049 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith7fe62082011-10-15 05:09:34 +00002050 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00002051
Richard Smith33762772012-03-08 23:06:02 +00002052 SourceLocation DiagLoc;
2053 unsigned DiagId = 0;
2054
2055 // We're past translation phase 6, so perform string literal concatenation
2056 // before checking for "".
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002057 SmallVector<Token, 4> Toks;
2058 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith33762772012-03-08 23:06:02 +00002059 while (isTokenStringLiteral()) {
2060 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002061 // C++11 [over.literal]p1:
2062 // The string-literal or user-defined-string-literal in a
2063 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith33762772012-03-08 23:06:02 +00002064 DiagLoc = Tok.getLocation();
2065 DiagId = diag::err_literal_operator_string_prefix;
2066 }
2067 Toks.push_back(Tok);
2068 TokLocs.push_back(ConsumeStringToken());
2069 }
2070
2071 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
2072 if (Literal.hadError)
2073 return true;
2074
2075 // Grab the literal operator's suffix, which will be either the next token
2076 // or a ud-suffix from the string literal.
2077 IdentifierInfo *II = 0;
2078 SourceLocation SuffixLoc;
2079 if (!Literal.getUDSuffix().empty()) {
2080 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2081 SuffixLoc =
2082 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2083 Literal.getUDSuffixOffset(),
David Blaikie4e4d0842012-03-11 07:00:24 +00002084 PP.getSourceManager(), getLangOpts());
Richard Smith33762772012-03-08 23:06:02 +00002085 } else if (Tok.is(tok::identifier)) {
2086 II = Tok.getIdentifierInfo();
2087 SuffixLoc = ConsumeToken();
2088 TokLocs.push_back(SuffixLoc);
2089 } else {
Sean Hunt0486d742009-11-28 04:44:28 +00002090 Diag(Tok.getLocation(), diag::err_expected_ident);
2091 return true;
2092 }
2093
Richard Smith33762772012-03-08 23:06:02 +00002094 // The string literal must be empty.
2095 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002096 // C++11 [over.literal]p1:
2097 // The string-literal or user-defined-string-literal in a
2098 // literal-operator-id shall [...] contain no characters
2099 // other than the implicit terminating '\0'.
Richard Smith33762772012-03-08 23:06:02 +00002100 DiagLoc = TokLocs.front();
2101 DiagId = diag::err_literal_operator_string_not_empty;
2102 }
2103
2104 if (DiagId) {
2105 // This isn't a valid literal-operator-id, but we think we know
2106 // what the user meant. Tell them what they should have written.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002107 SmallString<32> Str;
Richard Smith33762772012-03-08 23:06:02 +00002108 Str += "\"\" ";
2109 Str += II->getName();
2110 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2111 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2112 }
2113
2114 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Sean Hunt3e518bd2009-11-29 07:34:05 +00002115 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00002116 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002117
2118 // Parse a conversion-function-id.
2119 //
2120 // conversion-function-id: [C++ 12.3.2]
2121 // operator conversion-type-id
2122 //
2123 // conversion-type-id:
2124 // type-specifier-seq conversion-declarator[opt]
2125 //
2126 // conversion-declarator:
2127 // ptr-operator conversion-declarator[opt]
2128
2129 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00002130 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00002131 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002132 return true;
2133
2134 // Parse the conversion-declarator, which is merely a sequence of
2135 // ptr-operators.
Richard Smith14f78f42013-05-04 01:26:46 +00002136 Declarator D(DS, Declarator::ConversionIdContext);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002137 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2138
2139 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00002140 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002141 if (Ty.isInvalid())
2142 return true;
2143
2144 // Note that this is a conversion-function-id.
2145 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2146 D.getSourceRange().getEnd());
2147 return false;
2148}
2149
2150/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2151/// name of an entity.
2152///
2153/// \code
2154/// unqualified-id: [C++ expr.prim.general]
2155/// identifier
2156/// operator-function-id
2157/// conversion-function-id
2158/// [C++0x] literal-operator-id [TODO]
2159/// ~ class-name
2160/// template-id
2161///
2162/// \endcode
2163///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00002164/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002165/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2166///
2167/// \param EnteringContext whether we are entering the scope of the
2168/// nested-name-specifier.
2169///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002170/// \param AllowDestructorName whether we allow parsing of a destructor name.
2171///
2172/// \param AllowConstructorName whether we allow parsing a constructor name.
2173///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00002174/// \param ObjectType if this unqualified-id occurs within a member access
2175/// expression, the type of the base object whose member is being accessed.
2176///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002177/// \param Result on a successful parse, contains the parsed unqualified-id.
2178///
2179/// \returns true if parsing fails, false otherwise.
2180bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2181 bool AllowDestructorName,
2182 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002183 ParsedType ObjectType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002184 SourceLocation& TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002185 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00002186
2187 // Handle 'A::template B'. This is for template-ids which have not
2188 // already been annotated by ParseOptionalCXXScopeSpecifier().
2189 bool TemplateSpecified = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002190 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002191 (ObjectType || SS.isSet())) {
2192 TemplateSpecified = true;
2193 TemplateKWLoc = ConsumeToken();
2194 }
2195
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002196 // unqualified-id:
2197 // identifier
2198 // template-id (when it hasn't already been annotated)
2199 if (Tok.is(tok::identifier)) {
2200 // Consume the identifier.
2201 IdentifierInfo *Id = Tok.getIdentifierInfo();
2202 SourceLocation IdLoc = ConsumeToken();
2203
David Blaikie4e4d0842012-03-11 07:00:24 +00002204 if (!getLangOpts().CPlusPlus) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002205 // If we're not in C++, only identifiers matter. Record the
2206 // identifier and return.
2207 Result.setIdentifier(Id, IdLoc);
2208 return false;
2209 }
2210
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002211 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002212 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002213 // We have parsed a constructor name.
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002214 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2215 &SS, false, false,
2216 ParsedType(),
2217 /*IsCtorOrDtorName=*/true,
2218 /*NonTrivialTypeSourceInfo=*/true);
2219 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002220 } else {
2221 // We have parsed an identifier.
2222 Result.setIdentifier(Id, IdLoc);
2223 }
2224
2225 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00002226 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002227 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2228 EnteringContext, ObjectType,
2229 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002230
2231 return false;
2232 }
2233
2234 // unqualified-id:
2235 // template-id (already parsed and annotated)
2236 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002237 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002238
2239 // If the template-name names the current class, then this is a constructor
2240 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002241 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002242 if (SS.isSet()) {
2243 // C++ [class.qual]p2 specifies that a qualified template-name
2244 // is taken as the constructor name where a constructor can be
2245 // declared. Thus, the template arguments are extraneous, so
2246 // complain about them and remove them entirely.
2247 Diag(TemplateId->TemplateNameLoc,
2248 diag::err_out_of_line_constructor_template_id)
2249 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00002250 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002251 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002252 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2253 TemplateId->TemplateNameLoc,
2254 getCurScope(),
2255 &SS, false, false,
2256 ParsedType(),
2257 /*IsCtorOrDtorName=*/true,
2258 /*NontrivialTypeSourceInfo=*/true);
2259 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002260 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002261 ConsumeToken();
2262 return false;
2263 }
2264
2265 Result.setConstructorTemplateId(TemplateId);
2266 ConsumeToken();
2267 return false;
2268 }
2269
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002270 // We have already parsed a template-id; consume the annotation token as
2271 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002272 Result.setTemplateId(TemplateId);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002273 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002274 ConsumeToken();
2275 return false;
2276 }
2277
2278 // unqualified-id:
2279 // operator-function-id
2280 // conversion-function-id
2281 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002282 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002283 return true;
2284
Sean Hunte6252d12009-11-28 08:58:14 +00002285 // If we have an operator-function-id or a literal-operator-id and the next
2286 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002287 //
2288 // template-id:
2289 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00002290 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2291 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002292 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002293 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2294 0, SourceLocation(),
2295 EnteringContext, ObjectType,
2296 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002297
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002298 return false;
2299 }
2300
David Blaikie4e4d0842012-03-11 07:00:24 +00002301 if (getLangOpts().CPlusPlus &&
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002302 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002303 // C++ [expr.unary.op]p10:
2304 // There is an ambiguity in the unary-expression ~X(), where X is a
2305 // class-name. The ambiguity is resolved in favor of treating ~ as a
2306 // unary complement rather than treating ~X as referring to a destructor.
2307
2308 // Parse the '~'.
2309 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00002310
2311 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2312 DeclSpec DS(AttrFactory);
2313 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2314 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2315 Result.setDestructorName(TildeLoc, Type, EndLoc);
2316 return false;
2317 }
2318 return true;
2319 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002320
2321 // Parse the class-name.
2322 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00002323 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002324 return true;
2325 }
2326
2327 // Parse the class-name (or template-name in a simple-template-id).
2328 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2329 SourceLocation ClassNameLoc = ConsumeToken();
2330
Douglas Gregor0278e122010-05-05 05:58:24 +00002331 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002332 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002333 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2334 ClassName, ClassNameLoc,
2335 EnteringContext, ObjectType,
2336 Result, TemplateSpecified);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002337 }
2338
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002339 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002340 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2341 ClassNameLoc, getCurScope(),
2342 SS, ObjectType,
2343 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002344 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002345 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002346
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002347 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002348 return false;
2349 }
2350
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002351 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikie4e4d0842012-03-11 07:00:24 +00002352 << getLangOpts().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002353 return true;
2354}
2355
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002356/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2357/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002358///
Chris Lattner59232d32009-01-04 21:25:24 +00002359/// This method is called to parse the new expression after the optional :: has
2360/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2361/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002362///
2363/// new-expression:
2364/// '::'[opt] 'new' new-placement[opt] new-type-id
2365/// new-initializer[opt]
2366/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2367/// new-initializer[opt]
2368///
2369/// new-placement:
2370/// '(' expression-list ')'
2371///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002372/// new-type-id:
2373/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002374/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002375///
2376/// new-declarator:
2377/// ptr-operator new-declarator[opt]
2378/// direct-new-declarator
2379///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002380/// new-initializer:
2381/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002382/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002383///
John McCall60d7b3a2010-08-24 06:29:42 +00002384ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002385Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2386 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2387 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002388
2389 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2390 // second form of new-expression. It can't be a new-type-id.
2391
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002392 ExprVector PlacementArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002393 SourceLocation PlacementLParen, PlacementRParen;
2394
Douglas Gregor4bd40312010-07-13 15:54:32 +00002395 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002396 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002397 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002398 if (Tok.is(tok::l_paren)) {
2399 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002400 BalancedDelimiterTracker T(*this, tok::l_paren);
2401 T.consumeOpen();
2402 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002403 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2404 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002405 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002406 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002407
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002408 T.consumeClose();
2409 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002410 if (PlacementRParen.isInvalid()) {
2411 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002412 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002413 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002414
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002415 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002416 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002417 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002418 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002419 } else {
2420 // We still need the type.
2421 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002422 BalancedDelimiterTracker T(*this, tok::l_paren);
2423 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002424 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002425 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002426 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002427 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002428 T.consumeClose();
2429 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002430 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002431 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002432 if (ParseCXXTypeSpecifierSeq(DS))
2433 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002434 else {
2435 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002436 ParseDeclaratorInternal(DeclaratorInfo,
2437 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002438 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002439 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002440 }
2441 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002442 // A new-type-id is a simplified type-id, where essentially the
2443 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002444 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002445 if (ParseCXXTypeSpecifierSeq(DS))
2446 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002447 else {
2448 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002449 ParseDeclaratorInternal(DeclaratorInfo,
2450 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002451 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002452 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002453 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002454 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002455 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002456 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002457
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002458 ExprResult Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002459
2460 if (Tok.is(tok::l_paren)) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002461 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002462 ExprVector ConstructorArgs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002463 BalancedDelimiterTracker T(*this, tok::l_paren);
2464 T.consumeOpen();
2465 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002466 if (Tok.isNot(tok::r_paren)) {
2467 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002468 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2469 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002470 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002471 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002472 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002473 T.consumeClose();
2474 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002475 if (ConstructorRParen.isInvalid()) {
2476 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002477 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002478 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002479 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2480 ConstructorRParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002481 ConstructorArgs);
Richard Smith80ad52f2013-01-02 11:42:31 +00002482 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00002483 Diag(Tok.getLocation(),
2484 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002485 Initializer = ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002486 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002487 if (Initializer.isInvalid())
2488 return Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002489
Sebastian Redlf53597f2009-03-15 17:47:39 +00002490 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002491 PlacementArgs, PlacementRParen,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002492 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002493}
2494
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002495/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2496/// passed to ParseDeclaratorInternal.
2497///
2498/// direct-new-declarator:
2499/// '[' expression ']'
2500/// direct-new-declarator '[' constant-expression ']'
2501///
Chris Lattner59232d32009-01-04 21:25:24 +00002502void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002503 // Parse the array dimensions.
2504 bool first = true;
2505 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00002506 // An array-size expression can't start with a lambda.
2507 if (CheckProhibitedCXX11Attribute())
2508 continue;
2509
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002510 BalancedDelimiterTracker T(*this, tok::l_square);
2511 T.consumeOpen();
2512
John McCall60d7b3a2010-08-24 06:29:42 +00002513 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002514 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002515 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002516 // Recover
2517 SkipUntil(tok::r_square);
2518 return;
2519 }
2520 first = false;
2521
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002522 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002523
Bill Wendlingad017fa2012-12-20 19:22:21 +00002524 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith6ee326a2012-04-10 01:32:12 +00002525 ParsedAttributes Attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002526 MaybeParseCXX11Attributes(Attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00002527
John McCall0b7e6782011-03-24 11:26:52 +00002528 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002529 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002530 Size.release(),
2531 T.getOpenLocation(),
2532 T.getCloseLocation()),
Richard Smith6ee326a2012-04-10 01:32:12 +00002533 Attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002534
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002535 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002536 return;
2537 }
2538}
2539
2540/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2541/// This ambiguity appears in the syntax of the C++ new operator.
2542///
2543/// new-expression:
2544/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2545/// new-initializer[opt]
2546///
2547/// new-placement:
2548/// '(' expression-list ')'
2549///
John McCallca0408f2010-08-23 06:44:23 +00002550bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002551 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002552 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002553 // The '(' was already consumed.
2554 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002555 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002556 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002557 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002558 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002559 }
2560
2561 // It's not a type, it has to be an expression list.
2562 // Discard the comma locations - ActOnCXXNew has enough parameters.
2563 CommaLocsTy CommaLocs;
2564 return ParseExpressionList(PlacementArgs, CommaLocs);
2565}
2566
2567/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2568/// to free memory allocated by new.
2569///
Chris Lattner59232d32009-01-04 21:25:24 +00002570/// This method is called to parse the 'delete' expression after the optional
2571/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2572/// and "Start" is its location. Otherwise, "Start" is the location of the
2573/// 'delete' token.
2574///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002575/// delete-expression:
2576/// '::'[opt] 'delete' cast-expression
2577/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002578ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002579Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2580 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2581 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002582
2583 // Array delete?
2584 bool ArrayDelete = false;
Richard Smith6ee326a2012-04-10 01:32:12 +00002585 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith950435c2012-08-09 19:01:51 +00002586 // C++11 [expr.delete]p1:
2587 // Whenever the delete keyword is followed by empty square brackets, it
2588 // shall be interpreted as [array delete].
2589 // [Footnote: A lambda expression with a lambda-introducer that consists
2590 // of empty square brackets can follow the delete keyword if
2591 // the lambda expression is enclosed in parentheses.]
2592 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2593 // lambda-introducer.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002594 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002595 BalancedDelimiterTracker T(*this, tok::l_square);
2596
2597 T.consumeOpen();
2598 T.consumeClose();
2599 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002600 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002601 }
2602
John McCall60d7b3a2010-08-24 06:29:42 +00002603 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002604 if (Operand.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002605 return Operand;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002606
John McCall9ae2f072010-08-23 23:25:46 +00002607 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002608}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002609
Mike Stump1eb44332009-09-09 15:08:12 +00002610static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002611 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002612 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002613 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Joao Matos9ef98752013-03-27 01:34:16 +00002614 case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002615 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002616 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002617 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Joao Matos9ef98752013-03-27 01:34:16 +00002618 case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002619 case tok::kw___has_trivial_constructor:
2620 return UTT_HasTrivialDefaultConstructor;
Joao Matos9ef98752013-03-27 01:34:16 +00002621 case tok::kw___has_trivial_move_constructor:
2622 return UTT_HasTrivialMoveConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002623 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002624 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2625 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2626 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002627 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2628 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002629 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002630 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2631 case tok::kw___is_compound: return UTT_IsCompound;
2632 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002633 case tok::kw___is_empty: return UTT_IsEmpty;
2634 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002635 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002636 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2637 case tok::kw___is_function: return UTT_IsFunction;
2638 case tok::kw___is_fundamental: return UTT_IsFundamental;
2639 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallea30e2f2012-09-25 07:32:49 +00002640 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002641 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2642 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2643 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2644 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2645 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002646 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002647 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002648 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002649 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002650 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002651 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002652 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2653 case tok::kw___is_scalar: return UTT_IsScalar;
2654 case tok::kw___is_signed: return UTT_IsSigned;
2655 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2656 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002657 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002658 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002659 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2660 case tok::kw___is_void: return UTT_IsVoid;
2661 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002662 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002663}
2664
2665static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2666 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002667 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002668 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002669 case tok::kw___is_convertible: return BTT_IsConvertible;
2670 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002671 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002672 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002673 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002674 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002675}
2676
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002677static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2678 switch (kind) {
2679 default: llvm_unreachable("Not a known type trait");
2680 case tok::kw___is_trivially_constructible:
2681 return TT_IsTriviallyConstructible;
2682 }
2683}
2684
John Wiegley21ff2e52011-04-28 00:16:57 +00002685static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2686 switch(kind) {
2687 default: llvm_unreachable("Not a known binary type trait");
2688 case tok::kw___array_rank: return ATT_ArrayRank;
2689 case tok::kw___array_extent: return ATT_ArrayExtent;
2690 }
2691}
2692
John Wiegley55262202011-04-25 06:54:41 +00002693static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2694 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002695 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002696 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2697 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2698 }
2699}
2700
Sebastian Redl64b45f72009-01-05 20:52:13 +00002701/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2702/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2703/// templates.
2704///
2705/// primary-expression:
2706/// [GNU] unary-type-trait '(' type-id ')'
2707///
John McCall60d7b3a2010-08-24 06:29:42 +00002708ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002709 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2710 SourceLocation Loc = ConsumeToken();
2711
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002712 BalancedDelimiterTracker T(*this, tok::l_paren);
2713 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002714 return ExprError();
2715
2716 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2717 // there will be cryptic errors about mismatched parentheses and missing
2718 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002719 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002720
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002721 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002722
Douglas Gregor809070a2009-02-18 17:45:20 +00002723 if (Ty.isInvalid())
2724 return ExprError();
2725
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002726 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002727}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002728
Francois Pichet6ad6f282010-12-07 00:08:36 +00002729/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2730/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2731/// templates.
2732///
2733/// primary-expression:
2734/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2735///
2736ExprResult Parser::ParseBinaryTypeTrait() {
2737 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2738 SourceLocation Loc = ConsumeToken();
2739
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002740 BalancedDelimiterTracker T(*this, tok::l_paren);
2741 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002742 return ExprError();
2743
2744 TypeResult LhsTy = ParseTypeName();
2745 if (LhsTy.isInvalid()) {
2746 SkipUntil(tok::r_paren);
2747 return ExprError();
2748 }
2749
2750 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2751 SkipUntil(tok::r_paren);
2752 return ExprError();
2753 }
2754
2755 TypeResult RhsTy = ParseTypeName();
2756 if (RhsTy.isInvalid()) {
2757 SkipUntil(tok::r_paren);
2758 return ExprError();
2759 }
2760
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002761 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002762
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002763 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2764 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002765}
2766
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002767/// \brief Parse the built-in type-trait pseudo-functions that allow
2768/// implementation of the TR1/C++11 type traits templates.
2769///
2770/// primary-expression:
2771/// type-trait '(' type-id-seq ')'
2772///
2773/// type-id-seq:
2774/// type-id ...[opt] type-id-seq[opt]
2775///
2776ExprResult Parser::ParseTypeTrait() {
2777 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2778 SourceLocation Loc = ConsumeToken();
2779
2780 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2781 if (Parens.expectAndConsume(diag::err_expected_lparen))
2782 return ExprError();
2783
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002784 SmallVector<ParsedType, 2> Args;
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002785 do {
2786 // Parse the next type.
2787 TypeResult Ty = ParseTypeName();
2788 if (Ty.isInvalid()) {
2789 Parens.skipToEnd();
2790 return ExprError();
2791 }
2792
2793 // Parse the ellipsis, if present.
2794 if (Tok.is(tok::ellipsis)) {
2795 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2796 if (Ty.isInvalid()) {
2797 Parens.skipToEnd();
2798 return ExprError();
2799 }
2800 }
2801
2802 // Add this type to the list of arguments.
2803 Args.push_back(Ty.get());
2804
2805 if (Tok.is(tok::comma)) {
2806 ConsumeToken();
2807 continue;
2808 }
2809
2810 break;
2811 } while (true);
2812
2813 if (Parens.consumeClose())
2814 return ExprError();
2815
2816 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2817}
2818
John Wiegley21ff2e52011-04-28 00:16:57 +00002819/// ParseArrayTypeTrait - Parse the built-in array type-trait
2820/// pseudo-functions.
2821///
2822/// primary-expression:
2823/// [Embarcadero] '__array_rank' '(' type-id ')'
2824/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2825///
2826ExprResult Parser::ParseArrayTypeTrait() {
2827 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2828 SourceLocation Loc = ConsumeToken();
2829
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002830 BalancedDelimiterTracker T(*this, tok::l_paren);
2831 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002832 return ExprError();
2833
2834 TypeResult Ty = ParseTypeName();
2835 if (Ty.isInvalid()) {
2836 SkipUntil(tok::comma);
2837 SkipUntil(tok::r_paren);
2838 return ExprError();
2839 }
2840
2841 switch (ATT) {
2842 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002843 T.consumeClose();
2844 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2845 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002846 }
2847 case ATT_ArrayExtent: {
2848 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2849 SkipUntil(tok::r_paren);
2850 return ExprError();
2851 }
2852
2853 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002854 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002855
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002856 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2857 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002858 }
John Wiegley21ff2e52011-04-28 00:16:57 +00002859 }
David Blaikie30263482012-01-20 21:50:17 +00002860 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley21ff2e52011-04-28 00:16:57 +00002861}
2862
John Wiegley55262202011-04-25 06:54:41 +00002863/// ParseExpressionTrait - Parse built-in expression-trait
2864/// pseudo-functions like __is_lvalue_expr( xxx ).
2865///
2866/// primary-expression:
2867/// [Embarcadero] expression-trait '(' expression ')'
2868///
2869ExprResult Parser::ParseExpressionTrait() {
2870 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2871 SourceLocation Loc = ConsumeToken();
2872
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002873 BalancedDelimiterTracker T(*this, tok::l_paren);
2874 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002875 return ExprError();
2876
2877 ExprResult Expr = ParseExpression();
2878
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002879 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002880
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002881 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2882 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002883}
2884
2885
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002886/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2887/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2888/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002889ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002890Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002891 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002892 BalancedDelimiterTracker &Tracker) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002893 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002894 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2895 assert(isTypeIdInParens() && "Not a type-id!");
2896
John McCall60d7b3a2010-08-24 06:29:42 +00002897 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002898 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002899
2900 // We need to disambiguate a very ugly part of the C++ syntax:
2901 //
2902 // (T())x; - type-id
2903 // (T())*x; - type-id
2904 // (T())/x; - expression
2905 // (T()); - expression
2906 //
2907 // The bad news is that we cannot use the specialized tentative parser, since
2908 // it can only verify that the thing inside the parens can be parsed as
2909 // type-id, it is not useful for determining the context past the parens.
2910 //
2911 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002912 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002913 //
2914 // It uses a scheme similar to parsing inline methods. The parenthesized
2915 // tokens are cached, the context that follows is determined (possibly by
2916 // parsing a cast-expression), and then we re-introduce the cached tokens
2917 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002918
Mike Stump1eb44332009-09-09 15:08:12 +00002919 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002920 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002921
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002922 // Store the tokens of the parentheses. We will parse them after we determine
2923 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002924 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002925 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002926 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002927 return ExprError();
2928 }
2929
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002930 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002931 ParseAs = CompoundLiteral;
2932 } else {
2933 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002934 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2935 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2936 NotCastExpr = true;
2937 } else {
2938 // Try parsing the cast-expression that may follow.
2939 // If it is not a cast-expression, NotCastExpr will be true and no token
2940 // will be consumed.
2941 Result = ParseCastExpression(false/*isUnaryExpression*/,
2942 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002943 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002944 // type-id has priority.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002945 IsTypeCast);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002946 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002947
2948 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2949 // an expression.
2950 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002951 }
2952
Mike Stump1eb44332009-09-09 15:08:12 +00002953 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002954 Toks.push_back(Tok);
2955 // Re-enter the stored parenthesized tokens into the token stream, so we may
2956 // parse them now.
2957 PP.EnterTokenStream(Toks.data(), Toks.size(),
2958 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2959 // Drop the current token and bring the first cached one. It's the same token
2960 // as when we entered this function.
2961 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002962
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002963 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002964 // Parse the type declarator.
2965 DeclSpec DS(AttrFactory);
2966 ParseSpecifierQualifierList(DS);
2967 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2968 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002969
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002970 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002971 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002972
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002973 if (ParseAs == CompoundLiteral) {
2974 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002975 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002976 return ParseCompoundLiteralExpression(Ty.get(),
2977 Tracker.getOpenLocation(),
2978 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002979 }
Mike Stump1eb44332009-09-09 15:08:12 +00002980
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002981 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2982 assert(ParseAs == CastExpr);
2983
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002984 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002985 return ExprError();
2986
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002987 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002988 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002989 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2990 DeclaratorInfo, CastTy,
2991 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002992 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002993 }
Mike Stump1eb44332009-09-09 15:08:12 +00002994
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002995 // Not a compound literal, and not followed by a cast-expression.
2996 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002997
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002998 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002999 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003000 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003001 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
3002 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003003
3004 // Match the ')'.
3005 if (Result.isInvalid()) {
3006 SkipUntil(tok::r_paren);
3007 return ExprError();
3008 }
Mike Stump1eb44332009-09-09 15:08:12 +00003009
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003010 Tracker.consumeClose();
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00003011 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003012}