blob: e12f62fb290f09466025b0179bce3df70098afa0 [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
Mike Stump1eb44332009-09-09 15:08:12 +000023/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000024///
25/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000026/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000027/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000028///
29/// '::'[opt] nested-name-specifier
30/// '::'
31///
32/// nested-name-specifier:
33/// type-name '::'
34/// namespace-name '::'
35/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000036/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000037///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000038///
Mike Stump1eb44332009-09-09 15:08:12 +000039/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000040/// nested-name-specifier (or empty)
41///
Mike Stump1eb44332009-09-09 15:08:12 +000042/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000043/// the "." or "->" of a member access expression, this parameter provides the
44/// type of the object whose members are being accessed.
45///
46/// \param EnteringContext whether we will be entering into the context of
47/// the nested-name-specifier after parsing it.
48///
Douglas Gregord4dca082010-02-24 18:44:31 +000049/// \param MayBePseudoDestructor When non-NULL, points to a flag that
50/// indicates whether this nested-name-specifier may be part of a
51/// pseudo-destructor name. In this case, the flag will be set false
52/// if we don't actually end up parsing a destructor name. Moreorover,
53/// if we do end up determining that we are parsing a destructor name,
54/// the last component of the nested-name-specifier is not parsed as
55/// part of the scope specifier.
56
Douglas Gregorb10cd042010-02-21 18:36:56 +000057/// member access expression, e.g., the \p T:: in \p p->T::m.
58///
John McCall9ba61662010-02-26 08:45:28 +000059/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +000060bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +000061 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +000062 bool EnteringContext,
Douglas Gregord4dca082010-02-24 18:44:31 +000063 bool *MayBePseudoDestructor) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000064 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000065 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000066
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000067 if (Tok.is(tok::annot_cxxscope)) {
John McCallca0408f2010-08-23 06:44:23 +000068 SS.setScopeRep(static_cast<NestedNameSpecifier*>(Tok.getAnnotationValue()));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000069 SS.setRange(Tok.getAnnotationRange());
70 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +000071 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000072 }
Chris Lattnere607e802009-01-04 21:14:15 +000073
Douglas Gregor39a8de12009-02-25 19:37:18 +000074 bool HasScopeSpecifier = false;
75
Chris Lattner5b454732009-01-05 03:55:46 +000076 if (Tok.is(tok::coloncolon)) {
77 // ::new and ::delete aren't nested-name-specifiers.
78 tok::TokenKind NextKind = NextToken().getKind();
79 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
80 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner55a7cef2009-01-05 00:13:00 +000082 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000083 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
84 return true;
85
Douglas Gregor39a8de12009-02-25 19:37:18 +000086 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000087 }
88
Douglas Gregord4dca082010-02-24 18:44:31 +000089 bool CheckForDestructor = false;
90 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
91 CheckForDestructor = true;
92 *MayBePseudoDestructor = false;
93 }
94
Douglas Gregor39a8de12009-02-25 19:37:18 +000095 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000096 if (HasScopeSpecifier) {
97 // C++ [basic.lookup.classref]p5:
98 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000099 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000100 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000101 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000102 // the class-name-or-namespace-name is looked up in global scope as a
103 // class-name or namespace-name.
104 //
105 // To implement this, we clear out the object type as soon as we've
106 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000107 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000108
109 if (Tok.is(tok::code_completion)) {
110 // Code completion for a nested-name-specifier, where the code
111 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000112 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Douglas Gregordc845342010-05-25 05:58:43 +0000113 ConsumeCodeCompletionToken();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000114 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Douglas Gregor39a8de12009-02-25 19:37:18 +0000117 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000118 // nested-name-specifier 'template'[opt] simple-template-id '::'
119
120 // Parse the optional 'template' keyword, then make sure we have
121 // 'identifier <' after it.
122 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000124 // nested-name-specifier, since they aren't allowed to start with
125 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000126 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000127 break;
128
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000129 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000130 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000131
132 UnqualifiedId TemplateName;
133 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000134 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000135 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000136 ConsumeToken();
137 } else if (Tok.is(tok::kw_operator)) {
138 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000139 TemplateName)) {
140 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000141 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000142 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000143
Sean Hunte6252d12009-11-28 08:58:14 +0000144 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
145 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000146 Diag(TemplateName.getSourceRange().getBegin(),
147 diag::err_id_after_template_in_nested_name_spec)
148 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000149 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000150 break;
151 }
152 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000153 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000154 break;
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000157 // If the next token is not '<', we have a qualified-id that refers
158 // to a template name, such as T::template apply, but is not a
159 // template-id.
160 if (Tok.isNot(tok::less)) {
161 TPA.Revert();
162 break;
163 }
164
165 // Commit to parsing the template-id.
166 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000167 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000168 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000169 TemplateKWLoc,
170 SS,
171 TemplateName,
172 ObjectType,
173 EnteringContext,
174 Template)) {
175 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
176 TemplateKWLoc, false))
177 return true;
178 } else
John McCall9ba61662010-02-26 08:45:28 +0000179 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner77cf72a2009-06-26 03:47:46 +0000181 continue;
182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregor39a8de12009-02-25 19:37:18 +0000184 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000185 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000186 //
187 // simple-template-id '::'
188 //
189 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000190 // right kind (it should name a type or be dependent), and then
191 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000192 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000193 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord4dca082010-02-24 18:44:31 +0000194 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
195 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000196 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000197 }
198
Mike Stump1eb44332009-09-09 15:08:12 +0000199 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000200 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000201 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000202
Mike Stump1eb44332009-09-09 15:08:12 +0000203 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000204 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000205 Token TypeToken = Tok;
206 ConsumeToken();
207 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
208 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Douglas Gregor39a8de12009-02-25 19:37:18 +0000210 if (!HasScopeSpecifier) {
211 SS.setBeginLoc(TypeToken.getLocation());
212 HasScopeSpecifier = true;
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
John McCallb3d87482010-08-24 05:47:05 +0000215 if (ParsedType T = getTypeAnnotation(TypeToken)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000216 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), T, CCLoc, SS))
217 SS.SetInvalid(SourceRange(SS.getBeginLoc(), CCLoc));
218
219 continue;
220 } else {
221 SS.SetInvalid(SourceRange(SS.getBeginLoc(), CCLoc));
222 }
223
Douglas Gregor39a8de12009-02-25 19:37:18 +0000224 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattner67b9e832009-06-26 03:45:46 +0000227 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000228 }
229
Chris Lattner5c7f7862009-06-26 03:52:38 +0000230
231 // The rest of the nested-name-specifier possibilities start with
232 // tok::identifier.
233 if (Tok.isNot(tok::identifier))
234 break;
235
236 IdentifierInfo &II = *Tok.getIdentifierInfo();
237
238 // nested-name-specifier:
239 // type-name '::'
240 // namespace-name '::'
241 // nested-name-specifier identifier '::'
242 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000243
244 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
245 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000246 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000247 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
248 Tok.getLocation(),
249 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000250 EnteringContext) &&
251 // If the token after the colon isn't an identifier, it's still an
252 // error, but they probably meant something else strange so don't
253 // recover like this.
254 PP.LookAhead(1).is(tok::identifier)) {
255 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000256 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000257
258 // Recover as if the user wrote '::'.
259 Next.setKind(tok::coloncolon);
260 }
Chris Lattner46646492009-12-07 01:36:53 +0000261 }
262
Chris Lattner5c7f7862009-06-26 03:52:38 +0000263 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000264 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000265 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000266 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000267 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000268 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000269 }
270
Chris Lattner5c7f7862009-06-26 03:52:38 +0000271 // We have an identifier followed by a '::'. Lookup this name
272 // as the name in a nested-name-specifier.
273 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000274 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
275 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000276 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000278 HasScopeSpecifier = true;
279 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
280 ObjectType, EnteringContext, SS))
281 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
282
Chris Lattner5c7f7862009-06-26 03:52:38 +0000283 continue;
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner5c7f7862009-06-26 03:52:38 +0000286 // nested-name-specifier:
287 // type-name '<'
288 if (Next.is(tok::less)) {
289 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000290 UnqualifiedId TemplateName;
291 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000292 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000293 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000294 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000295 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000296 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000297 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000298 Template,
299 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000300 // We have found a template name, so annotate this this token
301 // with a template-id annotation. We do not permit the
302 // template-id to be translated into a type annotation,
303 // because some clients (e.g., the parsing of class template
304 // specializations) still want to see the original template-id
305 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000306 ConsumeToken();
307 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
308 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000309 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000310 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000311 }
312
313 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
314 IsTemplateArgumentList(1)) {
315 // We have something like t::getAs<T>, where getAs is a
316 // member of an unknown specialization. However, this will only
317 // parse correctly as a template, so suggest the keyword 'template'
318 // before 'getAs' and treat this as a dependent template name.
319 Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
320 << II.getName()
321 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
322
Douglas Gregord6ab2322010-06-16 23:00:59 +0000323 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000324 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000325 Tok.getLocation(), SS,
326 TemplateName, ObjectType,
327 EnteringContext, Template)) {
328 // Consume the identifier.
329 ConsumeToken();
330 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
331 SourceLocation(), false))
332 return true;
333 }
334 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000335 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000336
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000337 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000338 }
339 }
340
Douglas Gregor39a8de12009-02-25 19:37:18 +0000341 // We don't have any tokens that form the beginning of a
342 // nested-name-specifier, so we're done.
343 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Douglas Gregord4dca082010-02-24 18:44:31 +0000346 // Even if we didn't see any pieces of a nested-name-specifier, we
347 // still check whether there is a tilde in this position, which
348 // indicates a potential pseudo-destructor.
349 if (CheckForDestructor && Tok.is(tok::tilde))
350 *MayBePseudoDestructor = true;
351
John McCall9ba61662010-02-26 08:45:28 +0000352 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000353}
354
355/// ParseCXXIdExpression - Handle id-expression.
356///
357/// id-expression:
358/// unqualified-id
359/// qualified-id
360///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000361/// qualified-id:
362/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
363/// '::' identifier
364/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000365/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000366///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000367/// NOTE: The standard specifies that, for qualified-id, the parser does not
368/// expect:
369///
370/// '::' conversion-function-id
371/// '::' '~' class-name
372///
373/// This may cause a slight inconsistency on diagnostics:
374///
375/// class C {};
376/// namespace A {}
377/// void f() {
378/// :: A :: ~ C(); // Some Sema error about using destructor with a
379/// // namespace.
380/// :: ~ C(); // Some Parser error like 'unexpected ~'.
381/// }
382///
383/// We simplify the parser a bit and make it work like:
384///
385/// qualified-id:
386/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
387/// '::' unqualified-id
388///
389/// That way Sema can handle and report similar errors for namespaces and the
390/// global scope.
391///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000392/// The isAddressOfOperand parameter indicates that this id-expression is a
393/// direct operand of the address-of operator. This is, besides member contexts,
394/// the only place where a qualified-id naming a non-static class member may
395/// appear.
396///
John McCall60d7b3a2010-08-24 06:29:42 +0000397ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000398 // qualified-id:
399 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
400 // '::' unqualified-id
401 //
402 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000403 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000404
405 UnqualifiedId Name;
406 if (ParseUnqualifiedId(SS,
407 /*EnteringContext=*/false,
408 /*AllowDestructorName=*/false,
409 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000410 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000411 Name))
412 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000413
414 // This is only the direct operand of an & operator if it is not
415 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000416 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
417 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000418
Douglas Gregor23c94db2010-07-02 17:43:08 +0000419 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000420 isAddressOfOperand);
421
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000422}
423
Reid Spencer5f016e22007-07-11 17:01:13 +0000424/// ParseCXXCasts - This handles the various ways to cast expressions to another
425/// type.
426///
427/// postfix-expression: [C++ 5.2p1]
428/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
429/// 'static_cast' '<' type-name '>' '(' expression ')'
430/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
431/// 'const_cast' '<' type-name '>' '(' expression ')'
432///
John McCall60d7b3a2010-08-24 06:29:42 +0000433ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 tok::TokenKind Kind = Tok.getKind();
435 const char *CastName = 0; // For error messages
436
437 switch (Kind) {
438 default: assert(0 && "Unknown C++ cast!"); abort();
439 case tok::kw_const_cast: CastName = "const_cast"; break;
440 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
441 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
442 case tok::kw_static_cast: CastName = "static_cast"; break;
443 }
444
445 SourceLocation OpLoc = ConsumeToken();
446 SourceLocation LAngleBracketLoc = Tok.getLocation();
447
448 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000449 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000450
Douglas Gregor809070a2009-02-18 17:45:20 +0000451 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 SourceLocation RAngleBracketLoc = Tok.getLocation();
453
Chris Lattner1ab3b962008-11-18 07:48:38 +0000454 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000455 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000456
457 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
458
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000459 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
460 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000461
John McCall60d7b3a2010-08-24 06:29:42 +0000462 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000464 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000465 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000466
Douglas Gregor809070a2009-02-18 17:45:20 +0000467 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000468 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000469 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000470 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000471 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000472
Sebastian Redl20df9b72008-12-11 22:51:44 +0000473 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000474}
475
Sebastian Redlc42e1182008-11-11 11:37:55 +0000476/// ParseCXXTypeid - This handles the C++ typeid expression.
477///
478/// postfix-expression: [C++ 5.2p1]
479/// 'typeid' '(' expression ')'
480/// 'typeid' '(' type-id ')'
481///
John McCall60d7b3a2010-08-24 06:29:42 +0000482ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000483 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
484
485 SourceLocation OpLoc = ConsumeToken();
486 SourceLocation LParenLoc = Tok.getLocation();
487 SourceLocation RParenLoc;
488
489 // typeid expressions are always parenthesized.
490 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
491 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000492 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000493
John McCall60d7b3a2010-08-24 06:29:42 +0000494 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000495
496 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000497 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000498
499 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000500 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000501
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000502 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000503 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000504
505 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000506 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000507 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000508 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000509 // When typeid is applied to an expression other than an lvalue of a
510 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000511 // operand (Clause 5).
512 //
Mike Stump1eb44332009-09-09 15:08:12 +0000513 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000514 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000515 // we the expression is potentially potentially evaluated.
516 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000517 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000518 Result = ParseExpression();
519
520 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000521 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000522 SkipUntil(tok::r_paren);
523 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000524 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
525 if (RParenLoc.isInvalid())
526 return ExprError();
527
Sebastian Redlc42e1182008-11-11 11:37:55 +0000528 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000529 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000530 }
531 }
532
Sebastian Redl20df9b72008-12-11 22:51:44 +0000533 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000534}
535
Francois Pichet01b7c302010-09-08 12:20:18 +0000536/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
537///
538/// '__uuidof' '(' expression ')'
539/// '__uuidof' '(' type-id ')'
540///
541ExprResult Parser::ParseCXXUuidof() {
542 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
543
544 SourceLocation OpLoc = ConsumeToken();
545 SourceLocation LParenLoc = Tok.getLocation();
546 SourceLocation RParenLoc;
547
548 // __uuidof expressions are always parenthesized.
549 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
550 "__uuidof"))
551 return ExprError();
552
553 ExprResult Result;
554
555 if (isTypeIdInParens()) {
556 TypeResult Ty = ParseTypeName();
557
558 // Match the ')'.
559 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
560
561 if (Ty.isInvalid())
562 return ExprError();
563
564 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
565 Ty.get().getAsOpaquePtr(), RParenLoc);
566 } else {
567 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
568 Result = ParseExpression();
569
570 // Match the ')'.
571 if (Result.isInvalid())
572 SkipUntil(tok::r_paren);
573 else {
574 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
575
576 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
577 Result.release(), RParenLoc);
578 }
579 }
580
581 return move(Result);
582}
583
Douglas Gregord4dca082010-02-24 18:44:31 +0000584/// \brief Parse a C++ pseudo-destructor expression after the base,
585/// . or -> operator, and nested-name-specifier have already been
586/// parsed.
587///
588/// postfix-expression: [C++ 5.2]
589/// postfix-expression . pseudo-destructor-name
590/// postfix-expression -> pseudo-destructor-name
591///
592/// pseudo-destructor-name:
593/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
594/// ::[opt] nested-name-specifier template simple-template-id ::
595/// ~type-name
596/// ::[opt] nested-name-specifier[opt] ~type-name
597///
John McCall60d7b3a2010-08-24 06:29:42 +0000598ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000599Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
600 tok::TokenKind OpKind,
601 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000602 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000603 // We're parsing either a pseudo-destructor-name or a dependent
604 // member access that has the same form as a
605 // pseudo-destructor-name. We parse both in the same way and let
606 // the action model sort them out.
607 //
608 // Note that the ::[opt] nested-name-specifier[opt] has already
609 // been parsed, and if there was a simple-template-id, it has
610 // been coalesced into a template-id annotation token.
611 UnqualifiedId FirstTypeName;
612 SourceLocation CCLoc;
613 if (Tok.is(tok::identifier)) {
614 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
615 ConsumeToken();
616 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
617 CCLoc = ConsumeToken();
618 } else if (Tok.is(tok::annot_template_id)) {
619 FirstTypeName.setTemplateId(
620 (TemplateIdAnnotation *)Tok.getAnnotationValue());
621 ConsumeToken();
622 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
623 CCLoc = ConsumeToken();
624 } else {
625 FirstTypeName.setIdentifier(0, SourceLocation());
626 }
627
628 // Parse the tilde.
629 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
630 SourceLocation TildeLoc = ConsumeToken();
631 if (!Tok.is(tok::identifier)) {
632 Diag(Tok, diag::err_destructor_tilde_identifier);
633 return ExprError();
634 }
635
636 // Parse the second type.
637 UnqualifiedId SecondTypeName;
638 IdentifierInfo *Name = Tok.getIdentifierInfo();
639 SourceLocation NameLoc = ConsumeToken();
640 SecondTypeName.setIdentifier(Name, NameLoc);
641
642 // If there is a '<', the second type name is a template-id. Parse
643 // it as such.
644 if (Tok.is(tok::less) &&
645 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000646 SecondTypeName, /*AssumeTemplateName=*/true,
647 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000648 return ExprError();
649
John McCall9ae2f072010-08-23 23:25:46 +0000650 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
651 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000652 SS, FirstTypeName, CCLoc,
653 TildeLoc, SecondTypeName,
654 Tok.is(tok::l_paren));
655}
656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
658///
659/// boolean-literal: [C++ 2.13.5]
660/// 'true'
661/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000662ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000664 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000665}
Chris Lattner50dd2892008-02-26 00:51:44 +0000666
667/// ParseThrowExpression - This handles the C++ throw expression.
668///
669/// throw-expression: [C++ 15]
670/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000671ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000672 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000673 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000674
Chris Lattner2a2819a2008-04-06 06:02:23 +0000675 // If the current token isn't the start of an assignment-expression,
676 // then the expression is not present. This handles things like:
677 // "C ? throw : (void)42", which is crazy but legal.
678 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
679 case tok::semi:
680 case tok::r_paren:
681 case tok::r_square:
682 case tok::r_brace:
683 case tok::colon:
684 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000685 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000686
Chris Lattner2a2819a2008-04-06 06:02:23 +0000687 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000688 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000689 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000690 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000691 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000692}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000693
694/// ParseCXXThis - This handles the C++ 'this' pointer.
695///
696/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
697/// a non-lvalue expression whose value is the address of the object for which
698/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000699ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000700 assert(Tok.is(tok::kw_this) && "Not 'this'!");
701 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000702 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000703}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000704
705/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
706/// Can be interpreted either as function-style casting ("int(x)")
707/// or class type construction ("ClassType(x,y,z)")
708/// or creation of a value-initialized type ("int()").
709///
710/// postfix-expression: [C++ 5.2p1]
711/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
712/// typename-specifier '(' expression-list[opt] ')' [TODO]
713///
John McCall60d7b3a2010-08-24 06:29:42 +0000714ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000715Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000716 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000717 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000718
719 assert(Tok.is(tok::l_paren) && "Expected '('!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000720 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
721
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000722 SourceLocation LParenLoc = ConsumeParen();
723
Sebastian Redla55e52c2008-11-25 22:21:31 +0000724 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000725 CommaLocsTy CommaLocs;
726
727 if (Tok.isNot(tok::r_paren)) {
728 if (ParseExpressionList(Exprs, CommaLocs)) {
729 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000730 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000731 }
732 }
733
734 // Match the ')'.
735 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
736
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000737 // TypeRep could be null, if it references an invalid typedef.
738 if (!TypeRep)
739 return ExprError();
740
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000741 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
742 "Unexpected number of commas!");
Douglas Gregorab6677e2010-09-08 00:15:04 +0000743 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000744 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000745}
746
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000747/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000748///
749/// condition:
750/// expression
751/// type-specifier-seq declarator '=' assignment-expression
752/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
753/// '=' assignment-expression
754///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000755/// \param ExprResult if the condition was parsed as an expression, the
756/// parsed expression.
757///
758/// \param DeclResult if the condition was parsed as a declaration, the
759/// parsed declaration.
760///
Douglas Gregor586596f2010-05-06 17:25:47 +0000761/// \param Loc The location of the start of the statement that requires this
762/// condition, e.g., the "for" in a for loop.
763///
764/// \param ConvertToBoolean Whether the condition expression should be
765/// converted to a boolean value.
766///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000767/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000768bool Parser::ParseCXXCondition(ExprResult &ExprOut,
769 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000770 SourceLocation Loc,
771 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000772 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000773 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000774 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000775 }
776
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000777 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000778 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000779 ExprOut = ParseExpression(); // expression
780 DeclOut = 0;
781 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000782 return true;
783
784 // If required, convert to a boolean value.
785 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000786 ExprOut
787 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
788 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000789 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000790
791 // type-specifier-seq
792 DeclSpec DS;
793 ParseSpecifierQualifierList(DS);
794
795 // declarator
796 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
797 ParseDeclarator(DeclaratorInfo);
798
799 // simple-asm-expr[opt]
800 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000801 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000802 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000803 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000804 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000805 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000806 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000807 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000808 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000809 }
810
811 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000812 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000813
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000814 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000815 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000816 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000817 DeclOut = Dcl.get();
818 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000819
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000820 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000821 if (isTokenEqualOrMistypedEqualEqual(
822 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000823 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000824 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000825 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000826 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
827 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000828 } else {
829 // FIXME: C++0x allows a braced-init-list
830 Diag(Tok, diag::err_expected_equal_after_declarator);
831 }
832
Douglas Gregor586596f2010-05-06 17:25:47 +0000833 // FIXME: Build a reference to this declaration? Convert it to bool?
834 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +0000835
836 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +0000837
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000838 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000839}
840
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000841/// \brief Determine whether the current token starts a C++
842/// simple-type-specifier.
843bool Parser::isCXXSimpleTypeSpecifier() const {
844 switch (Tok.getKind()) {
845 case tok::annot_typename:
846 case tok::kw_short:
847 case tok::kw_long:
848 case tok::kw_signed:
849 case tok::kw_unsigned:
850 case tok::kw_void:
851 case tok::kw_char:
852 case tok::kw_int:
853 case tok::kw_float:
854 case tok::kw_double:
855 case tok::kw_wchar_t:
856 case tok::kw_char16_t:
857 case tok::kw_char32_t:
858 case tok::kw_bool:
859 // FIXME: C++0x decltype support.
860 // GNU typeof support.
861 case tok::kw_typeof:
862 return true;
863
864 default:
865 break;
866 }
867
868 return false;
869}
870
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000871/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
872/// This should only be called when the current token is known to be part of
873/// simple-type-specifier.
874///
875/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000876/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000877/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
878/// char
879/// wchar_t
880/// bool
881/// short
882/// int
883/// long
884/// signed
885/// unsigned
886/// float
887/// double
888/// void
889/// [GNU] typeof-specifier
890/// [C++0x] auto [TODO]
891///
892/// type-name:
893/// class-name
894/// enum-name
895/// typedef-name
896///
897void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
898 DS.SetRangeStart(Tok.getLocation());
899 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000900 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000901 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000903 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000904 case tok::identifier: // foo::bar
905 case tok::coloncolon: // ::foo::bar
906 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000907 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000908 assert(0 && "Not a simple-type-specifier token!");
909 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000910
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000911 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000912 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000913 if (getTypeAnnotation(Tok))
914 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
915 getTypeAnnotation(Tok));
916 else
917 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000918
919 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
920 ConsumeToken();
921
922 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
923 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
924 // Objective-C interface. If we don't have Objective-C or a '<', this is
925 // just a normal reference to a typedef name.
926 if (Tok.is(tok::less) && getLang().ObjC1)
927 ParseObjCProtocolQualifiers(DS);
928
929 DS.Finish(Diags, PP);
930 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000931 }
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000933 // builtin types
934 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000935 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000936 break;
937 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000938 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000939 break;
940 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000941 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000942 break;
943 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000944 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000945 break;
946 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000947 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000948 break;
949 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000950 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000951 break;
952 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000953 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000954 break;
955 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000956 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000957 break;
958 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000959 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000960 break;
961 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000962 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000963 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000964 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000965 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000966 break;
967 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000968 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000969 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000970 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000971 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000972 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000974 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000975 // GNU typeof support.
976 case tok::kw_typeof:
977 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000978 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000979 return;
980 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000981 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000982 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
983 else
984 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000985 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000986 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000987}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000988
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000989/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
990/// [dcl.name]), which is a non-empty sequence of type-specifiers,
991/// e.g., "const short int". Note that the DeclSpec is *not* finished
992/// by parsing the type-specifier-seq, because these sequences are
993/// typically followed by some form of declarator. Returns true and
994/// emits diagnostics if this is not a type-specifier-seq, false
995/// otherwise.
996///
997/// type-specifier-seq: [C++ 8.1]
998/// type-specifier type-specifier-seq[opt]
999///
1000bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1001 DS.SetRangeStart(Tok.getLocation());
1002 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001003 unsigned DiagID;
1004 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001005
1006 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001007 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1008 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001009 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001010 return true;
1011 }
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Sebastian Redld9bafa72010-02-03 21:21:43 +00001013 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1014 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1015 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001016
Douglas Gregor396a9f22010-02-24 23:13:13 +00001017 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001018 return false;
1019}
1020
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001021/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1022/// some form.
1023///
1024/// This routine is invoked when a '<' is encountered after an identifier or
1025/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1026/// whether the unqualified-id is actually a template-id. This routine will
1027/// then parse the template arguments and form the appropriate template-id to
1028/// return to the caller.
1029///
1030/// \param SS the nested-name-specifier that precedes this template-id, if
1031/// we're actually parsing a qualified-id.
1032///
1033/// \param Name for constructor and destructor names, this is the actual
1034/// identifier that may be a template-name.
1035///
1036/// \param NameLoc the location of the class-name in a constructor or
1037/// destructor.
1038///
1039/// \param EnteringContext whether we're entering the scope of the
1040/// nested-name-specifier.
1041///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001042/// \param ObjectType if this unqualified-id occurs within a member access
1043/// expression, the type of the base object whose member is being accessed.
1044///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001045/// \param Id as input, describes the template-name or operator-function-id
1046/// that precedes the '<'. If template arguments were parsed successfully,
1047/// will be updated with the template-id.
1048///
Douglas Gregord4dca082010-02-24 18:44:31 +00001049/// \param AssumeTemplateId When true, this routine will assume that the name
1050/// refers to a template without performing name lookup to verify.
1051///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001052/// \returns true if a parse error occurred, false otherwise.
1053bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1054 IdentifierInfo *Name,
1055 SourceLocation NameLoc,
1056 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001057 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001058 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001059 bool AssumeTemplateId,
1060 SourceLocation TemplateKWLoc) {
1061 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1062 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001063
1064 TemplateTy Template;
1065 TemplateNameKind TNK = TNK_Non_template;
1066 switch (Id.getKind()) {
1067 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001068 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001069 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001070 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001071 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001072 Id, ObjectType, EnteringContext,
1073 Template);
1074 if (TNK == TNK_Non_template)
1075 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001076 } else {
1077 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001078 TNK = Actions.isTemplateName(getCurScope(), SS,
1079 TemplateKWLoc.isValid(), Id,
1080 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001081 MemberOfUnknownSpecialization);
1082
1083 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1084 ObjectType && IsTemplateArgumentList()) {
1085 // We have something like t->getAs<T>(), where getAs is a
1086 // member of an unknown specialization. However, this will only
1087 // parse correctly as a template, so suggest the keyword 'template'
1088 // before 'getAs' and treat this as a dependent template name.
1089 std::string Name;
1090 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1091 Name = Id.Identifier->getName();
1092 else {
1093 Name = "operator ";
1094 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1095 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1096 else
1097 Name += Id.Identifier->getName();
1098 }
1099 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1100 << Name
1101 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001102 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001103 SS, Id, ObjectType,
1104 EnteringContext, Template);
1105 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001106 return true;
1107 }
1108 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001109 break;
1110
Douglas Gregor014e88d2009-11-03 23:16:33 +00001111 case UnqualifiedId::IK_ConstructorName: {
1112 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001113 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001114 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001115 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1116 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001117 EnteringContext, Template,
1118 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001119 break;
1120 }
1121
Douglas Gregor014e88d2009-11-03 23:16:33 +00001122 case UnqualifiedId::IK_DestructorName: {
1123 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001124 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001125 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001126 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001127 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001128 TemplateName, ObjectType,
1129 EnteringContext, Template);
1130 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001131 return true;
1132 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001133 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1134 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001135 EnteringContext, Template,
1136 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001137
John McCallb3d87482010-08-24 05:47:05 +00001138 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001139 Diag(NameLoc, diag::err_destructor_template_id)
1140 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001141 return true;
1142 }
1143 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001144 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001145 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001146
1147 default:
1148 return false;
1149 }
1150
1151 if (TNK == TNK_Non_template)
1152 return false;
1153
1154 // Parse the enclosed template argument list.
1155 SourceLocation LAngleLoc, RAngleLoc;
1156 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001157 if (Tok.is(tok::less) &&
1158 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001159 &SS, true, LAngleLoc,
1160 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001161 RAngleLoc))
1162 return true;
1163
1164 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001165 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1166 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001167 // Form a parsed representation of the template-id to be stored in the
1168 // UnqualifiedId.
1169 TemplateIdAnnotation *TemplateId
1170 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1171
1172 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1173 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001174 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001175 TemplateId->TemplateNameLoc = Id.StartLocation;
1176 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001177 TemplateId->Name = 0;
1178 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1179 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001180 }
1181
John McCall2b5289b2010-08-23 07:28:44 +00001182 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001183 TemplateId->Kind = TNK;
1184 TemplateId->LAngleLoc = LAngleLoc;
1185 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001186 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001187 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001188 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001189 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001190
1191 Id.setTemplateId(TemplateId);
1192 return false;
1193 }
1194
1195 // Bundle the template arguments together.
1196 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001197 TemplateArgs.size());
1198
1199 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001200 TypeResult Type
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001201 = Actions.ActOnTemplateIdType(Template, NameLoc,
1202 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001203 RAngleLoc);
1204 if (Type.isInvalid())
1205 return true;
1206
1207 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1208 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1209 else
1210 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1211
1212 return false;
1213}
1214
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001215/// \brief Parse an operator-function-id or conversion-function-id as part
1216/// of a C++ unqualified-id.
1217///
1218/// This routine is responsible only for parsing the operator-function-id or
1219/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001220///
1221/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001222/// operator-function-id: [C++ 13.5]
1223/// 'operator' operator
1224///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001225/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001226/// new delete new[] delete[]
1227/// + - * / % ^ & | ~
1228/// ! = < > += -= *= /= %=
1229/// ^= &= |= << >> >>= <<= == !=
1230/// <= >= && || ++ -- , ->* ->
1231/// () []
1232///
1233/// conversion-function-id: [C++ 12.3.2]
1234/// operator conversion-type-id
1235///
1236/// conversion-type-id:
1237/// type-specifier-seq conversion-declarator[opt]
1238///
1239/// conversion-declarator:
1240/// ptr-operator conversion-declarator[opt]
1241/// \endcode
1242///
1243/// \param The nested-name-specifier that preceded this unqualified-id. If
1244/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1245///
1246/// \param EnteringContext whether we are entering the scope of the
1247/// nested-name-specifier.
1248///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001249/// \param ObjectType if this unqualified-id occurs within a member access
1250/// expression, the type of the base object whose member is being accessed.
1251///
1252/// \param Result on a successful parse, contains the parsed unqualified-id.
1253///
1254/// \returns true if parsing fails, false otherwise.
1255bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001256 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001257 UnqualifiedId &Result) {
1258 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1259
1260 // Consume the 'operator' keyword.
1261 SourceLocation KeywordLoc = ConsumeToken();
1262
1263 // Determine what kind of operator name we have.
1264 unsigned SymbolIdx = 0;
1265 SourceLocation SymbolLocations[3];
1266 OverloadedOperatorKind Op = OO_None;
1267 switch (Tok.getKind()) {
1268 case tok::kw_new:
1269 case tok::kw_delete: {
1270 bool isNew = Tok.getKind() == tok::kw_new;
1271 // Consume the 'new' or 'delete'.
1272 SymbolLocations[SymbolIdx++] = ConsumeToken();
1273 if (Tok.is(tok::l_square)) {
1274 // Consume the '['.
1275 SourceLocation LBracketLoc = ConsumeBracket();
1276 // Consume the ']'.
1277 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1278 LBracketLoc);
1279 if (RBracketLoc.isInvalid())
1280 return true;
1281
1282 SymbolLocations[SymbolIdx++] = LBracketLoc;
1283 SymbolLocations[SymbolIdx++] = RBracketLoc;
1284 Op = isNew? OO_Array_New : OO_Array_Delete;
1285 } else {
1286 Op = isNew? OO_New : OO_Delete;
1287 }
1288 break;
1289 }
1290
1291#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1292 case tok::Token: \
1293 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1294 Op = OO_##Name; \
1295 break;
1296#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1297#include "clang/Basic/OperatorKinds.def"
1298
1299 case tok::l_paren: {
1300 // Consume the '('.
1301 SourceLocation LParenLoc = ConsumeParen();
1302 // Consume the ')'.
1303 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1304 LParenLoc);
1305 if (RParenLoc.isInvalid())
1306 return true;
1307
1308 SymbolLocations[SymbolIdx++] = LParenLoc;
1309 SymbolLocations[SymbolIdx++] = RParenLoc;
1310 Op = OO_Call;
1311 break;
1312 }
1313
1314 case tok::l_square: {
1315 // Consume the '['.
1316 SourceLocation LBracketLoc = ConsumeBracket();
1317 // Consume the ']'.
1318 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1319 LBracketLoc);
1320 if (RBracketLoc.isInvalid())
1321 return true;
1322
1323 SymbolLocations[SymbolIdx++] = LBracketLoc;
1324 SymbolLocations[SymbolIdx++] = RBracketLoc;
1325 Op = OO_Subscript;
1326 break;
1327 }
1328
1329 case tok::code_completion: {
1330 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001331 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001332
1333 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001334 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001335
1336 // Don't try to parse any further.
1337 return true;
1338 }
1339
1340 default:
1341 break;
1342 }
1343
1344 if (Op != OO_None) {
1345 // We have parsed an operator-function-id.
1346 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1347 return false;
1348 }
Sean Hunt0486d742009-11-28 04:44:28 +00001349
1350 // Parse a literal-operator-id.
1351 //
1352 // literal-operator-id: [C++0x 13.5.8]
1353 // operator "" identifier
1354
1355 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1356 if (Tok.getLength() != 2)
1357 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1358 ConsumeStringToken();
1359
1360 if (Tok.isNot(tok::identifier)) {
1361 Diag(Tok.getLocation(), diag::err_expected_ident);
1362 return true;
1363 }
1364
1365 IdentifierInfo *II = Tok.getIdentifierInfo();
1366 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001367 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001368 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001369
1370 // Parse a conversion-function-id.
1371 //
1372 // conversion-function-id: [C++ 12.3.2]
1373 // operator conversion-type-id
1374 //
1375 // conversion-type-id:
1376 // type-specifier-seq conversion-declarator[opt]
1377 //
1378 // conversion-declarator:
1379 // ptr-operator conversion-declarator[opt]
1380
1381 // Parse the type-specifier-seq.
1382 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001383 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001384 return true;
1385
1386 // Parse the conversion-declarator, which is merely a sequence of
1387 // ptr-operators.
1388 Declarator D(DS, Declarator::TypeNameContext);
1389 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1390
1391 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001392 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001393 if (Ty.isInvalid())
1394 return true;
1395
1396 // Note that this is a conversion-function-id.
1397 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1398 D.getSourceRange().getEnd());
1399 return false;
1400}
1401
1402/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1403/// name of an entity.
1404///
1405/// \code
1406/// unqualified-id: [C++ expr.prim.general]
1407/// identifier
1408/// operator-function-id
1409/// conversion-function-id
1410/// [C++0x] literal-operator-id [TODO]
1411/// ~ class-name
1412/// template-id
1413///
1414/// \endcode
1415///
1416/// \param The nested-name-specifier that preceded this unqualified-id. If
1417/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1418///
1419/// \param EnteringContext whether we are entering the scope of the
1420/// nested-name-specifier.
1421///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001422/// \param AllowDestructorName whether we allow parsing of a destructor name.
1423///
1424/// \param AllowConstructorName whether we allow parsing a constructor name.
1425///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001426/// \param ObjectType if this unqualified-id occurs within a member access
1427/// expression, the type of the base object whose member is being accessed.
1428///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001429/// \param Result on a successful parse, contains the parsed unqualified-id.
1430///
1431/// \returns true if parsing fails, false otherwise.
1432bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1433 bool AllowDestructorName,
1434 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001435 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001436 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001437
1438 // Handle 'A::template B'. This is for template-ids which have not
1439 // already been annotated by ParseOptionalCXXScopeSpecifier().
1440 bool TemplateSpecified = false;
1441 SourceLocation TemplateKWLoc;
1442 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1443 (ObjectType || SS.isSet())) {
1444 TemplateSpecified = true;
1445 TemplateKWLoc = ConsumeToken();
1446 }
1447
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001448 // unqualified-id:
1449 // identifier
1450 // template-id (when it hasn't already been annotated)
1451 if (Tok.is(tok::identifier)) {
1452 // Consume the identifier.
1453 IdentifierInfo *Id = Tok.getIdentifierInfo();
1454 SourceLocation IdLoc = ConsumeToken();
1455
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001456 if (!getLang().CPlusPlus) {
1457 // If we're not in C++, only identifiers matter. Record the
1458 // identifier and return.
1459 Result.setIdentifier(Id, IdLoc);
1460 return false;
1461 }
1462
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001463 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001464 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001465 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001466 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001467 &SS, false),
1468 IdLoc, IdLoc);
1469 } else {
1470 // We have parsed an identifier.
1471 Result.setIdentifier(Id, IdLoc);
1472 }
1473
1474 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001475 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001476 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001477 ObjectType, Result,
1478 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001479
1480 return false;
1481 }
1482
1483 // unqualified-id:
1484 // template-id (already parsed and annotated)
1485 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001486 TemplateIdAnnotation *TemplateId
1487 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1488
1489 // If the template-name names the current class, then this is a constructor
1490 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001491 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001492 if (SS.isSet()) {
1493 // C++ [class.qual]p2 specifies that a qualified template-name
1494 // is taken as the constructor name where a constructor can be
1495 // declared. Thus, the template arguments are extraneous, so
1496 // complain about them and remove them entirely.
1497 Diag(TemplateId->TemplateNameLoc,
1498 diag::err_out_of_line_constructor_template_id)
1499 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001500 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001501 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1502 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1503 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001504 getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001505 &SS, false),
1506 TemplateId->TemplateNameLoc,
1507 TemplateId->RAngleLoc);
1508 TemplateId->Destroy();
1509 ConsumeToken();
1510 return false;
1511 }
1512
1513 Result.setConstructorTemplateId(TemplateId);
1514 ConsumeToken();
1515 return false;
1516 }
1517
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001518 // We have already parsed a template-id; consume the annotation token as
1519 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001520 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001521 ConsumeToken();
1522 return false;
1523 }
1524
1525 // unqualified-id:
1526 // operator-function-id
1527 // conversion-function-id
1528 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001529 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001530 return true;
1531
Sean Hunte6252d12009-11-28 08:58:14 +00001532 // If we have an operator-function-id or a literal-operator-id and the next
1533 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001534 //
1535 // template-id:
1536 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001537 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1538 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001539 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001540 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1541 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001542 Result,
1543 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001544
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001545 return false;
1546 }
1547
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001548 if (getLang().CPlusPlus &&
1549 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001550 // C++ [expr.unary.op]p10:
1551 // There is an ambiguity in the unary-expression ~X(), where X is a
1552 // class-name. The ambiguity is resolved in favor of treating ~ as a
1553 // unary complement rather than treating ~X as referring to a destructor.
1554
1555 // Parse the '~'.
1556 SourceLocation TildeLoc = ConsumeToken();
1557
1558 // Parse the class-name.
1559 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001560 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001561 return true;
1562 }
1563
1564 // Parse the class-name (or template-name in a simple-template-id).
1565 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1566 SourceLocation ClassNameLoc = ConsumeToken();
1567
Douglas Gregor0278e122010-05-05 05:58:24 +00001568 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001569 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001570 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001571 EnteringContext, ObjectType, Result,
1572 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001573 }
1574
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001575 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001576 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1577 ClassNameLoc, getCurScope(),
1578 SS, ObjectType,
1579 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001580 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001581 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001582
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001583 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001584 return false;
1585 }
1586
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001587 Diag(Tok, diag::err_expected_unqualified_id)
1588 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001589 return true;
1590}
1591
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001592/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1593/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001594///
Chris Lattner59232d32009-01-04 21:25:24 +00001595/// This method is called to parse the new expression after the optional :: has
1596/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1597/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001598///
1599/// new-expression:
1600/// '::'[opt] 'new' new-placement[opt] new-type-id
1601/// new-initializer[opt]
1602/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1603/// new-initializer[opt]
1604///
1605/// new-placement:
1606/// '(' expression-list ')'
1607///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001608/// new-type-id:
1609/// type-specifier-seq new-declarator[opt]
1610///
1611/// new-declarator:
1612/// ptr-operator new-declarator[opt]
1613/// direct-new-declarator
1614///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001615/// new-initializer:
1616/// '(' expression-list[opt] ')'
1617/// [C++0x] braced-init-list [TODO]
1618///
John McCall60d7b3a2010-08-24 06:29:42 +00001619ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001620Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1621 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1622 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001623
1624 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1625 // second form of new-expression. It can't be a new-type-id.
1626
Sebastian Redla55e52c2008-11-25 22:21:31 +00001627 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001628 SourceLocation PlacementLParen, PlacementRParen;
1629
Douglas Gregor4bd40312010-07-13 15:54:32 +00001630 SourceRange TypeIdParens;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001631 DeclSpec DS;
1632 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001633 if (Tok.is(tok::l_paren)) {
1634 // If it turns out to be a placement, we change the type location.
1635 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001636 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1637 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001638 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001639 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001640
1641 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001642 if (PlacementRParen.isInvalid()) {
1643 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001644 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001645 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001646
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001647 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001648 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001649 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001650 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001651 } else {
1652 // We still need the type.
1653 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001654 TypeIdParens.setBegin(ConsumeParen());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001655 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001656 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001657 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001658 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1659 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001660 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001661 if (ParseCXXTypeSpecifierSeq(DS))
1662 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001663 else {
1664 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001665 ParseDeclaratorInternal(DeclaratorInfo,
1666 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001667 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001668 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001669 }
1670 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001671 // A new-type-id is a simplified type-id, where essentially the
1672 // direct-declarator is replaced by a direct-new-declarator.
1673 if (ParseCXXTypeSpecifierSeq(DS))
1674 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001675 else {
1676 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001677 ParseDeclaratorInternal(DeclaratorInfo,
1678 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001679 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001680 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001681 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001682 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001683 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001684 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001685
Sebastian Redla55e52c2008-11-25 22:21:31 +00001686 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001687 SourceLocation ConstructorLParen, ConstructorRParen;
1688
1689 if (Tok.is(tok::l_paren)) {
1690 ConstructorLParen = ConsumeParen();
1691 if (Tok.isNot(tok::r_paren)) {
1692 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001693 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1694 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001695 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001696 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001697 }
1698 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001699 if (ConstructorRParen.isInvalid()) {
1700 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001701 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001702 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001703 }
1704
Sebastian Redlf53597f2009-03-15 17:47:39 +00001705 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1706 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001707 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001708 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001709}
1710
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001711/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1712/// passed to ParseDeclaratorInternal.
1713///
1714/// direct-new-declarator:
1715/// '[' expression ']'
1716/// direct-new-declarator '[' constant-expression ']'
1717///
Chris Lattner59232d32009-01-04 21:25:24 +00001718void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001719 // Parse the array dimensions.
1720 bool first = true;
1721 while (Tok.is(tok::l_square)) {
1722 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001723 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001724 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001725 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001726 // Recover
1727 SkipUntil(tok::r_square);
1728 return;
1729 }
1730 first = false;
1731
Sebastian Redlab197ba2009-02-09 18:23:29 +00001732 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall7f040a92010-12-24 02:08:15 +00001733 D.AddTypeInfo(DeclaratorChunk::getArray(0, ParsedAttributes(),
1734 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001735 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001736 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001737
Sebastian Redlab197ba2009-02-09 18:23:29 +00001738 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001739 return;
1740 }
1741}
1742
1743/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1744/// This ambiguity appears in the syntax of the C++ new operator.
1745///
1746/// new-expression:
1747/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1748/// new-initializer[opt]
1749///
1750/// new-placement:
1751/// '(' expression-list ')'
1752///
John McCallca0408f2010-08-23 06:44:23 +00001753bool Parser::ParseExpressionListOrTypeId(
1754 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001755 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001756 // The '(' was already consumed.
1757 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001758 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001759 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001760 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001761 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001762 }
1763
1764 // It's not a type, it has to be an expression list.
1765 // Discard the comma locations - ActOnCXXNew has enough parameters.
1766 CommaLocsTy CommaLocs;
1767 return ParseExpressionList(PlacementArgs, CommaLocs);
1768}
1769
1770/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1771/// to free memory allocated by new.
1772///
Chris Lattner59232d32009-01-04 21:25:24 +00001773/// This method is called to parse the 'delete' expression after the optional
1774/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1775/// and "Start" is its location. Otherwise, "Start" is the location of the
1776/// 'delete' token.
1777///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001778/// delete-expression:
1779/// '::'[opt] 'delete' cast-expression
1780/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001781ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001782Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1783 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1784 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001785
1786 // Array delete?
1787 bool ArrayDelete = false;
1788 if (Tok.is(tok::l_square)) {
1789 ArrayDelete = true;
1790 SourceLocation LHS = ConsumeBracket();
1791 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1792 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001793 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001794 }
1795
John McCall60d7b3a2010-08-24 06:29:42 +00001796 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001797 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001798 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001799
John McCall9ae2f072010-08-23 23:25:46 +00001800 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001801}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001802
Mike Stump1eb44332009-09-09 15:08:12 +00001803static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001804 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001805 default: llvm_unreachable("Not a known unary type trait");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001806 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1807 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1808 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1809 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1810 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1811 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1812 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1813 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1814 case tok::kw___is_abstract: return UTT_IsAbstract;
1815 case tok::kw___is_class: return UTT_IsClass;
1816 case tok::kw___is_empty: return UTT_IsEmpty;
1817 case tok::kw___is_enum: return UTT_IsEnum;
1818 case tok::kw___is_pod: return UTT_IsPOD;
1819 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1820 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001821 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001822 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001823}
1824
1825static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1826 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001827 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001828 case tok::kw___is_base_of: return BTT_IsBaseOf;
1829 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001830 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001831 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001832}
1833
1834/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1835/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1836/// templates.
1837///
1838/// primary-expression:
1839/// [GNU] unary-type-trait '(' type-id ')'
1840///
John McCall60d7b3a2010-08-24 06:29:42 +00001841ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001842 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1843 SourceLocation Loc = ConsumeToken();
1844
1845 SourceLocation LParen = Tok.getLocation();
1846 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1847 return ExprError();
1848
1849 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1850 // there will be cryptic errors about mismatched parentheses and missing
1851 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001852 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001853
1854 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1855
Douglas Gregor809070a2009-02-18 17:45:20 +00001856 if (Ty.isInvalid())
1857 return ExprError();
1858
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00001859 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001860}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001861
Francois Pichet6ad6f282010-12-07 00:08:36 +00001862/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
1863/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1864/// templates.
1865///
1866/// primary-expression:
1867/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
1868///
1869ExprResult Parser::ParseBinaryTypeTrait() {
1870 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
1871 SourceLocation Loc = ConsumeToken();
1872
1873 SourceLocation LParen = Tok.getLocation();
1874 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1875 return ExprError();
1876
1877 TypeResult LhsTy = ParseTypeName();
1878 if (LhsTy.isInvalid()) {
1879 SkipUntil(tok::r_paren);
1880 return ExprError();
1881 }
1882
1883 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
1884 SkipUntil(tok::r_paren);
1885 return ExprError();
1886 }
1887
1888 TypeResult RhsTy = ParseTypeName();
1889 if (RhsTy.isInvalid()) {
1890 SkipUntil(tok::r_paren);
1891 return ExprError();
1892 }
1893
1894 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1895
1896 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
1897}
1898
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001899/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1900/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1901/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00001902ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001903Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00001904 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001905 SourceLocation LParenLoc,
1906 SourceLocation &RParenLoc) {
1907 assert(getLang().CPlusPlus && "Should only be called for C++!");
1908 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1909 assert(isTypeIdInParens() && "Not a type-id!");
1910
John McCall60d7b3a2010-08-24 06:29:42 +00001911 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00001912 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001913
1914 // We need to disambiguate a very ugly part of the C++ syntax:
1915 //
1916 // (T())x; - type-id
1917 // (T())*x; - type-id
1918 // (T())/x; - expression
1919 // (T()); - expression
1920 //
1921 // The bad news is that we cannot use the specialized tentative parser, since
1922 // it can only verify that the thing inside the parens can be parsed as
1923 // type-id, it is not useful for determining the context past the parens.
1924 //
1925 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001926 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001927 //
1928 // It uses a scheme similar to parsing inline methods. The parenthesized
1929 // tokens are cached, the context that follows is determined (possibly by
1930 // parsing a cast-expression), and then we re-introduce the cached tokens
1931 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001932
Mike Stump1eb44332009-09-09 15:08:12 +00001933 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001934 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001935
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001936 // Store the tokens of the parentheses. We will parse them after we determine
1937 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001938 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001939 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001940 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1941 return ExprError();
1942 }
1943
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001944 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001945 ParseAs = CompoundLiteral;
1946 } else {
1947 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001948 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1949 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1950 NotCastExpr = true;
1951 } else {
1952 // Try parsing the cast-expression that may follow.
1953 // If it is not a cast-expression, NotCastExpr will be true and no token
1954 // will be consumed.
1955 Result = ParseCastExpression(false/*isUnaryExpression*/,
1956 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00001957 NotCastExpr,
1958 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001959 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001960
1961 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1962 // an expression.
1963 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001964 }
1965
Mike Stump1eb44332009-09-09 15:08:12 +00001966 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001967 Toks.push_back(Tok);
1968 // Re-enter the stored parenthesized tokens into the token stream, so we may
1969 // parse them now.
1970 PP.EnterTokenStream(Toks.data(), Toks.size(),
1971 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1972 // Drop the current token and bring the first cached one. It's the same token
1973 // as when we entered this function.
1974 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001975
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001976 if (ParseAs >= CompoundLiteral) {
1977 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001978
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001979 // Match the ')'.
1980 if (Tok.is(tok::r_paren))
1981 RParenLoc = ConsumeParen();
1982 else
1983 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001984
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001985 if (ParseAs == CompoundLiteral) {
1986 ExprType = CompoundLiteral;
1987 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1988 }
Mike Stump1eb44332009-09-09 15:08:12 +00001989
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001990 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1991 assert(ParseAs == CastExpr);
1992
1993 if (Ty.isInvalid())
1994 return ExprError();
1995
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001996 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001997
1998 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001999 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002000 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002001 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002002 return move(Result);
2003 }
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002005 // Not a compound literal, and not followed by a cast-expression.
2006 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002007
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002008 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002009 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002010 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002011 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002012
2013 // Match the ')'.
2014 if (Result.isInvalid()) {
2015 SkipUntil(tok::r_paren);
2016 return ExprError();
2017 }
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002019 if (Tok.is(tok::r_paren))
2020 RParenLoc = ConsumeParen();
2021 else
2022 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2023
2024 return move(Result);
2025}