blob: ae2a47befd41c533ff09462c4bd2630e417c3148 [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///
Chris Lattner46646492009-12-07 01:36:53 +000048/// \param ColonIsSacred - If this is true, then a colon is valid after the
49/// specifier, so we should not try to recover from colons aggressively.
50///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000052bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000053 Action::TypeTy *ObjectType,
Chris Lattner46646492009-12-07 01:36:53 +000054 bool EnteringContext,
55 bool ColonIsSacred) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000056 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000057 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000058
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000059 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000060 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000061 SS.setRange(Tok.getAnnotationRange());
62 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000063 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000064 }
Chris Lattnere607e802009-01-04 21:14:15 +000065
Douglas Gregor39a8de12009-02-25 19:37:18 +000066 bool HasScopeSpecifier = false;
67
Chris Lattner5b454732009-01-05 03:55:46 +000068 if (Tok.is(tok::coloncolon)) {
69 // ::new and ::delete aren't nested-name-specifiers.
70 tok::TokenKind NextKind = NextToken().getKind();
71 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
72 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner55a7cef2009-01-05 00:13:00 +000074 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000075 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000076 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000077 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000078 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000079 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000080 }
81
Douglas Gregor39a8de12009-02-25 19:37:18 +000082 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000083 if (HasScopeSpecifier) {
84 // C++ [basic.lookup.classref]p5:
85 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000086 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000087 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000088 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000089 // the class-name-or-namespace-name is looked up in global scope as a
90 // class-name or namespace-name.
91 //
92 // To implement this, we clear out the object type as soon as we've
93 // seen a leading '::' or part of a nested-name-specifier.
94 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +000095
96 if (Tok.is(tok::code_completion)) {
97 // Code completion for a nested-name-specifier, where the code
98 // code completion token follows the '::'.
99 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
100 ConsumeToken();
101 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000102 }
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Douglas Gregor39a8de12009-02-25 19:37:18 +0000104 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000105 // nested-name-specifier 'template'[opt] simple-template-id '::'
106
107 // Parse the optional 'template' keyword, then make sure we have
108 // 'identifier <' after it.
109 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000110 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000111 // nested-name-specifier, since they aren't allowed to start with
112 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000113 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000114 break;
115
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000116 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000117 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000118
119 UnqualifiedId TemplateName;
120 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000121 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000122 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000123 ConsumeToken();
124 } else if (Tok.is(tok::kw_operator)) {
125 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000126 TemplateName)) {
127 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000128 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000129 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000130
Sean Hunte6252d12009-11-28 08:58:14 +0000131 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
132 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000133 Diag(TemplateName.getSourceRange().getBegin(),
134 diag::err_id_after_template_in_nested_name_spec)
135 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000136 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000137 break;
138 }
139 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000140 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000141 break;
142 }
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000144 // If the next token is not '<', we have a qualified-id that refers
145 // to a template name, such as T::template apply, but is not a
146 // template-id.
147 if (Tok.isNot(tok::less)) {
148 TPA.Revert();
149 break;
150 }
151
152 // Commit to parsing the template-id.
153 TPA.Commit();
Mike Stump1eb44332009-09-09 15:08:12 +0000154 TemplateTy Template
Douglas Gregor014e88d2009-11-03 23:16:33 +0000155 = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
Douglas Gregora481edb2009-11-20 23:39:24 +0000156 ObjectType, EnteringContext);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000157 if (!Template)
158 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000159 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000160 &SS, TemplateName, TemplateKWLoc, false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000161 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattner77cf72a2009-06-26 03:47:46 +0000163 continue;
164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Douglas Gregor39a8de12009-02-25 19:37:18 +0000166 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000167 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000168 //
169 // simple-template-id '::'
170 //
171 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000172 // right kind (it should name a type or be dependent), and then
173 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000174 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000175 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
176
Mike Stump1eb44332009-09-09 15:08:12 +0000177 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000178 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000179 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000180
Mike Stump1eb44332009-09-09 15:08:12 +0000181 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000182 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000183 Token TypeToken = Tok;
184 ConsumeToken();
185 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
186 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Douglas Gregor39a8de12009-02-25 19:37:18 +0000188 if (!HasScopeSpecifier) {
189 SS.setBeginLoc(TypeToken.getLocation());
190 HasScopeSpecifier = true;
191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Douglas Gregor31a19b62009-04-01 21:51:26 +0000193 if (TypeToken.getAnnotationValue())
194 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000195 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000196 TypeToken.getAnnotationValue(),
197 TypeToken.getAnnotationRange(),
198 CCLoc));
199 else
200 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000201 SS.setEndLoc(CCLoc);
202 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Chris Lattner67b9e832009-06-26 03:45:46 +0000205 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000206 }
207
Chris Lattner5c7f7862009-06-26 03:52:38 +0000208
209 // The rest of the nested-name-specifier possibilities start with
210 // tok::identifier.
211 if (Tok.isNot(tok::identifier))
212 break;
213
214 IdentifierInfo &II = *Tok.getIdentifierInfo();
215
216 // nested-name-specifier:
217 // type-name '::'
218 // namespace-name '::'
219 // nested-name-specifier identifier '::'
220 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000221
222 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
223 // and emit a fixit hint for it.
224 if (Next.is(tok::colon) && !ColonIsSacred &&
225 Actions.IsInvalidUnlessNestedName(CurScope, SS, II, ObjectType,
226 EnteringContext) &&
227 // If the token after the colon isn't an identifier, it's still an
228 // error, but they probably meant something else strange so don't
229 // recover like this.
230 PP.LookAhead(1).is(tok::identifier)) {
231 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
232 << CodeModificationHint::CreateReplacement(Next.getLocation(), "::");
233
234 // Recover as if the user wrote '::'.
235 Next.setKind(tok::coloncolon);
236 }
237
Chris Lattner5c7f7862009-06-26 03:52:38 +0000238 if (Next.is(tok::coloncolon)) {
239 // We have an identifier followed by a '::'. Lookup this name
240 // as the name in a nested-name-specifier.
241 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000242 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
243 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000244 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Chris Lattner5c7f7862009-06-26 03:52:38 +0000246 if (!HasScopeSpecifier) {
247 SS.setBeginLoc(IdLoc);
248 HasScopeSpecifier = true;
249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Chris Lattner5c7f7862009-06-26 03:52:38 +0000251 if (SS.isInvalid())
252 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Chris Lattner5c7f7862009-06-26 03:52:38 +0000254 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000255 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000256 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000257 SS.setEndLoc(CCLoc);
258 continue;
259 }
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Chris Lattner5c7f7862009-06-26 03:52:38 +0000261 // nested-name-specifier:
262 // type-name '<'
263 if (Next.is(tok::less)) {
264 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000265 UnqualifiedId TemplateName;
266 TemplateName.setIdentifier(&II, Tok.getLocation());
267 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
268 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000269 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000270 EnteringContext,
271 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000272 // We have found a template name, so annotate this this token
273 // with a template-id annotation. We do not permit the
274 // template-id to be translated into a type annotation,
275 // because some clients (e.g., the parsing of class template
276 // specializations) still want to see the original template-id
277 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000278 ConsumeToken();
279 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
280 SourceLocation(), false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000281 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000282 continue;
283 }
284 }
285
Douglas Gregor39a8de12009-02-25 19:37:18 +0000286 // We don't have any tokens that form the beginning of a
287 // nested-name-specifier, so we're done.
288 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Douglas Gregor39a8de12009-02-25 19:37:18 +0000291 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000292}
293
294/// ParseCXXIdExpression - Handle id-expression.
295///
296/// id-expression:
297/// unqualified-id
298/// qualified-id
299///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000300/// qualified-id:
301/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
302/// '::' identifier
303/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000304/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000305///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000306/// NOTE: The standard specifies that, for qualified-id, the parser does not
307/// expect:
308///
309/// '::' conversion-function-id
310/// '::' '~' class-name
311///
312/// This may cause a slight inconsistency on diagnostics:
313///
314/// class C {};
315/// namespace A {}
316/// void f() {
317/// :: A :: ~ C(); // Some Sema error about using destructor with a
318/// // namespace.
319/// :: ~ C(); // Some Parser error like 'unexpected ~'.
320/// }
321///
322/// We simplify the parser a bit and make it work like:
323///
324/// qualified-id:
325/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
326/// '::' unqualified-id
327///
328/// That way Sema can handle and report similar errors for namespaces and the
329/// global scope.
330///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000331/// The isAddressOfOperand parameter indicates that this id-expression is a
332/// direct operand of the address-of operator. This is, besides member contexts,
333/// the only place where a qualified-id naming a non-static class member may
334/// appear.
335///
336Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000337 // qualified-id:
338 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
339 // '::' unqualified-id
340 //
341 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000342 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000343
344 UnqualifiedId Name;
345 if (ParseUnqualifiedId(SS,
346 /*EnteringContext=*/false,
347 /*AllowDestructorName=*/false,
348 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000349 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000350 Name))
351 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000352
353 // This is only the direct operand of an & operator if it is not
354 // followed by a postfix-expression suffix.
355 if (isAddressOfOperand) {
356 switch (Tok.getKind()) {
357 case tok::l_square:
358 case tok::l_paren:
359 case tok::arrow:
360 case tok::period:
361 case tok::plusplus:
362 case tok::minusminus:
363 isAddressOfOperand = false;
364 break;
365
366 default:
367 break;
368 }
369 }
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000370
371 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
372 isAddressOfOperand);
373
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000374}
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376/// ParseCXXCasts - This handles the various ways to cast expressions to another
377/// type.
378///
379/// postfix-expression: [C++ 5.2p1]
380/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
381/// 'static_cast' '<' type-name '>' '(' expression ')'
382/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
383/// 'const_cast' '<' type-name '>' '(' expression ')'
384///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000385Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 tok::TokenKind Kind = Tok.getKind();
387 const char *CastName = 0; // For error messages
388
389 switch (Kind) {
390 default: assert(0 && "Unknown C++ cast!"); abort();
391 case tok::kw_const_cast: CastName = "const_cast"; break;
392 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
393 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
394 case tok::kw_static_cast: CastName = "static_cast"; break;
395 }
396
397 SourceLocation OpLoc = ConsumeToken();
398 SourceLocation LAngleBracketLoc = Tok.getLocation();
399
400 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000401 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000402
Douglas Gregor809070a2009-02-18 17:45:20 +0000403 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 SourceLocation RAngleBracketLoc = Tok.getLocation();
405
Chris Lattner1ab3b962008-11-18 07:48:38 +0000406 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000407 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000408
409 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
410
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000411 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
412 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000413
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000414 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000416 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000417 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000418
Douglas Gregor809070a2009-02-18 17:45:20 +0000419 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000420 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000421 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000422 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000423 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000424
Sebastian Redl20df9b72008-12-11 22:51:44 +0000425 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000426}
427
Sebastian Redlc42e1182008-11-11 11:37:55 +0000428/// ParseCXXTypeid - This handles the C++ typeid expression.
429///
430/// postfix-expression: [C++ 5.2p1]
431/// 'typeid' '(' expression ')'
432/// 'typeid' '(' type-id ')'
433///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000434Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000435 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
436
437 SourceLocation OpLoc = ConsumeToken();
438 SourceLocation LParenLoc = Tok.getLocation();
439 SourceLocation RParenLoc;
440
441 // typeid expressions are always parenthesized.
442 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
443 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000444 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000445
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000446 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000447
448 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000449 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000450
451 // Match the ')'.
452 MatchRHSPunctuation(tok::r_paren, LParenLoc);
453
Douglas Gregor809070a2009-02-18 17:45:20 +0000454 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000455 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000456
457 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000458 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000459 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000460 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000461 // When typeid is applied to an expression other than an lvalue of a
462 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000463 // operand (Clause 5).
464 //
Mike Stump1eb44332009-09-09 15:08:12 +0000465 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000466 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000467 // we the expression is potentially potentially evaluated.
468 EnterExpressionEvaluationContext Unevaluated(Actions,
469 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000470 Result = ParseExpression();
471
472 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000473 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000474 SkipUntil(tok::r_paren);
475 else {
476 MatchRHSPunctuation(tok::r_paren, LParenLoc);
477
478 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000479 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000480 }
481 }
482
Sebastian Redl20df9b72008-12-11 22:51:44 +0000483 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000484}
485
Reid Spencer5f016e22007-07-11 17:01:13 +0000486/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
487///
488/// boolean-literal: [C++ 2.13.5]
489/// 'true'
490/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000491Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000493 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000494}
Chris Lattner50dd2892008-02-26 00:51:44 +0000495
496/// ParseThrowExpression - This handles the C++ throw expression.
497///
498/// throw-expression: [C++ 15]
499/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000500Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000501 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000502 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000503
Chris Lattner2a2819a2008-04-06 06:02:23 +0000504 // If the current token isn't the start of an assignment-expression,
505 // then the expression is not present. This handles things like:
506 // "C ? throw : (void)42", which is crazy but legal.
507 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
508 case tok::semi:
509 case tok::r_paren:
510 case tok::r_square:
511 case tok::r_brace:
512 case tok::colon:
513 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000514 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000515
Chris Lattner2a2819a2008-04-06 06:02:23 +0000516 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000517 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000518 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000519 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000520 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000521}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000522
523/// ParseCXXThis - This handles the C++ 'this' pointer.
524///
525/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
526/// a non-lvalue expression whose value is the address of the object for which
527/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000528Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000529 assert(Tok.is(tok::kw_this) && "Not 'this'!");
530 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000531 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000532}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000533
534/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
535/// Can be interpreted either as function-style casting ("int(x)")
536/// or class type construction ("ClassType(x,y,z)")
537/// or creation of a value-initialized type ("int()").
538///
539/// postfix-expression: [C++ 5.2p1]
540/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
541/// typename-specifier '(' expression-list[opt] ')' [TODO]
542///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000543Parser::OwningExprResult
544Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000545 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000546 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000547
548 assert(Tok.is(tok::l_paren) && "Expected '('!");
549 SourceLocation LParenLoc = ConsumeParen();
550
Sebastian Redla55e52c2008-11-25 22:21:31 +0000551 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000552 CommaLocsTy CommaLocs;
553
554 if (Tok.isNot(tok::r_paren)) {
555 if (ParseExpressionList(Exprs, CommaLocs)) {
556 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000557 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000558 }
559 }
560
561 // Match the ')'.
562 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
563
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000564 // TypeRep could be null, if it references an invalid typedef.
565 if (!TypeRep)
566 return ExprError();
567
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000568 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
569 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000570 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
571 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000572 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000573}
574
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000575/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000576///
577/// condition:
578/// expression
579/// type-specifier-seq declarator '=' assignment-expression
580/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
581/// '=' assignment-expression
582///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000583/// \param ExprResult if the condition was parsed as an expression, the
584/// parsed expression.
585///
586/// \param DeclResult if the condition was parsed as a declaration, the
587/// parsed declaration.
588///
589/// \returns true if there was a parsing, false otherwise.
590bool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
591 DeclPtrTy &DeclResult) {
592 if (!isCXXConditionDeclaration()) {
593 ExprResult = ParseExpression(); // expression
594 DeclResult = DeclPtrTy();
595 return ExprResult.isInvalid();
596 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000597
598 // type-specifier-seq
599 DeclSpec DS;
600 ParseSpecifierQualifierList(DS);
601
602 // declarator
603 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
604 ParseDeclarator(DeclaratorInfo);
605
606 // simple-asm-expr[opt]
607 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000608 SourceLocation Loc;
609 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000610 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000611 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000612 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000613 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000614 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000615 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000616 }
617
618 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000619 if (Tok.is(tok::kw___attribute)) {
620 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000621 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000622 DeclaratorInfo.AddAttributes(AttrList, Loc);
623 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000624
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000625 // Type-check the declaration itself.
626 Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope,
627 DeclaratorInfo);
628 DeclResult = Dcl.get();
629 ExprResult = ExprError();
630
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000631 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000632 if (Tok.is(tok::equal)) {
633 SourceLocation EqualLoc = ConsumeToken();
634 OwningExprResult AssignExpr(ParseAssignmentExpression());
635 if (!AssignExpr.isInvalid())
636 Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
637 } else {
638 // FIXME: C++0x allows a braced-init-list
639 Diag(Tok, diag::err_expected_equal_after_declarator);
640 }
641
642 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000643}
644
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000645/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
646/// This should only be called when the current token is known to be part of
647/// simple-type-specifier.
648///
649/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000650/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000651/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
652/// char
653/// wchar_t
654/// bool
655/// short
656/// int
657/// long
658/// signed
659/// unsigned
660/// float
661/// double
662/// void
663/// [GNU] typeof-specifier
664/// [C++0x] auto [TODO]
665///
666/// type-name:
667/// class-name
668/// enum-name
669/// typedef-name
670///
671void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
672 DS.SetRangeStart(Tok.getLocation());
673 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000674 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000675 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000677 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000678 case tok::identifier: // foo::bar
679 case tok::coloncolon: // ::foo::bar
680 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000681 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000682 assert(0 && "Not a simple-type-specifier token!");
683 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000684
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000685 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000686 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000687 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000688 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000689 break;
690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000692 // builtin types
693 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000694 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000695 break;
696 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000697 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000698 break;
699 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000700 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000701 break;
702 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000703 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000704 break;
705 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000706 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000707 break;
708 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000709 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000710 break;
711 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000712 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000713 break;
714 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000715 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000716 break;
717 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000718 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000719 break;
720 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000721 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000722 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000723 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000724 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000725 break;
726 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000727 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000728 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000729 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000730 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000731 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000733 // GNU typeof support.
734 case tok::kw_typeof:
735 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000736 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000737 return;
738 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000739 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000740 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
741 else
742 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000743 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000744 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000745}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000746
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000747/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
748/// [dcl.name]), which is a non-empty sequence of type-specifiers,
749/// e.g., "const short int". Note that the DeclSpec is *not* finished
750/// by parsing the type-specifier-seq, because these sequences are
751/// typically followed by some form of declarator. Returns true and
752/// emits diagnostics if this is not a type-specifier-seq, false
753/// otherwise.
754///
755/// type-specifier-seq: [C++ 8.1]
756/// type-specifier type-specifier-seq[opt]
757///
758bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
759 DS.SetRangeStart(Tok.getLocation());
760 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000761 unsigned DiagID;
762 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000763
764 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000765 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000766 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000767 return true;
768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
John McCallfec54012009-08-03 20:12:06 +0000770 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000771
772 return false;
773}
774
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000775/// \brief Finish parsing a C++ unqualified-id that is a template-id of
776/// some form.
777///
778/// This routine is invoked when a '<' is encountered after an identifier or
779/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
780/// whether the unqualified-id is actually a template-id. This routine will
781/// then parse the template arguments and form the appropriate template-id to
782/// return to the caller.
783///
784/// \param SS the nested-name-specifier that precedes this template-id, if
785/// we're actually parsing a qualified-id.
786///
787/// \param Name for constructor and destructor names, this is the actual
788/// identifier that may be a template-name.
789///
790/// \param NameLoc the location of the class-name in a constructor or
791/// destructor.
792///
793/// \param EnteringContext whether we're entering the scope of the
794/// nested-name-specifier.
795///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000796/// \param ObjectType if this unqualified-id occurs within a member access
797/// expression, the type of the base object whose member is being accessed.
798///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000799/// \param Id as input, describes the template-name or operator-function-id
800/// that precedes the '<'. If template arguments were parsed successfully,
801/// will be updated with the template-id.
802///
803/// \returns true if a parse error occurred, false otherwise.
804bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
805 IdentifierInfo *Name,
806 SourceLocation NameLoc,
807 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000808 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000809 UnqualifiedId &Id) {
810 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
811
812 TemplateTy Template;
813 TemplateNameKind TNK = TNK_Non_template;
814 switch (Id.getKind()) {
815 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000816 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +0000817 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000818 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
819 Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000820 break;
821
Douglas Gregor014e88d2009-11-03 23:16:33 +0000822 case UnqualifiedId::IK_ConstructorName: {
823 UnqualifiedId TemplateName;
824 TemplateName.setIdentifier(Name, NameLoc);
825 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
826 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000827 break;
828 }
829
Douglas Gregor014e88d2009-11-03 23:16:33 +0000830 case UnqualifiedId::IK_DestructorName: {
831 UnqualifiedId TemplateName;
832 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000833 if (ObjectType) {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000834 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
Douglas Gregora481edb2009-11-20 23:39:24 +0000835 TemplateName, ObjectType,
836 EnteringContext);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000837 TNK = TNK_Dependent_template_name;
838 if (!Template.get())
839 return true;
840 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000841 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000842 EnteringContext, Template);
843
844 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
845 // The identifier following the destructor did not refer to a template
846 // or to a type. Complain.
847 if (ObjectType)
848 Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
849 << Name;
850 else
851 Diag(NameLoc, diag::err_destructor_class_name);
852 return true;
853 }
854 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000855 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000856 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000857
858 default:
859 return false;
860 }
861
862 if (TNK == TNK_Non_template)
863 return false;
864
865 // Parse the enclosed template argument list.
866 SourceLocation LAngleLoc, RAngleLoc;
867 TemplateArgList TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000868 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
869 &SS, true, LAngleLoc,
870 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000871 RAngleLoc))
872 return true;
873
874 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +0000875 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
876 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000877 // Form a parsed representation of the template-id to be stored in the
878 // UnqualifiedId.
879 TemplateIdAnnotation *TemplateId
880 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
881
882 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
883 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000884 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000885 TemplateId->TemplateNameLoc = Id.StartLocation;
886 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000887 TemplateId->Name = 0;
888 TemplateId->Operator = Id.OperatorFunctionId.Operator;
889 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000890 }
891
892 TemplateId->Template = Template.getAs<void*>();
893 TemplateId->Kind = TNK;
894 TemplateId->LAngleLoc = LAngleLoc;
895 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +0000896 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000897 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +0000898 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000899 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000900
901 Id.setTemplateId(TemplateId);
902 return false;
903 }
904
905 // Bundle the template arguments together.
906 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000907 TemplateArgs.size());
908
909 // Constructor and destructor names.
910 Action::TypeResult Type
911 = Actions.ActOnTemplateIdType(Template, NameLoc,
912 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000913 RAngleLoc);
914 if (Type.isInvalid())
915 return true;
916
917 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
918 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
919 else
920 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
921
922 return false;
923}
924
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000925/// \brief Parse an operator-function-id or conversion-function-id as part
926/// of a C++ unqualified-id.
927///
928/// This routine is responsible only for parsing the operator-function-id or
929/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000930///
931/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000932/// operator-function-id: [C++ 13.5]
933/// 'operator' operator
934///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000935/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000936/// new delete new[] delete[]
937/// + - * / % ^ & | ~
938/// ! = < > += -= *= /= %=
939/// ^= &= |= << >> >>= <<= == !=
940/// <= >= && || ++ -- , ->* ->
941/// () []
942///
943/// conversion-function-id: [C++ 12.3.2]
944/// operator conversion-type-id
945///
946/// conversion-type-id:
947/// type-specifier-seq conversion-declarator[opt]
948///
949/// conversion-declarator:
950/// ptr-operator conversion-declarator[opt]
951/// \endcode
952///
953/// \param The nested-name-specifier that preceded this unqualified-id. If
954/// non-empty, then we are parsing the unqualified-id of a qualified-id.
955///
956/// \param EnteringContext whether we are entering the scope of the
957/// nested-name-specifier.
958///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000959/// \param ObjectType if this unqualified-id occurs within a member access
960/// expression, the type of the base object whose member is being accessed.
961///
962/// \param Result on a successful parse, contains the parsed unqualified-id.
963///
964/// \returns true if parsing fails, false otherwise.
965bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
966 TypeTy *ObjectType,
967 UnqualifiedId &Result) {
968 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
969
970 // Consume the 'operator' keyword.
971 SourceLocation KeywordLoc = ConsumeToken();
972
973 // Determine what kind of operator name we have.
974 unsigned SymbolIdx = 0;
975 SourceLocation SymbolLocations[3];
976 OverloadedOperatorKind Op = OO_None;
977 switch (Tok.getKind()) {
978 case tok::kw_new:
979 case tok::kw_delete: {
980 bool isNew = Tok.getKind() == tok::kw_new;
981 // Consume the 'new' or 'delete'.
982 SymbolLocations[SymbolIdx++] = ConsumeToken();
983 if (Tok.is(tok::l_square)) {
984 // Consume the '['.
985 SourceLocation LBracketLoc = ConsumeBracket();
986 // Consume the ']'.
987 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
988 LBracketLoc);
989 if (RBracketLoc.isInvalid())
990 return true;
991
992 SymbolLocations[SymbolIdx++] = LBracketLoc;
993 SymbolLocations[SymbolIdx++] = RBracketLoc;
994 Op = isNew? OO_Array_New : OO_Array_Delete;
995 } else {
996 Op = isNew? OO_New : OO_Delete;
997 }
998 break;
999 }
1000
1001#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1002 case tok::Token: \
1003 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1004 Op = OO_##Name; \
1005 break;
1006#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1007#include "clang/Basic/OperatorKinds.def"
1008
1009 case tok::l_paren: {
1010 // Consume the '('.
1011 SourceLocation LParenLoc = ConsumeParen();
1012 // Consume the ')'.
1013 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1014 LParenLoc);
1015 if (RParenLoc.isInvalid())
1016 return true;
1017
1018 SymbolLocations[SymbolIdx++] = LParenLoc;
1019 SymbolLocations[SymbolIdx++] = RParenLoc;
1020 Op = OO_Call;
1021 break;
1022 }
1023
1024 case tok::l_square: {
1025 // Consume the '['.
1026 SourceLocation LBracketLoc = ConsumeBracket();
1027 // Consume the ']'.
1028 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1029 LBracketLoc);
1030 if (RBracketLoc.isInvalid())
1031 return true;
1032
1033 SymbolLocations[SymbolIdx++] = LBracketLoc;
1034 SymbolLocations[SymbolIdx++] = RBracketLoc;
1035 Op = OO_Subscript;
1036 break;
1037 }
1038
1039 case tok::code_completion: {
1040 // Code completion for the operator name.
1041 Actions.CodeCompleteOperatorName(CurScope);
1042
1043 // Consume the operator token.
1044 ConsumeToken();
1045
1046 // Don't try to parse any further.
1047 return true;
1048 }
1049
1050 default:
1051 break;
1052 }
1053
1054 if (Op != OO_None) {
1055 // We have parsed an operator-function-id.
1056 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1057 return false;
1058 }
Sean Hunt0486d742009-11-28 04:44:28 +00001059
1060 // Parse a literal-operator-id.
1061 //
1062 // literal-operator-id: [C++0x 13.5.8]
1063 // operator "" identifier
1064
1065 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1066 if (Tok.getLength() != 2)
1067 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1068 ConsumeStringToken();
1069
1070 if (Tok.isNot(tok::identifier)) {
1071 Diag(Tok.getLocation(), diag::err_expected_ident);
1072 return true;
1073 }
1074
1075 IdentifierInfo *II = Tok.getIdentifierInfo();
1076 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001077 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001078 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001079
1080 // Parse a conversion-function-id.
1081 //
1082 // conversion-function-id: [C++ 12.3.2]
1083 // operator conversion-type-id
1084 //
1085 // conversion-type-id:
1086 // type-specifier-seq conversion-declarator[opt]
1087 //
1088 // conversion-declarator:
1089 // ptr-operator conversion-declarator[opt]
1090
1091 // Parse the type-specifier-seq.
1092 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001093 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001094 return true;
1095
1096 // Parse the conversion-declarator, which is merely a sequence of
1097 // ptr-operators.
1098 Declarator D(DS, Declarator::TypeNameContext);
1099 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1100
1101 // Finish up the type.
1102 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1103 if (Ty.isInvalid())
1104 return true;
1105
1106 // Note that this is a conversion-function-id.
1107 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1108 D.getSourceRange().getEnd());
1109 return false;
1110}
1111
1112/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1113/// name of an entity.
1114///
1115/// \code
1116/// unqualified-id: [C++ expr.prim.general]
1117/// identifier
1118/// operator-function-id
1119/// conversion-function-id
1120/// [C++0x] literal-operator-id [TODO]
1121/// ~ class-name
1122/// template-id
1123///
1124/// \endcode
1125///
1126/// \param The nested-name-specifier that preceded this unqualified-id. If
1127/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1128///
1129/// \param EnteringContext whether we are entering the scope of the
1130/// nested-name-specifier.
1131///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001132/// \param AllowDestructorName whether we allow parsing of a destructor name.
1133///
1134/// \param AllowConstructorName whether we allow parsing a constructor name.
1135///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001136/// \param ObjectType if this unqualified-id occurs within a member access
1137/// expression, the type of the base object whose member is being accessed.
1138///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001139/// \param Result on a successful parse, contains the parsed unqualified-id.
1140///
1141/// \returns true if parsing fails, false otherwise.
1142bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1143 bool AllowDestructorName,
1144 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001145 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001146 UnqualifiedId &Result) {
1147 // unqualified-id:
1148 // identifier
1149 // template-id (when it hasn't already been annotated)
1150 if (Tok.is(tok::identifier)) {
1151 // Consume the identifier.
1152 IdentifierInfo *Id = Tok.getIdentifierInfo();
1153 SourceLocation IdLoc = ConsumeToken();
1154
1155 if (AllowConstructorName &&
1156 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1157 // We have parsed a constructor name.
1158 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1159 &SS, false),
1160 IdLoc, IdLoc);
1161 } else {
1162 // We have parsed an identifier.
1163 Result.setIdentifier(Id, IdLoc);
1164 }
1165
1166 // If the next token is a '<', we may have a template.
1167 if (Tok.is(tok::less))
1168 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001169 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001170
1171 return false;
1172 }
1173
1174 // unqualified-id:
1175 // template-id (already parsed and annotated)
1176 if (Tok.is(tok::annot_template_id)) {
1177 // FIXME: Could this be a constructor name???
1178
1179 // We have already parsed a template-id; consume the annotation token as
1180 // our unqualified-id.
1181 Result.setTemplateId(
1182 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1183 ConsumeToken();
1184 return false;
1185 }
1186
1187 // unqualified-id:
1188 // operator-function-id
1189 // conversion-function-id
1190 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001191 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001192 return true;
1193
Sean Hunte6252d12009-11-28 08:58:14 +00001194 // If we have an operator-function-id or a literal-operator-id and the next
1195 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001196 //
1197 // template-id:
1198 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001199 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1200 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001201 Tok.is(tok::less))
1202 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1203 EnteringContext, ObjectType,
1204 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001205
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001206 return false;
1207 }
1208
1209 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1210 // C++ [expr.unary.op]p10:
1211 // There is an ambiguity in the unary-expression ~X(), where X is a
1212 // class-name. The ambiguity is resolved in favor of treating ~ as a
1213 // unary complement rather than treating ~X as referring to a destructor.
1214
1215 // Parse the '~'.
1216 SourceLocation TildeLoc = ConsumeToken();
1217
1218 // Parse the class-name.
1219 if (Tok.isNot(tok::identifier)) {
1220 Diag(Tok, diag::err_destructor_class_name);
1221 return true;
1222 }
1223
1224 // Parse the class-name (or template-name in a simple-template-id).
1225 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1226 SourceLocation ClassNameLoc = ConsumeToken();
1227
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001228 if (Tok.is(tok::less)) {
1229 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1230 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1231 EnteringContext, ObjectType, Result);
1232 }
1233
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001234 // Note that this is a destructor name.
1235 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001236 CurScope, &SS, false, ObjectType);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001237 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001238 if (ObjectType)
1239 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1240 << ClassName;
1241 else
1242 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001243 return true;
1244 }
1245
1246 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001247 return false;
1248 }
1249
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001250 Diag(Tok, diag::err_expected_unqualified_id)
1251 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001252 return true;
1253}
1254
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001255/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1256/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001257///
Chris Lattner59232d32009-01-04 21:25:24 +00001258/// This method is called to parse the new expression after the optional :: has
1259/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1260/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001261///
1262/// new-expression:
1263/// '::'[opt] 'new' new-placement[opt] new-type-id
1264/// new-initializer[opt]
1265/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1266/// new-initializer[opt]
1267///
1268/// new-placement:
1269/// '(' expression-list ')'
1270///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001271/// new-type-id:
1272/// type-specifier-seq new-declarator[opt]
1273///
1274/// new-declarator:
1275/// ptr-operator new-declarator[opt]
1276/// direct-new-declarator
1277///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001278/// new-initializer:
1279/// '(' expression-list[opt] ')'
1280/// [C++0x] braced-init-list [TODO]
1281///
Chris Lattner59232d32009-01-04 21:25:24 +00001282Parser::OwningExprResult
1283Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1284 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1285 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001286
1287 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1288 // second form of new-expression. It can't be a new-type-id.
1289
Sebastian Redla55e52c2008-11-25 22:21:31 +00001290 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001291 SourceLocation PlacementLParen, PlacementRParen;
1292
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001293 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001294 DeclSpec DS;
1295 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001296 if (Tok.is(tok::l_paren)) {
1297 // If it turns out to be a placement, we change the type location.
1298 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001299 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1300 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001301 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001302 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001303
1304 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001305 if (PlacementRParen.isInvalid()) {
1306 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001307 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001308 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001309
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001310 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001311 // Reset the placement locations. There was no placement.
1312 PlacementLParen = PlacementRParen = SourceLocation();
1313 ParenTypeId = true;
1314 } else {
1315 // We still need the type.
1316 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001317 SourceLocation LParen = ConsumeParen();
1318 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001319 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001320 ParseDeclarator(DeclaratorInfo);
1321 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001322 ParenTypeId = true;
1323 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001324 if (ParseCXXTypeSpecifierSeq(DS))
1325 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001326 else {
1327 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001328 ParseDeclaratorInternal(DeclaratorInfo,
1329 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001330 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001331 ParenTypeId = false;
1332 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001333 }
1334 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001335 // A new-type-id is a simplified type-id, where essentially the
1336 // direct-declarator is replaced by a direct-new-declarator.
1337 if (ParseCXXTypeSpecifierSeq(DS))
1338 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001339 else {
1340 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001341 ParseDeclaratorInternal(DeclaratorInfo,
1342 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001343 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001344 ParenTypeId = false;
1345 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001346 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001347 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001348 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001349 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001350
Sebastian Redla55e52c2008-11-25 22:21:31 +00001351 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001352 SourceLocation ConstructorLParen, ConstructorRParen;
1353
1354 if (Tok.is(tok::l_paren)) {
1355 ConstructorLParen = ConsumeParen();
1356 if (Tok.isNot(tok::r_paren)) {
1357 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001358 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1359 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001360 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001361 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001362 }
1363 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001364 if (ConstructorRParen.isInvalid()) {
1365 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001366 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001367 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001368 }
1369
Sebastian Redlf53597f2009-03-15 17:47:39 +00001370 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1371 move_arg(PlacementArgs), PlacementRParen,
1372 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1373 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001374}
1375
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001376/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1377/// passed to ParseDeclaratorInternal.
1378///
1379/// direct-new-declarator:
1380/// '[' expression ']'
1381/// direct-new-declarator '[' constant-expression ']'
1382///
Chris Lattner59232d32009-01-04 21:25:24 +00001383void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001384 // Parse the array dimensions.
1385 bool first = true;
1386 while (Tok.is(tok::l_square)) {
1387 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001388 OwningExprResult Size(first ? ParseExpression()
1389 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001390 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001391 // Recover
1392 SkipUntil(tok::r_square);
1393 return;
1394 }
1395 first = false;
1396
Sebastian Redlab197ba2009-02-09 18:23:29 +00001397 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001398 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001399 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001400 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001401
Sebastian Redlab197ba2009-02-09 18:23:29 +00001402 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001403 return;
1404 }
1405}
1406
1407/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1408/// This ambiguity appears in the syntax of the C++ new operator.
1409///
1410/// new-expression:
1411/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1412/// new-initializer[opt]
1413///
1414/// new-placement:
1415/// '(' expression-list ')'
1416///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001417bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001418 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001419 // The '(' was already consumed.
1420 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001421 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001422 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001423 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001424 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001425 }
1426
1427 // It's not a type, it has to be an expression list.
1428 // Discard the comma locations - ActOnCXXNew has enough parameters.
1429 CommaLocsTy CommaLocs;
1430 return ParseExpressionList(PlacementArgs, CommaLocs);
1431}
1432
1433/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1434/// to free memory allocated by new.
1435///
Chris Lattner59232d32009-01-04 21:25:24 +00001436/// This method is called to parse the 'delete' expression after the optional
1437/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1438/// and "Start" is its location. Otherwise, "Start" is the location of the
1439/// 'delete' token.
1440///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001441/// delete-expression:
1442/// '::'[opt] 'delete' cast-expression
1443/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001444Parser::OwningExprResult
1445Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1446 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1447 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001448
1449 // Array delete?
1450 bool ArrayDelete = false;
1451 if (Tok.is(tok::l_square)) {
1452 ArrayDelete = true;
1453 SourceLocation LHS = ConsumeBracket();
1454 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1455 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001456 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001457 }
1458
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001459 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001460 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001461 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001462
Sebastian Redlf53597f2009-03-15 17:47:39 +00001463 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001464}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001465
Mike Stump1eb44332009-09-09 15:08:12 +00001466static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001467 switch(kind) {
1468 default: assert(false && "Not a known unary type trait.");
1469 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1470 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1471 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1472 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1473 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1474 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1475 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1476 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1477 case tok::kw___is_abstract: return UTT_IsAbstract;
1478 case tok::kw___is_class: return UTT_IsClass;
1479 case tok::kw___is_empty: return UTT_IsEmpty;
1480 case tok::kw___is_enum: return UTT_IsEnum;
1481 case tok::kw___is_pod: return UTT_IsPOD;
1482 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1483 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001484 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001485 }
1486}
1487
1488/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1489/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1490/// templates.
1491///
1492/// primary-expression:
1493/// [GNU] unary-type-trait '(' type-id ')'
1494///
Mike Stump1eb44332009-09-09 15:08:12 +00001495Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001496 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1497 SourceLocation Loc = ConsumeToken();
1498
1499 SourceLocation LParen = Tok.getLocation();
1500 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1501 return ExprError();
1502
1503 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1504 // there will be cryptic errors about mismatched parentheses and missing
1505 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001506 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001507
1508 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1509
Douglas Gregor809070a2009-02-18 17:45:20 +00001510 if (Ty.isInvalid())
1511 return ExprError();
1512
1513 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001514}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001515
1516/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1517/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1518/// based on the context past the parens.
1519Parser::OwningExprResult
1520Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1521 TypeTy *&CastTy,
1522 SourceLocation LParenLoc,
1523 SourceLocation &RParenLoc) {
1524 assert(getLang().CPlusPlus && "Should only be called for C++!");
1525 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1526 assert(isTypeIdInParens() && "Not a type-id!");
1527
1528 OwningExprResult Result(Actions, true);
1529 CastTy = 0;
1530
1531 // We need to disambiguate a very ugly part of the C++ syntax:
1532 //
1533 // (T())x; - type-id
1534 // (T())*x; - type-id
1535 // (T())/x; - expression
1536 // (T()); - expression
1537 //
1538 // The bad news is that we cannot use the specialized tentative parser, since
1539 // it can only verify that the thing inside the parens can be parsed as
1540 // type-id, it is not useful for determining the context past the parens.
1541 //
1542 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001543 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001544 //
1545 // It uses a scheme similar to parsing inline methods. The parenthesized
1546 // tokens are cached, the context that follows is determined (possibly by
1547 // parsing a cast-expression), and then we re-introduce the cached tokens
1548 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001549
Mike Stump1eb44332009-09-09 15:08:12 +00001550 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001551 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001552
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001553 // Store the tokens of the parentheses. We will parse them after we determine
1554 // the context that follows them.
1555 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1556 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001557 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1558 return ExprError();
1559 }
1560
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001561 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001562 ParseAs = CompoundLiteral;
1563 } else {
1564 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001565 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1566 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1567 NotCastExpr = true;
1568 } else {
1569 // Try parsing the cast-expression that may follow.
1570 // If it is not a cast-expression, NotCastExpr will be true and no token
1571 // will be consumed.
1572 Result = ParseCastExpression(false/*isUnaryExpression*/,
1573 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001574 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001575 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001576
1577 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1578 // an expression.
1579 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001580 }
1581
Mike Stump1eb44332009-09-09 15:08:12 +00001582 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001583 Toks.push_back(Tok);
1584 // Re-enter the stored parenthesized tokens into the token stream, so we may
1585 // parse them now.
1586 PP.EnterTokenStream(Toks.data(), Toks.size(),
1587 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1588 // Drop the current token and bring the first cached one. It's the same token
1589 // as when we entered this function.
1590 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001591
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001592 if (ParseAs >= CompoundLiteral) {
1593 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001594
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001595 // Match the ')'.
1596 if (Tok.is(tok::r_paren))
1597 RParenLoc = ConsumeParen();
1598 else
1599 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001600
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001601 if (ParseAs == CompoundLiteral) {
1602 ExprType = CompoundLiteral;
1603 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1604 }
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001606 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1607 assert(ParseAs == CastExpr);
1608
1609 if (Ty.isInvalid())
1610 return ExprError();
1611
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001612 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001613
1614 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001615 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001616 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001617 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001618 return move(Result);
1619 }
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001621 // Not a compound literal, and not followed by a cast-expression.
1622 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001623
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001624 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001625 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001626 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1627 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1628
1629 // Match the ')'.
1630 if (Result.isInvalid()) {
1631 SkipUntil(tok::r_paren);
1632 return ExprError();
1633 }
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001635 if (Tok.is(tok::r_paren))
1636 RParenLoc = ConsumeParen();
1637 else
1638 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1639
1640 return move(Result);
1641}