blob: d89f1e172ffbe5176ad91fe8a36f59ec10dd1c17 [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
Chris Lattner7a0ab5f2009-01-06 06:59:53 +000019/// ParseOptionalCXXScopeSpecifier - Parse global scope or
20/// nested-name-specifier if present. Returns true if a nested-name-specifier
21/// was parsed from the token stream. Note that this routine will not parse
22/// ::new or ::delete, it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000023///
24/// '::'[opt] nested-name-specifier
25/// '::'
26///
27/// nested-name-specifier:
28/// type-name '::'
29/// namespace-name '::'
30/// nested-name-specifier identifier '::'
31/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
32///
Chris Lattner7a0ab5f2009-01-06 06:59:53 +000033bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000034 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000035 "Call sites of this function should be guarded by checking for C++");
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000036
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000037 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000038 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000039 SS.setRange(Tok.getAnnotationRange());
40 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000041 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000042 }
Chris Lattnere607e802009-01-04 21:14:15 +000043
Douglas Gregor39a8de12009-02-25 19:37:18 +000044 bool HasScopeSpecifier = false;
45
Chris Lattner5b454732009-01-05 03:55:46 +000046 if (Tok.is(tok::coloncolon)) {
47 // ::new and ::delete aren't nested-name-specifiers.
48 tok::TokenKind NextKind = NextToken().getKind();
49 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
50 return false;
Chris Lattner55a7cef2009-01-05 00:13:00 +000051
Chris Lattner55a7cef2009-01-05 00:13:00 +000052 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000053 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000054 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000055 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000056 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000057 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000058 }
59
Douglas Gregor39a8de12009-02-25 19:37:18 +000060 while (true) {
61 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +000062 // nested-name-specifier 'template'[opt] simple-template-id '::'
63
64 // Parse the optional 'template' keyword, then make sure we have
65 // 'identifier <' after it.
66 if (Tok.is(tok::kw_template)) {
67 SourceLocation TemplateKWLoc = ConsumeToken();
68
69 if (Tok.isNot(tok::identifier)) {
70 Diag(Tok.getLocation(),
71 diag::err_id_after_template_in_nested_name_spec)
72 << SourceRange(TemplateKWLoc);
73 break;
74 }
75
76 if (NextToken().isNot(tok::less)) {
77 Diag(NextToken().getLocation(),
78 diag::err_less_after_template_name_in_nested_name_spec)
79 << Tok.getIdentifierInfo()->getName()
80 << SourceRange(TemplateKWLoc, Tok.getLocation());
81 break;
82 }
83
84 TemplateTy Template
85 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
86 *Tok.getIdentifierInfo(),
87 Tok.getLocation(), SS);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +000088 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
89 &SS, TemplateKWLoc, false))
90 break;
91
Chris Lattner77cf72a2009-06-26 03:47:46 +000092 continue;
93 }
94
Douglas Gregor39a8de12009-02-25 19:37:18 +000095 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
96 // We have
97 //
98 // simple-template-id '::'
99 //
100 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000101 // right kind (it should name a type or be dependent), and then
102 // convert it into a type within the nested-name-specifier.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000103 TemplateIdAnnotation *TemplateId
104 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
105
Douglas Gregorc45c2322009-03-31 00:43:58 +0000106 if (TemplateId->Kind == TNK_Type_template ||
107 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000108 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000109 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000110
111 assert(Tok.is(tok::annot_typename) &&
112 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000113 Token TypeToken = Tok;
114 ConsumeToken();
115 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
116 SourceLocation CCLoc = ConsumeToken();
117
118 if (!HasScopeSpecifier) {
119 SS.setBeginLoc(TypeToken.getLocation());
120 HasScopeSpecifier = true;
121 }
Douglas Gregor31a19b62009-04-01 21:51:26 +0000122
123 if (TypeToken.getAnnotationValue())
124 SS.setScopeRep(
125 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
126 TypeToken.getAnnotationValue(),
127 TypeToken.getAnnotationRange(),
128 CCLoc));
129 else
130 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000131 SS.setEndLoc(CCLoc);
132 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000133 }
134
135 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000136 }
137
Chris Lattner5c7f7862009-06-26 03:52:38 +0000138
139 // The rest of the nested-name-specifier possibilities start with
140 // tok::identifier.
141 if (Tok.isNot(tok::identifier))
142 break;
143
144 IdentifierInfo &II = *Tok.getIdentifierInfo();
145
146 // nested-name-specifier:
147 // type-name '::'
148 // namespace-name '::'
149 // nested-name-specifier identifier '::'
150 Token Next = NextToken();
151 if (Next.is(tok::coloncolon)) {
152 // We have an identifier followed by a '::'. Lookup this name
153 // as the name in a nested-name-specifier.
154 SourceLocation IdLoc = ConsumeToken();
155 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
156 SourceLocation CCLoc = ConsumeToken();
157
158 if (!HasScopeSpecifier) {
159 SS.setBeginLoc(IdLoc);
160 HasScopeSpecifier = true;
161 }
162
163 if (SS.isInvalid())
164 continue;
165
166 SS.setScopeRep(
167 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II));
168 SS.setEndLoc(CCLoc);
169 continue;
170 }
171
172 // nested-name-specifier:
173 // type-name '<'
174 if (Next.is(tok::less)) {
175 TemplateTy Template;
176 if (TemplateNameKind TNK = Actions.isTemplateName(II, CurScope,
177 Template, &SS)) {
178 // We have found a template name, so annotate this this token
179 // with a template-id annotation. We do not permit the
180 // template-id to be translated into a type annotation,
181 // because some clients (e.g., the parsing of class template
182 // specializations) still want to see the original template-id
183 // token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000184 if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(),
185 false))
186 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000187 continue;
188 }
189 }
190
Douglas Gregor39a8de12009-02-25 19:37:18 +0000191 // We don't have any tokens that form the beginning of a
192 // nested-name-specifier, so we're done.
193 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000194 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000195
196 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000197}
198
199/// ParseCXXIdExpression - Handle id-expression.
200///
201/// id-expression:
202/// unqualified-id
203/// qualified-id
204///
205/// unqualified-id:
206/// identifier
207/// operator-function-id
208/// conversion-function-id [TODO]
209/// '~' class-name [TODO]
210/// template-id [TODO]
211///
212/// qualified-id:
213/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
214/// '::' identifier
215/// '::' operator-function-id
216/// '::' template-id [TODO]
217///
218/// nested-name-specifier:
219/// type-name '::'
220/// namespace-name '::'
221/// nested-name-specifier identifier '::'
222/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
223///
224/// NOTE: The standard specifies that, for qualified-id, the parser does not
225/// expect:
226///
227/// '::' conversion-function-id
228/// '::' '~' class-name
229///
230/// This may cause a slight inconsistency on diagnostics:
231///
232/// class C {};
233/// namespace A {}
234/// void f() {
235/// :: A :: ~ C(); // Some Sema error about using destructor with a
236/// // namespace.
237/// :: ~ C(); // Some Parser error like 'unexpected ~'.
238/// }
239///
240/// We simplify the parser a bit and make it work like:
241///
242/// qualified-id:
243/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
244/// '::' unqualified-id
245///
246/// That way Sema can handle and report similar errors for namespaces and the
247/// global scope.
248///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000249/// The isAddressOfOperand parameter indicates that this id-expression is a
250/// direct operand of the address-of operator. This is, besides member contexts,
251/// the only place where a qualified-id naming a non-static class member may
252/// appear.
253///
254Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000255 // qualified-id:
256 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
257 // '::' unqualified-id
258 //
259 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000260 ParseOptionalCXXScopeSpecifier(SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000261
262 // unqualified-id:
263 // identifier
264 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000265 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000266 // '~' class-name [TODO]
267 // template-id [TODO]
268 //
269 switch (Tok.getKind()) {
270 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000271 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000272
273 case tok::identifier: {
274 // Consume the identifier so that we can see if it is followed by a '('.
275 IdentifierInfo &II = *Tok.getIdentifierInfo();
276 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000277 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
278 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000279 }
280
281 case tok::kw_operator: {
282 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000283 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000284 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000285 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
286 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000287 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000288 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000289 Tok.is(tok::l_paren), SS,
290 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000291
Douglas Gregor2def4832008-11-17 20:34:05 +0000292 // We already complained about a bad conversion-function-id,
293 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000294 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000295 }
296
297 } // switch.
298
299 assert(0 && "The switch was supposed to take care everything.");
300}
301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302/// ParseCXXCasts - This handles the various ways to cast expressions to another
303/// type.
304///
305/// postfix-expression: [C++ 5.2p1]
306/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
307/// 'static_cast' '<' type-name '>' '(' expression ')'
308/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
309/// 'const_cast' '<' type-name '>' '(' expression ')'
310///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000311Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 tok::TokenKind Kind = Tok.getKind();
313 const char *CastName = 0; // For error messages
314
315 switch (Kind) {
316 default: assert(0 && "Unknown C++ cast!"); abort();
317 case tok::kw_const_cast: CastName = "const_cast"; break;
318 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
319 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
320 case tok::kw_static_cast: CastName = "static_cast"; break;
321 }
322
323 SourceLocation OpLoc = ConsumeToken();
324 SourceLocation LAngleBracketLoc = Tok.getLocation();
325
326 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000327 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000328
Douglas Gregor809070a2009-02-18 17:45:20 +0000329 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 SourceLocation RAngleBracketLoc = Tok.getLocation();
331
Chris Lattner1ab3b962008-11-18 07:48:38 +0000332 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000333 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000334
335 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
336
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000337 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
338 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000339
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000340 OwningExprResult Result = ParseExpression();
341
342 // Match the ')'.
343 if (Result.isInvalid())
344 SkipUntil(tok::r_paren);
345
346 if (Tok.is(tok::r_paren))
347 RParenLoc = ConsumeParen();
348 else
349 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000350
Douglas Gregor809070a2009-02-18 17:45:20 +0000351 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000352 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000353 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000354 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000355 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000356
Sebastian Redl20df9b72008-12-11 22:51:44 +0000357 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000358}
359
Sebastian Redlc42e1182008-11-11 11:37:55 +0000360/// ParseCXXTypeid - This handles the C++ typeid expression.
361///
362/// postfix-expression: [C++ 5.2p1]
363/// 'typeid' '(' expression ')'
364/// 'typeid' '(' type-id ')'
365///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000366Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000367 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
368
369 SourceLocation OpLoc = ConsumeToken();
370 SourceLocation LParenLoc = Tok.getLocation();
371 SourceLocation RParenLoc;
372
373 // typeid expressions are always parenthesized.
374 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
375 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000376 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000377
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000378 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000379
380 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000381 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000382
383 // Match the ')'.
384 MatchRHSPunctuation(tok::r_paren, LParenLoc);
385
Douglas Gregor809070a2009-02-18 17:45:20 +0000386 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000387 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000388
389 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000390 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000391 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000392 // C++0x [expr.typeid]p3:
393 // When typeid is applied to an expression other than an lvalue of a
394 // polymorphic class type [...] The expression is an unevaluated
395 // operand (Clause 5).
396 //
397 // Note that we can't tell whether the expression is an lvalue of a
398 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000399 // we the expression is potentially potentially evaluated.
400 EnterExpressionEvaluationContext Unevaluated(Actions,
401 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000402 Result = ParseExpression();
403
404 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000405 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000406 SkipUntil(tok::r_paren);
407 else {
408 MatchRHSPunctuation(tok::r_paren, LParenLoc);
409
410 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000411 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000412 }
413 }
414
Sebastian Redl20df9b72008-12-11 22:51:44 +0000415 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000416}
417
Reid Spencer5f016e22007-07-11 17:01:13 +0000418/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
419///
420/// boolean-literal: [C++ 2.13.5]
421/// 'true'
422/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000423Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000425 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000426}
Chris Lattner50dd2892008-02-26 00:51:44 +0000427
428/// ParseThrowExpression - This handles the C++ throw expression.
429///
430/// throw-expression: [C++ 15]
431/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000432Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000433 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000434 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000435
Chris Lattner2a2819a2008-04-06 06:02:23 +0000436 // If the current token isn't the start of an assignment-expression,
437 // then the expression is not present. This handles things like:
438 // "C ? throw : (void)42", which is crazy but legal.
439 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
440 case tok::semi:
441 case tok::r_paren:
442 case tok::r_square:
443 case tok::r_brace:
444 case tok::colon:
445 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000446 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000447
Chris Lattner2a2819a2008-04-06 06:02:23 +0000448 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000449 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000450 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000451 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000452 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000453}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000454
455/// ParseCXXThis - This handles the C++ 'this' pointer.
456///
457/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
458/// a non-lvalue expression whose value is the address of the object for which
459/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000460Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000461 assert(Tok.is(tok::kw_this) && "Not 'this'!");
462 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000463 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000464}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000465
466/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
467/// Can be interpreted either as function-style casting ("int(x)")
468/// or class type construction ("ClassType(x,y,z)")
469/// or creation of a value-initialized type ("int()").
470///
471/// postfix-expression: [C++ 5.2p1]
472/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
473/// typename-specifier '(' expression-list[opt] ')' [TODO]
474///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000475Parser::OwningExprResult
476Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000477 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000478 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000479
480 assert(Tok.is(tok::l_paren) && "Expected '('!");
481 SourceLocation LParenLoc = ConsumeParen();
482
Sebastian Redla55e52c2008-11-25 22:21:31 +0000483 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000484 CommaLocsTy CommaLocs;
485
486 if (Tok.isNot(tok::r_paren)) {
487 if (ParseExpressionList(Exprs, CommaLocs)) {
488 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000489 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000490 }
491 }
492
493 // Match the ')'.
494 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
495
496 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
497 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000498 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
499 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000500 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000501}
502
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000503/// ParseCXXCondition - if/switch/while/for condition expression.
504///
505/// condition:
506/// expression
507/// type-specifier-seq declarator '=' assignment-expression
508/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
509/// '=' assignment-expression
510///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000511Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000512 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000513 return ParseExpression(); // expression
514
515 SourceLocation StartLoc = Tok.getLocation();
516
517 // type-specifier-seq
518 DeclSpec DS;
519 ParseSpecifierQualifierList(DS);
520
521 // declarator
522 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
523 ParseDeclarator(DeclaratorInfo);
524
525 // simple-asm-expr[opt]
526 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000527 SourceLocation Loc;
528 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000529 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000530 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000531 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000532 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000533 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000534 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000535 }
536
537 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000538 if (Tok.is(tok::kw___attribute)) {
539 SourceLocation Loc;
540 AttributeList *AttrList = ParseAttributes(&Loc);
541 DeclaratorInfo.AddAttributes(AttrList, Loc);
542 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000543
544 // '=' assignment-expression
545 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000546 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000547 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000548 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000549 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000550 return ExprError();
551
Sebastian Redlf53597f2009-03-15 17:47:39 +0000552 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
553 DeclaratorInfo,EqualLoc,
554 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000555}
556
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000557/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
558/// This should only be called when the current token is known to be part of
559/// simple-type-specifier.
560///
561/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000562/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000563/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
564/// char
565/// wchar_t
566/// bool
567/// short
568/// int
569/// long
570/// signed
571/// unsigned
572/// float
573/// double
574/// void
575/// [GNU] typeof-specifier
576/// [C++0x] auto [TODO]
577///
578/// type-name:
579/// class-name
580/// enum-name
581/// typedef-name
582///
583void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
584 DS.SetRangeStart(Tok.getLocation());
585 const char *PrevSpec;
586 SourceLocation Loc = Tok.getLocation();
587
588 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000589 case tok::identifier: // foo::bar
590 case tok::coloncolon: // ::foo::bar
591 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000592 default:
593 assert(0 && "Not a simple-type-specifier token!");
594 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000595
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000596 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000597 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000598 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000599 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000600 break;
601 }
602
603 // builtin types
604 case tok::kw_short:
605 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
606 break;
607 case tok::kw_long:
608 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
609 break;
610 case tok::kw_signed:
611 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
612 break;
613 case tok::kw_unsigned:
614 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
615 break;
616 case tok::kw_void:
617 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
618 break;
619 case tok::kw_char:
620 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
621 break;
622 case tok::kw_int:
623 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
624 break;
625 case tok::kw_float:
626 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
627 break;
628 case tok::kw_double:
629 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
630 break;
631 case tok::kw_wchar_t:
632 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
633 break;
634 case tok::kw_bool:
635 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
636 break;
637
638 // GNU typeof support.
639 case tok::kw_typeof:
640 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000641 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000642 return;
643 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000644 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000645 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
646 else
647 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000648 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000649 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000650}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000651
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000652/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
653/// [dcl.name]), which is a non-empty sequence of type-specifiers,
654/// e.g., "const short int". Note that the DeclSpec is *not* finished
655/// by parsing the type-specifier-seq, because these sequences are
656/// typically followed by some form of declarator. Returns true and
657/// emits diagnostics if this is not a type-specifier-seq, false
658/// otherwise.
659///
660/// type-specifier-seq: [C++ 8.1]
661/// type-specifier type-specifier-seq[opt]
662///
663bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
664 DS.SetRangeStart(Tok.getLocation());
665 const char *PrevSpec = 0;
666 int isInvalid = 0;
667
668 // Parse one or more of the type specifiers.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000669 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000670 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000671 return true;
672 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000673
Ted Kremenekb8006e52009-01-06 19:17:58 +0000674 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000675
676 return false;
677}
678
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000679/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000680/// operator name (C++ [over.oper]). If successful, returns the
681/// predefined identifier that corresponds to that overloaded
682/// operator. Otherwise, returns NULL and does not consume any tokens.
683///
684/// operator-function-id: [C++ 13.5]
685/// 'operator' operator
686///
687/// operator: one of
688/// new delete new[] delete[]
689/// + - * / % ^ & | ~
690/// ! = < > += -= *= /= %=
691/// ^= &= |= << >> >>= <<= == !=
692/// <= >= && || ++ -- , ->* ->
693/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +0000694OverloadedOperatorKind
695Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000696 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +0000697 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000698
699 OverloadedOperatorKind Op = OO_None;
700 switch (NextToken().getKind()) {
701 case tok::kw_new:
702 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000703 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000704 if (Tok.is(tok::l_square)) {
705 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000706 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000707 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
708 Op = OO_Array_New;
709 } else {
710 Op = OO_New;
711 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000712 if (EndLoc)
713 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000714 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000715
716 case tok::kw_delete:
717 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000718 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000719 if (Tok.is(tok::l_square)) {
720 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000721 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000722 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
723 Op = OO_Array_Delete;
724 } else {
725 Op = OO_Delete;
726 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000727 if (EndLoc)
728 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000729 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000730
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000731#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000732 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000733#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000734#include "clang/Basic/OperatorKinds.def"
735
736 case tok::l_paren:
737 ConsumeToken(); // 'operator'
738 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +0000739 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000740 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000741 if (EndLoc)
742 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000743 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000744
745 case tok::l_square:
746 ConsumeToken(); // 'operator'
747 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000748 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000749 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000750 if (EndLoc)
751 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000752 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000753
754 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000755 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000756 }
757
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000758 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000759 Loc = ConsumeAnyToken(); // the operator itself
760 if (EndLoc)
761 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000762 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000763}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000764
765/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
766/// which expresses the name of a user-defined conversion operator
767/// (C++ [class.conv.fct]p1). Returns the type that this operator is
768/// specifying a conversion for, or NULL if there was an error.
769///
770/// conversion-function-id: [C++ 12.3.2]
771/// operator conversion-type-id
772///
773/// conversion-type-id:
774/// type-specifier-seq conversion-declarator[opt]
775///
776/// conversion-declarator:
777/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +0000778Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000779 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
780 ConsumeToken(); // 'operator'
781
782 // Parse the type-specifier-seq.
783 DeclSpec DS;
784 if (ParseCXXTypeSpecifierSeq(DS))
785 return 0;
786
787 // Parse the conversion-declarator, which is merely a sequence of
788 // ptr-operators.
789 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000790 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000791 if (EndLoc)
792 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000793
794 // Finish up the type.
795 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000796 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000797 return 0;
798 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000799 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000800}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000801
802/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
803/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000804///
805/// This method is called to parse the new expression after the optional :: has
806/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
807/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000808///
809/// new-expression:
810/// '::'[opt] 'new' new-placement[opt] new-type-id
811/// new-initializer[opt]
812/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
813/// new-initializer[opt]
814///
815/// new-placement:
816/// '(' expression-list ')'
817///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000818/// new-type-id:
819/// type-specifier-seq new-declarator[opt]
820///
821/// new-declarator:
822/// ptr-operator new-declarator[opt]
823/// direct-new-declarator
824///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000825/// new-initializer:
826/// '(' expression-list[opt] ')'
827/// [C++0x] braced-init-list [TODO]
828///
Chris Lattner59232d32009-01-04 21:25:24 +0000829Parser::OwningExprResult
830Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
831 assert(Tok.is(tok::kw_new) && "expected 'new' token");
832 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000833
834 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
835 // second form of new-expression. It can't be a new-type-id.
836
Sebastian Redla55e52c2008-11-25 22:21:31 +0000837 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000838 SourceLocation PlacementLParen, PlacementRParen;
839
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000840 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000841 DeclSpec DS;
842 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000843 if (Tok.is(tok::l_paren)) {
844 // If it turns out to be a placement, we change the type location.
845 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000846 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
847 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000848 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000849 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000850
851 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000852 if (PlacementRParen.isInvalid()) {
853 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000854 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000855 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000856
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000857 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000858 // Reset the placement locations. There was no placement.
859 PlacementLParen = PlacementRParen = SourceLocation();
860 ParenTypeId = true;
861 } else {
862 // We still need the type.
863 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000864 SourceLocation LParen = ConsumeParen();
865 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000866 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000867 ParseDeclarator(DeclaratorInfo);
868 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000869 ParenTypeId = true;
870 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000871 if (ParseCXXTypeSpecifierSeq(DS))
872 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000873 else {
874 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000875 ParseDeclaratorInternal(DeclaratorInfo,
876 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000877 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000878 ParenTypeId = false;
879 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000880 }
881 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000882 // A new-type-id is a simplified type-id, where essentially the
883 // direct-declarator is replaced by a direct-new-declarator.
884 if (ParseCXXTypeSpecifierSeq(DS))
885 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000886 else {
887 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000888 ParseDeclaratorInternal(DeclaratorInfo,
889 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000890 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000891 ParenTypeId = false;
892 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000893 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000894 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000895 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000896 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000897
Sebastian Redla55e52c2008-11-25 22:21:31 +0000898 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000899 SourceLocation ConstructorLParen, ConstructorRParen;
900
901 if (Tok.is(tok::l_paren)) {
902 ConstructorLParen = ConsumeParen();
903 if (Tok.isNot(tok::r_paren)) {
904 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000905 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
906 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000907 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000908 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000909 }
910 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000911 if (ConstructorRParen.isInvalid()) {
912 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000913 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000914 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000915 }
916
Sebastian Redlf53597f2009-03-15 17:47:39 +0000917 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
918 move_arg(PlacementArgs), PlacementRParen,
919 ParenTypeId, DeclaratorInfo, ConstructorLParen,
920 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000921}
922
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000923/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
924/// passed to ParseDeclaratorInternal.
925///
926/// direct-new-declarator:
927/// '[' expression ']'
928/// direct-new-declarator '[' constant-expression ']'
929///
Chris Lattner59232d32009-01-04 21:25:24 +0000930void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000931 // Parse the array dimensions.
932 bool first = true;
933 while (Tok.is(tok::l_square)) {
934 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000935 OwningExprResult Size(first ? ParseExpression()
936 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000937 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000938 // Recover
939 SkipUntil(tok::r_square);
940 return;
941 }
942 first = false;
943
Sebastian Redlab197ba2009-02-09 18:23:29 +0000944 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000945 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Sebastian Redlab197ba2009-02-09 18:23:29 +0000946 Size.release(), LLoc),
947 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000948
Sebastian Redlab197ba2009-02-09 18:23:29 +0000949 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000950 return;
951 }
952}
953
954/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
955/// This ambiguity appears in the syntax of the C++ new operator.
956///
957/// new-expression:
958/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
959/// new-initializer[opt]
960///
961/// new-placement:
962/// '(' expression-list ')'
963///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000964bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +0000965 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000966 // The '(' was already consumed.
967 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000968 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000969 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000970 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000971 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000972 }
973
974 // It's not a type, it has to be an expression list.
975 // Discard the comma locations - ActOnCXXNew has enough parameters.
976 CommaLocsTy CommaLocs;
977 return ParseExpressionList(PlacementArgs, CommaLocs);
978}
979
980/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
981/// to free memory allocated by new.
982///
Chris Lattner59232d32009-01-04 21:25:24 +0000983/// This method is called to parse the 'delete' expression after the optional
984/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
985/// and "Start" is its location. Otherwise, "Start" is the location of the
986/// 'delete' token.
987///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000988/// delete-expression:
989/// '::'[opt] 'delete' cast-expression
990/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000991Parser::OwningExprResult
992Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
993 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
994 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000995
996 // Array delete?
997 bool ArrayDelete = false;
998 if (Tok.is(tok::l_square)) {
999 ArrayDelete = true;
1000 SourceLocation LHS = ConsumeBracket();
1001 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1002 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001003 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001004 }
1005
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001006 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001007 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001008 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001009
Sebastian Redlf53597f2009-03-15 17:47:39 +00001010 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001011}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001012
1013static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
1014{
1015 switch(kind) {
1016 default: assert(false && "Not a known unary type trait.");
1017 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1018 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1019 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1020 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1021 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1022 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1023 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1024 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1025 case tok::kw___is_abstract: return UTT_IsAbstract;
1026 case tok::kw___is_class: return UTT_IsClass;
1027 case tok::kw___is_empty: return UTT_IsEmpty;
1028 case tok::kw___is_enum: return UTT_IsEnum;
1029 case tok::kw___is_pod: return UTT_IsPOD;
1030 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1031 case tok::kw___is_union: return UTT_IsUnion;
1032 }
1033}
1034
1035/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1036/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1037/// templates.
1038///
1039/// primary-expression:
1040/// [GNU] unary-type-trait '(' type-id ')'
1041///
1042Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
1043{
1044 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1045 SourceLocation Loc = ConsumeToken();
1046
1047 SourceLocation LParen = Tok.getLocation();
1048 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1049 return ExprError();
1050
1051 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1052 // there will be cryptic errors about mismatched parentheses and missing
1053 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001054 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001055
1056 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1057
Douglas Gregor809070a2009-02-18 17:45:20 +00001058 if (Ty.isInvalid())
1059 return ExprError();
1060
1061 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001062}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001063
1064/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1065/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1066/// based on the context past the parens.
1067Parser::OwningExprResult
1068Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1069 TypeTy *&CastTy,
1070 SourceLocation LParenLoc,
1071 SourceLocation &RParenLoc) {
1072 assert(getLang().CPlusPlus && "Should only be called for C++!");
1073 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1074 assert(isTypeIdInParens() && "Not a type-id!");
1075
1076 OwningExprResult Result(Actions, true);
1077 CastTy = 0;
1078
1079 // We need to disambiguate a very ugly part of the C++ syntax:
1080 //
1081 // (T())x; - type-id
1082 // (T())*x; - type-id
1083 // (T())/x; - expression
1084 // (T()); - expression
1085 //
1086 // The bad news is that we cannot use the specialized tentative parser, since
1087 // it can only verify that the thing inside the parens can be parsed as
1088 // type-id, it is not useful for determining the context past the parens.
1089 //
1090 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001091 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001092 //
1093 // It uses a scheme similar to parsing inline methods. The parenthesized
1094 // tokens are cached, the context that follows is determined (possibly by
1095 // parsing a cast-expression), and then we re-introduce the cached tokens
1096 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001097
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001098 ParenParseOption ParseAs;
1099 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001100
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001101 // Store the tokens of the parentheses. We will parse them after we determine
1102 // the context that follows them.
1103 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1104 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001105 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1106 return ExprError();
1107 }
1108
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001109 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001110 ParseAs = CompoundLiteral;
1111 } else {
1112 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001113 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1114 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1115 NotCastExpr = true;
1116 } else {
1117 // Try parsing the cast-expression that may follow.
1118 // If it is not a cast-expression, NotCastExpr will be true and no token
1119 // will be consumed.
1120 Result = ParseCastExpression(false/*isUnaryExpression*/,
1121 false/*isAddressofOperand*/,
1122 NotCastExpr);
1123 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001124
1125 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1126 // an expression.
1127 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001128 }
1129
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001130 // The current token should go after the cached tokens.
1131 Toks.push_back(Tok);
1132 // Re-enter the stored parenthesized tokens into the token stream, so we may
1133 // parse them now.
1134 PP.EnterTokenStream(Toks.data(), Toks.size(),
1135 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1136 // Drop the current token and bring the first cached one. It's the same token
1137 // as when we entered this function.
1138 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001139
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001140 if (ParseAs >= CompoundLiteral) {
1141 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001142
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001143 // Match the ')'.
1144 if (Tok.is(tok::r_paren))
1145 RParenLoc = ConsumeParen();
1146 else
1147 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001148
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001149 if (ParseAs == CompoundLiteral) {
1150 ExprType = CompoundLiteral;
1151 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1152 }
1153
1154 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1155 assert(ParseAs == CastExpr);
1156
1157 if (Ty.isInvalid())
1158 return ExprError();
1159
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001160 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001161
1162 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001163 if (!Result.isInvalid())
1164 Result = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc,move(Result));
1165 return move(Result);
1166 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001167
1168 // Not a compound literal, and not followed by a cast-expression.
1169 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001170
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001171 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001172 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001173 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1174 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1175
1176 // Match the ')'.
1177 if (Result.isInvalid()) {
1178 SkipUntil(tok::r_paren);
1179 return ExprError();
1180 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001181
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001182 if (Tok.is(tok::r_paren))
1183 RParenLoc = ConsumeParen();
1184 else
1185 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1186
1187 return move(Result);
1188}