blob: e8ef8ea61ee7c07de713f1568ebeaa5498b16393 [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)) {
39 SS.setScopeRep(Tok.getAnnotationValue());
40 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
Chris Lattner5b454732009-01-05 03:55:46 +000045 if (Tok.is(tok::coloncolon)) {
46 // ::new and ::delete aren't nested-name-specifiers.
47 tok::TokenKind NextKind = NextToken().getKind();
48 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
49 return false;
Chris Lattner55a7cef2009-01-05 00:13:00 +000050
Chris Lattner55a7cef2009-01-05 00:13:00 +000051 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000052 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000053 SS.setBeginLoc(CCLoc);
54 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
55 SS.setEndLoc(CCLoc);
56 } else if (Tok.is(tok::identifier) && NextToken().is(tok::coloncolon)) {
57 SS.setBeginLoc(Tok.getLocation());
58 } else {
59 // Not a CXXScopeSpecifier.
60 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000061 }
62
63 // nested-name-specifier:
64 // type-name '::'
65 // namespace-name '::'
66 // nested-name-specifier identifier '::'
67 // nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000068 while (Tok.is(tok::identifier) && NextToken().is(tok::coloncolon)) {
69 IdentifierInfo *II = Tok.getIdentifierInfo();
70 SourceLocation IdLoc = ConsumeToken();
Chris Lattnere607e802009-01-04 21:14:15 +000071 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000072 SourceLocation CCLoc = ConsumeToken();
73 if (SS.isInvalid())
74 continue;
75
76 SS.setScopeRep(
Chris Lattnere607e802009-01-04 21:14:15 +000077 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, *II));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000078 SS.setEndLoc(CCLoc);
79 }
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000080
81 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000082}
83
84/// ParseCXXIdExpression - Handle id-expression.
85///
86/// id-expression:
87/// unqualified-id
88/// qualified-id
89///
90/// unqualified-id:
91/// identifier
92/// operator-function-id
93/// conversion-function-id [TODO]
94/// '~' class-name [TODO]
95/// template-id [TODO]
96///
97/// qualified-id:
98/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
99/// '::' identifier
100/// '::' operator-function-id
101/// '::' template-id [TODO]
102///
103/// nested-name-specifier:
104/// type-name '::'
105/// namespace-name '::'
106/// nested-name-specifier identifier '::'
107/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
108///
109/// NOTE: The standard specifies that, for qualified-id, the parser does not
110/// expect:
111///
112/// '::' conversion-function-id
113/// '::' '~' class-name
114///
115/// This may cause a slight inconsistency on diagnostics:
116///
117/// class C {};
118/// namespace A {}
119/// void f() {
120/// :: A :: ~ C(); // Some Sema error about using destructor with a
121/// // namespace.
122/// :: ~ C(); // Some Parser error like 'unexpected ~'.
123/// }
124///
125/// We simplify the parser a bit and make it work like:
126///
127/// qualified-id:
128/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
129/// '::' unqualified-id
130///
131/// That way Sema can handle and report similar errors for namespaces and the
132/// global scope.
133///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000134/// The isAddressOfOperand parameter indicates that this id-expression is a
135/// direct operand of the address-of operator. This is, besides member contexts,
136/// the only place where a qualified-id naming a non-static class member may
137/// appear.
138///
139Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000140 // qualified-id:
141 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
142 // '::' unqualified-id
143 //
144 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000145 ParseOptionalCXXScopeSpecifier(SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000146
147 // unqualified-id:
148 // identifier
149 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000150 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000151 // '~' class-name [TODO]
152 // template-id [TODO]
153 //
154 switch (Tok.getKind()) {
155 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000156 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000157
158 case tok::identifier: {
159 // Consume the identifier so that we can see if it is followed by a '('.
160 IdentifierInfo &II = *Tok.getIdentifierInfo();
161 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000162 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
163 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000164 }
165
166 case tok::kw_operator: {
167 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000168 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000169 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000170 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
171 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000172 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000173 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000174 Tok.is(tok::l_paren), SS,
175 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000176
Douglas Gregor2def4832008-11-17 20:34:05 +0000177 // We already complained about a bad conversion-function-id,
178 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000179 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000180 }
181
182 } // switch.
183
184 assert(0 && "The switch was supposed to take care everything.");
185}
186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187/// ParseCXXCasts - This handles the various ways to cast expressions to another
188/// type.
189///
190/// postfix-expression: [C++ 5.2p1]
191/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
192/// 'static_cast' '<' type-name '>' '(' expression ')'
193/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
194/// 'const_cast' '<' type-name '>' '(' expression ')'
195///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000196Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 tok::TokenKind Kind = Tok.getKind();
198 const char *CastName = 0; // For error messages
199
200 switch (Kind) {
201 default: assert(0 && "Unknown C++ cast!"); abort();
202 case tok::kw_const_cast: CastName = "const_cast"; break;
203 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
204 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
205 case tok::kw_static_cast: CastName = "static_cast"; break;
206 }
207
208 SourceLocation OpLoc = ConsumeToken();
209 SourceLocation LAngleBracketLoc = Tok.getLocation();
210
211 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000212 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000213
214 TypeTy *CastTy = ParseTypeName();
215 SourceLocation RAngleBracketLoc = Tok.getLocation();
216
Chris Lattner1ab3b962008-11-18 07:48:38 +0000217 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000218 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000219
220 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
221
Chris Lattner1ab3b962008-11-18 07:48:38 +0000222 if (Tok.isNot(tok::l_paren))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000223 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << CastName);
Reid Spencer5f016e22007-07-11 17:01:13 +0000224
Sebastian Redld8c4e152008-12-11 22:33:27 +0000225 OwningExprResult Result(ParseSimpleParenExpression(RParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000226
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000227 if (!Result.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000228 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
229 LAngleBracketLoc, CastTy, RAngleBracketLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000230 LParenLoc, Result.release(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000231
Sebastian Redl20df9b72008-12-11 22:51:44 +0000232 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000233}
234
Sebastian Redlc42e1182008-11-11 11:37:55 +0000235/// ParseCXXTypeid - This handles the C++ typeid expression.
236///
237/// postfix-expression: [C++ 5.2p1]
238/// 'typeid' '(' expression ')'
239/// 'typeid' '(' type-id ')'
240///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000241Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000242 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
243
244 SourceLocation OpLoc = ConsumeToken();
245 SourceLocation LParenLoc = Tok.getLocation();
246 SourceLocation RParenLoc;
247
248 // typeid expressions are always parenthesized.
249 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
250 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000251 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000252
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000253 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000254
255 if (isTypeIdInParens()) {
256 TypeTy *Ty = ParseTypeName();
257
258 // Match the ')'.
259 MatchRHSPunctuation(tok::r_paren, LParenLoc);
260
261 if (!Ty)
Sebastian Redl20df9b72008-12-11 22:51:44 +0000262 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000263
264 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
265 Ty, RParenLoc);
266 } else {
267 Result = ParseExpression();
268
269 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000270 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000271 SkipUntil(tok::r_paren);
272 else {
273 MatchRHSPunctuation(tok::r_paren, LParenLoc);
274
275 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000276 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000277 }
278 }
279
Sebastian Redl20df9b72008-12-11 22:51:44 +0000280 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000281}
282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
284///
285/// boolean-literal: [C++ 2.13.5]
286/// 'true'
287/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000288Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000290 return Owned(Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind));
Reid Spencer5f016e22007-07-11 17:01:13 +0000291}
Chris Lattner50dd2892008-02-26 00:51:44 +0000292
293/// ParseThrowExpression - This handles the C++ throw expression.
294///
295/// throw-expression: [C++ 15]
296/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000297Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000298 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000299 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000300
Chris Lattner2a2819a2008-04-06 06:02:23 +0000301 // If the current token isn't the start of an assignment-expression,
302 // then the expression is not present. This handles things like:
303 // "C ? throw : (void)42", which is crazy but legal.
304 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
305 case tok::semi:
306 case tok::r_paren:
307 case tok::r_square:
308 case tok::r_brace:
309 case tok::colon:
310 case tok::comma:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000311 return Owned(Actions.ActOnCXXThrow(ThrowLoc));
Chris Lattner50dd2892008-02-26 00:51:44 +0000312
Chris Lattner2a2819a2008-04-06 06:02:23 +0000313 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000314 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000315 if (Expr.isInvalid()) return move(Expr);
316 return Owned(Actions.ActOnCXXThrow(ThrowLoc, Expr.release()));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000317 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000318}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000319
320/// ParseCXXThis - This handles the C++ 'this' pointer.
321///
322/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
323/// a non-lvalue expression whose value is the address of the object for which
324/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000325Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000326 assert(Tok.is(tok::kw_this) && "Not 'this'!");
327 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000328 return Owned(Actions.ActOnCXXThis(ThisLoc));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000329}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000330
331/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
332/// Can be interpreted either as function-style casting ("int(x)")
333/// or class type construction ("ClassType(x,y,z)")
334/// or creation of a value-initialized type ("int()").
335///
336/// postfix-expression: [C++ 5.2p1]
337/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
338/// typename-specifier '(' expression-list[opt] ')' [TODO]
339///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000340Parser::OwningExprResult
341Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000342 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000343 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000344
345 assert(Tok.is(tok::l_paren) && "Expected '('!");
346 SourceLocation LParenLoc = ConsumeParen();
347
Sebastian Redla55e52c2008-11-25 22:21:31 +0000348 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000349 CommaLocsTy CommaLocs;
350
351 if (Tok.isNot(tok::r_paren)) {
352 if (ParseExpressionList(Exprs, CommaLocs)) {
353 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000354 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000355 }
356 }
357
358 // Match the ')'.
359 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
360
361 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
362 "Unexpected number of commas!");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000363 return Owned(Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
364 LParenLoc,
365 Exprs.take(), Exprs.size(),
366 &CommaLocs[0], RParenLoc));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000367}
368
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000369/// ParseCXXCondition - if/switch/while/for condition expression.
370///
371/// condition:
372/// expression
373/// type-specifier-seq declarator '=' assignment-expression
374/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
375/// '=' assignment-expression
376///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000377Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000378 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000379 return ParseExpression(); // expression
380
381 SourceLocation StartLoc = Tok.getLocation();
382
383 // type-specifier-seq
384 DeclSpec DS;
385 ParseSpecifierQualifierList(DS);
386
387 // declarator
388 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
389 ParseDeclarator(DeclaratorInfo);
390
391 // simple-asm-expr[opt]
392 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000393 SourceLocation Loc;
394 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000395 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000396 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000397 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000398 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000399 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000400 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000401 }
402
403 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000404 if (Tok.is(tok::kw___attribute)) {
405 SourceLocation Loc;
406 AttributeList *AttrList = ParseAttributes(&Loc);
407 DeclaratorInfo.AddAttributes(AttrList, Loc);
408 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000409
410 // '=' assignment-expression
411 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000412 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000413 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000414 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000415 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000416 return ExprError();
417
418 return Owned(Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
419 DeclaratorInfo,EqualLoc,
420 AssignExpr.release()));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000421}
422
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000423/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
424/// This should only be called when the current token is known to be part of
425/// simple-type-specifier.
426///
427/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000428/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000429/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
430/// char
431/// wchar_t
432/// bool
433/// short
434/// int
435/// long
436/// signed
437/// unsigned
438/// float
439/// double
440/// void
441/// [GNU] typeof-specifier
442/// [C++0x] auto [TODO]
443///
444/// type-name:
445/// class-name
446/// enum-name
447/// typedef-name
448///
449void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
450 DS.SetRangeStart(Tok.getLocation());
451 const char *PrevSpec;
452 SourceLocation Loc = Tok.getLocation();
453
454 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000455 case tok::identifier: // foo::bar
456 case tok::coloncolon: // ::foo::bar
457 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000458 default:
459 assert(0 && "Not a simple-type-specifier token!");
460 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000461
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000462 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000463 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000464 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000465 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000466 break;
467 }
468
469 // builtin types
470 case tok::kw_short:
471 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
472 break;
473 case tok::kw_long:
474 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
475 break;
476 case tok::kw_signed:
477 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
478 break;
479 case tok::kw_unsigned:
480 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
481 break;
482 case tok::kw_void:
483 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
484 break;
485 case tok::kw_char:
486 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
487 break;
488 case tok::kw_int:
489 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
490 break;
491 case tok::kw_float:
492 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
493 break;
494 case tok::kw_double:
495 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
496 break;
497 case tok::kw_wchar_t:
498 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
499 break;
500 case tok::kw_bool:
501 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
502 break;
503
504 // GNU typeof support.
505 case tok::kw_typeof:
506 ParseTypeofSpecifier(DS);
507 DS.Finish(Diags, PP.getSourceManager(), getLang());
508 return;
509 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000510 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000511 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
512 else
513 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000514 ConsumeToken();
515 DS.Finish(Diags, PP.getSourceManager(), getLang());
516}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000517
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000518/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
519/// [dcl.name]), which is a non-empty sequence of type-specifiers,
520/// e.g., "const short int". Note that the DeclSpec is *not* finished
521/// by parsing the type-specifier-seq, because these sequences are
522/// typically followed by some form of declarator. Returns true and
523/// emits diagnostics if this is not a type-specifier-seq, false
524/// otherwise.
525///
526/// type-specifier-seq: [C++ 8.1]
527/// type-specifier type-specifier-seq[opt]
528///
529bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
530 DS.SetRangeStart(Tok.getLocation());
531 const char *PrevSpec = 0;
532 int isInvalid = 0;
533
534 // Parse one or more of the type specifiers.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000535 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000536 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000537 return true;
538 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000539
Ted Kremenekb8006e52009-01-06 19:17:58 +0000540 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000541
542 return false;
543}
544
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000545/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000546/// operator name (C++ [over.oper]). If successful, returns the
547/// predefined identifier that corresponds to that overloaded
548/// operator. Otherwise, returns NULL and does not consume any tokens.
549///
550/// operator-function-id: [C++ 13.5]
551/// 'operator' operator
552///
553/// operator: one of
554/// new delete new[] delete[]
555/// + - * / % ^ & | ~
556/// ! = < > += -= *= /= %=
557/// ^= &= |= << >> >>= <<= == !=
558/// <= >= && || ++ -- , ->* ->
559/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +0000560OverloadedOperatorKind
561Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000562 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +0000563 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000564
565 OverloadedOperatorKind Op = OO_None;
566 switch (NextToken().getKind()) {
567 case tok::kw_new:
568 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000569 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000570 if (Tok.is(tok::l_square)) {
571 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000572 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000573 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
574 Op = OO_Array_New;
575 } else {
576 Op = OO_New;
577 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000578 if (EndLoc)
579 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000580 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000581
582 case tok::kw_delete:
583 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000584 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000585 if (Tok.is(tok::l_square)) {
586 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000587 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000588 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
589 Op = OO_Array_Delete;
590 } else {
591 Op = OO_Delete;
592 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000593 if (EndLoc)
594 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000595 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000596
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000597#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000598 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000599#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000600#include "clang/Basic/OperatorKinds.def"
601
602 case tok::l_paren:
603 ConsumeToken(); // 'operator'
604 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +0000605 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000606 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000607 if (EndLoc)
608 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000609 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000610
611 case tok::l_square:
612 ConsumeToken(); // 'operator'
613 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000614 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000615 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000616 if (EndLoc)
617 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000618 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000619
620 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000621 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000622 }
623
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000624 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000625 Loc = ConsumeAnyToken(); // the operator itself
626 if (EndLoc)
627 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000628 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000629}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000630
631/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
632/// which expresses the name of a user-defined conversion operator
633/// (C++ [class.conv.fct]p1). Returns the type that this operator is
634/// specifying a conversion for, or NULL if there was an error.
635///
636/// conversion-function-id: [C++ 12.3.2]
637/// operator conversion-type-id
638///
639/// conversion-type-id:
640/// type-specifier-seq conversion-declarator[opt]
641///
642/// conversion-declarator:
643/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +0000644Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000645 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
646 ConsumeToken(); // 'operator'
647
648 // Parse the type-specifier-seq.
649 DeclSpec DS;
650 if (ParseCXXTypeSpecifierSeq(DS))
651 return 0;
652
653 // Parse the conversion-declarator, which is merely a sequence of
654 // ptr-operators.
655 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000656 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000657 if (EndLoc)
658 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000659
660 // Finish up the type.
661 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000662 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000663 return 0;
664 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000665 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000666}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000667
668/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
669/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000670///
671/// This method is called to parse the new expression after the optional :: has
672/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
673/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000674///
675/// new-expression:
676/// '::'[opt] 'new' new-placement[opt] new-type-id
677/// new-initializer[opt]
678/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
679/// new-initializer[opt]
680///
681/// new-placement:
682/// '(' expression-list ')'
683///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000684/// new-type-id:
685/// type-specifier-seq new-declarator[opt]
686///
687/// new-declarator:
688/// ptr-operator new-declarator[opt]
689/// direct-new-declarator
690///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000691/// new-initializer:
692/// '(' expression-list[opt] ')'
693/// [C++0x] braced-init-list [TODO]
694///
Chris Lattner59232d32009-01-04 21:25:24 +0000695Parser::OwningExprResult
696Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
697 assert(Tok.is(tok::kw_new) && "expected 'new' token");
698 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000699
700 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
701 // second form of new-expression. It can't be a new-type-id.
702
Sebastian Redla55e52c2008-11-25 22:21:31 +0000703 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000704 SourceLocation PlacementLParen, PlacementRParen;
705
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000706 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000707 DeclSpec DS;
708 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000709 if (Tok.is(tok::l_paren)) {
710 // If it turns out to be a placement, we change the type location.
711 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000712 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
713 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000714 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000715 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000716
717 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000718 if (PlacementRParen.isInvalid()) {
719 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000720 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000721 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000722
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000723 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000724 // Reset the placement locations. There was no placement.
725 PlacementLParen = PlacementRParen = SourceLocation();
726 ParenTypeId = true;
727 } else {
728 // We still need the type.
729 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000730 SourceLocation LParen = ConsumeParen();
731 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000732 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000733 ParseDeclarator(DeclaratorInfo);
734 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000735 ParenTypeId = true;
736 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000737 if (ParseCXXTypeSpecifierSeq(DS))
738 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000739 else {
740 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000741 ParseDeclaratorInternal(DeclaratorInfo,
742 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000743 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000744 ParenTypeId = false;
745 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000746 }
747 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000748 // A new-type-id is a simplified type-id, where essentially the
749 // direct-declarator is replaced by a direct-new-declarator.
750 if (ParseCXXTypeSpecifierSeq(DS))
751 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000752 else {
753 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000754 ParseDeclaratorInternal(DeclaratorInfo,
755 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000756 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000757 ParenTypeId = false;
758 }
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000759 if (DeclaratorInfo.getInvalidType()) {
760 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000761 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000762 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000763
Sebastian Redla55e52c2008-11-25 22:21:31 +0000764 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000765 SourceLocation ConstructorLParen, ConstructorRParen;
766
767 if (Tok.is(tok::l_paren)) {
768 ConstructorLParen = ConsumeParen();
769 if (Tok.isNot(tok::r_paren)) {
770 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000771 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
772 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000773 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000774 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000775 }
776 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000777 if (ConstructorRParen.isInvalid()) {
778 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000779 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000780 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000781 }
782
Sebastian Redl20df9b72008-12-11 22:51:44 +0000783 return Owned(Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
784 PlacementArgs.take(), PlacementArgs.size(),
785 PlacementRParen, ParenTypeId, DeclaratorInfo,
786 ConstructorLParen, ConstructorArgs.take(),
787 ConstructorArgs.size(), ConstructorRParen));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000788}
789
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000790/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
791/// passed to ParseDeclaratorInternal.
792///
793/// direct-new-declarator:
794/// '[' expression ']'
795/// direct-new-declarator '[' constant-expression ']'
796///
Chris Lattner59232d32009-01-04 21:25:24 +0000797void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000798 // Parse the array dimensions.
799 bool first = true;
800 while (Tok.is(tok::l_square)) {
801 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000802 OwningExprResult Size(first ? ParseExpression()
803 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000804 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000805 // Recover
806 SkipUntil(tok::r_square);
807 return;
808 }
809 first = false;
810
Sebastian Redlab197ba2009-02-09 18:23:29 +0000811 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000812 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Sebastian Redlab197ba2009-02-09 18:23:29 +0000813 Size.release(), LLoc),
814 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000815
Sebastian Redlab197ba2009-02-09 18:23:29 +0000816 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000817 return;
818 }
819}
820
821/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
822/// This ambiguity appears in the syntax of the C++ new operator.
823///
824/// new-expression:
825/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
826/// new-initializer[opt]
827///
828/// new-placement:
829/// '(' expression-list ')'
830///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000831bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +0000832 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000833 // The '(' was already consumed.
834 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000835 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000836 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000837 ParseDeclarator(D);
838 return D.getInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000839 }
840
841 // It's not a type, it has to be an expression list.
842 // Discard the comma locations - ActOnCXXNew has enough parameters.
843 CommaLocsTy CommaLocs;
844 return ParseExpressionList(PlacementArgs, CommaLocs);
845}
846
847/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
848/// to free memory allocated by new.
849///
Chris Lattner59232d32009-01-04 21:25:24 +0000850/// This method is called to parse the 'delete' expression after the optional
851/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
852/// and "Start" is its location. Otherwise, "Start" is the location of the
853/// 'delete' token.
854///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000855/// delete-expression:
856/// '::'[opt] 'delete' cast-expression
857/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000858Parser::OwningExprResult
859Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
860 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
861 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000862
863 // Array delete?
864 bool ArrayDelete = false;
865 if (Tok.is(tok::l_square)) {
866 ArrayDelete = true;
867 SourceLocation LHS = ConsumeBracket();
868 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
869 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000870 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000871 }
872
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000873 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000874 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000875 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000876
Sebastian Redl20df9b72008-12-11 22:51:44 +0000877 return Owned(Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete,
878 Operand.release()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000879}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000880
881static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
882{
883 switch(kind) {
884 default: assert(false && "Not a known unary type trait.");
885 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
886 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
887 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
888 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
889 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
890 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
891 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
892 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
893 case tok::kw___is_abstract: return UTT_IsAbstract;
894 case tok::kw___is_class: return UTT_IsClass;
895 case tok::kw___is_empty: return UTT_IsEmpty;
896 case tok::kw___is_enum: return UTT_IsEnum;
897 case tok::kw___is_pod: return UTT_IsPOD;
898 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
899 case tok::kw___is_union: return UTT_IsUnion;
900 }
901}
902
903/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
904/// pseudo-functions that allow implementation of the TR1/C++0x type traits
905/// templates.
906///
907/// primary-expression:
908/// [GNU] unary-type-trait '(' type-id ')'
909///
910Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
911{
912 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
913 SourceLocation Loc = ConsumeToken();
914
915 SourceLocation LParen = Tok.getLocation();
916 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
917 return ExprError();
918
919 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
920 // there will be cryptic errors about mismatched parentheses and missing
921 // specifiers.
922 TypeTy *Ty = ParseTypeName();
923
924 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
925
926 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty, RParen);
927}