blob: 662a5abe4e72c837a98d3f8b3be088bcd18acff8 [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.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000248 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000249 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
250 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000251 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000252 }
253
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000254 // Consume the template-id token.
255 ConsumeToken();
256
257 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
258 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000260 if (!HasScopeSpecifier)
261 HasScopeSpecifier = true;
262
263 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
264 TemplateId->getTemplateArgs(),
265 TemplateId->NumArgs);
266
267 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
268 /*FIXME:*/SourceLocation(),
269 SS,
270 TemplateId->Template,
271 TemplateId->TemplateNameLoc,
272 TemplateId->LAngleLoc,
273 TemplateArgsPtr,
274 TemplateId->RAngleLoc,
275 CCLoc,
276 EnteringContext)) {
277 SourceLocation StartLoc
278 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
279 : TemplateId->TemplateNameLoc;
280 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000281 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000282
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000283 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000284 }
285
Chris Lattner5c7f7862009-06-26 03:52:38 +0000286
287 // The rest of the nested-name-specifier possibilities start with
288 // tok::identifier.
289 if (Tok.isNot(tok::identifier))
290 break;
291
292 IdentifierInfo &II = *Tok.getIdentifierInfo();
293
294 // nested-name-specifier:
295 // type-name '::'
296 // namespace-name '::'
297 // nested-name-specifier identifier '::'
298 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000299
300 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
301 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000302 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000303 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
304 Tok.getLocation(),
305 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000306 EnteringContext) &&
307 // If the token after the colon isn't an identifier, it's still an
308 // error, but they probably meant something else strange so don't
309 // recover like this.
310 PP.LookAhead(1).is(tok::identifier)) {
311 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000312 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000313
314 // Recover as if the user wrote '::'.
315 Next.setKind(tok::coloncolon);
316 }
Chris Lattner46646492009-12-07 01:36:53 +0000317 }
318
Chris Lattner5c7f7862009-06-26 03:52:38 +0000319 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000320 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000321 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000322 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000323 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000324 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000325 }
326
Chris Lattner5c7f7862009-06-26 03:52:38 +0000327 // We have an identifier followed by a '::'. Lookup this name
328 // as the name in a nested-name-specifier.
329 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000330 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
331 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000332 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000334 HasScopeSpecifier = true;
335 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
336 ObjectType, EnteringContext, SS))
337 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
338
Chris Lattner5c7f7862009-06-26 03:52:38 +0000339 continue;
340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Richard Smithea698b32011-04-14 21:45:45 +0000342 // Check for '<::' which should be '< ::' instead of '[:' when following
343 // a template name.
344 if (Next.is(tok::l_square) && Next.getLength() == 2) {
345 Token SecondToken = GetLookAheadToken(2);
346 if (SecondToken.is(tok::colon) &&
347 AreTokensAdjacent(PP, Next, SecondToken)) {
348 TemplateTy Template;
349 UnqualifiedId TemplateName;
350 TemplateName.setIdentifier(&II, Tok.getLocation());
351 bool MemberOfUnknownSpecialization;
352 if (Actions.isTemplateName(getCurScope(), SS,
353 /*hasTemplateKeyword=*/false,
354 TemplateName,
355 ObjectType,
356 EnteringContext,
357 Template,
358 MemberOfUnknownSpecialization)) {
359 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
360 /*AtDigraph*/false);
361 }
362 }
363 }
364
Chris Lattner5c7f7862009-06-26 03:52:38 +0000365 // nested-name-specifier:
366 // type-name '<'
367 if (Next.is(tok::less)) {
368 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000369 UnqualifiedId TemplateName;
370 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000371 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000372 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000373 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000374 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000375 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000376 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000377 Template,
378 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000379 // We have found a template name, so annotate this this token
380 // with a template-id annotation. We do not permit the
381 // template-id to be translated into a type annotation,
382 // because some clients (e.g., the parsing of class template
383 // specializations) still want to see the original template-id
384 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000385 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000386 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000387 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000388 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000389 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000390 }
391
392 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000393 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000394 // We have something like t::getAs<T>, where getAs is a
395 // member of an unknown specialization. However, this will only
396 // parse correctly as a template, so suggest the keyword 'template'
397 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000398 unsigned DiagID = diag::err_missing_dependent_template_keyword;
399 if (getLang().Microsoft)
Francois Pichetcf320c62011-04-22 08:25:24 +0000400 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000401
402 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000403 << II.getName()
404 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
405
Douglas Gregord6ab2322010-06-16 23:00:59 +0000406 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000407 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000408 Tok.getLocation(), SS,
409 TemplateName, ObjectType,
410 EnteringContext, Template)) {
411 // Consume the identifier.
412 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000413 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000414 SourceLocation(), false))
415 return true;
416 }
417 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000418 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000419
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000420 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000421 }
422 }
423
Douglas Gregor39a8de12009-02-25 19:37:18 +0000424 // We don't have any tokens that form the beginning of a
425 // nested-name-specifier, so we're done.
426 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Douglas Gregord4dca082010-02-24 18:44:31 +0000429 // Even if we didn't see any pieces of a nested-name-specifier, we
430 // still check whether there is a tilde in this position, which
431 // indicates a potential pseudo-destructor.
432 if (CheckForDestructor && Tok.is(tok::tilde))
433 *MayBePseudoDestructor = true;
434
John McCall9ba61662010-02-26 08:45:28 +0000435 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000436}
437
438/// ParseCXXIdExpression - Handle id-expression.
439///
440/// id-expression:
441/// unqualified-id
442/// qualified-id
443///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000444/// qualified-id:
445/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
446/// '::' identifier
447/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000448/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000449///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000450/// NOTE: The standard specifies that, for qualified-id, the parser does not
451/// expect:
452///
453/// '::' conversion-function-id
454/// '::' '~' class-name
455///
456/// This may cause a slight inconsistency on diagnostics:
457///
458/// class C {};
459/// namespace A {}
460/// void f() {
461/// :: A :: ~ C(); // Some Sema error about using destructor with a
462/// // namespace.
463/// :: ~ C(); // Some Parser error like 'unexpected ~'.
464/// }
465///
466/// We simplify the parser a bit and make it work like:
467///
468/// qualified-id:
469/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
470/// '::' unqualified-id
471///
472/// That way Sema can handle and report similar errors for namespaces and the
473/// global scope.
474///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000475/// The isAddressOfOperand parameter indicates that this id-expression is a
476/// direct operand of the address-of operator. This is, besides member contexts,
477/// the only place where a qualified-id naming a non-static class member may
478/// appear.
479///
John McCall60d7b3a2010-08-24 06:29:42 +0000480ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000481 // qualified-id:
482 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
483 // '::' unqualified-id
484 //
485 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000486 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000487
488 UnqualifiedId Name;
489 if (ParseUnqualifiedId(SS,
490 /*EnteringContext=*/false,
491 /*AllowDestructorName=*/false,
492 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000493 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000494 Name))
495 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000496
497 // This is only the direct operand of an & operator if it is not
498 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000499 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
500 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000501
Douglas Gregor23c94db2010-07-02 17:43:08 +0000502 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000503 isAddressOfOperand);
504
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000505}
506
Reid Spencer5f016e22007-07-11 17:01:13 +0000507/// ParseCXXCasts - This handles the various ways to cast expressions to another
508/// type.
509///
510/// postfix-expression: [C++ 5.2p1]
511/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
512/// 'static_cast' '<' type-name '>' '(' expression ')'
513/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
514/// 'const_cast' '<' type-name '>' '(' expression ')'
515///
John McCall60d7b3a2010-08-24 06:29:42 +0000516ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 tok::TokenKind Kind = Tok.getKind();
518 const char *CastName = 0; // For error messages
519
520 switch (Kind) {
521 default: assert(0 && "Unknown C++ cast!"); abort();
522 case tok::kw_const_cast: CastName = "const_cast"; break;
523 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
524 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
525 case tok::kw_static_cast: CastName = "static_cast"; break;
526 }
527
528 SourceLocation OpLoc = ConsumeToken();
529 SourceLocation LAngleBracketLoc = Tok.getLocation();
530
Richard Smithea698b32011-04-14 21:45:45 +0000531 // Check for "<::" which is parsed as "[:". If found, fix token stream,
532 // diagnose error, suggest fix, and recover parsing.
533 Token Next = NextToken();
534 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
535 AreTokensAdjacent(PP, Tok, Next))
536 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
537
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000539 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000540
Douglas Gregor809070a2009-02-18 17:45:20 +0000541 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 SourceLocation RAngleBracketLoc = Tok.getLocation();
543
Chris Lattner1ab3b962008-11-18 07:48:38 +0000544 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000545 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000546
547 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
548
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000549 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
550 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000551
John McCall60d7b3a2010-08-24 06:29:42 +0000552 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000554 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000555 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000556
Douglas Gregor809070a2009-02-18 17:45:20 +0000557 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000558 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000559 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000560 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000561 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000562
Sebastian Redl20df9b72008-12-11 22:51:44 +0000563 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000564}
565
Sebastian Redlc42e1182008-11-11 11:37:55 +0000566/// ParseCXXTypeid - This handles the C++ typeid expression.
567///
568/// postfix-expression: [C++ 5.2p1]
569/// 'typeid' '(' expression ')'
570/// 'typeid' '(' type-id ')'
571///
John McCall60d7b3a2010-08-24 06:29:42 +0000572ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000573 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
574
575 SourceLocation OpLoc = ConsumeToken();
576 SourceLocation LParenLoc = Tok.getLocation();
577 SourceLocation RParenLoc;
578
579 // typeid expressions are always parenthesized.
580 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
581 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000582 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000583
John McCall60d7b3a2010-08-24 06:29:42 +0000584 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000585
586 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000587 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000588
589 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000590 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000591
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000592 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000593 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000594
595 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000596 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000597 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000598 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000599 // When typeid is applied to an expression other than an lvalue of a
600 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000601 // operand (Clause 5).
602 //
Mike Stump1eb44332009-09-09 15:08:12 +0000603 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000604 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000605 // we the expression is potentially potentially evaluated.
606 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000607 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000608 Result = ParseExpression();
609
610 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000611 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000612 SkipUntil(tok::r_paren);
613 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000614 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
615 if (RParenLoc.isInvalid())
616 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000617
618 // If we are a foo<int> that identifies a single function, resolve it now...
619 Expr* e = Result.get();
620 if (e->getType() == Actions.Context.OverloadTy) {
621 ExprResult er =
622 Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
623 if (er.isUsable())
624 Result = er.release();
625 }
Sebastian Redlc42e1182008-11-11 11:37:55 +0000626 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000627 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000628 }
629 }
630
Sebastian Redl20df9b72008-12-11 22:51:44 +0000631 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000632}
633
Francois Pichet01b7c302010-09-08 12:20:18 +0000634/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
635///
636/// '__uuidof' '(' expression ')'
637/// '__uuidof' '(' type-id ')'
638///
639ExprResult Parser::ParseCXXUuidof() {
640 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
641
642 SourceLocation OpLoc = ConsumeToken();
643 SourceLocation LParenLoc = Tok.getLocation();
644 SourceLocation RParenLoc;
645
646 // __uuidof expressions are always parenthesized.
647 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
648 "__uuidof"))
649 return ExprError();
650
651 ExprResult Result;
652
653 if (isTypeIdInParens()) {
654 TypeResult Ty = ParseTypeName();
655
656 // Match the ')'.
657 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
658
659 if (Ty.isInvalid())
660 return ExprError();
661
662 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
663 Ty.get().getAsOpaquePtr(), RParenLoc);
664 } else {
665 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
666 Result = ParseExpression();
667
668 // Match the ')'.
669 if (Result.isInvalid())
670 SkipUntil(tok::r_paren);
671 else {
672 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
673
674 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
675 Result.release(), RParenLoc);
676 }
677 }
678
679 return move(Result);
680}
681
Douglas Gregord4dca082010-02-24 18:44:31 +0000682/// \brief Parse a C++ pseudo-destructor expression after the base,
683/// . or -> operator, and nested-name-specifier have already been
684/// parsed.
685///
686/// postfix-expression: [C++ 5.2]
687/// postfix-expression . pseudo-destructor-name
688/// postfix-expression -> pseudo-destructor-name
689///
690/// pseudo-destructor-name:
691/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
692/// ::[opt] nested-name-specifier template simple-template-id ::
693/// ~type-name
694/// ::[opt] nested-name-specifier[opt] ~type-name
695///
John McCall60d7b3a2010-08-24 06:29:42 +0000696ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000697Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
698 tok::TokenKind OpKind,
699 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000700 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000701 // We're parsing either a pseudo-destructor-name or a dependent
702 // member access that has the same form as a
703 // pseudo-destructor-name. We parse both in the same way and let
704 // the action model sort them out.
705 //
706 // Note that the ::[opt] nested-name-specifier[opt] has already
707 // been parsed, and if there was a simple-template-id, it has
708 // been coalesced into a template-id annotation token.
709 UnqualifiedId FirstTypeName;
710 SourceLocation CCLoc;
711 if (Tok.is(tok::identifier)) {
712 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
713 ConsumeToken();
714 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
715 CCLoc = ConsumeToken();
716 } else if (Tok.is(tok::annot_template_id)) {
717 FirstTypeName.setTemplateId(
718 (TemplateIdAnnotation *)Tok.getAnnotationValue());
719 ConsumeToken();
720 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
721 CCLoc = ConsumeToken();
722 } else {
723 FirstTypeName.setIdentifier(0, SourceLocation());
724 }
725
726 // Parse the tilde.
727 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
728 SourceLocation TildeLoc = ConsumeToken();
729 if (!Tok.is(tok::identifier)) {
730 Diag(Tok, diag::err_destructor_tilde_identifier);
731 return ExprError();
732 }
733
734 // Parse the second type.
735 UnqualifiedId SecondTypeName;
736 IdentifierInfo *Name = Tok.getIdentifierInfo();
737 SourceLocation NameLoc = ConsumeToken();
738 SecondTypeName.setIdentifier(Name, NameLoc);
739
740 // If there is a '<', the second type name is a template-id. Parse
741 // it as such.
742 if (Tok.is(tok::less) &&
743 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000744 SecondTypeName, /*AssumeTemplateName=*/true,
745 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000746 return ExprError();
747
John McCall9ae2f072010-08-23 23:25:46 +0000748 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
749 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000750 SS, FirstTypeName, CCLoc,
751 TildeLoc, SecondTypeName,
752 Tok.is(tok::l_paren));
753}
754
Reid Spencer5f016e22007-07-11 17:01:13 +0000755/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
756///
757/// boolean-literal: [C++ 2.13.5]
758/// 'true'
759/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000760ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000762 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000763}
Chris Lattner50dd2892008-02-26 00:51:44 +0000764
765/// ParseThrowExpression - This handles the C++ throw expression.
766///
767/// throw-expression: [C++ 15]
768/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000769ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000770 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000771 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000772
Chris Lattner2a2819a2008-04-06 06:02:23 +0000773 // If the current token isn't the start of an assignment-expression,
774 // then the expression is not present. This handles things like:
775 // "C ? throw : (void)42", which is crazy but legal.
776 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
777 case tok::semi:
778 case tok::r_paren:
779 case tok::r_square:
780 case tok::r_brace:
781 case tok::colon:
782 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000783 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000784
Chris Lattner2a2819a2008-04-06 06:02:23 +0000785 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000786 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000787 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000788 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000789 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000790}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000791
792/// ParseCXXThis - This handles the C++ 'this' pointer.
793///
794/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
795/// a non-lvalue expression whose value is the address of the object for which
796/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000797ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000798 assert(Tok.is(tok::kw_this) && "Not 'this'!");
799 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000800 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000801}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000802
803/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
804/// Can be interpreted either as function-style casting ("int(x)")
805/// or class type construction ("ClassType(x,y,z)")
806/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000807/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000808///
809/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000810/// simple-type-specifier '(' expression-list[opt] ')'
811/// [C++0x] simple-type-specifier braced-init-list
812/// typename-specifier '(' expression-list[opt] ')'
813/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000814///
John McCall60d7b3a2010-08-24 06:29:42 +0000815ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000816Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000817 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000818 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000819
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000820 assert((Tok.is(tok::l_paren) ||
821 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
822 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000823
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000824 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000825
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000826 // FIXME: Convert to a proper type construct expression.
827 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000828
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000829 } else {
830 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
831
832 SourceLocation LParenLoc = ConsumeParen();
833
834 ExprVector Exprs(Actions);
835 CommaLocsTy CommaLocs;
836
837 if (Tok.isNot(tok::r_paren)) {
838 if (ParseExpressionList(Exprs, CommaLocs)) {
839 SkipUntil(tok::r_paren);
840 return ExprError();
841 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000842 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000843
844 // Match the ')'.
845 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
846
847 // TypeRep could be null, if it references an invalid typedef.
848 if (!TypeRep)
849 return ExprError();
850
851 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
852 "Unexpected number of commas!");
853 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
854 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000855 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000856}
857
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000858/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000859///
860/// condition:
861/// expression
862/// type-specifier-seq declarator '=' assignment-expression
863/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
864/// '=' assignment-expression
865///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000866/// \param ExprResult if the condition was parsed as an expression, the
867/// parsed expression.
868///
869/// \param DeclResult if the condition was parsed as a declaration, the
870/// parsed declaration.
871///
Douglas Gregor586596f2010-05-06 17:25:47 +0000872/// \param Loc The location of the start of the statement that requires this
873/// condition, e.g., the "for" in a for loop.
874///
875/// \param ConvertToBoolean Whether the condition expression should be
876/// converted to a boolean value.
877///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000878/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000879bool Parser::ParseCXXCondition(ExprResult &ExprOut,
880 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000881 SourceLocation Loc,
882 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000883 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000884 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000885 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000886 }
887
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000888 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000889 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000890 ExprOut = ParseExpression(); // expression
891 DeclOut = 0;
892 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000893 return true;
894
895 // If required, convert to a boolean value.
896 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000897 ExprOut
898 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
899 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000900 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000901
902 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +0000903 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000904 ParseSpecifierQualifierList(DS);
905
906 // declarator
907 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
908 ParseDeclarator(DeclaratorInfo);
909
910 // simple-asm-expr[opt]
911 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000912 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000913 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000914 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000915 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000916 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000917 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000918 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000919 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000920 }
921
922 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000923 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000924
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000925 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000926 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000927 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000928 DeclOut = Dcl.get();
929 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000930
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000931 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000932 if (isTokenEqualOrMistypedEqualEqual(
933 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000934 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000935 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000936 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000937 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
938 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000939 } else {
940 // FIXME: C++0x allows a braced-init-list
941 Diag(Tok, diag::err_expected_equal_after_declarator);
942 }
943
Douglas Gregor586596f2010-05-06 17:25:47 +0000944 // FIXME: Build a reference to this declaration? Convert it to bool?
945 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +0000946
947 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +0000948
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000949 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000950}
951
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000952/// \brief Determine whether the current token starts a C++
953/// simple-type-specifier.
954bool Parser::isCXXSimpleTypeSpecifier() const {
955 switch (Tok.getKind()) {
956 case tok::annot_typename:
957 case tok::kw_short:
958 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000959 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000960 case tok::kw_signed:
961 case tok::kw_unsigned:
962 case tok::kw_void:
963 case tok::kw_char:
964 case tok::kw_int:
965 case tok::kw_float:
966 case tok::kw_double:
967 case tok::kw_wchar_t:
968 case tok::kw_char16_t:
969 case tok::kw_char32_t:
970 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +0000971 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000972 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000973 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000974 return true;
975
976 default:
977 break;
978 }
979
980 return false;
981}
982
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000983/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
984/// This should only be called when the current token is known to be part of
985/// simple-type-specifier.
986///
987/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000988/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000989/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
990/// char
991/// wchar_t
992/// bool
993/// short
994/// int
995/// long
996/// signed
997/// unsigned
998/// float
999/// double
1000/// void
1001/// [GNU] typeof-specifier
1002/// [C++0x] auto [TODO]
1003///
1004/// type-name:
1005/// class-name
1006/// enum-name
1007/// typedef-name
1008///
1009void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1010 DS.SetRangeStart(Tok.getLocation());
1011 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001012 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001013 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001015 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001016 case tok::identifier: // foo::bar
1017 case tok::coloncolon: // ::foo::bar
1018 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001019 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001020 assert(0 && "Not a simple-type-specifier token!");
1021 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +00001022
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001023 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001024 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001025 if (getTypeAnnotation(Tok))
1026 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1027 getTypeAnnotation(Tok));
1028 else
1029 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001030
1031 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1032 ConsumeToken();
1033
1034 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1035 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1036 // Objective-C interface. If we don't have Objective-C or a '<', this is
1037 // just a normal reference to a typedef name.
1038 if (Tok.is(tok::less) && getLang().ObjC1)
1039 ParseObjCProtocolQualifiers(DS);
1040
1041 DS.Finish(Diags, PP);
1042 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001043 }
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001045 // builtin types
1046 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001047 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001048 break;
1049 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001050 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001051 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001052 case tok::kw___int64:
1053 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1054 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001055 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001056 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001057 break;
1058 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001059 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001060 break;
1061 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001062 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001063 break;
1064 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001065 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001066 break;
1067 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001068 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001069 break;
1070 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001071 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001072 break;
1073 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001074 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001075 break;
1076 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001077 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001078 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001079 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001080 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001081 break;
1082 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001083 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001084 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001085 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001086 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001087 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001089 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001090 // GNU typeof support.
1091 case tok::kw_typeof:
1092 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001093 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001094 return;
1095 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001096 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001097 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1098 else
1099 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001100 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001101 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001102}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001103
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001104/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1105/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1106/// e.g., "const short int". Note that the DeclSpec is *not* finished
1107/// by parsing the type-specifier-seq, because these sequences are
1108/// typically followed by some form of declarator. Returns true and
1109/// emits diagnostics if this is not a type-specifier-seq, false
1110/// otherwise.
1111///
1112/// type-specifier-seq: [C++ 8.1]
1113/// type-specifier type-specifier-seq[opt]
1114///
1115bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1116 DS.SetRangeStart(Tok.getLocation());
1117 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001118 unsigned DiagID;
1119 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001120
1121 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001122 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1123 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001124 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001125 return true;
1126 }
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Sebastian Redld9bafa72010-02-03 21:21:43 +00001128 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1129 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1130 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001131
Douglas Gregor396a9f22010-02-24 23:13:13 +00001132 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001133 return false;
1134}
1135
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001136/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1137/// some form.
1138///
1139/// This routine is invoked when a '<' is encountered after an identifier or
1140/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1141/// whether the unqualified-id is actually a template-id. This routine will
1142/// then parse the template arguments and form the appropriate template-id to
1143/// return to the caller.
1144///
1145/// \param SS the nested-name-specifier that precedes this template-id, if
1146/// we're actually parsing a qualified-id.
1147///
1148/// \param Name for constructor and destructor names, this is the actual
1149/// identifier that may be a template-name.
1150///
1151/// \param NameLoc the location of the class-name in a constructor or
1152/// destructor.
1153///
1154/// \param EnteringContext whether we're entering the scope of the
1155/// nested-name-specifier.
1156///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001157/// \param ObjectType if this unqualified-id occurs within a member access
1158/// expression, the type of the base object whose member is being accessed.
1159///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001160/// \param Id as input, describes the template-name or operator-function-id
1161/// that precedes the '<'. If template arguments were parsed successfully,
1162/// will be updated with the template-id.
1163///
Douglas Gregord4dca082010-02-24 18:44:31 +00001164/// \param AssumeTemplateId When true, this routine will assume that the name
1165/// refers to a template without performing name lookup to verify.
1166///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001167/// \returns true if a parse error occurred, false otherwise.
1168bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1169 IdentifierInfo *Name,
1170 SourceLocation NameLoc,
1171 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001172 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001173 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001174 bool AssumeTemplateId,
1175 SourceLocation TemplateKWLoc) {
1176 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1177 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001178
1179 TemplateTy Template;
1180 TemplateNameKind TNK = TNK_Non_template;
1181 switch (Id.getKind()) {
1182 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001183 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001184 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001185 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001186 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001187 Id, ObjectType, EnteringContext,
1188 Template);
1189 if (TNK == TNK_Non_template)
1190 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001191 } else {
1192 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001193 TNK = Actions.isTemplateName(getCurScope(), SS,
1194 TemplateKWLoc.isValid(), Id,
1195 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001196 MemberOfUnknownSpecialization);
1197
1198 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1199 ObjectType && IsTemplateArgumentList()) {
1200 // We have something like t->getAs<T>(), where getAs is a
1201 // member of an unknown specialization. However, this will only
1202 // parse correctly as a template, so suggest the keyword 'template'
1203 // before 'getAs' and treat this as a dependent template name.
1204 std::string Name;
1205 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1206 Name = Id.Identifier->getName();
1207 else {
1208 Name = "operator ";
1209 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1210 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1211 else
1212 Name += Id.Identifier->getName();
1213 }
1214 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1215 << Name
1216 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001217 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001218 SS, Id, ObjectType,
1219 EnteringContext, Template);
1220 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001221 return true;
1222 }
1223 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001224 break;
1225
Douglas Gregor014e88d2009-11-03 23:16:33 +00001226 case UnqualifiedId::IK_ConstructorName: {
1227 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001228 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001229 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001230 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1231 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001232 EnteringContext, Template,
1233 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001234 break;
1235 }
1236
Douglas Gregor014e88d2009-11-03 23:16:33 +00001237 case UnqualifiedId::IK_DestructorName: {
1238 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001239 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001240 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001241 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001242 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001243 TemplateName, ObjectType,
1244 EnteringContext, Template);
1245 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001246 return true;
1247 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001248 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1249 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001250 EnteringContext, Template,
1251 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001252
John McCallb3d87482010-08-24 05:47:05 +00001253 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001254 Diag(NameLoc, diag::err_destructor_template_id)
1255 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001256 return true;
1257 }
1258 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001259 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001260 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001261
1262 default:
1263 return false;
1264 }
1265
1266 if (TNK == TNK_Non_template)
1267 return false;
1268
1269 // Parse the enclosed template argument list.
1270 SourceLocation LAngleLoc, RAngleLoc;
1271 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001272 if (Tok.is(tok::less) &&
1273 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001274 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001275 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001276 RAngleLoc))
1277 return true;
1278
1279 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001280 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1281 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001282 // Form a parsed representation of the template-id to be stored in the
1283 // UnqualifiedId.
1284 TemplateIdAnnotation *TemplateId
1285 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1286
1287 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1288 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001289 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001290 TemplateId->TemplateNameLoc = Id.StartLocation;
1291 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001292 TemplateId->Name = 0;
1293 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1294 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001295 }
1296
Douglas Gregor059101f2011-03-02 00:47:37 +00001297 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001298 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001299 TemplateId->Kind = TNK;
1300 TemplateId->LAngleLoc = LAngleLoc;
1301 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001302 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001303 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001304 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001305 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001306
1307 Id.setTemplateId(TemplateId);
1308 return false;
1309 }
1310
1311 // Bundle the template arguments together.
1312 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001313 TemplateArgs.size());
1314
1315 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001316 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001317 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001318 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001319 RAngleLoc);
1320 if (Type.isInvalid())
1321 return true;
1322
1323 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1324 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1325 else
1326 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1327
1328 return false;
1329}
1330
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001331/// \brief Parse an operator-function-id or conversion-function-id as part
1332/// of a C++ unqualified-id.
1333///
1334/// This routine is responsible only for parsing the operator-function-id or
1335/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001336///
1337/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001338/// operator-function-id: [C++ 13.5]
1339/// 'operator' operator
1340///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001341/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001342/// new delete new[] delete[]
1343/// + - * / % ^ & | ~
1344/// ! = < > += -= *= /= %=
1345/// ^= &= |= << >> >>= <<= == !=
1346/// <= >= && || ++ -- , ->* ->
1347/// () []
1348///
1349/// conversion-function-id: [C++ 12.3.2]
1350/// operator conversion-type-id
1351///
1352/// conversion-type-id:
1353/// type-specifier-seq conversion-declarator[opt]
1354///
1355/// conversion-declarator:
1356/// ptr-operator conversion-declarator[opt]
1357/// \endcode
1358///
1359/// \param The nested-name-specifier that preceded this unqualified-id. If
1360/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1361///
1362/// \param EnteringContext whether we are entering the scope of the
1363/// nested-name-specifier.
1364///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001365/// \param ObjectType if this unqualified-id occurs within a member access
1366/// expression, the type of the base object whose member is being accessed.
1367///
1368/// \param Result on a successful parse, contains the parsed unqualified-id.
1369///
1370/// \returns true if parsing fails, false otherwise.
1371bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001372 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001373 UnqualifiedId &Result) {
1374 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1375
1376 // Consume the 'operator' keyword.
1377 SourceLocation KeywordLoc = ConsumeToken();
1378
1379 // Determine what kind of operator name we have.
1380 unsigned SymbolIdx = 0;
1381 SourceLocation SymbolLocations[3];
1382 OverloadedOperatorKind Op = OO_None;
1383 switch (Tok.getKind()) {
1384 case tok::kw_new:
1385 case tok::kw_delete: {
1386 bool isNew = Tok.getKind() == tok::kw_new;
1387 // Consume the 'new' or 'delete'.
1388 SymbolLocations[SymbolIdx++] = ConsumeToken();
1389 if (Tok.is(tok::l_square)) {
1390 // Consume the '['.
1391 SourceLocation LBracketLoc = ConsumeBracket();
1392 // Consume the ']'.
1393 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1394 LBracketLoc);
1395 if (RBracketLoc.isInvalid())
1396 return true;
1397
1398 SymbolLocations[SymbolIdx++] = LBracketLoc;
1399 SymbolLocations[SymbolIdx++] = RBracketLoc;
1400 Op = isNew? OO_Array_New : OO_Array_Delete;
1401 } else {
1402 Op = isNew? OO_New : OO_Delete;
1403 }
1404 break;
1405 }
1406
1407#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1408 case tok::Token: \
1409 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1410 Op = OO_##Name; \
1411 break;
1412#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1413#include "clang/Basic/OperatorKinds.def"
1414
1415 case tok::l_paren: {
1416 // Consume the '('.
1417 SourceLocation LParenLoc = ConsumeParen();
1418 // Consume the ')'.
1419 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1420 LParenLoc);
1421 if (RParenLoc.isInvalid())
1422 return true;
1423
1424 SymbolLocations[SymbolIdx++] = LParenLoc;
1425 SymbolLocations[SymbolIdx++] = RParenLoc;
1426 Op = OO_Call;
1427 break;
1428 }
1429
1430 case tok::l_square: {
1431 // Consume the '['.
1432 SourceLocation LBracketLoc = ConsumeBracket();
1433 // Consume the ']'.
1434 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1435 LBracketLoc);
1436 if (RBracketLoc.isInvalid())
1437 return true;
1438
1439 SymbolLocations[SymbolIdx++] = LBracketLoc;
1440 SymbolLocations[SymbolIdx++] = RBracketLoc;
1441 Op = OO_Subscript;
1442 break;
1443 }
1444
1445 case tok::code_completion: {
1446 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001447 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001448
1449 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001450 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001451
1452 // Don't try to parse any further.
1453 return true;
1454 }
1455
1456 default:
1457 break;
1458 }
1459
1460 if (Op != OO_None) {
1461 // We have parsed an operator-function-id.
1462 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1463 return false;
1464 }
Sean Hunt0486d742009-11-28 04:44:28 +00001465
1466 // Parse a literal-operator-id.
1467 //
1468 // literal-operator-id: [C++0x 13.5.8]
1469 // operator "" identifier
1470
1471 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1472 if (Tok.getLength() != 2)
1473 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1474 ConsumeStringToken();
1475
1476 if (Tok.isNot(tok::identifier)) {
1477 Diag(Tok.getLocation(), diag::err_expected_ident);
1478 return true;
1479 }
1480
1481 IdentifierInfo *II = Tok.getIdentifierInfo();
1482 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001483 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001484 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001485
1486 // Parse a conversion-function-id.
1487 //
1488 // conversion-function-id: [C++ 12.3.2]
1489 // operator conversion-type-id
1490 //
1491 // conversion-type-id:
1492 // type-specifier-seq conversion-declarator[opt]
1493 //
1494 // conversion-declarator:
1495 // ptr-operator conversion-declarator[opt]
1496
1497 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001498 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001499 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001500 return true;
1501
1502 // Parse the conversion-declarator, which is merely a sequence of
1503 // ptr-operators.
1504 Declarator D(DS, Declarator::TypeNameContext);
1505 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1506
1507 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001508 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001509 if (Ty.isInvalid())
1510 return true;
1511
1512 // Note that this is a conversion-function-id.
1513 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1514 D.getSourceRange().getEnd());
1515 return false;
1516}
1517
1518/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1519/// name of an entity.
1520///
1521/// \code
1522/// unqualified-id: [C++ expr.prim.general]
1523/// identifier
1524/// operator-function-id
1525/// conversion-function-id
1526/// [C++0x] literal-operator-id [TODO]
1527/// ~ class-name
1528/// template-id
1529///
1530/// \endcode
1531///
1532/// \param The nested-name-specifier that preceded this unqualified-id. If
1533/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1534///
1535/// \param EnteringContext whether we are entering the scope of the
1536/// nested-name-specifier.
1537///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001538/// \param AllowDestructorName whether we allow parsing of a destructor name.
1539///
1540/// \param AllowConstructorName whether we allow parsing a constructor name.
1541///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001542/// \param ObjectType if this unqualified-id occurs within a member access
1543/// expression, the type of the base object whose member is being accessed.
1544///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001545/// \param Result on a successful parse, contains the parsed unqualified-id.
1546///
1547/// \returns true if parsing fails, false otherwise.
1548bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1549 bool AllowDestructorName,
1550 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001551 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001552 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001553
1554 // Handle 'A::template B'. This is for template-ids which have not
1555 // already been annotated by ParseOptionalCXXScopeSpecifier().
1556 bool TemplateSpecified = false;
1557 SourceLocation TemplateKWLoc;
1558 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1559 (ObjectType || SS.isSet())) {
1560 TemplateSpecified = true;
1561 TemplateKWLoc = ConsumeToken();
1562 }
1563
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001564 // unqualified-id:
1565 // identifier
1566 // template-id (when it hasn't already been annotated)
1567 if (Tok.is(tok::identifier)) {
1568 // Consume the identifier.
1569 IdentifierInfo *Id = Tok.getIdentifierInfo();
1570 SourceLocation IdLoc = ConsumeToken();
1571
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001572 if (!getLang().CPlusPlus) {
1573 // If we're not in C++, only identifiers matter. Record the
1574 // identifier and return.
1575 Result.setIdentifier(Id, IdLoc);
1576 return false;
1577 }
1578
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001579 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001580 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001581 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001582 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001583 &SS, false, false,
1584 ParsedType(),
1585 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001586 IdLoc, IdLoc);
1587 } else {
1588 // We have parsed an identifier.
1589 Result.setIdentifier(Id, IdLoc);
1590 }
1591
1592 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001593 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001594 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001595 ObjectType, Result,
1596 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001597
1598 return false;
1599 }
1600
1601 // unqualified-id:
1602 // template-id (already parsed and annotated)
1603 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001604 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001605
1606 // If the template-name names the current class, then this is a constructor
1607 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001608 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001609 if (SS.isSet()) {
1610 // C++ [class.qual]p2 specifies that a qualified template-name
1611 // is taken as the constructor name where a constructor can be
1612 // declared. Thus, the template arguments are extraneous, so
1613 // complain about them and remove them entirely.
1614 Diag(TemplateId->TemplateNameLoc,
1615 diag::err_out_of_line_constructor_template_id)
1616 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001617 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001618 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1619 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1620 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001621 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001622 &SS, false, false,
1623 ParsedType(),
1624 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001625 TemplateId->TemplateNameLoc,
1626 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001627 ConsumeToken();
1628 return false;
1629 }
1630
1631 Result.setConstructorTemplateId(TemplateId);
1632 ConsumeToken();
1633 return false;
1634 }
1635
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001636 // We have already parsed a template-id; consume the annotation token as
1637 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001638 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001639 ConsumeToken();
1640 return false;
1641 }
1642
1643 // unqualified-id:
1644 // operator-function-id
1645 // conversion-function-id
1646 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001647 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001648 return true;
1649
Sean Hunte6252d12009-11-28 08:58:14 +00001650 // If we have an operator-function-id or a literal-operator-id and the next
1651 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001652 //
1653 // template-id:
1654 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001655 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1656 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001657 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001658 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1659 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001660 Result,
1661 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001662
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001663 return false;
1664 }
1665
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001666 if (getLang().CPlusPlus &&
1667 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001668 // C++ [expr.unary.op]p10:
1669 // There is an ambiguity in the unary-expression ~X(), where X is a
1670 // class-name. The ambiguity is resolved in favor of treating ~ as a
1671 // unary complement rather than treating ~X as referring to a destructor.
1672
1673 // Parse the '~'.
1674 SourceLocation TildeLoc = ConsumeToken();
1675
1676 // Parse the class-name.
1677 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001678 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001679 return true;
1680 }
1681
1682 // Parse the class-name (or template-name in a simple-template-id).
1683 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1684 SourceLocation ClassNameLoc = ConsumeToken();
1685
Douglas Gregor0278e122010-05-05 05:58:24 +00001686 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001687 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001688 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001689 EnteringContext, ObjectType, Result,
1690 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001691 }
1692
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001693 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001694 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1695 ClassNameLoc, getCurScope(),
1696 SS, ObjectType,
1697 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001698 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001699 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001700
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001701 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001702 return false;
1703 }
1704
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001705 Diag(Tok, diag::err_expected_unqualified_id)
1706 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001707 return true;
1708}
1709
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001710/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1711/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001712///
Chris Lattner59232d32009-01-04 21:25:24 +00001713/// This method is called to parse the new expression after the optional :: has
1714/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1715/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001716///
1717/// new-expression:
1718/// '::'[opt] 'new' new-placement[opt] new-type-id
1719/// new-initializer[opt]
1720/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1721/// new-initializer[opt]
1722///
1723/// new-placement:
1724/// '(' expression-list ')'
1725///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001726/// new-type-id:
1727/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001728/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001729///
1730/// new-declarator:
1731/// ptr-operator new-declarator[opt]
1732/// direct-new-declarator
1733///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001734/// new-initializer:
1735/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001736/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001737///
John McCall60d7b3a2010-08-24 06:29:42 +00001738ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001739Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1740 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1741 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001742
1743 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1744 // second form of new-expression. It can't be a new-type-id.
1745
Sebastian Redla55e52c2008-11-25 22:21:31 +00001746 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001747 SourceLocation PlacementLParen, PlacementRParen;
1748
Douglas Gregor4bd40312010-07-13 15:54:32 +00001749 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00001750 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00001751 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001752 if (Tok.is(tok::l_paren)) {
1753 // If it turns out to be a placement, we change the type location.
1754 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001755 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1756 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001757 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001758 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001759
1760 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001761 if (PlacementRParen.isInvalid()) {
1762 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001763 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001764 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001765
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001766 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001767 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001768 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001769 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001770 } else {
1771 // We still need the type.
1772 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001773 TypeIdParens.setBegin(ConsumeParen());
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001774 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001775 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001776 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001777 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001778 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1779 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001780 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001781 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001782 if (ParseCXXTypeSpecifierSeq(DS))
1783 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001784 else {
1785 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001786 ParseDeclaratorInternal(DeclaratorInfo,
1787 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001788 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001789 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001790 }
1791 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001792 // A new-type-id is a simplified type-id, where essentially the
1793 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001794 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001795 if (ParseCXXTypeSpecifierSeq(DS))
1796 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001797 else {
1798 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001799 ParseDeclaratorInternal(DeclaratorInfo,
1800 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001801 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001802 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001803 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001804 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001805 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001806 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001807
Sebastian Redla55e52c2008-11-25 22:21:31 +00001808 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001809 SourceLocation ConstructorLParen, ConstructorRParen;
1810
1811 if (Tok.is(tok::l_paren)) {
1812 ConstructorLParen = ConsumeParen();
1813 if (Tok.isNot(tok::r_paren)) {
1814 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001815 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1816 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001817 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001818 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001819 }
1820 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001821 if (ConstructorRParen.isInvalid()) {
1822 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001823 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001824 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001825 } else if (Tok.is(tok::l_brace)) {
1826 // FIXME: Have to communicate the init-list to ActOnCXXNew.
1827 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001828 }
1829
Sebastian Redlf53597f2009-03-15 17:47:39 +00001830 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1831 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001832 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001833 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001834}
1835
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001836/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1837/// passed to ParseDeclaratorInternal.
1838///
1839/// direct-new-declarator:
1840/// '[' expression ']'
1841/// direct-new-declarator '[' constant-expression ']'
1842///
Chris Lattner59232d32009-01-04 21:25:24 +00001843void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001844 // Parse the array dimensions.
1845 bool first = true;
1846 while (Tok.is(tok::l_square)) {
1847 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001848 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001849 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001850 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001851 // Recover
1852 SkipUntil(tok::r_square);
1853 return;
1854 }
1855 first = false;
1856
Sebastian Redlab197ba2009-02-09 18:23:29 +00001857 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall0b7e6782011-03-24 11:26:52 +00001858
1859 ParsedAttributes attrs(AttrFactory);
1860 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00001861 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001862 Size.release(), LLoc, RLoc),
John McCall0b7e6782011-03-24 11:26:52 +00001863 attrs, RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001864
Sebastian Redlab197ba2009-02-09 18:23:29 +00001865 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001866 return;
1867 }
1868}
1869
1870/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1871/// This ambiguity appears in the syntax of the C++ new operator.
1872///
1873/// new-expression:
1874/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1875/// new-initializer[opt]
1876///
1877/// new-placement:
1878/// '(' expression-list ')'
1879///
John McCallca0408f2010-08-23 06:44:23 +00001880bool Parser::ParseExpressionListOrTypeId(
1881 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001882 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001883 // The '(' was already consumed.
1884 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001885 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001886 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001887 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001888 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001889 }
1890
1891 // It's not a type, it has to be an expression list.
1892 // Discard the comma locations - ActOnCXXNew has enough parameters.
1893 CommaLocsTy CommaLocs;
1894 return ParseExpressionList(PlacementArgs, CommaLocs);
1895}
1896
1897/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1898/// to free memory allocated by new.
1899///
Chris Lattner59232d32009-01-04 21:25:24 +00001900/// This method is called to parse the 'delete' expression after the optional
1901/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1902/// and "Start" is its location. Otherwise, "Start" is the location of the
1903/// 'delete' token.
1904///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001905/// delete-expression:
1906/// '::'[opt] 'delete' cast-expression
1907/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001908ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001909Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1910 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1911 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001912
1913 // Array delete?
1914 bool ArrayDelete = false;
1915 if (Tok.is(tok::l_square)) {
1916 ArrayDelete = true;
1917 SourceLocation LHS = ConsumeBracket();
1918 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1919 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001920 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001921 }
1922
John McCall60d7b3a2010-08-24 06:29:42 +00001923 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001924 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001925 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001926
John McCall9ae2f072010-08-23 23:25:46 +00001927 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001928}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001929
Mike Stump1eb44332009-09-09 15:08:12 +00001930static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001931 switch(kind) {
John Wiegley20c0da72011-04-27 23:09:49 +00001932 default: assert(false && "Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001933 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001934 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00001935 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001936 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00001937 case tok::kw___has_trivial_constructor:
1938 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00001939 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001940 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1941 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1942 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00001943 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
1944 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001945 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00001946 case tok::kw___is_complete_type: return UTT_IsCompleteType;
1947 case tok::kw___is_compound: return UTT_IsCompound;
1948 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001949 case tok::kw___is_empty: return UTT_IsEmpty;
1950 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley20c0da72011-04-27 23:09:49 +00001951 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
1952 case tok::kw___is_function: return UTT_IsFunction;
1953 case tok::kw___is_fundamental: return UTT_IsFundamental;
1954 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00001955 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
1956 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
1957 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
1958 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
1959 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001960 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00001961 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001962 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00001963 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001964 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00001965 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00001966 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
1967 case tok::kw___is_scalar: return UTT_IsScalar;
1968 case tok::kw___is_signed: return UTT_IsSigned;
1969 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
1970 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00001971 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001972 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00001973 case tok::kw___is_unsigned: return UTT_IsUnsigned;
1974 case tok::kw___is_void: return UTT_IsVoid;
1975 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001976 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001977}
1978
1979static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1980 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001981 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001982 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00001983 case tok::kw___is_convertible: return BTT_IsConvertible;
1984 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00001985 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001986 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001987 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001988}
1989
John Wiegley21ff2e52011-04-28 00:16:57 +00001990static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
1991 switch(kind) {
1992 default: llvm_unreachable("Not a known binary type trait");
1993 case tok::kw___array_rank: return ATT_ArrayRank;
1994 case tok::kw___array_extent: return ATT_ArrayExtent;
1995 }
1996}
1997
John Wiegley55262202011-04-25 06:54:41 +00001998static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
1999 switch(kind) {
2000 default: assert(false && "Not a known unary expression trait.");
2001 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2002 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2003 }
2004}
2005
Sebastian Redl64b45f72009-01-05 20:52:13 +00002006/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2007/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2008/// templates.
2009///
2010/// primary-expression:
2011/// [GNU] unary-type-trait '(' type-id ')'
2012///
John McCall60d7b3a2010-08-24 06:29:42 +00002013ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002014 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2015 SourceLocation Loc = ConsumeToken();
2016
2017 SourceLocation LParen = Tok.getLocation();
2018 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2019 return ExprError();
2020
2021 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2022 // there will be cryptic errors about mismatched parentheses and missing
2023 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002024 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002025
2026 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2027
Douglas Gregor809070a2009-02-18 17:45:20 +00002028 if (Ty.isInvalid())
2029 return ExprError();
2030
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002031 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00002032}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002033
Francois Pichet6ad6f282010-12-07 00:08:36 +00002034/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2035/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2036/// templates.
2037///
2038/// primary-expression:
2039/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2040///
2041ExprResult Parser::ParseBinaryTypeTrait() {
2042 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2043 SourceLocation Loc = ConsumeToken();
2044
2045 SourceLocation LParen = Tok.getLocation();
2046 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2047 return ExprError();
2048
2049 TypeResult LhsTy = ParseTypeName();
2050 if (LhsTy.isInvalid()) {
2051 SkipUntil(tok::r_paren);
2052 return ExprError();
2053 }
2054
2055 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2056 SkipUntil(tok::r_paren);
2057 return ExprError();
2058 }
2059
2060 TypeResult RhsTy = ParseTypeName();
2061 if (RhsTy.isInvalid()) {
2062 SkipUntil(tok::r_paren);
2063 return ExprError();
2064 }
2065
2066 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2067
2068 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
2069}
2070
John Wiegley21ff2e52011-04-28 00:16:57 +00002071/// ParseArrayTypeTrait - Parse the built-in array type-trait
2072/// pseudo-functions.
2073///
2074/// primary-expression:
2075/// [Embarcadero] '__array_rank' '(' type-id ')'
2076/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2077///
2078ExprResult Parser::ParseArrayTypeTrait() {
2079 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2080 SourceLocation Loc = ConsumeToken();
2081
2082 SourceLocation LParen = Tok.getLocation();
2083 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2084 return ExprError();
2085
2086 TypeResult Ty = ParseTypeName();
2087 if (Ty.isInvalid()) {
2088 SkipUntil(tok::comma);
2089 SkipUntil(tok::r_paren);
2090 return ExprError();
2091 }
2092
2093 switch (ATT) {
2094 case ATT_ArrayRank: {
2095 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2096 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, RParen);
2097 }
2098 case ATT_ArrayExtent: {
2099 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2100 SkipUntil(tok::r_paren);
2101 return ExprError();
2102 }
2103
2104 ExprResult DimExpr = ParseExpression();
2105 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2106
2107 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), RParen);
2108 }
2109 default:
2110 break;
2111 }
2112 return ExprError();
2113}
2114
John Wiegley55262202011-04-25 06:54:41 +00002115/// ParseExpressionTrait - Parse built-in expression-trait
2116/// pseudo-functions like __is_lvalue_expr( xxx ).
2117///
2118/// primary-expression:
2119/// [Embarcadero] expression-trait '(' expression ')'
2120///
2121ExprResult Parser::ParseExpressionTrait() {
2122 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2123 SourceLocation Loc = ConsumeToken();
2124
2125 SourceLocation LParen = Tok.getLocation();
2126 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2127 return ExprError();
2128
2129 ExprResult Expr = ParseExpression();
2130
2131 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2132
2133 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), RParen);
2134}
2135
2136
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002137/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2138/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2139/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002140ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002141Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002142 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002143 SourceLocation LParenLoc,
2144 SourceLocation &RParenLoc) {
2145 assert(getLang().CPlusPlus && "Should only be called for C++!");
2146 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2147 assert(isTypeIdInParens() && "Not a type-id!");
2148
John McCall60d7b3a2010-08-24 06:29:42 +00002149 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002150 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002151
2152 // We need to disambiguate a very ugly part of the C++ syntax:
2153 //
2154 // (T())x; - type-id
2155 // (T())*x; - type-id
2156 // (T())/x; - expression
2157 // (T()); - expression
2158 //
2159 // The bad news is that we cannot use the specialized tentative parser, since
2160 // it can only verify that the thing inside the parens can be parsed as
2161 // type-id, it is not useful for determining the context past the parens.
2162 //
2163 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002164 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002165 //
2166 // It uses a scheme similar to parsing inline methods. The parenthesized
2167 // tokens are cached, the context that follows is determined (possibly by
2168 // parsing a cast-expression), and then we re-introduce the cached tokens
2169 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002170
Mike Stump1eb44332009-09-09 15:08:12 +00002171 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002172 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002173
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002174 // Store the tokens of the parentheses. We will parse them after we determine
2175 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002176 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002177 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002178 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2179 return ExprError();
2180 }
2181
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002182 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002183 ParseAs = CompoundLiteral;
2184 } else {
2185 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002186 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2187 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2188 NotCastExpr = true;
2189 } else {
2190 // Try parsing the cast-expression that may follow.
2191 // If it is not a cast-expression, NotCastExpr will be true and no token
2192 // will be consumed.
2193 Result = ParseCastExpression(false/*isUnaryExpression*/,
2194 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002195 NotCastExpr,
2196 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002197 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002198
2199 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2200 // an expression.
2201 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002202 }
2203
Mike Stump1eb44332009-09-09 15:08:12 +00002204 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002205 Toks.push_back(Tok);
2206 // Re-enter the stored parenthesized tokens into the token stream, so we may
2207 // parse them now.
2208 PP.EnterTokenStream(Toks.data(), Toks.size(),
2209 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2210 // Drop the current token and bring the first cached one. It's the same token
2211 // as when we entered this function.
2212 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002213
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002214 if (ParseAs >= CompoundLiteral) {
2215 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002216
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002217 // Match the ')'.
2218 if (Tok.is(tok::r_paren))
2219 RParenLoc = ConsumeParen();
2220 else
2221 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002222
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002223 if (ParseAs == CompoundLiteral) {
2224 ExprType = CompoundLiteral;
2225 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2226 }
Mike Stump1eb44332009-09-09 15:08:12 +00002227
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002228 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2229 assert(ParseAs == CastExpr);
2230
2231 if (Ty.isInvalid())
2232 return ExprError();
2233
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002234 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002235
2236 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002237 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002238 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002239 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002240 return move(Result);
2241 }
Mike Stump1eb44332009-09-09 15:08:12 +00002242
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002243 // Not a compound literal, and not followed by a cast-expression.
2244 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002245
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002246 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002247 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002248 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002249 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002250
2251 // Match the ')'.
2252 if (Result.isInvalid()) {
2253 SkipUntil(tok::r_paren);
2254 return ExprError();
2255 }
Mike Stump1eb44332009-09-09 15:08:12 +00002256
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002257 if (Tok.is(tok::r_paren))
2258 RParenLoc = ConsumeParen();
2259 else
2260 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2261
2262 return move(Result);
2263}