blob: 1f41f5dbc5eb597b06863979df3862fa2ad3cfb3 [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();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000530
531 // If we are a foo<int> that identifies a single function, resolve it now...
532 Expr* e = Result.get();
533 if (e->getType() == Actions.Context.OverloadTy) {
534 ExprResult er =
535 Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
536 if (er.isUsable())
537 Result = er.release();
538 }
Sebastian Redlc42e1182008-11-11 11:37:55 +0000539 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000540 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000541 }
542 }
543
Sebastian Redl20df9b72008-12-11 22:51:44 +0000544 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000545}
546
Francois Pichet01b7c302010-09-08 12:20:18 +0000547/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
548///
549/// '__uuidof' '(' expression ')'
550/// '__uuidof' '(' type-id ')'
551///
552ExprResult Parser::ParseCXXUuidof() {
553 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
554
555 SourceLocation OpLoc = ConsumeToken();
556 SourceLocation LParenLoc = Tok.getLocation();
557 SourceLocation RParenLoc;
558
559 // __uuidof expressions are always parenthesized.
560 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
561 "__uuidof"))
562 return ExprError();
563
564 ExprResult Result;
565
566 if (isTypeIdInParens()) {
567 TypeResult Ty = ParseTypeName();
568
569 // Match the ')'.
570 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
571
572 if (Ty.isInvalid())
573 return ExprError();
574
575 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
576 Ty.get().getAsOpaquePtr(), RParenLoc);
577 } else {
578 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
579 Result = ParseExpression();
580
581 // Match the ')'.
582 if (Result.isInvalid())
583 SkipUntil(tok::r_paren);
584 else {
585 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
586
587 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
588 Result.release(), RParenLoc);
589 }
590 }
591
592 return move(Result);
593}
594
Douglas Gregord4dca082010-02-24 18:44:31 +0000595/// \brief Parse a C++ pseudo-destructor expression after the base,
596/// . or -> operator, and nested-name-specifier have already been
597/// parsed.
598///
599/// postfix-expression: [C++ 5.2]
600/// postfix-expression . pseudo-destructor-name
601/// postfix-expression -> pseudo-destructor-name
602///
603/// pseudo-destructor-name:
604/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
605/// ::[opt] nested-name-specifier template simple-template-id ::
606/// ~type-name
607/// ::[opt] nested-name-specifier[opt] ~type-name
608///
John McCall60d7b3a2010-08-24 06:29:42 +0000609ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000610Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
611 tok::TokenKind OpKind,
612 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000613 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000614 // We're parsing either a pseudo-destructor-name or a dependent
615 // member access that has the same form as a
616 // pseudo-destructor-name. We parse both in the same way and let
617 // the action model sort them out.
618 //
619 // Note that the ::[opt] nested-name-specifier[opt] has already
620 // been parsed, and if there was a simple-template-id, it has
621 // been coalesced into a template-id annotation token.
622 UnqualifiedId FirstTypeName;
623 SourceLocation CCLoc;
624 if (Tok.is(tok::identifier)) {
625 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
626 ConsumeToken();
627 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
628 CCLoc = ConsumeToken();
629 } else if (Tok.is(tok::annot_template_id)) {
630 FirstTypeName.setTemplateId(
631 (TemplateIdAnnotation *)Tok.getAnnotationValue());
632 ConsumeToken();
633 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
634 CCLoc = ConsumeToken();
635 } else {
636 FirstTypeName.setIdentifier(0, SourceLocation());
637 }
638
639 // Parse the tilde.
640 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
641 SourceLocation TildeLoc = ConsumeToken();
642 if (!Tok.is(tok::identifier)) {
643 Diag(Tok, diag::err_destructor_tilde_identifier);
644 return ExprError();
645 }
646
647 // Parse the second type.
648 UnqualifiedId SecondTypeName;
649 IdentifierInfo *Name = Tok.getIdentifierInfo();
650 SourceLocation NameLoc = ConsumeToken();
651 SecondTypeName.setIdentifier(Name, NameLoc);
652
653 // If there is a '<', the second type name is a template-id. Parse
654 // it as such.
655 if (Tok.is(tok::less) &&
656 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000657 SecondTypeName, /*AssumeTemplateName=*/true,
658 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000659 return ExprError();
660
John McCall9ae2f072010-08-23 23:25:46 +0000661 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
662 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000663 SS, FirstTypeName, CCLoc,
664 TildeLoc, SecondTypeName,
665 Tok.is(tok::l_paren));
666}
667
Reid Spencer5f016e22007-07-11 17:01:13 +0000668/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
669///
670/// boolean-literal: [C++ 2.13.5]
671/// 'true'
672/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000673ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000675 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000676}
Chris Lattner50dd2892008-02-26 00:51:44 +0000677
678/// ParseThrowExpression - This handles the C++ throw expression.
679///
680/// throw-expression: [C++ 15]
681/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000682ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000683 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000684 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000685
Chris Lattner2a2819a2008-04-06 06:02:23 +0000686 // If the current token isn't the start of an assignment-expression,
687 // then the expression is not present. This handles things like:
688 // "C ? throw : (void)42", which is crazy but legal.
689 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
690 case tok::semi:
691 case tok::r_paren:
692 case tok::r_square:
693 case tok::r_brace:
694 case tok::colon:
695 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000696 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000697
Chris Lattner2a2819a2008-04-06 06:02:23 +0000698 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000699 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000700 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000701 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000702 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000703}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000704
705/// ParseCXXThis - This handles the C++ 'this' pointer.
706///
707/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
708/// a non-lvalue expression whose value is the address of the object for which
709/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000710ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000711 assert(Tok.is(tok::kw_this) && "Not 'this'!");
712 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000713 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000714}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000715
716/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
717/// Can be interpreted either as function-style casting ("int(x)")
718/// or class type construction ("ClassType(x,y,z)")
719/// or creation of a value-initialized type ("int()").
720///
721/// postfix-expression: [C++ 5.2p1]
722/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
723/// typename-specifier '(' expression-list[opt] ')' [TODO]
724///
John McCall60d7b3a2010-08-24 06:29:42 +0000725ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000726Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000727 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000728 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000729
730 assert(Tok.is(tok::l_paren) && "Expected '('!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000731 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
732
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000733 SourceLocation LParenLoc = ConsumeParen();
734
Sebastian Redla55e52c2008-11-25 22:21:31 +0000735 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000736 CommaLocsTy CommaLocs;
737
738 if (Tok.isNot(tok::r_paren)) {
739 if (ParseExpressionList(Exprs, CommaLocs)) {
740 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000741 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000742 }
743 }
744
745 // Match the ')'.
746 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
747
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000748 // TypeRep could be null, if it references an invalid typedef.
749 if (!TypeRep)
750 return ExprError();
751
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000752 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
753 "Unexpected number of commas!");
Douglas Gregorab6677e2010-09-08 00:15:04 +0000754 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000755 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000756}
757
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000758/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000759///
760/// condition:
761/// expression
762/// type-specifier-seq declarator '=' assignment-expression
763/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
764/// '=' assignment-expression
765///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000766/// \param ExprResult if the condition was parsed as an expression, the
767/// parsed expression.
768///
769/// \param DeclResult if the condition was parsed as a declaration, the
770/// parsed declaration.
771///
Douglas Gregor586596f2010-05-06 17:25:47 +0000772/// \param Loc The location of the start of the statement that requires this
773/// condition, e.g., the "for" in a for loop.
774///
775/// \param ConvertToBoolean Whether the condition expression should be
776/// converted to a boolean value.
777///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000778/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000779bool Parser::ParseCXXCondition(ExprResult &ExprOut,
780 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000781 SourceLocation Loc,
782 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000783 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000784 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000785 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000786 }
787
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000788 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000789 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000790 ExprOut = ParseExpression(); // expression
791 DeclOut = 0;
792 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000793 return true;
794
795 // If required, convert to a boolean value.
796 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000797 ExprOut
798 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
799 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000800 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000801
802 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +0000803 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000804 ParseSpecifierQualifierList(DS);
805
806 // declarator
807 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
808 ParseDeclarator(DeclaratorInfo);
809
810 // simple-asm-expr[opt]
811 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000812 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000813 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000814 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000815 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000816 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000817 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000818 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000819 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000820 }
821
822 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000823 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000824
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000825 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000826 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000827 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000828 DeclOut = Dcl.get();
829 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000830
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000831 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000832 if (isTokenEqualOrMistypedEqualEqual(
833 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000834 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000835 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000836 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000837 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
838 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000839 } else {
840 // FIXME: C++0x allows a braced-init-list
841 Diag(Tok, diag::err_expected_equal_after_declarator);
842 }
843
Douglas Gregor586596f2010-05-06 17:25:47 +0000844 // FIXME: Build a reference to this declaration? Convert it to bool?
845 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +0000846
847 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +0000848
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000849 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000850}
851
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000852/// \brief Determine whether the current token starts a C++
853/// simple-type-specifier.
854bool Parser::isCXXSimpleTypeSpecifier() const {
855 switch (Tok.getKind()) {
856 case tok::annot_typename:
857 case tok::kw_short:
858 case tok::kw_long:
859 case tok::kw_signed:
860 case tok::kw_unsigned:
861 case tok::kw_void:
862 case tok::kw_char:
863 case tok::kw_int:
864 case tok::kw_float:
865 case tok::kw_double:
866 case tok::kw_wchar_t:
867 case tok::kw_char16_t:
868 case tok::kw_char32_t:
869 case tok::kw_bool:
870 // FIXME: C++0x decltype support.
871 // GNU typeof support.
872 case tok::kw_typeof:
873 return true;
874
875 default:
876 break;
877 }
878
879 return false;
880}
881
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000882/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
883/// This should only be called when the current token is known to be part of
884/// simple-type-specifier.
885///
886/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000887/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000888/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
889/// char
890/// wchar_t
891/// bool
892/// short
893/// int
894/// long
895/// signed
896/// unsigned
897/// float
898/// double
899/// void
900/// [GNU] typeof-specifier
901/// [C++0x] auto [TODO]
902///
903/// type-name:
904/// class-name
905/// enum-name
906/// typedef-name
907///
908void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
909 DS.SetRangeStart(Tok.getLocation());
910 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000911 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000912 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000914 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000915 case tok::identifier: // foo::bar
916 case tok::coloncolon: // ::foo::bar
917 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000918 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000919 assert(0 && "Not a simple-type-specifier token!");
920 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000921
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000922 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000923 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000924 if (getTypeAnnotation(Tok))
925 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
926 getTypeAnnotation(Tok));
927 else
928 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000929
930 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
931 ConsumeToken();
932
933 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
934 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
935 // Objective-C interface. If we don't have Objective-C or a '<', this is
936 // just a normal reference to a typedef name.
937 if (Tok.is(tok::less) && getLang().ObjC1)
938 ParseObjCProtocolQualifiers(DS);
939
940 DS.Finish(Diags, PP);
941 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000944 // builtin types
945 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000946 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000947 break;
948 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000949 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000950 break;
951 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000952 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000953 break;
954 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000955 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000956 break;
957 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000958 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000959 break;
960 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000961 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000962 break;
963 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000964 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000965 break;
966 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000967 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000968 break;
969 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000970 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000971 break;
972 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000973 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000974 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000975 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000976 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000977 break;
978 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000979 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000980 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000981 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000982 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000983 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000985 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000986 // GNU typeof support.
987 case tok::kw_typeof:
988 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000989 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000990 return;
991 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000992 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000993 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
994 else
995 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000996 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000997 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000998}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000999
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001000/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1001/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1002/// e.g., "const short int". Note that the DeclSpec is *not* finished
1003/// by parsing the type-specifier-seq, because these sequences are
1004/// typically followed by some form of declarator. Returns true and
1005/// emits diagnostics if this is not a type-specifier-seq, false
1006/// otherwise.
1007///
1008/// type-specifier-seq: [C++ 8.1]
1009/// type-specifier type-specifier-seq[opt]
1010///
1011bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1012 DS.SetRangeStart(Tok.getLocation());
1013 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001014 unsigned DiagID;
1015 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001016
1017 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001018 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1019 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001020 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001021 return true;
1022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Sebastian Redld9bafa72010-02-03 21:21:43 +00001024 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1025 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1026 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001027
Douglas Gregor396a9f22010-02-24 23:13:13 +00001028 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001029 return false;
1030}
1031
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001032/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1033/// some form.
1034///
1035/// This routine is invoked when a '<' is encountered after an identifier or
1036/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1037/// whether the unqualified-id is actually a template-id. This routine will
1038/// then parse the template arguments and form the appropriate template-id to
1039/// return to the caller.
1040///
1041/// \param SS the nested-name-specifier that precedes this template-id, if
1042/// we're actually parsing a qualified-id.
1043///
1044/// \param Name for constructor and destructor names, this is the actual
1045/// identifier that may be a template-name.
1046///
1047/// \param NameLoc the location of the class-name in a constructor or
1048/// destructor.
1049///
1050/// \param EnteringContext whether we're entering the scope of the
1051/// nested-name-specifier.
1052///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001053/// \param ObjectType if this unqualified-id occurs within a member access
1054/// expression, the type of the base object whose member is being accessed.
1055///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001056/// \param Id as input, describes the template-name or operator-function-id
1057/// that precedes the '<'. If template arguments were parsed successfully,
1058/// will be updated with the template-id.
1059///
Douglas Gregord4dca082010-02-24 18:44:31 +00001060/// \param AssumeTemplateId When true, this routine will assume that the name
1061/// refers to a template without performing name lookup to verify.
1062///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001063/// \returns true if a parse error occurred, false otherwise.
1064bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1065 IdentifierInfo *Name,
1066 SourceLocation NameLoc,
1067 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001068 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001069 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001070 bool AssumeTemplateId,
1071 SourceLocation TemplateKWLoc) {
1072 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1073 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001074
1075 TemplateTy Template;
1076 TemplateNameKind TNK = TNK_Non_template;
1077 switch (Id.getKind()) {
1078 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001079 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001080 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001081 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001082 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001083 Id, ObjectType, EnteringContext,
1084 Template);
1085 if (TNK == TNK_Non_template)
1086 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001087 } else {
1088 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001089 TNK = Actions.isTemplateName(getCurScope(), SS,
1090 TemplateKWLoc.isValid(), Id,
1091 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001092 MemberOfUnknownSpecialization);
1093
1094 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1095 ObjectType && IsTemplateArgumentList()) {
1096 // We have something like t->getAs<T>(), where getAs is a
1097 // member of an unknown specialization. However, this will only
1098 // parse correctly as a template, so suggest the keyword 'template'
1099 // before 'getAs' and treat this as a dependent template name.
1100 std::string Name;
1101 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1102 Name = Id.Identifier->getName();
1103 else {
1104 Name = "operator ";
1105 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1106 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1107 else
1108 Name += Id.Identifier->getName();
1109 }
1110 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1111 << Name
1112 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001113 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001114 SS, Id, ObjectType,
1115 EnteringContext, Template);
1116 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001117 return true;
1118 }
1119 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001120 break;
1121
Douglas Gregor014e88d2009-11-03 23:16:33 +00001122 case UnqualifiedId::IK_ConstructorName: {
1123 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001124 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001125 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001126 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1127 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001128 EnteringContext, Template,
1129 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001130 break;
1131 }
1132
Douglas Gregor014e88d2009-11-03 23:16:33 +00001133 case UnqualifiedId::IK_DestructorName: {
1134 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001135 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001136 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001137 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001138 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001139 TemplateName, ObjectType,
1140 EnteringContext, Template);
1141 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001142 return true;
1143 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001144 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1145 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001146 EnteringContext, Template,
1147 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001148
John McCallb3d87482010-08-24 05:47:05 +00001149 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001150 Diag(NameLoc, diag::err_destructor_template_id)
1151 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001152 return true;
1153 }
1154 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001155 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001156 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001157
1158 default:
1159 return false;
1160 }
1161
1162 if (TNK == TNK_Non_template)
1163 return false;
1164
1165 // Parse the enclosed template argument list.
1166 SourceLocation LAngleLoc, RAngleLoc;
1167 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001168 if (Tok.is(tok::less) &&
1169 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001170 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001171 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001172 RAngleLoc))
1173 return true;
1174
1175 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001176 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1177 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001178 // Form a parsed representation of the template-id to be stored in the
1179 // UnqualifiedId.
1180 TemplateIdAnnotation *TemplateId
1181 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1182
1183 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1184 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001185 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001186 TemplateId->TemplateNameLoc = Id.StartLocation;
1187 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001188 TemplateId->Name = 0;
1189 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1190 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001191 }
1192
Douglas Gregor059101f2011-03-02 00:47:37 +00001193 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001194 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001195 TemplateId->Kind = TNK;
1196 TemplateId->LAngleLoc = LAngleLoc;
1197 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001198 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001199 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001200 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001201 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001202
1203 Id.setTemplateId(TemplateId);
1204 return false;
1205 }
1206
1207 // Bundle the template arguments together.
1208 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001209 TemplateArgs.size());
1210
1211 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001212 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001213 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001214 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001215 RAngleLoc);
1216 if (Type.isInvalid())
1217 return true;
1218
1219 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1220 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1221 else
1222 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1223
1224 return false;
1225}
1226
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001227/// \brief Parse an operator-function-id or conversion-function-id as part
1228/// of a C++ unqualified-id.
1229///
1230/// This routine is responsible only for parsing the operator-function-id or
1231/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001232///
1233/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001234/// operator-function-id: [C++ 13.5]
1235/// 'operator' operator
1236///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001237/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001238/// new delete new[] delete[]
1239/// + - * / % ^ & | ~
1240/// ! = < > += -= *= /= %=
1241/// ^= &= |= << >> >>= <<= == !=
1242/// <= >= && || ++ -- , ->* ->
1243/// () []
1244///
1245/// conversion-function-id: [C++ 12.3.2]
1246/// operator conversion-type-id
1247///
1248/// conversion-type-id:
1249/// type-specifier-seq conversion-declarator[opt]
1250///
1251/// conversion-declarator:
1252/// ptr-operator conversion-declarator[opt]
1253/// \endcode
1254///
1255/// \param The nested-name-specifier that preceded this unqualified-id. If
1256/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1257///
1258/// \param EnteringContext whether we are entering the scope of the
1259/// nested-name-specifier.
1260///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001261/// \param ObjectType if this unqualified-id occurs within a member access
1262/// expression, the type of the base object whose member is being accessed.
1263///
1264/// \param Result on a successful parse, contains the parsed unqualified-id.
1265///
1266/// \returns true if parsing fails, false otherwise.
1267bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001268 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001269 UnqualifiedId &Result) {
1270 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1271
1272 // Consume the 'operator' keyword.
1273 SourceLocation KeywordLoc = ConsumeToken();
1274
1275 // Determine what kind of operator name we have.
1276 unsigned SymbolIdx = 0;
1277 SourceLocation SymbolLocations[3];
1278 OverloadedOperatorKind Op = OO_None;
1279 switch (Tok.getKind()) {
1280 case tok::kw_new:
1281 case tok::kw_delete: {
1282 bool isNew = Tok.getKind() == tok::kw_new;
1283 // Consume the 'new' or 'delete'.
1284 SymbolLocations[SymbolIdx++] = ConsumeToken();
1285 if (Tok.is(tok::l_square)) {
1286 // Consume the '['.
1287 SourceLocation LBracketLoc = ConsumeBracket();
1288 // Consume the ']'.
1289 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1290 LBracketLoc);
1291 if (RBracketLoc.isInvalid())
1292 return true;
1293
1294 SymbolLocations[SymbolIdx++] = LBracketLoc;
1295 SymbolLocations[SymbolIdx++] = RBracketLoc;
1296 Op = isNew? OO_Array_New : OO_Array_Delete;
1297 } else {
1298 Op = isNew? OO_New : OO_Delete;
1299 }
1300 break;
1301 }
1302
1303#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1304 case tok::Token: \
1305 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1306 Op = OO_##Name; \
1307 break;
1308#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1309#include "clang/Basic/OperatorKinds.def"
1310
1311 case tok::l_paren: {
1312 // Consume the '('.
1313 SourceLocation LParenLoc = ConsumeParen();
1314 // Consume the ')'.
1315 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1316 LParenLoc);
1317 if (RParenLoc.isInvalid())
1318 return true;
1319
1320 SymbolLocations[SymbolIdx++] = LParenLoc;
1321 SymbolLocations[SymbolIdx++] = RParenLoc;
1322 Op = OO_Call;
1323 break;
1324 }
1325
1326 case tok::l_square: {
1327 // Consume the '['.
1328 SourceLocation LBracketLoc = ConsumeBracket();
1329 // Consume the ']'.
1330 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1331 LBracketLoc);
1332 if (RBracketLoc.isInvalid())
1333 return true;
1334
1335 SymbolLocations[SymbolIdx++] = LBracketLoc;
1336 SymbolLocations[SymbolIdx++] = RBracketLoc;
1337 Op = OO_Subscript;
1338 break;
1339 }
1340
1341 case tok::code_completion: {
1342 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001343 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001344
1345 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001346 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001347
1348 // Don't try to parse any further.
1349 return true;
1350 }
1351
1352 default:
1353 break;
1354 }
1355
1356 if (Op != OO_None) {
1357 // We have parsed an operator-function-id.
1358 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1359 return false;
1360 }
Sean Hunt0486d742009-11-28 04:44:28 +00001361
1362 // Parse a literal-operator-id.
1363 //
1364 // literal-operator-id: [C++0x 13.5.8]
1365 // operator "" identifier
1366
1367 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1368 if (Tok.getLength() != 2)
1369 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1370 ConsumeStringToken();
1371
1372 if (Tok.isNot(tok::identifier)) {
1373 Diag(Tok.getLocation(), diag::err_expected_ident);
1374 return true;
1375 }
1376
1377 IdentifierInfo *II = Tok.getIdentifierInfo();
1378 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001379 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001380 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001381
1382 // Parse a conversion-function-id.
1383 //
1384 // conversion-function-id: [C++ 12.3.2]
1385 // operator conversion-type-id
1386 //
1387 // conversion-type-id:
1388 // type-specifier-seq conversion-declarator[opt]
1389 //
1390 // conversion-declarator:
1391 // ptr-operator conversion-declarator[opt]
1392
1393 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001394 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001395 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001396 return true;
1397
1398 // Parse the conversion-declarator, which is merely a sequence of
1399 // ptr-operators.
1400 Declarator D(DS, Declarator::TypeNameContext);
1401 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1402
1403 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001404 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001405 if (Ty.isInvalid())
1406 return true;
1407
1408 // Note that this is a conversion-function-id.
1409 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1410 D.getSourceRange().getEnd());
1411 return false;
1412}
1413
1414/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1415/// name of an entity.
1416///
1417/// \code
1418/// unqualified-id: [C++ expr.prim.general]
1419/// identifier
1420/// operator-function-id
1421/// conversion-function-id
1422/// [C++0x] literal-operator-id [TODO]
1423/// ~ class-name
1424/// template-id
1425///
1426/// \endcode
1427///
1428/// \param The nested-name-specifier that preceded this unqualified-id. If
1429/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1430///
1431/// \param EnteringContext whether we are entering the scope of the
1432/// nested-name-specifier.
1433///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001434/// \param AllowDestructorName whether we allow parsing of a destructor name.
1435///
1436/// \param AllowConstructorName whether we allow parsing a constructor name.
1437///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001438/// \param ObjectType if this unqualified-id occurs within a member access
1439/// expression, the type of the base object whose member is being accessed.
1440///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001441/// \param Result on a successful parse, contains the parsed unqualified-id.
1442///
1443/// \returns true if parsing fails, false otherwise.
1444bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1445 bool AllowDestructorName,
1446 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001447 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001448 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001449
1450 // Handle 'A::template B'. This is for template-ids which have not
1451 // already been annotated by ParseOptionalCXXScopeSpecifier().
1452 bool TemplateSpecified = false;
1453 SourceLocation TemplateKWLoc;
1454 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1455 (ObjectType || SS.isSet())) {
1456 TemplateSpecified = true;
1457 TemplateKWLoc = ConsumeToken();
1458 }
1459
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001460 // unqualified-id:
1461 // identifier
1462 // template-id (when it hasn't already been annotated)
1463 if (Tok.is(tok::identifier)) {
1464 // Consume the identifier.
1465 IdentifierInfo *Id = Tok.getIdentifierInfo();
1466 SourceLocation IdLoc = ConsumeToken();
1467
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001468 if (!getLang().CPlusPlus) {
1469 // If we're not in C++, only identifiers matter. Record the
1470 // identifier and return.
1471 Result.setIdentifier(Id, IdLoc);
1472 return false;
1473 }
1474
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001475 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001476 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001477 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001478 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001479 &SS, false, false,
1480 ParsedType(),
1481 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001482 IdLoc, IdLoc);
1483 } else {
1484 // We have parsed an identifier.
1485 Result.setIdentifier(Id, IdLoc);
1486 }
1487
1488 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001489 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001490 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001491 ObjectType, Result,
1492 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001493
1494 return false;
1495 }
1496
1497 // unqualified-id:
1498 // template-id (already parsed and annotated)
1499 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001500 TemplateIdAnnotation *TemplateId
1501 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1502
1503 // If the template-name names the current class, then this is a constructor
1504 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001505 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001506 if (SS.isSet()) {
1507 // C++ [class.qual]p2 specifies that a qualified template-name
1508 // is taken as the constructor name where a constructor can be
1509 // declared. Thus, the template arguments are extraneous, so
1510 // complain about them and remove them entirely.
1511 Diag(TemplateId->TemplateNameLoc,
1512 diag::err_out_of_line_constructor_template_id)
1513 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001514 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001515 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1516 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1517 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001518 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001519 &SS, false, false,
1520 ParsedType(),
1521 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001522 TemplateId->TemplateNameLoc,
1523 TemplateId->RAngleLoc);
1524 TemplateId->Destroy();
1525 ConsumeToken();
1526 return false;
1527 }
1528
1529 Result.setConstructorTemplateId(TemplateId);
1530 ConsumeToken();
1531 return false;
1532 }
1533
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001534 // We have already parsed a template-id; consume the annotation token as
1535 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001536 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001537 ConsumeToken();
1538 return false;
1539 }
1540
1541 // unqualified-id:
1542 // operator-function-id
1543 // conversion-function-id
1544 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001545 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001546 return true;
1547
Sean Hunte6252d12009-11-28 08:58:14 +00001548 // If we have an operator-function-id or a literal-operator-id and the next
1549 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001550 //
1551 // template-id:
1552 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001553 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1554 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001555 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001556 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1557 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001558 Result,
1559 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001560
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001561 return false;
1562 }
1563
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001564 if (getLang().CPlusPlus &&
1565 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001566 // C++ [expr.unary.op]p10:
1567 // There is an ambiguity in the unary-expression ~X(), where X is a
1568 // class-name. The ambiguity is resolved in favor of treating ~ as a
1569 // unary complement rather than treating ~X as referring to a destructor.
1570
1571 // Parse the '~'.
1572 SourceLocation TildeLoc = ConsumeToken();
1573
1574 // Parse the class-name.
1575 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001576 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001577 return true;
1578 }
1579
1580 // Parse the class-name (or template-name in a simple-template-id).
1581 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1582 SourceLocation ClassNameLoc = ConsumeToken();
1583
Douglas Gregor0278e122010-05-05 05:58:24 +00001584 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001585 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001586 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001587 EnteringContext, ObjectType, Result,
1588 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001589 }
1590
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001591 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001592 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1593 ClassNameLoc, getCurScope(),
1594 SS, ObjectType,
1595 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001596 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001597 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001598
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001599 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001600 return false;
1601 }
1602
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001603 Diag(Tok, diag::err_expected_unqualified_id)
1604 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001605 return true;
1606}
1607
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001608/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1609/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001610///
Chris Lattner59232d32009-01-04 21:25:24 +00001611/// This method is called to parse the new expression after the optional :: has
1612/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1613/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001614///
1615/// new-expression:
1616/// '::'[opt] 'new' new-placement[opt] new-type-id
1617/// new-initializer[opt]
1618/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1619/// new-initializer[opt]
1620///
1621/// new-placement:
1622/// '(' expression-list ')'
1623///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001624/// new-type-id:
1625/// type-specifier-seq new-declarator[opt]
1626///
1627/// new-declarator:
1628/// ptr-operator new-declarator[opt]
1629/// direct-new-declarator
1630///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001631/// new-initializer:
1632/// '(' expression-list[opt] ')'
1633/// [C++0x] braced-init-list [TODO]
1634///
John McCall60d7b3a2010-08-24 06:29:42 +00001635ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001636Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1637 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1638 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001639
1640 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1641 // second form of new-expression. It can't be a new-type-id.
1642
Sebastian Redla55e52c2008-11-25 22:21:31 +00001643 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001644 SourceLocation PlacementLParen, PlacementRParen;
1645
Douglas Gregor4bd40312010-07-13 15:54:32 +00001646 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00001647 DeclSpec DS(AttrFactory);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001648 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001649 if (Tok.is(tok::l_paren)) {
1650 // If it turns out to be a placement, we change the type location.
1651 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001652 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1653 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001654 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001655 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001656
1657 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001658 if (PlacementRParen.isInvalid()) {
1659 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001660 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001661 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001662
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001663 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001664 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001665 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001666 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001667 } else {
1668 // We still need the type.
1669 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001670 TypeIdParens.setBegin(ConsumeParen());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001671 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001672 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001673 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001674 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1675 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001676 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001677 if (ParseCXXTypeSpecifierSeq(DS))
1678 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001679 else {
1680 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001681 ParseDeclaratorInternal(DeclaratorInfo,
1682 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001683 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001684 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001685 }
1686 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001687 // A new-type-id is a simplified type-id, where essentially the
1688 // direct-declarator is replaced by a direct-new-declarator.
1689 if (ParseCXXTypeSpecifierSeq(DS))
1690 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001691 else {
1692 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001693 ParseDeclaratorInternal(DeclaratorInfo,
1694 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001695 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001696 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001697 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001698 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001699 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001700 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001701
Sebastian Redla55e52c2008-11-25 22:21:31 +00001702 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001703 SourceLocation ConstructorLParen, ConstructorRParen;
1704
1705 if (Tok.is(tok::l_paren)) {
1706 ConstructorLParen = ConsumeParen();
1707 if (Tok.isNot(tok::r_paren)) {
1708 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001709 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1710 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001711 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001712 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001713 }
1714 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001715 if (ConstructorRParen.isInvalid()) {
1716 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001717 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001718 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001719 }
1720
Sebastian Redlf53597f2009-03-15 17:47:39 +00001721 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1722 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001723 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001724 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001725}
1726
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001727/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1728/// passed to ParseDeclaratorInternal.
1729///
1730/// direct-new-declarator:
1731/// '[' expression ']'
1732/// direct-new-declarator '[' constant-expression ']'
1733///
Chris Lattner59232d32009-01-04 21:25:24 +00001734void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001735 // Parse the array dimensions.
1736 bool first = true;
1737 while (Tok.is(tok::l_square)) {
1738 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001739 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001740 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001741 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001742 // Recover
1743 SkipUntil(tok::r_square);
1744 return;
1745 }
1746 first = false;
1747
Sebastian Redlab197ba2009-02-09 18:23:29 +00001748 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall0b7e6782011-03-24 11:26:52 +00001749
1750 ParsedAttributes attrs(AttrFactory);
1751 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00001752 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001753 Size.release(), LLoc, RLoc),
John McCall0b7e6782011-03-24 11:26:52 +00001754 attrs, RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001755
Sebastian Redlab197ba2009-02-09 18:23:29 +00001756 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001757 return;
1758 }
1759}
1760
1761/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1762/// This ambiguity appears in the syntax of the C++ new operator.
1763///
1764/// new-expression:
1765/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1766/// new-initializer[opt]
1767///
1768/// new-placement:
1769/// '(' expression-list ')'
1770///
John McCallca0408f2010-08-23 06:44:23 +00001771bool Parser::ParseExpressionListOrTypeId(
1772 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001773 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001774 // The '(' was already consumed.
1775 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001776 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001777 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001778 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001779 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001780 }
1781
1782 // It's not a type, it has to be an expression list.
1783 // Discard the comma locations - ActOnCXXNew has enough parameters.
1784 CommaLocsTy CommaLocs;
1785 return ParseExpressionList(PlacementArgs, CommaLocs);
1786}
1787
1788/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1789/// to free memory allocated by new.
1790///
Chris Lattner59232d32009-01-04 21:25:24 +00001791/// This method is called to parse the 'delete' expression after the optional
1792/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1793/// and "Start" is its location. Otherwise, "Start" is the location of the
1794/// 'delete' token.
1795///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001796/// delete-expression:
1797/// '::'[opt] 'delete' cast-expression
1798/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001799ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001800Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1801 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1802 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001803
1804 // Array delete?
1805 bool ArrayDelete = false;
1806 if (Tok.is(tok::l_square)) {
1807 ArrayDelete = true;
1808 SourceLocation LHS = ConsumeBracket();
1809 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1810 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001811 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001812 }
1813
John McCall60d7b3a2010-08-24 06:29:42 +00001814 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001815 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001816 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001817
John McCall9ae2f072010-08-23 23:25:46 +00001818 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001819}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001820
Mike Stump1eb44332009-09-09 15:08:12 +00001821static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001822 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001823 default: llvm_unreachable("Not a known unary type trait");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001824 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1825 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1826 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1827 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1828 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1829 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1830 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1831 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1832 case tok::kw___is_abstract: return UTT_IsAbstract;
1833 case tok::kw___is_class: return UTT_IsClass;
1834 case tok::kw___is_empty: return UTT_IsEmpty;
1835 case tok::kw___is_enum: return UTT_IsEnum;
1836 case tok::kw___is_pod: return UTT_IsPOD;
1837 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1838 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001839 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001840 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001841}
1842
1843static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1844 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001845 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001846 case tok::kw___is_base_of: return BTT_IsBaseOf;
1847 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001848 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001849 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001850}
1851
1852/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1853/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1854/// templates.
1855///
1856/// primary-expression:
1857/// [GNU] unary-type-trait '(' type-id ')'
1858///
John McCall60d7b3a2010-08-24 06:29:42 +00001859ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001860 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1861 SourceLocation Loc = ConsumeToken();
1862
1863 SourceLocation LParen = Tok.getLocation();
1864 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1865 return ExprError();
1866
1867 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1868 // there will be cryptic errors about mismatched parentheses and missing
1869 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001870 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001871
1872 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1873
Douglas Gregor809070a2009-02-18 17:45:20 +00001874 if (Ty.isInvalid())
1875 return ExprError();
1876
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00001877 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001878}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001879
Francois Pichet6ad6f282010-12-07 00:08:36 +00001880/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
1881/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1882/// templates.
1883///
1884/// primary-expression:
1885/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
1886///
1887ExprResult Parser::ParseBinaryTypeTrait() {
1888 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
1889 SourceLocation Loc = ConsumeToken();
1890
1891 SourceLocation LParen = Tok.getLocation();
1892 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1893 return ExprError();
1894
1895 TypeResult LhsTy = ParseTypeName();
1896 if (LhsTy.isInvalid()) {
1897 SkipUntil(tok::r_paren);
1898 return ExprError();
1899 }
1900
1901 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
1902 SkipUntil(tok::r_paren);
1903 return ExprError();
1904 }
1905
1906 TypeResult RhsTy = ParseTypeName();
1907 if (RhsTy.isInvalid()) {
1908 SkipUntil(tok::r_paren);
1909 return ExprError();
1910 }
1911
1912 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1913
1914 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
1915}
1916
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001917/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1918/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1919/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00001920ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001921Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00001922 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001923 SourceLocation LParenLoc,
1924 SourceLocation &RParenLoc) {
1925 assert(getLang().CPlusPlus && "Should only be called for C++!");
1926 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1927 assert(isTypeIdInParens() && "Not a type-id!");
1928
John McCall60d7b3a2010-08-24 06:29:42 +00001929 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00001930 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001931
1932 // We need to disambiguate a very ugly part of the C++ syntax:
1933 //
1934 // (T())x; - type-id
1935 // (T())*x; - type-id
1936 // (T())/x; - expression
1937 // (T()); - expression
1938 //
1939 // The bad news is that we cannot use the specialized tentative parser, since
1940 // it can only verify that the thing inside the parens can be parsed as
1941 // type-id, it is not useful for determining the context past the parens.
1942 //
1943 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001944 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001945 //
1946 // It uses a scheme similar to parsing inline methods. The parenthesized
1947 // tokens are cached, the context that follows is determined (possibly by
1948 // parsing a cast-expression), and then we re-introduce the cached tokens
1949 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001950
Mike Stump1eb44332009-09-09 15:08:12 +00001951 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001952 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001953
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001954 // Store the tokens of the parentheses. We will parse them after we determine
1955 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001956 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001957 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001958 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1959 return ExprError();
1960 }
1961
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001962 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001963 ParseAs = CompoundLiteral;
1964 } else {
1965 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001966 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1967 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1968 NotCastExpr = true;
1969 } else {
1970 // Try parsing the cast-expression that may follow.
1971 // If it is not a cast-expression, NotCastExpr will be true and no token
1972 // will be consumed.
1973 Result = ParseCastExpression(false/*isUnaryExpression*/,
1974 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00001975 NotCastExpr,
1976 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001977 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001978
1979 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1980 // an expression.
1981 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001982 }
1983
Mike Stump1eb44332009-09-09 15:08:12 +00001984 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001985 Toks.push_back(Tok);
1986 // Re-enter the stored parenthesized tokens into the token stream, so we may
1987 // parse them now.
1988 PP.EnterTokenStream(Toks.data(), Toks.size(),
1989 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1990 // Drop the current token and bring the first cached one. It's the same token
1991 // as when we entered this function.
1992 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001993
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001994 if (ParseAs >= CompoundLiteral) {
1995 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001996
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001997 // Match the ')'.
1998 if (Tok.is(tok::r_paren))
1999 RParenLoc = ConsumeParen();
2000 else
2001 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002002
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002003 if (ParseAs == CompoundLiteral) {
2004 ExprType = CompoundLiteral;
2005 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2006 }
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002008 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2009 assert(ParseAs == CastExpr);
2010
2011 if (Ty.isInvalid())
2012 return ExprError();
2013
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002014 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002015
2016 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002017 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002018 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002019 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002020 return move(Result);
2021 }
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002023 // Not a compound literal, and not followed by a cast-expression.
2024 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002025
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002026 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002027 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002028 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002029 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002030
2031 // Match the ')'.
2032 if (Result.isInvalid()) {
2033 SkipUntil(tok::r_paren);
2034 return ExprError();
2035 }
Mike Stump1eb44332009-09-09 15:08:12 +00002036
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002037 if (Tok.is(tok::r_paren))
2038 RParenLoc = ConsumeParen();
2039 else
2040 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2041
2042 return move(Result);
2043}