blob: 265d13a7e4f630ee447542bfd85d9fe726afa4c8 [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 Gregor3f9a0562009-11-03 01:35:08 +000017#include "llvm/Support/ErrorHandling.h"
18
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Mike Stump1eb44332009-09-09 15:08:12 +000021/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000022///
23/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000024/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000025/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000026///
27/// '::'[opt] nested-name-specifier
28/// '::'
29///
30/// nested-name-specifier:
31/// type-name '::'
32/// namespace-name '::'
33/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000034/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000035///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000036///
Mike Stump1eb44332009-09-09 15:08:12 +000037/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000038/// nested-name-specifier (or empty)
39///
Mike Stump1eb44332009-09-09 15:08:12 +000040/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// the "." or "->" of a member access expression, this parameter provides the
42/// type of the object whose members are being accessed.
43///
44/// \param EnteringContext whether we will be entering into the context of
45/// the nested-name-specifier after parsing it.
46///
47/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000048bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000049 Action::TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +000050 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000051 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000052 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000053
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000054 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000055 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000056 SS.setRange(Tok.getAnnotationRange());
57 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000058 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000059 }
Chris Lattnere607e802009-01-04 21:14:15 +000060
Douglas Gregor39a8de12009-02-25 19:37:18 +000061 bool HasScopeSpecifier = false;
62
Chris Lattner5b454732009-01-05 03:55:46 +000063 if (Tok.is(tok::coloncolon)) {
64 // ::new and ::delete aren't nested-name-specifiers.
65 tok::TokenKind NextKind = NextToken().getKind();
66 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
67 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000068
Chris Lattner55a7cef2009-01-05 00:13:00 +000069 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000070 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000071 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000072 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000073 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000074 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000075 }
76
Douglas Gregor39a8de12009-02-25 19:37:18 +000077 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000078 if (HasScopeSpecifier) {
79 // C++ [basic.lookup.classref]p5:
80 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000081 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000082 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000083 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000084 // the class-name-or-namespace-name is looked up in global scope as a
85 // class-name or namespace-name.
86 //
87 // To implement this, we clear out the object type as soon as we've
88 // seen a leading '::' or part of a nested-name-specifier.
89 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +000090
91 if (Tok.is(tok::code_completion)) {
92 // Code completion for a nested-name-specifier, where the code
93 // code completion token follows the '::'.
94 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
95 ConsumeToken();
96 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Douglas Gregor39a8de12009-02-25 19:37:18 +000099 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000100 // nested-name-specifier 'template'[opt] simple-template-id '::'
101
102 // Parse the optional 'template' keyword, then make sure we have
103 // 'identifier <' after it.
104 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000105 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000106 // nested-name-specifier, since they aren't allowed to start with
107 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000108 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000109 break;
110
Chris Lattner77cf72a2009-06-26 03:47:46 +0000111 SourceLocation TemplateKWLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattner77cf72a2009-06-26 03:47:46 +0000113 if (Tok.isNot(tok::identifier)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000114 Diag(Tok.getLocation(),
Chris Lattner77cf72a2009-06-26 03:47:46 +0000115 diag::err_id_after_template_in_nested_name_spec)
116 << SourceRange(TemplateKWLoc);
117 break;
118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner77cf72a2009-06-26 03:47:46 +0000120 if (NextToken().isNot(tok::less)) {
121 Diag(NextToken().getLocation(),
122 diag::err_less_after_template_name_in_nested_name_spec)
123 << Tok.getIdentifierInfo()->getName()
124 << SourceRange(TemplateKWLoc, Tok.getLocation());
125 break;
126 }
Mike Stump1eb44332009-09-09 15:08:12 +0000127
128 TemplateTy Template
Chris Lattner77cf72a2009-06-26 03:47:46 +0000129 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
130 *Tok.getIdentifierInfo(),
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000131 Tok.getLocation(), SS,
132 ObjectType);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000133 if (!Template)
134 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000135 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
136 &SS, TemplateKWLoc, false))
137 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Chris Lattner77cf72a2009-06-26 03:47:46 +0000139 continue;
140 }
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Douglas Gregor39a8de12009-02-25 19:37:18 +0000142 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000143 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000144 //
145 // simple-template-id '::'
146 //
147 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000148 // right kind (it should name a type or be dependent), and then
149 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000150 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000151 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
152
Mike Stump1eb44332009-09-09 15:08:12 +0000153 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000154 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000155 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000156
Mike Stump1eb44332009-09-09 15:08:12 +0000157 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000158 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000159 Token TypeToken = Tok;
160 ConsumeToken();
161 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
162 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Douglas Gregor39a8de12009-02-25 19:37:18 +0000164 if (!HasScopeSpecifier) {
165 SS.setBeginLoc(TypeToken.getLocation());
166 HasScopeSpecifier = true;
167 }
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Douglas Gregor31a19b62009-04-01 21:51:26 +0000169 if (TypeToken.getAnnotationValue())
170 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000171 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000172 TypeToken.getAnnotationValue(),
173 TypeToken.getAnnotationRange(),
174 CCLoc));
175 else
176 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000177 SS.setEndLoc(CCLoc);
178 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner67b9e832009-06-26 03:45:46 +0000181 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000182 }
183
Chris Lattner5c7f7862009-06-26 03:52:38 +0000184
185 // The rest of the nested-name-specifier possibilities start with
186 // tok::identifier.
187 if (Tok.isNot(tok::identifier))
188 break;
189
190 IdentifierInfo &II = *Tok.getIdentifierInfo();
191
192 // nested-name-specifier:
193 // type-name '::'
194 // namespace-name '::'
195 // nested-name-specifier identifier '::'
196 Token Next = NextToken();
197 if (Next.is(tok::coloncolon)) {
198 // We have an identifier followed by a '::'. Lookup this name
199 // as the name in a nested-name-specifier.
200 SourceLocation IdLoc = ConsumeToken();
201 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
202 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattner5c7f7862009-06-26 03:52:38 +0000204 if (!HasScopeSpecifier) {
205 SS.setBeginLoc(IdLoc);
206 HasScopeSpecifier = true;
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattner5c7f7862009-06-26 03:52:38 +0000209 if (SS.isInvalid())
210 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattner5c7f7862009-06-26 03:52:38 +0000212 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000213 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000214 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000215 SS.setEndLoc(CCLoc);
216 continue;
217 }
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Chris Lattner5c7f7862009-06-26 03:52:38 +0000219 // nested-name-specifier:
220 // type-name '<'
221 if (Next.is(tok::less)) {
222 TemplateTy Template;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000223 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, II,
224 Tok.getLocation(),
225 &SS,
226 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000227 EnteringContext,
228 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000229 // We have found a template name, so annotate this this token
230 // with a template-id annotation. We do not permit the
231 // template-id to be translated into a type annotation,
232 // because some clients (e.g., the parsing of class template
233 // specializations) still want to see the original template-id
234 // token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000235 if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(),
236 false))
237 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000238 continue;
239 }
240 }
241
Douglas Gregor39a8de12009-02-25 19:37:18 +0000242 // We don't have any tokens that form the beginning of a
243 // nested-name-specifier, so we're done.
244 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Douglas Gregor39a8de12009-02-25 19:37:18 +0000247 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000248}
249
250/// ParseCXXIdExpression - Handle id-expression.
251///
252/// id-expression:
253/// unqualified-id
254/// qualified-id
255///
256/// unqualified-id:
257/// identifier
258/// operator-function-id
259/// conversion-function-id [TODO]
260/// '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000261/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000262///
263/// qualified-id:
264/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
265/// '::' identifier
266/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000267/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000268///
269/// nested-name-specifier:
270/// type-name '::'
271/// namespace-name '::'
272/// nested-name-specifier identifier '::'
273/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
274///
275/// NOTE: The standard specifies that, for qualified-id, the parser does not
276/// expect:
277///
278/// '::' conversion-function-id
279/// '::' '~' class-name
280///
281/// This may cause a slight inconsistency on diagnostics:
282///
283/// class C {};
284/// namespace A {}
285/// void f() {
286/// :: A :: ~ C(); // Some Sema error about using destructor with a
287/// // namespace.
288/// :: ~ C(); // Some Parser error like 'unexpected ~'.
289/// }
290///
291/// We simplify the parser a bit and make it work like:
292///
293/// qualified-id:
294/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
295/// '::' unqualified-id
296///
297/// That way Sema can handle and report similar errors for namespaces and the
298/// global scope.
299///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000300/// The isAddressOfOperand parameter indicates that this id-expression is a
301/// direct operand of the address-of operator. This is, besides member contexts,
302/// the only place where a qualified-id naming a non-static class member may
303/// appear.
304///
305Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000306 // qualified-id:
307 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
308 // '::' unqualified-id
309 //
310 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000311 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000312
313 UnqualifiedId Name;
314 if (ParseUnqualifiedId(SS,
315 /*EnteringContext=*/false,
316 /*AllowDestructorName=*/false,
317 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000318 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000319 Name))
320 return ExprError();
321
322 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
323 isAddressOfOperand);
324
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000325}
326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327/// ParseCXXCasts - This handles the various ways to cast expressions to another
328/// type.
329///
330/// postfix-expression: [C++ 5.2p1]
331/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
332/// 'static_cast' '<' type-name '>' '(' expression ')'
333/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
334/// 'const_cast' '<' type-name '>' '(' expression ')'
335///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000336Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 tok::TokenKind Kind = Tok.getKind();
338 const char *CastName = 0; // For error messages
339
340 switch (Kind) {
341 default: assert(0 && "Unknown C++ cast!"); abort();
342 case tok::kw_const_cast: CastName = "const_cast"; break;
343 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
344 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
345 case tok::kw_static_cast: CastName = "static_cast"; break;
346 }
347
348 SourceLocation OpLoc = ConsumeToken();
349 SourceLocation LAngleBracketLoc = Tok.getLocation();
350
351 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000352 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000353
Douglas Gregor809070a2009-02-18 17:45:20 +0000354 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 SourceLocation RAngleBracketLoc = Tok.getLocation();
356
Chris Lattner1ab3b962008-11-18 07:48:38 +0000357 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000358 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000359
360 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
361
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000362 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
363 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000364
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000365 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000367 // Match the ')'.
368 if (Result.isInvalid())
369 SkipUntil(tok::r_paren);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000371 if (Tok.is(tok::r_paren))
372 RParenLoc = ConsumeParen();
373 else
374 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000375
Douglas Gregor809070a2009-02-18 17:45:20 +0000376 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000377 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000378 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000379 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000380 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000381
Sebastian Redl20df9b72008-12-11 22:51:44 +0000382 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000383}
384
Sebastian Redlc42e1182008-11-11 11:37:55 +0000385/// ParseCXXTypeid - This handles the C++ typeid expression.
386///
387/// postfix-expression: [C++ 5.2p1]
388/// 'typeid' '(' expression ')'
389/// 'typeid' '(' type-id ')'
390///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000391Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000392 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
393
394 SourceLocation OpLoc = ConsumeToken();
395 SourceLocation LParenLoc = Tok.getLocation();
396 SourceLocation RParenLoc;
397
398 // typeid expressions are always parenthesized.
399 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
400 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000401 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000402
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000403 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000404
405 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000406 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000407
408 // Match the ')'.
409 MatchRHSPunctuation(tok::r_paren, LParenLoc);
410
Douglas Gregor809070a2009-02-18 17:45:20 +0000411 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000412 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000413
414 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000415 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000416 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000417 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000418 // When typeid is applied to an expression other than an lvalue of a
419 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000420 // operand (Clause 5).
421 //
Mike Stump1eb44332009-09-09 15:08:12 +0000422 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000423 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000424 // we the expression is potentially potentially evaluated.
425 EnterExpressionEvaluationContext Unevaluated(Actions,
426 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000427 Result = ParseExpression();
428
429 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000430 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000431 SkipUntil(tok::r_paren);
432 else {
433 MatchRHSPunctuation(tok::r_paren, LParenLoc);
434
435 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000436 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000437 }
438 }
439
Sebastian Redl20df9b72008-12-11 22:51:44 +0000440 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000441}
442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
444///
445/// boolean-literal: [C++ 2.13.5]
446/// 'true'
447/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000448Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000450 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000451}
Chris Lattner50dd2892008-02-26 00:51:44 +0000452
453/// ParseThrowExpression - This handles the C++ throw expression.
454///
455/// throw-expression: [C++ 15]
456/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000457Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000458 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000459 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000460
Chris Lattner2a2819a2008-04-06 06:02:23 +0000461 // If the current token isn't the start of an assignment-expression,
462 // then the expression is not present. This handles things like:
463 // "C ? throw : (void)42", which is crazy but legal.
464 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
465 case tok::semi:
466 case tok::r_paren:
467 case tok::r_square:
468 case tok::r_brace:
469 case tok::colon:
470 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000471 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000472
Chris Lattner2a2819a2008-04-06 06:02:23 +0000473 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000474 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000475 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000476 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000477 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000478}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000479
480/// ParseCXXThis - This handles the C++ 'this' pointer.
481///
482/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
483/// a non-lvalue expression whose value is the address of the object for which
484/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000485Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000486 assert(Tok.is(tok::kw_this) && "Not 'this'!");
487 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000488 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000489}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000490
491/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
492/// Can be interpreted either as function-style casting ("int(x)")
493/// or class type construction ("ClassType(x,y,z)")
494/// or creation of a value-initialized type ("int()").
495///
496/// postfix-expression: [C++ 5.2p1]
497/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
498/// typename-specifier '(' expression-list[opt] ')' [TODO]
499///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000500Parser::OwningExprResult
501Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000502 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000503 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000504
505 assert(Tok.is(tok::l_paren) && "Expected '('!");
506 SourceLocation LParenLoc = ConsumeParen();
507
Sebastian Redla55e52c2008-11-25 22:21:31 +0000508 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000509 CommaLocsTy CommaLocs;
510
511 if (Tok.isNot(tok::r_paren)) {
512 if (ParseExpressionList(Exprs, CommaLocs)) {
513 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000514 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000515 }
516 }
517
518 // Match the ')'.
519 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
520
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000521 // TypeRep could be null, if it references an invalid typedef.
522 if (!TypeRep)
523 return ExprError();
524
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000525 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
526 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000527 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
528 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000529 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000530}
531
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000532/// ParseCXXCondition - if/switch/while/for condition expression.
533///
534/// condition:
535/// expression
536/// type-specifier-seq declarator '=' assignment-expression
537/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
538/// '=' assignment-expression
539///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000540Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000541 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000542 return ParseExpression(); // expression
543
544 SourceLocation StartLoc = Tok.getLocation();
545
546 // type-specifier-seq
547 DeclSpec DS;
548 ParseSpecifierQualifierList(DS);
549
550 // declarator
551 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
552 ParseDeclarator(DeclaratorInfo);
553
554 // simple-asm-expr[opt]
555 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000556 SourceLocation Loc;
557 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000558 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000559 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000560 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000561 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000562 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000563 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000564 }
565
566 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000567 if (Tok.is(tok::kw___attribute)) {
568 SourceLocation Loc;
569 AttributeList *AttrList = ParseAttributes(&Loc);
570 DeclaratorInfo.AddAttributes(AttrList, Loc);
571 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000572
573 // '=' assignment-expression
574 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000575 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000576 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000577 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000578 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000579 return ExprError();
580
Sebastian Redlf53597f2009-03-15 17:47:39 +0000581 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
582 DeclaratorInfo,EqualLoc,
583 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000584}
585
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000586/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
587/// This should only be called when the current token is known to be part of
588/// simple-type-specifier.
589///
590/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000591/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000592/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
593/// char
594/// wchar_t
595/// bool
596/// short
597/// int
598/// long
599/// signed
600/// unsigned
601/// float
602/// double
603/// void
604/// [GNU] typeof-specifier
605/// [C++0x] auto [TODO]
606///
607/// type-name:
608/// class-name
609/// enum-name
610/// typedef-name
611///
612void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
613 DS.SetRangeStart(Tok.getLocation());
614 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000615 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000616 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000618 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000619 case tok::identifier: // foo::bar
620 case tok::coloncolon: // ::foo::bar
621 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000622 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000623 assert(0 && "Not a simple-type-specifier token!");
624 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000625
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000626 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000627 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000628 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000629 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000630 break;
631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000633 // builtin types
634 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000635 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000636 break;
637 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000638 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000639 break;
640 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000641 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000642 break;
643 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000644 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000645 break;
646 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000647 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000648 break;
649 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000650 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000651 break;
652 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000653 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000654 break;
655 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000656 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000657 break;
658 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000659 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000660 break;
661 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000662 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000663 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000664 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000665 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000666 break;
667 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000668 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000669 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000670 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000671 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000672 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000674 // GNU typeof support.
675 case tok::kw_typeof:
676 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000677 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000678 return;
679 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000680 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000681 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
682 else
683 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000684 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000685 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000686}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000687
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000688/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
689/// [dcl.name]), which is a non-empty sequence of type-specifiers,
690/// e.g., "const short int". Note that the DeclSpec is *not* finished
691/// by parsing the type-specifier-seq, because these sequences are
692/// typically followed by some form of declarator. Returns true and
693/// emits diagnostics if this is not a type-specifier-seq, false
694/// otherwise.
695///
696/// type-specifier-seq: [C++ 8.1]
697/// type-specifier type-specifier-seq[opt]
698///
699bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
700 DS.SetRangeStart(Tok.getLocation());
701 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000702 unsigned DiagID;
703 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000704
705 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000706 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000707 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000708 return true;
709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
John McCallfec54012009-08-03 20:12:06 +0000711 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000712
713 return false;
714}
715
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000716/// \brief Finish parsing a C++ unqualified-id that is a template-id of
717/// some form.
718///
719/// This routine is invoked when a '<' is encountered after an identifier or
720/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
721/// whether the unqualified-id is actually a template-id. This routine will
722/// then parse the template arguments and form the appropriate template-id to
723/// return to the caller.
724///
725/// \param SS the nested-name-specifier that precedes this template-id, if
726/// we're actually parsing a qualified-id.
727///
728/// \param Name for constructor and destructor names, this is the actual
729/// identifier that may be a template-name.
730///
731/// \param NameLoc the location of the class-name in a constructor or
732/// destructor.
733///
734/// \param EnteringContext whether we're entering the scope of the
735/// nested-name-specifier.
736///
737/// \param Id as input, describes the template-name or operator-function-id
738/// that precedes the '<'. If template arguments were parsed successfully,
739/// will be updated with the template-id.
740///
741/// \returns true if a parse error occurred, false otherwise.
742bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
743 IdentifierInfo *Name,
744 SourceLocation NameLoc,
745 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000746 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000747 UnqualifiedId &Id) {
748 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
749
750 TemplateTy Template;
751 TemplateNameKind TNK = TNK_Non_template;
752 switch (Id.getKind()) {
753 case UnqualifiedId::IK_Identifier:
754 TNK = Actions.isTemplateName(CurScope, *Id.Identifier, Id.StartLocation,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000755 &SS, ObjectType, EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000756 break;
757
758 case UnqualifiedId::IK_OperatorFunctionId: {
759 // FIXME: Temporary hack: warn that we are completely ignoring the
760 // template arguments for now.
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000761 // Parse the enclosed template argument list and throw it away.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000762 SourceLocation LAngleLoc, RAngleLoc;
763 TemplateArgList TemplateArgs;
764 TemplateArgIsTypeList TemplateArgIsType;
765 TemplateArgLocationList TemplateArgLocations;
766 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
767 &SS, true, LAngleLoc,
768 TemplateArgs,
769 TemplateArgIsType,
770 TemplateArgLocations,
771 RAngleLoc))
772 return true;
773
774 Diag(Id.StartLocation, diag::warn_operator_template_id_ignores_args)
775 << SourceRange(LAngleLoc, RAngleLoc);
776 break;
777 }
778
779 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000780 TNK = Actions.isTemplateName(CurScope, *Name, NameLoc, &SS, ObjectType,
781 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000782 break;
783
784 case UnqualifiedId::IK_DestructorName:
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000785 if (ObjectType) {
786 Template = Actions.ActOnDependentTemplateName(SourceLocation(), *Name,
787 NameLoc, SS, ObjectType);
788 TNK = TNK_Dependent_template_name;
789 if (!Template.get())
790 return true;
791 } else {
792 TNK = Actions.isTemplateName(CurScope, *Name, NameLoc, &SS, ObjectType,
793 EnteringContext, Template);
794
795 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
796 // The identifier following the destructor did not refer to a template
797 // or to a type. Complain.
798 if (ObjectType)
799 Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
800 << Name;
801 else
802 Diag(NameLoc, diag::err_destructor_class_name);
803 return true;
804 }
805 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000806 break;
807
808 default:
809 return false;
810 }
811
812 if (TNK == TNK_Non_template)
813 return false;
814
815 // Parse the enclosed template argument list.
816 SourceLocation LAngleLoc, RAngleLoc;
817 TemplateArgList TemplateArgs;
818 TemplateArgIsTypeList TemplateArgIsType;
819 TemplateArgLocationList TemplateArgLocations;
820 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
821 &SS, true, LAngleLoc,
822 TemplateArgs,
823 TemplateArgIsType,
824 TemplateArgLocations,
825 RAngleLoc))
826 return true;
827
828 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
829 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) {
830 // Form a parsed representation of the template-id to be stored in the
831 // UnqualifiedId.
832 TemplateIdAnnotation *TemplateId
833 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
834
835 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
836 TemplateId->Name = Id.Identifier;
837 TemplateId->TemplateNameLoc = Id.StartLocation;
838 } else {
839 // FIXME: Handle IK_OperatorFunctionId
840 }
841
842 TemplateId->Template = Template.getAs<void*>();
843 TemplateId->Kind = TNK;
844 TemplateId->LAngleLoc = LAngleLoc;
845 TemplateId->RAngleLoc = RAngleLoc;
846 void **Args = TemplateId->getTemplateArgs();
847 bool *ArgIsType = TemplateId->getTemplateArgIsType();
848 SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations();
849 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
850 Arg != ArgEnd; ++Arg) {
851 Args[Arg] = TemplateArgs[Arg];
852 ArgIsType[Arg] = TemplateArgIsType[Arg];
853 ArgLocs[Arg] = TemplateArgLocations[Arg];
854 }
855
856 Id.setTemplateId(TemplateId);
857 return false;
858 }
859
860 // Bundle the template arguments together.
861 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
862 TemplateArgIsType.data(),
863 TemplateArgs.size());
864
865 // Constructor and destructor names.
866 Action::TypeResult Type
867 = Actions.ActOnTemplateIdType(Template, NameLoc,
868 LAngleLoc, TemplateArgsPtr,
869 &TemplateArgLocations[0],
870 RAngleLoc);
871 if (Type.isInvalid())
872 return true;
873
874 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
875 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
876 else
877 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
878
879 return false;
880}
881
882/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
883/// name of an entity.
884///
885/// \code
886/// unqualified-id: [C++ expr.prim.general]
887/// identifier
888/// operator-function-id
889/// conversion-function-id
890/// [C++0x] literal-operator-id [TODO]
891/// ~ class-name
892/// template-id
893///
894/// operator-function-id: [C++ 13.5]
895/// 'operator' operator
896///
897/// operator: one of
898/// new delete new[] delete[]
899/// + - * / % ^ & | ~
900/// ! = < > += -= *= /= %=
901/// ^= &= |= << >> >>= <<= == !=
902/// <= >= && || ++ -- , ->* ->
903/// () []
904///
905/// conversion-function-id: [C++ 12.3.2]
906/// operator conversion-type-id
907///
908/// conversion-type-id:
909/// type-specifier-seq conversion-declarator[opt]
910///
911/// conversion-declarator:
912/// ptr-operator conversion-declarator[opt]
913/// \endcode
914///
915/// \param The nested-name-specifier that preceded this unqualified-id. If
916/// non-empty, then we are parsing the unqualified-id of a qualified-id.
917///
918/// \param EnteringContext whether we are entering the scope of the
919/// nested-name-specifier.
920///
921/// \param AllowDestructorName whether we allow parsing of a destructor name.
922///
923/// \param AllowConstructorName whether we allow parsing a constructor name.
924///
925/// \param Result on a successful parse, contains the parsed unqualified-id.
926///
927/// \returns true if parsing fails, false otherwise.
928bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
929 bool AllowDestructorName,
930 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000931 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000932 UnqualifiedId &Result) {
933 // unqualified-id:
934 // identifier
935 // template-id (when it hasn't already been annotated)
936 if (Tok.is(tok::identifier)) {
937 // Consume the identifier.
938 IdentifierInfo *Id = Tok.getIdentifierInfo();
939 SourceLocation IdLoc = ConsumeToken();
940
941 if (AllowConstructorName &&
942 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
943 // We have parsed a constructor name.
944 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
945 &SS, false),
946 IdLoc, IdLoc);
947 } else {
948 // We have parsed an identifier.
949 Result.setIdentifier(Id, IdLoc);
950 }
951
952 // If the next token is a '<', we may have a template.
953 if (Tok.is(tok::less))
954 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000955 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000956
957 return false;
958 }
959
960 // unqualified-id:
961 // template-id (already parsed and annotated)
962 if (Tok.is(tok::annot_template_id)) {
963 // FIXME: Could this be a constructor name???
964
965 // We have already parsed a template-id; consume the annotation token as
966 // our unqualified-id.
967 Result.setTemplateId(
968 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
969 ConsumeToken();
970 return false;
971 }
972
973 // unqualified-id:
974 // operator-function-id
975 // conversion-function-id
976 if (Tok.is(tok::kw_operator)) {
977 // Consume the 'operator' keyword.
978 SourceLocation KeywordLoc = ConsumeToken();
979
980 // Determine what kind of operator name we have.
981 unsigned SymbolIdx = 0;
982 SourceLocation SymbolLocations[3];
983 OverloadedOperatorKind Op = OO_None;
984 switch (Tok.getKind()) {
985 case tok::kw_new:
986 case tok::kw_delete: {
987 bool isNew = Tok.getKind() == tok::kw_new;
988 // Consume the 'new' or 'delete'.
989 SymbolLocations[SymbolIdx++] = ConsumeToken();
990 if (Tok.is(tok::l_square)) {
991 // Consume the '['.
992 SourceLocation LBracketLoc = ConsumeBracket();
993 // Consume the ']'.
994 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
995 LBracketLoc);
996 if (RBracketLoc.isInvalid())
997 return true;
998
999 SymbolLocations[SymbolIdx++] = LBracketLoc;
1000 SymbolLocations[SymbolIdx++] = RBracketLoc;
1001 Op = isNew? OO_Array_New : OO_Array_Delete;
1002 } else {
1003 Op = isNew? OO_New : OO_Delete;
1004 }
1005 break;
1006 }
1007
1008 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1009 case tok::Token: \
1010 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1011 Op = OO_##Name; \
1012 break;
1013 #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1014 #include "clang/Basic/OperatorKinds.def"
1015
1016 case tok::l_paren: {
1017 // Consume the '('.
1018 SourceLocation LParenLoc = ConsumeParen();
1019 // Consume the ')'.
1020 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1021 LParenLoc);
1022 if (RParenLoc.isInvalid())
1023 return true;
1024
1025 SymbolLocations[SymbolIdx++] = LParenLoc;
1026 SymbolLocations[SymbolIdx++] = RParenLoc;
1027 Op = OO_Call;
1028 break;
1029 }
1030
1031 case tok::l_square: {
1032 // Consume the '['.
1033 SourceLocation LBracketLoc = ConsumeBracket();
1034 // Consume the ']'.
1035 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1036 LBracketLoc);
1037 if (RBracketLoc.isInvalid())
1038 return true;
1039
1040 SymbolLocations[SymbolIdx++] = LBracketLoc;
1041 SymbolLocations[SymbolIdx++] = RBracketLoc;
1042 Op = OO_Subscript;
1043 break;
1044 }
1045
1046 case tok::code_completion: {
1047 // Code completion for the operator name.
1048 Actions.CodeCompleteOperatorName(CurScope);
1049
1050 // Consume the operator token.
1051 ConsumeToken();
1052
1053 // Don't try to parse any further.
1054 return true;
1055 }
1056
1057 default:
1058 break;
1059 }
1060
1061 if (Op != OO_None) {
1062 // We have parsed an operator-function-id.
1063 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1064
1065 // If the next token is a '<', we may have a template.
1066 if (Tok.is(tok::less))
1067 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001068 EnteringContext, ObjectType,
1069 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001070
1071 return false;
1072 }
1073
1074 // Parse a conversion-function-id.
1075 //
1076 // conversion-function-id: [C++ 12.3.2]
1077 // operator conversion-type-id
1078 //
1079 // conversion-type-id:
1080 // type-specifier-seq conversion-declarator[opt]
1081 //
1082 // conversion-declarator:
1083 // ptr-operator conversion-declarator[opt]
1084
1085 // Parse the type-specifier-seq.
1086 DeclSpec DS;
1087 if (ParseCXXTypeSpecifierSeq(DS))
1088 return true;
1089
1090 // Parse the conversion-declarator, which is merely a sequence of
1091 // ptr-operators.
1092 Declarator D(DS, Declarator::TypeNameContext);
1093 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1094
1095 // Finish up the type.
1096 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1097 if (Ty.isInvalid())
1098 return true;
1099
1100 // Note that this is a conversion-function-id.
1101 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1102 D.getSourceRange().getEnd());
1103 return false;
1104 }
1105
1106 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1107 // C++ [expr.unary.op]p10:
1108 // There is an ambiguity in the unary-expression ~X(), where X is a
1109 // class-name. The ambiguity is resolved in favor of treating ~ as a
1110 // unary complement rather than treating ~X as referring to a destructor.
1111
1112 // Parse the '~'.
1113 SourceLocation TildeLoc = ConsumeToken();
1114
1115 // Parse the class-name.
1116 if (Tok.isNot(tok::identifier)) {
1117 Diag(Tok, diag::err_destructor_class_name);
1118 return true;
1119 }
1120
1121 // Parse the class-name (or template-name in a simple-template-id).
1122 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1123 SourceLocation ClassNameLoc = ConsumeToken();
1124
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001125 if (Tok.is(tok::less)) {
1126 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1127 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1128 EnteringContext, ObjectType, Result);
1129 }
1130
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001131 // Note that this is a destructor name.
1132 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
1133 CurScope, &SS);
1134 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001135 if (ObjectType)
1136 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1137 << ClassName;
1138 else
1139 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001140 return true;
1141 }
1142
1143 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001144 return false;
1145 }
1146
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001147 Diag(Tok, diag::err_expected_unqualified_id)
1148 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001149 return true;
1150}
1151
Douglas Gregor43c7bad2008-11-17 16:14:12 +00001152/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001153/// operator name (C++ [over.oper]). If successful, returns the
1154/// predefined identifier that corresponds to that overloaded
1155/// operator. Otherwise, returns NULL and does not consume any tokens.
1156///
1157/// operator-function-id: [C++ 13.5]
1158/// 'operator' operator
1159///
1160/// operator: one of
1161/// new delete new[] delete[]
1162/// + - * / % ^ & | ~
1163/// ! = < > += -= *= /= %=
1164/// ^= &= |= << >> >>= <<= == !=
1165/// <= >= && || ++ -- , ->* ->
1166/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +00001167OverloadedOperatorKind
1168Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +00001169 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +00001170 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001171
1172 OverloadedOperatorKind Op = OO_None;
1173 switch (NextToken().getKind()) {
1174 case tok::kw_new:
1175 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001176 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001177 if (Tok.is(tok::l_square)) {
1178 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +00001179 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001180 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
1181 Op = OO_Array_New;
1182 } else {
1183 Op = OO_New;
1184 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001185 if (EndLoc)
1186 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001187 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001188
1189 case tok::kw_delete:
1190 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001191 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001192 if (Tok.is(tok::l_square)) {
1193 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +00001194 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001195 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
1196 Op = OO_Array_Delete;
1197 } else {
1198 Op = OO_Delete;
1199 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001200 if (EndLoc)
1201 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001202 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001203
Douglas Gregor02bcd4c2008-11-10 13:38:07 +00001204#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001205 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +00001206#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001207#include "clang/Basic/OperatorKinds.def"
1208
1209 case tok::l_paren:
1210 ConsumeToken(); // 'operator'
1211 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +00001212 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001213 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001214 if (EndLoc)
1215 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001216 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001217
1218 case tok::l_square:
1219 ConsumeToken(); // 'operator'
1220 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +00001221 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001222 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001223 if (EndLoc)
1224 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001225 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001226
Douglas Gregored8d3222009-09-18 20:05:18 +00001227 case tok::code_completion: {
1228 // Code completion for the operator name.
1229 Actions.CodeCompleteOperatorName(CurScope);
1230
1231 // Consume the 'operator' token, then replace the code-completion token
1232 // with an 'operator' token and try again.
1233 SourceLocation OperatorLoc = ConsumeToken();
1234 Tok.setLocation(OperatorLoc);
1235 Tok.setKind(tok::kw_operator);
1236 return TryParseOperatorFunctionId(EndLoc);
1237 }
1238
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001239 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001240 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001241 }
1242
Douglas Gregor43c7bad2008-11-17 16:14:12 +00001243 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001244 Loc = ConsumeAnyToken(); // the operator itself
1245 if (EndLoc)
1246 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001247 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001248}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001249
1250/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
1251/// which expresses the name of a user-defined conversion operator
1252/// (C++ [class.conv.fct]p1). Returns the type that this operator is
1253/// specifying a conversion for, or NULL if there was an error.
1254///
1255/// conversion-function-id: [C++ 12.3.2]
1256/// operator conversion-type-id
1257///
1258/// conversion-type-id:
1259/// type-specifier-seq conversion-declarator[opt]
1260///
1261/// conversion-declarator:
1262/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +00001263Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001264 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1265 ConsumeToken(); // 'operator'
1266
1267 // Parse the type-specifier-seq.
1268 DeclSpec DS;
1269 if (ParseCXXTypeSpecifierSeq(DS))
1270 return 0;
1271
1272 // Parse the conversion-declarator, which is merely a sequence of
1273 // ptr-operators.
1274 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001275 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001276 if (EndLoc)
1277 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001278
1279 // Finish up the type.
1280 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001281 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001282 return 0;
1283 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001284 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001285}
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001286
1287/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1288/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001289///
Chris Lattner59232d32009-01-04 21:25:24 +00001290/// This method is called to parse the new expression after the optional :: has
1291/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1292/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001293///
1294/// new-expression:
1295/// '::'[opt] 'new' new-placement[opt] new-type-id
1296/// new-initializer[opt]
1297/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1298/// new-initializer[opt]
1299///
1300/// new-placement:
1301/// '(' expression-list ')'
1302///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001303/// new-type-id:
1304/// type-specifier-seq new-declarator[opt]
1305///
1306/// new-declarator:
1307/// ptr-operator new-declarator[opt]
1308/// direct-new-declarator
1309///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001310/// new-initializer:
1311/// '(' expression-list[opt] ')'
1312/// [C++0x] braced-init-list [TODO]
1313///
Chris Lattner59232d32009-01-04 21:25:24 +00001314Parser::OwningExprResult
1315Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1316 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1317 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001318
1319 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1320 // second form of new-expression. It can't be a new-type-id.
1321
Sebastian Redla55e52c2008-11-25 22:21:31 +00001322 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001323 SourceLocation PlacementLParen, PlacementRParen;
1324
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001325 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001326 DeclSpec DS;
1327 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001328 if (Tok.is(tok::l_paren)) {
1329 // If it turns out to be a placement, we change the type location.
1330 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001331 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1332 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001333 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001334 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001335
1336 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001337 if (PlacementRParen.isInvalid()) {
1338 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001339 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001340 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001341
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001342 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001343 // Reset the placement locations. There was no placement.
1344 PlacementLParen = PlacementRParen = SourceLocation();
1345 ParenTypeId = true;
1346 } else {
1347 // We still need the type.
1348 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001349 SourceLocation LParen = ConsumeParen();
1350 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001351 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001352 ParseDeclarator(DeclaratorInfo);
1353 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001354 ParenTypeId = true;
1355 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001356 if (ParseCXXTypeSpecifierSeq(DS))
1357 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001358 else {
1359 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001360 ParseDeclaratorInternal(DeclaratorInfo,
1361 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001362 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001363 ParenTypeId = false;
1364 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001365 }
1366 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001367 // A new-type-id is a simplified type-id, where essentially the
1368 // direct-declarator is replaced by a direct-new-declarator.
1369 if (ParseCXXTypeSpecifierSeq(DS))
1370 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001371 else {
1372 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001373 ParseDeclaratorInternal(DeclaratorInfo,
1374 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001375 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001376 ParenTypeId = false;
1377 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001378 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001379 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001380 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001381 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001382
Sebastian Redla55e52c2008-11-25 22:21:31 +00001383 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001384 SourceLocation ConstructorLParen, ConstructorRParen;
1385
1386 if (Tok.is(tok::l_paren)) {
1387 ConstructorLParen = ConsumeParen();
1388 if (Tok.isNot(tok::r_paren)) {
1389 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001390 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1391 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001392 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001393 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001394 }
1395 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001396 if (ConstructorRParen.isInvalid()) {
1397 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001398 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001399 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001400 }
1401
Sebastian Redlf53597f2009-03-15 17:47:39 +00001402 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1403 move_arg(PlacementArgs), PlacementRParen,
1404 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1405 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001406}
1407
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001408/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1409/// passed to ParseDeclaratorInternal.
1410///
1411/// direct-new-declarator:
1412/// '[' expression ']'
1413/// direct-new-declarator '[' constant-expression ']'
1414///
Chris Lattner59232d32009-01-04 21:25:24 +00001415void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001416 // Parse the array dimensions.
1417 bool first = true;
1418 while (Tok.is(tok::l_square)) {
1419 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001420 OwningExprResult Size(first ? ParseExpression()
1421 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001422 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001423 // Recover
1424 SkipUntil(tok::r_square);
1425 return;
1426 }
1427 first = false;
1428
Sebastian Redlab197ba2009-02-09 18:23:29 +00001429 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001430 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001431 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001432 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001433
Sebastian Redlab197ba2009-02-09 18:23:29 +00001434 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001435 return;
1436 }
1437}
1438
1439/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1440/// This ambiguity appears in the syntax of the C++ new operator.
1441///
1442/// new-expression:
1443/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1444/// new-initializer[opt]
1445///
1446/// new-placement:
1447/// '(' expression-list ')'
1448///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001449bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001450 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001451 // The '(' was already consumed.
1452 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001453 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001454 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001455 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001456 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001457 }
1458
1459 // It's not a type, it has to be an expression list.
1460 // Discard the comma locations - ActOnCXXNew has enough parameters.
1461 CommaLocsTy CommaLocs;
1462 return ParseExpressionList(PlacementArgs, CommaLocs);
1463}
1464
1465/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1466/// to free memory allocated by new.
1467///
Chris Lattner59232d32009-01-04 21:25:24 +00001468/// This method is called to parse the 'delete' expression after the optional
1469/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1470/// and "Start" is its location. Otherwise, "Start" is the location of the
1471/// 'delete' token.
1472///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001473/// delete-expression:
1474/// '::'[opt] 'delete' cast-expression
1475/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001476Parser::OwningExprResult
1477Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1478 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1479 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001480
1481 // Array delete?
1482 bool ArrayDelete = false;
1483 if (Tok.is(tok::l_square)) {
1484 ArrayDelete = true;
1485 SourceLocation LHS = ConsumeBracket();
1486 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1487 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001488 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001489 }
1490
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001491 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001492 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001493 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001494
Sebastian Redlf53597f2009-03-15 17:47:39 +00001495 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001496}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001497
Mike Stump1eb44332009-09-09 15:08:12 +00001498static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001499 switch(kind) {
1500 default: assert(false && "Not a known unary type trait.");
1501 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1502 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1503 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1504 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1505 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1506 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1507 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1508 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1509 case tok::kw___is_abstract: return UTT_IsAbstract;
1510 case tok::kw___is_class: return UTT_IsClass;
1511 case tok::kw___is_empty: return UTT_IsEmpty;
1512 case tok::kw___is_enum: return UTT_IsEnum;
1513 case tok::kw___is_pod: return UTT_IsPOD;
1514 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1515 case tok::kw___is_union: return UTT_IsUnion;
1516 }
1517}
1518
1519/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1520/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1521/// templates.
1522///
1523/// primary-expression:
1524/// [GNU] unary-type-trait '(' type-id ')'
1525///
Mike Stump1eb44332009-09-09 15:08:12 +00001526Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001527 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1528 SourceLocation Loc = ConsumeToken();
1529
1530 SourceLocation LParen = Tok.getLocation();
1531 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1532 return ExprError();
1533
1534 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1535 // there will be cryptic errors about mismatched parentheses and missing
1536 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001537 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001538
1539 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1540
Douglas Gregor809070a2009-02-18 17:45:20 +00001541 if (Ty.isInvalid())
1542 return ExprError();
1543
1544 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001545}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001546
1547/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1548/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1549/// based on the context past the parens.
1550Parser::OwningExprResult
1551Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1552 TypeTy *&CastTy,
1553 SourceLocation LParenLoc,
1554 SourceLocation &RParenLoc) {
1555 assert(getLang().CPlusPlus && "Should only be called for C++!");
1556 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1557 assert(isTypeIdInParens() && "Not a type-id!");
1558
1559 OwningExprResult Result(Actions, true);
1560 CastTy = 0;
1561
1562 // We need to disambiguate a very ugly part of the C++ syntax:
1563 //
1564 // (T())x; - type-id
1565 // (T())*x; - type-id
1566 // (T())/x; - expression
1567 // (T()); - expression
1568 //
1569 // The bad news is that we cannot use the specialized tentative parser, since
1570 // it can only verify that the thing inside the parens can be parsed as
1571 // type-id, it is not useful for determining the context past the parens.
1572 //
1573 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001574 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001575 //
1576 // It uses a scheme similar to parsing inline methods. The parenthesized
1577 // tokens are cached, the context that follows is determined (possibly by
1578 // parsing a cast-expression), and then we re-introduce the cached tokens
1579 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001580
Mike Stump1eb44332009-09-09 15:08:12 +00001581 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001582 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001583
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001584 // Store the tokens of the parentheses. We will parse them after we determine
1585 // the context that follows them.
1586 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1587 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001588 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1589 return ExprError();
1590 }
1591
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001592 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001593 ParseAs = CompoundLiteral;
1594 } else {
1595 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001596 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1597 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1598 NotCastExpr = true;
1599 } else {
1600 // Try parsing the cast-expression that may follow.
1601 // If it is not a cast-expression, NotCastExpr will be true and no token
1602 // will be consumed.
1603 Result = ParseCastExpression(false/*isUnaryExpression*/,
1604 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001605 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001606 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001607
1608 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1609 // an expression.
1610 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001611 }
1612
Mike Stump1eb44332009-09-09 15:08:12 +00001613 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001614 Toks.push_back(Tok);
1615 // Re-enter the stored parenthesized tokens into the token stream, so we may
1616 // parse them now.
1617 PP.EnterTokenStream(Toks.data(), Toks.size(),
1618 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1619 // Drop the current token and bring the first cached one. It's the same token
1620 // as when we entered this function.
1621 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001622
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001623 if (ParseAs >= CompoundLiteral) {
1624 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001625
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001626 // Match the ')'.
1627 if (Tok.is(tok::r_paren))
1628 RParenLoc = ConsumeParen();
1629 else
1630 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001631
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001632 if (ParseAs == CompoundLiteral) {
1633 ExprType = CompoundLiteral;
1634 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001637 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1638 assert(ParseAs == CastExpr);
1639
1640 if (Ty.isInvalid())
1641 return ExprError();
1642
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001643 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001644
1645 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001646 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001647 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001648 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001649 return move(Result);
1650 }
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001652 // Not a compound literal, and not followed by a cast-expression.
1653 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001654
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001655 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001656 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001657 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1658 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1659
1660 // Match the ')'.
1661 if (Result.isInvalid()) {
1662 SkipUntil(tok::r_paren);
1663 return ExprError();
1664 }
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001666 if (Tok.is(tok::r_paren))
1667 RParenLoc = ConsumeParen();
1668 else
1669 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1670
1671 return move(Result);
1672}