blob: dd74c4e998c93e43e8f71b4e2a33f4c13d1f51b9 [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 Gregor35073692009-03-26 23:56:24 +000084 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, 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 '::'.
112 Actions.CodeCompleteQualifiedId(CurScope, 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;
168 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(CurScope,
169 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(
Mike Stump1eb44332009-09-09 15:08:12 +0000217 Actions.ActOnCXXNestedNameSpecifier(CurScope, 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 Gregoredc90502010-02-25 04:46:04 +0000247 if (Actions.IsInvalidUnlessNestedName(CurScope, 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) &&
263 !Actions.isNonTypeNestedNameSpecifier(CurScope, SS, Tok.getLocation(),
264 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
Chris Lattner5c7f7862009-06-26 03:52:38 +0000281 if (SS.isInvalid())
282 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Chris Lattner5c7f7862009-06-26 03:52:38 +0000284 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000285 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregoredc90502010-02-25 04:46:04 +0000286 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000287 SS.setEndLoc(CCLoc);
288 continue;
289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattner5c7f7862009-06-26 03:52:38 +0000291 // nested-name-specifier:
292 // type-name '<'
293 if (Next.is(tok::less)) {
294 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000295 UnqualifiedId TemplateName;
296 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000297 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000298 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
299 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000300 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000301 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000302 Template,
303 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000304 // We have found a template name, so annotate this this token
305 // with a template-id annotation. We do not permit the
306 // template-id to be translated into a type annotation,
307 // because some clients (e.g., the parsing of class template
308 // specializations) still want to see the original template-id
309 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000310 ConsumeToken();
311 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
312 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000313 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000314 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000315 }
316
317 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
318 IsTemplateArgumentList(1)) {
319 // We have something like t::getAs<T>, where getAs is a
320 // member of an unknown specialization. However, this will only
321 // parse correctly as a template, so suggest the keyword 'template'
322 // before 'getAs' and treat this as a dependent template name.
323 Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
324 << II.getName()
325 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
326
Douglas Gregord6ab2322010-06-16 23:00:59 +0000327 if (TemplateNameKind TNK
328 = Actions.ActOnDependentTemplateName(CurScope,
329 Tok.getLocation(), SS,
330 TemplateName, ObjectType,
331 EnteringContext, Template)) {
332 // Consume the identifier.
333 ConsumeToken();
334 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
335 SourceLocation(), false))
336 return true;
337 }
338 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000339 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000340
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000341 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000342 }
343 }
344
Douglas Gregor39a8de12009-02-25 19:37:18 +0000345 // We don't have any tokens that form the beginning of a
346 // nested-name-specifier, so we're done.
347 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Douglas Gregord4dca082010-02-24 18:44:31 +0000350 // Even if we didn't see any pieces of a nested-name-specifier, we
351 // still check whether there is a tilde in this position, which
352 // indicates a potential pseudo-destructor.
353 if (CheckForDestructor && Tok.is(tok::tilde))
354 *MayBePseudoDestructor = true;
355
John McCall9ba61662010-02-26 08:45:28 +0000356 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000357}
358
359/// ParseCXXIdExpression - Handle id-expression.
360///
361/// id-expression:
362/// unqualified-id
363/// qualified-id
364///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000365/// qualified-id:
366/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
367/// '::' identifier
368/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000369/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000370///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000371/// NOTE: The standard specifies that, for qualified-id, the parser does not
372/// expect:
373///
374/// '::' conversion-function-id
375/// '::' '~' class-name
376///
377/// This may cause a slight inconsistency on diagnostics:
378///
379/// class C {};
380/// namespace A {}
381/// void f() {
382/// :: A :: ~ C(); // Some Sema error about using destructor with a
383/// // namespace.
384/// :: ~ C(); // Some Parser error like 'unexpected ~'.
385/// }
386///
387/// We simplify the parser a bit and make it work like:
388///
389/// qualified-id:
390/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
391/// '::' unqualified-id
392///
393/// That way Sema can handle and report similar errors for namespaces and the
394/// global scope.
395///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000396/// The isAddressOfOperand parameter indicates that this id-expression is a
397/// direct operand of the address-of operator. This is, besides member contexts,
398/// the only place where a qualified-id naming a non-static class member may
399/// appear.
400///
401Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000402 // qualified-id:
403 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
404 // '::' unqualified-id
405 //
406 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000407 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000408
409 UnqualifiedId Name;
410 if (ParseUnqualifiedId(SS,
411 /*EnteringContext=*/false,
412 /*AllowDestructorName=*/false,
413 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000414 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000415 Name))
416 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000417
418 // This is only the direct operand of an & operator if it is not
419 // followed by a postfix-expression suffix.
420 if (isAddressOfOperand) {
421 switch (Tok.getKind()) {
422 case tok::l_square:
423 case tok::l_paren:
424 case tok::arrow:
425 case tok::period:
426 case tok::plusplus:
427 case tok::minusminus:
428 isAddressOfOperand = false;
429 break;
430
431 default:
432 break;
433 }
434 }
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000435
436 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
437 isAddressOfOperand);
438
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000439}
440
Reid Spencer5f016e22007-07-11 17:01:13 +0000441/// ParseCXXCasts - This handles the various ways to cast expressions to another
442/// type.
443///
444/// postfix-expression: [C++ 5.2p1]
445/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
446/// 'static_cast' '<' type-name '>' '(' expression ')'
447/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
448/// 'const_cast' '<' type-name '>' '(' expression ')'
449///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000450Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 tok::TokenKind Kind = Tok.getKind();
452 const char *CastName = 0; // For error messages
453
454 switch (Kind) {
455 default: assert(0 && "Unknown C++ cast!"); abort();
456 case tok::kw_const_cast: CastName = "const_cast"; break;
457 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
458 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
459 case tok::kw_static_cast: CastName = "static_cast"; break;
460 }
461
462 SourceLocation OpLoc = ConsumeToken();
463 SourceLocation LAngleBracketLoc = Tok.getLocation();
464
465 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000466 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000467
Douglas Gregor809070a2009-02-18 17:45:20 +0000468 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 SourceLocation RAngleBracketLoc = Tok.getLocation();
470
Chris Lattner1ab3b962008-11-18 07:48:38 +0000471 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000472 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000473
474 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
475
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000476 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
477 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000478
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000479 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000481 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000482 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000483
Douglas Gregor809070a2009-02-18 17:45:20 +0000484 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000485 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000486 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000487 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000488 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000489
Sebastian Redl20df9b72008-12-11 22:51:44 +0000490 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000491}
492
Sebastian Redlc42e1182008-11-11 11:37:55 +0000493/// ParseCXXTypeid - This handles the C++ typeid expression.
494///
495/// postfix-expression: [C++ 5.2p1]
496/// 'typeid' '(' expression ')'
497/// 'typeid' '(' type-id ')'
498///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000499Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000500 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
501
502 SourceLocation OpLoc = ConsumeToken();
503 SourceLocation LParenLoc = Tok.getLocation();
504 SourceLocation RParenLoc;
505
506 // typeid expressions are always parenthesized.
507 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
508 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000509 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000510
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000511 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000512
513 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000514 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000515
516 // Match the ')'.
517 MatchRHSPunctuation(tok::r_paren, LParenLoc);
518
Douglas Gregor809070a2009-02-18 17:45:20 +0000519 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000520 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000521
522 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000523 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000524 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000525 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000526 // When typeid is applied to an expression other than an lvalue of a
527 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000528 // operand (Clause 5).
529 //
Mike Stump1eb44332009-09-09 15:08:12 +0000530 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000531 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000532 // we the expression is potentially potentially evaluated.
533 EnterExpressionEvaluationContext Unevaluated(Actions,
534 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000535 Result = ParseExpression();
536
537 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000538 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000539 SkipUntil(tok::r_paren);
540 else {
541 MatchRHSPunctuation(tok::r_paren, LParenLoc);
542
543 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000544 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000545 }
546 }
547
Sebastian Redl20df9b72008-12-11 22:51:44 +0000548 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000549}
550
Douglas Gregord4dca082010-02-24 18:44:31 +0000551/// \brief Parse a C++ pseudo-destructor expression after the base,
552/// . or -> operator, and nested-name-specifier have already been
553/// parsed.
554///
555/// postfix-expression: [C++ 5.2]
556/// postfix-expression . pseudo-destructor-name
557/// postfix-expression -> pseudo-destructor-name
558///
559/// pseudo-destructor-name:
560/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
561/// ::[opt] nested-name-specifier template simple-template-id ::
562/// ~type-name
563/// ::[opt] nested-name-specifier[opt] ~type-name
564///
565Parser::OwningExprResult
566Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
567 tok::TokenKind OpKind,
568 CXXScopeSpec &SS,
569 Action::TypeTy *ObjectType) {
570 // We're parsing either a pseudo-destructor-name or a dependent
571 // member access that has the same form as a
572 // pseudo-destructor-name. We parse both in the same way and let
573 // the action model sort them out.
574 //
575 // Note that the ::[opt] nested-name-specifier[opt] has already
576 // been parsed, and if there was a simple-template-id, it has
577 // been coalesced into a template-id annotation token.
578 UnqualifiedId FirstTypeName;
579 SourceLocation CCLoc;
580 if (Tok.is(tok::identifier)) {
581 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
582 ConsumeToken();
583 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
584 CCLoc = ConsumeToken();
585 } else if (Tok.is(tok::annot_template_id)) {
586 FirstTypeName.setTemplateId(
587 (TemplateIdAnnotation *)Tok.getAnnotationValue());
588 ConsumeToken();
589 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
590 CCLoc = ConsumeToken();
591 } else {
592 FirstTypeName.setIdentifier(0, SourceLocation());
593 }
594
595 // Parse the tilde.
596 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
597 SourceLocation TildeLoc = ConsumeToken();
598 if (!Tok.is(tok::identifier)) {
599 Diag(Tok, diag::err_destructor_tilde_identifier);
600 return ExprError();
601 }
602
603 // Parse the second type.
604 UnqualifiedId SecondTypeName;
605 IdentifierInfo *Name = Tok.getIdentifierInfo();
606 SourceLocation NameLoc = ConsumeToken();
607 SecondTypeName.setIdentifier(Name, NameLoc);
608
609 // If there is a '<', the second type name is a template-id. Parse
610 // it as such.
611 if (Tok.is(tok::less) &&
612 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000613 SecondTypeName, /*AssumeTemplateName=*/true,
614 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000615 return ExprError();
616
617 return Actions.ActOnPseudoDestructorExpr(CurScope, move(Base), OpLoc, OpKind,
618 SS, FirstTypeName, CCLoc,
619 TildeLoc, SecondTypeName,
620 Tok.is(tok::l_paren));
621}
622
Reid Spencer5f016e22007-07-11 17:01:13 +0000623/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
624///
625/// boolean-literal: [C++ 2.13.5]
626/// 'true'
627/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000628Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000630 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000631}
Chris Lattner50dd2892008-02-26 00:51:44 +0000632
633/// ParseThrowExpression - This handles the C++ throw expression.
634///
635/// throw-expression: [C++ 15]
636/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000637Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000638 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000639 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000640
Chris Lattner2a2819a2008-04-06 06:02:23 +0000641 // If the current token isn't the start of an assignment-expression,
642 // then the expression is not present. This handles things like:
643 // "C ? throw : (void)42", which is crazy but legal.
644 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
645 case tok::semi:
646 case tok::r_paren:
647 case tok::r_square:
648 case tok::r_brace:
649 case tok::colon:
650 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000651 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000652
Chris Lattner2a2819a2008-04-06 06:02:23 +0000653 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000654 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000655 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000656 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000657 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000658}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000659
660/// ParseCXXThis - This handles the C++ 'this' pointer.
661///
662/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
663/// a non-lvalue expression whose value is the address of the object for which
664/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000665Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000666 assert(Tok.is(tok::kw_this) && "Not 'this'!");
667 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000668 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000669}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000670
671/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
672/// Can be interpreted either as function-style casting ("int(x)")
673/// or class type construction ("ClassType(x,y,z)")
674/// or creation of a value-initialized type ("int()").
675///
676/// postfix-expression: [C++ 5.2p1]
677/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
678/// typename-specifier '(' expression-list[opt] ')' [TODO]
679///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000680Parser::OwningExprResult
681Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000682 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000683 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000684
685 assert(Tok.is(tok::l_paren) && "Expected '('!");
686 SourceLocation LParenLoc = ConsumeParen();
687
Sebastian Redla55e52c2008-11-25 22:21:31 +0000688 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000689 CommaLocsTy CommaLocs;
690
691 if (Tok.isNot(tok::r_paren)) {
692 if (ParseExpressionList(Exprs, CommaLocs)) {
693 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000694 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000695 }
696 }
697
698 // Match the ')'.
699 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
700
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000701 // TypeRep could be null, if it references an invalid typedef.
702 if (!TypeRep)
703 return ExprError();
704
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000705 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
706 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000707 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
708 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000709 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000710}
711
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000712/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000713///
714/// condition:
715/// expression
716/// type-specifier-seq declarator '=' assignment-expression
717/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
718/// '=' assignment-expression
719///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000720/// \param ExprResult if the condition was parsed as an expression, the
721/// parsed expression.
722///
723/// \param DeclResult if the condition was parsed as a declaration, the
724/// parsed declaration.
725///
Douglas Gregor586596f2010-05-06 17:25:47 +0000726/// \param Loc The location of the start of the statement that requires this
727/// condition, e.g., the "for" in a for loop.
728///
729/// \param ConvertToBoolean Whether the condition expression should be
730/// converted to a boolean value.
731///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000732/// \returns true if there was a parsing, false otherwise.
733bool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000734 DeclPtrTy &DeclResult,
735 SourceLocation Loc,
736 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000737 if (Tok.is(tok::code_completion)) {
738 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +0000739 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000740 }
741
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000742 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000743 // Parse the expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000744 ExprResult = ParseExpression(); // expression
745 DeclResult = DeclPtrTy();
Douglas Gregor586596f2010-05-06 17:25:47 +0000746 if (ExprResult.isInvalid())
747 return true;
748
749 // If required, convert to a boolean value.
750 if (ConvertToBoolean)
751 ExprResult
752 = Actions.ActOnBooleanCondition(CurScope, Loc, move(ExprResult));
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000753 return ExprResult.isInvalid();
754 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000755
756 // type-specifier-seq
757 DeclSpec DS;
758 ParseSpecifierQualifierList(DS);
759
760 // declarator
761 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
762 ParseDeclarator(DeclaratorInfo);
763
764 // simple-asm-expr[opt]
765 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000766 SourceLocation Loc;
767 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000768 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000769 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000770 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000771 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000772 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000773 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000774 }
775
776 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000777 if (Tok.is(tok::kw___attribute)) {
778 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000779 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000780 DeclaratorInfo.AddAttributes(AttrList, Loc);
781 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000782
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000783 // Type-check the declaration itself.
784 Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope,
785 DeclaratorInfo);
786 DeclResult = Dcl.get();
787 ExprResult = ExprError();
788
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000789 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000790 if (Tok.is(tok::equal)) {
791 SourceLocation EqualLoc = ConsumeToken();
792 OwningExprResult AssignExpr(ParseAssignmentExpression());
793 if (!AssignExpr.isInvalid())
794 Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
795 } else {
796 // FIXME: C++0x allows a braced-init-list
797 Diag(Tok, diag::err_expected_equal_after_declarator);
798 }
799
Douglas Gregor586596f2010-05-06 17:25:47 +0000800 // FIXME: Build a reference to this declaration? Convert it to bool?
801 // (This is currently handled by Sema).
802
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000803 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000804}
805
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000806/// \brief Determine whether the current token starts a C++
807/// simple-type-specifier.
808bool Parser::isCXXSimpleTypeSpecifier() const {
809 switch (Tok.getKind()) {
810 case tok::annot_typename:
811 case tok::kw_short:
812 case tok::kw_long:
813 case tok::kw_signed:
814 case tok::kw_unsigned:
815 case tok::kw_void:
816 case tok::kw_char:
817 case tok::kw_int:
818 case tok::kw_float:
819 case tok::kw_double:
820 case tok::kw_wchar_t:
821 case tok::kw_char16_t:
822 case tok::kw_char32_t:
823 case tok::kw_bool:
824 // FIXME: C++0x decltype support.
825 // GNU typeof support.
826 case tok::kw_typeof:
827 return true;
828
829 default:
830 break;
831 }
832
833 return false;
834}
835
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000836/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
837/// This should only be called when the current token is known to be part of
838/// simple-type-specifier.
839///
840/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000841/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000842/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
843/// char
844/// wchar_t
845/// bool
846/// short
847/// int
848/// long
849/// signed
850/// unsigned
851/// float
852/// double
853/// void
854/// [GNU] typeof-specifier
855/// [C++0x] auto [TODO]
856///
857/// type-name:
858/// class-name
859/// enum-name
860/// typedef-name
861///
862void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
863 DS.SetRangeStart(Tok.getLocation());
864 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000865 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000866 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000868 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000869 case tok::identifier: // foo::bar
870 case tok::coloncolon: // ::foo::bar
871 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000872 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000873 assert(0 && "Not a simple-type-specifier token!");
874 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000875
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000876 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000877 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000878 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000879 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000880 break;
881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000883 // builtin types
884 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000885 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000886 break;
887 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000888 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000889 break;
890 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000891 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000892 break;
893 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000894 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000895 break;
896 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000897 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000898 break;
899 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000900 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000901 break;
902 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000903 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000904 break;
905 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000906 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000907 break;
908 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000909 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000910 break;
911 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000912 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000913 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000914 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000915 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000916 break;
917 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000918 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000919 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000920 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000921 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000922 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000924 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000925 // GNU typeof support.
926 case tok::kw_typeof:
927 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000928 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000929 return;
930 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000931 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000932 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
933 else
934 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000935 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000936 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000937}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000938
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000939/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
940/// [dcl.name]), which is a non-empty sequence of type-specifiers,
941/// e.g., "const short int". Note that the DeclSpec is *not* finished
942/// by parsing the type-specifier-seq, because these sequences are
943/// typically followed by some form of declarator. Returns true and
944/// emits diagnostics if this is not a type-specifier-seq, false
945/// otherwise.
946///
947/// type-specifier-seq: [C++ 8.1]
948/// type-specifier type-specifier-seq[opt]
949///
950bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
951 DS.SetRangeStart(Tok.getLocation());
952 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000953 unsigned DiagID;
954 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000955
956 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000957 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
958 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000959 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000960 return true;
961 }
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Sebastian Redld9bafa72010-02-03 21:21:43 +0000963 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
964 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
965 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000966
Douglas Gregor396a9f22010-02-24 23:13:13 +0000967 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000968 return false;
969}
970
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000971/// \brief Finish parsing a C++ unqualified-id that is a template-id of
972/// some form.
973///
974/// This routine is invoked when a '<' is encountered after an identifier or
975/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
976/// whether the unqualified-id is actually a template-id. This routine will
977/// then parse the template arguments and form the appropriate template-id to
978/// return to the caller.
979///
980/// \param SS the nested-name-specifier that precedes this template-id, if
981/// we're actually parsing a qualified-id.
982///
983/// \param Name for constructor and destructor names, this is the actual
984/// identifier that may be a template-name.
985///
986/// \param NameLoc the location of the class-name in a constructor or
987/// destructor.
988///
989/// \param EnteringContext whether we're entering the scope of the
990/// nested-name-specifier.
991///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000992/// \param ObjectType if this unqualified-id occurs within a member access
993/// expression, the type of the base object whose member is being accessed.
994///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000995/// \param Id as input, describes the template-name or operator-function-id
996/// that precedes the '<'. If template arguments were parsed successfully,
997/// will be updated with the template-id.
998///
Douglas Gregord4dca082010-02-24 18:44:31 +0000999/// \param AssumeTemplateId When true, this routine will assume that the name
1000/// refers to a template without performing name lookup to verify.
1001///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001002/// \returns true if a parse error occurred, false otherwise.
1003bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1004 IdentifierInfo *Name,
1005 SourceLocation NameLoc,
1006 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001007 TypeTy *ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001008 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001009 bool AssumeTemplateId,
1010 SourceLocation TemplateKWLoc) {
1011 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1012 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001013
1014 TemplateTy Template;
1015 TemplateNameKind TNK = TNK_Non_template;
1016 switch (Id.getKind()) {
1017 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001018 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001019 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001020 if (AssumeTemplateId) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00001021 TNK = Actions.ActOnDependentTemplateName(CurScope, TemplateKWLoc, SS,
1022 Id, ObjectType, EnteringContext,
1023 Template);
1024 if (TNK == TNK_Non_template)
1025 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001026 } else {
1027 bool MemberOfUnknownSpecialization;
Douglas Gregord4dca082010-02-24 18:44:31 +00001028 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001029 EnteringContext, Template,
1030 MemberOfUnknownSpecialization);
1031
1032 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1033 ObjectType && IsTemplateArgumentList()) {
1034 // We have something like t->getAs<T>(), where getAs is a
1035 // member of an unknown specialization. However, this will only
1036 // parse correctly as a template, so suggest the keyword 'template'
1037 // before 'getAs' and treat this as a dependent template name.
1038 std::string Name;
1039 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1040 Name = Id.Identifier->getName();
1041 else {
1042 Name = "operator ";
1043 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1044 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1045 else
1046 Name += Id.Identifier->getName();
1047 }
1048 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1049 << Name
1050 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregord6ab2322010-06-16 23:00:59 +00001051 TNK = Actions.ActOnDependentTemplateName(CurScope, TemplateKWLoc,
1052 SS, Id, ObjectType,
1053 EnteringContext, Template);
1054 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001055 return true;
1056 }
1057 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001058 break;
1059
Douglas Gregor014e88d2009-11-03 23:16:33 +00001060 case UnqualifiedId::IK_ConstructorName: {
1061 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001062 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001063 TemplateName.setIdentifier(Name, NameLoc);
1064 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001065 EnteringContext, Template,
1066 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001067 break;
1068 }
1069
Douglas Gregor014e88d2009-11-03 23:16:33 +00001070 case UnqualifiedId::IK_DestructorName: {
1071 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001072 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001073 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001074 if (ObjectType) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00001075 TNK = Actions.ActOnDependentTemplateName(CurScope, TemplateKWLoc, SS,
1076 TemplateName, ObjectType,
1077 EnteringContext, Template);
1078 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001079 return true;
1080 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001081 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001082 EnteringContext, Template,
1083 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001084
1085 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001086 Diag(NameLoc, diag::err_destructor_template_id)
1087 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001088 return true;
1089 }
1090 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001091 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001092 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001093
1094 default:
1095 return false;
1096 }
1097
1098 if (TNK == TNK_Non_template)
1099 return false;
1100
1101 // Parse the enclosed template argument list.
1102 SourceLocation LAngleLoc, RAngleLoc;
1103 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001104 if (Tok.is(tok::less) &&
1105 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001106 &SS, true, LAngleLoc,
1107 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001108 RAngleLoc))
1109 return true;
1110
1111 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001112 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1113 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001114 // Form a parsed representation of the template-id to be stored in the
1115 // UnqualifiedId.
1116 TemplateIdAnnotation *TemplateId
1117 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1118
1119 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1120 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001121 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001122 TemplateId->TemplateNameLoc = Id.StartLocation;
1123 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001124 TemplateId->Name = 0;
1125 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1126 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001127 }
1128
1129 TemplateId->Template = Template.getAs<void*>();
1130 TemplateId->Kind = TNK;
1131 TemplateId->LAngleLoc = LAngleLoc;
1132 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001133 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001134 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001135 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001136 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001137
1138 Id.setTemplateId(TemplateId);
1139 return false;
1140 }
1141
1142 // Bundle the template arguments together.
1143 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001144 TemplateArgs.size());
1145
1146 // Constructor and destructor names.
1147 Action::TypeResult Type
1148 = Actions.ActOnTemplateIdType(Template, NameLoc,
1149 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001150 RAngleLoc);
1151 if (Type.isInvalid())
1152 return true;
1153
1154 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1155 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1156 else
1157 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1158
1159 return false;
1160}
1161
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001162/// \brief Parse an operator-function-id or conversion-function-id as part
1163/// of a C++ unqualified-id.
1164///
1165/// This routine is responsible only for parsing the operator-function-id or
1166/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001167///
1168/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001169/// operator-function-id: [C++ 13.5]
1170/// 'operator' operator
1171///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001172/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001173/// new delete new[] delete[]
1174/// + - * / % ^ & | ~
1175/// ! = < > += -= *= /= %=
1176/// ^= &= |= << >> >>= <<= == !=
1177/// <= >= && || ++ -- , ->* ->
1178/// () []
1179///
1180/// conversion-function-id: [C++ 12.3.2]
1181/// operator conversion-type-id
1182///
1183/// conversion-type-id:
1184/// type-specifier-seq conversion-declarator[opt]
1185///
1186/// conversion-declarator:
1187/// ptr-operator conversion-declarator[opt]
1188/// \endcode
1189///
1190/// \param The nested-name-specifier that preceded this unqualified-id. If
1191/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1192///
1193/// \param EnteringContext whether we are entering the scope of the
1194/// nested-name-specifier.
1195///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001196/// \param ObjectType if this unqualified-id occurs within a member access
1197/// expression, the type of the base object whose member is being accessed.
1198///
1199/// \param Result on a successful parse, contains the parsed unqualified-id.
1200///
1201/// \returns true if parsing fails, false otherwise.
1202bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1203 TypeTy *ObjectType,
1204 UnqualifiedId &Result) {
1205 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1206
1207 // Consume the 'operator' keyword.
1208 SourceLocation KeywordLoc = ConsumeToken();
1209
1210 // Determine what kind of operator name we have.
1211 unsigned SymbolIdx = 0;
1212 SourceLocation SymbolLocations[3];
1213 OverloadedOperatorKind Op = OO_None;
1214 switch (Tok.getKind()) {
1215 case tok::kw_new:
1216 case tok::kw_delete: {
1217 bool isNew = Tok.getKind() == tok::kw_new;
1218 // Consume the 'new' or 'delete'.
1219 SymbolLocations[SymbolIdx++] = ConsumeToken();
1220 if (Tok.is(tok::l_square)) {
1221 // Consume the '['.
1222 SourceLocation LBracketLoc = ConsumeBracket();
1223 // Consume the ']'.
1224 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1225 LBracketLoc);
1226 if (RBracketLoc.isInvalid())
1227 return true;
1228
1229 SymbolLocations[SymbolIdx++] = LBracketLoc;
1230 SymbolLocations[SymbolIdx++] = RBracketLoc;
1231 Op = isNew? OO_Array_New : OO_Array_Delete;
1232 } else {
1233 Op = isNew? OO_New : OO_Delete;
1234 }
1235 break;
1236 }
1237
1238#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1239 case tok::Token: \
1240 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1241 Op = OO_##Name; \
1242 break;
1243#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1244#include "clang/Basic/OperatorKinds.def"
1245
1246 case tok::l_paren: {
1247 // Consume the '('.
1248 SourceLocation LParenLoc = ConsumeParen();
1249 // Consume the ')'.
1250 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1251 LParenLoc);
1252 if (RParenLoc.isInvalid())
1253 return true;
1254
1255 SymbolLocations[SymbolIdx++] = LParenLoc;
1256 SymbolLocations[SymbolIdx++] = RParenLoc;
1257 Op = OO_Call;
1258 break;
1259 }
1260
1261 case tok::l_square: {
1262 // Consume the '['.
1263 SourceLocation LBracketLoc = ConsumeBracket();
1264 // Consume the ']'.
1265 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1266 LBracketLoc);
1267 if (RBracketLoc.isInvalid())
1268 return true;
1269
1270 SymbolLocations[SymbolIdx++] = LBracketLoc;
1271 SymbolLocations[SymbolIdx++] = RBracketLoc;
1272 Op = OO_Subscript;
1273 break;
1274 }
1275
1276 case tok::code_completion: {
1277 // Code completion for the operator name.
1278 Actions.CodeCompleteOperatorName(CurScope);
1279
1280 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001281 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001282
1283 // Don't try to parse any further.
1284 return true;
1285 }
1286
1287 default:
1288 break;
1289 }
1290
1291 if (Op != OO_None) {
1292 // We have parsed an operator-function-id.
1293 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1294 return false;
1295 }
Sean Hunt0486d742009-11-28 04:44:28 +00001296
1297 // Parse a literal-operator-id.
1298 //
1299 // literal-operator-id: [C++0x 13.5.8]
1300 // operator "" identifier
1301
1302 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1303 if (Tok.getLength() != 2)
1304 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1305 ConsumeStringToken();
1306
1307 if (Tok.isNot(tok::identifier)) {
1308 Diag(Tok.getLocation(), diag::err_expected_ident);
1309 return true;
1310 }
1311
1312 IdentifierInfo *II = Tok.getIdentifierInfo();
1313 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001314 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001315 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001316
1317 // Parse a conversion-function-id.
1318 //
1319 // conversion-function-id: [C++ 12.3.2]
1320 // operator conversion-type-id
1321 //
1322 // conversion-type-id:
1323 // type-specifier-seq conversion-declarator[opt]
1324 //
1325 // conversion-declarator:
1326 // ptr-operator conversion-declarator[opt]
1327
1328 // Parse the type-specifier-seq.
1329 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001330 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001331 return true;
1332
1333 // Parse the conversion-declarator, which is merely a sequence of
1334 // ptr-operators.
1335 Declarator D(DS, Declarator::TypeNameContext);
1336 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1337
1338 // Finish up the type.
1339 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1340 if (Ty.isInvalid())
1341 return true;
1342
1343 // Note that this is a conversion-function-id.
1344 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1345 D.getSourceRange().getEnd());
1346 return false;
1347}
1348
1349/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1350/// name of an entity.
1351///
1352/// \code
1353/// unqualified-id: [C++ expr.prim.general]
1354/// identifier
1355/// operator-function-id
1356/// conversion-function-id
1357/// [C++0x] literal-operator-id [TODO]
1358/// ~ class-name
1359/// template-id
1360///
1361/// \endcode
1362///
1363/// \param The nested-name-specifier that preceded this unqualified-id. If
1364/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1365///
1366/// \param EnteringContext whether we are entering the scope of the
1367/// nested-name-specifier.
1368///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001369/// \param AllowDestructorName whether we allow parsing of a destructor name.
1370///
1371/// \param AllowConstructorName whether we allow parsing a constructor name.
1372///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001373/// \param ObjectType if this unqualified-id occurs within a member access
1374/// expression, the type of the base object whose member is being accessed.
1375///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001376/// \param Result on a successful parse, contains the parsed unqualified-id.
1377///
1378/// \returns true if parsing fails, false otherwise.
1379bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1380 bool AllowDestructorName,
1381 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001382 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001383 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001384
1385 // Handle 'A::template B'. This is for template-ids which have not
1386 // already been annotated by ParseOptionalCXXScopeSpecifier().
1387 bool TemplateSpecified = false;
1388 SourceLocation TemplateKWLoc;
1389 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1390 (ObjectType || SS.isSet())) {
1391 TemplateSpecified = true;
1392 TemplateKWLoc = ConsumeToken();
1393 }
1394
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001395 // unqualified-id:
1396 // identifier
1397 // template-id (when it hasn't already been annotated)
1398 if (Tok.is(tok::identifier)) {
1399 // Consume the identifier.
1400 IdentifierInfo *Id = Tok.getIdentifierInfo();
1401 SourceLocation IdLoc = ConsumeToken();
1402
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001403 if (!getLang().CPlusPlus) {
1404 // If we're not in C++, only identifiers matter. Record the
1405 // identifier and return.
1406 Result.setIdentifier(Id, IdLoc);
1407 return false;
1408 }
1409
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001410 if (AllowConstructorName &&
1411 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1412 // We have parsed a constructor name.
1413 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1414 &SS, false),
1415 IdLoc, IdLoc);
1416 } else {
1417 // We have parsed an identifier.
1418 Result.setIdentifier(Id, IdLoc);
1419 }
1420
1421 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001422 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001423 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001424 ObjectType, Result,
1425 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001426
1427 return false;
1428 }
1429
1430 // unqualified-id:
1431 // template-id (already parsed and annotated)
1432 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001433 TemplateIdAnnotation *TemplateId
1434 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1435
1436 // If the template-name names the current class, then this is a constructor
1437 if (AllowConstructorName && TemplateId->Name &&
1438 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
1439 if (SS.isSet()) {
1440 // C++ [class.qual]p2 specifies that a qualified template-name
1441 // is taken as the constructor name where a constructor can be
1442 // declared. Thus, the template arguments are extraneous, so
1443 // complain about them and remove them entirely.
1444 Diag(TemplateId->TemplateNameLoc,
1445 diag::err_out_of_line_constructor_template_id)
1446 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001447 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001448 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1449 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1450 TemplateId->TemplateNameLoc,
1451 CurScope,
1452 &SS, false),
1453 TemplateId->TemplateNameLoc,
1454 TemplateId->RAngleLoc);
1455 TemplateId->Destroy();
1456 ConsumeToken();
1457 return false;
1458 }
1459
1460 Result.setConstructorTemplateId(TemplateId);
1461 ConsumeToken();
1462 return false;
1463 }
1464
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001465 // We have already parsed a template-id; consume the annotation token as
1466 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001467 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001468 ConsumeToken();
1469 return false;
1470 }
1471
1472 // unqualified-id:
1473 // operator-function-id
1474 // conversion-function-id
1475 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001476 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001477 return true;
1478
Sean Hunte6252d12009-11-28 08:58:14 +00001479 // If we have an operator-function-id or a literal-operator-id and the next
1480 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001481 //
1482 // template-id:
1483 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001484 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1485 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001486 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001487 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1488 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001489 Result,
1490 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001491
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001492 return false;
1493 }
1494
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001495 if (getLang().CPlusPlus &&
1496 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001497 // C++ [expr.unary.op]p10:
1498 // There is an ambiguity in the unary-expression ~X(), where X is a
1499 // class-name. The ambiguity is resolved in favor of treating ~ as a
1500 // unary complement rather than treating ~X as referring to a destructor.
1501
1502 // Parse the '~'.
1503 SourceLocation TildeLoc = ConsumeToken();
1504
1505 // Parse the class-name.
1506 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001507 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001508 return true;
1509 }
1510
1511 // Parse the class-name (or template-name in a simple-template-id).
1512 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1513 SourceLocation ClassNameLoc = ConsumeToken();
1514
Douglas Gregor0278e122010-05-05 05:58:24 +00001515 if (TemplateSpecified || Tok.is(tok::less)) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001516 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1517 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001518 EnteringContext, ObjectType, Result,
1519 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001520 }
1521
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001522 // Note that this is a destructor name.
Douglas Gregor124b8782010-02-16 19:09:40 +00001523 Action::TypeTy *Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1524 ClassNameLoc, CurScope,
1525 SS, ObjectType,
1526 EnteringContext);
1527 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001528 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001529
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001530 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001531 return false;
1532 }
1533
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001534 Diag(Tok, diag::err_expected_unqualified_id)
1535 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001536 return true;
1537}
1538
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001539/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1540/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001541///
Chris Lattner59232d32009-01-04 21:25:24 +00001542/// This method is called to parse the new expression after the optional :: has
1543/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1544/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001545///
1546/// new-expression:
1547/// '::'[opt] 'new' new-placement[opt] new-type-id
1548/// new-initializer[opt]
1549/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1550/// new-initializer[opt]
1551///
1552/// new-placement:
1553/// '(' expression-list ')'
1554///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001555/// new-type-id:
1556/// type-specifier-seq new-declarator[opt]
1557///
1558/// new-declarator:
1559/// ptr-operator new-declarator[opt]
1560/// direct-new-declarator
1561///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001562/// new-initializer:
1563/// '(' expression-list[opt] ')'
1564/// [C++0x] braced-init-list [TODO]
1565///
Chris Lattner59232d32009-01-04 21:25:24 +00001566Parser::OwningExprResult
1567Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1568 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1569 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001570
1571 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1572 // second form of new-expression. It can't be a new-type-id.
1573
Sebastian Redla55e52c2008-11-25 22:21:31 +00001574 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001575 SourceLocation PlacementLParen, PlacementRParen;
1576
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001577 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001578 DeclSpec DS;
1579 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001580 if (Tok.is(tok::l_paren)) {
1581 // If it turns out to be a placement, we change the type location.
1582 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001583 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1584 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001585 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001586 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001587
1588 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001589 if (PlacementRParen.isInvalid()) {
1590 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001591 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001592 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001593
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001594 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001595 // Reset the placement locations. There was no placement.
1596 PlacementLParen = PlacementRParen = SourceLocation();
1597 ParenTypeId = true;
1598 } else {
1599 // We still need the type.
1600 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001601 SourceLocation LParen = ConsumeParen();
1602 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001603 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001604 ParseDeclarator(DeclaratorInfo);
1605 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001606 ParenTypeId = true;
1607 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001608 if (ParseCXXTypeSpecifierSeq(DS))
1609 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001610 else {
1611 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001612 ParseDeclaratorInternal(DeclaratorInfo,
1613 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001614 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001615 ParenTypeId = false;
1616 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001617 }
1618 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001619 // A new-type-id is a simplified type-id, where essentially the
1620 // direct-declarator is replaced by a direct-new-declarator.
1621 if (ParseCXXTypeSpecifierSeq(DS))
1622 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001623 else {
1624 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001625 ParseDeclaratorInternal(DeclaratorInfo,
1626 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001627 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001628 ParenTypeId = false;
1629 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001630 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001631 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001632 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001633 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001634
Sebastian Redla55e52c2008-11-25 22:21:31 +00001635 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001636 SourceLocation ConstructorLParen, ConstructorRParen;
1637
1638 if (Tok.is(tok::l_paren)) {
1639 ConstructorLParen = ConsumeParen();
1640 if (Tok.isNot(tok::r_paren)) {
1641 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001642 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1643 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001644 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001645 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001646 }
1647 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001648 if (ConstructorRParen.isInvalid()) {
1649 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001650 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001651 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001652 }
1653
Sebastian Redlf53597f2009-03-15 17:47:39 +00001654 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1655 move_arg(PlacementArgs), PlacementRParen,
1656 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1657 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001658}
1659
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001660/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1661/// passed to ParseDeclaratorInternal.
1662///
1663/// direct-new-declarator:
1664/// '[' expression ']'
1665/// direct-new-declarator '[' constant-expression ']'
1666///
Chris Lattner59232d32009-01-04 21:25:24 +00001667void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001668 // Parse the array dimensions.
1669 bool first = true;
1670 while (Tok.is(tok::l_square)) {
1671 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001672 OwningExprResult Size(first ? ParseExpression()
1673 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001674 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001675 // Recover
1676 SkipUntil(tok::r_square);
1677 return;
1678 }
1679 first = false;
1680
Sebastian Redlab197ba2009-02-09 18:23:29 +00001681 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001682 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001683 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001684 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001685
Sebastian Redlab197ba2009-02-09 18:23:29 +00001686 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001687 return;
1688 }
1689}
1690
1691/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1692/// This ambiguity appears in the syntax of the C++ new operator.
1693///
1694/// new-expression:
1695/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1696/// new-initializer[opt]
1697///
1698/// new-placement:
1699/// '(' expression-list ')'
1700///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001701bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001702 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001703 // The '(' was already consumed.
1704 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001705 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001706 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001707 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001708 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001709 }
1710
1711 // It's not a type, it has to be an expression list.
1712 // Discard the comma locations - ActOnCXXNew has enough parameters.
1713 CommaLocsTy CommaLocs;
1714 return ParseExpressionList(PlacementArgs, CommaLocs);
1715}
1716
1717/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1718/// to free memory allocated by new.
1719///
Chris Lattner59232d32009-01-04 21:25:24 +00001720/// This method is called to parse the 'delete' expression after the optional
1721/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1722/// and "Start" is its location. Otherwise, "Start" is the location of the
1723/// 'delete' token.
1724///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001725/// delete-expression:
1726/// '::'[opt] 'delete' cast-expression
1727/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001728Parser::OwningExprResult
1729Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1730 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1731 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001732
1733 // Array delete?
1734 bool ArrayDelete = false;
1735 if (Tok.is(tok::l_square)) {
1736 ArrayDelete = true;
1737 SourceLocation LHS = ConsumeBracket();
1738 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1739 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001740 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001741 }
1742
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001743 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001744 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001745 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001746
Sebastian Redlf53597f2009-03-15 17:47:39 +00001747 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001748}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001749
Mike Stump1eb44332009-09-09 15:08:12 +00001750static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001751 switch(kind) {
1752 default: assert(false && "Not a known unary type trait.");
1753 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1754 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1755 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1756 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1757 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1758 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1759 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1760 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1761 case tok::kw___is_abstract: return UTT_IsAbstract;
1762 case tok::kw___is_class: return UTT_IsClass;
1763 case tok::kw___is_empty: return UTT_IsEmpty;
1764 case tok::kw___is_enum: return UTT_IsEnum;
1765 case tok::kw___is_pod: return UTT_IsPOD;
1766 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1767 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001768 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001769 }
1770}
1771
1772/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1773/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1774/// templates.
1775///
1776/// primary-expression:
1777/// [GNU] unary-type-trait '(' type-id ')'
1778///
Mike Stump1eb44332009-09-09 15:08:12 +00001779Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001780 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1781 SourceLocation Loc = ConsumeToken();
1782
1783 SourceLocation LParen = Tok.getLocation();
1784 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1785 return ExprError();
1786
1787 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1788 // there will be cryptic errors about mismatched parentheses and missing
1789 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001790 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001791
1792 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1793
Douglas Gregor809070a2009-02-18 17:45:20 +00001794 if (Ty.isInvalid())
1795 return ExprError();
1796
1797 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001798}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001799
1800/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1801/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1802/// based on the context past the parens.
1803Parser::OwningExprResult
1804Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1805 TypeTy *&CastTy,
1806 SourceLocation LParenLoc,
1807 SourceLocation &RParenLoc) {
1808 assert(getLang().CPlusPlus && "Should only be called for C++!");
1809 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1810 assert(isTypeIdInParens() && "Not a type-id!");
1811
1812 OwningExprResult Result(Actions, true);
1813 CastTy = 0;
1814
1815 // We need to disambiguate a very ugly part of the C++ syntax:
1816 //
1817 // (T())x; - type-id
1818 // (T())*x; - type-id
1819 // (T())/x; - expression
1820 // (T()); - expression
1821 //
1822 // The bad news is that we cannot use the specialized tentative parser, since
1823 // it can only verify that the thing inside the parens can be parsed as
1824 // type-id, it is not useful for determining the context past the parens.
1825 //
1826 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001827 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001828 //
1829 // It uses a scheme similar to parsing inline methods. The parenthesized
1830 // tokens are cached, the context that follows is determined (possibly by
1831 // parsing a cast-expression), and then we re-introduce the cached tokens
1832 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001833
Mike Stump1eb44332009-09-09 15:08:12 +00001834 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001835 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001836
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001837 // Store the tokens of the parentheses. We will parse them after we determine
1838 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001839 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001840 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001841 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1842 return ExprError();
1843 }
1844
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001845 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001846 ParseAs = CompoundLiteral;
1847 } else {
1848 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001849 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1850 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1851 NotCastExpr = true;
1852 } else {
1853 // Try parsing the cast-expression that may follow.
1854 // If it is not a cast-expression, NotCastExpr will be true and no token
1855 // will be consumed.
1856 Result = ParseCastExpression(false/*isUnaryExpression*/,
1857 false/*isAddressofOperand*/,
Daniel Dunbarc0012d62010-06-02 15:47:03 +00001858 NotCastExpr, 0/*TypeOfCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001859 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001860
1861 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1862 // an expression.
1863 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001864 }
1865
Mike Stump1eb44332009-09-09 15:08:12 +00001866 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001867 Toks.push_back(Tok);
1868 // Re-enter the stored parenthesized tokens into the token stream, so we may
1869 // parse them now.
1870 PP.EnterTokenStream(Toks.data(), Toks.size(),
1871 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1872 // Drop the current token and bring the first cached one. It's the same token
1873 // as when we entered this function.
1874 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001875
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001876 if (ParseAs >= CompoundLiteral) {
1877 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001878
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001879 // Match the ')'.
1880 if (Tok.is(tok::r_paren))
1881 RParenLoc = ConsumeParen();
1882 else
1883 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001884
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001885 if (ParseAs == CompoundLiteral) {
1886 ExprType = CompoundLiteral;
1887 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1888 }
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001890 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1891 assert(ParseAs == CastExpr);
1892
1893 if (Ty.isInvalid())
1894 return ExprError();
1895
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001896 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001897
1898 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001899 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001900 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001901 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001902 return move(Result);
1903 }
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001905 // Not a compound literal, and not followed by a cast-expression.
1906 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001907
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001908 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001909 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001910 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1911 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1912
1913 // Match the ')'.
1914 if (Result.isInvalid()) {
1915 SkipUntil(tok::r_paren);
1916 return ExprError();
1917 }
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001919 if (Tok.is(tok::r_paren))
1920 RParenLoc = ConsumeParen();
1921 else
1922 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1923
1924 return move(Result);
1925}