blob: c065b4760a72946817d717028a9fa27fa8e140a5 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner545f39e2009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000015#include "clang/Parse/Parser.h"
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017using namespace clang;
18
Chris Lattnerd706dc82009-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.
Argiris Kirtzidis311db8c2008-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 Lattnerd706dc82009-01-06 06:59:53 +000033bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS) {
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +000034 assert(getLang().CPlusPlus &&
Chris Lattner8376d2e2009-01-05 01:24:05 +000035 "Call sites of this function should be guarded by checking for C++");
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +000036
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +000037 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor041e9292009-03-26 23:56:24 +000038 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +000039 SS.setRange(Tok.getAnnotationRange());
40 ConsumeToken();
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +000041 return true;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +000042 }
Chris Lattner99113932009-01-04 21:14:15 +000043
Douglas Gregor0c281a82009-02-25 19:37:18 +000044 bool HasScopeSpecifier = false;
45
Chris Lattner94a15bd2009-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 Lattner2c301452009-01-05 00:13:00 +000051
Chris Lattner2c301452009-01-05 00:13:00 +000052 // '::' - Global scope qualifier.
Chris Lattner094f9a82009-01-05 02:07:19 +000053 SourceLocation CCLoc = ConsumeToken();
Chris Lattner094f9a82009-01-05 02:07:19 +000054 SS.setBeginLoc(CCLoc);
Douglas Gregor041e9292009-03-26 23:56:24 +000055 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner094f9a82009-01-05 02:07:19 +000056 SS.setEndLoc(CCLoc);
Douglas Gregor0c281a82009-02-25 19:37:18 +000057 HasScopeSpecifier = true;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +000058 }
59
Douglas Gregor0c281a82009-02-25 19:37:18 +000060 while (true) {
61 // nested-name-specifier:
62 // type-name '::'
63 // namespace-name '::'
64 // nested-name-specifier identifier '::'
65 if (Tok.is(tok::identifier) && NextToken().is(tok::coloncolon)) {
66 // We have an identifier followed by a '::'. Lookup this name
67 // as the name in a nested-name-specifier.
68 IdentifierInfo *II = Tok.getIdentifierInfo();
69 SourceLocation IdLoc = ConsumeToken();
70 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
71 SourceLocation CCLoc = ConsumeToken();
72
73 if (!HasScopeSpecifier) {
74 SS.setBeginLoc(IdLoc);
75 HasScopeSpecifier = true;
76 }
77
78 if (SS.isInvalid())
79 continue;
80
Douglas Gregor041e9292009-03-26 23:56:24 +000081 SS.setScopeRep(
Douglas Gregor0c281a82009-02-25 19:37:18 +000082 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, *II));
83 SS.setEndLoc(CCLoc);
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +000084 continue;
Douglas Gregor0c281a82009-02-25 19:37:18 +000085 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +000086
Douglas Gregor0c281a82009-02-25 19:37:18 +000087 // nested-name-specifier:
88 // type-name '::'
89 // nested-name-specifier 'template'[opt] simple-template-id '::'
90 if ((Tok.is(tok::identifier) && NextToken().is(tok::less)) ||
91 Tok.is(tok::kw_template)) {
92 // Parse the optional 'template' keyword, then make sure we have
93 // 'identifier <' after it.
Douglas Gregor0c281a82009-02-25 19:37:18 +000094 if (Tok.is(tok::kw_template)) {
Douglas Gregoraabb8502009-03-31 00:43:58 +000095 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor0c281a82009-02-25 19:37:18 +000096
97 if (Tok.isNot(tok::identifier)) {
98 Diag(Tok.getLocation(),
99 diag::err_id_after_template_in_nested_name_spec)
100 << SourceRange(TemplateKWLoc);
101 break;
102 }
103
104 if (NextToken().isNot(tok::less)) {
105 Diag(NextToken().getLocation(),
106 diag::err_less_after_template_name_in_nested_name_spec)
107 << Tok.getIdentifierInfo()->getName()
108 << SourceRange(TemplateKWLoc, Tok.getLocation());
109 break;
110 }
Douglas Gregoraabb8502009-03-31 00:43:58 +0000111
112 TemplateTy Template
113 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
114 *Tok.getIdentifierInfo(),
115 Tok.getLocation(),
116 SS);
117 AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
118 &SS, TemplateKWLoc, false);
119 continue;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000120 }
121
Douglas Gregordd13e842009-03-30 22:58:21 +0000122 TemplateTy Template;
Douglas Gregoraabb8502009-03-31 00:43:58 +0000123 TemplateNameKind TNK = Actions.isTemplateName(*Tok.getIdentifierInfo(),
124 CurScope, Template, &SS);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000125 if (TNK) {
126 // We have found a template name, so annotate this this token
127 // with a template-id annotation. We do not permit the
128 // template-id to be translated into a type annotation,
129 // because some clients (e.g., the parsing of class template
130 // specializations) still want to see the original template-id
131 // token.
Douglas Gregoraabb8502009-03-31 00:43:58 +0000132 AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(), false);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000133 continue;
134 }
135 }
136
137 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
138 // We have
139 //
140 // simple-template-id '::'
141 //
142 // So we need to check whether the simple-template-id is of the
Douglas Gregoraabb8502009-03-31 00:43:58 +0000143 // right kind (it should name a type or be dependent), and then
144 // convert it into a type within the nested-name-specifier.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000145 TemplateIdAnnotation *TemplateId
146 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
147
Douglas Gregoraabb8502009-03-31 00:43:58 +0000148 if (TemplateId->Kind == TNK_Type_template ||
149 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000150 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor96b6df92009-05-14 00:28:11 +0000151 SS.setScopeRep(0);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000152
153 assert(Tok.is(tok::annot_typename) &&
154 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor0c281a82009-02-25 19:37:18 +0000155 Token TypeToken = Tok;
156 ConsumeToken();
157 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
158 SourceLocation CCLoc = ConsumeToken();
159
160 if (!HasScopeSpecifier) {
161 SS.setBeginLoc(TypeToken.getLocation());
162 HasScopeSpecifier = true;
163 }
Douglas Gregord7cb0372009-04-01 21:51:26 +0000164
165 if (TypeToken.getAnnotationValue())
166 SS.setScopeRep(
167 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
168 TypeToken.getAnnotationValue(),
169 TypeToken.getAnnotationRange(),
170 CCLoc));
171 else
172 SS.setScopeRep(0);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000173 SS.setEndLoc(CCLoc);
174 continue;
175 } else
Douglas Gregoraabb8502009-03-31 00:43:58 +0000176 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor0c281a82009-02-25 19:37:18 +0000177 }
178
179 // We don't have any tokens that form the beginning of a
180 // nested-name-specifier, so we're done.
181 break;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000182 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000183
184 return HasScopeSpecifier;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000185}
186
187/// ParseCXXIdExpression - Handle id-expression.
188///
189/// id-expression:
190/// unqualified-id
191/// qualified-id
192///
193/// unqualified-id:
194/// identifier
195/// operator-function-id
196/// conversion-function-id [TODO]
197/// '~' class-name [TODO]
198/// template-id [TODO]
199///
200/// qualified-id:
201/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
202/// '::' identifier
203/// '::' operator-function-id
204/// '::' template-id [TODO]
205///
206/// nested-name-specifier:
207/// type-name '::'
208/// namespace-name '::'
209/// nested-name-specifier identifier '::'
210/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
211///
212/// NOTE: The standard specifies that, for qualified-id, the parser does not
213/// expect:
214///
215/// '::' conversion-function-id
216/// '::' '~' class-name
217///
218/// This may cause a slight inconsistency on diagnostics:
219///
220/// class C {};
221/// namespace A {}
222/// void f() {
223/// :: A :: ~ C(); // Some Sema error about using destructor with a
224/// // namespace.
225/// :: ~ C(); // Some Parser error like 'unexpected ~'.
226/// }
227///
228/// We simplify the parser a bit and make it work like:
229///
230/// qualified-id:
231/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
232/// '::' unqualified-id
233///
234/// That way Sema can handle and report similar errors for namespaces and the
235/// global scope.
236///
Sebastian Redl0c9da212009-02-03 20:19:35 +0000237/// The isAddressOfOperand parameter indicates that this id-expression is a
238/// direct operand of the address-of operator. This is, besides member contexts,
239/// the only place where a qualified-id naming a non-static class member may
240/// appear.
241///
242Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000243 // qualified-id:
244 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
245 // '::' unqualified-id
246 //
247 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +0000248 ParseOptionalCXXScopeSpecifier(SS);
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000249
250 // unqualified-id:
251 // identifier
252 // operator-function-id
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000253 // conversion-function-id
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000254 // '~' class-name [TODO]
255 // template-id [TODO]
256 //
257 switch (Tok.getKind()) {
258 default:
Sebastian Redl39d4f022008-12-11 22:51:44 +0000259 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000260
261 case tok::identifier: {
262 // Consume the identifier so that we can see if it is followed by a '('.
263 IdentifierInfo &II = *Tok.getIdentifierInfo();
264 SourceLocation L = ConsumeToken();
Sebastian Redl0c9da212009-02-03 20:19:35 +0000265 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
266 &SS, isAddressOfOperand);
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000267 }
268
269 case tok::kw_operator: {
270 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner8376d2e2009-01-05 01:24:05 +0000271 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd883f72009-01-18 18:53:16 +0000272 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redl0c9da212009-02-03 20:19:35 +0000273 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
274 isAddressOfOperand);
Chris Lattner8376d2e2009-01-05 01:24:05 +0000275 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd883f72009-01-18 18:53:16 +0000276 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redl0c9da212009-02-03 20:19:35 +0000277 Tok.is(tok::l_paren), SS,
278 isAddressOfOperand);
Sebastian Redl39d4f022008-12-11 22:51:44 +0000279
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000280 // We already complained about a bad conversion-function-id,
281 // above.
Sebastian Redl39d4f022008-12-11 22:51:44 +0000282 return ExprError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000283 }
284
285 } // switch.
286
287 assert(0 && "The switch was supposed to take care everything.");
288}
289
Chris Lattner4b009652007-07-25 00:24:17 +0000290/// ParseCXXCasts - This handles the various ways to cast expressions to another
291/// type.
292///
293/// postfix-expression: [C++ 5.2p1]
294/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
295/// 'static_cast' '<' type-name '>' '(' expression ')'
296/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
297/// 'const_cast' '<' type-name '>' '(' expression ')'
298///
Sebastian Redl39d4f022008-12-11 22:51:44 +0000299Parser::OwningExprResult Parser::ParseCXXCasts() {
Chris Lattner4b009652007-07-25 00:24:17 +0000300 tok::TokenKind Kind = Tok.getKind();
301 const char *CastName = 0; // For error messages
302
303 switch (Kind) {
304 default: assert(0 && "Unknown C++ cast!"); abort();
305 case tok::kw_const_cast: CastName = "const_cast"; break;
306 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
307 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
308 case tok::kw_static_cast: CastName = "static_cast"; break;
309 }
310
311 SourceLocation OpLoc = ConsumeToken();
312 SourceLocation LAngleBracketLoc = Tok.getLocation();
313
314 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000315 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000316
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000317 TypeResult CastTy = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +0000318 SourceLocation RAngleBracketLoc = Tok.getLocation();
319
Chris Lattnerf006a222008-11-18 07:48:38 +0000320 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000321 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner4b009652007-07-25 00:24:17 +0000322
323 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
324
Chris Lattnerf006a222008-11-18 07:48:38 +0000325 if (Tok.isNot(tok::l_paren))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000326 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << CastName);
Chris Lattner4b009652007-07-25 00:24:17 +0000327
Sebastian Redla6817a02008-12-11 22:33:27 +0000328 OwningExprResult Result(ParseSimpleParenExpression(RParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000329
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000330 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor21a04f32008-10-27 19:41:14 +0000331 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000332 LAngleBracketLoc, CastTy.get(),
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000333 RAngleBracketLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000334 LParenLoc, move(Result), RParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000335
Sebastian Redl39d4f022008-12-11 22:51:44 +0000336 return move(Result);
Chris Lattner4b009652007-07-25 00:24:17 +0000337}
338
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000339/// ParseCXXTypeid - This handles the C++ typeid expression.
340///
341/// postfix-expression: [C++ 5.2p1]
342/// 'typeid' '(' expression ')'
343/// 'typeid' '(' type-id ')'
344///
Sebastian Redl39d4f022008-12-11 22:51:44 +0000345Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000346 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
347
348 SourceLocation OpLoc = ConsumeToken();
349 SourceLocation LParenLoc = Tok.getLocation();
350 SourceLocation RParenLoc;
351
352 // typeid expressions are always parenthesized.
353 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
354 "typeid"))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000355 return ExprError();
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000356
Sebastian Redl62261042008-12-09 20:22:58 +0000357 OwningExprResult Result(Actions);
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000358
359 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000360 TypeResult Ty = ParseTypeName();
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000361
362 // Match the ')'.
363 MatchRHSPunctuation(tok::r_paren, LParenLoc);
364
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000365 if (Ty.isInvalid())
Sebastian Redl39d4f022008-12-11 22:51:44 +0000366 return ExprError();
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000367
368 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000369 Ty.get(), RParenLoc);
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000370 } else {
371 Result = ParseExpression();
372
373 // Match the ')'.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000374 if (Result.isInvalid())
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000375 SkipUntil(tok::r_paren);
376 else {
377 MatchRHSPunctuation(tok::r_paren, LParenLoc);
378
379 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000380 Result.release(), RParenLoc);
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000381 }
382 }
383
Sebastian Redl39d4f022008-12-11 22:51:44 +0000384 return move(Result);
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000385}
386
Chris Lattner4b009652007-07-25 00:24:17 +0000387/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
388///
389/// boolean-literal: [C++ 2.13.5]
390/// 'true'
391/// 'false'
Sebastian Redl39d4f022008-12-11 22:51:44 +0000392Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Chris Lattner4b009652007-07-25 00:24:17 +0000393 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000394 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Chris Lattner4b009652007-07-25 00:24:17 +0000395}
Chris Lattnera7447ba2008-02-26 00:51:44 +0000396
397/// ParseThrowExpression - This handles the C++ throw expression.
398///
399/// throw-expression: [C++ 15]
400/// 'throw' assignment-expression[opt]
Sebastian Redl39d4f022008-12-11 22:51:44 +0000401Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000402 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnera7447ba2008-02-26 00:51:44 +0000403 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl39d4f022008-12-11 22:51:44 +0000404
Chris Lattner6b8842d2008-04-06 06:02:23 +0000405 // If the current token isn't the start of an assignment-expression,
406 // then the expression is not present. This handles things like:
407 // "C ? throw : (void)42", which is crazy but legal.
408 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
409 case tok::semi:
410 case tok::r_paren:
411 case tok::r_square:
412 case tok::r_brace:
413 case tok::colon:
414 case tok::comma:
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000415 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattnera7447ba2008-02-26 00:51:44 +0000416
Chris Lattner6b8842d2008-04-06 06:02:23 +0000417 default:
Sebastian Redl14ca7412008-12-11 21:36:32 +0000418 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl39d4f022008-12-11 22:51:44 +0000419 if (Expr.isInvalid()) return move(Expr);
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000420 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner6b8842d2008-04-06 06:02:23 +0000421 }
Chris Lattnera7447ba2008-02-26 00:51:44 +0000422}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000423
424/// ParseCXXThis - This handles the C++ 'this' pointer.
425///
426/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
427/// a non-lvalue expression whose value is the address of the object for which
428/// the function is called.
Sebastian Redl39d4f022008-12-11 22:51:44 +0000429Parser::OwningExprResult Parser::ParseCXXThis() {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000430 assert(Tok.is(tok::kw_this) && "Not 'this'!");
431 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000432 return Actions.ActOnCXXThis(ThisLoc);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000433}
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000434
435/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
436/// Can be interpreted either as function-style casting ("int(x)")
437/// or class type construction ("ClassType(x,y,z)")
438/// or creation of a value-initialized type ("int()").
439///
440/// postfix-expression: [C++ 5.2p1]
441/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
442/// typename-specifier '(' expression-list[opt] ')' [TODO]
443///
Sebastian Redl39d4f022008-12-11 22:51:44 +0000444Parser::OwningExprResult
445Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000446 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000447 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000448
449 assert(Tok.is(tok::l_paren) && "Expected '('!");
450 SourceLocation LParenLoc = ConsumeParen();
451
Sebastian Redl6008ac32008-11-25 22:21:31 +0000452 ExprVector Exprs(Actions);
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000453 CommaLocsTy CommaLocs;
454
455 if (Tok.isNot(tok::r_paren)) {
456 if (ParseExpressionList(Exprs, CommaLocs)) {
457 SkipUntil(tok::r_paren);
Sebastian Redl39d4f022008-12-11 22:51:44 +0000458 return ExprError();
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000459 }
460 }
461
462 // Match the ')'.
463 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
464
465 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
466 "Unexpected number of commas!");
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000467 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
468 LParenLoc, move_arg(Exprs),
Jay Foad9e6bef42009-05-21 09:52:38 +0000469 CommaLocs.data(), RParenLoc);
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000470}
471
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000472/// ParseCXXCondition - if/switch/while/for condition expression.
473///
474/// condition:
475/// expression
476/// type-specifier-seq declarator '=' assignment-expression
477/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
478/// '=' assignment-expression
479///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000480Parser::OwningExprResult Parser::ParseCXXCondition() {
Argiris Kirtzidis88527fb2008-10-05 15:03:47 +0000481 if (!isCXXConditionDeclaration())
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000482 return ParseExpression(); // expression
483
484 SourceLocation StartLoc = Tok.getLocation();
485
486 // type-specifier-seq
487 DeclSpec DS;
488 ParseSpecifierQualifierList(DS);
489
490 // declarator
491 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
492 ParseDeclarator(DeclaratorInfo);
493
494 // simple-asm-expr[opt]
495 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000496 SourceLocation Loc;
497 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000498 if (AsmLabel.isInvalid()) {
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000499 SkipUntil(tok::semi);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000500 return ExprError();
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000501 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000502 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000503 DeclaratorInfo.SetRangeEnd(Loc);
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000504 }
505
506 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000507 if (Tok.is(tok::kw___attribute)) {
508 SourceLocation Loc;
509 AttributeList *AttrList = ParseAttributes(&Loc);
510 DeclaratorInfo.AddAttributes(AttrList, Loc);
511 }
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000512
513 // '=' assignment-expression
514 if (Tok.isNot(tok::equal))
Sebastian Redl14ca7412008-12-11 21:36:32 +0000515 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000516 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000517 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000518 if (AssignExpr.isInvalid())
Sebastian Redl14ca7412008-12-11 21:36:32 +0000519 return ExprError();
520
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000521 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
522 DeclaratorInfo,EqualLoc,
523 move(AssignExpr));
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000524}
525
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000526/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
527/// This should only be called when the current token is known to be part of
528/// simple-type-specifier.
529///
530/// simple-type-specifier:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000531/// '::'[opt] nested-name-specifier[opt] type-name
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000532/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
533/// char
534/// wchar_t
535/// bool
536/// short
537/// int
538/// long
539/// signed
540/// unsigned
541/// float
542/// double
543/// void
544/// [GNU] typeof-specifier
545/// [C++0x] auto [TODO]
546///
547/// type-name:
548/// class-name
549/// enum-name
550/// typedef-name
551///
552void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
553 DS.SetRangeStart(Tok.getLocation());
554 const char *PrevSpec;
555 SourceLocation Loc = Tok.getLocation();
556
557 switch (Tok.getKind()) {
Chris Lattner2c301452009-01-05 00:13:00 +0000558 case tok::identifier: // foo::bar
559 case tok::coloncolon: // ::foo::bar
560 assert(0 && "Annotation token should already be formed!");
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000561 default:
562 assert(0 && "Not a simple-type-specifier token!");
563 abort();
Chris Lattner2c301452009-01-05 00:13:00 +0000564
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000565 // type-name
Chris Lattner5d7eace2009-01-06 05:06:21 +0000566 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000567 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000568 Tok.getAnnotationValue());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000569 break;
570 }
571
572 // builtin types
573 case tok::kw_short:
574 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
575 break;
576 case tok::kw_long:
577 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
578 break;
579 case tok::kw_signed:
580 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
581 break;
582 case tok::kw_unsigned:
583 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
584 break;
585 case tok::kw_void:
586 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
587 break;
588 case tok::kw_char:
589 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
590 break;
591 case tok::kw_int:
592 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
593 break;
594 case tok::kw_float:
595 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
596 break;
597 case tok::kw_double:
598 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
599 break;
600 case tok::kw_wchar_t:
601 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
602 break;
603 case tok::kw_bool:
604 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
605 break;
606
607 // GNU typeof support.
608 case tok::kw_typeof:
609 ParseTypeofSpecifier(DS);
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000610 DS.Finish(Diags, PP);
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000611 return;
612 }
Chris Lattner5d7eace2009-01-06 05:06:21 +0000613 if (Tok.is(tok::annot_typename))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000614 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
615 else
616 DS.SetRangeEnd(Tok.getLocation());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000617 ConsumeToken();
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000618 DS.Finish(Diags, PP);
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000619}
Douglas Gregore60e5d32008-11-06 22:13:31 +0000620
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000621/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
622/// [dcl.name]), which is a non-empty sequence of type-specifiers,
623/// e.g., "const short int". Note that the DeclSpec is *not* finished
624/// by parsing the type-specifier-seq, because these sequences are
625/// typically followed by some form of declarator. Returns true and
626/// emits diagnostics if this is not a type-specifier-seq, false
627/// otherwise.
628///
629/// type-specifier-seq: [C++ 8.1]
630/// type-specifier type-specifier-seq[opt]
631///
632bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
633 DS.SetRangeStart(Tok.getLocation());
634 const char *PrevSpec = 0;
635 int isInvalid = 0;
636
637 // Parse one or more of the type specifiers.
Chris Lattnerd706dc82009-01-06 06:59:53 +0000638 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000639 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000640 return true;
641 }
Chris Lattnerd706dc82009-01-06 06:59:53 +0000642
Ted Kremenek34034d72009-01-06 19:17:58 +0000643 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ;
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000644
645 return false;
646}
647
Douglas Gregor682a8cf2008-11-17 16:14:12 +0000648/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregore60e5d32008-11-06 22:13:31 +0000649/// operator name (C++ [over.oper]). If successful, returns the
650/// predefined identifier that corresponds to that overloaded
651/// operator. Otherwise, returns NULL and does not consume any tokens.
652///
653/// operator-function-id: [C++ 13.5]
654/// 'operator' operator
655///
656/// operator: one of
657/// new delete new[] delete[]
658/// + - * / % ^ & | ~
659/// ! = < > += -= *= /= %=
660/// ^= &= |= << >> >>= <<= == !=
661/// <= >= && || ++ -- , ->* ->
662/// () []
Sebastian Redl0c986032009-02-09 18:23:29 +0000663OverloadedOperatorKind
664Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argiris Kirtzidisa9d57b62008-11-07 15:54:02 +0000665 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redl0c986032009-02-09 18:23:29 +0000666 SourceLocation Loc;
Douglas Gregore60e5d32008-11-06 22:13:31 +0000667
668 OverloadedOperatorKind Op = OO_None;
669 switch (NextToken().getKind()) {
670 case tok::kw_new:
671 ConsumeToken(); // 'operator'
Sebastian Redl0c986032009-02-09 18:23:29 +0000672 Loc = ConsumeToken(); // 'new'
Douglas Gregore60e5d32008-11-06 22:13:31 +0000673 if (Tok.is(tok::l_square)) {
674 ConsumeBracket(); // '['
Sebastian Redl0c986032009-02-09 18:23:29 +0000675 Loc = Tok.getLocation();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000676 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
677 Op = OO_Array_New;
678 } else {
679 Op = OO_New;
680 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000681 if (EndLoc)
682 *EndLoc = Loc;
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000683 return Op;
Douglas Gregore60e5d32008-11-06 22:13:31 +0000684
685 case tok::kw_delete:
686 ConsumeToken(); // 'operator'
Sebastian Redl0c986032009-02-09 18:23:29 +0000687 Loc = ConsumeToken(); // 'delete'
Douglas Gregore60e5d32008-11-06 22:13:31 +0000688 if (Tok.is(tok::l_square)) {
689 ConsumeBracket(); // '['
Sebastian Redl0c986032009-02-09 18:23:29 +0000690 Loc = Tok.getLocation();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000691 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
692 Op = OO_Array_Delete;
693 } else {
694 Op = OO_Delete;
695 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000696 if (EndLoc)
697 *EndLoc = Loc;
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000698 return Op;
Douglas Gregore60e5d32008-11-06 22:13:31 +0000699
Douglas Gregor9c6210b2008-11-10 13:38:07 +0000700#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregore60e5d32008-11-06 22:13:31 +0000701 case tok::Token: Op = OO_##Name; break;
Douglas Gregor9c6210b2008-11-10 13:38:07 +0000702#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregore60e5d32008-11-06 22:13:31 +0000703#include "clang/Basic/OperatorKinds.def"
704
705 case tok::l_paren:
706 ConsumeToken(); // 'operator'
707 ConsumeParen(); // '('
Sebastian Redl0c986032009-02-09 18:23:29 +0000708 Loc = Tok.getLocation();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000709 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redl0c986032009-02-09 18:23:29 +0000710 if (EndLoc)
711 *EndLoc = Loc;
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000712 return OO_Call;
Douglas Gregore60e5d32008-11-06 22:13:31 +0000713
714 case tok::l_square:
715 ConsumeToken(); // 'operator'
716 ConsumeBracket(); // '['
Sebastian Redl0c986032009-02-09 18:23:29 +0000717 Loc = Tok.getLocation();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000718 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redl0c986032009-02-09 18:23:29 +0000719 if (EndLoc)
720 *EndLoc = Loc;
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000721 return OO_Subscript;
Douglas Gregore60e5d32008-11-06 22:13:31 +0000722
723 default:
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000724 return OO_None;
Douglas Gregore60e5d32008-11-06 22:13:31 +0000725 }
726
Douglas Gregor682a8cf2008-11-17 16:14:12 +0000727 ConsumeToken(); // 'operator'
Sebastian Redl0c986032009-02-09 18:23:29 +0000728 Loc = ConsumeAnyToken(); // the operator itself
729 if (EndLoc)
730 *EndLoc = Loc;
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000731 return Op;
Douglas Gregore60e5d32008-11-06 22:13:31 +0000732}
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000733
734/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
735/// which expresses the name of a user-defined conversion operator
736/// (C++ [class.conv.fct]p1). Returns the type that this operator is
737/// specifying a conversion for, or NULL if there was an error.
738///
739/// conversion-function-id: [C++ 12.3.2]
740/// operator conversion-type-id
741///
742/// conversion-type-id:
743/// type-specifier-seq conversion-declarator[opt]
744///
745/// conversion-declarator:
746/// ptr-operator conversion-declarator[opt]
Sebastian Redl0c986032009-02-09 18:23:29 +0000747Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000748 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
749 ConsumeToken(); // 'operator'
750
751 // Parse the type-specifier-seq.
752 DeclSpec DS;
753 if (ParseCXXTypeSpecifierSeq(DS))
754 return 0;
755
756 // Parse the conversion-declarator, which is merely a sequence of
757 // ptr-operators.
758 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000759 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redl0c986032009-02-09 18:23:29 +0000760 if (EndLoc)
761 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000762
763 // Finish up the type.
764 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000765 if (Result.isInvalid())
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000766 return 0;
767 else
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000768 return Result.get();
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000769}
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000770
771/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
772/// memory in a typesafe manner and call constructors.
Chris Lattnere7de3612009-01-04 21:25:24 +0000773///
774/// This method is called to parse the new expression after the optional :: has
775/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
776/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000777///
778/// new-expression:
779/// '::'[opt] 'new' new-placement[opt] new-type-id
780/// new-initializer[opt]
781/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
782/// new-initializer[opt]
783///
784/// new-placement:
785/// '(' expression-list ')'
786///
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000787/// new-type-id:
788/// type-specifier-seq new-declarator[opt]
789///
790/// new-declarator:
791/// ptr-operator new-declarator[opt]
792/// direct-new-declarator
793///
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000794/// new-initializer:
795/// '(' expression-list[opt] ')'
796/// [C++0x] braced-init-list [TODO]
797///
Chris Lattnere7de3612009-01-04 21:25:24 +0000798Parser::OwningExprResult
799Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
800 assert(Tok.is(tok::kw_new) && "expected 'new' token");
801 ConsumeToken(); // Consume 'new'
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000802
803 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
804 // second form of new-expression. It can't be a new-type-id.
805
Sebastian Redl6008ac32008-11-25 22:21:31 +0000806 ExprVector PlacementArgs(Actions);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000807 SourceLocation PlacementLParen, PlacementRParen;
808
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000809 bool ParenTypeId;
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000810 DeclSpec DS;
811 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000812 if (Tok.is(tok::l_paren)) {
813 // If it turns out to be a placement, we change the type location.
814 PlacementLParen = ConsumeParen();
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000815 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
816 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl39d4f022008-12-11 22:51:44 +0000817 return ExprError();
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000818 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000819
820 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000821 if (PlacementRParen.isInvalid()) {
822 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl39d4f022008-12-11 22:51:44 +0000823 return ExprError();
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000824 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000825
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000826 if (PlacementArgs.empty()) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000827 // Reset the placement locations. There was no placement.
828 PlacementLParen = PlacementRParen = SourceLocation();
829 ParenTypeId = true;
830 } else {
831 // We still need the type.
832 if (Tok.is(tok::l_paren)) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000833 SourceLocation LParen = ConsumeParen();
834 ParseSpecifierQualifierList(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +0000835 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000836 ParseDeclarator(DeclaratorInfo);
837 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000838 ParenTypeId = true;
839 } else {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000840 if (ParseCXXTypeSpecifierSeq(DS))
841 DeclaratorInfo.setInvalidType(true);
Sebastian Redl0c986032009-02-09 18:23:29 +0000842 else {
843 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000844 ParseDeclaratorInternal(DeclaratorInfo,
845 &Parser::ParseDirectNewDeclarator);
Sebastian Redl0c986032009-02-09 18:23:29 +0000846 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000847 ParenTypeId = false;
848 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000849 }
850 } else {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000851 // A new-type-id is a simplified type-id, where essentially the
852 // direct-declarator is replaced by a direct-new-declarator.
853 if (ParseCXXTypeSpecifierSeq(DS))
854 DeclaratorInfo.setInvalidType(true);
Sebastian Redl0c986032009-02-09 18:23:29 +0000855 else {
856 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000857 ParseDeclaratorInternal(DeclaratorInfo,
858 &Parser::ParseDirectNewDeclarator);
Sebastian Redl0c986032009-02-09 18:23:29 +0000859 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000860 ParenTypeId = false;
861 }
Chris Lattner34c61332009-04-25 08:06:05 +0000862 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000863 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl39d4f022008-12-11 22:51:44 +0000864 return ExprError();
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000865 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000866
Sebastian Redl6008ac32008-11-25 22:21:31 +0000867 ExprVector ConstructorArgs(Actions);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000868 SourceLocation ConstructorLParen, ConstructorRParen;
869
870 if (Tok.is(tok::l_paren)) {
871 ConstructorLParen = ConsumeParen();
872 if (Tok.isNot(tok::r_paren)) {
873 CommaLocsTy CommaLocs;
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000874 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
875 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl39d4f022008-12-11 22:51:44 +0000876 return ExprError();
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000877 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000878 }
879 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000880 if (ConstructorRParen.isInvalid()) {
881 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl39d4f022008-12-11 22:51:44 +0000882 return ExprError();
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000883 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000884 }
885
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000886 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
887 move_arg(PlacementArgs), PlacementRParen,
888 ParenTypeId, DeclaratorInfo, ConstructorLParen,
889 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000890}
891
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000892/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
893/// passed to ParseDeclaratorInternal.
894///
895/// direct-new-declarator:
896/// '[' expression ']'
897/// direct-new-declarator '[' constant-expression ']'
898///
Chris Lattnere7de3612009-01-04 21:25:24 +0000899void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000900 // Parse the array dimensions.
901 bool first = true;
902 while (Tok.is(tok::l_square)) {
903 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000904 OwningExprResult Size(first ? ParseExpression()
905 : ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000906 if (Size.isInvalid()) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000907 // Recover
908 SkipUntil(tok::r_square);
909 return;
910 }
911 first = false;
912
Sebastian Redl0c986032009-02-09 18:23:29 +0000913 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000914 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Sebastian Redl0c986032009-02-09 18:23:29 +0000915 Size.release(), LLoc),
916 RLoc);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000917
Sebastian Redl0c986032009-02-09 18:23:29 +0000918 if (RLoc.isInvalid())
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000919 return;
920 }
921}
922
923/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
924/// This ambiguity appears in the syntax of the C++ new operator.
925///
926/// new-expression:
927/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
928/// new-initializer[opt]
929///
930/// new-placement:
931/// '(' expression-list ')'
932///
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000933bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattnere7de3612009-01-04 21:25:24 +0000934 Declarator &D) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000935 // The '(' was already consumed.
936 if (isTypeIdInParens()) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000937 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redl0c986032009-02-09 18:23:29 +0000938 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000939 ParseDeclarator(D);
Chris Lattner34c61332009-04-25 08:06:05 +0000940 return D.isInvalidType();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000941 }
942
943 // It's not a type, it has to be an expression list.
944 // Discard the comma locations - ActOnCXXNew has enough parameters.
945 CommaLocsTy CommaLocs;
946 return ParseExpressionList(PlacementArgs, CommaLocs);
947}
948
949/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
950/// to free memory allocated by new.
951///
Chris Lattnere7de3612009-01-04 21:25:24 +0000952/// This method is called to parse the 'delete' expression after the optional
953/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
954/// and "Start" is its location. Otherwise, "Start" is the location of the
955/// 'delete' token.
956///
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000957/// delete-expression:
958/// '::'[opt] 'delete' cast-expression
959/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000960Parser::OwningExprResult
961Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
962 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
963 ConsumeToken(); // Consume 'delete'
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000964
965 // Array delete?
966 bool ArrayDelete = false;
967 if (Tok.is(tok::l_square)) {
968 ArrayDelete = true;
969 SourceLocation LHS = ConsumeBracket();
970 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
971 if (RHS.isInvalid())
Sebastian Redl39d4f022008-12-11 22:51:44 +0000972 return ExprError();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000973 }
974
Sebastian Redl14ca7412008-12-11 21:36:32 +0000975 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000976 if (Operand.isInvalid())
Sebastian Redl39d4f022008-12-11 22:51:44 +0000977 return move(Operand);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000978
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000979 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000980}
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000981
982static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
983{
984 switch(kind) {
985 default: assert(false && "Not a known unary type trait.");
986 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
987 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
988 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
989 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
990 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
991 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
992 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
993 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
994 case tok::kw___is_abstract: return UTT_IsAbstract;
995 case tok::kw___is_class: return UTT_IsClass;
996 case tok::kw___is_empty: return UTT_IsEmpty;
997 case tok::kw___is_enum: return UTT_IsEnum;
998 case tok::kw___is_pod: return UTT_IsPOD;
999 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1000 case tok::kw___is_union: return UTT_IsUnion;
1001 }
1002}
1003
1004/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1005/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1006/// templates.
1007///
1008/// primary-expression:
1009/// [GNU] unary-type-trait '(' type-id ')'
1010///
1011Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
1012{
1013 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1014 SourceLocation Loc = ConsumeToken();
1015
1016 SourceLocation LParen = Tok.getLocation();
1017 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1018 return ExprError();
1019
1020 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1021 // there will be cryptic errors about mismatched parentheses and missing
1022 // specifiers.
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001023 TypeResult Ty = ParseTypeName();
Sebastian Redl39c0f6f2009-01-05 20:52:13 +00001024
1025 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1026
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001027 if (Ty.isInvalid())
1028 return ExprError();
1029
1030 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl39c0f6f2009-01-05 20:52:13 +00001031}