blob: 7c90a6319ce85916d6aa44ca06fd3a61fba139ca [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017using namespace clang;
18
Chris Lattner7a0ab5f2009-01-06 06:59:53 +000019/// ParseOptionalCXXScopeSpecifier - Parse global scope or
20/// nested-name-specifier if present. Returns true if a nested-name-specifier
21/// was parsed from the token stream. Note that this routine will not parse
22/// ::new or ::delete, it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000023///
24/// '::'[opt] nested-name-specifier
25/// '::'
26///
27/// nested-name-specifier:
28/// type-name '::'
29/// namespace-name '::'
30/// nested-name-specifier identifier '::'
31/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
32///
Chris Lattner7a0ab5f2009-01-06 06:59:53 +000033bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000034 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000035 "Call sites of this function should be guarded by checking for C++");
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000036
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000037 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000038 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000039 SS.setRange(Tok.getAnnotationRange());
40 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000041 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000042 }
Chris Lattnere607e802009-01-04 21:14:15 +000043
Douglas Gregor39a8de12009-02-25 19:37:18 +000044 bool HasScopeSpecifier = false;
45
Chris Lattner5b454732009-01-05 03:55:46 +000046 if (Tok.is(tok::coloncolon)) {
47 // ::new and ::delete aren't nested-name-specifiers.
48 tok::TokenKind NextKind = NextToken().getKind();
49 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
50 return false;
Chris Lattner55a7cef2009-01-05 00:13:00 +000051
Chris Lattner55a7cef2009-01-05 00:13:00 +000052 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000053 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000054 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000055 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000056 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000057 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000058 }
59
Douglas Gregor39a8de12009-02-25 19:37:18 +000060 while (true) {
61 // nested-name-specifier:
62 // type-name '::'
63 // namespace-name '::'
64 // nested-name-specifier identifier '::'
65 if (Tok.is(tok::identifier) && NextToken().is(tok::coloncolon)) {
66 // We have an identifier followed by a '::'. Lookup this name
67 // as the name in a nested-name-specifier.
68 IdentifierInfo *II = Tok.getIdentifierInfo();
69 SourceLocation IdLoc = ConsumeToken();
70 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
71 SourceLocation CCLoc = ConsumeToken();
72
73 if (!HasScopeSpecifier) {
74 SS.setBeginLoc(IdLoc);
75 HasScopeSpecifier = true;
76 }
77
78 if (SS.isInvalid())
79 continue;
80
Douglas Gregor35073692009-03-26 23:56:24 +000081 SS.setScopeRep(
Douglas Gregor39a8de12009-02-25 19:37:18 +000082 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, *II));
83 SS.setEndLoc(CCLoc);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000084 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +000085 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000086
Douglas Gregor39a8de12009-02-25 19:37:18 +000087 // nested-name-specifier:
88 // type-name '::'
89 // nested-name-specifier 'template'[opt] simple-template-id '::'
90 if ((Tok.is(tok::identifier) && NextToken().is(tok::less)) ||
91 Tok.is(tok::kw_template)) {
92 // Parse the optional 'template' keyword, then make sure we have
93 // 'identifier <' after it.
Douglas Gregor39a8de12009-02-25 19:37:18 +000094 if (Tok.is(tok::kw_template)) {
Douglas Gregorc45c2322009-03-31 00:43:58 +000095 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +000096
97 if (Tok.isNot(tok::identifier)) {
98 Diag(Tok.getLocation(),
99 diag::err_id_after_template_in_nested_name_spec)
100 << SourceRange(TemplateKWLoc);
101 break;
102 }
103
104 if (NextToken().isNot(tok::less)) {
105 Diag(NextToken().getLocation(),
106 diag::err_less_after_template_name_in_nested_name_spec)
107 << Tok.getIdentifierInfo()->getName()
108 << SourceRange(TemplateKWLoc, Tok.getLocation());
109 break;
110 }
Douglas Gregorc45c2322009-03-31 00:43:58 +0000111
112 TemplateTy Template
113 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
114 *Tok.getIdentifierInfo(),
115 Tok.getLocation(),
116 SS);
117 AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
118 &SS, TemplateKWLoc, false);
119 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000120 }
121
Douglas Gregor7532dc62009-03-30 22:58:21 +0000122 TemplateTy Template;
Douglas Gregorc45c2322009-03-31 00:43:58 +0000123 TemplateNameKind TNK = Actions.isTemplateName(*Tok.getIdentifierInfo(),
124 CurScope, Template, &SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000125 if (TNK) {
126 // We have found a template name, so annotate this this token
127 // with a template-id annotation. We do not permit the
128 // template-id to be translated into a type annotation,
129 // because some clients (e.g., the parsing of class template
130 // specializations) still want to see the original template-id
131 // token.
Douglas Gregorc45c2322009-03-31 00:43:58 +0000132 AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(), false);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000133 continue;
134 }
135 }
136
137 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
138 // We have
139 //
140 // simple-template-id '::'
141 //
142 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000143 // right kind (it should name a type or be dependent), and then
144 // convert it into a type within the nested-name-specifier.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000145 TemplateIdAnnotation *TemplateId
146 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
147
Douglas Gregorc45c2322009-03-31 00:43:58 +0000148 if (TemplateId->Kind == TNK_Type_template ||
149 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000150 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000151 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000152
153 assert(Tok.is(tok::annot_typename) &&
154 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000155 Token TypeToken = Tok;
156 ConsumeToken();
157 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
158 SourceLocation CCLoc = ConsumeToken();
159
160 if (!HasScopeSpecifier) {
161 SS.setBeginLoc(TypeToken.getLocation());
162 HasScopeSpecifier = true;
163 }
Douglas Gregor31a19b62009-04-01 21:51:26 +0000164
165 if (TypeToken.getAnnotationValue())
166 SS.setScopeRep(
167 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
168 TypeToken.getAnnotationValue(),
169 TypeToken.getAnnotationRange(),
170 CCLoc));
171 else
172 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000173 SS.setEndLoc(CCLoc);
174 continue;
175 } else
Douglas Gregorc45c2322009-03-31 00:43:58 +0000176 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000177 }
178
179 // We don't have any tokens that form the beginning of a
180 // nested-name-specifier, so we're done.
181 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000182 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000183
184 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000185}
186
187/// ParseCXXIdExpression - Handle id-expression.
188///
189/// id-expression:
190/// unqualified-id
191/// qualified-id
192///
193/// unqualified-id:
194/// identifier
195/// operator-function-id
196/// conversion-function-id [TODO]
197/// '~' class-name [TODO]
198/// template-id [TODO]
199///
200/// qualified-id:
201/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
202/// '::' identifier
203/// '::' operator-function-id
204/// '::' template-id [TODO]
205///
206/// nested-name-specifier:
207/// type-name '::'
208/// namespace-name '::'
209/// nested-name-specifier identifier '::'
210/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
211///
212/// NOTE: The standard specifies that, for qualified-id, the parser does not
213/// expect:
214///
215/// '::' conversion-function-id
216/// '::' '~' class-name
217///
218/// This may cause a slight inconsistency on diagnostics:
219///
220/// class C {};
221/// namespace A {}
222/// void f() {
223/// :: A :: ~ C(); // Some Sema error about using destructor with a
224/// // namespace.
225/// :: ~ C(); // Some Parser error like 'unexpected ~'.
226/// }
227///
228/// We simplify the parser a bit and make it work like:
229///
230/// qualified-id:
231/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
232/// '::' unqualified-id
233///
234/// That way Sema can handle and report similar errors for namespaces and the
235/// global scope.
236///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000237/// The isAddressOfOperand parameter indicates that this id-expression is a
238/// direct operand of the address-of operator. This is, besides member contexts,
239/// the only place where a qualified-id naming a non-static class member may
240/// appear.
241///
242Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000243 // qualified-id:
244 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
245 // '::' unqualified-id
246 //
247 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000248 ParseOptionalCXXScopeSpecifier(SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000249
250 // unqualified-id:
251 // identifier
252 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000253 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000254 // '~' class-name [TODO]
255 // template-id [TODO]
256 //
257 switch (Tok.getKind()) {
258 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000259 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000260
261 case tok::identifier: {
262 // Consume the identifier so that we can see if it is followed by a '('.
263 IdentifierInfo &II = *Tok.getIdentifierInfo();
264 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000265 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
266 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000267 }
268
269 case tok::kw_operator: {
270 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000271 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000272 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000273 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
274 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000275 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000276 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000277 Tok.is(tok::l_paren), SS,
278 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000279
Douglas Gregor2def4832008-11-17 20:34:05 +0000280 // We already complained about a bad conversion-function-id,
281 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000282 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000283 }
284
285 } // switch.
286
287 assert(0 && "The switch was supposed to take care everything.");
288}
289
Reid Spencer5f016e22007-07-11 17:01:13 +0000290/// ParseCXXCasts - This handles the various ways to cast expressions to another
291/// type.
292///
293/// postfix-expression: [C++ 5.2p1]
294/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
295/// 'static_cast' '<' type-name '>' '(' expression ')'
296/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
297/// 'const_cast' '<' type-name '>' '(' expression ')'
298///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000299Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 tok::TokenKind Kind = Tok.getKind();
301 const char *CastName = 0; // For error messages
302
303 switch (Kind) {
304 default: assert(0 && "Unknown C++ cast!"); abort();
305 case tok::kw_const_cast: CastName = "const_cast"; break;
306 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
307 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
308 case tok::kw_static_cast: CastName = "static_cast"; break;
309 }
310
311 SourceLocation OpLoc = ConsumeToken();
312 SourceLocation LAngleBracketLoc = Tok.getLocation();
313
314 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000315 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000316
Douglas Gregor809070a2009-02-18 17:45:20 +0000317 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 SourceLocation RAngleBracketLoc = Tok.getLocation();
319
Chris Lattner1ab3b962008-11-18 07:48:38 +0000320 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000321 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000322
323 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
324
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000325 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
326 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000327
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000328 OwningExprResult Result = ParseExpression();
329
330 // Match the ')'.
331 if (Result.isInvalid())
332 SkipUntil(tok::r_paren);
333
334 if (Tok.is(tok::r_paren))
335 RParenLoc = ConsumeParen();
336 else
337 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000338
Douglas Gregor809070a2009-02-18 17:45:20 +0000339 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000340 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000341 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000342 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000343 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000344
Sebastian Redl20df9b72008-12-11 22:51:44 +0000345 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000346}
347
Sebastian Redlc42e1182008-11-11 11:37:55 +0000348/// ParseCXXTypeid - This handles the C++ typeid expression.
349///
350/// postfix-expression: [C++ 5.2p1]
351/// 'typeid' '(' expression ')'
352/// 'typeid' '(' type-id ')'
353///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000354Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000355 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
356
357 SourceLocation OpLoc = ConsumeToken();
358 SourceLocation LParenLoc = Tok.getLocation();
359 SourceLocation RParenLoc;
360
361 // typeid expressions are always parenthesized.
362 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
363 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000364 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000365
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000366 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000367
368 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000369 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000370
371 // Match the ')'.
372 MatchRHSPunctuation(tok::r_paren, LParenLoc);
373
Douglas Gregor809070a2009-02-18 17:45:20 +0000374 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000375 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000376
377 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000378 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000379 } else {
380 Result = ParseExpression();
381
382 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000383 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000384 SkipUntil(tok::r_paren);
385 else {
386 MatchRHSPunctuation(tok::r_paren, LParenLoc);
387
388 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000389 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000390 }
391 }
392
Sebastian Redl20df9b72008-12-11 22:51:44 +0000393 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000394}
395
Reid Spencer5f016e22007-07-11 17:01:13 +0000396/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
397///
398/// boolean-literal: [C++ 2.13.5]
399/// 'true'
400/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000401Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000403 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000404}
Chris Lattner50dd2892008-02-26 00:51:44 +0000405
406/// ParseThrowExpression - This handles the C++ throw expression.
407///
408/// throw-expression: [C++ 15]
409/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000410Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000411 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000412 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000413
Chris Lattner2a2819a2008-04-06 06:02:23 +0000414 // If the current token isn't the start of an assignment-expression,
415 // then the expression is not present. This handles things like:
416 // "C ? throw : (void)42", which is crazy but legal.
417 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
418 case tok::semi:
419 case tok::r_paren:
420 case tok::r_square:
421 case tok::r_brace:
422 case tok::colon:
423 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000424 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000425
Chris Lattner2a2819a2008-04-06 06:02:23 +0000426 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000427 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000428 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000429 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000430 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000431}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000432
433/// ParseCXXThis - This handles the C++ 'this' pointer.
434///
435/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
436/// a non-lvalue expression whose value is the address of the object for which
437/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000438Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000439 assert(Tok.is(tok::kw_this) && "Not 'this'!");
440 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000441 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000442}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000443
444/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
445/// Can be interpreted either as function-style casting ("int(x)")
446/// or class type construction ("ClassType(x,y,z)")
447/// or creation of a value-initialized type ("int()").
448///
449/// postfix-expression: [C++ 5.2p1]
450/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
451/// typename-specifier '(' expression-list[opt] ')' [TODO]
452///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000453Parser::OwningExprResult
454Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000455 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000456 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000457
458 assert(Tok.is(tok::l_paren) && "Expected '('!");
459 SourceLocation LParenLoc = ConsumeParen();
460
Sebastian Redla55e52c2008-11-25 22:21:31 +0000461 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000462 CommaLocsTy CommaLocs;
463
464 if (Tok.isNot(tok::r_paren)) {
465 if (ParseExpressionList(Exprs, CommaLocs)) {
466 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000467 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000468 }
469 }
470
471 // Match the ')'.
472 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
473
474 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
475 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000476 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
477 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000478 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000479}
480
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000481/// ParseCXXCondition - if/switch/while/for condition expression.
482///
483/// condition:
484/// expression
485/// type-specifier-seq declarator '=' assignment-expression
486/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
487/// '=' assignment-expression
488///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000489Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000490 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000491 return ParseExpression(); // expression
492
493 SourceLocation StartLoc = Tok.getLocation();
494
495 // type-specifier-seq
496 DeclSpec DS;
497 ParseSpecifierQualifierList(DS);
498
499 // declarator
500 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
501 ParseDeclarator(DeclaratorInfo);
502
503 // simple-asm-expr[opt]
504 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000505 SourceLocation Loc;
506 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000507 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000508 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000509 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000510 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000511 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000512 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000513 }
514
515 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000516 if (Tok.is(tok::kw___attribute)) {
517 SourceLocation Loc;
518 AttributeList *AttrList = ParseAttributes(&Loc);
519 DeclaratorInfo.AddAttributes(AttrList, Loc);
520 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000521
522 // '=' assignment-expression
523 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000524 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000525 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000526 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000527 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000528 return ExprError();
529
Sebastian Redlf53597f2009-03-15 17:47:39 +0000530 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
531 DeclaratorInfo,EqualLoc,
532 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000533}
534
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000535/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
536/// This should only be called when the current token is known to be part of
537/// simple-type-specifier.
538///
539/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000540/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000541/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
542/// char
543/// wchar_t
544/// bool
545/// short
546/// int
547/// long
548/// signed
549/// unsigned
550/// float
551/// double
552/// void
553/// [GNU] typeof-specifier
554/// [C++0x] auto [TODO]
555///
556/// type-name:
557/// class-name
558/// enum-name
559/// typedef-name
560///
561void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
562 DS.SetRangeStart(Tok.getLocation());
563 const char *PrevSpec;
564 SourceLocation Loc = Tok.getLocation();
565
566 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000567 case tok::identifier: // foo::bar
568 case tok::coloncolon: // ::foo::bar
569 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000570 default:
571 assert(0 && "Not a simple-type-specifier token!");
572 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000573
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000574 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000575 case tok::annot_typename: {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000576 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000577 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000578 break;
579 }
580
581 // builtin types
582 case tok::kw_short:
583 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
584 break;
585 case tok::kw_long:
586 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
587 break;
588 case tok::kw_signed:
589 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
590 break;
591 case tok::kw_unsigned:
592 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
593 break;
594 case tok::kw_void:
595 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
596 break;
597 case tok::kw_char:
598 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
599 break;
600 case tok::kw_int:
601 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
602 break;
603 case tok::kw_float:
604 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
605 break;
606 case tok::kw_double:
607 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
608 break;
609 case tok::kw_wchar_t:
610 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
611 break;
612 case tok::kw_bool:
613 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
614 break;
615
616 // GNU typeof support.
617 case tok::kw_typeof:
618 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000619 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000620 return;
621 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000622 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000623 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
624 else
625 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000626 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000627 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000628}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000629
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000630/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
631/// [dcl.name]), which is a non-empty sequence of type-specifiers,
632/// e.g., "const short int". Note that the DeclSpec is *not* finished
633/// by parsing the type-specifier-seq, because these sequences are
634/// typically followed by some form of declarator. Returns true and
635/// emits diagnostics if this is not a type-specifier-seq, false
636/// otherwise.
637///
638/// type-specifier-seq: [C++ 8.1]
639/// type-specifier type-specifier-seq[opt]
640///
641bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
642 DS.SetRangeStart(Tok.getLocation());
643 const char *PrevSpec = 0;
644 int isInvalid = 0;
645
646 // Parse one or more of the type specifiers.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000647 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000648 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000649 return true;
650 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000651
Ted Kremenekb8006e52009-01-06 19:17:58 +0000652 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000653
654 return false;
655}
656
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000657/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000658/// operator name (C++ [over.oper]). If successful, returns the
659/// predefined identifier that corresponds to that overloaded
660/// operator. Otherwise, returns NULL and does not consume any tokens.
661///
662/// operator-function-id: [C++ 13.5]
663/// 'operator' operator
664///
665/// operator: one of
666/// new delete new[] delete[]
667/// + - * / % ^ & | ~
668/// ! = < > += -= *= /= %=
669/// ^= &= |= << >> >>= <<= == !=
670/// <= >= && || ++ -- , ->* ->
671/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +0000672OverloadedOperatorKind
673Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000674 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +0000675 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000676
677 OverloadedOperatorKind Op = OO_None;
678 switch (NextToken().getKind()) {
679 case tok::kw_new:
680 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000681 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000682 if (Tok.is(tok::l_square)) {
683 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000684 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000685 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
686 Op = OO_Array_New;
687 } else {
688 Op = OO_New;
689 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000690 if (EndLoc)
691 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000692 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000693
694 case tok::kw_delete:
695 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000696 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000697 if (Tok.is(tok::l_square)) {
698 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000699 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000700 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
701 Op = OO_Array_Delete;
702 } else {
703 Op = OO_Delete;
704 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000705 if (EndLoc)
706 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000707 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000708
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000709#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000710 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000711#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000712#include "clang/Basic/OperatorKinds.def"
713
714 case tok::l_paren:
715 ConsumeToken(); // 'operator'
716 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +0000717 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000718 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000719 if (EndLoc)
720 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000721 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000722
723 case tok::l_square:
724 ConsumeToken(); // 'operator'
725 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000726 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000727 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000728 if (EndLoc)
729 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000730 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000731
732 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000733 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000734 }
735
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000736 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000737 Loc = ConsumeAnyToken(); // the operator itself
738 if (EndLoc)
739 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000740 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000741}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000742
743/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
744/// which expresses the name of a user-defined conversion operator
745/// (C++ [class.conv.fct]p1). Returns the type that this operator is
746/// specifying a conversion for, or NULL if there was an error.
747///
748/// conversion-function-id: [C++ 12.3.2]
749/// operator conversion-type-id
750///
751/// conversion-type-id:
752/// type-specifier-seq conversion-declarator[opt]
753///
754/// conversion-declarator:
755/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +0000756Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000757 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
758 ConsumeToken(); // 'operator'
759
760 // Parse the type-specifier-seq.
761 DeclSpec DS;
762 if (ParseCXXTypeSpecifierSeq(DS))
763 return 0;
764
765 // Parse the conversion-declarator, which is merely a sequence of
766 // ptr-operators.
767 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000768 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000769 if (EndLoc)
770 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000771
772 // Finish up the type.
773 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000774 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000775 return 0;
776 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000777 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000778}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000779
780/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
781/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000782///
783/// This method is called to parse the new expression after the optional :: has
784/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
785/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000786///
787/// new-expression:
788/// '::'[opt] 'new' new-placement[opt] new-type-id
789/// new-initializer[opt]
790/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
791/// new-initializer[opt]
792///
793/// new-placement:
794/// '(' expression-list ')'
795///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000796/// new-type-id:
797/// type-specifier-seq new-declarator[opt]
798///
799/// new-declarator:
800/// ptr-operator new-declarator[opt]
801/// direct-new-declarator
802///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000803/// new-initializer:
804/// '(' expression-list[opt] ')'
805/// [C++0x] braced-init-list [TODO]
806///
Chris Lattner59232d32009-01-04 21:25:24 +0000807Parser::OwningExprResult
808Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
809 assert(Tok.is(tok::kw_new) && "expected 'new' token");
810 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000811
812 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
813 // second form of new-expression. It can't be a new-type-id.
814
Sebastian Redla55e52c2008-11-25 22:21:31 +0000815 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000816 SourceLocation PlacementLParen, PlacementRParen;
817
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000818 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000819 DeclSpec DS;
820 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000821 if (Tok.is(tok::l_paren)) {
822 // If it turns out to be a placement, we change the type location.
823 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000824 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
825 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000826 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000827 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000828
829 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000830 if (PlacementRParen.isInvalid()) {
831 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000832 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000833 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000834
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000835 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000836 // Reset the placement locations. There was no placement.
837 PlacementLParen = PlacementRParen = SourceLocation();
838 ParenTypeId = true;
839 } else {
840 // We still need the type.
841 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000842 SourceLocation LParen = ConsumeParen();
843 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000844 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000845 ParseDeclarator(DeclaratorInfo);
846 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000847 ParenTypeId = true;
848 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000849 if (ParseCXXTypeSpecifierSeq(DS))
850 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000851 else {
852 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000853 ParseDeclaratorInternal(DeclaratorInfo,
854 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000855 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000856 ParenTypeId = false;
857 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000858 }
859 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000860 // A new-type-id is a simplified type-id, where essentially the
861 // direct-declarator is replaced by a direct-new-declarator.
862 if (ParseCXXTypeSpecifierSeq(DS))
863 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000864 else {
865 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000866 ParseDeclaratorInternal(DeclaratorInfo,
867 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000868 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000869 ParenTypeId = false;
870 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000871 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000872 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000873 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000874 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000875
Sebastian Redla55e52c2008-11-25 22:21:31 +0000876 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000877 SourceLocation ConstructorLParen, ConstructorRParen;
878
879 if (Tok.is(tok::l_paren)) {
880 ConstructorLParen = ConsumeParen();
881 if (Tok.isNot(tok::r_paren)) {
882 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000883 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
884 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000885 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000886 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000887 }
888 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000889 if (ConstructorRParen.isInvalid()) {
890 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000891 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000892 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000893 }
894
Sebastian Redlf53597f2009-03-15 17:47:39 +0000895 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
896 move_arg(PlacementArgs), PlacementRParen,
897 ParenTypeId, DeclaratorInfo, ConstructorLParen,
898 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000899}
900
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000901/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
902/// passed to ParseDeclaratorInternal.
903///
904/// direct-new-declarator:
905/// '[' expression ']'
906/// direct-new-declarator '[' constant-expression ']'
907///
Chris Lattner59232d32009-01-04 21:25:24 +0000908void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000909 // Parse the array dimensions.
910 bool first = true;
911 while (Tok.is(tok::l_square)) {
912 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000913 OwningExprResult Size(first ? ParseExpression()
914 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000915 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000916 // Recover
917 SkipUntil(tok::r_square);
918 return;
919 }
920 first = false;
921
Sebastian Redlab197ba2009-02-09 18:23:29 +0000922 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000923 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Sebastian Redlab197ba2009-02-09 18:23:29 +0000924 Size.release(), LLoc),
925 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000926
Sebastian Redlab197ba2009-02-09 18:23:29 +0000927 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000928 return;
929 }
930}
931
932/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
933/// This ambiguity appears in the syntax of the C++ new operator.
934///
935/// new-expression:
936/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
937/// new-initializer[opt]
938///
939/// new-placement:
940/// '(' expression-list ')'
941///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000942bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +0000943 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000944 // The '(' was already consumed.
945 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000946 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000947 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000948 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000949 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000950 }
951
952 // It's not a type, it has to be an expression list.
953 // Discard the comma locations - ActOnCXXNew has enough parameters.
954 CommaLocsTy CommaLocs;
955 return ParseExpressionList(PlacementArgs, CommaLocs);
956}
957
958/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
959/// to free memory allocated by new.
960///
Chris Lattner59232d32009-01-04 21:25:24 +0000961/// This method is called to parse the 'delete' expression after the optional
962/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
963/// and "Start" is its location. Otherwise, "Start" is the location of the
964/// 'delete' token.
965///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000966/// delete-expression:
967/// '::'[opt] 'delete' cast-expression
968/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000969Parser::OwningExprResult
970Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
971 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
972 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000973
974 // Array delete?
975 bool ArrayDelete = false;
976 if (Tok.is(tok::l_square)) {
977 ArrayDelete = true;
978 SourceLocation LHS = ConsumeBracket();
979 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
980 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000981 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000982 }
983
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000984 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000985 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000986 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000987
Sebastian Redlf53597f2009-03-15 17:47:39 +0000988 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000989}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000990
991static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
992{
993 switch(kind) {
994 default: assert(false && "Not a known unary type trait.");
995 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
996 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
997 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
998 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
999 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1000 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1001 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1002 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1003 case tok::kw___is_abstract: return UTT_IsAbstract;
1004 case tok::kw___is_class: return UTT_IsClass;
1005 case tok::kw___is_empty: return UTT_IsEmpty;
1006 case tok::kw___is_enum: return UTT_IsEnum;
1007 case tok::kw___is_pod: return UTT_IsPOD;
1008 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1009 case tok::kw___is_union: return UTT_IsUnion;
1010 }
1011}
1012
1013/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1014/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1015/// templates.
1016///
1017/// primary-expression:
1018/// [GNU] unary-type-trait '(' type-id ')'
1019///
1020Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
1021{
1022 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1023 SourceLocation Loc = ConsumeToken();
1024
1025 SourceLocation LParen = Tok.getLocation();
1026 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1027 return ExprError();
1028
1029 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1030 // there will be cryptic errors about mismatched parentheses and missing
1031 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001032 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001033
1034 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1035
Douglas Gregor809070a2009-02-18 17:45:20 +00001036 if (Ty.isInvalid())
1037 return ExprError();
1038
1039 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001040}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001041
1042/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1043/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1044/// based on the context past the parens.
1045Parser::OwningExprResult
1046Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1047 TypeTy *&CastTy,
1048 SourceLocation LParenLoc,
1049 SourceLocation &RParenLoc) {
1050 assert(getLang().CPlusPlus && "Should only be called for C++!");
1051 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1052 assert(isTypeIdInParens() && "Not a type-id!");
1053
1054 OwningExprResult Result(Actions, true);
1055 CastTy = 0;
1056
1057 // We need to disambiguate a very ugly part of the C++ syntax:
1058 //
1059 // (T())x; - type-id
1060 // (T())*x; - type-id
1061 // (T())/x; - expression
1062 // (T()); - expression
1063 //
1064 // The bad news is that we cannot use the specialized tentative parser, since
1065 // it can only verify that the thing inside the parens can be parsed as
1066 // type-id, it is not useful for determining the context past the parens.
1067 //
1068 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001069 // making any unnecessary Action calls.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001070
1071 // Start tentantive parsing.
1072 TentativeParsingAction PA(*this);
1073
1074 // Parse the type-id but don't create a type with ActOnTypeName yet.
1075 DeclSpec DS;
1076 ParseSpecifierQualifierList(DS);
1077
1078 // Parse the abstract-declarator, if present.
1079 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1080 ParseDeclarator(DeclaratorInfo);
1081
1082 if (!Tok.is(tok::r_paren)) {
1083 PA.Commit();
1084 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1085 return ExprError();
1086 }
1087
1088 RParenLoc = ConsumeParen();
1089
1090 if (Tok.is(tok::l_brace)) {
1091 // Compound literal. Ok, we can commit the parsed tokens and continue
1092 // normal parsing.
1093 ExprType = CompoundLiteral;
1094 PA.Commit();
1095 TypeResult Ty = true;
1096 if (!DeclaratorInfo.isInvalidType())
1097 Ty = Actions.ActOnTypeName(CurScope, DeclaratorInfo);
1098 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1099 }
1100
1101 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
1102
1103 if (DeclaratorInfo.isInvalidType()) {
1104 PA.Commit();
1105 return ExprError();
1106 }
1107
1108 bool NotCastExpr;
1109 // Parse the cast-expression that follows it next.
1110 Result = ParseCastExpression(false/*isUnaryExpression*/,
1111 false/*isAddressofOperand*/,
1112 NotCastExpr);
1113
1114 if (NotCastExpr == false) {
1115 // We parsed a cast-expression. That means it's really a type-id, so commit
1116 // the parsed tokens and continue normal parsing.
1117 PA.Commit();
1118 TypeResult Ty = Actions.ActOnTypeName(CurScope, DeclaratorInfo);
1119 CastTy = Ty.get();
1120 if (!Result.isInvalid())
1121 Result = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc,move(Result));
1122 return move(Result);
1123 }
1124
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001125 // If we get here, the things after the parens are not the start of
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001126 // a cast-expression. This means we must actually parse the tokens inside
1127 // the parens as an expression.
1128 PA.Revert();
1129
1130 Result = ParseExpression();
1131 ExprType = SimpleExpr;
1132 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1133 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1134
1135 // Match the ')'.
1136 if (Result.isInvalid()) {
1137 SkipUntil(tok::r_paren);
1138 return ExprError();
1139 }
1140
1141 if (Tok.is(tok::r_paren))
1142 RParenLoc = ConsumeParen();
1143 else
1144 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1145
1146 return move(Result);
1147}