blob: 2abec6f0cc5ccd769f69131673cc46a203602384 [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)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +000068 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
69 Tok.getAnnotationRange(),
70 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000071 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +000072 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000073 }
Chris Lattnere607e802009-01-04 21:14:15 +000074
Douglas Gregor39a8de12009-02-25 19:37:18 +000075 bool HasScopeSpecifier = false;
76
Chris Lattner5b454732009-01-05 03:55:46 +000077 if (Tok.is(tok::coloncolon)) {
78 // ::new and ::delete aren't nested-name-specifiers.
79 tok::TokenKind NextKind = NextToken().getKind();
80 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
81 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattner55a7cef2009-01-05 00:13:00 +000083 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000084 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
85 return true;
86
Douglas Gregor39a8de12009-02-25 19:37:18 +000087 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000088 }
89
Douglas Gregord4dca082010-02-24 18:44:31 +000090 bool CheckForDestructor = false;
91 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
92 CheckForDestructor = true;
93 *MayBePseudoDestructor = false;
94 }
95
Douglas Gregor39a8de12009-02-25 19:37:18 +000096 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000097 if (HasScopeSpecifier) {
98 // C++ [basic.lookup.classref]p5:
99 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000100 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000101 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000102 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000103 // the class-name-or-namespace-name is looked up in global scope as a
104 // class-name or namespace-name.
105 //
106 // To implement this, we clear out the object type as soon as we've
107 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000108 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000109
110 if (Tok.is(tok::code_completion)) {
111 // Code completion for a nested-name-specifier, where the code
112 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000113 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Douglas Gregordc845342010-05-25 05:58:43 +0000114 ConsumeCodeCompletionToken();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000115 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor39a8de12009-02-25 19:37:18 +0000118 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000119 // nested-name-specifier 'template'[opt] simple-template-id '::'
120
121 // Parse the optional 'template' keyword, then make sure we have
122 // 'identifier <' after it.
123 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000124 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000125 // nested-name-specifier, since they aren't allowed to start with
126 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000127 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000128 break;
129
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000130 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000131 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000132
133 UnqualifiedId TemplateName;
134 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000135 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000136 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000137 ConsumeToken();
138 } else if (Tok.is(tok::kw_operator)) {
139 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000140 TemplateName)) {
141 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000142 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000143 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000144
Sean Hunte6252d12009-11-28 08:58:14 +0000145 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
146 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000147 Diag(TemplateName.getSourceRange().getBegin(),
148 diag::err_id_after_template_in_nested_name_spec)
149 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000150 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000151 break;
152 }
153 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000154 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000155 break;
156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000158 // If the next token is not '<', we have a qualified-id that refers
159 // to a template name, such as T::template apply, but is not a
160 // template-id.
161 if (Tok.isNot(tok::less)) {
162 TPA.Revert();
163 break;
164 }
165
166 // Commit to parsing the template-id.
167 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000168 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000169 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000170 TemplateKWLoc,
171 SS,
172 TemplateName,
173 ObjectType,
174 EnteringContext,
175 Template)) {
176 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
177 TemplateKWLoc, false))
178 return true;
179 } else
John McCall9ba61662010-02-26 08:45:28 +0000180 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Chris Lattner77cf72a2009-06-26 03:47:46 +0000182 continue;
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Douglas Gregor39a8de12009-02-25 19:37:18 +0000185 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000186 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000187 //
188 // simple-template-id '::'
189 //
190 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000191 // right kind (it should name a type or be dependent), and then
192 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000193 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000194 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord4dca082010-02-24 18:44:31 +0000195 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
196 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000197 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000198 }
199
Mike Stump1eb44332009-09-09 15:08:12 +0000200 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000201 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000202 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000203
Mike Stump1eb44332009-09-09 15:08:12 +0000204 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000205 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000206 Token TypeToken = Tok;
207 ConsumeToken();
208 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
209 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Douglas Gregorc34348a2011-02-24 17:54:50 +0000211 if (!HasScopeSpecifier)
Douglas Gregor39a8de12009-02-25 19:37:18 +0000212 HasScopeSpecifier = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000213
John McCallb3d87482010-08-24 05:47:05 +0000214 if (ParsedType T = getTypeAnnotation(TypeToken)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000215 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), T, CCLoc, SS))
216 SS.SetInvalid(SourceRange(SS.getBeginLoc(), CCLoc));
217
218 continue;
219 } else {
220 SS.SetInvalid(SourceRange(SS.getBeginLoc(), CCLoc));
221 }
222
Douglas Gregor39a8de12009-02-25 19:37:18 +0000223 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000224 }
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Chris Lattner67b9e832009-06-26 03:45:46 +0000226 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000227 }
228
Chris Lattner5c7f7862009-06-26 03:52:38 +0000229
230 // The rest of the nested-name-specifier possibilities start with
231 // tok::identifier.
232 if (Tok.isNot(tok::identifier))
233 break;
234
235 IdentifierInfo &II = *Tok.getIdentifierInfo();
236
237 // nested-name-specifier:
238 // type-name '::'
239 // namespace-name '::'
240 // nested-name-specifier identifier '::'
241 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000242
243 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
244 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000245 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000246 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
247 Tok.getLocation(),
248 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000249 EnteringContext) &&
250 // If the token after the colon isn't an identifier, it's still an
251 // error, but they probably meant something else strange so don't
252 // recover like this.
253 PP.LookAhead(1).is(tok::identifier)) {
254 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000255 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000256
257 // Recover as if the user wrote '::'.
258 Next.setKind(tok::coloncolon);
259 }
Chris Lattner46646492009-12-07 01:36:53 +0000260 }
261
Chris Lattner5c7f7862009-06-26 03:52:38 +0000262 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000263 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000265 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000266 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000267 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000268 }
269
Chris Lattner5c7f7862009-06-26 03:52:38 +0000270 // We have an identifier followed by a '::'. Lookup this name
271 // as the name in a nested-name-specifier.
272 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000273 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
274 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000275 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000277 HasScopeSpecifier = true;
278 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
279 ObjectType, EnteringContext, SS))
280 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
281
Chris Lattner5c7f7862009-06-26 03:52:38 +0000282 continue;
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chris Lattner5c7f7862009-06-26 03:52:38 +0000285 // nested-name-specifier:
286 // type-name '<'
287 if (Next.is(tok::less)) {
288 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000289 UnqualifiedId TemplateName;
290 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000291 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000292 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000293 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000294 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000295 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000296 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000297 Template,
298 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000299 // We have found a template name, so annotate this this token
300 // with a template-id annotation. We do not permit the
301 // template-id to be translated into a type annotation,
302 // because some clients (e.g., the parsing of class template
303 // specializations) still want to see the original template-id
304 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000305 ConsumeToken();
306 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
307 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000308 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000309 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000310 }
311
312 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
313 IsTemplateArgumentList(1)) {
314 // We have something like t::getAs<T>, where getAs is a
315 // member of an unknown specialization. However, this will only
316 // parse correctly as a template, so suggest the keyword 'template'
317 // before 'getAs' and treat this as a dependent template name.
318 Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
319 << II.getName()
320 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
321
Douglas Gregord6ab2322010-06-16 23:00:59 +0000322 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000323 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000324 Tok.getLocation(), SS,
325 TemplateName, ObjectType,
326 EnteringContext, Template)) {
327 // Consume the identifier.
328 ConsumeToken();
329 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
330 SourceLocation(), false))
331 return true;
332 }
333 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000334 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000335
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000336 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000337 }
338 }
339
Douglas Gregor39a8de12009-02-25 19:37:18 +0000340 // We don't have any tokens that form the beginning of a
341 // nested-name-specifier, so we're done.
342 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000343 }
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Douglas Gregord4dca082010-02-24 18:44:31 +0000345 // Even if we didn't see any pieces of a nested-name-specifier, we
346 // still check whether there is a tilde in this position, which
347 // indicates a potential pseudo-destructor.
348 if (CheckForDestructor && Tok.is(tok::tilde))
349 *MayBePseudoDestructor = true;
350
John McCall9ba61662010-02-26 08:45:28 +0000351 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000352}
353
354/// ParseCXXIdExpression - Handle id-expression.
355///
356/// id-expression:
357/// unqualified-id
358/// qualified-id
359///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000360/// qualified-id:
361/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
362/// '::' identifier
363/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000364/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000365///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000366/// NOTE: The standard specifies that, for qualified-id, the parser does not
367/// expect:
368///
369/// '::' conversion-function-id
370/// '::' '~' class-name
371///
372/// This may cause a slight inconsistency on diagnostics:
373///
374/// class C {};
375/// namespace A {}
376/// void f() {
377/// :: A :: ~ C(); // Some Sema error about using destructor with a
378/// // namespace.
379/// :: ~ C(); // Some Parser error like 'unexpected ~'.
380/// }
381///
382/// We simplify the parser a bit and make it work like:
383///
384/// qualified-id:
385/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
386/// '::' unqualified-id
387///
388/// That way Sema can handle and report similar errors for namespaces and the
389/// global scope.
390///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000391/// The isAddressOfOperand parameter indicates that this id-expression is a
392/// direct operand of the address-of operator. This is, besides member contexts,
393/// the only place where a qualified-id naming a non-static class member may
394/// appear.
395///
John McCall60d7b3a2010-08-24 06:29:42 +0000396ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000397 // qualified-id:
398 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
399 // '::' unqualified-id
400 //
401 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000402 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000403
404 UnqualifiedId Name;
405 if (ParseUnqualifiedId(SS,
406 /*EnteringContext=*/false,
407 /*AllowDestructorName=*/false,
408 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000409 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000410 Name))
411 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000412
413 // This is only the direct operand of an & operator if it is not
414 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000415 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
416 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000417
Douglas Gregor23c94db2010-07-02 17:43:08 +0000418 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000419 isAddressOfOperand);
420
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000421}
422
Reid Spencer5f016e22007-07-11 17:01:13 +0000423/// ParseCXXCasts - This handles the various ways to cast expressions to another
424/// type.
425///
426/// postfix-expression: [C++ 5.2p1]
427/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
428/// 'static_cast' '<' type-name '>' '(' expression ')'
429/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
430/// 'const_cast' '<' type-name '>' '(' expression ')'
431///
John McCall60d7b3a2010-08-24 06:29:42 +0000432ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 tok::TokenKind Kind = Tok.getKind();
434 const char *CastName = 0; // For error messages
435
436 switch (Kind) {
437 default: assert(0 && "Unknown C++ cast!"); abort();
438 case tok::kw_const_cast: CastName = "const_cast"; break;
439 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
440 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
441 case tok::kw_static_cast: CastName = "static_cast"; break;
442 }
443
444 SourceLocation OpLoc = ConsumeToken();
445 SourceLocation LAngleBracketLoc = Tok.getLocation();
446
447 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000448 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000449
Douglas Gregor809070a2009-02-18 17:45:20 +0000450 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 SourceLocation RAngleBracketLoc = Tok.getLocation();
452
Chris Lattner1ab3b962008-11-18 07:48:38 +0000453 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000454 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000455
456 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
457
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000458 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
459 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000460
John McCall60d7b3a2010-08-24 06:29:42 +0000461 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000463 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000464 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000465
Douglas Gregor809070a2009-02-18 17:45:20 +0000466 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000467 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000468 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000469 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000470 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000471
Sebastian Redl20df9b72008-12-11 22:51:44 +0000472 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000473}
474
Sebastian Redlc42e1182008-11-11 11:37:55 +0000475/// ParseCXXTypeid - This handles the C++ typeid expression.
476///
477/// postfix-expression: [C++ 5.2p1]
478/// 'typeid' '(' expression ')'
479/// 'typeid' '(' type-id ')'
480///
John McCall60d7b3a2010-08-24 06:29:42 +0000481ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000482 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
483
484 SourceLocation OpLoc = ConsumeToken();
485 SourceLocation LParenLoc = Tok.getLocation();
486 SourceLocation RParenLoc;
487
488 // typeid expressions are always parenthesized.
489 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
490 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000491 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000492
John McCall60d7b3a2010-08-24 06:29:42 +0000493 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000494
495 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000496 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000497
498 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000499 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000500
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000501 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000502 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000503
504 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000505 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000506 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000507 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000508 // When typeid is applied to an expression other than an lvalue of a
509 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000510 // operand (Clause 5).
511 //
Mike Stump1eb44332009-09-09 15:08:12 +0000512 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000513 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000514 // we the expression is potentially potentially evaluated.
515 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000516 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000517 Result = ParseExpression();
518
519 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000520 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000521 SkipUntil(tok::r_paren);
522 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000523 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
524 if (RParenLoc.isInvalid())
525 return ExprError();
526
Sebastian Redlc42e1182008-11-11 11:37:55 +0000527 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000528 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000529 }
530 }
531
Sebastian Redl20df9b72008-12-11 22:51:44 +0000532 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000533}
534
Francois Pichet01b7c302010-09-08 12:20:18 +0000535/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
536///
537/// '__uuidof' '(' expression ')'
538/// '__uuidof' '(' type-id ')'
539///
540ExprResult Parser::ParseCXXUuidof() {
541 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
542
543 SourceLocation OpLoc = ConsumeToken();
544 SourceLocation LParenLoc = Tok.getLocation();
545 SourceLocation RParenLoc;
546
547 // __uuidof expressions are always parenthesized.
548 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
549 "__uuidof"))
550 return ExprError();
551
552 ExprResult Result;
553
554 if (isTypeIdInParens()) {
555 TypeResult Ty = ParseTypeName();
556
557 // Match the ')'.
558 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
559
560 if (Ty.isInvalid())
561 return ExprError();
562
563 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
564 Ty.get().getAsOpaquePtr(), RParenLoc);
565 } else {
566 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
567 Result = ParseExpression();
568
569 // Match the ')'.
570 if (Result.isInvalid())
571 SkipUntil(tok::r_paren);
572 else {
573 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
574
575 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
576 Result.release(), RParenLoc);
577 }
578 }
579
580 return move(Result);
581}
582
Douglas Gregord4dca082010-02-24 18:44:31 +0000583/// \brief Parse a C++ pseudo-destructor expression after the base,
584/// . or -> operator, and nested-name-specifier have already been
585/// parsed.
586///
587/// postfix-expression: [C++ 5.2]
588/// postfix-expression . pseudo-destructor-name
589/// postfix-expression -> pseudo-destructor-name
590///
591/// pseudo-destructor-name:
592/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
593/// ::[opt] nested-name-specifier template simple-template-id ::
594/// ~type-name
595/// ::[opt] nested-name-specifier[opt] ~type-name
596///
John McCall60d7b3a2010-08-24 06:29:42 +0000597ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000598Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
599 tok::TokenKind OpKind,
600 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000601 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000602 // We're parsing either a pseudo-destructor-name or a dependent
603 // member access that has the same form as a
604 // pseudo-destructor-name. We parse both in the same way and let
605 // the action model sort them out.
606 //
607 // Note that the ::[opt] nested-name-specifier[opt] has already
608 // been parsed, and if there was a simple-template-id, it has
609 // been coalesced into a template-id annotation token.
610 UnqualifiedId FirstTypeName;
611 SourceLocation CCLoc;
612 if (Tok.is(tok::identifier)) {
613 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
614 ConsumeToken();
615 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
616 CCLoc = ConsumeToken();
617 } else if (Tok.is(tok::annot_template_id)) {
618 FirstTypeName.setTemplateId(
619 (TemplateIdAnnotation *)Tok.getAnnotationValue());
620 ConsumeToken();
621 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
622 CCLoc = ConsumeToken();
623 } else {
624 FirstTypeName.setIdentifier(0, SourceLocation());
625 }
626
627 // Parse the tilde.
628 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
629 SourceLocation TildeLoc = ConsumeToken();
630 if (!Tok.is(tok::identifier)) {
631 Diag(Tok, diag::err_destructor_tilde_identifier);
632 return ExprError();
633 }
634
635 // Parse the second type.
636 UnqualifiedId SecondTypeName;
637 IdentifierInfo *Name = Tok.getIdentifierInfo();
638 SourceLocation NameLoc = ConsumeToken();
639 SecondTypeName.setIdentifier(Name, NameLoc);
640
641 // If there is a '<', the second type name is a template-id. Parse
642 // it as such.
643 if (Tok.is(tok::less) &&
644 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000645 SecondTypeName, /*AssumeTemplateName=*/true,
646 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000647 return ExprError();
648
John McCall9ae2f072010-08-23 23:25:46 +0000649 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
650 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000651 SS, FirstTypeName, CCLoc,
652 TildeLoc, SecondTypeName,
653 Tok.is(tok::l_paren));
654}
655
Reid Spencer5f016e22007-07-11 17:01:13 +0000656/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
657///
658/// boolean-literal: [C++ 2.13.5]
659/// 'true'
660/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000661ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000663 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000664}
Chris Lattner50dd2892008-02-26 00:51:44 +0000665
666/// ParseThrowExpression - This handles the C++ throw expression.
667///
668/// throw-expression: [C++ 15]
669/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000670ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000671 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000672 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000673
Chris Lattner2a2819a2008-04-06 06:02:23 +0000674 // If the current token isn't the start of an assignment-expression,
675 // then the expression is not present. This handles things like:
676 // "C ? throw : (void)42", which is crazy but legal.
677 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
678 case tok::semi:
679 case tok::r_paren:
680 case tok::r_square:
681 case tok::r_brace:
682 case tok::colon:
683 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000684 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000685
Chris Lattner2a2819a2008-04-06 06:02:23 +0000686 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000687 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000688 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000689 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000690 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000691}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000692
693/// ParseCXXThis - This handles the C++ 'this' pointer.
694///
695/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
696/// a non-lvalue expression whose value is the address of the object for which
697/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000698ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000699 assert(Tok.is(tok::kw_this) && "Not 'this'!");
700 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000701 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000702}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000703
704/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
705/// Can be interpreted either as function-style casting ("int(x)")
706/// or class type construction ("ClassType(x,y,z)")
707/// or creation of a value-initialized type ("int()").
708///
709/// postfix-expression: [C++ 5.2p1]
710/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
711/// typename-specifier '(' expression-list[opt] ')' [TODO]
712///
John McCall60d7b3a2010-08-24 06:29:42 +0000713ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000714Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000715 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000716 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000717
718 assert(Tok.is(tok::l_paren) && "Expected '('!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000719 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
720
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000721 SourceLocation LParenLoc = ConsumeParen();
722
Sebastian Redla55e52c2008-11-25 22:21:31 +0000723 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000724 CommaLocsTy CommaLocs;
725
726 if (Tok.isNot(tok::r_paren)) {
727 if (ParseExpressionList(Exprs, CommaLocs)) {
728 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000729 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000730 }
731 }
732
733 // Match the ')'.
734 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
735
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000736 // TypeRep could be null, if it references an invalid typedef.
737 if (!TypeRep)
738 return ExprError();
739
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000740 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
741 "Unexpected number of commas!");
Douglas Gregorab6677e2010-09-08 00:15:04 +0000742 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000743 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000744}
745
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000746/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000747///
748/// condition:
749/// expression
750/// type-specifier-seq declarator '=' assignment-expression
751/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
752/// '=' assignment-expression
753///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000754/// \param ExprResult if the condition was parsed as an expression, the
755/// parsed expression.
756///
757/// \param DeclResult if the condition was parsed as a declaration, the
758/// parsed declaration.
759///
Douglas Gregor586596f2010-05-06 17:25:47 +0000760/// \param Loc The location of the start of the statement that requires this
761/// condition, e.g., the "for" in a for loop.
762///
763/// \param ConvertToBoolean Whether the condition expression should be
764/// converted to a boolean value.
765///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000766/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000767bool Parser::ParseCXXCondition(ExprResult &ExprOut,
768 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000769 SourceLocation Loc,
770 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000771 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000772 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000773 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000774 }
775
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000776 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000777 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000778 ExprOut = ParseExpression(); // expression
779 DeclOut = 0;
780 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000781 return true;
782
783 // If required, convert to a boolean value.
784 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000785 ExprOut
786 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
787 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000788 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000789
790 // type-specifier-seq
791 DeclSpec DS;
792 ParseSpecifierQualifierList(DS);
793
794 // declarator
795 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
796 ParseDeclarator(DeclaratorInfo);
797
798 // simple-asm-expr[opt]
799 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000800 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000801 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000802 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000803 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000804 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000805 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000806 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000807 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000808 }
809
810 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000811 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000812
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000813 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000814 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000815 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000816 DeclOut = Dcl.get();
817 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000818
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000819 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000820 if (isTokenEqualOrMistypedEqualEqual(
821 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000822 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000823 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000824 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000825 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
826 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000827 } else {
828 // FIXME: C++0x allows a braced-init-list
829 Diag(Tok, diag::err_expected_equal_after_declarator);
830 }
831
Douglas Gregor586596f2010-05-06 17:25:47 +0000832 // FIXME: Build a reference to this declaration? Convert it to bool?
833 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +0000834
835 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +0000836
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000837 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000838}
839
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000840/// \brief Determine whether the current token starts a C++
841/// simple-type-specifier.
842bool Parser::isCXXSimpleTypeSpecifier() const {
843 switch (Tok.getKind()) {
844 case tok::annot_typename:
845 case tok::kw_short:
846 case tok::kw_long:
847 case tok::kw_signed:
848 case tok::kw_unsigned:
849 case tok::kw_void:
850 case tok::kw_char:
851 case tok::kw_int:
852 case tok::kw_float:
853 case tok::kw_double:
854 case tok::kw_wchar_t:
855 case tok::kw_char16_t:
856 case tok::kw_char32_t:
857 case tok::kw_bool:
858 // FIXME: C++0x decltype support.
859 // GNU typeof support.
860 case tok::kw_typeof:
861 return true;
862
863 default:
864 break;
865 }
866
867 return false;
868}
869
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000870/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
871/// This should only be called when the current token is known to be part of
872/// simple-type-specifier.
873///
874/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000875/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000876/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
877/// char
878/// wchar_t
879/// bool
880/// short
881/// int
882/// long
883/// signed
884/// unsigned
885/// float
886/// double
887/// void
888/// [GNU] typeof-specifier
889/// [C++0x] auto [TODO]
890///
891/// type-name:
892/// class-name
893/// enum-name
894/// typedef-name
895///
896void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
897 DS.SetRangeStart(Tok.getLocation());
898 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000899 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000900 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000902 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000903 case tok::identifier: // foo::bar
904 case tok::coloncolon: // ::foo::bar
905 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000906 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000907 assert(0 && "Not a simple-type-specifier token!");
908 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000909
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000910 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000911 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000912 if (getTypeAnnotation(Tok))
913 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
914 getTypeAnnotation(Tok));
915 else
916 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000917
918 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
919 ConsumeToken();
920
921 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
922 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
923 // Objective-C interface. If we don't have Objective-C or a '<', this is
924 // just a normal reference to a typedef name.
925 if (Tok.is(tok::less) && getLang().ObjC1)
926 ParseObjCProtocolQualifiers(DS);
927
928 DS.Finish(Diags, PP);
929 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000932 // builtin types
933 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000934 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000935 break;
936 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000937 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000938 break;
939 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000940 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000941 break;
942 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000943 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000944 break;
945 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000946 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000947 break;
948 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000949 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000950 break;
951 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000952 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000953 break;
954 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000955 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000956 break;
957 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000958 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000959 break;
960 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000961 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000962 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000963 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000964 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000965 break;
966 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000967 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000968 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000969 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000970 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000971 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000973 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000974 // GNU typeof support.
975 case tok::kw_typeof:
976 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000977 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000978 return;
979 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000980 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000981 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
982 else
983 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000984 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000985 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000986}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000987
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000988/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
989/// [dcl.name]), which is a non-empty sequence of type-specifiers,
990/// e.g., "const short int". Note that the DeclSpec is *not* finished
991/// by parsing the type-specifier-seq, because these sequences are
992/// typically followed by some form of declarator. Returns true and
993/// emits diagnostics if this is not a type-specifier-seq, false
994/// otherwise.
995///
996/// type-specifier-seq: [C++ 8.1]
997/// type-specifier type-specifier-seq[opt]
998///
999bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1000 DS.SetRangeStart(Tok.getLocation());
1001 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001002 unsigned DiagID;
1003 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001004
1005 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001006 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1007 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001008 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001009 return true;
1010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Sebastian Redld9bafa72010-02-03 21:21:43 +00001012 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1013 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1014 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001015
Douglas Gregor396a9f22010-02-24 23:13:13 +00001016 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001017 return false;
1018}
1019
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001020/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1021/// some form.
1022///
1023/// This routine is invoked when a '<' is encountered after an identifier or
1024/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1025/// whether the unqualified-id is actually a template-id. This routine will
1026/// then parse the template arguments and form the appropriate template-id to
1027/// return to the caller.
1028///
1029/// \param SS the nested-name-specifier that precedes this template-id, if
1030/// we're actually parsing a qualified-id.
1031///
1032/// \param Name for constructor and destructor names, this is the actual
1033/// identifier that may be a template-name.
1034///
1035/// \param NameLoc the location of the class-name in a constructor or
1036/// destructor.
1037///
1038/// \param EnteringContext whether we're entering the scope of the
1039/// nested-name-specifier.
1040///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001041/// \param ObjectType if this unqualified-id occurs within a member access
1042/// expression, the type of the base object whose member is being accessed.
1043///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001044/// \param Id as input, describes the template-name or operator-function-id
1045/// that precedes the '<'. If template arguments were parsed successfully,
1046/// will be updated with the template-id.
1047///
Douglas Gregord4dca082010-02-24 18:44:31 +00001048/// \param AssumeTemplateId When true, this routine will assume that the name
1049/// refers to a template without performing name lookup to verify.
1050///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001051/// \returns true if a parse error occurred, false otherwise.
1052bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1053 IdentifierInfo *Name,
1054 SourceLocation NameLoc,
1055 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001056 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001057 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001058 bool AssumeTemplateId,
1059 SourceLocation TemplateKWLoc) {
1060 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1061 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001062
1063 TemplateTy Template;
1064 TemplateNameKind TNK = TNK_Non_template;
1065 switch (Id.getKind()) {
1066 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001067 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001068 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001069 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001070 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001071 Id, ObjectType, EnteringContext,
1072 Template);
1073 if (TNK == TNK_Non_template)
1074 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001075 } else {
1076 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001077 TNK = Actions.isTemplateName(getCurScope(), SS,
1078 TemplateKWLoc.isValid(), Id,
1079 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001080 MemberOfUnknownSpecialization);
1081
1082 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1083 ObjectType && IsTemplateArgumentList()) {
1084 // We have something like t->getAs<T>(), where getAs is a
1085 // member of an unknown specialization. However, this will only
1086 // parse correctly as a template, so suggest the keyword 'template'
1087 // before 'getAs' and treat this as a dependent template name.
1088 std::string Name;
1089 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1090 Name = Id.Identifier->getName();
1091 else {
1092 Name = "operator ";
1093 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1094 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1095 else
1096 Name += Id.Identifier->getName();
1097 }
1098 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1099 << Name
1100 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001101 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001102 SS, Id, ObjectType,
1103 EnteringContext, Template);
1104 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001105 return true;
1106 }
1107 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001108 break;
1109
Douglas Gregor014e88d2009-11-03 23:16:33 +00001110 case UnqualifiedId::IK_ConstructorName: {
1111 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001112 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001113 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001114 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1115 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001116 EnteringContext, Template,
1117 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001118 break;
1119 }
1120
Douglas Gregor014e88d2009-11-03 23:16:33 +00001121 case UnqualifiedId::IK_DestructorName: {
1122 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001123 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001124 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001125 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001126 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001127 TemplateName, ObjectType,
1128 EnteringContext, Template);
1129 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001130 return true;
1131 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001132 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1133 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001134 EnteringContext, Template,
1135 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001136
John McCallb3d87482010-08-24 05:47:05 +00001137 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001138 Diag(NameLoc, diag::err_destructor_template_id)
1139 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001140 return true;
1141 }
1142 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001143 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001144 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001145
1146 default:
1147 return false;
1148 }
1149
1150 if (TNK == TNK_Non_template)
1151 return false;
1152
1153 // Parse the enclosed template argument list.
1154 SourceLocation LAngleLoc, RAngleLoc;
1155 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001156 if (Tok.is(tok::less) &&
1157 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001158 &SS, true, LAngleLoc,
1159 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001160 RAngleLoc))
1161 return true;
1162
1163 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001164 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1165 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001166 // Form a parsed representation of the template-id to be stored in the
1167 // UnqualifiedId.
1168 TemplateIdAnnotation *TemplateId
1169 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1170
1171 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1172 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001173 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001174 TemplateId->TemplateNameLoc = Id.StartLocation;
1175 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001176 TemplateId->Name = 0;
1177 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1178 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001179 }
1180
John McCall2b5289b2010-08-23 07:28:44 +00001181 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001182 TemplateId->Kind = TNK;
1183 TemplateId->LAngleLoc = LAngleLoc;
1184 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001185 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001186 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001187 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001188 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001189
1190 Id.setTemplateId(TemplateId);
1191 return false;
1192 }
1193
1194 // Bundle the template arguments together.
1195 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001196 TemplateArgs.size());
1197
1198 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001199 TypeResult Type
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001200 = Actions.ActOnTemplateIdType(Template, NameLoc,
1201 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001202 RAngleLoc);
1203 if (Type.isInvalid())
1204 return true;
1205
1206 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1207 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1208 else
1209 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1210
1211 return false;
1212}
1213
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001214/// \brief Parse an operator-function-id or conversion-function-id as part
1215/// of a C++ unqualified-id.
1216///
1217/// This routine is responsible only for parsing the operator-function-id or
1218/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001219///
1220/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001221/// operator-function-id: [C++ 13.5]
1222/// 'operator' operator
1223///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001224/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001225/// new delete new[] delete[]
1226/// + - * / % ^ & | ~
1227/// ! = < > += -= *= /= %=
1228/// ^= &= |= << >> >>= <<= == !=
1229/// <= >= && || ++ -- , ->* ->
1230/// () []
1231///
1232/// conversion-function-id: [C++ 12.3.2]
1233/// operator conversion-type-id
1234///
1235/// conversion-type-id:
1236/// type-specifier-seq conversion-declarator[opt]
1237///
1238/// conversion-declarator:
1239/// ptr-operator conversion-declarator[opt]
1240/// \endcode
1241///
1242/// \param The nested-name-specifier that preceded this unqualified-id. If
1243/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1244///
1245/// \param EnteringContext whether we are entering the scope of the
1246/// nested-name-specifier.
1247///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001248/// \param ObjectType if this unqualified-id occurs within a member access
1249/// expression, the type of the base object whose member is being accessed.
1250///
1251/// \param Result on a successful parse, contains the parsed unqualified-id.
1252///
1253/// \returns true if parsing fails, false otherwise.
1254bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001255 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001256 UnqualifiedId &Result) {
1257 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1258
1259 // Consume the 'operator' keyword.
1260 SourceLocation KeywordLoc = ConsumeToken();
1261
1262 // Determine what kind of operator name we have.
1263 unsigned SymbolIdx = 0;
1264 SourceLocation SymbolLocations[3];
1265 OverloadedOperatorKind Op = OO_None;
1266 switch (Tok.getKind()) {
1267 case tok::kw_new:
1268 case tok::kw_delete: {
1269 bool isNew = Tok.getKind() == tok::kw_new;
1270 // Consume the 'new' or 'delete'.
1271 SymbolLocations[SymbolIdx++] = ConsumeToken();
1272 if (Tok.is(tok::l_square)) {
1273 // Consume the '['.
1274 SourceLocation LBracketLoc = ConsumeBracket();
1275 // Consume the ']'.
1276 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1277 LBracketLoc);
1278 if (RBracketLoc.isInvalid())
1279 return true;
1280
1281 SymbolLocations[SymbolIdx++] = LBracketLoc;
1282 SymbolLocations[SymbolIdx++] = RBracketLoc;
1283 Op = isNew? OO_Array_New : OO_Array_Delete;
1284 } else {
1285 Op = isNew? OO_New : OO_Delete;
1286 }
1287 break;
1288 }
1289
1290#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1291 case tok::Token: \
1292 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1293 Op = OO_##Name; \
1294 break;
1295#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1296#include "clang/Basic/OperatorKinds.def"
1297
1298 case tok::l_paren: {
1299 // Consume the '('.
1300 SourceLocation LParenLoc = ConsumeParen();
1301 // Consume the ')'.
1302 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1303 LParenLoc);
1304 if (RParenLoc.isInvalid())
1305 return true;
1306
1307 SymbolLocations[SymbolIdx++] = LParenLoc;
1308 SymbolLocations[SymbolIdx++] = RParenLoc;
1309 Op = OO_Call;
1310 break;
1311 }
1312
1313 case tok::l_square: {
1314 // Consume the '['.
1315 SourceLocation LBracketLoc = ConsumeBracket();
1316 // Consume the ']'.
1317 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1318 LBracketLoc);
1319 if (RBracketLoc.isInvalid())
1320 return true;
1321
1322 SymbolLocations[SymbolIdx++] = LBracketLoc;
1323 SymbolLocations[SymbolIdx++] = RBracketLoc;
1324 Op = OO_Subscript;
1325 break;
1326 }
1327
1328 case tok::code_completion: {
1329 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001330 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001331
1332 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001333 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001334
1335 // Don't try to parse any further.
1336 return true;
1337 }
1338
1339 default:
1340 break;
1341 }
1342
1343 if (Op != OO_None) {
1344 // We have parsed an operator-function-id.
1345 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1346 return false;
1347 }
Sean Hunt0486d742009-11-28 04:44:28 +00001348
1349 // Parse a literal-operator-id.
1350 //
1351 // literal-operator-id: [C++0x 13.5.8]
1352 // operator "" identifier
1353
1354 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1355 if (Tok.getLength() != 2)
1356 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1357 ConsumeStringToken();
1358
1359 if (Tok.isNot(tok::identifier)) {
1360 Diag(Tok.getLocation(), diag::err_expected_ident);
1361 return true;
1362 }
1363
1364 IdentifierInfo *II = Tok.getIdentifierInfo();
1365 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001366 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001367 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001368
1369 // Parse a conversion-function-id.
1370 //
1371 // conversion-function-id: [C++ 12.3.2]
1372 // operator conversion-type-id
1373 //
1374 // conversion-type-id:
1375 // type-specifier-seq conversion-declarator[opt]
1376 //
1377 // conversion-declarator:
1378 // ptr-operator conversion-declarator[opt]
1379
1380 // Parse the type-specifier-seq.
1381 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001382 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001383 return true;
1384
1385 // Parse the conversion-declarator, which is merely a sequence of
1386 // ptr-operators.
1387 Declarator D(DS, Declarator::TypeNameContext);
1388 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1389
1390 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001391 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001392 if (Ty.isInvalid())
1393 return true;
1394
1395 // Note that this is a conversion-function-id.
1396 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1397 D.getSourceRange().getEnd());
1398 return false;
1399}
1400
1401/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1402/// name of an entity.
1403///
1404/// \code
1405/// unqualified-id: [C++ expr.prim.general]
1406/// identifier
1407/// operator-function-id
1408/// conversion-function-id
1409/// [C++0x] literal-operator-id [TODO]
1410/// ~ class-name
1411/// template-id
1412///
1413/// \endcode
1414///
1415/// \param The nested-name-specifier that preceded this unqualified-id. If
1416/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1417///
1418/// \param EnteringContext whether we are entering the scope of the
1419/// nested-name-specifier.
1420///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001421/// \param AllowDestructorName whether we allow parsing of a destructor name.
1422///
1423/// \param AllowConstructorName whether we allow parsing a constructor name.
1424///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001425/// \param ObjectType if this unqualified-id occurs within a member access
1426/// expression, the type of the base object whose member is being accessed.
1427///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001428/// \param Result on a successful parse, contains the parsed unqualified-id.
1429///
1430/// \returns true if parsing fails, false otherwise.
1431bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1432 bool AllowDestructorName,
1433 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001434 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001435 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001436
1437 // Handle 'A::template B'. This is for template-ids which have not
1438 // already been annotated by ParseOptionalCXXScopeSpecifier().
1439 bool TemplateSpecified = false;
1440 SourceLocation TemplateKWLoc;
1441 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1442 (ObjectType || SS.isSet())) {
1443 TemplateSpecified = true;
1444 TemplateKWLoc = ConsumeToken();
1445 }
1446
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001447 // unqualified-id:
1448 // identifier
1449 // template-id (when it hasn't already been annotated)
1450 if (Tok.is(tok::identifier)) {
1451 // Consume the identifier.
1452 IdentifierInfo *Id = Tok.getIdentifierInfo();
1453 SourceLocation IdLoc = ConsumeToken();
1454
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001455 if (!getLang().CPlusPlus) {
1456 // If we're not in C++, only identifiers matter. Record the
1457 // identifier and return.
1458 Result.setIdentifier(Id, IdLoc);
1459 return false;
1460 }
1461
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001462 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001463 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001464 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001465 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001466 &SS, false),
1467 IdLoc, IdLoc);
1468 } else {
1469 // We have parsed an identifier.
1470 Result.setIdentifier(Id, IdLoc);
1471 }
1472
1473 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001474 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001475 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001476 ObjectType, Result,
1477 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001478
1479 return false;
1480 }
1481
1482 // unqualified-id:
1483 // template-id (already parsed and annotated)
1484 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001485 TemplateIdAnnotation *TemplateId
1486 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1487
1488 // If the template-name names the current class, then this is a constructor
1489 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001490 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001491 if (SS.isSet()) {
1492 // C++ [class.qual]p2 specifies that a qualified template-name
1493 // is taken as the constructor name where a constructor can be
1494 // declared. Thus, the template arguments are extraneous, so
1495 // complain about them and remove them entirely.
1496 Diag(TemplateId->TemplateNameLoc,
1497 diag::err_out_of_line_constructor_template_id)
1498 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001499 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001500 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1501 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1502 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001503 getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001504 &SS, false),
1505 TemplateId->TemplateNameLoc,
1506 TemplateId->RAngleLoc);
1507 TemplateId->Destroy();
1508 ConsumeToken();
1509 return false;
1510 }
1511
1512 Result.setConstructorTemplateId(TemplateId);
1513 ConsumeToken();
1514 return false;
1515 }
1516
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001517 // We have already parsed a template-id; consume the annotation token as
1518 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001519 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001520 ConsumeToken();
1521 return false;
1522 }
1523
1524 // unqualified-id:
1525 // operator-function-id
1526 // conversion-function-id
1527 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001528 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001529 return true;
1530
Sean Hunte6252d12009-11-28 08:58:14 +00001531 // If we have an operator-function-id or a literal-operator-id and the next
1532 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001533 //
1534 // template-id:
1535 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001536 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1537 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001538 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001539 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1540 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001541 Result,
1542 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001543
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001544 return false;
1545 }
1546
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001547 if (getLang().CPlusPlus &&
1548 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001549 // C++ [expr.unary.op]p10:
1550 // There is an ambiguity in the unary-expression ~X(), where X is a
1551 // class-name. The ambiguity is resolved in favor of treating ~ as a
1552 // unary complement rather than treating ~X as referring to a destructor.
1553
1554 // Parse the '~'.
1555 SourceLocation TildeLoc = ConsumeToken();
1556
1557 // Parse the class-name.
1558 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001559 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001560 return true;
1561 }
1562
1563 // Parse the class-name (or template-name in a simple-template-id).
1564 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1565 SourceLocation ClassNameLoc = ConsumeToken();
1566
Douglas Gregor0278e122010-05-05 05:58:24 +00001567 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001568 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001569 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001570 EnteringContext, ObjectType, Result,
1571 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001572 }
1573
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001574 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001575 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1576 ClassNameLoc, getCurScope(),
1577 SS, ObjectType,
1578 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001579 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001580 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001581
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001582 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001583 return false;
1584 }
1585
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001586 Diag(Tok, diag::err_expected_unqualified_id)
1587 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001588 return true;
1589}
1590
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001591/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1592/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001593///
Chris Lattner59232d32009-01-04 21:25:24 +00001594/// This method is called to parse the new expression after the optional :: has
1595/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1596/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001597///
1598/// new-expression:
1599/// '::'[opt] 'new' new-placement[opt] new-type-id
1600/// new-initializer[opt]
1601/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1602/// new-initializer[opt]
1603///
1604/// new-placement:
1605/// '(' expression-list ')'
1606///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001607/// new-type-id:
1608/// type-specifier-seq new-declarator[opt]
1609///
1610/// new-declarator:
1611/// ptr-operator new-declarator[opt]
1612/// direct-new-declarator
1613///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001614/// new-initializer:
1615/// '(' expression-list[opt] ')'
1616/// [C++0x] braced-init-list [TODO]
1617///
John McCall60d7b3a2010-08-24 06:29:42 +00001618ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001619Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1620 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1621 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001622
1623 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1624 // second form of new-expression. It can't be a new-type-id.
1625
Sebastian Redla55e52c2008-11-25 22:21:31 +00001626 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001627 SourceLocation PlacementLParen, PlacementRParen;
1628
Douglas Gregor4bd40312010-07-13 15:54:32 +00001629 SourceRange TypeIdParens;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001630 DeclSpec DS;
1631 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001632 if (Tok.is(tok::l_paren)) {
1633 // If it turns out to be a placement, we change the type location.
1634 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001635 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1636 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001637 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001638 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001639
1640 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001641 if (PlacementRParen.isInvalid()) {
1642 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001643 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001644 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001645
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001646 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001647 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001648 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001649 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001650 } else {
1651 // We still need the type.
1652 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001653 TypeIdParens.setBegin(ConsumeParen());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001654 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001655 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001656 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001657 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1658 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001659 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001660 if (ParseCXXTypeSpecifierSeq(DS))
1661 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001662 else {
1663 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001664 ParseDeclaratorInternal(DeclaratorInfo,
1665 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001666 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001667 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001668 }
1669 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001670 // A new-type-id is a simplified type-id, where essentially the
1671 // direct-declarator is replaced by a direct-new-declarator.
1672 if (ParseCXXTypeSpecifierSeq(DS))
1673 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001674 else {
1675 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001676 ParseDeclaratorInternal(DeclaratorInfo,
1677 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001678 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001679 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001680 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001681 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001682 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001683 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001684
Sebastian Redla55e52c2008-11-25 22:21:31 +00001685 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001686 SourceLocation ConstructorLParen, ConstructorRParen;
1687
1688 if (Tok.is(tok::l_paren)) {
1689 ConstructorLParen = ConsumeParen();
1690 if (Tok.isNot(tok::r_paren)) {
1691 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001692 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1693 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001694 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001695 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001696 }
1697 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001698 if (ConstructorRParen.isInvalid()) {
1699 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001700 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001701 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001702 }
1703
Sebastian Redlf53597f2009-03-15 17:47:39 +00001704 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1705 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001706 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001707 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001708}
1709
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001710/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1711/// passed to ParseDeclaratorInternal.
1712///
1713/// direct-new-declarator:
1714/// '[' expression ']'
1715/// direct-new-declarator '[' constant-expression ']'
1716///
Chris Lattner59232d32009-01-04 21:25:24 +00001717void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001718 // Parse the array dimensions.
1719 bool first = true;
1720 while (Tok.is(tok::l_square)) {
1721 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001722 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001723 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001724 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001725 // Recover
1726 SkipUntil(tok::r_square);
1727 return;
1728 }
1729 first = false;
1730
Sebastian Redlab197ba2009-02-09 18:23:29 +00001731 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall7f040a92010-12-24 02:08:15 +00001732 D.AddTypeInfo(DeclaratorChunk::getArray(0, ParsedAttributes(),
1733 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001734 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001735 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001736
Sebastian Redlab197ba2009-02-09 18:23:29 +00001737 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001738 return;
1739 }
1740}
1741
1742/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1743/// This ambiguity appears in the syntax of the C++ new operator.
1744///
1745/// new-expression:
1746/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1747/// new-initializer[opt]
1748///
1749/// new-placement:
1750/// '(' expression-list ')'
1751///
John McCallca0408f2010-08-23 06:44:23 +00001752bool Parser::ParseExpressionListOrTypeId(
1753 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001754 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001755 // The '(' was already consumed.
1756 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001757 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001758 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001759 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001760 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001761 }
1762
1763 // It's not a type, it has to be an expression list.
1764 // Discard the comma locations - ActOnCXXNew has enough parameters.
1765 CommaLocsTy CommaLocs;
1766 return ParseExpressionList(PlacementArgs, CommaLocs);
1767}
1768
1769/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1770/// to free memory allocated by new.
1771///
Chris Lattner59232d32009-01-04 21:25:24 +00001772/// This method is called to parse the 'delete' expression after the optional
1773/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1774/// and "Start" is its location. Otherwise, "Start" is the location of the
1775/// 'delete' token.
1776///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001777/// delete-expression:
1778/// '::'[opt] 'delete' cast-expression
1779/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001780ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001781Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1782 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1783 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001784
1785 // Array delete?
1786 bool ArrayDelete = false;
1787 if (Tok.is(tok::l_square)) {
1788 ArrayDelete = true;
1789 SourceLocation LHS = ConsumeBracket();
1790 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1791 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001792 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001793 }
1794
John McCall60d7b3a2010-08-24 06:29:42 +00001795 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001796 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001797 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001798
John McCall9ae2f072010-08-23 23:25:46 +00001799 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001800}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001801
Mike Stump1eb44332009-09-09 15:08:12 +00001802static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001803 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001804 default: llvm_unreachable("Not a known unary type trait");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001805 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1806 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1807 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1808 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1809 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1810 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1811 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1812 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1813 case tok::kw___is_abstract: return UTT_IsAbstract;
1814 case tok::kw___is_class: return UTT_IsClass;
1815 case tok::kw___is_empty: return UTT_IsEmpty;
1816 case tok::kw___is_enum: return UTT_IsEnum;
1817 case tok::kw___is_pod: return UTT_IsPOD;
1818 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1819 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001820 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001821 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001822}
1823
1824static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1825 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001826 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001827 case tok::kw___is_base_of: return BTT_IsBaseOf;
1828 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001829 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001830 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001831}
1832
1833/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1834/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1835/// templates.
1836///
1837/// primary-expression:
1838/// [GNU] unary-type-trait '(' type-id ')'
1839///
John McCall60d7b3a2010-08-24 06:29:42 +00001840ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001841 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1842 SourceLocation Loc = ConsumeToken();
1843
1844 SourceLocation LParen = Tok.getLocation();
1845 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1846 return ExprError();
1847
1848 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1849 // there will be cryptic errors about mismatched parentheses and missing
1850 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001851 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001852
1853 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1854
Douglas Gregor809070a2009-02-18 17:45:20 +00001855 if (Ty.isInvalid())
1856 return ExprError();
1857
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00001858 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001859}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001860
Francois Pichet6ad6f282010-12-07 00:08:36 +00001861/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
1862/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1863/// templates.
1864///
1865/// primary-expression:
1866/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
1867///
1868ExprResult Parser::ParseBinaryTypeTrait() {
1869 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
1870 SourceLocation Loc = ConsumeToken();
1871
1872 SourceLocation LParen = Tok.getLocation();
1873 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1874 return ExprError();
1875
1876 TypeResult LhsTy = ParseTypeName();
1877 if (LhsTy.isInvalid()) {
1878 SkipUntil(tok::r_paren);
1879 return ExprError();
1880 }
1881
1882 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
1883 SkipUntil(tok::r_paren);
1884 return ExprError();
1885 }
1886
1887 TypeResult RhsTy = ParseTypeName();
1888 if (RhsTy.isInvalid()) {
1889 SkipUntil(tok::r_paren);
1890 return ExprError();
1891 }
1892
1893 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1894
1895 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
1896}
1897
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001898/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1899/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1900/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00001901ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001902Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00001903 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001904 SourceLocation LParenLoc,
1905 SourceLocation &RParenLoc) {
1906 assert(getLang().CPlusPlus && "Should only be called for C++!");
1907 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1908 assert(isTypeIdInParens() && "Not a type-id!");
1909
John McCall60d7b3a2010-08-24 06:29:42 +00001910 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00001911 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001912
1913 // We need to disambiguate a very ugly part of the C++ syntax:
1914 //
1915 // (T())x; - type-id
1916 // (T())*x; - type-id
1917 // (T())/x; - expression
1918 // (T()); - expression
1919 //
1920 // The bad news is that we cannot use the specialized tentative parser, since
1921 // it can only verify that the thing inside the parens can be parsed as
1922 // type-id, it is not useful for determining the context past the parens.
1923 //
1924 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001925 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001926 //
1927 // It uses a scheme similar to parsing inline methods. The parenthesized
1928 // tokens are cached, the context that follows is determined (possibly by
1929 // parsing a cast-expression), and then we re-introduce the cached tokens
1930 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001931
Mike Stump1eb44332009-09-09 15:08:12 +00001932 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001933 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001934
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001935 // Store the tokens of the parentheses. We will parse them after we determine
1936 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001937 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001938 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001939 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1940 return ExprError();
1941 }
1942
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001943 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001944 ParseAs = CompoundLiteral;
1945 } else {
1946 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001947 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1948 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1949 NotCastExpr = true;
1950 } else {
1951 // Try parsing the cast-expression that may follow.
1952 // If it is not a cast-expression, NotCastExpr will be true and no token
1953 // will be consumed.
1954 Result = ParseCastExpression(false/*isUnaryExpression*/,
1955 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00001956 NotCastExpr,
1957 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001958 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001959
1960 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1961 // an expression.
1962 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001963 }
1964
Mike Stump1eb44332009-09-09 15:08:12 +00001965 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001966 Toks.push_back(Tok);
1967 // Re-enter the stored parenthesized tokens into the token stream, so we may
1968 // parse them now.
1969 PP.EnterTokenStream(Toks.data(), Toks.size(),
1970 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1971 // Drop the current token and bring the first cached one. It's the same token
1972 // as when we entered this function.
1973 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001974
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001975 if (ParseAs >= CompoundLiteral) {
1976 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001977
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001978 // Match the ')'.
1979 if (Tok.is(tok::r_paren))
1980 RParenLoc = ConsumeParen();
1981 else
1982 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001983
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001984 if (ParseAs == CompoundLiteral) {
1985 ExprType = CompoundLiteral;
1986 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1987 }
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001989 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1990 assert(ParseAs == CastExpr);
1991
1992 if (Ty.isInvalid())
1993 return ExprError();
1994
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001995 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001996
1997 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001998 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001999 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002000 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002001 return move(Result);
2002 }
Mike Stump1eb44332009-09-09 15:08:12 +00002003
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002004 // Not a compound literal, and not followed by a cast-expression.
2005 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002006
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002007 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002008 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002009 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002010 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002011
2012 // Match the ')'.
2013 if (Result.isInvalid()) {
2014 SkipUntil(tok::r_paren);
2015 return ExprError();
2016 }
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002018 if (Tok.is(tok::r_paren))
2019 RParenLoc = ConsumeParen();
2020 else
2021 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2022
2023 return move(Result);
2024}