blob: 9d4389b1ff65ff627ac8cb66197a4e1167dd255a [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"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000018#include "llvm/Support/ErrorHandling.h"
19
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Mike Stump1eb44332009-09-09 15:08:12 +000022/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000023///
24/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000025/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000026/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000027///
28/// '::'[opt] nested-name-specifier
29/// '::'
30///
31/// nested-name-specifier:
32/// type-name '::'
33/// namespace-name '::'
34/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000035/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000036///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000037///
Mike Stump1eb44332009-09-09 15:08:12 +000038/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000039/// nested-name-specifier (or empty)
40///
Mike Stump1eb44332009-09-09 15:08:12 +000041/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000042/// the "." or "->" of a member access expression, this parameter provides the
43/// type of the object whose members are being accessed.
44///
45/// \param EnteringContext whether we will be entering into the context of
46/// the nested-name-specifier after parsing it.
47///
Douglas Gregord4dca082010-02-24 18:44:31 +000048/// \param MayBePseudoDestructor When non-NULL, points to a flag that
49/// indicates whether this nested-name-specifier may be part of a
50/// pseudo-destructor name. In this case, the flag will be set false
51/// if we don't actually end up parsing a destructor name. Moreorover,
52/// if we do end up determining that we are parsing a destructor name,
53/// the last component of the nested-name-specifier is not parsed as
54/// part of the scope specifier.
55
Douglas Gregorb10cd042010-02-21 18:36:56 +000056/// member access expression, e.g., the \p T:: in \p p->T::m.
57///
John McCall9ba61662010-02-26 08:45:28 +000058/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +000059bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000060 Action::TypeTy *ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +000061 bool EnteringContext,
Douglas Gregord4dca082010-02-24 18:44:31 +000062 bool *MayBePseudoDestructor) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000063 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000064 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000065
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000066 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000067 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000068 SS.setRange(Tok.getAnnotationRange());
69 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +000070 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000071 }
Chris Lattnere607e802009-01-04 21:14:15 +000072
Douglas Gregor39a8de12009-02-25 19:37:18 +000073 bool HasScopeSpecifier = false;
74
Chris Lattner5b454732009-01-05 03:55:46 +000075 if (Tok.is(tok::coloncolon)) {
76 // ::new and ::delete aren't nested-name-specifiers.
77 tok::TokenKind NextKind = NextToken().getKind();
78 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
79 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattner55a7cef2009-01-05 00:13:00 +000081 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000082 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000083 SS.setBeginLoc(CCLoc);
Douglas Gregor23c94db2010-07-02 17:43:08 +000084 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000085 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000086 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000087 }
88
Douglas Gregord4dca082010-02-24 18:44:31 +000089 bool CheckForDestructor = false;
90 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
91 CheckForDestructor = true;
92 *MayBePseudoDestructor = false;
93 }
94
Douglas Gregor39a8de12009-02-25 19:37:18 +000095 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000096 if (HasScopeSpecifier) {
97 // C++ [basic.lookup.classref]p5:
98 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000099 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000100 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000101 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000102 // the class-name-or-namespace-name is looked up in global scope as a
103 // class-name or namespace-name.
104 //
105 // To implement this, we clear out the object type as soon as we've
106 // seen a leading '::' or part of a nested-name-specifier.
107 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000108
109 if (Tok.is(tok::code_completion)) {
110 // Code completion for a nested-name-specifier, where the code
111 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000112 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Douglas Gregordc845342010-05-25 05:58:43 +0000113 ConsumeCodeCompletionToken();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000114 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Douglas Gregor39a8de12009-02-25 19:37:18 +0000117 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000118 // nested-name-specifier 'template'[opt] simple-template-id '::'
119
120 // Parse the optional 'template' keyword, then make sure we have
121 // 'identifier <' after it.
122 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000124 // nested-name-specifier, since they aren't allowed to start with
125 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000126 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000127 break;
128
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000129 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000130 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000131
132 UnqualifiedId TemplateName;
133 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000134 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000135 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000136 ConsumeToken();
137 } else if (Tok.is(tok::kw_operator)) {
138 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000139 TemplateName)) {
140 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000141 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000142 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000143
Sean Hunte6252d12009-11-28 08:58:14 +0000144 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
145 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000146 Diag(TemplateName.getSourceRange().getBegin(),
147 diag::err_id_after_template_in_nested_name_spec)
148 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000149 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000150 break;
151 }
152 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000153 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000154 break;
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000157 // If the next token is not '<', we have a qualified-id that refers
158 // to a template name, such as T::template apply, but is not a
159 // template-id.
160 if (Tok.isNot(tok::less)) {
161 TPA.Revert();
162 break;
163 }
164
165 // Commit to parsing the template-id.
166 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000167 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000168 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000169 TemplateKWLoc,
170 SS,
171 TemplateName,
172 ObjectType,
173 EnteringContext,
174 Template)) {
175 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
176 TemplateKWLoc, false))
177 return true;
178 } else
John McCall9ba61662010-02-26 08:45:28 +0000179 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner77cf72a2009-06-26 03:47:46 +0000181 continue;
182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregor39a8de12009-02-25 19:37:18 +0000184 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000185 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000186 //
187 // simple-template-id '::'
188 //
189 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000190 // right kind (it should name a type or be dependent), and then
191 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000192 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000193 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord4dca082010-02-24 18:44:31 +0000194 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
195 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000196 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000197 }
198
Mike Stump1eb44332009-09-09 15:08:12 +0000199 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000200 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000201 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000202
Mike Stump1eb44332009-09-09 15:08:12 +0000203 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000204 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000205 Token TypeToken = Tok;
206 ConsumeToken();
207 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
208 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Douglas Gregor39a8de12009-02-25 19:37:18 +0000210 if (!HasScopeSpecifier) {
211 SS.setBeginLoc(TypeToken.getLocation());
212 HasScopeSpecifier = true;
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Douglas Gregor31a19b62009-04-01 21:51:26 +0000215 if (TypeToken.getAnnotationValue())
216 SS.setScopeRep(
Douglas Gregor23c94db2010-07-02 17:43:08 +0000217 Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000218 TypeToken.getAnnotationValue(),
219 TypeToken.getAnnotationRange(),
Douglas Gregoredc90502010-02-25 04:46:04 +0000220 CCLoc));
Douglas Gregor31a19b62009-04-01 21:51:26 +0000221 else
222 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000223 SS.setEndLoc(CCLoc);
224 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattner67b9e832009-06-26 03:45:46 +0000227 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000228 }
229
Chris Lattner5c7f7862009-06-26 03:52:38 +0000230
231 // The rest of the nested-name-specifier possibilities start with
232 // tok::identifier.
233 if (Tok.isNot(tok::identifier))
234 break;
235
236 IdentifierInfo &II = *Tok.getIdentifierInfo();
237
238 // nested-name-specifier:
239 // type-name '::'
240 // namespace-name '::'
241 // nested-name-specifier identifier '::'
242 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000243
244 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
245 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000246 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000247 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II, ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000248 EnteringContext) &&
249 // If the token after the colon isn't an identifier, it's still an
250 // error, but they probably meant something else strange so don't
251 // recover like this.
252 PP.LookAhead(1).is(tok::identifier)) {
253 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000254 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000255
256 // Recover as if the user wrote '::'.
257 Next.setKind(tok::coloncolon);
258 }
Chris Lattner46646492009-12-07 01:36:53 +0000259 }
260
Chris Lattner5c7f7862009-06-26 03:52:38 +0000261 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000262 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000263 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000264 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000265 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000266 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000267 }
268
Chris Lattner5c7f7862009-06-26 03:52:38 +0000269 // We have an identifier followed by a '::'. Lookup this name
270 // as the name in a nested-name-specifier.
271 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000272 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
273 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000274 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattner5c7f7862009-06-26 03:52:38 +0000276 if (!HasScopeSpecifier) {
277 SS.setBeginLoc(IdLoc);
278 HasScopeSpecifier = true;
279 }
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Argyrios Kyrtzidis661c36b2010-06-22 11:30:04 +0000281 if (!SS.isInvalid())
282 SS.setScopeRep(
Douglas Gregor23c94db2010-07-02 17:43:08 +0000283 Actions.ActOnCXXNestedNameSpecifier(getCurScope(), SS, IdLoc, CCLoc, II,
Argyrios Kyrtzidis661c36b2010-06-22 11:30:04 +0000284 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000285 SS.setEndLoc(CCLoc);
286 continue;
287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattner5c7f7862009-06-26 03:52:38 +0000289 // nested-name-specifier:
290 // type-name '<'
291 if (Next.is(tok::less)) {
292 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000293 UnqualifiedId TemplateName;
294 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000295 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000296 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000297 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000298 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000299 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000300 Template,
301 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000302 // We have found a template name, so annotate this this token
303 // with a template-id annotation. We do not permit the
304 // template-id to be translated into a type annotation,
305 // because some clients (e.g., the parsing of class template
306 // specializations) still want to see the original template-id
307 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000308 ConsumeToken();
309 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
310 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000311 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000312 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000313 }
314
315 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
316 IsTemplateArgumentList(1)) {
317 // We have something like t::getAs<T>, where getAs is a
318 // member of an unknown specialization. However, this will only
319 // parse correctly as a template, so suggest the keyword 'template'
320 // before 'getAs' and treat this as a dependent template name.
321 Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
322 << II.getName()
323 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
324
Douglas Gregord6ab2322010-06-16 23:00:59 +0000325 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000326 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000327 Tok.getLocation(), SS,
328 TemplateName, ObjectType,
329 EnteringContext, Template)) {
330 // Consume the identifier.
331 ConsumeToken();
332 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
333 SourceLocation(), false))
334 return true;
335 }
336 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000337 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000338
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000339 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000340 }
341 }
342
Douglas Gregor39a8de12009-02-25 19:37:18 +0000343 // We don't have any tokens that form the beginning of a
344 // nested-name-specifier, so we're done.
345 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Douglas Gregord4dca082010-02-24 18:44:31 +0000348 // Even if we didn't see any pieces of a nested-name-specifier, we
349 // still check whether there is a tilde in this position, which
350 // indicates a potential pseudo-destructor.
351 if (CheckForDestructor && Tok.is(tok::tilde))
352 *MayBePseudoDestructor = true;
353
John McCall9ba61662010-02-26 08:45:28 +0000354 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000355}
356
357/// ParseCXXIdExpression - Handle id-expression.
358///
359/// id-expression:
360/// unqualified-id
361/// qualified-id
362///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000363/// qualified-id:
364/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
365/// '::' identifier
366/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000367/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000368///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000369/// NOTE: The standard specifies that, for qualified-id, the parser does not
370/// expect:
371///
372/// '::' conversion-function-id
373/// '::' '~' class-name
374///
375/// This may cause a slight inconsistency on diagnostics:
376///
377/// class C {};
378/// namespace A {}
379/// void f() {
380/// :: A :: ~ C(); // Some Sema error about using destructor with a
381/// // namespace.
382/// :: ~ C(); // Some Parser error like 'unexpected ~'.
383/// }
384///
385/// We simplify the parser a bit and make it work like:
386///
387/// qualified-id:
388/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
389/// '::' unqualified-id
390///
391/// That way Sema can handle and report similar errors for namespaces and the
392/// global scope.
393///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000394/// The isAddressOfOperand parameter indicates that this id-expression is a
395/// direct operand of the address-of operator. This is, besides member contexts,
396/// the only place where a qualified-id naming a non-static class member may
397/// appear.
398///
399Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000400 // qualified-id:
401 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
402 // '::' unqualified-id
403 //
404 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000405 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000406
407 UnqualifiedId Name;
408 if (ParseUnqualifiedId(SS,
409 /*EnteringContext=*/false,
410 /*AllowDestructorName=*/false,
411 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000412 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000413 Name))
414 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000415
416 // This is only the direct operand of an & operator if it is not
417 // followed by a postfix-expression suffix.
418 if (isAddressOfOperand) {
419 switch (Tok.getKind()) {
420 case tok::l_square:
421 case tok::l_paren:
422 case tok::arrow:
423 case tok::period:
424 case tok::plusplus:
425 case tok::minusminus:
426 isAddressOfOperand = false;
427 break;
428
429 default:
430 break;
431 }
432 }
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000433
Douglas Gregor23c94db2010-07-02 17:43:08 +0000434 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000435 isAddressOfOperand);
436
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000437}
438
Reid Spencer5f016e22007-07-11 17:01:13 +0000439/// ParseCXXCasts - This handles the various ways to cast expressions to another
440/// type.
441///
442/// postfix-expression: [C++ 5.2p1]
443/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
444/// 'static_cast' '<' type-name '>' '(' expression ')'
445/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
446/// 'const_cast' '<' type-name '>' '(' expression ')'
447///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000448Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 tok::TokenKind Kind = Tok.getKind();
450 const char *CastName = 0; // For error messages
451
452 switch (Kind) {
453 default: assert(0 && "Unknown C++ cast!"); abort();
454 case tok::kw_const_cast: CastName = "const_cast"; break;
455 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
456 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
457 case tok::kw_static_cast: CastName = "static_cast"; break;
458 }
459
460 SourceLocation OpLoc = ConsumeToken();
461 SourceLocation LAngleBracketLoc = Tok.getLocation();
462
463 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000464 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000465
Douglas Gregor809070a2009-02-18 17:45:20 +0000466 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 SourceLocation RAngleBracketLoc = Tok.getLocation();
468
Chris Lattner1ab3b962008-11-18 07:48:38 +0000469 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000470 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000471
472 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
473
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000474 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
475 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000476
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000477 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000479 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000480 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000481
Douglas Gregor809070a2009-02-18 17:45:20 +0000482 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000483 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000484 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000485 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000486 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000487
Sebastian Redl20df9b72008-12-11 22:51:44 +0000488 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000489}
490
Sebastian Redlc42e1182008-11-11 11:37:55 +0000491/// ParseCXXTypeid - This handles the C++ typeid expression.
492///
493/// postfix-expression: [C++ 5.2p1]
494/// 'typeid' '(' expression ')'
495/// 'typeid' '(' type-id ')'
496///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000497Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000498 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
499
500 SourceLocation OpLoc = ConsumeToken();
501 SourceLocation LParenLoc = Tok.getLocation();
502 SourceLocation RParenLoc;
503
504 // typeid expressions are always parenthesized.
505 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
506 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000507 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000508
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000509 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000510
511 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000512 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000513
514 // Match the ')'.
515 MatchRHSPunctuation(tok::r_paren, LParenLoc);
516
Douglas Gregor809070a2009-02-18 17:45:20 +0000517 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000518 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000519
520 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000521 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000522 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000523 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000524 // When typeid is applied to an expression other than an lvalue of a
525 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000526 // operand (Clause 5).
527 //
Mike Stump1eb44332009-09-09 15:08:12 +0000528 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000529 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000530 // we the expression is potentially potentially evaluated.
531 EnterExpressionEvaluationContext Unevaluated(Actions,
532 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000533 Result = ParseExpression();
534
535 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000536 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000537 SkipUntil(tok::r_paren);
538 else {
539 MatchRHSPunctuation(tok::r_paren, LParenLoc);
540
541 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000542 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000543 }
544 }
545
Sebastian Redl20df9b72008-12-11 22:51:44 +0000546 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000547}
548
Douglas Gregord4dca082010-02-24 18:44:31 +0000549/// \brief Parse a C++ pseudo-destructor expression after the base,
550/// . or -> operator, and nested-name-specifier have already been
551/// parsed.
552///
553/// postfix-expression: [C++ 5.2]
554/// postfix-expression . pseudo-destructor-name
555/// postfix-expression -> pseudo-destructor-name
556///
557/// pseudo-destructor-name:
558/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
559/// ::[opt] nested-name-specifier template simple-template-id ::
560/// ~type-name
561/// ::[opt] nested-name-specifier[opt] ~type-name
562///
563Parser::OwningExprResult
564Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
565 tok::TokenKind OpKind,
566 CXXScopeSpec &SS,
567 Action::TypeTy *ObjectType) {
568 // We're parsing either a pseudo-destructor-name or a dependent
569 // member access that has the same form as a
570 // pseudo-destructor-name. We parse both in the same way and let
571 // the action model sort them out.
572 //
573 // Note that the ::[opt] nested-name-specifier[opt] has already
574 // been parsed, and if there was a simple-template-id, it has
575 // been coalesced into a template-id annotation token.
576 UnqualifiedId FirstTypeName;
577 SourceLocation CCLoc;
578 if (Tok.is(tok::identifier)) {
579 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
580 ConsumeToken();
581 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
582 CCLoc = ConsumeToken();
583 } else if (Tok.is(tok::annot_template_id)) {
584 FirstTypeName.setTemplateId(
585 (TemplateIdAnnotation *)Tok.getAnnotationValue());
586 ConsumeToken();
587 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
588 CCLoc = ConsumeToken();
589 } else {
590 FirstTypeName.setIdentifier(0, SourceLocation());
591 }
592
593 // Parse the tilde.
594 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
595 SourceLocation TildeLoc = ConsumeToken();
596 if (!Tok.is(tok::identifier)) {
597 Diag(Tok, diag::err_destructor_tilde_identifier);
598 return ExprError();
599 }
600
601 // Parse the second type.
602 UnqualifiedId SecondTypeName;
603 IdentifierInfo *Name = Tok.getIdentifierInfo();
604 SourceLocation NameLoc = ConsumeToken();
605 SecondTypeName.setIdentifier(Name, NameLoc);
606
607 // If there is a '<', the second type name is a template-id. Parse
608 // it as such.
609 if (Tok.is(tok::less) &&
610 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000611 SecondTypeName, /*AssumeTemplateName=*/true,
612 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000613 return ExprError();
614
Douglas Gregor23c94db2010-07-02 17:43:08 +0000615 return Actions.ActOnPseudoDestructorExpr(getCurScope(), move(Base), OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +0000616 SS, FirstTypeName, CCLoc,
617 TildeLoc, SecondTypeName,
618 Tok.is(tok::l_paren));
619}
620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
622///
623/// boolean-literal: [C++ 2.13.5]
624/// 'true'
625/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000626Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000628 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000629}
Chris Lattner50dd2892008-02-26 00:51:44 +0000630
631/// ParseThrowExpression - This handles the C++ throw expression.
632///
633/// throw-expression: [C++ 15]
634/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000635Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000636 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000637 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000638
Chris Lattner2a2819a2008-04-06 06:02:23 +0000639 // If the current token isn't the start of an assignment-expression,
640 // then the expression is not present. This handles things like:
641 // "C ? throw : (void)42", which is crazy but legal.
642 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
643 case tok::semi:
644 case tok::r_paren:
645 case tok::r_square:
646 case tok::r_brace:
647 case tok::colon:
648 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000649 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000650
Chris Lattner2a2819a2008-04-06 06:02:23 +0000651 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000652 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000653 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000654 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000655 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000656}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000657
658/// ParseCXXThis - This handles the C++ 'this' pointer.
659///
660/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
661/// a non-lvalue expression whose value is the address of the object for which
662/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000663Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000664 assert(Tok.is(tok::kw_this) && "Not 'this'!");
665 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000666 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000667}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000668
669/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
670/// Can be interpreted either as function-style casting ("int(x)")
671/// or class type construction ("ClassType(x,y,z)")
672/// or creation of a value-initialized type ("int()").
673///
674/// postfix-expression: [C++ 5.2p1]
675/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
676/// typename-specifier '(' expression-list[opt] ')' [TODO]
677///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000678Parser::OwningExprResult
679Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000680 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000681 TypeTy *TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000682
683 assert(Tok.is(tok::l_paren) && "Expected '('!");
684 SourceLocation LParenLoc = ConsumeParen();
685
Sebastian Redla55e52c2008-11-25 22:21:31 +0000686 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000687 CommaLocsTy CommaLocs;
688
689 if (Tok.isNot(tok::r_paren)) {
690 if (ParseExpressionList(Exprs, CommaLocs)) {
691 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000692 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000693 }
694 }
695
696 // Match the ')'.
697 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
698
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000699 // TypeRep could be null, if it references an invalid typedef.
700 if (!TypeRep)
701 return ExprError();
702
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000703 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
704 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000705 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
706 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000707 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000708}
709
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000710/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000711///
712/// condition:
713/// expression
714/// type-specifier-seq declarator '=' assignment-expression
715/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
716/// '=' assignment-expression
717///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000718/// \param ExprResult if the condition was parsed as an expression, the
719/// parsed expression.
720///
721/// \param DeclResult if the condition was parsed as a declaration, the
722/// parsed declaration.
723///
Douglas Gregor586596f2010-05-06 17:25:47 +0000724/// \param Loc The location of the start of the statement that requires this
725/// condition, e.g., the "for" in a for loop.
726///
727/// \param ConvertToBoolean Whether the condition expression should be
728/// converted to a boolean value.
729///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000730/// \returns true if there was a parsing, false otherwise.
731bool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000732 DeclPtrTy &DeclResult,
733 SourceLocation Loc,
734 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000735 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000736 Actions.CodeCompleteOrdinaryName(getCurScope(), Action::CCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000737 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000738 }
739
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000740 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000741 // Parse the expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000742 ExprResult = ParseExpression(); // expression
743 DeclResult = DeclPtrTy();
Douglas Gregor586596f2010-05-06 17:25:47 +0000744 if (ExprResult.isInvalid())
745 return true;
746
747 // If required, convert to a boolean value.
748 if (ConvertToBoolean)
749 ExprResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000750 = Actions.ActOnBooleanCondition(getCurScope(), Loc, move(ExprResult));
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000751 return ExprResult.isInvalid();
752 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000753
754 // type-specifier-seq
755 DeclSpec DS;
756 ParseSpecifierQualifierList(DS);
757
758 // declarator
759 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
760 ParseDeclarator(DeclaratorInfo);
761
762 // simple-asm-expr[opt]
763 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000764 SourceLocation Loc;
765 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000766 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000767 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000768 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000769 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000770 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000771 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000772 }
773
774 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000775 if (Tok.is(tok::kw___attribute)) {
776 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000777 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000778 DeclaratorInfo.AddAttributes(AttrList, Loc);
779 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000780
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000781 // Type-check the declaration itself.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000782 Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000783 DeclaratorInfo);
784 DeclResult = Dcl.get();
785 ExprResult = ExprError();
786
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000787 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000788 if (Tok.is(tok::equal)) {
789 SourceLocation EqualLoc = ConsumeToken();
790 OwningExprResult AssignExpr(ParseAssignmentExpression());
791 if (!AssignExpr.isInvalid())
792 Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
793 } else {
794 // FIXME: C++0x allows a braced-init-list
795 Diag(Tok, diag::err_expected_equal_after_declarator);
796 }
797
Douglas Gregor586596f2010-05-06 17:25:47 +0000798 // FIXME: Build a reference to this declaration? Convert it to bool?
799 // (This is currently handled by Sema).
800
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000801 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000802}
803
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000804/// \brief Determine whether the current token starts a C++
805/// simple-type-specifier.
806bool Parser::isCXXSimpleTypeSpecifier() const {
807 switch (Tok.getKind()) {
808 case tok::annot_typename:
809 case tok::kw_short:
810 case tok::kw_long:
811 case tok::kw_signed:
812 case tok::kw_unsigned:
813 case tok::kw_void:
814 case tok::kw_char:
815 case tok::kw_int:
816 case tok::kw_float:
817 case tok::kw_double:
818 case tok::kw_wchar_t:
819 case tok::kw_char16_t:
820 case tok::kw_char32_t:
821 case tok::kw_bool:
822 // FIXME: C++0x decltype support.
823 // GNU typeof support.
824 case tok::kw_typeof:
825 return true;
826
827 default:
828 break;
829 }
830
831 return false;
832}
833
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000834/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
835/// This should only be called when the current token is known to be part of
836/// simple-type-specifier.
837///
838/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000839/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000840/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
841/// char
842/// wchar_t
843/// bool
844/// short
845/// int
846/// long
847/// signed
848/// unsigned
849/// float
850/// double
851/// void
852/// [GNU] typeof-specifier
853/// [C++0x] auto [TODO]
854///
855/// type-name:
856/// class-name
857/// enum-name
858/// typedef-name
859///
860void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
861 DS.SetRangeStart(Tok.getLocation());
862 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000863 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000864 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000866 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000867 case tok::identifier: // foo::bar
868 case tok::coloncolon: // ::foo::bar
869 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000870 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000871 assert(0 && "Not a simple-type-specifier token!");
872 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000873
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000874 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000875 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000876 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000877 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000878 break;
879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000881 // builtin types
882 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000883 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000884 break;
885 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000886 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000887 break;
888 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000889 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000890 break;
891 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000892 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000893 break;
894 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000895 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000896 break;
897 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000898 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000899 break;
900 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000901 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000902 break;
903 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000904 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000905 break;
906 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000907 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000908 break;
909 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000910 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000911 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000912 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000913 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000914 break;
915 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000916 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000917 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000918 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000919 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000920 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000922 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000923 // GNU typeof support.
924 case tok::kw_typeof:
925 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000926 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000927 return;
928 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000929 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000930 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
931 else
932 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000933 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000934 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000935}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000936
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000937/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
938/// [dcl.name]), which is a non-empty sequence of type-specifiers,
939/// e.g., "const short int". Note that the DeclSpec is *not* finished
940/// by parsing the type-specifier-seq, because these sequences are
941/// typically followed by some form of declarator. Returns true and
942/// emits diagnostics if this is not a type-specifier-seq, false
943/// otherwise.
944///
945/// type-specifier-seq: [C++ 8.1]
946/// type-specifier type-specifier-seq[opt]
947///
948bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
949 DS.SetRangeStart(Tok.getLocation());
950 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000951 unsigned DiagID;
952 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000953
954 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000955 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
956 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000957 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000958 return true;
959 }
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Sebastian Redld9bafa72010-02-03 21:21:43 +0000961 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
962 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
963 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000964
Douglas Gregor396a9f22010-02-24 23:13:13 +0000965 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000966 return false;
967}
968
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000969/// \brief Finish parsing a C++ unqualified-id that is a template-id of
970/// some form.
971///
972/// This routine is invoked when a '<' is encountered after an identifier or
973/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
974/// whether the unqualified-id is actually a template-id. This routine will
975/// then parse the template arguments and form the appropriate template-id to
976/// return to the caller.
977///
978/// \param SS the nested-name-specifier that precedes this template-id, if
979/// we're actually parsing a qualified-id.
980///
981/// \param Name for constructor and destructor names, this is the actual
982/// identifier that may be a template-name.
983///
984/// \param NameLoc the location of the class-name in a constructor or
985/// destructor.
986///
987/// \param EnteringContext whether we're entering the scope of the
988/// nested-name-specifier.
989///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000990/// \param ObjectType if this unqualified-id occurs within a member access
991/// expression, the type of the base object whose member is being accessed.
992///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000993/// \param Id as input, describes the template-name or operator-function-id
994/// that precedes the '<'. If template arguments were parsed successfully,
995/// will be updated with the template-id.
996///
Douglas Gregord4dca082010-02-24 18:44:31 +0000997/// \param AssumeTemplateId When true, this routine will assume that the name
998/// refers to a template without performing name lookup to verify.
999///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001000/// \returns true if a parse error occurred, false otherwise.
1001bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1002 IdentifierInfo *Name,
1003 SourceLocation NameLoc,
1004 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001005 TypeTy *ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001006 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001007 bool AssumeTemplateId,
1008 SourceLocation TemplateKWLoc) {
1009 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1010 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001011
1012 TemplateTy Template;
1013 TemplateNameKind TNK = TNK_Non_template;
1014 switch (Id.getKind()) {
1015 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001016 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001017 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001018 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001019 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001020 Id, ObjectType, EnteringContext,
1021 Template);
1022 if (TNK == TNK_Non_template)
1023 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001024 } else {
1025 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001026 TNK = Actions.isTemplateName(getCurScope(), SS, Id, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001027 EnteringContext, Template,
1028 MemberOfUnknownSpecialization);
1029
1030 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1031 ObjectType && IsTemplateArgumentList()) {
1032 // We have something like t->getAs<T>(), where getAs is a
1033 // member of an unknown specialization. However, this will only
1034 // parse correctly as a template, so suggest the keyword 'template'
1035 // before 'getAs' and treat this as a dependent template name.
1036 std::string Name;
1037 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1038 Name = Id.Identifier->getName();
1039 else {
1040 Name = "operator ";
1041 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1042 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1043 else
1044 Name += Id.Identifier->getName();
1045 }
1046 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1047 << Name
1048 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001049 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001050 SS, Id, ObjectType,
1051 EnteringContext, Template);
1052 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001053 return true;
1054 }
1055 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001056 break;
1057
Douglas Gregor014e88d2009-11-03 23:16:33 +00001058 case UnqualifiedId::IK_ConstructorName: {
1059 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001060 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001061 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001062 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001063 EnteringContext, Template,
1064 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001065 break;
1066 }
1067
Douglas Gregor014e88d2009-11-03 23:16:33 +00001068 case UnqualifiedId::IK_DestructorName: {
1069 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001070 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001071 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001072 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001073 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001074 TemplateName, ObjectType,
1075 EnteringContext, Template);
1076 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001077 return true;
1078 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001079 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001080 EnteringContext, Template,
1081 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001082
1083 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001084 Diag(NameLoc, diag::err_destructor_template_id)
1085 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001086 return true;
1087 }
1088 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001089 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001090 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001091
1092 default:
1093 return false;
1094 }
1095
1096 if (TNK == TNK_Non_template)
1097 return false;
1098
1099 // Parse the enclosed template argument list.
1100 SourceLocation LAngleLoc, RAngleLoc;
1101 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001102 if (Tok.is(tok::less) &&
1103 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001104 &SS, true, LAngleLoc,
1105 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001106 RAngleLoc))
1107 return true;
1108
1109 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001110 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1111 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001112 // Form a parsed representation of the template-id to be stored in the
1113 // UnqualifiedId.
1114 TemplateIdAnnotation *TemplateId
1115 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1116
1117 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1118 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001119 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001120 TemplateId->TemplateNameLoc = Id.StartLocation;
1121 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001122 TemplateId->Name = 0;
1123 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1124 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001125 }
1126
1127 TemplateId->Template = Template.getAs<void*>();
1128 TemplateId->Kind = TNK;
1129 TemplateId->LAngleLoc = LAngleLoc;
1130 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001131 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001132 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001133 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001134 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001135
1136 Id.setTemplateId(TemplateId);
1137 return false;
1138 }
1139
1140 // Bundle the template arguments together.
1141 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001142 TemplateArgs.size());
1143
1144 // Constructor and destructor names.
1145 Action::TypeResult Type
1146 = Actions.ActOnTemplateIdType(Template, NameLoc,
1147 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001148 RAngleLoc);
1149 if (Type.isInvalid())
1150 return true;
1151
1152 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1153 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1154 else
1155 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1156
1157 return false;
1158}
1159
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001160/// \brief Parse an operator-function-id or conversion-function-id as part
1161/// of a C++ unqualified-id.
1162///
1163/// This routine is responsible only for parsing the operator-function-id or
1164/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001165///
1166/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001167/// operator-function-id: [C++ 13.5]
1168/// 'operator' operator
1169///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001170/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001171/// new delete new[] delete[]
1172/// + - * / % ^ & | ~
1173/// ! = < > += -= *= /= %=
1174/// ^= &= |= << >> >>= <<= == !=
1175/// <= >= && || ++ -- , ->* ->
1176/// () []
1177///
1178/// conversion-function-id: [C++ 12.3.2]
1179/// operator conversion-type-id
1180///
1181/// conversion-type-id:
1182/// type-specifier-seq conversion-declarator[opt]
1183///
1184/// conversion-declarator:
1185/// ptr-operator conversion-declarator[opt]
1186/// \endcode
1187///
1188/// \param The nested-name-specifier that preceded this unqualified-id. If
1189/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1190///
1191/// \param EnteringContext whether we are entering the scope of the
1192/// nested-name-specifier.
1193///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001194/// \param ObjectType if this unqualified-id occurs within a member access
1195/// expression, the type of the base object whose member is being accessed.
1196///
1197/// \param Result on a successful parse, contains the parsed unqualified-id.
1198///
1199/// \returns true if parsing fails, false otherwise.
1200bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1201 TypeTy *ObjectType,
1202 UnqualifiedId &Result) {
1203 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1204
1205 // Consume the 'operator' keyword.
1206 SourceLocation KeywordLoc = ConsumeToken();
1207
1208 // Determine what kind of operator name we have.
1209 unsigned SymbolIdx = 0;
1210 SourceLocation SymbolLocations[3];
1211 OverloadedOperatorKind Op = OO_None;
1212 switch (Tok.getKind()) {
1213 case tok::kw_new:
1214 case tok::kw_delete: {
1215 bool isNew = Tok.getKind() == tok::kw_new;
1216 // Consume the 'new' or 'delete'.
1217 SymbolLocations[SymbolIdx++] = ConsumeToken();
1218 if (Tok.is(tok::l_square)) {
1219 // Consume the '['.
1220 SourceLocation LBracketLoc = ConsumeBracket();
1221 // Consume the ']'.
1222 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1223 LBracketLoc);
1224 if (RBracketLoc.isInvalid())
1225 return true;
1226
1227 SymbolLocations[SymbolIdx++] = LBracketLoc;
1228 SymbolLocations[SymbolIdx++] = RBracketLoc;
1229 Op = isNew? OO_Array_New : OO_Array_Delete;
1230 } else {
1231 Op = isNew? OO_New : OO_Delete;
1232 }
1233 break;
1234 }
1235
1236#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1237 case tok::Token: \
1238 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1239 Op = OO_##Name; \
1240 break;
1241#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1242#include "clang/Basic/OperatorKinds.def"
1243
1244 case tok::l_paren: {
1245 // Consume the '('.
1246 SourceLocation LParenLoc = ConsumeParen();
1247 // Consume the ')'.
1248 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1249 LParenLoc);
1250 if (RParenLoc.isInvalid())
1251 return true;
1252
1253 SymbolLocations[SymbolIdx++] = LParenLoc;
1254 SymbolLocations[SymbolIdx++] = RParenLoc;
1255 Op = OO_Call;
1256 break;
1257 }
1258
1259 case tok::l_square: {
1260 // Consume the '['.
1261 SourceLocation LBracketLoc = ConsumeBracket();
1262 // Consume the ']'.
1263 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1264 LBracketLoc);
1265 if (RBracketLoc.isInvalid())
1266 return true;
1267
1268 SymbolLocations[SymbolIdx++] = LBracketLoc;
1269 SymbolLocations[SymbolIdx++] = RBracketLoc;
1270 Op = OO_Subscript;
1271 break;
1272 }
1273
1274 case tok::code_completion: {
1275 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001276 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001277
1278 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001279 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001280
1281 // Don't try to parse any further.
1282 return true;
1283 }
1284
1285 default:
1286 break;
1287 }
1288
1289 if (Op != OO_None) {
1290 // We have parsed an operator-function-id.
1291 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1292 return false;
1293 }
Sean Hunt0486d742009-11-28 04:44:28 +00001294
1295 // Parse a literal-operator-id.
1296 //
1297 // literal-operator-id: [C++0x 13.5.8]
1298 // operator "" identifier
1299
1300 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1301 if (Tok.getLength() != 2)
1302 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1303 ConsumeStringToken();
1304
1305 if (Tok.isNot(tok::identifier)) {
1306 Diag(Tok.getLocation(), diag::err_expected_ident);
1307 return true;
1308 }
1309
1310 IdentifierInfo *II = Tok.getIdentifierInfo();
1311 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001312 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001313 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001314
1315 // Parse a conversion-function-id.
1316 //
1317 // conversion-function-id: [C++ 12.3.2]
1318 // operator conversion-type-id
1319 //
1320 // conversion-type-id:
1321 // type-specifier-seq conversion-declarator[opt]
1322 //
1323 // conversion-declarator:
1324 // ptr-operator conversion-declarator[opt]
1325
1326 // Parse the type-specifier-seq.
1327 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001328 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001329 return true;
1330
1331 // Parse the conversion-declarator, which is merely a sequence of
1332 // ptr-operators.
1333 Declarator D(DS, Declarator::TypeNameContext);
1334 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1335
1336 // Finish up the type.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001337 Action::TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001338 if (Ty.isInvalid())
1339 return true;
1340
1341 // Note that this is a conversion-function-id.
1342 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1343 D.getSourceRange().getEnd());
1344 return false;
1345}
1346
1347/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1348/// name of an entity.
1349///
1350/// \code
1351/// unqualified-id: [C++ expr.prim.general]
1352/// identifier
1353/// operator-function-id
1354/// conversion-function-id
1355/// [C++0x] literal-operator-id [TODO]
1356/// ~ class-name
1357/// template-id
1358///
1359/// \endcode
1360///
1361/// \param The nested-name-specifier that preceded this unqualified-id. If
1362/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1363///
1364/// \param EnteringContext whether we are entering the scope of the
1365/// nested-name-specifier.
1366///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001367/// \param AllowDestructorName whether we allow parsing of a destructor name.
1368///
1369/// \param AllowConstructorName whether we allow parsing a constructor name.
1370///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001371/// \param ObjectType if this unqualified-id occurs within a member access
1372/// expression, the type of the base object whose member is being accessed.
1373///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001374/// \param Result on a successful parse, contains the parsed unqualified-id.
1375///
1376/// \returns true if parsing fails, false otherwise.
1377bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1378 bool AllowDestructorName,
1379 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001380 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001381 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001382
1383 // Handle 'A::template B'. This is for template-ids which have not
1384 // already been annotated by ParseOptionalCXXScopeSpecifier().
1385 bool TemplateSpecified = false;
1386 SourceLocation TemplateKWLoc;
1387 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1388 (ObjectType || SS.isSet())) {
1389 TemplateSpecified = true;
1390 TemplateKWLoc = ConsumeToken();
1391 }
1392
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001393 // unqualified-id:
1394 // identifier
1395 // template-id (when it hasn't already been annotated)
1396 if (Tok.is(tok::identifier)) {
1397 // Consume the identifier.
1398 IdentifierInfo *Id = Tok.getIdentifierInfo();
1399 SourceLocation IdLoc = ConsumeToken();
1400
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001401 if (!getLang().CPlusPlus) {
1402 // If we're not in C++, only identifiers matter. Record the
1403 // identifier and return.
1404 Result.setIdentifier(Id, IdLoc);
1405 return false;
1406 }
1407
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001408 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001409 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001410 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001411 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001412 &SS, false),
1413 IdLoc, IdLoc);
1414 } else {
1415 // We have parsed an identifier.
1416 Result.setIdentifier(Id, IdLoc);
1417 }
1418
1419 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001420 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001421 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001422 ObjectType, Result,
1423 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001424
1425 return false;
1426 }
1427
1428 // unqualified-id:
1429 // template-id (already parsed and annotated)
1430 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001431 TemplateIdAnnotation *TemplateId
1432 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1433
1434 // If the template-name names the current class, then this is a constructor
1435 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001436 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001437 if (SS.isSet()) {
1438 // C++ [class.qual]p2 specifies that a qualified template-name
1439 // is taken as the constructor name where a constructor can be
1440 // declared. Thus, the template arguments are extraneous, so
1441 // complain about them and remove them entirely.
1442 Diag(TemplateId->TemplateNameLoc,
1443 diag::err_out_of_line_constructor_template_id)
1444 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001445 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001446 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1447 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1448 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001449 getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001450 &SS, false),
1451 TemplateId->TemplateNameLoc,
1452 TemplateId->RAngleLoc);
1453 TemplateId->Destroy();
1454 ConsumeToken();
1455 return false;
1456 }
1457
1458 Result.setConstructorTemplateId(TemplateId);
1459 ConsumeToken();
1460 return false;
1461 }
1462
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001463 // We have already parsed a template-id; consume the annotation token as
1464 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001465 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001466 ConsumeToken();
1467 return false;
1468 }
1469
1470 // unqualified-id:
1471 // operator-function-id
1472 // conversion-function-id
1473 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001474 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001475 return true;
1476
Sean Hunte6252d12009-11-28 08:58:14 +00001477 // If we have an operator-function-id or a literal-operator-id and the next
1478 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001479 //
1480 // template-id:
1481 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001482 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1483 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001484 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001485 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1486 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001487 Result,
1488 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001489
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001490 return false;
1491 }
1492
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001493 if (getLang().CPlusPlus &&
1494 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001495 // C++ [expr.unary.op]p10:
1496 // There is an ambiguity in the unary-expression ~X(), where X is a
1497 // class-name. The ambiguity is resolved in favor of treating ~ as a
1498 // unary complement rather than treating ~X as referring to a destructor.
1499
1500 // Parse the '~'.
1501 SourceLocation TildeLoc = ConsumeToken();
1502
1503 // Parse the class-name.
1504 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001505 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001506 return true;
1507 }
1508
1509 // Parse the class-name (or template-name in a simple-template-id).
1510 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1511 SourceLocation ClassNameLoc = ConsumeToken();
1512
Douglas Gregor0278e122010-05-05 05:58:24 +00001513 if (TemplateSpecified || Tok.is(tok::less)) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001514 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1515 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001516 EnteringContext, ObjectType, Result,
1517 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001518 }
1519
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001520 // Note that this is a destructor name.
Douglas Gregor124b8782010-02-16 19:09:40 +00001521 Action::TypeTy *Ty = Actions.getDestructorName(TildeLoc, *ClassName,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001522 ClassNameLoc, getCurScope(),
Douglas Gregor124b8782010-02-16 19:09:40 +00001523 SS, ObjectType,
1524 EnteringContext);
1525 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001526 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001527
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001528 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001529 return false;
1530 }
1531
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001532 Diag(Tok, diag::err_expected_unqualified_id)
1533 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001534 return true;
1535}
1536
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001537/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1538/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001539///
Chris Lattner59232d32009-01-04 21:25:24 +00001540/// This method is called to parse the new expression after the optional :: has
1541/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1542/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001543///
1544/// new-expression:
1545/// '::'[opt] 'new' new-placement[opt] new-type-id
1546/// new-initializer[opt]
1547/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1548/// new-initializer[opt]
1549///
1550/// new-placement:
1551/// '(' expression-list ')'
1552///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001553/// new-type-id:
1554/// type-specifier-seq new-declarator[opt]
1555///
1556/// new-declarator:
1557/// ptr-operator new-declarator[opt]
1558/// direct-new-declarator
1559///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001560/// new-initializer:
1561/// '(' expression-list[opt] ')'
1562/// [C++0x] braced-init-list [TODO]
1563///
Chris Lattner59232d32009-01-04 21:25:24 +00001564Parser::OwningExprResult
1565Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1566 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1567 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001568
1569 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1570 // second form of new-expression. It can't be a new-type-id.
1571
Sebastian Redla55e52c2008-11-25 22:21:31 +00001572 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001573 SourceLocation PlacementLParen, PlacementRParen;
1574
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001575 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001576 DeclSpec DS;
1577 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001578 if (Tok.is(tok::l_paren)) {
1579 // If it turns out to be a placement, we change the type location.
1580 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001581 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1582 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001583 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001584 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001585
1586 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001587 if (PlacementRParen.isInvalid()) {
1588 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001589 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001590 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001591
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001592 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001593 // Reset the placement locations. There was no placement.
1594 PlacementLParen = PlacementRParen = SourceLocation();
1595 ParenTypeId = true;
1596 } else {
1597 // We still need the type.
1598 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001599 SourceLocation LParen = ConsumeParen();
1600 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001601 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001602 ParseDeclarator(DeclaratorInfo);
1603 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001604 ParenTypeId = true;
1605 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001606 if (ParseCXXTypeSpecifierSeq(DS))
1607 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001608 else {
1609 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001610 ParseDeclaratorInternal(DeclaratorInfo,
1611 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001612 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001613 ParenTypeId = false;
1614 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001615 }
1616 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001617 // A new-type-id is a simplified type-id, where essentially the
1618 // direct-declarator is replaced by a direct-new-declarator.
1619 if (ParseCXXTypeSpecifierSeq(DS))
1620 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001621 else {
1622 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001623 ParseDeclaratorInternal(DeclaratorInfo,
1624 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001625 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001626 ParenTypeId = false;
1627 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001628 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001629 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001630 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001631 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001632
Sebastian Redla55e52c2008-11-25 22:21:31 +00001633 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001634 SourceLocation ConstructorLParen, ConstructorRParen;
1635
1636 if (Tok.is(tok::l_paren)) {
1637 ConstructorLParen = ConsumeParen();
1638 if (Tok.isNot(tok::r_paren)) {
1639 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001640 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1641 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001642 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001643 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001644 }
1645 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001646 if (ConstructorRParen.isInvalid()) {
1647 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001648 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001649 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001650 }
1651
Sebastian Redlf53597f2009-03-15 17:47:39 +00001652 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1653 move_arg(PlacementArgs), PlacementRParen,
1654 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1655 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001656}
1657
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001658/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1659/// passed to ParseDeclaratorInternal.
1660///
1661/// direct-new-declarator:
1662/// '[' expression ']'
1663/// direct-new-declarator '[' constant-expression ']'
1664///
Chris Lattner59232d32009-01-04 21:25:24 +00001665void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001666 // Parse the array dimensions.
1667 bool first = true;
1668 while (Tok.is(tok::l_square)) {
1669 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001670 OwningExprResult Size(first ? ParseExpression()
1671 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001672 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001673 // Recover
1674 SkipUntil(tok::r_square);
1675 return;
1676 }
1677 first = false;
1678
Sebastian Redlab197ba2009-02-09 18:23:29 +00001679 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001680 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001681 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001682 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001683
Sebastian Redlab197ba2009-02-09 18:23:29 +00001684 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001685 return;
1686 }
1687}
1688
1689/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1690/// This ambiguity appears in the syntax of the C++ new operator.
1691///
1692/// new-expression:
1693/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1694/// new-initializer[opt]
1695///
1696/// new-placement:
1697/// '(' expression-list ')'
1698///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001699bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001700 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001701 // The '(' was already consumed.
1702 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001703 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001704 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001705 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001706 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001707 }
1708
1709 // It's not a type, it has to be an expression list.
1710 // Discard the comma locations - ActOnCXXNew has enough parameters.
1711 CommaLocsTy CommaLocs;
1712 return ParseExpressionList(PlacementArgs, CommaLocs);
1713}
1714
1715/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1716/// to free memory allocated by new.
1717///
Chris Lattner59232d32009-01-04 21:25:24 +00001718/// This method is called to parse the 'delete' expression after the optional
1719/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1720/// and "Start" is its location. Otherwise, "Start" is the location of the
1721/// 'delete' token.
1722///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001723/// delete-expression:
1724/// '::'[opt] 'delete' cast-expression
1725/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001726Parser::OwningExprResult
1727Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1728 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1729 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001730
1731 // Array delete?
1732 bool ArrayDelete = false;
1733 if (Tok.is(tok::l_square)) {
1734 ArrayDelete = true;
1735 SourceLocation LHS = ConsumeBracket();
1736 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1737 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001738 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001739 }
1740
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001741 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001742 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001743 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001744
Sebastian Redlf53597f2009-03-15 17:47:39 +00001745 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001746}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001747
Mike Stump1eb44332009-09-09 15:08:12 +00001748static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001749 switch(kind) {
1750 default: assert(false && "Not a known unary type trait.");
1751 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1752 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1753 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1754 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1755 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1756 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1757 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1758 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1759 case tok::kw___is_abstract: return UTT_IsAbstract;
1760 case tok::kw___is_class: return UTT_IsClass;
1761 case tok::kw___is_empty: return UTT_IsEmpty;
1762 case tok::kw___is_enum: return UTT_IsEnum;
1763 case tok::kw___is_pod: return UTT_IsPOD;
1764 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1765 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001766 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001767 }
1768}
1769
1770/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1771/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1772/// templates.
1773///
1774/// primary-expression:
1775/// [GNU] unary-type-trait '(' type-id ')'
1776///
Mike Stump1eb44332009-09-09 15:08:12 +00001777Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001778 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1779 SourceLocation Loc = ConsumeToken();
1780
1781 SourceLocation LParen = Tok.getLocation();
1782 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1783 return ExprError();
1784
1785 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1786 // there will be cryptic errors about mismatched parentheses and missing
1787 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001788 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001789
1790 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1791
Douglas Gregor809070a2009-02-18 17:45:20 +00001792 if (Ty.isInvalid())
1793 return ExprError();
1794
1795 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001796}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001797
1798/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1799/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1800/// based on the context past the parens.
1801Parser::OwningExprResult
1802Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1803 TypeTy *&CastTy,
1804 SourceLocation LParenLoc,
1805 SourceLocation &RParenLoc) {
1806 assert(getLang().CPlusPlus && "Should only be called for C++!");
1807 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1808 assert(isTypeIdInParens() && "Not a type-id!");
1809
1810 OwningExprResult Result(Actions, true);
1811 CastTy = 0;
1812
1813 // We need to disambiguate a very ugly part of the C++ syntax:
1814 //
1815 // (T())x; - type-id
1816 // (T())*x; - type-id
1817 // (T())/x; - expression
1818 // (T()); - expression
1819 //
1820 // The bad news is that we cannot use the specialized tentative parser, since
1821 // it can only verify that the thing inside the parens can be parsed as
1822 // type-id, it is not useful for determining the context past the parens.
1823 //
1824 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001825 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001826 //
1827 // It uses a scheme similar to parsing inline methods. The parenthesized
1828 // tokens are cached, the context that follows is determined (possibly by
1829 // parsing a cast-expression), and then we re-introduce the cached tokens
1830 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001831
Mike Stump1eb44332009-09-09 15:08:12 +00001832 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001833 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001834
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001835 // Store the tokens of the parentheses. We will parse them after we determine
1836 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001837 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001838 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001839 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1840 return ExprError();
1841 }
1842
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001843 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001844 ParseAs = CompoundLiteral;
1845 } else {
1846 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001847 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1848 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1849 NotCastExpr = true;
1850 } else {
1851 // Try parsing the cast-expression that may follow.
1852 // If it is not a cast-expression, NotCastExpr will be true and no token
1853 // will be consumed.
1854 Result = ParseCastExpression(false/*isUnaryExpression*/,
1855 false/*isAddressofOperand*/,
Daniel Dunbarc0012d62010-06-02 15:47:03 +00001856 NotCastExpr, 0/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001857 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001858
1859 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1860 // an expression.
1861 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001862 }
1863
Mike Stump1eb44332009-09-09 15:08:12 +00001864 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001865 Toks.push_back(Tok);
1866 // Re-enter the stored parenthesized tokens into the token stream, so we may
1867 // parse them now.
1868 PP.EnterTokenStream(Toks.data(), Toks.size(),
1869 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1870 // Drop the current token and bring the first cached one. It's the same token
1871 // as when we entered this function.
1872 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001873
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001874 if (ParseAs >= CompoundLiteral) {
1875 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001876
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001877 // Match the ')'.
1878 if (Tok.is(tok::r_paren))
1879 RParenLoc = ConsumeParen();
1880 else
1881 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001882
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001883 if (ParseAs == CompoundLiteral) {
1884 ExprType = CompoundLiteral;
1885 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1886 }
Mike Stump1eb44332009-09-09 15:08:12 +00001887
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001888 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1889 assert(ParseAs == CastExpr);
1890
1891 if (Ty.isInvalid())
1892 return ExprError();
1893
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001894 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001895
1896 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001897 if (!Result.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001898 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001899 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001900 return move(Result);
1901 }
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001903 // Not a compound literal, and not followed by a cast-expression.
1904 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001905
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001906 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001907 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001908 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1909 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1910
1911 // Match the ')'.
1912 if (Result.isInvalid()) {
1913 SkipUntil(tok::r_paren);
1914 return ExprError();
1915 }
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001917 if (Tok.is(tok::r_paren))
1918 RParenLoc = ConsumeParen();
1919 else
1920 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1921
1922 return move(Result);
1923}