blob: 632499295f38e9376234320fa6f7a8cfddb2eb5e [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
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000541 // Parse the common declaration-specifiers piece.
542 DeclSpec DS(AttrFactory);
543 ParseSpecifierQualifierList(DS);
544
545 // Parse the abstract-declarator, if present.
546 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
547 ParseDeclarator(DeclaratorInfo);
548
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 SourceLocation RAngleBracketLoc = Tok.getLocation();
550
Chris Lattner1ab3b962008-11-18 07:48:38 +0000551 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000552 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000553
554 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
555
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000556 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
557 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000558
John McCall60d7b3a2010-08-24 06:29:42 +0000559 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000561 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000562 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000563
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000564 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +0000565 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000566 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +0000567 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000568 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000569
Sebastian Redl20df9b72008-12-11 22:51:44 +0000570 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000571}
572
Sebastian Redlc42e1182008-11-11 11:37:55 +0000573/// ParseCXXTypeid - This handles the C++ typeid expression.
574///
575/// postfix-expression: [C++ 5.2p1]
576/// 'typeid' '(' expression ')'
577/// 'typeid' '(' type-id ')'
578///
John McCall60d7b3a2010-08-24 06:29:42 +0000579ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000580 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
581
582 SourceLocation OpLoc = ConsumeToken();
583 SourceLocation LParenLoc = Tok.getLocation();
584 SourceLocation RParenLoc;
585
586 // typeid expressions are always parenthesized.
587 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
588 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000589 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000590
John McCall60d7b3a2010-08-24 06:29:42 +0000591 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000592
593 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000594 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000595
596 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000597 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000598
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000599 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000600 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000601
602 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000603 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000604 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000605 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000606 // When typeid is applied to an expression other than an lvalue of a
607 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000608 // operand (Clause 5).
609 //
Mike Stump1eb44332009-09-09 15:08:12 +0000610 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000611 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000612 // we the expression is potentially potentially evaluated.
613 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000614 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000615 Result = ParseExpression();
616
617 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000618 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000619 SkipUntil(tok::r_paren);
620 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000621 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
622 if (RParenLoc.isInvalid())
623 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000624
625 // If we are a foo<int> that identifies a single function, resolve it now...
626 Expr* e = Result.get();
627 if (e->getType() == Actions.Context.OverloadTy) {
628 ExprResult er =
629 Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
630 if (er.isUsable())
631 Result = er.release();
632 }
Sebastian Redlc42e1182008-11-11 11:37:55 +0000633 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000634 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000635 }
636 }
637
Sebastian Redl20df9b72008-12-11 22:51:44 +0000638 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000639}
640
Francois Pichet01b7c302010-09-08 12:20:18 +0000641/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
642///
643/// '__uuidof' '(' expression ')'
644/// '__uuidof' '(' type-id ')'
645///
646ExprResult Parser::ParseCXXUuidof() {
647 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
648
649 SourceLocation OpLoc = ConsumeToken();
650 SourceLocation LParenLoc = Tok.getLocation();
651 SourceLocation RParenLoc;
652
653 // __uuidof expressions are always parenthesized.
654 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
655 "__uuidof"))
656 return ExprError();
657
658 ExprResult Result;
659
660 if (isTypeIdInParens()) {
661 TypeResult Ty = ParseTypeName();
662
663 // Match the ')'.
664 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
665
666 if (Ty.isInvalid())
667 return ExprError();
668
669 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
670 Ty.get().getAsOpaquePtr(), RParenLoc);
671 } else {
672 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
673 Result = ParseExpression();
674
675 // Match the ')'.
676 if (Result.isInvalid())
677 SkipUntil(tok::r_paren);
678 else {
679 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
680
681 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
682 Result.release(), RParenLoc);
683 }
684 }
685
686 return move(Result);
687}
688
Douglas Gregord4dca082010-02-24 18:44:31 +0000689/// \brief Parse a C++ pseudo-destructor expression after the base,
690/// . or -> operator, and nested-name-specifier have already been
691/// parsed.
692///
693/// postfix-expression: [C++ 5.2]
694/// postfix-expression . pseudo-destructor-name
695/// postfix-expression -> pseudo-destructor-name
696///
697/// pseudo-destructor-name:
698/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
699/// ::[opt] nested-name-specifier template simple-template-id ::
700/// ~type-name
701/// ::[opt] nested-name-specifier[opt] ~type-name
702///
John McCall60d7b3a2010-08-24 06:29:42 +0000703ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000704Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
705 tok::TokenKind OpKind,
706 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000707 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000708 // We're parsing either a pseudo-destructor-name or a dependent
709 // member access that has the same form as a
710 // pseudo-destructor-name. We parse both in the same way and let
711 // the action model sort them out.
712 //
713 // Note that the ::[opt] nested-name-specifier[opt] has already
714 // been parsed, and if there was a simple-template-id, it has
715 // been coalesced into a template-id annotation token.
716 UnqualifiedId FirstTypeName;
717 SourceLocation CCLoc;
718 if (Tok.is(tok::identifier)) {
719 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
720 ConsumeToken();
721 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
722 CCLoc = ConsumeToken();
723 } else if (Tok.is(tok::annot_template_id)) {
724 FirstTypeName.setTemplateId(
725 (TemplateIdAnnotation *)Tok.getAnnotationValue());
726 ConsumeToken();
727 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
728 CCLoc = ConsumeToken();
729 } else {
730 FirstTypeName.setIdentifier(0, SourceLocation());
731 }
732
733 // Parse the tilde.
734 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
735 SourceLocation TildeLoc = ConsumeToken();
736 if (!Tok.is(tok::identifier)) {
737 Diag(Tok, diag::err_destructor_tilde_identifier);
738 return ExprError();
739 }
740
741 // Parse the second type.
742 UnqualifiedId SecondTypeName;
743 IdentifierInfo *Name = Tok.getIdentifierInfo();
744 SourceLocation NameLoc = ConsumeToken();
745 SecondTypeName.setIdentifier(Name, NameLoc);
746
747 // If there is a '<', the second type name is a template-id. Parse
748 // it as such.
749 if (Tok.is(tok::less) &&
750 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000751 SecondTypeName, /*AssumeTemplateName=*/true,
752 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000753 return ExprError();
754
John McCall9ae2f072010-08-23 23:25:46 +0000755 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
756 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000757 SS, FirstTypeName, CCLoc,
758 TildeLoc, SecondTypeName,
759 Tok.is(tok::l_paren));
760}
761
Reid Spencer5f016e22007-07-11 17:01:13 +0000762/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
763///
764/// boolean-literal: [C++ 2.13.5]
765/// 'true'
766/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000767ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000769 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000770}
Chris Lattner50dd2892008-02-26 00:51:44 +0000771
772/// ParseThrowExpression - This handles the C++ throw expression.
773///
774/// throw-expression: [C++ 15]
775/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000776ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000777 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000778 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000779
Chris Lattner2a2819a2008-04-06 06:02:23 +0000780 // If the current token isn't the start of an assignment-expression,
781 // then the expression is not present. This handles things like:
782 // "C ? throw : (void)42", which is crazy but legal.
783 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
784 case tok::semi:
785 case tok::r_paren:
786 case tok::r_square:
787 case tok::r_brace:
788 case tok::colon:
789 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000790 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000791
Chris Lattner2a2819a2008-04-06 06:02:23 +0000792 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000793 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000794 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000795 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000796 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000797}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000798
799/// ParseCXXThis - This handles the C++ 'this' pointer.
800///
801/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
802/// a non-lvalue expression whose value is the address of the object for which
803/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000804ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000805 assert(Tok.is(tok::kw_this) && "Not 'this'!");
806 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000807 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000808}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000809
810/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
811/// Can be interpreted either as function-style casting ("int(x)")
812/// or class type construction ("ClassType(x,y,z)")
813/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000814/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000815///
816/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000817/// simple-type-specifier '(' expression-list[opt] ')'
818/// [C++0x] simple-type-specifier braced-init-list
819/// typename-specifier '(' expression-list[opt] ')'
820/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000821///
John McCall60d7b3a2010-08-24 06:29:42 +0000822ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000823Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000824 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000825 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000826
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000827 assert((Tok.is(tok::l_paren) ||
828 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
829 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000830
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000831 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000832
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000833 // FIXME: Convert to a proper type construct expression.
834 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000835
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000836 } else {
837 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
838
839 SourceLocation LParenLoc = ConsumeParen();
840
841 ExprVector Exprs(Actions);
842 CommaLocsTy CommaLocs;
843
844 if (Tok.isNot(tok::r_paren)) {
845 if (ParseExpressionList(Exprs, CommaLocs)) {
846 SkipUntil(tok::r_paren);
847 return ExprError();
848 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000849 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000850
851 // Match the ')'.
852 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
853
854 // TypeRep could be null, if it references an invalid typedef.
855 if (!TypeRep)
856 return ExprError();
857
858 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
859 "Unexpected number of commas!");
860 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
861 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000862 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000863}
864
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000865/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000866///
867/// condition:
868/// expression
869/// type-specifier-seq declarator '=' assignment-expression
870/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
871/// '=' assignment-expression
872///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000873/// \param ExprResult if the condition was parsed as an expression, the
874/// parsed expression.
875///
876/// \param DeclResult if the condition was parsed as a declaration, the
877/// parsed declaration.
878///
Douglas Gregor586596f2010-05-06 17:25:47 +0000879/// \param Loc The location of the start of the statement that requires this
880/// condition, e.g., the "for" in a for loop.
881///
882/// \param ConvertToBoolean Whether the condition expression should be
883/// converted to a boolean value.
884///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000885/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000886bool Parser::ParseCXXCondition(ExprResult &ExprOut,
887 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000888 SourceLocation Loc,
889 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000890 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000891 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000892 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000893 }
894
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000895 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000896 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000897 ExprOut = ParseExpression(); // expression
898 DeclOut = 0;
899 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000900 return true;
901
902 // If required, convert to a boolean value.
903 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000904 ExprOut
905 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
906 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000907 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000908
909 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +0000910 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000911 ParseSpecifierQualifierList(DS);
912
913 // declarator
914 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
915 ParseDeclarator(DeclaratorInfo);
916
917 // simple-asm-expr[opt]
918 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000919 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000920 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000921 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000922 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000923 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000924 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000925 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000926 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000927 }
928
929 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000930 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000931
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000932 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000933 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000934 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000935 DeclOut = Dcl.get();
936 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000937
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000938 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000939 if (isTokenEqualOrMistypedEqualEqual(
940 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000941 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000942 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000943 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000944 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
945 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000946 } else {
947 // FIXME: C++0x allows a braced-init-list
948 Diag(Tok, diag::err_expected_equal_after_declarator);
949 }
950
Douglas Gregor586596f2010-05-06 17:25:47 +0000951 // FIXME: Build a reference to this declaration? Convert it to bool?
952 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +0000953
954 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +0000955
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000956 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000957}
958
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000959/// \brief Determine whether the current token starts a C++
960/// simple-type-specifier.
961bool Parser::isCXXSimpleTypeSpecifier() const {
962 switch (Tok.getKind()) {
963 case tok::annot_typename:
964 case tok::kw_short:
965 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000966 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000967 case tok::kw_signed:
968 case tok::kw_unsigned:
969 case tok::kw_void:
970 case tok::kw_char:
971 case tok::kw_int:
972 case tok::kw_float:
973 case tok::kw_double:
974 case tok::kw_wchar_t:
975 case tok::kw_char16_t:
976 case tok::kw_char32_t:
977 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +0000978 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000979 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000980 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000981 return true;
982
983 default:
984 break;
985 }
986
987 return false;
988}
989
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000990/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
991/// This should only be called when the current token is known to be part of
992/// simple-type-specifier.
993///
994/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000995/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000996/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
997/// char
998/// wchar_t
999/// bool
1000/// short
1001/// int
1002/// long
1003/// signed
1004/// unsigned
1005/// float
1006/// double
1007/// void
1008/// [GNU] typeof-specifier
1009/// [C++0x] auto [TODO]
1010///
1011/// type-name:
1012/// class-name
1013/// enum-name
1014/// typedef-name
1015///
1016void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1017 DS.SetRangeStart(Tok.getLocation());
1018 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001019 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001020 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001022 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001023 case tok::identifier: // foo::bar
1024 case tok::coloncolon: // ::foo::bar
1025 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001026 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001027 assert(0 && "Not a simple-type-specifier token!");
1028 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +00001029
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001030 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001031 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001032 if (getTypeAnnotation(Tok))
1033 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1034 getTypeAnnotation(Tok));
1035 else
1036 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001037
1038 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1039 ConsumeToken();
1040
1041 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1042 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1043 // Objective-C interface. If we don't have Objective-C or a '<', this is
1044 // just a normal reference to a typedef name.
1045 if (Tok.is(tok::less) && getLang().ObjC1)
1046 ParseObjCProtocolQualifiers(DS);
1047
1048 DS.Finish(Diags, PP);
1049 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001050 }
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001052 // builtin types
1053 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001054 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001055 break;
1056 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001057 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001058 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001059 case tok::kw___int64:
1060 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1061 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001062 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001063 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001064 break;
1065 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001066 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001067 break;
1068 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001069 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001070 break;
1071 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001072 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001073 break;
1074 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001075 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001076 break;
1077 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001078 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001079 break;
1080 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001081 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001082 break;
1083 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001084 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001085 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001086 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001087 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001088 break;
1089 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001090 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001091 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001092 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001093 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001094 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001096 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001097 // GNU typeof support.
1098 case tok::kw_typeof:
1099 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001100 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001101 return;
1102 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001103 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001104 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1105 else
1106 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001107 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001108 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001109}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001110
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001111/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1112/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1113/// e.g., "const short int". Note that the DeclSpec is *not* finished
1114/// by parsing the type-specifier-seq, because these sequences are
1115/// typically followed by some form of declarator. Returns true and
1116/// emits diagnostics if this is not a type-specifier-seq, false
1117/// otherwise.
1118///
1119/// type-specifier-seq: [C++ 8.1]
1120/// type-specifier type-specifier-seq[opt]
1121///
1122bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1123 DS.SetRangeStart(Tok.getLocation());
1124 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001125 unsigned DiagID;
1126 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001127
1128 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001129 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1130 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001131 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001132 return true;
1133 }
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Sebastian Redld9bafa72010-02-03 21:21:43 +00001135 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1136 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1137 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001138
Douglas Gregor396a9f22010-02-24 23:13:13 +00001139 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001140 return false;
1141}
1142
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001143/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1144/// some form.
1145///
1146/// This routine is invoked when a '<' is encountered after an identifier or
1147/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1148/// whether the unqualified-id is actually a template-id. This routine will
1149/// then parse the template arguments and form the appropriate template-id to
1150/// return to the caller.
1151///
1152/// \param SS the nested-name-specifier that precedes this template-id, if
1153/// we're actually parsing a qualified-id.
1154///
1155/// \param Name for constructor and destructor names, this is the actual
1156/// identifier that may be a template-name.
1157///
1158/// \param NameLoc the location of the class-name in a constructor or
1159/// destructor.
1160///
1161/// \param EnteringContext whether we're entering the scope of the
1162/// nested-name-specifier.
1163///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001164/// \param ObjectType if this unqualified-id occurs within a member access
1165/// expression, the type of the base object whose member is being accessed.
1166///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001167/// \param Id as input, describes the template-name or operator-function-id
1168/// that precedes the '<'. If template arguments were parsed successfully,
1169/// will be updated with the template-id.
1170///
Douglas Gregord4dca082010-02-24 18:44:31 +00001171/// \param AssumeTemplateId When true, this routine will assume that the name
1172/// refers to a template without performing name lookup to verify.
1173///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001174/// \returns true if a parse error occurred, false otherwise.
1175bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1176 IdentifierInfo *Name,
1177 SourceLocation NameLoc,
1178 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001179 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001180 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001181 bool AssumeTemplateId,
1182 SourceLocation TemplateKWLoc) {
1183 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1184 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001185
1186 TemplateTy Template;
1187 TemplateNameKind TNK = TNK_Non_template;
1188 switch (Id.getKind()) {
1189 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001190 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001191 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001192 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001193 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001194 Id, ObjectType, EnteringContext,
1195 Template);
1196 if (TNK == TNK_Non_template)
1197 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001198 } else {
1199 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001200 TNK = Actions.isTemplateName(getCurScope(), SS,
1201 TemplateKWLoc.isValid(), Id,
1202 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001203 MemberOfUnknownSpecialization);
1204
1205 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1206 ObjectType && IsTemplateArgumentList()) {
1207 // We have something like t->getAs<T>(), where getAs is a
1208 // member of an unknown specialization. However, this will only
1209 // parse correctly as a template, so suggest the keyword 'template'
1210 // before 'getAs' and treat this as a dependent template name.
1211 std::string Name;
1212 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1213 Name = Id.Identifier->getName();
1214 else {
1215 Name = "operator ";
1216 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1217 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1218 else
1219 Name += Id.Identifier->getName();
1220 }
1221 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1222 << Name
1223 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001224 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001225 SS, Id, ObjectType,
1226 EnteringContext, Template);
1227 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001228 return true;
1229 }
1230 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001231 break;
1232
Douglas Gregor014e88d2009-11-03 23:16:33 +00001233 case UnqualifiedId::IK_ConstructorName: {
1234 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001235 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001236 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001237 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1238 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001239 EnteringContext, Template,
1240 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001241 break;
1242 }
1243
Douglas Gregor014e88d2009-11-03 23:16:33 +00001244 case UnqualifiedId::IK_DestructorName: {
1245 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001246 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001247 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001248 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001249 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001250 TemplateName, ObjectType,
1251 EnteringContext, Template);
1252 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001253 return true;
1254 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001255 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1256 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001257 EnteringContext, Template,
1258 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001259
John McCallb3d87482010-08-24 05:47:05 +00001260 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001261 Diag(NameLoc, diag::err_destructor_template_id)
1262 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001263 return true;
1264 }
1265 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001266 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001267 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001268
1269 default:
1270 return false;
1271 }
1272
1273 if (TNK == TNK_Non_template)
1274 return false;
1275
1276 // Parse the enclosed template argument list.
1277 SourceLocation LAngleLoc, RAngleLoc;
1278 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001279 if (Tok.is(tok::less) &&
1280 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001281 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001282 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001283 RAngleLoc))
1284 return true;
1285
1286 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001287 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1288 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001289 // Form a parsed representation of the template-id to be stored in the
1290 // UnqualifiedId.
1291 TemplateIdAnnotation *TemplateId
1292 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1293
1294 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1295 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001296 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001297 TemplateId->TemplateNameLoc = Id.StartLocation;
1298 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001299 TemplateId->Name = 0;
1300 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1301 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001302 }
1303
Douglas Gregor059101f2011-03-02 00:47:37 +00001304 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001305 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001306 TemplateId->Kind = TNK;
1307 TemplateId->LAngleLoc = LAngleLoc;
1308 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001309 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001310 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001311 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001312 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001313
1314 Id.setTemplateId(TemplateId);
1315 return false;
1316 }
1317
1318 // Bundle the template arguments together.
1319 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001320 TemplateArgs.size());
1321
1322 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001323 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001324 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001325 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001326 RAngleLoc);
1327 if (Type.isInvalid())
1328 return true;
1329
1330 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1331 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1332 else
1333 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1334
1335 return false;
1336}
1337
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001338/// \brief Parse an operator-function-id or conversion-function-id as part
1339/// of a C++ unqualified-id.
1340///
1341/// This routine is responsible only for parsing the operator-function-id or
1342/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001343///
1344/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001345/// operator-function-id: [C++ 13.5]
1346/// 'operator' operator
1347///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001348/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001349/// new delete new[] delete[]
1350/// + - * / % ^ & | ~
1351/// ! = < > += -= *= /= %=
1352/// ^= &= |= << >> >>= <<= == !=
1353/// <= >= && || ++ -- , ->* ->
1354/// () []
1355///
1356/// conversion-function-id: [C++ 12.3.2]
1357/// operator conversion-type-id
1358///
1359/// conversion-type-id:
1360/// type-specifier-seq conversion-declarator[opt]
1361///
1362/// conversion-declarator:
1363/// ptr-operator conversion-declarator[opt]
1364/// \endcode
1365///
1366/// \param The nested-name-specifier that preceded this unqualified-id. If
1367/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1368///
1369/// \param EnteringContext whether we are entering the scope of the
1370/// nested-name-specifier.
1371///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001372/// \param ObjectType if this unqualified-id occurs within a member access
1373/// expression, the type of the base object whose member is being accessed.
1374///
1375/// \param Result on a successful parse, contains the parsed unqualified-id.
1376///
1377/// \returns true if parsing fails, false otherwise.
1378bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001379 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001380 UnqualifiedId &Result) {
1381 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1382
1383 // Consume the 'operator' keyword.
1384 SourceLocation KeywordLoc = ConsumeToken();
1385
1386 // Determine what kind of operator name we have.
1387 unsigned SymbolIdx = 0;
1388 SourceLocation SymbolLocations[3];
1389 OverloadedOperatorKind Op = OO_None;
1390 switch (Tok.getKind()) {
1391 case tok::kw_new:
1392 case tok::kw_delete: {
1393 bool isNew = Tok.getKind() == tok::kw_new;
1394 // Consume the 'new' or 'delete'.
1395 SymbolLocations[SymbolIdx++] = ConsumeToken();
1396 if (Tok.is(tok::l_square)) {
1397 // Consume the '['.
1398 SourceLocation LBracketLoc = ConsumeBracket();
1399 // Consume the ']'.
1400 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1401 LBracketLoc);
1402 if (RBracketLoc.isInvalid())
1403 return true;
1404
1405 SymbolLocations[SymbolIdx++] = LBracketLoc;
1406 SymbolLocations[SymbolIdx++] = RBracketLoc;
1407 Op = isNew? OO_Array_New : OO_Array_Delete;
1408 } else {
1409 Op = isNew? OO_New : OO_Delete;
1410 }
1411 break;
1412 }
1413
1414#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1415 case tok::Token: \
1416 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1417 Op = OO_##Name; \
1418 break;
1419#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1420#include "clang/Basic/OperatorKinds.def"
1421
1422 case tok::l_paren: {
1423 // Consume the '('.
1424 SourceLocation LParenLoc = ConsumeParen();
1425 // Consume the ')'.
1426 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1427 LParenLoc);
1428 if (RParenLoc.isInvalid())
1429 return true;
1430
1431 SymbolLocations[SymbolIdx++] = LParenLoc;
1432 SymbolLocations[SymbolIdx++] = RParenLoc;
1433 Op = OO_Call;
1434 break;
1435 }
1436
1437 case tok::l_square: {
1438 // Consume the '['.
1439 SourceLocation LBracketLoc = ConsumeBracket();
1440 // Consume the ']'.
1441 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1442 LBracketLoc);
1443 if (RBracketLoc.isInvalid())
1444 return true;
1445
1446 SymbolLocations[SymbolIdx++] = LBracketLoc;
1447 SymbolLocations[SymbolIdx++] = RBracketLoc;
1448 Op = OO_Subscript;
1449 break;
1450 }
1451
1452 case tok::code_completion: {
1453 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001454 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001455
1456 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001457 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001458
1459 // Don't try to parse any further.
1460 return true;
1461 }
1462
1463 default:
1464 break;
1465 }
1466
1467 if (Op != OO_None) {
1468 // We have parsed an operator-function-id.
1469 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1470 return false;
1471 }
Sean Hunt0486d742009-11-28 04:44:28 +00001472
1473 // Parse a literal-operator-id.
1474 //
1475 // literal-operator-id: [C++0x 13.5.8]
1476 // operator "" identifier
1477
1478 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1479 if (Tok.getLength() != 2)
1480 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1481 ConsumeStringToken();
1482
1483 if (Tok.isNot(tok::identifier)) {
1484 Diag(Tok.getLocation(), diag::err_expected_ident);
1485 return true;
1486 }
1487
1488 IdentifierInfo *II = Tok.getIdentifierInfo();
1489 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001490 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001491 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001492
1493 // Parse a conversion-function-id.
1494 //
1495 // conversion-function-id: [C++ 12.3.2]
1496 // operator conversion-type-id
1497 //
1498 // conversion-type-id:
1499 // type-specifier-seq conversion-declarator[opt]
1500 //
1501 // conversion-declarator:
1502 // ptr-operator conversion-declarator[opt]
1503
1504 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001505 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001506 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001507 return true;
1508
1509 // Parse the conversion-declarator, which is merely a sequence of
1510 // ptr-operators.
1511 Declarator D(DS, Declarator::TypeNameContext);
1512 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1513
1514 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001515 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001516 if (Ty.isInvalid())
1517 return true;
1518
1519 // Note that this is a conversion-function-id.
1520 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1521 D.getSourceRange().getEnd());
1522 return false;
1523}
1524
1525/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1526/// name of an entity.
1527///
1528/// \code
1529/// unqualified-id: [C++ expr.prim.general]
1530/// identifier
1531/// operator-function-id
1532/// conversion-function-id
1533/// [C++0x] literal-operator-id [TODO]
1534/// ~ class-name
1535/// template-id
1536///
1537/// \endcode
1538///
1539/// \param The nested-name-specifier that preceded this unqualified-id. If
1540/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1541///
1542/// \param EnteringContext whether we are entering the scope of the
1543/// nested-name-specifier.
1544///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001545/// \param AllowDestructorName whether we allow parsing of a destructor name.
1546///
1547/// \param AllowConstructorName whether we allow parsing a constructor name.
1548///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001549/// \param ObjectType if this unqualified-id occurs within a member access
1550/// expression, the type of the base object whose member is being accessed.
1551///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001552/// \param Result on a successful parse, contains the parsed unqualified-id.
1553///
1554/// \returns true if parsing fails, false otherwise.
1555bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1556 bool AllowDestructorName,
1557 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001558 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001559 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001560
1561 // Handle 'A::template B'. This is for template-ids which have not
1562 // already been annotated by ParseOptionalCXXScopeSpecifier().
1563 bool TemplateSpecified = false;
1564 SourceLocation TemplateKWLoc;
1565 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1566 (ObjectType || SS.isSet())) {
1567 TemplateSpecified = true;
1568 TemplateKWLoc = ConsumeToken();
1569 }
1570
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001571 // unqualified-id:
1572 // identifier
1573 // template-id (when it hasn't already been annotated)
1574 if (Tok.is(tok::identifier)) {
1575 // Consume the identifier.
1576 IdentifierInfo *Id = Tok.getIdentifierInfo();
1577 SourceLocation IdLoc = ConsumeToken();
1578
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001579 if (!getLang().CPlusPlus) {
1580 // If we're not in C++, only identifiers matter. Record the
1581 // identifier and return.
1582 Result.setIdentifier(Id, IdLoc);
1583 return false;
1584 }
1585
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001586 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001587 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001588 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001589 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001590 &SS, false, false,
1591 ParsedType(),
1592 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001593 IdLoc, IdLoc);
1594 } else {
1595 // We have parsed an identifier.
1596 Result.setIdentifier(Id, IdLoc);
1597 }
1598
1599 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001600 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001601 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001602 ObjectType, Result,
1603 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001604
1605 return false;
1606 }
1607
1608 // unqualified-id:
1609 // template-id (already parsed and annotated)
1610 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001611 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001612
1613 // If the template-name names the current class, then this is a constructor
1614 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001615 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001616 if (SS.isSet()) {
1617 // C++ [class.qual]p2 specifies that a qualified template-name
1618 // is taken as the constructor name where a constructor can be
1619 // declared. Thus, the template arguments are extraneous, so
1620 // complain about them and remove them entirely.
1621 Diag(TemplateId->TemplateNameLoc,
1622 diag::err_out_of_line_constructor_template_id)
1623 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001624 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001625 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1626 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1627 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001628 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001629 &SS, false, false,
1630 ParsedType(),
1631 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001632 TemplateId->TemplateNameLoc,
1633 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001634 ConsumeToken();
1635 return false;
1636 }
1637
1638 Result.setConstructorTemplateId(TemplateId);
1639 ConsumeToken();
1640 return false;
1641 }
1642
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001643 // We have already parsed a template-id; consume the annotation token as
1644 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001645 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001646 ConsumeToken();
1647 return false;
1648 }
1649
1650 // unqualified-id:
1651 // operator-function-id
1652 // conversion-function-id
1653 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001654 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001655 return true;
1656
Sean Hunte6252d12009-11-28 08:58:14 +00001657 // If we have an operator-function-id or a literal-operator-id and the next
1658 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001659 //
1660 // template-id:
1661 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001662 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1663 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001664 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001665 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1666 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001667 Result,
1668 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001669
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001670 return false;
1671 }
1672
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001673 if (getLang().CPlusPlus &&
1674 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001675 // C++ [expr.unary.op]p10:
1676 // There is an ambiguity in the unary-expression ~X(), where X is a
1677 // class-name. The ambiguity is resolved in favor of treating ~ as a
1678 // unary complement rather than treating ~X as referring to a destructor.
1679
1680 // Parse the '~'.
1681 SourceLocation TildeLoc = ConsumeToken();
1682
1683 // Parse the class-name.
1684 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001685 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001686 return true;
1687 }
1688
1689 // Parse the class-name (or template-name in a simple-template-id).
1690 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1691 SourceLocation ClassNameLoc = ConsumeToken();
1692
Douglas Gregor0278e122010-05-05 05:58:24 +00001693 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001694 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001695 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001696 EnteringContext, ObjectType, Result,
1697 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001698 }
1699
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001700 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001701 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1702 ClassNameLoc, getCurScope(),
1703 SS, ObjectType,
1704 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001705 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001706 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001707
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001708 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001709 return false;
1710 }
1711
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001712 Diag(Tok, diag::err_expected_unqualified_id)
1713 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001714 return true;
1715}
1716
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001717/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1718/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001719///
Chris Lattner59232d32009-01-04 21:25:24 +00001720/// This method is called to parse the new expression after the optional :: has
1721/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1722/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001723///
1724/// new-expression:
1725/// '::'[opt] 'new' new-placement[opt] new-type-id
1726/// new-initializer[opt]
1727/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1728/// new-initializer[opt]
1729///
1730/// new-placement:
1731/// '(' expression-list ')'
1732///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001733/// new-type-id:
1734/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001735/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001736///
1737/// new-declarator:
1738/// ptr-operator new-declarator[opt]
1739/// direct-new-declarator
1740///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001741/// new-initializer:
1742/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001743/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001744///
John McCall60d7b3a2010-08-24 06:29:42 +00001745ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001746Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1747 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1748 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001749
1750 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1751 // second form of new-expression. It can't be a new-type-id.
1752
Sebastian Redla55e52c2008-11-25 22:21:31 +00001753 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001754 SourceLocation PlacementLParen, PlacementRParen;
1755
Douglas Gregor4bd40312010-07-13 15:54:32 +00001756 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00001757 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00001758 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001759 if (Tok.is(tok::l_paren)) {
1760 // If it turns out to be a placement, we change the type location.
1761 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001762 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1763 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001764 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001765 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001766
1767 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001768 if (PlacementRParen.isInvalid()) {
1769 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001770 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001771 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001772
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001773 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001774 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001775 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001776 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001777 } else {
1778 // We still need the type.
1779 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001780 TypeIdParens.setBegin(ConsumeParen());
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001781 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001782 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001783 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001784 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001785 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1786 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001787 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001788 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001789 if (ParseCXXTypeSpecifierSeq(DS))
1790 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001791 else {
1792 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001793 ParseDeclaratorInternal(DeclaratorInfo,
1794 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001795 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001796 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001797 }
1798 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001799 // A new-type-id is a simplified type-id, where essentially the
1800 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001801 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001802 if (ParseCXXTypeSpecifierSeq(DS))
1803 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001804 else {
1805 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001806 ParseDeclaratorInternal(DeclaratorInfo,
1807 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001808 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001809 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001810 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001811 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001812 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001813 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001814
Sebastian Redla55e52c2008-11-25 22:21:31 +00001815 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001816 SourceLocation ConstructorLParen, ConstructorRParen;
1817
1818 if (Tok.is(tok::l_paren)) {
1819 ConstructorLParen = ConsumeParen();
1820 if (Tok.isNot(tok::r_paren)) {
1821 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001822 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1823 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001824 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001825 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001826 }
1827 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001828 if (ConstructorRParen.isInvalid()) {
1829 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001830 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001831 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001832 } else if (Tok.is(tok::l_brace)) {
1833 // FIXME: Have to communicate the init-list to ActOnCXXNew.
1834 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001835 }
1836
Sebastian Redlf53597f2009-03-15 17:47:39 +00001837 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1838 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001839 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001840 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001841}
1842
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001843/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1844/// passed to ParseDeclaratorInternal.
1845///
1846/// direct-new-declarator:
1847/// '[' expression ']'
1848/// direct-new-declarator '[' constant-expression ']'
1849///
Chris Lattner59232d32009-01-04 21:25:24 +00001850void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001851 // Parse the array dimensions.
1852 bool first = true;
1853 while (Tok.is(tok::l_square)) {
1854 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001855 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001856 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001857 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001858 // Recover
1859 SkipUntil(tok::r_square);
1860 return;
1861 }
1862 first = false;
1863
Sebastian Redlab197ba2009-02-09 18:23:29 +00001864 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall0b7e6782011-03-24 11:26:52 +00001865
1866 ParsedAttributes attrs(AttrFactory);
1867 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00001868 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001869 Size.release(), LLoc, RLoc),
John McCall0b7e6782011-03-24 11:26:52 +00001870 attrs, RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001871
Sebastian Redlab197ba2009-02-09 18:23:29 +00001872 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001873 return;
1874 }
1875}
1876
1877/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1878/// This ambiguity appears in the syntax of the C++ new operator.
1879///
1880/// new-expression:
1881/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1882/// new-initializer[opt]
1883///
1884/// new-placement:
1885/// '(' expression-list ')'
1886///
John McCallca0408f2010-08-23 06:44:23 +00001887bool Parser::ParseExpressionListOrTypeId(
1888 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001889 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001890 // The '(' was already consumed.
1891 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001892 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001893 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001894 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001895 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001896 }
1897
1898 // It's not a type, it has to be an expression list.
1899 // Discard the comma locations - ActOnCXXNew has enough parameters.
1900 CommaLocsTy CommaLocs;
1901 return ParseExpressionList(PlacementArgs, CommaLocs);
1902}
1903
1904/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1905/// to free memory allocated by new.
1906///
Chris Lattner59232d32009-01-04 21:25:24 +00001907/// This method is called to parse the 'delete' expression after the optional
1908/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1909/// and "Start" is its location. Otherwise, "Start" is the location of the
1910/// 'delete' token.
1911///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001912/// delete-expression:
1913/// '::'[opt] 'delete' cast-expression
1914/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001915ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001916Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1917 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1918 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001919
1920 // Array delete?
1921 bool ArrayDelete = false;
1922 if (Tok.is(tok::l_square)) {
1923 ArrayDelete = true;
1924 SourceLocation LHS = ConsumeBracket();
1925 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1926 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001927 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001928 }
1929
John McCall60d7b3a2010-08-24 06:29:42 +00001930 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001931 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001932 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001933
John McCall9ae2f072010-08-23 23:25:46 +00001934 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001935}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001936
Mike Stump1eb44332009-09-09 15:08:12 +00001937static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001938 switch(kind) {
John Wiegley20c0da72011-04-27 23:09:49 +00001939 default: assert(false && "Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001940 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001941 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00001942 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001943 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00001944 case tok::kw___has_trivial_constructor:
1945 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00001946 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001947 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1948 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1949 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00001950 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
1951 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001952 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00001953 case tok::kw___is_complete_type: return UTT_IsCompleteType;
1954 case tok::kw___is_compound: return UTT_IsCompound;
1955 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001956 case tok::kw___is_empty: return UTT_IsEmpty;
1957 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley20c0da72011-04-27 23:09:49 +00001958 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
1959 case tok::kw___is_function: return UTT_IsFunction;
1960 case tok::kw___is_fundamental: return UTT_IsFundamental;
1961 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00001962 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
1963 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
1964 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
1965 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
1966 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001967 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00001968 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001969 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00001970 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001971 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00001972 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00001973 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
1974 case tok::kw___is_scalar: return UTT_IsScalar;
1975 case tok::kw___is_signed: return UTT_IsSigned;
1976 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
1977 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00001978 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001979 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00001980 case tok::kw___is_unsigned: return UTT_IsUnsigned;
1981 case tok::kw___is_void: return UTT_IsVoid;
1982 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001983 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001984}
1985
1986static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1987 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001988 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001989 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00001990 case tok::kw___is_convertible: return BTT_IsConvertible;
1991 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00001992 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001993 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001994 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001995}
1996
John Wiegley21ff2e52011-04-28 00:16:57 +00001997static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
1998 switch(kind) {
1999 default: llvm_unreachable("Not a known binary type trait");
2000 case tok::kw___array_rank: return ATT_ArrayRank;
2001 case tok::kw___array_extent: return ATT_ArrayExtent;
2002 }
2003}
2004
John Wiegley55262202011-04-25 06:54:41 +00002005static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2006 switch(kind) {
2007 default: assert(false && "Not a known unary expression trait.");
2008 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2009 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2010 }
2011}
2012
Sebastian Redl64b45f72009-01-05 20:52:13 +00002013/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2014/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2015/// templates.
2016///
2017/// primary-expression:
2018/// [GNU] unary-type-trait '(' type-id ')'
2019///
John McCall60d7b3a2010-08-24 06:29:42 +00002020ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002021 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2022 SourceLocation Loc = ConsumeToken();
2023
2024 SourceLocation LParen = Tok.getLocation();
2025 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2026 return ExprError();
2027
2028 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2029 // there will be cryptic errors about mismatched parentheses and missing
2030 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002031 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002032
2033 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2034
Douglas Gregor809070a2009-02-18 17:45:20 +00002035 if (Ty.isInvalid())
2036 return ExprError();
2037
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002038 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00002039}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002040
Francois Pichet6ad6f282010-12-07 00:08:36 +00002041/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2042/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2043/// templates.
2044///
2045/// primary-expression:
2046/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2047///
2048ExprResult Parser::ParseBinaryTypeTrait() {
2049 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2050 SourceLocation Loc = ConsumeToken();
2051
2052 SourceLocation LParen = Tok.getLocation();
2053 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2054 return ExprError();
2055
2056 TypeResult LhsTy = ParseTypeName();
2057 if (LhsTy.isInvalid()) {
2058 SkipUntil(tok::r_paren);
2059 return ExprError();
2060 }
2061
2062 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2063 SkipUntil(tok::r_paren);
2064 return ExprError();
2065 }
2066
2067 TypeResult RhsTy = ParseTypeName();
2068 if (RhsTy.isInvalid()) {
2069 SkipUntil(tok::r_paren);
2070 return ExprError();
2071 }
2072
2073 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2074
2075 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
2076}
2077
John Wiegley21ff2e52011-04-28 00:16:57 +00002078/// ParseArrayTypeTrait - Parse the built-in array type-trait
2079/// pseudo-functions.
2080///
2081/// primary-expression:
2082/// [Embarcadero] '__array_rank' '(' type-id ')'
2083/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2084///
2085ExprResult Parser::ParseArrayTypeTrait() {
2086 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2087 SourceLocation Loc = ConsumeToken();
2088
2089 SourceLocation LParen = Tok.getLocation();
2090 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2091 return ExprError();
2092
2093 TypeResult Ty = ParseTypeName();
2094 if (Ty.isInvalid()) {
2095 SkipUntil(tok::comma);
2096 SkipUntil(tok::r_paren);
2097 return ExprError();
2098 }
2099
2100 switch (ATT) {
2101 case ATT_ArrayRank: {
2102 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2103 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, RParen);
2104 }
2105 case ATT_ArrayExtent: {
2106 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2107 SkipUntil(tok::r_paren);
2108 return ExprError();
2109 }
2110
2111 ExprResult DimExpr = ParseExpression();
2112 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2113
2114 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), RParen);
2115 }
2116 default:
2117 break;
2118 }
2119 return ExprError();
2120}
2121
John Wiegley55262202011-04-25 06:54:41 +00002122/// ParseExpressionTrait - Parse built-in expression-trait
2123/// pseudo-functions like __is_lvalue_expr( xxx ).
2124///
2125/// primary-expression:
2126/// [Embarcadero] expression-trait '(' expression ')'
2127///
2128ExprResult Parser::ParseExpressionTrait() {
2129 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2130 SourceLocation Loc = ConsumeToken();
2131
2132 SourceLocation LParen = Tok.getLocation();
2133 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2134 return ExprError();
2135
2136 ExprResult Expr = ParseExpression();
2137
2138 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2139
2140 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), RParen);
2141}
2142
2143
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002144/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2145/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2146/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002147ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002148Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002149 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002150 SourceLocation LParenLoc,
2151 SourceLocation &RParenLoc) {
2152 assert(getLang().CPlusPlus && "Should only be called for C++!");
2153 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2154 assert(isTypeIdInParens() && "Not a type-id!");
2155
John McCall60d7b3a2010-08-24 06:29:42 +00002156 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002157 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002158
2159 // We need to disambiguate a very ugly part of the C++ syntax:
2160 //
2161 // (T())x; - type-id
2162 // (T())*x; - type-id
2163 // (T())/x; - expression
2164 // (T()); - expression
2165 //
2166 // The bad news is that we cannot use the specialized tentative parser, since
2167 // it can only verify that the thing inside the parens can be parsed as
2168 // type-id, it is not useful for determining the context past the parens.
2169 //
2170 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002171 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002172 //
2173 // It uses a scheme similar to parsing inline methods. The parenthesized
2174 // tokens are cached, the context that follows is determined (possibly by
2175 // parsing a cast-expression), and then we re-introduce the cached tokens
2176 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002177
Mike Stump1eb44332009-09-09 15:08:12 +00002178 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002179 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002180
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002181 // Store the tokens of the parentheses. We will parse them after we determine
2182 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002183 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002184 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002185 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2186 return ExprError();
2187 }
2188
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002189 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002190 ParseAs = CompoundLiteral;
2191 } else {
2192 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002193 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2194 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2195 NotCastExpr = true;
2196 } else {
2197 // Try parsing the cast-expression that may follow.
2198 // If it is not a cast-expression, NotCastExpr will be true and no token
2199 // will be consumed.
2200 Result = ParseCastExpression(false/*isUnaryExpression*/,
2201 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002202 NotCastExpr,
2203 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002204 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002205
2206 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2207 // an expression.
2208 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002209 }
2210
Mike Stump1eb44332009-09-09 15:08:12 +00002211 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002212 Toks.push_back(Tok);
2213 // Re-enter the stored parenthesized tokens into the token stream, so we may
2214 // parse them now.
2215 PP.EnterTokenStream(Toks.data(), Toks.size(),
2216 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2217 // Drop the current token and bring the first cached one. It's the same token
2218 // as when we entered this function.
2219 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002220
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002221 if (ParseAs >= CompoundLiteral) {
2222 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002223
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002224 // Match the ')'.
2225 if (Tok.is(tok::r_paren))
2226 RParenLoc = ConsumeParen();
2227 else
2228 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002229
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002230 if (ParseAs == CompoundLiteral) {
2231 ExprType = CompoundLiteral;
2232 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2233 }
Mike Stump1eb44332009-09-09 15:08:12 +00002234
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002235 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2236 assert(ParseAs == CastExpr);
2237
2238 if (Ty.isInvalid())
2239 return ExprError();
2240
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002241 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002242
2243 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002244 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002245 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002246 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002247 return move(Result);
2248 }
Mike Stump1eb44332009-09-09 15:08:12 +00002249
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002250 // Not a compound literal, and not followed by a cast-expression.
2251 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002252
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002253 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002254 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002255 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002256 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002257
2258 // Match the ')'.
2259 if (Result.isInvalid()) {
2260 SkipUntil(tok::r_paren);
2261 return ExprError();
2262 }
Mike Stump1eb44332009-09-09 15:08:12 +00002263
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002264 if (Tok.is(tok::r_paren))
2265 RParenLoc = ConsumeParen();
2266 else
2267 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2268
2269 return move(Result);
2270}