blob: 1dc5e68717e34c88eaaf0cbefd0ac0ecee48dec2 [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///
48/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000049bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000050 Action::TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +000051 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000052 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000053 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000054
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000055 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000056 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000057 SS.setRange(Tok.getAnnotationRange());
58 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000059 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000060 }
Chris Lattnere607e802009-01-04 21:14:15 +000061
Douglas Gregor39a8de12009-02-25 19:37:18 +000062 bool HasScopeSpecifier = false;
63
Chris Lattner5b454732009-01-05 03:55:46 +000064 if (Tok.is(tok::coloncolon)) {
65 // ::new and ::delete aren't nested-name-specifiers.
66 tok::TokenKind NextKind = NextToken().getKind();
67 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
68 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Chris Lattner55a7cef2009-01-05 00:13:00 +000070 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000071 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000072 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000073 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000074 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000075 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000076 }
77
Douglas Gregor39a8de12009-02-25 19:37:18 +000078 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000079 if (HasScopeSpecifier) {
80 // C++ [basic.lookup.classref]p5:
81 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000082 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000083 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000084 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000085 // the class-name-or-namespace-name is looked up in global scope as a
86 // class-name or namespace-name.
87 //
88 // To implement this, we clear out the object type as soon as we've
89 // seen a leading '::' or part of a nested-name-specifier.
90 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +000091
92 if (Tok.is(tok::code_completion)) {
93 // Code completion for a nested-name-specifier, where the code
94 // code completion token follows the '::'.
95 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
96 ConsumeToken();
97 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +000098 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Douglas Gregor39a8de12009-02-25 19:37:18 +0000100 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000101 // nested-name-specifier 'template'[opt] simple-template-id '::'
102
103 // Parse the optional 'template' keyword, then make sure we have
104 // 'identifier <' after it.
105 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000106 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000107 // nested-name-specifier, since they aren't allowed to start with
108 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000109 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000110 break;
111
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000112 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000113 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000114
115 UnqualifiedId TemplateName;
116 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000117 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000118 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000119 ConsumeToken();
120 } else if (Tok.is(tok::kw_operator)) {
121 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000122 TemplateName)) {
123 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000124 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000125 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000126
Sean Hunte6252d12009-11-28 08:58:14 +0000127 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
128 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000129 Diag(TemplateName.getSourceRange().getBegin(),
130 diag::err_id_after_template_in_nested_name_spec)
131 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000132 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000133 break;
134 }
135 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000136 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000137 break;
138 }
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000140 // If the next token is not '<', we have a qualified-id that refers
141 // to a template name, such as T::template apply, but is not a
142 // template-id.
143 if (Tok.isNot(tok::less)) {
144 TPA.Revert();
145 break;
146 }
147
148 // Commit to parsing the template-id.
149 TPA.Commit();
Mike Stump1eb44332009-09-09 15:08:12 +0000150 TemplateTy Template
Douglas Gregor014e88d2009-11-03 23:16:33 +0000151 = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
Douglas Gregora481edb2009-11-20 23:39:24 +0000152 ObjectType, EnteringContext);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000153 if (!Template)
154 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000155 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000156 &SS, TemplateName, TemplateKWLoc, false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000157 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattner77cf72a2009-06-26 03:47:46 +0000159 continue;
160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Douglas Gregor39a8de12009-02-25 19:37:18 +0000162 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000163 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000164 //
165 // simple-template-id '::'
166 //
167 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000168 // right kind (it should name a type or be dependent), and then
169 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000170 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000171 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
172
Mike Stump1eb44332009-09-09 15:08:12 +0000173 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000174 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000175 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000176
Mike Stump1eb44332009-09-09 15:08:12 +0000177 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000178 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000179 Token TypeToken = Tok;
180 ConsumeToken();
181 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
182 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregor39a8de12009-02-25 19:37:18 +0000184 if (!HasScopeSpecifier) {
185 SS.setBeginLoc(TypeToken.getLocation());
186 HasScopeSpecifier = true;
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Douglas Gregor31a19b62009-04-01 21:51:26 +0000189 if (TypeToken.getAnnotationValue())
190 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000191 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000192 TypeToken.getAnnotationValue(),
193 TypeToken.getAnnotationRange(),
194 CCLoc));
195 else
196 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000197 SS.setEndLoc(CCLoc);
198 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattner67b9e832009-06-26 03:45:46 +0000201 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000202 }
203
Chris Lattner5c7f7862009-06-26 03:52:38 +0000204
205 // The rest of the nested-name-specifier possibilities start with
206 // tok::identifier.
207 if (Tok.isNot(tok::identifier))
208 break;
209
210 IdentifierInfo &II = *Tok.getIdentifierInfo();
211
212 // nested-name-specifier:
213 // type-name '::'
214 // namespace-name '::'
215 // nested-name-specifier identifier '::'
216 Token Next = NextToken();
217 if (Next.is(tok::coloncolon)) {
218 // We have an identifier followed by a '::'. Lookup this name
219 // as the name in a nested-name-specifier.
220 SourceLocation IdLoc = ConsumeToken();
221 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
222 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Chris Lattner5c7f7862009-06-26 03:52:38 +0000224 if (!HasScopeSpecifier) {
225 SS.setBeginLoc(IdLoc);
226 HasScopeSpecifier = true;
227 }
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Chris Lattner5c7f7862009-06-26 03:52:38 +0000229 if (SS.isInvalid())
230 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Chris Lattner5c7f7862009-06-26 03:52:38 +0000232 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000233 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000234 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000235 SS.setEndLoc(CCLoc);
236 continue;
237 }
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Chris Lattner5c7f7862009-06-26 03:52:38 +0000239 // nested-name-specifier:
240 // type-name '<'
241 if (Next.is(tok::less)) {
242 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000243 UnqualifiedId TemplateName;
244 TemplateName.setIdentifier(&II, Tok.getLocation());
245 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
246 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000247 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000248 EnteringContext,
249 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000250 // We have found a template name, so annotate this this token
251 // with a template-id annotation. We do not permit the
252 // template-id to be translated into a type annotation,
253 // because some clients (e.g., the parsing of class template
254 // specializations) still want to see the original template-id
255 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000256 ConsumeToken();
257 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
258 SourceLocation(), false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000259 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000260 continue;
261 }
262 }
263
Douglas Gregor39a8de12009-02-25 19:37:18 +0000264 // We don't have any tokens that form the beginning of a
265 // nested-name-specifier, so we're done.
266 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Douglas Gregor39a8de12009-02-25 19:37:18 +0000269 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000270}
271
272/// ParseCXXIdExpression - Handle id-expression.
273///
274/// id-expression:
275/// unqualified-id
276/// qualified-id
277///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000278/// qualified-id:
279/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
280/// '::' identifier
281/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000282/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000283///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000284/// NOTE: The standard specifies that, for qualified-id, the parser does not
285/// expect:
286///
287/// '::' conversion-function-id
288/// '::' '~' class-name
289///
290/// This may cause a slight inconsistency on diagnostics:
291///
292/// class C {};
293/// namespace A {}
294/// void f() {
295/// :: A :: ~ C(); // Some Sema error about using destructor with a
296/// // namespace.
297/// :: ~ C(); // Some Parser error like 'unexpected ~'.
298/// }
299///
300/// We simplify the parser a bit and make it work like:
301///
302/// qualified-id:
303/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
304/// '::' unqualified-id
305///
306/// That way Sema can handle and report similar errors for namespaces and the
307/// global scope.
308///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000309/// The isAddressOfOperand parameter indicates that this id-expression is a
310/// direct operand of the address-of operator. This is, besides member contexts,
311/// the only place where a qualified-id naming a non-static class member may
312/// appear.
313///
314Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000315 // qualified-id:
316 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
317 // '::' unqualified-id
318 //
319 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000320 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000321
322 UnqualifiedId Name;
323 if (ParseUnqualifiedId(SS,
324 /*EnteringContext=*/false,
325 /*AllowDestructorName=*/false,
326 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000327 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000328 Name))
329 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000330
331 // This is only the direct operand of an & operator if it is not
332 // followed by a postfix-expression suffix.
333 if (isAddressOfOperand) {
334 switch (Tok.getKind()) {
335 case tok::l_square:
336 case tok::l_paren:
337 case tok::arrow:
338 case tok::period:
339 case tok::plusplus:
340 case tok::minusminus:
341 isAddressOfOperand = false;
342 break;
343
344 default:
345 break;
346 }
347 }
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000348
349 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
350 isAddressOfOperand);
351
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000352}
353
Reid Spencer5f016e22007-07-11 17:01:13 +0000354/// ParseCXXCasts - This handles the various ways to cast expressions to another
355/// type.
356///
357/// postfix-expression: [C++ 5.2p1]
358/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
359/// 'static_cast' '<' type-name '>' '(' expression ')'
360/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
361/// 'const_cast' '<' type-name '>' '(' expression ')'
362///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000363Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 tok::TokenKind Kind = Tok.getKind();
365 const char *CastName = 0; // For error messages
366
367 switch (Kind) {
368 default: assert(0 && "Unknown C++ cast!"); abort();
369 case tok::kw_const_cast: CastName = "const_cast"; break;
370 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
371 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
372 case tok::kw_static_cast: CastName = "static_cast"; break;
373 }
374
375 SourceLocation OpLoc = ConsumeToken();
376 SourceLocation LAngleBracketLoc = Tok.getLocation();
377
378 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000379 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000380
Douglas Gregor809070a2009-02-18 17:45:20 +0000381 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 SourceLocation RAngleBracketLoc = Tok.getLocation();
383
Chris Lattner1ab3b962008-11-18 07:48:38 +0000384 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000385 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000386
387 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
388
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000389 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
390 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000391
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000392 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000394 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000395 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000396
Douglas Gregor809070a2009-02-18 17:45:20 +0000397 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000398 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000399 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000400 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000401 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000402
Sebastian Redl20df9b72008-12-11 22:51:44 +0000403 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000404}
405
Sebastian Redlc42e1182008-11-11 11:37:55 +0000406/// ParseCXXTypeid - This handles the C++ typeid expression.
407///
408/// postfix-expression: [C++ 5.2p1]
409/// 'typeid' '(' expression ')'
410/// 'typeid' '(' type-id ')'
411///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000412Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000413 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
414
415 SourceLocation OpLoc = ConsumeToken();
416 SourceLocation LParenLoc = Tok.getLocation();
417 SourceLocation RParenLoc;
418
419 // typeid expressions are always parenthesized.
420 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
421 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000422 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000423
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000424 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000425
426 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000427 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000428
429 // Match the ')'.
430 MatchRHSPunctuation(tok::r_paren, LParenLoc);
431
Douglas Gregor809070a2009-02-18 17:45:20 +0000432 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000433 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000434
435 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000436 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000437 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000438 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000439 // When typeid is applied to an expression other than an lvalue of a
440 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000441 // operand (Clause 5).
442 //
Mike Stump1eb44332009-09-09 15:08:12 +0000443 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000444 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000445 // we the expression is potentially potentially evaluated.
446 EnterExpressionEvaluationContext Unevaluated(Actions,
447 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000448 Result = ParseExpression();
449
450 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000451 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000452 SkipUntil(tok::r_paren);
453 else {
454 MatchRHSPunctuation(tok::r_paren, LParenLoc);
455
456 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000457 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000458 }
459 }
460
Sebastian Redl20df9b72008-12-11 22:51:44 +0000461 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000462}
463
Reid Spencer5f016e22007-07-11 17:01:13 +0000464/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
465///
466/// boolean-literal: [C++ 2.13.5]
467/// 'true'
468/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000469Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000471 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000472}
Chris Lattner50dd2892008-02-26 00:51:44 +0000473
474/// ParseThrowExpression - This handles the C++ throw expression.
475///
476/// throw-expression: [C++ 15]
477/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000478Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000479 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000480 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000481
Chris Lattner2a2819a2008-04-06 06:02:23 +0000482 // If the current token isn't the start of an assignment-expression,
483 // then the expression is not present. This handles things like:
484 // "C ? throw : (void)42", which is crazy but legal.
485 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
486 case tok::semi:
487 case tok::r_paren:
488 case tok::r_square:
489 case tok::r_brace:
490 case tok::colon:
491 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000492 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000493
Chris Lattner2a2819a2008-04-06 06:02:23 +0000494 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000495 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000496 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000497 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000498 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000499}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000500
501/// ParseCXXThis - This handles the C++ 'this' pointer.
502///
503/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
504/// a non-lvalue expression whose value is the address of the object for which
505/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000506Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000507 assert(Tok.is(tok::kw_this) && "Not 'this'!");
508 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000509 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000510}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000511
512/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
513/// Can be interpreted either as function-style casting ("int(x)")
514/// or class type construction ("ClassType(x,y,z)")
515/// or creation of a value-initialized type ("int()").
516///
517/// postfix-expression: [C++ 5.2p1]
518/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
519/// typename-specifier '(' expression-list[opt] ')' [TODO]
520///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000521Parser::OwningExprResult
522Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000523 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000524 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000525
526 assert(Tok.is(tok::l_paren) && "Expected '('!");
527 SourceLocation LParenLoc = ConsumeParen();
528
Sebastian Redla55e52c2008-11-25 22:21:31 +0000529 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000530 CommaLocsTy CommaLocs;
531
532 if (Tok.isNot(tok::r_paren)) {
533 if (ParseExpressionList(Exprs, CommaLocs)) {
534 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000535 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000536 }
537 }
538
539 // Match the ')'.
540 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
541
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000542 // TypeRep could be null, if it references an invalid typedef.
543 if (!TypeRep)
544 return ExprError();
545
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000546 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
547 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000548 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
549 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000550 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000551}
552
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000553/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000554///
555/// condition:
556/// expression
557/// type-specifier-seq declarator '=' assignment-expression
558/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
559/// '=' assignment-expression
560///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000561/// \param ExprResult if the condition was parsed as an expression, the
562/// parsed expression.
563///
564/// \param DeclResult if the condition was parsed as a declaration, the
565/// parsed declaration.
566///
567/// \returns true if there was a parsing, false otherwise.
568bool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
569 DeclPtrTy &DeclResult) {
570 if (!isCXXConditionDeclaration()) {
571 ExprResult = ParseExpression(); // expression
572 DeclResult = DeclPtrTy();
573 return ExprResult.isInvalid();
574 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000575
576 // type-specifier-seq
577 DeclSpec DS;
578 ParseSpecifierQualifierList(DS);
579
580 // declarator
581 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
582 ParseDeclarator(DeclaratorInfo);
583
584 // simple-asm-expr[opt]
585 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000586 SourceLocation Loc;
587 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000588 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000589 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000590 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000591 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000592 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000593 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000594 }
595
596 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000597 if (Tok.is(tok::kw___attribute)) {
598 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000599 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000600 DeclaratorInfo.AddAttributes(AttrList, Loc);
601 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000602
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000603 // Type-check the declaration itself.
604 Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope,
605 DeclaratorInfo);
606 DeclResult = Dcl.get();
607 ExprResult = ExprError();
608
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000609 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000610 if (Tok.is(tok::equal)) {
611 SourceLocation EqualLoc = ConsumeToken();
612 OwningExprResult AssignExpr(ParseAssignmentExpression());
613 if (!AssignExpr.isInvalid())
614 Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
615 } else {
616 // FIXME: C++0x allows a braced-init-list
617 Diag(Tok, diag::err_expected_equal_after_declarator);
618 }
619
620 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000621}
622
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000623/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
624/// This should only be called when the current token is known to be part of
625/// simple-type-specifier.
626///
627/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000628/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000629/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
630/// char
631/// wchar_t
632/// bool
633/// short
634/// int
635/// long
636/// signed
637/// unsigned
638/// float
639/// double
640/// void
641/// [GNU] typeof-specifier
642/// [C++0x] auto [TODO]
643///
644/// type-name:
645/// class-name
646/// enum-name
647/// typedef-name
648///
649void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
650 DS.SetRangeStart(Tok.getLocation());
651 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000652 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000653 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000655 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000656 case tok::identifier: // foo::bar
657 case tok::coloncolon: // ::foo::bar
658 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000659 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000660 assert(0 && "Not a simple-type-specifier token!");
661 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000662
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000663 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000664 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000665 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000666 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000667 break;
668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000670 // builtin types
671 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000672 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000673 break;
674 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000675 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000676 break;
677 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000678 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000679 break;
680 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000681 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000682 break;
683 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000684 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000685 break;
686 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000687 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000688 break;
689 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000690 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000691 break;
692 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000693 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000694 break;
695 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000696 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000697 break;
698 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000699 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000700 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000701 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000702 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000703 break;
704 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000705 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000706 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000707 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000708 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000709 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000711 // GNU typeof support.
712 case tok::kw_typeof:
713 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000714 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000715 return;
716 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000717 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000718 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
719 else
720 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000721 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000722 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000723}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000724
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000725/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
726/// [dcl.name]), which is a non-empty sequence of type-specifiers,
727/// e.g., "const short int". Note that the DeclSpec is *not* finished
728/// by parsing the type-specifier-seq, because these sequences are
729/// typically followed by some form of declarator. Returns true and
730/// emits diagnostics if this is not a type-specifier-seq, false
731/// otherwise.
732///
733/// type-specifier-seq: [C++ 8.1]
734/// type-specifier type-specifier-seq[opt]
735///
736bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
737 DS.SetRangeStart(Tok.getLocation());
738 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000739 unsigned DiagID;
740 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000741
742 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000743 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000744 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000745 return true;
746 }
Mike Stump1eb44332009-09-09 15:08:12 +0000747
John McCallfec54012009-08-03 20:12:06 +0000748 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000749
750 return false;
751}
752
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000753/// \brief Finish parsing a C++ unqualified-id that is a template-id of
754/// some form.
755///
756/// This routine is invoked when a '<' is encountered after an identifier or
757/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
758/// whether the unqualified-id is actually a template-id. This routine will
759/// then parse the template arguments and form the appropriate template-id to
760/// return to the caller.
761///
762/// \param SS the nested-name-specifier that precedes this template-id, if
763/// we're actually parsing a qualified-id.
764///
765/// \param Name for constructor and destructor names, this is the actual
766/// identifier that may be a template-name.
767///
768/// \param NameLoc the location of the class-name in a constructor or
769/// destructor.
770///
771/// \param EnteringContext whether we're entering the scope of the
772/// nested-name-specifier.
773///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000774/// \param ObjectType if this unqualified-id occurs within a member access
775/// expression, the type of the base object whose member is being accessed.
776///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000777/// \param Id as input, describes the template-name or operator-function-id
778/// that precedes the '<'. If template arguments were parsed successfully,
779/// will be updated with the template-id.
780///
781/// \returns true if a parse error occurred, false otherwise.
782bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
783 IdentifierInfo *Name,
784 SourceLocation NameLoc,
785 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000786 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000787 UnqualifiedId &Id) {
788 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
789
790 TemplateTy Template;
791 TemplateNameKind TNK = TNK_Non_template;
792 switch (Id.getKind()) {
793 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000794 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +0000795 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000796 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
797 Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000798 break;
799
Douglas Gregor014e88d2009-11-03 23:16:33 +0000800 case UnqualifiedId::IK_ConstructorName: {
801 UnqualifiedId TemplateName;
802 TemplateName.setIdentifier(Name, NameLoc);
803 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
804 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000805 break;
806 }
807
Douglas Gregor014e88d2009-11-03 23:16:33 +0000808 case UnqualifiedId::IK_DestructorName: {
809 UnqualifiedId TemplateName;
810 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000811 if (ObjectType) {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000812 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
Douglas Gregora481edb2009-11-20 23:39:24 +0000813 TemplateName, ObjectType,
814 EnteringContext);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000815 TNK = TNK_Dependent_template_name;
816 if (!Template.get())
817 return true;
818 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000819 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000820 EnteringContext, Template);
821
822 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
823 // The identifier following the destructor did not refer to a template
824 // or to a type. Complain.
825 if (ObjectType)
826 Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
827 << Name;
828 else
829 Diag(NameLoc, diag::err_destructor_class_name);
830 return true;
831 }
832 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000833 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000834 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000835
836 default:
837 return false;
838 }
839
840 if (TNK == TNK_Non_template)
841 return false;
842
843 // Parse the enclosed template argument list.
844 SourceLocation LAngleLoc, RAngleLoc;
845 TemplateArgList TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000846 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
847 &SS, true, LAngleLoc,
848 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000849 RAngleLoc))
850 return true;
851
852 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +0000853 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
854 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000855 // Form a parsed representation of the template-id to be stored in the
856 // UnqualifiedId.
857 TemplateIdAnnotation *TemplateId
858 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
859
860 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
861 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000862 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000863 TemplateId->TemplateNameLoc = Id.StartLocation;
864 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000865 TemplateId->Name = 0;
866 TemplateId->Operator = Id.OperatorFunctionId.Operator;
867 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000868 }
869
870 TemplateId->Template = Template.getAs<void*>();
871 TemplateId->Kind = TNK;
872 TemplateId->LAngleLoc = LAngleLoc;
873 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +0000874 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000875 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +0000876 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000877 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000878
879 Id.setTemplateId(TemplateId);
880 return false;
881 }
882
883 // Bundle the template arguments together.
884 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000885 TemplateArgs.size());
886
887 // Constructor and destructor names.
888 Action::TypeResult Type
889 = Actions.ActOnTemplateIdType(Template, NameLoc,
890 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000891 RAngleLoc);
892 if (Type.isInvalid())
893 return true;
894
895 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
896 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
897 else
898 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
899
900 return false;
901}
902
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000903/// \brief Parse an operator-function-id or conversion-function-id as part
904/// of a C++ unqualified-id.
905///
906/// This routine is responsible only for parsing the operator-function-id or
907/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000908///
909/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000910/// operator-function-id: [C++ 13.5]
911/// 'operator' operator
912///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000913/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000914/// new delete new[] delete[]
915/// + - * / % ^ & | ~
916/// ! = < > += -= *= /= %=
917/// ^= &= |= << >> >>= <<= == !=
918/// <= >= && || ++ -- , ->* ->
919/// () []
920///
921/// conversion-function-id: [C++ 12.3.2]
922/// operator conversion-type-id
923///
924/// conversion-type-id:
925/// type-specifier-seq conversion-declarator[opt]
926///
927/// conversion-declarator:
928/// ptr-operator conversion-declarator[opt]
929/// \endcode
930///
931/// \param The nested-name-specifier that preceded this unqualified-id. If
932/// non-empty, then we are parsing the unqualified-id of a qualified-id.
933///
934/// \param EnteringContext whether we are entering the scope of the
935/// nested-name-specifier.
936///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000937/// \param ObjectType if this unqualified-id occurs within a member access
938/// expression, the type of the base object whose member is being accessed.
939///
940/// \param Result on a successful parse, contains the parsed unqualified-id.
941///
942/// \returns true if parsing fails, false otherwise.
943bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
944 TypeTy *ObjectType,
945 UnqualifiedId &Result) {
946 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
947
948 // Consume the 'operator' keyword.
949 SourceLocation KeywordLoc = ConsumeToken();
950
951 // Determine what kind of operator name we have.
952 unsigned SymbolIdx = 0;
953 SourceLocation SymbolLocations[3];
954 OverloadedOperatorKind Op = OO_None;
955 switch (Tok.getKind()) {
956 case tok::kw_new:
957 case tok::kw_delete: {
958 bool isNew = Tok.getKind() == tok::kw_new;
959 // Consume the 'new' or 'delete'.
960 SymbolLocations[SymbolIdx++] = ConsumeToken();
961 if (Tok.is(tok::l_square)) {
962 // Consume the '['.
963 SourceLocation LBracketLoc = ConsumeBracket();
964 // Consume the ']'.
965 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
966 LBracketLoc);
967 if (RBracketLoc.isInvalid())
968 return true;
969
970 SymbolLocations[SymbolIdx++] = LBracketLoc;
971 SymbolLocations[SymbolIdx++] = RBracketLoc;
972 Op = isNew? OO_Array_New : OO_Array_Delete;
973 } else {
974 Op = isNew? OO_New : OO_Delete;
975 }
976 break;
977 }
978
979#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
980 case tok::Token: \
981 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
982 Op = OO_##Name; \
983 break;
984#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
985#include "clang/Basic/OperatorKinds.def"
986
987 case tok::l_paren: {
988 // Consume the '('.
989 SourceLocation LParenLoc = ConsumeParen();
990 // Consume the ')'.
991 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
992 LParenLoc);
993 if (RParenLoc.isInvalid())
994 return true;
995
996 SymbolLocations[SymbolIdx++] = LParenLoc;
997 SymbolLocations[SymbolIdx++] = RParenLoc;
998 Op = OO_Call;
999 break;
1000 }
1001
1002 case tok::l_square: {
1003 // Consume the '['.
1004 SourceLocation LBracketLoc = ConsumeBracket();
1005 // Consume the ']'.
1006 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1007 LBracketLoc);
1008 if (RBracketLoc.isInvalid())
1009 return true;
1010
1011 SymbolLocations[SymbolIdx++] = LBracketLoc;
1012 SymbolLocations[SymbolIdx++] = RBracketLoc;
1013 Op = OO_Subscript;
1014 break;
1015 }
1016
1017 case tok::code_completion: {
1018 // Code completion for the operator name.
1019 Actions.CodeCompleteOperatorName(CurScope);
1020
1021 // Consume the operator token.
1022 ConsumeToken();
1023
1024 // Don't try to parse any further.
1025 return true;
1026 }
1027
1028 default:
1029 break;
1030 }
1031
1032 if (Op != OO_None) {
1033 // We have parsed an operator-function-id.
1034 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1035 return false;
1036 }
Sean Hunt0486d742009-11-28 04:44:28 +00001037
1038 // Parse a literal-operator-id.
1039 //
1040 // literal-operator-id: [C++0x 13.5.8]
1041 // operator "" identifier
1042
1043 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1044 if (Tok.getLength() != 2)
1045 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1046 ConsumeStringToken();
1047
1048 if (Tok.isNot(tok::identifier)) {
1049 Diag(Tok.getLocation(), diag::err_expected_ident);
1050 return true;
1051 }
1052
1053 IdentifierInfo *II = Tok.getIdentifierInfo();
1054 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
1055 Diag(KeywordLoc, diag::err_unsupported_literal_operator);
1056 return true;
1057 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001058
1059 // Parse a conversion-function-id.
1060 //
1061 // conversion-function-id: [C++ 12.3.2]
1062 // operator conversion-type-id
1063 //
1064 // conversion-type-id:
1065 // type-specifier-seq conversion-declarator[opt]
1066 //
1067 // conversion-declarator:
1068 // ptr-operator conversion-declarator[opt]
1069
1070 // Parse the type-specifier-seq.
1071 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001072 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001073 return true;
1074
1075 // Parse the conversion-declarator, which is merely a sequence of
1076 // ptr-operators.
1077 Declarator D(DS, Declarator::TypeNameContext);
1078 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1079
1080 // Finish up the type.
1081 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1082 if (Ty.isInvalid())
1083 return true;
1084
1085 // Note that this is a conversion-function-id.
1086 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1087 D.getSourceRange().getEnd());
1088 return false;
1089}
1090
1091/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1092/// name of an entity.
1093///
1094/// \code
1095/// unqualified-id: [C++ expr.prim.general]
1096/// identifier
1097/// operator-function-id
1098/// conversion-function-id
1099/// [C++0x] literal-operator-id [TODO]
1100/// ~ class-name
1101/// template-id
1102///
1103/// \endcode
1104///
1105/// \param The nested-name-specifier that preceded this unqualified-id. If
1106/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1107///
1108/// \param EnteringContext whether we are entering the scope of the
1109/// nested-name-specifier.
1110///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001111/// \param AllowDestructorName whether we allow parsing of a destructor name.
1112///
1113/// \param AllowConstructorName whether we allow parsing a constructor name.
1114///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001115/// \param ObjectType if this unqualified-id occurs within a member access
1116/// expression, the type of the base object whose member is being accessed.
1117///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001118/// \param Result on a successful parse, contains the parsed unqualified-id.
1119///
1120/// \returns true if parsing fails, false otherwise.
1121bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1122 bool AllowDestructorName,
1123 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001124 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001125 UnqualifiedId &Result) {
1126 // unqualified-id:
1127 // identifier
1128 // template-id (when it hasn't already been annotated)
1129 if (Tok.is(tok::identifier)) {
1130 // Consume the identifier.
1131 IdentifierInfo *Id = Tok.getIdentifierInfo();
1132 SourceLocation IdLoc = ConsumeToken();
1133
1134 if (AllowConstructorName &&
1135 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1136 // We have parsed a constructor name.
1137 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1138 &SS, false),
1139 IdLoc, IdLoc);
1140 } else {
1141 // We have parsed an identifier.
1142 Result.setIdentifier(Id, IdLoc);
1143 }
1144
1145 // If the next token is a '<', we may have a template.
1146 if (Tok.is(tok::less))
1147 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001148 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001149
1150 return false;
1151 }
1152
1153 // unqualified-id:
1154 // template-id (already parsed and annotated)
1155 if (Tok.is(tok::annot_template_id)) {
1156 // FIXME: Could this be a constructor name???
1157
1158 // We have already parsed a template-id; consume the annotation token as
1159 // our unqualified-id.
1160 Result.setTemplateId(
1161 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1162 ConsumeToken();
1163 return false;
1164 }
1165
1166 // unqualified-id:
1167 // operator-function-id
1168 // conversion-function-id
1169 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001170 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001171 return true;
1172
Sean Hunte6252d12009-11-28 08:58:14 +00001173 // If we have an operator-function-id or a literal-operator-id and the next
1174 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001175 //
1176 // template-id:
1177 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001178 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1179 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001180 Tok.is(tok::less))
1181 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1182 EnteringContext, ObjectType,
1183 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001184
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001185 return false;
1186 }
1187
1188 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1189 // C++ [expr.unary.op]p10:
1190 // There is an ambiguity in the unary-expression ~X(), where X is a
1191 // class-name. The ambiguity is resolved in favor of treating ~ as a
1192 // unary complement rather than treating ~X as referring to a destructor.
1193
1194 // Parse the '~'.
1195 SourceLocation TildeLoc = ConsumeToken();
1196
1197 // Parse the class-name.
1198 if (Tok.isNot(tok::identifier)) {
1199 Diag(Tok, diag::err_destructor_class_name);
1200 return true;
1201 }
1202
1203 // Parse the class-name (or template-name in a simple-template-id).
1204 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1205 SourceLocation ClassNameLoc = ConsumeToken();
1206
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001207 if (Tok.is(tok::less)) {
1208 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1209 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1210 EnteringContext, ObjectType, Result);
1211 }
1212
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001213 // Note that this is a destructor name.
1214 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001215 CurScope, &SS, false, ObjectType);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001216 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001217 if (ObjectType)
1218 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1219 << ClassName;
1220 else
1221 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001222 return true;
1223 }
1224
1225 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001226 return false;
1227 }
1228
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001229 Diag(Tok, diag::err_expected_unqualified_id)
1230 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001231 return true;
1232}
1233
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001234/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1235/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001236///
Chris Lattner59232d32009-01-04 21:25:24 +00001237/// This method is called to parse the new expression after the optional :: has
1238/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1239/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001240///
1241/// new-expression:
1242/// '::'[opt] 'new' new-placement[opt] new-type-id
1243/// new-initializer[opt]
1244/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1245/// new-initializer[opt]
1246///
1247/// new-placement:
1248/// '(' expression-list ')'
1249///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001250/// new-type-id:
1251/// type-specifier-seq new-declarator[opt]
1252///
1253/// new-declarator:
1254/// ptr-operator new-declarator[opt]
1255/// direct-new-declarator
1256///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001257/// new-initializer:
1258/// '(' expression-list[opt] ')'
1259/// [C++0x] braced-init-list [TODO]
1260///
Chris Lattner59232d32009-01-04 21:25:24 +00001261Parser::OwningExprResult
1262Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1263 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1264 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001265
1266 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1267 // second form of new-expression. It can't be a new-type-id.
1268
Sebastian Redla55e52c2008-11-25 22:21:31 +00001269 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001270 SourceLocation PlacementLParen, PlacementRParen;
1271
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001272 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001273 DeclSpec DS;
1274 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001275 if (Tok.is(tok::l_paren)) {
1276 // If it turns out to be a placement, we change the type location.
1277 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001278 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1279 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001280 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001281 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001282
1283 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001284 if (PlacementRParen.isInvalid()) {
1285 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001286 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001287 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001288
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001289 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001290 // Reset the placement locations. There was no placement.
1291 PlacementLParen = PlacementRParen = SourceLocation();
1292 ParenTypeId = true;
1293 } else {
1294 // We still need the type.
1295 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001296 SourceLocation LParen = ConsumeParen();
1297 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001298 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001299 ParseDeclarator(DeclaratorInfo);
1300 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001301 ParenTypeId = true;
1302 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001303 if (ParseCXXTypeSpecifierSeq(DS))
1304 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001305 else {
1306 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001307 ParseDeclaratorInternal(DeclaratorInfo,
1308 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001309 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001310 ParenTypeId = false;
1311 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001312 }
1313 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001314 // A new-type-id is a simplified type-id, where essentially the
1315 // direct-declarator is replaced by a direct-new-declarator.
1316 if (ParseCXXTypeSpecifierSeq(DS))
1317 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001318 else {
1319 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001320 ParseDeclaratorInternal(DeclaratorInfo,
1321 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001322 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001323 ParenTypeId = false;
1324 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001325 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001326 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001327 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001328 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001329
Sebastian Redla55e52c2008-11-25 22:21:31 +00001330 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001331 SourceLocation ConstructorLParen, ConstructorRParen;
1332
1333 if (Tok.is(tok::l_paren)) {
1334 ConstructorLParen = ConsumeParen();
1335 if (Tok.isNot(tok::r_paren)) {
1336 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001337 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
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 }
1342 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001343 if (ConstructorRParen.isInvalid()) {
1344 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001345 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001346 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001347 }
1348
Sebastian Redlf53597f2009-03-15 17:47:39 +00001349 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1350 move_arg(PlacementArgs), PlacementRParen,
1351 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1352 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001353}
1354
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001355/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1356/// passed to ParseDeclaratorInternal.
1357///
1358/// direct-new-declarator:
1359/// '[' expression ']'
1360/// direct-new-declarator '[' constant-expression ']'
1361///
Chris Lattner59232d32009-01-04 21:25:24 +00001362void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001363 // Parse the array dimensions.
1364 bool first = true;
1365 while (Tok.is(tok::l_square)) {
1366 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001367 OwningExprResult Size(first ? ParseExpression()
1368 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001369 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001370 // Recover
1371 SkipUntil(tok::r_square);
1372 return;
1373 }
1374 first = false;
1375
Sebastian Redlab197ba2009-02-09 18:23:29 +00001376 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001377 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001378 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001379 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001380
Sebastian Redlab197ba2009-02-09 18:23:29 +00001381 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001382 return;
1383 }
1384}
1385
1386/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1387/// This ambiguity appears in the syntax of the C++ new operator.
1388///
1389/// new-expression:
1390/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1391/// new-initializer[opt]
1392///
1393/// new-placement:
1394/// '(' expression-list ')'
1395///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001396bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001397 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001398 // The '(' was already consumed.
1399 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001400 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001401 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001402 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001403 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001404 }
1405
1406 // It's not a type, it has to be an expression list.
1407 // Discard the comma locations - ActOnCXXNew has enough parameters.
1408 CommaLocsTy CommaLocs;
1409 return ParseExpressionList(PlacementArgs, CommaLocs);
1410}
1411
1412/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1413/// to free memory allocated by new.
1414///
Chris Lattner59232d32009-01-04 21:25:24 +00001415/// This method is called to parse the 'delete' expression after the optional
1416/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1417/// and "Start" is its location. Otherwise, "Start" is the location of the
1418/// 'delete' token.
1419///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001420/// delete-expression:
1421/// '::'[opt] 'delete' cast-expression
1422/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001423Parser::OwningExprResult
1424Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1425 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1426 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001427
1428 // Array delete?
1429 bool ArrayDelete = false;
1430 if (Tok.is(tok::l_square)) {
1431 ArrayDelete = true;
1432 SourceLocation LHS = ConsumeBracket();
1433 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1434 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001435 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001436 }
1437
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001438 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001439 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001440 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001441
Sebastian Redlf53597f2009-03-15 17:47:39 +00001442 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001443}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001444
Mike Stump1eb44332009-09-09 15:08:12 +00001445static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001446 switch(kind) {
1447 default: assert(false && "Not a known unary type trait.");
1448 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1449 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1450 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1451 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1452 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1453 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1454 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1455 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1456 case tok::kw___is_abstract: return UTT_IsAbstract;
1457 case tok::kw___is_class: return UTT_IsClass;
1458 case tok::kw___is_empty: return UTT_IsEmpty;
1459 case tok::kw___is_enum: return UTT_IsEnum;
1460 case tok::kw___is_pod: return UTT_IsPOD;
1461 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1462 case tok::kw___is_union: return UTT_IsUnion;
1463 }
1464}
1465
1466/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1467/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1468/// templates.
1469///
1470/// primary-expression:
1471/// [GNU] unary-type-trait '(' type-id ')'
1472///
Mike Stump1eb44332009-09-09 15:08:12 +00001473Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001474 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1475 SourceLocation Loc = ConsumeToken();
1476
1477 SourceLocation LParen = Tok.getLocation();
1478 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1479 return ExprError();
1480
1481 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1482 // there will be cryptic errors about mismatched parentheses and missing
1483 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001484 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001485
1486 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1487
Douglas Gregor809070a2009-02-18 17:45:20 +00001488 if (Ty.isInvalid())
1489 return ExprError();
1490
1491 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001492}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001493
1494/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1495/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1496/// based on the context past the parens.
1497Parser::OwningExprResult
1498Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1499 TypeTy *&CastTy,
1500 SourceLocation LParenLoc,
1501 SourceLocation &RParenLoc) {
1502 assert(getLang().CPlusPlus && "Should only be called for C++!");
1503 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1504 assert(isTypeIdInParens() && "Not a type-id!");
1505
1506 OwningExprResult Result(Actions, true);
1507 CastTy = 0;
1508
1509 // We need to disambiguate a very ugly part of the C++ syntax:
1510 //
1511 // (T())x; - type-id
1512 // (T())*x; - type-id
1513 // (T())/x; - expression
1514 // (T()); - expression
1515 //
1516 // The bad news is that we cannot use the specialized tentative parser, since
1517 // it can only verify that the thing inside the parens can be parsed as
1518 // type-id, it is not useful for determining the context past the parens.
1519 //
1520 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001521 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001522 //
1523 // It uses a scheme similar to parsing inline methods. The parenthesized
1524 // tokens are cached, the context that follows is determined (possibly by
1525 // parsing a cast-expression), and then we re-introduce the cached tokens
1526 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001527
Mike Stump1eb44332009-09-09 15:08:12 +00001528 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001529 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001530
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001531 // Store the tokens of the parentheses. We will parse them after we determine
1532 // the context that follows them.
1533 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1534 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001535 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1536 return ExprError();
1537 }
1538
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001539 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001540 ParseAs = CompoundLiteral;
1541 } else {
1542 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001543 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1544 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1545 NotCastExpr = true;
1546 } else {
1547 // Try parsing the cast-expression that may follow.
1548 // If it is not a cast-expression, NotCastExpr will be true and no token
1549 // will be consumed.
1550 Result = ParseCastExpression(false/*isUnaryExpression*/,
1551 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001552 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001553 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001554
1555 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1556 // an expression.
1557 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001558 }
1559
Mike Stump1eb44332009-09-09 15:08:12 +00001560 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001561 Toks.push_back(Tok);
1562 // Re-enter the stored parenthesized tokens into the token stream, so we may
1563 // parse them now.
1564 PP.EnterTokenStream(Toks.data(), Toks.size(),
1565 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1566 // Drop the current token and bring the first cached one. It's the same token
1567 // as when we entered this function.
1568 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001569
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001570 if (ParseAs >= CompoundLiteral) {
1571 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001572
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001573 // Match the ')'.
1574 if (Tok.is(tok::r_paren))
1575 RParenLoc = ConsumeParen();
1576 else
1577 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001578
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001579 if (ParseAs == CompoundLiteral) {
1580 ExprType = CompoundLiteral;
1581 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001584 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1585 assert(ParseAs == CastExpr);
1586
1587 if (Ty.isInvalid())
1588 return ExprError();
1589
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001590 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001591
1592 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001593 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001594 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001595 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001596 return move(Result);
1597 }
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001599 // Not a compound literal, and not followed by a cast-expression.
1600 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001601
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001602 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001603 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001604 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1605 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1606
1607 // Match the ')'.
1608 if (Result.isInvalid()) {
1609 SkipUntil(tok::r_paren);
1610 return ExprError();
1611 }
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001613 if (Tok.is(tok::r_paren))
1614 RParenLoc = ConsumeParen();
1615 else
1616 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1617
1618 return move(Result);
1619}