blob: d09f20e113afdaa5bd2e19550a1dcf9272a177ec [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)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000176 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000177 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
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000200 // Consume the template-id token.
201 ConsumeToken();
202
203 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
204 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000206 if (!HasScopeSpecifier)
207 HasScopeSpecifier = true;
208
209 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
210 TemplateId->getTemplateArgs(),
211 TemplateId->NumArgs);
212
213 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
214 /*FIXME:*/SourceLocation(),
215 SS,
216 TemplateId->Template,
217 TemplateId->TemplateNameLoc,
218 TemplateId->LAngleLoc,
219 TemplateArgsPtr,
220 TemplateId->RAngleLoc,
221 CCLoc,
222 EnteringContext)) {
223 SourceLocation StartLoc
224 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
225 : TemplateId->TemplateNameLoc;
226 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000227 }
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000228
229 TemplateId->Destroy();
230 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000231 }
232
Chris Lattner5c7f7862009-06-26 03:52:38 +0000233
234 // The rest of the nested-name-specifier possibilities start with
235 // tok::identifier.
236 if (Tok.isNot(tok::identifier))
237 break;
238
239 IdentifierInfo &II = *Tok.getIdentifierInfo();
240
241 // nested-name-specifier:
242 // type-name '::'
243 // namespace-name '::'
244 // nested-name-specifier identifier '::'
245 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000246
247 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
248 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000249 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000250 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
251 Tok.getLocation(),
252 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000253 EnteringContext) &&
254 // If the token after the colon isn't an identifier, it's still an
255 // error, but they probably meant something else strange so don't
256 // recover like this.
257 PP.LookAhead(1).is(tok::identifier)) {
258 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000259 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000260
261 // Recover as if the user wrote '::'.
262 Next.setKind(tok::coloncolon);
263 }
Chris Lattner46646492009-12-07 01:36:53 +0000264 }
265
Chris Lattner5c7f7862009-06-26 03:52:38 +0000266 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000267 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000268 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000269 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000270 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000271 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000272 }
273
Chris Lattner5c7f7862009-06-26 03:52:38 +0000274 // We have an identifier followed by a '::'. Lookup this name
275 // as the name in a nested-name-specifier.
276 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000277 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
278 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000279 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000281 HasScopeSpecifier = true;
282 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
283 ObjectType, EnteringContext, SS))
284 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
285
Chris Lattner5c7f7862009-06-26 03:52:38 +0000286 continue;
287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattner5c7f7862009-06-26 03:52:38 +0000289 // nested-name-specifier:
290 // type-name '<'
291 if (Next.is(tok::less)) {
292 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000293 UnqualifiedId TemplateName;
294 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000295 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000296 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000297 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000298 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000299 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000300 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000301 Template,
302 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000303 // We have found a template name, so annotate this this token
304 // with a template-id annotation. We do not permit the
305 // template-id to be translated into a type annotation,
306 // because some clients (e.g., the parsing of class template
307 // specializations) still want to see the original template-id
308 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000309 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000310 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000311 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000312 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000313 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000314 }
315
316 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
317 IsTemplateArgumentList(1)) {
318 // We have something like t::getAs<T>, where getAs is a
319 // member of an unknown specialization. However, this will only
320 // parse correctly as a template, so suggest the keyword 'template'
321 // before 'getAs' and treat this as a dependent template name.
322 Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
323 << II.getName()
324 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
325
Douglas Gregord6ab2322010-06-16 23:00:59 +0000326 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000327 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000328 Tok.getLocation(), SS,
329 TemplateName, ObjectType,
330 EnteringContext, Template)) {
331 // Consume the identifier.
332 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000333 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000334 SourceLocation(), false))
335 return true;
336 }
337 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000338 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000339
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000340 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000341 }
342 }
343
Douglas Gregor39a8de12009-02-25 19:37:18 +0000344 // We don't have any tokens that form the beginning of a
345 // nested-name-specifier, so we're done.
346 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Douglas Gregord4dca082010-02-24 18:44:31 +0000349 // Even if we didn't see any pieces of a nested-name-specifier, we
350 // still check whether there is a tilde in this position, which
351 // indicates a potential pseudo-destructor.
352 if (CheckForDestructor && Tok.is(tok::tilde))
353 *MayBePseudoDestructor = true;
354
John McCall9ba61662010-02-26 08:45:28 +0000355 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000356}
357
358/// ParseCXXIdExpression - Handle id-expression.
359///
360/// id-expression:
361/// unqualified-id
362/// qualified-id
363///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000364/// qualified-id:
365/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
366/// '::' identifier
367/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000368/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000369///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000370/// NOTE: The standard specifies that, for qualified-id, the parser does not
371/// expect:
372///
373/// '::' conversion-function-id
374/// '::' '~' class-name
375///
376/// This may cause a slight inconsistency on diagnostics:
377///
378/// class C {};
379/// namespace A {}
380/// void f() {
381/// :: A :: ~ C(); // Some Sema error about using destructor with a
382/// // namespace.
383/// :: ~ C(); // Some Parser error like 'unexpected ~'.
384/// }
385///
386/// We simplify the parser a bit and make it work like:
387///
388/// qualified-id:
389/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
390/// '::' unqualified-id
391///
392/// That way Sema can handle and report similar errors for namespaces and the
393/// global scope.
394///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000395/// The isAddressOfOperand parameter indicates that this id-expression is a
396/// direct operand of the address-of operator. This is, besides member contexts,
397/// the only place where a qualified-id naming a non-static class member may
398/// appear.
399///
John McCall60d7b3a2010-08-24 06:29:42 +0000400ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000401 // qualified-id:
402 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
403 // '::' unqualified-id
404 //
405 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000406 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000407
408 UnqualifiedId Name;
409 if (ParseUnqualifiedId(SS,
410 /*EnteringContext=*/false,
411 /*AllowDestructorName=*/false,
412 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000413 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000414 Name))
415 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000416
417 // This is only the direct operand of an & operator if it is not
418 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000419 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
420 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000421
Douglas Gregor23c94db2010-07-02 17:43:08 +0000422 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000423 isAddressOfOperand);
424
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000425}
426
Reid Spencer5f016e22007-07-11 17:01:13 +0000427/// ParseCXXCasts - This handles the various ways to cast expressions to another
428/// type.
429///
430/// postfix-expression: [C++ 5.2p1]
431/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
432/// 'static_cast' '<' type-name '>' '(' expression ')'
433/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
434/// 'const_cast' '<' type-name '>' '(' expression ')'
435///
John McCall60d7b3a2010-08-24 06:29:42 +0000436ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 tok::TokenKind Kind = Tok.getKind();
438 const char *CastName = 0; // For error messages
439
440 switch (Kind) {
441 default: assert(0 && "Unknown C++ cast!"); abort();
442 case tok::kw_const_cast: CastName = "const_cast"; break;
443 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
444 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
445 case tok::kw_static_cast: CastName = "static_cast"; break;
446 }
447
448 SourceLocation OpLoc = ConsumeToken();
449 SourceLocation LAngleBracketLoc = Tok.getLocation();
450
451 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000452 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000453
Douglas Gregor809070a2009-02-18 17:45:20 +0000454 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 SourceLocation RAngleBracketLoc = Tok.getLocation();
456
Chris Lattner1ab3b962008-11-18 07:48:38 +0000457 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000458 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000459
460 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
461
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000462 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
463 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000464
John McCall60d7b3a2010-08-24 06:29:42 +0000465 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000467 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000468 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000469
Douglas Gregor809070a2009-02-18 17:45:20 +0000470 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000471 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000472 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000473 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000474 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000475
Sebastian Redl20df9b72008-12-11 22:51:44 +0000476 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000477}
478
Sebastian Redlc42e1182008-11-11 11:37:55 +0000479/// ParseCXXTypeid - This handles the C++ typeid expression.
480///
481/// postfix-expression: [C++ 5.2p1]
482/// 'typeid' '(' expression ')'
483/// 'typeid' '(' type-id ')'
484///
John McCall60d7b3a2010-08-24 06:29:42 +0000485ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000486 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
487
488 SourceLocation OpLoc = ConsumeToken();
489 SourceLocation LParenLoc = Tok.getLocation();
490 SourceLocation RParenLoc;
491
492 // typeid expressions are always parenthesized.
493 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
494 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000495 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000496
John McCall60d7b3a2010-08-24 06:29:42 +0000497 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000498
499 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000500 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000501
502 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000503 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000504
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000505 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000506 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000507
508 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000509 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000510 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000511 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000512 // When typeid is applied to an expression other than an lvalue of a
513 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000514 // operand (Clause 5).
515 //
Mike Stump1eb44332009-09-09 15:08:12 +0000516 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000517 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000518 // we the expression is potentially potentially evaluated.
519 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000520 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000521 Result = ParseExpression();
522
523 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000524 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000525 SkipUntil(tok::r_paren);
526 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000527 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
528 if (RParenLoc.isInvalid())
529 return ExprError();
530
Sebastian Redlc42e1182008-11-11 11:37:55 +0000531 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000532 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000533 }
534 }
535
Sebastian Redl20df9b72008-12-11 22:51:44 +0000536 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000537}
538
Francois Pichet01b7c302010-09-08 12:20:18 +0000539/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
540///
541/// '__uuidof' '(' expression ')'
542/// '__uuidof' '(' type-id ')'
543///
544ExprResult Parser::ParseCXXUuidof() {
545 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
546
547 SourceLocation OpLoc = ConsumeToken();
548 SourceLocation LParenLoc = Tok.getLocation();
549 SourceLocation RParenLoc;
550
551 // __uuidof expressions are always parenthesized.
552 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
553 "__uuidof"))
554 return ExprError();
555
556 ExprResult Result;
557
558 if (isTypeIdInParens()) {
559 TypeResult Ty = ParseTypeName();
560
561 // Match the ')'.
562 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
563
564 if (Ty.isInvalid())
565 return ExprError();
566
567 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
568 Ty.get().getAsOpaquePtr(), RParenLoc);
569 } else {
570 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
571 Result = ParseExpression();
572
573 // Match the ')'.
574 if (Result.isInvalid())
575 SkipUntil(tok::r_paren);
576 else {
577 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
578
579 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
580 Result.release(), RParenLoc);
581 }
582 }
583
584 return move(Result);
585}
586
Douglas Gregord4dca082010-02-24 18:44:31 +0000587/// \brief Parse a C++ pseudo-destructor expression after the base,
588/// . or -> operator, and nested-name-specifier have already been
589/// parsed.
590///
591/// postfix-expression: [C++ 5.2]
592/// postfix-expression . pseudo-destructor-name
593/// postfix-expression -> pseudo-destructor-name
594///
595/// pseudo-destructor-name:
596/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
597/// ::[opt] nested-name-specifier template simple-template-id ::
598/// ~type-name
599/// ::[opt] nested-name-specifier[opt] ~type-name
600///
John McCall60d7b3a2010-08-24 06:29:42 +0000601ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000602Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
603 tok::TokenKind OpKind,
604 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000605 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000606 // We're parsing either a pseudo-destructor-name or a dependent
607 // member access that has the same form as a
608 // pseudo-destructor-name. We parse both in the same way and let
609 // the action model sort them out.
610 //
611 // Note that the ::[opt] nested-name-specifier[opt] has already
612 // been parsed, and if there was a simple-template-id, it has
613 // been coalesced into a template-id annotation token.
614 UnqualifiedId FirstTypeName;
615 SourceLocation CCLoc;
616 if (Tok.is(tok::identifier)) {
617 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
618 ConsumeToken();
619 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
620 CCLoc = ConsumeToken();
621 } else if (Tok.is(tok::annot_template_id)) {
622 FirstTypeName.setTemplateId(
623 (TemplateIdAnnotation *)Tok.getAnnotationValue());
624 ConsumeToken();
625 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
626 CCLoc = ConsumeToken();
627 } else {
628 FirstTypeName.setIdentifier(0, SourceLocation());
629 }
630
631 // Parse the tilde.
632 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
633 SourceLocation TildeLoc = ConsumeToken();
634 if (!Tok.is(tok::identifier)) {
635 Diag(Tok, diag::err_destructor_tilde_identifier);
636 return ExprError();
637 }
638
639 // Parse the second type.
640 UnqualifiedId SecondTypeName;
641 IdentifierInfo *Name = Tok.getIdentifierInfo();
642 SourceLocation NameLoc = ConsumeToken();
643 SecondTypeName.setIdentifier(Name, NameLoc);
644
645 // If there is a '<', the second type name is a template-id. Parse
646 // it as such.
647 if (Tok.is(tok::less) &&
648 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000649 SecondTypeName, /*AssumeTemplateName=*/true,
650 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000651 return ExprError();
652
John McCall9ae2f072010-08-23 23:25:46 +0000653 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
654 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000655 SS, FirstTypeName, CCLoc,
656 TildeLoc, SecondTypeName,
657 Tok.is(tok::l_paren));
658}
659
Reid Spencer5f016e22007-07-11 17:01:13 +0000660/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
661///
662/// boolean-literal: [C++ 2.13.5]
663/// 'true'
664/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000665ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000667 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000668}
Chris Lattner50dd2892008-02-26 00:51:44 +0000669
670/// ParseThrowExpression - This handles the C++ throw expression.
671///
672/// throw-expression: [C++ 15]
673/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000674ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000675 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000676 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000677
Chris Lattner2a2819a2008-04-06 06:02:23 +0000678 // If the current token isn't the start of an assignment-expression,
679 // then the expression is not present. This handles things like:
680 // "C ? throw : (void)42", which is crazy but legal.
681 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
682 case tok::semi:
683 case tok::r_paren:
684 case tok::r_square:
685 case tok::r_brace:
686 case tok::colon:
687 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000688 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000689
Chris Lattner2a2819a2008-04-06 06:02:23 +0000690 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000691 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000692 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000693 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000694 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000695}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000696
697/// ParseCXXThis - This handles the C++ 'this' pointer.
698///
699/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
700/// a non-lvalue expression whose value is the address of the object for which
701/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000702ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000703 assert(Tok.is(tok::kw_this) && "Not 'this'!");
704 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000705 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000706}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000707
708/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
709/// Can be interpreted either as function-style casting ("int(x)")
710/// or class type construction ("ClassType(x,y,z)")
711/// or creation of a value-initialized type ("int()").
712///
713/// postfix-expression: [C++ 5.2p1]
714/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
715/// typename-specifier '(' expression-list[opt] ')' [TODO]
716///
John McCall60d7b3a2010-08-24 06:29:42 +0000717ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000718Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000719 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000720 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000721
722 assert(Tok.is(tok::l_paren) && "Expected '('!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000723 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
724
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000725 SourceLocation LParenLoc = ConsumeParen();
726
Sebastian Redla55e52c2008-11-25 22:21:31 +0000727 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000728 CommaLocsTy CommaLocs;
729
730 if (Tok.isNot(tok::r_paren)) {
731 if (ParseExpressionList(Exprs, CommaLocs)) {
732 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000733 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000734 }
735 }
736
737 // Match the ')'.
738 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
739
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000740 // TypeRep could be null, if it references an invalid typedef.
741 if (!TypeRep)
742 return ExprError();
743
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000744 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
745 "Unexpected number of commas!");
Douglas Gregorab6677e2010-09-08 00:15:04 +0000746 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000747 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000748}
749
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000750/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000751///
752/// condition:
753/// expression
754/// type-specifier-seq declarator '=' assignment-expression
755/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
756/// '=' assignment-expression
757///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000758/// \param ExprResult if the condition was parsed as an expression, the
759/// parsed expression.
760///
761/// \param DeclResult if the condition was parsed as a declaration, the
762/// parsed declaration.
763///
Douglas Gregor586596f2010-05-06 17:25:47 +0000764/// \param Loc The location of the start of the statement that requires this
765/// condition, e.g., the "for" in a for loop.
766///
767/// \param ConvertToBoolean Whether the condition expression should be
768/// converted to a boolean value.
769///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000770/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000771bool Parser::ParseCXXCondition(ExprResult &ExprOut,
772 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000773 SourceLocation Loc,
774 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000775 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000776 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000777 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000778 }
779
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000780 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000781 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000782 ExprOut = ParseExpression(); // expression
783 DeclOut = 0;
784 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000785 return true;
786
787 // If required, convert to a boolean value.
788 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000789 ExprOut
790 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
791 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000792 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000793
794 // type-specifier-seq
795 DeclSpec DS;
796 ParseSpecifierQualifierList(DS);
797
798 // declarator
799 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
800 ParseDeclarator(DeclaratorInfo);
801
802 // simple-asm-expr[opt]
803 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000804 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000805 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000806 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000807 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000808 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000809 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000810 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000811 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000812 }
813
814 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000815 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000816
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000817 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000818 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000819 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000820 DeclOut = Dcl.get();
821 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000822
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000823 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000824 if (isTokenEqualOrMistypedEqualEqual(
825 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000826 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000827 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000828 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000829 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
830 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000831 } else {
832 // FIXME: C++0x allows a braced-init-list
833 Diag(Tok, diag::err_expected_equal_after_declarator);
834 }
835
Douglas Gregor586596f2010-05-06 17:25:47 +0000836 // FIXME: Build a reference to this declaration? Convert it to bool?
837 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +0000838
839 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +0000840
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000841 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000842}
843
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000844/// \brief Determine whether the current token starts a C++
845/// simple-type-specifier.
846bool Parser::isCXXSimpleTypeSpecifier() const {
847 switch (Tok.getKind()) {
848 case tok::annot_typename:
849 case tok::kw_short:
850 case tok::kw_long:
851 case tok::kw_signed:
852 case tok::kw_unsigned:
853 case tok::kw_void:
854 case tok::kw_char:
855 case tok::kw_int:
856 case tok::kw_float:
857 case tok::kw_double:
858 case tok::kw_wchar_t:
859 case tok::kw_char16_t:
860 case tok::kw_char32_t:
861 case tok::kw_bool:
862 // FIXME: C++0x decltype support.
863 // GNU typeof support.
864 case tok::kw_typeof:
865 return true;
866
867 default:
868 break;
869 }
870
871 return false;
872}
873
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000874/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
875/// This should only be called when the current token is known to be part of
876/// simple-type-specifier.
877///
878/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000879/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000880/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
881/// char
882/// wchar_t
883/// bool
884/// short
885/// int
886/// long
887/// signed
888/// unsigned
889/// float
890/// double
891/// void
892/// [GNU] typeof-specifier
893/// [C++0x] auto [TODO]
894///
895/// type-name:
896/// class-name
897/// enum-name
898/// typedef-name
899///
900void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
901 DS.SetRangeStart(Tok.getLocation());
902 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000903 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000904 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000906 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000907 case tok::identifier: // foo::bar
908 case tok::coloncolon: // ::foo::bar
909 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000910 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000911 assert(0 && "Not a simple-type-specifier token!");
912 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000913
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000914 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000915 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000916 if (getTypeAnnotation(Tok))
917 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
918 getTypeAnnotation(Tok));
919 else
920 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000921
922 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
923 ConsumeToken();
924
925 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
926 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
927 // Objective-C interface. If we don't have Objective-C or a '<', this is
928 // just a normal reference to a typedef name.
929 if (Tok.is(tok::less) && getLang().ObjC1)
930 ParseObjCProtocolQualifiers(DS);
931
932 DS.Finish(Diags, PP);
933 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000936 // builtin types
937 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000938 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000939 break;
940 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000941 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000942 break;
943 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000944 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000945 break;
946 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000947 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000948 break;
949 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000950 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000951 break;
952 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000953 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000954 break;
955 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000956 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000957 break;
958 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000959 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000960 break;
961 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000962 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000963 break;
964 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000965 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000966 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000967 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000968 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000969 break;
970 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000971 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000972 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000973 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000974 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000975 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000977 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000978 // GNU typeof support.
979 case tok::kw_typeof:
980 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000981 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000982 return;
983 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000984 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000985 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
986 else
987 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000988 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000989 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000990}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000991
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000992/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
993/// [dcl.name]), which is a non-empty sequence of type-specifiers,
994/// e.g., "const short int". Note that the DeclSpec is *not* finished
995/// by parsing the type-specifier-seq, because these sequences are
996/// typically followed by some form of declarator. Returns true and
997/// emits diagnostics if this is not a type-specifier-seq, false
998/// otherwise.
999///
1000/// type-specifier-seq: [C++ 8.1]
1001/// type-specifier type-specifier-seq[opt]
1002///
1003bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1004 DS.SetRangeStart(Tok.getLocation());
1005 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001006 unsigned DiagID;
1007 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001008
1009 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001010 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1011 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001012 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001013 return true;
1014 }
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Sebastian Redld9bafa72010-02-03 21:21:43 +00001016 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1017 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1018 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001019
Douglas Gregor396a9f22010-02-24 23:13:13 +00001020 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001021 return false;
1022}
1023
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001024/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1025/// some form.
1026///
1027/// This routine is invoked when a '<' is encountered after an identifier or
1028/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1029/// whether the unqualified-id is actually a template-id. This routine will
1030/// then parse the template arguments and form the appropriate template-id to
1031/// return to the caller.
1032///
1033/// \param SS the nested-name-specifier that precedes this template-id, if
1034/// we're actually parsing a qualified-id.
1035///
1036/// \param Name for constructor and destructor names, this is the actual
1037/// identifier that may be a template-name.
1038///
1039/// \param NameLoc the location of the class-name in a constructor or
1040/// destructor.
1041///
1042/// \param EnteringContext whether we're entering the scope of the
1043/// nested-name-specifier.
1044///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001045/// \param ObjectType if this unqualified-id occurs within a member access
1046/// expression, the type of the base object whose member is being accessed.
1047///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001048/// \param Id as input, describes the template-name or operator-function-id
1049/// that precedes the '<'. If template arguments were parsed successfully,
1050/// will be updated with the template-id.
1051///
Douglas Gregord4dca082010-02-24 18:44:31 +00001052/// \param AssumeTemplateId When true, this routine will assume that the name
1053/// refers to a template without performing name lookup to verify.
1054///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001055/// \returns true if a parse error occurred, false otherwise.
1056bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1057 IdentifierInfo *Name,
1058 SourceLocation NameLoc,
1059 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001060 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001061 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001062 bool AssumeTemplateId,
1063 SourceLocation TemplateKWLoc) {
1064 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1065 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001066
1067 TemplateTy Template;
1068 TemplateNameKind TNK = TNK_Non_template;
1069 switch (Id.getKind()) {
1070 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001071 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001072 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001073 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001074 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001075 Id, ObjectType, EnteringContext,
1076 Template);
1077 if (TNK == TNK_Non_template)
1078 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001079 } else {
1080 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001081 TNK = Actions.isTemplateName(getCurScope(), SS,
1082 TemplateKWLoc.isValid(), Id,
1083 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001084 MemberOfUnknownSpecialization);
1085
1086 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1087 ObjectType && IsTemplateArgumentList()) {
1088 // We have something like t->getAs<T>(), where getAs is a
1089 // member of an unknown specialization. However, this will only
1090 // parse correctly as a template, so suggest the keyword 'template'
1091 // before 'getAs' and treat this as a dependent template name.
1092 std::string Name;
1093 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1094 Name = Id.Identifier->getName();
1095 else {
1096 Name = "operator ";
1097 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1098 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1099 else
1100 Name += Id.Identifier->getName();
1101 }
1102 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1103 << Name
1104 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001105 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001106 SS, Id, ObjectType,
1107 EnteringContext, Template);
1108 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001109 return true;
1110 }
1111 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001112 break;
1113
Douglas Gregor014e88d2009-11-03 23:16:33 +00001114 case UnqualifiedId::IK_ConstructorName: {
1115 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001116 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001117 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001118 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1119 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001120 EnteringContext, Template,
1121 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001122 break;
1123 }
1124
Douglas Gregor014e88d2009-11-03 23:16:33 +00001125 case UnqualifiedId::IK_DestructorName: {
1126 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001127 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001128 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001129 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001130 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001131 TemplateName, ObjectType,
1132 EnteringContext, Template);
1133 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001134 return true;
1135 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001136 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1137 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001138 EnteringContext, Template,
1139 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001140
John McCallb3d87482010-08-24 05:47:05 +00001141 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001142 Diag(NameLoc, diag::err_destructor_template_id)
1143 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001144 return true;
1145 }
1146 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001147 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001148 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001149
1150 default:
1151 return false;
1152 }
1153
1154 if (TNK == TNK_Non_template)
1155 return false;
1156
1157 // Parse the enclosed template argument list.
1158 SourceLocation LAngleLoc, RAngleLoc;
1159 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001160 if (Tok.is(tok::less) &&
1161 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001162 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001163 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001164 RAngleLoc))
1165 return true;
1166
1167 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001168 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1169 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001170 // Form a parsed representation of the template-id to be stored in the
1171 // UnqualifiedId.
1172 TemplateIdAnnotation *TemplateId
1173 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1174
1175 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1176 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001177 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001178 TemplateId->TemplateNameLoc = Id.StartLocation;
1179 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001180 TemplateId->Name = 0;
1181 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1182 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001183 }
1184
Douglas Gregor059101f2011-03-02 00:47:37 +00001185 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001186 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001187 TemplateId->Kind = TNK;
1188 TemplateId->LAngleLoc = LAngleLoc;
1189 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001190 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001191 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001192 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001193 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001194
1195 Id.setTemplateId(TemplateId);
1196 return false;
1197 }
1198
1199 // Bundle the template arguments together.
1200 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001201 TemplateArgs.size());
1202
1203 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001204 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001205 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001206 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001207 RAngleLoc);
1208 if (Type.isInvalid())
1209 return true;
1210
1211 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1212 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1213 else
1214 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1215
1216 return false;
1217}
1218
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001219/// \brief Parse an operator-function-id or conversion-function-id as part
1220/// of a C++ unqualified-id.
1221///
1222/// This routine is responsible only for parsing the operator-function-id or
1223/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001224///
1225/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001226/// operator-function-id: [C++ 13.5]
1227/// 'operator' operator
1228///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001229/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001230/// new delete new[] delete[]
1231/// + - * / % ^ & | ~
1232/// ! = < > += -= *= /= %=
1233/// ^= &= |= << >> >>= <<= == !=
1234/// <= >= && || ++ -- , ->* ->
1235/// () []
1236///
1237/// conversion-function-id: [C++ 12.3.2]
1238/// operator conversion-type-id
1239///
1240/// conversion-type-id:
1241/// type-specifier-seq conversion-declarator[opt]
1242///
1243/// conversion-declarator:
1244/// ptr-operator conversion-declarator[opt]
1245/// \endcode
1246///
1247/// \param The nested-name-specifier that preceded this unqualified-id. If
1248/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1249///
1250/// \param EnteringContext whether we are entering the scope of the
1251/// nested-name-specifier.
1252///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001253/// \param ObjectType if this unqualified-id occurs within a member access
1254/// expression, the type of the base object whose member is being accessed.
1255///
1256/// \param Result on a successful parse, contains the parsed unqualified-id.
1257///
1258/// \returns true if parsing fails, false otherwise.
1259bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001260 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001261 UnqualifiedId &Result) {
1262 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1263
1264 // Consume the 'operator' keyword.
1265 SourceLocation KeywordLoc = ConsumeToken();
1266
1267 // Determine what kind of operator name we have.
1268 unsigned SymbolIdx = 0;
1269 SourceLocation SymbolLocations[3];
1270 OverloadedOperatorKind Op = OO_None;
1271 switch (Tok.getKind()) {
1272 case tok::kw_new:
1273 case tok::kw_delete: {
1274 bool isNew = Tok.getKind() == tok::kw_new;
1275 // Consume the 'new' or 'delete'.
1276 SymbolLocations[SymbolIdx++] = ConsumeToken();
1277 if (Tok.is(tok::l_square)) {
1278 // Consume the '['.
1279 SourceLocation LBracketLoc = ConsumeBracket();
1280 // Consume the ']'.
1281 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1282 LBracketLoc);
1283 if (RBracketLoc.isInvalid())
1284 return true;
1285
1286 SymbolLocations[SymbolIdx++] = LBracketLoc;
1287 SymbolLocations[SymbolIdx++] = RBracketLoc;
1288 Op = isNew? OO_Array_New : OO_Array_Delete;
1289 } else {
1290 Op = isNew? OO_New : OO_Delete;
1291 }
1292 break;
1293 }
1294
1295#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1296 case tok::Token: \
1297 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1298 Op = OO_##Name; \
1299 break;
1300#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1301#include "clang/Basic/OperatorKinds.def"
1302
1303 case tok::l_paren: {
1304 // Consume the '('.
1305 SourceLocation LParenLoc = ConsumeParen();
1306 // Consume the ')'.
1307 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1308 LParenLoc);
1309 if (RParenLoc.isInvalid())
1310 return true;
1311
1312 SymbolLocations[SymbolIdx++] = LParenLoc;
1313 SymbolLocations[SymbolIdx++] = RParenLoc;
1314 Op = OO_Call;
1315 break;
1316 }
1317
1318 case tok::l_square: {
1319 // Consume the '['.
1320 SourceLocation LBracketLoc = ConsumeBracket();
1321 // Consume the ']'.
1322 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1323 LBracketLoc);
1324 if (RBracketLoc.isInvalid())
1325 return true;
1326
1327 SymbolLocations[SymbolIdx++] = LBracketLoc;
1328 SymbolLocations[SymbolIdx++] = RBracketLoc;
1329 Op = OO_Subscript;
1330 break;
1331 }
1332
1333 case tok::code_completion: {
1334 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001335 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001336
1337 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001338 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001339
1340 // Don't try to parse any further.
1341 return true;
1342 }
1343
1344 default:
1345 break;
1346 }
1347
1348 if (Op != OO_None) {
1349 // We have parsed an operator-function-id.
1350 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1351 return false;
1352 }
Sean Hunt0486d742009-11-28 04:44:28 +00001353
1354 // Parse a literal-operator-id.
1355 //
1356 // literal-operator-id: [C++0x 13.5.8]
1357 // operator "" identifier
1358
1359 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1360 if (Tok.getLength() != 2)
1361 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1362 ConsumeStringToken();
1363
1364 if (Tok.isNot(tok::identifier)) {
1365 Diag(Tok.getLocation(), diag::err_expected_ident);
1366 return true;
1367 }
1368
1369 IdentifierInfo *II = Tok.getIdentifierInfo();
1370 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001371 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001372 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001373
1374 // Parse a conversion-function-id.
1375 //
1376 // conversion-function-id: [C++ 12.3.2]
1377 // operator conversion-type-id
1378 //
1379 // conversion-type-id:
1380 // type-specifier-seq conversion-declarator[opt]
1381 //
1382 // conversion-declarator:
1383 // ptr-operator conversion-declarator[opt]
1384
1385 // Parse the type-specifier-seq.
1386 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001387 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001388 return true;
1389
1390 // Parse the conversion-declarator, which is merely a sequence of
1391 // ptr-operators.
1392 Declarator D(DS, Declarator::TypeNameContext);
1393 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1394
1395 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001396 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001397 if (Ty.isInvalid())
1398 return true;
1399
1400 // Note that this is a conversion-function-id.
1401 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1402 D.getSourceRange().getEnd());
1403 return false;
1404}
1405
1406/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1407/// name of an entity.
1408///
1409/// \code
1410/// unqualified-id: [C++ expr.prim.general]
1411/// identifier
1412/// operator-function-id
1413/// conversion-function-id
1414/// [C++0x] literal-operator-id [TODO]
1415/// ~ class-name
1416/// template-id
1417///
1418/// \endcode
1419///
1420/// \param The nested-name-specifier that preceded this unqualified-id. If
1421/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1422///
1423/// \param EnteringContext whether we are entering the scope of the
1424/// nested-name-specifier.
1425///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001426/// \param AllowDestructorName whether we allow parsing of a destructor name.
1427///
1428/// \param AllowConstructorName whether we allow parsing a constructor name.
1429///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001430/// \param ObjectType if this unqualified-id occurs within a member access
1431/// expression, the type of the base object whose member is being accessed.
1432///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001433/// \param Result on a successful parse, contains the parsed unqualified-id.
1434///
1435/// \returns true if parsing fails, false otherwise.
1436bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1437 bool AllowDestructorName,
1438 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001439 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001440 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001441
1442 // Handle 'A::template B'. This is for template-ids which have not
1443 // already been annotated by ParseOptionalCXXScopeSpecifier().
1444 bool TemplateSpecified = false;
1445 SourceLocation TemplateKWLoc;
1446 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1447 (ObjectType || SS.isSet())) {
1448 TemplateSpecified = true;
1449 TemplateKWLoc = ConsumeToken();
1450 }
1451
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001452 // unqualified-id:
1453 // identifier
1454 // template-id (when it hasn't already been annotated)
1455 if (Tok.is(tok::identifier)) {
1456 // Consume the identifier.
1457 IdentifierInfo *Id = Tok.getIdentifierInfo();
1458 SourceLocation IdLoc = ConsumeToken();
1459
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001460 if (!getLang().CPlusPlus) {
1461 // If we're not in C++, only identifiers matter. Record the
1462 // identifier and return.
1463 Result.setIdentifier(Id, IdLoc);
1464 return false;
1465 }
1466
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001467 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001468 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001469 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001470 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001471 &SS, false, false,
1472 ParsedType(),
1473 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001474 IdLoc, IdLoc);
1475 } else {
1476 // We have parsed an identifier.
1477 Result.setIdentifier(Id, IdLoc);
1478 }
1479
1480 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001481 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001482 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001483 ObjectType, Result,
1484 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001485
1486 return false;
1487 }
1488
1489 // unqualified-id:
1490 // template-id (already parsed and annotated)
1491 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001492 TemplateIdAnnotation *TemplateId
1493 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1494
1495 // If the template-name names the current class, then this is a constructor
1496 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001497 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001498 if (SS.isSet()) {
1499 // C++ [class.qual]p2 specifies that a qualified template-name
1500 // is taken as the constructor name where a constructor can be
1501 // declared. Thus, the template arguments are extraneous, so
1502 // complain about them and remove them entirely.
1503 Diag(TemplateId->TemplateNameLoc,
1504 diag::err_out_of_line_constructor_template_id)
1505 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001506 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001507 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1508 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1509 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001510 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001511 &SS, false, false,
1512 ParsedType(),
1513 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001514 TemplateId->TemplateNameLoc,
1515 TemplateId->RAngleLoc);
1516 TemplateId->Destroy();
1517 ConsumeToken();
1518 return false;
1519 }
1520
1521 Result.setConstructorTemplateId(TemplateId);
1522 ConsumeToken();
1523 return false;
1524 }
1525
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001526 // We have already parsed a template-id; consume the annotation token as
1527 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001528 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001529 ConsumeToken();
1530 return false;
1531 }
1532
1533 // unqualified-id:
1534 // operator-function-id
1535 // conversion-function-id
1536 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001537 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001538 return true;
1539
Sean Hunte6252d12009-11-28 08:58:14 +00001540 // If we have an operator-function-id or a literal-operator-id and the next
1541 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001542 //
1543 // template-id:
1544 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001545 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1546 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001547 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001548 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1549 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001550 Result,
1551 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001552
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001553 return false;
1554 }
1555
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001556 if (getLang().CPlusPlus &&
1557 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001558 // C++ [expr.unary.op]p10:
1559 // There is an ambiguity in the unary-expression ~X(), where X is a
1560 // class-name. The ambiguity is resolved in favor of treating ~ as a
1561 // unary complement rather than treating ~X as referring to a destructor.
1562
1563 // Parse the '~'.
1564 SourceLocation TildeLoc = ConsumeToken();
1565
1566 // Parse the class-name.
1567 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001568 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001569 return true;
1570 }
1571
1572 // Parse the class-name (or template-name in a simple-template-id).
1573 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1574 SourceLocation ClassNameLoc = ConsumeToken();
1575
Douglas Gregor0278e122010-05-05 05:58:24 +00001576 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001577 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001578 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001579 EnteringContext, ObjectType, Result,
1580 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001581 }
1582
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001583 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001584 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1585 ClassNameLoc, getCurScope(),
1586 SS, ObjectType,
1587 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001588 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001589 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001590
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001591 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001592 return false;
1593 }
1594
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001595 Diag(Tok, diag::err_expected_unqualified_id)
1596 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001597 return true;
1598}
1599
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001600/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1601/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001602///
Chris Lattner59232d32009-01-04 21:25:24 +00001603/// This method is called to parse the new expression after the optional :: has
1604/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1605/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001606///
1607/// new-expression:
1608/// '::'[opt] 'new' new-placement[opt] new-type-id
1609/// new-initializer[opt]
1610/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1611/// new-initializer[opt]
1612///
1613/// new-placement:
1614/// '(' expression-list ')'
1615///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001616/// new-type-id:
1617/// type-specifier-seq new-declarator[opt]
1618///
1619/// new-declarator:
1620/// ptr-operator new-declarator[opt]
1621/// direct-new-declarator
1622///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001623/// new-initializer:
1624/// '(' expression-list[opt] ')'
1625/// [C++0x] braced-init-list [TODO]
1626///
John McCall60d7b3a2010-08-24 06:29:42 +00001627ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001628Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1629 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1630 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001631
1632 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1633 // second form of new-expression. It can't be a new-type-id.
1634
Sebastian Redla55e52c2008-11-25 22:21:31 +00001635 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001636 SourceLocation PlacementLParen, PlacementRParen;
1637
Douglas Gregor4bd40312010-07-13 15:54:32 +00001638 SourceRange TypeIdParens;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001639 DeclSpec DS;
1640 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001641 if (Tok.is(tok::l_paren)) {
1642 // If it turns out to be a placement, we change the type location.
1643 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001644 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1645 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001646 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001647 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001648
1649 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001650 if (PlacementRParen.isInvalid()) {
1651 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001652 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001653 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001654
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001655 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001656 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001657 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001658 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001659 } else {
1660 // We still need the type.
1661 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001662 TypeIdParens.setBegin(ConsumeParen());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001663 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001664 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001665 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001666 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1667 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001668 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001669 if (ParseCXXTypeSpecifierSeq(DS))
1670 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001671 else {
1672 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001673 ParseDeclaratorInternal(DeclaratorInfo,
1674 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001675 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001676 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001677 }
1678 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001679 // A new-type-id is a simplified type-id, where essentially the
1680 // direct-declarator is replaced by a direct-new-declarator.
1681 if (ParseCXXTypeSpecifierSeq(DS))
1682 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001683 else {
1684 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001685 ParseDeclaratorInternal(DeclaratorInfo,
1686 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001687 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001688 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001689 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001690 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001691 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001692 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001693
Sebastian Redla55e52c2008-11-25 22:21:31 +00001694 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001695 SourceLocation ConstructorLParen, ConstructorRParen;
1696
1697 if (Tok.is(tok::l_paren)) {
1698 ConstructorLParen = ConsumeParen();
1699 if (Tok.isNot(tok::r_paren)) {
1700 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001701 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1702 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001703 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001704 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001705 }
1706 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001707 if (ConstructorRParen.isInvalid()) {
1708 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001709 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001710 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001711 }
1712
Sebastian Redlf53597f2009-03-15 17:47:39 +00001713 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1714 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001715 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001716 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001717}
1718
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001719/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1720/// passed to ParseDeclaratorInternal.
1721///
1722/// direct-new-declarator:
1723/// '[' expression ']'
1724/// direct-new-declarator '[' constant-expression ']'
1725///
Chris Lattner59232d32009-01-04 21:25:24 +00001726void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001727 // Parse the array dimensions.
1728 bool first = true;
1729 while (Tok.is(tok::l_square)) {
1730 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001731 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001732 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001733 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001734 // Recover
1735 SkipUntil(tok::r_square);
1736 return;
1737 }
1738 first = false;
1739
Sebastian Redlab197ba2009-02-09 18:23:29 +00001740 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall7f040a92010-12-24 02:08:15 +00001741 D.AddTypeInfo(DeclaratorChunk::getArray(0, ParsedAttributes(),
1742 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001743 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001744 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001745
Sebastian Redlab197ba2009-02-09 18:23:29 +00001746 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001747 return;
1748 }
1749}
1750
1751/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1752/// This ambiguity appears in the syntax of the C++ new operator.
1753///
1754/// new-expression:
1755/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1756/// new-initializer[opt]
1757///
1758/// new-placement:
1759/// '(' expression-list ')'
1760///
John McCallca0408f2010-08-23 06:44:23 +00001761bool Parser::ParseExpressionListOrTypeId(
1762 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001763 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001764 // The '(' was already consumed.
1765 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001766 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001767 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001768 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001769 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001770 }
1771
1772 // It's not a type, it has to be an expression list.
1773 // Discard the comma locations - ActOnCXXNew has enough parameters.
1774 CommaLocsTy CommaLocs;
1775 return ParseExpressionList(PlacementArgs, CommaLocs);
1776}
1777
1778/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1779/// to free memory allocated by new.
1780///
Chris Lattner59232d32009-01-04 21:25:24 +00001781/// This method is called to parse the 'delete' expression after the optional
1782/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1783/// and "Start" is its location. Otherwise, "Start" is the location of the
1784/// 'delete' token.
1785///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001786/// delete-expression:
1787/// '::'[opt] 'delete' cast-expression
1788/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001789ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001790Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1791 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1792 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001793
1794 // Array delete?
1795 bool ArrayDelete = false;
1796 if (Tok.is(tok::l_square)) {
1797 ArrayDelete = true;
1798 SourceLocation LHS = ConsumeBracket();
1799 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1800 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001801 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001802 }
1803
John McCall60d7b3a2010-08-24 06:29:42 +00001804 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001805 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001806 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001807
John McCall9ae2f072010-08-23 23:25:46 +00001808 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001809}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001810
Mike Stump1eb44332009-09-09 15:08:12 +00001811static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001812 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001813 default: llvm_unreachable("Not a known unary type trait");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001814 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1815 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1816 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1817 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1818 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1819 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1820 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1821 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1822 case tok::kw___is_abstract: return UTT_IsAbstract;
1823 case tok::kw___is_class: return UTT_IsClass;
1824 case tok::kw___is_empty: return UTT_IsEmpty;
1825 case tok::kw___is_enum: return UTT_IsEnum;
1826 case tok::kw___is_pod: return UTT_IsPOD;
1827 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1828 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001829 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001830 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001831}
1832
1833static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1834 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001835 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001836 case tok::kw___is_base_of: return BTT_IsBaseOf;
1837 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001838 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001839 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001840}
1841
1842/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1843/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1844/// templates.
1845///
1846/// primary-expression:
1847/// [GNU] unary-type-trait '(' type-id ')'
1848///
John McCall60d7b3a2010-08-24 06:29:42 +00001849ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001850 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1851 SourceLocation Loc = ConsumeToken();
1852
1853 SourceLocation LParen = Tok.getLocation();
1854 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1855 return ExprError();
1856
1857 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1858 // there will be cryptic errors about mismatched parentheses and missing
1859 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001860 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001861
1862 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1863
Douglas Gregor809070a2009-02-18 17:45:20 +00001864 if (Ty.isInvalid())
1865 return ExprError();
1866
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00001867 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001868}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001869
Francois Pichet6ad6f282010-12-07 00:08:36 +00001870/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
1871/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1872/// templates.
1873///
1874/// primary-expression:
1875/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
1876///
1877ExprResult Parser::ParseBinaryTypeTrait() {
1878 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
1879 SourceLocation Loc = ConsumeToken();
1880
1881 SourceLocation LParen = Tok.getLocation();
1882 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1883 return ExprError();
1884
1885 TypeResult LhsTy = ParseTypeName();
1886 if (LhsTy.isInvalid()) {
1887 SkipUntil(tok::r_paren);
1888 return ExprError();
1889 }
1890
1891 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
1892 SkipUntil(tok::r_paren);
1893 return ExprError();
1894 }
1895
1896 TypeResult RhsTy = ParseTypeName();
1897 if (RhsTy.isInvalid()) {
1898 SkipUntil(tok::r_paren);
1899 return ExprError();
1900 }
1901
1902 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1903
1904 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
1905}
1906
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001907/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1908/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1909/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00001910ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001911Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00001912 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001913 SourceLocation LParenLoc,
1914 SourceLocation &RParenLoc) {
1915 assert(getLang().CPlusPlus && "Should only be called for C++!");
1916 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1917 assert(isTypeIdInParens() && "Not a type-id!");
1918
John McCall60d7b3a2010-08-24 06:29:42 +00001919 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00001920 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001921
1922 // We need to disambiguate a very ugly part of the C++ syntax:
1923 //
1924 // (T())x; - type-id
1925 // (T())*x; - type-id
1926 // (T())/x; - expression
1927 // (T()); - expression
1928 //
1929 // The bad news is that we cannot use the specialized tentative parser, since
1930 // it can only verify that the thing inside the parens can be parsed as
1931 // type-id, it is not useful for determining the context past the parens.
1932 //
1933 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001934 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001935 //
1936 // It uses a scheme similar to parsing inline methods. The parenthesized
1937 // tokens are cached, the context that follows is determined (possibly by
1938 // parsing a cast-expression), and then we re-introduce the cached tokens
1939 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001940
Mike Stump1eb44332009-09-09 15:08:12 +00001941 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001942 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001943
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001944 // Store the tokens of the parentheses. We will parse them after we determine
1945 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001946 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001947 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001948 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1949 return ExprError();
1950 }
1951
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001952 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001953 ParseAs = CompoundLiteral;
1954 } else {
1955 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001956 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1957 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1958 NotCastExpr = true;
1959 } else {
1960 // Try parsing the cast-expression that may follow.
1961 // If it is not a cast-expression, NotCastExpr will be true and no token
1962 // will be consumed.
1963 Result = ParseCastExpression(false/*isUnaryExpression*/,
1964 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00001965 NotCastExpr,
1966 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001967 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001968
1969 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1970 // an expression.
1971 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001972 }
1973
Mike Stump1eb44332009-09-09 15:08:12 +00001974 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001975 Toks.push_back(Tok);
1976 // Re-enter the stored parenthesized tokens into the token stream, so we may
1977 // parse them now.
1978 PP.EnterTokenStream(Toks.data(), Toks.size(),
1979 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1980 // Drop the current token and bring the first cached one. It's the same token
1981 // as when we entered this function.
1982 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001983
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001984 if (ParseAs >= CompoundLiteral) {
1985 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001986
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001987 // Match the ')'.
1988 if (Tok.is(tok::r_paren))
1989 RParenLoc = ConsumeParen();
1990 else
1991 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001992
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001993 if (ParseAs == CompoundLiteral) {
1994 ExprType = CompoundLiteral;
1995 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1996 }
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001998 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1999 assert(ParseAs == CastExpr);
2000
2001 if (Ty.isInvalid())
2002 return ExprError();
2003
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002004 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002005
2006 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002007 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002008 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002009 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002010 return move(Result);
2011 }
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002013 // Not a compound literal, and not followed by a cast-expression.
2014 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002015
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002016 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002017 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002018 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002019 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002020
2021 // Match the ')'.
2022 if (Result.isInvalid()) {
2023 SkipUntil(tok::r_paren);
2024 return ExprError();
2025 }
Mike Stump1eb44332009-09-09 15:08:12 +00002026
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002027 if (Tok.is(tok::r_paren))
2028 RParenLoc = ConsumeParen();
2029 else
2030 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2031
2032 return move(Result);
2033}