blob: ca3242ba46f1b550b50a35ea788b5c9f50e0b408 [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 }
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000283
284 TemplateId->Destroy();
285 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000286 }
287
Chris Lattner5c7f7862009-06-26 03:52:38 +0000288
289 // The rest of the nested-name-specifier possibilities start with
290 // tok::identifier.
291 if (Tok.isNot(tok::identifier))
292 break;
293
294 IdentifierInfo &II = *Tok.getIdentifierInfo();
295
296 // nested-name-specifier:
297 // type-name '::'
298 // namespace-name '::'
299 // nested-name-specifier identifier '::'
300 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000301
302 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
303 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000304 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000305 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
306 Tok.getLocation(),
307 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000308 EnteringContext) &&
309 // If the token after the colon isn't an identifier, it's still an
310 // error, but they probably meant something else strange so don't
311 // recover like this.
312 PP.LookAhead(1).is(tok::identifier)) {
313 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000314 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000315
316 // Recover as if the user wrote '::'.
317 Next.setKind(tok::coloncolon);
318 }
Chris Lattner46646492009-12-07 01:36:53 +0000319 }
320
Chris Lattner5c7f7862009-06-26 03:52:38 +0000321 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000322 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000323 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000324 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000325 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000326 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000327 }
328
Chris Lattner5c7f7862009-06-26 03:52:38 +0000329 // We have an identifier followed by a '::'. Lookup this name
330 // as the name in a nested-name-specifier.
331 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000332 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
333 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000334 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000336 HasScopeSpecifier = true;
337 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
338 ObjectType, EnteringContext, SS))
339 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
340
Chris Lattner5c7f7862009-06-26 03:52:38 +0000341 continue;
342 }
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Richard Smithea698b32011-04-14 21:45:45 +0000344 // Check for '<::' which should be '< ::' instead of '[:' when following
345 // a template name.
346 if (Next.is(tok::l_square) && Next.getLength() == 2) {
347 Token SecondToken = GetLookAheadToken(2);
348 if (SecondToken.is(tok::colon) &&
349 AreTokensAdjacent(PP, Next, SecondToken)) {
350 TemplateTy Template;
351 UnqualifiedId TemplateName;
352 TemplateName.setIdentifier(&II, Tok.getLocation());
353 bool MemberOfUnknownSpecialization;
354 if (Actions.isTemplateName(getCurScope(), SS,
355 /*hasTemplateKeyword=*/false,
356 TemplateName,
357 ObjectType,
358 EnteringContext,
359 Template,
360 MemberOfUnknownSpecialization)) {
361 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
362 /*AtDigraph*/false);
363 }
364 }
365 }
366
Chris Lattner5c7f7862009-06-26 03:52:38 +0000367 // nested-name-specifier:
368 // type-name '<'
369 if (Next.is(tok::less)) {
370 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000371 UnqualifiedId TemplateName;
372 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000373 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000374 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000375 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000376 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000377 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000378 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000379 Template,
380 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000381 // We have found a template name, so annotate this this token
382 // with a template-id annotation. We do not permit the
383 // template-id to be translated into a type annotation,
384 // because some clients (e.g., the parsing of class template
385 // specializations) still want to see the original template-id
386 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000387 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000388 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000389 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000390 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000391 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000392 }
393
394 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000395 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000396 // We have something like t::getAs<T>, where getAs is a
397 // member of an unknown specialization. However, this will only
398 // parse correctly as a template, so suggest the keyword 'template'
399 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000400 unsigned DiagID = diag::err_missing_dependent_template_keyword;
401 if (getLang().Microsoft)
Francois Pichetcf320c62011-04-22 08:25:24 +0000402 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000403
404 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000405 << II.getName()
406 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
407
Douglas Gregord6ab2322010-06-16 23:00:59 +0000408 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000409 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000410 Tok.getLocation(), SS,
411 TemplateName, ObjectType,
412 EnteringContext, Template)) {
413 // Consume the identifier.
414 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000415 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000416 SourceLocation(), false))
417 return true;
418 }
419 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000420 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000421
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000422 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000423 }
424 }
425
Douglas Gregor39a8de12009-02-25 19:37:18 +0000426 // We don't have any tokens that form the beginning of a
427 // nested-name-specifier, so we're done.
428 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Douglas Gregord4dca082010-02-24 18:44:31 +0000431 // Even if we didn't see any pieces of a nested-name-specifier, we
432 // still check whether there is a tilde in this position, which
433 // indicates a potential pseudo-destructor.
434 if (CheckForDestructor && Tok.is(tok::tilde))
435 *MayBePseudoDestructor = true;
436
John McCall9ba61662010-02-26 08:45:28 +0000437 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000438}
439
440/// ParseCXXIdExpression - Handle id-expression.
441///
442/// id-expression:
443/// unqualified-id
444/// qualified-id
445///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000446/// qualified-id:
447/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
448/// '::' identifier
449/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000450/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000451///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000452/// NOTE: The standard specifies that, for qualified-id, the parser does not
453/// expect:
454///
455/// '::' conversion-function-id
456/// '::' '~' class-name
457///
458/// This may cause a slight inconsistency on diagnostics:
459///
460/// class C {};
461/// namespace A {}
462/// void f() {
463/// :: A :: ~ C(); // Some Sema error about using destructor with a
464/// // namespace.
465/// :: ~ C(); // Some Parser error like 'unexpected ~'.
466/// }
467///
468/// We simplify the parser a bit and make it work like:
469///
470/// qualified-id:
471/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
472/// '::' unqualified-id
473///
474/// That way Sema can handle and report similar errors for namespaces and the
475/// global scope.
476///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000477/// The isAddressOfOperand parameter indicates that this id-expression is a
478/// direct operand of the address-of operator. This is, besides member contexts,
479/// the only place where a qualified-id naming a non-static class member may
480/// appear.
481///
John McCall60d7b3a2010-08-24 06:29:42 +0000482ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000483 // qualified-id:
484 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
485 // '::' unqualified-id
486 //
487 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000488 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000489
490 UnqualifiedId Name;
491 if (ParseUnqualifiedId(SS,
492 /*EnteringContext=*/false,
493 /*AllowDestructorName=*/false,
494 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000495 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000496 Name))
497 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000498
499 // This is only the direct operand of an & operator if it is not
500 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000501 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
502 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000503
Douglas Gregor23c94db2010-07-02 17:43:08 +0000504 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000505 isAddressOfOperand);
506
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000507}
508
Reid Spencer5f016e22007-07-11 17:01:13 +0000509/// ParseCXXCasts - This handles the various ways to cast expressions to another
510/// type.
511///
512/// postfix-expression: [C++ 5.2p1]
513/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
514/// 'static_cast' '<' type-name '>' '(' expression ')'
515/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
516/// 'const_cast' '<' type-name '>' '(' expression ')'
517///
John McCall60d7b3a2010-08-24 06:29:42 +0000518ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 tok::TokenKind Kind = Tok.getKind();
520 const char *CastName = 0; // For error messages
521
522 switch (Kind) {
523 default: assert(0 && "Unknown C++ cast!"); abort();
524 case tok::kw_const_cast: CastName = "const_cast"; break;
525 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
526 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
527 case tok::kw_static_cast: CastName = "static_cast"; break;
528 }
529
530 SourceLocation OpLoc = ConsumeToken();
531 SourceLocation LAngleBracketLoc = Tok.getLocation();
532
Richard Smithea698b32011-04-14 21:45:45 +0000533 // Check for "<::" which is parsed as "[:". If found, fix token stream,
534 // diagnose error, suggest fix, and recover parsing.
535 Token Next = NextToken();
536 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
537 AreTokensAdjacent(PP, Tok, Next))
538 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
539
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000541 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000542
Douglas Gregor809070a2009-02-18 17:45:20 +0000543 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 SourceLocation RAngleBracketLoc = Tok.getLocation();
545
Chris Lattner1ab3b962008-11-18 07:48:38 +0000546 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000547 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000548
549 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
550
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000551 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
552 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000553
John McCall60d7b3a2010-08-24 06:29:42 +0000554 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000556 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000557 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000558
Douglas Gregor809070a2009-02-18 17:45:20 +0000559 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000560 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000561 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000562 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000563 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000564
Sebastian Redl20df9b72008-12-11 22:51:44 +0000565 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000566}
567
Sebastian Redlc42e1182008-11-11 11:37:55 +0000568/// ParseCXXTypeid - This handles the C++ typeid expression.
569///
570/// postfix-expression: [C++ 5.2p1]
571/// 'typeid' '(' expression ')'
572/// 'typeid' '(' type-id ')'
573///
John McCall60d7b3a2010-08-24 06:29:42 +0000574ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000575 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
576
577 SourceLocation OpLoc = ConsumeToken();
578 SourceLocation LParenLoc = Tok.getLocation();
579 SourceLocation RParenLoc;
580
581 // typeid expressions are always parenthesized.
582 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
583 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000584 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000585
John McCall60d7b3a2010-08-24 06:29:42 +0000586 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000587
588 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000589 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000590
591 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000592 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000593
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000594 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000595 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000596
597 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000598 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000599 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000600 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000601 // When typeid is applied to an expression other than an lvalue of a
602 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000603 // operand (Clause 5).
604 //
Mike Stump1eb44332009-09-09 15:08:12 +0000605 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000606 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000607 // we the expression is potentially potentially evaluated.
608 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000609 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000610 Result = ParseExpression();
611
612 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000613 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000614 SkipUntil(tok::r_paren);
615 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000616 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
617 if (RParenLoc.isInvalid())
618 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000619
620 // If we are a foo<int> that identifies a single function, resolve it now...
621 Expr* e = Result.get();
622 if (e->getType() == Actions.Context.OverloadTy) {
623 ExprResult er =
624 Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
625 if (er.isUsable())
626 Result = er.release();
627 }
Sebastian Redlc42e1182008-11-11 11:37:55 +0000628 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000629 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000630 }
631 }
632
Sebastian Redl20df9b72008-12-11 22:51:44 +0000633 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000634}
635
Francois Pichet01b7c302010-09-08 12:20:18 +0000636/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
637///
638/// '__uuidof' '(' expression ')'
639/// '__uuidof' '(' type-id ')'
640///
641ExprResult Parser::ParseCXXUuidof() {
642 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
643
644 SourceLocation OpLoc = ConsumeToken();
645 SourceLocation LParenLoc = Tok.getLocation();
646 SourceLocation RParenLoc;
647
648 // __uuidof expressions are always parenthesized.
649 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
650 "__uuidof"))
651 return ExprError();
652
653 ExprResult Result;
654
655 if (isTypeIdInParens()) {
656 TypeResult Ty = ParseTypeName();
657
658 // Match the ')'.
659 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
660
661 if (Ty.isInvalid())
662 return ExprError();
663
664 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
665 Ty.get().getAsOpaquePtr(), RParenLoc);
666 } else {
667 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
668 Result = ParseExpression();
669
670 // Match the ')'.
671 if (Result.isInvalid())
672 SkipUntil(tok::r_paren);
673 else {
674 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
675
676 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
677 Result.release(), RParenLoc);
678 }
679 }
680
681 return move(Result);
682}
683
Douglas Gregord4dca082010-02-24 18:44:31 +0000684/// \brief Parse a C++ pseudo-destructor expression after the base,
685/// . or -> operator, and nested-name-specifier have already been
686/// parsed.
687///
688/// postfix-expression: [C++ 5.2]
689/// postfix-expression . pseudo-destructor-name
690/// postfix-expression -> pseudo-destructor-name
691///
692/// pseudo-destructor-name:
693/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
694/// ::[opt] nested-name-specifier template simple-template-id ::
695/// ~type-name
696/// ::[opt] nested-name-specifier[opt] ~type-name
697///
John McCall60d7b3a2010-08-24 06:29:42 +0000698ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000699Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
700 tok::TokenKind OpKind,
701 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000702 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000703 // We're parsing either a pseudo-destructor-name or a dependent
704 // member access that has the same form as a
705 // pseudo-destructor-name. We parse both in the same way and let
706 // the action model sort them out.
707 //
708 // Note that the ::[opt] nested-name-specifier[opt] has already
709 // been parsed, and if there was a simple-template-id, it has
710 // been coalesced into a template-id annotation token.
711 UnqualifiedId FirstTypeName;
712 SourceLocation CCLoc;
713 if (Tok.is(tok::identifier)) {
714 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
715 ConsumeToken();
716 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
717 CCLoc = ConsumeToken();
718 } else if (Tok.is(tok::annot_template_id)) {
719 FirstTypeName.setTemplateId(
720 (TemplateIdAnnotation *)Tok.getAnnotationValue());
721 ConsumeToken();
722 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
723 CCLoc = ConsumeToken();
724 } else {
725 FirstTypeName.setIdentifier(0, SourceLocation());
726 }
727
728 // Parse the tilde.
729 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
730 SourceLocation TildeLoc = ConsumeToken();
731 if (!Tok.is(tok::identifier)) {
732 Diag(Tok, diag::err_destructor_tilde_identifier);
733 return ExprError();
734 }
735
736 // Parse the second type.
737 UnqualifiedId SecondTypeName;
738 IdentifierInfo *Name = Tok.getIdentifierInfo();
739 SourceLocation NameLoc = ConsumeToken();
740 SecondTypeName.setIdentifier(Name, NameLoc);
741
742 // If there is a '<', the second type name is a template-id. Parse
743 // it as such.
744 if (Tok.is(tok::less) &&
745 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000746 SecondTypeName, /*AssumeTemplateName=*/true,
747 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000748 return ExprError();
749
John McCall9ae2f072010-08-23 23:25:46 +0000750 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
751 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000752 SS, FirstTypeName, CCLoc,
753 TildeLoc, SecondTypeName,
754 Tok.is(tok::l_paren));
755}
756
Reid Spencer5f016e22007-07-11 17:01:13 +0000757/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
758///
759/// boolean-literal: [C++ 2.13.5]
760/// 'true'
761/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000762ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000764 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000765}
Chris Lattner50dd2892008-02-26 00:51:44 +0000766
767/// ParseThrowExpression - This handles the C++ throw expression.
768///
769/// throw-expression: [C++ 15]
770/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000771ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000772 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000773 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000774
Chris Lattner2a2819a2008-04-06 06:02:23 +0000775 // If the current token isn't the start of an assignment-expression,
776 // then the expression is not present. This handles things like:
777 // "C ? throw : (void)42", which is crazy but legal.
778 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
779 case tok::semi:
780 case tok::r_paren:
781 case tok::r_square:
782 case tok::r_brace:
783 case tok::colon:
784 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000785 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000786
Chris Lattner2a2819a2008-04-06 06:02:23 +0000787 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000788 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000789 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000790 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000791 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000792}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000793
794/// ParseCXXThis - This handles the C++ 'this' pointer.
795///
796/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
797/// a non-lvalue expression whose value is the address of the object for which
798/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000799ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000800 assert(Tok.is(tok::kw_this) && "Not 'this'!");
801 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000802 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000803}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000804
805/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
806/// Can be interpreted either as function-style casting ("int(x)")
807/// or class type construction ("ClassType(x,y,z)")
808/// or creation of a value-initialized type ("int()").
809///
810/// postfix-expression: [C++ 5.2p1]
811/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
812/// typename-specifier '(' expression-list[opt] ')' [TODO]
813///
John McCall60d7b3a2010-08-24 06:29:42 +0000814ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000815Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000816 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000817 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000818
819 assert(Tok.is(tok::l_paren) && "Expected '('!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000820 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
821
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000822 SourceLocation LParenLoc = ConsumeParen();
823
Sebastian Redla55e52c2008-11-25 22:21:31 +0000824 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000825 CommaLocsTy CommaLocs;
826
827 if (Tok.isNot(tok::r_paren)) {
828 if (ParseExpressionList(Exprs, CommaLocs)) {
829 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000830 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000831 }
832 }
833
834 // Match the ')'.
835 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
836
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000837 // TypeRep could be null, if it references an invalid typedef.
838 if (!TypeRep)
839 return ExprError();
840
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000841 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
842 "Unexpected number of commas!");
Douglas Gregorab6677e2010-09-08 00:15:04 +0000843 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000844 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000845}
846
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000847/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000848///
849/// condition:
850/// expression
851/// type-specifier-seq declarator '=' assignment-expression
852/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
853/// '=' assignment-expression
854///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000855/// \param ExprResult if the condition was parsed as an expression, the
856/// parsed expression.
857///
858/// \param DeclResult if the condition was parsed as a declaration, the
859/// parsed declaration.
860///
Douglas Gregor586596f2010-05-06 17:25:47 +0000861/// \param Loc The location of the start of the statement that requires this
862/// condition, e.g., the "for" in a for loop.
863///
864/// \param ConvertToBoolean Whether the condition expression should be
865/// converted to a boolean value.
866///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000867/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000868bool Parser::ParseCXXCondition(ExprResult &ExprOut,
869 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000870 SourceLocation Loc,
871 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000872 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000873 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000874 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000875 }
876
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000877 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000878 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000879 ExprOut = ParseExpression(); // expression
880 DeclOut = 0;
881 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000882 return true;
883
884 // If required, convert to a boolean value.
885 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000886 ExprOut
887 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
888 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000889 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000890
891 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +0000892 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000893 ParseSpecifierQualifierList(DS);
894
895 // declarator
896 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
897 ParseDeclarator(DeclaratorInfo);
898
899 // simple-asm-expr[opt]
900 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000901 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000902 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000903 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000904 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000905 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000906 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000907 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000908 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000909 }
910
911 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000912 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000913
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000914 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000915 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000916 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000917 DeclOut = Dcl.get();
918 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000919
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000920 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000921 if (isTokenEqualOrMistypedEqualEqual(
922 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000923 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000924 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000925 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000926 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
927 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000928 } else {
929 // FIXME: C++0x allows a braced-init-list
930 Diag(Tok, diag::err_expected_equal_after_declarator);
931 }
932
Douglas Gregor586596f2010-05-06 17:25:47 +0000933 // FIXME: Build a reference to this declaration? Convert it to bool?
934 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +0000935
936 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +0000937
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000938 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000939}
940
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000941/// \brief Determine whether the current token starts a C++
942/// simple-type-specifier.
943bool Parser::isCXXSimpleTypeSpecifier() const {
944 switch (Tok.getKind()) {
945 case tok::annot_typename:
946 case tok::kw_short:
947 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000948 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000949 case tok::kw_signed:
950 case tok::kw_unsigned:
951 case tok::kw_void:
952 case tok::kw_char:
953 case tok::kw_int:
954 case tok::kw_float:
955 case tok::kw_double:
956 case tok::kw_wchar_t:
957 case tok::kw_char16_t:
958 case tok::kw_char32_t:
959 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +0000960 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000961 case tok::kw_typeof:
962 return true;
963
964 default:
965 break;
966 }
967
968 return false;
969}
970
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000971/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
972/// This should only be called when the current token is known to be part of
973/// simple-type-specifier.
974///
975/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000976/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000977/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
978/// char
979/// wchar_t
980/// bool
981/// short
982/// int
983/// long
984/// signed
985/// unsigned
986/// float
987/// double
988/// void
989/// [GNU] typeof-specifier
990/// [C++0x] auto [TODO]
991///
992/// type-name:
993/// class-name
994/// enum-name
995/// typedef-name
996///
997void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
998 DS.SetRangeStart(Tok.getLocation());
999 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001000 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001001 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001003 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001004 case tok::identifier: // foo::bar
1005 case tok::coloncolon: // ::foo::bar
1006 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001007 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001008 assert(0 && "Not a simple-type-specifier token!");
1009 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +00001010
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001011 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001012 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001013 if (getTypeAnnotation(Tok))
1014 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1015 getTypeAnnotation(Tok));
1016 else
1017 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001018
1019 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1020 ConsumeToken();
1021
1022 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1023 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1024 // Objective-C interface. If we don't have Objective-C or a '<', this is
1025 // just a normal reference to a typedef name.
1026 if (Tok.is(tok::less) && getLang().ObjC1)
1027 ParseObjCProtocolQualifiers(DS);
1028
1029 DS.Finish(Diags, PP);
1030 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001031 }
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001033 // builtin types
1034 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001035 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001036 break;
1037 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001038 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001039 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001040 case tok::kw___int64:
1041 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1042 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001043 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001044 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001045 break;
1046 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001047 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001048 break;
1049 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001050 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001051 break;
1052 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001053 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001054 break;
1055 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001056 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001057 break;
1058 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001059 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001060 break;
1061 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001062 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001063 break;
1064 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001065 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001066 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001067 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001068 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001069 break;
1070 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001071 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001072 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001073 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001074 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001075 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001077 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001078 // GNU typeof support.
1079 case tok::kw_typeof:
1080 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001081 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001082 return;
1083 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001084 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001085 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1086 else
1087 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001088 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001089 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001090}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001091
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001092/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1093/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1094/// e.g., "const short int". Note that the DeclSpec is *not* finished
1095/// by parsing the type-specifier-seq, because these sequences are
1096/// typically followed by some form of declarator. Returns true and
1097/// emits diagnostics if this is not a type-specifier-seq, false
1098/// otherwise.
1099///
1100/// type-specifier-seq: [C++ 8.1]
1101/// type-specifier type-specifier-seq[opt]
1102///
1103bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1104 DS.SetRangeStart(Tok.getLocation());
1105 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001106 unsigned DiagID;
1107 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001108
1109 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001110 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1111 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001112 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001113 return true;
1114 }
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Sebastian Redld9bafa72010-02-03 21:21:43 +00001116 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1117 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1118 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001119
Douglas Gregor396a9f22010-02-24 23:13:13 +00001120 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001121 return false;
1122}
1123
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001124/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1125/// some form.
1126///
1127/// This routine is invoked when a '<' is encountered after an identifier or
1128/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1129/// whether the unqualified-id is actually a template-id. This routine will
1130/// then parse the template arguments and form the appropriate template-id to
1131/// return to the caller.
1132///
1133/// \param SS the nested-name-specifier that precedes this template-id, if
1134/// we're actually parsing a qualified-id.
1135///
1136/// \param Name for constructor and destructor names, this is the actual
1137/// identifier that may be a template-name.
1138///
1139/// \param NameLoc the location of the class-name in a constructor or
1140/// destructor.
1141///
1142/// \param EnteringContext whether we're entering the scope of the
1143/// nested-name-specifier.
1144///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001145/// \param ObjectType if this unqualified-id occurs within a member access
1146/// expression, the type of the base object whose member is being accessed.
1147///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001148/// \param Id as input, describes the template-name or operator-function-id
1149/// that precedes the '<'. If template arguments were parsed successfully,
1150/// will be updated with the template-id.
1151///
Douglas Gregord4dca082010-02-24 18:44:31 +00001152/// \param AssumeTemplateId When true, this routine will assume that the name
1153/// refers to a template without performing name lookup to verify.
1154///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001155/// \returns true if a parse error occurred, false otherwise.
1156bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1157 IdentifierInfo *Name,
1158 SourceLocation NameLoc,
1159 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001160 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001161 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001162 bool AssumeTemplateId,
1163 SourceLocation TemplateKWLoc) {
1164 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1165 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001166
1167 TemplateTy Template;
1168 TemplateNameKind TNK = TNK_Non_template;
1169 switch (Id.getKind()) {
1170 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001171 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001172 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001173 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001174 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001175 Id, ObjectType, EnteringContext,
1176 Template);
1177 if (TNK == TNK_Non_template)
1178 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001179 } else {
1180 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001181 TNK = Actions.isTemplateName(getCurScope(), SS,
1182 TemplateKWLoc.isValid(), Id,
1183 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001184 MemberOfUnknownSpecialization);
1185
1186 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1187 ObjectType && IsTemplateArgumentList()) {
1188 // We have something like t->getAs<T>(), where getAs is a
1189 // member of an unknown specialization. However, this will only
1190 // parse correctly as a template, so suggest the keyword 'template'
1191 // before 'getAs' and treat this as a dependent template name.
1192 std::string Name;
1193 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1194 Name = Id.Identifier->getName();
1195 else {
1196 Name = "operator ";
1197 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1198 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1199 else
1200 Name += Id.Identifier->getName();
1201 }
1202 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1203 << Name
1204 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001205 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001206 SS, Id, ObjectType,
1207 EnteringContext, Template);
1208 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001209 return true;
1210 }
1211 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001212 break;
1213
Douglas Gregor014e88d2009-11-03 23:16:33 +00001214 case UnqualifiedId::IK_ConstructorName: {
1215 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001216 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001217 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001218 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1219 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001220 EnteringContext, Template,
1221 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001222 break;
1223 }
1224
Douglas Gregor014e88d2009-11-03 23:16:33 +00001225 case UnqualifiedId::IK_DestructorName: {
1226 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001227 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001228 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001229 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001230 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001231 TemplateName, ObjectType,
1232 EnteringContext, Template);
1233 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001234 return true;
1235 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001236 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1237 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001238 EnteringContext, Template,
1239 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001240
John McCallb3d87482010-08-24 05:47:05 +00001241 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001242 Diag(NameLoc, diag::err_destructor_template_id)
1243 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001244 return true;
1245 }
1246 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001247 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001248 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001249
1250 default:
1251 return false;
1252 }
1253
1254 if (TNK == TNK_Non_template)
1255 return false;
1256
1257 // Parse the enclosed template argument list.
1258 SourceLocation LAngleLoc, RAngleLoc;
1259 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001260 if (Tok.is(tok::less) &&
1261 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001262 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001263 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001264 RAngleLoc))
1265 return true;
1266
1267 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001268 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1269 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001270 // Form a parsed representation of the template-id to be stored in the
1271 // UnqualifiedId.
1272 TemplateIdAnnotation *TemplateId
1273 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1274
1275 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1276 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001277 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001278 TemplateId->TemplateNameLoc = Id.StartLocation;
1279 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001280 TemplateId->Name = 0;
1281 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1282 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001283 }
1284
Douglas Gregor059101f2011-03-02 00:47:37 +00001285 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001286 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001287 TemplateId->Kind = TNK;
1288 TemplateId->LAngleLoc = LAngleLoc;
1289 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001290 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001291 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001292 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001293 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001294
1295 Id.setTemplateId(TemplateId);
1296 return false;
1297 }
1298
1299 // Bundle the template arguments together.
1300 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001301 TemplateArgs.size());
1302
1303 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001304 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001305 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001306 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001307 RAngleLoc);
1308 if (Type.isInvalid())
1309 return true;
1310
1311 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1312 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1313 else
1314 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1315
1316 return false;
1317}
1318
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001319/// \brief Parse an operator-function-id or conversion-function-id as part
1320/// of a C++ unqualified-id.
1321///
1322/// This routine is responsible only for parsing the operator-function-id or
1323/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001324///
1325/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001326/// operator-function-id: [C++ 13.5]
1327/// 'operator' operator
1328///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001329/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001330/// new delete new[] delete[]
1331/// + - * / % ^ & | ~
1332/// ! = < > += -= *= /= %=
1333/// ^= &= |= << >> >>= <<= == !=
1334/// <= >= && || ++ -- , ->* ->
1335/// () []
1336///
1337/// conversion-function-id: [C++ 12.3.2]
1338/// operator conversion-type-id
1339///
1340/// conversion-type-id:
1341/// type-specifier-seq conversion-declarator[opt]
1342///
1343/// conversion-declarator:
1344/// ptr-operator conversion-declarator[opt]
1345/// \endcode
1346///
1347/// \param The nested-name-specifier that preceded this unqualified-id. If
1348/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1349///
1350/// \param EnteringContext whether we are entering the scope of the
1351/// nested-name-specifier.
1352///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001353/// \param ObjectType if this unqualified-id occurs within a member access
1354/// expression, the type of the base object whose member is being accessed.
1355///
1356/// \param Result on a successful parse, contains the parsed unqualified-id.
1357///
1358/// \returns true if parsing fails, false otherwise.
1359bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001360 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001361 UnqualifiedId &Result) {
1362 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1363
1364 // Consume the 'operator' keyword.
1365 SourceLocation KeywordLoc = ConsumeToken();
1366
1367 // Determine what kind of operator name we have.
1368 unsigned SymbolIdx = 0;
1369 SourceLocation SymbolLocations[3];
1370 OverloadedOperatorKind Op = OO_None;
1371 switch (Tok.getKind()) {
1372 case tok::kw_new:
1373 case tok::kw_delete: {
1374 bool isNew = Tok.getKind() == tok::kw_new;
1375 // Consume the 'new' or 'delete'.
1376 SymbolLocations[SymbolIdx++] = ConsumeToken();
1377 if (Tok.is(tok::l_square)) {
1378 // Consume the '['.
1379 SourceLocation LBracketLoc = ConsumeBracket();
1380 // Consume the ']'.
1381 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1382 LBracketLoc);
1383 if (RBracketLoc.isInvalid())
1384 return true;
1385
1386 SymbolLocations[SymbolIdx++] = LBracketLoc;
1387 SymbolLocations[SymbolIdx++] = RBracketLoc;
1388 Op = isNew? OO_Array_New : OO_Array_Delete;
1389 } else {
1390 Op = isNew? OO_New : OO_Delete;
1391 }
1392 break;
1393 }
1394
1395#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1396 case tok::Token: \
1397 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1398 Op = OO_##Name; \
1399 break;
1400#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1401#include "clang/Basic/OperatorKinds.def"
1402
1403 case tok::l_paren: {
1404 // Consume the '('.
1405 SourceLocation LParenLoc = ConsumeParen();
1406 // Consume the ')'.
1407 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1408 LParenLoc);
1409 if (RParenLoc.isInvalid())
1410 return true;
1411
1412 SymbolLocations[SymbolIdx++] = LParenLoc;
1413 SymbolLocations[SymbolIdx++] = RParenLoc;
1414 Op = OO_Call;
1415 break;
1416 }
1417
1418 case tok::l_square: {
1419 // Consume the '['.
1420 SourceLocation LBracketLoc = ConsumeBracket();
1421 // Consume the ']'.
1422 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1423 LBracketLoc);
1424 if (RBracketLoc.isInvalid())
1425 return true;
1426
1427 SymbolLocations[SymbolIdx++] = LBracketLoc;
1428 SymbolLocations[SymbolIdx++] = RBracketLoc;
1429 Op = OO_Subscript;
1430 break;
1431 }
1432
1433 case tok::code_completion: {
1434 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001435 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001436
1437 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001438 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001439
1440 // Don't try to parse any further.
1441 return true;
1442 }
1443
1444 default:
1445 break;
1446 }
1447
1448 if (Op != OO_None) {
1449 // We have parsed an operator-function-id.
1450 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1451 return false;
1452 }
Sean Hunt0486d742009-11-28 04:44:28 +00001453
1454 // Parse a literal-operator-id.
1455 //
1456 // literal-operator-id: [C++0x 13.5.8]
1457 // operator "" identifier
1458
1459 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1460 if (Tok.getLength() != 2)
1461 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1462 ConsumeStringToken();
1463
1464 if (Tok.isNot(tok::identifier)) {
1465 Diag(Tok.getLocation(), diag::err_expected_ident);
1466 return true;
1467 }
1468
1469 IdentifierInfo *II = Tok.getIdentifierInfo();
1470 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001471 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001472 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001473
1474 // Parse a conversion-function-id.
1475 //
1476 // conversion-function-id: [C++ 12.3.2]
1477 // operator conversion-type-id
1478 //
1479 // conversion-type-id:
1480 // type-specifier-seq conversion-declarator[opt]
1481 //
1482 // conversion-declarator:
1483 // ptr-operator conversion-declarator[opt]
1484
1485 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001486 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001487 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001488 return true;
1489
1490 // Parse the conversion-declarator, which is merely a sequence of
1491 // ptr-operators.
1492 Declarator D(DS, Declarator::TypeNameContext);
1493 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1494
1495 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001496 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001497 if (Ty.isInvalid())
1498 return true;
1499
1500 // Note that this is a conversion-function-id.
1501 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1502 D.getSourceRange().getEnd());
1503 return false;
1504}
1505
1506/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1507/// name of an entity.
1508///
1509/// \code
1510/// unqualified-id: [C++ expr.prim.general]
1511/// identifier
1512/// operator-function-id
1513/// conversion-function-id
1514/// [C++0x] literal-operator-id [TODO]
1515/// ~ class-name
1516/// template-id
1517///
1518/// \endcode
1519///
1520/// \param The nested-name-specifier that preceded this unqualified-id. If
1521/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1522///
1523/// \param EnteringContext whether we are entering the scope of the
1524/// nested-name-specifier.
1525///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001526/// \param AllowDestructorName whether we allow parsing of a destructor name.
1527///
1528/// \param AllowConstructorName whether we allow parsing a constructor name.
1529///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001530/// \param ObjectType if this unqualified-id occurs within a member access
1531/// expression, the type of the base object whose member is being accessed.
1532///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001533/// \param Result on a successful parse, contains the parsed unqualified-id.
1534///
1535/// \returns true if parsing fails, false otherwise.
1536bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1537 bool AllowDestructorName,
1538 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001539 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001540 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001541
1542 // Handle 'A::template B'. This is for template-ids which have not
1543 // already been annotated by ParseOptionalCXXScopeSpecifier().
1544 bool TemplateSpecified = false;
1545 SourceLocation TemplateKWLoc;
1546 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1547 (ObjectType || SS.isSet())) {
1548 TemplateSpecified = true;
1549 TemplateKWLoc = ConsumeToken();
1550 }
1551
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001552 // unqualified-id:
1553 // identifier
1554 // template-id (when it hasn't already been annotated)
1555 if (Tok.is(tok::identifier)) {
1556 // Consume the identifier.
1557 IdentifierInfo *Id = Tok.getIdentifierInfo();
1558 SourceLocation IdLoc = ConsumeToken();
1559
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001560 if (!getLang().CPlusPlus) {
1561 // If we're not in C++, only identifiers matter. Record the
1562 // identifier and return.
1563 Result.setIdentifier(Id, IdLoc);
1564 return false;
1565 }
1566
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001567 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001568 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001569 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001570 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001571 &SS, false, false,
1572 ParsedType(),
1573 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001574 IdLoc, IdLoc);
1575 } else {
1576 // We have parsed an identifier.
1577 Result.setIdentifier(Id, IdLoc);
1578 }
1579
1580 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001581 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001582 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001583 ObjectType, Result,
1584 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001585
1586 return false;
1587 }
1588
1589 // unqualified-id:
1590 // template-id (already parsed and annotated)
1591 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001592 TemplateIdAnnotation *TemplateId
1593 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1594
1595 // If the template-name names the current class, then this is a constructor
1596 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001597 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001598 if (SS.isSet()) {
1599 // C++ [class.qual]p2 specifies that a qualified template-name
1600 // is taken as the constructor name where a constructor can be
1601 // declared. Thus, the template arguments are extraneous, so
1602 // complain about them and remove them entirely.
1603 Diag(TemplateId->TemplateNameLoc,
1604 diag::err_out_of_line_constructor_template_id)
1605 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001606 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001607 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1608 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1609 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001610 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001611 &SS, false, false,
1612 ParsedType(),
1613 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001614 TemplateId->TemplateNameLoc,
1615 TemplateId->RAngleLoc);
1616 TemplateId->Destroy();
1617 ConsumeToken();
1618 return false;
1619 }
1620
1621 Result.setConstructorTemplateId(TemplateId);
1622 ConsumeToken();
1623 return false;
1624 }
1625
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001626 // We have already parsed a template-id; consume the annotation token as
1627 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001628 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001629 ConsumeToken();
1630 return false;
1631 }
1632
1633 // unqualified-id:
1634 // operator-function-id
1635 // conversion-function-id
1636 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001637 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001638 return true;
1639
Sean Hunte6252d12009-11-28 08:58:14 +00001640 // If we have an operator-function-id or a literal-operator-id and the next
1641 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001642 //
1643 // template-id:
1644 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001645 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1646 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001647 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001648 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1649 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001650 Result,
1651 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001652
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001653 return false;
1654 }
1655
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001656 if (getLang().CPlusPlus &&
1657 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001658 // C++ [expr.unary.op]p10:
1659 // There is an ambiguity in the unary-expression ~X(), where X is a
1660 // class-name. The ambiguity is resolved in favor of treating ~ as a
1661 // unary complement rather than treating ~X as referring to a destructor.
1662
1663 // Parse the '~'.
1664 SourceLocation TildeLoc = ConsumeToken();
1665
1666 // Parse the class-name.
1667 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001668 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001669 return true;
1670 }
1671
1672 // Parse the class-name (or template-name in a simple-template-id).
1673 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1674 SourceLocation ClassNameLoc = ConsumeToken();
1675
Douglas Gregor0278e122010-05-05 05:58:24 +00001676 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001677 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001678 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001679 EnteringContext, ObjectType, Result,
1680 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001681 }
1682
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001683 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001684 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1685 ClassNameLoc, getCurScope(),
1686 SS, ObjectType,
1687 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001688 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001689 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001690
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001691 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001692 return false;
1693 }
1694
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001695 Diag(Tok, diag::err_expected_unqualified_id)
1696 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001697 return true;
1698}
1699
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001700/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1701/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001702///
Chris Lattner59232d32009-01-04 21:25:24 +00001703/// This method is called to parse the new expression after the optional :: has
1704/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1705/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001706///
1707/// new-expression:
1708/// '::'[opt] 'new' new-placement[opt] new-type-id
1709/// new-initializer[opt]
1710/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1711/// new-initializer[opt]
1712///
1713/// new-placement:
1714/// '(' expression-list ')'
1715///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001716/// new-type-id:
1717/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001718/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001719///
1720/// new-declarator:
1721/// ptr-operator new-declarator[opt]
1722/// direct-new-declarator
1723///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001724/// new-initializer:
1725/// '(' expression-list[opt] ')'
1726/// [C++0x] braced-init-list [TODO]
1727///
John McCall60d7b3a2010-08-24 06:29:42 +00001728ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001729Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1730 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1731 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001732
1733 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1734 // second form of new-expression. It can't be a new-type-id.
1735
Sebastian Redla55e52c2008-11-25 22:21:31 +00001736 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001737 SourceLocation PlacementLParen, PlacementRParen;
1738
Douglas Gregor4bd40312010-07-13 15:54:32 +00001739 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00001740 DeclSpec DS(AttrFactory);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001741 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001742 if (Tok.is(tok::l_paren)) {
1743 // If it turns out to be a placement, we change the type location.
1744 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001745 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1746 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001747 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001748 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001749
1750 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001751 if (PlacementRParen.isInvalid()) {
1752 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001753 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001754 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001755
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001756 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001757 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001758 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001759 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001760 } else {
1761 // We still need the type.
1762 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001763 TypeIdParens.setBegin(ConsumeParen());
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001764 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001765 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001766 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001767 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001768 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1769 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001770 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001771 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001772 if (ParseCXXTypeSpecifierSeq(DS))
1773 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001774 else {
1775 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001776 ParseDeclaratorInternal(DeclaratorInfo,
1777 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001778 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001779 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001780 }
1781 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001782 // A new-type-id is a simplified type-id, where essentially the
1783 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001784 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001785 if (ParseCXXTypeSpecifierSeq(DS))
1786 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001787 else {
1788 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001789 ParseDeclaratorInternal(DeclaratorInfo,
1790 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001791 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001792 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001793 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001794 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001795 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001796 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001797
Sebastian Redla55e52c2008-11-25 22:21:31 +00001798 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001799 SourceLocation ConstructorLParen, ConstructorRParen;
1800
1801 if (Tok.is(tok::l_paren)) {
1802 ConstructorLParen = ConsumeParen();
1803 if (Tok.isNot(tok::r_paren)) {
1804 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001805 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1806 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001807 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001808 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001809 }
1810 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001811 if (ConstructorRParen.isInvalid()) {
1812 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001813 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001814 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001815 }
1816
Sebastian Redlf53597f2009-03-15 17:47:39 +00001817 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1818 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001819 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001820 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001821}
1822
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001823/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1824/// passed to ParseDeclaratorInternal.
1825///
1826/// direct-new-declarator:
1827/// '[' expression ']'
1828/// direct-new-declarator '[' constant-expression ']'
1829///
Chris Lattner59232d32009-01-04 21:25:24 +00001830void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001831 // Parse the array dimensions.
1832 bool first = true;
1833 while (Tok.is(tok::l_square)) {
1834 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001835 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001836 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001837 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001838 // Recover
1839 SkipUntil(tok::r_square);
1840 return;
1841 }
1842 first = false;
1843
Sebastian Redlab197ba2009-02-09 18:23:29 +00001844 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall0b7e6782011-03-24 11:26:52 +00001845
1846 ParsedAttributes attrs(AttrFactory);
1847 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00001848 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001849 Size.release(), LLoc, RLoc),
John McCall0b7e6782011-03-24 11:26:52 +00001850 attrs, RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001851
Sebastian Redlab197ba2009-02-09 18:23:29 +00001852 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001853 return;
1854 }
1855}
1856
1857/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1858/// This ambiguity appears in the syntax of the C++ new operator.
1859///
1860/// new-expression:
1861/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1862/// new-initializer[opt]
1863///
1864/// new-placement:
1865/// '(' expression-list ')'
1866///
John McCallca0408f2010-08-23 06:44:23 +00001867bool Parser::ParseExpressionListOrTypeId(
1868 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001869 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001870 // The '(' was already consumed.
1871 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001872 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001873 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001874 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001875 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001876 }
1877
1878 // It's not a type, it has to be an expression list.
1879 // Discard the comma locations - ActOnCXXNew has enough parameters.
1880 CommaLocsTy CommaLocs;
1881 return ParseExpressionList(PlacementArgs, CommaLocs);
1882}
1883
1884/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1885/// to free memory allocated by new.
1886///
Chris Lattner59232d32009-01-04 21:25:24 +00001887/// This method is called to parse the 'delete' expression after the optional
1888/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1889/// and "Start" is its location. Otherwise, "Start" is the location of the
1890/// 'delete' token.
1891///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001892/// delete-expression:
1893/// '::'[opt] 'delete' cast-expression
1894/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001895ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001896Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1897 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1898 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001899
1900 // Array delete?
1901 bool ArrayDelete = false;
1902 if (Tok.is(tok::l_square)) {
1903 ArrayDelete = true;
1904 SourceLocation LHS = ConsumeBracket();
1905 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1906 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001907 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001908 }
1909
John McCall60d7b3a2010-08-24 06:29:42 +00001910 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001911 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001912 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001913
John McCall9ae2f072010-08-23 23:25:46 +00001914 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001915}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001916
Mike Stump1eb44332009-09-09 15:08:12 +00001917static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001918 switch(kind) {
John Wiegley20c0da72011-04-27 23:09:49 +00001919 default: assert(false && "Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001920 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001921 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00001922 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001923 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001924 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00001925 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001926 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1927 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1928 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00001929 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
1930 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001931 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00001932 case tok::kw___is_complete_type: return UTT_IsCompleteType;
1933 case tok::kw___is_compound: return UTT_IsCompound;
1934 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001935 case tok::kw___is_empty: return UTT_IsEmpty;
1936 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley20c0da72011-04-27 23:09:49 +00001937 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
1938 case tok::kw___is_function: return UTT_IsFunction;
1939 case tok::kw___is_fundamental: return UTT_IsFundamental;
1940 case tok::kw___is_integral: return UTT_IsIntegral;
1941 case tok::kw___is_lvalue_expr: return UTT_IsLvalueExpr;
1942 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
1943 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
1944 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
1945 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
1946 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001947 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00001948 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001949 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00001950 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001951 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00001952 case tok::kw___is_reference: return UTT_IsReference;
1953 case tok::kw___is_rvalue_expr: return UTT_IsRvalueExpr;
1954 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
1955 case tok::kw___is_scalar: return UTT_IsScalar;
1956 case tok::kw___is_signed: return UTT_IsSigned;
1957 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
1958 case tok::kw___is_trivial: return UTT_IsTrivial;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001959 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00001960 case tok::kw___is_unsigned: return UTT_IsUnsigned;
1961 case tok::kw___is_void: return UTT_IsVoid;
1962 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001963 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001964}
1965
1966static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1967 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001968 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001969 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00001970 case tok::kw___is_convertible: return BTT_IsConvertible;
1971 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00001972 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001973 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001974 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001975}
1976
John Wiegley21ff2e52011-04-28 00:16:57 +00001977static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
1978 switch(kind) {
1979 default: llvm_unreachable("Not a known binary type trait");
1980 case tok::kw___array_rank: return ATT_ArrayRank;
1981 case tok::kw___array_extent: return ATT_ArrayExtent;
1982 }
1983}
1984
John Wiegley55262202011-04-25 06:54:41 +00001985static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
1986 switch(kind) {
1987 default: assert(false && "Not a known unary expression trait.");
1988 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
1989 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
1990 }
1991}
1992
Sebastian Redl64b45f72009-01-05 20:52:13 +00001993/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1994/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1995/// templates.
1996///
1997/// primary-expression:
1998/// [GNU] unary-type-trait '(' type-id ')'
1999///
John McCall60d7b3a2010-08-24 06:29:42 +00002000ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002001 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2002 SourceLocation Loc = ConsumeToken();
2003
2004 SourceLocation LParen = Tok.getLocation();
2005 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2006 return ExprError();
2007
2008 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2009 // there will be cryptic errors about mismatched parentheses and missing
2010 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002011 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002012
2013 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2014
Douglas Gregor809070a2009-02-18 17:45:20 +00002015 if (Ty.isInvalid())
2016 return ExprError();
2017
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002018 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00002019}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002020
Francois Pichet6ad6f282010-12-07 00:08:36 +00002021/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2022/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2023/// templates.
2024///
2025/// primary-expression:
2026/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2027///
2028ExprResult Parser::ParseBinaryTypeTrait() {
2029 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2030 SourceLocation Loc = ConsumeToken();
2031
2032 SourceLocation LParen = Tok.getLocation();
2033 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2034 return ExprError();
2035
2036 TypeResult LhsTy = ParseTypeName();
2037 if (LhsTy.isInvalid()) {
2038 SkipUntil(tok::r_paren);
2039 return ExprError();
2040 }
2041
2042 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2043 SkipUntil(tok::r_paren);
2044 return ExprError();
2045 }
2046
2047 TypeResult RhsTy = ParseTypeName();
2048 if (RhsTy.isInvalid()) {
2049 SkipUntil(tok::r_paren);
2050 return ExprError();
2051 }
2052
2053 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2054
2055 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
2056}
2057
John Wiegley21ff2e52011-04-28 00:16:57 +00002058/// ParseArrayTypeTrait - Parse the built-in array type-trait
2059/// pseudo-functions.
2060///
2061/// primary-expression:
2062/// [Embarcadero] '__array_rank' '(' type-id ')'
2063/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2064///
2065ExprResult Parser::ParseArrayTypeTrait() {
2066 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2067 SourceLocation Loc = ConsumeToken();
2068
2069 SourceLocation LParen = Tok.getLocation();
2070 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2071 return ExprError();
2072
2073 TypeResult Ty = ParseTypeName();
2074 if (Ty.isInvalid()) {
2075 SkipUntil(tok::comma);
2076 SkipUntil(tok::r_paren);
2077 return ExprError();
2078 }
2079
2080 switch (ATT) {
2081 case ATT_ArrayRank: {
2082 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2083 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, RParen);
2084 }
2085 case ATT_ArrayExtent: {
2086 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2087 SkipUntil(tok::r_paren);
2088 return ExprError();
2089 }
2090
2091 ExprResult DimExpr = ParseExpression();
2092 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2093
2094 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), RParen);
2095 }
2096 default:
2097 break;
2098 }
2099 return ExprError();
2100}
2101
John Wiegley55262202011-04-25 06:54:41 +00002102/// ParseExpressionTrait - Parse built-in expression-trait
2103/// pseudo-functions like __is_lvalue_expr( xxx ).
2104///
2105/// primary-expression:
2106/// [Embarcadero] expression-trait '(' expression ')'
2107///
2108ExprResult Parser::ParseExpressionTrait() {
2109 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2110 SourceLocation Loc = ConsumeToken();
2111
2112 SourceLocation LParen = Tok.getLocation();
2113 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2114 return ExprError();
2115
2116 ExprResult Expr = ParseExpression();
2117
2118 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2119
2120 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), RParen);
2121}
2122
2123
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002124/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2125/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2126/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002127ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002128Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002129 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002130 SourceLocation LParenLoc,
2131 SourceLocation &RParenLoc) {
2132 assert(getLang().CPlusPlus && "Should only be called for C++!");
2133 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2134 assert(isTypeIdInParens() && "Not a type-id!");
2135
John McCall60d7b3a2010-08-24 06:29:42 +00002136 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002137 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002138
2139 // We need to disambiguate a very ugly part of the C++ syntax:
2140 //
2141 // (T())x; - type-id
2142 // (T())*x; - type-id
2143 // (T())/x; - expression
2144 // (T()); - expression
2145 //
2146 // The bad news is that we cannot use the specialized tentative parser, since
2147 // it can only verify that the thing inside the parens can be parsed as
2148 // type-id, it is not useful for determining the context past the parens.
2149 //
2150 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002151 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002152 //
2153 // It uses a scheme similar to parsing inline methods. The parenthesized
2154 // tokens are cached, the context that follows is determined (possibly by
2155 // parsing a cast-expression), and then we re-introduce the cached tokens
2156 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002157
Mike Stump1eb44332009-09-09 15:08:12 +00002158 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002159 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002160
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002161 // Store the tokens of the parentheses. We will parse them after we determine
2162 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002163 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002164 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002165 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2166 return ExprError();
2167 }
2168
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002169 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002170 ParseAs = CompoundLiteral;
2171 } else {
2172 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002173 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2174 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2175 NotCastExpr = true;
2176 } else {
2177 // Try parsing the cast-expression that may follow.
2178 // If it is not a cast-expression, NotCastExpr will be true and no token
2179 // will be consumed.
2180 Result = ParseCastExpression(false/*isUnaryExpression*/,
2181 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002182 NotCastExpr,
2183 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002184 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002185
2186 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2187 // an expression.
2188 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002189 }
2190
Mike Stump1eb44332009-09-09 15:08:12 +00002191 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002192 Toks.push_back(Tok);
2193 // Re-enter the stored parenthesized tokens into the token stream, so we may
2194 // parse them now.
2195 PP.EnterTokenStream(Toks.data(), Toks.size(),
2196 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2197 // Drop the current token and bring the first cached one. It's the same token
2198 // as when we entered this function.
2199 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002200
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002201 if (ParseAs >= CompoundLiteral) {
2202 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002203
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002204 // Match the ')'.
2205 if (Tok.is(tok::r_paren))
2206 RParenLoc = ConsumeParen();
2207 else
2208 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002209
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002210 if (ParseAs == CompoundLiteral) {
2211 ExprType = CompoundLiteral;
2212 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2213 }
Mike Stump1eb44332009-09-09 15:08:12 +00002214
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002215 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2216 assert(ParseAs == CastExpr);
2217
2218 if (Ty.isInvalid())
2219 return ExprError();
2220
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002221 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002222
2223 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002224 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002225 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002226 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002227 return move(Result);
2228 }
Mike Stump1eb44332009-09-09 15:08:12 +00002229
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002230 // Not a compound literal, and not followed by a cast-expression.
2231 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002232
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002233 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002234 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002235 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002236 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002237
2238 // Match the ')'.
2239 if (Result.isInvalid()) {
2240 SkipUntil(tok::r_paren);
2241 return ExprError();
2242 }
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002244 if (Tok.is(tok::r_paren))
2245 RParenLoc = ConsumeParen();
2246 else
2247 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2248
2249 return move(Result);
2250}