blob: 52003e6fa1b26526833583e46ab31ee2948cde35 [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());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001055 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001056 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001057
1058 // Parse a conversion-function-id.
1059 //
1060 // conversion-function-id: [C++ 12.3.2]
1061 // operator conversion-type-id
1062 //
1063 // conversion-type-id:
1064 // type-specifier-seq conversion-declarator[opt]
1065 //
1066 // conversion-declarator:
1067 // ptr-operator conversion-declarator[opt]
1068
1069 // Parse the type-specifier-seq.
1070 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001071 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001072 return true;
1073
1074 // Parse the conversion-declarator, which is merely a sequence of
1075 // ptr-operators.
1076 Declarator D(DS, Declarator::TypeNameContext);
1077 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1078
1079 // Finish up the type.
1080 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1081 if (Ty.isInvalid())
1082 return true;
1083
1084 // Note that this is a conversion-function-id.
1085 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1086 D.getSourceRange().getEnd());
1087 return false;
1088}
1089
1090/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1091/// name of an entity.
1092///
1093/// \code
1094/// unqualified-id: [C++ expr.prim.general]
1095/// identifier
1096/// operator-function-id
1097/// conversion-function-id
1098/// [C++0x] literal-operator-id [TODO]
1099/// ~ class-name
1100/// template-id
1101///
1102/// \endcode
1103///
1104/// \param The nested-name-specifier that preceded this unqualified-id. If
1105/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1106///
1107/// \param EnteringContext whether we are entering the scope of the
1108/// nested-name-specifier.
1109///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001110/// \param AllowDestructorName whether we allow parsing of a destructor name.
1111///
1112/// \param AllowConstructorName whether we allow parsing a constructor name.
1113///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001114/// \param ObjectType if this unqualified-id occurs within a member access
1115/// expression, the type of the base object whose member is being accessed.
1116///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001117/// \param Result on a successful parse, contains the parsed unqualified-id.
1118///
1119/// \returns true if parsing fails, false otherwise.
1120bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1121 bool AllowDestructorName,
1122 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001123 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001124 UnqualifiedId &Result) {
1125 // unqualified-id:
1126 // identifier
1127 // template-id (when it hasn't already been annotated)
1128 if (Tok.is(tok::identifier)) {
1129 // Consume the identifier.
1130 IdentifierInfo *Id = Tok.getIdentifierInfo();
1131 SourceLocation IdLoc = ConsumeToken();
1132
1133 if (AllowConstructorName &&
1134 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1135 // We have parsed a constructor name.
1136 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1137 &SS, false),
1138 IdLoc, IdLoc);
1139 } else {
1140 // We have parsed an identifier.
1141 Result.setIdentifier(Id, IdLoc);
1142 }
1143
1144 // If the next token is a '<', we may have a template.
1145 if (Tok.is(tok::less))
1146 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001147 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001148
1149 return false;
1150 }
1151
1152 // unqualified-id:
1153 // template-id (already parsed and annotated)
1154 if (Tok.is(tok::annot_template_id)) {
1155 // FIXME: Could this be a constructor name???
1156
1157 // We have already parsed a template-id; consume the annotation token as
1158 // our unqualified-id.
1159 Result.setTemplateId(
1160 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1161 ConsumeToken();
1162 return false;
1163 }
1164
1165 // unqualified-id:
1166 // operator-function-id
1167 // conversion-function-id
1168 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001169 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001170 return true;
1171
Sean Hunte6252d12009-11-28 08:58:14 +00001172 // If we have an operator-function-id or a literal-operator-id and the next
1173 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001174 //
1175 // template-id:
1176 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001177 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1178 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001179 Tok.is(tok::less))
1180 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1181 EnteringContext, ObjectType,
1182 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001183
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001184 return false;
1185 }
1186
1187 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1188 // C++ [expr.unary.op]p10:
1189 // There is an ambiguity in the unary-expression ~X(), where X is a
1190 // class-name. The ambiguity is resolved in favor of treating ~ as a
1191 // unary complement rather than treating ~X as referring to a destructor.
1192
1193 // Parse the '~'.
1194 SourceLocation TildeLoc = ConsumeToken();
1195
1196 // Parse the class-name.
1197 if (Tok.isNot(tok::identifier)) {
1198 Diag(Tok, diag::err_destructor_class_name);
1199 return true;
1200 }
1201
1202 // Parse the class-name (or template-name in a simple-template-id).
1203 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1204 SourceLocation ClassNameLoc = ConsumeToken();
1205
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001206 if (Tok.is(tok::less)) {
1207 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1208 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1209 EnteringContext, ObjectType, Result);
1210 }
1211
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001212 // Note that this is a destructor name.
1213 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001214 CurScope, &SS, false, ObjectType);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001215 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001216 if (ObjectType)
1217 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1218 << ClassName;
1219 else
1220 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001221 return true;
1222 }
1223
1224 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001225 return false;
1226 }
1227
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001228 Diag(Tok, diag::err_expected_unqualified_id)
1229 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001230 return true;
1231}
1232
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001233/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1234/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001235///
Chris Lattner59232d32009-01-04 21:25:24 +00001236/// This method is called to parse the new expression after the optional :: has
1237/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1238/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001239///
1240/// new-expression:
1241/// '::'[opt] 'new' new-placement[opt] new-type-id
1242/// new-initializer[opt]
1243/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1244/// new-initializer[opt]
1245///
1246/// new-placement:
1247/// '(' expression-list ')'
1248///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001249/// new-type-id:
1250/// type-specifier-seq new-declarator[opt]
1251///
1252/// new-declarator:
1253/// ptr-operator new-declarator[opt]
1254/// direct-new-declarator
1255///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001256/// new-initializer:
1257/// '(' expression-list[opt] ')'
1258/// [C++0x] braced-init-list [TODO]
1259///
Chris Lattner59232d32009-01-04 21:25:24 +00001260Parser::OwningExprResult
1261Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1262 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1263 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001264
1265 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1266 // second form of new-expression. It can't be a new-type-id.
1267
Sebastian Redla55e52c2008-11-25 22:21:31 +00001268 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001269 SourceLocation PlacementLParen, PlacementRParen;
1270
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001271 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001272 DeclSpec DS;
1273 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001274 if (Tok.is(tok::l_paren)) {
1275 // If it turns out to be a placement, we change the type location.
1276 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001277 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1278 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001279 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001280 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001281
1282 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001283 if (PlacementRParen.isInvalid()) {
1284 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001285 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001286 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001287
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001288 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001289 // Reset the placement locations. There was no placement.
1290 PlacementLParen = PlacementRParen = SourceLocation();
1291 ParenTypeId = true;
1292 } else {
1293 // We still need the type.
1294 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001295 SourceLocation LParen = ConsumeParen();
1296 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001297 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001298 ParseDeclarator(DeclaratorInfo);
1299 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001300 ParenTypeId = true;
1301 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001302 if (ParseCXXTypeSpecifierSeq(DS))
1303 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001304 else {
1305 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001306 ParseDeclaratorInternal(DeclaratorInfo,
1307 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001308 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001309 ParenTypeId = false;
1310 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001311 }
1312 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001313 // A new-type-id is a simplified type-id, where essentially the
1314 // direct-declarator is replaced by a direct-new-declarator.
1315 if (ParseCXXTypeSpecifierSeq(DS))
1316 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001317 else {
1318 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001319 ParseDeclaratorInternal(DeclaratorInfo,
1320 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001321 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001322 ParenTypeId = false;
1323 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001324 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001325 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001326 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001327 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001328
Sebastian Redla55e52c2008-11-25 22:21:31 +00001329 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001330 SourceLocation ConstructorLParen, ConstructorRParen;
1331
1332 if (Tok.is(tok::l_paren)) {
1333 ConstructorLParen = ConsumeParen();
1334 if (Tok.isNot(tok::r_paren)) {
1335 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001336 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1337 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001338 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001339 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001340 }
1341 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001342 if (ConstructorRParen.isInvalid()) {
1343 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001344 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001345 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001346 }
1347
Sebastian Redlf53597f2009-03-15 17:47:39 +00001348 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1349 move_arg(PlacementArgs), PlacementRParen,
1350 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1351 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001352}
1353
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001354/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1355/// passed to ParseDeclaratorInternal.
1356///
1357/// direct-new-declarator:
1358/// '[' expression ']'
1359/// direct-new-declarator '[' constant-expression ']'
1360///
Chris Lattner59232d32009-01-04 21:25:24 +00001361void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001362 // Parse the array dimensions.
1363 bool first = true;
1364 while (Tok.is(tok::l_square)) {
1365 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001366 OwningExprResult Size(first ? ParseExpression()
1367 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001368 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001369 // Recover
1370 SkipUntil(tok::r_square);
1371 return;
1372 }
1373 first = false;
1374
Sebastian Redlab197ba2009-02-09 18:23:29 +00001375 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001376 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001377 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001378 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001379
Sebastian Redlab197ba2009-02-09 18:23:29 +00001380 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001381 return;
1382 }
1383}
1384
1385/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1386/// This ambiguity appears in the syntax of the C++ new operator.
1387///
1388/// new-expression:
1389/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1390/// new-initializer[opt]
1391///
1392/// new-placement:
1393/// '(' expression-list ')'
1394///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001395bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001396 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001397 // The '(' was already consumed.
1398 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001399 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001400 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001401 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001402 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001403 }
1404
1405 // It's not a type, it has to be an expression list.
1406 // Discard the comma locations - ActOnCXXNew has enough parameters.
1407 CommaLocsTy CommaLocs;
1408 return ParseExpressionList(PlacementArgs, CommaLocs);
1409}
1410
1411/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1412/// to free memory allocated by new.
1413///
Chris Lattner59232d32009-01-04 21:25:24 +00001414/// This method is called to parse the 'delete' expression after the optional
1415/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1416/// and "Start" is its location. Otherwise, "Start" is the location of the
1417/// 'delete' token.
1418///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001419/// delete-expression:
1420/// '::'[opt] 'delete' cast-expression
1421/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001422Parser::OwningExprResult
1423Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1424 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1425 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001426
1427 // Array delete?
1428 bool ArrayDelete = false;
1429 if (Tok.is(tok::l_square)) {
1430 ArrayDelete = true;
1431 SourceLocation LHS = ConsumeBracket();
1432 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1433 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001434 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001435 }
1436
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001437 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001438 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001439 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001440
Sebastian Redlf53597f2009-03-15 17:47:39 +00001441 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001442}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001443
Mike Stump1eb44332009-09-09 15:08:12 +00001444static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001445 switch(kind) {
1446 default: assert(false && "Not a known unary type trait.");
1447 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1448 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1449 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1450 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1451 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1452 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1453 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1454 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1455 case tok::kw___is_abstract: return UTT_IsAbstract;
1456 case tok::kw___is_class: return UTT_IsClass;
1457 case tok::kw___is_empty: return UTT_IsEmpty;
1458 case tok::kw___is_enum: return UTT_IsEnum;
1459 case tok::kw___is_pod: return UTT_IsPOD;
1460 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1461 case tok::kw___is_union: return UTT_IsUnion;
1462 }
1463}
1464
1465/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1466/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1467/// templates.
1468///
1469/// primary-expression:
1470/// [GNU] unary-type-trait '(' type-id ')'
1471///
Mike Stump1eb44332009-09-09 15:08:12 +00001472Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001473 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1474 SourceLocation Loc = ConsumeToken();
1475
1476 SourceLocation LParen = Tok.getLocation();
1477 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1478 return ExprError();
1479
1480 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1481 // there will be cryptic errors about mismatched parentheses and missing
1482 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001483 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001484
1485 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1486
Douglas Gregor809070a2009-02-18 17:45:20 +00001487 if (Ty.isInvalid())
1488 return ExprError();
1489
1490 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001491}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001492
1493/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1494/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1495/// based on the context past the parens.
1496Parser::OwningExprResult
1497Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1498 TypeTy *&CastTy,
1499 SourceLocation LParenLoc,
1500 SourceLocation &RParenLoc) {
1501 assert(getLang().CPlusPlus && "Should only be called for C++!");
1502 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1503 assert(isTypeIdInParens() && "Not a type-id!");
1504
1505 OwningExprResult Result(Actions, true);
1506 CastTy = 0;
1507
1508 // We need to disambiguate a very ugly part of the C++ syntax:
1509 //
1510 // (T())x; - type-id
1511 // (T())*x; - type-id
1512 // (T())/x; - expression
1513 // (T()); - expression
1514 //
1515 // The bad news is that we cannot use the specialized tentative parser, since
1516 // it can only verify that the thing inside the parens can be parsed as
1517 // type-id, it is not useful for determining the context past the parens.
1518 //
1519 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001520 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001521 //
1522 // It uses a scheme similar to parsing inline methods. The parenthesized
1523 // tokens are cached, the context that follows is determined (possibly by
1524 // parsing a cast-expression), and then we re-introduce the cached tokens
1525 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001526
Mike Stump1eb44332009-09-09 15:08:12 +00001527 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001528 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001529
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001530 // Store the tokens of the parentheses. We will parse them after we determine
1531 // the context that follows them.
1532 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1533 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001534 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1535 return ExprError();
1536 }
1537
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001538 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001539 ParseAs = CompoundLiteral;
1540 } else {
1541 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001542 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1543 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1544 NotCastExpr = true;
1545 } else {
1546 // Try parsing the cast-expression that may follow.
1547 // If it is not a cast-expression, NotCastExpr will be true and no token
1548 // will be consumed.
1549 Result = ParseCastExpression(false/*isUnaryExpression*/,
1550 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001551 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001552 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001553
1554 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1555 // an expression.
1556 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001557 }
1558
Mike Stump1eb44332009-09-09 15:08:12 +00001559 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001560 Toks.push_back(Tok);
1561 // Re-enter the stored parenthesized tokens into the token stream, so we may
1562 // parse them now.
1563 PP.EnterTokenStream(Toks.data(), Toks.size(),
1564 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1565 // Drop the current token and bring the first cached one. It's the same token
1566 // as when we entered this function.
1567 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001568
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001569 if (ParseAs >= CompoundLiteral) {
1570 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001571
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001572 // Match the ')'.
1573 if (Tok.is(tok::r_paren))
1574 RParenLoc = ConsumeParen();
1575 else
1576 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001577
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001578 if (ParseAs == CompoundLiteral) {
1579 ExprType = CompoundLiteral;
1580 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1581 }
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001583 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1584 assert(ParseAs == CastExpr);
1585
1586 if (Ty.isInvalid())
1587 return ExprError();
1588
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001589 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001590
1591 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001592 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001593 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001594 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001595 return move(Result);
1596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001598 // Not a compound literal, and not followed by a cast-expression.
1599 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001600
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001601 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001602 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001603 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1604 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1605
1606 // Match the ')'.
1607 if (Result.isInvalid()) {
1608 SkipUntil(tok::r_paren);
1609 return ExprError();
1610 }
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001612 if (Tok.is(tok::r_paren))
1613 RParenLoc = ConsumeParen();
1614 else
1615 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1616
1617 return move(Result);
1618}