blob: a248a5dbeef1ef16ab4fb83f61ad19877949896c [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000017using namespace clang;
18
Douglas Gregor2dd078a2009-09-02 22:59:36 +000019/// \brief Parse global scope or nested-name-specifier if present.
20///
21/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
22/// may be preceded by '::'). Note that this routine will not parse ::new or
23/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000024///
25/// '::'[opt] nested-name-specifier
26/// '::'
27///
28/// nested-name-specifier:
29/// type-name '::'
30/// namespace-name '::'
31/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000032/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000033///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000034///
35/// \param SS the scope specifier that will be set to the parsed
36/// nested-name-specifier (or empty)
37///
38/// \param ObjectType if this nested-name-specifier is being parsed following
39/// the "." or "->" of a member access expression, this parameter provides the
40/// type of the object whose members are being accessed.
41///
42/// \param EnteringContext whether we will be entering into the context of
43/// the nested-name-specifier after parsing it.
44///
45/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000046bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000047 Action::TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +000048 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000049 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000050 "Call sites of this function should be guarded by checking for C++");
Douglas Gregor495c35d2009-08-25 22:51:20 +000051
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000052 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000053 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000054 SS.setRange(Tok.getAnnotationRange());
55 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000056 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000057 }
Chris Lattnere607e802009-01-04 21:14:15 +000058
Douglas Gregor39a8de12009-02-25 19:37:18 +000059 bool HasScopeSpecifier = false;
60
Chris Lattner5b454732009-01-05 03:55:46 +000061 if (Tok.is(tok::coloncolon)) {
62 // ::new and ::delete aren't nested-name-specifiers.
63 tok::TokenKind NextKind = NextToken().getKind();
64 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
65 return false;
Chris Lattner55a7cef2009-01-05 00:13:00 +000066
Chris Lattner55a7cef2009-01-05 00:13:00 +000067 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000068 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000069 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000070 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000071 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000072 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000073 }
74
Douglas Gregor39a8de12009-02-25 19:37:18 +000075 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 if (HasScopeSpecifier) {
77 // C++ [basic.lookup.classref]p5:
78 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000079 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000080 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000081 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000082 // the class-name-or-namespace-name is looked up in global scope as a
83 // class-name or namespace-name.
84 //
85 // To implement this, we clear out the object type as soon as we've
86 // seen a leading '::' or part of a nested-name-specifier.
87 ObjectType = 0;
88 }
89
Douglas Gregor39a8de12009-02-25 19:37:18 +000090 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +000091 // nested-name-specifier 'template'[opt] simple-template-id '::'
92
93 // Parse the optional 'template' keyword, then make sure we have
94 // 'identifier <' after it.
95 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000096 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +000097 // nested-name-specifier, since they aren't allowed to start with
98 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000099 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000100 break;
101
Chris Lattner77cf72a2009-06-26 03:47:46 +0000102 SourceLocation TemplateKWLoc = ConsumeToken();
103
104 if (Tok.isNot(tok::identifier)) {
105 Diag(Tok.getLocation(),
106 diag::err_id_after_template_in_nested_name_spec)
107 << SourceRange(TemplateKWLoc);
108 break;
109 }
110
111 if (NextToken().isNot(tok::less)) {
112 Diag(NextToken().getLocation(),
113 diag::err_less_after_template_name_in_nested_name_spec)
114 << Tok.getIdentifierInfo()->getName()
115 << SourceRange(TemplateKWLoc, Tok.getLocation());
116 break;
117 }
118
119 TemplateTy Template
120 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
121 *Tok.getIdentifierInfo(),
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000122 Tok.getLocation(), SS,
123 ObjectType);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000124 if (!Template)
125 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000126 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
127 &SS, TemplateKWLoc, false))
128 break;
129
Chris Lattner77cf72a2009-06-26 03:47:46 +0000130 continue;
131 }
132
Douglas Gregor39a8de12009-02-25 19:37:18 +0000133 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
134 // We have
135 //
136 // simple-template-id '::'
137 //
138 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000139 // right kind (it should name a type or be dependent), and then
140 // convert it into a type within the nested-name-specifier.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000141 TemplateIdAnnotation *TemplateId
142 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
143
Douglas Gregorc45c2322009-03-31 00:43:58 +0000144 if (TemplateId->Kind == TNK_Type_template ||
145 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000146 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000147
148 assert(Tok.is(tok::annot_typename) &&
149 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000150 Token TypeToken = Tok;
151 ConsumeToken();
152 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
153 SourceLocation CCLoc = ConsumeToken();
154
155 if (!HasScopeSpecifier) {
156 SS.setBeginLoc(TypeToken.getLocation());
157 HasScopeSpecifier = true;
158 }
Douglas Gregor31a19b62009-04-01 21:51:26 +0000159
160 if (TypeToken.getAnnotationValue())
161 SS.setScopeRep(
162 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
163 TypeToken.getAnnotationValue(),
164 TypeToken.getAnnotationRange(),
165 CCLoc));
166 else
167 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000168 SS.setEndLoc(CCLoc);
169 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000170 }
171
172 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000173 }
174
Chris Lattner5c7f7862009-06-26 03:52:38 +0000175
176 // The rest of the nested-name-specifier possibilities start with
177 // tok::identifier.
178 if (Tok.isNot(tok::identifier))
179 break;
180
181 IdentifierInfo &II = *Tok.getIdentifierInfo();
182
183 // nested-name-specifier:
184 // type-name '::'
185 // namespace-name '::'
186 // nested-name-specifier identifier '::'
187 Token Next = NextToken();
188 if (Next.is(tok::coloncolon)) {
189 // We have an identifier followed by a '::'. Lookup this name
190 // as the name in a nested-name-specifier.
191 SourceLocation IdLoc = ConsumeToken();
192 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
193 SourceLocation CCLoc = ConsumeToken();
194
195 if (!HasScopeSpecifier) {
196 SS.setBeginLoc(IdLoc);
197 HasScopeSpecifier = true;
198 }
199
200 if (SS.isInvalid())
201 continue;
202
203 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000204 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000205 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000206 SS.setEndLoc(CCLoc);
207 continue;
208 }
209
210 // nested-name-specifier:
211 // type-name '<'
212 if (Next.is(tok::less)) {
213 TemplateTy Template;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000214 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, II,
215 Tok.getLocation(),
216 &SS,
217 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000218 EnteringContext,
219 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000220 // We have found a template name, so annotate this this token
221 // with a template-id annotation. We do not permit the
222 // template-id to be translated into a type annotation,
223 // because some clients (e.g., the parsing of class template
224 // specializations) still want to see the original template-id
225 // token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000226 if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(),
227 false))
228 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000229 continue;
230 }
231 }
232
Douglas Gregor39a8de12009-02-25 19:37:18 +0000233 // We don't have any tokens that form the beginning of a
234 // nested-name-specifier, so we're done.
235 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000236 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000237
238 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000239}
240
241/// ParseCXXIdExpression - Handle id-expression.
242///
243/// id-expression:
244/// unqualified-id
245/// qualified-id
246///
247/// unqualified-id:
248/// identifier
249/// operator-function-id
250/// conversion-function-id [TODO]
251/// '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000252/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000253///
254/// qualified-id:
255/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
256/// '::' identifier
257/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000258/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000259///
260/// nested-name-specifier:
261/// type-name '::'
262/// namespace-name '::'
263/// nested-name-specifier identifier '::'
264/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
265///
266/// NOTE: The standard specifies that, for qualified-id, the parser does not
267/// expect:
268///
269/// '::' conversion-function-id
270/// '::' '~' class-name
271///
272/// This may cause a slight inconsistency on diagnostics:
273///
274/// class C {};
275/// namespace A {}
276/// void f() {
277/// :: A :: ~ C(); // Some Sema error about using destructor with a
278/// // namespace.
279/// :: ~ C(); // Some Parser error like 'unexpected ~'.
280/// }
281///
282/// We simplify the parser a bit and make it work like:
283///
284/// qualified-id:
285/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
286/// '::' unqualified-id
287///
288/// That way Sema can handle and report similar errors for namespaces and the
289/// global scope.
290///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000291/// The isAddressOfOperand parameter indicates that this id-expression is a
292/// direct operand of the address-of operator. This is, besides member contexts,
293/// the only place where a qualified-id naming a non-static class member may
294/// appear.
295///
296Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000297 // qualified-id:
298 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
299 // '::' unqualified-id
300 //
301 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000302 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000303
304 // unqualified-id:
305 // identifier
306 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000307 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000308 // '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000309 // template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000310 //
311 switch (Tok.getKind()) {
312 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000313 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000314
315 case tok::identifier: {
316 // Consume the identifier so that we can see if it is followed by a '('.
317 IdentifierInfo &II = *Tok.getIdentifierInfo();
318 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000319 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
320 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000321 }
322
323 case tok::kw_operator: {
324 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000325 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000326 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000327 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
328 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000329 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000330 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000331 Tok.is(tok::l_paren), SS,
332 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000333
Douglas Gregor2def4832008-11-17 20:34:05 +0000334 // We already complained about a bad conversion-function-id,
335 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000336 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000337 }
338
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000339 case tok::annot_template_id: {
340 TemplateIdAnnotation *TemplateId
341 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
342 assert((TemplateId->Kind == TNK_Function_template ||
343 TemplateId->Kind == TNK_Dependent_template_name) &&
344 "A template type name is not an ID expression");
345
346 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
347 TemplateId->getTemplateArgs(),
348 TemplateId->getTemplateArgIsType(),
349 TemplateId->NumArgs);
350
351 OwningExprResult Result
352 = Actions.ActOnTemplateIdExpr(TemplateTy::make(TemplateId->Template),
353 TemplateId->TemplateNameLoc,
354 TemplateId->LAngleLoc,
355 TemplateArgsPtr,
356 TemplateId->getTemplateArgLocations(),
357 TemplateId->RAngleLoc);
358 ConsumeToken(); // Consume the template-id token
359 return move(Result);
360 }
361
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000362 } // switch.
363
364 assert(0 && "The switch was supposed to take care everything.");
365}
366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367/// ParseCXXCasts - This handles the various ways to cast expressions to another
368/// type.
369///
370/// postfix-expression: [C++ 5.2p1]
371/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
372/// 'static_cast' '<' type-name '>' '(' expression ')'
373/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
374/// 'const_cast' '<' type-name '>' '(' expression ')'
375///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000376Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 tok::TokenKind Kind = Tok.getKind();
378 const char *CastName = 0; // For error messages
379
380 switch (Kind) {
381 default: assert(0 && "Unknown C++ cast!"); abort();
382 case tok::kw_const_cast: CastName = "const_cast"; break;
383 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
384 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
385 case tok::kw_static_cast: CastName = "static_cast"; break;
386 }
387
388 SourceLocation OpLoc = ConsumeToken();
389 SourceLocation LAngleBracketLoc = Tok.getLocation();
390
391 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000392 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000393
Douglas Gregor809070a2009-02-18 17:45:20 +0000394 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 SourceLocation RAngleBracketLoc = Tok.getLocation();
396
Chris Lattner1ab3b962008-11-18 07:48:38 +0000397 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000398 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000399
400 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
401
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000402 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
403 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000404
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000405 OwningExprResult Result = ParseExpression();
406
407 // Match the ')'.
408 if (Result.isInvalid())
409 SkipUntil(tok::r_paren);
410
411 if (Tok.is(tok::r_paren))
412 RParenLoc = ConsumeParen();
413 else
414 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000415
Douglas Gregor809070a2009-02-18 17:45:20 +0000416 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000417 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000418 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000419 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000420 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000421
Sebastian Redl20df9b72008-12-11 22:51:44 +0000422 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000423}
424
Sebastian Redlc42e1182008-11-11 11:37:55 +0000425/// ParseCXXTypeid - This handles the C++ typeid expression.
426///
427/// postfix-expression: [C++ 5.2p1]
428/// 'typeid' '(' expression ')'
429/// 'typeid' '(' type-id ')'
430///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000431Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000432 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
433
434 SourceLocation OpLoc = ConsumeToken();
435 SourceLocation LParenLoc = Tok.getLocation();
436 SourceLocation RParenLoc;
437
438 // typeid expressions are always parenthesized.
439 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
440 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000441 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000442
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000443 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000444
445 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000446 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000447
448 // Match the ')'.
449 MatchRHSPunctuation(tok::r_paren, LParenLoc);
450
Douglas Gregor809070a2009-02-18 17:45:20 +0000451 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000452 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000453
454 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000455 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000456 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000457 // C++0x [expr.typeid]p3:
458 // When typeid is applied to an expression other than an lvalue of a
459 // polymorphic class type [...] The expression is an unevaluated
460 // operand (Clause 5).
461 //
462 // Note that we can't tell whether the expression is an lvalue of a
463 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000464 // we the expression is potentially potentially evaluated.
465 EnterExpressionEvaluationContext Unevaluated(Actions,
466 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000467 Result = ParseExpression();
468
469 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000470 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000471 SkipUntil(tok::r_paren);
472 else {
473 MatchRHSPunctuation(tok::r_paren, LParenLoc);
474
475 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000476 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000477 }
478 }
479
Sebastian Redl20df9b72008-12-11 22:51:44 +0000480 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000481}
482
Reid Spencer5f016e22007-07-11 17:01:13 +0000483/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
484///
485/// boolean-literal: [C++ 2.13.5]
486/// 'true'
487/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000488Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000490 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000491}
Chris Lattner50dd2892008-02-26 00:51:44 +0000492
493/// ParseThrowExpression - This handles the C++ throw expression.
494///
495/// throw-expression: [C++ 15]
496/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000497Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000498 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000499 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000500
Chris Lattner2a2819a2008-04-06 06:02:23 +0000501 // If the current token isn't the start of an assignment-expression,
502 // then the expression is not present. This handles things like:
503 // "C ? throw : (void)42", which is crazy but legal.
504 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
505 case tok::semi:
506 case tok::r_paren:
507 case tok::r_square:
508 case tok::r_brace:
509 case tok::colon:
510 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000511 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000512
Chris Lattner2a2819a2008-04-06 06:02:23 +0000513 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000514 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000515 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000516 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000517 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000518}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000519
520/// ParseCXXThis - This handles the C++ 'this' pointer.
521///
522/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
523/// a non-lvalue expression whose value is the address of the object for which
524/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000525Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000526 assert(Tok.is(tok::kw_this) && "Not 'this'!");
527 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000528 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000529}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000530
531/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
532/// Can be interpreted either as function-style casting ("int(x)")
533/// or class type construction ("ClassType(x,y,z)")
534/// or creation of a value-initialized type ("int()").
535///
536/// postfix-expression: [C++ 5.2p1]
537/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
538/// typename-specifier '(' expression-list[opt] ')' [TODO]
539///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000540Parser::OwningExprResult
541Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000542 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000543 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000544
545 assert(Tok.is(tok::l_paren) && "Expected '('!");
546 SourceLocation LParenLoc = ConsumeParen();
547
Sebastian Redla55e52c2008-11-25 22:21:31 +0000548 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000549 CommaLocsTy CommaLocs;
550
551 if (Tok.isNot(tok::r_paren)) {
552 if (ParseExpressionList(Exprs, CommaLocs)) {
553 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000554 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000555 }
556 }
557
558 // Match the ')'.
559 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
560
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000561 // TypeRep could be null, if it references an invalid typedef.
562 if (!TypeRep)
563 return ExprError();
564
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000565 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
566 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000567 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
568 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000569 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000570}
571
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000572/// ParseCXXCondition - if/switch/while/for condition expression.
573///
574/// condition:
575/// expression
576/// type-specifier-seq declarator '=' assignment-expression
577/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
578/// '=' assignment-expression
579///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000580Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000581 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000582 return ParseExpression(); // expression
583
584 SourceLocation StartLoc = Tok.getLocation();
585
586 // type-specifier-seq
587 DeclSpec DS;
588 ParseSpecifierQualifierList(DS);
589
590 // declarator
591 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
592 ParseDeclarator(DeclaratorInfo);
593
594 // simple-asm-expr[opt]
595 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000596 SourceLocation Loc;
597 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000598 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000599 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000600 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000601 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000602 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000603 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000604 }
605
606 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000607 if (Tok.is(tok::kw___attribute)) {
608 SourceLocation Loc;
609 AttributeList *AttrList = ParseAttributes(&Loc);
610 DeclaratorInfo.AddAttributes(AttrList, Loc);
611 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000612
613 // '=' assignment-expression
614 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000615 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000616 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000617 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000618 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000619 return ExprError();
620
Sebastian Redlf53597f2009-03-15 17:47:39 +0000621 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
622 DeclaratorInfo,EqualLoc,
623 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000624}
625
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000626/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
627/// This should only be called when the current token is known to be part of
628/// simple-type-specifier.
629///
630/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000631/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000632/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
633/// char
634/// wchar_t
635/// bool
636/// short
637/// int
638/// long
639/// signed
640/// unsigned
641/// float
642/// double
643/// void
644/// [GNU] typeof-specifier
645/// [C++0x] auto [TODO]
646///
647/// type-name:
648/// class-name
649/// enum-name
650/// typedef-name
651///
652void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
653 DS.SetRangeStart(Tok.getLocation());
654 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000655 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000656 SourceLocation Loc = Tok.getLocation();
657
658 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000659 case tok::identifier: // foo::bar
660 case tok::coloncolon: // ::foo::bar
661 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000662 default:
663 assert(0 && "Not a simple-type-specifier token!");
664 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000665
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000666 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000667 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000668 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000669 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000670 break;
671 }
672
673 // builtin types
674 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000675 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000676 break;
677 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000678 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000679 break;
680 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000681 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000682 break;
683 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000684 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000685 break;
686 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000687 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000688 break;
689 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000690 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000691 break;
692 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000693 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000694 break;
695 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000696 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000697 break;
698 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000699 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000700 break;
701 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000702 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000703 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000704 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000705 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000706 break;
707 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000708 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000709 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000710 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000711 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000712 break;
713
714 // GNU typeof support.
715 case tok::kw_typeof:
716 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000717 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000718 return;
719 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000720 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000721 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
722 else
723 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000724 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000725 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000726}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000727
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000728/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
729/// [dcl.name]), which is a non-empty sequence of type-specifiers,
730/// e.g., "const short int". Note that the DeclSpec is *not* finished
731/// by parsing the type-specifier-seq, because these sequences are
732/// typically followed by some form of declarator. Returns true and
733/// emits diagnostics if this is not a type-specifier-seq, false
734/// otherwise.
735///
736/// type-specifier-seq: [C++ 8.1]
737/// type-specifier type-specifier-seq[opt]
738///
739bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
740 DS.SetRangeStart(Tok.getLocation());
741 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000742 unsigned DiagID;
743 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000744
745 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000746 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000747 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000748 return true;
749 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000750
John McCallfec54012009-08-03 20:12:06 +0000751 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000752
753 return false;
754}
755
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000756/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000757/// operator name (C++ [over.oper]). If successful, returns the
758/// predefined identifier that corresponds to that overloaded
759/// operator. Otherwise, returns NULL and does not consume any tokens.
760///
761/// operator-function-id: [C++ 13.5]
762/// 'operator' operator
763///
764/// operator: one of
765/// new delete new[] delete[]
766/// + - * / % ^ & | ~
767/// ! = < > += -= *= /= %=
768/// ^= &= |= << >> >>= <<= == !=
769/// <= >= && || ++ -- , ->* ->
770/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +0000771OverloadedOperatorKind
772Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000773 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +0000774 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000775
776 OverloadedOperatorKind Op = OO_None;
777 switch (NextToken().getKind()) {
778 case tok::kw_new:
779 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000780 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000781 if (Tok.is(tok::l_square)) {
782 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000783 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000784 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
785 Op = OO_Array_New;
786 } else {
787 Op = OO_New;
788 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000789 if (EndLoc)
790 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000791 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000792
793 case tok::kw_delete:
794 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000795 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000796 if (Tok.is(tok::l_square)) {
797 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000798 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000799 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
800 Op = OO_Array_Delete;
801 } else {
802 Op = OO_Delete;
803 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000804 if (EndLoc)
805 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000806 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000807
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000808#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000809 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000810#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000811#include "clang/Basic/OperatorKinds.def"
812
813 case tok::l_paren:
814 ConsumeToken(); // 'operator'
815 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +0000816 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000817 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000818 if (EndLoc)
819 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000820 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000821
822 case tok::l_square:
823 ConsumeToken(); // 'operator'
824 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000825 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000826 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000827 if (EndLoc)
828 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000829 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000830
831 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000832 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000833 }
834
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000835 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000836 Loc = ConsumeAnyToken(); // the operator itself
837 if (EndLoc)
838 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000839 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000840}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000841
842/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
843/// which expresses the name of a user-defined conversion operator
844/// (C++ [class.conv.fct]p1). Returns the type that this operator is
845/// specifying a conversion for, or NULL if there was an error.
846///
847/// conversion-function-id: [C++ 12.3.2]
848/// operator conversion-type-id
849///
850/// conversion-type-id:
851/// type-specifier-seq conversion-declarator[opt]
852///
853/// conversion-declarator:
854/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +0000855Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000856 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
857 ConsumeToken(); // 'operator'
858
859 // Parse the type-specifier-seq.
860 DeclSpec DS;
861 if (ParseCXXTypeSpecifierSeq(DS))
862 return 0;
863
864 // Parse the conversion-declarator, which is merely a sequence of
865 // ptr-operators.
866 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000867 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000868 if (EndLoc)
869 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000870
871 // Finish up the type.
872 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000873 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000874 return 0;
875 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000876 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000877}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000878
879/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
880/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000881///
882/// This method is called to parse the new expression after the optional :: has
883/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
884/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000885///
886/// new-expression:
887/// '::'[opt] 'new' new-placement[opt] new-type-id
888/// new-initializer[opt]
889/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
890/// new-initializer[opt]
891///
892/// new-placement:
893/// '(' expression-list ')'
894///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000895/// new-type-id:
896/// type-specifier-seq new-declarator[opt]
897///
898/// new-declarator:
899/// ptr-operator new-declarator[opt]
900/// direct-new-declarator
901///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000902/// new-initializer:
903/// '(' expression-list[opt] ')'
904/// [C++0x] braced-init-list [TODO]
905///
Chris Lattner59232d32009-01-04 21:25:24 +0000906Parser::OwningExprResult
907Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
908 assert(Tok.is(tok::kw_new) && "expected 'new' token");
909 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000910
911 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
912 // second form of new-expression. It can't be a new-type-id.
913
Sebastian Redla55e52c2008-11-25 22:21:31 +0000914 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000915 SourceLocation PlacementLParen, PlacementRParen;
916
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000917 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000918 DeclSpec DS;
919 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000920 if (Tok.is(tok::l_paren)) {
921 // If it turns out to be a placement, we change the type location.
922 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000923 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
924 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000925 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000926 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000927
928 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000929 if (PlacementRParen.isInvalid()) {
930 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000931 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000932 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000933
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000934 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000935 // Reset the placement locations. There was no placement.
936 PlacementLParen = PlacementRParen = SourceLocation();
937 ParenTypeId = true;
938 } else {
939 // We still need the type.
940 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000941 SourceLocation LParen = ConsumeParen();
942 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000943 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000944 ParseDeclarator(DeclaratorInfo);
945 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000946 ParenTypeId = true;
947 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000948 if (ParseCXXTypeSpecifierSeq(DS))
949 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000950 else {
951 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000952 ParseDeclaratorInternal(DeclaratorInfo,
953 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000954 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000955 ParenTypeId = false;
956 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000957 }
958 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000959 // A new-type-id is a simplified type-id, where essentially the
960 // direct-declarator is replaced by a direct-new-declarator.
961 if (ParseCXXTypeSpecifierSeq(DS))
962 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000963 else {
964 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000965 ParseDeclaratorInternal(DeclaratorInfo,
966 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000967 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000968 ParenTypeId = false;
969 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000970 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000971 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000972 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000973 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000974
Sebastian Redla55e52c2008-11-25 22:21:31 +0000975 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000976 SourceLocation ConstructorLParen, ConstructorRParen;
977
978 if (Tok.is(tok::l_paren)) {
979 ConstructorLParen = ConsumeParen();
980 if (Tok.isNot(tok::r_paren)) {
981 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000982 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
983 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000984 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000985 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000986 }
987 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000988 if (ConstructorRParen.isInvalid()) {
989 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000990 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000991 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000992 }
993
Sebastian Redlf53597f2009-03-15 17:47:39 +0000994 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
995 move_arg(PlacementArgs), PlacementRParen,
996 ParenTypeId, DeclaratorInfo, ConstructorLParen,
997 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000998}
999
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001000/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1001/// passed to ParseDeclaratorInternal.
1002///
1003/// direct-new-declarator:
1004/// '[' expression ']'
1005/// direct-new-declarator '[' constant-expression ']'
1006///
Chris Lattner59232d32009-01-04 21:25:24 +00001007void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001008 // Parse the array dimensions.
1009 bool first = true;
1010 while (Tok.is(tok::l_square)) {
1011 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001012 OwningExprResult Size(first ? ParseExpression()
1013 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001014 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001015 // Recover
1016 SkipUntil(tok::r_square);
1017 return;
1018 }
1019 first = false;
1020
Sebastian Redlab197ba2009-02-09 18:23:29 +00001021 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001022 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001023 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001024 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001025
Sebastian Redlab197ba2009-02-09 18:23:29 +00001026 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001027 return;
1028 }
1029}
1030
1031/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1032/// This ambiguity appears in the syntax of the C++ new operator.
1033///
1034/// new-expression:
1035/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1036/// new-initializer[opt]
1037///
1038/// new-placement:
1039/// '(' expression-list ')'
1040///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001041bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001042 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001043 // The '(' was already consumed.
1044 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001045 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001046 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001047 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001048 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001049 }
1050
1051 // It's not a type, it has to be an expression list.
1052 // Discard the comma locations - ActOnCXXNew has enough parameters.
1053 CommaLocsTy CommaLocs;
1054 return ParseExpressionList(PlacementArgs, CommaLocs);
1055}
1056
1057/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1058/// to free memory allocated by new.
1059///
Chris Lattner59232d32009-01-04 21:25:24 +00001060/// This method is called to parse the 'delete' expression after the optional
1061/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1062/// and "Start" is its location. Otherwise, "Start" is the location of the
1063/// 'delete' token.
1064///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001065/// delete-expression:
1066/// '::'[opt] 'delete' cast-expression
1067/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001068Parser::OwningExprResult
1069Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1070 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1071 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001072
1073 // Array delete?
1074 bool ArrayDelete = false;
1075 if (Tok.is(tok::l_square)) {
1076 ArrayDelete = true;
1077 SourceLocation LHS = ConsumeBracket();
1078 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1079 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001080 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001081 }
1082
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001083 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001084 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001085 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001086
Sebastian Redlf53597f2009-03-15 17:47:39 +00001087 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001088}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001089
1090static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
1091{
1092 switch(kind) {
1093 default: assert(false && "Not a known unary type trait.");
1094 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1095 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1096 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1097 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1098 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1099 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1100 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1101 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1102 case tok::kw___is_abstract: return UTT_IsAbstract;
1103 case tok::kw___is_class: return UTT_IsClass;
1104 case tok::kw___is_empty: return UTT_IsEmpty;
1105 case tok::kw___is_enum: return UTT_IsEnum;
1106 case tok::kw___is_pod: return UTT_IsPOD;
1107 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1108 case tok::kw___is_union: return UTT_IsUnion;
1109 }
1110}
1111
1112/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1113/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1114/// templates.
1115///
1116/// primary-expression:
1117/// [GNU] unary-type-trait '(' type-id ')'
1118///
1119Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
1120{
1121 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1122 SourceLocation Loc = ConsumeToken();
1123
1124 SourceLocation LParen = Tok.getLocation();
1125 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1126 return ExprError();
1127
1128 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1129 // there will be cryptic errors about mismatched parentheses and missing
1130 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001131 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001132
1133 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1134
Douglas Gregor809070a2009-02-18 17:45:20 +00001135 if (Ty.isInvalid())
1136 return ExprError();
1137
1138 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001139}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001140
1141/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1142/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1143/// based on the context past the parens.
1144Parser::OwningExprResult
1145Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1146 TypeTy *&CastTy,
1147 SourceLocation LParenLoc,
1148 SourceLocation &RParenLoc) {
1149 assert(getLang().CPlusPlus && "Should only be called for C++!");
1150 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1151 assert(isTypeIdInParens() && "Not a type-id!");
1152
1153 OwningExprResult Result(Actions, true);
1154 CastTy = 0;
1155
1156 // We need to disambiguate a very ugly part of the C++ syntax:
1157 //
1158 // (T())x; - type-id
1159 // (T())*x; - type-id
1160 // (T())/x; - expression
1161 // (T()); - expression
1162 //
1163 // The bad news is that we cannot use the specialized tentative parser, since
1164 // it can only verify that the thing inside the parens can be parsed as
1165 // type-id, it is not useful for determining the context past the parens.
1166 //
1167 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001168 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001169 //
1170 // It uses a scheme similar to parsing inline methods. The parenthesized
1171 // tokens are cached, the context that follows is determined (possibly by
1172 // parsing a cast-expression), and then we re-introduce the cached tokens
1173 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001174
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001175 ParenParseOption ParseAs;
1176 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001177
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001178 // Store the tokens of the parentheses. We will parse them after we determine
1179 // the context that follows them.
1180 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1181 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001182 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1183 return ExprError();
1184 }
1185
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001186 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001187 ParseAs = CompoundLiteral;
1188 } else {
1189 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001190 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1191 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1192 NotCastExpr = true;
1193 } else {
1194 // Try parsing the cast-expression that may follow.
1195 // If it is not a cast-expression, NotCastExpr will be true and no token
1196 // will be consumed.
1197 Result = ParseCastExpression(false/*isUnaryExpression*/,
1198 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001199 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001200 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001201
1202 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1203 // an expression.
1204 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001205 }
1206
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001207 // The current token should go after the cached tokens.
1208 Toks.push_back(Tok);
1209 // Re-enter the stored parenthesized tokens into the token stream, so we may
1210 // parse them now.
1211 PP.EnterTokenStream(Toks.data(), Toks.size(),
1212 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1213 // Drop the current token and bring the first cached one. It's the same token
1214 // as when we entered this function.
1215 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001216
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001217 if (ParseAs >= CompoundLiteral) {
1218 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001219
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001220 // Match the ')'.
1221 if (Tok.is(tok::r_paren))
1222 RParenLoc = ConsumeParen();
1223 else
1224 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001225
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001226 if (ParseAs == CompoundLiteral) {
1227 ExprType = CompoundLiteral;
1228 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1229 }
1230
1231 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1232 assert(ParseAs == CastExpr);
1233
1234 if (Ty.isInvalid())
1235 return ExprError();
1236
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001237 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001238
1239 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001240 if (!Result.isInvalid())
Nate Begeman2ef13e52009-08-10 23:49:36 +00001241 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
1242 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001243 return move(Result);
1244 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001245
1246 // Not a compound literal, and not followed by a cast-expression.
1247 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001248
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001249 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001250 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001251 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1252 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1253
1254 // Match the ')'.
1255 if (Result.isInvalid()) {
1256 SkipUntil(tok::r_paren);
1257 return ExprError();
1258 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001259
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001260 if (Tok.is(tok::r_paren))
1261 RParenLoc = ConsumeParen();
1262 else
1263 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1264
1265 return move(Result);
1266}