blob: abd26d7d4905cecbe7229d88b7470e4184ddb9ca [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,
Chris Lattner08d92ec2009-12-10 00:32:41 +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();
Chris Lattner46646492009-12-07 01:36:53 +0000217
218 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
219 // and emit a fixit hint for it.
220 if (Next.is(tok::colon) && !ColonIsSacred &&
221 Actions.IsInvalidUnlessNestedName(CurScope, SS, II, ObjectType,
222 EnteringContext) &&
223 // If the token after the colon isn't an identifier, it's still an
224 // error, but they probably meant something else strange so don't
225 // recover like this.
226 PP.LookAhead(1).is(tok::identifier)) {
227 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
228 << CodeModificationHint::CreateReplacement(Next.getLocation(), "::");
229
230 // Recover as if the user wrote '::'.
231 Next.setKind(tok::coloncolon);
232 }
233
Chris Lattner5c7f7862009-06-26 03:52:38 +0000234 if (Next.is(tok::coloncolon)) {
235 // We have an identifier followed by a '::'. Lookup this name
236 // as the name in a nested-name-specifier.
237 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000238 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
239 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000240 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner5c7f7862009-06-26 03:52:38 +0000242 if (!HasScopeSpecifier) {
243 SS.setBeginLoc(IdLoc);
244 HasScopeSpecifier = true;
245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Chris Lattner5c7f7862009-06-26 03:52:38 +0000247 if (SS.isInvalid())
248 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Chris Lattner5c7f7862009-06-26 03:52:38 +0000250 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000251 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000252 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000253 SS.setEndLoc(CCLoc);
254 continue;
255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattner5c7f7862009-06-26 03:52:38 +0000257 // nested-name-specifier:
258 // type-name '<'
259 if (Next.is(tok::less)) {
260 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000261 UnqualifiedId TemplateName;
262 TemplateName.setIdentifier(&II, Tok.getLocation());
263 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
264 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000265 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000266 EnteringContext,
267 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000268 // We have found a template name, so annotate this this token
269 // with a template-id annotation. We do not permit the
270 // template-id to be translated into a type annotation,
271 // because some clients (e.g., the parsing of class template
272 // specializations) still want to see the original template-id
273 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000274 ConsumeToken();
275 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
276 SourceLocation(), false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000277 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000278 continue;
279 }
280 }
281
Douglas Gregor39a8de12009-02-25 19:37:18 +0000282 // We don't have any tokens that form the beginning of a
283 // nested-name-specifier, so we're done.
284 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000285 }
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Douglas Gregor39a8de12009-02-25 19:37:18 +0000287 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000288}
289
290/// ParseCXXIdExpression - Handle id-expression.
291///
292/// id-expression:
293/// unqualified-id
294/// qualified-id
295///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000296/// qualified-id:
297/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
298/// '::' identifier
299/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000300/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000301///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000302/// NOTE: The standard specifies that, for qualified-id, the parser does not
303/// expect:
304///
305/// '::' conversion-function-id
306/// '::' '~' class-name
307///
308/// This may cause a slight inconsistency on diagnostics:
309///
310/// class C {};
311/// namespace A {}
312/// void f() {
313/// :: A :: ~ C(); // Some Sema error about using destructor with a
314/// // namespace.
315/// :: ~ C(); // Some Parser error like 'unexpected ~'.
316/// }
317///
318/// We simplify the parser a bit and make it work like:
319///
320/// qualified-id:
321/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
322/// '::' unqualified-id
323///
324/// That way Sema can handle and report similar errors for namespaces and the
325/// global scope.
326///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000327/// The isAddressOfOperand parameter indicates that this id-expression is a
328/// direct operand of the address-of operator. This is, besides member contexts,
329/// the only place where a qualified-id naming a non-static class member may
330/// appear.
331///
332Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000333 // qualified-id:
334 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
335 // '::' unqualified-id
336 //
337 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000338 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000339
340 UnqualifiedId Name;
341 if (ParseUnqualifiedId(SS,
342 /*EnteringContext=*/false,
343 /*AllowDestructorName=*/false,
344 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000345 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000346 Name))
347 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000348
349 // This is only the direct operand of an & operator if it is not
350 // followed by a postfix-expression suffix.
351 if (isAddressOfOperand) {
352 switch (Tok.getKind()) {
353 case tok::l_square:
354 case tok::l_paren:
355 case tok::arrow:
356 case tok::period:
357 case tok::plusplus:
358 case tok::minusminus:
359 isAddressOfOperand = false;
360 break;
361
362 default:
363 break;
364 }
365 }
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000366
367 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
368 isAddressOfOperand);
369
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000370}
371
Reid Spencer5f016e22007-07-11 17:01:13 +0000372/// ParseCXXCasts - This handles the various ways to cast expressions to another
373/// type.
374///
375/// postfix-expression: [C++ 5.2p1]
376/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
377/// 'static_cast' '<' type-name '>' '(' expression ')'
378/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
379/// 'const_cast' '<' type-name '>' '(' expression ')'
380///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000381Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 tok::TokenKind Kind = Tok.getKind();
383 const char *CastName = 0; // For error messages
384
385 switch (Kind) {
386 default: assert(0 && "Unknown C++ cast!"); abort();
387 case tok::kw_const_cast: CastName = "const_cast"; break;
388 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
389 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
390 case tok::kw_static_cast: CastName = "static_cast"; break;
391 }
392
393 SourceLocation OpLoc = ConsumeToken();
394 SourceLocation LAngleBracketLoc = Tok.getLocation();
395
396 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000397 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000398
Douglas Gregor809070a2009-02-18 17:45:20 +0000399 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 SourceLocation RAngleBracketLoc = Tok.getLocation();
401
Chris Lattner1ab3b962008-11-18 07:48:38 +0000402 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000403 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000404
405 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
406
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000407 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
408 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000409
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000410 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000412 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000413 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000414
Douglas Gregor809070a2009-02-18 17:45:20 +0000415 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000416 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000417 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000418 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000419 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000420
Sebastian Redl20df9b72008-12-11 22:51:44 +0000421 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000422}
423
Sebastian Redlc42e1182008-11-11 11:37:55 +0000424/// ParseCXXTypeid - This handles the C++ typeid expression.
425///
426/// postfix-expression: [C++ 5.2p1]
427/// 'typeid' '(' expression ')'
428/// 'typeid' '(' type-id ')'
429///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000430Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000431 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
432
433 SourceLocation OpLoc = ConsumeToken();
434 SourceLocation LParenLoc = Tok.getLocation();
435 SourceLocation RParenLoc;
436
437 // typeid expressions are always parenthesized.
438 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
439 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000440 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000441
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000442 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000443
444 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000445 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000446
447 // Match the ')'.
448 MatchRHSPunctuation(tok::r_paren, LParenLoc);
449
Douglas Gregor809070a2009-02-18 17:45:20 +0000450 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000451 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000452
453 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000454 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000455 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000456 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000457 // When typeid is applied to an expression other than an lvalue of a
458 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000459 // operand (Clause 5).
460 //
Mike Stump1eb44332009-09-09 15:08:12 +0000461 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000462 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000463 // we the expression is potentially potentially evaluated.
464 EnterExpressionEvaluationContext Unevaluated(Actions,
465 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000466 Result = ParseExpression();
467
468 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000469 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000470 SkipUntil(tok::r_paren);
471 else {
472 MatchRHSPunctuation(tok::r_paren, LParenLoc);
473
474 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000475 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000476 }
477 }
478
Sebastian Redl20df9b72008-12-11 22:51:44 +0000479 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000480}
481
Reid Spencer5f016e22007-07-11 17:01:13 +0000482/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
483///
484/// boolean-literal: [C++ 2.13.5]
485/// 'true'
486/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000487Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000489 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000490}
Chris Lattner50dd2892008-02-26 00:51:44 +0000491
492/// ParseThrowExpression - This handles the C++ throw expression.
493///
494/// throw-expression: [C++ 15]
495/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000496Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000497 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000498 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000499
Chris Lattner2a2819a2008-04-06 06:02:23 +0000500 // If the current token isn't the start of an assignment-expression,
501 // then the expression is not present. This handles things like:
502 // "C ? throw : (void)42", which is crazy but legal.
503 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
504 case tok::semi:
505 case tok::r_paren:
506 case tok::r_square:
507 case tok::r_brace:
508 case tok::colon:
509 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000510 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000511
Chris Lattner2a2819a2008-04-06 06:02:23 +0000512 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000513 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000514 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000515 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000516 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000517}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000518
519/// ParseCXXThis - This handles the C++ 'this' pointer.
520///
521/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
522/// a non-lvalue expression whose value is the address of the object for which
523/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000524Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000525 assert(Tok.is(tok::kw_this) && "Not 'this'!");
526 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000527 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000528}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000529
530/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
531/// Can be interpreted either as function-style casting ("int(x)")
532/// or class type construction ("ClassType(x,y,z)")
533/// or creation of a value-initialized type ("int()").
534///
535/// postfix-expression: [C++ 5.2p1]
536/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
537/// typename-specifier '(' expression-list[opt] ')' [TODO]
538///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000539Parser::OwningExprResult
540Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000541 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000542 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000543
544 assert(Tok.is(tok::l_paren) && "Expected '('!");
545 SourceLocation LParenLoc = ConsumeParen();
546
Sebastian Redla55e52c2008-11-25 22:21:31 +0000547 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000548 CommaLocsTy CommaLocs;
549
550 if (Tok.isNot(tok::r_paren)) {
551 if (ParseExpressionList(Exprs, CommaLocs)) {
552 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000553 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000554 }
555 }
556
557 // Match the ')'.
558 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
559
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000560 // TypeRep could be null, if it references an invalid typedef.
561 if (!TypeRep)
562 return ExprError();
563
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000564 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
565 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000566 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
567 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000568 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000569}
570
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000571/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000572///
573/// condition:
574/// expression
575/// type-specifier-seq declarator '=' assignment-expression
576/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
577/// '=' assignment-expression
578///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000579/// \param ExprResult if the condition was parsed as an expression, the
580/// parsed expression.
581///
582/// \param DeclResult if the condition was parsed as a declaration, the
583/// parsed declaration.
584///
585/// \returns true if there was a parsing, false otherwise.
586bool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
587 DeclPtrTy &DeclResult) {
588 if (!isCXXConditionDeclaration()) {
589 ExprResult = ParseExpression(); // expression
590 DeclResult = DeclPtrTy();
591 return ExprResult.isInvalid();
592 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000593
594 // type-specifier-seq
595 DeclSpec DS;
596 ParseSpecifierQualifierList(DS);
597
598 // declarator
599 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
600 ParseDeclarator(DeclaratorInfo);
601
602 // simple-asm-expr[opt]
603 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000604 SourceLocation Loc;
605 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000606 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000607 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000608 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000609 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000610 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000611 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000612 }
613
614 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000615 if (Tok.is(tok::kw___attribute)) {
616 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000617 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000618 DeclaratorInfo.AddAttributes(AttrList, Loc);
619 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000620
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000621 // Type-check the declaration itself.
622 Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope,
623 DeclaratorInfo);
624 DeclResult = Dcl.get();
625 ExprResult = ExprError();
626
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000627 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000628 if (Tok.is(tok::equal)) {
629 SourceLocation EqualLoc = ConsumeToken();
630 OwningExprResult AssignExpr(ParseAssignmentExpression());
631 if (!AssignExpr.isInvalid())
632 Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
633 } else {
634 // FIXME: C++0x allows a braced-init-list
635 Diag(Tok, diag::err_expected_equal_after_declarator);
636 }
637
638 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000639}
640
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000641/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
642/// This should only be called when the current token is known to be part of
643/// simple-type-specifier.
644///
645/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000646/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000647/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
648/// char
649/// wchar_t
650/// bool
651/// short
652/// int
653/// long
654/// signed
655/// unsigned
656/// float
657/// double
658/// void
659/// [GNU] typeof-specifier
660/// [C++0x] auto [TODO]
661///
662/// type-name:
663/// class-name
664/// enum-name
665/// typedef-name
666///
667void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
668 DS.SetRangeStart(Tok.getLocation());
669 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000670 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000671 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000673 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000674 case tok::identifier: // foo::bar
675 case tok::coloncolon: // ::foo::bar
676 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000677 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000678 assert(0 && "Not a simple-type-specifier token!");
679 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000680
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000681 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000682 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000683 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000684 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000685 break;
686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000688 // builtin types
689 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000690 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000691 break;
692 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000693 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000694 break;
695 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000696 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000697 break;
698 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000699 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000700 break;
701 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000702 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000703 break;
704 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000705 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000706 break;
707 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000708 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000709 break;
710 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000711 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000712 break;
713 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000714 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000715 break;
716 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000717 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000718 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000719 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000720 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000721 break;
722 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000723 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000724 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000725 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000726 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000727 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000729 // GNU typeof support.
730 case tok::kw_typeof:
731 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000732 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000733 return;
734 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000735 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000736 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
737 else
738 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000739 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000740 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000741}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000742
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000743/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
744/// [dcl.name]), which is a non-empty sequence of type-specifiers,
745/// e.g., "const short int". Note that the DeclSpec is *not* finished
746/// by parsing the type-specifier-seq, because these sequences are
747/// typically followed by some form of declarator. Returns true and
748/// emits diagnostics if this is not a type-specifier-seq, false
749/// otherwise.
750///
751/// type-specifier-seq: [C++ 8.1]
752/// type-specifier type-specifier-seq[opt]
753///
754bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
755 DS.SetRangeStart(Tok.getLocation());
756 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000757 unsigned DiagID;
758 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000759
760 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000761 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000762 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000763 return true;
764 }
Mike Stump1eb44332009-09-09 15:08:12 +0000765
John McCallfec54012009-08-03 20:12:06 +0000766 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000767
768 return false;
769}
770
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000771/// \brief Finish parsing a C++ unqualified-id that is a template-id of
772/// some form.
773///
774/// This routine is invoked when a '<' is encountered after an identifier or
775/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
776/// whether the unqualified-id is actually a template-id. This routine will
777/// then parse the template arguments and form the appropriate template-id to
778/// return to the caller.
779///
780/// \param SS the nested-name-specifier that precedes this template-id, if
781/// we're actually parsing a qualified-id.
782///
783/// \param Name for constructor and destructor names, this is the actual
784/// identifier that may be a template-name.
785///
786/// \param NameLoc the location of the class-name in a constructor or
787/// destructor.
788///
789/// \param EnteringContext whether we're entering the scope of the
790/// nested-name-specifier.
791///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000792/// \param ObjectType if this unqualified-id occurs within a member access
793/// expression, the type of the base object whose member is being accessed.
794///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000795/// \param Id as input, describes the template-name or operator-function-id
796/// that precedes the '<'. If template arguments were parsed successfully,
797/// will be updated with the template-id.
798///
799/// \returns true if a parse error occurred, false otherwise.
800bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
801 IdentifierInfo *Name,
802 SourceLocation NameLoc,
803 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000804 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000805 UnqualifiedId &Id) {
806 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
807
808 TemplateTy Template;
809 TemplateNameKind TNK = TNK_Non_template;
810 switch (Id.getKind()) {
811 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000812 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +0000813 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000814 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
815 Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000816 break;
817
Douglas Gregor014e88d2009-11-03 23:16:33 +0000818 case UnqualifiedId::IK_ConstructorName: {
819 UnqualifiedId TemplateName;
820 TemplateName.setIdentifier(Name, NameLoc);
821 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
822 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000823 break;
824 }
825
Douglas Gregor014e88d2009-11-03 23:16:33 +0000826 case UnqualifiedId::IK_DestructorName: {
827 UnqualifiedId TemplateName;
828 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000829 if (ObjectType) {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000830 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
Douglas Gregora481edb2009-11-20 23:39:24 +0000831 TemplateName, ObjectType,
832 EnteringContext);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000833 TNK = TNK_Dependent_template_name;
834 if (!Template.get())
835 return true;
836 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000837 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000838 EnteringContext, Template);
839
840 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
841 // The identifier following the destructor did not refer to a template
842 // or to a type. Complain.
843 if (ObjectType)
844 Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
845 << Name;
846 else
847 Diag(NameLoc, diag::err_destructor_class_name);
848 return true;
849 }
850 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000851 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000852 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000853
854 default:
855 return false;
856 }
857
858 if (TNK == TNK_Non_template)
859 return false;
860
861 // Parse the enclosed template argument list.
862 SourceLocation LAngleLoc, RAngleLoc;
863 TemplateArgList TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000864 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
865 &SS, true, LAngleLoc,
866 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000867 RAngleLoc))
868 return true;
869
870 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +0000871 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
872 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000873 // Form a parsed representation of the template-id to be stored in the
874 // UnqualifiedId.
875 TemplateIdAnnotation *TemplateId
876 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
877
878 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
879 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000880 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000881 TemplateId->TemplateNameLoc = Id.StartLocation;
882 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000883 TemplateId->Name = 0;
884 TemplateId->Operator = Id.OperatorFunctionId.Operator;
885 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000886 }
887
888 TemplateId->Template = Template.getAs<void*>();
889 TemplateId->Kind = TNK;
890 TemplateId->LAngleLoc = LAngleLoc;
891 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +0000892 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000893 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +0000894 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000895 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000896
897 Id.setTemplateId(TemplateId);
898 return false;
899 }
900
901 // Bundle the template arguments together.
902 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000903 TemplateArgs.size());
904
905 // Constructor and destructor names.
906 Action::TypeResult Type
907 = Actions.ActOnTemplateIdType(Template, NameLoc,
908 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000909 RAngleLoc);
910 if (Type.isInvalid())
911 return true;
912
913 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
914 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
915 else
916 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
917
918 return false;
919}
920
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000921/// \brief Parse an operator-function-id or conversion-function-id as part
922/// of a C++ unqualified-id.
923///
924/// This routine is responsible only for parsing the operator-function-id or
925/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000926///
927/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000928/// operator-function-id: [C++ 13.5]
929/// 'operator' operator
930///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000931/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000932/// new delete new[] delete[]
933/// + - * / % ^ & | ~
934/// ! = < > += -= *= /= %=
935/// ^= &= |= << >> >>= <<= == !=
936/// <= >= && || ++ -- , ->* ->
937/// () []
938///
939/// conversion-function-id: [C++ 12.3.2]
940/// operator conversion-type-id
941///
942/// conversion-type-id:
943/// type-specifier-seq conversion-declarator[opt]
944///
945/// conversion-declarator:
946/// ptr-operator conversion-declarator[opt]
947/// \endcode
948///
949/// \param The nested-name-specifier that preceded this unqualified-id. If
950/// non-empty, then we are parsing the unqualified-id of a qualified-id.
951///
952/// \param EnteringContext whether we are entering the scope of the
953/// nested-name-specifier.
954///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000955/// \param ObjectType if this unqualified-id occurs within a member access
956/// expression, the type of the base object whose member is being accessed.
957///
958/// \param Result on a successful parse, contains the parsed unqualified-id.
959///
960/// \returns true if parsing fails, false otherwise.
961bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
962 TypeTy *ObjectType,
963 UnqualifiedId &Result) {
964 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
965
966 // Consume the 'operator' keyword.
967 SourceLocation KeywordLoc = ConsumeToken();
968
969 // Determine what kind of operator name we have.
970 unsigned SymbolIdx = 0;
971 SourceLocation SymbolLocations[3];
972 OverloadedOperatorKind Op = OO_None;
973 switch (Tok.getKind()) {
974 case tok::kw_new:
975 case tok::kw_delete: {
976 bool isNew = Tok.getKind() == tok::kw_new;
977 // Consume the 'new' or 'delete'.
978 SymbolLocations[SymbolIdx++] = ConsumeToken();
979 if (Tok.is(tok::l_square)) {
980 // Consume the '['.
981 SourceLocation LBracketLoc = ConsumeBracket();
982 // Consume the ']'.
983 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
984 LBracketLoc);
985 if (RBracketLoc.isInvalid())
986 return true;
987
988 SymbolLocations[SymbolIdx++] = LBracketLoc;
989 SymbolLocations[SymbolIdx++] = RBracketLoc;
990 Op = isNew? OO_Array_New : OO_Array_Delete;
991 } else {
992 Op = isNew? OO_New : OO_Delete;
993 }
994 break;
995 }
996
997#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
998 case tok::Token: \
999 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1000 Op = OO_##Name; \
1001 break;
1002#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1003#include "clang/Basic/OperatorKinds.def"
1004
1005 case tok::l_paren: {
1006 // Consume the '('.
1007 SourceLocation LParenLoc = ConsumeParen();
1008 // Consume the ')'.
1009 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1010 LParenLoc);
1011 if (RParenLoc.isInvalid())
1012 return true;
1013
1014 SymbolLocations[SymbolIdx++] = LParenLoc;
1015 SymbolLocations[SymbolIdx++] = RParenLoc;
1016 Op = OO_Call;
1017 break;
1018 }
1019
1020 case tok::l_square: {
1021 // Consume the '['.
1022 SourceLocation LBracketLoc = ConsumeBracket();
1023 // Consume the ']'.
1024 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1025 LBracketLoc);
1026 if (RBracketLoc.isInvalid())
1027 return true;
1028
1029 SymbolLocations[SymbolIdx++] = LBracketLoc;
1030 SymbolLocations[SymbolIdx++] = RBracketLoc;
1031 Op = OO_Subscript;
1032 break;
1033 }
1034
1035 case tok::code_completion: {
1036 // Code completion for the operator name.
1037 Actions.CodeCompleteOperatorName(CurScope);
1038
1039 // Consume the operator token.
1040 ConsumeToken();
1041
1042 // Don't try to parse any further.
1043 return true;
1044 }
1045
1046 default:
1047 break;
1048 }
1049
1050 if (Op != OO_None) {
1051 // We have parsed an operator-function-id.
1052 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1053 return false;
1054 }
Sean Hunt0486d742009-11-28 04:44:28 +00001055
1056 // Parse a literal-operator-id.
1057 //
1058 // literal-operator-id: [C++0x 13.5.8]
1059 // operator "" identifier
1060
1061 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1062 if (Tok.getLength() != 2)
1063 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1064 ConsumeStringToken();
1065
1066 if (Tok.isNot(tok::identifier)) {
1067 Diag(Tok.getLocation(), diag::err_expected_ident);
1068 return true;
1069 }
1070
1071 IdentifierInfo *II = Tok.getIdentifierInfo();
1072 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001073 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001074 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001075
1076 // Parse a conversion-function-id.
1077 //
1078 // conversion-function-id: [C++ 12.3.2]
1079 // operator conversion-type-id
1080 //
1081 // conversion-type-id:
1082 // type-specifier-seq conversion-declarator[opt]
1083 //
1084 // conversion-declarator:
1085 // ptr-operator conversion-declarator[opt]
1086
1087 // Parse the type-specifier-seq.
1088 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001089 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001090 return true;
1091
1092 // Parse the conversion-declarator, which is merely a sequence of
1093 // ptr-operators.
1094 Declarator D(DS, Declarator::TypeNameContext);
1095 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1096
1097 // Finish up the type.
1098 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1099 if (Ty.isInvalid())
1100 return true;
1101
1102 // Note that this is a conversion-function-id.
1103 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1104 D.getSourceRange().getEnd());
1105 return false;
1106}
1107
1108/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1109/// name of an entity.
1110///
1111/// \code
1112/// unqualified-id: [C++ expr.prim.general]
1113/// identifier
1114/// operator-function-id
1115/// conversion-function-id
1116/// [C++0x] literal-operator-id [TODO]
1117/// ~ class-name
1118/// template-id
1119///
1120/// \endcode
1121///
1122/// \param The nested-name-specifier that preceded this unqualified-id. If
1123/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1124///
1125/// \param EnteringContext whether we are entering the scope of the
1126/// nested-name-specifier.
1127///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001128/// \param AllowDestructorName whether we allow parsing of a destructor name.
1129///
1130/// \param AllowConstructorName whether we allow parsing a constructor name.
1131///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001132/// \param ObjectType if this unqualified-id occurs within a member access
1133/// expression, the type of the base object whose member is being accessed.
1134///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001135/// \param Result on a successful parse, contains the parsed unqualified-id.
1136///
1137/// \returns true if parsing fails, false otherwise.
1138bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1139 bool AllowDestructorName,
1140 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001141 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001142 UnqualifiedId &Result) {
1143 // unqualified-id:
1144 // identifier
1145 // template-id (when it hasn't already been annotated)
1146 if (Tok.is(tok::identifier)) {
1147 // Consume the identifier.
1148 IdentifierInfo *Id = Tok.getIdentifierInfo();
1149 SourceLocation IdLoc = ConsumeToken();
1150
1151 if (AllowConstructorName &&
1152 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1153 // We have parsed a constructor name.
1154 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1155 &SS, false),
1156 IdLoc, IdLoc);
1157 } else {
1158 // We have parsed an identifier.
1159 Result.setIdentifier(Id, IdLoc);
1160 }
1161
1162 // If the next token is a '<', we may have a template.
1163 if (Tok.is(tok::less))
1164 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001165 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001166
1167 return false;
1168 }
1169
1170 // unqualified-id:
1171 // template-id (already parsed and annotated)
1172 if (Tok.is(tok::annot_template_id)) {
1173 // FIXME: Could this be a constructor name???
1174
1175 // We have already parsed a template-id; consume the annotation token as
1176 // our unqualified-id.
1177 Result.setTemplateId(
1178 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1179 ConsumeToken();
1180 return false;
1181 }
1182
1183 // unqualified-id:
1184 // operator-function-id
1185 // conversion-function-id
1186 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001187 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001188 return true;
1189
Sean Hunte6252d12009-11-28 08:58:14 +00001190 // If we have an operator-function-id or a literal-operator-id and the next
1191 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001192 //
1193 // template-id:
1194 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001195 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1196 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001197 Tok.is(tok::less))
1198 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1199 EnteringContext, ObjectType,
1200 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001201
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001202 return false;
1203 }
1204
1205 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1206 // C++ [expr.unary.op]p10:
1207 // There is an ambiguity in the unary-expression ~X(), where X is a
1208 // class-name. The ambiguity is resolved in favor of treating ~ as a
1209 // unary complement rather than treating ~X as referring to a destructor.
1210
1211 // Parse the '~'.
1212 SourceLocation TildeLoc = ConsumeToken();
1213
1214 // Parse the class-name.
1215 if (Tok.isNot(tok::identifier)) {
1216 Diag(Tok, diag::err_destructor_class_name);
1217 return true;
1218 }
1219
1220 // Parse the class-name (or template-name in a simple-template-id).
1221 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1222 SourceLocation ClassNameLoc = ConsumeToken();
1223
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001224 if (Tok.is(tok::less)) {
1225 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1226 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1227 EnteringContext, ObjectType, Result);
1228 }
1229
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001230 // Note that this is a destructor name.
1231 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001232 CurScope, &SS, false, ObjectType);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001233 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001234 if (ObjectType)
1235 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1236 << ClassName;
1237 else
1238 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001239 return true;
1240 }
1241
1242 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001243 return false;
1244 }
1245
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001246 Diag(Tok, diag::err_expected_unqualified_id)
1247 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001248 return true;
1249}
1250
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001251/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1252/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001253///
Chris Lattner59232d32009-01-04 21:25:24 +00001254/// This method is called to parse the new expression after the optional :: has
1255/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1256/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001257///
1258/// new-expression:
1259/// '::'[opt] 'new' new-placement[opt] new-type-id
1260/// new-initializer[opt]
1261/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1262/// new-initializer[opt]
1263///
1264/// new-placement:
1265/// '(' expression-list ')'
1266///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001267/// new-type-id:
1268/// type-specifier-seq new-declarator[opt]
1269///
1270/// new-declarator:
1271/// ptr-operator new-declarator[opt]
1272/// direct-new-declarator
1273///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001274/// new-initializer:
1275/// '(' expression-list[opt] ')'
1276/// [C++0x] braced-init-list [TODO]
1277///
Chris Lattner59232d32009-01-04 21:25:24 +00001278Parser::OwningExprResult
1279Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1280 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1281 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001282
1283 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1284 // second form of new-expression. It can't be a new-type-id.
1285
Sebastian Redla55e52c2008-11-25 22:21:31 +00001286 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001287 SourceLocation PlacementLParen, PlacementRParen;
1288
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001289 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001290 DeclSpec DS;
1291 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001292 if (Tok.is(tok::l_paren)) {
1293 // If it turns out to be a placement, we change the type location.
1294 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001295 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1296 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001297 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001298 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001299
1300 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001301 if (PlacementRParen.isInvalid()) {
1302 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001303 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001304 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001305
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001306 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001307 // Reset the placement locations. There was no placement.
1308 PlacementLParen = PlacementRParen = SourceLocation();
1309 ParenTypeId = true;
1310 } else {
1311 // We still need the type.
1312 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001313 SourceLocation LParen = ConsumeParen();
1314 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001315 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001316 ParseDeclarator(DeclaratorInfo);
1317 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001318 ParenTypeId = true;
1319 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001320 if (ParseCXXTypeSpecifierSeq(DS))
1321 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001322 else {
1323 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001324 ParseDeclaratorInternal(DeclaratorInfo,
1325 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001326 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001327 ParenTypeId = false;
1328 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001329 }
1330 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001331 // A new-type-id is a simplified type-id, where essentially the
1332 // direct-declarator is replaced by a direct-new-declarator.
1333 if (ParseCXXTypeSpecifierSeq(DS))
1334 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001335 else {
1336 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001337 ParseDeclaratorInternal(DeclaratorInfo,
1338 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001339 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001340 ParenTypeId = false;
1341 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001342 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001343 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
Sebastian Redla55e52c2008-11-25 22:21:31 +00001347 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001348 SourceLocation ConstructorLParen, ConstructorRParen;
1349
1350 if (Tok.is(tok::l_paren)) {
1351 ConstructorLParen = ConsumeParen();
1352 if (Tok.isNot(tok::r_paren)) {
1353 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001354 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1355 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001356 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001357 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001358 }
1359 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001360 if (ConstructorRParen.isInvalid()) {
1361 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001362 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001363 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001364 }
1365
Sebastian Redlf53597f2009-03-15 17:47:39 +00001366 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1367 move_arg(PlacementArgs), PlacementRParen,
1368 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1369 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001370}
1371
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001372/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1373/// passed to ParseDeclaratorInternal.
1374///
1375/// direct-new-declarator:
1376/// '[' expression ']'
1377/// direct-new-declarator '[' constant-expression ']'
1378///
Chris Lattner59232d32009-01-04 21:25:24 +00001379void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001380 // Parse the array dimensions.
1381 bool first = true;
1382 while (Tok.is(tok::l_square)) {
1383 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001384 OwningExprResult Size(first ? ParseExpression()
1385 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001386 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001387 // Recover
1388 SkipUntil(tok::r_square);
1389 return;
1390 }
1391 first = false;
1392
Sebastian Redlab197ba2009-02-09 18:23:29 +00001393 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001394 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001395 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001396 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001397
Sebastian Redlab197ba2009-02-09 18:23:29 +00001398 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001399 return;
1400 }
1401}
1402
1403/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1404/// This ambiguity appears in the syntax of the C++ new operator.
1405///
1406/// new-expression:
1407/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1408/// new-initializer[opt]
1409///
1410/// new-placement:
1411/// '(' expression-list ')'
1412///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001413bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001414 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001415 // The '(' was already consumed.
1416 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001417 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001418 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001419 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001420 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001421 }
1422
1423 // It's not a type, it has to be an expression list.
1424 // Discard the comma locations - ActOnCXXNew has enough parameters.
1425 CommaLocsTy CommaLocs;
1426 return ParseExpressionList(PlacementArgs, CommaLocs);
1427}
1428
1429/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1430/// to free memory allocated by new.
1431///
Chris Lattner59232d32009-01-04 21:25:24 +00001432/// This method is called to parse the 'delete' expression after the optional
1433/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1434/// and "Start" is its location. Otherwise, "Start" is the location of the
1435/// 'delete' token.
1436///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001437/// delete-expression:
1438/// '::'[opt] 'delete' cast-expression
1439/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001440Parser::OwningExprResult
1441Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1442 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1443 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001444
1445 // Array delete?
1446 bool ArrayDelete = false;
1447 if (Tok.is(tok::l_square)) {
1448 ArrayDelete = true;
1449 SourceLocation LHS = ConsumeBracket();
1450 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1451 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001452 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001453 }
1454
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001455 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001456 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001457 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001458
Sebastian Redlf53597f2009-03-15 17:47:39 +00001459 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001460}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001461
Mike Stump1eb44332009-09-09 15:08:12 +00001462static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001463 switch(kind) {
1464 default: assert(false && "Not a known unary type trait.");
1465 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1466 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1467 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1468 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1469 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1470 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1471 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1472 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1473 case tok::kw___is_abstract: return UTT_IsAbstract;
1474 case tok::kw___is_class: return UTT_IsClass;
1475 case tok::kw___is_empty: return UTT_IsEmpty;
1476 case tok::kw___is_enum: return UTT_IsEnum;
1477 case tok::kw___is_pod: return UTT_IsPOD;
1478 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1479 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001480 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001481 }
1482}
1483
1484/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1485/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1486/// templates.
1487///
1488/// primary-expression:
1489/// [GNU] unary-type-trait '(' type-id ')'
1490///
Mike Stump1eb44332009-09-09 15:08:12 +00001491Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001492 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1493 SourceLocation Loc = ConsumeToken();
1494
1495 SourceLocation LParen = Tok.getLocation();
1496 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1497 return ExprError();
1498
1499 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1500 // there will be cryptic errors about mismatched parentheses and missing
1501 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001502 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001503
1504 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1505
Douglas Gregor809070a2009-02-18 17:45:20 +00001506 if (Ty.isInvalid())
1507 return ExprError();
1508
1509 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001510}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001511
1512/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1513/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1514/// based on the context past the parens.
1515Parser::OwningExprResult
1516Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1517 TypeTy *&CastTy,
1518 SourceLocation LParenLoc,
1519 SourceLocation &RParenLoc) {
1520 assert(getLang().CPlusPlus && "Should only be called for C++!");
1521 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1522 assert(isTypeIdInParens() && "Not a type-id!");
1523
1524 OwningExprResult Result(Actions, true);
1525 CastTy = 0;
1526
1527 // We need to disambiguate a very ugly part of the C++ syntax:
1528 //
1529 // (T())x; - type-id
1530 // (T())*x; - type-id
1531 // (T())/x; - expression
1532 // (T()); - expression
1533 //
1534 // The bad news is that we cannot use the specialized tentative parser, since
1535 // it can only verify that the thing inside the parens can be parsed as
1536 // type-id, it is not useful for determining the context past the parens.
1537 //
1538 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001539 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001540 //
1541 // It uses a scheme similar to parsing inline methods. The parenthesized
1542 // tokens are cached, the context that follows is determined (possibly by
1543 // parsing a cast-expression), and then we re-introduce the cached tokens
1544 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001545
Mike Stump1eb44332009-09-09 15:08:12 +00001546 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001547 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001548
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001549 // Store the tokens of the parentheses. We will parse them after we determine
1550 // the context that follows them.
1551 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1552 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001553 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1554 return ExprError();
1555 }
1556
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001557 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001558 ParseAs = CompoundLiteral;
1559 } else {
1560 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001561 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1562 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1563 NotCastExpr = true;
1564 } else {
1565 // Try parsing the cast-expression that may follow.
1566 // If it is not a cast-expression, NotCastExpr will be true and no token
1567 // will be consumed.
1568 Result = ParseCastExpression(false/*isUnaryExpression*/,
1569 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001570 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001571 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001572
1573 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1574 // an expression.
1575 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001576 }
1577
Mike Stump1eb44332009-09-09 15:08:12 +00001578 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001579 Toks.push_back(Tok);
1580 // Re-enter the stored parenthesized tokens into the token stream, so we may
1581 // parse them now.
1582 PP.EnterTokenStream(Toks.data(), Toks.size(),
1583 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1584 // Drop the current token and bring the first cached one. It's the same token
1585 // as when we entered this function.
1586 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001587
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001588 if (ParseAs >= CompoundLiteral) {
1589 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001590
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001591 // Match the ')'.
1592 if (Tok.is(tok::r_paren))
1593 RParenLoc = ConsumeParen();
1594 else
1595 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001596
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001597 if (ParseAs == CompoundLiteral) {
1598 ExprType = CompoundLiteral;
1599 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1600 }
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001602 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1603 assert(ParseAs == CastExpr);
1604
1605 if (Ty.isInvalid())
1606 return ExprError();
1607
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001608 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001609
1610 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001611 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001612 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001613 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001614 return move(Result);
1615 }
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001617 // Not a compound literal, and not followed by a cast-expression.
1618 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001619
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001620 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001621 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001622 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1623 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1624
1625 // Match the ')'.
1626 if (Result.isInvalid()) {
1627 SkipUntil(tok::r_paren);
1628 return ExprError();
1629 }
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001631 if (Tok.is(tok::r_paren))
1632 RParenLoc = ConsumeParen();
1633 else
1634 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1635
1636 return move(Result);
1637}