blob: 0862af323e001d838ca6925e7e83f9f79b4bc926 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000019#include "llvm/Support/ErrorHandling.h"
20
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Richard Smithea698b32011-04-14 21:45:45 +000023static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
24 switch (Kind) {
25 case tok::kw_template: return 0;
26 case tok::kw_const_cast: return 1;
27 case tok::kw_dynamic_cast: return 2;
28 case tok::kw_reinterpret_cast: return 3;
29 case tok::kw_static_cast: return 4;
30 default:
31 assert(0 && "Unknown type for digraph error message.");
32 return -1;
33 }
34}
35
36// Are the two tokens adjacent in the same source file?
37static bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
38 SourceManager &SM = PP.getSourceManager();
39 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
40 SourceLocation FirstEnd = FirstLoc.getFileLocWithOffset(First.getLength());
41 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
42}
43
44// Suggest fixit for "<::" after a cast.
45static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
46 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
47 // Pull '<:' and ':' off token stream.
48 if (!AtDigraph)
49 PP.Lex(DigraphToken);
50 PP.Lex(ColonToken);
51
52 SourceRange Range;
53 Range.setBegin(DigraphToken.getLocation());
54 Range.setEnd(ColonToken.getLocation());
55 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
56 << SelectDigraphErrorMessage(Kind)
57 << FixItHint::CreateReplacement(Range, "< ::");
58
59 // Update token information to reflect their change in token type.
60 ColonToken.setKind(tok::coloncolon);
61 ColonToken.setLocation(ColonToken.getLocation().getFileLocWithOffset(-1));
62 ColonToken.setLength(2);
63 DigraphToken.setKind(tok::less);
64 DigraphToken.setLength(1);
65
66 // Push new tokens back to token stream.
67 PP.EnterToken(ColonToken);
68 if (!AtDigraph)
69 PP.EnterToken(DigraphToken);
70}
71
Mike Stump1eb44332009-09-09 15:08:12 +000072/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073///
74/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000075/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000077///
78/// '::'[opt] nested-name-specifier
79/// '::'
80///
81/// nested-name-specifier:
82/// type-name '::'
83/// namespace-name '::'
84/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000085/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000086///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000087///
Mike Stump1eb44332009-09-09 15:08:12 +000088/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000089/// nested-name-specifier (or empty)
90///
Mike Stump1eb44332009-09-09 15:08:12 +000091/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000092/// the "." or "->" of a member access expression, this parameter provides the
93/// type of the object whose members are being accessed.
94///
95/// \param EnteringContext whether we will be entering into the context of
96/// the nested-name-specifier after parsing it.
97///
Douglas Gregord4dca082010-02-24 18:44:31 +000098/// \param MayBePseudoDestructor When non-NULL, points to a flag that
99/// indicates whether this nested-name-specifier may be part of a
100/// pseudo-destructor name. In this case, the flag will be set false
101/// if we don't actually end up parsing a destructor name. Moreorover,
102/// if we do end up determining that we are parsing a destructor name,
103/// the last component of the nested-name-specifier is not parsed as
104/// part of the scope specifier.
105
Douglas Gregorb10cd042010-02-21 18:36:56 +0000106/// member access expression, e.g., the \p T:: in \p p->T::m.
107///
John McCall9ba61662010-02-26 08:45:28 +0000108/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000109bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000110 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000111 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000112 bool *MayBePseudoDestructor,
113 bool IsTypename) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000114 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000115 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000117 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +0000118 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
119 Tok.getAnnotationRange(),
120 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000121 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000122 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000123 }
Chris Lattnere607e802009-01-04 21:14:15 +0000124
Douglas Gregor39a8de12009-02-25 19:37:18 +0000125 bool HasScopeSpecifier = false;
126
Chris Lattner5b454732009-01-05 03:55:46 +0000127 if (Tok.is(tok::coloncolon)) {
128 // ::new and ::delete aren't nested-name-specifiers.
129 tok::TokenKind NextKind = NextToken().getKind();
130 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
131 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattner55a7cef2009-01-05 00:13:00 +0000133 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000134 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
135 return true;
136
Douglas Gregor39a8de12009-02-25 19:37:18 +0000137 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000138 }
139
Douglas Gregord4dca082010-02-24 18:44:31 +0000140 bool CheckForDestructor = false;
141 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
142 CheckForDestructor = true;
143 *MayBePseudoDestructor = false;
144 }
145
Douglas Gregor39a8de12009-02-25 19:37:18 +0000146 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000147 if (HasScopeSpecifier) {
148 // C++ [basic.lookup.classref]p5:
149 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000150 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000151 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000152 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000153 // the class-name-or-namespace-name is looked up in global scope as a
154 // class-name or namespace-name.
155 //
156 // To implement this, we clear out the object type as soon as we've
157 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000158 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000159
160 if (Tok.is(tok::code_completion)) {
161 // Code completion for a nested-name-specifier, where the code
162 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000163 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000164 SourceLocation ccLoc = ConsumeCodeCompletionToken();
165 // Include code completion token into the range of the scope otherwise
166 // when we try to annotate the scope tokens the dangling code completion
167 // token will cause assertion in
168 // Preprocessor::AnnotatePreviousCachedTokens.
169 SS.setEndLoc(ccLoc);
Douglas Gregor81b747b2009-09-17 21:32:03 +0000170 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000171 }
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Douglas Gregor39a8de12009-02-25 19:37:18 +0000173 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000174 // nested-name-specifier 'template'[opt] simple-template-id '::'
175
176 // Parse the optional 'template' keyword, then make sure we have
177 // 'identifier <' after it.
178 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000179 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000180 // nested-name-specifier, since they aren't allowed to start with
181 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000182 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000183 break;
184
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000185 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000186 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000187
188 UnqualifiedId TemplateName;
189 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000190 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000191 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000192 ConsumeToken();
193 } else if (Tok.is(tok::kw_operator)) {
194 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000195 TemplateName)) {
196 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000197 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000198 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000199
Sean Hunte6252d12009-11-28 08:58:14 +0000200 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
201 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000202 Diag(TemplateName.getSourceRange().getBegin(),
203 diag::err_id_after_template_in_nested_name_spec)
204 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000205 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000206 break;
207 }
208 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000209 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000210 break;
211 }
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000213 // If the next token is not '<', we have a qualified-id that refers
214 // to a template name, such as T::template apply, but is not a
215 // template-id.
216 if (Tok.isNot(tok::less)) {
217 TPA.Revert();
218 break;
219 }
220
221 // Commit to parsing the template-id.
222 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000223 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000224 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000225 TemplateKWLoc,
226 SS,
227 TemplateName,
228 ObjectType,
229 EnteringContext,
230 Template)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000231 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000232 TemplateKWLoc, false))
233 return true;
234 } else
John McCall9ba61662010-02-26 08:45:28 +0000235 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattner77cf72a2009-06-26 03:47:46 +0000237 continue;
238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Douglas Gregor39a8de12009-02-25 19:37:18 +0000240 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000241 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000242 //
243 // simple-template-id '::'
244 //
245 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000246 // right kind (it should name a type or be dependent), and then
247 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000248 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000249 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord4dca082010-02-24 18:44:31 +0000250 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
251 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000252 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000253 }
254
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000255 // Consume the template-id token.
256 ConsumeToken();
257
258 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
259 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000261 if (!HasScopeSpecifier)
262 HasScopeSpecifier = true;
263
264 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
265 TemplateId->getTemplateArgs(),
266 TemplateId->NumArgs);
267
268 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
269 /*FIXME:*/SourceLocation(),
270 SS,
271 TemplateId->Template,
272 TemplateId->TemplateNameLoc,
273 TemplateId->LAngleLoc,
274 TemplateArgsPtr,
275 TemplateId->RAngleLoc,
276 CCLoc,
277 EnteringContext)) {
278 SourceLocation StartLoc
279 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
280 : TemplateId->TemplateNameLoc;
281 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000282 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000283
284 // If we are caching tokens we will process the TemplateId again,
285 // otherwise destroy it.
286 if (!PP.isBacktrackEnabled())
287 TemplateId->Destroy();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000288 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000289 }
290
Chris Lattner5c7f7862009-06-26 03:52:38 +0000291
292 // The rest of the nested-name-specifier possibilities start with
293 // tok::identifier.
294 if (Tok.isNot(tok::identifier))
295 break;
296
297 IdentifierInfo &II = *Tok.getIdentifierInfo();
298
299 // nested-name-specifier:
300 // type-name '::'
301 // namespace-name '::'
302 // nested-name-specifier identifier '::'
303 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000304
305 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
306 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000307 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000308 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
309 Tok.getLocation(),
310 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000311 EnteringContext) &&
312 // If the token after the colon isn't an identifier, it's still an
313 // error, but they probably meant something else strange so don't
314 // recover like this.
315 PP.LookAhead(1).is(tok::identifier)) {
316 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000317 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000318
319 // Recover as if the user wrote '::'.
320 Next.setKind(tok::coloncolon);
321 }
Chris Lattner46646492009-12-07 01:36:53 +0000322 }
323
Chris Lattner5c7f7862009-06-26 03:52:38 +0000324 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000325 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000326 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000327 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000328 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000329 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000330 }
331
Chris Lattner5c7f7862009-06-26 03:52:38 +0000332 // We have an identifier followed by a '::'. Lookup this name
333 // as the name in a nested-name-specifier.
334 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000335 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
336 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000337 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000339 HasScopeSpecifier = true;
340 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
341 ObjectType, EnteringContext, SS))
342 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
343
Chris Lattner5c7f7862009-06-26 03:52:38 +0000344 continue;
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Richard Smithea698b32011-04-14 21:45:45 +0000347 // Check for '<::' which should be '< ::' instead of '[:' when following
348 // a template name.
349 if (Next.is(tok::l_square) && Next.getLength() == 2) {
350 Token SecondToken = GetLookAheadToken(2);
351 if (SecondToken.is(tok::colon) &&
352 AreTokensAdjacent(PP, Next, SecondToken)) {
353 TemplateTy Template;
354 UnqualifiedId TemplateName;
355 TemplateName.setIdentifier(&II, Tok.getLocation());
356 bool MemberOfUnknownSpecialization;
357 if (Actions.isTemplateName(getCurScope(), SS,
358 /*hasTemplateKeyword=*/false,
359 TemplateName,
360 ObjectType,
361 EnteringContext,
362 Template,
363 MemberOfUnknownSpecialization)) {
364 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
365 /*AtDigraph*/false);
366 }
367 }
368 }
369
Chris Lattner5c7f7862009-06-26 03:52:38 +0000370 // nested-name-specifier:
371 // type-name '<'
372 if (Next.is(tok::less)) {
373 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000374 UnqualifiedId TemplateName;
375 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000376 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000377 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000378 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000379 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000380 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000381 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000382 Template,
383 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000384 // We have found a template name, so annotate this this token
385 // with a template-id annotation. We do not permit the
386 // template-id to be translated into a type annotation,
387 // because some clients (e.g., the parsing of class template
388 // specializations) still want to see the original template-id
389 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000390 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000391 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000392 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000393 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000394 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000395 }
396
397 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000398 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000399 // We have something like t::getAs<T>, where getAs is a
400 // member of an unknown specialization. However, this will only
401 // parse correctly as a template, so suggest the keyword 'template'
402 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000403 unsigned DiagID = diag::err_missing_dependent_template_keyword;
404 if (getLang().Microsoft)
Francois Pichetcf320c62011-04-22 08:25:24 +0000405 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000406
407 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000408 << II.getName()
409 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
410
Douglas Gregord6ab2322010-06-16 23:00:59 +0000411 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000412 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000413 Tok.getLocation(), SS,
414 TemplateName, ObjectType,
415 EnteringContext, Template)) {
416 // Consume the identifier.
417 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000418 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000419 SourceLocation(), false))
420 return true;
421 }
422 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000423 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000424
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000425 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000426 }
427 }
428
Douglas Gregor39a8de12009-02-25 19:37:18 +0000429 // We don't have any tokens that form the beginning of a
430 // nested-name-specifier, so we're done.
431 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Douglas Gregord4dca082010-02-24 18:44:31 +0000434 // Even if we didn't see any pieces of a nested-name-specifier, we
435 // still check whether there is a tilde in this position, which
436 // indicates a potential pseudo-destructor.
437 if (CheckForDestructor && Tok.is(tok::tilde))
438 *MayBePseudoDestructor = true;
439
John McCall9ba61662010-02-26 08:45:28 +0000440 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000441}
442
443/// ParseCXXIdExpression - Handle id-expression.
444///
445/// id-expression:
446/// unqualified-id
447/// qualified-id
448///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000449/// qualified-id:
450/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
451/// '::' identifier
452/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000453/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000454///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000455/// NOTE: The standard specifies that, for qualified-id, the parser does not
456/// expect:
457///
458/// '::' conversion-function-id
459/// '::' '~' class-name
460///
461/// This may cause a slight inconsistency on diagnostics:
462///
463/// class C {};
464/// namespace A {}
465/// void f() {
466/// :: A :: ~ C(); // Some Sema error about using destructor with a
467/// // namespace.
468/// :: ~ C(); // Some Parser error like 'unexpected ~'.
469/// }
470///
471/// We simplify the parser a bit and make it work like:
472///
473/// qualified-id:
474/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
475/// '::' unqualified-id
476///
477/// That way Sema can handle and report similar errors for namespaces and the
478/// global scope.
479///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000480/// The isAddressOfOperand parameter indicates that this id-expression is a
481/// direct operand of the address-of operator. This is, besides member contexts,
482/// the only place where a qualified-id naming a non-static class member may
483/// appear.
484///
John McCall60d7b3a2010-08-24 06:29:42 +0000485ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000486 // qualified-id:
487 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
488 // '::' unqualified-id
489 //
490 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000491 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000492
493 UnqualifiedId Name;
494 if (ParseUnqualifiedId(SS,
495 /*EnteringContext=*/false,
496 /*AllowDestructorName=*/false,
497 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000498 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000499 Name))
500 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000501
502 // This is only the direct operand of an & operator if it is not
503 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000504 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
505 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000506
Douglas Gregor23c94db2010-07-02 17:43:08 +0000507 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000508 isAddressOfOperand);
509
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000510}
511
Reid Spencer5f016e22007-07-11 17:01:13 +0000512/// ParseCXXCasts - This handles the various ways to cast expressions to another
513/// type.
514///
515/// postfix-expression: [C++ 5.2p1]
516/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
517/// 'static_cast' '<' type-name '>' '(' expression ')'
518/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
519/// 'const_cast' '<' type-name '>' '(' expression ')'
520///
John McCall60d7b3a2010-08-24 06:29:42 +0000521ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 tok::TokenKind Kind = Tok.getKind();
523 const char *CastName = 0; // For error messages
524
525 switch (Kind) {
526 default: assert(0 && "Unknown C++ cast!"); abort();
527 case tok::kw_const_cast: CastName = "const_cast"; break;
528 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
529 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
530 case tok::kw_static_cast: CastName = "static_cast"; break;
531 }
532
533 SourceLocation OpLoc = ConsumeToken();
534 SourceLocation LAngleBracketLoc = Tok.getLocation();
535
Richard Smithea698b32011-04-14 21:45:45 +0000536 // Check for "<::" which is parsed as "[:". If found, fix token stream,
537 // diagnose error, suggest fix, and recover parsing.
538 Token Next = NextToken();
539 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
540 AreTokensAdjacent(PP, Tok, Next))
541 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
542
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000544 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000545
Douglas Gregor809070a2009-02-18 17:45:20 +0000546 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 SourceLocation RAngleBracketLoc = Tok.getLocation();
548
Chris Lattner1ab3b962008-11-18 07:48:38 +0000549 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000550 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000551
552 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
553
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000554 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
555 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000556
John McCall60d7b3a2010-08-24 06:29:42 +0000557 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000559 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000560 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000561
Douglas Gregor809070a2009-02-18 17:45:20 +0000562 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000563 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000564 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000565 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000566 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000567
Sebastian Redl20df9b72008-12-11 22:51:44 +0000568 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000569}
570
Sebastian Redlc42e1182008-11-11 11:37:55 +0000571/// ParseCXXTypeid - This handles the C++ typeid expression.
572///
573/// postfix-expression: [C++ 5.2p1]
574/// 'typeid' '(' expression ')'
575/// 'typeid' '(' type-id ')'
576///
John McCall60d7b3a2010-08-24 06:29:42 +0000577ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000578 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
579
580 SourceLocation OpLoc = ConsumeToken();
581 SourceLocation LParenLoc = Tok.getLocation();
582 SourceLocation RParenLoc;
583
584 // typeid expressions are always parenthesized.
585 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
586 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000587 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000588
John McCall60d7b3a2010-08-24 06:29:42 +0000589 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000590
591 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000592 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000593
594 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000595 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000596
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000597 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000598 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000599
600 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000601 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000602 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000603 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000604 // When typeid is applied to an expression other than an lvalue of a
605 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000606 // operand (Clause 5).
607 //
Mike Stump1eb44332009-09-09 15:08:12 +0000608 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000609 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000610 // we the expression is potentially potentially evaluated.
611 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000612 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000613 Result = ParseExpression();
614
615 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000616 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000617 SkipUntil(tok::r_paren);
618 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000619 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
620 if (RParenLoc.isInvalid())
621 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000622
623 // If we are a foo<int> that identifies a single function, resolve it now...
624 Expr* e = Result.get();
625 if (e->getType() == Actions.Context.OverloadTy) {
626 ExprResult er =
627 Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
628 if (er.isUsable())
629 Result = er.release();
630 }
Sebastian Redlc42e1182008-11-11 11:37:55 +0000631 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000632 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000633 }
634 }
635
Sebastian Redl20df9b72008-12-11 22:51:44 +0000636 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000637}
638
Francois Pichet01b7c302010-09-08 12:20:18 +0000639/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
640///
641/// '__uuidof' '(' expression ')'
642/// '__uuidof' '(' type-id ')'
643///
644ExprResult Parser::ParseCXXUuidof() {
645 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
646
647 SourceLocation OpLoc = ConsumeToken();
648 SourceLocation LParenLoc = Tok.getLocation();
649 SourceLocation RParenLoc;
650
651 // __uuidof expressions are always parenthesized.
652 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
653 "__uuidof"))
654 return ExprError();
655
656 ExprResult Result;
657
658 if (isTypeIdInParens()) {
659 TypeResult Ty = ParseTypeName();
660
661 // Match the ')'.
662 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
663
664 if (Ty.isInvalid())
665 return ExprError();
666
667 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
668 Ty.get().getAsOpaquePtr(), RParenLoc);
669 } else {
670 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
671 Result = ParseExpression();
672
673 // Match the ')'.
674 if (Result.isInvalid())
675 SkipUntil(tok::r_paren);
676 else {
677 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
678
679 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
680 Result.release(), RParenLoc);
681 }
682 }
683
684 return move(Result);
685}
686
Douglas Gregord4dca082010-02-24 18:44:31 +0000687/// \brief Parse a C++ pseudo-destructor expression after the base,
688/// . or -> operator, and nested-name-specifier have already been
689/// parsed.
690///
691/// postfix-expression: [C++ 5.2]
692/// postfix-expression . pseudo-destructor-name
693/// postfix-expression -> pseudo-destructor-name
694///
695/// pseudo-destructor-name:
696/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
697/// ::[opt] nested-name-specifier template simple-template-id ::
698/// ~type-name
699/// ::[opt] nested-name-specifier[opt] ~type-name
700///
John McCall60d7b3a2010-08-24 06:29:42 +0000701ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000702Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
703 tok::TokenKind OpKind,
704 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000705 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000706 // We're parsing either a pseudo-destructor-name or a dependent
707 // member access that has the same form as a
708 // pseudo-destructor-name. We parse both in the same way and let
709 // the action model sort them out.
710 //
711 // Note that the ::[opt] nested-name-specifier[opt] has already
712 // been parsed, and if there was a simple-template-id, it has
713 // been coalesced into a template-id annotation token.
714 UnqualifiedId FirstTypeName;
715 SourceLocation CCLoc;
716 if (Tok.is(tok::identifier)) {
717 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
718 ConsumeToken();
719 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
720 CCLoc = ConsumeToken();
721 } else if (Tok.is(tok::annot_template_id)) {
722 FirstTypeName.setTemplateId(
723 (TemplateIdAnnotation *)Tok.getAnnotationValue());
724 ConsumeToken();
725 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
726 CCLoc = ConsumeToken();
727 } else {
728 FirstTypeName.setIdentifier(0, SourceLocation());
729 }
730
731 // Parse the tilde.
732 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
733 SourceLocation TildeLoc = ConsumeToken();
734 if (!Tok.is(tok::identifier)) {
735 Diag(Tok, diag::err_destructor_tilde_identifier);
736 return ExprError();
737 }
738
739 // Parse the second type.
740 UnqualifiedId SecondTypeName;
741 IdentifierInfo *Name = Tok.getIdentifierInfo();
742 SourceLocation NameLoc = ConsumeToken();
743 SecondTypeName.setIdentifier(Name, NameLoc);
744
745 // If there is a '<', the second type name is a template-id. Parse
746 // it as such.
747 if (Tok.is(tok::less) &&
748 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000749 SecondTypeName, /*AssumeTemplateName=*/true,
750 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000751 return ExprError();
752
John McCall9ae2f072010-08-23 23:25:46 +0000753 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
754 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000755 SS, FirstTypeName, CCLoc,
756 TildeLoc, SecondTypeName,
757 Tok.is(tok::l_paren));
758}
759
Reid Spencer5f016e22007-07-11 17:01:13 +0000760/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
761///
762/// boolean-literal: [C++ 2.13.5]
763/// 'true'
764/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000765ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000767 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000768}
Chris Lattner50dd2892008-02-26 00:51:44 +0000769
770/// ParseThrowExpression - This handles the C++ throw expression.
771///
772/// throw-expression: [C++ 15]
773/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000774ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000775 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000776 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000777
Chris Lattner2a2819a2008-04-06 06:02:23 +0000778 // If the current token isn't the start of an assignment-expression,
779 // then the expression is not present. This handles things like:
780 // "C ? throw : (void)42", which is crazy but legal.
781 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
782 case tok::semi:
783 case tok::r_paren:
784 case tok::r_square:
785 case tok::r_brace:
786 case tok::colon:
787 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000788 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000789
Chris Lattner2a2819a2008-04-06 06:02:23 +0000790 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000791 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000792 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000793 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000794 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000795}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000796
797/// ParseCXXThis - This handles the C++ 'this' pointer.
798///
799/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
800/// a non-lvalue expression whose value is the address of the object for which
801/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000802ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000803 assert(Tok.is(tok::kw_this) && "Not 'this'!");
804 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000805 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000806}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000807
808/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
809/// Can be interpreted either as function-style casting ("int(x)")
810/// or class type construction ("ClassType(x,y,z)")
811/// or creation of a value-initialized type ("int()").
812///
813/// postfix-expression: [C++ 5.2p1]
814/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
815/// typename-specifier '(' expression-list[opt] ')' [TODO]
816///
John McCall60d7b3a2010-08-24 06:29:42 +0000817ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000818Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000819 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000820 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000821
822 assert(Tok.is(tok::l_paren) && "Expected '('!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000823 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
824
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000825 SourceLocation LParenLoc = ConsumeParen();
826
Sebastian Redla55e52c2008-11-25 22:21:31 +0000827 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000828 CommaLocsTy CommaLocs;
829
830 if (Tok.isNot(tok::r_paren)) {
831 if (ParseExpressionList(Exprs, CommaLocs)) {
832 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000833 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000834 }
835 }
836
837 // Match the ')'.
838 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
839
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000840 // TypeRep could be null, if it references an invalid typedef.
841 if (!TypeRep)
842 return ExprError();
843
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000844 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
845 "Unexpected number of commas!");
Douglas Gregorab6677e2010-09-08 00:15:04 +0000846 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000847 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000848}
849
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000850/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000851///
852/// condition:
853/// expression
854/// type-specifier-seq declarator '=' assignment-expression
855/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
856/// '=' assignment-expression
857///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000858/// \param ExprResult if the condition was parsed as an expression, the
859/// parsed expression.
860///
861/// \param DeclResult if the condition was parsed as a declaration, the
862/// parsed declaration.
863///
Douglas Gregor586596f2010-05-06 17:25:47 +0000864/// \param Loc The location of the start of the statement that requires this
865/// condition, e.g., the "for" in a for loop.
866///
867/// \param ConvertToBoolean Whether the condition expression should be
868/// converted to a boolean value.
869///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000870/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000871bool Parser::ParseCXXCondition(ExprResult &ExprOut,
872 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000873 SourceLocation Loc,
874 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000875 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000876 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000877 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000878 }
879
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000880 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000881 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000882 ExprOut = ParseExpression(); // expression
883 DeclOut = 0;
884 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000885 return true;
886
887 // If required, convert to a boolean value.
888 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000889 ExprOut
890 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
891 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000892 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000893
894 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +0000895 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000896 ParseSpecifierQualifierList(DS);
897
898 // declarator
899 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
900 ParseDeclarator(DeclaratorInfo);
901
902 // simple-asm-expr[opt]
903 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000904 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000905 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000906 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000907 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000908 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000909 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000910 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000911 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000912 }
913
914 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000915 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000916
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000917 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000918 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000919 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000920 DeclOut = Dcl.get();
921 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000922
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000923 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000924 if (isTokenEqualOrMistypedEqualEqual(
925 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000926 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000927 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000928 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000929 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
930 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000931 } else {
932 // FIXME: C++0x allows a braced-init-list
933 Diag(Tok, diag::err_expected_equal_after_declarator);
934 }
935
Douglas Gregor586596f2010-05-06 17:25:47 +0000936 // FIXME: Build a reference to this declaration? Convert it to bool?
937 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +0000938
939 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +0000940
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000941 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000942}
943
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000944/// \brief Determine whether the current token starts a C++
945/// simple-type-specifier.
946bool Parser::isCXXSimpleTypeSpecifier() const {
947 switch (Tok.getKind()) {
948 case tok::annot_typename:
949 case tok::kw_short:
950 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000951 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000952 case tok::kw_signed:
953 case tok::kw_unsigned:
954 case tok::kw_void:
955 case tok::kw_char:
956 case tok::kw_int:
957 case tok::kw_float:
958 case tok::kw_double:
959 case tok::kw_wchar_t:
960 case tok::kw_char16_t:
961 case tok::kw_char32_t:
962 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +0000963 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000964 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000965 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000966 return true;
967
968 default:
969 break;
970 }
971
972 return false;
973}
974
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000975/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
976/// This should only be called when the current token is known to be part of
977/// simple-type-specifier.
978///
979/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000980/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000981/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
982/// char
983/// wchar_t
984/// bool
985/// short
986/// int
987/// long
988/// signed
989/// unsigned
990/// float
991/// double
992/// void
993/// [GNU] typeof-specifier
994/// [C++0x] auto [TODO]
995///
996/// type-name:
997/// class-name
998/// enum-name
999/// typedef-name
1000///
1001void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1002 DS.SetRangeStart(Tok.getLocation());
1003 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001004 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001005 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001007 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001008 case tok::identifier: // foo::bar
1009 case tok::coloncolon: // ::foo::bar
1010 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001011 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001012 assert(0 && "Not a simple-type-specifier token!");
1013 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +00001014
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001015 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001016 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001017 if (getTypeAnnotation(Tok))
1018 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1019 getTypeAnnotation(Tok));
1020 else
1021 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001022
1023 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1024 ConsumeToken();
1025
1026 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1027 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1028 // Objective-C interface. If we don't have Objective-C or a '<', this is
1029 // just a normal reference to a typedef name.
1030 if (Tok.is(tok::less) && getLang().ObjC1)
1031 ParseObjCProtocolQualifiers(DS);
1032
1033 DS.Finish(Diags, PP);
1034 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001035 }
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001037 // builtin types
1038 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001039 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001040 break;
1041 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001042 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001043 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001044 case tok::kw___int64:
1045 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1046 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001047 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001048 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001049 break;
1050 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001051 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001052 break;
1053 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001054 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001055 break;
1056 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001057 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001058 break;
1059 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001060 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001061 break;
1062 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001063 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001064 break;
1065 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001066 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001067 break;
1068 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001069 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001070 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001071 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001072 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001073 break;
1074 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001075 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001076 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001077 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001078 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001079 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001081 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001082 // GNU typeof support.
1083 case tok::kw_typeof:
1084 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001085 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001086 return;
1087 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001088 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001089 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1090 else
1091 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001092 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001093 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001094}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001095
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001096/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1097/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1098/// e.g., "const short int". Note that the DeclSpec is *not* finished
1099/// by parsing the type-specifier-seq, because these sequences are
1100/// typically followed by some form of declarator. Returns true and
1101/// emits diagnostics if this is not a type-specifier-seq, false
1102/// otherwise.
1103///
1104/// type-specifier-seq: [C++ 8.1]
1105/// type-specifier type-specifier-seq[opt]
1106///
1107bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1108 DS.SetRangeStart(Tok.getLocation());
1109 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001110 unsigned DiagID;
1111 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001112
1113 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001114 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1115 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001116 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001117 return true;
1118 }
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Sebastian Redld9bafa72010-02-03 21:21:43 +00001120 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1121 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1122 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001123
Douglas Gregor396a9f22010-02-24 23:13:13 +00001124 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001125 return false;
1126}
1127
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001128/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1129/// some form.
1130///
1131/// This routine is invoked when a '<' is encountered after an identifier or
1132/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1133/// whether the unqualified-id is actually a template-id. This routine will
1134/// then parse the template arguments and form the appropriate template-id to
1135/// return to the caller.
1136///
1137/// \param SS the nested-name-specifier that precedes this template-id, if
1138/// we're actually parsing a qualified-id.
1139///
1140/// \param Name for constructor and destructor names, this is the actual
1141/// identifier that may be a template-name.
1142///
1143/// \param NameLoc the location of the class-name in a constructor or
1144/// destructor.
1145///
1146/// \param EnteringContext whether we're entering the scope of the
1147/// nested-name-specifier.
1148///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001149/// \param ObjectType if this unqualified-id occurs within a member access
1150/// expression, the type of the base object whose member is being accessed.
1151///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001152/// \param Id as input, describes the template-name or operator-function-id
1153/// that precedes the '<'. If template arguments were parsed successfully,
1154/// will be updated with the template-id.
1155///
Douglas Gregord4dca082010-02-24 18:44:31 +00001156/// \param AssumeTemplateId When true, this routine will assume that the name
1157/// refers to a template without performing name lookup to verify.
1158///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001159/// \returns true if a parse error occurred, false otherwise.
1160bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1161 IdentifierInfo *Name,
1162 SourceLocation NameLoc,
1163 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001164 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001165 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001166 bool AssumeTemplateId,
1167 SourceLocation TemplateKWLoc) {
1168 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1169 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001170
1171 TemplateTy Template;
1172 TemplateNameKind TNK = TNK_Non_template;
1173 switch (Id.getKind()) {
1174 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001175 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001176 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001177 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001178 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001179 Id, ObjectType, EnteringContext,
1180 Template);
1181 if (TNK == TNK_Non_template)
1182 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001183 } else {
1184 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001185 TNK = Actions.isTemplateName(getCurScope(), SS,
1186 TemplateKWLoc.isValid(), Id,
1187 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001188 MemberOfUnknownSpecialization);
1189
1190 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1191 ObjectType && IsTemplateArgumentList()) {
1192 // We have something like t->getAs<T>(), where getAs is a
1193 // member of an unknown specialization. However, this will only
1194 // parse correctly as a template, so suggest the keyword 'template'
1195 // before 'getAs' and treat this as a dependent template name.
1196 std::string Name;
1197 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1198 Name = Id.Identifier->getName();
1199 else {
1200 Name = "operator ";
1201 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1202 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1203 else
1204 Name += Id.Identifier->getName();
1205 }
1206 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1207 << Name
1208 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001209 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001210 SS, Id, ObjectType,
1211 EnteringContext, Template);
1212 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001213 return true;
1214 }
1215 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001216 break;
1217
Douglas Gregor014e88d2009-11-03 23:16:33 +00001218 case UnqualifiedId::IK_ConstructorName: {
1219 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001220 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001221 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001222 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1223 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001224 EnteringContext, Template,
1225 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001226 break;
1227 }
1228
Douglas Gregor014e88d2009-11-03 23:16:33 +00001229 case UnqualifiedId::IK_DestructorName: {
1230 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001231 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001232 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001233 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001234 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001235 TemplateName, ObjectType,
1236 EnteringContext, Template);
1237 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001238 return true;
1239 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001240 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1241 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001242 EnteringContext, Template,
1243 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001244
John McCallb3d87482010-08-24 05:47:05 +00001245 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001246 Diag(NameLoc, diag::err_destructor_template_id)
1247 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001248 return true;
1249 }
1250 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001251 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001252 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001253
1254 default:
1255 return false;
1256 }
1257
1258 if (TNK == TNK_Non_template)
1259 return false;
1260
1261 // Parse the enclosed template argument list.
1262 SourceLocation LAngleLoc, RAngleLoc;
1263 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001264 if (Tok.is(tok::less) &&
1265 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001266 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001267 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001268 RAngleLoc))
1269 return true;
1270
1271 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001272 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1273 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001274 // Form a parsed representation of the template-id to be stored in the
1275 // UnqualifiedId.
1276 TemplateIdAnnotation *TemplateId
1277 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1278
1279 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1280 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001281 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001282 TemplateId->TemplateNameLoc = Id.StartLocation;
1283 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001284 TemplateId->Name = 0;
1285 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1286 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001287 }
1288
Douglas Gregor059101f2011-03-02 00:47:37 +00001289 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001290 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001291 TemplateId->Kind = TNK;
1292 TemplateId->LAngleLoc = LAngleLoc;
1293 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001294 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001295 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001296 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001297 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001298
1299 Id.setTemplateId(TemplateId);
1300 return false;
1301 }
1302
1303 // Bundle the template arguments together.
1304 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001305 TemplateArgs.size());
1306
1307 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001308 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001309 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001310 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001311 RAngleLoc);
1312 if (Type.isInvalid())
1313 return true;
1314
1315 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1316 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1317 else
1318 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1319
1320 return false;
1321}
1322
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001323/// \brief Parse an operator-function-id or conversion-function-id as part
1324/// of a C++ unqualified-id.
1325///
1326/// This routine is responsible only for parsing the operator-function-id or
1327/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001328///
1329/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001330/// operator-function-id: [C++ 13.5]
1331/// 'operator' operator
1332///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001333/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001334/// new delete new[] delete[]
1335/// + - * / % ^ & | ~
1336/// ! = < > += -= *= /= %=
1337/// ^= &= |= << >> >>= <<= == !=
1338/// <= >= && || ++ -- , ->* ->
1339/// () []
1340///
1341/// conversion-function-id: [C++ 12.3.2]
1342/// operator conversion-type-id
1343///
1344/// conversion-type-id:
1345/// type-specifier-seq conversion-declarator[opt]
1346///
1347/// conversion-declarator:
1348/// ptr-operator conversion-declarator[opt]
1349/// \endcode
1350///
1351/// \param The nested-name-specifier that preceded this unqualified-id. If
1352/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1353///
1354/// \param EnteringContext whether we are entering the scope of the
1355/// nested-name-specifier.
1356///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001357/// \param ObjectType if this unqualified-id occurs within a member access
1358/// expression, the type of the base object whose member is being accessed.
1359///
1360/// \param Result on a successful parse, contains the parsed unqualified-id.
1361///
1362/// \returns true if parsing fails, false otherwise.
1363bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001364 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001365 UnqualifiedId &Result) {
1366 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1367
1368 // Consume the 'operator' keyword.
1369 SourceLocation KeywordLoc = ConsumeToken();
1370
1371 // Determine what kind of operator name we have.
1372 unsigned SymbolIdx = 0;
1373 SourceLocation SymbolLocations[3];
1374 OverloadedOperatorKind Op = OO_None;
1375 switch (Tok.getKind()) {
1376 case tok::kw_new:
1377 case tok::kw_delete: {
1378 bool isNew = Tok.getKind() == tok::kw_new;
1379 // Consume the 'new' or 'delete'.
1380 SymbolLocations[SymbolIdx++] = ConsumeToken();
1381 if (Tok.is(tok::l_square)) {
1382 // Consume the '['.
1383 SourceLocation LBracketLoc = ConsumeBracket();
1384 // Consume the ']'.
1385 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1386 LBracketLoc);
1387 if (RBracketLoc.isInvalid())
1388 return true;
1389
1390 SymbolLocations[SymbolIdx++] = LBracketLoc;
1391 SymbolLocations[SymbolIdx++] = RBracketLoc;
1392 Op = isNew? OO_Array_New : OO_Array_Delete;
1393 } else {
1394 Op = isNew? OO_New : OO_Delete;
1395 }
1396 break;
1397 }
1398
1399#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1400 case tok::Token: \
1401 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1402 Op = OO_##Name; \
1403 break;
1404#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1405#include "clang/Basic/OperatorKinds.def"
1406
1407 case tok::l_paren: {
1408 // Consume the '('.
1409 SourceLocation LParenLoc = ConsumeParen();
1410 // Consume the ')'.
1411 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1412 LParenLoc);
1413 if (RParenLoc.isInvalid())
1414 return true;
1415
1416 SymbolLocations[SymbolIdx++] = LParenLoc;
1417 SymbolLocations[SymbolIdx++] = RParenLoc;
1418 Op = OO_Call;
1419 break;
1420 }
1421
1422 case tok::l_square: {
1423 // Consume the '['.
1424 SourceLocation LBracketLoc = ConsumeBracket();
1425 // Consume the ']'.
1426 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1427 LBracketLoc);
1428 if (RBracketLoc.isInvalid())
1429 return true;
1430
1431 SymbolLocations[SymbolIdx++] = LBracketLoc;
1432 SymbolLocations[SymbolIdx++] = RBracketLoc;
1433 Op = OO_Subscript;
1434 break;
1435 }
1436
1437 case tok::code_completion: {
1438 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001439 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001440
1441 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001442 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001443
1444 // Don't try to parse any further.
1445 return true;
1446 }
1447
1448 default:
1449 break;
1450 }
1451
1452 if (Op != OO_None) {
1453 // We have parsed an operator-function-id.
1454 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1455 return false;
1456 }
Sean Hunt0486d742009-11-28 04:44:28 +00001457
1458 // Parse a literal-operator-id.
1459 //
1460 // literal-operator-id: [C++0x 13.5.8]
1461 // operator "" identifier
1462
1463 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1464 if (Tok.getLength() != 2)
1465 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1466 ConsumeStringToken();
1467
1468 if (Tok.isNot(tok::identifier)) {
1469 Diag(Tok.getLocation(), diag::err_expected_ident);
1470 return true;
1471 }
1472
1473 IdentifierInfo *II = Tok.getIdentifierInfo();
1474 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001475 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001476 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001477
1478 // Parse a conversion-function-id.
1479 //
1480 // conversion-function-id: [C++ 12.3.2]
1481 // operator conversion-type-id
1482 //
1483 // conversion-type-id:
1484 // type-specifier-seq conversion-declarator[opt]
1485 //
1486 // conversion-declarator:
1487 // ptr-operator conversion-declarator[opt]
1488
1489 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001490 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001491 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001492 return true;
1493
1494 // Parse the conversion-declarator, which is merely a sequence of
1495 // ptr-operators.
1496 Declarator D(DS, Declarator::TypeNameContext);
1497 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1498
1499 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001500 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001501 if (Ty.isInvalid())
1502 return true;
1503
1504 // Note that this is a conversion-function-id.
1505 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1506 D.getSourceRange().getEnd());
1507 return false;
1508}
1509
1510/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1511/// name of an entity.
1512///
1513/// \code
1514/// unqualified-id: [C++ expr.prim.general]
1515/// identifier
1516/// operator-function-id
1517/// conversion-function-id
1518/// [C++0x] literal-operator-id [TODO]
1519/// ~ class-name
1520/// template-id
1521///
1522/// \endcode
1523///
1524/// \param The nested-name-specifier that preceded this unqualified-id. If
1525/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1526///
1527/// \param EnteringContext whether we are entering the scope of the
1528/// nested-name-specifier.
1529///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001530/// \param AllowDestructorName whether we allow parsing of a destructor name.
1531///
1532/// \param AllowConstructorName whether we allow parsing a constructor name.
1533///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001534/// \param ObjectType if this unqualified-id occurs within a member access
1535/// expression, the type of the base object whose member is being accessed.
1536///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001537/// \param Result on a successful parse, contains the parsed unqualified-id.
1538///
1539/// \returns true if parsing fails, false otherwise.
1540bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1541 bool AllowDestructorName,
1542 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001543 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001544 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001545
1546 // Handle 'A::template B'. This is for template-ids which have not
1547 // already been annotated by ParseOptionalCXXScopeSpecifier().
1548 bool TemplateSpecified = false;
1549 SourceLocation TemplateKWLoc;
1550 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1551 (ObjectType || SS.isSet())) {
1552 TemplateSpecified = true;
1553 TemplateKWLoc = ConsumeToken();
1554 }
1555
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001556 // unqualified-id:
1557 // identifier
1558 // template-id (when it hasn't already been annotated)
1559 if (Tok.is(tok::identifier)) {
1560 // Consume the identifier.
1561 IdentifierInfo *Id = Tok.getIdentifierInfo();
1562 SourceLocation IdLoc = ConsumeToken();
1563
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001564 if (!getLang().CPlusPlus) {
1565 // If we're not in C++, only identifiers matter. Record the
1566 // identifier and return.
1567 Result.setIdentifier(Id, IdLoc);
1568 return false;
1569 }
1570
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001571 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001572 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001573 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001574 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001575 &SS, false, false,
1576 ParsedType(),
1577 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001578 IdLoc, IdLoc);
1579 } else {
1580 // We have parsed an identifier.
1581 Result.setIdentifier(Id, IdLoc);
1582 }
1583
1584 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001585 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001586 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001587 ObjectType, Result,
1588 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001589
1590 return false;
1591 }
1592
1593 // unqualified-id:
1594 // template-id (already parsed and annotated)
1595 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001596 TemplateIdAnnotation *TemplateId
1597 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1598
1599 // If the template-name names the current class, then this is a constructor
1600 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001601 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001602 if (SS.isSet()) {
1603 // C++ [class.qual]p2 specifies that a qualified template-name
1604 // is taken as the constructor name where a constructor can be
1605 // declared. Thus, the template arguments are extraneous, so
1606 // complain about them and remove them entirely.
1607 Diag(TemplateId->TemplateNameLoc,
1608 diag::err_out_of_line_constructor_template_id)
1609 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001610 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001611 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1612 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1613 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001614 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001615 &SS, false, false,
1616 ParsedType(),
1617 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001618 TemplateId->TemplateNameLoc,
1619 TemplateId->RAngleLoc);
1620 TemplateId->Destroy();
1621 ConsumeToken();
1622 return false;
1623 }
1624
1625 Result.setConstructorTemplateId(TemplateId);
1626 ConsumeToken();
1627 return false;
1628 }
1629
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001630 // We have already parsed a template-id; consume the annotation token as
1631 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001632 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001633 ConsumeToken();
1634 return false;
1635 }
1636
1637 // unqualified-id:
1638 // operator-function-id
1639 // conversion-function-id
1640 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001641 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001642 return true;
1643
Sean Hunte6252d12009-11-28 08:58:14 +00001644 // If we have an operator-function-id or a literal-operator-id and the next
1645 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001646 //
1647 // template-id:
1648 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001649 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1650 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001651 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001652 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1653 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001654 Result,
1655 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001656
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001657 return false;
1658 }
1659
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001660 if (getLang().CPlusPlus &&
1661 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001662 // C++ [expr.unary.op]p10:
1663 // There is an ambiguity in the unary-expression ~X(), where X is a
1664 // class-name. The ambiguity is resolved in favor of treating ~ as a
1665 // unary complement rather than treating ~X as referring to a destructor.
1666
1667 // Parse the '~'.
1668 SourceLocation TildeLoc = ConsumeToken();
1669
1670 // Parse the class-name.
1671 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001672 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001673 return true;
1674 }
1675
1676 // Parse the class-name (or template-name in a simple-template-id).
1677 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1678 SourceLocation ClassNameLoc = ConsumeToken();
1679
Douglas Gregor0278e122010-05-05 05:58:24 +00001680 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001681 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001682 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001683 EnteringContext, ObjectType, Result,
1684 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001685 }
1686
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001687 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001688 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1689 ClassNameLoc, getCurScope(),
1690 SS, ObjectType,
1691 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001692 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001693 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001694
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001695 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001696 return false;
1697 }
1698
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001699 Diag(Tok, diag::err_expected_unqualified_id)
1700 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001701 return true;
1702}
1703
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001704/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1705/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001706///
Chris Lattner59232d32009-01-04 21:25:24 +00001707/// This method is called to parse the new expression after the optional :: has
1708/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1709/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001710///
1711/// new-expression:
1712/// '::'[opt] 'new' new-placement[opt] new-type-id
1713/// new-initializer[opt]
1714/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1715/// new-initializer[opt]
1716///
1717/// new-placement:
1718/// '(' expression-list ')'
1719///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001720/// new-type-id:
1721/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001722/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001723///
1724/// new-declarator:
1725/// ptr-operator new-declarator[opt]
1726/// direct-new-declarator
1727///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001728/// new-initializer:
1729/// '(' expression-list[opt] ')'
1730/// [C++0x] braced-init-list [TODO]
1731///
John McCall60d7b3a2010-08-24 06:29:42 +00001732ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001733Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1734 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1735 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001736
1737 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1738 // second form of new-expression. It can't be a new-type-id.
1739
Sebastian Redla55e52c2008-11-25 22:21:31 +00001740 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001741 SourceLocation PlacementLParen, PlacementRParen;
1742
Douglas Gregor4bd40312010-07-13 15:54:32 +00001743 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00001744 DeclSpec DS(AttrFactory);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001745 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001746 if (Tok.is(tok::l_paren)) {
1747 // If it turns out to be a placement, we change the type location.
1748 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001749 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1750 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001751 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001752 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001753
1754 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001755 if (PlacementRParen.isInvalid()) {
1756 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001757 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001758 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001759
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001760 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001761 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001762 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001763 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001764 } else {
1765 // We still need the type.
1766 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001767 TypeIdParens.setBegin(ConsumeParen());
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001768 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001769 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001770 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001771 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001772 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1773 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001774 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001775 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001776 if (ParseCXXTypeSpecifierSeq(DS))
1777 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001778 else {
1779 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001780 ParseDeclaratorInternal(DeclaratorInfo,
1781 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001782 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001783 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001784 }
1785 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001786 // A new-type-id is a simplified type-id, where essentially the
1787 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001788 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001789 if (ParseCXXTypeSpecifierSeq(DS))
1790 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001791 else {
1792 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001793 ParseDeclaratorInternal(DeclaratorInfo,
1794 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001795 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001796 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001797 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001798 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001799 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001800 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001801
Sebastian Redla55e52c2008-11-25 22:21:31 +00001802 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001803 SourceLocation ConstructorLParen, ConstructorRParen;
1804
1805 if (Tok.is(tok::l_paren)) {
1806 ConstructorLParen = ConsumeParen();
1807 if (Tok.isNot(tok::r_paren)) {
1808 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001809 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1810 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001811 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001812 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001813 }
1814 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001815 if (ConstructorRParen.isInvalid()) {
1816 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001817 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001818 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001819 }
1820
Sebastian Redlf53597f2009-03-15 17:47:39 +00001821 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1822 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001823 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001824 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001825}
1826
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001827/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1828/// passed to ParseDeclaratorInternal.
1829///
1830/// direct-new-declarator:
1831/// '[' expression ']'
1832/// direct-new-declarator '[' constant-expression ']'
1833///
Chris Lattner59232d32009-01-04 21:25:24 +00001834void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001835 // Parse the array dimensions.
1836 bool first = true;
1837 while (Tok.is(tok::l_square)) {
1838 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001839 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001840 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001841 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001842 // Recover
1843 SkipUntil(tok::r_square);
1844 return;
1845 }
1846 first = false;
1847
Sebastian Redlab197ba2009-02-09 18:23:29 +00001848 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall0b7e6782011-03-24 11:26:52 +00001849
1850 ParsedAttributes attrs(AttrFactory);
1851 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00001852 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001853 Size.release(), LLoc, RLoc),
John McCall0b7e6782011-03-24 11:26:52 +00001854 attrs, RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001855
Sebastian Redlab197ba2009-02-09 18:23:29 +00001856 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001857 return;
1858 }
1859}
1860
1861/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1862/// This ambiguity appears in the syntax of the C++ new operator.
1863///
1864/// new-expression:
1865/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1866/// new-initializer[opt]
1867///
1868/// new-placement:
1869/// '(' expression-list ')'
1870///
John McCallca0408f2010-08-23 06:44:23 +00001871bool Parser::ParseExpressionListOrTypeId(
1872 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001873 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001874 // The '(' was already consumed.
1875 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001876 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001877 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001878 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001879 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001880 }
1881
1882 // It's not a type, it has to be an expression list.
1883 // Discard the comma locations - ActOnCXXNew has enough parameters.
1884 CommaLocsTy CommaLocs;
1885 return ParseExpressionList(PlacementArgs, CommaLocs);
1886}
1887
1888/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1889/// to free memory allocated by new.
1890///
Chris Lattner59232d32009-01-04 21:25:24 +00001891/// This method is called to parse the 'delete' expression after the optional
1892/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1893/// and "Start" is its location. Otherwise, "Start" is the location of the
1894/// 'delete' token.
1895///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001896/// delete-expression:
1897/// '::'[opt] 'delete' cast-expression
1898/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001899ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001900Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1901 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1902 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001903
1904 // Array delete?
1905 bool ArrayDelete = false;
1906 if (Tok.is(tok::l_square)) {
1907 ArrayDelete = true;
1908 SourceLocation LHS = ConsumeBracket();
1909 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1910 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001911 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001912 }
1913
John McCall60d7b3a2010-08-24 06:29:42 +00001914 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001915 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001916 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001917
John McCall9ae2f072010-08-23 23:25:46 +00001918 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001919}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001920
Mike Stump1eb44332009-09-09 15:08:12 +00001921static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001922 switch(kind) {
John Wiegley20c0da72011-04-27 23:09:49 +00001923 default: assert(false && "Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001924 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001925 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00001926 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001927 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00001928 case tok::kw___has_trivial_constructor:
1929 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00001930 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001931 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1932 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1933 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00001934 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
1935 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001936 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00001937 case tok::kw___is_complete_type: return UTT_IsCompleteType;
1938 case tok::kw___is_compound: return UTT_IsCompound;
1939 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001940 case tok::kw___is_empty: return UTT_IsEmpty;
1941 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley20c0da72011-04-27 23:09:49 +00001942 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
1943 case tok::kw___is_function: return UTT_IsFunction;
1944 case tok::kw___is_fundamental: return UTT_IsFundamental;
1945 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00001946 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
1947 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
1948 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
1949 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
1950 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001951 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00001952 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001953 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00001954 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001955 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00001956 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00001957 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
1958 case tok::kw___is_scalar: return UTT_IsScalar;
1959 case tok::kw___is_signed: return UTT_IsSigned;
1960 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
1961 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00001962 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001963 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00001964 case tok::kw___is_unsigned: return UTT_IsUnsigned;
1965 case tok::kw___is_void: return UTT_IsVoid;
1966 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001967 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001968}
1969
1970static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1971 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001972 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001973 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00001974 case tok::kw___is_convertible: return BTT_IsConvertible;
1975 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00001976 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001977 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001978 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001979}
1980
John Wiegley21ff2e52011-04-28 00:16:57 +00001981static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
1982 switch(kind) {
1983 default: llvm_unreachable("Not a known binary type trait");
1984 case tok::kw___array_rank: return ATT_ArrayRank;
1985 case tok::kw___array_extent: return ATT_ArrayExtent;
1986 }
1987}
1988
John Wiegley55262202011-04-25 06:54:41 +00001989static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
1990 switch(kind) {
1991 default: assert(false && "Not a known unary expression trait.");
1992 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
1993 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
1994 }
1995}
1996
Sebastian Redl64b45f72009-01-05 20:52:13 +00001997/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1998/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1999/// templates.
2000///
2001/// primary-expression:
2002/// [GNU] unary-type-trait '(' type-id ')'
2003///
John McCall60d7b3a2010-08-24 06:29:42 +00002004ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002005 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2006 SourceLocation Loc = ConsumeToken();
2007
2008 SourceLocation LParen = Tok.getLocation();
2009 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2010 return ExprError();
2011
2012 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2013 // there will be cryptic errors about mismatched parentheses and missing
2014 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002015 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002016
2017 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2018
Douglas Gregor809070a2009-02-18 17:45:20 +00002019 if (Ty.isInvalid())
2020 return ExprError();
2021
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002022 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00002023}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002024
Francois Pichet6ad6f282010-12-07 00:08:36 +00002025/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2026/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2027/// templates.
2028///
2029/// primary-expression:
2030/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2031///
2032ExprResult Parser::ParseBinaryTypeTrait() {
2033 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2034 SourceLocation Loc = ConsumeToken();
2035
2036 SourceLocation LParen = Tok.getLocation();
2037 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2038 return ExprError();
2039
2040 TypeResult LhsTy = ParseTypeName();
2041 if (LhsTy.isInvalid()) {
2042 SkipUntil(tok::r_paren);
2043 return ExprError();
2044 }
2045
2046 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2047 SkipUntil(tok::r_paren);
2048 return ExprError();
2049 }
2050
2051 TypeResult RhsTy = ParseTypeName();
2052 if (RhsTy.isInvalid()) {
2053 SkipUntil(tok::r_paren);
2054 return ExprError();
2055 }
2056
2057 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2058
2059 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
2060}
2061
John Wiegley21ff2e52011-04-28 00:16:57 +00002062/// ParseArrayTypeTrait - Parse the built-in array type-trait
2063/// pseudo-functions.
2064///
2065/// primary-expression:
2066/// [Embarcadero] '__array_rank' '(' type-id ')'
2067/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2068///
2069ExprResult Parser::ParseArrayTypeTrait() {
2070 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2071 SourceLocation Loc = ConsumeToken();
2072
2073 SourceLocation LParen = Tok.getLocation();
2074 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2075 return ExprError();
2076
2077 TypeResult Ty = ParseTypeName();
2078 if (Ty.isInvalid()) {
2079 SkipUntil(tok::comma);
2080 SkipUntil(tok::r_paren);
2081 return ExprError();
2082 }
2083
2084 switch (ATT) {
2085 case ATT_ArrayRank: {
2086 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2087 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, RParen);
2088 }
2089 case ATT_ArrayExtent: {
2090 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2091 SkipUntil(tok::r_paren);
2092 return ExprError();
2093 }
2094
2095 ExprResult DimExpr = ParseExpression();
2096 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2097
2098 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), RParen);
2099 }
2100 default:
2101 break;
2102 }
2103 return ExprError();
2104}
2105
John Wiegley55262202011-04-25 06:54:41 +00002106/// ParseExpressionTrait - Parse built-in expression-trait
2107/// pseudo-functions like __is_lvalue_expr( xxx ).
2108///
2109/// primary-expression:
2110/// [Embarcadero] expression-trait '(' expression ')'
2111///
2112ExprResult Parser::ParseExpressionTrait() {
2113 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2114 SourceLocation Loc = ConsumeToken();
2115
2116 SourceLocation LParen = Tok.getLocation();
2117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2118 return ExprError();
2119
2120 ExprResult Expr = ParseExpression();
2121
2122 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2123
2124 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), RParen);
2125}
2126
2127
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002128/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2129/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2130/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002131ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002132Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002133 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002134 SourceLocation LParenLoc,
2135 SourceLocation &RParenLoc) {
2136 assert(getLang().CPlusPlus && "Should only be called for C++!");
2137 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2138 assert(isTypeIdInParens() && "Not a type-id!");
2139
John McCall60d7b3a2010-08-24 06:29:42 +00002140 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002141 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002142
2143 // We need to disambiguate a very ugly part of the C++ syntax:
2144 //
2145 // (T())x; - type-id
2146 // (T())*x; - type-id
2147 // (T())/x; - expression
2148 // (T()); - expression
2149 //
2150 // The bad news is that we cannot use the specialized tentative parser, since
2151 // it can only verify that the thing inside the parens can be parsed as
2152 // type-id, it is not useful for determining the context past the parens.
2153 //
2154 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002155 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002156 //
2157 // It uses a scheme similar to parsing inline methods. The parenthesized
2158 // tokens are cached, the context that follows is determined (possibly by
2159 // parsing a cast-expression), and then we re-introduce the cached tokens
2160 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002161
Mike Stump1eb44332009-09-09 15:08:12 +00002162 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002163 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002164
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002165 // Store the tokens of the parentheses. We will parse them after we determine
2166 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002167 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002168 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002169 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2170 return ExprError();
2171 }
2172
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002173 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002174 ParseAs = CompoundLiteral;
2175 } else {
2176 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002177 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2178 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2179 NotCastExpr = true;
2180 } else {
2181 // Try parsing the cast-expression that may follow.
2182 // If it is not a cast-expression, NotCastExpr will be true and no token
2183 // will be consumed.
2184 Result = ParseCastExpression(false/*isUnaryExpression*/,
2185 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002186 NotCastExpr,
2187 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002188 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002189
2190 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2191 // an expression.
2192 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002193 }
2194
Mike Stump1eb44332009-09-09 15:08:12 +00002195 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002196 Toks.push_back(Tok);
2197 // Re-enter the stored parenthesized tokens into the token stream, so we may
2198 // parse them now.
2199 PP.EnterTokenStream(Toks.data(), Toks.size(),
2200 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2201 // Drop the current token and bring the first cached one. It's the same token
2202 // as when we entered this function.
2203 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002204
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002205 if (ParseAs >= CompoundLiteral) {
2206 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002207
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002208 // Match the ')'.
2209 if (Tok.is(tok::r_paren))
2210 RParenLoc = ConsumeParen();
2211 else
2212 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002213
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002214 if (ParseAs == CompoundLiteral) {
2215 ExprType = CompoundLiteral;
2216 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2217 }
Mike Stump1eb44332009-09-09 15:08:12 +00002218
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002219 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2220 assert(ParseAs == CastExpr);
2221
2222 if (Ty.isInvalid())
2223 return ExprError();
2224
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002225 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002226
2227 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002228 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002229 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002230 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002231 return move(Result);
2232 }
Mike Stump1eb44332009-09-09 15:08:12 +00002233
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002234 // Not a compound literal, and not followed by a cast-expression.
2235 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002236
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002237 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002238 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002239 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002240 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002241
2242 // Match the ')'.
2243 if (Result.isInvalid()) {
2244 SkipUntil(tok::r_paren);
2245 return ExprError();
2246 }
Mike Stump1eb44332009-09-09 15:08:12 +00002247
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002248 if (Tok.is(tok::r_paren))
2249 RParenLoc = ConsumeParen();
2250 else
2251 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2252
2253 return move(Result);
2254}