blob: acfe83a79bb0481a6e75fdd6b50b4b062adc1581 [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);
Douglas Gregordc845342010-05-25 05:58:43 +0000164 ConsumeCodeCompletionToken();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000165 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000166 }
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Douglas Gregor39a8de12009-02-25 19:37:18 +0000168 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000169 // nested-name-specifier 'template'[opt] simple-template-id '::'
170
171 // Parse the optional 'template' keyword, then make sure we have
172 // 'identifier <' after it.
173 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000174 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000175 // nested-name-specifier, since they aren't allowed to start with
176 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000177 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000178 break;
179
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000180 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000181 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000182
183 UnqualifiedId TemplateName;
184 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000185 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000186 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000187 ConsumeToken();
188 } else if (Tok.is(tok::kw_operator)) {
189 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000190 TemplateName)) {
191 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000192 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000193 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000194
Sean Hunte6252d12009-11-28 08:58:14 +0000195 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
196 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000197 Diag(TemplateName.getSourceRange().getBegin(),
198 diag::err_id_after_template_in_nested_name_spec)
199 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000200 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000201 break;
202 }
203 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000204 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000205 break;
206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000208 // If the next token is not '<', we have a qualified-id that refers
209 // to a template name, such as T::template apply, but is not a
210 // template-id.
211 if (Tok.isNot(tok::less)) {
212 TPA.Revert();
213 break;
214 }
215
216 // Commit to parsing the template-id.
217 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000218 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000219 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000220 TemplateKWLoc,
221 SS,
222 TemplateName,
223 ObjectType,
224 EnteringContext,
225 Template)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000226 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000227 TemplateKWLoc, false))
228 return true;
229 } else
John McCall9ba61662010-02-26 08:45:28 +0000230 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Chris Lattner77cf72a2009-06-26 03:47:46 +0000232 continue;
233 }
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Douglas Gregor39a8de12009-02-25 19:37:18 +0000235 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000236 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000237 //
238 // simple-template-id '::'
239 //
240 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000241 // right kind (it should name a type or be dependent), and then
242 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000243 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000244 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord4dca082010-02-24 18:44:31 +0000245 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
246 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000247 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000248 }
249
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000250 // Consume the template-id token.
251 ConsumeToken();
252
253 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
254 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000256 if (!HasScopeSpecifier)
257 HasScopeSpecifier = true;
258
259 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
260 TemplateId->getTemplateArgs(),
261 TemplateId->NumArgs);
262
263 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
264 /*FIXME:*/SourceLocation(),
265 SS,
266 TemplateId->Template,
267 TemplateId->TemplateNameLoc,
268 TemplateId->LAngleLoc,
269 TemplateArgsPtr,
270 TemplateId->RAngleLoc,
271 CCLoc,
272 EnteringContext)) {
273 SourceLocation StartLoc
274 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
275 : TemplateId->TemplateNameLoc;
276 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000277 }
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000278
279 TemplateId->Destroy();
280 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000281 }
282
Chris Lattner5c7f7862009-06-26 03:52:38 +0000283
284 // The rest of the nested-name-specifier possibilities start with
285 // tok::identifier.
286 if (Tok.isNot(tok::identifier))
287 break;
288
289 IdentifierInfo &II = *Tok.getIdentifierInfo();
290
291 // nested-name-specifier:
292 // type-name '::'
293 // namespace-name '::'
294 // nested-name-specifier identifier '::'
295 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000296
297 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
298 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000299 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000300 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
301 Tok.getLocation(),
302 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000303 EnteringContext) &&
304 // If the token after the colon isn't an identifier, it's still an
305 // error, but they probably meant something else strange so don't
306 // recover like this.
307 PP.LookAhead(1).is(tok::identifier)) {
308 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000309 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000310
311 // Recover as if the user wrote '::'.
312 Next.setKind(tok::coloncolon);
313 }
Chris Lattner46646492009-12-07 01:36:53 +0000314 }
315
Chris Lattner5c7f7862009-06-26 03:52:38 +0000316 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000317 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000318 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000319 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000320 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000321 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000322 }
323
Chris Lattner5c7f7862009-06-26 03:52:38 +0000324 // We have an identifier followed by a '::'. Lookup this name
325 // as the name in a nested-name-specifier.
326 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000327 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
328 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000329 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000331 HasScopeSpecifier = true;
332 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
333 ObjectType, EnteringContext, SS))
334 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
335
Chris Lattner5c7f7862009-06-26 03:52:38 +0000336 continue;
337 }
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Richard Smithea698b32011-04-14 21:45:45 +0000339 // Check for '<::' which should be '< ::' instead of '[:' when following
340 // a template name.
341 if (Next.is(tok::l_square) && Next.getLength() == 2) {
342 Token SecondToken = GetLookAheadToken(2);
343 if (SecondToken.is(tok::colon) &&
344 AreTokensAdjacent(PP, Next, SecondToken)) {
345 TemplateTy Template;
346 UnqualifiedId TemplateName;
347 TemplateName.setIdentifier(&II, Tok.getLocation());
348 bool MemberOfUnknownSpecialization;
349 if (Actions.isTemplateName(getCurScope(), SS,
350 /*hasTemplateKeyword=*/false,
351 TemplateName,
352 ObjectType,
353 EnteringContext,
354 Template,
355 MemberOfUnknownSpecialization)) {
356 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
357 /*AtDigraph*/false);
358 }
359 }
360 }
361
Chris Lattner5c7f7862009-06-26 03:52:38 +0000362 // nested-name-specifier:
363 // type-name '<'
364 if (Next.is(tok::less)) {
365 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000366 UnqualifiedId TemplateName;
367 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000368 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000369 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000370 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000371 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000372 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000373 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000374 Template,
375 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000376 // We have found a template name, so annotate this this token
377 // with a template-id annotation. We do not permit the
378 // template-id to be translated into a type annotation,
379 // because some clients (e.g., the parsing of class template
380 // specializations) still want to see the original template-id
381 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000382 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000383 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000384 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000385 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000386 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000387 }
388
389 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000390 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000391 // We have something like t::getAs<T>, where getAs is a
392 // member of an unknown specialization. However, this will only
393 // parse correctly as a template, so suggest the keyword 'template'
394 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000395 unsigned DiagID = diag::err_missing_dependent_template_keyword;
396 if (getLang().Microsoft)
397 DiagID = diag::war_missing_dependent_template_keyword;
398
399 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000400 << II.getName()
401 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
402
Douglas Gregord6ab2322010-06-16 23:00:59 +0000403 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000404 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000405 Tok.getLocation(), SS,
406 TemplateName, ObjectType,
407 EnteringContext, Template)) {
408 // Consume the identifier.
409 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000410 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000411 SourceLocation(), false))
412 return true;
413 }
414 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000415 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000416
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000417 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000418 }
419 }
420
Douglas Gregor39a8de12009-02-25 19:37:18 +0000421 // We don't have any tokens that form the beginning of a
422 // nested-name-specifier, so we're done.
423 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Douglas Gregord4dca082010-02-24 18:44:31 +0000426 // Even if we didn't see any pieces of a nested-name-specifier, we
427 // still check whether there is a tilde in this position, which
428 // indicates a potential pseudo-destructor.
429 if (CheckForDestructor && Tok.is(tok::tilde))
430 *MayBePseudoDestructor = true;
431
John McCall9ba61662010-02-26 08:45:28 +0000432 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000433}
434
435/// ParseCXXIdExpression - Handle id-expression.
436///
437/// id-expression:
438/// unqualified-id
439/// qualified-id
440///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000441/// qualified-id:
442/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
443/// '::' identifier
444/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000445/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000446///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000447/// NOTE: The standard specifies that, for qualified-id, the parser does not
448/// expect:
449///
450/// '::' conversion-function-id
451/// '::' '~' class-name
452///
453/// This may cause a slight inconsistency on diagnostics:
454///
455/// class C {};
456/// namespace A {}
457/// void f() {
458/// :: A :: ~ C(); // Some Sema error about using destructor with a
459/// // namespace.
460/// :: ~ C(); // Some Parser error like 'unexpected ~'.
461/// }
462///
463/// We simplify the parser a bit and make it work like:
464///
465/// qualified-id:
466/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
467/// '::' unqualified-id
468///
469/// That way Sema can handle and report similar errors for namespaces and the
470/// global scope.
471///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000472/// The isAddressOfOperand parameter indicates that this id-expression is a
473/// direct operand of the address-of operator. This is, besides member contexts,
474/// the only place where a qualified-id naming a non-static class member may
475/// appear.
476///
John McCall60d7b3a2010-08-24 06:29:42 +0000477ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000478 // qualified-id:
479 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
480 // '::' unqualified-id
481 //
482 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000483 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000484
485 UnqualifiedId Name;
486 if (ParseUnqualifiedId(SS,
487 /*EnteringContext=*/false,
488 /*AllowDestructorName=*/false,
489 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000490 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000491 Name))
492 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000493
494 // This is only the direct operand of an & operator if it is not
495 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000496 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
497 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000498
Douglas Gregor23c94db2010-07-02 17:43:08 +0000499 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000500 isAddressOfOperand);
501
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000502}
503
Reid Spencer5f016e22007-07-11 17:01:13 +0000504/// ParseCXXCasts - This handles the various ways to cast expressions to another
505/// type.
506///
507/// postfix-expression: [C++ 5.2p1]
508/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
509/// 'static_cast' '<' type-name '>' '(' expression ')'
510/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
511/// 'const_cast' '<' type-name '>' '(' expression ')'
512///
John McCall60d7b3a2010-08-24 06:29:42 +0000513ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 tok::TokenKind Kind = Tok.getKind();
515 const char *CastName = 0; // For error messages
516
517 switch (Kind) {
518 default: assert(0 && "Unknown C++ cast!"); abort();
519 case tok::kw_const_cast: CastName = "const_cast"; break;
520 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
521 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
522 case tok::kw_static_cast: CastName = "static_cast"; break;
523 }
524
525 SourceLocation OpLoc = ConsumeToken();
526 SourceLocation LAngleBracketLoc = Tok.getLocation();
527
Richard Smithea698b32011-04-14 21:45:45 +0000528 // Check for "<::" which is parsed as "[:". If found, fix token stream,
529 // diagnose error, suggest fix, and recover parsing.
530 Token Next = NextToken();
531 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
532 AreTokensAdjacent(PP, Tok, Next))
533 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
534
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000536 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000537
Douglas Gregor809070a2009-02-18 17:45:20 +0000538 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 SourceLocation RAngleBracketLoc = Tok.getLocation();
540
Chris Lattner1ab3b962008-11-18 07:48:38 +0000541 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000542 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000543
544 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
545
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000546 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
547 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000548
John McCall60d7b3a2010-08-24 06:29:42 +0000549 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000551 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000552 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000553
Douglas Gregor809070a2009-02-18 17:45:20 +0000554 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000555 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000556 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000557 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000558 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000559
Sebastian Redl20df9b72008-12-11 22:51:44 +0000560 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000561}
562
Sebastian Redlc42e1182008-11-11 11:37:55 +0000563/// ParseCXXTypeid - This handles the C++ typeid expression.
564///
565/// postfix-expression: [C++ 5.2p1]
566/// 'typeid' '(' expression ')'
567/// 'typeid' '(' type-id ')'
568///
John McCall60d7b3a2010-08-24 06:29:42 +0000569ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000570 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
571
572 SourceLocation OpLoc = ConsumeToken();
573 SourceLocation LParenLoc = Tok.getLocation();
574 SourceLocation RParenLoc;
575
576 // typeid expressions are always parenthesized.
577 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
578 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000579 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000580
John McCall60d7b3a2010-08-24 06:29:42 +0000581 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000582
583 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000584 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000585
586 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000587 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000588
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000589 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000590 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000591
592 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000593 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000594 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000595 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000596 // When typeid is applied to an expression other than an lvalue of a
597 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000598 // operand (Clause 5).
599 //
Mike Stump1eb44332009-09-09 15:08:12 +0000600 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000601 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000602 // we the expression is potentially potentially evaluated.
603 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000604 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000605 Result = ParseExpression();
606
607 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000608 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000609 SkipUntil(tok::r_paren);
610 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000611 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
612 if (RParenLoc.isInvalid())
613 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000614
615 // If we are a foo<int> that identifies a single function, resolve it now...
616 Expr* e = Result.get();
617 if (e->getType() == Actions.Context.OverloadTy) {
618 ExprResult er =
619 Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
620 if (er.isUsable())
621 Result = er.release();
622 }
Sebastian Redlc42e1182008-11-11 11:37:55 +0000623 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000624 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000625 }
626 }
627
Sebastian Redl20df9b72008-12-11 22:51:44 +0000628 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000629}
630
Francois Pichet01b7c302010-09-08 12:20:18 +0000631/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
632///
633/// '__uuidof' '(' expression ')'
634/// '__uuidof' '(' type-id ')'
635///
636ExprResult Parser::ParseCXXUuidof() {
637 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
638
639 SourceLocation OpLoc = ConsumeToken();
640 SourceLocation LParenLoc = Tok.getLocation();
641 SourceLocation RParenLoc;
642
643 // __uuidof expressions are always parenthesized.
644 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
645 "__uuidof"))
646 return ExprError();
647
648 ExprResult Result;
649
650 if (isTypeIdInParens()) {
651 TypeResult Ty = ParseTypeName();
652
653 // Match the ')'.
654 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
655
656 if (Ty.isInvalid())
657 return ExprError();
658
659 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
660 Ty.get().getAsOpaquePtr(), RParenLoc);
661 } else {
662 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
663 Result = ParseExpression();
664
665 // Match the ')'.
666 if (Result.isInvalid())
667 SkipUntil(tok::r_paren);
668 else {
669 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
670
671 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
672 Result.release(), RParenLoc);
673 }
674 }
675
676 return move(Result);
677}
678
Douglas Gregord4dca082010-02-24 18:44:31 +0000679/// \brief Parse a C++ pseudo-destructor expression after the base,
680/// . or -> operator, and nested-name-specifier have already been
681/// parsed.
682///
683/// postfix-expression: [C++ 5.2]
684/// postfix-expression . pseudo-destructor-name
685/// postfix-expression -> pseudo-destructor-name
686///
687/// pseudo-destructor-name:
688/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
689/// ::[opt] nested-name-specifier template simple-template-id ::
690/// ~type-name
691/// ::[opt] nested-name-specifier[opt] ~type-name
692///
John McCall60d7b3a2010-08-24 06:29:42 +0000693ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000694Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
695 tok::TokenKind OpKind,
696 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000697 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000698 // We're parsing either a pseudo-destructor-name or a dependent
699 // member access that has the same form as a
700 // pseudo-destructor-name. We parse both in the same way and let
701 // the action model sort them out.
702 //
703 // Note that the ::[opt] nested-name-specifier[opt] has already
704 // been parsed, and if there was a simple-template-id, it has
705 // been coalesced into a template-id annotation token.
706 UnqualifiedId FirstTypeName;
707 SourceLocation CCLoc;
708 if (Tok.is(tok::identifier)) {
709 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
710 ConsumeToken();
711 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
712 CCLoc = ConsumeToken();
713 } else if (Tok.is(tok::annot_template_id)) {
714 FirstTypeName.setTemplateId(
715 (TemplateIdAnnotation *)Tok.getAnnotationValue());
716 ConsumeToken();
717 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
718 CCLoc = ConsumeToken();
719 } else {
720 FirstTypeName.setIdentifier(0, SourceLocation());
721 }
722
723 // Parse the tilde.
724 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
725 SourceLocation TildeLoc = ConsumeToken();
726 if (!Tok.is(tok::identifier)) {
727 Diag(Tok, diag::err_destructor_tilde_identifier);
728 return ExprError();
729 }
730
731 // Parse the second type.
732 UnqualifiedId SecondTypeName;
733 IdentifierInfo *Name = Tok.getIdentifierInfo();
734 SourceLocation NameLoc = ConsumeToken();
735 SecondTypeName.setIdentifier(Name, NameLoc);
736
737 // If there is a '<', the second type name is a template-id. Parse
738 // it as such.
739 if (Tok.is(tok::less) &&
740 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000741 SecondTypeName, /*AssumeTemplateName=*/true,
742 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000743 return ExprError();
744
John McCall9ae2f072010-08-23 23:25:46 +0000745 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
746 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000747 SS, FirstTypeName, CCLoc,
748 TildeLoc, SecondTypeName,
749 Tok.is(tok::l_paren));
750}
751
Reid Spencer5f016e22007-07-11 17:01:13 +0000752/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
753///
754/// boolean-literal: [C++ 2.13.5]
755/// 'true'
756/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000757ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000759 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000760}
Chris Lattner50dd2892008-02-26 00:51:44 +0000761
762/// ParseThrowExpression - This handles the C++ throw expression.
763///
764/// throw-expression: [C++ 15]
765/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000766ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000767 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000768 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000769
Chris Lattner2a2819a2008-04-06 06:02:23 +0000770 // If the current token isn't the start of an assignment-expression,
771 // then the expression is not present. This handles things like:
772 // "C ? throw : (void)42", which is crazy but legal.
773 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
774 case tok::semi:
775 case tok::r_paren:
776 case tok::r_square:
777 case tok::r_brace:
778 case tok::colon:
779 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000780 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000781
Chris Lattner2a2819a2008-04-06 06:02:23 +0000782 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000783 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000784 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000785 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000786 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000787}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000788
789/// ParseCXXThis - This handles the C++ 'this' pointer.
790///
791/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
792/// a non-lvalue expression whose value is the address of the object for which
793/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000794ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000795 assert(Tok.is(tok::kw_this) && "Not 'this'!");
796 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000797 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000798}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000799
800/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
801/// Can be interpreted either as function-style casting ("int(x)")
802/// or class type construction ("ClassType(x,y,z)")
803/// or creation of a value-initialized type ("int()").
804///
805/// postfix-expression: [C++ 5.2p1]
806/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
807/// typename-specifier '(' expression-list[opt] ')' [TODO]
808///
John McCall60d7b3a2010-08-24 06:29:42 +0000809ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000810Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000811 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000812 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000813
814 assert(Tok.is(tok::l_paren) && "Expected '('!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000815 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
816
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000817 SourceLocation LParenLoc = ConsumeParen();
818
Sebastian Redla55e52c2008-11-25 22:21:31 +0000819 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000820 CommaLocsTy CommaLocs;
821
822 if (Tok.isNot(tok::r_paren)) {
823 if (ParseExpressionList(Exprs, CommaLocs)) {
824 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000825 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000826 }
827 }
828
829 // Match the ')'.
830 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
831
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000832 // TypeRep could be null, if it references an invalid typedef.
833 if (!TypeRep)
834 return ExprError();
835
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000836 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
837 "Unexpected number of commas!");
Douglas Gregorab6677e2010-09-08 00:15:04 +0000838 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000839 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000840}
841
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000842/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000843///
844/// condition:
845/// expression
846/// type-specifier-seq declarator '=' assignment-expression
847/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
848/// '=' assignment-expression
849///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000850/// \param ExprResult if the condition was parsed as an expression, the
851/// parsed expression.
852///
853/// \param DeclResult if the condition was parsed as a declaration, the
854/// parsed declaration.
855///
Douglas Gregor586596f2010-05-06 17:25:47 +0000856/// \param Loc The location of the start of the statement that requires this
857/// condition, e.g., the "for" in a for loop.
858///
859/// \param ConvertToBoolean Whether the condition expression should be
860/// converted to a boolean value.
861///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000862/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000863bool Parser::ParseCXXCondition(ExprResult &ExprOut,
864 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000865 SourceLocation Loc,
866 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000867 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000868 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000869 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000870 }
871
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000872 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000873 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000874 ExprOut = ParseExpression(); // expression
875 DeclOut = 0;
876 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000877 return true;
878
879 // If required, convert to a boolean value.
880 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000881 ExprOut
882 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
883 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000884 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000885
886 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +0000887 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000888 ParseSpecifierQualifierList(DS);
889
890 // declarator
891 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
892 ParseDeclarator(DeclaratorInfo);
893
894 // simple-asm-expr[opt]
895 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000896 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000897 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000898 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000899 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000900 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000901 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000902 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000903 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000904 }
905
906 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000907 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000908
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000909 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000910 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000911 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000912 DeclOut = Dcl.get();
913 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000914
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000915 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000916 if (isTokenEqualOrMistypedEqualEqual(
917 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000918 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000919 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000920 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000921 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
922 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000923 } else {
924 // FIXME: C++0x allows a braced-init-list
925 Diag(Tok, diag::err_expected_equal_after_declarator);
926 }
927
Douglas Gregor586596f2010-05-06 17:25:47 +0000928 // FIXME: Build a reference to this declaration? Convert it to bool?
929 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +0000930
931 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +0000932
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000933 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000934}
935
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000936/// \brief Determine whether the current token starts a C++
937/// simple-type-specifier.
938bool Parser::isCXXSimpleTypeSpecifier() const {
939 switch (Tok.getKind()) {
940 case tok::annot_typename:
941 case tok::kw_short:
942 case tok::kw_long:
943 case tok::kw_signed:
944 case tok::kw_unsigned:
945 case tok::kw_void:
946 case tok::kw_char:
947 case tok::kw_int:
948 case tok::kw_float:
949 case tok::kw_double:
950 case tok::kw_wchar_t:
951 case tok::kw_char16_t:
952 case tok::kw_char32_t:
953 case tok::kw_bool:
954 // FIXME: C++0x decltype support.
955 // GNU typeof support.
956 case tok::kw_typeof:
957 return true;
958
959 default:
960 break;
961 }
962
963 return false;
964}
965
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000966/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
967/// This should only be called when the current token is known to be part of
968/// simple-type-specifier.
969///
970/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000971/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000972/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
973/// char
974/// wchar_t
975/// bool
976/// short
977/// int
978/// long
979/// signed
980/// unsigned
981/// float
982/// double
983/// void
984/// [GNU] typeof-specifier
985/// [C++0x] auto [TODO]
986///
987/// type-name:
988/// class-name
989/// enum-name
990/// typedef-name
991///
992void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
993 DS.SetRangeStart(Tok.getLocation());
994 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000995 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000996 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000998 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000999 case tok::identifier: // foo::bar
1000 case tok::coloncolon: // ::foo::bar
1001 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001002 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001003 assert(0 && "Not a simple-type-specifier token!");
1004 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +00001005
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001006 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001007 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001008 if (getTypeAnnotation(Tok))
1009 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1010 getTypeAnnotation(Tok));
1011 else
1012 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001013
1014 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1015 ConsumeToken();
1016
1017 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1018 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1019 // Objective-C interface. If we don't have Objective-C or a '<', this is
1020 // just a normal reference to a typedef name.
1021 if (Tok.is(tok::less) && getLang().ObjC1)
1022 ParseObjCProtocolQualifiers(DS);
1023
1024 DS.Finish(Diags, PP);
1025 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001026 }
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001028 // builtin types
1029 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001030 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001031 break;
1032 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001033 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001034 break;
1035 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001036 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001037 break;
1038 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001039 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001040 break;
1041 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001042 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001043 break;
1044 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001045 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001046 break;
1047 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001048 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001049 break;
1050 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001051 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001052 break;
1053 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001054 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001055 break;
1056 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001057 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001058 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001059 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001060 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001061 break;
1062 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001063 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001064 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001065 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001066 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001067 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001069 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001070 // GNU typeof support.
1071 case tok::kw_typeof:
1072 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001073 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001074 return;
1075 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001076 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001077 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1078 else
1079 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001080 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001081 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001082}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001083
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001084/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1085/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1086/// e.g., "const short int". Note that the DeclSpec is *not* finished
1087/// by parsing the type-specifier-seq, because these sequences are
1088/// typically followed by some form of declarator. Returns true and
1089/// emits diagnostics if this is not a type-specifier-seq, false
1090/// otherwise.
1091///
1092/// type-specifier-seq: [C++ 8.1]
1093/// type-specifier type-specifier-seq[opt]
1094///
1095bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1096 DS.SetRangeStart(Tok.getLocation());
1097 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001098 unsigned DiagID;
1099 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001100
1101 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001102 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1103 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001104 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001105 return true;
1106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Sebastian Redld9bafa72010-02-03 21:21:43 +00001108 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1109 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1110 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001111
Douglas Gregor396a9f22010-02-24 23:13:13 +00001112 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001113 return false;
1114}
1115
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001116/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1117/// some form.
1118///
1119/// This routine is invoked when a '<' is encountered after an identifier or
1120/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1121/// whether the unqualified-id is actually a template-id. This routine will
1122/// then parse the template arguments and form the appropriate template-id to
1123/// return to the caller.
1124///
1125/// \param SS the nested-name-specifier that precedes this template-id, if
1126/// we're actually parsing a qualified-id.
1127///
1128/// \param Name for constructor and destructor names, this is the actual
1129/// identifier that may be a template-name.
1130///
1131/// \param NameLoc the location of the class-name in a constructor or
1132/// destructor.
1133///
1134/// \param EnteringContext whether we're entering the scope of the
1135/// nested-name-specifier.
1136///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001137/// \param ObjectType if this unqualified-id occurs within a member access
1138/// expression, the type of the base object whose member is being accessed.
1139///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001140/// \param Id as input, describes the template-name or operator-function-id
1141/// that precedes the '<'. If template arguments were parsed successfully,
1142/// will be updated with the template-id.
1143///
Douglas Gregord4dca082010-02-24 18:44:31 +00001144/// \param AssumeTemplateId When true, this routine will assume that the name
1145/// refers to a template without performing name lookup to verify.
1146///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001147/// \returns true if a parse error occurred, false otherwise.
1148bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1149 IdentifierInfo *Name,
1150 SourceLocation NameLoc,
1151 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001152 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001153 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001154 bool AssumeTemplateId,
1155 SourceLocation TemplateKWLoc) {
1156 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1157 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001158
1159 TemplateTy Template;
1160 TemplateNameKind TNK = TNK_Non_template;
1161 switch (Id.getKind()) {
1162 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001163 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001164 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001165 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001166 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001167 Id, ObjectType, EnteringContext,
1168 Template);
1169 if (TNK == TNK_Non_template)
1170 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001171 } else {
1172 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001173 TNK = Actions.isTemplateName(getCurScope(), SS,
1174 TemplateKWLoc.isValid(), Id,
1175 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001176 MemberOfUnknownSpecialization);
1177
1178 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1179 ObjectType && IsTemplateArgumentList()) {
1180 // We have something like t->getAs<T>(), where getAs is a
1181 // member of an unknown specialization. However, this will only
1182 // parse correctly as a template, so suggest the keyword 'template'
1183 // before 'getAs' and treat this as a dependent template name.
1184 std::string Name;
1185 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1186 Name = Id.Identifier->getName();
1187 else {
1188 Name = "operator ";
1189 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1190 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1191 else
1192 Name += Id.Identifier->getName();
1193 }
1194 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1195 << Name
1196 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001197 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001198 SS, Id, ObjectType,
1199 EnteringContext, Template);
1200 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001201 return true;
1202 }
1203 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001204 break;
1205
Douglas Gregor014e88d2009-11-03 23:16:33 +00001206 case UnqualifiedId::IK_ConstructorName: {
1207 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001208 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001209 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001210 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1211 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001212 EnteringContext, Template,
1213 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001214 break;
1215 }
1216
Douglas Gregor014e88d2009-11-03 23:16:33 +00001217 case UnqualifiedId::IK_DestructorName: {
1218 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001219 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001220 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001221 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001222 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001223 TemplateName, ObjectType,
1224 EnteringContext, Template);
1225 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001226 return true;
1227 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001228 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1229 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001230 EnteringContext, Template,
1231 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001232
John McCallb3d87482010-08-24 05:47:05 +00001233 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001234 Diag(NameLoc, diag::err_destructor_template_id)
1235 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001236 return true;
1237 }
1238 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001239 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001240 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001241
1242 default:
1243 return false;
1244 }
1245
1246 if (TNK == TNK_Non_template)
1247 return false;
1248
1249 // Parse the enclosed template argument list.
1250 SourceLocation LAngleLoc, RAngleLoc;
1251 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001252 if (Tok.is(tok::less) &&
1253 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001254 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001255 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001256 RAngleLoc))
1257 return true;
1258
1259 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001260 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1261 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001262 // Form a parsed representation of the template-id to be stored in the
1263 // UnqualifiedId.
1264 TemplateIdAnnotation *TemplateId
1265 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1266
1267 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1268 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001269 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001270 TemplateId->TemplateNameLoc = Id.StartLocation;
1271 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001272 TemplateId->Name = 0;
1273 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1274 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001275 }
1276
Douglas Gregor059101f2011-03-02 00:47:37 +00001277 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001278 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001279 TemplateId->Kind = TNK;
1280 TemplateId->LAngleLoc = LAngleLoc;
1281 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001282 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001283 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001284 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001285 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001286
1287 Id.setTemplateId(TemplateId);
1288 return false;
1289 }
1290
1291 // Bundle the template arguments together.
1292 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001293 TemplateArgs.size());
1294
1295 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001296 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001297 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001298 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001299 RAngleLoc);
1300 if (Type.isInvalid())
1301 return true;
1302
1303 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1304 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1305 else
1306 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1307
1308 return false;
1309}
1310
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001311/// \brief Parse an operator-function-id or conversion-function-id as part
1312/// of a C++ unqualified-id.
1313///
1314/// This routine is responsible only for parsing the operator-function-id or
1315/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001316///
1317/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001318/// operator-function-id: [C++ 13.5]
1319/// 'operator' operator
1320///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001321/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001322/// new delete new[] delete[]
1323/// + - * / % ^ & | ~
1324/// ! = < > += -= *= /= %=
1325/// ^= &= |= << >> >>= <<= == !=
1326/// <= >= && || ++ -- , ->* ->
1327/// () []
1328///
1329/// conversion-function-id: [C++ 12.3.2]
1330/// operator conversion-type-id
1331///
1332/// conversion-type-id:
1333/// type-specifier-seq conversion-declarator[opt]
1334///
1335/// conversion-declarator:
1336/// ptr-operator conversion-declarator[opt]
1337/// \endcode
1338///
1339/// \param The nested-name-specifier that preceded this unqualified-id. If
1340/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1341///
1342/// \param EnteringContext whether we are entering the scope of the
1343/// nested-name-specifier.
1344///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001345/// \param ObjectType if this unqualified-id occurs within a member access
1346/// expression, the type of the base object whose member is being accessed.
1347///
1348/// \param Result on a successful parse, contains the parsed unqualified-id.
1349///
1350/// \returns true if parsing fails, false otherwise.
1351bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001352 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001353 UnqualifiedId &Result) {
1354 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1355
1356 // Consume the 'operator' keyword.
1357 SourceLocation KeywordLoc = ConsumeToken();
1358
1359 // Determine what kind of operator name we have.
1360 unsigned SymbolIdx = 0;
1361 SourceLocation SymbolLocations[3];
1362 OverloadedOperatorKind Op = OO_None;
1363 switch (Tok.getKind()) {
1364 case tok::kw_new:
1365 case tok::kw_delete: {
1366 bool isNew = Tok.getKind() == tok::kw_new;
1367 // Consume the 'new' or 'delete'.
1368 SymbolLocations[SymbolIdx++] = ConsumeToken();
1369 if (Tok.is(tok::l_square)) {
1370 // Consume the '['.
1371 SourceLocation LBracketLoc = ConsumeBracket();
1372 // Consume the ']'.
1373 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1374 LBracketLoc);
1375 if (RBracketLoc.isInvalid())
1376 return true;
1377
1378 SymbolLocations[SymbolIdx++] = LBracketLoc;
1379 SymbolLocations[SymbolIdx++] = RBracketLoc;
1380 Op = isNew? OO_Array_New : OO_Array_Delete;
1381 } else {
1382 Op = isNew? OO_New : OO_Delete;
1383 }
1384 break;
1385 }
1386
1387#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1388 case tok::Token: \
1389 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1390 Op = OO_##Name; \
1391 break;
1392#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1393#include "clang/Basic/OperatorKinds.def"
1394
1395 case tok::l_paren: {
1396 // Consume the '('.
1397 SourceLocation LParenLoc = ConsumeParen();
1398 // Consume the ')'.
1399 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1400 LParenLoc);
1401 if (RParenLoc.isInvalid())
1402 return true;
1403
1404 SymbolLocations[SymbolIdx++] = LParenLoc;
1405 SymbolLocations[SymbolIdx++] = RParenLoc;
1406 Op = OO_Call;
1407 break;
1408 }
1409
1410 case tok::l_square: {
1411 // Consume the '['.
1412 SourceLocation LBracketLoc = ConsumeBracket();
1413 // Consume the ']'.
1414 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1415 LBracketLoc);
1416 if (RBracketLoc.isInvalid())
1417 return true;
1418
1419 SymbolLocations[SymbolIdx++] = LBracketLoc;
1420 SymbolLocations[SymbolIdx++] = RBracketLoc;
1421 Op = OO_Subscript;
1422 break;
1423 }
1424
1425 case tok::code_completion: {
1426 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001427 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001428
1429 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001430 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001431
1432 // Don't try to parse any further.
1433 return true;
1434 }
1435
1436 default:
1437 break;
1438 }
1439
1440 if (Op != OO_None) {
1441 // We have parsed an operator-function-id.
1442 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1443 return false;
1444 }
Sean Hunt0486d742009-11-28 04:44:28 +00001445
1446 // Parse a literal-operator-id.
1447 //
1448 // literal-operator-id: [C++0x 13.5.8]
1449 // operator "" identifier
1450
1451 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1452 if (Tok.getLength() != 2)
1453 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1454 ConsumeStringToken();
1455
1456 if (Tok.isNot(tok::identifier)) {
1457 Diag(Tok.getLocation(), diag::err_expected_ident);
1458 return true;
1459 }
1460
1461 IdentifierInfo *II = Tok.getIdentifierInfo();
1462 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001463 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001464 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001465
1466 // Parse a conversion-function-id.
1467 //
1468 // conversion-function-id: [C++ 12.3.2]
1469 // operator conversion-type-id
1470 //
1471 // conversion-type-id:
1472 // type-specifier-seq conversion-declarator[opt]
1473 //
1474 // conversion-declarator:
1475 // ptr-operator conversion-declarator[opt]
1476
1477 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001478 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001479 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001480 return true;
1481
1482 // Parse the conversion-declarator, which is merely a sequence of
1483 // ptr-operators.
1484 Declarator D(DS, Declarator::TypeNameContext);
1485 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1486
1487 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001488 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001489 if (Ty.isInvalid())
1490 return true;
1491
1492 // Note that this is a conversion-function-id.
1493 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1494 D.getSourceRange().getEnd());
1495 return false;
1496}
1497
1498/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1499/// name of an entity.
1500///
1501/// \code
1502/// unqualified-id: [C++ expr.prim.general]
1503/// identifier
1504/// operator-function-id
1505/// conversion-function-id
1506/// [C++0x] literal-operator-id [TODO]
1507/// ~ class-name
1508/// template-id
1509///
1510/// \endcode
1511///
1512/// \param The nested-name-specifier that preceded this unqualified-id. If
1513/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1514///
1515/// \param EnteringContext whether we are entering the scope of the
1516/// nested-name-specifier.
1517///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001518/// \param AllowDestructorName whether we allow parsing of a destructor name.
1519///
1520/// \param AllowConstructorName whether we allow parsing a constructor name.
1521///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001522/// \param ObjectType if this unqualified-id occurs within a member access
1523/// expression, the type of the base object whose member is being accessed.
1524///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001525/// \param Result on a successful parse, contains the parsed unqualified-id.
1526///
1527/// \returns true if parsing fails, false otherwise.
1528bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1529 bool AllowDestructorName,
1530 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001531 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001532 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001533
1534 // Handle 'A::template B'. This is for template-ids which have not
1535 // already been annotated by ParseOptionalCXXScopeSpecifier().
1536 bool TemplateSpecified = false;
1537 SourceLocation TemplateKWLoc;
1538 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1539 (ObjectType || SS.isSet())) {
1540 TemplateSpecified = true;
1541 TemplateKWLoc = ConsumeToken();
1542 }
1543
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001544 // unqualified-id:
1545 // identifier
1546 // template-id (when it hasn't already been annotated)
1547 if (Tok.is(tok::identifier)) {
1548 // Consume the identifier.
1549 IdentifierInfo *Id = Tok.getIdentifierInfo();
1550 SourceLocation IdLoc = ConsumeToken();
1551
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001552 if (!getLang().CPlusPlus) {
1553 // If we're not in C++, only identifiers matter. Record the
1554 // identifier and return.
1555 Result.setIdentifier(Id, IdLoc);
1556 return false;
1557 }
1558
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001559 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001560 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001561 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001562 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001563 &SS, false, false,
1564 ParsedType(),
1565 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001566 IdLoc, IdLoc);
1567 } else {
1568 // We have parsed an identifier.
1569 Result.setIdentifier(Id, IdLoc);
1570 }
1571
1572 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001573 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001574 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001575 ObjectType, Result,
1576 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001577
1578 return false;
1579 }
1580
1581 // unqualified-id:
1582 // template-id (already parsed and annotated)
1583 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001584 TemplateIdAnnotation *TemplateId
1585 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1586
1587 // If the template-name names the current class, then this is a constructor
1588 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001589 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001590 if (SS.isSet()) {
1591 // C++ [class.qual]p2 specifies that a qualified template-name
1592 // is taken as the constructor name where a constructor can be
1593 // declared. Thus, the template arguments are extraneous, so
1594 // complain about them and remove them entirely.
1595 Diag(TemplateId->TemplateNameLoc,
1596 diag::err_out_of_line_constructor_template_id)
1597 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001598 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001599 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1600 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1601 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001602 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001603 &SS, false, false,
1604 ParsedType(),
1605 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001606 TemplateId->TemplateNameLoc,
1607 TemplateId->RAngleLoc);
1608 TemplateId->Destroy();
1609 ConsumeToken();
1610 return false;
1611 }
1612
1613 Result.setConstructorTemplateId(TemplateId);
1614 ConsumeToken();
1615 return false;
1616 }
1617
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001618 // We have already parsed a template-id; consume the annotation token as
1619 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001620 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001621 ConsumeToken();
1622 return false;
1623 }
1624
1625 // unqualified-id:
1626 // operator-function-id
1627 // conversion-function-id
1628 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001629 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001630 return true;
1631
Sean Hunte6252d12009-11-28 08:58:14 +00001632 // If we have an operator-function-id or a literal-operator-id and the next
1633 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001634 //
1635 // template-id:
1636 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001637 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1638 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001639 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001640 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1641 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001642 Result,
1643 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001644
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001645 return false;
1646 }
1647
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001648 if (getLang().CPlusPlus &&
1649 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001650 // C++ [expr.unary.op]p10:
1651 // There is an ambiguity in the unary-expression ~X(), where X is a
1652 // class-name. The ambiguity is resolved in favor of treating ~ as a
1653 // unary complement rather than treating ~X as referring to a destructor.
1654
1655 // Parse the '~'.
1656 SourceLocation TildeLoc = ConsumeToken();
1657
1658 // Parse the class-name.
1659 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001660 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001661 return true;
1662 }
1663
1664 // Parse the class-name (or template-name in a simple-template-id).
1665 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1666 SourceLocation ClassNameLoc = ConsumeToken();
1667
Douglas Gregor0278e122010-05-05 05:58:24 +00001668 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001669 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001670 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001671 EnteringContext, ObjectType, Result,
1672 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001673 }
1674
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001675 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001676 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1677 ClassNameLoc, getCurScope(),
1678 SS, ObjectType,
1679 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001680 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001681 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001682
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001683 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001684 return false;
1685 }
1686
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001687 Diag(Tok, diag::err_expected_unqualified_id)
1688 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001689 return true;
1690}
1691
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001692/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1693/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001694///
Chris Lattner59232d32009-01-04 21:25:24 +00001695/// This method is called to parse the new expression after the optional :: has
1696/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1697/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001698///
1699/// new-expression:
1700/// '::'[opt] 'new' new-placement[opt] new-type-id
1701/// new-initializer[opt]
1702/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1703/// new-initializer[opt]
1704///
1705/// new-placement:
1706/// '(' expression-list ')'
1707///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001708/// new-type-id:
1709/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001710/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001711///
1712/// new-declarator:
1713/// ptr-operator new-declarator[opt]
1714/// direct-new-declarator
1715///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001716/// new-initializer:
1717/// '(' expression-list[opt] ')'
1718/// [C++0x] braced-init-list [TODO]
1719///
John McCall60d7b3a2010-08-24 06:29:42 +00001720ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001721Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1722 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1723 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001724
1725 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1726 // second form of new-expression. It can't be a new-type-id.
1727
Sebastian Redla55e52c2008-11-25 22:21:31 +00001728 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001729 SourceLocation PlacementLParen, PlacementRParen;
1730
Douglas Gregor4bd40312010-07-13 15:54:32 +00001731 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00001732 DeclSpec DS(AttrFactory);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001733 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001734 if (Tok.is(tok::l_paren)) {
1735 // If it turns out to be a placement, we change the type location.
1736 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001737 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1738 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001739 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001740 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001741
1742 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001743 if (PlacementRParen.isInvalid()) {
1744 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001745 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001746 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001747
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001748 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001749 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001750 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001751 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001752 } else {
1753 // We still need the type.
1754 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001755 TypeIdParens.setBegin(ConsumeParen());
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001756 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001757 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001758 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001759 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001760 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1761 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001762 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001763 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001764 if (ParseCXXTypeSpecifierSeq(DS))
1765 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001766 else {
1767 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001768 ParseDeclaratorInternal(DeclaratorInfo,
1769 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001770 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001771 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001772 }
1773 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001774 // A new-type-id is a simplified type-id, where essentially the
1775 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001776 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001777 if (ParseCXXTypeSpecifierSeq(DS))
1778 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001779 else {
1780 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001781 ParseDeclaratorInternal(DeclaratorInfo,
1782 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001783 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001784 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001785 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001786 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001787 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001788 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001789
Sebastian Redla55e52c2008-11-25 22:21:31 +00001790 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001791 SourceLocation ConstructorLParen, ConstructorRParen;
1792
1793 if (Tok.is(tok::l_paren)) {
1794 ConstructorLParen = ConsumeParen();
1795 if (Tok.isNot(tok::r_paren)) {
1796 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001797 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1798 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001799 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001800 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001801 }
1802 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001803 if (ConstructorRParen.isInvalid()) {
1804 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001805 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001806 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001807 }
1808
Sebastian Redlf53597f2009-03-15 17:47:39 +00001809 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1810 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001811 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001812 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001813}
1814
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001815/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1816/// passed to ParseDeclaratorInternal.
1817///
1818/// direct-new-declarator:
1819/// '[' expression ']'
1820/// direct-new-declarator '[' constant-expression ']'
1821///
Chris Lattner59232d32009-01-04 21:25:24 +00001822void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001823 // Parse the array dimensions.
1824 bool first = true;
1825 while (Tok.is(tok::l_square)) {
1826 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001827 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001828 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001829 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001830 // Recover
1831 SkipUntil(tok::r_square);
1832 return;
1833 }
1834 first = false;
1835
Sebastian Redlab197ba2009-02-09 18:23:29 +00001836 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall0b7e6782011-03-24 11:26:52 +00001837
1838 ParsedAttributes attrs(AttrFactory);
1839 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00001840 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001841 Size.release(), LLoc, RLoc),
John McCall0b7e6782011-03-24 11:26:52 +00001842 attrs, RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001843
Sebastian Redlab197ba2009-02-09 18:23:29 +00001844 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001845 return;
1846 }
1847}
1848
1849/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1850/// This ambiguity appears in the syntax of the C++ new operator.
1851///
1852/// new-expression:
1853/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1854/// new-initializer[opt]
1855///
1856/// new-placement:
1857/// '(' expression-list ')'
1858///
John McCallca0408f2010-08-23 06:44:23 +00001859bool Parser::ParseExpressionListOrTypeId(
1860 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001861 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001862 // The '(' was already consumed.
1863 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001864 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001865 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001866 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001867 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001868 }
1869
1870 // It's not a type, it has to be an expression list.
1871 // Discard the comma locations - ActOnCXXNew has enough parameters.
1872 CommaLocsTy CommaLocs;
1873 return ParseExpressionList(PlacementArgs, CommaLocs);
1874}
1875
1876/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1877/// to free memory allocated by new.
1878///
Chris Lattner59232d32009-01-04 21:25:24 +00001879/// This method is called to parse the 'delete' expression after the optional
1880/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1881/// and "Start" is its location. Otherwise, "Start" is the location of the
1882/// 'delete' token.
1883///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001884/// delete-expression:
1885/// '::'[opt] 'delete' cast-expression
1886/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001887ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001888Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1889 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1890 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001891
1892 // Array delete?
1893 bool ArrayDelete = false;
1894 if (Tok.is(tok::l_square)) {
1895 ArrayDelete = true;
1896 SourceLocation LHS = ConsumeBracket();
1897 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1898 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001899 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001900 }
1901
John McCall60d7b3a2010-08-24 06:29:42 +00001902 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001903 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001904 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001905
John McCall9ae2f072010-08-23 23:25:46 +00001906 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001907}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001908
Mike Stump1eb44332009-09-09 15:08:12 +00001909static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001910 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001911 default: llvm_unreachable("Not a known unary type trait");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001912 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1913 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1914 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1915 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1916 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1917 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1918 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1919 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1920 case tok::kw___is_abstract: return UTT_IsAbstract;
1921 case tok::kw___is_class: return UTT_IsClass;
1922 case tok::kw___is_empty: return UTT_IsEmpty;
1923 case tok::kw___is_enum: return UTT_IsEnum;
1924 case tok::kw___is_pod: return UTT_IsPOD;
1925 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1926 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001927 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001928 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001929}
1930
1931static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1932 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001933 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001934 case tok::kw___is_base_of: return BTT_IsBaseOf;
1935 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001936 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001937 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001938}
1939
1940/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1941/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1942/// templates.
1943///
1944/// primary-expression:
1945/// [GNU] unary-type-trait '(' type-id ')'
1946///
John McCall60d7b3a2010-08-24 06:29:42 +00001947ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001948 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1949 SourceLocation Loc = ConsumeToken();
1950
1951 SourceLocation LParen = Tok.getLocation();
1952 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1953 return ExprError();
1954
1955 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1956 // there will be cryptic errors about mismatched parentheses and missing
1957 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001958 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001959
1960 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1961
Douglas Gregor809070a2009-02-18 17:45:20 +00001962 if (Ty.isInvalid())
1963 return ExprError();
1964
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00001965 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001966}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001967
Francois Pichet6ad6f282010-12-07 00:08:36 +00001968/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
1969/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1970/// templates.
1971///
1972/// primary-expression:
1973/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
1974///
1975ExprResult Parser::ParseBinaryTypeTrait() {
1976 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
1977 SourceLocation Loc = ConsumeToken();
1978
1979 SourceLocation LParen = Tok.getLocation();
1980 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1981 return ExprError();
1982
1983 TypeResult LhsTy = ParseTypeName();
1984 if (LhsTy.isInvalid()) {
1985 SkipUntil(tok::r_paren);
1986 return ExprError();
1987 }
1988
1989 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
1990 SkipUntil(tok::r_paren);
1991 return ExprError();
1992 }
1993
1994 TypeResult RhsTy = ParseTypeName();
1995 if (RhsTy.isInvalid()) {
1996 SkipUntil(tok::r_paren);
1997 return ExprError();
1998 }
1999
2000 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2001
2002 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
2003}
2004
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002005/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2006/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2007/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002008ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002009Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002010 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002011 SourceLocation LParenLoc,
2012 SourceLocation &RParenLoc) {
2013 assert(getLang().CPlusPlus && "Should only be called for C++!");
2014 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2015 assert(isTypeIdInParens() && "Not a type-id!");
2016
John McCall60d7b3a2010-08-24 06:29:42 +00002017 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002018 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002019
2020 // We need to disambiguate a very ugly part of the C++ syntax:
2021 //
2022 // (T())x; - type-id
2023 // (T())*x; - type-id
2024 // (T())/x; - expression
2025 // (T()); - expression
2026 //
2027 // The bad news is that we cannot use the specialized tentative parser, since
2028 // it can only verify that the thing inside the parens can be parsed as
2029 // type-id, it is not useful for determining the context past the parens.
2030 //
2031 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002032 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002033 //
2034 // It uses a scheme similar to parsing inline methods. The parenthesized
2035 // tokens are cached, the context that follows is determined (possibly by
2036 // parsing a cast-expression), and then we re-introduce the cached tokens
2037 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002038
Mike Stump1eb44332009-09-09 15:08:12 +00002039 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002040 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002041
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002042 // Store the tokens of the parentheses. We will parse them after we determine
2043 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002044 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002045 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002046 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2047 return ExprError();
2048 }
2049
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002050 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002051 ParseAs = CompoundLiteral;
2052 } else {
2053 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002054 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2055 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2056 NotCastExpr = true;
2057 } else {
2058 // Try parsing the cast-expression that may follow.
2059 // If it is not a cast-expression, NotCastExpr will be true and no token
2060 // will be consumed.
2061 Result = ParseCastExpression(false/*isUnaryExpression*/,
2062 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002063 NotCastExpr,
2064 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002065 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002066
2067 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2068 // an expression.
2069 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002070 }
2071
Mike Stump1eb44332009-09-09 15:08:12 +00002072 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002073 Toks.push_back(Tok);
2074 // Re-enter the stored parenthesized tokens into the token stream, so we may
2075 // parse them now.
2076 PP.EnterTokenStream(Toks.data(), Toks.size(),
2077 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2078 // Drop the current token and bring the first cached one. It's the same token
2079 // as when we entered this function.
2080 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002081
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002082 if (ParseAs >= CompoundLiteral) {
2083 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002084
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002085 // Match the ')'.
2086 if (Tok.is(tok::r_paren))
2087 RParenLoc = ConsumeParen();
2088 else
2089 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002090
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002091 if (ParseAs == CompoundLiteral) {
2092 ExprType = CompoundLiteral;
2093 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2094 }
Mike Stump1eb44332009-09-09 15:08:12 +00002095
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002096 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2097 assert(ParseAs == CastExpr);
2098
2099 if (Ty.isInvalid())
2100 return ExprError();
2101
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002102 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002103
2104 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002105 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002106 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002107 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002108 return move(Result);
2109 }
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002111 // Not a compound literal, and not followed by a cast-expression.
2112 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002113
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002114 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002115 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002116 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002117 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002118
2119 // Match the ')'.
2120 if (Result.isInvalid()) {
2121 SkipUntil(tok::r_paren);
2122 return ExprError();
2123 }
Mike Stump1eb44332009-09-09 15:08:12 +00002124
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002125 if (Tok.is(tok::r_paren))
2126 RParenLoc = ConsumeParen();
2127 else
2128 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2129
2130 return move(Result);
2131}