blob: 50e6657ad89744d900d02ff28856aa63d5c34660 [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
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);
56 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
57 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
82 SS.setScopeRep(
83 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.
95 SourceLocation TemplateKWLoc;
96 if (Tok.is(tok::kw_template)) {
97 TemplateKWLoc = ConsumeToken();
98
99 if (Tok.isNot(tok::identifier)) {
100 Diag(Tok.getLocation(),
101 diag::err_id_after_template_in_nested_name_spec)
102 << SourceRange(TemplateKWLoc);
103 break;
104 }
105
106 if (NextToken().isNot(tok::less)) {
107 Diag(NextToken().getLocation(),
108 diag::err_less_after_template_name_in_nested_name_spec)
109 << Tok.getIdentifierInfo()->getName()
110 << SourceRange(TemplateKWLoc, Tok.getLocation());
111 break;
112 }
113 }
114 else {
115 // FIXME: If the nested-name-specifier thus far is dependent,
116 // we need to break out of here, because this '<' is taken as
117 // an operator and not as part of a simple-template-id.
118 }
119
120 DeclTy *Template = 0;
121 TemplateNameKind TNK = TNK_Non_template;
122 // FIXME: If the nested-name-specifier thus far is dependent,
123 // set TNK = TNK_Dependent_template_name and skip the
124 // "isTemplateName" check.
125 TNK = Actions.isTemplateName(*Tok.getIdentifierInfo(),
126 CurScope, Template, &SS);
127 if (TNK) {
128 // We have found a template name, so annotate this this token
129 // with a template-id annotation. We do not permit the
130 // template-id to be translated into a type annotation,
131 // because some clients (e.g., the parsing of class template
132 // specializations) still want to see the original template-id
133 // token.
134 AnnotateTemplateIdToken(Template, TNK, &SS, TemplateKWLoc, false);
135 continue;
136 }
137 }
138
139 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
140 // We have
141 //
142 // simple-template-id '::'
143 //
144 // So we need to check whether the simple-template-id is of the
145 // right kind (it should name a type), and then convert it into
146 // a type within the nested-name-specifier.
147 TemplateIdAnnotation *TemplateId
148 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
149
150 if (TemplateId->Kind == TNK_Class_template) {
151 if (AnnotateTemplateIdTokenAsType(&SS))
152 SS.setScopeRep(0);
153
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
167 SS.setScopeRep(
168 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
169 TypeToken.getAnnotationValue(),
170 TypeToken.getAnnotationRange(),
171 CCLoc));
172 SS.setEndLoc(CCLoc);
173 continue;
174 } else
175 assert(false && "FIXME: Only class template names supported here");
176 }
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,
Douglas Gregor809070a2009-02-18 17:45:20 +0000331 LAngleBracketLoc, CastTy.get(),
332 RAngleBracketLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000333 LParenLoc, Result.release(), 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 Redl20df9b72008-12-11 22:51:44 +0000393 return Owned(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 Redl20df9b72008-12-11 22:51:44 +0000414 return Owned(Actions.ActOnCXXThrow(ThrowLoc));
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);
419 return Owned(Actions.ActOnCXXThrow(ThrowLoc, Expr.release()));
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 Redl20df9b72008-12-11 22:51:44 +0000431 return Owned(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 Redl20df9b72008-12-11 22:51:44 +0000466 return Owned(Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
467 LParenLoc,
468 Exprs.take(), Exprs.size(),
469 &CommaLocs[0], RParenLoc));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000470}
471
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000472/// ParseCXXCondition - if/switch/while/for condition expression.
473///
474/// condition:
475/// expression
476/// type-specifier-seq declarator '=' assignment-expression
477/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
478/// '=' assignment-expression
479///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000480Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000481 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000482 return ParseExpression(); // expression
483
484 SourceLocation StartLoc = Tok.getLocation();
485
486 // type-specifier-seq
487 DeclSpec DS;
488 ParseSpecifierQualifierList(DS);
489
490 // declarator
491 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
492 ParseDeclarator(DeclaratorInfo);
493
494 // simple-asm-expr[opt]
495 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000496 SourceLocation Loc;
497 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000498 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000499 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000500 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000501 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000502 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000503 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000504 }
505
506 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000507 if (Tok.is(tok::kw___attribute)) {
508 SourceLocation Loc;
509 AttributeList *AttrList = ParseAttributes(&Loc);
510 DeclaratorInfo.AddAttributes(AttrList, Loc);
511 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000512
513 // '=' assignment-expression
514 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000515 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000516 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000517 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000518 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000519 return ExprError();
520
521 return Owned(Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
522 DeclaratorInfo,EqualLoc,
523 AssignExpr.release()));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000524}
525
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000526/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
527/// This should only be called when the current token is known to be part of
528/// simple-type-specifier.
529///
530/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000531/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000532/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
533/// char
534/// wchar_t
535/// bool
536/// short
537/// int
538/// long
539/// signed
540/// unsigned
541/// float
542/// double
543/// void
544/// [GNU] typeof-specifier
545/// [C++0x] auto [TODO]
546///
547/// type-name:
548/// class-name
549/// enum-name
550/// typedef-name
551///
552void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
553 DS.SetRangeStart(Tok.getLocation());
554 const char *PrevSpec;
555 SourceLocation Loc = Tok.getLocation();
556
557 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000558 case tok::identifier: // foo::bar
559 case tok::coloncolon: // ::foo::bar
560 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000561 default:
562 assert(0 && "Not a simple-type-specifier token!");
563 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000564
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000565 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000566 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000567 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000568 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000569 break;
570 }
571
572 // builtin types
573 case tok::kw_short:
574 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
575 break;
576 case tok::kw_long:
577 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
578 break;
579 case tok::kw_signed:
580 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
581 break;
582 case tok::kw_unsigned:
583 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
584 break;
585 case tok::kw_void:
586 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
587 break;
588 case tok::kw_char:
589 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
590 break;
591 case tok::kw_int:
592 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
593 break;
594 case tok::kw_float:
595 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
596 break;
597 case tok::kw_double:
598 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
599 break;
600 case tok::kw_wchar_t:
601 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
602 break;
603 case tok::kw_bool:
604 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
605 break;
606
607 // GNU typeof support.
608 case tok::kw_typeof:
609 ParseTypeofSpecifier(DS);
610 DS.Finish(Diags, PP.getSourceManager(), getLang());
611 return;
612 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000613 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000614 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
615 else
616 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000617 ConsumeToken();
618 DS.Finish(Diags, PP.getSourceManager(), getLang());
619}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000620
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000621/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
622/// [dcl.name]), which is a non-empty sequence of type-specifiers,
623/// e.g., "const short int". Note that the DeclSpec is *not* finished
624/// by parsing the type-specifier-seq, because these sequences are
625/// typically followed by some form of declarator. Returns true and
626/// emits diagnostics if this is not a type-specifier-seq, false
627/// otherwise.
628///
629/// type-specifier-seq: [C++ 8.1]
630/// type-specifier type-specifier-seq[opt]
631///
632bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
633 DS.SetRangeStart(Tok.getLocation());
634 const char *PrevSpec = 0;
635 int isInvalid = 0;
636
637 // Parse one or more of the type specifiers.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000638 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000639 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000640 return true;
641 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000642
Ted Kremenekb8006e52009-01-06 19:17:58 +0000643 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000644
645 return false;
646}
647
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000648/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000649/// operator name (C++ [over.oper]). If successful, returns the
650/// predefined identifier that corresponds to that overloaded
651/// operator. Otherwise, returns NULL and does not consume any tokens.
652///
653/// operator-function-id: [C++ 13.5]
654/// 'operator' operator
655///
656/// operator: one of
657/// new delete new[] delete[]
658/// + - * / % ^ & | ~
659/// ! = < > += -= *= /= %=
660/// ^= &= |= << >> >>= <<= == !=
661/// <= >= && || ++ -- , ->* ->
662/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +0000663OverloadedOperatorKind
664Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000665 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +0000666 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000667
668 OverloadedOperatorKind Op = OO_None;
669 switch (NextToken().getKind()) {
670 case tok::kw_new:
671 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000672 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000673 if (Tok.is(tok::l_square)) {
674 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000675 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000676 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
677 Op = OO_Array_New;
678 } else {
679 Op = OO_New;
680 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000681 if (EndLoc)
682 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000683 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000684
685 case tok::kw_delete:
686 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000687 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000688 if (Tok.is(tok::l_square)) {
689 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000690 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000691 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
692 Op = OO_Array_Delete;
693 } else {
694 Op = OO_Delete;
695 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000696 if (EndLoc)
697 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000698 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000699
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000700#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000701 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000702#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000703#include "clang/Basic/OperatorKinds.def"
704
705 case tok::l_paren:
706 ConsumeToken(); // 'operator'
707 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +0000708 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000709 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000710 if (EndLoc)
711 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000712 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000713
714 case tok::l_square:
715 ConsumeToken(); // 'operator'
716 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000717 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000718 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000719 if (EndLoc)
720 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000721 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000722
723 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000724 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000725 }
726
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000727 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000728 Loc = ConsumeAnyToken(); // the operator itself
729 if (EndLoc)
730 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000731 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000732}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000733
734/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
735/// which expresses the name of a user-defined conversion operator
736/// (C++ [class.conv.fct]p1). Returns the type that this operator is
737/// specifying a conversion for, or NULL if there was an error.
738///
739/// conversion-function-id: [C++ 12.3.2]
740/// operator conversion-type-id
741///
742/// conversion-type-id:
743/// type-specifier-seq conversion-declarator[opt]
744///
745/// conversion-declarator:
746/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +0000747Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000748 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
749 ConsumeToken(); // 'operator'
750
751 // Parse the type-specifier-seq.
752 DeclSpec DS;
753 if (ParseCXXTypeSpecifierSeq(DS))
754 return 0;
755
756 // Parse the conversion-declarator, which is merely a sequence of
757 // ptr-operators.
758 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000759 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000760 if (EndLoc)
761 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000762
763 // Finish up the type.
764 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000765 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000766 return 0;
767 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000768 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000769}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000770
771/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
772/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000773///
774/// This method is called to parse the new expression after the optional :: has
775/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
776/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000777///
778/// new-expression:
779/// '::'[opt] 'new' new-placement[opt] new-type-id
780/// new-initializer[opt]
781/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
782/// new-initializer[opt]
783///
784/// new-placement:
785/// '(' expression-list ')'
786///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000787/// new-type-id:
788/// type-specifier-seq new-declarator[opt]
789///
790/// new-declarator:
791/// ptr-operator new-declarator[opt]
792/// direct-new-declarator
793///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000794/// new-initializer:
795/// '(' expression-list[opt] ')'
796/// [C++0x] braced-init-list [TODO]
797///
Chris Lattner59232d32009-01-04 21:25:24 +0000798Parser::OwningExprResult
799Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
800 assert(Tok.is(tok::kw_new) && "expected 'new' token");
801 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000802
803 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
804 // second form of new-expression. It can't be a new-type-id.
805
Sebastian Redla55e52c2008-11-25 22:21:31 +0000806 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000807 SourceLocation PlacementLParen, PlacementRParen;
808
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000809 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000810 DeclSpec DS;
811 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000812 if (Tok.is(tok::l_paren)) {
813 // If it turns out to be a placement, we change the type location.
814 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000815 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
816 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000817 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000818 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000819
820 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000821 if (PlacementRParen.isInvalid()) {
822 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000823 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000824 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000825
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000826 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000827 // Reset the placement locations. There was no placement.
828 PlacementLParen = PlacementRParen = SourceLocation();
829 ParenTypeId = true;
830 } else {
831 // We still need the type.
832 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000833 SourceLocation LParen = ConsumeParen();
834 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000835 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000836 ParseDeclarator(DeclaratorInfo);
837 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000838 ParenTypeId = true;
839 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000840 if (ParseCXXTypeSpecifierSeq(DS))
841 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000842 else {
843 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000844 ParseDeclaratorInternal(DeclaratorInfo,
845 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000846 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000847 ParenTypeId = false;
848 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000849 }
850 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000851 // A new-type-id is a simplified type-id, where essentially the
852 // direct-declarator is replaced by a direct-new-declarator.
853 if (ParseCXXTypeSpecifierSeq(DS))
854 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000855 else {
856 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000857 ParseDeclaratorInternal(DeclaratorInfo,
858 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000859 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000860 ParenTypeId = false;
861 }
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000862 if (DeclaratorInfo.getInvalidType()) {
863 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000864 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000865 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000866
Sebastian Redla55e52c2008-11-25 22:21:31 +0000867 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000868 SourceLocation ConstructorLParen, ConstructorRParen;
869
870 if (Tok.is(tok::l_paren)) {
871 ConstructorLParen = ConsumeParen();
872 if (Tok.isNot(tok::r_paren)) {
873 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000874 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
875 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000876 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000877 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000878 }
879 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000880 if (ConstructorRParen.isInvalid()) {
881 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000882 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000883 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000884 }
885
Sebastian Redl20df9b72008-12-11 22:51:44 +0000886 return Owned(Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
887 PlacementArgs.take(), PlacementArgs.size(),
888 PlacementRParen, ParenTypeId, DeclaratorInfo,
889 ConstructorLParen, ConstructorArgs.take(),
890 ConstructorArgs.size(), ConstructorRParen));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000891}
892
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000893/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
894/// passed to ParseDeclaratorInternal.
895///
896/// direct-new-declarator:
897/// '[' expression ']'
898/// direct-new-declarator '[' constant-expression ']'
899///
Chris Lattner59232d32009-01-04 21:25:24 +0000900void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000901 // Parse the array dimensions.
902 bool first = true;
903 while (Tok.is(tok::l_square)) {
904 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000905 OwningExprResult Size(first ? ParseExpression()
906 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000907 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000908 // Recover
909 SkipUntil(tok::r_square);
910 return;
911 }
912 first = false;
913
Sebastian Redlab197ba2009-02-09 18:23:29 +0000914 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000915 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Sebastian Redlab197ba2009-02-09 18:23:29 +0000916 Size.release(), LLoc),
917 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000918
Sebastian Redlab197ba2009-02-09 18:23:29 +0000919 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000920 return;
921 }
922}
923
924/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
925/// This ambiguity appears in the syntax of the C++ new operator.
926///
927/// new-expression:
928/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
929/// new-initializer[opt]
930///
931/// new-placement:
932/// '(' expression-list ')'
933///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000934bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +0000935 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000936 // The '(' was already consumed.
937 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000938 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000939 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000940 ParseDeclarator(D);
941 return D.getInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000942 }
943
944 // It's not a type, it has to be an expression list.
945 // Discard the comma locations - ActOnCXXNew has enough parameters.
946 CommaLocsTy CommaLocs;
947 return ParseExpressionList(PlacementArgs, CommaLocs);
948}
949
950/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
951/// to free memory allocated by new.
952///
Chris Lattner59232d32009-01-04 21:25:24 +0000953/// This method is called to parse the 'delete' expression after the optional
954/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
955/// and "Start" is its location. Otherwise, "Start" is the location of the
956/// 'delete' token.
957///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000958/// delete-expression:
959/// '::'[opt] 'delete' cast-expression
960/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000961Parser::OwningExprResult
962Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
963 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
964 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000965
966 // Array delete?
967 bool ArrayDelete = false;
968 if (Tok.is(tok::l_square)) {
969 ArrayDelete = true;
970 SourceLocation LHS = ConsumeBracket();
971 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
972 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000973 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000974 }
975
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000976 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000977 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000978 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000979
Sebastian Redl20df9b72008-12-11 22:51:44 +0000980 return Owned(Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete,
981 Operand.release()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000982}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000983
984static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
985{
986 switch(kind) {
987 default: assert(false && "Not a known unary type trait.");
988 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
989 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
990 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
991 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
992 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
993 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
994 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
995 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
996 case tok::kw___is_abstract: return UTT_IsAbstract;
997 case tok::kw___is_class: return UTT_IsClass;
998 case tok::kw___is_empty: return UTT_IsEmpty;
999 case tok::kw___is_enum: return UTT_IsEnum;
1000 case tok::kw___is_pod: return UTT_IsPOD;
1001 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1002 case tok::kw___is_union: return UTT_IsUnion;
1003 }
1004}
1005
1006/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1007/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1008/// templates.
1009///
1010/// primary-expression:
1011/// [GNU] unary-type-trait '(' type-id ')'
1012///
1013Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
1014{
1015 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1016 SourceLocation Loc = ConsumeToken();
1017
1018 SourceLocation LParen = Tok.getLocation();
1019 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1020 return ExprError();
1021
1022 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1023 // there will be cryptic errors about mismatched parentheses and missing
1024 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001025 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001026
1027 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1028
Douglas Gregor809070a2009-02-18 17:45:20 +00001029 if (Ty.isInvalid())
1030 return ExprError();
1031
1032 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001033}