blob: 4d46b92a0349028a73632c03a631997401069303 [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///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000058/// \returns true if a scope specifier was parsed.
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();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000070 return true;
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)
171 break;
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))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000174 break;
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;
191 return HasScopeSpecifier;
192 }
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 Gregorb10cd042010-02-21 18:36:56 +0000215 CCLoc,
Douglas Gregord4dca082010-02-24 18:44:31 +0000216 false));
Douglas Gregor31a19b62009-04-01 21:51:26 +0000217 else
218 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000219 SS.setEndLoc(CCLoc);
220 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000221 }
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Chris Lattner67b9e832009-06-26 03:45:46 +0000223 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000224 }
225
Chris Lattner5c7f7862009-06-26 03:52:38 +0000226
227 // The rest of the nested-name-specifier possibilities start with
228 // tok::identifier.
229 if (Tok.isNot(tok::identifier))
230 break;
231
232 IdentifierInfo &II = *Tok.getIdentifierInfo();
233
234 // nested-name-specifier:
235 // type-name '::'
236 // namespace-name '::'
237 // nested-name-specifier identifier '::'
238 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000239
240 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
241 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000242 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor77549082010-02-24 21:29:12 +0000243 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
244 !Actions.isNonTypeNestedNameSpecifier(CurScope, SS, Tok.getLocation(),
245 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000246 *MayBePseudoDestructor = true;
247 return HasScopeSpecifier;
248 }
249
Douglas Gregorb10cd042010-02-21 18:36:56 +0000250 if (Actions.IsInvalidUnlessNestedName(CurScope, SS, II,
Douglas Gregord4dca082010-02-24 18:44:31 +0000251 false, ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000252 EnteringContext) &&
253 // If the token after the colon isn't an identifier, it's still an
254 // error, but they probably meant something else strange so don't
255 // recover like this.
256 PP.LookAhead(1).is(tok::identifier)) {
257 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
258 << CodeModificationHint::CreateReplacement(Next.getLocation(), "::");
259
260 // Recover as if the user wrote '::'.
261 Next.setKind(tok::coloncolon);
262 }
Chris Lattner46646492009-12-07 01:36:53 +0000263 }
264
Chris Lattner5c7f7862009-06-26 03:52:38 +0000265 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000266 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
267 !Actions.isNonTypeNestedNameSpecifier(CurScope, SS, Tok.getLocation(),
268 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000269 *MayBePseudoDestructor = true;
270 return HasScopeSpecifier;
271 }
272
Chris Lattner5c7f7862009-06-26 03:52:38 +0000273 // We have an identifier followed by a '::'. Lookup this name
274 // as the name in a nested-name-specifier.
275 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000276 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
277 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000278 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattner5c7f7862009-06-26 03:52:38 +0000280 if (!HasScopeSpecifier) {
281 SS.setBeginLoc(IdLoc);
282 HasScopeSpecifier = true;
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chris Lattner5c7f7862009-06-26 03:52:38 +0000285 if (SS.isInvalid())
286 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattner5c7f7862009-06-26 03:52:38 +0000288 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000289 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregord4dca082010-02-24 18:44:31 +0000290 false, ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000291 EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000292 SS.setEndLoc(CCLoc);
293 continue;
294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Chris Lattner5c7f7862009-06-26 03:52:38 +0000296 // nested-name-specifier:
297 // type-name '<'
298 if (Next.is(tok::less)) {
299 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000300 UnqualifiedId TemplateName;
301 TemplateName.setIdentifier(&II, Tok.getLocation());
302 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
303 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000304 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000305 EnteringContext,
306 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000307 // We have found a template name, so annotate this this token
308 // with a template-id annotation. We do not permit the
309 // template-id to be translated into a type annotation,
310 // because some clients (e.g., the parsing of class template
311 // specializations) still want to see the original template-id
312 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000313 ConsumeToken();
314 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
315 SourceLocation(), false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000316 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000317 continue;
318 }
319 }
320
Douglas Gregor39a8de12009-02-25 19:37:18 +0000321 // We don't have any tokens that form the beginning of a
322 // nested-name-specifier, so we're done.
323 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000324 }
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Douglas Gregord4dca082010-02-24 18:44:31 +0000326 // Even if we didn't see any pieces of a nested-name-specifier, we
327 // still check whether there is a tilde in this position, which
328 // indicates a potential pseudo-destructor.
329 if (CheckForDestructor && Tok.is(tok::tilde))
330 *MayBePseudoDestructor = true;
331
Douglas Gregor39a8de12009-02-25 19:37:18 +0000332 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000333}
334
335/// ParseCXXIdExpression - Handle id-expression.
336///
337/// id-expression:
338/// unqualified-id
339/// qualified-id
340///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000341/// qualified-id:
342/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
343/// '::' identifier
344/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000345/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000346///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000347/// NOTE: The standard specifies that, for qualified-id, the parser does not
348/// expect:
349///
350/// '::' conversion-function-id
351/// '::' '~' class-name
352///
353/// This may cause a slight inconsistency on diagnostics:
354///
355/// class C {};
356/// namespace A {}
357/// void f() {
358/// :: A :: ~ C(); // Some Sema error about using destructor with a
359/// // namespace.
360/// :: ~ C(); // Some Parser error like 'unexpected ~'.
361/// }
362///
363/// We simplify the parser a bit and make it work like:
364///
365/// qualified-id:
366/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
367/// '::' unqualified-id
368///
369/// That way Sema can handle and report similar errors for namespaces and the
370/// global scope.
371///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000372/// The isAddressOfOperand parameter indicates that this id-expression is a
373/// direct operand of the address-of operator. This is, besides member contexts,
374/// the only place where a qualified-id naming a non-static class member may
375/// appear.
376///
377Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000378 // qualified-id:
379 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
380 // '::' unqualified-id
381 //
382 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000383 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000384
385 UnqualifiedId Name;
386 if (ParseUnqualifiedId(SS,
387 /*EnteringContext=*/false,
388 /*AllowDestructorName=*/false,
389 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000390 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000391 Name))
392 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000393
394 // This is only the direct operand of an & operator if it is not
395 // followed by a postfix-expression suffix.
396 if (isAddressOfOperand) {
397 switch (Tok.getKind()) {
398 case tok::l_square:
399 case tok::l_paren:
400 case tok::arrow:
401 case tok::period:
402 case tok::plusplus:
403 case tok::minusminus:
404 isAddressOfOperand = false;
405 break;
406
407 default:
408 break;
409 }
410 }
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000411
412 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
413 isAddressOfOperand);
414
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000415}
416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417/// ParseCXXCasts - This handles the various ways to cast expressions to another
418/// type.
419///
420/// postfix-expression: [C++ 5.2p1]
421/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
422/// 'static_cast' '<' type-name '>' '(' expression ')'
423/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
424/// 'const_cast' '<' type-name '>' '(' expression ')'
425///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000426Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 tok::TokenKind Kind = Tok.getKind();
428 const char *CastName = 0; // For error messages
429
430 switch (Kind) {
431 default: assert(0 && "Unknown C++ cast!"); abort();
432 case tok::kw_const_cast: CastName = "const_cast"; break;
433 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
434 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
435 case tok::kw_static_cast: CastName = "static_cast"; break;
436 }
437
438 SourceLocation OpLoc = ConsumeToken();
439 SourceLocation LAngleBracketLoc = Tok.getLocation();
440
441 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000442 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000443
Douglas Gregor809070a2009-02-18 17:45:20 +0000444 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 SourceLocation RAngleBracketLoc = Tok.getLocation();
446
Chris Lattner1ab3b962008-11-18 07:48:38 +0000447 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000448 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000449
450 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
451
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000452 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
453 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000454
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000455 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000457 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000458 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000459
Douglas Gregor809070a2009-02-18 17:45:20 +0000460 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000461 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000462 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000463 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000464 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000465
Sebastian Redl20df9b72008-12-11 22:51:44 +0000466 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000467}
468
Sebastian Redlc42e1182008-11-11 11:37:55 +0000469/// ParseCXXTypeid - This handles the C++ typeid expression.
470///
471/// postfix-expression: [C++ 5.2p1]
472/// 'typeid' '(' expression ')'
473/// 'typeid' '(' type-id ')'
474///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000475Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000476 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
477
478 SourceLocation OpLoc = ConsumeToken();
479 SourceLocation LParenLoc = Tok.getLocation();
480 SourceLocation RParenLoc;
481
482 // typeid expressions are always parenthesized.
483 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
484 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000485 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000486
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000487 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000488
489 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000490 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000491
492 // Match the ')'.
493 MatchRHSPunctuation(tok::r_paren, LParenLoc);
494
Douglas Gregor809070a2009-02-18 17:45:20 +0000495 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000496 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000497
498 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000499 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000500 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000501 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000502 // When typeid is applied to an expression other than an lvalue of a
503 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000504 // operand (Clause 5).
505 //
Mike Stump1eb44332009-09-09 15:08:12 +0000506 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000507 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000508 // we the expression is potentially potentially evaluated.
509 EnterExpressionEvaluationContext Unevaluated(Actions,
510 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000511 Result = ParseExpression();
512
513 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000514 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000515 SkipUntil(tok::r_paren);
516 else {
517 MatchRHSPunctuation(tok::r_paren, LParenLoc);
518
519 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000520 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000521 }
522 }
523
Sebastian Redl20df9b72008-12-11 22:51:44 +0000524 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000525}
526
Douglas Gregord4dca082010-02-24 18:44:31 +0000527/// \brief Parse a C++ pseudo-destructor expression after the base,
528/// . or -> operator, and nested-name-specifier have already been
529/// parsed.
530///
531/// postfix-expression: [C++ 5.2]
532/// postfix-expression . pseudo-destructor-name
533/// postfix-expression -> pseudo-destructor-name
534///
535/// pseudo-destructor-name:
536/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
537/// ::[opt] nested-name-specifier template simple-template-id ::
538/// ~type-name
539/// ::[opt] nested-name-specifier[opt] ~type-name
540///
541Parser::OwningExprResult
542Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
543 tok::TokenKind OpKind,
544 CXXScopeSpec &SS,
545 Action::TypeTy *ObjectType) {
546 // We're parsing either a pseudo-destructor-name or a dependent
547 // member access that has the same form as a
548 // pseudo-destructor-name. We parse both in the same way and let
549 // the action model sort them out.
550 //
551 // Note that the ::[opt] nested-name-specifier[opt] has already
552 // been parsed, and if there was a simple-template-id, it has
553 // been coalesced into a template-id annotation token.
554 UnqualifiedId FirstTypeName;
555 SourceLocation CCLoc;
556 if (Tok.is(tok::identifier)) {
557 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
558 ConsumeToken();
559 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
560 CCLoc = ConsumeToken();
561 } else if (Tok.is(tok::annot_template_id)) {
562 FirstTypeName.setTemplateId(
563 (TemplateIdAnnotation *)Tok.getAnnotationValue());
564 ConsumeToken();
565 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
566 CCLoc = ConsumeToken();
567 } else {
568 FirstTypeName.setIdentifier(0, SourceLocation());
569 }
570
571 // Parse the tilde.
572 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
573 SourceLocation TildeLoc = ConsumeToken();
574 if (!Tok.is(tok::identifier)) {
575 Diag(Tok, diag::err_destructor_tilde_identifier);
576 return ExprError();
577 }
578
579 // Parse the second type.
580 UnqualifiedId SecondTypeName;
581 IdentifierInfo *Name = Tok.getIdentifierInfo();
582 SourceLocation NameLoc = ConsumeToken();
583 SecondTypeName.setIdentifier(Name, NameLoc);
584
585 // If there is a '<', the second type name is a template-id. Parse
586 // it as such.
587 if (Tok.is(tok::less) &&
588 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
589 SecondTypeName, /*AssumeTemplateName=*/true))
590 return ExprError();
591
592 return Actions.ActOnPseudoDestructorExpr(CurScope, move(Base), OpLoc, OpKind,
593 SS, FirstTypeName, CCLoc,
594 TildeLoc, SecondTypeName,
595 Tok.is(tok::l_paren));
596}
597
Reid Spencer5f016e22007-07-11 17:01:13 +0000598/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
599///
600/// boolean-literal: [C++ 2.13.5]
601/// 'true'
602/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000603Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000605 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000606}
Chris Lattner50dd2892008-02-26 00:51:44 +0000607
608/// ParseThrowExpression - This handles the C++ throw expression.
609///
610/// throw-expression: [C++ 15]
611/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000612Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000613 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000614 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000615
Chris Lattner2a2819a2008-04-06 06:02:23 +0000616 // If the current token isn't the start of an assignment-expression,
617 // then the expression is not present. This handles things like:
618 // "C ? throw : (void)42", which is crazy but legal.
619 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
620 case tok::semi:
621 case tok::r_paren:
622 case tok::r_square:
623 case tok::r_brace:
624 case tok::colon:
625 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000626 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000627
Chris Lattner2a2819a2008-04-06 06:02:23 +0000628 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000629 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000630 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000631 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000632 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000633}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000634
635/// ParseCXXThis - This handles the C++ 'this' pointer.
636///
637/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
638/// a non-lvalue expression whose value is the address of the object for which
639/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000640Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000641 assert(Tok.is(tok::kw_this) && "Not 'this'!");
642 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000643 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000644}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000645
646/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
647/// Can be interpreted either as function-style casting ("int(x)")
648/// or class type construction ("ClassType(x,y,z)")
649/// or creation of a value-initialized type ("int()").
650///
651/// postfix-expression: [C++ 5.2p1]
652/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
653/// typename-specifier '(' expression-list[opt] ')' [TODO]
654///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000655Parser::OwningExprResult
656Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000657 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000658 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000659
660 assert(Tok.is(tok::l_paren) && "Expected '('!");
661 SourceLocation LParenLoc = ConsumeParen();
662
Sebastian Redla55e52c2008-11-25 22:21:31 +0000663 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000664 CommaLocsTy CommaLocs;
665
666 if (Tok.isNot(tok::r_paren)) {
667 if (ParseExpressionList(Exprs, CommaLocs)) {
668 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000669 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000670 }
671 }
672
673 // Match the ')'.
674 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
675
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000676 // TypeRep could be null, if it references an invalid typedef.
677 if (!TypeRep)
678 return ExprError();
679
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000680 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
681 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000682 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
683 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000684 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000685}
686
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000687/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000688///
689/// condition:
690/// expression
691/// type-specifier-seq declarator '=' assignment-expression
692/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
693/// '=' assignment-expression
694///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000695/// \param ExprResult if the condition was parsed as an expression, the
696/// parsed expression.
697///
698/// \param DeclResult if the condition was parsed as a declaration, the
699/// parsed declaration.
700///
701/// \returns true if there was a parsing, false otherwise.
702bool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
703 DeclPtrTy &DeclResult) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000704 if (Tok.is(tok::code_completion)) {
705 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Condition);
706 ConsumeToken();
707 }
708
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000709 if (!isCXXConditionDeclaration()) {
710 ExprResult = ParseExpression(); // expression
711 DeclResult = DeclPtrTy();
712 return ExprResult.isInvalid();
713 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000714
715 // type-specifier-seq
716 DeclSpec DS;
717 ParseSpecifierQualifierList(DS);
718
719 // declarator
720 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
721 ParseDeclarator(DeclaratorInfo);
722
723 // simple-asm-expr[opt]
724 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000725 SourceLocation Loc;
726 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000727 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000728 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000729 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000730 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000731 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000732 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000733 }
734
735 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000736 if (Tok.is(tok::kw___attribute)) {
737 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000738 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000739 DeclaratorInfo.AddAttributes(AttrList, Loc);
740 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000741
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000742 // Type-check the declaration itself.
743 Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope,
744 DeclaratorInfo);
745 DeclResult = Dcl.get();
746 ExprResult = ExprError();
747
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000748 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000749 if (Tok.is(tok::equal)) {
750 SourceLocation EqualLoc = ConsumeToken();
751 OwningExprResult AssignExpr(ParseAssignmentExpression());
752 if (!AssignExpr.isInvalid())
753 Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
754 } else {
755 // FIXME: C++0x allows a braced-init-list
756 Diag(Tok, diag::err_expected_equal_after_declarator);
757 }
758
759 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000760}
761
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000762/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
763/// This should only be called when the current token is known to be part of
764/// simple-type-specifier.
765///
766/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000767/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000768/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
769/// char
770/// wchar_t
771/// bool
772/// short
773/// int
774/// long
775/// signed
776/// unsigned
777/// float
778/// double
779/// void
780/// [GNU] typeof-specifier
781/// [C++0x] auto [TODO]
782///
783/// type-name:
784/// class-name
785/// enum-name
786/// typedef-name
787///
788void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
789 DS.SetRangeStart(Tok.getLocation());
790 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000791 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000792 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000794 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000795 case tok::identifier: // foo::bar
796 case tok::coloncolon: // ::foo::bar
797 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000798 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000799 assert(0 && "Not a simple-type-specifier token!");
800 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000801
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000802 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000803 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000804 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000805 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000806 break;
807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000809 // builtin types
810 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000811 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000812 break;
813 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000814 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000815 break;
816 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000817 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000818 break;
819 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000820 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000821 break;
822 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000823 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000824 break;
825 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000826 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000827 break;
828 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000829 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000830 break;
831 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000832 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000833 break;
834 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000835 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000836 break;
837 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000838 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000839 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000840 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000841 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000842 break;
843 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000844 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000845 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000846 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000847 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000848 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000850 // GNU typeof support.
851 case tok::kw_typeof:
852 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000853 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000854 return;
855 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000856 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000857 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
858 else
859 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000860 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000861 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000862}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000863
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000864/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
865/// [dcl.name]), which is a non-empty sequence of type-specifiers,
866/// e.g., "const short int". Note that the DeclSpec is *not* finished
867/// by parsing the type-specifier-seq, because these sequences are
868/// typically followed by some form of declarator. Returns true and
869/// emits diagnostics if this is not a type-specifier-seq, false
870/// otherwise.
871///
872/// type-specifier-seq: [C++ 8.1]
873/// type-specifier type-specifier-seq[opt]
874///
875bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
876 DS.SetRangeStart(Tok.getLocation());
877 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000878 unsigned DiagID;
879 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000880
881 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000882 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
883 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000884 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000885 return true;
886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Sebastian Redld9bafa72010-02-03 21:21:43 +0000888 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
889 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
890 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000891
Douglas Gregor396a9f22010-02-24 23:13:13 +0000892 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000893 return false;
894}
895
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000896/// \brief Finish parsing a C++ unqualified-id that is a template-id of
897/// some form.
898///
899/// This routine is invoked when a '<' is encountered after an identifier or
900/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
901/// whether the unqualified-id is actually a template-id. This routine will
902/// then parse the template arguments and form the appropriate template-id to
903/// return to the caller.
904///
905/// \param SS the nested-name-specifier that precedes this template-id, if
906/// we're actually parsing a qualified-id.
907///
908/// \param Name for constructor and destructor names, this is the actual
909/// identifier that may be a template-name.
910///
911/// \param NameLoc the location of the class-name in a constructor or
912/// destructor.
913///
914/// \param EnteringContext whether we're entering the scope of the
915/// nested-name-specifier.
916///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000917/// \param ObjectType if this unqualified-id occurs within a member access
918/// expression, the type of the base object whose member is being accessed.
919///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000920/// \param Id as input, describes the template-name or operator-function-id
921/// that precedes the '<'. If template arguments were parsed successfully,
922/// will be updated with the template-id.
923///
Douglas Gregord4dca082010-02-24 18:44:31 +0000924/// \param AssumeTemplateId When true, this routine will assume that the name
925/// refers to a template without performing name lookup to verify.
926///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000927/// \returns true if a parse error occurred, false otherwise.
928bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
929 IdentifierInfo *Name,
930 SourceLocation NameLoc,
931 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000932 TypeTy *ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +0000933 UnqualifiedId &Id,
934 bool AssumeTemplateId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000935 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
936
937 TemplateTy Template;
938 TemplateNameKind TNK = TNK_Non_template;
939 switch (Id.getKind()) {
940 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000941 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +0000942 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +0000943 if (AssumeTemplateId) {
944 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
945 Id, ObjectType,
946 EnteringContext);
947 TNK = TNK_Dependent_template_name;
948 if (!Template.get())
949 return true;
950 } else
951 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType,
952 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000953 break;
954
Douglas Gregor014e88d2009-11-03 23:16:33 +0000955 case UnqualifiedId::IK_ConstructorName: {
956 UnqualifiedId TemplateName;
957 TemplateName.setIdentifier(Name, NameLoc);
958 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
959 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000960 break;
961 }
962
Douglas Gregor014e88d2009-11-03 23:16:33 +0000963 case UnqualifiedId::IK_DestructorName: {
964 UnqualifiedId TemplateName;
965 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000966 if (ObjectType) {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000967 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
Douglas Gregora481edb2009-11-20 23:39:24 +0000968 TemplateName, ObjectType,
969 EnteringContext);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000970 TNK = TNK_Dependent_template_name;
971 if (!Template.get())
972 return true;
973 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000974 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000975 EnteringContext, Template);
976
977 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000978 Diag(NameLoc, diag::err_destructor_template_id)
979 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000980 return true;
981 }
982 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000983 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000984 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000985
986 default:
987 return false;
988 }
989
990 if (TNK == TNK_Non_template)
991 return false;
992
993 // Parse the enclosed template argument list.
994 SourceLocation LAngleLoc, RAngleLoc;
995 TemplateArgList TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000996 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
997 &SS, true, LAngleLoc,
998 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000999 RAngleLoc))
1000 return true;
1001
1002 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001003 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1004 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001005 // Form a parsed representation of the template-id to be stored in the
1006 // UnqualifiedId.
1007 TemplateIdAnnotation *TemplateId
1008 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1009
1010 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1011 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001012 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001013 TemplateId->TemplateNameLoc = Id.StartLocation;
1014 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001015 TemplateId->Name = 0;
1016 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1017 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001018 }
1019
1020 TemplateId->Template = Template.getAs<void*>();
1021 TemplateId->Kind = TNK;
1022 TemplateId->LAngleLoc = LAngleLoc;
1023 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001024 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001025 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001026 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001027 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001028
1029 Id.setTemplateId(TemplateId);
1030 return false;
1031 }
1032
1033 // Bundle the template arguments together.
1034 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001035 TemplateArgs.size());
1036
1037 // Constructor and destructor names.
1038 Action::TypeResult Type
1039 = Actions.ActOnTemplateIdType(Template, NameLoc,
1040 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001041 RAngleLoc);
1042 if (Type.isInvalid())
1043 return true;
1044
1045 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1046 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1047 else
1048 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1049
1050 return false;
1051}
1052
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001053/// \brief Parse an operator-function-id or conversion-function-id as part
1054/// of a C++ unqualified-id.
1055///
1056/// This routine is responsible only for parsing the operator-function-id or
1057/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001058///
1059/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001060/// operator-function-id: [C++ 13.5]
1061/// 'operator' operator
1062///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001063/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001064/// new delete new[] delete[]
1065/// + - * / % ^ & | ~
1066/// ! = < > += -= *= /= %=
1067/// ^= &= |= << >> >>= <<= == !=
1068/// <= >= && || ++ -- , ->* ->
1069/// () []
1070///
1071/// conversion-function-id: [C++ 12.3.2]
1072/// operator conversion-type-id
1073///
1074/// conversion-type-id:
1075/// type-specifier-seq conversion-declarator[opt]
1076///
1077/// conversion-declarator:
1078/// ptr-operator conversion-declarator[opt]
1079/// \endcode
1080///
1081/// \param The nested-name-specifier that preceded this unqualified-id. If
1082/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1083///
1084/// \param EnteringContext whether we are entering the scope of the
1085/// nested-name-specifier.
1086///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001087/// \param ObjectType if this unqualified-id occurs within a member access
1088/// expression, the type of the base object whose member is being accessed.
1089///
1090/// \param Result on a successful parse, contains the parsed unqualified-id.
1091///
1092/// \returns true if parsing fails, false otherwise.
1093bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1094 TypeTy *ObjectType,
1095 UnqualifiedId &Result) {
1096 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1097
1098 // Consume the 'operator' keyword.
1099 SourceLocation KeywordLoc = ConsumeToken();
1100
1101 // Determine what kind of operator name we have.
1102 unsigned SymbolIdx = 0;
1103 SourceLocation SymbolLocations[3];
1104 OverloadedOperatorKind Op = OO_None;
1105 switch (Tok.getKind()) {
1106 case tok::kw_new:
1107 case tok::kw_delete: {
1108 bool isNew = Tok.getKind() == tok::kw_new;
1109 // Consume the 'new' or 'delete'.
1110 SymbolLocations[SymbolIdx++] = ConsumeToken();
1111 if (Tok.is(tok::l_square)) {
1112 // Consume the '['.
1113 SourceLocation LBracketLoc = ConsumeBracket();
1114 // Consume the ']'.
1115 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1116 LBracketLoc);
1117 if (RBracketLoc.isInvalid())
1118 return true;
1119
1120 SymbolLocations[SymbolIdx++] = LBracketLoc;
1121 SymbolLocations[SymbolIdx++] = RBracketLoc;
1122 Op = isNew? OO_Array_New : OO_Array_Delete;
1123 } else {
1124 Op = isNew? OO_New : OO_Delete;
1125 }
1126 break;
1127 }
1128
1129#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1130 case tok::Token: \
1131 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1132 Op = OO_##Name; \
1133 break;
1134#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1135#include "clang/Basic/OperatorKinds.def"
1136
1137 case tok::l_paren: {
1138 // Consume the '('.
1139 SourceLocation LParenLoc = ConsumeParen();
1140 // Consume the ')'.
1141 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1142 LParenLoc);
1143 if (RParenLoc.isInvalid())
1144 return true;
1145
1146 SymbolLocations[SymbolIdx++] = LParenLoc;
1147 SymbolLocations[SymbolIdx++] = RParenLoc;
1148 Op = OO_Call;
1149 break;
1150 }
1151
1152 case tok::l_square: {
1153 // Consume the '['.
1154 SourceLocation LBracketLoc = ConsumeBracket();
1155 // Consume the ']'.
1156 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1157 LBracketLoc);
1158 if (RBracketLoc.isInvalid())
1159 return true;
1160
1161 SymbolLocations[SymbolIdx++] = LBracketLoc;
1162 SymbolLocations[SymbolIdx++] = RBracketLoc;
1163 Op = OO_Subscript;
1164 break;
1165 }
1166
1167 case tok::code_completion: {
1168 // Code completion for the operator name.
1169 Actions.CodeCompleteOperatorName(CurScope);
1170
1171 // Consume the operator token.
1172 ConsumeToken();
1173
1174 // Don't try to parse any further.
1175 return true;
1176 }
1177
1178 default:
1179 break;
1180 }
1181
1182 if (Op != OO_None) {
1183 // We have parsed an operator-function-id.
1184 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1185 return false;
1186 }
Sean Hunt0486d742009-11-28 04:44:28 +00001187
1188 // Parse a literal-operator-id.
1189 //
1190 // literal-operator-id: [C++0x 13.5.8]
1191 // operator "" identifier
1192
1193 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1194 if (Tok.getLength() != 2)
1195 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1196 ConsumeStringToken();
1197
1198 if (Tok.isNot(tok::identifier)) {
1199 Diag(Tok.getLocation(), diag::err_expected_ident);
1200 return true;
1201 }
1202
1203 IdentifierInfo *II = Tok.getIdentifierInfo();
1204 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001205 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001206 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001207
1208 // Parse a conversion-function-id.
1209 //
1210 // conversion-function-id: [C++ 12.3.2]
1211 // operator conversion-type-id
1212 //
1213 // conversion-type-id:
1214 // type-specifier-seq conversion-declarator[opt]
1215 //
1216 // conversion-declarator:
1217 // ptr-operator conversion-declarator[opt]
1218
1219 // Parse the type-specifier-seq.
1220 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001221 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001222 return true;
1223
1224 // Parse the conversion-declarator, which is merely a sequence of
1225 // ptr-operators.
1226 Declarator D(DS, Declarator::TypeNameContext);
1227 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1228
1229 // Finish up the type.
1230 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1231 if (Ty.isInvalid())
1232 return true;
1233
1234 // Note that this is a conversion-function-id.
1235 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1236 D.getSourceRange().getEnd());
1237 return false;
1238}
1239
1240/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1241/// name of an entity.
1242///
1243/// \code
1244/// unqualified-id: [C++ expr.prim.general]
1245/// identifier
1246/// operator-function-id
1247/// conversion-function-id
1248/// [C++0x] literal-operator-id [TODO]
1249/// ~ class-name
1250/// template-id
1251///
1252/// \endcode
1253///
1254/// \param The nested-name-specifier that preceded this unqualified-id. If
1255/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1256///
1257/// \param EnteringContext whether we are entering the scope of the
1258/// nested-name-specifier.
1259///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001260/// \param AllowDestructorName whether we allow parsing of a destructor name.
1261///
1262/// \param AllowConstructorName whether we allow parsing a constructor name.
1263///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001264/// \param ObjectType if this unqualified-id occurs within a member access
1265/// expression, the type of the base object whose member is being accessed.
1266///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001267/// \param Result on a successful parse, contains the parsed unqualified-id.
1268///
1269/// \returns true if parsing fails, false otherwise.
1270bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1271 bool AllowDestructorName,
1272 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001273 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001274 UnqualifiedId &Result) {
1275 // unqualified-id:
1276 // identifier
1277 // template-id (when it hasn't already been annotated)
1278 if (Tok.is(tok::identifier)) {
1279 // Consume the identifier.
1280 IdentifierInfo *Id = Tok.getIdentifierInfo();
1281 SourceLocation IdLoc = ConsumeToken();
1282
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001283 if (!getLang().CPlusPlus) {
1284 // If we're not in C++, only identifiers matter. Record the
1285 // identifier and return.
1286 Result.setIdentifier(Id, IdLoc);
1287 return false;
1288 }
1289
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001290 if (AllowConstructorName &&
1291 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1292 // We have parsed a constructor name.
1293 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1294 &SS, false),
1295 IdLoc, IdLoc);
1296 } else {
1297 // We have parsed an identifier.
1298 Result.setIdentifier(Id, IdLoc);
1299 }
1300
1301 // If the next token is a '<', we may have a template.
1302 if (Tok.is(tok::less))
1303 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001304 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001305
1306 return false;
1307 }
1308
1309 // unqualified-id:
1310 // template-id (already parsed and annotated)
1311 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001312 TemplateIdAnnotation *TemplateId
1313 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1314
1315 // If the template-name names the current class, then this is a constructor
1316 if (AllowConstructorName && TemplateId->Name &&
1317 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
1318 if (SS.isSet()) {
1319 // C++ [class.qual]p2 specifies that a qualified template-name
1320 // is taken as the constructor name where a constructor can be
1321 // declared. Thus, the template arguments are extraneous, so
1322 // complain about them and remove them entirely.
1323 Diag(TemplateId->TemplateNameLoc,
1324 diag::err_out_of_line_constructor_template_id)
1325 << TemplateId->Name
1326 << CodeModificationHint::CreateRemoval(
1327 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1328 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1329 TemplateId->TemplateNameLoc,
1330 CurScope,
1331 &SS, false),
1332 TemplateId->TemplateNameLoc,
1333 TemplateId->RAngleLoc);
1334 TemplateId->Destroy();
1335 ConsumeToken();
1336 return false;
1337 }
1338
1339 Result.setConstructorTemplateId(TemplateId);
1340 ConsumeToken();
1341 return false;
1342 }
1343
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001344 // We have already parsed a template-id; consume the annotation token as
1345 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001346 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001347 ConsumeToken();
1348 return false;
1349 }
1350
1351 // unqualified-id:
1352 // operator-function-id
1353 // conversion-function-id
1354 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001355 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001356 return true;
1357
Sean Hunte6252d12009-11-28 08:58:14 +00001358 // If we have an operator-function-id or a literal-operator-id and the next
1359 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001360 //
1361 // template-id:
1362 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001363 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1364 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001365 Tok.is(tok::less))
1366 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1367 EnteringContext, ObjectType,
1368 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001369
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001370 return false;
1371 }
1372
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001373 if (getLang().CPlusPlus &&
1374 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001375 // C++ [expr.unary.op]p10:
1376 // There is an ambiguity in the unary-expression ~X(), where X is a
1377 // class-name. The ambiguity is resolved in favor of treating ~ as a
1378 // unary complement rather than treating ~X as referring to a destructor.
1379
1380 // Parse the '~'.
1381 SourceLocation TildeLoc = ConsumeToken();
1382
1383 // Parse the class-name.
1384 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001385 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001386 return true;
1387 }
1388
1389 // Parse the class-name (or template-name in a simple-template-id).
1390 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1391 SourceLocation ClassNameLoc = ConsumeToken();
1392
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001393 if (Tok.is(tok::less)) {
1394 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1395 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1396 EnteringContext, ObjectType, Result);
1397 }
1398
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001399 // Note that this is a destructor name.
Douglas Gregor124b8782010-02-16 19:09:40 +00001400 Action::TypeTy *Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1401 ClassNameLoc, CurScope,
1402 SS, ObjectType,
1403 EnteringContext);
1404 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001405 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001406
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001407 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001408 return false;
1409 }
1410
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001411 Diag(Tok, diag::err_expected_unqualified_id)
1412 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001413 return true;
1414}
1415
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001416/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1417/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001418///
Chris Lattner59232d32009-01-04 21:25:24 +00001419/// This method is called to parse the new expression after the optional :: has
1420/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1421/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001422///
1423/// new-expression:
1424/// '::'[opt] 'new' new-placement[opt] new-type-id
1425/// new-initializer[opt]
1426/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1427/// new-initializer[opt]
1428///
1429/// new-placement:
1430/// '(' expression-list ')'
1431///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001432/// new-type-id:
1433/// type-specifier-seq new-declarator[opt]
1434///
1435/// new-declarator:
1436/// ptr-operator new-declarator[opt]
1437/// direct-new-declarator
1438///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001439/// new-initializer:
1440/// '(' expression-list[opt] ')'
1441/// [C++0x] braced-init-list [TODO]
1442///
Chris Lattner59232d32009-01-04 21:25:24 +00001443Parser::OwningExprResult
1444Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1445 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1446 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001447
1448 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1449 // second form of new-expression. It can't be a new-type-id.
1450
Sebastian Redla55e52c2008-11-25 22:21:31 +00001451 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001452 SourceLocation PlacementLParen, PlacementRParen;
1453
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001454 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001455 DeclSpec DS;
1456 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001457 if (Tok.is(tok::l_paren)) {
1458 // If it turns out to be a placement, we change the type location.
1459 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001460 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1461 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001462 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001463 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001464
1465 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001466 if (PlacementRParen.isInvalid()) {
1467 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001468 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001469 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001470
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001471 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001472 // Reset the placement locations. There was no placement.
1473 PlacementLParen = PlacementRParen = SourceLocation();
1474 ParenTypeId = true;
1475 } else {
1476 // We still need the type.
1477 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001478 SourceLocation LParen = ConsumeParen();
1479 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001480 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001481 ParseDeclarator(DeclaratorInfo);
1482 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001483 ParenTypeId = true;
1484 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001485 if (ParseCXXTypeSpecifierSeq(DS))
1486 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001487 else {
1488 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001489 ParseDeclaratorInternal(DeclaratorInfo,
1490 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001491 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001492 ParenTypeId = false;
1493 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001494 }
1495 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001496 // A new-type-id is a simplified type-id, where essentially the
1497 // direct-declarator is replaced by a direct-new-declarator.
1498 if (ParseCXXTypeSpecifierSeq(DS))
1499 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001500 else {
1501 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001502 ParseDeclaratorInternal(DeclaratorInfo,
1503 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001504 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001505 ParenTypeId = false;
1506 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001507 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001508 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001509 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001510 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001511
Sebastian Redla55e52c2008-11-25 22:21:31 +00001512 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001513 SourceLocation ConstructorLParen, ConstructorRParen;
1514
1515 if (Tok.is(tok::l_paren)) {
1516 ConstructorLParen = ConsumeParen();
1517 if (Tok.isNot(tok::r_paren)) {
1518 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001519 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1520 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001521 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001522 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001523 }
1524 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001525 if (ConstructorRParen.isInvalid()) {
1526 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001527 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001528 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001529 }
1530
Sebastian Redlf53597f2009-03-15 17:47:39 +00001531 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1532 move_arg(PlacementArgs), PlacementRParen,
1533 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1534 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001535}
1536
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001537/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1538/// passed to ParseDeclaratorInternal.
1539///
1540/// direct-new-declarator:
1541/// '[' expression ']'
1542/// direct-new-declarator '[' constant-expression ']'
1543///
Chris Lattner59232d32009-01-04 21:25:24 +00001544void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001545 // Parse the array dimensions.
1546 bool first = true;
1547 while (Tok.is(tok::l_square)) {
1548 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001549 OwningExprResult Size(first ? ParseExpression()
1550 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001551 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001552 // Recover
1553 SkipUntil(tok::r_square);
1554 return;
1555 }
1556 first = false;
1557
Sebastian Redlab197ba2009-02-09 18:23:29 +00001558 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001559 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001560 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001561 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001562
Sebastian Redlab197ba2009-02-09 18:23:29 +00001563 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001564 return;
1565 }
1566}
1567
1568/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1569/// This ambiguity appears in the syntax of the C++ new operator.
1570///
1571/// new-expression:
1572/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1573/// new-initializer[opt]
1574///
1575/// new-placement:
1576/// '(' expression-list ')'
1577///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001578bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001579 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001580 // The '(' was already consumed.
1581 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001582 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001583 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001584 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001585 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001586 }
1587
1588 // It's not a type, it has to be an expression list.
1589 // Discard the comma locations - ActOnCXXNew has enough parameters.
1590 CommaLocsTy CommaLocs;
1591 return ParseExpressionList(PlacementArgs, CommaLocs);
1592}
1593
1594/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1595/// to free memory allocated by new.
1596///
Chris Lattner59232d32009-01-04 21:25:24 +00001597/// This method is called to parse the 'delete' expression after the optional
1598/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1599/// and "Start" is its location. Otherwise, "Start" is the location of the
1600/// 'delete' token.
1601///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001602/// delete-expression:
1603/// '::'[opt] 'delete' cast-expression
1604/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001605Parser::OwningExprResult
1606Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1607 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1608 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001609
1610 // Array delete?
1611 bool ArrayDelete = false;
1612 if (Tok.is(tok::l_square)) {
1613 ArrayDelete = true;
1614 SourceLocation LHS = ConsumeBracket();
1615 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1616 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001617 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001618 }
1619
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001620 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001621 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001622 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001623
Sebastian Redlf53597f2009-03-15 17:47:39 +00001624 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001625}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001626
Mike Stump1eb44332009-09-09 15:08:12 +00001627static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001628 switch(kind) {
1629 default: assert(false && "Not a known unary type trait.");
1630 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1631 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1632 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1633 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1634 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1635 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1636 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1637 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1638 case tok::kw___is_abstract: return UTT_IsAbstract;
1639 case tok::kw___is_class: return UTT_IsClass;
1640 case tok::kw___is_empty: return UTT_IsEmpty;
1641 case tok::kw___is_enum: return UTT_IsEnum;
1642 case tok::kw___is_pod: return UTT_IsPOD;
1643 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1644 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001645 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001646 }
1647}
1648
1649/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1650/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1651/// templates.
1652///
1653/// primary-expression:
1654/// [GNU] unary-type-trait '(' type-id ')'
1655///
Mike Stump1eb44332009-09-09 15:08:12 +00001656Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001657 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1658 SourceLocation Loc = ConsumeToken();
1659
1660 SourceLocation LParen = Tok.getLocation();
1661 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1662 return ExprError();
1663
1664 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1665 // there will be cryptic errors about mismatched parentheses and missing
1666 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001667 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001668
1669 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1670
Douglas Gregor809070a2009-02-18 17:45:20 +00001671 if (Ty.isInvalid())
1672 return ExprError();
1673
1674 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001675}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001676
1677/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1678/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1679/// based on the context past the parens.
1680Parser::OwningExprResult
1681Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1682 TypeTy *&CastTy,
1683 SourceLocation LParenLoc,
1684 SourceLocation &RParenLoc) {
1685 assert(getLang().CPlusPlus && "Should only be called for C++!");
1686 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1687 assert(isTypeIdInParens() && "Not a type-id!");
1688
1689 OwningExprResult Result(Actions, true);
1690 CastTy = 0;
1691
1692 // We need to disambiguate a very ugly part of the C++ syntax:
1693 //
1694 // (T())x; - type-id
1695 // (T())*x; - type-id
1696 // (T())/x; - expression
1697 // (T()); - expression
1698 //
1699 // The bad news is that we cannot use the specialized tentative parser, since
1700 // it can only verify that the thing inside the parens can be parsed as
1701 // type-id, it is not useful for determining the context past the parens.
1702 //
1703 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001704 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001705 //
1706 // It uses a scheme similar to parsing inline methods. The parenthesized
1707 // tokens are cached, the context that follows is determined (possibly by
1708 // parsing a cast-expression), and then we re-introduce the cached tokens
1709 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001710
Mike Stump1eb44332009-09-09 15:08:12 +00001711 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001712 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001713
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001714 // Store the tokens of the parentheses. We will parse them after we determine
1715 // the context that follows them.
1716 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1717 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001718 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1719 return ExprError();
1720 }
1721
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001722 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001723 ParseAs = CompoundLiteral;
1724 } else {
1725 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001726 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1727 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1728 NotCastExpr = true;
1729 } else {
1730 // Try parsing the cast-expression that may follow.
1731 // If it is not a cast-expression, NotCastExpr will be true and no token
1732 // will be consumed.
1733 Result = ParseCastExpression(false/*isUnaryExpression*/,
1734 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001735 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001736 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001737
1738 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1739 // an expression.
1740 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001741 }
1742
Mike Stump1eb44332009-09-09 15:08:12 +00001743 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001744 Toks.push_back(Tok);
1745 // Re-enter the stored parenthesized tokens into the token stream, so we may
1746 // parse them now.
1747 PP.EnterTokenStream(Toks.data(), Toks.size(),
1748 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1749 // Drop the current token and bring the first cached one. It's the same token
1750 // as when we entered this function.
1751 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001752
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001753 if (ParseAs >= CompoundLiteral) {
1754 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001755
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001756 // Match the ')'.
1757 if (Tok.is(tok::r_paren))
1758 RParenLoc = ConsumeParen();
1759 else
1760 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001761
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001762 if (ParseAs == CompoundLiteral) {
1763 ExprType = CompoundLiteral;
1764 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1765 }
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001767 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1768 assert(ParseAs == CastExpr);
1769
1770 if (Ty.isInvalid())
1771 return ExprError();
1772
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001773 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001774
1775 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001776 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001777 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001778 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001779 return move(Result);
1780 }
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001782 // Not a compound literal, and not followed by a cast-expression.
1783 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001784
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001785 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001786 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001787 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1788 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1789
1790 // Match the ')'.
1791 if (Result.isInvalid()) {
1792 SkipUntil(tok::r_paren);
1793 return ExprError();
1794 }
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001796 if (Tok.is(tok::r_paren))
1797 RParenLoc = ConsumeParen();
1798 else
1799 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1800
1801 return move(Result);
1802}