blob: 2c6963a05f212ac89733b2a235337c0b456c9fa3 [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"
Sebastian Redla55e52c2008-11-25 22:21:31 +000017#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
Chris Lattner7a0ab5f2009-01-06 06:59:53 +000020/// ParseOptionalCXXScopeSpecifier - Parse global scope or
21/// nested-name-specifier if present. Returns true if a nested-name-specifier
22/// was parsed from the token stream. Note that this routine will not parse
23/// ::new or ::delete, it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000024///
25/// '::'[opt] nested-name-specifier
26/// '::'
27///
28/// nested-name-specifier:
29/// type-name '::'
30/// namespace-name '::'
31/// nested-name-specifier identifier '::'
32/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
33///
Chris Lattner7a0ab5f2009-01-06 06:59:53 +000034bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000035 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000036 "Call sites of this function should be guarded by checking for C++");
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000037
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000038 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000039 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000040 SS.setRange(Tok.getAnnotationRange());
41 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000042 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000043 }
Chris Lattnere607e802009-01-04 21:14:15 +000044
Douglas Gregor39a8de12009-02-25 19:37:18 +000045 bool HasScopeSpecifier = false;
46
Chris Lattner5b454732009-01-05 03:55:46 +000047 if (Tok.is(tok::coloncolon)) {
48 // ::new and ::delete aren't nested-name-specifiers.
49 tok::TokenKind NextKind = NextToken().getKind();
50 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
51 return false;
Chris Lattner55a7cef2009-01-05 00:13:00 +000052
Chris Lattner55a7cef2009-01-05 00:13:00 +000053 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000054 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000055 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000056 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000057 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000058 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000059 }
60
Douglas Gregor39a8de12009-02-25 19:37:18 +000061 while (true) {
62 // nested-name-specifier:
63 // type-name '::'
64 // namespace-name '::'
65 // nested-name-specifier identifier '::'
66 if (Tok.is(tok::identifier) && NextToken().is(tok::coloncolon)) {
67 // We have an identifier followed by a '::'. Lookup this name
68 // as the name in a nested-name-specifier.
69 IdentifierInfo *II = Tok.getIdentifierInfo();
70 SourceLocation IdLoc = ConsumeToken();
71 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
72 SourceLocation CCLoc = ConsumeToken();
73
74 if (!HasScopeSpecifier) {
75 SS.setBeginLoc(IdLoc);
76 HasScopeSpecifier = true;
77 }
78
79 if (SS.isInvalid())
80 continue;
81
Douglas Gregor35073692009-03-26 23:56:24 +000082 SS.setScopeRep(
Douglas Gregor39a8de12009-02-25 19:37:18 +000083 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, *II));
84 SS.setEndLoc(CCLoc);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000085 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +000086 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000087
Douglas Gregor39a8de12009-02-25 19:37:18 +000088 // nested-name-specifier:
89 // type-name '::'
90 // nested-name-specifier 'template'[opt] simple-template-id '::'
91 if ((Tok.is(tok::identifier) && NextToken().is(tok::less)) ||
92 Tok.is(tok::kw_template)) {
93 // Parse the optional 'template' keyword, then make sure we have
94 // 'identifier <' after it.
Douglas Gregor39a8de12009-02-25 19:37:18 +000095 if (Tok.is(tok::kw_template)) {
Douglas Gregorc45c2322009-03-31 00:43:58 +000096 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +000097
98 if (Tok.isNot(tok::identifier)) {
99 Diag(Tok.getLocation(),
100 diag::err_id_after_template_in_nested_name_spec)
101 << SourceRange(TemplateKWLoc);
102 break;
103 }
104
105 if (NextToken().isNot(tok::less)) {
106 Diag(NextToken().getLocation(),
107 diag::err_less_after_template_name_in_nested_name_spec)
108 << Tok.getIdentifierInfo()->getName()
109 << SourceRange(TemplateKWLoc, Tok.getLocation());
110 break;
111 }
Douglas Gregorc45c2322009-03-31 00:43:58 +0000112
113 TemplateTy Template
114 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
115 *Tok.getIdentifierInfo(),
116 Tok.getLocation(),
117 SS);
118 AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
119 &SS, TemplateKWLoc, false);
120 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000121 }
122
Douglas Gregor7532dc62009-03-30 22:58:21 +0000123 TemplateTy Template;
Douglas Gregorc45c2322009-03-31 00:43:58 +0000124 TemplateNameKind TNK = Actions.isTemplateName(*Tok.getIdentifierInfo(),
125 CurScope, Template, &SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000126 if (TNK) {
127 // We have found a template name, so annotate this this token
128 // with a template-id annotation. We do not permit the
129 // template-id to be translated into a type annotation,
130 // because some clients (e.g., the parsing of class template
131 // specializations) still want to see the original template-id
132 // token.
Douglas Gregorc45c2322009-03-31 00:43:58 +0000133 AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(), false);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000134 continue;
135 }
136 }
137
138 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
139 // We have
140 //
141 // simple-template-id '::'
142 //
143 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000144 // right kind (it should name a type or be dependent), and then
145 // convert it into a type within the nested-name-specifier.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000146 TemplateIdAnnotation *TemplateId
147 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
148
Douglas Gregorc45c2322009-03-31 00:43:58 +0000149 if (TemplateId->Kind == TNK_Type_template ||
150 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000151 if (AnnotateTemplateIdTokenAsType(&SS))
Douglas Gregore4e5b052009-03-19 00:18:19 +0000152 SS.clear();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000153
154 assert(Tok.is(tok::annot_typename) &&
155 "AnnotateTemplateIdTokenAsType isn't working");
156
157 Token TypeToken = Tok;
158 ConsumeToken();
159 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
160 SourceLocation CCLoc = ConsumeToken();
161
162 if (!HasScopeSpecifier) {
163 SS.setBeginLoc(TypeToken.getLocation());
164 HasScopeSpecifier = true;
165 }
166
Douglas Gregor35073692009-03-26 23:56:24 +0000167 SS.setScopeRep(
Douglas Gregor39a8de12009-02-25 19:37:18 +0000168 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
169 TypeToken.getAnnotationValue(),
170 TypeToken.getAnnotationRange(),
171 CCLoc));
172 SS.setEndLoc(CCLoc);
173 continue;
174 } else
Douglas Gregorc45c2322009-03-31 00:43:58 +0000175 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000176 }
177
178 // We don't have any tokens that form the beginning of a
179 // nested-name-specifier, so we're done.
180 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000181 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000182
183 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000184}
185
186/// ParseCXXIdExpression - Handle id-expression.
187///
188/// id-expression:
189/// unqualified-id
190/// qualified-id
191///
192/// unqualified-id:
193/// identifier
194/// operator-function-id
195/// conversion-function-id [TODO]
196/// '~' class-name [TODO]
197/// template-id [TODO]
198///
199/// qualified-id:
200/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
201/// '::' identifier
202/// '::' operator-function-id
203/// '::' template-id [TODO]
204///
205/// nested-name-specifier:
206/// type-name '::'
207/// namespace-name '::'
208/// nested-name-specifier identifier '::'
209/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
210///
211/// NOTE: The standard specifies that, for qualified-id, the parser does not
212/// expect:
213///
214/// '::' conversion-function-id
215/// '::' '~' class-name
216///
217/// This may cause a slight inconsistency on diagnostics:
218///
219/// class C {};
220/// namespace A {}
221/// void f() {
222/// :: A :: ~ C(); // Some Sema error about using destructor with a
223/// // namespace.
224/// :: ~ C(); // Some Parser error like 'unexpected ~'.
225/// }
226///
227/// We simplify the parser a bit and make it work like:
228///
229/// qualified-id:
230/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
231/// '::' unqualified-id
232///
233/// That way Sema can handle and report similar errors for namespaces and the
234/// global scope.
235///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000236/// The isAddressOfOperand parameter indicates that this id-expression is a
237/// direct operand of the address-of operator. This is, besides member contexts,
238/// the only place where a qualified-id naming a non-static class member may
239/// appear.
240///
241Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000242 // qualified-id:
243 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
244 // '::' unqualified-id
245 //
246 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000247 ParseOptionalCXXScopeSpecifier(SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000248
249 // unqualified-id:
250 // identifier
251 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000252 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000253 // '~' class-name [TODO]
254 // template-id [TODO]
255 //
256 switch (Tok.getKind()) {
257 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000258 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000259
260 case tok::identifier: {
261 // Consume the identifier so that we can see if it is followed by a '('.
262 IdentifierInfo &II = *Tok.getIdentifierInfo();
263 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000264 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
265 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000266 }
267
268 case tok::kw_operator: {
269 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000270 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000271 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000272 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
273 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000274 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000275 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000276 Tok.is(tok::l_paren), SS,
277 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000278
Douglas Gregor2def4832008-11-17 20:34:05 +0000279 // We already complained about a bad conversion-function-id,
280 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000281 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000282 }
283
284 } // switch.
285
286 assert(0 && "The switch was supposed to take care everything.");
287}
288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289/// ParseCXXCasts - This handles the various ways to cast expressions to another
290/// type.
291///
292/// postfix-expression: [C++ 5.2p1]
293/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
294/// 'static_cast' '<' type-name '>' '(' expression ')'
295/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
296/// 'const_cast' '<' type-name '>' '(' expression ')'
297///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000298Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 tok::TokenKind Kind = Tok.getKind();
300 const char *CastName = 0; // For error messages
301
302 switch (Kind) {
303 default: assert(0 && "Unknown C++ cast!"); abort();
304 case tok::kw_const_cast: CastName = "const_cast"; break;
305 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
306 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
307 case tok::kw_static_cast: CastName = "static_cast"; break;
308 }
309
310 SourceLocation OpLoc = ConsumeToken();
311 SourceLocation LAngleBracketLoc = Tok.getLocation();
312
313 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000314 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000315
Douglas Gregor809070a2009-02-18 17:45:20 +0000316 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 SourceLocation RAngleBracketLoc = Tok.getLocation();
318
Chris Lattner1ab3b962008-11-18 07:48:38 +0000319 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000320 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000321
322 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
323
Chris Lattner1ab3b962008-11-18 07:48:38 +0000324 if (Tok.isNot(tok::l_paren))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000325 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << CastName);
Reid Spencer5f016e22007-07-11 17:01:13 +0000326
Sebastian Redld8c4e152008-12-11 22:33:27 +0000327 OwningExprResult Result(ParseSimpleParenExpression(RParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000328
Douglas Gregor809070a2009-02-18 17:45:20 +0000329 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000330 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000331 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000332 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000333 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000334
Sebastian Redl20df9b72008-12-11 22:51:44 +0000335 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000336}
337
Sebastian Redlc42e1182008-11-11 11:37:55 +0000338/// ParseCXXTypeid - This handles the C++ typeid expression.
339///
340/// postfix-expression: [C++ 5.2p1]
341/// 'typeid' '(' expression ')'
342/// 'typeid' '(' type-id ')'
343///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000344Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000345 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
346
347 SourceLocation OpLoc = ConsumeToken();
348 SourceLocation LParenLoc = Tok.getLocation();
349 SourceLocation RParenLoc;
350
351 // typeid expressions are always parenthesized.
352 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
353 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000354 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000355
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000356 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000357
358 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000359 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000360
361 // Match the ')'.
362 MatchRHSPunctuation(tok::r_paren, LParenLoc);
363
Douglas Gregor809070a2009-02-18 17:45:20 +0000364 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000365 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000366
367 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000368 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000369 } else {
370 Result = ParseExpression();
371
372 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000373 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000374 SkipUntil(tok::r_paren);
375 else {
376 MatchRHSPunctuation(tok::r_paren, LParenLoc);
377
378 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000379 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000380 }
381 }
382
Sebastian Redl20df9b72008-12-11 22:51:44 +0000383 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000384}
385
Reid Spencer5f016e22007-07-11 17:01:13 +0000386/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
387///
388/// boolean-literal: [C++ 2.13.5]
389/// 'true'
390/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000391Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000393 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000394}
Chris Lattner50dd2892008-02-26 00:51:44 +0000395
396/// ParseThrowExpression - This handles the C++ throw expression.
397///
398/// throw-expression: [C++ 15]
399/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000400Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000401 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000402 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000403
Chris Lattner2a2819a2008-04-06 06:02:23 +0000404 // If the current token isn't the start of an assignment-expression,
405 // then the expression is not present. This handles things like:
406 // "C ? throw : (void)42", which is crazy but legal.
407 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
408 case tok::semi:
409 case tok::r_paren:
410 case tok::r_square:
411 case tok::r_brace:
412 case tok::colon:
413 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000414 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000415
Chris Lattner2a2819a2008-04-06 06:02:23 +0000416 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000417 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000418 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000419 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000420 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000421}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000422
423/// ParseCXXThis - This handles the C++ 'this' pointer.
424///
425/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
426/// a non-lvalue expression whose value is the address of the object for which
427/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000428Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000429 assert(Tok.is(tok::kw_this) && "Not 'this'!");
430 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000431 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000432}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000433
434/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
435/// Can be interpreted either as function-style casting ("int(x)")
436/// or class type construction ("ClassType(x,y,z)")
437/// or creation of a value-initialized type ("int()").
438///
439/// postfix-expression: [C++ 5.2p1]
440/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
441/// typename-specifier '(' expression-list[opt] ')' [TODO]
442///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000443Parser::OwningExprResult
444Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000445 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000446 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000447
448 assert(Tok.is(tok::l_paren) && "Expected '('!");
449 SourceLocation LParenLoc = ConsumeParen();
450
Sebastian Redla55e52c2008-11-25 22:21:31 +0000451 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000452 CommaLocsTy CommaLocs;
453
454 if (Tok.isNot(tok::r_paren)) {
455 if (ParseExpressionList(Exprs, CommaLocs)) {
456 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000457 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000458 }
459 }
460
461 // Match the ')'.
462 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
463
464 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
465 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000466 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
467 LParenLoc, move_arg(Exprs),
468 &CommaLocs[0], RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000469}
470
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000471/// ParseCXXCondition - if/switch/while/for condition expression.
472///
473/// condition:
474/// expression
475/// type-specifier-seq declarator '=' assignment-expression
476/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
477/// '=' assignment-expression
478///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000479Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000480 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000481 return ParseExpression(); // expression
482
483 SourceLocation StartLoc = Tok.getLocation();
484
485 // type-specifier-seq
486 DeclSpec DS;
487 ParseSpecifierQualifierList(DS);
488
489 // declarator
490 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
491 ParseDeclarator(DeclaratorInfo);
492
493 // simple-asm-expr[opt]
494 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000495 SourceLocation Loc;
496 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000497 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000498 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000499 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000500 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000501 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000502 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000503 }
504
505 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000506 if (Tok.is(tok::kw___attribute)) {
507 SourceLocation Loc;
508 AttributeList *AttrList = ParseAttributes(&Loc);
509 DeclaratorInfo.AddAttributes(AttrList, Loc);
510 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000511
512 // '=' assignment-expression
513 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000514 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000515 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000516 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000517 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000518 return ExprError();
519
Sebastian Redlf53597f2009-03-15 17:47:39 +0000520 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
521 DeclaratorInfo,EqualLoc,
522 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000523}
524
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000525/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
526/// This should only be called when the current token is known to be part of
527/// simple-type-specifier.
528///
529/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000530/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000531/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
532/// char
533/// wchar_t
534/// bool
535/// short
536/// int
537/// long
538/// signed
539/// unsigned
540/// float
541/// double
542/// void
543/// [GNU] typeof-specifier
544/// [C++0x] auto [TODO]
545///
546/// type-name:
547/// class-name
548/// enum-name
549/// typedef-name
550///
551void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
552 DS.SetRangeStart(Tok.getLocation());
553 const char *PrevSpec;
554 SourceLocation Loc = Tok.getLocation();
555
556 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000557 case tok::identifier: // foo::bar
558 case tok::coloncolon: // ::foo::bar
559 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000560 default:
561 assert(0 && "Not a simple-type-specifier token!");
562 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000563
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000564 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000565 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000566 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000567 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000568 break;
569 }
570
571 // builtin types
572 case tok::kw_short:
573 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
574 break;
575 case tok::kw_long:
576 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
577 break;
578 case tok::kw_signed:
579 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
580 break;
581 case tok::kw_unsigned:
582 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
583 break;
584 case tok::kw_void:
585 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
586 break;
587 case tok::kw_char:
588 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
589 break;
590 case tok::kw_int:
591 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
592 break;
593 case tok::kw_float:
594 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
595 break;
596 case tok::kw_double:
597 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
598 break;
599 case tok::kw_wchar_t:
600 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
601 break;
602 case tok::kw_bool:
603 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
604 break;
605
606 // GNU typeof support.
607 case tok::kw_typeof:
608 ParseTypeofSpecifier(DS);
609 DS.Finish(Diags, PP.getSourceManager(), getLang());
610 return;
611 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000612 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000613 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
614 else
615 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000616 ConsumeToken();
617 DS.Finish(Diags, PP.getSourceManager(), getLang());
618}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000619
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000620/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
621/// [dcl.name]), which is a non-empty sequence of type-specifiers,
622/// e.g., "const short int". Note that the DeclSpec is *not* finished
623/// by parsing the type-specifier-seq, because these sequences are
624/// typically followed by some form of declarator. Returns true and
625/// emits diagnostics if this is not a type-specifier-seq, false
626/// otherwise.
627///
628/// type-specifier-seq: [C++ 8.1]
629/// type-specifier type-specifier-seq[opt]
630///
631bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
632 DS.SetRangeStart(Tok.getLocation());
633 const char *PrevSpec = 0;
634 int isInvalid = 0;
635
636 // Parse one or more of the type specifiers.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000637 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000638 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000639 return true;
640 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000641
Ted Kremenekb8006e52009-01-06 19:17:58 +0000642 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000643
644 return false;
645}
646
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000647/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000648/// operator name (C++ [over.oper]). If successful, returns the
649/// predefined identifier that corresponds to that overloaded
650/// operator. Otherwise, returns NULL and does not consume any tokens.
651///
652/// operator-function-id: [C++ 13.5]
653/// 'operator' operator
654///
655/// operator: one of
656/// new delete new[] delete[]
657/// + - * / % ^ & | ~
658/// ! = < > += -= *= /= %=
659/// ^= &= |= << >> >>= <<= == !=
660/// <= >= && || ++ -- , ->* ->
661/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +0000662OverloadedOperatorKind
663Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000664 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +0000665 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000666
667 OverloadedOperatorKind Op = OO_None;
668 switch (NextToken().getKind()) {
669 case tok::kw_new:
670 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000671 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000672 if (Tok.is(tok::l_square)) {
673 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000674 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000675 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
676 Op = OO_Array_New;
677 } else {
678 Op = OO_New;
679 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000680 if (EndLoc)
681 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000682 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000683
684 case tok::kw_delete:
685 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000686 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000687 if (Tok.is(tok::l_square)) {
688 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000689 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000690 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
691 Op = OO_Array_Delete;
692 } else {
693 Op = OO_Delete;
694 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000695 if (EndLoc)
696 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000697 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000698
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000699#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000700 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000701#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000702#include "clang/Basic/OperatorKinds.def"
703
704 case tok::l_paren:
705 ConsumeToken(); // 'operator'
706 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +0000707 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000708 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000709 if (EndLoc)
710 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000711 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000712
713 case tok::l_square:
714 ConsumeToken(); // 'operator'
715 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000716 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000717 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000718 if (EndLoc)
719 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000720 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000721
722 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000723 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000724 }
725
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000726 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000727 Loc = ConsumeAnyToken(); // the operator itself
728 if (EndLoc)
729 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000730 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000731}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000732
733/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
734/// which expresses the name of a user-defined conversion operator
735/// (C++ [class.conv.fct]p1). Returns the type that this operator is
736/// specifying a conversion for, or NULL if there was an error.
737///
738/// conversion-function-id: [C++ 12.3.2]
739/// operator conversion-type-id
740///
741/// conversion-type-id:
742/// type-specifier-seq conversion-declarator[opt]
743///
744/// conversion-declarator:
745/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +0000746Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000747 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
748 ConsumeToken(); // 'operator'
749
750 // Parse the type-specifier-seq.
751 DeclSpec DS;
752 if (ParseCXXTypeSpecifierSeq(DS))
753 return 0;
754
755 // Parse the conversion-declarator, which is merely a sequence of
756 // ptr-operators.
757 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000758 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000759 if (EndLoc)
760 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000761
762 // Finish up the type.
763 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000764 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000765 return 0;
766 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000767 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000768}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000769
770/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
771/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000772///
773/// This method is called to parse the new expression after the optional :: has
774/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
775/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000776///
777/// new-expression:
778/// '::'[opt] 'new' new-placement[opt] new-type-id
779/// new-initializer[opt]
780/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
781/// new-initializer[opt]
782///
783/// new-placement:
784/// '(' expression-list ')'
785///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000786/// new-type-id:
787/// type-specifier-seq new-declarator[opt]
788///
789/// new-declarator:
790/// ptr-operator new-declarator[opt]
791/// direct-new-declarator
792///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000793/// new-initializer:
794/// '(' expression-list[opt] ')'
795/// [C++0x] braced-init-list [TODO]
796///
Chris Lattner59232d32009-01-04 21:25:24 +0000797Parser::OwningExprResult
798Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
799 assert(Tok.is(tok::kw_new) && "expected 'new' token");
800 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000801
802 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
803 // second form of new-expression. It can't be a new-type-id.
804
Sebastian Redla55e52c2008-11-25 22:21:31 +0000805 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000806 SourceLocation PlacementLParen, PlacementRParen;
807
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000808 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000809 DeclSpec DS;
810 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000811 if (Tok.is(tok::l_paren)) {
812 // If it turns out to be a placement, we change the type location.
813 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000814 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
815 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000816 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000817 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000818
819 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000820 if (PlacementRParen.isInvalid()) {
821 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000822 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000823 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000824
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000825 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000826 // Reset the placement locations. There was no placement.
827 PlacementLParen = PlacementRParen = SourceLocation();
828 ParenTypeId = true;
829 } else {
830 // We still need the type.
831 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000832 SourceLocation LParen = ConsumeParen();
833 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000834 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000835 ParseDeclarator(DeclaratorInfo);
836 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000837 ParenTypeId = true;
838 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000839 if (ParseCXXTypeSpecifierSeq(DS))
840 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000841 else {
842 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000843 ParseDeclaratorInternal(DeclaratorInfo,
844 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000845 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000846 ParenTypeId = false;
847 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000848 }
849 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000850 // A new-type-id is a simplified type-id, where essentially the
851 // direct-declarator is replaced by a direct-new-declarator.
852 if (ParseCXXTypeSpecifierSeq(DS))
853 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000854 else {
855 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000856 ParseDeclaratorInternal(DeclaratorInfo,
857 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000858 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000859 ParenTypeId = false;
860 }
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000861 if (DeclaratorInfo.getInvalidType()) {
862 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000863 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000864 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000865
Sebastian Redla55e52c2008-11-25 22:21:31 +0000866 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000867 SourceLocation ConstructorLParen, ConstructorRParen;
868
869 if (Tok.is(tok::l_paren)) {
870 ConstructorLParen = ConsumeParen();
871 if (Tok.isNot(tok::r_paren)) {
872 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000873 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
874 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000875 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000876 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000877 }
878 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000879 if (ConstructorRParen.isInvalid()) {
880 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000881 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000882 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000883 }
884
Sebastian Redlf53597f2009-03-15 17:47:39 +0000885 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
886 move_arg(PlacementArgs), PlacementRParen,
887 ParenTypeId, DeclaratorInfo, ConstructorLParen,
888 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000889}
890
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000891/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
892/// passed to ParseDeclaratorInternal.
893///
894/// direct-new-declarator:
895/// '[' expression ']'
896/// direct-new-declarator '[' constant-expression ']'
897///
Chris Lattner59232d32009-01-04 21:25:24 +0000898void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000899 // Parse the array dimensions.
900 bool first = true;
901 while (Tok.is(tok::l_square)) {
902 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000903 OwningExprResult Size(first ? ParseExpression()
904 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000905 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000906 // Recover
907 SkipUntil(tok::r_square);
908 return;
909 }
910 first = false;
911
Sebastian Redlab197ba2009-02-09 18:23:29 +0000912 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000913 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Sebastian Redlab197ba2009-02-09 18:23:29 +0000914 Size.release(), LLoc),
915 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000916
Sebastian Redlab197ba2009-02-09 18:23:29 +0000917 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000918 return;
919 }
920}
921
922/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
923/// This ambiguity appears in the syntax of the C++ new operator.
924///
925/// new-expression:
926/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
927/// new-initializer[opt]
928///
929/// new-placement:
930/// '(' expression-list ')'
931///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000932bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +0000933 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000934 // The '(' was already consumed.
935 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000936 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000937 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000938 ParseDeclarator(D);
939 return D.getInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000940 }
941
942 // It's not a type, it has to be an expression list.
943 // Discard the comma locations - ActOnCXXNew has enough parameters.
944 CommaLocsTy CommaLocs;
945 return ParseExpressionList(PlacementArgs, CommaLocs);
946}
947
948/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
949/// to free memory allocated by new.
950///
Chris Lattner59232d32009-01-04 21:25:24 +0000951/// This method is called to parse the 'delete' expression after the optional
952/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
953/// and "Start" is its location. Otherwise, "Start" is the location of the
954/// 'delete' token.
955///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000956/// delete-expression:
957/// '::'[opt] 'delete' cast-expression
958/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000959Parser::OwningExprResult
960Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
961 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
962 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000963
964 // Array delete?
965 bool ArrayDelete = false;
966 if (Tok.is(tok::l_square)) {
967 ArrayDelete = true;
968 SourceLocation LHS = ConsumeBracket();
969 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
970 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000971 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000972 }
973
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000974 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000975 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000976 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000977
Sebastian Redlf53597f2009-03-15 17:47:39 +0000978 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000979}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000980
981static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
982{
983 switch(kind) {
984 default: assert(false && "Not a known unary type trait.");
985 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
986 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
987 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
988 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
989 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
990 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
991 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
992 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
993 case tok::kw___is_abstract: return UTT_IsAbstract;
994 case tok::kw___is_class: return UTT_IsClass;
995 case tok::kw___is_empty: return UTT_IsEmpty;
996 case tok::kw___is_enum: return UTT_IsEnum;
997 case tok::kw___is_pod: return UTT_IsPOD;
998 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
999 case tok::kw___is_union: return UTT_IsUnion;
1000 }
1001}
1002
1003/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1004/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1005/// templates.
1006///
1007/// primary-expression:
1008/// [GNU] unary-type-trait '(' type-id ')'
1009///
1010Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
1011{
1012 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1013 SourceLocation Loc = ConsumeToken();
1014
1015 SourceLocation LParen = Tok.getLocation();
1016 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1017 return ExprError();
1018
1019 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1020 // there will be cryptic errors about mismatched parentheses and missing
1021 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001022 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001023
1024 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1025
Douglas Gregor809070a2009-02-18 17:45:20 +00001026 if (Ty.isInvalid())
1027 return ExprError();
1028
1029 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001030}