blob: 0a909f626ffec7b077a54c75d72a18032d8d9f29 [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);
113 ConsumeToken();
114 }
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();
Mike Stump1eb44332009-09-09 15:08:12 +0000167 TemplateTy Template
Douglas Gregor014e88d2009-11-03 23:16:33 +0000168 = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
Douglas Gregora481edb2009-11-20 23:39:24 +0000169 ObjectType, EnteringContext);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000170 if (!Template)
John McCall9ba61662010-02-26 08:45:28 +0000171 return true;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000172 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000173 &SS, TemplateName, TemplateKWLoc, false))
John McCall9ba61662010-02-26 08:45:28 +0000174 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Chris Lattner77cf72a2009-06-26 03:47:46 +0000176 continue;
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Douglas Gregor39a8de12009-02-25 19:37:18 +0000179 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000180 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000181 //
182 // simple-template-id '::'
183 //
184 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000185 // right kind (it should name a type or be dependent), and then
186 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000187 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000188 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord4dca082010-02-24 18:44:31 +0000189 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
190 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000191 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000192 }
193
Mike Stump1eb44332009-09-09 15:08:12 +0000194 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000195 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000196 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000197
Mike Stump1eb44332009-09-09 15:08:12 +0000198 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000199 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000200 Token TypeToken = Tok;
201 ConsumeToken();
202 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
203 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Douglas Gregor39a8de12009-02-25 19:37:18 +0000205 if (!HasScopeSpecifier) {
206 SS.setBeginLoc(TypeToken.getLocation());
207 HasScopeSpecifier = true;
208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Douglas Gregor31a19b62009-04-01 21:51:26 +0000210 if (TypeToken.getAnnotationValue())
211 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000212 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000213 TypeToken.getAnnotationValue(),
214 TypeToken.getAnnotationRange(),
Douglas Gregoredc90502010-02-25 04:46:04 +0000215 CCLoc));
Douglas Gregor31a19b62009-04-01 21:51:26 +0000216 else
217 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000218 SS.setEndLoc(CCLoc);
219 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000220 }
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Chris Lattner67b9e832009-06-26 03:45:46 +0000222 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000223 }
224
Chris Lattner5c7f7862009-06-26 03:52:38 +0000225
226 // The rest of the nested-name-specifier possibilities start with
227 // tok::identifier.
228 if (Tok.isNot(tok::identifier))
229 break;
230
231 IdentifierInfo &II = *Tok.getIdentifierInfo();
232
233 // nested-name-specifier:
234 // type-name '::'
235 // namespace-name '::'
236 // nested-name-specifier identifier '::'
237 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000238
239 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
240 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000241 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregoredc90502010-02-25 04:46:04 +0000242 if (Actions.IsInvalidUnlessNestedName(CurScope, SS, II, ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000243 EnteringContext) &&
244 // If the token after the colon isn't an identifier, it's still an
245 // error, but they probably meant something else strange so don't
246 // recover like this.
247 PP.LookAhead(1).is(tok::identifier)) {
248 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000249 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000250
251 // Recover as if the user wrote '::'.
252 Next.setKind(tok::coloncolon);
253 }
Chris Lattner46646492009-12-07 01:36:53 +0000254 }
255
Chris Lattner5c7f7862009-06-26 03:52:38 +0000256 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000257 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
258 !Actions.isNonTypeNestedNameSpecifier(CurScope, SS, Tok.getLocation(),
259 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000260 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000261 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000262 }
263
Chris Lattner5c7f7862009-06-26 03:52:38 +0000264 // We have an identifier followed by a '::'. Lookup this name
265 // as the name in a nested-name-specifier.
266 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000267 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
268 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000269 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattner5c7f7862009-06-26 03:52:38 +0000271 if (!HasScopeSpecifier) {
272 SS.setBeginLoc(IdLoc);
273 HasScopeSpecifier = true;
274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattner5c7f7862009-06-26 03:52:38 +0000276 if (SS.isInvalid())
277 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Chris Lattner5c7f7862009-06-26 03:52:38 +0000279 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000280 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregoredc90502010-02-25 04:46:04 +0000281 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000282 SS.setEndLoc(CCLoc);
283 continue;
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner5c7f7862009-06-26 03:52:38 +0000286 // nested-name-specifier:
287 // type-name '<'
288 if (Next.is(tok::less)) {
289 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000290 UnqualifiedId TemplateName;
291 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000292 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000293 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
294 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000295 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000296 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000297 Template,
298 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000299 // We have found a template name, so annotate this this token
300 // with a template-id annotation. We do not permit the
301 // template-id to be translated into a type annotation,
302 // because some clients (e.g., the parsing of class template
303 // specializations) still want to see the original template-id
304 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000305 ConsumeToken();
306 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
307 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000308 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000309 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000310 }
311
312 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
313 IsTemplateArgumentList(1)) {
314 // We have something like t::getAs<T>, where getAs is a
315 // member of an unknown specialization. However, this will only
316 // parse correctly as a template, so suggest the keyword 'template'
317 // before 'getAs' and treat this as a dependent template name.
318 Diag(Tok.getLocation(), diag::err_missing_dependent_template_keyword)
319 << II.getName()
320 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
321
322 Template = Actions.ActOnDependentTemplateName(Tok.getLocation(), SS,
323 TemplateName, ObjectType,
324 EnteringContext);
325 if (!Template.get())
326 return true;
327
328 // Consume the identifier.
329 ConsumeToken();
330 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name, &SS,
331 TemplateName, SourceLocation(), false))
332 return true;
333
334 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000335 }
336 }
337
Douglas Gregor39a8de12009-02-25 19:37:18 +0000338 // We don't have any tokens that form the beginning of a
339 // nested-name-specifier, so we're done.
340 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Douglas Gregord4dca082010-02-24 18:44:31 +0000343 // Even if we didn't see any pieces of a nested-name-specifier, we
344 // still check whether there is a tilde in this position, which
345 // indicates a potential pseudo-destructor.
346 if (CheckForDestructor && Tok.is(tok::tilde))
347 *MayBePseudoDestructor = true;
348
John McCall9ba61662010-02-26 08:45:28 +0000349 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000350}
351
352/// ParseCXXIdExpression - Handle id-expression.
353///
354/// id-expression:
355/// unqualified-id
356/// qualified-id
357///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000358/// qualified-id:
359/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
360/// '::' identifier
361/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000362/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000363///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000364/// NOTE: The standard specifies that, for qualified-id, the parser does not
365/// expect:
366///
367/// '::' conversion-function-id
368/// '::' '~' class-name
369///
370/// This may cause a slight inconsistency on diagnostics:
371///
372/// class C {};
373/// namespace A {}
374/// void f() {
375/// :: A :: ~ C(); // Some Sema error about using destructor with a
376/// // namespace.
377/// :: ~ C(); // Some Parser error like 'unexpected ~'.
378/// }
379///
380/// We simplify the parser a bit and make it work like:
381///
382/// qualified-id:
383/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
384/// '::' unqualified-id
385///
386/// That way Sema can handle and report similar errors for namespaces and the
387/// global scope.
388///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000389/// The isAddressOfOperand parameter indicates that this id-expression is a
390/// direct operand of the address-of operator. This is, besides member contexts,
391/// the only place where a qualified-id naming a non-static class member may
392/// appear.
393///
394Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000395 // qualified-id:
396 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
397 // '::' unqualified-id
398 //
399 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000400 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000401
402 UnqualifiedId Name;
403 if (ParseUnqualifiedId(SS,
404 /*EnteringContext=*/false,
405 /*AllowDestructorName=*/false,
406 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000407 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000408 Name))
409 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000410
411 // This is only the direct operand of an & operator if it is not
412 // followed by a postfix-expression suffix.
413 if (isAddressOfOperand) {
414 switch (Tok.getKind()) {
415 case tok::l_square:
416 case tok::l_paren:
417 case tok::arrow:
418 case tok::period:
419 case tok::plusplus:
420 case tok::minusminus:
421 isAddressOfOperand = false;
422 break;
423
424 default:
425 break;
426 }
427 }
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000428
429 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
430 isAddressOfOperand);
431
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000432}
433
Reid Spencer5f016e22007-07-11 17:01:13 +0000434/// ParseCXXCasts - This handles the various ways to cast expressions to another
435/// type.
436///
437/// postfix-expression: [C++ 5.2p1]
438/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
439/// 'static_cast' '<' type-name '>' '(' expression ')'
440/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
441/// 'const_cast' '<' type-name '>' '(' expression ')'
442///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000443Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000444 tok::TokenKind Kind = Tok.getKind();
445 const char *CastName = 0; // For error messages
446
447 switch (Kind) {
448 default: assert(0 && "Unknown C++ cast!"); abort();
449 case tok::kw_const_cast: CastName = "const_cast"; break;
450 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
451 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
452 case tok::kw_static_cast: CastName = "static_cast"; break;
453 }
454
455 SourceLocation OpLoc = ConsumeToken();
456 SourceLocation LAngleBracketLoc = Tok.getLocation();
457
458 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000459 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000460
Douglas Gregor809070a2009-02-18 17:45:20 +0000461 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 SourceLocation RAngleBracketLoc = Tok.getLocation();
463
Chris Lattner1ab3b962008-11-18 07:48:38 +0000464 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000465 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000466
467 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
468
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000469 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
470 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000471
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000472 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000474 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000475 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000476
Douglas Gregor809070a2009-02-18 17:45:20 +0000477 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000478 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000479 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000480 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000481 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000482
Sebastian Redl20df9b72008-12-11 22:51:44 +0000483 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000484}
485
Sebastian Redlc42e1182008-11-11 11:37:55 +0000486/// ParseCXXTypeid - This handles the C++ typeid expression.
487///
488/// postfix-expression: [C++ 5.2p1]
489/// 'typeid' '(' expression ')'
490/// 'typeid' '(' type-id ')'
491///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000492Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000493 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
494
495 SourceLocation OpLoc = ConsumeToken();
496 SourceLocation LParenLoc = Tok.getLocation();
497 SourceLocation RParenLoc;
498
499 // typeid expressions are always parenthesized.
500 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
501 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000502 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000503
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000504 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000505
506 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000507 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000508
509 // Match the ')'.
510 MatchRHSPunctuation(tok::r_paren, LParenLoc);
511
Douglas Gregor809070a2009-02-18 17:45:20 +0000512 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000513 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000514
515 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000516 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000517 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000518 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000519 // When typeid is applied to an expression other than an lvalue of a
520 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000521 // operand (Clause 5).
522 //
Mike Stump1eb44332009-09-09 15:08:12 +0000523 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000524 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000525 // we the expression is potentially potentially evaluated.
526 EnterExpressionEvaluationContext Unevaluated(Actions,
527 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000528 Result = ParseExpression();
529
530 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000531 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000532 SkipUntil(tok::r_paren);
533 else {
534 MatchRHSPunctuation(tok::r_paren, LParenLoc);
535
536 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000537 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000538 }
539 }
540
Sebastian Redl20df9b72008-12-11 22:51:44 +0000541 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000542}
543
Douglas Gregord4dca082010-02-24 18:44:31 +0000544/// \brief Parse a C++ pseudo-destructor expression after the base,
545/// . or -> operator, and nested-name-specifier have already been
546/// parsed.
547///
548/// postfix-expression: [C++ 5.2]
549/// postfix-expression . pseudo-destructor-name
550/// postfix-expression -> pseudo-destructor-name
551///
552/// pseudo-destructor-name:
553/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
554/// ::[opt] nested-name-specifier template simple-template-id ::
555/// ~type-name
556/// ::[opt] nested-name-specifier[opt] ~type-name
557///
558Parser::OwningExprResult
559Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
560 tok::TokenKind OpKind,
561 CXXScopeSpec &SS,
562 Action::TypeTy *ObjectType) {
563 // We're parsing either a pseudo-destructor-name or a dependent
564 // member access that has the same form as a
565 // pseudo-destructor-name. We parse both in the same way and let
566 // the action model sort them out.
567 //
568 // Note that the ::[opt] nested-name-specifier[opt] has already
569 // been parsed, and if there was a simple-template-id, it has
570 // been coalesced into a template-id annotation token.
571 UnqualifiedId FirstTypeName;
572 SourceLocation CCLoc;
573 if (Tok.is(tok::identifier)) {
574 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
575 ConsumeToken();
576 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
577 CCLoc = ConsumeToken();
578 } else if (Tok.is(tok::annot_template_id)) {
579 FirstTypeName.setTemplateId(
580 (TemplateIdAnnotation *)Tok.getAnnotationValue());
581 ConsumeToken();
582 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
583 CCLoc = ConsumeToken();
584 } else {
585 FirstTypeName.setIdentifier(0, SourceLocation());
586 }
587
588 // Parse the tilde.
589 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
590 SourceLocation TildeLoc = ConsumeToken();
591 if (!Tok.is(tok::identifier)) {
592 Diag(Tok, diag::err_destructor_tilde_identifier);
593 return ExprError();
594 }
595
596 // Parse the second type.
597 UnqualifiedId SecondTypeName;
598 IdentifierInfo *Name = Tok.getIdentifierInfo();
599 SourceLocation NameLoc = ConsumeToken();
600 SecondTypeName.setIdentifier(Name, NameLoc);
601
602 // If there is a '<', the second type name is a template-id. Parse
603 // it as such.
604 if (Tok.is(tok::less) &&
605 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000606 SecondTypeName, /*AssumeTemplateName=*/true,
607 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000608 return ExprError();
609
610 return Actions.ActOnPseudoDestructorExpr(CurScope, move(Base), OpLoc, OpKind,
611 SS, FirstTypeName, CCLoc,
612 TildeLoc, SecondTypeName,
613 Tok.is(tok::l_paren));
614}
615
Reid Spencer5f016e22007-07-11 17:01:13 +0000616/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
617///
618/// boolean-literal: [C++ 2.13.5]
619/// 'true'
620/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000621Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000623 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000624}
Chris Lattner50dd2892008-02-26 00:51:44 +0000625
626/// ParseThrowExpression - This handles the C++ throw expression.
627///
628/// throw-expression: [C++ 15]
629/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000630Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000631 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000632 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000633
Chris Lattner2a2819a2008-04-06 06:02:23 +0000634 // If the current token isn't the start of an assignment-expression,
635 // then the expression is not present. This handles things like:
636 // "C ? throw : (void)42", which is crazy but legal.
637 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
638 case tok::semi:
639 case tok::r_paren:
640 case tok::r_square:
641 case tok::r_brace:
642 case tok::colon:
643 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000644 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000645
Chris Lattner2a2819a2008-04-06 06:02:23 +0000646 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000647 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000648 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000649 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000650 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000651}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000652
653/// ParseCXXThis - This handles the C++ 'this' pointer.
654///
655/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
656/// a non-lvalue expression whose value is the address of the object for which
657/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000658Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000659 assert(Tok.is(tok::kw_this) && "Not 'this'!");
660 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000661 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000662}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000663
664/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
665/// Can be interpreted either as function-style casting ("int(x)")
666/// or class type construction ("ClassType(x,y,z)")
667/// or creation of a value-initialized type ("int()").
668///
669/// postfix-expression: [C++ 5.2p1]
670/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
671/// typename-specifier '(' expression-list[opt] ')' [TODO]
672///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000673Parser::OwningExprResult
674Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000675 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000676 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000677
678 assert(Tok.is(tok::l_paren) && "Expected '('!");
679 SourceLocation LParenLoc = ConsumeParen();
680
Sebastian Redla55e52c2008-11-25 22:21:31 +0000681 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000682 CommaLocsTy CommaLocs;
683
684 if (Tok.isNot(tok::r_paren)) {
685 if (ParseExpressionList(Exprs, CommaLocs)) {
686 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000687 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000688 }
689 }
690
691 // Match the ')'.
692 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
693
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000694 // TypeRep could be null, if it references an invalid typedef.
695 if (!TypeRep)
696 return ExprError();
697
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000698 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
699 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000700 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
701 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000702 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000703}
704
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000705/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000706///
707/// condition:
708/// expression
709/// type-specifier-seq declarator '=' assignment-expression
710/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
711/// '=' assignment-expression
712///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000713/// \param ExprResult if the condition was parsed as an expression, the
714/// parsed expression.
715///
716/// \param DeclResult if the condition was parsed as a declaration, the
717/// parsed declaration.
718///
Douglas Gregor586596f2010-05-06 17:25:47 +0000719/// \param Loc The location of the start of the statement that requires this
720/// condition, e.g., the "for" in a for loop.
721///
722/// \param ConvertToBoolean Whether the condition expression should be
723/// converted to a boolean value.
724///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000725/// \returns true if there was a parsing, false otherwise.
726bool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000727 DeclPtrTy &DeclResult,
728 SourceLocation Loc,
729 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000730 if (Tok.is(tok::code_completion)) {
731 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Condition);
732 ConsumeToken();
733 }
734
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000735 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000736 // Parse the expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000737 ExprResult = ParseExpression(); // expression
738 DeclResult = DeclPtrTy();
Douglas Gregor586596f2010-05-06 17:25:47 +0000739 if (ExprResult.isInvalid())
740 return true;
741
742 // If required, convert to a boolean value.
743 if (ConvertToBoolean)
744 ExprResult
745 = Actions.ActOnBooleanCondition(CurScope, Loc, move(ExprResult));
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000746 return ExprResult.isInvalid();
747 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000748
749 // type-specifier-seq
750 DeclSpec DS;
751 ParseSpecifierQualifierList(DS);
752
753 // declarator
754 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
755 ParseDeclarator(DeclaratorInfo);
756
757 // simple-asm-expr[opt]
758 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000759 SourceLocation Loc;
760 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000761 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000762 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000763 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000764 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000765 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000766 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000767 }
768
769 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000770 if (Tok.is(tok::kw___attribute)) {
771 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000772 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000773 DeclaratorInfo.AddAttributes(AttrList, Loc);
774 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000775
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000776 // Type-check the declaration itself.
777 Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope,
778 DeclaratorInfo);
779 DeclResult = Dcl.get();
780 ExprResult = ExprError();
781
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000782 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000783 if (Tok.is(tok::equal)) {
784 SourceLocation EqualLoc = ConsumeToken();
785 OwningExprResult AssignExpr(ParseAssignmentExpression());
786 if (!AssignExpr.isInvalid())
787 Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
788 } else {
789 // FIXME: C++0x allows a braced-init-list
790 Diag(Tok, diag::err_expected_equal_after_declarator);
791 }
792
Douglas Gregor586596f2010-05-06 17:25:47 +0000793 // FIXME: Build a reference to this declaration? Convert it to bool?
794 // (This is currently handled by Sema).
795
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000796 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000797}
798
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000799/// \brief Determine whether the current token starts a C++
800/// simple-type-specifier.
801bool Parser::isCXXSimpleTypeSpecifier() const {
802 switch (Tok.getKind()) {
803 case tok::annot_typename:
804 case tok::kw_short:
805 case tok::kw_long:
806 case tok::kw_signed:
807 case tok::kw_unsigned:
808 case tok::kw_void:
809 case tok::kw_char:
810 case tok::kw_int:
811 case tok::kw_float:
812 case tok::kw_double:
813 case tok::kw_wchar_t:
814 case tok::kw_char16_t:
815 case tok::kw_char32_t:
816 case tok::kw_bool:
817 // FIXME: C++0x decltype support.
818 // GNU typeof support.
819 case tok::kw_typeof:
820 return true;
821
822 default:
823 break;
824 }
825
826 return false;
827}
828
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000829/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
830/// This should only be called when the current token is known to be part of
831/// simple-type-specifier.
832///
833/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000834/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000835/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
836/// char
837/// wchar_t
838/// bool
839/// short
840/// int
841/// long
842/// signed
843/// unsigned
844/// float
845/// double
846/// void
847/// [GNU] typeof-specifier
848/// [C++0x] auto [TODO]
849///
850/// type-name:
851/// class-name
852/// enum-name
853/// typedef-name
854///
855void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
856 DS.SetRangeStart(Tok.getLocation());
857 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000858 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000859 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000861 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000862 case tok::identifier: // foo::bar
863 case tok::coloncolon: // ::foo::bar
864 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000865 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000866 assert(0 && "Not a simple-type-specifier token!");
867 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000868
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000869 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000870 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000871 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000872 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000873 break;
874 }
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000876 // builtin types
877 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000878 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000879 break;
880 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000881 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000882 break;
883 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000884 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000885 break;
886 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000887 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000888 break;
889 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000890 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000891 break;
892 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000893 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000894 break;
895 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000896 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000897 break;
898 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000899 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000900 break;
901 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000902 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000903 break;
904 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000905 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000906 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000907 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000908 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000909 break;
910 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000911 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000912 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000913 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000914 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000915 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000917 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000918 // GNU typeof support.
919 case tok::kw_typeof:
920 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000921 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000922 return;
923 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000924 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000925 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
926 else
927 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000928 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000929 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000930}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000931
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000932/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
933/// [dcl.name]), which is a non-empty sequence of type-specifiers,
934/// e.g., "const short int". Note that the DeclSpec is *not* finished
935/// by parsing the type-specifier-seq, because these sequences are
936/// typically followed by some form of declarator. Returns true and
937/// emits diagnostics if this is not a type-specifier-seq, false
938/// otherwise.
939///
940/// type-specifier-seq: [C++ 8.1]
941/// type-specifier type-specifier-seq[opt]
942///
943bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
944 DS.SetRangeStart(Tok.getLocation());
945 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000946 unsigned DiagID;
947 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000948
949 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000950 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
951 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000952 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000953 return true;
954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Sebastian Redld9bafa72010-02-03 21:21:43 +0000956 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
957 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
958 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000959
Douglas Gregor396a9f22010-02-24 23:13:13 +0000960 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000961 return false;
962}
963
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000964/// \brief Finish parsing a C++ unqualified-id that is a template-id of
965/// some form.
966///
967/// This routine is invoked when a '<' is encountered after an identifier or
968/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
969/// whether the unqualified-id is actually a template-id. This routine will
970/// then parse the template arguments and form the appropriate template-id to
971/// return to the caller.
972///
973/// \param SS the nested-name-specifier that precedes this template-id, if
974/// we're actually parsing a qualified-id.
975///
976/// \param Name for constructor and destructor names, this is the actual
977/// identifier that may be a template-name.
978///
979/// \param NameLoc the location of the class-name in a constructor or
980/// destructor.
981///
982/// \param EnteringContext whether we're entering the scope of the
983/// nested-name-specifier.
984///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000985/// \param ObjectType if this unqualified-id occurs within a member access
986/// expression, the type of the base object whose member is being accessed.
987///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000988/// \param Id as input, describes the template-name or operator-function-id
989/// that precedes the '<'. If template arguments were parsed successfully,
990/// will be updated with the template-id.
991///
Douglas Gregord4dca082010-02-24 18:44:31 +0000992/// \param AssumeTemplateId When true, this routine will assume that the name
993/// refers to a template without performing name lookup to verify.
994///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000995/// \returns true if a parse error occurred, false otherwise.
996bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
997 IdentifierInfo *Name,
998 SourceLocation NameLoc,
999 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001000 TypeTy *ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001001 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001002 bool AssumeTemplateId,
1003 SourceLocation TemplateKWLoc) {
1004 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1005 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001006
1007 TemplateTy Template;
1008 TemplateNameKind TNK = TNK_Non_template;
1009 switch (Id.getKind()) {
1010 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001011 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001012 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001013 if (AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001014 Template = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS,
Douglas Gregord4dca082010-02-24 18:44:31 +00001015 Id, ObjectType,
1016 EnteringContext);
1017 TNK = TNK_Dependent_template_name;
1018 if (!Template.get())
1019 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001020 } else {
1021 bool MemberOfUnknownSpecialization;
Douglas Gregord4dca082010-02-24 18:44:31 +00001022 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001023 EnteringContext, Template,
1024 MemberOfUnknownSpecialization);
1025
1026 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1027 ObjectType && IsTemplateArgumentList()) {
1028 // We have something like t->getAs<T>(), where getAs is a
1029 // member of an unknown specialization. However, this will only
1030 // parse correctly as a template, so suggest the keyword 'template'
1031 // before 'getAs' and treat this as a dependent template name.
1032 std::string Name;
1033 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1034 Name = Id.Identifier->getName();
1035 else {
1036 Name = "operator ";
1037 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1038 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1039 else
1040 Name += Id.Identifier->getName();
1041 }
1042 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1043 << Name
1044 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
1045 Template = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS,
1046 Id, ObjectType,
1047 EnteringContext);
1048 TNK = TNK_Dependent_template_name;
1049 if (!Template.get())
1050 return true;
1051 }
1052 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001053 break;
1054
Douglas Gregor014e88d2009-11-03 23:16:33 +00001055 case UnqualifiedId::IK_ConstructorName: {
1056 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001057 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001058 TemplateName.setIdentifier(Name, NameLoc);
1059 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001060 EnteringContext, Template,
1061 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001062 break;
1063 }
1064
Douglas Gregor014e88d2009-11-03 23:16:33 +00001065 case UnqualifiedId::IK_DestructorName: {
1066 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001067 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001068 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001069 if (ObjectType) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001070 Template = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS,
Douglas Gregora481edb2009-11-20 23:39:24 +00001071 TemplateName, ObjectType,
1072 EnteringContext);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001073 TNK = TNK_Dependent_template_name;
1074 if (!Template.get())
1075 return true;
1076 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001077 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001078 EnteringContext, Template,
1079 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001080
1081 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001082 Diag(NameLoc, diag::err_destructor_template_id)
1083 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001084 return true;
1085 }
1086 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001087 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001088 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001089
1090 default:
1091 return false;
1092 }
1093
1094 if (TNK == TNK_Non_template)
1095 return false;
1096
1097 // Parse the enclosed template argument list.
1098 SourceLocation LAngleLoc, RAngleLoc;
1099 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001100 if (Tok.is(tok::less) &&
1101 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001102 &SS, true, LAngleLoc,
1103 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001104 RAngleLoc))
1105 return true;
1106
1107 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001108 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1109 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001110 // Form a parsed representation of the template-id to be stored in the
1111 // UnqualifiedId.
1112 TemplateIdAnnotation *TemplateId
1113 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1114
1115 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1116 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001117 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001118 TemplateId->TemplateNameLoc = Id.StartLocation;
1119 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001120 TemplateId->Name = 0;
1121 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1122 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001123 }
1124
1125 TemplateId->Template = Template.getAs<void*>();
1126 TemplateId->Kind = TNK;
1127 TemplateId->LAngleLoc = LAngleLoc;
1128 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001129 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001130 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001131 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001132 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001133
1134 Id.setTemplateId(TemplateId);
1135 return false;
1136 }
1137
1138 // Bundle the template arguments together.
1139 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001140 TemplateArgs.size());
1141
1142 // Constructor and destructor names.
1143 Action::TypeResult Type
1144 = Actions.ActOnTemplateIdType(Template, NameLoc,
1145 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001146 RAngleLoc);
1147 if (Type.isInvalid())
1148 return true;
1149
1150 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1151 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1152 else
1153 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1154
1155 return false;
1156}
1157
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001158/// \brief Parse an operator-function-id or conversion-function-id as part
1159/// of a C++ unqualified-id.
1160///
1161/// This routine is responsible only for parsing the operator-function-id or
1162/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001163///
1164/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001165/// operator-function-id: [C++ 13.5]
1166/// 'operator' operator
1167///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001168/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001169/// new delete new[] delete[]
1170/// + - * / % ^ & | ~
1171/// ! = < > += -= *= /= %=
1172/// ^= &= |= << >> >>= <<= == !=
1173/// <= >= && || ++ -- , ->* ->
1174/// () []
1175///
1176/// conversion-function-id: [C++ 12.3.2]
1177/// operator conversion-type-id
1178///
1179/// conversion-type-id:
1180/// type-specifier-seq conversion-declarator[opt]
1181///
1182/// conversion-declarator:
1183/// ptr-operator conversion-declarator[opt]
1184/// \endcode
1185///
1186/// \param The nested-name-specifier that preceded this unqualified-id. If
1187/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1188///
1189/// \param EnteringContext whether we are entering the scope of the
1190/// nested-name-specifier.
1191///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001192/// \param ObjectType if this unqualified-id occurs within a member access
1193/// expression, the type of the base object whose member is being accessed.
1194///
1195/// \param Result on a successful parse, contains the parsed unqualified-id.
1196///
1197/// \returns true if parsing fails, false otherwise.
1198bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1199 TypeTy *ObjectType,
1200 UnqualifiedId &Result) {
1201 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1202
1203 // Consume the 'operator' keyword.
1204 SourceLocation KeywordLoc = ConsumeToken();
1205
1206 // Determine what kind of operator name we have.
1207 unsigned SymbolIdx = 0;
1208 SourceLocation SymbolLocations[3];
1209 OverloadedOperatorKind Op = OO_None;
1210 switch (Tok.getKind()) {
1211 case tok::kw_new:
1212 case tok::kw_delete: {
1213 bool isNew = Tok.getKind() == tok::kw_new;
1214 // Consume the 'new' or 'delete'.
1215 SymbolLocations[SymbolIdx++] = ConsumeToken();
1216 if (Tok.is(tok::l_square)) {
1217 // Consume the '['.
1218 SourceLocation LBracketLoc = ConsumeBracket();
1219 // Consume the ']'.
1220 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1221 LBracketLoc);
1222 if (RBracketLoc.isInvalid())
1223 return true;
1224
1225 SymbolLocations[SymbolIdx++] = LBracketLoc;
1226 SymbolLocations[SymbolIdx++] = RBracketLoc;
1227 Op = isNew? OO_Array_New : OO_Array_Delete;
1228 } else {
1229 Op = isNew? OO_New : OO_Delete;
1230 }
1231 break;
1232 }
1233
1234#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1235 case tok::Token: \
1236 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1237 Op = OO_##Name; \
1238 break;
1239#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1240#include "clang/Basic/OperatorKinds.def"
1241
1242 case tok::l_paren: {
1243 // Consume the '('.
1244 SourceLocation LParenLoc = ConsumeParen();
1245 // Consume the ')'.
1246 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1247 LParenLoc);
1248 if (RParenLoc.isInvalid())
1249 return true;
1250
1251 SymbolLocations[SymbolIdx++] = LParenLoc;
1252 SymbolLocations[SymbolIdx++] = RParenLoc;
1253 Op = OO_Call;
1254 break;
1255 }
1256
1257 case tok::l_square: {
1258 // Consume the '['.
1259 SourceLocation LBracketLoc = ConsumeBracket();
1260 // Consume the ']'.
1261 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1262 LBracketLoc);
1263 if (RBracketLoc.isInvalid())
1264 return true;
1265
1266 SymbolLocations[SymbolIdx++] = LBracketLoc;
1267 SymbolLocations[SymbolIdx++] = RBracketLoc;
1268 Op = OO_Subscript;
1269 break;
1270 }
1271
1272 case tok::code_completion: {
1273 // Code completion for the operator name.
1274 Actions.CodeCompleteOperatorName(CurScope);
1275
1276 // Consume the operator token.
1277 ConsumeToken();
1278
1279 // Don't try to parse any further.
1280 return true;
1281 }
1282
1283 default:
1284 break;
1285 }
1286
1287 if (Op != OO_None) {
1288 // We have parsed an operator-function-id.
1289 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1290 return false;
1291 }
Sean Hunt0486d742009-11-28 04:44:28 +00001292
1293 // Parse a literal-operator-id.
1294 //
1295 // literal-operator-id: [C++0x 13.5.8]
1296 // operator "" identifier
1297
1298 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1299 if (Tok.getLength() != 2)
1300 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1301 ConsumeStringToken();
1302
1303 if (Tok.isNot(tok::identifier)) {
1304 Diag(Tok.getLocation(), diag::err_expected_ident);
1305 return true;
1306 }
1307
1308 IdentifierInfo *II = Tok.getIdentifierInfo();
1309 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001310 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001311 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001312
1313 // Parse a conversion-function-id.
1314 //
1315 // conversion-function-id: [C++ 12.3.2]
1316 // operator conversion-type-id
1317 //
1318 // conversion-type-id:
1319 // type-specifier-seq conversion-declarator[opt]
1320 //
1321 // conversion-declarator:
1322 // ptr-operator conversion-declarator[opt]
1323
1324 // Parse the type-specifier-seq.
1325 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001326 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001327 return true;
1328
1329 // Parse the conversion-declarator, which is merely a sequence of
1330 // ptr-operators.
1331 Declarator D(DS, Declarator::TypeNameContext);
1332 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1333
1334 // Finish up the type.
1335 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1336 if (Ty.isInvalid())
1337 return true;
1338
1339 // Note that this is a conversion-function-id.
1340 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1341 D.getSourceRange().getEnd());
1342 return false;
1343}
1344
1345/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1346/// name of an entity.
1347///
1348/// \code
1349/// unqualified-id: [C++ expr.prim.general]
1350/// identifier
1351/// operator-function-id
1352/// conversion-function-id
1353/// [C++0x] literal-operator-id [TODO]
1354/// ~ class-name
1355/// template-id
1356///
1357/// \endcode
1358///
1359/// \param The nested-name-specifier that preceded this unqualified-id. If
1360/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1361///
1362/// \param EnteringContext whether we are entering the scope of the
1363/// nested-name-specifier.
1364///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001365/// \param AllowDestructorName whether we allow parsing of a destructor name.
1366///
1367/// \param AllowConstructorName whether we allow parsing a constructor name.
1368///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001369/// \param ObjectType if this unqualified-id occurs within a member access
1370/// expression, the type of the base object whose member is being accessed.
1371///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001372/// \param Result on a successful parse, contains the parsed unqualified-id.
1373///
1374/// \returns true if parsing fails, false otherwise.
1375bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1376 bool AllowDestructorName,
1377 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001378 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001379 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001380
1381 // Handle 'A::template B'. This is for template-ids which have not
1382 // already been annotated by ParseOptionalCXXScopeSpecifier().
1383 bool TemplateSpecified = false;
1384 SourceLocation TemplateKWLoc;
1385 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1386 (ObjectType || SS.isSet())) {
1387 TemplateSpecified = true;
1388 TemplateKWLoc = ConsumeToken();
1389 }
1390
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001391 // unqualified-id:
1392 // identifier
1393 // template-id (when it hasn't already been annotated)
1394 if (Tok.is(tok::identifier)) {
1395 // Consume the identifier.
1396 IdentifierInfo *Id = Tok.getIdentifierInfo();
1397 SourceLocation IdLoc = ConsumeToken();
1398
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001399 if (!getLang().CPlusPlus) {
1400 // If we're not in C++, only identifiers matter. Record the
1401 // identifier and return.
1402 Result.setIdentifier(Id, IdLoc);
1403 return false;
1404 }
1405
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001406 if (AllowConstructorName &&
1407 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1408 // We have parsed a constructor name.
1409 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1410 &SS, false),
1411 IdLoc, IdLoc);
1412 } else {
1413 // We have parsed an identifier.
1414 Result.setIdentifier(Id, IdLoc);
1415 }
1416
1417 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001418 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001419 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001420 ObjectType, Result,
1421 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001422
1423 return false;
1424 }
1425
1426 // unqualified-id:
1427 // template-id (already parsed and annotated)
1428 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001429 TemplateIdAnnotation *TemplateId
1430 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1431
1432 // If the template-name names the current class, then this is a constructor
1433 if (AllowConstructorName && TemplateId->Name &&
1434 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
1435 if (SS.isSet()) {
1436 // C++ [class.qual]p2 specifies that a qualified template-name
1437 // is taken as the constructor name where a constructor can be
1438 // declared. Thus, the template arguments are extraneous, so
1439 // complain about them and remove them entirely.
1440 Diag(TemplateId->TemplateNameLoc,
1441 diag::err_out_of_line_constructor_template_id)
1442 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001443 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001444 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1445 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1446 TemplateId->TemplateNameLoc,
1447 CurScope,
1448 &SS, false),
1449 TemplateId->TemplateNameLoc,
1450 TemplateId->RAngleLoc);
1451 TemplateId->Destroy();
1452 ConsumeToken();
1453 return false;
1454 }
1455
1456 Result.setConstructorTemplateId(TemplateId);
1457 ConsumeToken();
1458 return false;
1459 }
1460
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001461 // We have already parsed a template-id; consume the annotation token as
1462 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001463 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001464 ConsumeToken();
1465 return false;
1466 }
1467
1468 // unqualified-id:
1469 // operator-function-id
1470 // conversion-function-id
1471 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001472 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001473 return true;
1474
Sean Hunte6252d12009-11-28 08:58:14 +00001475 // If we have an operator-function-id or a literal-operator-id and the next
1476 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001477 //
1478 // template-id:
1479 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001480 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1481 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001482 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001483 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1484 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001485 Result,
1486 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001487
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001488 return false;
1489 }
1490
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001491 if (getLang().CPlusPlus &&
1492 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001493 // C++ [expr.unary.op]p10:
1494 // There is an ambiguity in the unary-expression ~X(), where X is a
1495 // class-name. The ambiguity is resolved in favor of treating ~ as a
1496 // unary complement rather than treating ~X as referring to a destructor.
1497
1498 // Parse the '~'.
1499 SourceLocation TildeLoc = ConsumeToken();
1500
1501 // Parse the class-name.
1502 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001503 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001504 return true;
1505 }
1506
1507 // Parse the class-name (or template-name in a simple-template-id).
1508 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1509 SourceLocation ClassNameLoc = ConsumeToken();
1510
Douglas Gregor0278e122010-05-05 05:58:24 +00001511 if (TemplateSpecified || Tok.is(tok::less)) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001512 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1513 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001514 EnteringContext, ObjectType, Result,
1515 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001516 }
1517
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001518 // Note that this is a destructor name.
Douglas Gregor124b8782010-02-16 19:09:40 +00001519 Action::TypeTy *Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1520 ClassNameLoc, CurScope,
1521 SS, ObjectType,
1522 EnteringContext);
1523 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001524 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001525
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001526 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001527 return false;
1528 }
1529
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001530 Diag(Tok, diag::err_expected_unqualified_id)
1531 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001532 return true;
1533}
1534
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001535/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1536/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001537///
Chris Lattner59232d32009-01-04 21:25:24 +00001538/// This method is called to parse the new expression after the optional :: has
1539/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1540/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001541///
1542/// new-expression:
1543/// '::'[opt] 'new' new-placement[opt] new-type-id
1544/// new-initializer[opt]
1545/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1546/// new-initializer[opt]
1547///
1548/// new-placement:
1549/// '(' expression-list ')'
1550///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001551/// new-type-id:
1552/// type-specifier-seq new-declarator[opt]
1553///
1554/// new-declarator:
1555/// ptr-operator new-declarator[opt]
1556/// direct-new-declarator
1557///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001558/// new-initializer:
1559/// '(' expression-list[opt] ')'
1560/// [C++0x] braced-init-list [TODO]
1561///
Chris Lattner59232d32009-01-04 21:25:24 +00001562Parser::OwningExprResult
1563Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1564 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1565 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001566
1567 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1568 // second form of new-expression. It can't be a new-type-id.
1569
Sebastian Redla55e52c2008-11-25 22:21:31 +00001570 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001571 SourceLocation PlacementLParen, PlacementRParen;
1572
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001573 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001574 DeclSpec DS;
1575 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001576 if (Tok.is(tok::l_paren)) {
1577 // If it turns out to be a placement, we change the type location.
1578 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001579 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1580 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001581 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001582 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001583
1584 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001585 if (PlacementRParen.isInvalid()) {
1586 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001587 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001588 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001589
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001590 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001591 // Reset the placement locations. There was no placement.
1592 PlacementLParen = PlacementRParen = SourceLocation();
1593 ParenTypeId = true;
1594 } else {
1595 // We still need the type.
1596 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001597 SourceLocation LParen = ConsumeParen();
1598 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001599 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001600 ParseDeclarator(DeclaratorInfo);
1601 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001602 ParenTypeId = true;
1603 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001604 if (ParseCXXTypeSpecifierSeq(DS))
1605 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001606 else {
1607 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001608 ParseDeclaratorInternal(DeclaratorInfo,
1609 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001610 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001611 ParenTypeId = false;
1612 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001613 }
1614 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001615 // A new-type-id is a simplified type-id, where essentially the
1616 // direct-declarator is replaced by a direct-new-declarator.
1617 if (ParseCXXTypeSpecifierSeq(DS))
1618 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001619 else {
1620 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001621 ParseDeclaratorInternal(DeclaratorInfo,
1622 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001623 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001624 ParenTypeId = false;
1625 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001626 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001627 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001628 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001629 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001630
Sebastian Redla55e52c2008-11-25 22:21:31 +00001631 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001632 SourceLocation ConstructorLParen, ConstructorRParen;
1633
1634 if (Tok.is(tok::l_paren)) {
1635 ConstructorLParen = ConsumeParen();
1636 if (Tok.isNot(tok::r_paren)) {
1637 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001638 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1639 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001640 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001641 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001642 }
1643 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001644 if (ConstructorRParen.isInvalid()) {
1645 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001646 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001647 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001648 }
1649
Sebastian Redlf53597f2009-03-15 17:47:39 +00001650 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1651 move_arg(PlacementArgs), PlacementRParen,
1652 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1653 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001654}
1655
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001656/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1657/// passed to ParseDeclaratorInternal.
1658///
1659/// direct-new-declarator:
1660/// '[' expression ']'
1661/// direct-new-declarator '[' constant-expression ']'
1662///
Chris Lattner59232d32009-01-04 21:25:24 +00001663void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001664 // Parse the array dimensions.
1665 bool first = true;
1666 while (Tok.is(tok::l_square)) {
1667 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001668 OwningExprResult Size(first ? ParseExpression()
1669 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001670 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001671 // Recover
1672 SkipUntil(tok::r_square);
1673 return;
1674 }
1675 first = false;
1676
Sebastian Redlab197ba2009-02-09 18:23:29 +00001677 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001678 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001679 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001680 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001681
Sebastian Redlab197ba2009-02-09 18:23:29 +00001682 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001683 return;
1684 }
1685}
1686
1687/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1688/// This ambiguity appears in the syntax of the C++ new operator.
1689///
1690/// new-expression:
1691/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1692/// new-initializer[opt]
1693///
1694/// new-placement:
1695/// '(' expression-list ')'
1696///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001697bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001698 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001699 // The '(' was already consumed.
1700 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001701 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001702 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001703 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001704 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001705 }
1706
1707 // It's not a type, it has to be an expression list.
1708 // Discard the comma locations - ActOnCXXNew has enough parameters.
1709 CommaLocsTy CommaLocs;
1710 return ParseExpressionList(PlacementArgs, CommaLocs);
1711}
1712
1713/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1714/// to free memory allocated by new.
1715///
Chris Lattner59232d32009-01-04 21:25:24 +00001716/// This method is called to parse the 'delete' expression after the optional
1717/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1718/// and "Start" is its location. Otherwise, "Start" is the location of the
1719/// 'delete' token.
1720///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001721/// delete-expression:
1722/// '::'[opt] 'delete' cast-expression
1723/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001724Parser::OwningExprResult
1725Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1726 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1727 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001728
1729 // Array delete?
1730 bool ArrayDelete = false;
1731 if (Tok.is(tok::l_square)) {
1732 ArrayDelete = true;
1733 SourceLocation LHS = ConsumeBracket();
1734 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1735 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001736 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001737 }
1738
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001739 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001740 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001741 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001742
Sebastian Redlf53597f2009-03-15 17:47:39 +00001743 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001744}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001745
Mike Stump1eb44332009-09-09 15:08:12 +00001746static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001747 switch(kind) {
1748 default: assert(false && "Not a known unary type trait.");
1749 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1750 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1751 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1752 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1753 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1754 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1755 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1756 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1757 case tok::kw___is_abstract: return UTT_IsAbstract;
1758 case tok::kw___is_class: return UTT_IsClass;
1759 case tok::kw___is_empty: return UTT_IsEmpty;
1760 case tok::kw___is_enum: return UTT_IsEnum;
1761 case tok::kw___is_pod: return UTT_IsPOD;
1762 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1763 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001764 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001765 }
1766}
1767
1768/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1769/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1770/// templates.
1771///
1772/// primary-expression:
1773/// [GNU] unary-type-trait '(' type-id ')'
1774///
Mike Stump1eb44332009-09-09 15:08:12 +00001775Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001776 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1777 SourceLocation Loc = ConsumeToken();
1778
1779 SourceLocation LParen = Tok.getLocation();
1780 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1781 return ExprError();
1782
1783 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1784 // there will be cryptic errors about mismatched parentheses and missing
1785 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001786 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001787
1788 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1789
Douglas Gregor809070a2009-02-18 17:45:20 +00001790 if (Ty.isInvalid())
1791 return ExprError();
1792
1793 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001794}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001795
1796/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1797/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1798/// based on the context past the parens.
1799Parser::OwningExprResult
1800Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1801 TypeTy *&CastTy,
1802 SourceLocation LParenLoc,
1803 SourceLocation &RParenLoc) {
1804 assert(getLang().CPlusPlus && "Should only be called for C++!");
1805 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1806 assert(isTypeIdInParens() && "Not a type-id!");
1807
1808 OwningExprResult Result(Actions, true);
1809 CastTy = 0;
1810
1811 // We need to disambiguate a very ugly part of the C++ syntax:
1812 //
1813 // (T())x; - type-id
1814 // (T())*x; - type-id
1815 // (T())/x; - expression
1816 // (T()); - expression
1817 //
1818 // The bad news is that we cannot use the specialized tentative parser, since
1819 // it can only verify that the thing inside the parens can be parsed as
1820 // type-id, it is not useful for determining the context past the parens.
1821 //
1822 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001823 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001824 //
1825 // It uses a scheme similar to parsing inline methods. The parenthesized
1826 // tokens are cached, the context that follows is determined (possibly by
1827 // parsing a cast-expression), and then we re-introduce the cached tokens
1828 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001829
Mike Stump1eb44332009-09-09 15:08:12 +00001830 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001831 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001832
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001833 // Store the tokens of the parentheses. We will parse them after we determine
1834 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001835 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001836 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001837 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1838 return ExprError();
1839 }
1840
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001841 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001842 ParseAs = CompoundLiteral;
1843 } else {
1844 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001845 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1846 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1847 NotCastExpr = true;
1848 } else {
1849 // Try parsing the cast-expression that may follow.
1850 // If it is not a cast-expression, NotCastExpr will be true and no token
1851 // will be consumed.
1852 Result = ParseCastExpression(false/*isUnaryExpression*/,
1853 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001854 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001855 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001856
1857 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1858 // an expression.
1859 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001860 }
1861
Mike Stump1eb44332009-09-09 15:08:12 +00001862 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001863 Toks.push_back(Tok);
1864 // Re-enter the stored parenthesized tokens into the token stream, so we may
1865 // parse them now.
1866 PP.EnterTokenStream(Toks.data(), Toks.size(),
1867 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1868 // Drop the current token and bring the first cached one. It's the same token
1869 // as when we entered this function.
1870 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001871
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001872 if (ParseAs >= CompoundLiteral) {
1873 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001874
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001875 // Match the ')'.
1876 if (Tok.is(tok::r_paren))
1877 RParenLoc = ConsumeParen();
1878 else
1879 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001880
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001881 if (ParseAs == CompoundLiteral) {
1882 ExprType = CompoundLiteral;
1883 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1884 }
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001886 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1887 assert(ParseAs == CastExpr);
1888
1889 if (Ty.isInvalid())
1890 return ExprError();
1891
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001892 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001893
1894 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001895 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001896 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001897 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001898 return move(Result);
1899 }
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001901 // Not a compound literal, and not followed by a cast-expression.
1902 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001903
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001904 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001905 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001906 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1907 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1908
1909 // Match the ')'.
1910 if (Result.isInvalid()) {
1911 SkipUntil(tok::r_paren);
1912 return ExprError();
1913 }
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001915 if (Tok.is(tok::r_paren))
1916 RParenLoc = ConsumeParen();
1917 else
1918 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1919
1920 return move(Result);
1921}