blob: e73578f23e36c5b7052e208d39a8e43b51284f00 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000019#include "llvm/Support/ErrorHandling.h"
20
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Mike Stump1eb44332009-09-09 15:08:12 +000023/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000024///
25/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000026/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000027/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000028///
29/// '::'[opt] nested-name-specifier
30/// '::'
31///
32/// nested-name-specifier:
33/// type-name '::'
34/// namespace-name '::'
35/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000036/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000037///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000038///
Mike Stump1eb44332009-09-09 15:08:12 +000039/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000040/// nested-name-specifier (or empty)
41///
Mike Stump1eb44332009-09-09 15:08:12 +000042/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000043/// the "." or "->" of a member access expression, this parameter provides the
44/// type of the object whose members are being accessed.
45///
46/// \param EnteringContext whether we will be entering into the context of
47/// the nested-name-specifier after parsing it.
48///
Douglas Gregord4dca082010-02-24 18:44:31 +000049/// \param MayBePseudoDestructor When non-NULL, points to a flag that
50/// indicates whether this nested-name-specifier may be part of a
51/// pseudo-destructor name. In this case, the flag will be set false
52/// if we don't actually end up parsing a destructor name. Moreorover,
53/// if we do end up determining that we are parsing a destructor name,
54/// the last component of the nested-name-specifier is not parsed as
55/// part of the scope specifier.
56
Douglas Gregorb10cd042010-02-21 18:36:56 +000057/// member access expression, e.g., the \p T:: in \p p->T::m.
58///
John McCall9ba61662010-02-26 08:45:28 +000059/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +000060bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +000061 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +000062 bool EnteringContext,
Douglas Gregord4dca082010-02-24 18:44:31 +000063 bool *MayBePseudoDestructor) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000064 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000065 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000066
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000067 if (Tok.is(tok::annot_cxxscope)) {
John McCallca0408f2010-08-23 06:44:23 +000068 SS.setScopeRep(static_cast<NestedNameSpecifier*>(Tok.getAnnotationValue()));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000069 SS.setRange(Tok.getAnnotationRange());
70 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +000071 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000072 }
Chris Lattnere607e802009-01-04 21:14:15 +000073
Douglas Gregor39a8de12009-02-25 19:37:18 +000074 bool HasScopeSpecifier = false;
75
Chris Lattner5b454732009-01-05 03:55:46 +000076 if (Tok.is(tok::coloncolon)) {
77 // ::new and ::delete aren't nested-name-specifiers.
78 tok::TokenKind NextKind = NextToken().getKind();
79 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
80 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner55a7cef2009-01-05 00:13:00 +000082 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000083 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000084 SS.setBeginLoc(CCLoc);
Douglas Gregor23c94db2010-07-02 17:43:08 +000085 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000086 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000087 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000088 }
89
Douglas Gregord4dca082010-02-24 18:44:31 +000090 bool CheckForDestructor = false;
91 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
92 CheckForDestructor = true;
93 *MayBePseudoDestructor = false;
94 }
95
Douglas Gregor39a8de12009-02-25 19:37:18 +000096 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000097 if (HasScopeSpecifier) {
98 // C++ [basic.lookup.classref]p5:
99 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000100 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000101 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000102 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000103 // the class-name-or-namespace-name is looked up in global scope as a
104 // class-name or namespace-name.
105 //
106 // To implement this, we clear out the object type as soon as we've
107 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000108 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000109
110 if (Tok.is(tok::code_completion)) {
111 // Code completion for a nested-name-specifier, where the code
112 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000113 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Douglas Gregordc845342010-05-25 05:58:43 +0000114 ConsumeCodeCompletionToken();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000115 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor39a8de12009-02-25 19:37:18 +0000118 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000119 // nested-name-specifier 'template'[opt] simple-template-id '::'
120
121 // Parse the optional 'template' keyword, then make sure we have
122 // 'identifier <' after it.
123 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000124 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000125 // nested-name-specifier, since they aren't allowed to start with
126 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000127 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000128 break;
129
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000130 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000131 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000132
133 UnqualifiedId TemplateName;
134 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000135 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000136 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000137 ConsumeToken();
138 } else if (Tok.is(tok::kw_operator)) {
139 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000140 TemplateName)) {
141 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000142 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000143 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000144
Sean Hunte6252d12009-11-28 08:58:14 +0000145 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
146 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000147 Diag(TemplateName.getSourceRange().getBegin(),
148 diag::err_id_after_template_in_nested_name_spec)
149 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000150 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000151 break;
152 }
153 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000154 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000155 break;
156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000158 // If the next token is not '<', we have a qualified-id that refers
159 // to a template name, such as T::template apply, but is not a
160 // template-id.
161 if (Tok.isNot(tok::less)) {
162 TPA.Revert();
163 break;
164 }
165
166 // Commit to parsing the template-id.
167 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000168 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000169 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000170 TemplateKWLoc,
171 SS,
172 TemplateName,
173 ObjectType,
174 EnteringContext,
175 Template)) {
176 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
177 TemplateKWLoc, false))
178 return true;
179 } else
John McCall9ba61662010-02-26 08:45:28 +0000180 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Chris Lattner77cf72a2009-06-26 03:47:46 +0000182 continue;
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Douglas Gregor39a8de12009-02-25 19:37:18 +0000185 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000186 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000187 //
188 // simple-template-id '::'
189 //
190 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000191 // right kind (it should name a type or be dependent), and then
192 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000193 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000194 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord4dca082010-02-24 18:44:31 +0000195 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
196 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000197 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000198 }
199
Mike Stump1eb44332009-09-09 15:08:12 +0000200 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000201 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000202 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000203
Mike Stump1eb44332009-09-09 15:08:12 +0000204 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000205 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000206 Token TypeToken = Tok;
207 ConsumeToken();
208 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
209 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Douglas Gregor39a8de12009-02-25 19:37:18 +0000211 if (!HasScopeSpecifier) {
212 SS.setBeginLoc(TypeToken.getLocation());
213 HasScopeSpecifier = true;
214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
John McCallb3d87482010-08-24 05:47:05 +0000216 if (ParsedType T = getTypeAnnotation(TypeToken)) {
217 CXXScopeTy *Scope =
218 Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, T,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000219 TypeToken.getAnnotationRange(),
John McCallb3d87482010-08-24 05:47:05 +0000220 CCLoc);
221 SS.setScopeRep(Scope);
222 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +0000223 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000224 SS.setEndLoc(CCLoc);
225 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000226 }
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattner67b9e832009-06-26 03:45:46 +0000228 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000229 }
230
Chris Lattner5c7f7862009-06-26 03:52:38 +0000231
232 // The rest of the nested-name-specifier possibilities start with
233 // tok::identifier.
234 if (Tok.isNot(tok::identifier))
235 break;
236
237 IdentifierInfo &II = *Tok.getIdentifierInfo();
238
239 // nested-name-specifier:
240 // type-name '::'
241 // namespace-name '::'
242 // nested-name-specifier identifier '::'
243 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000244
245 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
246 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000247 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000248 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II, ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000249 EnteringContext) &&
250 // If the token after the colon isn't an identifier, it's still an
251 // error, but they probably meant something else strange so don't
252 // recover like this.
253 PP.LookAhead(1).is(tok::identifier)) {
254 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000255 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000256
257 // Recover as if the user wrote '::'.
258 Next.setKind(tok::coloncolon);
259 }
Chris Lattner46646492009-12-07 01:36:53 +0000260 }
261
Chris Lattner5c7f7862009-06-26 03:52:38 +0000262 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000263 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000265 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000266 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000267 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000268 }
269
Chris Lattner5c7f7862009-06-26 03:52:38 +0000270 // We have an identifier followed by a '::'. Lookup this name
271 // as the name in a nested-name-specifier.
272 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000273 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
274 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000275 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattner5c7f7862009-06-26 03:52:38 +0000277 if (!HasScopeSpecifier) {
278 SS.setBeginLoc(IdLoc);
279 HasScopeSpecifier = true;
280 }
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Argyrios Kyrtzidis661c36b2010-06-22 11:30:04 +0000282 if (!SS.isInvalid())
283 SS.setScopeRep(
Douglas Gregor23c94db2010-07-02 17:43:08 +0000284 Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, IdLoc, CCLoc, II,
Argyrios Kyrtzidis661c36b2010-06-22 11:30:04 +0000285 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000286 SS.setEndLoc(CCLoc);
287 continue;
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattner5c7f7862009-06-26 03:52:38 +0000290 // nested-name-specifier:
291 // type-name '<'
292 if (Next.is(tok::less)) {
293 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000294 UnqualifiedId TemplateName;
295 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000296 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000297 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000298 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000299 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000300 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000301 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000302 Template,
303 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000304 // We have found a template name, so annotate this this token
305 // with a template-id annotation. We do not permit the
306 // template-id to be translated into a type annotation,
307 // because some clients (e.g., the parsing of class template
308 // specializations) still want to see the original template-id
309 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000310 ConsumeToken();
311 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
312 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000313 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000314 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000315 }
316
317 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
318 IsTemplateArgumentList(1)) {
319 // We have something like t::getAs<T>, where getAs is a
320 // member of an unknown specialization. However, this will only
321 // parse correctly as a template, so suggest the keyword 'template'
322 // before 'getAs' and treat this as a dependent template name.
323 Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
324 << II.getName()
325 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
326
Douglas Gregord6ab2322010-06-16 23:00:59 +0000327 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000328 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000329 Tok.getLocation(), SS,
330 TemplateName, ObjectType,
331 EnteringContext, Template)) {
332 // Consume the identifier.
333 ConsumeToken();
334 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
335 SourceLocation(), false))
336 return true;
337 }
338 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000339 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000340
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000341 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000342 }
343 }
344
Douglas Gregor39a8de12009-02-25 19:37:18 +0000345 // We don't have any tokens that form the beginning of a
346 // nested-name-specifier, so we're done.
347 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Douglas Gregord4dca082010-02-24 18:44:31 +0000350 // Even if we didn't see any pieces of a nested-name-specifier, we
351 // still check whether there is a tilde in this position, which
352 // indicates a potential pseudo-destructor.
353 if (CheckForDestructor && Tok.is(tok::tilde))
354 *MayBePseudoDestructor = true;
355
John McCall9ba61662010-02-26 08:45:28 +0000356 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000357}
358
359/// ParseCXXIdExpression - Handle id-expression.
360///
361/// id-expression:
362/// unqualified-id
363/// qualified-id
364///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000365/// qualified-id:
366/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
367/// '::' identifier
368/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000369/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000370///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000371/// NOTE: The standard specifies that, for qualified-id, the parser does not
372/// expect:
373///
374/// '::' conversion-function-id
375/// '::' '~' class-name
376///
377/// This may cause a slight inconsistency on diagnostics:
378///
379/// class C {};
380/// namespace A {}
381/// void f() {
382/// :: A :: ~ C(); // Some Sema error about using destructor with a
383/// // namespace.
384/// :: ~ C(); // Some Parser error like 'unexpected ~'.
385/// }
386///
387/// We simplify the parser a bit and make it work like:
388///
389/// qualified-id:
390/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
391/// '::' unqualified-id
392///
393/// That way Sema can handle and report similar errors for namespaces and the
394/// global scope.
395///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000396/// The isAddressOfOperand parameter indicates that this id-expression is a
397/// direct operand of the address-of operator. This is, besides member contexts,
398/// the only place where a qualified-id naming a non-static class member may
399/// appear.
400///
John McCall60d7b3a2010-08-24 06:29:42 +0000401ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000402 // qualified-id:
403 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
404 // '::' unqualified-id
405 //
406 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000407 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000408
409 UnqualifiedId Name;
410 if (ParseUnqualifiedId(SS,
411 /*EnteringContext=*/false,
412 /*AllowDestructorName=*/false,
413 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000414 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000415 Name))
416 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000417
418 // This is only the direct operand of an & operator if it is not
419 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000420 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
421 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000422
Douglas Gregor23c94db2010-07-02 17:43:08 +0000423 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000424 isAddressOfOperand);
425
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000426}
427
Reid Spencer5f016e22007-07-11 17:01:13 +0000428/// ParseCXXCasts - This handles the various ways to cast expressions to another
429/// type.
430///
431/// postfix-expression: [C++ 5.2p1]
432/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
433/// 'static_cast' '<' type-name '>' '(' expression ')'
434/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
435/// 'const_cast' '<' type-name '>' '(' expression ')'
436///
John McCall60d7b3a2010-08-24 06:29:42 +0000437ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 tok::TokenKind Kind = Tok.getKind();
439 const char *CastName = 0; // For error messages
440
441 switch (Kind) {
442 default: assert(0 && "Unknown C++ cast!"); abort();
443 case tok::kw_const_cast: CastName = "const_cast"; break;
444 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
445 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
446 case tok::kw_static_cast: CastName = "static_cast"; break;
447 }
448
449 SourceLocation OpLoc = ConsumeToken();
450 SourceLocation LAngleBracketLoc = Tok.getLocation();
451
452 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000453 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000454
Douglas Gregor809070a2009-02-18 17:45:20 +0000455 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 SourceLocation RAngleBracketLoc = Tok.getLocation();
457
Chris Lattner1ab3b962008-11-18 07:48:38 +0000458 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000459 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000460
461 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
462
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000463 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
464 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000465
John McCall60d7b3a2010-08-24 06:29:42 +0000466 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000468 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000469 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000470
Douglas Gregor809070a2009-02-18 17:45:20 +0000471 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000472 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000473 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000474 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000475 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000476
Sebastian Redl20df9b72008-12-11 22:51:44 +0000477 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000478}
479
Sebastian Redlc42e1182008-11-11 11:37:55 +0000480/// ParseCXXTypeid - This handles the C++ typeid expression.
481///
482/// postfix-expression: [C++ 5.2p1]
483/// 'typeid' '(' expression ')'
484/// 'typeid' '(' type-id ')'
485///
John McCall60d7b3a2010-08-24 06:29:42 +0000486ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000487 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
488
489 SourceLocation OpLoc = ConsumeToken();
490 SourceLocation LParenLoc = Tok.getLocation();
491 SourceLocation RParenLoc;
492
493 // typeid expressions are always parenthesized.
494 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
495 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000496 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000497
John McCall60d7b3a2010-08-24 06:29:42 +0000498 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000499
500 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000501 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000502
503 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000504 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000505
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000506 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000507 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000508
509 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000510 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000511 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000512 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000513 // When typeid is applied to an expression other than an lvalue of a
514 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000515 // operand (Clause 5).
516 //
Mike Stump1eb44332009-09-09 15:08:12 +0000517 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000518 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000519 // we the expression is potentially potentially evaluated.
520 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000521 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000522 Result = ParseExpression();
523
524 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000525 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000526 SkipUntil(tok::r_paren);
527 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000528 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
529 if (RParenLoc.isInvalid())
530 return ExprError();
531
Sebastian Redlc42e1182008-11-11 11:37:55 +0000532 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000533 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000534 }
535 }
536
Sebastian Redl20df9b72008-12-11 22:51:44 +0000537 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000538}
539
Francois Pichet01b7c302010-09-08 12:20:18 +0000540/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
541///
542/// '__uuidof' '(' expression ')'
543/// '__uuidof' '(' type-id ')'
544///
545ExprResult Parser::ParseCXXUuidof() {
546 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
547
548 SourceLocation OpLoc = ConsumeToken();
549 SourceLocation LParenLoc = Tok.getLocation();
550 SourceLocation RParenLoc;
551
552 // __uuidof expressions are always parenthesized.
553 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
554 "__uuidof"))
555 return ExprError();
556
557 ExprResult Result;
558
559 if (isTypeIdInParens()) {
560 TypeResult Ty = ParseTypeName();
561
562 // Match the ')'.
563 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
564
565 if (Ty.isInvalid())
566 return ExprError();
567
568 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
569 Ty.get().getAsOpaquePtr(), RParenLoc);
570 } else {
571 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
572 Result = ParseExpression();
573
574 // Match the ')'.
575 if (Result.isInvalid())
576 SkipUntil(tok::r_paren);
577 else {
578 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
579
580 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
581 Result.release(), RParenLoc);
582 }
583 }
584
585 return move(Result);
586}
587
Douglas Gregord4dca082010-02-24 18:44:31 +0000588/// \brief Parse a C++ pseudo-destructor expression after the base,
589/// . or -> operator, and nested-name-specifier have already been
590/// parsed.
591///
592/// postfix-expression: [C++ 5.2]
593/// postfix-expression . pseudo-destructor-name
594/// postfix-expression -> pseudo-destructor-name
595///
596/// pseudo-destructor-name:
597/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
598/// ::[opt] nested-name-specifier template simple-template-id ::
599/// ~type-name
600/// ::[opt] nested-name-specifier[opt] ~type-name
601///
John McCall60d7b3a2010-08-24 06:29:42 +0000602ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000603Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
604 tok::TokenKind OpKind,
605 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000606 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000607 // We're parsing either a pseudo-destructor-name or a dependent
608 // member access that has the same form as a
609 // pseudo-destructor-name. We parse both in the same way and let
610 // the action model sort them out.
611 //
612 // Note that the ::[opt] nested-name-specifier[opt] has already
613 // been parsed, and if there was a simple-template-id, it has
614 // been coalesced into a template-id annotation token.
615 UnqualifiedId FirstTypeName;
616 SourceLocation CCLoc;
617 if (Tok.is(tok::identifier)) {
618 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
619 ConsumeToken();
620 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
621 CCLoc = ConsumeToken();
622 } else if (Tok.is(tok::annot_template_id)) {
623 FirstTypeName.setTemplateId(
624 (TemplateIdAnnotation *)Tok.getAnnotationValue());
625 ConsumeToken();
626 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
627 CCLoc = ConsumeToken();
628 } else {
629 FirstTypeName.setIdentifier(0, SourceLocation());
630 }
631
632 // Parse the tilde.
633 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
634 SourceLocation TildeLoc = ConsumeToken();
635 if (!Tok.is(tok::identifier)) {
636 Diag(Tok, diag::err_destructor_tilde_identifier);
637 return ExprError();
638 }
639
640 // Parse the second type.
641 UnqualifiedId SecondTypeName;
642 IdentifierInfo *Name = Tok.getIdentifierInfo();
643 SourceLocation NameLoc = ConsumeToken();
644 SecondTypeName.setIdentifier(Name, NameLoc);
645
646 // If there is a '<', the second type name is a template-id. Parse
647 // it as such.
648 if (Tok.is(tok::less) &&
649 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000650 SecondTypeName, /*AssumeTemplateName=*/true,
651 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000652 return ExprError();
653
John McCall9ae2f072010-08-23 23:25:46 +0000654 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
655 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000656 SS, FirstTypeName, CCLoc,
657 TildeLoc, SecondTypeName,
658 Tok.is(tok::l_paren));
659}
660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
662///
663/// boolean-literal: [C++ 2.13.5]
664/// 'true'
665/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000666ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000668 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000669}
Chris Lattner50dd2892008-02-26 00:51:44 +0000670
671/// ParseThrowExpression - This handles the C++ throw expression.
672///
673/// throw-expression: [C++ 15]
674/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000675ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000676 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000677 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000678
Chris Lattner2a2819a2008-04-06 06:02:23 +0000679 // If the current token isn't the start of an assignment-expression,
680 // then the expression is not present. This handles things like:
681 // "C ? throw : (void)42", which is crazy but legal.
682 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
683 case tok::semi:
684 case tok::r_paren:
685 case tok::r_square:
686 case tok::r_brace:
687 case tok::colon:
688 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000689 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000690
Chris Lattner2a2819a2008-04-06 06:02:23 +0000691 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000692 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000693 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000694 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000695 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000696}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000697
698/// ParseCXXThis - This handles the C++ 'this' pointer.
699///
700/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
701/// a non-lvalue expression whose value is the address of the object for which
702/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000703ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000704 assert(Tok.is(tok::kw_this) && "Not 'this'!");
705 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000706 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000707}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000708
709/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
710/// Can be interpreted either as function-style casting ("int(x)")
711/// or class type construction ("ClassType(x,y,z)")
712/// or creation of a value-initialized type ("int()").
713///
714/// postfix-expression: [C++ 5.2p1]
715/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
716/// typename-specifier '(' expression-list[opt] ')' [TODO]
717///
John McCall60d7b3a2010-08-24 06:29:42 +0000718ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000719Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000720 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000721 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000722
723 assert(Tok.is(tok::l_paren) && "Expected '('!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000724 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
725
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000726 SourceLocation LParenLoc = ConsumeParen();
727
Sebastian Redla55e52c2008-11-25 22:21:31 +0000728 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000729 CommaLocsTy CommaLocs;
730
731 if (Tok.isNot(tok::r_paren)) {
732 if (ParseExpressionList(Exprs, CommaLocs)) {
733 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000734 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000735 }
736 }
737
738 // Match the ')'.
739 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
740
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000741 // TypeRep could be null, if it references an invalid typedef.
742 if (!TypeRep)
743 return ExprError();
744
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000745 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
746 "Unexpected number of commas!");
Douglas Gregorab6677e2010-09-08 00:15:04 +0000747 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000748 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000749}
750
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000751/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000752///
753/// condition:
754/// expression
755/// type-specifier-seq declarator '=' assignment-expression
756/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
757/// '=' assignment-expression
758///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000759/// \param ExprResult if the condition was parsed as an expression, the
760/// parsed expression.
761///
762/// \param DeclResult if the condition was parsed as a declaration, the
763/// parsed declaration.
764///
Douglas Gregor586596f2010-05-06 17:25:47 +0000765/// \param Loc The location of the start of the statement that requires this
766/// condition, e.g., the "for" in a for loop.
767///
768/// \param ConvertToBoolean Whether the condition expression should be
769/// converted to a boolean value.
770///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000771/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000772bool Parser::ParseCXXCondition(ExprResult &ExprOut,
773 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000774 SourceLocation Loc,
775 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000776 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000777 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000778 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000779 }
780
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000781 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000782 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000783 ExprOut = ParseExpression(); // expression
784 DeclOut = 0;
785 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000786 return true;
787
788 // If required, convert to a boolean value.
789 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000790 ExprOut
791 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
792 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000793 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000794
795 // type-specifier-seq
796 DeclSpec DS;
797 ParseSpecifierQualifierList(DS);
798
799 // declarator
800 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
801 ParseDeclarator(DeclaratorInfo);
802
803 // simple-asm-expr[opt]
804 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000805 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000806 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000807 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000808 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000809 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000810 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000811 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000812 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000813 }
814
815 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000816 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000817
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000818 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000819 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000820 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000821 DeclOut = Dcl.get();
822 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000823
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000824 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000825 if (isTokenEqualOrMistypedEqualEqual(
826 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000827 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000828 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000829 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000830 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
831 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000832 } else {
833 // FIXME: C++0x allows a braced-init-list
834 Diag(Tok, diag::err_expected_equal_after_declarator);
835 }
836
Douglas Gregor586596f2010-05-06 17:25:47 +0000837 // FIXME: Build a reference to this declaration? Convert it to bool?
838 // (This is currently handled by Sema).
839
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000840 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000841}
842
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000843/// \brief Determine whether the current token starts a C++
844/// simple-type-specifier.
845bool Parser::isCXXSimpleTypeSpecifier() const {
846 switch (Tok.getKind()) {
847 case tok::annot_typename:
848 case tok::kw_short:
849 case tok::kw_long:
850 case tok::kw_signed:
851 case tok::kw_unsigned:
852 case tok::kw_void:
853 case tok::kw_char:
854 case tok::kw_int:
855 case tok::kw_float:
856 case tok::kw_double:
857 case tok::kw_wchar_t:
858 case tok::kw_char16_t:
859 case tok::kw_char32_t:
860 case tok::kw_bool:
861 // FIXME: C++0x decltype support.
862 // GNU typeof support.
863 case tok::kw_typeof:
864 return true;
865
866 default:
867 break;
868 }
869
870 return false;
871}
872
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000873/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
874/// This should only be called when the current token is known to be part of
875/// simple-type-specifier.
876///
877/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000878/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000879/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
880/// char
881/// wchar_t
882/// bool
883/// short
884/// int
885/// long
886/// signed
887/// unsigned
888/// float
889/// double
890/// void
891/// [GNU] typeof-specifier
892/// [C++0x] auto [TODO]
893///
894/// type-name:
895/// class-name
896/// enum-name
897/// typedef-name
898///
899void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
900 DS.SetRangeStart(Tok.getLocation());
901 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000902 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000903 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000905 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000906 case tok::identifier: // foo::bar
907 case tok::coloncolon: // ::foo::bar
908 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000909 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000910 assert(0 && "Not a simple-type-specifier token!");
911 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000912
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000913 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000914 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000915 if (getTypeAnnotation(Tok))
916 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
917 getTypeAnnotation(Tok));
918 else
919 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000920
921 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
922 ConsumeToken();
923
924 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
925 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
926 // Objective-C interface. If we don't have Objective-C or a '<', this is
927 // just a normal reference to a typedef name.
928 if (Tok.is(tok::less) && getLang().ObjC1)
929 ParseObjCProtocolQualifiers(DS);
930
931 DS.Finish(Diags, PP);
932 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000933 }
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000935 // builtin types
936 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000937 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000938 break;
939 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000940 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000941 break;
942 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000943 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000944 break;
945 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000946 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000947 break;
948 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000949 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000950 break;
951 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000952 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000953 break;
954 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000955 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000956 break;
957 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000958 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000959 break;
960 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000961 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000962 break;
963 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000964 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000965 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000966 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000967 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000968 break;
969 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000970 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000971 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000972 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000973 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000974 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000976 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000977 // GNU typeof support.
978 case tok::kw_typeof:
979 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000980 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000981 return;
982 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000983 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000984 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
985 else
986 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000987 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000988 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000989}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000990
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000991/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
992/// [dcl.name]), which is a non-empty sequence of type-specifiers,
993/// e.g., "const short int". Note that the DeclSpec is *not* finished
994/// by parsing the type-specifier-seq, because these sequences are
995/// typically followed by some form of declarator. Returns true and
996/// emits diagnostics if this is not a type-specifier-seq, false
997/// otherwise.
998///
999/// type-specifier-seq: [C++ 8.1]
1000/// type-specifier type-specifier-seq[opt]
1001///
1002bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1003 DS.SetRangeStart(Tok.getLocation());
1004 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001005 unsigned DiagID;
1006 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001007
1008 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001009 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1010 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001011 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001012 return true;
1013 }
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Sebastian Redld9bafa72010-02-03 21:21:43 +00001015 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1016 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1017 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001018
Douglas Gregor396a9f22010-02-24 23:13:13 +00001019 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001020 return false;
1021}
1022
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001023/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1024/// some form.
1025///
1026/// This routine is invoked when a '<' is encountered after an identifier or
1027/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1028/// whether the unqualified-id is actually a template-id. This routine will
1029/// then parse the template arguments and form the appropriate template-id to
1030/// return to the caller.
1031///
1032/// \param SS the nested-name-specifier that precedes this template-id, if
1033/// we're actually parsing a qualified-id.
1034///
1035/// \param Name for constructor and destructor names, this is the actual
1036/// identifier that may be a template-name.
1037///
1038/// \param NameLoc the location of the class-name in a constructor or
1039/// destructor.
1040///
1041/// \param EnteringContext whether we're entering the scope of the
1042/// nested-name-specifier.
1043///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001044/// \param ObjectType if this unqualified-id occurs within a member access
1045/// expression, the type of the base object whose member is being accessed.
1046///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001047/// \param Id as input, describes the template-name or operator-function-id
1048/// that precedes the '<'. If template arguments were parsed successfully,
1049/// will be updated with the template-id.
1050///
Douglas Gregord4dca082010-02-24 18:44:31 +00001051/// \param AssumeTemplateId When true, this routine will assume that the name
1052/// refers to a template without performing name lookup to verify.
1053///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001054/// \returns true if a parse error occurred, false otherwise.
1055bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1056 IdentifierInfo *Name,
1057 SourceLocation NameLoc,
1058 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001059 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001060 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001061 bool AssumeTemplateId,
1062 SourceLocation TemplateKWLoc) {
1063 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1064 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001065
1066 TemplateTy Template;
1067 TemplateNameKind TNK = TNK_Non_template;
1068 switch (Id.getKind()) {
1069 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001070 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001071 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001072 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001073 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001074 Id, ObjectType, EnteringContext,
1075 Template);
1076 if (TNK == TNK_Non_template)
1077 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001078 } else {
1079 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001080 TNK = Actions.isTemplateName(getCurScope(), SS,
1081 TemplateKWLoc.isValid(), Id,
1082 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001083 MemberOfUnknownSpecialization);
1084
1085 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1086 ObjectType && IsTemplateArgumentList()) {
1087 // We have something like t->getAs<T>(), where getAs is a
1088 // member of an unknown specialization. However, this will only
1089 // parse correctly as a template, so suggest the keyword 'template'
1090 // before 'getAs' and treat this as a dependent template name.
1091 std::string Name;
1092 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1093 Name = Id.Identifier->getName();
1094 else {
1095 Name = "operator ";
1096 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1097 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1098 else
1099 Name += Id.Identifier->getName();
1100 }
1101 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1102 << Name
1103 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001104 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001105 SS, Id, ObjectType,
1106 EnteringContext, Template);
1107 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001108 return true;
1109 }
1110 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001111 break;
1112
Douglas Gregor014e88d2009-11-03 23:16:33 +00001113 case UnqualifiedId::IK_ConstructorName: {
1114 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001115 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001116 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001117 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1118 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001119 EnteringContext, Template,
1120 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001121 break;
1122 }
1123
Douglas Gregor014e88d2009-11-03 23:16:33 +00001124 case UnqualifiedId::IK_DestructorName: {
1125 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001126 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001127 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001128 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001129 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001130 TemplateName, ObjectType,
1131 EnteringContext, Template);
1132 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001133 return true;
1134 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001135 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1136 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001137 EnteringContext, Template,
1138 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001139
John McCallb3d87482010-08-24 05:47:05 +00001140 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001141 Diag(NameLoc, diag::err_destructor_template_id)
1142 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001143 return true;
1144 }
1145 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001146 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001147 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001148
1149 default:
1150 return false;
1151 }
1152
1153 if (TNK == TNK_Non_template)
1154 return false;
1155
1156 // Parse the enclosed template argument list.
1157 SourceLocation LAngleLoc, RAngleLoc;
1158 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001159 if (Tok.is(tok::less) &&
1160 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001161 &SS, true, LAngleLoc,
1162 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001163 RAngleLoc))
1164 return true;
1165
1166 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001167 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1168 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001169 // Form a parsed representation of the template-id to be stored in the
1170 // UnqualifiedId.
1171 TemplateIdAnnotation *TemplateId
1172 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1173
1174 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1175 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001176 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001177 TemplateId->TemplateNameLoc = Id.StartLocation;
1178 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001179 TemplateId->Name = 0;
1180 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1181 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001182 }
1183
John McCall2b5289b2010-08-23 07:28:44 +00001184 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001185 TemplateId->Kind = TNK;
1186 TemplateId->LAngleLoc = LAngleLoc;
1187 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001188 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001189 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001190 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001191 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001192
1193 Id.setTemplateId(TemplateId);
1194 return false;
1195 }
1196
1197 // Bundle the template arguments together.
1198 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001199 TemplateArgs.size());
1200
1201 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001202 TypeResult Type
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001203 = Actions.ActOnTemplateIdType(Template, NameLoc,
1204 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001205 RAngleLoc);
1206 if (Type.isInvalid())
1207 return true;
1208
1209 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1210 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1211 else
1212 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1213
1214 return false;
1215}
1216
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001217/// \brief Parse an operator-function-id or conversion-function-id as part
1218/// of a C++ unqualified-id.
1219///
1220/// This routine is responsible only for parsing the operator-function-id or
1221/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001222///
1223/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001224/// operator-function-id: [C++ 13.5]
1225/// 'operator' operator
1226///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001227/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001228/// new delete new[] delete[]
1229/// + - * / % ^ & | ~
1230/// ! = < > += -= *= /= %=
1231/// ^= &= |= << >> >>= <<= == !=
1232/// <= >= && || ++ -- , ->* ->
1233/// () []
1234///
1235/// conversion-function-id: [C++ 12.3.2]
1236/// operator conversion-type-id
1237///
1238/// conversion-type-id:
1239/// type-specifier-seq conversion-declarator[opt]
1240///
1241/// conversion-declarator:
1242/// ptr-operator conversion-declarator[opt]
1243/// \endcode
1244///
1245/// \param The nested-name-specifier that preceded this unqualified-id. If
1246/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1247///
1248/// \param EnteringContext whether we are entering the scope of the
1249/// nested-name-specifier.
1250///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001251/// \param ObjectType if this unqualified-id occurs within a member access
1252/// expression, the type of the base object whose member is being accessed.
1253///
1254/// \param Result on a successful parse, contains the parsed unqualified-id.
1255///
1256/// \returns true if parsing fails, false otherwise.
1257bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001258 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001259 UnqualifiedId &Result) {
1260 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1261
1262 // Consume the 'operator' keyword.
1263 SourceLocation KeywordLoc = ConsumeToken();
1264
1265 // Determine what kind of operator name we have.
1266 unsigned SymbolIdx = 0;
1267 SourceLocation SymbolLocations[3];
1268 OverloadedOperatorKind Op = OO_None;
1269 switch (Tok.getKind()) {
1270 case tok::kw_new:
1271 case tok::kw_delete: {
1272 bool isNew = Tok.getKind() == tok::kw_new;
1273 // Consume the 'new' or 'delete'.
1274 SymbolLocations[SymbolIdx++] = ConsumeToken();
1275 if (Tok.is(tok::l_square)) {
1276 // Consume the '['.
1277 SourceLocation LBracketLoc = ConsumeBracket();
1278 // Consume the ']'.
1279 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1280 LBracketLoc);
1281 if (RBracketLoc.isInvalid())
1282 return true;
1283
1284 SymbolLocations[SymbolIdx++] = LBracketLoc;
1285 SymbolLocations[SymbolIdx++] = RBracketLoc;
1286 Op = isNew? OO_Array_New : OO_Array_Delete;
1287 } else {
1288 Op = isNew? OO_New : OO_Delete;
1289 }
1290 break;
1291 }
1292
1293#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1294 case tok::Token: \
1295 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1296 Op = OO_##Name; \
1297 break;
1298#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1299#include "clang/Basic/OperatorKinds.def"
1300
1301 case tok::l_paren: {
1302 // Consume the '('.
1303 SourceLocation LParenLoc = ConsumeParen();
1304 // Consume the ')'.
1305 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1306 LParenLoc);
1307 if (RParenLoc.isInvalid())
1308 return true;
1309
1310 SymbolLocations[SymbolIdx++] = LParenLoc;
1311 SymbolLocations[SymbolIdx++] = RParenLoc;
1312 Op = OO_Call;
1313 break;
1314 }
1315
1316 case tok::l_square: {
1317 // Consume the '['.
1318 SourceLocation LBracketLoc = ConsumeBracket();
1319 // Consume the ']'.
1320 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1321 LBracketLoc);
1322 if (RBracketLoc.isInvalid())
1323 return true;
1324
1325 SymbolLocations[SymbolIdx++] = LBracketLoc;
1326 SymbolLocations[SymbolIdx++] = RBracketLoc;
1327 Op = OO_Subscript;
1328 break;
1329 }
1330
1331 case tok::code_completion: {
1332 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001333 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001334
1335 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001336 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001337
1338 // Don't try to parse any further.
1339 return true;
1340 }
1341
1342 default:
1343 break;
1344 }
1345
1346 if (Op != OO_None) {
1347 // We have parsed an operator-function-id.
1348 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1349 return false;
1350 }
Sean Hunt0486d742009-11-28 04:44:28 +00001351
1352 // Parse a literal-operator-id.
1353 //
1354 // literal-operator-id: [C++0x 13.5.8]
1355 // operator "" identifier
1356
1357 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1358 if (Tok.getLength() != 2)
1359 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1360 ConsumeStringToken();
1361
1362 if (Tok.isNot(tok::identifier)) {
1363 Diag(Tok.getLocation(), diag::err_expected_ident);
1364 return true;
1365 }
1366
1367 IdentifierInfo *II = Tok.getIdentifierInfo();
1368 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001369 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001370 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001371
1372 // Parse a conversion-function-id.
1373 //
1374 // conversion-function-id: [C++ 12.3.2]
1375 // operator conversion-type-id
1376 //
1377 // conversion-type-id:
1378 // type-specifier-seq conversion-declarator[opt]
1379 //
1380 // conversion-declarator:
1381 // ptr-operator conversion-declarator[opt]
1382
1383 // Parse the type-specifier-seq.
1384 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001385 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001386 return true;
1387
1388 // Parse the conversion-declarator, which is merely a sequence of
1389 // ptr-operators.
1390 Declarator D(DS, Declarator::TypeNameContext);
1391 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1392
1393 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001394 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001395 if (Ty.isInvalid())
1396 return true;
1397
1398 // Note that this is a conversion-function-id.
1399 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1400 D.getSourceRange().getEnd());
1401 return false;
1402}
1403
1404/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1405/// name of an entity.
1406///
1407/// \code
1408/// unqualified-id: [C++ expr.prim.general]
1409/// identifier
1410/// operator-function-id
1411/// conversion-function-id
1412/// [C++0x] literal-operator-id [TODO]
1413/// ~ class-name
1414/// template-id
1415///
1416/// \endcode
1417///
1418/// \param The nested-name-specifier that preceded this unqualified-id. If
1419/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1420///
1421/// \param EnteringContext whether we are entering the scope of the
1422/// nested-name-specifier.
1423///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001424/// \param AllowDestructorName whether we allow parsing of a destructor name.
1425///
1426/// \param AllowConstructorName whether we allow parsing a constructor name.
1427///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001428/// \param ObjectType if this unqualified-id occurs within a member access
1429/// expression, the type of the base object whose member is being accessed.
1430///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001431/// \param Result on a successful parse, contains the parsed unqualified-id.
1432///
1433/// \returns true if parsing fails, false otherwise.
1434bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1435 bool AllowDestructorName,
1436 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001437 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001438 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001439
1440 // Handle 'A::template B'. This is for template-ids which have not
1441 // already been annotated by ParseOptionalCXXScopeSpecifier().
1442 bool TemplateSpecified = false;
1443 SourceLocation TemplateKWLoc;
1444 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1445 (ObjectType || SS.isSet())) {
1446 TemplateSpecified = true;
1447 TemplateKWLoc = ConsumeToken();
1448 }
1449
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001450 // unqualified-id:
1451 // identifier
1452 // template-id (when it hasn't already been annotated)
1453 if (Tok.is(tok::identifier)) {
1454 // Consume the identifier.
1455 IdentifierInfo *Id = Tok.getIdentifierInfo();
1456 SourceLocation IdLoc = ConsumeToken();
1457
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001458 if (!getLang().CPlusPlus) {
1459 // If we're not in C++, only identifiers matter. Record the
1460 // identifier and return.
1461 Result.setIdentifier(Id, IdLoc);
1462 return false;
1463 }
1464
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001465 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001466 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001467 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001468 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001469 &SS, false),
1470 IdLoc, IdLoc);
1471 } else {
1472 // We have parsed an identifier.
1473 Result.setIdentifier(Id, IdLoc);
1474 }
1475
1476 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001477 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001478 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001479 ObjectType, Result,
1480 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001481
1482 return false;
1483 }
1484
1485 // unqualified-id:
1486 // template-id (already parsed and annotated)
1487 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001488 TemplateIdAnnotation *TemplateId
1489 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1490
1491 // If the template-name names the current class, then this is a constructor
1492 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001493 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001494 if (SS.isSet()) {
1495 // C++ [class.qual]p2 specifies that a qualified template-name
1496 // is taken as the constructor name where a constructor can be
1497 // declared. Thus, the template arguments are extraneous, so
1498 // complain about them and remove them entirely.
1499 Diag(TemplateId->TemplateNameLoc,
1500 diag::err_out_of_line_constructor_template_id)
1501 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001502 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001503 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1504 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1505 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001506 getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001507 &SS, false),
1508 TemplateId->TemplateNameLoc,
1509 TemplateId->RAngleLoc);
1510 TemplateId->Destroy();
1511 ConsumeToken();
1512 return false;
1513 }
1514
1515 Result.setConstructorTemplateId(TemplateId);
1516 ConsumeToken();
1517 return false;
1518 }
1519
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001520 // We have already parsed a template-id; consume the annotation token as
1521 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001522 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001523 ConsumeToken();
1524 return false;
1525 }
1526
1527 // unqualified-id:
1528 // operator-function-id
1529 // conversion-function-id
1530 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001531 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001532 return true;
1533
Sean Hunte6252d12009-11-28 08:58:14 +00001534 // If we have an operator-function-id or a literal-operator-id and the next
1535 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001536 //
1537 // template-id:
1538 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001539 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1540 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001541 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001542 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1543 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001544 Result,
1545 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001546
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001547 return false;
1548 }
1549
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001550 if (getLang().CPlusPlus &&
1551 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001552 // C++ [expr.unary.op]p10:
1553 // There is an ambiguity in the unary-expression ~X(), where X is a
1554 // class-name. The ambiguity is resolved in favor of treating ~ as a
1555 // unary complement rather than treating ~X as referring to a destructor.
1556
1557 // Parse the '~'.
1558 SourceLocation TildeLoc = ConsumeToken();
1559
1560 // Parse the class-name.
1561 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001562 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001563 return true;
1564 }
1565
1566 // Parse the class-name (or template-name in a simple-template-id).
1567 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1568 SourceLocation ClassNameLoc = ConsumeToken();
1569
Douglas Gregor0278e122010-05-05 05:58:24 +00001570 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001571 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001572 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001573 EnteringContext, ObjectType, Result,
1574 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001575 }
1576
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001577 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001578 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1579 ClassNameLoc, getCurScope(),
1580 SS, ObjectType,
1581 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001582 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001583 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001584
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001585 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001586 return false;
1587 }
1588
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001589 Diag(Tok, diag::err_expected_unqualified_id)
1590 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001591 return true;
1592}
1593
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001594/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1595/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001596///
Chris Lattner59232d32009-01-04 21:25:24 +00001597/// This method is called to parse the new expression after the optional :: has
1598/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1599/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001600///
1601/// new-expression:
1602/// '::'[opt] 'new' new-placement[opt] new-type-id
1603/// new-initializer[opt]
1604/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1605/// new-initializer[opt]
1606///
1607/// new-placement:
1608/// '(' expression-list ')'
1609///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001610/// new-type-id:
1611/// type-specifier-seq new-declarator[opt]
1612///
1613/// new-declarator:
1614/// ptr-operator new-declarator[opt]
1615/// direct-new-declarator
1616///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001617/// new-initializer:
1618/// '(' expression-list[opt] ')'
1619/// [C++0x] braced-init-list [TODO]
1620///
John McCall60d7b3a2010-08-24 06:29:42 +00001621ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001622Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1623 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1624 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001625
1626 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1627 // second form of new-expression. It can't be a new-type-id.
1628
Sebastian Redla55e52c2008-11-25 22:21:31 +00001629 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001630 SourceLocation PlacementLParen, PlacementRParen;
1631
Douglas Gregor4bd40312010-07-13 15:54:32 +00001632 SourceRange TypeIdParens;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001633 DeclSpec DS;
1634 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001635 if (Tok.is(tok::l_paren)) {
1636 // If it turns out to be a placement, we change the type location.
1637 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001638 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1639 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001640 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001641 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001642
1643 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001644 if (PlacementRParen.isInvalid()) {
1645 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001646 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001647 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001648
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001649 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001650 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001651 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001652 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001653 } else {
1654 // We still need the type.
1655 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001656 TypeIdParens.setBegin(ConsumeParen());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001657 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001658 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001659 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001660 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1661 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001662 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001663 if (ParseCXXTypeSpecifierSeq(DS))
1664 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001665 else {
1666 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001667 ParseDeclaratorInternal(DeclaratorInfo,
1668 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001669 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001670 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001671 }
1672 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001673 // A new-type-id is a simplified type-id, where essentially the
1674 // direct-declarator is replaced by a direct-new-declarator.
1675 if (ParseCXXTypeSpecifierSeq(DS))
1676 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001677 else {
1678 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001679 ParseDeclaratorInternal(DeclaratorInfo,
1680 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001681 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001682 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001683 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001684 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001685 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001686 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001687
Sebastian Redla55e52c2008-11-25 22:21:31 +00001688 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001689 SourceLocation ConstructorLParen, ConstructorRParen;
1690
1691 if (Tok.is(tok::l_paren)) {
1692 ConstructorLParen = ConsumeParen();
1693 if (Tok.isNot(tok::r_paren)) {
1694 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001695 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1696 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001697 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001698 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001699 }
1700 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001701 if (ConstructorRParen.isInvalid()) {
1702 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001703 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001704 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001705 }
1706
Sebastian Redlf53597f2009-03-15 17:47:39 +00001707 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1708 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001709 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001710 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001711}
1712
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001713/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1714/// passed to ParseDeclaratorInternal.
1715///
1716/// direct-new-declarator:
1717/// '[' expression ']'
1718/// direct-new-declarator '[' constant-expression ']'
1719///
Chris Lattner59232d32009-01-04 21:25:24 +00001720void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001721 // Parse the array dimensions.
1722 bool first = true;
1723 while (Tok.is(tok::l_square)) {
1724 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001725 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001726 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001727 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001728 // Recover
1729 SkipUntil(tok::r_square);
1730 return;
1731 }
1732 first = false;
1733
Sebastian Redlab197ba2009-02-09 18:23:29 +00001734 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall7f040a92010-12-24 02:08:15 +00001735 D.AddTypeInfo(DeclaratorChunk::getArray(0, ParsedAttributes(),
1736 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001737 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001738 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001739
Sebastian Redlab197ba2009-02-09 18:23:29 +00001740 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001741 return;
1742 }
1743}
1744
1745/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1746/// This ambiguity appears in the syntax of the C++ new operator.
1747///
1748/// new-expression:
1749/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1750/// new-initializer[opt]
1751///
1752/// new-placement:
1753/// '(' expression-list ')'
1754///
John McCallca0408f2010-08-23 06:44:23 +00001755bool Parser::ParseExpressionListOrTypeId(
1756 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001757 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001758 // The '(' was already consumed.
1759 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001760 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001761 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001762 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001763 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001764 }
1765
1766 // It's not a type, it has to be an expression list.
1767 // Discard the comma locations - ActOnCXXNew has enough parameters.
1768 CommaLocsTy CommaLocs;
1769 return ParseExpressionList(PlacementArgs, CommaLocs);
1770}
1771
1772/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1773/// to free memory allocated by new.
1774///
Chris Lattner59232d32009-01-04 21:25:24 +00001775/// This method is called to parse the 'delete' expression after the optional
1776/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1777/// and "Start" is its location. Otherwise, "Start" is the location of the
1778/// 'delete' token.
1779///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001780/// delete-expression:
1781/// '::'[opt] 'delete' cast-expression
1782/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001783ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001784Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1785 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1786 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001787
1788 // Array delete?
1789 bool ArrayDelete = false;
1790 if (Tok.is(tok::l_square)) {
1791 ArrayDelete = true;
1792 SourceLocation LHS = ConsumeBracket();
1793 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1794 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001795 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001796 }
1797
John McCall60d7b3a2010-08-24 06:29:42 +00001798 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001799 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001800 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001801
John McCall9ae2f072010-08-23 23:25:46 +00001802 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001803}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001804
Mike Stump1eb44332009-09-09 15:08:12 +00001805static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001806 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001807 default: llvm_unreachable("Not a known unary type trait");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001808 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1809 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1810 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1811 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1812 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1813 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1814 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1815 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1816 case tok::kw___is_abstract: return UTT_IsAbstract;
1817 case tok::kw___is_class: return UTT_IsClass;
1818 case tok::kw___is_empty: return UTT_IsEmpty;
1819 case tok::kw___is_enum: return UTT_IsEnum;
1820 case tok::kw___is_pod: return UTT_IsPOD;
1821 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1822 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001823 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001824 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001825}
1826
1827static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1828 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001829 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001830 case tok::kw___is_base_of: return BTT_IsBaseOf;
1831 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001832 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001833 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001834}
1835
1836/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1837/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1838/// templates.
1839///
1840/// primary-expression:
1841/// [GNU] unary-type-trait '(' type-id ')'
1842///
John McCall60d7b3a2010-08-24 06:29:42 +00001843ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001844 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1845 SourceLocation Loc = ConsumeToken();
1846
1847 SourceLocation LParen = Tok.getLocation();
1848 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1849 return ExprError();
1850
1851 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1852 // there will be cryptic errors about mismatched parentheses and missing
1853 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001854 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001855
1856 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1857
Douglas Gregor809070a2009-02-18 17:45:20 +00001858 if (Ty.isInvalid())
1859 return ExprError();
1860
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00001861 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001862}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001863
Francois Pichet6ad6f282010-12-07 00:08:36 +00001864/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
1865/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1866/// templates.
1867///
1868/// primary-expression:
1869/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
1870///
1871ExprResult Parser::ParseBinaryTypeTrait() {
1872 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
1873 SourceLocation Loc = ConsumeToken();
1874
1875 SourceLocation LParen = Tok.getLocation();
1876 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1877 return ExprError();
1878
1879 TypeResult LhsTy = ParseTypeName();
1880 if (LhsTy.isInvalid()) {
1881 SkipUntil(tok::r_paren);
1882 return ExprError();
1883 }
1884
1885 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
1886 SkipUntil(tok::r_paren);
1887 return ExprError();
1888 }
1889
1890 TypeResult RhsTy = ParseTypeName();
1891 if (RhsTy.isInvalid()) {
1892 SkipUntil(tok::r_paren);
1893 return ExprError();
1894 }
1895
1896 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1897
1898 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
1899}
1900
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001901/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1902/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1903/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00001904ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001905Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00001906 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001907 SourceLocation LParenLoc,
1908 SourceLocation &RParenLoc) {
1909 assert(getLang().CPlusPlus && "Should only be called for C++!");
1910 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1911 assert(isTypeIdInParens() && "Not a type-id!");
1912
John McCall60d7b3a2010-08-24 06:29:42 +00001913 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00001914 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001915
1916 // We need to disambiguate a very ugly part of the C++ syntax:
1917 //
1918 // (T())x; - type-id
1919 // (T())*x; - type-id
1920 // (T())/x; - expression
1921 // (T()); - expression
1922 //
1923 // The bad news is that we cannot use the specialized tentative parser, since
1924 // it can only verify that the thing inside the parens can be parsed as
1925 // type-id, it is not useful for determining the context past the parens.
1926 //
1927 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001928 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001929 //
1930 // It uses a scheme similar to parsing inline methods. The parenthesized
1931 // tokens are cached, the context that follows is determined (possibly by
1932 // parsing a cast-expression), and then we re-introduce the cached tokens
1933 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001934
Mike Stump1eb44332009-09-09 15:08:12 +00001935 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001936 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001937
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001938 // Store the tokens of the parentheses. We will parse them after we determine
1939 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001940 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001941 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001942 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1943 return ExprError();
1944 }
1945
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001946 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001947 ParseAs = CompoundLiteral;
1948 } else {
1949 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001950 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1951 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1952 NotCastExpr = true;
1953 } else {
1954 // Try parsing the cast-expression that may follow.
1955 // If it is not a cast-expression, NotCastExpr will be true and no token
1956 // will be consumed.
1957 Result = ParseCastExpression(false/*isUnaryExpression*/,
1958 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00001959 NotCastExpr,
1960 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001961 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001962
1963 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1964 // an expression.
1965 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001966 }
1967
Mike Stump1eb44332009-09-09 15:08:12 +00001968 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001969 Toks.push_back(Tok);
1970 // Re-enter the stored parenthesized tokens into the token stream, so we may
1971 // parse them now.
1972 PP.EnterTokenStream(Toks.data(), Toks.size(),
1973 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1974 // Drop the current token and bring the first cached one. It's the same token
1975 // as when we entered this function.
1976 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001977
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001978 if (ParseAs >= CompoundLiteral) {
1979 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001980
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001981 // Match the ')'.
1982 if (Tok.is(tok::r_paren))
1983 RParenLoc = ConsumeParen();
1984 else
1985 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001986
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001987 if (ParseAs == CompoundLiteral) {
1988 ExprType = CompoundLiteral;
1989 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1990 }
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001992 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1993 assert(ParseAs == CastExpr);
1994
1995 if (Ty.isInvalid())
1996 return ExprError();
1997
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001998 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001999
2000 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002001 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002002 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002003 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002004 return move(Result);
2005 }
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002007 // Not a compound literal, and not followed by a cast-expression.
2008 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002009
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002010 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002011 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002012 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002013 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002014
2015 // Match the ')'.
2016 if (Result.isInvalid()) {
2017 SkipUntil(tok::r_paren);
2018 return ExprError();
2019 }
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002021 if (Tok.is(tok::r_paren))
2022 RParenLoc = ConsumeParen();
2023 else
2024 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2025
2026 return move(Result);
2027}