blob: 6aca1cbbcc46761aa77dc37ff8f941878214ba70 [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"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000018#include "llvm/Support/ErrorHandling.h"
19
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Mike Stump1eb44332009-09-09 15:08:12 +000022/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000023///
24/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000025/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000026/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000027///
28/// '::'[opt] nested-name-specifier
29/// '::'
30///
31/// nested-name-specifier:
32/// type-name '::'
33/// namespace-name '::'
34/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000035/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000036///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000037///
Mike Stump1eb44332009-09-09 15:08:12 +000038/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000039/// nested-name-specifier (or empty)
40///
Mike Stump1eb44332009-09-09 15:08:12 +000041/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000042/// the "." or "->" of a member access expression, this parameter provides the
43/// type of the object whose members are being accessed.
44///
45/// \param EnteringContext whether we will be entering into the context of
46/// the nested-name-specifier after parsing it.
47///
48/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000049bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000050 Action::TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +000051 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000052 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000053 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000054
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000055 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000056 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000057 SS.setRange(Tok.getAnnotationRange());
58 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000059 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000060 }
Chris Lattnere607e802009-01-04 21:14:15 +000061
Douglas Gregor39a8de12009-02-25 19:37:18 +000062 bool HasScopeSpecifier = false;
63
Chris Lattner5b454732009-01-05 03:55:46 +000064 if (Tok.is(tok::coloncolon)) {
65 // ::new and ::delete aren't nested-name-specifiers.
66 tok::TokenKind NextKind = NextToken().getKind();
67 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
68 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Chris Lattner55a7cef2009-01-05 00:13:00 +000070 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000071 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000072 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000073 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000074 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000075 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000076 }
77
Douglas Gregor39a8de12009-02-25 19:37:18 +000078 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000079 if (HasScopeSpecifier) {
80 // C++ [basic.lookup.classref]p5:
81 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000082 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000083 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000084 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000085 // the class-name-or-namespace-name is looked up in global scope as a
86 // class-name or namespace-name.
87 //
88 // To implement this, we clear out the object type as soon as we've
89 // seen a leading '::' or part of a nested-name-specifier.
90 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +000091
92 if (Tok.is(tok::code_completion)) {
93 // Code completion for a nested-name-specifier, where the code
94 // code completion token follows the '::'.
95 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
96 ConsumeToken();
97 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +000098 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Douglas Gregor39a8de12009-02-25 19:37:18 +0000100 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000101 // nested-name-specifier 'template'[opt] simple-template-id '::'
102
103 // Parse the optional 'template' keyword, then make sure we have
104 // 'identifier <' after it.
105 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000106 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000107 // nested-name-specifier, since they aren't allowed to start with
108 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000109 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000110 break;
111
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000112 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000113 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000114
115 UnqualifiedId TemplateName;
116 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000117 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000118 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000119 ConsumeToken();
120 } else if (Tok.is(tok::kw_operator)) {
121 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000122 TemplateName)) {
123 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000124 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000125 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000126
127 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId) {
128 Diag(TemplateName.getSourceRange().getBegin(),
129 diag::err_id_after_template_in_nested_name_spec)
130 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000131 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000132 break;
133 }
134 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000135 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000136 break;
137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000139 // If the next token is not '<', we have a qualified-id that refers
140 // to a template name, such as T::template apply, but is not a
141 // template-id.
142 if (Tok.isNot(tok::less)) {
143 TPA.Revert();
144 break;
145 }
146
147 // Commit to parsing the template-id.
148 TPA.Commit();
Mike Stump1eb44332009-09-09 15:08:12 +0000149 TemplateTy Template
Douglas Gregor014e88d2009-11-03 23:16:33 +0000150 = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
Douglas Gregora481edb2009-11-20 23:39:24 +0000151 ObjectType, EnteringContext);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000152 if (!Template)
153 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000154 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000155 &SS, TemplateName, TemplateKWLoc, false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000156 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattner77cf72a2009-06-26 03:47:46 +0000158 continue;
159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Douglas Gregor39a8de12009-02-25 19:37:18 +0000161 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000162 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000163 //
164 // simple-template-id '::'
165 //
166 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000167 // right kind (it should name a type or be dependent), and then
168 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000169 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000170 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
171
Mike Stump1eb44332009-09-09 15:08:12 +0000172 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000173 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000174 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000175
Mike Stump1eb44332009-09-09 15:08:12 +0000176 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000177 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000178 Token TypeToken = Tok;
179 ConsumeToken();
180 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
181 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Douglas Gregor39a8de12009-02-25 19:37:18 +0000183 if (!HasScopeSpecifier) {
184 SS.setBeginLoc(TypeToken.getLocation());
185 HasScopeSpecifier = true;
186 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Douglas Gregor31a19b62009-04-01 21:51:26 +0000188 if (TypeToken.getAnnotationValue())
189 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000190 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000191 TypeToken.getAnnotationValue(),
192 TypeToken.getAnnotationRange(),
193 CCLoc));
194 else
195 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000196 SS.setEndLoc(CCLoc);
197 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Chris Lattner67b9e832009-06-26 03:45:46 +0000200 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000201 }
202
Chris Lattner5c7f7862009-06-26 03:52:38 +0000203
204 // The rest of the nested-name-specifier possibilities start with
205 // tok::identifier.
206 if (Tok.isNot(tok::identifier))
207 break;
208
209 IdentifierInfo &II = *Tok.getIdentifierInfo();
210
211 // nested-name-specifier:
212 // type-name '::'
213 // namespace-name '::'
214 // nested-name-specifier identifier '::'
215 Token Next = NextToken();
216 if (Next.is(tok::coloncolon)) {
217 // We have an identifier followed by a '::'. Lookup this name
218 // as the name in a nested-name-specifier.
219 SourceLocation IdLoc = ConsumeToken();
220 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
221 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Chris Lattner5c7f7862009-06-26 03:52:38 +0000223 if (!HasScopeSpecifier) {
224 SS.setBeginLoc(IdLoc);
225 HasScopeSpecifier = true;
226 }
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattner5c7f7862009-06-26 03:52:38 +0000228 if (SS.isInvalid())
229 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Chris Lattner5c7f7862009-06-26 03:52:38 +0000231 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000232 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000233 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000234 SS.setEndLoc(CCLoc);
235 continue;
236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattner5c7f7862009-06-26 03:52:38 +0000238 // nested-name-specifier:
239 // type-name '<'
240 if (Next.is(tok::less)) {
241 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000242 UnqualifiedId TemplateName;
243 TemplateName.setIdentifier(&II, Tok.getLocation());
244 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
245 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000246 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000247 EnteringContext,
248 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000249 // We have found a template name, so annotate this this token
250 // with a template-id annotation. We do not permit the
251 // template-id to be translated into a type annotation,
252 // because some clients (e.g., the parsing of class template
253 // specializations) still want to see the original template-id
254 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000255 ConsumeToken();
256 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
257 SourceLocation(), false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000258 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000259 continue;
260 }
261 }
262
Douglas Gregor39a8de12009-02-25 19:37:18 +0000263 // We don't have any tokens that form the beginning of a
264 // nested-name-specifier, so we're done.
265 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Douglas Gregor39a8de12009-02-25 19:37:18 +0000268 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000269}
270
271/// ParseCXXIdExpression - Handle id-expression.
272///
273/// id-expression:
274/// unqualified-id
275/// qualified-id
276///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000277/// qualified-id:
278/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
279/// '::' identifier
280/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000281/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000282///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000283/// NOTE: The standard specifies that, for qualified-id, the parser does not
284/// expect:
285///
286/// '::' conversion-function-id
287/// '::' '~' class-name
288///
289/// This may cause a slight inconsistency on diagnostics:
290///
291/// class C {};
292/// namespace A {}
293/// void f() {
294/// :: A :: ~ C(); // Some Sema error about using destructor with a
295/// // namespace.
296/// :: ~ C(); // Some Parser error like 'unexpected ~'.
297/// }
298///
299/// We simplify the parser a bit and make it work like:
300///
301/// qualified-id:
302/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
303/// '::' unqualified-id
304///
305/// That way Sema can handle and report similar errors for namespaces and the
306/// global scope.
307///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000308/// The isAddressOfOperand parameter indicates that this id-expression is a
309/// direct operand of the address-of operator. This is, besides member contexts,
310/// the only place where a qualified-id naming a non-static class member may
311/// appear.
312///
313Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000314 // qualified-id:
315 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
316 // '::' unqualified-id
317 //
318 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000319 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000320
321 UnqualifiedId Name;
322 if (ParseUnqualifiedId(SS,
323 /*EnteringContext=*/false,
324 /*AllowDestructorName=*/false,
325 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000326 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000327 Name))
328 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000329
330 // This is only the direct operand of an & operator if it is not
331 // followed by a postfix-expression suffix.
332 if (isAddressOfOperand) {
333 switch (Tok.getKind()) {
334 case tok::l_square:
335 case tok::l_paren:
336 case tok::arrow:
337 case tok::period:
338 case tok::plusplus:
339 case tok::minusminus:
340 isAddressOfOperand = false;
341 break;
342
343 default:
344 break;
345 }
346 }
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000347
348 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
349 isAddressOfOperand);
350
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000351}
352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353/// ParseCXXCasts - This handles the various ways to cast expressions to another
354/// type.
355///
356/// postfix-expression: [C++ 5.2p1]
357/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
358/// 'static_cast' '<' type-name '>' '(' expression ')'
359/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
360/// 'const_cast' '<' type-name '>' '(' expression ')'
361///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000362Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 tok::TokenKind Kind = Tok.getKind();
364 const char *CastName = 0; // For error messages
365
366 switch (Kind) {
367 default: assert(0 && "Unknown C++ cast!"); abort();
368 case tok::kw_const_cast: CastName = "const_cast"; break;
369 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
370 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
371 case tok::kw_static_cast: CastName = "static_cast"; break;
372 }
373
374 SourceLocation OpLoc = ConsumeToken();
375 SourceLocation LAngleBracketLoc = Tok.getLocation();
376
377 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000378 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000379
Douglas Gregor809070a2009-02-18 17:45:20 +0000380 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 SourceLocation RAngleBracketLoc = Tok.getLocation();
382
Chris Lattner1ab3b962008-11-18 07:48:38 +0000383 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000384 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000385
386 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
387
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000388 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
389 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000390
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000391 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000393 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000394 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000395
Douglas Gregor809070a2009-02-18 17:45:20 +0000396 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000397 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000398 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000399 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000400 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000401
Sebastian Redl20df9b72008-12-11 22:51:44 +0000402 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000403}
404
Sebastian Redlc42e1182008-11-11 11:37:55 +0000405/// ParseCXXTypeid - This handles the C++ typeid expression.
406///
407/// postfix-expression: [C++ 5.2p1]
408/// 'typeid' '(' expression ')'
409/// 'typeid' '(' type-id ')'
410///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000411Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000412 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
413
414 SourceLocation OpLoc = ConsumeToken();
415 SourceLocation LParenLoc = Tok.getLocation();
416 SourceLocation RParenLoc;
417
418 // typeid expressions are always parenthesized.
419 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
420 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000421 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000422
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000423 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000424
425 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000426 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000427
428 // Match the ')'.
429 MatchRHSPunctuation(tok::r_paren, LParenLoc);
430
Douglas Gregor809070a2009-02-18 17:45:20 +0000431 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000432 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000433
434 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000435 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000436 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000437 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000438 // When typeid is applied to an expression other than an lvalue of a
439 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000440 // operand (Clause 5).
441 //
Mike Stump1eb44332009-09-09 15:08:12 +0000442 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000443 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000444 // we the expression is potentially potentially evaluated.
445 EnterExpressionEvaluationContext Unevaluated(Actions,
446 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000447 Result = ParseExpression();
448
449 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000450 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000451 SkipUntil(tok::r_paren);
452 else {
453 MatchRHSPunctuation(tok::r_paren, LParenLoc);
454
455 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000456 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000457 }
458 }
459
Sebastian Redl20df9b72008-12-11 22:51:44 +0000460 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000461}
462
Reid Spencer5f016e22007-07-11 17:01:13 +0000463/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
464///
465/// boolean-literal: [C++ 2.13.5]
466/// 'true'
467/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000468Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000470 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000471}
Chris Lattner50dd2892008-02-26 00:51:44 +0000472
473/// ParseThrowExpression - This handles the C++ throw expression.
474///
475/// throw-expression: [C++ 15]
476/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000477Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000478 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000479 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000480
Chris Lattner2a2819a2008-04-06 06:02:23 +0000481 // If the current token isn't the start of an assignment-expression,
482 // then the expression is not present. This handles things like:
483 // "C ? throw : (void)42", which is crazy but legal.
484 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
485 case tok::semi:
486 case tok::r_paren:
487 case tok::r_square:
488 case tok::r_brace:
489 case tok::colon:
490 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000491 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000492
Chris Lattner2a2819a2008-04-06 06:02:23 +0000493 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000494 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000495 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000496 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000497 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000498}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000499
500/// ParseCXXThis - This handles the C++ 'this' pointer.
501///
502/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
503/// a non-lvalue expression whose value is the address of the object for which
504/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000505Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000506 assert(Tok.is(tok::kw_this) && "Not 'this'!");
507 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000508 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000509}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000510
511/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
512/// Can be interpreted either as function-style casting ("int(x)")
513/// or class type construction ("ClassType(x,y,z)")
514/// or creation of a value-initialized type ("int()").
515///
516/// postfix-expression: [C++ 5.2p1]
517/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
518/// typename-specifier '(' expression-list[opt] ')' [TODO]
519///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000520Parser::OwningExprResult
521Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000522 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000523 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000524
525 assert(Tok.is(tok::l_paren) && "Expected '('!");
526 SourceLocation LParenLoc = ConsumeParen();
527
Sebastian Redla55e52c2008-11-25 22:21:31 +0000528 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000529 CommaLocsTy CommaLocs;
530
531 if (Tok.isNot(tok::r_paren)) {
532 if (ParseExpressionList(Exprs, CommaLocs)) {
533 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000534 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000535 }
536 }
537
538 // Match the ')'.
539 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
540
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000541 // TypeRep could be null, if it references an invalid typedef.
542 if (!TypeRep)
543 return ExprError();
544
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000545 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
546 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000547 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
548 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000549 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000550}
551
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000552/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000553///
554/// condition:
555/// expression
556/// type-specifier-seq declarator '=' assignment-expression
557/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
558/// '=' assignment-expression
559///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000560/// \param ExprResult if the condition was parsed as an expression, the
561/// parsed expression.
562///
563/// \param DeclResult if the condition was parsed as a declaration, the
564/// parsed declaration.
565///
566/// \returns true if there was a parsing, false otherwise.
567bool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
568 DeclPtrTy &DeclResult) {
569 if (!isCXXConditionDeclaration()) {
570 ExprResult = ParseExpression(); // expression
571 DeclResult = DeclPtrTy();
572 return ExprResult.isInvalid();
573 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000574
575 // type-specifier-seq
576 DeclSpec DS;
577 ParseSpecifierQualifierList(DS);
578
579 // declarator
580 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
581 ParseDeclarator(DeclaratorInfo);
582
583 // simple-asm-expr[opt]
584 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000585 SourceLocation Loc;
586 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000587 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000588 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000589 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000590 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000591 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000592 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000593 }
594
595 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000596 if (Tok.is(tok::kw___attribute)) {
597 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000598 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000599 DeclaratorInfo.AddAttributes(AttrList, Loc);
600 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000601
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000602 // Type-check the declaration itself.
603 Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope,
604 DeclaratorInfo);
605 DeclResult = Dcl.get();
606 ExprResult = ExprError();
607
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000608 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000609 if (Tok.is(tok::equal)) {
610 SourceLocation EqualLoc = ConsumeToken();
611 OwningExprResult AssignExpr(ParseAssignmentExpression());
612 if (!AssignExpr.isInvalid())
613 Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
614 } else {
615 // FIXME: C++0x allows a braced-init-list
616 Diag(Tok, diag::err_expected_equal_after_declarator);
617 }
618
619 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000620}
621
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000622/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
623/// This should only be called when the current token is known to be part of
624/// simple-type-specifier.
625///
626/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000627/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000628/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
629/// char
630/// wchar_t
631/// bool
632/// short
633/// int
634/// long
635/// signed
636/// unsigned
637/// float
638/// double
639/// void
640/// [GNU] typeof-specifier
641/// [C++0x] auto [TODO]
642///
643/// type-name:
644/// class-name
645/// enum-name
646/// typedef-name
647///
648void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
649 DS.SetRangeStart(Tok.getLocation());
650 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000651 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000652 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000654 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000655 case tok::identifier: // foo::bar
656 case tok::coloncolon: // ::foo::bar
657 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000658 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000659 assert(0 && "Not a simple-type-specifier token!");
660 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000661
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000662 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000663 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000664 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000665 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000666 break;
667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000669 // builtin types
670 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000671 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000672 break;
673 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000674 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000675 break;
676 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000677 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000678 break;
679 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000680 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000681 break;
682 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000683 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000684 break;
685 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000686 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000687 break;
688 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000689 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000690 break;
691 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000692 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000693 break;
694 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000695 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000696 break;
697 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000698 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000699 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000700 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000701 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000702 break;
703 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000704 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000705 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000706 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000707 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000708 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000710 // GNU typeof support.
711 case tok::kw_typeof:
712 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000713 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000714 return;
715 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000716 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000717 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
718 else
719 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000720 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000721 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000722}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000723
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000724/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
725/// [dcl.name]), which is a non-empty sequence of type-specifiers,
726/// e.g., "const short int". Note that the DeclSpec is *not* finished
727/// by parsing the type-specifier-seq, because these sequences are
728/// typically followed by some form of declarator. Returns true and
729/// emits diagnostics if this is not a type-specifier-seq, false
730/// otherwise.
731///
732/// type-specifier-seq: [C++ 8.1]
733/// type-specifier type-specifier-seq[opt]
734///
735bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
736 DS.SetRangeStart(Tok.getLocation());
737 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000738 unsigned DiagID;
739 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000740
741 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000742 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000743 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000744 return true;
745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
John McCallfec54012009-08-03 20:12:06 +0000747 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000748
749 return false;
750}
751
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000752/// \brief Finish parsing a C++ unqualified-id that is a template-id of
753/// some form.
754///
755/// This routine is invoked when a '<' is encountered after an identifier or
756/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
757/// whether the unqualified-id is actually a template-id. This routine will
758/// then parse the template arguments and form the appropriate template-id to
759/// return to the caller.
760///
761/// \param SS the nested-name-specifier that precedes this template-id, if
762/// we're actually parsing a qualified-id.
763///
764/// \param Name for constructor and destructor names, this is the actual
765/// identifier that may be a template-name.
766///
767/// \param NameLoc the location of the class-name in a constructor or
768/// destructor.
769///
770/// \param EnteringContext whether we're entering the scope of the
771/// nested-name-specifier.
772///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000773/// \param ObjectType if this unqualified-id occurs within a member access
774/// expression, the type of the base object whose member is being accessed.
775///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000776/// \param Id as input, describes the template-name or operator-function-id
777/// that precedes the '<'. If template arguments were parsed successfully,
778/// will be updated with the template-id.
779///
780/// \returns true if a parse error occurred, false otherwise.
781bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
782 IdentifierInfo *Name,
783 SourceLocation NameLoc,
784 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000785 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000786 UnqualifiedId &Id) {
787 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
788
789 TemplateTy Template;
790 TemplateNameKind TNK = TNK_Non_template;
791 switch (Id.getKind()) {
792 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000793 case UnqualifiedId::IK_OperatorFunctionId:
794 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
795 Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000796 break;
797
Douglas Gregor014e88d2009-11-03 23:16:33 +0000798 case UnqualifiedId::IK_ConstructorName: {
799 UnqualifiedId TemplateName;
800 TemplateName.setIdentifier(Name, NameLoc);
801 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
802 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000803 break;
804 }
805
Douglas Gregor014e88d2009-11-03 23:16:33 +0000806 case UnqualifiedId::IK_DestructorName: {
807 UnqualifiedId TemplateName;
808 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000809 if (ObjectType) {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000810 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
Douglas Gregora481edb2009-11-20 23:39:24 +0000811 TemplateName, ObjectType,
812 EnteringContext);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000813 TNK = TNK_Dependent_template_name;
814 if (!Template.get())
815 return true;
816 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000817 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000818 EnteringContext, Template);
819
820 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
821 // The identifier following the destructor did not refer to a template
822 // or to a type. Complain.
823 if (ObjectType)
824 Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
825 << Name;
826 else
827 Diag(NameLoc, diag::err_destructor_class_name);
828 return true;
829 }
830 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000831 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000832 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000833
834 default:
835 return false;
836 }
837
838 if (TNK == TNK_Non_template)
839 return false;
840
841 // Parse the enclosed template argument list.
842 SourceLocation LAngleLoc, RAngleLoc;
843 TemplateArgList TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000844 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
845 &SS, true, LAngleLoc,
846 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000847 RAngleLoc))
848 return true;
849
850 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
851 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) {
852 // Form a parsed representation of the template-id to be stored in the
853 // UnqualifiedId.
854 TemplateIdAnnotation *TemplateId
855 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
856
857 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
858 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000859 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000860 TemplateId->TemplateNameLoc = Id.StartLocation;
861 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000862 TemplateId->Name = 0;
863 TemplateId->Operator = Id.OperatorFunctionId.Operator;
864 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000865 }
866
867 TemplateId->Template = Template.getAs<void*>();
868 TemplateId->Kind = TNK;
869 TemplateId->LAngleLoc = LAngleLoc;
870 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +0000871 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000872 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +0000873 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000874 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000875
876 Id.setTemplateId(TemplateId);
877 return false;
878 }
879
880 // Bundle the template arguments together.
881 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000882 TemplateArgs.size());
883
884 // Constructor and destructor names.
885 Action::TypeResult Type
886 = Actions.ActOnTemplateIdType(Template, NameLoc,
887 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000888 RAngleLoc);
889 if (Type.isInvalid())
890 return true;
891
892 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
893 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
894 else
895 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
896
897 return false;
898}
899
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000900/// \brief Parse an operator-function-id or conversion-function-id as part
901/// of a C++ unqualified-id.
902///
903/// This routine is responsible only for parsing the operator-function-id or
904/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000905///
906/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000907/// operator-function-id: [C++ 13.5]
908/// 'operator' operator
909///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000910/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000911/// new delete new[] delete[]
912/// + - * / % ^ & | ~
913/// ! = < > += -= *= /= %=
914/// ^= &= |= << >> >>= <<= == !=
915/// <= >= && || ++ -- , ->* ->
916/// () []
917///
918/// conversion-function-id: [C++ 12.3.2]
919/// operator conversion-type-id
920///
921/// conversion-type-id:
922/// type-specifier-seq conversion-declarator[opt]
923///
924/// conversion-declarator:
925/// ptr-operator conversion-declarator[opt]
926/// \endcode
927///
928/// \param The nested-name-specifier that preceded this unqualified-id. If
929/// non-empty, then we are parsing the unqualified-id of a qualified-id.
930///
931/// \param EnteringContext whether we are entering the scope of the
932/// nested-name-specifier.
933///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000934/// \param ObjectType if this unqualified-id occurs within a member access
935/// expression, the type of the base object whose member is being accessed.
936///
937/// \param Result on a successful parse, contains the parsed unqualified-id.
938///
939/// \returns true if parsing fails, false otherwise.
940bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
941 TypeTy *ObjectType,
942 UnqualifiedId &Result) {
943 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
944
945 // Consume the 'operator' keyword.
946 SourceLocation KeywordLoc = ConsumeToken();
947
948 // Determine what kind of operator name we have.
949 unsigned SymbolIdx = 0;
950 SourceLocation SymbolLocations[3];
951 OverloadedOperatorKind Op = OO_None;
952 switch (Tok.getKind()) {
953 case tok::kw_new:
954 case tok::kw_delete: {
955 bool isNew = Tok.getKind() == tok::kw_new;
956 // Consume the 'new' or 'delete'.
957 SymbolLocations[SymbolIdx++] = ConsumeToken();
958 if (Tok.is(tok::l_square)) {
959 // Consume the '['.
960 SourceLocation LBracketLoc = ConsumeBracket();
961 // Consume the ']'.
962 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
963 LBracketLoc);
964 if (RBracketLoc.isInvalid())
965 return true;
966
967 SymbolLocations[SymbolIdx++] = LBracketLoc;
968 SymbolLocations[SymbolIdx++] = RBracketLoc;
969 Op = isNew? OO_Array_New : OO_Array_Delete;
970 } else {
971 Op = isNew? OO_New : OO_Delete;
972 }
973 break;
974 }
975
976#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
977 case tok::Token: \
978 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
979 Op = OO_##Name; \
980 break;
981#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
982#include "clang/Basic/OperatorKinds.def"
983
984 case tok::l_paren: {
985 // Consume the '('.
986 SourceLocation LParenLoc = ConsumeParen();
987 // Consume the ')'.
988 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
989 LParenLoc);
990 if (RParenLoc.isInvalid())
991 return true;
992
993 SymbolLocations[SymbolIdx++] = LParenLoc;
994 SymbolLocations[SymbolIdx++] = RParenLoc;
995 Op = OO_Call;
996 break;
997 }
998
999 case tok::l_square: {
1000 // Consume the '['.
1001 SourceLocation LBracketLoc = ConsumeBracket();
1002 // Consume the ']'.
1003 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1004 LBracketLoc);
1005 if (RBracketLoc.isInvalid())
1006 return true;
1007
1008 SymbolLocations[SymbolIdx++] = LBracketLoc;
1009 SymbolLocations[SymbolIdx++] = RBracketLoc;
1010 Op = OO_Subscript;
1011 break;
1012 }
1013
1014 case tok::code_completion: {
1015 // Code completion for the operator name.
1016 Actions.CodeCompleteOperatorName(CurScope);
1017
1018 // Consume the operator token.
1019 ConsumeToken();
1020
1021 // Don't try to parse any further.
1022 return true;
1023 }
1024
1025 default:
1026 break;
1027 }
1028
1029 if (Op != OO_None) {
1030 // We have parsed an operator-function-id.
1031 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1032 return false;
1033 }
Sean Hunt0486d742009-11-28 04:44:28 +00001034
1035 // Parse a literal-operator-id.
1036 //
1037 // literal-operator-id: [C++0x 13.5.8]
1038 // operator "" identifier
1039
1040 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1041 if (Tok.getLength() != 2)
1042 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1043 ConsumeStringToken();
1044
1045 if (Tok.isNot(tok::identifier)) {
1046 Diag(Tok.getLocation(), diag::err_expected_ident);
1047 return true;
1048 }
1049
1050 IdentifierInfo *II = Tok.getIdentifierInfo();
1051 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
1052 Diag(KeywordLoc, diag::err_unsupported_literal_operator);
1053 return true;
1054 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001055
1056 // Parse a conversion-function-id.
1057 //
1058 // conversion-function-id: [C++ 12.3.2]
1059 // operator conversion-type-id
1060 //
1061 // conversion-type-id:
1062 // type-specifier-seq conversion-declarator[opt]
1063 //
1064 // conversion-declarator:
1065 // ptr-operator conversion-declarator[opt]
1066
1067 // Parse the type-specifier-seq.
1068 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001069 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001070 return true;
1071
1072 // Parse the conversion-declarator, which is merely a sequence of
1073 // ptr-operators.
1074 Declarator D(DS, Declarator::TypeNameContext);
1075 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1076
1077 // Finish up the type.
1078 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1079 if (Ty.isInvalid())
1080 return true;
1081
1082 // Note that this is a conversion-function-id.
1083 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1084 D.getSourceRange().getEnd());
1085 return false;
1086}
1087
1088/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1089/// name of an entity.
1090///
1091/// \code
1092/// unqualified-id: [C++ expr.prim.general]
1093/// identifier
1094/// operator-function-id
1095/// conversion-function-id
1096/// [C++0x] literal-operator-id [TODO]
1097/// ~ class-name
1098/// template-id
1099///
1100/// \endcode
1101///
1102/// \param The nested-name-specifier that preceded this unqualified-id. If
1103/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1104///
1105/// \param EnteringContext whether we are entering the scope of the
1106/// nested-name-specifier.
1107///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001108/// \param AllowDestructorName whether we allow parsing of a destructor name.
1109///
1110/// \param AllowConstructorName whether we allow parsing a constructor name.
1111///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001112/// \param ObjectType if this unqualified-id occurs within a member access
1113/// expression, the type of the base object whose member is being accessed.
1114///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001115/// \param Result on a successful parse, contains the parsed unqualified-id.
1116///
1117/// \returns true if parsing fails, false otherwise.
1118bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1119 bool AllowDestructorName,
1120 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001121 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001122 UnqualifiedId &Result) {
1123 // unqualified-id:
1124 // identifier
1125 // template-id (when it hasn't already been annotated)
1126 if (Tok.is(tok::identifier)) {
1127 // Consume the identifier.
1128 IdentifierInfo *Id = Tok.getIdentifierInfo();
1129 SourceLocation IdLoc = ConsumeToken();
1130
1131 if (AllowConstructorName &&
1132 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1133 // We have parsed a constructor name.
1134 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1135 &SS, false),
1136 IdLoc, IdLoc);
1137 } else {
1138 // We have parsed an identifier.
1139 Result.setIdentifier(Id, IdLoc);
1140 }
1141
1142 // If the next token is a '<', we may have a template.
1143 if (Tok.is(tok::less))
1144 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001145 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001146
1147 return false;
1148 }
1149
1150 // unqualified-id:
1151 // template-id (already parsed and annotated)
1152 if (Tok.is(tok::annot_template_id)) {
1153 // FIXME: Could this be a constructor name???
1154
1155 // We have already parsed a template-id; consume the annotation token as
1156 // our unqualified-id.
1157 Result.setTemplateId(
1158 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1159 ConsumeToken();
1160 return false;
1161 }
1162
1163 // unqualified-id:
1164 // operator-function-id
1165 // conversion-function-id
1166 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001167 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001168 return true;
1169
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001170 // If we have an operator-function-id and the next token is a '<', we may
1171 // have a
1172 //
1173 // template-id:
1174 // operator-function-id < template-argument-list[opt] >
1175 if (Result.getKind() == UnqualifiedId::IK_OperatorFunctionId &&
1176 Tok.is(tok::less))
1177 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1178 EnteringContext, ObjectType,
1179 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001180
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001181 return false;
1182 }
1183
1184 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1185 // C++ [expr.unary.op]p10:
1186 // There is an ambiguity in the unary-expression ~X(), where X is a
1187 // class-name. The ambiguity is resolved in favor of treating ~ as a
1188 // unary complement rather than treating ~X as referring to a destructor.
1189
1190 // Parse the '~'.
1191 SourceLocation TildeLoc = ConsumeToken();
1192
1193 // Parse the class-name.
1194 if (Tok.isNot(tok::identifier)) {
1195 Diag(Tok, diag::err_destructor_class_name);
1196 return true;
1197 }
1198
1199 // Parse the class-name (or template-name in a simple-template-id).
1200 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1201 SourceLocation ClassNameLoc = ConsumeToken();
1202
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001203 if (Tok.is(tok::less)) {
1204 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1205 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1206 EnteringContext, ObjectType, Result);
1207 }
1208
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001209 // Note that this is a destructor name.
1210 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001211 CurScope, &SS, false, ObjectType);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001212 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001213 if (ObjectType)
1214 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1215 << ClassName;
1216 else
1217 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001218 return true;
1219 }
1220
1221 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001222 return false;
1223 }
1224
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001225 Diag(Tok, diag::err_expected_unqualified_id)
1226 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001227 return true;
1228}
1229
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001230/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1231/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001232///
Chris Lattner59232d32009-01-04 21:25:24 +00001233/// This method is called to parse the new expression after the optional :: has
1234/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1235/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001236///
1237/// new-expression:
1238/// '::'[opt] 'new' new-placement[opt] new-type-id
1239/// new-initializer[opt]
1240/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1241/// new-initializer[opt]
1242///
1243/// new-placement:
1244/// '(' expression-list ')'
1245///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001246/// new-type-id:
1247/// type-specifier-seq new-declarator[opt]
1248///
1249/// new-declarator:
1250/// ptr-operator new-declarator[opt]
1251/// direct-new-declarator
1252///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001253/// new-initializer:
1254/// '(' expression-list[opt] ')'
1255/// [C++0x] braced-init-list [TODO]
1256///
Chris Lattner59232d32009-01-04 21:25:24 +00001257Parser::OwningExprResult
1258Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1259 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1260 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001261
1262 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1263 // second form of new-expression. It can't be a new-type-id.
1264
Sebastian Redla55e52c2008-11-25 22:21:31 +00001265 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001266 SourceLocation PlacementLParen, PlacementRParen;
1267
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001268 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001269 DeclSpec DS;
1270 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001271 if (Tok.is(tok::l_paren)) {
1272 // If it turns out to be a placement, we change the type location.
1273 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001274 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1275 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001276 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001277 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001278
1279 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001280 if (PlacementRParen.isInvalid()) {
1281 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001282 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001283 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001284
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001285 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001286 // Reset the placement locations. There was no placement.
1287 PlacementLParen = PlacementRParen = SourceLocation();
1288 ParenTypeId = true;
1289 } else {
1290 // We still need the type.
1291 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001292 SourceLocation LParen = ConsumeParen();
1293 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001294 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001295 ParseDeclarator(DeclaratorInfo);
1296 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001297 ParenTypeId = true;
1298 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001299 if (ParseCXXTypeSpecifierSeq(DS))
1300 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001301 else {
1302 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001303 ParseDeclaratorInternal(DeclaratorInfo,
1304 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001305 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001306 ParenTypeId = false;
1307 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001308 }
1309 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001310 // A new-type-id is a simplified type-id, where essentially the
1311 // direct-declarator is replaced by a direct-new-declarator.
1312 if (ParseCXXTypeSpecifierSeq(DS))
1313 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001314 else {
1315 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001316 ParseDeclaratorInternal(DeclaratorInfo,
1317 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001318 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001319 ParenTypeId = false;
1320 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001321 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001322 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001323 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001324 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001325
Sebastian Redla55e52c2008-11-25 22:21:31 +00001326 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001327 SourceLocation ConstructorLParen, ConstructorRParen;
1328
1329 if (Tok.is(tok::l_paren)) {
1330 ConstructorLParen = ConsumeParen();
1331 if (Tok.isNot(tok::r_paren)) {
1332 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001333 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1334 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001335 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001336 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001337 }
1338 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001339 if (ConstructorRParen.isInvalid()) {
1340 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001341 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001342 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001343 }
1344
Sebastian Redlf53597f2009-03-15 17:47:39 +00001345 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1346 move_arg(PlacementArgs), PlacementRParen,
1347 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1348 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001349}
1350
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001351/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1352/// passed to ParseDeclaratorInternal.
1353///
1354/// direct-new-declarator:
1355/// '[' expression ']'
1356/// direct-new-declarator '[' constant-expression ']'
1357///
Chris Lattner59232d32009-01-04 21:25:24 +00001358void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001359 // Parse the array dimensions.
1360 bool first = true;
1361 while (Tok.is(tok::l_square)) {
1362 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001363 OwningExprResult Size(first ? ParseExpression()
1364 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001365 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001366 // Recover
1367 SkipUntil(tok::r_square);
1368 return;
1369 }
1370 first = false;
1371
Sebastian Redlab197ba2009-02-09 18:23:29 +00001372 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001373 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001374 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001375 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001376
Sebastian Redlab197ba2009-02-09 18:23:29 +00001377 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001378 return;
1379 }
1380}
1381
1382/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1383/// This ambiguity appears in the syntax of the C++ new operator.
1384///
1385/// new-expression:
1386/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1387/// new-initializer[opt]
1388///
1389/// new-placement:
1390/// '(' expression-list ')'
1391///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001392bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001393 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001394 // The '(' was already consumed.
1395 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001396 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001397 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001398 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001399 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001400 }
1401
1402 // It's not a type, it has to be an expression list.
1403 // Discard the comma locations - ActOnCXXNew has enough parameters.
1404 CommaLocsTy CommaLocs;
1405 return ParseExpressionList(PlacementArgs, CommaLocs);
1406}
1407
1408/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1409/// to free memory allocated by new.
1410///
Chris Lattner59232d32009-01-04 21:25:24 +00001411/// This method is called to parse the 'delete' expression after the optional
1412/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1413/// and "Start" is its location. Otherwise, "Start" is the location of the
1414/// 'delete' token.
1415///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001416/// delete-expression:
1417/// '::'[opt] 'delete' cast-expression
1418/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001419Parser::OwningExprResult
1420Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1421 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1422 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001423
1424 // Array delete?
1425 bool ArrayDelete = false;
1426 if (Tok.is(tok::l_square)) {
1427 ArrayDelete = true;
1428 SourceLocation LHS = ConsumeBracket();
1429 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1430 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001431 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001432 }
1433
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001434 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001435 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001436 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001437
Sebastian Redlf53597f2009-03-15 17:47:39 +00001438 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001439}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001440
Mike Stump1eb44332009-09-09 15:08:12 +00001441static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001442 switch(kind) {
1443 default: assert(false && "Not a known unary type trait.");
1444 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1445 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1446 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1447 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1448 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1449 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1450 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1451 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1452 case tok::kw___is_abstract: return UTT_IsAbstract;
1453 case tok::kw___is_class: return UTT_IsClass;
1454 case tok::kw___is_empty: return UTT_IsEmpty;
1455 case tok::kw___is_enum: return UTT_IsEnum;
1456 case tok::kw___is_pod: return UTT_IsPOD;
1457 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1458 case tok::kw___is_union: return UTT_IsUnion;
1459 }
1460}
1461
1462/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1463/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1464/// templates.
1465///
1466/// primary-expression:
1467/// [GNU] unary-type-trait '(' type-id ')'
1468///
Mike Stump1eb44332009-09-09 15:08:12 +00001469Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001470 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1471 SourceLocation Loc = ConsumeToken();
1472
1473 SourceLocation LParen = Tok.getLocation();
1474 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1475 return ExprError();
1476
1477 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1478 // there will be cryptic errors about mismatched parentheses and missing
1479 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001480 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001481
1482 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1483
Douglas Gregor809070a2009-02-18 17:45:20 +00001484 if (Ty.isInvalid())
1485 return ExprError();
1486
1487 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001488}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001489
1490/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1491/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1492/// based on the context past the parens.
1493Parser::OwningExprResult
1494Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1495 TypeTy *&CastTy,
1496 SourceLocation LParenLoc,
1497 SourceLocation &RParenLoc) {
1498 assert(getLang().CPlusPlus && "Should only be called for C++!");
1499 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1500 assert(isTypeIdInParens() && "Not a type-id!");
1501
1502 OwningExprResult Result(Actions, true);
1503 CastTy = 0;
1504
1505 // We need to disambiguate a very ugly part of the C++ syntax:
1506 //
1507 // (T())x; - type-id
1508 // (T())*x; - type-id
1509 // (T())/x; - expression
1510 // (T()); - expression
1511 //
1512 // The bad news is that we cannot use the specialized tentative parser, since
1513 // it can only verify that the thing inside the parens can be parsed as
1514 // type-id, it is not useful for determining the context past the parens.
1515 //
1516 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001517 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001518 //
1519 // It uses a scheme similar to parsing inline methods. The parenthesized
1520 // tokens are cached, the context that follows is determined (possibly by
1521 // parsing a cast-expression), and then we re-introduce the cached tokens
1522 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001523
Mike Stump1eb44332009-09-09 15:08:12 +00001524 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001525 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001526
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001527 // Store the tokens of the parentheses. We will parse them after we determine
1528 // the context that follows them.
1529 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1530 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001531 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1532 return ExprError();
1533 }
1534
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001535 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001536 ParseAs = CompoundLiteral;
1537 } else {
1538 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001539 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1540 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1541 NotCastExpr = true;
1542 } else {
1543 // Try parsing the cast-expression that may follow.
1544 // If it is not a cast-expression, NotCastExpr will be true and no token
1545 // will be consumed.
1546 Result = ParseCastExpression(false/*isUnaryExpression*/,
1547 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001548 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001549 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001550
1551 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1552 // an expression.
1553 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001554 }
1555
Mike Stump1eb44332009-09-09 15:08:12 +00001556 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001557 Toks.push_back(Tok);
1558 // Re-enter the stored parenthesized tokens into the token stream, so we may
1559 // parse them now.
1560 PP.EnterTokenStream(Toks.data(), Toks.size(),
1561 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1562 // Drop the current token and bring the first cached one. It's the same token
1563 // as when we entered this function.
1564 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001565
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001566 if (ParseAs >= CompoundLiteral) {
1567 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001568
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001569 // Match the ')'.
1570 if (Tok.is(tok::r_paren))
1571 RParenLoc = ConsumeParen();
1572 else
1573 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001574
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001575 if (ParseAs == CompoundLiteral) {
1576 ExprType = CompoundLiteral;
1577 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1578 }
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001580 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1581 assert(ParseAs == CastExpr);
1582
1583 if (Ty.isInvalid())
1584 return ExprError();
1585
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001586 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001587
1588 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001589 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001590 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001591 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001592 return move(Result);
1593 }
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001595 // Not a compound literal, and not followed by a cast-expression.
1596 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001597
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001598 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001599 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001600 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1601 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1602
1603 // Match the ')'.
1604 if (Result.isInvalid()) {
1605 SkipUntil(tok::r_paren);
1606 return ExprError();
1607 }
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001609 if (Tok.is(tok::r_paren))
1610 RParenLoc = ConsumeParen();
1611 else
1612 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1613
1614 return move(Result);
1615}