blob: b1250350f70c4724903e821d28a08fcbf9f933a7 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000017#include "llvm/Support/ErrorHandling.h"
18
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Mike Stump1eb44332009-09-09 15:08:12 +000021/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000022///
23/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000024/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000025/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000026///
27/// '::'[opt] nested-name-specifier
28/// '::'
29///
30/// nested-name-specifier:
31/// type-name '::'
32/// namespace-name '::'
33/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000034/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000035///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000036///
Mike Stump1eb44332009-09-09 15:08:12 +000037/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000038/// nested-name-specifier (or empty)
39///
Mike Stump1eb44332009-09-09 15:08:12 +000040/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// the "." or "->" of a member access expression, this parameter provides the
42/// type of the object whose members are being accessed.
43///
44/// \param EnteringContext whether we will be entering into the context of
45/// the nested-name-specifier after parsing it.
46///
47/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000048bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000049 Action::TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +000050 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000051 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000052 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000053
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000054 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000055 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000056 SS.setRange(Tok.getAnnotationRange());
57 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000058 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000059 }
Chris Lattnere607e802009-01-04 21:14:15 +000060
Douglas Gregor39a8de12009-02-25 19:37:18 +000061 bool HasScopeSpecifier = false;
62
Chris Lattner5b454732009-01-05 03:55:46 +000063 if (Tok.is(tok::coloncolon)) {
64 // ::new and ::delete aren't nested-name-specifiers.
65 tok::TokenKind NextKind = NextToken().getKind();
66 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
67 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000068
Chris Lattner55a7cef2009-01-05 00:13:00 +000069 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000070 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000071 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000072 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000073 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000074 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000075 }
76
Douglas Gregor39a8de12009-02-25 19:37:18 +000077 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000078 if (HasScopeSpecifier) {
79 // C++ [basic.lookup.classref]p5:
80 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000081 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000082 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000083 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000084 // the class-name-or-namespace-name is looked up in global scope as a
85 // class-name or namespace-name.
86 //
87 // To implement this, we clear out the object type as soon as we've
88 // seen a leading '::' or part of a nested-name-specifier.
89 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +000090
91 if (Tok.is(tok::code_completion)) {
92 // Code completion for a nested-name-specifier, where the code
93 // code completion token follows the '::'.
94 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
95 ConsumeToken();
96 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Douglas Gregor39a8de12009-02-25 19:37:18 +000099 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000100 // nested-name-specifier 'template'[opt] simple-template-id '::'
101
102 // Parse the optional 'template' keyword, then make sure we have
103 // 'identifier <' after it.
104 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000105 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000106 // nested-name-specifier, since they aren't allowed to start with
107 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000108 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000109 break;
110
Chris Lattner77cf72a2009-06-26 03:47:46 +0000111 SourceLocation TemplateKWLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattner77cf72a2009-06-26 03:47:46 +0000113 if (Tok.isNot(tok::identifier)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000114 Diag(Tok.getLocation(),
Chris Lattner77cf72a2009-06-26 03:47:46 +0000115 diag::err_id_after_template_in_nested_name_spec)
116 << SourceRange(TemplateKWLoc);
117 break;
118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner77cf72a2009-06-26 03:47:46 +0000120 if (NextToken().isNot(tok::less)) {
121 Diag(NextToken().getLocation(),
122 diag::err_less_after_template_name_in_nested_name_spec)
123 << Tok.getIdentifierInfo()->getName()
124 << SourceRange(TemplateKWLoc, Tok.getLocation());
125 break;
126 }
Mike Stump1eb44332009-09-09 15:08:12 +0000127
128 TemplateTy Template
Chris Lattner77cf72a2009-06-26 03:47:46 +0000129 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
130 *Tok.getIdentifierInfo(),
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000131 Tok.getLocation(), SS,
132 ObjectType);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000133 if (!Template)
134 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000135 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
136 &SS, TemplateKWLoc, false))
137 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Chris Lattner77cf72a2009-06-26 03:47:46 +0000139 continue;
140 }
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Douglas Gregor39a8de12009-02-25 19:37:18 +0000142 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000143 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000144 //
145 // simple-template-id '::'
146 //
147 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000148 // right kind (it should name a type or be dependent), and then
149 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000150 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000151 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
152
Mike Stump1eb44332009-09-09 15:08:12 +0000153 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000154 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000155 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000156
Mike Stump1eb44332009-09-09 15:08:12 +0000157 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000158 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000159 Token TypeToken = Tok;
160 ConsumeToken();
161 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
162 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Douglas Gregor39a8de12009-02-25 19:37:18 +0000164 if (!HasScopeSpecifier) {
165 SS.setBeginLoc(TypeToken.getLocation());
166 HasScopeSpecifier = true;
167 }
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Douglas Gregor31a19b62009-04-01 21:51:26 +0000169 if (TypeToken.getAnnotationValue())
170 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000171 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000172 TypeToken.getAnnotationValue(),
173 TypeToken.getAnnotationRange(),
174 CCLoc));
175 else
176 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000177 SS.setEndLoc(CCLoc);
178 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner67b9e832009-06-26 03:45:46 +0000181 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000182 }
183
Chris Lattner5c7f7862009-06-26 03:52:38 +0000184
185 // The rest of the nested-name-specifier possibilities start with
186 // tok::identifier.
187 if (Tok.isNot(tok::identifier))
188 break;
189
190 IdentifierInfo &II = *Tok.getIdentifierInfo();
191
192 // nested-name-specifier:
193 // type-name '::'
194 // namespace-name '::'
195 // nested-name-specifier identifier '::'
196 Token Next = NextToken();
197 if (Next.is(tok::coloncolon)) {
198 // We have an identifier followed by a '::'. Lookup this name
199 // as the name in a nested-name-specifier.
200 SourceLocation IdLoc = ConsumeToken();
201 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
202 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattner5c7f7862009-06-26 03:52:38 +0000204 if (!HasScopeSpecifier) {
205 SS.setBeginLoc(IdLoc);
206 HasScopeSpecifier = true;
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattner5c7f7862009-06-26 03:52:38 +0000209 if (SS.isInvalid())
210 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattner5c7f7862009-06-26 03:52:38 +0000212 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000213 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000214 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000215 SS.setEndLoc(CCLoc);
216 continue;
217 }
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Chris Lattner5c7f7862009-06-26 03:52:38 +0000219 // nested-name-specifier:
220 // type-name '<'
221 if (Next.is(tok::less)) {
222 TemplateTy Template;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000223 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, II,
224 Tok.getLocation(),
225 &SS,
226 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000227 EnteringContext,
228 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000229 // We have found a template name, so annotate this this token
230 // with a template-id annotation. We do not permit the
231 // template-id to be translated into a type annotation,
232 // because some clients (e.g., the parsing of class template
233 // specializations) still want to see the original template-id
234 // token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000235 if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(),
236 false))
237 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000238 continue;
239 }
240 }
241
Douglas Gregor39a8de12009-02-25 19:37:18 +0000242 // We don't have any tokens that form the beginning of a
243 // nested-name-specifier, so we're done.
244 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Douglas Gregor39a8de12009-02-25 19:37:18 +0000247 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000248}
249
250/// ParseCXXIdExpression - Handle id-expression.
251///
252/// id-expression:
253/// unqualified-id
254/// qualified-id
255///
256/// unqualified-id:
257/// identifier
258/// operator-function-id
259/// conversion-function-id [TODO]
260/// '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000261/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000262///
263/// qualified-id:
264/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
265/// '::' identifier
266/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000267/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000268///
269/// nested-name-specifier:
270/// type-name '::'
271/// namespace-name '::'
272/// nested-name-specifier identifier '::'
273/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
274///
275/// NOTE: The standard specifies that, for qualified-id, the parser does not
276/// expect:
277///
278/// '::' conversion-function-id
279/// '::' '~' class-name
280///
281/// This may cause a slight inconsistency on diagnostics:
282///
283/// class C {};
284/// namespace A {}
285/// void f() {
286/// :: A :: ~ C(); // Some Sema error about using destructor with a
287/// // namespace.
288/// :: ~ C(); // Some Parser error like 'unexpected ~'.
289/// }
290///
291/// We simplify the parser a bit and make it work like:
292///
293/// qualified-id:
294/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
295/// '::' unqualified-id
296///
297/// That way Sema can handle and report similar errors for namespaces and the
298/// global scope.
299///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000300/// The isAddressOfOperand parameter indicates that this id-expression is a
301/// direct operand of the address-of operator. This is, besides member contexts,
302/// the only place where a qualified-id naming a non-static class member may
303/// appear.
304///
305Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000306 // qualified-id:
307 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
308 // '::' unqualified-id
309 //
310 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000311 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000312
313 UnqualifiedId Name;
314 if (ParseUnqualifiedId(SS,
315 /*EnteringContext=*/false,
316 /*AllowDestructorName=*/false,
317 /*AllowConstructorName=*/false,
318 Name))
319 return ExprError();
320
321 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
322 isAddressOfOperand);
323
324#if 0
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000325 // unqualified-id:
326 // identifier
327 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000328 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000329 // '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000330 // template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000331 //
332 switch (Tok.getKind()) {
333 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000334 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000335
336 case tok::identifier: {
337 // Consume the identifier so that we can see if it is followed by a '('.
338 IdentifierInfo &II = *Tok.getIdentifierInfo();
339 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000340 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
341 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000342 }
343
344 case tok::kw_operator: {
345 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000346 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000347 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000348 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
349 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000350 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000351 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000352 Tok.is(tok::l_paren), SS,
353 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000354
Douglas Gregor2def4832008-11-17 20:34:05 +0000355 // We already complained about a bad conversion-function-id,
356 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000357 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000358 }
359
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000360 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +0000361 TemplateIdAnnotation *TemplateId
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000362 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
363 assert((TemplateId->Kind == TNK_Function_template ||
364 TemplateId->Kind == TNK_Dependent_template_name) &&
365 "A template type name is not an ID expression");
366
Mike Stump1eb44332009-09-09 15:08:12 +0000367 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000368 TemplateId->getTemplateArgs(),
369 TemplateId->getTemplateArgIsType(),
370 TemplateId->NumArgs);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000372 OwningExprResult Result
Douglas Gregorf17bb742009-10-22 17:20:55 +0000373 = Actions.ActOnTemplateIdExpr(SS,
374 TemplateTy::make(TemplateId->Template),
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000375 TemplateId->TemplateNameLoc,
376 TemplateId->LAngleLoc,
377 TemplateArgsPtr,
378 TemplateId->getTemplateArgLocations(),
379 TemplateId->RAngleLoc);
380 ConsumeToken(); // Consume the template-id token
381 return move(Result);
382 }
383
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000384 } // switch.
385
386 assert(0 && "The switch was supposed to take care everything.");
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000387#endif
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000388}
389
Reid Spencer5f016e22007-07-11 17:01:13 +0000390/// ParseCXXCasts - This handles the various ways to cast expressions to another
391/// type.
392///
393/// postfix-expression: [C++ 5.2p1]
394/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
395/// 'static_cast' '<' type-name '>' '(' expression ')'
396/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
397/// 'const_cast' '<' type-name '>' '(' expression ')'
398///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000399Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 tok::TokenKind Kind = Tok.getKind();
401 const char *CastName = 0; // For error messages
402
403 switch (Kind) {
404 default: assert(0 && "Unknown C++ cast!"); abort();
405 case tok::kw_const_cast: CastName = "const_cast"; break;
406 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
407 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
408 case tok::kw_static_cast: CastName = "static_cast"; break;
409 }
410
411 SourceLocation OpLoc = ConsumeToken();
412 SourceLocation LAngleBracketLoc = Tok.getLocation();
413
414 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000415 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000416
Douglas Gregor809070a2009-02-18 17:45:20 +0000417 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 SourceLocation RAngleBracketLoc = Tok.getLocation();
419
Chris Lattner1ab3b962008-11-18 07:48:38 +0000420 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000421 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000422
423 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
424
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000425 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
426 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000427
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000428 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000430 // Match the ')'.
431 if (Result.isInvalid())
432 SkipUntil(tok::r_paren);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000434 if (Tok.is(tok::r_paren))
435 RParenLoc = ConsumeParen();
436 else
437 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000438
Douglas Gregor809070a2009-02-18 17:45:20 +0000439 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000440 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000441 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000442 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000443 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000444
Sebastian Redl20df9b72008-12-11 22:51:44 +0000445 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000446}
447
Sebastian Redlc42e1182008-11-11 11:37:55 +0000448/// ParseCXXTypeid - This handles the C++ typeid expression.
449///
450/// postfix-expression: [C++ 5.2p1]
451/// 'typeid' '(' expression ')'
452/// 'typeid' '(' type-id ')'
453///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000454Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000455 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
456
457 SourceLocation OpLoc = ConsumeToken();
458 SourceLocation LParenLoc = Tok.getLocation();
459 SourceLocation RParenLoc;
460
461 // typeid expressions are always parenthesized.
462 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
463 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000464 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000465
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000466 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000467
468 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000469 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000470
471 // Match the ')'.
472 MatchRHSPunctuation(tok::r_paren, LParenLoc);
473
Douglas Gregor809070a2009-02-18 17:45:20 +0000474 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000475 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000476
477 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000478 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000479 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000480 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000481 // When typeid is applied to an expression other than an lvalue of a
482 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000483 // operand (Clause 5).
484 //
Mike Stump1eb44332009-09-09 15:08:12 +0000485 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000486 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000487 // we the expression is potentially potentially evaluated.
488 EnterExpressionEvaluationContext Unevaluated(Actions,
489 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000490 Result = ParseExpression();
491
492 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000493 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000494 SkipUntil(tok::r_paren);
495 else {
496 MatchRHSPunctuation(tok::r_paren, LParenLoc);
497
498 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000499 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000500 }
501 }
502
Sebastian Redl20df9b72008-12-11 22:51:44 +0000503 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000504}
505
Reid Spencer5f016e22007-07-11 17:01:13 +0000506/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
507///
508/// boolean-literal: [C++ 2.13.5]
509/// 'true'
510/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000511Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000513 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000514}
Chris Lattner50dd2892008-02-26 00:51:44 +0000515
516/// ParseThrowExpression - This handles the C++ throw expression.
517///
518/// throw-expression: [C++ 15]
519/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000520Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000521 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000522 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000523
Chris Lattner2a2819a2008-04-06 06:02:23 +0000524 // If the current token isn't the start of an assignment-expression,
525 // then the expression is not present. This handles things like:
526 // "C ? throw : (void)42", which is crazy but legal.
527 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
528 case tok::semi:
529 case tok::r_paren:
530 case tok::r_square:
531 case tok::r_brace:
532 case tok::colon:
533 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000534 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000535
Chris Lattner2a2819a2008-04-06 06:02:23 +0000536 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000537 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000538 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000539 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000540 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000541}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000542
543/// ParseCXXThis - This handles the C++ 'this' pointer.
544///
545/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
546/// a non-lvalue expression whose value is the address of the object for which
547/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000548Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000549 assert(Tok.is(tok::kw_this) && "Not 'this'!");
550 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000551 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000552}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000553
554/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
555/// Can be interpreted either as function-style casting ("int(x)")
556/// or class type construction ("ClassType(x,y,z)")
557/// or creation of a value-initialized type ("int()").
558///
559/// postfix-expression: [C++ 5.2p1]
560/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
561/// typename-specifier '(' expression-list[opt] ')' [TODO]
562///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000563Parser::OwningExprResult
564Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000565 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000566 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000567
568 assert(Tok.is(tok::l_paren) && "Expected '('!");
569 SourceLocation LParenLoc = ConsumeParen();
570
Sebastian Redla55e52c2008-11-25 22:21:31 +0000571 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000572 CommaLocsTy CommaLocs;
573
574 if (Tok.isNot(tok::r_paren)) {
575 if (ParseExpressionList(Exprs, CommaLocs)) {
576 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000577 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000578 }
579 }
580
581 // Match the ')'.
582 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
583
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000584 // TypeRep could be null, if it references an invalid typedef.
585 if (!TypeRep)
586 return ExprError();
587
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000588 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
589 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000590 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
591 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000592 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000593}
594
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000595/// ParseCXXCondition - if/switch/while/for condition expression.
596///
597/// condition:
598/// expression
599/// type-specifier-seq declarator '=' assignment-expression
600/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
601/// '=' assignment-expression
602///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000603Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000604 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000605 return ParseExpression(); // expression
606
607 SourceLocation StartLoc = Tok.getLocation();
608
609 // type-specifier-seq
610 DeclSpec DS;
611 ParseSpecifierQualifierList(DS);
612
613 // declarator
614 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
615 ParseDeclarator(DeclaratorInfo);
616
617 // simple-asm-expr[opt]
618 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000619 SourceLocation Loc;
620 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000621 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000622 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000623 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000624 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000625 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000626 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000627 }
628
629 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000630 if (Tok.is(tok::kw___attribute)) {
631 SourceLocation Loc;
632 AttributeList *AttrList = ParseAttributes(&Loc);
633 DeclaratorInfo.AddAttributes(AttrList, Loc);
634 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000635
636 // '=' assignment-expression
637 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000638 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000639 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000640 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000641 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000642 return ExprError();
643
Sebastian Redlf53597f2009-03-15 17:47:39 +0000644 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
645 DeclaratorInfo,EqualLoc,
646 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000647}
648
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000649/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
650/// This should only be called when the current token is known to be part of
651/// simple-type-specifier.
652///
653/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000654/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000655/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
656/// char
657/// wchar_t
658/// bool
659/// short
660/// int
661/// long
662/// signed
663/// unsigned
664/// float
665/// double
666/// void
667/// [GNU] typeof-specifier
668/// [C++0x] auto [TODO]
669///
670/// type-name:
671/// class-name
672/// enum-name
673/// typedef-name
674///
675void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
676 DS.SetRangeStart(Tok.getLocation());
677 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000678 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000679 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000681 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000682 case tok::identifier: // foo::bar
683 case tok::coloncolon: // ::foo::bar
684 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000685 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000686 assert(0 && "Not a simple-type-specifier token!");
687 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000688
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000689 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000690 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000691 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000692 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000693 break;
694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000696 // builtin types
697 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000698 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000699 break;
700 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000701 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000702 break;
703 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000704 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000705 break;
706 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000707 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000708 break;
709 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000710 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000711 break;
712 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000713 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000714 break;
715 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000716 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000717 break;
718 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000719 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000720 break;
721 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000722 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000723 break;
724 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000725 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000726 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000727 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000728 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000729 break;
730 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000731 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000732 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000733 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000734 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000735 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000737 // GNU typeof support.
738 case tok::kw_typeof:
739 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000740 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000741 return;
742 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000743 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000744 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
745 else
746 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000747 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000748 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000749}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000750
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000751/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
752/// [dcl.name]), which is a non-empty sequence of type-specifiers,
753/// e.g., "const short int". Note that the DeclSpec is *not* finished
754/// by parsing the type-specifier-seq, because these sequences are
755/// typically followed by some form of declarator. Returns true and
756/// emits diagnostics if this is not a type-specifier-seq, false
757/// otherwise.
758///
759/// type-specifier-seq: [C++ 8.1]
760/// type-specifier type-specifier-seq[opt]
761///
762bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
763 DS.SetRangeStart(Tok.getLocation());
764 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000765 unsigned DiagID;
766 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000767
768 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000769 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000770 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000771 return true;
772 }
Mike Stump1eb44332009-09-09 15:08:12 +0000773
John McCallfec54012009-08-03 20:12:06 +0000774 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000775
776 return false;
777}
778
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000779/// \brief Finish parsing a C++ unqualified-id that is a template-id of
780/// some form.
781///
782/// This routine is invoked when a '<' is encountered after an identifier or
783/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
784/// whether the unqualified-id is actually a template-id. This routine will
785/// then parse the template arguments and form the appropriate template-id to
786/// return to the caller.
787///
788/// \param SS the nested-name-specifier that precedes this template-id, if
789/// we're actually parsing a qualified-id.
790///
791/// \param Name for constructor and destructor names, this is the actual
792/// identifier that may be a template-name.
793///
794/// \param NameLoc the location of the class-name in a constructor or
795/// destructor.
796///
797/// \param EnteringContext whether we're entering the scope of the
798/// nested-name-specifier.
799///
800/// \param Id as input, describes the template-name or operator-function-id
801/// that precedes the '<'. If template arguments were parsed successfully,
802/// will be updated with the template-id.
803///
804/// \returns true if a parse error occurred, false otherwise.
805bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
806 IdentifierInfo *Name,
807 SourceLocation NameLoc,
808 bool EnteringContext,
809 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:
816 TNK = Actions.isTemplateName(CurScope, *Id.Identifier, Id.StartLocation,
817 &SS, /*ObjectType=*/0, EnteringContext,
818 Template);
819 break;
820
821 case UnqualifiedId::IK_OperatorFunctionId: {
822 // FIXME: Temporary hack: warn that we are completely ignoring the
823 // template arguments for now.
824 // Parse the enclosed template argument list.
825 SourceLocation LAngleLoc, RAngleLoc;
826 TemplateArgList TemplateArgs;
827 TemplateArgIsTypeList TemplateArgIsType;
828 TemplateArgLocationList TemplateArgLocations;
829 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
830 &SS, true, LAngleLoc,
831 TemplateArgs,
832 TemplateArgIsType,
833 TemplateArgLocations,
834 RAngleLoc))
835 return true;
836
837 Diag(Id.StartLocation, diag::warn_operator_template_id_ignores_args)
838 << SourceRange(LAngleLoc, RAngleLoc);
839 break;
840 }
841
842 case UnqualifiedId::IK_ConstructorName:
843 TNK = Actions.isTemplateName(CurScope, *Name, NameLoc,
844 &SS, /*ObjectType=*/0, EnteringContext,
845 Template);
846 break;
847
848 case UnqualifiedId::IK_DestructorName:
849 TNK = Actions.isTemplateName(CurScope, *Name, NameLoc,
850 &SS, /*ObjectType=*/0, EnteringContext,
851 Template);
852 break;
853
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;
864 TemplateArgIsTypeList TemplateArgIsType;
865 TemplateArgLocationList TemplateArgLocations;
866 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
867 &SS, true, LAngleLoc,
868 TemplateArgs,
869 TemplateArgIsType,
870 TemplateArgLocations,
871 RAngleLoc))
872 return true;
873
874 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
875 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) {
876 // Form a parsed representation of the template-id to be stored in the
877 // UnqualifiedId.
878 TemplateIdAnnotation *TemplateId
879 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
880
881 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
882 TemplateId->Name = Id.Identifier;
883 TemplateId->TemplateNameLoc = Id.StartLocation;
884 } else {
885 // FIXME: Handle IK_OperatorFunctionId
886 }
887
888 TemplateId->Template = Template.getAs<void*>();
889 TemplateId->Kind = TNK;
890 TemplateId->LAngleLoc = LAngleLoc;
891 TemplateId->RAngleLoc = RAngleLoc;
892 void **Args = TemplateId->getTemplateArgs();
893 bool *ArgIsType = TemplateId->getTemplateArgIsType();
894 SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations();
895 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
896 Arg != ArgEnd; ++Arg) {
897 Args[Arg] = TemplateArgs[Arg];
898 ArgIsType[Arg] = TemplateArgIsType[Arg];
899 ArgLocs[Arg] = TemplateArgLocations[Arg];
900 }
901
902 Id.setTemplateId(TemplateId);
903 return false;
904 }
905
906 // Bundle the template arguments together.
907 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
908 TemplateArgIsType.data(),
909 TemplateArgs.size());
910
911 // Constructor and destructor names.
912 Action::TypeResult Type
913 = Actions.ActOnTemplateIdType(Template, NameLoc,
914 LAngleLoc, TemplateArgsPtr,
915 &TemplateArgLocations[0],
916 RAngleLoc);
917 if (Type.isInvalid())
918 return true;
919
920 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
921 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
922 else
923 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
924
925 return false;
926}
927
928/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
929/// name of an entity.
930///
931/// \code
932/// unqualified-id: [C++ expr.prim.general]
933/// identifier
934/// operator-function-id
935/// conversion-function-id
936/// [C++0x] literal-operator-id [TODO]
937/// ~ class-name
938/// template-id
939///
940/// operator-function-id: [C++ 13.5]
941/// 'operator' operator
942///
943/// operator: one of
944/// new delete new[] delete[]
945/// + - * / % ^ & | ~
946/// ! = < > += -= *= /= %=
947/// ^= &= |= << >> >>= <<= == !=
948/// <= >= && || ++ -- , ->* ->
949/// () []
950///
951/// conversion-function-id: [C++ 12.3.2]
952/// operator conversion-type-id
953///
954/// conversion-type-id:
955/// type-specifier-seq conversion-declarator[opt]
956///
957/// conversion-declarator:
958/// ptr-operator conversion-declarator[opt]
959/// \endcode
960///
961/// \param The nested-name-specifier that preceded this unqualified-id. If
962/// non-empty, then we are parsing the unqualified-id of a qualified-id.
963///
964/// \param EnteringContext whether we are entering the scope of the
965/// nested-name-specifier.
966///
967/// \param AllowDestructorName whether we allow parsing of a destructor name.
968///
969/// \param AllowConstructorName whether we allow parsing a constructor name.
970///
971/// \param Result on a successful parse, contains the parsed unqualified-id.
972///
973/// \returns true if parsing fails, false otherwise.
974bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
975 bool AllowDestructorName,
976 bool AllowConstructorName,
977 UnqualifiedId &Result) {
978 // unqualified-id:
979 // identifier
980 // template-id (when it hasn't already been annotated)
981 if (Tok.is(tok::identifier)) {
982 // Consume the identifier.
983 IdentifierInfo *Id = Tok.getIdentifierInfo();
984 SourceLocation IdLoc = ConsumeToken();
985
986 if (AllowConstructorName &&
987 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
988 // We have parsed a constructor name.
989 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
990 &SS, false),
991 IdLoc, IdLoc);
992 } else {
993 // We have parsed an identifier.
994 Result.setIdentifier(Id, IdLoc);
995 }
996
997 // If the next token is a '<', we may have a template.
998 if (Tok.is(tok::less))
999 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
1000 Result);
1001
1002 return false;
1003 }
1004
1005 // unqualified-id:
1006 // template-id (already parsed and annotated)
1007 if (Tok.is(tok::annot_template_id)) {
1008 // FIXME: Could this be a constructor name???
1009
1010 // We have already parsed a template-id; consume the annotation token as
1011 // our unqualified-id.
1012 Result.setTemplateId(
1013 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1014 ConsumeToken();
1015 return false;
1016 }
1017
1018 // unqualified-id:
1019 // operator-function-id
1020 // conversion-function-id
1021 if (Tok.is(tok::kw_operator)) {
1022 // Consume the 'operator' keyword.
1023 SourceLocation KeywordLoc = ConsumeToken();
1024
1025 // Determine what kind of operator name we have.
1026 unsigned SymbolIdx = 0;
1027 SourceLocation SymbolLocations[3];
1028 OverloadedOperatorKind Op = OO_None;
1029 switch (Tok.getKind()) {
1030 case tok::kw_new:
1031 case tok::kw_delete: {
1032 bool isNew = Tok.getKind() == tok::kw_new;
1033 // Consume the 'new' or 'delete'.
1034 SymbolLocations[SymbolIdx++] = ConsumeToken();
1035 if (Tok.is(tok::l_square)) {
1036 // Consume the '['.
1037 SourceLocation LBracketLoc = ConsumeBracket();
1038 // Consume the ']'.
1039 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1040 LBracketLoc);
1041 if (RBracketLoc.isInvalid())
1042 return true;
1043
1044 SymbolLocations[SymbolIdx++] = LBracketLoc;
1045 SymbolLocations[SymbolIdx++] = RBracketLoc;
1046 Op = isNew? OO_Array_New : OO_Array_Delete;
1047 } else {
1048 Op = isNew? OO_New : OO_Delete;
1049 }
1050 break;
1051 }
1052
1053 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1054 case tok::Token: \
1055 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1056 Op = OO_##Name; \
1057 break;
1058 #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1059 #include "clang/Basic/OperatorKinds.def"
1060
1061 case tok::l_paren: {
1062 // Consume the '('.
1063 SourceLocation LParenLoc = ConsumeParen();
1064 // Consume the ')'.
1065 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1066 LParenLoc);
1067 if (RParenLoc.isInvalid())
1068 return true;
1069
1070 SymbolLocations[SymbolIdx++] = LParenLoc;
1071 SymbolLocations[SymbolIdx++] = RParenLoc;
1072 Op = OO_Call;
1073 break;
1074 }
1075
1076 case tok::l_square: {
1077 // Consume the '['.
1078 SourceLocation LBracketLoc = ConsumeBracket();
1079 // Consume the ']'.
1080 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1081 LBracketLoc);
1082 if (RBracketLoc.isInvalid())
1083 return true;
1084
1085 SymbolLocations[SymbolIdx++] = LBracketLoc;
1086 SymbolLocations[SymbolIdx++] = RBracketLoc;
1087 Op = OO_Subscript;
1088 break;
1089 }
1090
1091 case tok::code_completion: {
1092 // Code completion for the operator name.
1093 Actions.CodeCompleteOperatorName(CurScope);
1094
1095 // Consume the operator token.
1096 ConsumeToken();
1097
1098 // Don't try to parse any further.
1099 return true;
1100 }
1101
1102 default:
1103 break;
1104 }
1105
1106 if (Op != OO_None) {
1107 // We have parsed an operator-function-id.
1108 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1109
1110 // If the next token is a '<', we may have a template.
1111 if (Tok.is(tok::less))
1112 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1113 EnteringContext, Result);
1114
1115 return false;
1116 }
1117
1118 // Parse a conversion-function-id.
1119 //
1120 // conversion-function-id: [C++ 12.3.2]
1121 // operator conversion-type-id
1122 //
1123 // conversion-type-id:
1124 // type-specifier-seq conversion-declarator[opt]
1125 //
1126 // conversion-declarator:
1127 // ptr-operator conversion-declarator[opt]
1128
1129 // Parse the type-specifier-seq.
1130 DeclSpec DS;
1131 if (ParseCXXTypeSpecifierSeq(DS))
1132 return true;
1133
1134 // Parse the conversion-declarator, which is merely a sequence of
1135 // ptr-operators.
1136 Declarator D(DS, Declarator::TypeNameContext);
1137 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1138
1139 // Finish up the type.
1140 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1141 if (Ty.isInvalid())
1142 return true;
1143
1144 // Note that this is a conversion-function-id.
1145 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1146 D.getSourceRange().getEnd());
1147 return false;
1148 }
1149
1150 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1151 // C++ [expr.unary.op]p10:
1152 // There is an ambiguity in the unary-expression ~X(), where X is a
1153 // class-name. The ambiguity is resolved in favor of treating ~ as a
1154 // unary complement rather than treating ~X as referring to a destructor.
1155
1156 // Parse the '~'.
1157 SourceLocation TildeLoc = ConsumeToken();
1158
1159 // Parse the class-name.
1160 if (Tok.isNot(tok::identifier)) {
1161 Diag(Tok, diag::err_destructor_class_name);
1162 return true;
1163 }
1164
1165 // Parse the class-name (or template-name in a simple-template-id).
1166 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1167 SourceLocation ClassNameLoc = ConsumeToken();
1168
1169 // Note that this is a destructor name.
1170 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
1171 CurScope, &SS);
1172 if (!Ty) {
1173 Diag(ClassNameLoc, diag::err_destructor_class_name);
1174 return true;
1175 }
1176
1177 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
1178
1179 if (Tok.is(tok::less))
1180 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1181 EnteringContext, Result);
1182
1183 return false;
1184 }
1185
1186 Diag(Tok, diag::err_expected_unqualified_id);
1187 return true;
1188}
1189
Douglas Gregor43c7bad2008-11-17 16:14:12 +00001190/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001191/// operator name (C++ [over.oper]). If successful, returns the
1192/// predefined identifier that corresponds to that overloaded
1193/// operator. Otherwise, returns NULL and does not consume any tokens.
1194///
1195/// operator-function-id: [C++ 13.5]
1196/// 'operator' operator
1197///
1198/// operator: one of
1199/// new delete new[] delete[]
1200/// + - * / % ^ & | ~
1201/// ! = < > += -= *= /= %=
1202/// ^= &= |= << >> >>= <<= == !=
1203/// <= >= && || ++ -- , ->* ->
1204/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +00001205OverloadedOperatorKind
1206Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +00001207 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +00001208 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001209
1210 OverloadedOperatorKind Op = OO_None;
1211 switch (NextToken().getKind()) {
1212 case tok::kw_new:
1213 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001214 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001215 if (Tok.is(tok::l_square)) {
1216 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +00001217 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001218 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
1219 Op = OO_Array_New;
1220 } else {
1221 Op = OO_New;
1222 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001223 if (EndLoc)
1224 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001225 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001226
1227 case tok::kw_delete:
1228 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001229 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001230 if (Tok.is(tok::l_square)) {
1231 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +00001232 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001233 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
1234 Op = OO_Array_Delete;
1235 } else {
1236 Op = OO_Delete;
1237 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001238 if (EndLoc)
1239 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001240 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001241
Douglas Gregor02bcd4c2008-11-10 13:38:07 +00001242#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001243 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +00001244#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001245#include "clang/Basic/OperatorKinds.def"
1246
1247 case tok::l_paren:
1248 ConsumeToken(); // 'operator'
1249 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +00001250 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001251 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001252 if (EndLoc)
1253 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001254 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001255
1256 case tok::l_square:
1257 ConsumeToken(); // 'operator'
1258 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +00001259 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001260 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001261 if (EndLoc)
1262 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001263 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001264
Douglas Gregored8d3222009-09-18 20:05:18 +00001265 case tok::code_completion: {
1266 // Code completion for the operator name.
1267 Actions.CodeCompleteOperatorName(CurScope);
1268
1269 // Consume the 'operator' token, then replace the code-completion token
1270 // with an 'operator' token and try again.
1271 SourceLocation OperatorLoc = ConsumeToken();
1272 Tok.setLocation(OperatorLoc);
1273 Tok.setKind(tok::kw_operator);
1274 return TryParseOperatorFunctionId(EndLoc);
1275 }
1276
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001277 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001278 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001279 }
1280
Douglas Gregor43c7bad2008-11-17 16:14:12 +00001281 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001282 Loc = ConsumeAnyToken(); // the operator itself
1283 if (EndLoc)
1284 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001285 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001286}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001287
1288/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
1289/// which expresses the name of a user-defined conversion operator
1290/// (C++ [class.conv.fct]p1). Returns the type that this operator is
1291/// specifying a conversion for, or NULL if there was an error.
1292///
1293/// conversion-function-id: [C++ 12.3.2]
1294/// operator conversion-type-id
1295///
1296/// conversion-type-id:
1297/// type-specifier-seq conversion-declarator[opt]
1298///
1299/// conversion-declarator:
1300/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +00001301Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001302 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1303 ConsumeToken(); // 'operator'
1304
1305 // Parse the type-specifier-seq.
1306 DeclSpec DS;
1307 if (ParseCXXTypeSpecifierSeq(DS))
1308 return 0;
1309
1310 // Parse the conversion-declarator, which is merely a sequence of
1311 // ptr-operators.
1312 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001313 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001314 if (EndLoc)
1315 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001316
1317 // Finish up the type.
1318 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001319 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001320 return 0;
1321 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001322 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001323}
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001324
1325/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1326/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001327///
Chris Lattner59232d32009-01-04 21:25:24 +00001328/// This method is called to parse the new expression after the optional :: has
1329/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1330/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001331///
1332/// new-expression:
1333/// '::'[opt] 'new' new-placement[opt] new-type-id
1334/// new-initializer[opt]
1335/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1336/// new-initializer[opt]
1337///
1338/// new-placement:
1339/// '(' expression-list ')'
1340///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001341/// new-type-id:
1342/// type-specifier-seq new-declarator[opt]
1343///
1344/// new-declarator:
1345/// ptr-operator new-declarator[opt]
1346/// direct-new-declarator
1347///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001348/// new-initializer:
1349/// '(' expression-list[opt] ')'
1350/// [C++0x] braced-init-list [TODO]
1351///
Chris Lattner59232d32009-01-04 21:25:24 +00001352Parser::OwningExprResult
1353Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1354 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1355 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001356
1357 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1358 // second form of new-expression. It can't be a new-type-id.
1359
Sebastian Redla55e52c2008-11-25 22:21:31 +00001360 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001361 SourceLocation PlacementLParen, PlacementRParen;
1362
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001363 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001364 DeclSpec DS;
1365 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001366 if (Tok.is(tok::l_paren)) {
1367 // If it turns out to be a placement, we change the type location.
1368 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001369 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1370 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001371 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001372 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001373
1374 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001375 if (PlacementRParen.isInvalid()) {
1376 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001377 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001378 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001379
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001380 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001381 // Reset the placement locations. There was no placement.
1382 PlacementLParen = PlacementRParen = SourceLocation();
1383 ParenTypeId = true;
1384 } else {
1385 // We still need the type.
1386 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001387 SourceLocation LParen = ConsumeParen();
1388 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001389 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001390 ParseDeclarator(DeclaratorInfo);
1391 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001392 ParenTypeId = true;
1393 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001394 if (ParseCXXTypeSpecifierSeq(DS))
1395 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001396 else {
1397 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001398 ParseDeclaratorInternal(DeclaratorInfo,
1399 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001400 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001401 ParenTypeId = false;
1402 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001403 }
1404 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001405 // A new-type-id is a simplified type-id, where essentially the
1406 // direct-declarator is replaced by a direct-new-declarator.
1407 if (ParseCXXTypeSpecifierSeq(DS))
1408 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001409 else {
1410 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001411 ParseDeclaratorInternal(DeclaratorInfo,
1412 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001413 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001414 ParenTypeId = false;
1415 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001416 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001417 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001418 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001419 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001420
Sebastian Redla55e52c2008-11-25 22:21:31 +00001421 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001422 SourceLocation ConstructorLParen, ConstructorRParen;
1423
1424 if (Tok.is(tok::l_paren)) {
1425 ConstructorLParen = ConsumeParen();
1426 if (Tok.isNot(tok::r_paren)) {
1427 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001428 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1429 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001430 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001431 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001432 }
1433 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001434 if (ConstructorRParen.isInvalid()) {
1435 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001436 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001437 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001438 }
1439
Sebastian Redlf53597f2009-03-15 17:47:39 +00001440 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1441 move_arg(PlacementArgs), PlacementRParen,
1442 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1443 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001444}
1445
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001446/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1447/// passed to ParseDeclaratorInternal.
1448///
1449/// direct-new-declarator:
1450/// '[' expression ']'
1451/// direct-new-declarator '[' constant-expression ']'
1452///
Chris Lattner59232d32009-01-04 21:25:24 +00001453void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001454 // Parse the array dimensions.
1455 bool first = true;
1456 while (Tok.is(tok::l_square)) {
1457 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001458 OwningExprResult Size(first ? ParseExpression()
1459 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001460 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001461 // Recover
1462 SkipUntil(tok::r_square);
1463 return;
1464 }
1465 first = false;
1466
Sebastian Redlab197ba2009-02-09 18:23:29 +00001467 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001468 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001469 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001470 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001471
Sebastian Redlab197ba2009-02-09 18:23:29 +00001472 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001473 return;
1474 }
1475}
1476
1477/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1478/// This ambiguity appears in the syntax of the C++ new operator.
1479///
1480/// new-expression:
1481/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1482/// new-initializer[opt]
1483///
1484/// new-placement:
1485/// '(' expression-list ')'
1486///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001487bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001488 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001489 // The '(' was already consumed.
1490 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001491 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001492 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001493 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001494 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001495 }
1496
1497 // It's not a type, it has to be an expression list.
1498 // Discard the comma locations - ActOnCXXNew has enough parameters.
1499 CommaLocsTy CommaLocs;
1500 return ParseExpressionList(PlacementArgs, CommaLocs);
1501}
1502
1503/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1504/// to free memory allocated by new.
1505///
Chris Lattner59232d32009-01-04 21:25:24 +00001506/// This method is called to parse the 'delete' expression after the optional
1507/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1508/// and "Start" is its location. Otherwise, "Start" is the location of the
1509/// 'delete' token.
1510///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001511/// delete-expression:
1512/// '::'[opt] 'delete' cast-expression
1513/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001514Parser::OwningExprResult
1515Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1516 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1517 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001518
1519 // Array delete?
1520 bool ArrayDelete = false;
1521 if (Tok.is(tok::l_square)) {
1522 ArrayDelete = true;
1523 SourceLocation LHS = ConsumeBracket();
1524 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1525 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001526 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001527 }
1528
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001529 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001530 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001531 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001532
Sebastian Redlf53597f2009-03-15 17:47:39 +00001533 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001534}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001535
Mike Stump1eb44332009-09-09 15:08:12 +00001536static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001537 switch(kind) {
1538 default: assert(false && "Not a known unary type trait.");
1539 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1540 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1541 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1542 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1543 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1544 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1545 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1546 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1547 case tok::kw___is_abstract: return UTT_IsAbstract;
1548 case tok::kw___is_class: return UTT_IsClass;
1549 case tok::kw___is_empty: return UTT_IsEmpty;
1550 case tok::kw___is_enum: return UTT_IsEnum;
1551 case tok::kw___is_pod: return UTT_IsPOD;
1552 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1553 case tok::kw___is_union: return UTT_IsUnion;
1554 }
1555}
1556
1557/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1558/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1559/// templates.
1560///
1561/// primary-expression:
1562/// [GNU] unary-type-trait '(' type-id ')'
1563///
Mike Stump1eb44332009-09-09 15:08:12 +00001564Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001565 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1566 SourceLocation Loc = ConsumeToken();
1567
1568 SourceLocation LParen = Tok.getLocation();
1569 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1570 return ExprError();
1571
1572 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1573 // there will be cryptic errors about mismatched parentheses and missing
1574 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001575 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001576
1577 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1578
Douglas Gregor809070a2009-02-18 17:45:20 +00001579 if (Ty.isInvalid())
1580 return ExprError();
1581
1582 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001583}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001584
1585/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1586/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1587/// based on the context past the parens.
1588Parser::OwningExprResult
1589Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1590 TypeTy *&CastTy,
1591 SourceLocation LParenLoc,
1592 SourceLocation &RParenLoc) {
1593 assert(getLang().CPlusPlus && "Should only be called for C++!");
1594 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1595 assert(isTypeIdInParens() && "Not a type-id!");
1596
1597 OwningExprResult Result(Actions, true);
1598 CastTy = 0;
1599
1600 // We need to disambiguate a very ugly part of the C++ syntax:
1601 //
1602 // (T())x; - type-id
1603 // (T())*x; - type-id
1604 // (T())/x; - expression
1605 // (T()); - expression
1606 //
1607 // The bad news is that we cannot use the specialized tentative parser, since
1608 // it can only verify that the thing inside the parens can be parsed as
1609 // type-id, it is not useful for determining the context past the parens.
1610 //
1611 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001612 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001613 //
1614 // It uses a scheme similar to parsing inline methods. The parenthesized
1615 // tokens are cached, the context that follows is determined (possibly by
1616 // parsing a cast-expression), and then we re-introduce the cached tokens
1617 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001618
Mike Stump1eb44332009-09-09 15:08:12 +00001619 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001620 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001621
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001622 // Store the tokens of the parentheses. We will parse them after we determine
1623 // the context that follows them.
1624 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1625 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001626 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1627 return ExprError();
1628 }
1629
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001630 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001631 ParseAs = CompoundLiteral;
1632 } else {
1633 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001634 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1635 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1636 NotCastExpr = true;
1637 } else {
1638 // Try parsing the cast-expression that may follow.
1639 // If it is not a cast-expression, NotCastExpr will be true and no token
1640 // will be consumed.
1641 Result = ParseCastExpression(false/*isUnaryExpression*/,
1642 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001643 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001644 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001645
1646 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1647 // an expression.
1648 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001649 }
1650
Mike Stump1eb44332009-09-09 15:08:12 +00001651 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001652 Toks.push_back(Tok);
1653 // Re-enter the stored parenthesized tokens into the token stream, so we may
1654 // parse them now.
1655 PP.EnterTokenStream(Toks.data(), Toks.size(),
1656 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1657 // Drop the current token and bring the first cached one. It's the same token
1658 // as when we entered this function.
1659 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001660
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001661 if (ParseAs >= CompoundLiteral) {
1662 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001663
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001664 // Match the ')'.
1665 if (Tok.is(tok::r_paren))
1666 RParenLoc = ConsumeParen();
1667 else
1668 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001669
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001670 if (ParseAs == CompoundLiteral) {
1671 ExprType = CompoundLiteral;
1672 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1673 }
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001675 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1676 assert(ParseAs == CastExpr);
1677
1678 if (Ty.isInvalid())
1679 return ExprError();
1680
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001681 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001682
1683 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001684 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001685 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001686 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001687 return move(Result);
1688 }
Mike Stump1eb44332009-09-09 15:08:12 +00001689
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001690 // Not a compound literal, and not followed by a cast-expression.
1691 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001692
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001693 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001694 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001695 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1696 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1697
1698 // Match the ')'.
1699 if (Result.isInvalid()) {
1700 SkipUntil(tok::r_paren);
1701 return ExprError();
1702 }
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001704 if (Tok.is(tok::r_paren))
1705 RParenLoc = ConsumeParen();
1706 else
1707 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1708
1709 return move(Result);
1710}