blob: 18259ceb786c0ad121e08fca1ec3009a617c39e4 [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"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/DeclSpec.h"
17#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000018#include "llvm/Support/ErrorHandling.h"
19
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Mike Stump1eb44332009-09-09 15:08:12 +000022/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000023///
24/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000025/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000026/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000027///
28/// '::'[opt] nested-name-specifier
29/// '::'
30///
31/// nested-name-specifier:
32/// type-name '::'
33/// namespace-name '::'
34/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000035/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000036///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000037///
Mike Stump1eb44332009-09-09 15:08:12 +000038/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000039/// nested-name-specifier (or empty)
40///
Mike Stump1eb44332009-09-09 15:08:12 +000041/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000042/// the "." or "->" of a member access expression, this parameter provides the
43/// type of the object whose members are being accessed.
44///
45/// \param EnteringContext whether we will be entering into the context of
46/// the nested-name-specifier after parsing it.
47///
Douglas Gregord4dca082010-02-24 18:44:31 +000048/// \param MayBePseudoDestructor When non-NULL, points to a flag that
49/// indicates whether this nested-name-specifier may be part of a
50/// pseudo-destructor name. In this case, the flag will be set false
51/// if we don't actually end up parsing a destructor name. Moreorover,
52/// if we do end up determining that we are parsing a destructor name,
53/// the last component of the nested-name-specifier is not parsed as
54/// part of the scope specifier.
55
Douglas Gregorb10cd042010-02-21 18:36:56 +000056/// member access expression, e.g., the \p T:: in \p p->T::m.
57///
John McCall9ba61662010-02-26 08:45:28 +000058/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +000059bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +000060 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +000061 bool EnteringContext,
Douglas Gregord4dca082010-02-24 18:44:31 +000062 bool *MayBePseudoDestructor) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000063 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000064 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000065
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000066 if (Tok.is(tok::annot_cxxscope)) {
John McCallca0408f2010-08-23 06:44:23 +000067 SS.setScopeRep(static_cast<NestedNameSpecifier*>(Tok.getAnnotationValue()));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000068 SS.setRange(Tok.getAnnotationRange());
69 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +000070 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000071 }
Chris Lattnere607e802009-01-04 21:14:15 +000072
Douglas Gregor39a8de12009-02-25 19:37:18 +000073 bool HasScopeSpecifier = false;
74
Chris Lattner5b454732009-01-05 03:55:46 +000075 if (Tok.is(tok::coloncolon)) {
76 // ::new and ::delete aren't nested-name-specifiers.
77 tok::TokenKind NextKind = NextToken().getKind();
78 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
79 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattner55a7cef2009-01-05 00:13:00 +000081 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000082 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000083 SS.setBeginLoc(CCLoc);
Douglas Gregor23c94db2010-07-02 17:43:08 +000084 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000085 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000086 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000087 }
88
Douglas Gregord4dca082010-02-24 18:44:31 +000089 bool CheckForDestructor = false;
90 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
91 CheckForDestructor = true;
92 *MayBePseudoDestructor = false;
93 }
94
Douglas Gregor39a8de12009-02-25 19:37:18 +000095 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000096 if (HasScopeSpecifier) {
97 // C++ [basic.lookup.classref]p5:
98 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000099 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000100 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000101 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000102 // the class-name-or-namespace-name is looked up in global scope as a
103 // class-name or namespace-name.
104 //
105 // To implement this, we clear out the object type as soon as we've
106 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000107 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000108
109 if (Tok.is(tok::code_completion)) {
110 // Code completion for a nested-name-specifier, where the code
111 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000112 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Douglas Gregordc845342010-05-25 05:58:43 +0000113 ConsumeCodeCompletionToken();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000114 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Douglas Gregor39a8de12009-02-25 19:37:18 +0000117 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000118 // nested-name-specifier 'template'[opt] simple-template-id '::'
119
120 // Parse the optional 'template' keyword, then make sure we have
121 // 'identifier <' after it.
122 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000124 // nested-name-specifier, since they aren't allowed to start with
125 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000126 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000127 break;
128
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000129 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000130 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000131
132 UnqualifiedId TemplateName;
133 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000134 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000135 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000136 ConsumeToken();
137 } else if (Tok.is(tok::kw_operator)) {
138 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000139 TemplateName)) {
140 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000141 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000142 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000143
Sean Hunte6252d12009-11-28 08:58:14 +0000144 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
145 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000146 Diag(TemplateName.getSourceRange().getBegin(),
147 diag::err_id_after_template_in_nested_name_spec)
148 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000149 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000150 break;
151 }
152 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000153 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000154 break;
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000157 // If the next token is not '<', we have a qualified-id that refers
158 // to a template name, such as T::template apply, but is not a
159 // template-id.
160 if (Tok.isNot(tok::less)) {
161 TPA.Revert();
162 break;
163 }
164
165 // Commit to parsing the template-id.
166 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000167 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000168 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000169 TemplateKWLoc,
170 SS,
171 TemplateName,
172 ObjectType,
173 EnteringContext,
174 Template)) {
175 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
176 TemplateKWLoc, false))
177 return true;
178 } else
John McCall9ba61662010-02-26 08:45:28 +0000179 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner77cf72a2009-06-26 03:47:46 +0000181 continue;
182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregor39a8de12009-02-25 19:37:18 +0000184 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000185 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000186 //
187 // simple-template-id '::'
188 //
189 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000190 // right kind (it should name a type or be dependent), and then
191 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000192 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000193 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord4dca082010-02-24 18:44:31 +0000194 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
195 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000196 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000197 }
198
Mike Stump1eb44332009-09-09 15:08:12 +0000199 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000200 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000201 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000202
Mike Stump1eb44332009-09-09 15:08:12 +0000203 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000204 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000205 Token TypeToken = Tok;
206 ConsumeToken();
207 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
208 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Douglas Gregor39a8de12009-02-25 19:37:18 +0000210 if (!HasScopeSpecifier) {
211 SS.setBeginLoc(TypeToken.getLocation());
212 HasScopeSpecifier = true;
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
John McCallb3d87482010-08-24 05:47:05 +0000215 if (ParsedType T = getTypeAnnotation(TypeToken)) {
216 CXXScopeTy *Scope =
217 Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, T,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000218 TypeToken.getAnnotationRange(),
John McCallb3d87482010-08-24 05:47:05 +0000219 CCLoc);
220 SS.setScopeRep(Scope);
221 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +0000222 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000223 SS.setEndLoc(CCLoc);
224 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattner67b9e832009-06-26 03:45:46 +0000227 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000228 }
229
Chris Lattner5c7f7862009-06-26 03:52:38 +0000230
231 // The rest of the nested-name-specifier possibilities start with
232 // tok::identifier.
233 if (Tok.isNot(tok::identifier))
234 break;
235
236 IdentifierInfo &II = *Tok.getIdentifierInfo();
237
238 // nested-name-specifier:
239 // type-name '::'
240 // namespace-name '::'
241 // nested-name-specifier identifier '::'
242 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000243
244 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
245 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000246 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000247 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II, ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000248 EnteringContext) &&
249 // If the token after the colon isn't an identifier, it's still an
250 // error, but they probably meant something else strange so don't
251 // recover like this.
252 PP.LookAhead(1).is(tok::identifier)) {
253 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000254 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000255
256 // Recover as if the user wrote '::'.
257 Next.setKind(tok::coloncolon);
258 }
Chris Lattner46646492009-12-07 01:36:53 +0000259 }
260
Chris Lattner5c7f7862009-06-26 03:52:38 +0000261 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000262 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000263 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000264 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000265 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000266 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000267 }
268
Chris Lattner5c7f7862009-06-26 03:52:38 +0000269 // We have an identifier followed by a '::'. Lookup this name
270 // as the name in a nested-name-specifier.
271 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000272 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
273 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000274 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattner5c7f7862009-06-26 03:52:38 +0000276 if (!HasScopeSpecifier) {
277 SS.setBeginLoc(IdLoc);
278 HasScopeSpecifier = true;
279 }
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Argyrios Kyrtzidis661c36b2010-06-22 11:30:04 +0000281 if (!SS.isInvalid())
282 SS.setScopeRep(
Douglas Gregor23c94db2010-07-02 17:43:08 +0000283 Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, IdLoc, CCLoc, II,
Argyrios Kyrtzidis661c36b2010-06-22 11:30:04 +0000284 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000285 SS.setEndLoc(CCLoc);
286 continue;
287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattner5c7f7862009-06-26 03:52:38 +0000289 // nested-name-specifier:
290 // type-name '<'
291 if (Next.is(tok::less)) {
292 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000293 UnqualifiedId TemplateName;
294 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000295 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000296 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000297 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000298 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000299 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000300 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000301 Template,
302 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000303 // We have found a template name, so annotate this this token
304 // with a template-id annotation. We do not permit the
305 // template-id to be translated into a type annotation,
306 // because some clients (e.g., the parsing of class template
307 // specializations) still want to see the original template-id
308 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000309 ConsumeToken();
310 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
311 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000312 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000313 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000314 }
315
316 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
317 IsTemplateArgumentList(1)) {
318 // We have something like t::getAs<T>, where getAs is a
319 // member of an unknown specialization. However, this will only
320 // parse correctly as a template, so suggest the keyword 'template'
321 // before 'getAs' and treat this as a dependent template name.
322 Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
323 << II.getName()
324 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
325
Douglas Gregord6ab2322010-06-16 23:00:59 +0000326 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000327 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000328 Tok.getLocation(), SS,
329 TemplateName, ObjectType,
330 EnteringContext, Template)) {
331 // Consume the identifier.
332 ConsumeToken();
333 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
334 SourceLocation(), false))
335 return true;
336 }
337 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000338 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000339
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000340 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000341 }
342 }
343
Douglas Gregor39a8de12009-02-25 19:37:18 +0000344 // We don't have any tokens that form the beginning of a
345 // nested-name-specifier, so we're done.
346 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Douglas Gregord4dca082010-02-24 18:44:31 +0000349 // Even if we didn't see any pieces of a nested-name-specifier, we
350 // still check whether there is a tilde in this position, which
351 // indicates a potential pseudo-destructor.
352 if (CheckForDestructor && Tok.is(tok::tilde))
353 *MayBePseudoDestructor = true;
354
John McCall9ba61662010-02-26 08:45:28 +0000355 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000356}
357
358/// ParseCXXIdExpression - Handle id-expression.
359///
360/// id-expression:
361/// unqualified-id
362/// qualified-id
363///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000364/// qualified-id:
365/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
366/// '::' identifier
367/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000368/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000369///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000370/// NOTE: The standard specifies that, for qualified-id, the parser does not
371/// expect:
372///
373/// '::' conversion-function-id
374/// '::' '~' class-name
375///
376/// This may cause a slight inconsistency on diagnostics:
377///
378/// class C {};
379/// namespace A {}
380/// void f() {
381/// :: A :: ~ C(); // Some Sema error about using destructor with a
382/// // namespace.
383/// :: ~ C(); // Some Parser error like 'unexpected ~'.
384/// }
385///
386/// We simplify the parser a bit and make it work like:
387///
388/// qualified-id:
389/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
390/// '::' unqualified-id
391///
392/// That way Sema can handle and report similar errors for namespaces and the
393/// global scope.
394///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000395/// The isAddressOfOperand parameter indicates that this id-expression is a
396/// direct operand of the address-of operator. This is, besides member contexts,
397/// the only place where a qualified-id naming a non-static class member may
398/// appear.
399///
John McCall60d7b3a2010-08-24 06:29:42 +0000400ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000401 // qualified-id:
402 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
403 // '::' unqualified-id
404 //
405 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000406 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000407
408 UnqualifiedId Name;
409 if (ParseUnqualifiedId(SS,
410 /*EnteringContext=*/false,
411 /*AllowDestructorName=*/false,
412 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000413 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000414 Name))
415 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000416
417 // This is only the direct operand of an & operator if it is not
418 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000419 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
420 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000421
Douglas Gregor23c94db2010-07-02 17:43:08 +0000422 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000423 isAddressOfOperand);
424
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000425}
426
Reid Spencer5f016e22007-07-11 17:01:13 +0000427/// ParseCXXCasts - This handles the various ways to cast expressions to another
428/// type.
429///
430/// postfix-expression: [C++ 5.2p1]
431/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
432/// 'static_cast' '<' type-name '>' '(' expression ')'
433/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
434/// 'const_cast' '<' type-name '>' '(' expression ')'
435///
John McCall60d7b3a2010-08-24 06:29:42 +0000436ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 tok::TokenKind Kind = Tok.getKind();
438 const char *CastName = 0; // For error messages
439
440 switch (Kind) {
441 default: assert(0 && "Unknown C++ cast!"); abort();
442 case tok::kw_const_cast: CastName = "const_cast"; break;
443 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
444 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
445 case tok::kw_static_cast: CastName = "static_cast"; break;
446 }
447
448 SourceLocation OpLoc = ConsumeToken();
449 SourceLocation LAngleBracketLoc = Tok.getLocation();
450
451 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000452 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000453
Douglas Gregor809070a2009-02-18 17:45:20 +0000454 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 SourceLocation RAngleBracketLoc = Tok.getLocation();
456
Chris Lattner1ab3b962008-11-18 07:48:38 +0000457 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000458 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000459
460 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
461
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000462 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
463 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000464
John McCall60d7b3a2010-08-24 06:29:42 +0000465 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000467 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000468 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000469
Douglas Gregor809070a2009-02-18 17:45:20 +0000470 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000471 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000472 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000473 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000474 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000475
Sebastian Redl20df9b72008-12-11 22:51:44 +0000476 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000477}
478
Sebastian Redlc42e1182008-11-11 11:37:55 +0000479/// ParseCXXTypeid - This handles the C++ typeid expression.
480///
481/// postfix-expression: [C++ 5.2p1]
482/// 'typeid' '(' expression ')'
483/// 'typeid' '(' type-id ')'
484///
John McCall60d7b3a2010-08-24 06:29:42 +0000485ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000486 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
487
488 SourceLocation OpLoc = ConsumeToken();
489 SourceLocation LParenLoc = Tok.getLocation();
490 SourceLocation RParenLoc;
491
492 // typeid expressions are always parenthesized.
493 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
494 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000495 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000496
John McCall60d7b3a2010-08-24 06:29:42 +0000497 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000498
499 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000500 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000501
502 // Match the ')'.
503 MatchRHSPunctuation(tok::r_paren, LParenLoc);
504
Douglas Gregor809070a2009-02-18 17:45:20 +0000505 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000506 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000507
508 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000509 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000510 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000511 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000512 // When typeid is applied to an expression other than an lvalue of a
513 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000514 // operand (Clause 5).
515 //
Mike Stump1eb44332009-09-09 15:08:12 +0000516 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000517 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000518 // we the expression is potentially potentially evaluated.
519 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000520 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000521 Result = ParseExpression();
522
523 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000524 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000525 SkipUntil(tok::r_paren);
526 else {
527 MatchRHSPunctuation(tok::r_paren, LParenLoc);
528
529 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000530 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000531 }
532 }
533
Sebastian Redl20df9b72008-12-11 22:51:44 +0000534 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000535}
536
Francois Pichet01b7c302010-09-08 12:20:18 +0000537/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
538///
539/// '__uuidof' '(' expression ')'
540/// '__uuidof' '(' type-id ')'
541///
542ExprResult Parser::ParseCXXUuidof() {
543 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
544
545 SourceLocation OpLoc = ConsumeToken();
546 SourceLocation LParenLoc = Tok.getLocation();
547 SourceLocation RParenLoc;
548
549 // __uuidof expressions are always parenthesized.
550 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
551 "__uuidof"))
552 return ExprError();
553
554 ExprResult Result;
555
556 if (isTypeIdInParens()) {
557 TypeResult Ty = ParseTypeName();
558
559 // Match the ')'.
560 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
561
562 if (Ty.isInvalid())
563 return ExprError();
564
565 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
566 Ty.get().getAsOpaquePtr(), RParenLoc);
567 } else {
568 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
569 Result = ParseExpression();
570
571 // Match the ')'.
572 if (Result.isInvalid())
573 SkipUntil(tok::r_paren);
574 else {
575 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
576
577 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
578 Result.release(), RParenLoc);
579 }
580 }
581
582 return move(Result);
583}
584
Douglas Gregord4dca082010-02-24 18:44:31 +0000585/// \brief Parse a C++ pseudo-destructor expression after the base,
586/// . or -> operator, and nested-name-specifier have already been
587/// parsed.
588///
589/// postfix-expression: [C++ 5.2]
590/// postfix-expression . pseudo-destructor-name
591/// postfix-expression -> pseudo-destructor-name
592///
593/// pseudo-destructor-name:
594/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
595/// ::[opt] nested-name-specifier template simple-template-id ::
596/// ~type-name
597/// ::[opt] nested-name-specifier[opt] ~type-name
598///
John McCall60d7b3a2010-08-24 06:29:42 +0000599ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000600Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
601 tok::TokenKind OpKind,
602 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000603 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000604 // We're parsing either a pseudo-destructor-name or a dependent
605 // member access that has the same form as a
606 // pseudo-destructor-name. We parse both in the same way and let
607 // the action model sort them out.
608 //
609 // Note that the ::[opt] nested-name-specifier[opt] has already
610 // been parsed, and if there was a simple-template-id, it has
611 // been coalesced into a template-id annotation token.
612 UnqualifiedId FirstTypeName;
613 SourceLocation CCLoc;
614 if (Tok.is(tok::identifier)) {
615 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
616 ConsumeToken();
617 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
618 CCLoc = ConsumeToken();
619 } else if (Tok.is(tok::annot_template_id)) {
620 FirstTypeName.setTemplateId(
621 (TemplateIdAnnotation *)Tok.getAnnotationValue());
622 ConsumeToken();
623 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
624 CCLoc = ConsumeToken();
625 } else {
626 FirstTypeName.setIdentifier(0, SourceLocation());
627 }
628
629 // Parse the tilde.
630 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
631 SourceLocation TildeLoc = ConsumeToken();
632 if (!Tok.is(tok::identifier)) {
633 Diag(Tok, diag::err_destructor_tilde_identifier);
634 return ExprError();
635 }
636
637 // Parse the second type.
638 UnqualifiedId SecondTypeName;
639 IdentifierInfo *Name = Tok.getIdentifierInfo();
640 SourceLocation NameLoc = ConsumeToken();
641 SecondTypeName.setIdentifier(Name, NameLoc);
642
643 // If there is a '<', the second type name is a template-id. Parse
644 // it as such.
645 if (Tok.is(tok::less) &&
646 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000647 SecondTypeName, /*AssumeTemplateName=*/true,
648 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000649 return ExprError();
650
John McCall9ae2f072010-08-23 23:25:46 +0000651 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
652 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000653 SS, FirstTypeName, CCLoc,
654 TildeLoc, SecondTypeName,
655 Tok.is(tok::l_paren));
656}
657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
659///
660/// boolean-literal: [C++ 2.13.5]
661/// 'true'
662/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000663ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000665 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000666}
Chris Lattner50dd2892008-02-26 00:51:44 +0000667
668/// ParseThrowExpression - This handles the C++ throw expression.
669///
670/// throw-expression: [C++ 15]
671/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000672ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000673 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000674 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000675
Chris Lattner2a2819a2008-04-06 06:02:23 +0000676 // If the current token isn't the start of an assignment-expression,
677 // then the expression is not present. This handles things like:
678 // "C ? throw : (void)42", which is crazy but legal.
679 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
680 case tok::semi:
681 case tok::r_paren:
682 case tok::r_square:
683 case tok::r_brace:
684 case tok::colon:
685 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000686 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000687
Chris Lattner2a2819a2008-04-06 06:02:23 +0000688 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000689 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000690 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000691 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000692 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000693}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000694
695/// ParseCXXThis - This handles the C++ 'this' pointer.
696///
697/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
698/// a non-lvalue expression whose value is the address of the object for which
699/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000700ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000701 assert(Tok.is(tok::kw_this) && "Not 'this'!");
702 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000703 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000704}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000705
706/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
707/// Can be interpreted either as function-style casting ("int(x)")
708/// or class type construction ("ClassType(x,y,z)")
709/// or creation of a value-initialized type ("int()").
710///
711/// postfix-expression: [C++ 5.2p1]
712/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
713/// typename-specifier '(' expression-list[opt] ')' [TODO]
714///
John McCall60d7b3a2010-08-24 06:29:42 +0000715ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000716Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000717 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000718 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000719
720 assert(Tok.is(tok::l_paren) && "Expected '('!");
721 SourceLocation LParenLoc = ConsumeParen();
722
Sebastian Redla55e52c2008-11-25 22:21:31 +0000723 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000724 CommaLocsTy CommaLocs;
725
726 if (Tok.isNot(tok::r_paren)) {
727 if (ParseExpressionList(Exprs, CommaLocs)) {
728 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000729 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000730 }
731 }
732
733 // Match the ')'.
734 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
735
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000736 // TypeRep could be null, if it references an invalid typedef.
737 if (!TypeRep)
738 return ExprError();
739
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000740 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
741 "Unexpected number of commas!");
Douglas Gregorab6677e2010-09-08 00:15:04 +0000742 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000743 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000744}
745
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000746/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000747///
748/// condition:
749/// expression
750/// type-specifier-seq declarator '=' assignment-expression
751/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
752/// '=' assignment-expression
753///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000754/// \param ExprResult if the condition was parsed as an expression, the
755/// parsed expression.
756///
757/// \param DeclResult if the condition was parsed as a declaration, the
758/// parsed declaration.
759///
Douglas Gregor586596f2010-05-06 17:25:47 +0000760/// \param Loc The location of the start of the statement that requires this
761/// condition, e.g., the "for" in a for loop.
762///
763/// \param ConvertToBoolean Whether the condition expression should be
764/// converted to a boolean value.
765///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000766/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000767bool Parser::ParseCXXCondition(ExprResult &ExprOut,
768 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000769 SourceLocation Loc,
770 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000771 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000772 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000773 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000774 }
775
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000776 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000777 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000778 ExprOut = ParseExpression(); // expression
779 DeclOut = 0;
780 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000781 return true;
782
783 // If required, convert to a boolean value.
784 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000785 ExprOut
786 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
787 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000788 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000789
790 // type-specifier-seq
791 DeclSpec DS;
792 ParseSpecifierQualifierList(DS);
793
794 // declarator
795 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
796 ParseDeclarator(DeclaratorInfo);
797
798 // simple-asm-expr[opt]
799 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000800 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000801 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000802 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000803 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000804 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000805 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000806 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000807 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000808 }
809
810 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000811 if (Tok.is(tok::kw___attribute)) {
812 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000813 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000814 DeclaratorInfo.AddAttributes(AttrList, Loc);
815 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000816
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000817 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000818 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000819 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000820 DeclOut = Dcl.get();
821 ExprOut = ExprError();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000822
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000823 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000824 if (Tok.is(tok::equal)) {
825 SourceLocation EqualLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000826 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000827 if (!AssignExpr.isInvalid())
John McCall60d7b3a2010-08-24 06:29:42 +0000828 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000829 } else {
830 // FIXME: C++0x allows a braced-init-list
831 Diag(Tok, diag::err_expected_equal_after_declarator);
832 }
833
Douglas Gregor586596f2010-05-06 17:25:47 +0000834 // FIXME: Build a reference to this declaration? Convert it to bool?
835 // (This is currently handled by Sema).
836
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000837 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000838}
839
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000840/// \brief Determine whether the current token starts a C++
841/// simple-type-specifier.
842bool Parser::isCXXSimpleTypeSpecifier() const {
843 switch (Tok.getKind()) {
844 case tok::annot_typename:
845 case tok::kw_short:
846 case tok::kw_long:
847 case tok::kw_signed:
848 case tok::kw_unsigned:
849 case tok::kw_void:
850 case tok::kw_char:
851 case tok::kw_int:
852 case tok::kw_float:
853 case tok::kw_double:
854 case tok::kw_wchar_t:
855 case tok::kw_char16_t:
856 case tok::kw_char32_t:
857 case tok::kw_bool:
858 // FIXME: C++0x decltype support.
859 // GNU typeof support.
860 case tok::kw_typeof:
861 return true;
862
863 default:
864 break;
865 }
866
867 return false;
868}
869
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000870/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
871/// This should only be called when the current token is known to be part of
872/// simple-type-specifier.
873///
874/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000875/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000876/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
877/// char
878/// wchar_t
879/// bool
880/// short
881/// int
882/// long
883/// signed
884/// unsigned
885/// float
886/// double
887/// void
888/// [GNU] typeof-specifier
889/// [C++0x] auto [TODO]
890///
891/// type-name:
892/// class-name
893/// enum-name
894/// typedef-name
895///
896void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
897 DS.SetRangeStart(Tok.getLocation());
898 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000899 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000900 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000902 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000903 case tok::identifier: // foo::bar
904 case tok::coloncolon: // ::foo::bar
905 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000906 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000907 assert(0 && "Not a simple-type-specifier token!");
908 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000909
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000910 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000911 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000912 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000913 getTypeAnnotation(Tok));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000914 break;
915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000917 // builtin types
918 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000919 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000920 break;
921 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000922 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000923 break;
924 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000925 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000926 break;
927 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000928 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000929 break;
930 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000931 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000932 break;
933 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000934 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000935 break;
936 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000937 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000938 break;
939 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000940 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000941 break;
942 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000943 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000944 break;
945 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000946 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000947 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000948 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000949 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000950 break;
951 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000952 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000953 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000954 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000955 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000956 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000958 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000959 // GNU typeof support.
960 case tok::kw_typeof:
961 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000962 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000963 return;
964 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000965 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000966 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
967 else
968 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000969 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000970 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000971}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000972
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000973/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
974/// [dcl.name]), which is a non-empty sequence of type-specifiers,
975/// e.g., "const short int". Note that the DeclSpec is *not* finished
976/// by parsing the type-specifier-seq, because these sequences are
977/// typically followed by some form of declarator. Returns true and
978/// emits diagnostics if this is not a type-specifier-seq, false
979/// otherwise.
980///
981/// type-specifier-seq: [C++ 8.1]
982/// type-specifier type-specifier-seq[opt]
983///
984bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
985 DS.SetRangeStart(Tok.getLocation());
986 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000987 unsigned DiagID;
988 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000989
990 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000991 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
992 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000993 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000994 return true;
995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Sebastian Redld9bafa72010-02-03 21:21:43 +0000997 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
998 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
999 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001000
Douglas Gregor396a9f22010-02-24 23:13:13 +00001001 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001002 return false;
1003}
1004
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001005/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1006/// some form.
1007///
1008/// This routine is invoked when a '<' is encountered after an identifier or
1009/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1010/// whether the unqualified-id is actually a template-id. This routine will
1011/// then parse the template arguments and form the appropriate template-id to
1012/// return to the caller.
1013///
1014/// \param SS the nested-name-specifier that precedes this template-id, if
1015/// we're actually parsing a qualified-id.
1016///
1017/// \param Name for constructor and destructor names, this is the actual
1018/// identifier that may be a template-name.
1019///
1020/// \param NameLoc the location of the class-name in a constructor or
1021/// destructor.
1022///
1023/// \param EnteringContext whether we're entering the scope of the
1024/// nested-name-specifier.
1025///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001026/// \param ObjectType if this unqualified-id occurs within a member access
1027/// expression, the type of the base object whose member is being accessed.
1028///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001029/// \param Id as input, describes the template-name or operator-function-id
1030/// that precedes the '<'. If template arguments were parsed successfully,
1031/// will be updated with the template-id.
1032///
Douglas Gregord4dca082010-02-24 18:44:31 +00001033/// \param AssumeTemplateId When true, this routine will assume that the name
1034/// refers to a template without performing name lookup to verify.
1035///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001036/// \returns true if a parse error occurred, false otherwise.
1037bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1038 IdentifierInfo *Name,
1039 SourceLocation NameLoc,
1040 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001041 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001042 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001043 bool AssumeTemplateId,
1044 SourceLocation TemplateKWLoc) {
1045 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1046 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001047
1048 TemplateTy Template;
1049 TemplateNameKind TNK = TNK_Non_template;
1050 switch (Id.getKind()) {
1051 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001052 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001053 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001054 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001055 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001056 Id, ObjectType, EnteringContext,
1057 Template);
1058 if (TNK == TNK_Non_template)
1059 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001060 } else {
1061 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001062 TNK = Actions.isTemplateName(getCurScope(), SS,
1063 TemplateKWLoc.isValid(), Id,
1064 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001065 MemberOfUnknownSpecialization);
1066
1067 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1068 ObjectType && IsTemplateArgumentList()) {
1069 // We have something like t->getAs<T>(), where getAs is a
1070 // member of an unknown specialization. However, this will only
1071 // parse correctly as a template, so suggest the keyword 'template'
1072 // before 'getAs' and treat this as a dependent template name.
1073 std::string Name;
1074 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1075 Name = Id.Identifier->getName();
1076 else {
1077 Name = "operator ";
1078 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1079 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1080 else
1081 Name += Id.Identifier->getName();
1082 }
1083 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1084 << Name
1085 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001086 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001087 SS, Id, ObjectType,
1088 EnteringContext, Template);
1089 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001090 return true;
1091 }
1092 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001093 break;
1094
Douglas Gregor014e88d2009-11-03 23:16:33 +00001095 case UnqualifiedId::IK_ConstructorName: {
1096 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001097 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001098 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001099 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1100 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001101 EnteringContext, Template,
1102 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001103 break;
1104 }
1105
Douglas Gregor014e88d2009-11-03 23:16:33 +00001106 case UnqualifiedId::IK_DestructorName: {
1107 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001108 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001109 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001110 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001111 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001112 TemplateName, ObjectType,
1113 EnteringContext, Template);
1114 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001115 return true;
1116 } else {
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 Gregor2d1c2142009-11-03 19:44:04 +00001121
John McCallb3d87482010-08-24 05:47:05 +00001122 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001123 Diag(NameLoc, diag::err_destructor_template_id)
1124 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001125 return true;
1126 }
1127 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001128 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001129 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001130
1131 default:
1132 return false;
1133 }
1134
1135 if (TNK == TNK_Non_template)
1136 return false;
1137
1138 // Parse the enclosed template argument list.
1139 SourceLocation LAngleLoc, RAngleLoc;
1140 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001141 if (Tok.is(tok::less) &&
1142 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001143 &SS, true, LAngleLoc,
1144 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001145 RAngleLoc))
1146 return true;
1147
1148 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001149 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1150 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001151 // Form a parsed representation of the template-id to be stored in the
1152 // UnqualifiedId.
1153 TemplateIdAnnotation *TemplateId
1154 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1155
1156 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1157 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001158 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001159 TemplateId->TemplateNameLoc = Id.StartLocation;
1160 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001161 TemplateId->Name = 0;
1162 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1163 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001164 }
1165
John McCall2b5289b2010-08-23 07:28:44 +00001166 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001167 TemplateId->Kind = TNK;
1168 TemplateId->LAngleLoc = LAngleLoc;
1169 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001170 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001171 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001172 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001173 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001174
1175 Id.setTemplateId(TemplateId);
1176 return false;
1177 }
1178
1179 // Bundle the template arguments together.
1180 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001181 TemplateArgs.size());
1182
1183 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001184 TypeResult Type
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001185 = Actions.ActOnTemplateIdType(Template, NameLoc,
1186 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001187 RAngleLoc);
1188 if (Type.isInvalid())
1189 return true;
1190
1191 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1192 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1193 else
1194 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1195
1196 return false;
1197}
1198
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001199/// \brief Parse an operator-function-id or conversion-function-id as part
1200/// of a C++ unqualified-id.
1201///
1202/// This routine is responsible only for parsing the operator-function-id or
1203/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001204///
1205/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001206/// operator-function-id: [C++ 13.5]
1207/// 'operator' operator
1208///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001209/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001210/// new delete new[] delete[]
1211/// + - * / % ^ & | ~
1212/// ! = < > += -= *= /= %=
1213/// ^= &= |= << >> >>= <<= == !=
1214/// <= >= && || ++ -- , ->* ->
1215/// () []
1216///
1217/// conversion-function-id: [C++ 12.3.2]
1218/// operator conversion-type-id
1219///
1220/// conversion-type-id:
1221/// type-specifier-seq conversion-declarator[opt]
1222///
1223/// conversion-declarator:
1224/// ptr-operator conversion-declarator[opt]
1225/// \endcode
1226///
1227/// \param The nested-name-specifier that preceded this unqualified-id. If
1228/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1229///
1230/// \param EnteringContext whether we are entering the scope of the
1231/// nested-name-specifier.
1232///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001233/// \param ObjectType if this unqualified-id occurs within a member access
1234/// expression, the type of the base object whose member is being accessed.
1235///
1236/// \param Result on a successful parse, contains the parsed unqualified-id.
1237///
1238/// \returns true if parsing fails, false otherwise.
1239bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001240 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001241 UnqualifiedId &Result) {
1242 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1243
1244 // Consume the 'operator' keyword.
1245 SourceLocation KeywordLoc = ConsumeToken();
1246
1247 // Determine what kind of operator name we have.
1248 unsigned SymbolIdx = 0;
1249 SourceLocation SymbolLocations[3];
1250 OverloadedOperatorKind Op = OO_None;
1251 switch (Tok.getKind()) {
1252 case tok::kw_new:
1253 case tok::kw_delete: {
1254 bool isNew = Tok.getKind() == tok::kw_new;
1255 // Consume the 'new' or 'delete'.
1256 SymbolLocations[SymbolIdx++] = ConsumeToken();
1257 if (Tok.is(tok::l_square)) {
1258 // Consume the '['.
1259 SourceLocation LBracketLoc = ConsumeBracket();
1260 // Consume the ']'.
1261 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1262 LBracketLoc);
1263 if (RBracketLoc.isInvalid())
1264 return true;
1265
1266 SymbolLocations[SymbolIdx++] = LBracketLoc;
1267 SymbolLocations[SymbolIdx++] = RBracketLoc;
1268 Op = isNew? OO_Array_New : OO_Array_Delete;
1269 } else {
1270 Op = isNew? OO_New : OO_Delete;
1271 }
1272 break;
1273 }
1274
1275#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1276 case tok::Token: \
1277 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1278 Op = OO_##Name; \
1279 break;
1280#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1281#include "clang/Basic/OperatorKinds.def"
1282
1283 case tok::l_paren: {
1284 // Consume the '('.
1285 SourceLocation LParenLoc = ConsumeParen();
1286 // Consume the ')'.
1287 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1288 LParenLoc);
1289 if (RParenLoc.isInvalid())
1290 return true;
1291
1292 SymbolLocations[SymbolIdx++] = LParenLoc;
1293 SymbolLocations[SymbolIdx++] = RParenLoc;
1294 Op = OO_Call;
1295 break;
1296 }
1297
1298 case tok::l_square: {
1299 // Consume the '['.
1300 SourceLocation LBracketLoc = ConsumeBracket();
1301 // Consume the ']'.
1302 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1303 LBracketLoc);
1304 if (RBracketLoc.isInvalid())
1305 return true;
1306
1307 SymbolLocations[SymbolIdx++] = LBracketLoc;
1308 SymbolLocations[SymbolIdx++] = RBracketLoc;
1309 Op = OO_Subscript;
1310 break;
1311 }
1312
1313 case tok::code_completion: {
1314 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001315 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001316
1317 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001318 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001319
1320 // Don't try to parse any further.
1321 return true;
1322 }
1323
1324 default:
1325 break;
1326 }
1327
1328 if (Op != OO_None) {
1329 // We have parsed an operator-function-id.
1330 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1331 return false;
1332 }
Sean Hunt0486d742009-11-28 04:44:28 +00001333
1334 // Parse a literal-operator-id.
1335 //
1336 // literal-operator-id: [C++0x 13.5.8]
1337 // operator "" identifier
1338
1339 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1340 if (Tok.getLength() != 2)
1341 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1342 ConsumeStringToken();
1343
1344 if (Tok.isNot(tok::identifier)) {
1345 Diag(Tok.getLocation(), diag::err_expected_ident);
1346 return true;
1347 }
1348
1349 IdentifierInfo *II = Tok.getIdentifierInfo();
1350 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001351 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001352 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001353
1354 // Parse a conversion-function-id.
1355 //
1356 // conversion-function-id: [C++ 12.3.2]
1357 // operator conversion-type-id
1358 //
1359 // conversion-type-id:
1360 // type-specifier-seq conversion-declarator[opt]
1361 //
1362 // conversion-declarator:
1363 // ptr-operator conversion-declarator[opt]
1364
1365 // Parse the type-specifier-seq.
1366 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001367 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001368 return true;
1369
1370 // Parse the conversion-declarator, which is merely a sequence of
1371 // ptr-operators.
1372 Declarator D(DS, Declarator::TypeNameContext);
1373 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1374
1375 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001376 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001377 if (Ty.isInvalid())
1378 return true;
1379
1380 // Note that this is a conversion-function-id.
1381 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1382 D.getSourceRange().getEnd());
1383 return false;
1384}
1385
1386/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1387/// name of an entity.
1388///
1389/// \code
1390/// unqualified-id: [C++ expr.prim.general]
1391/// identifier
1392/// operator-function-id
1393/// conversion-function-id
1394/// [C++0x] literal-operator-id [TODO]
1395/// ~ class-name
1396/// template-id
1397///
1398/// \endcode
1399///
1400/// \param The nested-name-specifier that preceded this unqualified-id. If
1401/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1402///
1403/// \param EnteringContext whether we are entering the scope of the
1404/// nested-name-specifier.
1405///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001406/// \param AllowDestructorName whether we allow parsing of a destructor name.
1407///
1408/// \param AllowConstructorName whether we allow parsing a constructor name.
1409///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001410/// \param ObjectType if this unqualified-id occurs within a member access
1411/// expression, the type of the base object whose member is being accessed.
1412///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001413/// \param Result on a successful parse, contains the parsed unqualified-id.
1414///
1415/// \returns true if parsing fails, false otherwise.
1416bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1417 bool AllowDestructorName,
1418 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001419 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001420 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001421
1422 // Handle 'A::template B'. This is for template-ids which have not
1423 // already been annotated by ParseOptionalCXXScopeSpecifier().
1424 bool TemplateSpecified = false;
1425 SourceLocation TemplateKWLoc;
1426 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1427 (ObjectType || SS.isSet())) {
1428 TemplateSpecified = true;
1429 TemplateKWLoc = ConsumeToken();
1430 }
1431
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001432 // unqualified-id:
1433 // identifier
1434 // template-id (when it hasn't already been annotated)
1435 if (Tok.is(tok::identifier)) {
1436 // Consume the identifier.
1437 IdentifierInfo *Id = Tok.getIdentifierInfo();
1438 SourceLocation IdLoc = ConsumeToken();
1439
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001440 if (!getLang().CPlusPlus) {
1441 // If we're not in C++, only identifiers matter. Record the
1442 // identifier and return.
1443 Result.setIdentifier(Id, IdLoc);
1444 return false;
1445 }
1446
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001447 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001448 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001449 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001450 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001451 &SS, false),
1452 IdLoc, IdLoc);
1453 } else {
1454 // We have parsed an identifier.
1455 Result.setIdentifier(Id, IdLoc);
1456 }
1457
1458 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001459 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001460 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001461 ObjectType, Result,
1462 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001463
1464 return false;
1465 }
1466
1467 // unqualified-id:
1468 // template-id (already parsed and annotated)
1469 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001470 TemplateIdAnnotation *TemplateId
1471 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1472
1473 // If the template-name names the current class, then this is a constructor
1474 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001475 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001476 if (SS.isSet()) {
1477 // C++ [class.qual]p2 specifies that a qualified template-name
1478 // is taken as the constructor name where a constructor can be
1479 // declared. Thus, the template arguments are extraneous, so
1480 // complain about them and remove them entirely.
1481 Diag(TemplateId->TemplateNameLoc,
1482 diag::err_out_of_line_constructor_template_id)
1483 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001484 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001485 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1486 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1487 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001488 getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001489 &SS, false),
1490 TemplateId->TemplateNameLoc,
1491 TemplateId->RAngleLoc);
1492 TemplateId->Destroy();
1493 ConsumeToken();
1494 return false;
1495 }
1496
1497 Result.setConstructorTemplateId(TemplateId);
1498 ConsumeToken();
1499 return false;
1500 }
1501
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001502 // We have already parsed a template-id; consume the annotation token as
1503 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001504 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001505 ConsumeToken();
1506 return false;
1507 }
1508
1509 // unqualified-id:
1510 // operator-function-id
1511 // conversion-function-id
1512 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001513 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001514 return true;
1515
Sean Hunte6252d12009-11-28 08:58:14 +00001516 // If we have an operator-function-id or a literal-operator-id and the next
1517 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001518 //
1519 // template-id:
1520 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001521 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1522 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001523 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001524 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1525 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001526 Result,
1527 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001528
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001529 return false;
1530 }
1531
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001532 if (getLang().CPlusPlus &&
1533 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001534 // C++ [expr.unary.op]p10:
1535 // There is an ambiguity in the unary-expression ~X(), where X is a
1536 // class-name. The ambiguity is resolved in favor of treating ~ as a
1537 // unary complement rather than treating ~X as referring to a destructor.
1538
1539 // Parse the '~'.
1540 SourceLocation TildeLoc = ConsumeToken();
1541
1542 // Parse the class-name.
1543 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001544 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001545 return true;
1546 }
1547
1548 // Parse the class-name (or template-name in a simple-template-id).
1549 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1550 SourceLocation ClassNameLoc = ConsumeToken();
1551
Douglas Gregor0278e122010-05-05 05:58:24 +00001552 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001553 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001554 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001555 EnteringContext, ObjectType, Result,
1556 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001557 }
1558
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001559 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001560 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1561 ClassNameLoc, getCurScope(),
1562 SS, ObjectType,
1563 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001564 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001565 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001566
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001567 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001568 return false;
1569 }
1570
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001571 Diag(Tok, diag::err_expected_unqualified_id)
1572 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001573 return true;
1574}
1575
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001576/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1577/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001578///
Chris Lattner59232d32009-01-04 21:25:24 +00001579/// This method is called to parse the new expression after the optional :: has
1580/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1581/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001582///
1583/// new-expression:
1584/// '::'[opt] 'new' new-placement[opt] new-type-id
1585/// new-initializer[opt]
1586/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1587/// new-initializer[opt]
1588///
1589/// new-placement:
1590/// '(' expression-list ')'
1591///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001592/// new-type-id:
1593/// type-specifier-seq new-declarator[opt]
1594///
1595/// new-declarator:
1596/// ptr-operator new-declarator[opt]
1597/// direct-new-declarator
1598///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001599/// new-initializer:
1600/// '(' expression-list[opt] ')'
1601/// [C++0x] braced-init-list [TODO]
1602///
John McCall60d7b3a2010-08-24 06:29:42 +00001603ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001604Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1605 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1606 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001607
1608 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1609 // second form of new-expression. It can't be a new-type-id.
1610
Sebastian Redla55e52c2008-11-25 22:21:31 +00001611 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001612 SourceLocation PlacementLParen, PlacementRParen;
1613
Douglas Gregor4bd40312010-07-13 15:54:32 +00001614 SourceRange TypeIdParens;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001615 DeclSpec DS;
1616 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001617 if (Tok.is(tok::l_paren)) {
1618 // If it turns out to be a placement, we change the type location.
1619 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001620 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1621 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001622 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001623 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001624
1625 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001626 if (PlacementRParen.isInvalid()) {
1627 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001628 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001629 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001630
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001631 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001632 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001633 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001634 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001635 } else {
1636 // We still need the type.
1637 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001638 TypeIdParens.setBegin(ConsumeParen());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001639 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001640 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001641 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001642 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1643 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001644 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001645 if (ParseCXXTypeSpecifierSeq(DS))
1646 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001647 else {
1648 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001649 ParseDeclaratorInternal(DeclaratorInfo,
1650 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001651 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001652 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001653 }
1654 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001655 // A new-type-id is a simplified type-id, where essentially the
1656 // direct-declarator is replaced by a direct-new-declarator.
1657 if (ParseCXXTypeSpecifierSeq(DS))
1658 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001659 else {
1660 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001661 ParseDeclaratorInternal(DeclaratorInfo,
1662 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001663 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001664 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001665 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001666 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001667 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001668 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001669
Sebastian Redla55e52c2008-11-25 22:21:31 +00001670 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001671 SourceLocation ConstructorLParen, ConstructorRParen;
1672
1673 if (Tok.is(tok::l_paren)) {
1674 ConstructorLParen = ConsumeParen();
1675 if (Tok.isNot(tok::r_paren)) {
1676 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001677 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1678 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001679 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001680 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001681 }
1682 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001683 if (ConstructorRParen.isInvalid()) {
1684 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 }
1688
Sebastian Redlf53597f2009-03-15 17:47:39 +00001689 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1690 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001691 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001692 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001693}
1694
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001695/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1696/// passed to ParseDeclaratorInternal.
1697///
1698/// direct-new-declarator:
1699/// '[' expression ']'
1700/// direct-new-declarator '[' constant-expression ']'
1701///
Chris Lattner59232d32009-01-04 21:25:24 +00001702void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001703 // Parse the array dimensions.
1704 bool first = true;
1705 while (Tok.is(tok::l_square)) {
1706 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001707 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001708 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001709 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001710 // Recover
1711 SkipUntil(tok::r_square);
1712 return;
1713 }
1714 first = false;
1715
Sebastian Redlab197ba2009-02-09 18:23:29 +00001716 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001717 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001718 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001719 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001720
Sebastian Redlab197ba2009-02-09 18:23:29 +00001721 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001722 return;
1723 }
1724}
1725
1726/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1727/// This ambiguity appears in the syntax of the C++ new operator.
1728///
1729/// new-expression:
1730/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1731/// new-initializer[opt]
1732///
1733/// new-placement:
1734/// '(' expression-list ')'
1735///
John McCallca0408f2010-08-23 06:44:23 +00001736bool Parser::ParseExpressionListOrTypeId(
1737 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001738 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001739 // The '(' was already consumed.
1740 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001741 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001742 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001743 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001744 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001745 }
1746
1747 // It's not a type, it has to be an expression list.
1748 // Discard the comma locations - ActOnCXXNew has enough parameters.
1749 CommaLocsTy CommaLocs;
1750 return ParseExpressionList(PlacementArgs, CommaLocs);
1751}
1752
1753/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1754/// to free memory allocated by new.
1755///
Chris Lattner59232d32009-01-04 21:25:24 +00001756/// This method is called to parse the 'delete' expression after the optional
1757/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1758/// and "Start" is its location. Otherwise, "Start" is the location of the
1759/// 'delete' token.
1760///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001761/// delete-expression:
1762/// '::'[opt] 'delete' cast-expression
1763/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001764ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001765Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1766 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1767 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001768
1769 // Array delete?
1770 bool ArrayDelete = false;
1771 if (Tok.is(tok::l_square)) {
1772 ArrayDelete = true;
1773 SourceLocation LHS = ConsumeBracket();
1774 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1775 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001776 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001777 }
1778
John McCall60d7b3a2010-08-24 06:29:42 +00001779 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001780 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001781 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001782
John McCall9ae2f072010-08-23 23:25:46 +00001783 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001784}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001785
Mike Stump1eb44332009-09-09 15:08:12 +00001786static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001787 switch(kind) {
1788 default: assert(false && "Not a known unary type trait.");
1789 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1790 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1791 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1792 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1793 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1794 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1795 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1796 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1797 case tok::kw___is_abstract: return UTT_IsAbstract;
1798 case tok::kw___is_class: return UTT_IsClass;
1799 case tok::kw___is_empty: return UTT_IsEmpty;
1800 case tok::kw___is_enum: return UTT_IsEnum;
1801 case tok::kw___is_pod: return UTT_IsPOD;
1802 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1803 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001804 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001805 }
1806}
1807
1808/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1809/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1810/// templates.
1811///
1812/// primary-expression:
1813/// [GNU] unary-type-trait '(' type-id ')'
1814///
John McCall60d7b3a2010-08-24 06:29:42 +00001815ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001816 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1817 SourceLocation Loc = ConsumeToken();
1818
1819 SourceLocation LParen = Tok.getLocation();
1820 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1821 return ExprError();
1822
1823 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1824 // there will be cryptic errors about mismatched parentheses and missing
1825 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001826 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001827
1828 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1829
Douglas Gregor809070a2009-02-18 17:45:20 +00001830 if (Ty.isInvalid())
1831 return ExprError();
1832
1833 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001834}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001835
1836/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1837/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1838/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00001839ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001840Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00001841 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001842 SourceLocation LParenLoc,
1843 SourceLocation &RParenLoc) {
1844 assert(getLang().CPlusPlus && "Should only be called for C++!");
1845 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1846 assert(isTypeIdInParens() && "Not a type-id!");
1847
John McCall60d7b3a2010-08-24 06:29:42 +00001848 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00001849 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001850
1851 // We need to disambiguate a very ugly part of the C++ syntax:
1852 //
1853 // (T())x; - type-id
1854 // (T())*x; - type-id
1855 // (T())/x; - expression
1856 // (T()); - expression
1857 //
1858 // The bad news is that we cannot use the specialized tentative parser, since
1859 // it can only verify that the thing inside the parens can be parsed as
1860 // type-id, it is not useful for determining the context past the parens.
1861 //
1862 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001863 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001864 //
1865 // It uses a scheme similar to parsing inline methods. The parenthesized
1866 // tokens are cached, the context that follows is determined (possibly by
1867 // parsing a cast-expression), and then we re-introduce the cached tokens
1868 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001869
Mike Stump1eb44332009-09-09 15:08:12 +00001870 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001871 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001872
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001873 // Store the tokens of the parentheses. We will parse them after we determine
1874 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001875 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001876 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001877 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1878 return ExprError();
1879 }
1880
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001881 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001882 ParseAs = CompoundLiteral;
1883 } else {
1884 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001885 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1886 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1887 NotCastExpr = true;
1888 } else {
1889 // Try parsing the cast-expression that may follow.
1890 // If it is not a cast-expression, NotCastExpr will be true and no token
1891 // will be consumed.
1892 Result = ParseCastExpression(false/*isUnaryExpression*/,
1893 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00001894 NotCastExpr,
1895 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001896 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001897
1898 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1899 // an expression.
1900 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001901 }
1902
Mike Stump1eb44332009-09-09 15:08:12 +00001903 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001904 Toks.push_back(Tok);
1905 // Re-enter the stored parenthesized tokens into the token stream, so we may
1906 // parse them now.
1907 PP.EnterTokenStream(Toks.data(), Toks.size(),
1908 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1909 // Drop the current token and bring the first cached one. It's the same token
1910 // as when we entered this function.
1911 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001912
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001913 if (ParseAs >= CompoundLiteral) {
1914 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001915
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001916 // Match the ')'.
1917 if (Tok.is(tok::r_paren))
1918 RParenLoc = ConsumeParen();
1919 else
1920 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001921
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001922 if (ParseAs == CompoundLiteral) {
1923 ExprType = CompoundLiteral;
1924 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1925 }
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001927 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1928 assert(ParseAs == CastExpr);
1929
1930 if (Ty.isInvalid())
1931 return ExprError();
1932
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001933 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001934
1935 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001936 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001937 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001938 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001939 return move(Result);
1940 }
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001942 // Not a compound literal, and not followed by a cast-expression.
1943 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001944
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001945 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001946 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001947 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00001948 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001949
1950 // Match the ')'.
1951 if (Result.isInvalid()) {
1952 SkipUntil(tok::r_paren);
1953 return ExprError();
1954 }
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001956 if (Tok.is(tok::r_paren))
1957 RParenLoc = ConsumeParen();
1958 else
1959 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1960
1961 return move(Result);
1962}