blob: 0e23e43a0dfbf276e537b6b0cc2a748b2e59b5d0 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000019#include "llvm/Support/ErrorHandling.h"
20
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Mike Stump1eb44332009-09-09 15:08:12 +000023/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000024///
25/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000026/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000027/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000028///
29/// '::'[opt] nested-name-specifier
30/// '::'
31///
32/// nested-name-specifier:
33/// type-name '::'
34/// namespace-name '::'
35/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000036/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000037///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000038///
Mike Stump1eb44332009-09-09 15:08:12 +000039/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000040/// nested-name-specifier (or empty)
41///
Mike Stump1eb44332009-09-09 15:08:12 +000042/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000043/// the "." or "->" of a member access expression, this parameter provides the
44/// type of the object whose members are being accessed.
45///
46/// \param EnteringContext whether we will be entering into the context of
47/// the nested-name-specifier after parsing it.
48///
Douglas Gregord4dca082010-02-24 18:44:31 +000049/// \param MayBePseudoDestructor When non-NULL, points to a flag that
50/// indicates whether this nested-name-specifier may be part of a
51/// pseudo-destructor name. In this case, the flag will be set false
52/// if we don't actually end up parsing a destructor name. Moreorover,
53/// if we do end up determining that we are parsing a destructor name,
54/// the last component of the nested-name-specifier is not parsed as
55/// part of the scope specifier.
56
Douglas Gregorb10cd042010-02-21 18:36:56 +000057/// member access expression, e.g., the \p T:: in \p p->T::m.
58///
John McCall9ba61662010-02-26 08:45:28 +000059/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +000060bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +000061 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +000062 bool EnteringContext,
Douglas Gregord4dca082010-02-24 18:44:31 +000063 bool *MayBePseudoDestructor) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000064 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000065 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000066
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000067 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +000068 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
69 Tok.getAnnotationRange(),
70 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000071 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +000072 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000073 }
Chris Lattnere607e802009-01-04 21:14:15 +000074
Douglas Gregor39a8de12009-02-25 19:37:18 +000075 bool HasScopeSpecifier = false;
76
Chris Lattner5b454732009-01-05 03:55:46 +000077 if (Tok.is(tok::coloncolon)) {
78 // ::new and ::delete aren't nested-name-specifiers.
79 tok::TokenKind NextKind = NextToken().getKind();
80 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
81 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattner55a7cef2009-01-05 00:13:00 +000083 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000084 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
85 return true;
86
Douglas Gregor39a8de12009-02-25 19:37:18 +000087 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000088 }
89
Douglas Gregord4dca082010-02-24 18:44:31 +000090 bool CheckForDestructor = false;
91 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
92 CheckForDestructor = true;
93 *MayBePseudoDestructor = false;
94 }
95
Douglas Gregor39a8de12009-02-25 19:37:18 +000096 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000097 if (HasScopeSpecifier) {
98 // C++ [basic.lookup.classref]p5:
99 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000100 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000101 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000102 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000103 // the class-name-or-namespace-name is looked up in global scope as a
104 // class-name or namespace-name.
105 //
106 // To implement this, we clear out the object type as soon as we've
107 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000108 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000109
110 if (Tok.is(tok::code_completion)) {
111 // Code completion for a nested-name-specifier, where the code
112 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000113 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Douglas Gregordc845342010-05-25 05:58:43 +0000114 ConsumeCodeCompletionToken();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000115 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor39a8de12009-02-25 19:37:18 +0000118 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000119 // nested-name-specifier 'template'[opt] simple-template-id '::'
120
121 // Parse the optional 'template' keyword, then make sure we have
122 // 'identifier <' after it.
123 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000124 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000125 // nested-name-specifier, since they aren't allowed to start with
126 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000127 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000128 break;
129
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000130 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000131 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000132
133 UnqualifiedId TemplateName;
134 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000135 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000136 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000137 ConsumeToken();
138 } else if (Tok.is(tok::kw_operator)) {
139 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000140 TemplateName)) {
141 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000142 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000143 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000144
Sean Hunte6252d12009-11-28 08:58:14 +0000145 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
146 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000147 Diag(TemplateName.getSourceRange().getBegin(),
148 diag::err_id_after_template_in_nested_name_spec)
149 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000150 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000151 break;
152 }
153 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000154 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000155 break;
156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000158 // If the next token is not '<', we have a qualified-id that refers
159 // to a template name, such as T::template apply, but is not a
160 // template-id.
161 if (Tok.isNot(tok::less)) {
162 TPA.Revert();
163 break;
164 }
165
166 // Commit to parsing the template-id.
167 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000168 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000169 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000170 TemplateKWLoc,
171 SS,
172 TemplateName,
173 ObjectType,
174 EnteringContext,
175 Template)) {
176 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
177 TemplateKWLoc, false))
178 return true;
179 } else
John McCall9ba61662010-02-26 08:45:28 +0000180 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Chris Lattner77cf72a2009-06-26 03:47:46 +0000182 continue;
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Douglas Gregor39a8de12009-02-25 19:37:18 +0000185 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000186 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000187 //
188 // simple-template-id '::'
189 //
190 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000191 // right kind (it should name a type or be dependent), and then
192 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000193 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000194 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord4dca082010-02-24 18:44:31 +0000195 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
196 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000197 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000198 }
199
Mike Stump1eb44332009-09-09 15:08:12 +0000200 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000201 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000202 // Consume the template-id token.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000203 ConsumeToken();
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000204
Douglas Gregor39a8de12009-02-25 19:37:18 +0000205 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
206 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Douglas Gregorc34348a2011-02-24 17:54:50 +0000208 if (!HasScopeSpecifier)
Douglas Gregor39a8de12009-02-25 19:37:18 +0000209 HasScopeSpecifier = true;
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000210
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000211 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
212 TemplateId->getTemplateArgs(),
213 TemplateId->NumArgs);
214
215 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
216 /*FIXME:*/SourceLocation(),
217 SS,
218 TemplateId->Template,
219 TemplateId->TemplateNameLoc,
220 TemplateId->LAngleLoc,
221 TemplateArgsPtr,
222 TemplateId->RAngleLoc,
223 CCLoc,
Douglas Gregor640519e2011-03-01 00:34:57 +0000224 EnteringContext)) {
225 SourceLocation StartLoc
226 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
227 : TemplateId->TemplateNameLoc;
228 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
229 }
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000230
231 TemplateId->Destroy();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000232 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000233 }
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Chris Lattner67b9e832009-06-26 03:45:46 +0000235 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000236 }
237
Chris Lattner5c7f7862009-06-26 03:52:38 +0000238
239 // The rest of the nested-name-specifier possibilities start with
240 // tok::identifier.
241 if (Tok.isNot(tok::identifier))
242 break;
243
244 IdentifierInfo &II = *Tok.getIdentifierInfo();
245
246 // nested-name-specifier:
247 // type-name '::'
248 // namespace-name '::'
249 // nested-name-specifier identifier '::'
250 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000251
252 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
253 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000254 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000255 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
256 Tok.getLocation(),
257 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000258 EnteringContext) &&
259 // If the token after the colon isn't an identifier, it's still an
260 // error, but they probably meant something else strange so don't
261 // recover like this.
262 PP.LookAhead(1).is(tok::identifier)) {
263 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000264 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000265
266 // Recover as if the user wrote '::'.
267 Next.setKind(tok::coloncolon);
268 }
Chris Lattner46646492009-12-07 01:36:53 +0000269 }
270
Chris Lattner5c7f7862009-06-26 03:52:38 +0000271 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000272 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000273 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000274 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000275 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000276 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000277 }
278
Chris Lattner5c7f7862009-06-26 03:52:38 +0000279 // We have an identifier followed by a '::'. Lookup this name
280 // as the name in a nested-name-specifier.
281 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000282 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
283 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000284 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000286 HasScopeSpecifier = true;
287 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
288 ObjectType, EnteringContext, SS))
289 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
290
Chris Lattner5c7f7862009-06-26 03:52:38 +0000291 continue;
292 }
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattner5c7f7862009-06-26 03:52:38 +0000294 // nested-name-specifier:
295 // type-name '<'
296 if (Next.is(tok::less)) {
297 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000298 UnqualifiedId TemplateName;
299 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000300 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000301 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000302 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000303 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000304 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000305 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000306 Template,
307 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000308 // We have found a template name, so annotate this this token
309 // with a template-id annotation. We do not permit the
310 // template-id to be translated into a type annotation,
311 // because some clients (e.g., the parsing of class template
312 // specializations) still want to see the original template-id
313 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000314 ConsumeToken();
315 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
316 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000317 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000318 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000319 }
320
321 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
322 IsTemplateArgumentList(1)) {
323 // We have something like t::getAs<T>, where getAs is a
324 // member of an unknown specialization. However, this will only
325 // parse correctly as a template, so suggest the keyword 'template'
326 // before 'getAs' and treat this as a dependent template name.
327 Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
328 << II.getName()
329 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
330
Douglas Gregord6ab2322010-06-16 23:00:59 +0000331 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000332 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000333 Tok.getLocation(), SS,
334 TemplateName, ObjectType,
335 EnteringContext, Template)) {
336 // Consume the identifier.
337 ConsumeToken();
338 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
339 SourceLocation(), false))
340 return true;
341 }
342 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000343 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000344
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000345 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000346 }
347 }
348
Douglas Gregor39a8de12009-02-25 19:37:18 +0000349 // We don't have any tokens that form the beginning of a
350 // nested-name-specifier, so we're done.
351 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Douglas Gregord4dca082010-02-24 18:44:31 +0000354 // Even if we didn't see any pieces of a nested-name-specifier, we
355 // still check whether there is a tilde in this position, which
356 // indicates a potential pseudo-destructor.
357 if (CheckForDestructor && Tok.is(tok::tilde))
358 *MayBePseudoDestructor = true;
359
John McCall9ba61662010-02-26 08:45:28 +0000360 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000361}
362
363/// ParseCXXIdExpression - Handle id-expression.
364///
365/// id-expression:
366/// unqualified-id
367/// qualified-id
368///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000369/// qualified-id:
370/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
371/// '::' identifier
372/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000373/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000374///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000375/// NOTE: The standard specifies that, for qualified-id, the parser does not
376/// expect:
377///
378/// '::' conversion-function-id
379/// '::' '~' class-name
380///
381/// This may cause a slight inconsistency on diagnostics:
382///
383/// class C {};
384/// namespace A {}
385/// void f() {
386/// :: A :: ~ C(); // Some Sema error about using destructor with a
387/// // namespace.
388/// :: ~ C(); // Some Parser error like 'unexpected ~'.
389/// }
390///
391/// We simplify the parser a bit and make it work like:
392///
393/// qualified-id:
394/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
395/// '::' unqualified-id
396///
397/// That way Sema can handle and report similar errors for namespaces and the
398/// global scope.
399///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000400/// The isAddressOfOperand parameter indicates that this id-expression is a
401/// direct operand of the address-of operator. This is, besides member contexts,
402/// the only place where a qualified-id naming a non-static class member may
403/// appear.
404///
John McCall60d7b3a2010-08-24 06:29:42 +0000405ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000406 // qualified-id:
407 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
408 // '::' unqualified-id
409 //
410 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000411 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000412
413 UnqualifiedId Name;
414 if (ParseUnqualifiedId(SS,
415 /*EnteringContext=*/false,
416 /*AllowDestructorName=*/false,
417 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000418 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000419 Name))
420 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000421
422 // This is only the direct operand of an & operator if it is not
423 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000424 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
425 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000426
Douglas Gregor23c94db2010-07-02 17:43:08 +0000427 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000428 isAddressOfOperand);
429
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000430}
431
Reid Spencer5f016e22007-07-11 17:01:13 +0000432/// ParseCXXCasts - This handles the various ways to cast expressions to another
433/// type.
434///
435/// postfix-expression: [C++ 5.2p1]
436/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
437/// 'static_cast' '<' type-name '>' '(' expression ')'
438/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
439/// 'const_cast' '<' type-name '>' '(' expression ')'
440///
John McCall60d7b3a2010-08-24 06:29:42 +0000441ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 tok::TokenKind Kind = Tok.getKind();
443 const char *CastName = 0; // For error messages
444
445 switch (Kind) {
446 default: assert(0 && "Unknown C++ cast!"); abort();
447 case tok::kw_const_cast: CastName = "const_cast"; break;
448 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
449 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
450 case tok::kw_static_cast: CastName = "static_cast"; break;
451 }
452
453 SourceLocation OpLoc = ConsumeToken();
454 SourceLocation LAngleBracketLoc = Tok.getLocation();
455
456 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000457 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000458
Douglas Gregor809070a2009-02-18 17:45:20 +0000459 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 SourceLocation RAngleBracketLoc = Tok.getLocation();
461
Chris Lattner1ab3b962008-11-18 07:48:38 +0000462 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000463 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000464
465 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
466
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000467 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
468 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000469
John McCall60d7b3a2010-08-24 06:29:42 +0000470 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000472 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000473 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000474
Douglas Gregor809070a2009-02-18 17:45:20 +0000475 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000476 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000477 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000478 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000479 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000480
Sebastian Redl20df9b72008-12-11 22:51:44 +0000481 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000482}
483
Sebastian Redlc42e1182008-11-11 11:37:55 +0000484/// ParseCXXTypeid - This handles the C++ typeid expression.
485///
486/// postfix-expression: [C++ 5.2p1]
487/// 'typeid' '(' expression ')'
488/// 'typeid' '(' type-id ')'
489///
John McCall60d7b3a2010-08-24 06:29:42 +0000490ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000491 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
492
493 SourceLocation OpLoc = ConsumeToken();
494 SourceLocation LParenLoc = Tok.getLocation();
495 SourceLocation RParenLoc;
496
497 // typeid expressions are always parenthesized.
498 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
499 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000500 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000501
John McCall60d7b3a2010-08-24 06:29:42 +0000502 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000503
504 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000505 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000506
507 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000508 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000509
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000510 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000511 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000512
513 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000514 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000515 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000516 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000517 // When typeid is applied to an expression other than an lvalue of a
518 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000519 // operand (Clause 5).
520 //
Mike Stump1eb44332009-09-09 15:08:12 +0000521 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000522 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000523 // we the expression is potentially potentially evaluated.
524 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000525 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000526 Result = ParseExpression();
527
528 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000529 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000530 SkipUntil(tok::r_paren);
531 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000532 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
533 if (RParenLoc.isInvalid())
534 return ExprError();
535
Sebastian Redlc42e1182008-11-11 11:37:55 +0000536 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000537 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000538 }
539 }
540
Sebastian Redl20df9b72008-12-11 22:51:44 +0000541 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000542}
543
Francois Pichet01b7c302010-09-08 12:20:18 +0000544/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
545///
546/// '__uuidof' '(' expression ')'
547/// '__uuidof' '(' type-id ')'
548///
549ExprResult Parser::ParseCXXUuidof() {
550 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
551
552 SourceLocation OpLoc = ConsumeToken();
553 SourceLocation LParenLoc = Tok.getLocation();
554 SourceLocation RParenLoc;
555
556 // __uuidof expressions are always parenthesized.
557 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
558 "__uuidof"))
559 return ExprError();
560
561 ExprResult Result;
562
563 if (isTypeIdInParens()) {
564 TypeResult Ty = ParseTypeName();
565
566 // Match the ')'.
567 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
568
569 if (Ty.isInvalid())
570 return ExprError();
571
572 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
573 Ty.get().getAsOpaquePtr(), RParenLoc);
574 } else {
575 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
576 Result = ParseExpression();
577
578 // Match the ')'.
579 if (Result.isInvalid())
580 SkipUntil(tok::r_paren);
581 else {
582 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
583
584 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
585 Result.release(), RParenLoc);
586 }
587 }
588
589 return move(Result);
590}
591
Douglas Gregord4dca082010-02-24 18:44:31 +0000592/// \brief Parse a C++ pseudo-destructor expression after the base,
593/// . or -> operator, and nested-name-specifier have already been
594/// parsed.
595///
596/// postfix-expression: [C++ 5.2]
597/// postfix-expression . pseudo-destructor-name
598/// postfix-expression -> pseudo-destructor-name
599///
600/// pseudo-destructor-name:
601/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
602/// ::[opt] nested-name-specifier template simple-template-id ::
603/// ~type-name
604/// ::[opt] nested-name-specifier[opt] ~type-name
605///
John McCall60d7b3a2010-08-24 06:29:42 +0000606ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000607Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
608 tok::TokenKind OpKind,
609 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000610 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000611 // We're parsing either a pseudo-destructor-name or a dependent
612 // member access that has the same form as a
613 // pseudo-destructor-name. We parse both in the same way and let
614 // the action model sort them out.
615 //
616 // Note that the ::[opt] nested-name-specifier[opt] has already
617 // been parsed, and if there was a simple-template-id, it has
618 // been coalesced into a template-id annotation token.
619 UnqualifiedId FirstTypeName;
620 SourceLocation CCLoc;
621 if (Tok.is(tok::identifier)) {
622 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
623 ConsumeToken();
624 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
625 CCLoc = ConsumeToken();
626 } else if (Tok.is(tok::annot_template_id)) {
627 FirstTypeName.setTemplateId(
628 (TemplateIdAnnotation *)Tok.getAnnotationValue());
629 ConsumeToken();
630 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
631 CCLoc = ConsumeToken();
632 } else {
633 FirstTypeName.setIdentifier(0, SourceLocation());
634 }
635
636 // Parse the tilde.
637 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
638 SourceLocation TildeLoc = ConsumeToken();
639 if (!Tok.is(tok::identifier)) {
640 Diag(Tok, diag::err_destructor_tilde_identifier);
641 return ExprError();
642 }
643
644 // Parse the second type.
645 UnqualifiedId SecondTypeName;
646 IdentifierInfo *Name = Tok.getIdentifierInfo();
647 SourceLocation NameLoc = ConsumeToken();
648 SecondTypeName.setIdentifier(Name, NameLoc);
649
650 // If there is a '<', the second type name is a template-id. Parse
651 // it as such.
652 if (Tok.is(tok::less) &&
653 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000654 SecondTypeName, /*AssumeTemplateName=*/true,
655 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000656 return ExprError();
657
John McCall9ae2f072010-08-23 23:25:46 +0000658 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
659 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000660 SS, FirstTypeName, CCLoc,
661 TildeLoc, SecondTypeName,
662 Tok.is(tok::l_paren));
663}
664
Reid Spencer5f016e22007-07-11 17:01:13 +0000665/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
666///
667/// boolean-literal: [C++ 2.13.5]
668/// 'true'
669/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +0000670ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000672 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000673}
Chris Lattner50dd2892008-02-26 00:51:44 +0000674
675/// ParseThrowExpression - This handles the C++ throw expression.
676///
677/// throw-expression: [C++ 15]
678/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +0000679ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000680 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000681 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000682
Chris Lattner2a2819a2008-04-06 06:02:23 +0000683 // If the current token isn't the start of an assignment-expression,
684 // then the expression is not present. This handles things like:
685 // "C ? throw : (void)42", which is crazy but legal.
686 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
687 case tok::semi:
688 case tok::r_paren:
689 case tok::r_square:
690 case tok::r_brace:
691 case tok::colon:
692 case tok::comma:
John McCall9ae2f072010-08-23 23:25:46 +0000693 return Actions.ActOnCXXThrow(ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +0000694
Chris Lattner2a2819a2008-04-06 06:02:23 +0000695 default:
John McCall60d7b3a2010-08-24 06:29:42 +0000696 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000697 if (Expr.isInvalid()) return move(Expr);
John McCall9ae2f072010-08-23 23:25:46 +0000698 return Actions.ActOnCXXThrow(ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +0000699 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000700}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000701
702/// ParseCXXThis - This handles the C++ 'this' pointer.
703///
704/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
705/// a non-lvalue expression whose value is the address of the object for which
706/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +0000707ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000708 assert(Tok.is(tok::kw_this) && "Not 'this'!");
709 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000710 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000711}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000712
713/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
714/// Can be interpreted either as function-style casting ("int(x)")
715/// or class type construction ("ClassType(x,y,z)")
716/// or creation of a value-initialized type ("int()").
717///
718/// postfix-expression: [C++ 5.2p1]
719/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
720/// typename-specifier '(' expression-list[opt] ')' [TODO]
721///
John McCall60d7b3a2010-08-24 06:29:42 +0000722ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +0000723Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000724 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +0000725 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000726
727 assert(Tok.is(tok::l_paren) && "Expected '('!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +0000728 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
729
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000730 SourceLocation LParenLoc = ConsumeParen();
731
Sebastian Redla55e52c2008-11-25 22:21:31 +0000732 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000733 CommaLocsTy CommaLocs;
734
735 if (Tok.isNot(tok::r_paren)) {
736 if (ParseExpressionList(Exprs, CommaLocs)) {
737 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000738 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000739 }
740 }
741
742 // Match the ')'.
743 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
744
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000745 // TypeRep could be null, if it references an invalid typedef.
746 if (!TypeRep)
747 return ExprError();
748
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000749 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
750 "Unexpected number of commas!");
Douglas Gregorab6677e2010-09-08 00:15:04 +0000751 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000752 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000753}
754
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000755/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000756///
757/// condition:
758/// expression
759/// type-specifier-seq declarator '=' assignment-expression
760/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
761/// '=' assignment-expression
762///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000763/// \param ExprResult if the condition was parsed as an expression, the
764/// parsed expression.
765///
766/// \param DeclResult if the condition was parsed as a declaration, the
767/// parsed declaration.
768///
Douglas Gregor586596f2010-05-06 17:25:47 +0000769/// \param Loc The location of the start of the statement that requires this
770/// condition, e.g., the "for" in a for loop.
771///
772/// \param ConvertToBoolean Whether the condition expression should be
773/// converted to a boolean value.
774///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000775/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +0000776bool Parser::ParseCXXCondition(ExprResult &ExprOut,
777 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +0000778 SourceLocation Loc,
779 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000780 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +0000781 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000782 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000783 }
784
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000785 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000786 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000787 ExprOut = ParseExpression(); // expression
788 DeclOut = 0;
789 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +0000790 return true;
791
792 // If required, convert to a boolean value.
793 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +0000794 ExprOut
795 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
796 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000797 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000798
799 // type-specifier-seq
800 DeclSpec DS;
801 ParseSpecifierQualifierList(DS);
802
803 // declarator
804 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
805 ParseDeclarator(DeclaratorInfo);
806
807 // simple-asm-expr[opt]
808 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000809 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000810 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000811 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000812 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000813 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000814 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000815 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000816 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000817 }
818
819 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000820 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000821
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000822 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +0000823 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +0000824 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +0000825 DeclOut = Dcl.get();
826 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000827
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000828 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000829 if (isTokenEqualOrMistypedEqualEqual(
830 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000831 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +0000832 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000833 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +0000834 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
835 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000836 } else {
837 // FIXME: C++0x allows a braced-init-list
838 Diag(Tok, diag::err_expected_equal_after_declarator);
839 }
840
Douglas Gregor586596f2010-05-06 17:25:47 +0000841 // FIXME: Build a reference to this declaration? Convert it to bool?
842 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +0000843
844 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +0000845
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000846 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000847}
848
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000849/// \brief Determine whether the current token starts a C++
850/// simple-type-specifier.
851bool Parser::isCXXSimpleTypeSpecifier() const {
852 switch (Tok.getKind()) {
853 case tok::annot_typename:
854 case tok::kw_short:
855 case tok::kw_long:
856 case tok::kw_signed:
857 case tok::kw_unsigned:
858 case tok::kw_void:
859 case tok::kw_char:
860 case tok::kw_int:
861 case tok::kw_float:
862 case tok::kw_double:
863 case tok::kw_wchar_t:
864 case tok::kw_char16_t:
865 case tok::kw_char32_t:
866 case tok::kw_bool:
867 // FIXME: C++0x decltype support.
868 // GNU typeof support.
869 case tok::kw_typeof:
870 return true;
871
872 default:
873 break;
874 }
875
876 return false;
877}
878
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000879/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
880/// This should only be called when the current token is known to be part of
881/// simple-type-specifier.
882///
883/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000884/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000885/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
886/// char
887/// wchar_t
888/// bool
889/// short
890/// int
891/// long
892/// signed
893/// unsigned
894/// float
895/// double
896/// void
897/// [GNU] typeof-specifier
898/// [C++0x] auto [TODO]
899///
900/// type-name:
901/// class-name
902/// enum-name
903/// typedef-name
904///
905void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
906 DS.SetRangeStart(Tok.getLocation());
907 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000908 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000909 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000911 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000912 case tok::identifier: // foo::bar
913 case tok::coloncolon: // ::foo::bar
914 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000915 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000916 assert(0 && "Not a simple-type-specifier token!");
917 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000918
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000919 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000920 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000921 if (getTypeAnnotation(Tok))
922 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
923 getTypeAnnotation(Tok));
924 else
925 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +0000926
927 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
928 ConsumeToken();
929
930 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
931 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
932 // Objective-C interface. If we don't have Objective-C or a '<', this is
933 // just a normal reference to a typedef name.
934 if (Tok.is(tok::less) && getLang().ObjC1)
935 ParseObjCProtocolQualifiers(DS);
936
937 DS.Finish(Diags, PP);
938 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000941 // builtin types
942 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000943 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000944 break;
945 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000946 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000947 break;
948 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000949 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000950 break;
951 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000952 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000953 break;
954 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000955 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000956 break;
957 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000958 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000959 break;
960 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000961 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000962 break;
963 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000964 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000965 break;
966 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000967 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000968 break;
969 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000970 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000971 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000972 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000973 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000974 break;
975 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000976 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000977 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000978 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000979 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000980 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000982 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000983 // GNU typeof support.
984 case tok::kw_typeof:
985 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000986 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000987 return;
988 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000989 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000990 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
991 else
992 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000993 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000994 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000995}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000996
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000997/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
998/// [dcl.name]), which is a non-empty sequence of type-specifiers,
999/// e.g., "const short int". Note that the DeclSpec is *not* finished
1000/// by parsing the type-specifier-seq, because these sequences are
1001/// typically followed by some form of declarator. Returns true and
1002/// emits diagnostics if this is not a type-specifier-seq, false
1003/// otherwise.
1004///
1005/// type-specifier-seq: [C++ 8.1]
1006/// type-specifier type-specifier-seq[opt]
1007///
1008bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1009 DS.SetRangeStart(Tok.getLocation());
1010 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001011 unsigned DiagID;
1012 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001013
1014 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001015 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1016 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001017 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001018 return true;
1019 }
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Sebastian Redld9bafa72010-02-03 21:21:43 +00001021 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1022 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1023 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001024
Douglas Gregor396a9f22010-02-24 23:13:13 +00001025 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001026 return false;
1027}
1028
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001029/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1030/// some form.
1031///
1032/// This routine is invoked when a '<' is encountered after an identifier or
1033/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1034/// whether the unqualified-id is actually a template-id. This routine will
1035/// then parse the template arguments and form the appropriate template-id to
1036/// return to the caller.
1037///
1038/// \param SS the nested-name-specifier that precedes this template-id, if
1039/// we're actually parsing a qualified-id.
1040///
1041/// \param Name for constructor and destructor names, this is the actual
1042/// identifier that may be a template-name.
1043///
1044/// \param NameLoc the location of the class-name in a constructor or
1045/// destructor.
1046///
1047/// \param EnteringContext whether we're entering the scope of the
1048/// nested-name-specifier.
1049///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001050/// \param ObjectType if this unqualified-id occurs within a member access
1051/// expression, the type of the base object whose member is being accessed.
1052///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001053/// \param Id as input, describes the template-name or operator-function-id
1054/// that precedes the '<'. If template arguments were parsed successfully,
1055/// will be updated with the template-id.
1056///
Douglas Gregord4dca082010-02-24 18:44:31 +00001057/// \param AssumeTemplateId When true, this routine will assume that the name
1058/// refers to a template without performing name lookup to verify.
1059///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001060/// \returns true if a parse error occurred, false otherwise.
1061bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1062 IdentifierInfo *Name,
1063 SourceLocation NameLoc,
1064 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001065 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001066 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001067 bool AssumeTemplateId,
1068 SourceLocation TemplateKWLoc) {
1069 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1070 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001071
1072 TemplateTy Template;
1073 TemplateNameKind TNK = TNK_Non_template;
1074 switch (Id.getKind()) {
1075 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001076 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001077 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001078 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001079 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001080 Id, ObjectType, EnteringContext,
1081 Template);
1082 if (TNK == TNK_Non_template)
1083 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001084 } else {
1085 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001086 TNK = Actions.isTemplateName(getCurScope(), SS,
1087 TemplateKWLoc.isValid(), Id,
1088 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001089 MemberOfUnknownSpecialization);
1090
1091 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1092 ObjectType && IsTemplateArgumentList()) {
1093 // We have something like t->getAs<T>(), where getAs is a
1094 // member of an unknown specialization. However, this will only
1095 // parse correctly as a template, so suggest the keyword 'template'
1096 // before 'getAs' and treat this as a dependent template name.
1097 std::string Name;
1098 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1099 Name = Id.Identifier->getName();
1100 else {
1101 Name = "operator ";
1102 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1103 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1104 else
1105 Name += Id.Identifier->getName();
1106 }
1107 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1108 << Name
1109 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001110 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001111 SS, Id, ObjectType,
1112 EnteringContext, Template);
1113 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001114 return true;
1115 }
1116 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001117 break;
1118
Douglas Gregor014e88d2009-11-03 23:16:33 +00001119 case UnqualifiedId::IK_ConstructorName: {
1120 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001121 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001122 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001123 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1124 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001125 EnteringContext, Template,
1126 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001127 break;
1128 }
1129
Douglas Gregor014e88d2009-11-03 23:16:33 +00001130 case UnqualifiedId::IK_DestructorName: {
1131 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001132 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001133 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001134 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001135 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001136 TemplateName, ObjectType,
1137 EnteringContext, Template);
1138 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001139 return true;
1140 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001141 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1142 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001143 EnteringContext, Template,
1144 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001145
John McCallb3d87482010-08-24 05:47:05 +00001146 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001147 Diag(NameLoc, diag::err_destructor_template_id)
1148 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001149 return true;
1150 }
1151 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001152 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001153 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001154
1155 default:
1156 return false;
1157 }
1158
1159 if (TNK == TNK_Non_template)
1160 return false;
1161
1162 // Parse the enclosed template argument list.
1163 SourceLocation LAngleLoc, RAngleLoc;
1164 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001165 if (Tok.is(tok::less) &&
1166 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001167 &SS, true, LAngleLoc,
1168 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001169 RAngleLoc))
1170 return true;
1171
1172 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001173 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1174 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001175 // Form a parsed representation of the template-id to be stored in the
1176 // UnqualifiedId.
1177 TemplateIdAnnotation *TemplateId
1178 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1179
1180 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1181 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001182 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001183 TemplateId->TemplateNameLoc = Id.StartLocation;
1184 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001185 TemplateId->Name = 0;
1186 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1187 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001188 }
1189
John McCall2b5289b2010-08-23 07:28:44 +00001190 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001191 TemplateId->Kind = TNK;
1192 TemplateId->LAngleLoc = LAngleLoc;
1193 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001194 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001195 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001196 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001197 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001198
1199 Id.setTemplateId(TemplateId);
1200 return false;
1201 }
1202
1203 // Bundle the template arguments together.
1204 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001205 TemplateArgs.size());
1206
1207 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001208 TypeResult Type
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001209 = Actions.ActOnTemplateIdType(Template, NameLoc,
1210 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001211 RAngleLoc);
1212 if (Type.isInvalid())
1213 return true;
1214
1215 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1216 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1217 else
1218 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1219
1220 return false;
1221}
1222
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001223/// \brief Parse an operator-function-id or conversion-function-id as part
1224/// of a C++ unqualified-id.
1225///
1226/// This routine is responsible only for parsing the operator-function-id or
1227/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001228///
1229/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001230/// operator-function-id: [C++ 13.5]
1231/// 'operator' operator
1232///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001233/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001234/// new delete new[] delete[]
1235/// + - * / % ^ & | ~
1236/// ! = < > += -= *= /= %=
1237/// ^= &= |= << >> >>= <<= == !=
1238/// <= >= && || ++ -- , ->* ->
1239/// () []
1240///
1241/// conversion-function-id: [C++ 12.3.2]
1242/// operator conversion-type-id
1243///
1244/// conversion-type-id:
1245/// type-specifier-seq conversion-declarator[opt]
1246///
1247/// conversion-declarator:
1248/// ptr-operator conversion-declarator[opt]
1249/// \endcode
1250///
1251/// \param The nested-name-specifier that preceded this unqualified-id. If
1252/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1253///
1254/// \param EnteringContext whether we are entering the scope of the
1255/// nested-name-specifier.
1256///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001257/// \param ObjectType if this unqualified-id occurs within a member access
1258/// expression, the type of the base object whose member is being accessed.
1259///
1260/// \param Result on a successful parse, contains the parsed unqualified-id.
1261///
1262/// \returns true if parsing fails, false otherwise.
1263bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001264 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001265 UnqualifiedId &Result) {
1266 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1267
1268 // Consume the 'operator' keyword.
1269 SourceLocation KeywordLoc = ConsumeToken();
1270
1271 // Determine what kind of operator name we have.
1272 unsigned SymbolIdx = 0;
1273 SourceLocation SymbolLocations[3];
1274 OverloadedOperatorKind Op = OO_None;
1275 switch (Tok.getKind()) {
1276 case tok::kw_new:
1277 case tok::kw_delete: {
1278 bool isNew = Tok.getKind() == tok::kw_new;
1279 // Consume the 'new' or 'delete'.
1280 SymbolLocations[SymbolIdx++] = ConsumeToken();
1281 if (Tok.is(tok::l_square)) {
1282 // Consume the '['.
1283 SourceLocation LBracketLoc = ConsumeBracket();
1284 // Consume the ']'.
1285 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1286 LBracketLoc);
1287 if (RBracketLoc.isInvalid())
1288 return true;
1289
1290 SymbolLocations[SymbolIdx++] = LBracketLoc;
1291 SymbolLocations[SymbolIdx++] = RBracketLoc;
1292 Op = isNew? OO_Array_New : OO_Array_Delete;
1293 } else {
1294 Op = isNew? OO_New : OO_Delete;
1295 }
1296 break;
1297 }
1298
1299#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1300 case tok::Token: \
1301 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1302 Op = OO_##Name; \
1303 break;
1304#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1305#include "clang/Basic/OperatorKinds.def"
1306
1307 case tok::l_paren: {
1308 // Consume the '('.
1309 SourceLocation LParenLoc = ConsumeParen();
1310 // Consume the ')'.
1311 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1312 LParenLoc);
1313 if (RParenLoc.isInvalid())
1314 return true;
1315
1316 SymbolLocations[SymbolIdx++] = LParenLoc;
1317 SymbolLocations[SymbolIdx++] = RParenLoc;
1318 Op = OO_Call;
1319 break;
1320 }
1321
1322 case tok::l_square: {
1323 // Consume the '['.
1324 SourceLocation LBracketLoc = ConsumeBracket();
1325 // Consume the ']'.
1326 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1327 LBracketLoc);
1328 if (RBracketLoc.isInvalid())
1329 return true;
1330
1331 SymbolLocations[SymbolIdx++] = LBracketLoc;
1332 SymbolLocations[SymbolIdx++] = RBracketLoc;
1333 Op = OO_Subscript;
1334 break;
1335 }
1336
1337 case tok::code_completion: {
1338 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001339 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001340
1341 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001342 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001343
1344 // Don't try to parse any further.
1345 return true;
1346 }
1347
1348 default:
1349 break;
1350 }
1351
1352 if (Op != OO_None) {
1353 // We have parsed an operator-function-id.
1354 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1355 return false;
1356 }
Sean Hunt0486d742009-11-28 04:44:28 +00001357
1358 // Parse a literal-operator-id.
1359 //
1360 // literal-operator-id: [C++0x 13.5.8]
1361 // operator "" identifier
1362
1363 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1364 if (Tok.getLength() != 2)
1365 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1366 ConsumeStringToken();
1367
1368 if (Tok.isNot(tok::identifier)) {
1369 Diag(Tok.getLocation(), diag::err_expected_ident);
1370 return true;
1371 }
1372
1373 IdentifierInfo *II = Tok.getIdentifierInfo();
1374 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001375 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001376 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001377
1378 // Parse a conversion-function-id.
1379 //
1380 // conversion-function-id: [C++ 12.3.2]
1381 // operator conversion-type-id
1382 //
1383 // conversion-type-id:
1384 // type-specifier-seq conversion-declarator[opt]
1385 //
1386 // conversion-declarator:
1387 // ptr-operator conversion-declarator[opt]
1388
1389 // Parse the type-specifier-seq.
1390 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001391 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001392 return true;
1393
1394 // Parse the conversion-declarator, which is merely a sequence of
1395 // ptr-operators.
1396 Declarator D(DS, Declarator::TypeNameContext);
1397 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1398
1399 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001400 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001401 if (Ty.isInvalid())
1402 return true;
1403
1404 // Note that this is a conversion-function-id.
1405 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1406 D.getSourceRange().getEnd());
1407 return false;
1408}
1409
1410/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1411/// name of an entity.
1412///
1413/// \code
1414/// unqualified-id: [C++ expr.prim.general]
1415/// identifier
1416/// operator-function-id
1417/// conversion-function-id
1418/// [C++0x] literal-operator-id [TODO]
1419/// ~ class-name
1420/// template-id
1421///
1422/// \endcode
1423///
1424/// \param The nested-name-specifier that preceded this unqualified-id. If
1425/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1426///
1427/// \param EnteringContext whether we are entering the scope of the
1428/// nested-name-specifier.
1429///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001430/// \param AllowDestructorName whether we allow parsing of a destructor name.
1431///
1432/// \param AllowConstructorName whether we allow parsing a constructor name.
1433///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001434/// \param ObjectType if this unqualified-id occurs within a member access
1435/// expression, the type of the base object whose member is being accessed.
1436///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001437/// \param Result on a successful parse, contains the parsed unqualified-id.
1438///
1439/// \returns true if parsing fails, false otherwise.
1440bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1441 bool AllowDestructorName,
1442 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001443 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001444 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001445
1446 // Handle 'A::template B'. This is for template-ids which have not
1447 // already been annotated by ParseOptionalCXXScopeSpecifier().
1448 bool TemplateSpecified = false;
1449 SourceLocation TemplateKWLoc;
1450 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1451 (ObjectType || SS.isSet())) {
1452 TemplateSpecified = true;
1453 TemplateKWLoc = ConsumeToken();
1454 }
1455
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001456 // unqualified-id:
1457 // identifier
1458 // template-id (when it hasn't already been annotated)
1459 if (Tok.is(tok::identifier)) {
1460 // Consume the identifier.
1461 IdentifierInfo *Id = Tok.getIdentifierInfo();
1462 SourceLocation IdLoc = ConsumeToken();
1463
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001464 if (!getLang().CPlusPlus) {
1465 // If we're not in C++, only identifiers matter. Record the
1466 // identifier and return.
1467 Result.setIdentifier(Id, IdLoc);
1468 return false;
1469 }
1470
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001471 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001472 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001473 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001474 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001475 &SS, false, false,
1476 ParsedType(),
1477 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001478 IdLoc, IdLoc);
1479 } else {
1480 // We have parsed an identifier.
1481 Result.setIdentifier(Id, IdLoc);
1482 }
1483
1484 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001485 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001486 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001487 ObjectType, Result,
1488 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001489
1490 return false;
1491 }
1492
1493 // unqualified-id:
1494 // template-id (already parsed and annotated)
1495 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001496 TemplateIdAnnotation *TemplateId
1497 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1498
1499 // If the template-name names the current class, then this is a constructor
1500 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001501 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001502 if (SS.isSet()) {
1503 // C++ [class.qual]p2 specifies that a qualified template-name
1504 // is taken as the constructor name where a constructor can be
1505 // declared. Thus, the template arguments are extraneous, so
1506 // complain about them and remove them entirely.
1507 Diag(TemplateId->TemplateNameLoc,
1508 diag::err_out_of_line_constructor_template_id)
1509 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001510 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001511 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1512 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1513 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001514 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001515 &SS, false, false,
1516 ParsedType(),
1517 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001518 TemplateId->TemplateNameLoc,
1519 TemplateId->RAngleLoc);
1520 TemplateId->Destroy();
1521 ConsumeToken();
1522 return false;
1523 }
1524
1525 Result.setConstructorTemplateId(TemplateId);
1526 ConsumeToken();
1527 return false;
1528 }
1529
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001530 // We have already parsed a template-id; consume the annotation token as
1531 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001532 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001533 ConsumeToken();
1534 return false;
1535 }
1536
1537 // unqualified-id:
1538 // operator-function-id
1539 // conversion-function-id
1540 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001541 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001542 return true;
1543
Sean Hunte6252d12009-11-28 08:58:14 +00001544 // If we have an operator-function-id or a literal-operator-id and the next
1545 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001546 //
1547 // template-id:
1548 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001549 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1550 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001551 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001552 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1553 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001554 Result,
1555 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001556
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001557 return false;
1558 }
1559
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001560 if (getLang().CPlusPlus &&
1561 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001562 // C++ [expr.unary.op]p10:
1563 // There is an ambiguity in the unary-expression ~X(), where X is a
1564 // class-name. The ambiguity is resolved in favor of treating ~ as a
1565 // unary complement rather than treating ~X as referring to a destructor.
1566
1567 // Parse the '~'.
1568 SourceLocation TildeLoc = ConsumeToken();
1569
1570 // Parse the class-name.
1571 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001572 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001573 return true;
1574 }
1575
1576 // Parse the class-name (or template-name in a simple-template-id).
1577 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1578 SourceLocation ClassNameLoc = ConsumeToken();
1579
Douglas Gregor0278e122010-05-05 05:58:24 +00001580 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001581 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001582 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001583 EnteringContext, ObjectType, Result,
1584 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001585 }
1586
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001587 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001588 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1589 ClassNameLoc, getCurScope(),
1590 SS, ObjectType,
1591 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001592 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001593 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001594
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001595 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001596 return false;
1597 }
1598
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001599 Diag(Tok, diag::err_expected_unqualified_id)
1600 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001601 return true;
1602}
1603
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001604/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1605/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001606///
Chris Lattner59232d32009-01-04 21:25:24 +00001607/// This method is called to parse the new expression after the optional :: has
1608/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1609/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001610///
1611/// new-expression:
1612/// '::'[opt] 'new' new-placement[opt] new-type-id
1613/// new-initializer[opt]
1614/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1615/// new-initializer[opt]
1616///
1617/// new-placement:
1618/// '(' expression-list ')'
1619///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001620/// new-type-id:
1621/// type-specifier-seq new-declarator[opt]
1622///
1623/// new-declarator:
1624/// ptr-operator new-declarator[opt]
1625/// direct-new-declarator
1626///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001627/// new-initializer:
1628/// '(' expression-list[opt] ')'
1629/// [C++0x] braced-init-list [TODO]
1630///
John McCall60d7b3a2010-08-24 06:29:42 +00001631ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001632Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1633 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1634 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001635
1636 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1637 // second form of new-expression. It can't be a new-type-id.
1638
Sebastian Redla55e52c2008-11-25 22:21:31 +00001639 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001640 SourceLocation PlacementLParen, PlacementRParen;
1641
Douglas Gregor4bd40312010-07-13 15:54:32 +00001642 SourceRange TypeIdParens;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001643 DeclSpec DS;
1644 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001645 if (Tok.is(tok::l_paren)) {
1646 // If it turns out to be a placement, we change the type location.
1647 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001648 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1649 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001650 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001651 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001652
1653 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001654 if (PlacementRParen.isInvalid()) {
1655 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001656 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001657 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001658
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001659 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001660 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00001661 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001662 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001663 } else {
1664 // We still need the type.
1665 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00001666 TypeIdParens.setBegin(ConsumeParen());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001667 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001668 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001669 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00001670 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
1671 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001672 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001673 if (ParseCXXTypeSpecifierSeq(DS))
1674 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001675 else {
1676 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001677 ParseDeclaratorInternal(DeclaratorInfo,
1678 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001679 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001680 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001681 }
1682 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001683 // A new-type-id is a simplified type-id, where essentially the
1684 // direct-declarator is replaced by a direct-new-declarator.
1685 if (ParseCXXTypeSpecifierSeq(DS))
1686 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001687 else {
1688 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001689 ParseDeclaratorInternal(DeclaratorInfo,
1690 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001691 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001692 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001693 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001694 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001695 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001696 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001697
Sebastian Redla55e52c2008-11-25 22:21:31 +00001698 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001699 SourceLocation ConstructorLParen, ConstructorRParen;
1700
1701 if (Tok.is(tok::l_paren)) {
1702 ConstructorLParen = ConsumeParen();
1703 if (Tok.isNot(tok::r_paren)) {
1704 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001705 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1706 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001707 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001708 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001709 }
1710 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001711 if (ConstructorRParen.isInvalid()) {
1712 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001713 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001714 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001715 }
1716
Sebastian Redlf53597f2009-03-15 17:47:39 +00001717 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1718 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001719 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00001720 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001721}
1722
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001723/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1724/// passed to ParseDeclaratorInternal.
1725///
1726/// direct-new-declarator:
1727/// '[' expression ']'
1728/// direct-new-declarator '[' constant-expression ']'
1729///
Chris Lattner59232d32009-01-04 21:25:24 +00001730void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001731 // Parse the array dimensions.
1732 bool first = true;
1733 while (Tok.is(tok::l_square)) {
1734 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00001735 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001736 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001737 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001738 // Recover
1739 SkipUntil(tok::r_square);
1740 return;
1741 }
1742 first = false;
1743
Sebastian Redlab197ba2009-02-09 18:23:29 +00001744 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall7f040a92010-12-24 02:08:15 +00001745 D.AddTypeInfo(DeclaratorChunk::getArray(0, ParsedAttributes(),
1746 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001747 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001748 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001749
Sebastian Redlab197ba2009-02-09 18:23:29 +00001750 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001751 return;
1752 }
1753}
1754
1755/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1756/// This ambiguity appears in the syntax of the C++ new operator.
1757///
1758/// new-expression:
1759/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1760/// new-initializer[opt]
1761///
1762/// new-placement:
1763/// '(' expression-list ')'
1764///
John McCallca0408f2010-08-23 06:44:23 +00001765bool Parser::ParseExpressionListOrTypeId(
1766 llvm::SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001767 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001768 // The '(' was already consumed.
1769 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001770 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001771 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001772 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001773 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001774 }
1775
1776 // It's not a type, it has to be an expression list.
1777 // Discard the comma locations - ActOnCXXNew has enough parameters.
1778 CommaLocsTy CommaLocs;
1779 return ParseExpressionList(PlacementArgs, CommaLocs);
1780}
1781
1782/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1783/// to free memory allocated by new.
1784///
Chris Lattner59232d32009-01-04 21:25:24 +00001785/// This method is called to parse the 'delete' expression after the optional
1786/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1787/// and "Start" is its location. Otherwise, "Start" is the location of the
1788/// 'delete' token.
1789///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001790/// delete-expression:
1791/// '::'[opt] 'delete' cast-expression
1792/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001793ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00001794Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1795 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1796 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001797
1798 // Array delete?
1799 bool ArrayDelete = false;
1800 if (Tok.is(tok::l_square)) {
1801 ArrayDelete = true;
1802 SourceLocation LHS = ConsumeBracket();
1803 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1804 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001805 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001806 }
1807
John McCall60d7b3a2010-08-24 06:29:42 +00001808 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001809 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001810 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001811
John McCall9ae2f072010-08-23 23:25:46 +00001812 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001813}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001814
Mike Stump1eb44332009-09-09 15:08:12 +00001815static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001816 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001817 default: llvm_unreachable("Not a known unary type trait");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001818 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1819 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1820 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1821 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1822 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1823 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1824 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1825 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1826 case tok::kw___is_abstract: return UTT_IsAbstract;
1827 case tok::kw___is_class: return UTT_IsClass;
1828 case tok::kw___is_empty: return UTT_IsEmpty;
1829 case tok::kw___is_enum: return UTT_IsEnum;
1830 case tok::kw___is_pod: return UTT_IsPOD;
1831 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1832 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001833 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001834 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001835}
1836
1837static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
1838 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001839 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001840 case tok::kw___is_base_of: return BTT_IsBaseOf;
1841 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00001842 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00001843 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001844}
1845
1846/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1847/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1848/// templates.
1849///
1850/// primary-expression:
1851/// [GNU] unary-type-trait '(' type-id ')'
1852///
John McCall60d7b3a2010-08-24 06:29:42 +00001853ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001854 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1855 SourceLocation Loc = ConsumeToken();
1856
1857 SourceLocation LParen = Tok.getLocation();
1858 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1859 return ExprError();
1860
1861 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1862 // there will be cryptic errors about mismatched parentheses and missing
1863 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001864 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001865
1866 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1867
Douglas Gregor809070a2009-02-18 17:45:20 +00001868 if (Ty.isInvalid())
1869 return ExprError();
1870
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00001871 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001872}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001873
Francois Pichet6ad6f282010-12-07 00:08:36 +00001874/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
1875/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1876/// templates.
1877///
1878/// primary-expression:
1879/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
1880///
1881ExprResult Parser::ParseBinaryTypeTrait() {
1882 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
1883 SourceLocation Loc = ConsumeToken();
1884
1885 SourceLocation LParen = Tok.getLocation();
1886 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1887 return ExprError();
1888
1889 TypeResult LhsTy = ParseTypeName();
1890 if (LhsTy.isInvalid()) {
1891 SkipUntil(tok::r_paren);
1892 return ExprError();
1893 }
1894
1895 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
1896 SkipUntil(tok::r_paren);
1897 return ExprError();
1898 }
1899
1900 TypeResult RhsTy = ParseTypeName();
1901 if (RhsTy.isInvalid()) {
1902 SkipUntil(tok::r_paren);
1903 return ExprError();
1904 }
1905
1906 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1907
1908 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
1909}
1910
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001911/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1912/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1913/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00001914ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001915Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00001916 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001917 SourceLocation LParenLoc,
1918 SourceLocation &RParenLoc) {
1919 assert(getLang().CPlusPlus && "Should only be called for C++!");
1920 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1921 assert(isTypeIdInParens() && "Not a type-id!");
1922
John McCall60d7b3a2010-08-24 06:29:42 +00001923 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00001924 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001925
1926 // We need to disambiguate a very ugly part of the C++ syntax:
1927 //
1928 // (T())x; - type-id
1929 // (T())*x; - type-id
1930 // (T())/x; - expression
1931 // (T()); - expression
1932 //
1933 // The bad news is that we cannot use the specialized tentative parser, since
1934 // it can only verify that the thing inside the parens can be parsed as
1935 // type-id, it is not useful for determining the context past the parens.
1936 //
1937 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001938 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001939 //
1940 // It uses a scheme similar to parsing inline methods. The parenthesized
1941 // tokens are cached, the context that follows is determined (possibly by
1942 // parsing a cast-expression), and then we re-introduce the cached tokens
1943 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001944
Mike Stump1eb44332009-09-09 15:08:12 +00001945 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001946 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001947
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001948 // Store the tokens of the parentheses. We will parse them after we determine
1949 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001950 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001951 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001952 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1953 return ExprError();
1954 }
1955
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001956 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001957 ParseAs = CompoundLiteral;
1958 } else {
1959 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001960 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1961 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1962 NotCastExpr = true;
1963 } else {
1964 // Try parsing the cast-expression that may follow.
1965 // If it is not a cast-expression, NotCastExpr will be true and no token
1966 // will be consumed.
1967 Result = ParseCastExpression(false/*isUnaryExpression*/,
1968 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00001969 NotCastExpr,
1970 ParsedType()/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001971 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001972
1973 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1974 // an expression.
1975 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001976 }
1977
Mike Stump1eb44332009-09-09 15:08:12 +00001978 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001979 Toks.push_back(Tok);
1980 // Re-enter the stored parenthesized tokens into the token stream, so we may
1981 // parse them now.
1982 PP.EnterTokenStream(Toks.data(), Toks.size(),
1983 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1984 // Drop the current token and bring the first cached one. It's the same token
1985 // as when we entered this function.
1986 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001987
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001988 if (ParseAs >= CompoundLiteral) {
1989 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001990
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001991 // Match the ')'.
1992 if (Tok.is(tok::r_paren))
1993 RParenLoc = ConsumeParen();
1994 else
1995 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001996
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001997 if (ParseAs == CompoundLiteral) {
1998 ExprType = CompoundLiteral;
1999 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2000 }
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002002 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2003 assert(ParseAs == CastExpr);
2004
2005 if (Ty.isInvalid())
2006 return ExprError();
2007
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002008 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002009
2010 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002011 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002012 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
John McCall9ae2f072010-08-23 23:25:46 +00002013 Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002014 return move(Result);
2015 }
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002017 // Not a compound literal, and not followed by a cast-expression.
2018 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002019
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002020 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002021 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002022 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002023 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002024
2025 // Match the ')'.
2026 if (Result.isInvalid()) {
2027 SkipUntil(tok::r_paren);
2028 return ExprError();
2029 }
Mike Stump1eb44332009-09-09 15:08:12 +00002030
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002031 if (Tok.is(tok::r_paren))
2032 RParenLoc = ConsumeParen();
2033 else
2034 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2035
2036 return move(Result);
2037}