blob: 56484720db2d37b951664ef87494283524979828 [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
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000552/// ParseCXXCondition - if/switch/while/for condition expression.
553///
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///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000560Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000561 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000562 return ParseExpression(); // expression
563
564 SourceLocation StartLoc = Tok.getLocation();
565
566 // type-specifier-seq
567 DeclSpec DS;
568 ParseSpecifierQualifierList(DS);
569
570 // declarator
571 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
572 ParseDeclarator(DeclaratorInfo);
573
574 // simple-asm-expr[opt]
575 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000576 SourceLocation Loc;
577 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000578 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000579 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000580 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000581 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000582 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000583 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000584 }
585
586 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000587 if (Tok.is(tok::kw___attribute)) {
588 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000589 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000590 DeclaratorInfo.AddAttributes(AttrList, Loc);
591 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000592
593 // '=' assignment-expression
594 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000595 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000596 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000597 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000598 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000599 return ExprError();
600
Sebastian Redlf53597f2009-03-15 17:47:39 +0000601 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
602 DeclaratorInfo,EqualLoc,
603 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000604}
605
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000606/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
607/// This should only be called when the current token is known to be part of
608/// simple-type-specifier.
609///
610/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000611/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000612/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
613/// char
614/// wchar_t
615/// bool
616/// short
617/// int
618/// long
619/// signed
620/// unsigned
621/// float
622/// double
623/// void
624/// [GNU] typeof-specifier
625/// [C++0x] auto [TODO]
626///
627/// type-name:
628/// class-name
629/// enum-name
630/// typedef-name
631///
632void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
633 DS.SetRangeStart(Tok.getLocation());
634 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000635 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000636 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000638 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000639 case tok::identifier: // foo::bar
640 case tok::coloncolon: // ::foo::bar
641 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000642 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000643 assert(0 && "Not a simple-type-specifier token!");
644 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000645
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000646 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000647 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000648 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000649 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000650 break;
651 }
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000653 // builtin types
654 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000655 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000656 break;
657 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000658 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000659 break;
660 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000661 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000662 break;
663 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000664 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000665 break;
666 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000667 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000668 break;
669 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000670 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000671 break;
672 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000673 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000674 break;
675 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000676 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000677 break;
678 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000679 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000680 break;
681 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000682 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000683 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000684 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000685 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000686 break;
687 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000688 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000689 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000690 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000691 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000692 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000694 // GNU typeof support.
695 case tok::kw_typeof:
696 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000697 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000698 return;
699 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000700 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000701 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
702 else
703 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000704 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000705 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000706}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000707
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000708/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
709/// [dcl.name]), which is a non-empty sequence of type-specifiers,
710/// e.g., "const short int". Note that the DeclSpec is *not* finished
711/// by parsing the type-specifier-seq, because these sequences are
712/// typically followed by some form of declarator. Returns true and
713/// emits diagnostics if this is not a type-specifier-seq, false
714/// otherwise.
715///
716/// type-specifier-seq: [C++ 8.1]
717/// type-specifier type-specifier-seq[opt]
718///
719bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
720 DS.SetRangeStart(Tok.getLocation());
721 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000722 unsigned DiagID;
723 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000724
725 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000726 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000727 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000728 return true;
729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
John McCallfec54012009-08-03 20:12:06 +0000731 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000732
733 return false;
734}
735
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000736/// \brief Finish parsing a C++ unqualified-id that is a template-id of
737/// some form.
738///
739/// This routine is invoked when a '<' is encountered after an identifier or
740/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
741/// whether the unqualified-id is actually a template-id. This routine will
742/// then parse the template arguments and form the appropriate template-id to
743/// return to the caller.
744///
745/// \param SS the nested-name-specifier that precedes this template-id, if
746/// we're actually parsing a qualified-id.
747///
748/// \param Name for constructor and destructor names, this is the actual
749/// identifier that may be a template-name.
750///
751/// \param NameLoc the location of the class-name in a constructor or
752/// destructor.
753///
754/// \param EnteringContext whether we're entering the scope of the
755/// nested-name-specifier.
756///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000757/// \param ObjectType if this unqualified-id occurs within a member access
758/// expression, the type of the base object whose member is being accessed.
759///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000760/// \param Id as input, describes the template-name or operator-function-id
761/// that precedes the '<'. If template arguments were parsed successfully,
762/// will be updated with the template-id.
763///
764/// \returns true if a parse error occurred, false otherwise.
765bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
766 IdentifierInfo *Name,
767 SourceLocation NameLoc,
768 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000769 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000770 UnqualifiedId &Id) {
771 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
772
773 TemplateTy Template;
774 TemplateNameKind TNK = TNK_Non_template;
775 switch (Id.getKind()) {
776 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000777 case UnqualifiedId::IK_OperatorFunctionId:
778 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
779 Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000780 break;
781
Douglas Gregor014e88d2009-11-03 23:16:33 +0000782 case UnqualifiedId::IK_ConstructorName: {
783 UnqualifiedId TemplateName;
784 TemplateName.setIdentifier(Name, NameLoc);
785 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
786 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000787 break;
788 }
789
Douglas Gregor014e88d2009-11-03 23:16:33 +0000790 case UnqualifiedId::IK_DestructorName: {
791 UnqualifiedId TemplateName;
792 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000793 if (ObjectType) {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000794 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
Douglas Gregora481edb2009-11-20 23:39:24 +0000795 TemplateName, ObjectType,
796 EnteringContext);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000797 TNK = TNK_Dependent_template_name;
798 if (!Template.get())
799 return true;
800 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000801 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000802 EnteringContext, Template);
803
804 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
805 // The identifier following the destructor did not refer to a template
806 // or to a type. Complain.
807 if (ObjectType)
808 Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
809 << Name;
810 else
811 Diag(NameLoc, diag::err_destructor_class_name);
812 return true;
813 }
814 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000815 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000816 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000817
818 default:
819 return false;
820 }
821
822 if (TNK == TNK_Non_template)
823 return false;
824
825 // Parse the enclosed template argument list.
826 SourceLocation LAngleLoc, RAngleLoc;
827 TemplateArgList TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000828 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
829 &SS, true, LAngleLoc,
830 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000831 RAngleLoc))
832 return true;
833
834 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
835 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) {
836 // Form a parsed representation of the template-id to be stored in the
837 // UnqualifiedId.
838 TemplateIdAnnotation *TemplateId
839 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
840
841 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
842 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000843 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000844 TemplateId->TemplateNameLoc = Id.StartLocation;
845 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000846 TemplateId->Name = 0;
847 TemplateId->Operator = Id.OperatorFunctionId.Operator;
848 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000849 }
850
851 TemplateId->Template = Template.getAs<void*>();
852 TemplateId->Kind = TNK;
853 TemplateId->LAngleLoc = LAngleLoc;
854 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +0000855 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000856 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +0000857 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000858 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000859
860 Id.setTemplateId(TemplateId);
861 return false;
862 }
863
864 // Bundle the template arguments together.
865 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000866 TemplateArgs.size());
867
868 // Constructor and destructor names.
869 Action::TypeResult Type
870 = Actions.ActOnTemplateIdType(Template, NameLoc,
871 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000872 RAngleLoc);
873 if (Type.isInvalid())
874 return true;
875
876 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
877 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
878 else
879 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
880
881 return false;
882}
883
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000884/// \brief Parse an operator-function-id or conversion-function-id as part
885/// of a C++ unqualified-id.
886///
887/// This routine is responsible only for parsing the operator-function-id or
888/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000889///
890/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000891/// operator-function-id: [C++ 13.5]
892/// 'operator' operator
893///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000894/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000895/// new delete new[] delete[]
896/// + - * / % ^ & | ~
897/// ! = < > += -= *= /= %=
898/// ^= &= |= << >> >>= <<= == !=
899/// <= >= && || ++ -- , ->* ->
900/// () []
901///
902/// conversion-function-id: [C++ 12.3.2]
903/// operator conversion-type-id
904///
905/// conversion-type-id:
906/// type-specifier-seq conversion-declarator[opt]
907///
908/// conversion-declarator:
909/// ptr-operator conversion-declarator[opt]
910/// \endcode
911///
912/// \param The nested-name-specifier that preceded this unqualified-id. If
913/// non-empty, then we are parsing the unqualified-id of a qualified-id.
914///
915/// \param EnteringContext whether we are entering the scope of the
916/// nested-name-specifier.
917///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000918/// \param ObjectType if this unqualified-id occurs within a member access
919/// expression, the type of the base object whose member is being accessed.
920///
921/// \param Result on a successful parse, contains the parsed unqualified-id.
922///
923/// \returns true if parsing fails, false otherwise.
924bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
925 TypeTy *ObjectType,
926 UnqualifiedId &Result) {
927 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
928
929 // Consume the 'operator' keyword.
930 SourceLocation KeywordLoc = ConsumeToken();
931
932 // Determine what kind of operator name we have.
933 unsigned SymbolIdx = 0;
934 SourceLocation SymbolLocations[3];
935 OverloadedOperatorKind Op = OO_None;
936 switch (Tok.getKind()) {
937 case tok::kw_new:
938 case tok::kw_delete: {
939 bool isNew = Tok.getKind() == tok::kw_new;
940 // Consume the 'new' or 'delete'.
941 SymbolLocations[SymbolIdx++] = ConsumeToken();
942 if (Tok.is(tok::l_square)) {
943 // Consume the '['.
944 SourceLocation LBracketLoc = ConsumeBracket();
945 // Consume the ']'.
946 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
947 LBracketLoc);
948 if (RBracketLoc.isInvalid())
949 return true;
950
951 SymbolLocations[SymbolIdx++] = LBracketLoc;
952 SymbolLocations[SymbolIdx++] = RBracketLoc;
953 Op = isNew? OO_Array_New : OO_Array_Delete;
954 } else {
955 Op = isNew? OO_New : OO_Delete;
956 }
957 break;
958 }
959
960#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
961 case tok::Token: \
962 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
963 Op = OO_##Name; \
964 break;
965#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
966#include "clang/Basic/OperatorKinds.def"
967
968 case tok::l_paren: {
969 // Consume the '('.
970 SourceLocation LParenLoc = ConsumeParen();
971 // Consume the ')'.
972 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
973 LParenLoc);
974 if (RParenLoc.isInvalid())
975 return true;
976
977 SymbolLocations[SymbolIdx++] = LParenLoc;
978 SymbolLocations[SymbolIdx++] = RParenLoc;
979 Op = OO_Call;
980 break;
981 }
982
983 case tok::l_square: {
984 // Consume the '['.
985 SourceLocation LBracketLoc = ConsumeBracket();
986 // Consume the ']'.
987 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
988 LBracketLoc);
989 if (RBracketLoc.isInvalid())
990 return true;
991
992 SymbolLocations[SymbolIdx++] = LBracketLoc;
993 SymbolLocations[SymbolIdx++] = RBracketLoc;
994 Op = OO_Subscript;
995 break;
996 }
997
998 case tok::code_completion: {
999 // Code completion for the operator name.
1000 Actions.CodeCompleteOperatorName(CurScope);
1001
1002 // Consume the operator token.
1003 ConsumeToken();
1004
1005 // Don't try to parse any further.
1006 return true;
1007 }
1008
1009 default:
1010 break;
1011 }
1012
1013 if (Op != OO_None) {
1014 // We have parsed an operator-function-id.
1015 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1016 return false;
1017 }
1018
1019 // Parse a conversion-function-id.
1020 //
1021 // conversion-function-id: [C++ 12.3.2]
1022 // operator conversion-type-id
1023 //
1024 // conversion-type-id:
1025 // type-specifier-seq conversion-declarator[opt]
1026 //
1027 // conversion-declarator:
1028 // ptr-operator conversion-declarator[opt]
1029
1030 // Parse the type-specifier-seq.
1031 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001032 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001033 return true;
1034
1035 // Parse the conversion-declarator, which is merely a sequence of
1036 // ptr-operators.
1037 Declarator D(DS, Declarator::TypeNameContext);
1038 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1039
1040 // Finish up the type.
1041 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1042 if (Ty.isInvalid())
1043 return true;
1044
1045 // Note that this is a conversion-function-id.
1046 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1047 D.getSourceRange().getEnd());
1048 return false;
1049}
1050
1051/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1052/// name of an entity.
1053///
1054/// \code
1055/// unqualified-id: [C++ expr.prim.general]
1056/// identifier
1057/// operator-function-id
1058/// conversion-function-id
1059/// [C++0x] literal-operator-id [TODO]
1060/// ~ class-name
1061/// template-id
1062///
1063/// \endcode
1064///
1065/// \param The nested-name-specifier that preceded this unqualified-id. If
1066/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1067///
1068/// \param EnteringContext whether we are entering the scope of the
1069/// nested-name-specifier.
1070///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001071/// \param AllowDestructorName whether we allow parsing of a destructor name.
1072///
1073/// \param AllowConstructorName whether we allow parsing a constructor name.
1074///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001075/// \param ObjectType if this unqualified-id occurs within a member access
1076/// expression, the type of the base object whose member is being accessed.
1077///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001078/// \param Result on a successful parse, contains the parsed unqualified-id.
1079///
1080/// \returns true if parsing fails, false otherwise.
1081bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1082 bool AllowDestructorName,
1083 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001084 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001085 UnqualifiedId &Result) {
1086 // unqualified-id:
1087 // identifier
1088 // template-id (when it hasn't already been annotated)
1089 if (Tok.is(tok::identifier)) {
1090 // Consume the identifier.
1091 IdentifierInfo *Id = Tok.getIdentifierInfo();
1092 SourceLocation IdLoc = ConsumeToken();
1093
1094 if (AllowConstructorName &&
1095 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1096 // We have parsed a constructor name.
1097 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1098 &SS, false),
1099 IdLoc, IdLoc);
1100 } else {
1101 // We have parsed an identifier.
1102 Result.setIdentifier(Id, IdLoc);
1103 }
1104
1105 // If the next token is a '<', we may have a template.
1106 if (Tok.is(tok::less))
1107 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001108 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001109
1110 return false;
1111 }
1112
1113 // unqualified-id:
1114 // template-id (already parsed and annotated)
1115 if (Tok.is(tok::annot_template_id)) {
1116 // FIXME: Could this be a constructor name???
1117
1118 // We have already parsed a template-id; consume the annotation token as
1119 // our unqualified-id.
1120 Result.setTemplateId(
1121 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1122 ConsumeToken();
1123 return false;
1124 }
1125
1126 // unqualified-id:
1127 // operator-function-id
1128 // conversion-function-id
1129 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001130 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001131 return true;
1132
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001133 // If we have an operator-function-id and the next token is a '<', we may
1134 // have a
1135 //
1136 // template-id:
1137 // operator-function-id < template-argument-list[opt] >
1138 if (Result.getKind() == UnqualifiedId::IK_OperatorFunctionId &&
1139 Tok.is(tok::less))
1140 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1141 EnteringContext, ObjectType,
1142 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001143
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001144 return false;
1145 }
1146
1147 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1148 // C++ [expr.unary.op]p10:
1149 // There is an ambiguity in the unary-expression ~X(), where X is a
1150 // class-name. The ambiguity is resolved in favor of treating ~ as a
1151 // unary complement rather than treating ~X as referring to a destructor.
1152
1153 // Parse the '~'.
1154 SourceLocation TildeLoc = ConsumeToken();
1155
1156 // Parse the class-name.
1157 if (Tok.isNot(tok::identifier)) {
1158 Diag(Tok, diag::err_destructor_class_name);
1159 return true;
1160 }
1161
1162 // Parse the class-name (or template-name in a simple-template-id).
1163 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1164 SourceLocation ClassNameLoc = ConsumeToken();
1165
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001166 if (Tok.is(tok::less)) {
1167 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1168 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1169 EnteringContext, ObjectType, Result);
1170 }
1171
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001172 // Note that this is a destructor name.
1173 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001174 CurScope, &SS, false, ObjectType);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001175 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001176 if (ObjectType)
1177 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1178 << ClassName;
1179 else
1180 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001181 return true;
1182 }
1183
1184 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001185 return false;
1186 }
1187
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001188 Diag(Tok, diag::err_expected_unqualified_id)
1189 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001190 return true;
1191}
1192
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001193/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1194/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001195///
Chris Lattner59232d32009-01-04 21:25:24 +00001196/// This method is called to parse the new expression after the optional :: has
1197/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1198/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001199///
1200/// new-expression:
1201/// '::'[opt] 'new' new-placement[opt] new-type-id
1202/// new-initializer[opt]
1203/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1204/// new-initializer[opt]
1205///
1206/// new-placement:
1207/// '(' expression-list ')'
1208///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001209/// new-type-id:
1210/// type-specifier-seq new-declarator[opt]
1211///
1212/// new-declarator:
1213/// ptr-operator new-declarator[opt]
1214/// direct-new-declarator
1215///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001216/// new-initializer:
1217/// '(' expression-list[opt] ')'
1218/// [C++0x] braced-init-list [TODO]
1219///
Chris Lattner59232d32009-01-04 21:25:24 +00001220Parser::OwningExprResult
1221Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1222 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1223 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001224
1225 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1226 // second form of new-expression. It can't be a new-type-id.
1227
Sebastian Redla55e52c2008-11-25 22:21:31 +00001228 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001229 SourceLocation PlacementLParen, PlacementRParen;
1230
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001231 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001232 DeclSpec DS;
1233 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001234 if (Tok.is(tok::l_paren)) {
1235 // If it turns out to be a placement, we change the type location.
1236 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001237 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1238 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001239 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001240 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001241
1242 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001243 if (PlacementRParen.isInvalid()) {
1244 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001245 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001246 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001247
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001248 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001249 // Reset the placement locations. There was no placement.
1250 PlacementLParen = PlacementRParen = SourceLocation();
1251 ParenTypeId = true;
1252 } else {
1253 // We still need the type.
1254 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001255 SourceLocation LParen = ConsumeParen();
1256 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001257 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001258 ParseDeclarator(DeclaratorInfo);
1259 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001260 ParenTypeId = true;
1261 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001262 if (ParseCXXTypeSpecifierSeq(DS))
1263 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001264 else {
1265 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001266 ParseDeclaratorInternal(DeclaratorInfo,
1267 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001268 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001269 ParenTypeId = false;
1270 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001271 }
1272 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001273 // A new-type-id is a simplified type-id, where essentially the
1274 // direct-declarator is replaced by a direct-new-declarator.
1275 if (ParseCXXTypeSpecifierSeq(DS))
1276 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001277 else {
1278 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001279 ParseDeclaratorInternal(DeclaratorInfo,
1280 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001281 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001282 ParenTypeId = false;
1283 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001284 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001285 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001286 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001287 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001288
Sebastian Redla55e52c2008-11-25 22:21:31 +00001289 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001290 SourceLocation ConstructorLParen, ConstructorRParen;
1291
1292 if (Tok.is(tok::l_paren)) {
1293 ConstructorLParen = ConsumeParen();
1294 if (Tok.isNot(tok::r_paren)) {
1295 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001296 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1297 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001298 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001299 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001300 }
1301 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001302 if (ConstructorRParen.isInvalid()) {
1303 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001304 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001305 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001306 }
1307
Sebastian Redlf53597f2009-03-15 17:47:39 +00001308 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1309 move_arg(PlacementArgs), PlacementRParen,
1310 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1311 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001312}
1313
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001314/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1315/// passed to ParseDeclaratorInternal.
1316///
1317/// direct-new-declarator:
1318/// '[' expression ']'
1319/// direct-new-declarator '[' constant-expression ']'
1320///
Chris Lattner59232d32009-01-04 21:25:24 +00001321void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001322 // Parse the array dimensions.
1323 bool first = true;
1324 while (Tok.is(tok::l_square)) {
1325 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001326 OwningExprResult Size(first ? ParseExpression()
1327 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001328 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001329 // Recover
1330 SkipUntil(tok::r_square);
1331 return;
1332 }
1333 first = false;
1334
Sebastian Redlab197ba2009-02-09 18:23:29 +00001335 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001336 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001337 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001338 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001339
Sebastian Redlab197ba2009-02-09 18:23:29 +00001340 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001341 return;
1342 }
1343}
1344
1345/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1346/// This ambiguity appears in the syntax of the C++ new operator.
1347///
1348/// new-expression:
1349/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1350/// new-initializer[opt]
1351///
1352/// new-placement:
1353/// '(' expression-list ')'
1354///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001355bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001356 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001357 // The '(' was already consumed.
1358 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001359 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001360 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001361 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001362 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001363 }
1364
1365 // It's not a type, it has to be an expression list.
1366 // Discard the comma locations - ActOnCXXNew has enough parameters.
1367 CommaLocsTy CommaLocs;
1368 return ParseExpressionList(PlacementArgs, CommaLocs);
1369}
1370
1371/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1372/// to free memory allocated by new.
1373///
Chris Lattner59232d32009-01-04 21:25:24 +00001374/// This method is called to parse the 'delete' expression after the optional
1375/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1376/// and "Start" is its location. Otherwise, "Start" is the location of the
1377/// 'delete' token.
1378///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001379/// delete-expression:
1380/// '::'[opt] 'delete' cast-expression
1381/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001382Parser::OwningExprResult
1383Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1384 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1385 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001386
1387 // Array delete?
1388 bool ArrayDelete = false;
1389 if (Tok.is(tok::l_square)) {
1390 ArrayDelete = true;
1391 SourceLocation LHS = ConsumeBracket();
1392 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1393 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001394 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001395 }
1396
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001397 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001398 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001399 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001400
Sebastian Redlf53597f2009-03-15 17:47:39 +00001401 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001402}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001403
Mike Stump1eb44332009-09-09 15:08:12 +00001404static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001405 switch(kind) {
1406 default: assert(false && "Not a known unary type trait.");
1407 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1408 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1409 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1410 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1411 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1412 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1413 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1414 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1415 case tok::kw___is_abstract: return UTT_IsAbstract;
1416 case tok::kw___is_class: return UTT_IsClass;
1417 case tok::kw___is_empty: return UTT_IsEmpty;
1418 case tok::kw___is_enum: return UTT_IsEnum;
1419 case tok::kw___is_pod: return UTT_IsPOD;
1420 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1421 case tok::kw___is_union: return UTT_IsUnion;
1422 }
1423}
1424
1425/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1426/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1427/// templates.
1428///
1429/// primary-expression:
1430/// [GNU] unary-type-trait '(' type-id ')'
1431///
Mike Stump1eb44332009-09-09 15:08:12 +00001432Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001433 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1434 SourceLocation Loc = ConsumeToken();
1435
1436 SourceLocation LParen = Tok.getLocation();
1437 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1438 return ExprError();
1439
1440 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1441 // there will be cryptic errors about mismatched parentheses and missing
1442 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001443 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001444
1445 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1446
Douglas Gregor809070a2009-02-18 17:45:20 +00001447 if (Ty.isInvalid())
1448 return ExprError();
1449
1450 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001451}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001452
1453/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1454/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1455/// based on the context past the parens.
1456Parser::OwningExprResult
1457Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1458 TypeTy *&CastTy,
1459 SourceLocation LParenLoc,
1460 SourceLocation &RParenLoc) {
1461 assert(getLang().CPlusPlus && "Should only be called for C++!");
1462 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1463 assert(isTypeIdInParens() && "Not a type-id!");
1464
1465 OwningExprResult Result(Actions, true);
1466 CastTy = 0;
1467
1468 // We need to disambiguate a very ugly part of the C++ syntax:
1469 //
1470 // (T())x; - type-id
1471 // (T())*x; - type-id
1472 // (T())/x; - expression
1473 // (T()); - expression
1474 //
1475 // The bad news is that we cannot use the specialized tentative parser, since
1476 // it can only verify that the thing inside the parens can be parsed as
1477 // type-id, it is not useful for determining the context past the parens.
1478 //
1479 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001480 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001481 //
1482 // It uses a scheme similar to parsing inline methods. The parenthesized
1483 // tokens are cached, the context that follows is determined (possibly by
1484 // parsing a cast-expression), and then we re-introduce the cached tokens
1485 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001486
Mike Stump1eb44332009-09-09 15:08:12 +00001487 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001488 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001489
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001490 // Store the tokens of the parentheses. We will parse them after we determine
1491 // the context that follows them.
1492 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1493 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001494 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1495 return ExprError();
1496 }
1497
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001498 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001499 ParseAs = CompoundLiteral;
1500 } else {
1501 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001502 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1503 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1504 NotCastExpr = true;
1505 } else {
1506 // Try parsing the cast-expression that may follow.
1507 // If it is not a cast-expression, NotCastExpr will be true and no token
1508 // will be consumed.
1509 Result = ParseCastExpression(false/*isUnaryExpression*/,
1510 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001511 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001512 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001513
1514 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1515 // an expression.
1516 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001517 }
1518
Mike Stump1eb44332009-09-09 15:08:12 +00001519 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001520 Toks.push_back(Tok);
1521 // Re-enter the stored parenthesized tokens into the token stream, so we may
1522 // parse them now.
1523 PP.EnterTokenStream(Toks.data(), Toks.size(),
1524 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1525 // Drop the current token and bring the first cached one. It's the same token
1526 // as when we entered this function.
1527 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001528
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001529 if (ParseAs >= CompoundLiteral) {
1530 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001531
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001532 // Match the ')'.
1533 if (Tok.is(tok::r_paren))
1534 RParenLoc = ConsumeParen();
1535 else
1536 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001537
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001538 if (ParseAs == CompoundLiteral) {
1539 ExprType = CompoundLiteral;
1540 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1541 }
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001543 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1544 assert(ParseAs == CastExpr);
1545
1546 if (Ty.isInvalid())
1547 return ExprError();
1548
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001549 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001550
1551 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001552 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001553 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001554 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001555 return move(Result);
1556 }
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001558 // Not a compound literal, and not followed by a cast-expression.
1559 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001560
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001561 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001562 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001563 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1564 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1565
1566 // Match the ')'.
1567 if (Result.isInvalid()) {
1568 SkipUntil(tok::r_paren);
1569 return ExprError();
1570 }
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001572 if (Tok.is(tok::r_paren))
1573 RParenLoc = ConsumeParen();
1574 else
1575 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1576
1577 return move(Result);
1578}