blob: 157d8837a934b192a2e8c523aae2fb403a6de450 [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 }
1034
1035 // Parse a conversion-function-id.
1036 //
1037 // conversion-function-id: [C++ 12.3.2]
1038 // operator conversion-type-id
1039 //
1040 // conversion-type-id:
1041 // type-specifier-seq conversion-declarator[opt]
1042 //
1043 // conversion-declarator:
1044 // ptr-operator conversion-declarator[opt]
1045
1046 // Parse the type-specifier-seq.
1047 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001048 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001049 return true;
1050
1051 // Parse the conversion-declarator, which is merely a sequence of
1052 // ptr-operators.
1053 Declarator D(DS, Declarator::TypeNameContext);
1054 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1055
1056 // Finish up the type.
1057 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1058 if (Ty.isInvalid())
1059 return true;
1060
1061 // Note that this is a conversion-function-id.
1062 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1063 D.getSourceRange().getEnd());
1064 return false;
1065}
1066
1067/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1068/// name of an entity.
1069///
1070/// \code
1071/// unqualified-id: [C++ expr.prim.general]
1072/// identifier
1073/// operator-function-id
1074/// conversion-function-id
1075/// [C++0x] literal-operator-id [TODO]
1076/// ~ class-name
1077/// template-id
1078///
1079/// \endcode
1080///
1081/// \param The nested-name-specifier that preceded this unqualified-id. If
1082/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1083///
1084/// \param EnteringContext whether we are entering the scope of the
1085/// nested-name-specifier.
1086///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001087/// \param AllowDestructorName whether we allow parsing of a destructor name.
1088///
1089/// \param AllowConstructorName whether we allow parsing a constructor name.
1090///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001091/// \param ObjectType if this unqualified-id occurs within a member access
1092/// expression, the type of the base object whose member is being accessed.
1093///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001094/// \param Result on a successful parse, contains the parsed unqualified-id.
1095///
1096/// \returns true if parsing fails, false otherwise.
1097bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1098 bool AllowDestructorName,
1099 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001100 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001101 UnqualifiedId &Result) {
1102 // unqualified-id:
1103 // identifier
1104 // template-id (when it hasn't already been annotated)
1105 if (Tok.is(tok::identifier)) {
1106 // Consume the identifier.
1107 IdentifierInfo *Id = Tok.getIdentifierInfo();
1108 SourceLocation IdLoc = ConsumeToken();
1109
1110 if (AllowConstructorName &&
1111 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1112 // We have parsed a constructor name.
1113 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1114 &SS, false),
1115 IdLoc, IdLoc);
1116 } else {
1117 // We have parsed an identifier.
1118 Result.setIdentifier(Id, IdLoc);
1119 }
1120
1121 // If the next token is a '<', we may have a template.
1122 if (Tok.is(tok::less))
1123 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001124 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001125
1126 return false;
1127 }
1128
1129 // unqualified-id:
1130 // template-id (already parsed and annotated)
1131 if (Tok.is(tok::annot_template_id)) {
1132 // FIXME: Could this be a constructor name???
1133
1134 // We have already parsed a template-id; consume the annotation token as
1135 // our unqualified-id.
1136 Result.setTemplateId(
1137 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1138 ConsumeToken();
1139 return false;
1140 }
1141
1142 // unqualified-id:
1143 // operator-function-id
1144 // conversion-function-id
1145 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001146 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001147 return true;
1148
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001149 // If we have an operator-function-id and the next token is a '<', we may
1150 // have a
1151 //
1152 // template-id:
1153 // operator-function-id < template-argument-list[opt] >
1154 if (Result.getKind() == UnqualifiedId::IK_OperatorFunctionId &&
1155 Tok.is(tok::less))
1156 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1157 EnteringContext, ObjectType,
1158 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001159
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001160 return false;
1161 }
1162
1163 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1164 // C++ [expr.unary.op]p10:
1165 // There is an ambiguity in the unary-expression ~X(), where X is a
1166 // class-name. The ambiguity is resolved in favor of treating ~ as a
1167 // unary complement rather than treating ~X as referring to a destructor.
1168
1169 // Parse the '~'.
1170 SourceLocation TildeLoc = ConsumeToken();
1171
1172 // Parse the class-name.
1173 if (Tok.isNot(tok::identifier)) {
1174 Diag(Tok, diag::err_destructor_class_name);
1175 return true;
1176 }
1177
1178 // Parse the class-name (or template-name in a simple-template-id).
1179 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1180 SourceLocation ClassNameLoc = ConsumeToken();
1181
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001182 if (Tok.is(tok::less)) {
1183 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1184 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1185 EnteringContext, ObjectType, Result);
1186 }
1187
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001188 // Note that this is a destructor name.
1189 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001190 CurScope, &SS, false, ObjectType);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001191 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001192 if (ObjectType)
1193 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1194 << ClassName;
1195 else
1196 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001197 return true;
1198 }
1199
1200 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001201 return false;
1202 }
1203
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001204 Diag(Tok, diag::err_expected_unqualified_id)
1205 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001206 return true;
1207}
1208
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001209/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1210/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001211///
Chris Lattner59232d32009-01-04 21:25:24 +00001212/// This method is called to parse the new expression after the optional :: has
1213/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1214/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001215///
1216/// new-expression:
1217/// '::'[opt] 'new' new-placement[opt] new-type-id
1218/// new-initializer[opt]
1219/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1220/// new-initializer[opt]
1221///
1222/// new-placement:
1223/// '(' expression-list ')'
1224///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001225/// new-type-id:
1226/// type-specifier-seq new-declarator[opt]
1227///
1228/// new-declarator:
1229/// ptr-operator new-declarator[opt]
1230/// direct-new-declarator
1231///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001232/// new-initializer:
1233/// '(' expression-list[opt] ')'
1234/// [C++0x] braced-init-list [TODO]
1235///
Chris Lattner59232d32009-01-04 21:25:24 +00001236Parser::OwningExprResult
1237Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1238 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1239 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001240
1241 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1242 // second form of new-expression. It can't be a new-type-id.
1243
Sebastian Redla55e52c2008-11-25 22:21:31 +00001244 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001245 SourceLocation PlacementLParen, PlacementRParen;
1246
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001247 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001248 DeclSpec DS;
1249 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001250 if (Tok.is(tok::l_paren)) {
1251 // If it turns out to be a placement, we change the type location.
1252 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001253 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1254 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001255 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001256 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001257
1258 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001259 if (PlacementRParen.isInvalid()) {
1260 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001261 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001262 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001263
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001264 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001265 // Reset the placement locations. There was no placement.
1266 PlacementLParen = PlacementRParen = SourceLocation();
1267 ParenTypeId = true;
1268 } else {
1269 // We still need the type.
1270 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001271 SourceLocation LParen = ConsumeParen();
1272 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001273 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001274 ParseDeclarator(DeclaratorInfo);
1275 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001276 ParenTypeId = true;
1277 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001278 if (ParseCXXTypeSpecifierSeq(DS))
1279 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001280 else {
1281 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001282 ParseDeclaratorInternal(DeclaratorInfo,
1283 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001284 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001285 ParenTypeId = false;
1286 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001287 }
1288 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001289 // A new-type-id is a simplified type-id, where essentially the
1290 // direct-declarator is replaced by a direct-new-declarator.
1291 if (ParseCXXTypeSpecifierSeq(DS))
1292 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001293 else {
1294 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001295 ParseDeclaratorInternal(DeclaratorInfo,
1296 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001297 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001298 ParenTypeId = false;
1299 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001300 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001301 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001302 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001303 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001304
Sebastian Redla55e52c2008-11-25 22:21:31 +00001305 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001306 SourceLocation ConstructorLParen, ConstructorRParen;
1307
1308 if (Tok.is(tok::l_paren)) {
1309 ConstructorLParen = ConsumeParen();
1310 if (Tok.isNot(tok::r_paren)) {
1311 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001312 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1313 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001314 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001315 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001316 }
1317 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001318 if (ConstructorRParen.isInvalid()) {
1319 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001320 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001321 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001322 }
1323
Sebastian Redlf53597f2009-03-15 17:47:39 +00001324 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1325 move_arg(PlacementArgs), PlacementRParen,
1326 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1327 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001328}
1329
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001330/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1331/// passed to ParseDeclaratorInternal.
1332///
1333/// direct-new-declarator:
1334/// '[' expression ']'
1335/// direct-new-declarator '[' constant-expression ']'
1336///
Chris Lattner59232d32009-01-04 21:25:24 +00001337void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001338 // Parse the array dimensions.
1339 bool first = true;
1340 while (Tok.is(tok::l_square)) {
1341 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001342 OwningExprResult Size(first ? ParseExpression()
1343 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001344 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001345 // Recover
1346 SkipUntil(tok::r_square);
1347 return;
1348 }
1349 first = false;
1350
Sebastian Redlab197ba2009-02-09 18:23:29 +00001351 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001352 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001353 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001354 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001355
Sebastian Redlab197ba2009-02-09 18:23:29 +00001356 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001357 return;
1358 }
1359}
1360
1361/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1362/// This ambiguity appears in the syntax of the C++ new operator.
1363///
1364/// new-expression:
1365/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1366/// new-initializer[opt]
1367///
1368/// new-placement:
1369/// '(' expression-list ')'
1370///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001371bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001372 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001373 // The '(' was already consumed.
1374 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001375 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001376 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001377 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001378 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001379 }
1380
1381 // It's not a type, it has to be an expression list.
1382 // Discard the comma locations - ActOnCXXNew has enough parameters.
1383 CommaLocsTy CommaLocs;
1384 return ParseExpressionList(PlacementArgs, CommaLocs);
1385}
1386
1387/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1388/// to free memory allocated by new.
1389///
Chris Lattner59232d32009-01-04 21:25:24 +00001390/// This method is called to parse the 'delete' expression after the optional
1391/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1392/// and "Start" is its location. Otherwise, "Start" is the location of the
1393/// 'delete' token.
1394///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001395/// delete-expression:
1396/// '::'[opt] 'delete' cast-expression
1397/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001398Parser::OwningExprResult
1399Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1400 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1401 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001402
1403 // Array delete?
1404 bool ArrayDelete = false;
1405 if (Tok.is(tok::l_square)) {
1406 ArrayDelete = true;
1407 SourceLocation LHS = ConsumeBracket();
1408 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1409 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001410 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001411 }
1412
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001413 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001414 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001415 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001416
Sebastian Redlf53597f2009-03-15 17:47:39 +00001417 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001418}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001419
Mike Stump1eb44332009-09-09 15:08:12 +00001420static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001421 switch(kind) {
1422 default: assert(false && "Not a known unary type trait.");
1423 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1424 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1425 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1426 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1427 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1428 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1429 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1430 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1431 case tok::kw___is_abstract: return UTT_IsAbstract;
1432 case tok::kw___is_class: return UTT_IsClass;
1433 case tok::kw___is_empty: return UTT_IsEmpty;
1434 case tok::kw___is_enum: return UTT_IsEnum;
1435 case tok::kw___is_pod: return UTT_IsPOD;
1436 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1437 case tok::kw___is_union: return UTT_IsUnion;
1438 }
1439}
1440
1441/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1442/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1443/// templates.
1444///
1445/// primary-expression:
1446/// [GNU] unary-type-trait '(' type-id ')'
1447///
Mike Stump1eb44332009-09-09 15:08:12 +00001448Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001449 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1450 SourceLocation Loc = ConsumeToken();
1451
1452 SourceLocation LParen = Tok.getLocation();
1453 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1454 return ExprError();
1455
1456 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1457 // there will be cryptic errors about mismatched parentheses and missing
1458 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001459 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001460
1461 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1462
Douglas Gregor809070a2009-02-18 17:45:20 +00001463 if (Ty.isInvalid())
1464 return ExprError();
1465
1466 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001467}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001468
1469/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1470/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1471/// based on the context past the parens.
1472Parser::OwningExprResult
1473Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1474 TypeTy *&CastTy,
1475 SourceLocation LParenLoc,
1476 SourceLocation &RParenLoc) {
1477 assert(getLang().CPlusPlus && "Should only be called for C++!");
1478 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1479 assert(isTypeIdInParens() && "Not a type-id!");
1480
1481 OwningExprResult Result(Actions, true);
1482 CastTy = 0;
1483
1484 // We need to disambiguate a very ugly part of the C++ syntax:
1485 //
1486 // (T())x; - type-id
1487 // (T())*x; - type-id
1488 // (T())/x; - expression
1489 // (T()); - expression
1490 //
1491 // The bad news is that we cannot use the specialized tentative parser, since
1492 // it can only verify that the thing inside the parens can be parsed as
1493 // type-id, it is not useful for determining the context past the parens.
1494 //
1495 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001496 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001497 //
1498 // It uses a scheme similar to parsing inline methods. The parenthesized
1499 // tokens are cached, the context that follows is determined (possibly by
1500 // parsing a cast-expression), and then we re-introduce the cached tokens
1501 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001502
Mike Stump1eb44332009-09-09 15:08:12 +00001503 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001504 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001505
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001506 // Store the tokens of the parentheses. We will parse them after we determine
1507 // the context that follows them.
1508 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1509 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001510 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1511 return ExprError();
1512 }
1513
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001514 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001515 ParseAs = CompoundLiteral;
1516 } else {
1517 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001518 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1519 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1520 NotCastExpr = true;
1521 } else {
1522 // Try parsing the cast-expression that may follow.
1523 // If it is not a cast-expression, NotCastExpr will be true and no token
1524 // will be consumed.
1525 Result = ParseCastExpression(false/*isUnaryExpression*/,
1526 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001527 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001528 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001529
1530 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1531 // an expression.
1532 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001533 }
1534
Mike Stump1eb44332009-09-09 15:08:12 +00001535 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001536 Toks.push_back(Tok);
1537 // Re-enter the stored parenthesized tokens into the token stream, so we may
1538 // parse them now.
1539 PP.EnterTokenStream(Toks.data(), Toks.size(),
1540 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1541 // Drop the current token and bring the first cached one. It's the same token
1542 // as when we entered this function.
1543 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001544
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001545 if (ParseAs >= CompoundLiteral) {
1546 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001547
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001548 // Match the ')'.
1549 if (Tok.is(tok::r_paren))
1550 RParenLoc = ConsumeParen();
1551 else
1552 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001553
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001554 if (ParseAs == CompoundLiteral) {
1555 ExprType = CompoundLiteral;
1556 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1557 }
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001559 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1560 assert(ParseAs == CastExpr);
1561
1562 if (Ty.isInvalid())
1563 return ExprError();
1564
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001565 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001566
1567 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001568 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001569 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001570 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001571 return move(Result);
1572 }
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001574 // Not a compound literal, and not followed by a cast-expression.
1575 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001576
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001577 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001578 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001579 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1580 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1581
1582 // Match the ')'.
1583 if (Result.isInvalid()) {
1584 SkipUntil(tok::r_paren);
1585 return ExprError();
1586 }
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001588 if (Tok.is(tok::r_paren))
1589 RParenLoc = ConsumeParen();
1590 else
1591 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1592
1593 return move(Result);
1594}