blob: 77cb4492f6750278649981a9ca97bc1826b7b4ce [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner29375652006-12-04 18:06:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner29375652006-12-04 18:06:35 +000015#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/DeclSpec.h"
17#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor7861a802009-11-03 01:35:08 +000018#include "llvm/Support/ErrorHandling.h"
19
Chris Lattner29375652006-12-04 18:06:35 +000020using namespace clang;
21
Mike Stump11289f42009-09-09 15:08:12 +000022/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +000023///
24/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +000025/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +000026/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-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 Gregorb7bfe792009-09-02 22:59:36 +000035/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000036///
Douglas Gregorb7bfe792009-09-02 22:59:36 +000037///
Mike Stump11289f42009-09-09 15:08:12 +000038/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +000039/// nested-name-specifier (or empty)
40///
Mike Stump11289f42009-09-09 15:08:12 +000041/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-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 Gregore610ada2010-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 Gregor90d554e2010-02-21 18:36:56 +000056/// member access expression, e.g., the \p T:: in \p p->T::m.
57///
John McCall1f476a12010-02-26 08:45:28 +000058/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +000059bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +000060 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +000061 bool EnteringContext,
Douglas Gregore610ada2010-02-24 18:44:31 +000062 bool *MayBePseudoDestructor) {
Argyrios Kyrtzidisace521a2008-11-26 21:41:52 +000063 assert(getLang().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +000064 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +000065
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000066 if (Tok.is(tok::annot_cxxscope)) {
John McCall37ad5512010-08-23 06:44:23 +000067 SS.setScopeRep(static_cast<NestedNameSpecifier*>(Tok.getAnnotationValue()));
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000068 SS.setRange(Tok.getAnnotationRange());
69 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +000070 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000071 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +000072
Douglas Gregor7f741122009-02-25 19:37:18 +000073 bool HasScopeSpecifier = false;
74
Chris Lattner8a7d10d2009-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 Stump11289f42009-09-09 15:08:12 +000080
Chris Lattner45ddec32009-01-05 00:13:00 +000081 // '::' - Global scope qualifier.
Chris Lattner6b87b9d2009-01-05 02:07:19 +000082 SourceLocation CCLoc = ConsumeToken();
Chris Lattner6b87b9d2009-01-05 02:07:19 +000083 SS.setBeginLoc(CCLoc);
Douglas Gregor0be31a22010-07-02 17:43:08 +000084 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), CCLoc));
Chris Lattner6b87b9d2009-01-05 02:07:19 +000085 SS.setEndLoc(CCLoc);
Douglas Gregor7f741122009-02-25 19:37:18 +000086 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000087 }
88
Douglas Gregore610ada2010-02-24 18:44:31 +000089 bool CheckForDestructor = false;
90 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
91 CheckForDestructor = true;
92 *MayBePseudoDestructor = false;
93 }
94
Douglas Gregor7f741122009-02-25 19:37:18 +000095 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +000096 if (HasScopeSpecifier) {
97 // C++ [basic.lookup.classref]p5:
98 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +000099 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000100 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000101 //
Douglas Gregorb7bfe792009-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 McCallba7bf592010-08-24 05:47:05 +0000107 ObjectType = ParsedType();
Douglas Gregor2436e712009-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 Gregor0be31a22010-07-02 17:43:08 +0000112 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000113 ConsumeCodeCompletionToken();
Douglas Gregor2436e712009-09-17 21:32:03 +0000114 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000115 }
Mike Stump11289f42009-09-09 15:08:12 +0000116
Douglas Gregor7f741122009-02-25 19:37:18 +0000117 // nested-name-specifier:
Chris Lattner0eed3a62009-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 Gregorb7bfe792009-09-02 22:59:36 +0000123 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000124 // nested-name-specifier, since they aren't allowed to start with
125 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000126 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000127 break;
128
Douglas Gregor120635b2009-11-11 16:39:34 +0000129 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000130 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000131
132 UnqualifiedId TemplateName;
133 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000134 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000135 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000136 ConsumeToken();
137 } else if (Tok.is(tok::kw_operator)) {
138 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000139 TemplateName)) {
140 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000141 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000142 }
Douglas Gregor71395fa2009-11-04 00:56:37 +0000143
Alexis Hunted0530f2009-11-28 08:58:14 +0000144 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
145 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000146 Diag(TemplateName.getSourceRange().getBegin(),
147 diag::err_id_after_template_in_nested_name_spec)
148 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000149 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000150 break;
151 }
152 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000153 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000154 break;
155 }
Mike Stump11289f42009-09-09 15:08:12 +0000156
Douglas Gregor120635b2009-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 Gregorbb119652010-06-16 23:00:59 +0000167 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000168 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregorbb119652010-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 McCall1f476a12010-02-26 08:45:28 +0000179 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000180
Chris Lattner0eed3a62009-06-26 03:47:46 +0000181 continue;
182 }
Mike Stump11289f42009-09-09 15:08:12 +0000183
Douglas Gregor7f741122009-02-25 19:37:18 +0000184 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000185 // We have
Douglas Gregor7f741122009-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 Gregorb67535d2009-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 Stump11289f42009-09-09 15:08:12 +0000192 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000193 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregore610ada2010-02-24 18:44:31 +0000194 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
195 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000196 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000197 }
198
Mike Stump11289f42009-09-09 15:08:12 +0000199 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorb67535d2009-03-31 00:43:58 +0000200 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000201 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor7f741122009-02-25 19:37:18 +0000202
Mike Stump11289f42009-09-09 15:08:12 +0000203 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor7f741122009-02-25 19:37:18 +0000204 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor7f741122009-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 Stump11289f42009-09-09 15:08:12 +0000209
Douglas Gregor7f741122009-02-25 19:37:18 +0000210 if (!HasScopeSpecifier) {
211 SS.setBeginLoc(TypeToken.getLocation());
212 HasScopeSpecifier = true;
213 }
Mike Stump11289f42009-09-09 15:08:12 +0000214
John McCallba7bf592010-08-24 05:47:05 +0000215 if (ParsedType T = getTypeAnnotation(TypeToken)) {
216 CXXScopeTy *Scope =
217 Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, T,
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000218 TypeToken.getAnnotationRange(),
John McCallba7bf592010-08-24 05:47:05 +0000219 CCLoc);
220 SS.setScopeRep(Scope);
221 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000222 SS.setScopeRep(0);
Douglas Gregor7f741122009-02-25 19:37:18 +0000223 SS.setEndLoc(CCLoc);
224 continue;
Chris Lattner704edfb2009-06-26 03:45:46 +0000225 }
Mike Stump11289f42009-09-09 15:08:12 +0000226
Chris Lattner704edfb2009-06-26 03:45:46 +0000227 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor7f741122009-02-25 19:37:18 +0000228 }
229
Chris Lattnere2355f72009-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 Lattner1c428032009-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 Gregor90d554e2010-02-21 18:36:56 +0000246 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000247 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II, ObjectType,
Douglas Gregor90d554e2010-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 Gregora771f462010-03-31 17:46:05 +0000254 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000255
256 // Recover as if the user wrote '::'.
257 Next.setKind(tok::coloncolon);
258 }
Chris Lattner1c428032009-12-07 01:36:53 +0000259 }
260
Chris Lattnere2355f72009-06-26 03:52:38 +0000261 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000262 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000263 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000264 II, ObjectType)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000265 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000266 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000267 }
268
Chris Lattnere2355f72009-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 Lattner1c428032009-12-07 01:36:53 +0000272 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
273 "NextToken() not working properly!");
Chris Lattnere2355f72009-06-26 03:52:38 +0000274 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000275
Chris Lattnere2355f72009-06-26 03:52:38 +0000276 if (!HasScopeSpecifier) {
277 SS.setBeginLoc(IdLoc);
278 HasScopeSpecifier = true;
279 }
Mike Stump11289f42009-09-09 15:08:12 +0000280
Argyrios Kyrtzidis75000b62010-06-22 11:30:04 +0000281 if (!SS.isInvalid())
282 SS.setScopeRep(
Douglas Gregor0be31a22010-07-02 17:43:08 +0000283 Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, IdLoc, CCLoc, II,
Argyrios Kyrtzidis75000b62010-06-22 11:30:04 +0000284 ObjectType, EnteringContext));
Chris Lattnere2355f72009-06-26 03:52:38 +0000285 SS.setEndLoc(CCLoc);
286 continue;
287 }
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattnere2355f72009-06-26 03:52:38 +0000289 // nested-name-specifier:
290 // type-name '<'
291 if (Next.is(tok::less)) {
292 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000293 UnqualifiedId TemplateName;
294 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000295 bool MemberOfUnknownSpecialization;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000296 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000297 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000298 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000299 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000300 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000301 Template,
302 MemberOfUnknownSpecialization)) {
Chris Lattnere2355f72009-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 Gregor71395fa2009-11-04 00:56:37 +0000309 ConsumeToken();
310 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
311 SourceLocation(), false))
John McCall1f476a12010-02-26 08:45:28 +0000312 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000313 continue;
Douglas Gregor20c38a72010-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 Gregorbb119652010-06-16 23:00:59 +0000326 if (TemplateNameKind TNK
Douglas Gregor0be31a22010-07-02 17:43:08 +0000327 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregorbb119652010-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 Gregor20c38a72010-05-21 23:43:39 +0000338 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000339
Douglas Gregor20c38a72010-05-21 23:43:39 +0000340 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000341 }
342 }
343
Douglas Gregor7f741122009-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 Kyrtzidis32a03792008-11-08 16:45:02 +0000347 }
Mike Stump11289f42009-09-09 15:08:12 +0000348
Douglas Gregore610ada2010-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 McCall1f476a12010-02-26 08:45:28 +0000355 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000356}
357
358/// ParseCXXIdExpression - Handle id-expression.
359///
360/// id-expression:
361/// unqualified-id
362/// qualified-id
363///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000364/// qualified-id:
365/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
366/// '::' identifier
367/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000368/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000369///
Argyrios Kyrtzidis32a03792008-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 Redl3d3f75a2009-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 McCalldadc5752010-08-24 06:29:42 +0000400ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-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 McCallba7bf592010-08-24 05:47:05 +0000406 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregora121b752009-11-03 16:56:39 +0000407
408 UnqualifiedId Name;
409 if (ParseUnqualifiedId(SS,
410 /*EnteringContext=*/false,
411 /*AllowDestructorName=*/false,
412 /*AllowConstructorName=*/false,
John McCallba7bf592010-08-24 05:47:05 +0000413 /*ObjectType=*/ ParsedType(),
Douglas Gregora121b752009-11-03 16:56:39 +0000414 Name))
415 return ExprError();
John McCalla9ee3252009-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 McCall8d08b9b2010-08-27 09:08:28 +0000419 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
420 isAddressOfOperand = false;
Douglas Gregora121b752009-11-03 16:56:39 +0000421
Douglas Gregor0be31a22010-07-02 17:43:08 +0000422 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregora121b752009-11-03 16:56:39 +0000423 isAddressOfOperand);
424
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000425}
426
Chris Lattner29375652006-12-04 18:06:35 +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 McCalldadc5752010-08-24 06:29:42 +0000436ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +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 Redld65cea82008-12-11 22:51:44 +0000452 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000453
Douglas Gregor220cac52009-02-18 17:45:20 +0000454 TypeResult CastTy = ParseTypeName();
Chris Lattner29375652006-12-04 18:06:35 +0000455 SourceLocation RAngleBracketLoc = Tok.getLocation();
456
Chris Lattner6d29c102008-11-18 07:48:38 +0000457 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +0000458 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +0000459
460 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
461
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000462 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
463 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000464
John McCalldadc5752010-08-24 06:29:42 +0000465 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +0000466
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000467 // Match the ')'.
Douglas Gregorf4f2ff72009-11-06 05:48:00 +0000468 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner29375652006-12-04 18:06:35 +0000469
Douglas Gregor220cac52009-02-18 17:45:20 +0000470 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregore200adc2008-10-27 19:41:14 +0000471 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000472 LAngleBracketLoc, CastTy.get(),
Douglas Gregor220cac52009-02-18 17:45:20 +0000473 RAngleBracketLoc,
John McCallb268a282010-08-23 23:25:46 +0000474 LParenLoc, Result.take(), RParenLoc);
Chris Lattner29375652006-12-04 18:06:35 +0000475
Sebastian Redld65cea82008-12-11 22:51:44 +0000476 return move(Result);
Chris Lattner29375652006-12-04 18:06:35 +0000477}
Bill Wendling4073ed52007-02-13 01:51:42 +0000478
Sebastian Redlc4704762008-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 McCalldadc5752010-08-24 06:29:42 +0000485ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-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 Redld65cea82008-12-11 22:51:44 +0000495 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +0000496
John McCalldadc5752010-08-24 06:29:42 +0000497 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +0000498
499 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +0000500 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +0000501
502 // Match the ')'.
Douglas Gregor4c7c1092010-09-08 23:14:30 +0000503 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +0000504
Douglas Gregor4c7c1092010-09-08 23:14:30 +0000505 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +0000506 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +0000507
508 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +0000509 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +0000510 } else {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000511 // C++0x [expr.typeid]p3:
Mike Stump11289f42009-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 Gregorc9c02ed2009-06-19 23:52:42 +0000514 // operand (Clause 5).
515 //
Mike Stump11289f42009-09-09 15:08:12 +0000516 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000517 // polymorphic class type until after we've parsed the expression, so
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000518 // we the expression is potentially potentially evaluated.
519 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallfaf5fb42010-08-26 23:41:50 +0000520 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc4704762008-11-11 11:37:55 +0000521 Result = ParseExpression();
522
523 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000524 if (Result.isInvalid())
Sebastian Redlc4704762008-11-11 11:37:55 +0000525 SkipUntil(tok::r_paren);
526 else {
Douglas Gregor4c7c1092010-09-08 23:14:30 +0000527 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
528 if (RParenLoc.isInvalid())
529 return ExprError();
530
Sebastian Redlc4704762008-11-11 11:37:55 +0000531 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000532 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +0000533 }
534 }
535
Sebastian Redld65cea82008-12-11 22:51:44 +0000536 return move(Result);
Sebastian Redlc4704762008-11-11 11:37:55 +0000537}
538
Francois Pichet9f4f2072010-09-08 12:20:18 +0000539/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
540///
541/// '__uuidof' '(' expression ')'
542/// '__uuidof' '(' type-id ')'
543///
544ExprResult Parser::ParseCXXUuidof() {
545 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
546
547 SourceLocation OpLoc = ConsumeToken();
548 SourceLocation LParenLoc = Tok.getLocation();
549 SourceLocation RParenLoc;
550
551 // __uuidof expressions are always parenthesized.
552 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
553 "__uuidof"))
554 return ExprError();
555
556 ExprResult Result;
557
558 if (isTypeIdInParens()) {
559 TypeResult Ty = ParseTypeName();
560
561 // Match the ')'.
562 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
563
564 if (Ty.isInvalid())
565 return ExprError();
566
567 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
568 Ty.get().getAsOpaquePtr(), RParenLoc);
569 } else {
570 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
571 Result = ParseExpression();
572
573 // Match the ')'.
574 if (Result.isInvalid())
575 SkipUntil(tok::r_paren);
576 else {
577 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
578
579 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
580 Result.release(), RParenLoc);
581 }
582 }
583
584 return move(Result);
585}
586
Douglas Gregore610ada2010-02-24 18:44:31 +0000587/// \brief Parse a C++ pseudo-destructor expression after the base,
588/// . or -> operator, and nested-name-specifier have already been
589/// parsed.
590///
591/// postfix-expression: [C++ 5.2]
592/// postfix-expression . pseudo-destructor-name
593/// postfix-expression -> pseudo-destructor-name
594///
595/// pseudo-destructor-name:
596/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
597/// ::[opt] nested-name-specifier template simple-template-id ::
598/// ~type-name
599/// ::[opt] nested-name-specifier[opt] ~type-name
600///
John McCalldadc5752010-08-24 06:29:42 +0000601ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +0000602Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
603 tok::TokenKind OpKind,
604 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000605 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000606 // We're parsing either a pseudo-destructor-name or a dependent
607 // member access that has the same form as a
608 // pseudo-destructor-name. We parse both in the same way and let
609 // the action model sort them out.
610 //
611 // Note that the ::[opt] nested-name-specifier[opt] has already
612 // been parsed, and if there was a simple-template-id, it has
613 // been coalesced into a template-id annotation token.
614 UnqualifiedId FirstTypeName;
615 SourceLocation CCLoc;
616 if (Tok.is(tok::identifier)) {
617 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
618 ConsumeToken();
619 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
620 CCLoc = ConsumeToken();
621 } else if (Tok.is(tok::annot_template_id)) {
622 FirstTypeName.setTemplateId(
623 (TemplateIdAnnotation *)Tok.getAnnotationValue());
624 ConsumeToken();
625 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
626 CCLoc = ConsumeToken();
627 } else {
628 FirstTypeName.setIdentifier(0, SourceLocation());
629 }
630
631 // Parse the tilde.
632 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
633 SourceLocation TildeLoc = ConsumeToken();
634 if (!Tok.is(tok::identifier)) {
635 Diag(Tok, diag::err_destructor_tilde_identifier);
636 return ExprError();
637 }
638
639 // Parse the second type.
640 UnqualifiedId SecondTypeName;
641 IdentifierInfo *Name = Tok.getIdentifierInfo();
642 SourceLocation NameLoc = ConsumeToken();
643 SecondTypeName.setIdentifier(Name, NameLoc);
644
645 // If there is a '<', the second type name is a template-id. Parse
646 // it as such.
647 if (Tok.is(tok::less) &&
648 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregorb22ee882010-05-05 05:58:24 +0000649 SecondTypeName, /*AssumeTemplateName=*/true,
650 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregore610ada2010-02-24 18:44:31 +0000651 return ExprError();
652
John McCallb268a282010-08-23 23:25:46 +0000653 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
654 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +0000655 SS, FirstTypeName, CCLoc,
656 TildeLoc, SecondTypeName,
657 Tok.is(tok::l_paren));
658}
659
Bill Wendling4073ed52007-02-13 01:51:42 +0000660/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
661///
662/// boolean-literal: [C++ 2.13.5]
663/// 'true'
664/// 'false'
John McCalldadc5752010-08-24 06:29:42 +0000665ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +0000666 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000667 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +0000668}
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000669
670/// ParseThrowExpression - This handles the C++ throw expression.
671///
672/// throw-expression: [C++ 15]
673/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +0000674ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000675 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000676 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +0000677
Chris Lattner65dd8432008-04-06 06:02:23 +0000678 // If the current token isn't the start of an assignment-expression,
679 // then the expression is not present. This handles things like:
680 // "C ? throw : (void)42", which is crazy but legal.
681 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
682 case tok::semi:
683 case tok::r_paren:
684 case tok::r_square:
685 case tok::r_brace:
686 case tok::colon:
687 case tok::comma:
John McCallb268a282010-08-23 23:25:46 +0000688 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000689
Chris Lattner65dd8432008-04-06 06:02:23 +0000690 default:
John McCalldadc5752010-08-24 06:29:42 +0000691 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redld65cea82008-12-11 22:51:44 +0000692 if (Expr.isInvalid()) return move(Expr);
John McCallb268a282010-08-23 23:25:46 +0000693 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +0000694 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000695}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000696
697/// ParseCXXThis - This handles the C++ 'this' pointer.
698///
699/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
700/// a non-lvalue expression whose value is the address of the object for which
701/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +0000702ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000703 assert(Tok.is(tok::kw_this) && "Not 'this'!");
704 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000705 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000706}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000707
708/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
709/// Can be interpreted either as function-style casting ("int(x)")
710/// or class type construction ("ClassType(x,y,z)")
711/// or creation of a value-initialized type ("int()").
712///
713/// postfix-expression: [C++ 5.2p1]
714/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
715/// typename-specifier '(' expression-list[opt] ')' [TODO]
716///
John McCalldadc5752010-08-24 06:29:42 +0000717ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +0000718Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000719 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +0000720 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000721
722 assert(Tok.is(tok::l_paren) && "Expected '('!");
723 SourceLocation LParenLoc = ConsumeParen();
724
Sebastian Redl511ed552008-11-25 22:21:31 +0000725 ExprVector Exprs(Actions);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000726 CommaLocsTy CommaLocs;
727
728 if (Tok.isNot(tok::r_paren)) {
729 if (ParseExpressionList(Exprs, CommaLocs)) {
730 SkipUntil(tok::r_paren);
Sebastian Redld65cea82008-12-11 22:51:44 +0000731 return ExprError();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000732 }
733 }
734
735 // Match the ')'.
736 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
737
Sebastian Redl955a0672009-07-29 13:50:23 +0000738 // TypeRep could be null, if it references an invalid typedef.
739 if (!TypeRep)
740 return ExprError();
741
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000742 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
743 "Unexpected number of commas!");
Douglas Gregor2b88c112010-09-08 00:15:04 +0000744 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Douglas Gregorce5aa332010-09-09 16:33:13 +0000745 RParenLoc);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000746}
747
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000748/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000749///
750/// condition:
751/// expression
752/// type-specifier-seq declarator '=' assignment-expression
753/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
754/// '=' assignment-expression
755///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000756/// \param ExprResult if the condition was parsed as an expression, the
757/// parsed expression.
758///
759/// \param DeclResult if the condition was parsed as a declaration, the
760/// parsed declaration.
761///
Douglas Gregore60e41a2010-05-06 17:25:47 +0000762/// \param Loc The location of the start of the statement that requires this
763/// condition, e.g., the "for" in a for loop.
764///
765/// \param ConvertToBoolean Whether the condition expression should be
766/// converted to a boolean value.
767///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000768/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +0000769bool Parser::ParseCXXCondition(ExprResult &ExprOut,
770 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000771 SourceLocation Loc,
772 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000773 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +0000774 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000775 ConsumeCodeCompletionToken();
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000776 }
777
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000778 if (!isCXXConditionDeclaration()) {
Douglas Gregore60e41a2010-05-06 17:25:47 +0000779 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +0000780 ExprOut = ParseExpression(); // expression
781 DeclOut = 0;
782 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +0000783 return true;
784
785 // If required, convert to a boolean value.
786 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +0000787 ExprOut
788 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
789 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000790 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000791
792 // type-specifier-seq
793 DeclSpec DS;
794 ParseSpecifierQualifierList(DS);
795
796 // declarator
797 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
798 ParseDeclarator(DeclaratorInfo);
799
800 // simple-asm-expr[opt]
801 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000802 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +0000803 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000804 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000805 SkipUntil(tok::semi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000806 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000807 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000808 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000809 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000810 }
811
812 // If attributes are present, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000813 if (Tok.is(tok::kw___attribute)) {
814 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000815 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000816 DeclaratorInfo.AddAttributes(AttrList, Loc);
817 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000818
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000819 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +0000820 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000821 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +0000822 DeclOut = Dcl.get();
823 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000824
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000825 // '=' assignment-expression
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000826 if (isTokenEqualOrMistypedEqualEqual(
827 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000828 SourceLocation EqualLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +0000829 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000830 if (!AssignExpr.isInvalid())
John McCalldadc5752010-08-24 06:29:42 +0000831 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000832 } else {
833 // FIXME: C++0x allows a braced-init-list
834 Diag(Tok, diag::err_expected_equal_after_declarator);
835 }
836
Douglas Gregore60e41a2010-05-06 17:25:47 +0000837 // FIXME: Build a reference to this declaration? Convert it to bool?
838 // (This is currently handled by Sema).
839
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000840 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000841}
842
Douglas Gregor8d4de672010-04-21 22:36:40 +0000843/// \brief Determine whether the current token starts a C++
844/// simple-type-specifier.
845bool Parser::isCXXSimpleTypeSpecifier() const {
846 switch (Tok.getKind()) {
847 case tok::annot_typename:
848 case tok::kw_short:
849 case tok::kw_long:
850 case tok::kw_signed:
851 case tok::kw_unsigned:
852 case tok::kw_void:
853 case tok::kw_char:
854 case tok::kw_int:
855 case tok::kw_float:
856 case tok::kw_double:
857 case tok::kw_wchar_t:
858 case tok::kw_char16_t:
859 case tok::kw_char32_t:
860 case tok::kw_bool:
861 // FIXME: C++0x decltype support.
862 // GNU typeof support.
863 case tok::kw_typeof:
864 return true;
865
866 default:
867 break;
868 }
869
870 return false;
871}
872
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000873/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
874/// This should only be called when the current token is known to be part of
875/// simple-type-specifier.
876///
877/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000878/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000879/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
880/// char
881/// wchar_t
882/// bool
883/// short
884/// int
885/// long
886/// signed
887/// unsigned
888/// float
889/// double
890/// void
891/// [GNU] typeof-specifier
892/// [C++0x] auto [TODO]
893///
894/// type-name:
895/// class-name
896/// enum-name
897/// typedef-name
898///
899void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
900 DS.SetRangeStart(Tok.getLocation());
901 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000902 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000903 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000904
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000905 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +0000906 case tok::identifier: // foo::bar
907 case tok::coloncolon: // ::foo::bar
908 assert(0 && "Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +0000909 default:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000910 assert(0 && "Not a simple-type-specifier token!");
911 abort();
Chris Lattner45ddec32009-01-05 00:13:00 +0000912
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000913 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +0000914 case tok::annot_typename: {
John McCall49bfce42009-08-03 20:12:06 +0000915 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +0000916 getTypeAnnotation(Tok));
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000917 break;
918 }
Mike Stump11289f42009-09-09 15:08:12 +0000919
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000920 // builtin types
921 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +0000922 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000923 break;
924 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +0000925 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000926 break;
927 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +0000928 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000929 break;
930 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +0000931 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000932 break;
933 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +0000934 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000935 break;
936 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +0000937 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000938 break;
939 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +0000940 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000941 break;
942 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +0000943 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000944 break;
945 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +0000946 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000947 break;
948 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +0000949 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000950 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000951 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +0000952 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000953 break;
954 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +0000955 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000956 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000957 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +0000958 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000959 break;
Mike Stump11289f42009-09-09 15:08:12 +0000960
Douglas Gregor8d4de672010-04-21 22:36:40 +0000961 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000962 // GNU typeof support.
963 case tok::kw_typeof:
964 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +0000965 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000966 return;
967 }
Chris Lattnera8a3f732009-01-06 05:06:21 +0000968 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000969 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
970 else
971 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000972 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +0000973 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000974}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000975
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000976/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
977/// [dcl.name]), which is a non-empty sequence of type-specifiers,
978/// e.g., "const short int". Note that the DeclSpec is *not* finished
979/// by parsing the type-specifier-seq, because these sequences are
980/// typically followed by some form of declarator. Returns true and
981/// emits diagnostics if this is not a type-specifier-seq, false
982/// otherwise.
983///
984/// type-specifier-seq: [C++ 8.1]
985/// type-specifier type-specifier-seq[opt]
986///
987bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
988 DS.SetRangeStart(Tok.getLocation());
989 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000990 unsigned DiagID;
991 bool isInvalid = 0;
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000992
993 // Parse one or more of the type specifiers.
Sebastian Redl2b372722010-02-03 21:21:43 +0000994 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
995 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000996 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000997 return true;
998 }
Mike Stump11289f42009-09-09 15:08:12 +0000999
Sebastian Redl2b372722010-02-03 21:21:43 +00001000 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1001 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1002 {}
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001003
Douglas Gregor40d732f2010-02-24 23:13:13 +00001004 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001005 return false;
1006}
1007
Douglas Gregor7861a802009-11-03 01:35:08 +00001008/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1009/// some form.
1010///
1011/// This routine is invoked when a '<' is encountered after an identifier or
1012/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1013/// whether the unqualified-id is actually a template-id. This routine will
1014/// then parse the template arguments and form the appropriate template-id to
1015/// return to the caller.
1016///
1017/// \param SS the nested-name-specifier that precedes this template-id, if
1018/// we're actually parsing a qualified-id.
1019///
1020/// \param Name for constructor and destructor names, this is the actual
1021/// identifier that may be a template-name.
1022///
1023/// \param NameLoc the location of the class-name in a constructor or
1024/// destructor.
1025///
1026/// \param EnteringContext whether we're entering the scope of the
1027/// nested-name-specifier.
1028///
Douglas Gregor127ea592009-11-03 21:24:04 +00001029/// \param ObjectType if this unqualified-id occurs within a member access
1030/// expression, the type of the base object whose member is being accessed.
1031///
Douglas Gregor7861a802009-11-03 01:35:08 +00001032/// \param Id as input, describes the template-name or operator-function-id
1033/// that precedes the '<'. If template arguments were parsed successfully,
1034/// will be updated with the template-id.
1035///
Douglas Gregore610ada2010-02-24 18:44:31 +00001036/// \param AssumeTemplateId When true, this routine will assume that the name
1037/// refers to a template without performing name lookup to verify.
1038///
Douglas Gregor7861a802009-11-03 01:35:08 +00001039/// \returns true if a parse error occurred, false otherwise.
1040bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1041 IdentifierInfo *Name,
1042 SourceLocation NameLoc,
1043 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001044 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001045 UnqualifiedId &Id,
Douglas Gregorb22ee882010-05-05 05:58:24 +00001046 bool AssumeTemplateId,
1047 SourceLocation TemplateKWLoc) {
1048 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1049 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001050
1051 TemplateTy Template;
1052 TemplateNameKind TNK = TNK_Non_template;
1053 switch (Id.getKind()) {
1054 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001055 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001056 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001057 if (AssumeTemplateId) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001058 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregorbb119652010-06-16 23:00:59 +00001059 Id, ObjectType, EnteringContext,
1060 Template);
1061 if (TNK == TNK_Non_template)
1062 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001063 } else {
1064 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001065 TNK = Actions.isTemplateName(getCurScope(), SS,
1066 TemplateKWLoc.isValid(), Id,
1067 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001068 MemberOfUnknownSpecialization);
1069
1070 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1071 ObjectType && IsTemplateArgumentList()) {
1072 // We have something like t->getAs<T>(), where getAs is a
1073 // member of an unknown specialization. However, this will only
1074 // parse correctly as a template, so suggest the keyword 'template'
1075 // before 'getAs' and treat this as a dependent template name.
1076 std::string Name;
1077 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1078 Name = Id.Identifier->getName();
1079 else {
1080 Name = "operator ";
1081 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1082 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1083 else
1084 Name += Id.Identifier->getName();
1085 }
1086 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1087 << Name
1088 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor0be31a22010-07-02 17:43:08 +00001089 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001090 SS, Id, ObjectType,
1091 EnteringContext, Template);
1092 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001093 return true;
1094 }
1095 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001096 break;
1097
Douglas Gregor3cf81312009-11-03 23:16:33 +00001098 case UnqualifiedId::IK_ConstructorName: {
1099 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001100 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001101 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001102 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1103 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001104 EnteringContext, Template,
1105 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001106 break;
1107 }
1108
Douglas Gregor3cf81312009-11-03 23:16:33 +00001109 case UnqualifiedId::IK_DestructorName: {
1110 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001111 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001112 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001113 if (ObjectType) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001114 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregorbb119652010-06-16 23:00:59 +00001115 TemplateName, ObjectType,
1116 EnteringContext, Template);
1117 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001118 return true;
1119 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001120 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1121 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001122 EnteringContext, Template,
1123 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001124
John McCallba7bf592010-08-24 05:47:05 +00001125 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001126 Diag(NameLoc, diag::err_destructor_template_id)
1127 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001128 return true;
1129 }
1130 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001131 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001132 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001133
1134 default:
1135 return false;
1136 }
1137
1138 if (TNK == TNK_Non_template)
1139 return false;
1140
1141 // Parse the enclosed template argument list.
1142 SourceLocation LAngleLoc, RAngleLoc;
1143 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001144 if (Tok.is(tok::less) &&
1145 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor7861a802009-11-03 01:35:08 +00001146 &SS, true, LAngleLoc,
1147 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001148 RAngleLoc))
1149 return true;
1150
1151 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001152 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1153 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001154 // Form a parsed representation of the template-id to be stored in the
1155 // UnqualifiedId.
1156 TemplateIdAnnotation *TemplateId
1157 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1158
1159 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1160 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001161 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001162 TemplateId->TemplateNameLoc = Id.StartLocation;
1163 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001164 TemplateId->Name = 0;
1165 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1166 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001167 }
1168
John McCall3e56fd42010-08-23 07:28:44 +00001169 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001170 TemplateId->Kind = TNK;
1171 TemplateId->LAngleLoc = LAngleLoc;
1172 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001173 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001174 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001175 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001176 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001177
1178 Id.setTemplateId(TemplateId);
1179 return false;
1180 }
1181
1182 // Bundle the template arguments together.
1183 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor7861a802009-11-03 01:35:08 +00001184 TemplateArgs.size());
1185
1186 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001187 TypeResult Type
Douglas Gregor7861a802009-11-03 01:35:08 +00001188 = Actions.ActOnTemplateIdType(Template, NameLoc,
1189 LAngleLoc, TemplateArgsPtr,
Douglas Gregor7861a802009-11-03 01:35:08 +00001190 RAngleLoc);
1191 if (Type.isInvalid())
1192 return true;
1193
1194 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1195 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1196 else
1197 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1198
1199 return false;
1200}
1201
Douglas Gregor71395fa2009-11-04 00:56:37 +00001202/// \brief Parse an operator-function-id or conversion-function-id as part
1203/// of a C++ unqualified-id.
1204///
1205/// This routine is responsible only for parsing the operator-function-id or
1206/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001207///
1208/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001209/// operator-function-id: [C++ 13.5]
1210/// 'operator' operator
1211///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001212/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001213/// new delete new[] delete[]
1214/// + - * / % ^ & | ~
1215/// ! = < > += -= *= /= %=
1216/// ^= &= |= << >> >>= <<= == !=
1217/// <= >= && || ++ -- , ->* ->
1218/// () []
1219///
1220/// conversion-function-id: [C++ 12.3.2]
1221/// operator conversion-type-id
1222///
1223/// conversion-type-id:
1224/// type-specifier-seq conversion-declarator[opt]
1225///
1226/// conversion-declarator:
1227/// ptr-operator conversion-declarator[opt]
1228/// \endcode
1229///
1230/// \param The nested-name-specifier that preceded this unqualified-id. If
1231/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1232///
1233/// \param EnteringContext whether we are entering the scope of the
1234/// nested-name-specifier.
1235///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001236/// \param ObjectType if this unqualified-id occurs within a member access
1237/// expression, the type of the base object whose member is being accessed.
1238///
1239/// \param Result on a successful parse, contains the parsed unqualified-id.
1240///
1241/// \returns true if parsing fails, false otherwise.
1242bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001243 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001244 UnqualifiedId &Result) {
1245 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1246
1247 // Consume the 'operator' keyword.
1248 SourceLocation KeywordLoc = ConsumeToken();
1249
1250 // Determine what kind of operator name we have.
1251 unsigned SymbolIdx = 0;
1252 SourceLocation SymbolLocations[3];
1253 OverloadedOperatorKind Op = OO_None;
1254 switch (Tok.getKind()) {
1255 case tok::kw_new:
1256 case tok::kw_delete: {
1257 bool isNew = Tok.getKind() == tok::kw_new;
1258 // Consume the 'new' or 'delete'.
1259 SymbolLocations[SymbolIdx++] = ConsumeToken();
1260 if (Tok.is(tok::l_square)) {
1261 // Consume the '['.
1262 SourceLocation LBracketLoc = ConsumeBracket();
1263 // Consume the ']'.
1264 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1265 LBracketLoc);
1266 if (RBracketLoc.isInvalid())
1267 return true;
1268
1269 SymbolLocations[SymbolIdx++] = LBracketLoc;
1270 SymbolLocations[SymbolIdx++] = RBracketLoc;
1271 Op = isNew? OO_Array_New : OO_Array_Delete;
1272 } else {
1273 Op = isNew? OO_New : OO_Delete;
1274 }
1275 break;
1276 }
1277
1278#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1279 case tok::Token: \
1280 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1281 Op = OO_##Name; \
1282 break;
1283#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1284#include "clang/Basic/OperatorKinds.def"
1285
1286 case tok::l_paren: {
1287 // Consume the '('.
1288 SourceLocation LParenLoc = ConsumeParen();
1289 // Consume the ')'.
1290 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1291 LParenLoc);
1292 if (RParenLoc.isInvalid())
1293 return true;
1294
1295 SymbolLocations[SymbolIdx++] = LParenLoc;
1296 SymbolLocations[SymbolIdx++] = RParenLoc;
1297 Op = OO_Call;
1298 break;
1299 }
1300
1301 case tok::l_square: {
1302 // Consume the '['.
1303 SourceLocation LBracketLoc = ConsumeBracket();
1304 // Consume the ']'.
1305 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1306 LBracketLoc);
1307 if (RBracketLoc.isInvalid())
1308 return true;
1309
1310 SymbolLocations[SymbolIdx++] = LBracketLoc;
1311 SymbolLocations[SymbolIdx++] = RBracketLoc;
1312 Op = OO_Subscript;
1313 break;
1314 }
1315
1316 case tok::code_completion: {
1317 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001318 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregor71395fa2009-11-04 00:56:37 +00001319
1320 // Consume the operator token.
Douglas Gregor6da3db42010-05-25 05:58:43 +00001321 ConsumeCodeCompletionToken();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001322
1323 // Don't try to parse any further.
1324 return true;
1325 }
1326
1327 default:
1328 break;
1329 }
1330
1331 if (Op != OO_None) {
1332 // We have parsed an operator-function-id.
1333 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1334 return false;
1335 }
Alexis Hunt34458502009-11-28 04:44:28 +00001336
1337 // Parse a literal-operator-id.
1338 //
1339 // literal-operator-id: [C++0x 13.5.8]
1340 // operator "" identifier
1341
1342 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1343 if (Tok.getLength() != 2)
1344 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1345 ConsumeStringToken();
1346
1347 if (Tok.isNot(tok::identifier)) {
1348 Diag(Tok.getLocation(), diag::err_expected_ident);
1349 return true;
1350 }
1351
1352 IdentifierInfo *II = Tok.getIdentifierInfo();
1353 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Alexis Hunt3d221f22009-11-29 07:34:05 +00001354 return false;
Alexis Hunt34458502009-11-28 04:44:28 +00001355 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00001356
1357 // Parse a conversion-function-id.
1358 //
1359 // conversion-function-id: [C++ 12.3.2]
1360 // operator conversion-type-id
1361 //
1362 // conversion-type-id:
1363 // type-specifier-seq conversion-declarator[opt]
1364 //
1365 // conversion-declarator:
1366 // ptr-operator conversion-declarator[opt]
1367
1368 // Parse the type-specifier-seq.
1369 DeclSpec DS;
Douglas Gregora25d65d2009-11-20 22:03:38 +00001370 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00001371 return true;
1372
1373 // Parse the conversion-declarator, which is merely a sequence of
1374 // ptr-operators.
1375 Declarator D(DS, Declarator::TypeNameContext);
1376 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1377
1378 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00001379 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00001380 if (Ty.isInvalid())
1381 return true;
1382
1383 // Note that this is a conversion-function-id.
1384 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1385 D.getSourceRange().getEnd());
1386 return false;
1387}
1388
1389/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1390/// name of an entity.
1391///
1392/// \code
1393/// unqualified-id: [C++ expr.prim.general]
1394/// identifier
1395/// operator-function-id
1396/// conversion-function-id
1397/// [C++0x] literal-operator-id [TODO]
1398/// ~ class-name
1399/// template-id
1400///
1401/// \endcode
1402///
1403/// \param The nested-name-specifier that preceded this unqualified-id. If
1404/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1405///
1406/// \param EnteringContext whether we are entering the scope of the
1407/// nested-name-specifier.
1408///
Douglas Gregor7861a802009-11-03 01:35:08 +00001409/// \param AllowDestructorName whether we allow parsing of a destructor name.
1410///
1411/// \param AllowConstructorName whether we allow parsing a constructor name.
1412///
Douglas Gregor127ea592009-11-03 21:24:04 +00001413/// \param ObjectType if this unqualified-id occurs within a member access
1414/// expression, the type of the base object whose member is being accessed.
1415///
Douglas Gregor7861a802009-11-03 01:35:08 +00001416/// \param Result on a successful parse, contains the parsed unqualified-id.
1417///
1418/// \returns true if parsing fails, false otherwise.
1419bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1420 bool AllowDestructorName,
1421 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00001422 ParsedType ObjectType,
Douglas Gregor7861a802009-11-03 01:35:08 +00001423 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001424
1425 // Handle 'A::template B'. This is for template-ids which have not
1426 // already been annotated by ParseOptionalCXXScopeSpecifier().
1427 bool TemplateSpecified = false;
1428 SourceLocation TemplateKWLoc;
1429 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1430 (ObjectType || SS.isSet())) {
1431 TemplateSpecified = true;
1432 TemplateKWLoc = ConsumeToken();
1433 }
1434
Douglas Gregor7861a802009-11-03 01:35:08 +00001435 // unqualified-id:
1436 // identifier
1437 // template-id (when it hasn't already been annotated)
1438 if (Tok.is(tok::identifier)) {
1439 // Consume the identifier.
1440 IdentifierInfo *Id = Tok.getIdentifierInfo();
1441 SourceLocation IdLoc = ConsumeToken();
1442
Douglas Gregor411e5ac2010-01-11 23:29:10 +00001443 if (!getLang().CPlusPlus) {
1444 // If we're not in C++, only identifiers matter. Record the
1445 // identifier and return.
1446 Result.setIdentifier(Id, IdLoc);
1447 return false;
1448 }
1449
Douglas Gregor7861a802009-11-03 01:35:08 +00001450 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001451 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001452 // We have parsed a constructor name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001453 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor7861a802009-11-03 01:35:08 +00001454 &SS, false),
1455 IdLoc, IdLoc);
1456 } else {
1457 // We have parsed an identifier.
1458 Result.setIdentifier(Id, IdLoc);
1459 }
1460
1461 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00001462 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor7861a802009-11-03 01:35:08 +00001463 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregorb22ee882010-05-05 05:58:24 +00001464 ObjectType, Result,
1465 TemplateSpecified, TemplateKWLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00001466
1467 return false;
1468 }
1469
1470 // unqualified-id:
1471 // template-id (already parsed and annotated)
1472 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001473 TemplateIdAnnotation *TemplateId
1474 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1475
1476 // If the template-name names the current class, then this is a constructor
1477 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001478 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001479 if (SS.isSet()) {
1480 // C++ [class.qual]p2 specifies that a qualified template-name
1481 // is taken as the constructor name where a constructor can be
1482 // declared. Thus, the template arguments are extraneous, so
1483 // complain about them and remove them entirely.
1484 Diag(TemplateId->TemplateNameLoc,
1485 diag::err_out_of_line_constructor_template_id)
1486 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00001487 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001488 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1489 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1490 TemplateId->TemplateNameLoc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001491 getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001492 &SS, false),
1493 TemplateId->TemplateNameLoc,
1494 TemplateId->RAngleLoc);
1495 TemplateId->Destroy();
1496 ConsumeToken();
1497 return false;
1498 }
1499
1500 Result.setConstructorTemplateId(TemplateId);
1501 ConsumeToken();
1502 return false;
1503 }
1504
Douglas Gregor7861a802009-11-03 01:35:08 +00001505 // We have already parsed a template-id; consume the annotation token as
1506 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001507 Result.setTemplateId(TemplateId);
Douglas Gregor7861a802009-11-03 01:35:08 +00001508 ConsumeToken();
1509 return false;
1510 }
1511
1512 // unqualified-id:
1513 // operator-function-id
1514 // conversion-function-id
1515 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00001516 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00001517 return true;
1518
Alexis Hunted0530f2009-11-28 08:58:14 +00001519 // If we have an operator-function-id or a literal-operator-id and the next
1520 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00001521 //
1522 // template-id:
1523 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00001524 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1525 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00001526 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregor71395fa2009-11-04 00:56:37 +00001527 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1528 EnteringContext, ObjectType,
Douglas Gregorb22ee882010-05-05 05:58:24 +00001529 Result,
1530 TemplateSpecified, TemplateKWLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00001531
Douglas Gregor7861a802009-11-03 01:35:08 +00001532 return false;
1533 }
1534
Douglas Gregor411e5ac2010-01-11 23:29:10 +00001535 if (getLang().CPlusPlus &&
1536 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001537 // C++ [expr.unary.op]p10:
1538 // There is an ambiguity in the unary-expression ~X(), where X is a
1539 // class-name. The ambiguity is resolved in favor of treating ~ as a
1540 // unary complement rather than treating ~X as referring to a destructor.
1541
1542 // Parse the '~'.
1543 SourceLocation TildeLoc = ConsumeToken();
1544
1545 // Parse the class-name.
1546 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001547 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00001548 return true;
1549 }
1550
1551 // Parse the class-name (or template-name in a simple-template-id).
1552 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1553 SourceLocation ClassNameLoc = ConsumeToken();
1554
Douglas Gregorb22ee882010-05-05 05:58:24 +00001555 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00001556 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001557 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregorb22ee882010-05-05 05:58:24 +00001558 EnteringContext, ObjectType, Result,
1559 TemplateSpecified, TemplateKWLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001560 }
1561
Douglas Gregor7861a802009-11-03 01:35:08 +00001562 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00001563 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1564 ClassNameLoc, getCurScope(),
1565 SS, ObjectType,
1566 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00001567 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00001568 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00001569
Douglas Gregor7861a802009-11-03 01:35:08 +00001570 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00001571 return false;
1572 }
1573
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001574 Diag(Tok, diag::err_expected_unqualified_id)
1575 << getLang().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00001576 return true;
1577}
1578
Sebastian Redlbd150f42008-11-21 19:14:01 +00001579/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1580/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00001581///
Chris Lattner109faf22009-01-04 21:25:24 +00001582/// This method is called to parse the new expression after the optional :: has
1583/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1584/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00001585///
1586/// new-expression:
1587/// '::'[opt] 'new' new-placement[opt] new-type-id
1588/// new-initializer[opt]
1589/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1590/// new-initializer[opt]
1591///
1592/// new-placement:
1593/// '(' expression-list ')'
1594///
Sebastian Redl351bb782008-12-02 14:43:59 +00001595/// new-type-id:
1596/// type-specifier-seq new-declarator[opt]
1597///
1598/// new-declarator:
1599/// ptr-operator new-declarator[opt]
1600/// direct-new-declarator
1601///
Sebastian Redlbd150f42008-11-21 19:14:01 +00001602/// new-initializer:
1603/// '(' expression-list[opt] ')'
1604/// [C++0x] braced-init-list [TODO]
1605///
John McCalldadc5752010-08-24 06:29:42 +00001606ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00001607Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1608 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1609 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00001610
1611 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1612 // second form of new-expression. It can't be a new-type-id.
1613
Sebastian Redl511ed552008-11-25 22:21:31 +00001614 ExprVector PlacementArgs(Actions);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001615 SourceLocation PlacementLParen, PlacementRParen;
1616
Douglas Gregorf2753b32010-07-13 15:54:32 +00001617 SourceRange TypeIdParens;
Sebastian Redl351bb782008-12-02 14:43:59 +00001618 DeclSpec DS;
1619 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001620 if (Tok.is(tok::l_paren)) {
1621 // If it turns out to be a placement, we change the type location.
1622 PlacementLParen = ConsumeParen();
Sebastian Redl351bb782008-12-02 14:43:59 +00001623 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1624 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00001625 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00001626 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00001627
1628 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redl351bb782008-12-02 14:43:59 +00001629 if (PlacementRParen.isInvalid()) {
1630 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00001631 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00001632 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00001633
Sebastian Redl351bb782008-12-02 14:43:59 +00001634 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001635 // Reset the placement locations. There was no placement.
Douglas Gregorf2753b32010-07-13 15:54:32 +00001636 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001637 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00001638 } else {
1639 // We still need the type.
1640 if (Tok.is(tok::l_paren)) {
Douglas Gregorf2753b32010-07-13 15:54:32 +00001641 TypeIdParens.setBegin(ConsumeParen());
Sebastian Redl351bb782008-12-02 14:43:59 +00001642 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001643 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00001644 ParseDeclarator(DeclaratorInfo);
Douglas Gregorf2753b32010-07-13 15:54:32 +00001645 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1646 TypeIdParens.getBegin()));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001647 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00001648 if (ParseCXXTypeSpecifierSeq(DS))
1649 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001650 else {
1651 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00001652 ParseDeclaratorInternal(DeclaratorInfo,
1653 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001654 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00001655 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00001656 }
1657 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00001658 // A new-type-id is a simplified type-id, where essentially the
1659 // direct-declarator is replaced by a direct-new-declarator.
1660 if (ParseCXXTypeSpecifierSeq(DS))
1661 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001662 else {
1663 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00001664 ParseDeclaratorInternal(DeclaratorInfo,
1665 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001666 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00001667 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00001668 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00001669 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00001670 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00001671 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00001672
Sebastian Redl511ed552008-11-25 22:21:31 +00001673 ExprVector ConstructorArgs(Actions);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001674 SourceLocation ConstructorLParen, ConstructorRParen;
1675
1676 if (Tok.is(tok::l_paren)) {
1677 ConstructorLParen = ConsumeParen();
1678 if (Tok.isNot(tok::r_paren)) {
1679 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00001680 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1681 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00001682 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00001683 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00001684 }
1685 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redl351bb782008-12-02 14:43:59 +00001686 if (ConstructorRParen.isInvalid()) {
1687 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00001688 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00001689 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00001690 }
1691
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001692 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1693 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregorf2753b32010-07-13 15:54:32 +00001694 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001695 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001696}
1697
Sebastian Redlbd150f42008-11-21 19:14:01 +00001698/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1699/// passed to ParseDeclaratorInternal.
1700///
1701/// direct-new-declarator:
1702/// '[' expression ']'
1703/// direct-new-declarator '[' constant-expression ']'
1704///
Chris Lattner109faf22009-01-04 21:25:24 +00001705void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001706 // Parse the array dimensions.
1707 bool first = true;
1708 while (Tok.is(tok::l_square)) {
1709 SourceLocation LLoc = ConsumeBracket();
John McCalldadc5752010-08-24 06:29:42 +00001710 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00001711 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001712 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001713 // Recover
1714 SkipUntil(tok::r_square);
1715 return;
1716 }
1717 first = false;
1718
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001719 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001720 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor04318252009-07-06 15:59:29 +00001721 Size.release(), LLoc, RLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001722 RLoc);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001723
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001724 if (RLoc.isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00001725 return;
1726 }
1727}
1728
1729/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1730/// This ambiguity appears in the syntax of the C++ new operator.
1731///
1732/// new-expression:
1733/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1734/// new-initializer[opt]
1735///
1736/// new-placement:
1737/// '(' expression-list ')'
1738///
John McCall37ad5512010-08-23 06:44:23 +00001739bool Parser::ParseExpressionListOrTypeId(
1740 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00001741 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001742 // The '(' was already consumed.
1743 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00001744 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001745 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00001746 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00001747 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00001748 }
1749
1750 // It's not a type, it has to be an expression list.
1751 // Discard the comma locations - ActOnCXXNew has enough parameters.
1752 CommaLocsTy CommaLocs;
1753 return ParseExpressionList(PlacementArgs, CommaLocs);
1754}
1755
1756/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1757/// to free memory allocated by new.
1758///
Chris Lattner109faf22009-01-04 21:25:24 +00001759/// This method is called to parse the 'delete' expression after the optional
1760/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1761/// and "Start" is its location. Otherwise, "Start" is the location of the
1762/// 'delete' token.
1763///
Sebastian Redlbd150f42008-11-21 19:14:01 +00001764/// delete-expression:
1765/// '::'[opt] 'delete' cast-expression
1766/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00001767ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00001768Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1769 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1770 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00001771
1772 // Array delete?
1773 bool ArrayDelete = false;
1774 if (Tok.is(tok::l_square)) {
1775 ArrayDelete = true;
1776 SourceLocation LHS = ConsumeBracket();
1777 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1778 if (RHS.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001779 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00001780 }
1781
John McCalldadc5752010-08-24 06:29:42 +00001782 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001783 if (Operand.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001784 return move(Operand);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001785
John McCallb268a282010-08-23 23:25:46 +00001786 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001787}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001788
Mike Stump11289f42009-09-09 15:08:12 +00001789static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001790 switch(kind) {
1791 default: assert(false && "Not a known unary type trait.");
1792 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1793 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1794 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1795 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1796 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1797 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1798 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1799 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1800 case tok::kw___is_abstract: return UTT_IsAbstract;
1801 case tok::kw___is_class: return UTT_IsClass;
1802 case tok::kw___is_empty: return UTT_IsEmpty;
1803 case tok::kw___is_enum: return UTT_IsEnum;
1804 case tok::kw___is_pod: return UTT_IsPOD;
1805 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1806 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redl79eba1c2009-12-03 00:13:20 +00001807 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001808 }
1809}
1810
1811/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1812/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1813/// templates.
1814///
1815/// primary-expression:
1816/// [GNU] unary-type-trait '(' type-id ')'
1817///
John McCalldadc5752010-08-24 06:29:42 +00001818ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001819 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1820 SourceLocation Loc = ConsumeToken();
1821
1822 SourceLocation LParen = Tok.getLocation();
1823 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1824 return ExprError();
1825
1826 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1827 // there will be cryptic errors about mismatched parentheses and missing
1828 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00001829 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001830
1831 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1832
Douglas Gregor220cac52009-02-18 17:45:20 +00001833 if (Ty.isInvalid())
1834 return ExprError();
1835
Douglas Gregor54e5b132010-09-09 16:14:44 +00001836 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001837}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001838
1839/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1840/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1841/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00001842ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001843Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00001844 ParsedType &CastTy,
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001845 SourceLocation LParenLoc,
1846 SourceLocation &RParenLoc) {
1847 assert(getLang().CPlusPlus && "Should only be called for C++!");
1848 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1849 assert(isTypeIdInParens() && "Not a type-id!");
1850
John McCalldadc5752010-08-24 06:29:42 +00001851 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00001852 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001853
1854 // We need to disambiguate a very ugly part of the C++ syntax:
1855 //
1856 // (T())x; - type-id
1857 // (T())*x; - type-id
1858 // (T())/x; - expression
1859 // (T()); - expression
1860 //
1861 // The bad news is that we cannot use the specialized tentative parser, since
1862 // it can only verify that the thing inside the parens can be parsed as
1863 // type-id, it is not useful for determining the context past the parens.
1864 //
1865 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00001866 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001867 //
1868 // It uses a scheme similar to parsing inline methods. The parenthesized
1869 // tokens are cached, the context that follows is determined (possibly by
1870 // parsing a cast-expression), and then we re-introduce the cached tokens
1871 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001872
Mike Stump11289f42009-09-09 15:08:12 +00001873 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001874 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001875
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001876 // Store the tokens of the parentheses. We will parse them after we determine
1877 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00001878 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001879 // We didn't find the ')' we expected.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001880 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1881 return ExprError();
1882 }
1883
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001884 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001885 ParseAs = CompoundLiteral;
1886 } else {
1887 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00001888 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1889 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1890 NotCastExpr = true;
1891 } else {
1892 // Try parsing the cast-expression that may follow.
1893 // If it is not a cast-expression, NotCastExpr will be true and no token
1894 // will be consumed.
1895 Result = ParseCastExpression(false/*isUnaryExpression*/,
1896 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00001897 NotCastExpr,
1898 ParsedType()/*TypeOfCast*/);
Eli Friedmancf7530f2009-05-25 19:41:42 +00001899 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001900
1901 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1902 // an expression.
1903 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001904 }
1905
Mike Stump11289f42009-09-09 15:08:12 +00001906 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001907 Toks.push_back(Tok);
1908 // Re-enter the stored parenthesized tokens into the token stream, so we may
1909 // parse them now.
1910 PP.EnterTokenStream(Toks.data(), Toks.size(),
1911 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1912 // Drop the current token and bring the first cached one. It's the same token
1913 // as when we entered this function.
1914 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001915
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001916 if (ParseAs >= CompoundLiteral) {
1917 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001918
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001919 // Match the ')'.
1920 if (Tok.is(tok::r_paren))
1921 RParenLoc = ConsumeParen();
1922 else
1923 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001924
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001925 if (ParseAs == CompoundLiteral) {
1926 ExprType = CompoundLiteral;
1927 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1928 }
Mike Stump11289f42009-09-09 15:08:12 +00001929
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001930 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1931 assert(ParseAs == CastExpr);
1932
1933 if (Ty.isInvalid())
1934 return ExprError();
1935
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001936 CastTy = Ty.get();
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001937
1938 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001939 if (!Result.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001940 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCallb268a282010-08-23 23:25:46 +00001941 Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001942 return move(Result);
1943 }
Mike Stump11289f42009-09-09 15:08:12 +00001944
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001945 // Not a compound literal, and not followed by a cast-expression.
1946 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001947
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001948 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001949 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001950 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCallb268a282010-08-23 23:25:46 +00001951 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001952
1953 // Match the ')'.
1954 if (Result.isInvalid()) {
1955 SkipUntil(tok::r_paren);
1956 return ExprError();
1957 }
Mike Stump11289f42009-09-09 15:08:12 +00001958
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001959 if (Tok.is(tok::r_paren))
1960 RParenLoc = ConsumeParen();
1961 else
1962 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1963
1964 return move(Result);
1965}