blob: a8fbaff01591dd49736d0a133fbfc824a077d596 [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 Gregorb10cd042010-02-21 18:36:56 +000048/// \param InMemberAccessExpr Whether this scope specifier is within a
49/// member access expression, e.g., the \p T:: in \p p->T::m.
50///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000052bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000053 Action::TypeTy *ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +000054 bool EnteringContext,
55 bool InMemberAccessExpr) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000056 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000057 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000058
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000059 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000060 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000061 SS.setRange(Tok.getAnnotationRange());
62 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000063 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000064 }
Chris Lattnere607e802009-01-04 21:14:15 +000065
Douglas Gregor39a8de12009-02-25 19:37:18 +000066 bool HasScopeSpecifier = false;
67
Chris Lattner5b454732009-01-05 03:55:46 +000068 if (Tok.is(tok::coloncolon)) {
69 // ::new and ::delete aren't nested-name-specifiers.
70 tok::TokenKind NextKind = NextToken().getKind();
71 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
72 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner55a7cef2009-01-05 00:13:00 +000074 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000075 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000076 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000077 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000078 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000079 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000080 }
81
Douglas Gregor39a8de12009-02-25 19:37:18 +000082 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000083 if (HasScopeSpecifier) {
84 // C++ [basic.lookup.classref]p5:
85 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000086 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000087 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000088 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000089 // the class-name-or-namespace-name is looked up in global scope as a
90 // class-name or namespace-name.
91 //
92 // To implement this, we clear out the object type as soon as we've
93 // seen a leading '::' or part of a nested-name-specifier.
94 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +000095
96 if (Tok.is(tok::code_completion)) {
97 // Code completion for a nested-name-specifier, where the code
98 // code completion token follows the '::'.
99 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
100 ConsumeToken();
101 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000102 }
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Douglas Gregor39a8de12009-02-25 19:37:18 +0000104 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000105 // nested-name-specifier 'template'[opt] simple-template-id '::'
106
107 // Parse the optional 'template' keyword, then make sure we have
108 // 'identifier <' after it.
109 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000110 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000111 // nested-name-specifier, since they aren't allowed to start with
112 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000113 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000114 break;
115
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000116 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000117 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000118
119 UnqualifiedId TemplateName;
120 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000121 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000122 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000123 ConsumeToken();
124 } else if (Tok.is(tok::kw_operator)) {
125 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000126 TemplateName)) {
127 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000128 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000129 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000130
Sean Hunte6252d12009-11-28 08:58:14 +0000131 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
132 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000133 Diag(TemplateName.getSourceRange().getBegin(),
134 diag::err_id_after_template_in_nested_name_spec)
135 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000136 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000137 break;
138 }
139 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000140 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000141 break;
142 }
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000144 // If the next token is not '<', we have a qualified-id that refers
145 // to a template name, such as T::template apply, but is not a
146 // template-id.
147 if (Tok.isNot(tok::less)) {
148 TPA.Revert();
149 break;
150 }
151
152 // Commit to parsing the template-id.
153 TPA.Commit();
Mike Stump1eb44332009-09-09 15:08:12 +0000154 TemplateTy Template
Douglas Gregor014e88d2009-11-03 23:16:33 +0000155 = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
Douglas Gregora481edb2009-11-20 23:39:24 +0000156 ObjectType, EnteringContext);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000157 if (!Template)
158 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000159 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000160 &SS, TemplateName, TemplateKWLoc, false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000161 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattner77cf72a2009-06-26 03:47:46 +0000163 continue;
164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Douglas Gregor39a8de12009-02-25 19:37:18 +0000166 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000167 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000168 //
169 // simple-template-id '::'
170 //
171 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000172 // right kind (it should name a type or be dependent), and then
173 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000174 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000175 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb10cd042010-02-21 18:36:56 +0000176 bool MayBePseudoDestructor
177 = InMemberAccessExpr && GetLookAheadToken(2).is(tok::tilde);
Mike Stump1eb44332009-09-09 15:08:12 +0000178 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000179 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000180 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000181
Mike Stump1eb44332009-09-09 15:08:12 +0000182 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000183 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000184 Token TypeToken = Tok;
185 ConsumeToken();
186 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
187 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Douglas Gregor39a8de12009-02-25 19:37:18 +0000189 if (!HasScopeSpecifier) {
190 SS.setBeginLoc(TypeToken.getLocation());
191 HasScopeSpecifier = true;
192 }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Douglas Gregor31a19b62009-04-01 21:51:26 +0000194 if (TypeToken.getAnnotationValue())
195 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000196 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000197 TypeToken.getAnnotationValue(),
198 TypeToken.getAnnotationRange(),
Douglas Gregorb10cd042010-02-21 18:36:56 +0000199 CCLoc,
200 MayBePseudoDestructor));
Douglas Gregor31a19b62009-04-01 21:51:26 +0000201 else
202 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000203 SS.setEndLoc(CCLoc);
204 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Chris Lattner67b9e832009-06-26 03:45:46 +0000207 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000208 }
209
Chris Lattner5c7f7862009-06-26 03:52:38 +0000210
211 // The rest of the nested-name-specifier possibilities start with
212 // tok::identifier.
213 if (Tok.isNot(tok::identifier))
214 break;
215
216 IdentifierInfo &II = *Tok.getIdentifierInfo();
217
218 // nested-name-specifier:
219 // type-name '::'
220 // namespace-name '::'
221 // nested-name-specifier identifier '::'
222 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000223
224 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
225 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000226 if (Next.is(tok::colon) && !ColonIsSacred) {
227 bool MayBePseudoDestructor
228 = InMemberAccessExpr && GetLookAheadToken(2).is(tok::tilde);
Chris Lattner46646492009-12-07 01:36:53 +0000229
Douglas Gregorb10cd042010-02-21 18:36:56 +0000230 if (Actions.IsInvalidUnlessNestedName(CurScope, SS, II,
231 MayBePseudoDestructor, ObjectType,
232 EnteringContext) &&
233 // If the token after the colon isn't an identifier, it's still an
234 // error, but they probably meant something else strange so don't
235 // recover like this.
236 PP.LookAhead(1).is(tok::identifier)) {
237 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
238 << CodeModificationHint::CreateReplacement(Next.getLocation(), "::");
239
240 // Recover as if the user wrote '::'.
241 Next.setKind(tok::coloncolon);
242 }
Chris Lattner46646492009-12-07 01:36:53 +0000243 }
244
Chris Lattner5c7f7862009-06-26 03:52:38 +0000245 if (Next.is(tok::coloncolon)) {
246 // We have an identifier followed by a '::'. Lookup this name
247 // as the name in a nested-name-specifier.
248 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000249 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
250 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000251 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Chris Lattner5c7f7862009-06-26 03:52:38 +0000253 if (!HasScopeSpecifier) {
254 SS.setBeginLoc(IdLoc);
255 HasScopeSpecifier = true;
256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner5c7f7862009-06-26 03:52:38 +0000258 if (SS.isInvalid())
259 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Douglas Gregorb10cd042010-02-21 18:36:56 +0000261 bool MayBePseudoDestructor = InMemberAccessExpr && Tok.is(tok::tilde);
262
Chris Lattner5c7f7862009-06-26 03:52:38 +0000263 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000264 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000265 MayBePseudoDestructor, ObjectType,
266 EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000267 SS.setEndLoc(CCLoc);
268 continue;
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattner5c7f7862009-06-26 03:52:38 +0000271 // nested-name-specifier:
272 // type-name '<'
273 if (Next.is(tok::less)) {
274 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000275 UnqualifiedId TemplateName;
276 TemplateName.setIdentifier(&II, Tok.getLocation());
277 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
278 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000279 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000280 EnteringContext,
281 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000282 // We have found a template name, so annotate this this token
283 // with a template-id annotation. We do not permit the
284 // template-id to be translated into a type annotation,
285 // because some clients (e.g., the parsing of class template
286 // specializations) still want to see the original template-id
287 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000288 ConsumeToken();
289 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
290 SourceLocation(), false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000291 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000292 continue;
293 }
294 }
295
Douglas Gregor39a8de12009-02-25 19:37:18 +0000296 // We don't have any tokens that form the beginning of a
297 // nested-name-specifier, so we're done.
298 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000299 }
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Douglas Gregor39a8de12009-02-25 19:37:18 +0000301 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000302}
303
304/// ParseCXXIdExpression - Handle id-expression.
305///
306/// id-expression:
307/// unqualified-id
308/// qualified-id
309///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000310/// qualified-id:
311/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
312/// '::' identifier
313/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000314/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000315///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000316/// NOTE: The standard specifies that, for qualified-id, the parser does not
317/// expect:
318///
319/// '::' conversion-function-id
320/// '::' '~' class-name
321///
322/// This may cause a slight inconsistency on diagnostics:
323///
324/// class C {};
325/// namespace A {}
326/// void f() {
327/// :: A :: ~ C(); // Some Sema error about using destructor with a
328/// // namespace.
329/// :: ~ C(); // Some Parser error like 'unexpected ~'.
330/// }
331///
332/// We simplify the parser a bit and make it work like:
333///
334/// qualified-id:
335/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
336/// '::' unqualified-id
337///
338/// That way Sema can handle and report similar errors for namespaces and the
339/// global scope.
340///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000341/// The isAddressOfOperand parameter indicates that this id-expression is a
342/// direct operand of the address-of operator. This is, besides member contexts,
343/// the only place where a qualified-id naming a non-static class member may
344/// appear.
345///
346Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000347 // qualified-id:
348 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
349 // '::' unqualified-id
350 //
351 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000352 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000353
354 UnqualifiedId Name;
355 if (ParseUnqualifiedId(SS,
356 /*EnteringContext=*/false,
357 /*AllowDestructorName=*/false,
358 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000359 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000360 Name))
361 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000362
363 // This is only the direct operand of an & operator if it is not
364 // followed by a postfix-expression suffix.
365 if (isAddressOfOperand) {
366 switch (Tok.getKind()) {
367 case tok::l_square:
368 case tok::l_paren:
369 case tok::arrow:
370 case tok::period:
371 case tok::plusplus:
372 case tok::minusminus:
373 isAddressOfOperand = false;
374 break;
375
376 default:
377 break;
378 }
379 }
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000380
381 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
382 isAddressOfOperand);
383
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000384}
385
Reid Spencer5f016e22007-07-11 17:01:13 +0000386/// ParseCXXCasts - This handles the various ways to cast expressions to another
387/// type.
388///
389/// postfix-expression: [C++ 5.2p1]
390/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
391/// 'static_cast' '<' type-name '>' '(' expression ')'
392/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
393/// 'const_cast' '<' type-name '>' '(' expression ')'
394///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000395Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 tok::TokenKind Kind = Tok.getKind();
397 const char *CastName = 0; // For error messages
398
399 switch (Kind) {
400 default: assert(0 && "Unknown C++ cast!"); abort();
401 case tok::kw_const_cast: CastName = "const_cast"; break;
402 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
403 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
404 case tok::kw_static_cast: CastName = "static_cast"; break;
405 }
406
407 SourceLocation OpLoc = ConsumeToken();
408 SourceLocation LAngleBracketLoc = Tok.getLocation();
409
410 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000411 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000412
Douglas Gregor809070a2009-02-18 17:45:20 +0000413 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 SourceLocation RAngleBracketLoc = Tok.getLocation();
415
Chris Lattner1ab3b962008-11-18 07:48:38 +0000416 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000417 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000418
419 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
420
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000421 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
422 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000423
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000424 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000426 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000427 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000428
Douglas Gregor809070a2009-02-18 17:45:20 +0000429 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000430 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000431 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000432 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000433 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000434
Sebastian Redl20df9b72008-12-11 22:51:44 +0000435 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000436}
437
Sebastian Redlc42e1182008-11-11 11:37:55 +0000438/// ParseCXXTypeid - This handles the C++ typeid expression.
439///
440/// postfix-expression: [C++ 5.2p1]
441/// 'typeid' '(' expression ')'
442/// 'typeid' '(' type-id ')'
443///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000444Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000445 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
446
447 SourceLocation OpLoc = ConsumeToken();
448 SourceLocation LParenLoc = Tok.getLocation();
449 SourceLocation RParenLoc;
450
451 // typeid expressions are always parenthesized.
452 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
453 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000454 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000455
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000456 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000457
458 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000459 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000460
461 // Match the ')'.
462 MatchRHSPunctuation(tok::r_paren, LParenLoc);
463
Douglas Gregor809070a2009-02-18 17:45:20 +0000464 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000465 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000466
467 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000468 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000469 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000470 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000471 // When typeid is applied to an expression other than an lvalue of a
472 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000473 // operand (Clause 5).
474 //
Mike Stump1eb44332009-09-09 15:08:12 +0000475 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000476 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000477 // we the expression is potentially potentially evaluated.
478 EnterExpressionEvaluationContext Unevaluated(Actions,
479 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000480 Result = ParseExpression();
481
482 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000483 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000484 SkipUntil(tok::r_paren);
485 else {
486 MatchRHSPunctuation(tok::r_paren, LParenLoc);
487
488 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000489 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000490 }
491 }
492
Sebastian Redl20df9b72008-12-11 22:51:44 +0000493 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000494}
495
Reid Spencer5f016e22007-07-11 17:01:13 +0000496/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
497///
498/// boolean-literal: [C++ 2.13.5]
499/// 'true'
500/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000501Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000503 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000504}
Chris Lattner50dd2892008-02-26 00:51:44 +0000505
506/// ParseThrowExpression - This handles the C++ throw expression.
507///
508/// throw-expression: [C++ 15]
509/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000510Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000511 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000512 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000513
Chris Lattner2a2819a2008-04-06 06:02:23 +0000514 // If the current token isn't the start of an assignment-expression,
515 // then the expression is not present. This handles things like:
516 // "C ? throw : (void)42", which is crazy but legal.
517 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
518 case tok::semi:
519 case tok::r_paren:
520 case tok::r_square:
521 case tok::r_brace:
522 case tok::colon:
523 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000524 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000525
Chris Lattner2a2819a2008-04-06 06:02:23 +0000526 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000527 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000528 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000529 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000530 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000531}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000532
533/// ParseCXXThis - This handles the C++ 'this' pointer.
534///
535/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
536/// a non-lvalue expression whose value is the address of the object for which
537/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000538Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000539 assert(Tok.is(tok::kw_this) && "Not 'this'!");
540 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000541 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000542}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000543
544/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
545/// Can be interpreted either as function-style casting ("int(x)")
546/// or class type construction ("ClassType(x,y,z)")
547/// or creation of a value-initialized type ("int()").
548///
549/// postfix-expression: [C++ 5.2p1]
550/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
551/// typename-specifier '(' expression-list[opt] ')' [TODO]
552///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000553Parser::OwningExprResult
554Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000555 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000556 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000557
558 assert(Tok.is(tok::l_paren) && "Expected '('!");
559 SourceLocation LParenLoc = ConsumeParen();
560
Sebastian Redla55e52c2008-11-25 22:21:31 +0000561 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000562 CommaLocsTy CommaLocs;
563
564 if (Tok.isNot(tok::r_paren)) {
565 if (ParseExpressionList(Exprs, CommaLocs)) {
566 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000567 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000568 }
569 }
570
571 // Match the ')'.
572 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
573
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000574 // TypeRep could be null, if it references an invalid typedef.
575 if (!TypeRep)
576 return ExprError();
577
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000578 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
579 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000580 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
581 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000582 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000583}
584
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000585/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000586///
587/// condition:
588/// expression
589/// type-specifier-seq declarator '=' assignment-expression
590/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
591/// '=' assignment-expression
592///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000593/// \param ExprResult if the condition was parsed as an expression, the
594/// parsed expression.
595///
596/// \param DeclResult if the condition was parsed as a declaration, the
597/// parsed declaration.
598///
599/// \returns true if there was a parsing, false otherwise.
600bool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
601 DeclPtrTy &DeclResult) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000602 if (Tok.is(tok::code_completion)) {
603 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Condition);
604 ConsumeToken();
605 }
606
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000607 if (!isCXXConditionDeclaration()) {
608 ExprResult = ParseExpression(); // expression
609 DeclResult = DeclPtrTy();
610 return ExprResult.isInvalid();
611 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000612
613 // type-specifier-seq
614 DeclSpec DS;
615 ParseSpecifierQualifierList(DS);
616
617 // declarator
618 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
619 ParseDeclarator(DeclaratorInfo);
620
621 // simple-asm-expr[opt]
622 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000623 SourceLocation Loc;
624 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000625 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000626 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000627 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000628 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000629 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000630 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000631 }
632
633 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000634 if (Tok.is(tok::kw___attribute)) {
635 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000636 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000637 DeclaratorInfo.AddAttributes(AttrList, Loc);
638 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000639
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000640 // Type-check the declaration itself.
641 Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope,
642 DeclaratorInfo);
643 DeclResult = Dcl.get();
644 ExprResult = ExprError();
645
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000646 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000647 if (Tok.is(tok::equal)) {
648 SourceLocation EqualLoc = ConsumeToken();
649 OwningExprResult AssignExpr(ParseAssignmentExpression());
650 if (!AssignExpr.isInvalid())
651 Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
652 } else {
653 // FIXME: C++0x allows a braced-init-list
654 Diag(Tok, diag::err_expected_equal_after_declarator);
655 }
656
657 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000658}
659
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000660/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
661/// This should only be called when the current token is known to be part of
662/// simple-type-specifier.
663///
664/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000665/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000666/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
667/// char
668/// wchar_t
669/// bool
670/// short
671/// int
672/// long
673/// signed
674/// unsigned
675/// float
676/// double
677/// void
678/// [GNU] typeof-specifier
679/// [C++0x] auto [TODO]
680///
681/// type-name:
682/// class-name
683/// enum-name
684/// typedef-name
685///
686void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
687 DS.SetRangeStart(Tok.getLocation());
688 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000689 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000690 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000692 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000693 case tok::identifier: // foo::bar
694 case tok::coloncolon: // ::foo::bar
695 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000696 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000697 assert(0 && "Not a simple-type-specifier token!");
698 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000699
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000700 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000701 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000702 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000703 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000704 break;
705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000707 // builtin types
708 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000709 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000710 break;
711 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000712 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000713 break;
714 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000715 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000716 break;
717 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000718 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000719 break;
720 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000721 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000722 break;
723 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000724 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000725 break;
726 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000727 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000728 break;
729 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000730 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000731 break;
732 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000733 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000734 break;
735 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000736 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000737 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000738 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000739 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000740 break;
741 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000742 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000743 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000744 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000745 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000746 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000748 // GNU typeof support.
749 case tok::kw_typeof:
750 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000751 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000752 return;
753 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000754 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000755 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
756 else
757 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000758 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000759 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000760}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000761
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000762/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
763/// [dcl.name]), which is a non-empty sequence of type-specifiers,
764/// e.g., "const short int". Note that the DeclSpec is *not* finished
765/// by parsing the type-specifier-seq, because these sequences are
766/// typically followed by some form of declarator. Returns true and
767/// emits diagnostics if this is not a type-specifier-seq, false
768/// otherwise.
769///
770/// type-specifier-seq: [C++ 8.1]
771/// type-specifier type-specifier-seq[opt]
772///
773bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
774 DS.SetRangeStart(Tok.getLocation());
775 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000776 unsigned DiagID;
777 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000778
779 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000780 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
781 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000782 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000783 return true;
784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Sebastian Redld9bafa72010-02-03 21:21:43 +0000786 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
787 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
788 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000789
790 return false;
791}
792
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000793/// \brief Finish parsing a C++ unqualified-id that is a template-id of
794/// some form.
795///
796/// This routine is invoked when a '<' is encountered after an identifier or
797/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
798/// whether the unqualified-id is actually a template-id. This routine will
799/// then parse the template arguments and form the appropriate template-id to
800/// return to the caller.
801///
802/// \param SS the nested-name-specifier that precedes this template-id, if
803/// we're actually parsing a qualified-id.
804///
805/// \param Name for constructor and destructor names, this is the actual
806/// identifier that may be a template-name.
807///
808/// \param NameLoc the location of the class-name in a constructor or
809/// destructor.
810///
811/// \param EnteringContext whether we're entering the scope of the
812/// nested-name-specifier.
813///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000814/// \param ObjectType if this unqualified-id occurs within a member access
815/// expression, the type of the base object whose member is being accessed.
816///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000817/// \param Id as input, describes the template-name or operator-function-id
818/// that precedes the '<'. If template arguments were parsed successfully,
819/// will be updated with the template-id.
820///
821/// \returns true if a parse error occurred, false otherwise.
822bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
823 IdentifierInfo *Name,
824 SourceLocation NameLoc,
825 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000826 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000827 UnqualifiedId &Id) {
828 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
829
830 TemplateTy Template;
831 TemplateNameKind TNK = TNK_Non_template;
832 switch (Id.getKind()) {
833 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000834 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +0000835 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000836 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
837 Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000838 break;
839
Douglas Gregor014e88d2009-11-03 23:16:33 +0000840 case UnqualifiedId::IK_ConstructorName: {
841 UnqualifiedId TemplateName;
842 TemplateName.setIdentifier(Name, NameLoc);
843 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
844 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000845 break;
846 }
847
Douglas Gregor014e88d2009-11-03 23:16:33 +0000848 case UnqualifiedId::IK_DestructorName: {
849 UnqualifiedId TemplateName;
850 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000851 if (ObjectType) {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000852 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
Douglas Gregora481edb2009-11-20 23:39:24 +0000853 TemplateName, ObjectType,
854 EnteringContext);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000855 TNK = TNK_Dependent_template_name;
856 if (!Template.get())
857 return true;
858 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000859 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000860 EnteringContext, Template);
861
862 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000863 Diag(NameLoc, diag::err_destructor_template_id)
864 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000865 return true;
866 }
867 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000868 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000869 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000870
871 default:
872 return false;
873 }
874
875 if (TNK == TNK_Non_template)
876 return false;
877
878 // Parse the enclosed template argument list.
879 SourceLocation LAngleLoc, RAngleLoc;
880 TemplateArgList TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000881 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
882 &SS, true, LAngleLoc,
883 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000884 RAngleLoc))
885 return true;
886
887 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +0000888 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
889 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000890 // Form a parsed representation of the template-id to be stored in the
891 // UnqualifiedId.
892 TemplateIdAnnotation *TemplateId
893 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
894
895 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
896 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000897 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000898 TemplateId->TemplateNameLoc = Id.StartLocation;
899 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000900 TemplateId->Name = 0;
901 TemplateId->Operator = Id.OperatorFunctionId.Operator;
902 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000903 }
904
905 TemplateId->Template = Template.getAs<void*>();
906 TemplateId->Kind = TNK;
907 TemplateId->LAngleLoc = LAngleLoc;
908 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +0000909 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000910 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +0000911 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000912 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000913
914 Id.setTemplateId(TemplateId);
915 return false;
916 }
917
918 // Bundle the template arguments together.
919 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000920 TemplateArgs.size());
921
922 // Constructor and destructor names.
923 Action::TypeResult Type
924 = Actions.ActOnTemplateIdType(Template, NameLoc,
925 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000926 RAngleLoc);
927 if (Type.isInvalid())
928 return true;
929
930 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
931 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
932 else
933 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
934
935 return false;
936}
937
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000938/// \brief Parse an operator-function-id or conversion-function-id as part
939/// of a C++ unqualified-id.
940///
941/// This routine is responsible only for parsing the operator-function-id or
942/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000943///
944/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000945/// operator-function-id: [C++ 13.5]
946/// 'operator' operator
947///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000948/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000949/// new delete new[] delete[]
950/// + - * / % ^ & | ~
951/// ! = < > += -= *= /= %=
952/// ^= &= |= << >> >>= <<= == !=
953/// <= >= && || ++ -- , ->* ->
954/// () []
955///
956/// conversion-function-id: [C++ 12.3.2]
957/// operator conversion-type-id
958///
959/// conversion-type-id:
960/// type-specifier-seq conversion-declarator[opt]
961///
962/// conversion-declarator:
963/// ptr-operator conversion-declarator[opt]
964/// \endcode
965///
966/// \param The nested-name-specifier that preceded this unqualified-id. If
967/// non-empty, then we are parsing the unqualified-id of a qualified-id.
968///
969/// \param EnteringContext whether we are entering the scope of the
970/// nested-name-specifier.
971///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000972/// \param ObjectType if this unqualified-id occurs within a member access
973/// expression, the type of the base object whose member is being accessed.
974///
975/// \param Result on a successful parse, contains the parsed unqualified-id.
976///
977/// \returns true if parsing fails, false otherwise.
978bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
979 TypeTy *ObjectType,
980 UnqualifiedId &Result) {
981 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
982
983 // Consume the 'operator' keyword.
984 SourceLocation KeywordLoc = ConsumeToken();
985
986 // Determine what kind of operator name we have.
987 unsigned SymbolIdx = 0;
988 SourceLocation SymbolLocations[3];
989 OverloadedOperatorKind Op = OO_None;
990 switch (Tok.getKind()) {
991 case tok::kw_new:
992 case tok::kw_delete: {
993 bool isNew = Tok.getKind() == tok::kw_new;
994 // Consume the 'new' or 'delete'.
995 SymbolLocations[SymbolIdx++] = ConsumeToken();
996 if (Tok.is(tok::l_square)) {
997 // Consume the '['.
998 SourceLocation LBracketLoc = ConsumeBracket();
999 // Consume the ']'.
1000 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1001 LBracketLoc);
1002 if (RBracketLoc.isInvalid())
1003 return true;
1004
1005 SymbolLocations[SymbolIdx++] = LBracketLoc;
1006 SymbolLocations[SymbolIdx++] = RBracketLoc;
1007 Op = isNew? OO_Array_New : OO_Array_Delete;
1008 } else {
1009 Op = isNew? OO_New : OO_Delete;
1010 }
1011 break;
1012 }
1013
1014#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1015 case tok::Token: \
1016 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1017 Op = OO_##Name; \
1018 break;
1019#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1020#include "clang/Basic/OperatorKinds.def"
1021
1022 case tok::l_paren: {
1023 // Consume the '('.
1024 SourceLocation LParenLoc = ConsumeParen();
1025 // Consume the ')'.
1026 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1027 LParenLoc);
1028 if (RParenLoc.isInvalid())
1029 return true;
1030
1031 SymbolLocations[SymbolIdx++] = LParenLoc;
1032 SymbolLocations[SymbolIdx++] = RParenLoc;
1033 Op = OO_Call;
1034 break;
1035 }
1036
1037 case tok::l_square: {
1038 // Consume the '['.
1039 SourceLocation LBracketLoc = ConsumeBracket();
1040 // Consume the ']'.
1041 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1042 LBracketLoc);
1043 if (RBracketLoc.isInvalid())
1044 return true;
1045
1046 SymbolLocations[SymbolIdx++] = LBracketLoc;
1047 SymbolLocations[SymbolIdx++] = RBracketLoc;
1048 Op = OO_Subscript;
1049 break;
1050 }
1051
1052 case tok::code_completion: {
1053 // Code completion for the operator name.
1054 Actions.CodeCompleteOperatorName(CurScope);
1055
1056 // Consume the operator token.
1057 ConsumeToken();
1058
1059 // Don't try to parse any further.
1060 return true;
1061 }
1062
1063 default:
1064 break;
1065 }
1066
1067 if (Op != OO_None) {
1068 // We have parsed an operator-function-id.
1069 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1070 return false;
1071 }
Sean Hunt0486d742009-11-28 04:44:28 +00001072
1073 // Parse a literal-operator-id.
1074 //
1075 // literal-operator-id: [C++0x 13.5.8]
1076 // operator "" identifier
1077
1078 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1079 if (Tok.getLength() != 2)
1080 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1081 ConsumeStringToken();
1082
1083 if (Tok.isNot(tok::identifier)) {
1084 Diag(Tok.getLocation(), diag::err_expected_ident);
1085 return true;
1086 }
1087
1088 IdentifierInfo *II = Tok.getIdentifierInfo();
1089 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001090 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001091 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001092
1093 // Parse a conversion-function-id.
1094 //
1095 // conversion-function-id: [C++ 12.3.2]
1096 // operator conversion-type-id
1097 //
1098 // conversion-type-id:
1099 // type-specifier-seq conversion-declarator[opt]
1100 //
1101 // conversion-declarator:
1102 // ptr-operator conversion-declarator[opt]
1103
1104 // Parse the type-specifier-seq.
1105 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001106 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001107 return true;
1108
1109 // Parse the conversion-declarator, which is merely a sequence of
1110 // ptr-operators.
1111 Declarator D(DS, Declarator::TypeNameContext);
1112 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1113
1114 // Finish up the type.
1115 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1116 if (Ty.isInvalid())
1117 return true;
1118
1119 // Note that this is a conversion-function-id.
1120 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1121 D.getSourceRange().getEnd());
1122 return false;
1123}
1124
1125/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1126/// name of an entity.
1127///
1128/// \code
1129/// unqualified-id: [C++ expr.prim.general]
1130/// identifier
1131/// operator-function-id
1132/// conversion-function-id
1133/// [C++0x] literal-operator-id [TODO]
1134/// ~ class-name
1135/// template-id
1136///
1137/// \endcode
1138///
1139/// \param The nested-name-specifier that preceded this unqualified-id. If
1140/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1141///
1142/// \param EnteringContext whether we are entering the scope of the
1143/// nested-name-specifier.
1144///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001145/// \param AllowDestructorName whether we allow parsing of a destructor name.
1146///
1147/// \param AllowConstructorName whether we allow parsing a constructor name.
1148///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001149/// \param ObjectType if this unqualified-id occurs within a member access
1150/// expression, the type of the base object whose member is being accessed.
1151///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001152/// \param Result on a successful parse, contains the parsed unqualified-id.
1153///
1154/// \returns true if parsing fails, false otherwise.
1155bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1156 bool AllowDestructorName,
1157 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001158 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001159 UnqualifiedId &Result) {
1160 // unqualified-id:
1161 // identifier
1162 // template-id (when it hasn't already been annotated)
1163 if (Tok.is(tok::identifier)) {
1164 // Consume the identifier.
1165 IdentifierInfo *Id = Tok.getIdentifierInfo();
1166 SourceLocation IdLoc = ConsumeToken();
1167
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001168 if (!getLang().CPlusPlus) {
1169 // If we're not in C++, only identifiers matter. Record the
1170 // identifier and return.
1171 Result.setIdentifier(Id, IdLoc);
1172 return false;
1173 }
1174
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001175 if (AllowConstructorName &&
1176 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1177 // We have parsed a constructor name.
1178 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1179 &SS, false),
1180 IdLoc, IdLoc);
1181 } else {
1182 // We have parsed an identifier.
1183 Result.setIdentifier(Id, IdLoc);
1184 }
1185
1186 // If the next token is a '<', we may have a template.
1187 if (Tok.is(tok::less))
1188 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001189 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001190
1191 return false;
1192 }
1193
1194 // unqualified-id:
1195 // template-id (already parsed and annotated)
1196 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001197 TemplateIdAnnotation *TemplateId
1198 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1199
1200 // If the template-name names the current class, then this is a constructor
1201 if (AllowConstructorName && TemplateId->Name &&
1202 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
1203 if (SS.isSet()) {
1204 // C++ [class.qual]p2 specifies that a qualified template-name
1205 // is taken as the constructor name where a constructor can be
1206 // declared. Thus, the template arguments are extraneous, so
1207 // complain about them and remove them entirely.
1208 Diag(TemplateId->TemplateNameLoc,
1209 diag::err_out_of_line_constructor_template_id)
1210 << TemplateId->Name
1211 << CodeModificationHint::CreateRemoval(
1212 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1213 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1214 TemplateId->TemplateNameLoc,
1215 CurScope,
1216 &SS, false),
1217 TemplateId->TemplateNameLoc,
1218 TemplateId->RAngleLoc);
1219 TemplateId->Destroy();
1220 ConsumeToken();
1221 return false;
1222 }
1223
1224 Result.setConstructorTemplateId(TemplateId);
1225 ConsumeToken();
1226 return false;
1227 }
1228
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001229 // We have already parsed a template-id; consume the annotation token as
1230 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001231 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001232 ConsumeToken();
1233 return false;
1234 }
1235
1236 // unqualified-id:
1237 // operator-function-id
1238 // conversion-function-id
1239 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001240 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001241 return true;
1242
Sean Hunte6252d12009-11-28 08:58:14 +00001243 // If we have an operator-function-id or a literal-operator-id and the next
1244 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001245 //
1246 // template-id:
1247 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001248 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1249 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001250 Tok.is(tok::less))
1251 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1252 EnteringContext, ObjectType,
1253 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001254
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001255 return false;
1256 }
1257
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001258 if (getLang().CPlusPlus &&
1259 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001260 // C++ [expr.unary.op]p10:
1261 // There is an ambiguity in the unary-expression ~X(), where X is a
1262 // class-name. The ambiguity is resolved in favor of treating ~ as a
1263 // unary complement rather than treating ~X as referring to a destructor.
1264
1265 // Parse the '~'.
1266 SourceLocation TildeLoc = ConsumeToken();
1267
1268 // Parse the class-name.
1269 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001270 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001271 return true;
1272 }
1273
1274 // Parse the class-name (or template-name in a simple-template-id).
1275 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1276 SourceLocation ClassNameLoc = ConsumeToken();
1277
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001278 if (Tok.is(tok::less)) {
1279 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1280 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1281 EnteringContext, ObjectType, Result);
1282 }
1283
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001284 // Note that this is a destructor name.
Douglas Gregor124b8782010-02-16 19:09:40 +00001285 Action::TypeTy *Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1286 ClassNameLoc, CurScope,
1287 SS, ObjectType,
1288 EnteringContext);
1289 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001290 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001291
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001292 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001293 return false;
1294 }
1295
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001296 Diag(Tok, diag::err_expected_unqualified_id)
1297 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001298 return true;
1299}
1300
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001301/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1302/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001303///
Chris Lattner59232d32009-01-04 21:25:24 +00001304/// This method is called to parse the new expression after the optional :: has
1305/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1306/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001307///
1308/// new-expression:
1309/// '::'[opt] 'new' new-placement[opt] new-type-id
1310/// new-initializer[opt]
1311/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1312/// new-initializer[opt]
1313///
1314/// new-placement:
1315/// '(' expression-list ')'
1316///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001317/// new-type-id:
1318/// type-specifier-seq new-declarator[opt]
1319///
1320/// new-declarator:
1321/// ptr-operator new-declarator[opt]
1322/// direct-new-declarator
1323///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001324/// new-initializer:
1325/// '(' expression-list[opt] ')'
1326/// [C++0x] braced-init-list [TODO]
1327///
Chris Lattner59232d32009-01-04 21:25:24 +00001328Parser::OwningExprResult
1329Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1330 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1331 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001332
1333 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1334 // second form of new-expression. It can't be a new-type-id.
1335
Sebastian Redla55e52c2008-11-25 22:21:31 +00001336 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001337 SourceLocation PlacementLParen, PlacementRParen;
1338
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001339 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001340 DeclSpec DS;
1341 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001342 if (Tok.is(tok::l_paren)) {
1343 // If it turns out to be a placement, we change the type location.
1344 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001345 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1346 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001347 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001348 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001349
1350 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001351 if (PlacementRParen.isInvalid()) {
1352 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001353 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001354 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001355
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001356 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001357 // Reset the placement locations. There was no placement.
1358 PlacementLParen = PlacementRParen = SourceLocation();
1359 ParenTypeId = true;
1360 } else {
1361 // We still need the type.
1362 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001363 SourceLocation LParen = ConsumeParen();
1364 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001365 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001366 ParseDeclarator(DeclaratorInfo);
1367 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001368 ParenTypeId = true;
1369 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001370 if (ParseCXXTypeSpecifierSeq(DS))
1371 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001372 else {
1373 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001374 ParseDeclaratorInternal(DeclaratorInfo,
1375 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001376 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001377 ParenTypeId = false;
1378 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001379 }
1380 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001381 // A new-type-id is a simplified type-id, where essentially the
1382 // direct-declarator is replaced by a direct-new-declarator.
1383 if (ParseCXXTypeSpecifierSeq(DS))
1384 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001385 else {
1386 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001387 ParseDeclaratorInternal(DeclaratorInfo,
1388 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001389 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001390 ParenTypeId = false;
1391 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001392 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001393 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001394 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001395 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001396
Sebastian Redla55e52c2008-11-25 22:21:31 +00001397 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001398 SourceLocation ConstructorLParen, ConstructorRParen;
1399
1400 if (Tok.is(tok::l_paren)) {
1401 ConstructorLParen = ConsumeParen();
1402 if (Tok.isNot(tok::r_paren)) {
1403 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001404 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1405 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001406 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001407 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001408 }
1409 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001410 if (ConstructorRParen.isInvalid()) {
1411 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001412 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001413 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001414 }
1415
Sebastian Redlf53597f2009-03-15 17:47:39 +00001416 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1417 move_arg(PlacementArgs), PlacementRParen,
1418 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1419 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001420}
1421
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001422/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1423/// passed to ParseDeclaratorInternal.
1424///
1425/// direct-new-declarator:
1426/// '[' expression ']'
1427/// direct-new-declarator '[' constant-expression ']'
1428///
Chris Lattner59232d32009-01-04 21:25:24 +00001429void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001430 // Parse the array dimensions.
1431 bool first = true;
1432 while (Tok.is(tok::l_square)) {
1433 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001434 OwningExprResult Size(first ? ParseExpression()
1435 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001436 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001437 // Recover
1438 SkipUntil(tok::r_square);
1439 return;
1440 }
1441 first = false;
1442
Sebastian Redlab197ba2009-02-09 18:23:29 +00001443 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001444 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001445 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001446 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001447
Sebastian Redlab197ba2009-02-09 18:23:29 +00001448 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001449 return;
1450 }
1451}
1452
1453/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1454/// This ambiguity appears in the syntax of the C++ new operator.
1455///
1456/// new-expression:
1457/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1458/// new-initializer[opt]
1459///
1460/// new-placement:
1461/// '(' expression-list ')'
1462///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001463bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001464 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001465 // The '(' was already consumed.
1466 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001467 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001468 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001469 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001470 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001471 }
1472
1473 // It's not a type, it has to be an expression list.
1474 // Discard the comma locations - ActOnCXXNew has enough parameters.
1475 CommaLocsTy CommaLocs;
1476 return ParseExpressionList(PlacementArgs, CommaLocs);
1477}
1478
1479/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1480/// to free memory allocated by new.
1481///
Chris Lattner59232d32009-01-04 21:25:24 +00001482/// This method is called to parse the 'delete' expression after the optional
1483/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1484/// and "Start" is its location. Otherwise, "Start" is the location of the
1485/// 'delete' token.
1486///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001487/// delete-expression:
1488/// '::'[opt] 'delete' cast-expression
1489/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001490Parser::OwningExprResult
1491Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1492 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1493 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001494
1495 // Array delete?
1496 bool ArrayDelete = false;
1497 if (Tok.is(tok::l_square)) {
1498 ArrayDelete = true;
1499 SourceLocation LHS = ConsumeBracket();
1500 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1501 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001502 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001503 }
1504
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001505 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001506 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001507 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001508
Sebastian Redlf53597f2009-03-15 17:47:39 +00001509 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001510}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001511
Mike Stump1eb44332009-09-09 15:08:12 +00001512static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001513 switch(kind) {
1514 default: assert(false && "Not a known unary type trait.");
1515 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1516 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1517 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1518 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1519 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1520 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1521 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1522 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1523 case tok::kw___is_abstract: return UTT_IsAbstract;
1524 case tok::kw___is_class: return UTT_IsClass;
1525 case tok::kw___is_empty: return UTT_IsEmpty;
1526 case tok::kw___is_enum: return UTT_IsEnum;
1527 case tok::kw___is_pod: return UTT_IsPOD;
1528 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1529 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001530 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001531 }
1532}
1533
1534/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1535/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1536/// templates.
1537///
1538/// primary-expression:
1539/// [GNU] unary-type-trait '(' type-id ')'
1540///
Mike Stump1eb44332009-09-09 15:08:12 +00001541Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001542 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1543 SourceLocation Loc = ConsumeToken();
1544
1545 SourceLocation LParen = Tok.getLocation();
1546 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1547 return ExprError();
1548
1549 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1550 // there will be cryptic errors about mismatched parentheses and missing
1551 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001552 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001553
1554 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1555
Douglas Gregor809070a2009-02-18 17:45:20 +00001556 if (Ty.isInvalid())
1557 return ExprError();
1558
1559 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001560}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001561
1562/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1563/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1564/// based on the context past the parens.
1565Parser::OwningExprResult
1566Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1567 TypeTy *&CastTy,
1568 SourceLocation LParenLoc,
1569 SourceLocation &RParenLoc) {
1570 assert(getLang().CPlusPlus && "Should only be called for C++!");
1571 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1572 assert(isTypeIdInParens() && "Not a type-id!");
1573
1574 OwningExprResult Result(Actions, true);
1575 CastTy = 0;
1576
1577 // We need to disambiguate a very ugly part of the C++ syntax:
1578 //
1579 // (T())x; - type-id
1580 // (T())*x; - type-id
1581 // (T())/x; - expression
1582 // (T()); - expression
1583 //
1584 // The bad news is that we cannot use the specialized tentative parser, since
1585 // it can only verify that the thing inside the parens can be parsed as
1586 // type-id, it is not useful for determining the context past the parens.
1587 //
1588 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001589 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001590 //
1591 // It uses a scheme similar to parsing inline methods. The parenthesized
1592 // tokens are cached, the context that follows is determined (possibly by
1593 // parsing a cast-expression), and then we re-introduce the cached tokens
1594 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001595
Mike Stump1eb44332009-09-09 15:08:12 +00001596 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001597 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001598
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001599 // Store the tokens of the parentheses. We will parse them after we determine
1600 // the context that follows them.
1601 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1602 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001603 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1604 return ExprError();
1605 }
1606
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001607 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001608 ParseAs = CompoundLiteral;
1609 } else {
1610 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001611 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1612 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1613 NotCastExpr = true;
1614 } else {
1615 // Try parsing the cast-expression that may follow.
1616 // If it is not a cast-expression, NotCastExpr will be true and no token
1617 // will be consumed.
1618 Result = ParseCastExpression(false/*isUnaryExpression*/,
1619 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001620 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001621 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001622
1623 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1624 // an expression.
1625 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001626 }
1627
Mike Stump1eb44332009-09-09 15:08:12 +00001628 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001629 Toks.push_back(Tok);
1630 // Re-enter the stored parenthesized tokens into the token stream, so we may
1631 // parse them now.
1632 PP.EnterTokenStream(Toks.data(), Toks.size(),
1633 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1634 // Drop the current token and bring the first cached one. It's the same token
1635 // as when we entered this function.
1636 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001637
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001638 if (ParseAs >= CompoundLiteral) {
1639 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001640
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001641 // Match the ')'.
1642 if (Tok.is(tok::r_paren))
1643 RParenLoc = ConsumeParen();
1644 else
1645 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001646
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001647 if (ParseAs == CompoundLiteral) {
1648 ExprType = CompoundLiteral;
1649 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1650 }
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001652 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1653 assert(ParseAs == CastExpr);
1654
1655 if (Ty.isInvalid())
1656 return ExprError();
1657
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001658 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001659
1660 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001661 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001662 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001663 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001664 return move(Result);
1665 }
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001667 // Not a compound literal, and not followed by a cast-expression.
1668 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001669
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001670 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001671 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001672 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1673 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1674
1675 // Match the ')'.
1676 if (Result.isInvalid()) {
1677 SkipUntil(tok::r_paren);
1678 return ExprError();
1679 }
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001681 if (Tok.is(tok::r_paren))
1682 RParenLoc = ConsumeParen();
1683 else
1684 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1685
1686 return move(Result);
1687}