blob: a7ca0c54dbe6de8f1d348c592d9211e3c26d4ece [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 Gregor3f9a0562009-11-03 01:35:08 +000017#include "llvm/Support/ErrorHandling.h"
18
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Mike Stump1eb44332009-09-09 15:08:12 +000021/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000022///
23/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000024/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000025/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000026///
27/// '::'[opt] nested-name-specifier
28/// '::'
29///
30/// nested-name-specifier:
31/// type-name '::'
32/// namespace-name '::'
33/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000034/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000035///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000036///
Mike Stump1eb44332009-09-09 15:08:12 +000037/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000038/// nested-name-specifier (or empty)
39///
Mike Stump1eb44332009-09-09 15:08:12 +000040/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// the "." or "->" of a member access expression, this parameter provides the
42/// type of the object whose members are being accessed.
43///
44/// \param EnteringContext whether we will be entering into the context of
45/// the nested-name-specifier after parsing it.
46///
47/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000048bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000049 Action::TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +000050 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000051 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000052 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000053
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000054 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000055 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000056 SS.setRange(Tok.getAnnotationRange());
57 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000058 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000059 }
Chris Lattnere607e802009-01-04 21:14:15 +000060
Douglas Gregor39a8de12009-02-25 19:37:18 +000061 bool HasScopeSpecifier = false;
62
Chris Lattner5b454732009-01-05 03:55:46 +000063 if (Tok.is(tok::coloncolon)) {
64 // ::new and ::delete aren't nested-name-specifiers.
65 tok::TokenKind NextKind = NextToken().getKind();
66 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
67 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000068
Chris Lattner55a7cef2009-01-05 00:13:00 +000069 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000070 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000071 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000072 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000073 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000074 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000075 }
76
Douglas Gregor39a8de12009-02-25 19:37:18 +000077 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000078 if (HasScopeSpecifier) {
79 // C++ [basic.lookup.classref]p5:
80 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000081 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000082 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000083 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000084 // the class-name-or-namespace-name is looked up in global scope as a
85 // class-name or namespace-name.
86 //
87 // To implement this, we clear out the object type as soon as we've
88 // seen a leading '::' or part of a nested-name-specifier.
89 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +000090
91 if (Tok.is(tok::code_completion)) {
92 // Code completion for a nested-name-specifier, where the code
93 // code completion token follows the '::'.
94 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
95 ConsumeToken();
96 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Douglas Gregor39a8de12009-02-25 19:37:18 +000099 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000100 // nested-name-specifier 'template'[opt] simple-template-id '::'
101
102 // Parse the optional 'template' keyword, then make sure we have
103 // 'identifier <' after it.
104 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000105 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000106 // nested-name-specifier, since they aren't allowed to start with
107 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000108 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000109 break;
110
Chris Lattner77cf72a2009-06-26 03:47:46 +0000111 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000112
113 UnqualifiedId TemplateName;
114 if (Tok.is(tok::identifier)) {
115 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
116
117 // If the next token is not '<', we may have a stray 'template' keyword.
118 // Complain and suggest removing the template keyword, but otherwise
119 // allow parsing to continue.
120 if (NextToken().isNot(tok::less)) {
121 Diag(NextToken().getLocation(),
122 diag::err_less_after_template_name_in_nested_name_spec)
123 << Tok.getIdentifierInfo()->getName()
124 << CodeModificationHint::CreateRemoval(SourceRange(TemplateKWLoc));
125 break;
126 }
127
128 // Consume the identifier.
129 ConsumeToken();
130 } else if (Tok.is(tok::kw_operator)) {
131 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
132 TemplateName))
133 break;
134
135 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId) {
136 Diag(TemplateName.getSourceRange().getBegin(),
137 diag::err_id_after_template_in_nested_name_spec)
138 << TemplateName.getSourceRange();
139 break;
140 } else if (Tok.isNot(tok::less)) {
141 std::string OperatorName = "operator ";
142 OperatorName += getOperatorSpelling(
143 TemplateName.OperatorFunctionId.Operator);
144 Diag(Tok.getLocation(),
145 diag::err_less_after_template_name_in_nested_name_spec)
146 << OperatorName
147 << TemplateName.getSourceRange();
148 break;
149 }
150 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000151 Diag(Tok.getLocation(),
Chris Lattner77cf72a2009-06-26 03:47:46 +0000152 diag::err_id_after_template_in_nested_name_spec)
153 << SourceRange(TemplateKWLoc);
154 break;
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Mike Stump1eb44332009-09-09 15:08:12 +0000157 TemplateTy Template
Douglas Gregor014e88d2009-11-03 23:16:33 +0000158 = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000159 ObjectType);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000160 if (!Template)
161 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000162 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000163 &SS, TemplateName, TemplateKWLoc, false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000164 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Chris Lattner77cf72a2009-06-26 03:47:46 +0000166 continue;
167 }
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Douglas Gregor39a8de12009-02-25 19:37:18 +0000169 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000170 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000171 //
172 // simple-template-id '::'
173 //
174 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000175 // right kind (it should name a type or be dependent), and then
176 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000177 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000178 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
179
Mike Stump1eb44332009-09-09 15:08:12 +0000180 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000181 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000182 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000183
Mike Stump1eb44332009-09-09 15:08:12 +0000184 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000185 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000186 Token TypeToken = Tok;
187 ConsumeToken();
188 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
189 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Douglas Gregor39a8de12009-02-25 19:37:18 +0000191 if (!HasScopeSpecifier) {
192 SS.setBeginLoc(TypeToken.getLocation());
193 HasScopeSpecifier = true;
194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Douglas Gregor31a19b62009-04-01 21:51:26 +0000196 if (TypeToken.getAnnotationValue())
197 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000198 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000199 TypeToken.getAnnotationValue(),
200 TypeToken.getAnnotationRange(),
201 CCLoc));
202 else
203 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000204 SS.setEndLoc(CCLoc);
205 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Chris Lattner67b9e832009-06-26 03:45:46 +0000208 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000209 }
210
Chris Lattner5c7f7862009-06-26 03:52:38 +0000211
212 // The rest of the nested-name-specifier possibilities start with
213 // tok::identifier.
214 if (Tok.isNot(tok::identifier))
215 break;
216
217 IdentifierInfo &II = *Tok.getIdentifierInfo();
218
219 // nested-name-specifier:
220 // type-name '::'
221 // namespace-name '::'
222 // nested-name-specifier identifier '::'
223 Token Next = NextToken();
224 if (Next.is(tok::coloncolon)) {
225 // We have an identifier followed by a '::'. Lookup this name
226 // as the name in a nested-name-specifier.
227 SourceLocation IdLoc = ConsumeToken();
228 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
229 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Chris Lattner5c7f7862009-06-26 03:52:38 +0000231 if (!HasScopeSpecifier) {
232 SS.setBeginLoc(IdLoc);
233 HasScopeSpecifier = true;
234 }
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Chris Lattner5c7f7862009-06-26 03:52:38 +0000236 if (SS.isInvalid())
237 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Chris Lattner5c7f7862009-06-26 03:52:38 +0000239 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000240 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000241 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000242 SS.setEndLoc(CCLoc);
243 continue;
244 }
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Chris Lattner5c7f7862009-06-26 03:52:38 +0000246 // nested-name-specifier:
247 // type-name '<'
248 if (Next.is(tok::less)) {
249 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000250 UnqualifiedId TemplateName;
251 TemplateName.setIdentifier(&II, Tok.getLocation());
252 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
253 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000254 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000255 EnteringContext,
256 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000257 // We have found a template name, so annotate this this token
258 // with a template-id annotation. We do not permit the
259 // template-id to be translated into a type annotation,
260 // because some clients (e.g., the parsing of class template
261 // specializations) still want to see the original template-id
262 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000263 ConsumeToken();
264 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
265 SourceLocation(), false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000266 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000267 continue;
268 }
269 }
270
Douglas Gregor39a8de12009-02-25 19:37:18 +0000271 // We don't have any tokens that form the beginning of a
272 // nested-name-specifier, so we're done.
273 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Douglas Gregor39a8de12009-02-25 19:37:18 +0000276 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000277}
278
279/// ParseCXXIdExpression - Handle id-expression.
280///
281/// id-expression:
282/// unqualified-id
283/// qualified-id
284///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000285/// qualified-id:
286/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
287/// '::' identifier
288/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000289/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000290///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000291/// NOTE: The standard specifies that, for qualified-id, the parser does not
292/// expect:
293///
294/// '::' conversion-function-id
295/// '::' '~' class-name
296///
297/// This may cause a slight inconsistency on diagnostics:
298///
299/// class C {};
300/// namespace A {}
301/// void f() {
302/// :: A :: ~ C(); // Some Sema error about using destructor with a
303/// // namespace.
304/// :: ~ C(); // Some Parser error like 'unexpected ~'.
305/// }
306///
307/// We simplify the parser a bit and make it work like:
308///
309/// qualified-id:
310/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
311/// '::' unqualified-id
312///
313/// That way Sema can handle and report similar errors for namespaces and the
314/// global scope.
315///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000316/// The isAddressOfOperand parameter indicates that this id-expression is a
317/// direct operand of the address-of operator. This is, besides member contexts,
318/// the only place where a qualified-id naming a non-static class member may
319/// appear.
320///
321Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000322 // qualified-id:
323 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
324 // '::' unqualified-id
325 //
326 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000327 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000328
329 UnqualifiedId Name;
330 if (ParseUnqualifiedId(SS,
331 /*EnteringContext=*/false,
332 /*AllowDestructorName=*/false,
333 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000334 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000335 Name))
336 return ExprError();
337
338 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
339 isAddressOfOperand);
340
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000341}
342
Reid Spencer5f016e22007-07-11 17:01:13 +0000343/// ParseCXXCasts - This handles the various ways to cast expressions to another
344/// type.
345///
346/// postfix-expression: [C++ 5.2p1]
347/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
348/// 'static_cast' '<' type-name '>' '(' expression ')'
349/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
350/// 'const_cast' '<' type-name '>' '(' expression ')'
351///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000352Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 tok::TokenKind Kind = Tok.getKind();
354 const char *CastName = 0; // For error messages
355
356 switch (Kind) {
357 default: assert(0 && "Unknown C++ cast!"); abort();
358 case tok::kw_const_cast: CastName = "const_cast"; break;
359 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
360 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
361 case tok::kw_static_cast: CastName = "static_cast"; break;
362 }
363
364 SourceLocation OpLoc = ConsumeToken();
365 SourceLocation LAngleBracketLoc = Tok.getLocation();
366
367 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000368 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000369
Douglas Gregor809070a2009-02-18 17:45:20 +0000370 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 SourceLocation RAngleBracketLoc = Tok.getLocation();
372
Chris Lattner1ab3b962008-11-18 07:48:38 +0000373 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000374 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000375
376 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
377
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000378 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
379 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000380
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000381 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000383 // Match the ')'.
384 if (Result.isInvalid())
385 SkipUntil(tok::r_paren);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000387 if (Tok.is(tok::r_paren))
388 RParenLoc = ConsumeParen();
389 else
390 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000391
Douglas Gregor809070a2009-02-18 17:45:20 +0000392 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000393 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000394 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000395 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000396 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000397
Sebastian Redl20df9b72008-12-11 22:51:44 +0000398 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000399}
400
Sebastian Redlc42e1182008-11-11 11:37:55 +0000401/// ParseCXXTypeid - This handles the C++ typeid expression.
402///
403/// postfix-expression: [C++ 5.2p1]
404/// 'typeid' '(' expression ')'
405/// 'typeid' '(' type-id ')'
406///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000407Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000408 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
409
410 SourceLocation OpLoc = ConsumeToken();
411 SourceLocation LParenLoc = Tok.getLocation();
412 SourceLocation RParenLoc;
413
414 // typeid expressions are always parenthesized.
415 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
416 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000417 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000418
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000419 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000420
421 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000422 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000423
424 // Match the ')'.
425 MatchRHSPunctuation(tok::r_paren, LParenLoc);
426
Douglas Gregor809070a2009-02-18 17:45:20 +0000427 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000428 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000429
430 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000431 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000432 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000433 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000434 // When typeid is applied to an expression other than an lvalue of a
435 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000436 // operand (Clause 5).
437 //
Mike Stump1eb44332009-09-09 15:08:12 +0000438 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000439 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000440 // we the expression is potentially potentially evaluated.
441 EnterExpressionEvaluationContext Unevaluated(Actions,
442 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000443 Result = ParseExpression();
444
445 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000446 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000447 SkipUntil(tok::r_paren);
448 else {
449 MatchRHSPunctuation(tok::r_paren, LParenLoc);
450
451 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000452 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000453 }
454 }
455
Sebastian Redl20df9b72008-12-11 22:51:44 +0000456 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000457}
458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
460///
461/// boolean-literal: [C++ 2.13.5]
462/// 'true'
463/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000464Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000466 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000467}
Chris Lattner50dd2892008-02-26 00:51:44 +0000468
469/// ParseThrowExpression - This handles the C++ throw expression.
470///
471/// throw-expression: [C++ 15]
472/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000473Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000474 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000475 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000476
Chris Lattner2a2819a2008-04-06 06:02:23 +0000477 // If the current token isn't the start of an assignment-expression,
478 // then the expression is not present. This handles things like:
479 // "C ? throw : (void)42", which is crazy but legal.
480 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
481 case tok::semi:
482 case tok::r_paren:
483 case tok::r_square:
484 case tok::r_brace:
485 case tok::colon:
486 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000487 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000488
Chris Lattner2a2819a2008-04-06 06:02:23 +0000489 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000490 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000491 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000492 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000493 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000494}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000495
496/// ParseCXXThis - This handles the C++ 'this' pointer.
497///
498/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
499/// a non-lvalue expression whose value is the address of the object for which
500/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000501Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000502 assert(Tok.is(tok::kw_this) && "Not 'this'!");
503 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000504 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000505}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000506
507/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
508/// Can be interpreted either as function-style casting ("int(x)")
509/// or class type construction ("ClassType(x,y,z)")
510/// or creation of a value-initialized type ("int()").
511///
512/// postfix-expression: [C++ 5.2p1]
513/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
514/// typename-specifier '(' expression-list[opt] ')' [TODO]
515///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000516Parser::OwningExprResult
517Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000518 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000519 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000520
521 assert(Tok.is(tok::l_paren) && "Expected '('!");
522 SourceLocation LParenLoc = ConsumeParen();
523
Sebastian Redla55e52c2008-11-25 22:21:31 +0000524 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000525 CommaLocsTy CommaLocs;
526
527 if (Tok.isNot(tok::r_paren)) {
528 if (ParseExpressionList(Exprs, CommaLocs)) {
529 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000530 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000531 }
532 }
533
534 // Match the ')'.
535 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
536
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000537 // TypeRep could be null, if it references an invalid typedef.
538 if (!TypeRep)
539 return ExprError();
540
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000541 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
542 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000543 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
544 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000545 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000546}
547
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000548/// ParseCXXCondition - if/switch/while/for condition expression.
549///
550/// condition:
551/// expression
552/// type-specifier-seq declarator '=' assignment-expression
553/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
554/// '=' assignment-expression
555///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000556Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000557 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000558 return ParseExpression(); // expression
559
560 SourceLocation StartLoc = Tok.getLocation();
561
562 // type-specifier-seq
563 DeclSpec DS;
564 ParseSpecifierQualifierList(DS);
565
566 // declarator
567 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
568 ParseDeclarator(DeclaratorInfo);
569
570 // simple-asm-expr[opt]
571 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000572 SourceLocation Loc;
573 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000574 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000575 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000576 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000577 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000578 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000579 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000580 }
581
582 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000583 if (Tok.is(tok::kw___attribute)) {
584 SourceLocation Loc;
585 AttributeList *AttrList = ParseAttributes(&Loc);
586 DeclaratorInfo.AddAttributes(AttrList, Loc);
587 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000588
589 // '=' assignment-expression
590 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000591 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000592 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000593 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000594 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000595 return ExprError();
596
Sebastian Redlf53597f2009-03-15 17:47:39 +0000597 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
598 DeclaratorInfo,EqualLoc,
599 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000600}
601
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000602/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
603/// This should only be called when the current token is known to be part of
604/// simple-type-specifier.
605///
606/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000607/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000608/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
609/// char
610/// wchar_t
611/// bool
612/// short
613/// int
614/// long
615/// signed
616/// unsigned
617/// float
618/// double
619/// void
620/// [GNU] typeof-specifier
621/// [C++0x] auto [TODO]
622///
623/// type-name:
624/// class-name
625/// enum-name
626/// typedef-name
627///
628void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
629 DS.SetRangeStart(Tok.getLocation());
630 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000631 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000632 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000634 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000635 case tok::identifier: // foo::bar
636 case tok::coloncolon: // ::foo::bar
637 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000638 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000639 assert(0 && "Not a simple-type-specifier token!");
640 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000641
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000642 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000643 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000644 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000645 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000646 break;
647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000649 // builtin types
650 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000651 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000652 break;
653 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000654 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000655 break;
656 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000657 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000658 break;
659 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000660 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000661 break;
662 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000663 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000664 break;
665 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000666 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000667 break;
668 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000669 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000670 break;
671 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000672 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000673 break;
674 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000675 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000676 break;
677 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000678 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000679 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000680 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000681 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000682 break;
683 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000684 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000685 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000686 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000687 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000688 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000690 // GNU typeof support.
691 case tok::kw_typeof:
692 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000693 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000694 return;
695 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000696 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000697 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
698 else
699 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000700 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000701 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000702}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000703
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000704/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
705/// [dcl.name]), which is a non-empty sequence of type-specifiers,
706/// e.g., "const short int". Note that the DeclSpec is *not* finished
707/// by parsing the type-specifier-seq, because these sequences are
708/// typically followed by some form of declarator. Returns true and
709/// emits diagnostics if this is not a type-specifier-seq, false
710/// otherwise.
711///
712/// type-specifier-seq: [C++ 8.1]
713/// type-specifier type-specifier-seq[opt]
714///
715bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
716 DS.SetRangeStart(Tok.getLocation());
717 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000718 unsigned DiagID;
719 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000720
721 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000722 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000723 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000724 return true;
725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
John McCallfec54012009-08-03 20:12:06 +0000727 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000728
729 return false;
730}
731
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000732/// \brief Finish parsing a C++ unqualified-id that is a template-id of
733/// some form.
734///
735/// This routine is invoked when a '<' is encountered after an identifier or
736/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
737/// whether the unqualified-id is actually a template-id. This routine will
738/// then parse the template arguments and form the appropriate template-id to
739/// return to the caller.
740///
741/// \param SS the nested-name-specifier that precedes this template-id, if
742/// we're actually parsing a qualified-id.
743///
744/// \param Name for constructor and destructor names, this is the actual
745/// identifier that may be a template-name.
746///
747/// \param NameLoc the location of the class-name in a constructor or
748/// destructor.
749///
750/// \param EnteringContext whether we're entering the scope of the
751/// nested-name-specifier.
752///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000753/// \param ObjectType if this unqualified-id occurs within a member access
754/// expression, the type of the base object whose member is being accessed.
755///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000756/// \param Id as input, describes the template-name or operator-function-id
757/// that precedes the '<'. If template arguments were parsed successfully,
758/// will be updated with the template-id.
759///
760/// \returns true if a parse error occurred, false otherwise.
761bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
762 IdentifierInfo *Name,
763 SourceLocation NameLoc,
764 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000765 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000766 UnqualifiedId &Id) {
767 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
768
769 TemplateTy Template;
770 TemplateNameKind TNK = TNK_Non_template;
771 switch (Id.getKind()) {
772 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000773 case UnqualifiedId::IK_OperatorFunctionId:
774 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
775 Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000776 break;
777
Douglas Gregor014e88d2009-11-03 23:16:33 +0000778 case UnqualifiedId::IK_ConstructorName: {
779 UnqualifiedId TemplateName;
780 TemplateName.setIdentifier(Name, NameLoc);
781 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
782 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000783 break;
784 }
785
Douglas Gregor014e88d2009-11-03 23:16:33 +0000786 case UnqualifiedId::IK_DestructorName: {
787 UnqualifiedId TemplateName;
788 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000789 if (ObjectType) {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000790 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
791 TemplateName, ObjectType);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000792 TNK = TNK_Dependent_template_name;
793 if (!Template.get())
794 return true;
795 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000796 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000797 EnteringContext, Template);
798
799 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
800 // The identifier following the destructor did not refer to a template
801 // or to a type. Complain.
802 if (ObjectType)
803 Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
804 << Name;
805 else
806 Diag(NameLoc, diag::err_destructor_class_name);
807 return true;
808 }
809 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000810 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000811 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000812
813 default:
814 return false;
815 }
816
817 if (TNK == TNK_Non_template)
818 return false;
819
820 // Parse the enclosed template argument list.
821 SourceLocation LAngleLoc, RAngleLoc;
822 TemplateArgList TemplateArgs;
823 TemplateArgIsTypeList TemplateArgIsType;
824 TemplateArgLocationList TemplateArgLocations;
825 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
826 &SS, true, LAngleLoc,
827 TemplateArgs,
828 TemplateArgIsType,
829 TemplateArgLocations,
830 RAngleLoc))
831 return true;
832
833 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
834 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) {
835 // Form a parsed representation of the template-id to be stored in the
836 // UnqualifiedId.
837 TemplateIdAnnotation *TemplateId
838 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
839
840 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
841 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000842 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000843 TemplateId->TemplateNameLoc = Id.StartLocation;
844 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000845 TemplateId->Name = 0;
846 TemplateId->Operator = Id.OperatorFunctionId.Operator;
847 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000848 }
849
850 TemplateId->Template = Template.getAs<void*>();
851 TemplateId->Kind = TNK;
852 TemplateId->LAngleLoc = LAngleLoc;
853 TemplateId->RAngleLoc = RAngleLoc;
854 void **Args = TemplateId->getTemplateArgs();
855 bool *ArgIsType = TemplateId->getTemplateArgIsType();
856 SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations();
857 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
858 Arg != ArgEnd; ++Arg) {
859 Args[Arg] = TemplateArgs[Arg];
860 ArgIsType[Arg] = TemplateArgIsType[Arg];
861 ArgLocs[Arg] = TemplateArgLocations[Arg];
862 }
863
864 Id.setTemplateId(TemplateId);
865 return false;
866 }
867
868 // Bundle the template arguments together.
869 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
870 TemplateArgIsType.data(),
871 TemplateArgs.size());
872
873 // Constructor and destructor names.
874 Action::TypeResult Type
875 = Actions.ActOnTemplateIdType(Template, NameLoc,
876 LAngleLoc, TemplateArgsPtr,
877 &TemplateArgLocations[0],
878 RAngleLoc);
879 if (Type.isInvalid())
880 return true;
881
882 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
883 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
884 else
885 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
886
887 return false;
888}
889
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000890/// \brief Parse an operator-function-id or conversion-function-id as part
891/// of a C++ unqualified-id.
892///
893/// This routine is responsible only for parsing the operator-function-id or
894/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000895///
896/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000897/// operator-function-id: [C++ 13.5]
898/// 'operator' operator
899///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000900/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000901/// new delete new[] delete[]
902/// + - * / % ^ & | ~
903/// ! = < > += -= *= /= %=
904/// ^= &= |= << >> >>= <<= == !=
905/// <= >= && || ++ -- , ->* ->
906/// () []
907///
908/// conversion-function-id: [C++ 12.3.2]
909/// operator conversion-type-id
910///
911/// conversion-type-id:
912/// type-specifier-seq conversion-declarator[opt]
913///
914/// conversion-declarator:
915/// ptr-operator conversion-declarator[opt]
916/// \endcode
917///
918/// \param The nested-name-specifier that preceded this unqualified-id. If
919/// non-empty, then we are parsing the unqualified-id of a qualified-id.
920///
921/// \param EnteringContext whether we are entering the scope of the
922/// nested-name-specifier.
923///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000924/// \param ObjectType if this unqualified-id occurs within a member access
925/// expression, the type of the base object whose member is being accessed.
926///
927/// \param Result on a successful parse, contains the parsed unqualified-id.
928///
929/// \returns true if parsing fails, false otherwise.
930bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
931 TypeTy *ObjectType,
932 UnqualifiedId &Result) {
933 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
934
935 // Consume the 'operator' keyword.
936 SourceLocation KeywordLoc = ConsumeToken();
937
938 // Determine what kind of operator name we have.
939 unsigned SymbolIdx = 0;
940 SourceLocation SymbolLocations[3];
941 OverloadedOperatorKind Op = OO_None;
942 switch (Tok.getKind()) {
943 case tok::kw_new:
944 case tok::kw_delete: {
945 bool isNew = Tok.getKind() == tok::kw_new;
946 // Consume the 'new' or 'delete'.
947 SymbolLocations[SymbolIdx++] = ConsumeToken();
948 if (Tok.is(tok::l_square)) {
949 // Consume the '['.
950 SourceLocation LBracketLoc = ConsumeBracket();
951 // Consume the ']'.
952 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
953 LBracketLoc);
954 if (RBracketLoc.isInvalid())
955 return true;
956
957 SymbolLocations[SymbolIdx++] = LBracketLoc;
958 SymbolLocations[SymbolIdx++] = RBracketLoc;
959 Op = isNew? OO_Array_New : OO_Array_Delete;
960 } else {
961 Op = isNew? OO_New : OO_Delete;
962 }
963 break;
964 }
965
966#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
967 case tok::Token: \
968 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
969 Op = OO_##Name; \
970 break;
971#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
972#include "clang/Basic/OperatorKinds.def"
973
974 case tok::l_paren: {
975 // Consume the '('.
976 SourceLocation LParenLoc = ConsumeParen();
977 // Consume the ')'.
978 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
979 LParenLoc);
980 if (RParenLoc.isInvalid())
981 return true;
982
983 SymbolLocations[SymbolIdx++] = LParenLoc;
984 SymbolLocations[SymbolIdx++] = RParenLoc;
985 Op = OO_Call;
986 break;
987 }
988
989 case tok::l_square: {
990 // Consume the '['.
991 SourceLocation LBracketLoc = ConsumeBracket();
992 // Consume the ']'.
993 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
994 LBracketLoc);
995 if (RBracketLoc.isInvalid())
996 return true;
997
998 SymbolLocations[SymbolIdx++] = LBracketLoc;
999 SymbolLocations[SymbolIdx++] = RBracketLoc;
1000 Op = OO_Subscript;
1001 break;
1002 }
1003
1004 case tok::code_completion: {
1005 // Code completion for the operator name.
1006 Actions.CodeCompleteOperatorName(CurScope);
1007
1008 // Consume the operator token.
1009 ConsumeToken();
1010
1011 // Don't try to parse any further.
1012 return true;
1013 }
1014
1015 default:
1016 break;
1017 }
1018
1019 if (Op != OO_None) {
1020 // We have parsed an operator-function-id.
1021 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1022 return false;
1023 }
1024
1025 // Parse a conversion-function-id.
1026 //
1027 // conversion-function-id: [C++ 12.3.2]
1028 // operator conversion-type-id
1029 //
1030 // conversion-type-id:
1031 // type-specifier-seq conversion-declarator[opt]
1032 //
1033 // conversion-declarator:
1034 // ptr-operator conversion-declarator[opt]
1035
1036 // Parse the type-specifier-seq.
1037 DeclSpec DS;
1038 if (ParseCXXTypeSpecifierSeq(DS))
1039 return true;
1040
1041 // Parse the conversion-declarator, which is merely a sequence of
1042 // ptr-operators.
1043 Declarator D(DS, Declarator::TypeNameContext);
1044 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1045
1046 // Finish up the type.
1047 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1048 if (Ty.isInvalid())
1049 return true;
1050
1051 // Note that this is a conversion-function-id.
1052 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1053 D.getSourceRange().getEnd());
1054 return false;
1055}
1056
1057/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1058/// name of an entity.
1059///
1060/// \code
1061/// unqualified-id: [C++ expr.prim.general]
1062/// identifier
1063/// operator-function-id
1064/// conversion-function-id
1065/// [C++0x] literal-operator-id [TODO]
1066/// ~ class-name
1067/// template-id
1068///
1069/// \endcode
1070///
1071/// \param The nested-name-specifier that preceded this unqualified-id. If
1072/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1073///
1074/// \param EnteringContext whether we are entering the scope of the
1075/// nested-name-specifier.
1076///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001077/// \param AllowDestructorName whether we allow parsing of a destructor name.
1078///
1079/// \param AllowConstructorName whether we allow parsing a constructor name.
1080///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001081/// \param ObjectType if this unqualified-id occurs within a member access
1082/// expression, the type of the base object whose member is being accessed.
1083///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001084/// \param Result on a successful parse, contains the parsed unqualified-id.
1085///
1086/// \returns true if parsing fails, false otherwise.
1087bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1088 bool AllowDestructorName,
1089 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001090 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001091 UnqualifiedId &Result) {
1092 // unqualified-id:
1093 // identifier
1094 // template-id (when it hasn't already been annotated)
1095 if (Tok.is(tok::identifier)) {
1096 // Consume the identifier.
1097 IdentifierInfo *Id = Tok.getIdentifierInfo();
1098 SourceLocation IdLoc = ConsumeToken();
1099
1100 if (AllowConstructorName &&
1101 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1102 // We have parsed a constructor name.
1103 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1104 &SS, false),
1105 IdLoc, IdLoc);
1106 } else {
1107 // We have parsed an identifier.
1108 Result.setIdentifier(Id, IdLoc);
1109 }
1110
1111 // If the next token is a '<', we may have a template.
1112 if (Tok.is(tok::less))
1113 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001114 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001115
1116 return false;
1117 }
1118
1119 // unqualified-id:
1120 // template-id (already parsed and annotated)
1121 if (Tok.is(tok::annot_template_id)) {
1122 // FIXME: Could this be a constructor name???
1123
1124 // We have already parsed a template-id; consume the annotation token as
1125 // our unqualified-id.
1126 Result.setTemplateId(
1127 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1128 ConsumeToken();
1129 return false;
1130 }
1131
1132 // unqualified-id:
1133 // operator-function-id
1134 // conversion-function-id
1135 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001136 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001137 return true;
1138
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001139 // If we have an operator-function-id and the next token is a '<', we may
1140 // have a
1141 //
1142 // template-id:
1143 // operator-function-id < template-argument-list[opt] >
1144 if (Result.getKind() == UnqualifiedId::IK_OperatorFunctionId &&
1145 Tok.is(tok::less))
1146 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1147 EnteringContext, ObjectType,
1148 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001149
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001150 return false;
1151 }
1152
1153 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1154 // C++ [expr.unary.op]p10:
1155 // There is an ambiguity in the unary-expression ~X(), where X is a
1156 // class-name. The ambiguity is resolved in favor of treating ~ as a
1157 // unary complement rather than treating ~X as referring to a destructor.
1158
1159 // Parse the '~'.
1160 SourceLocation TildeLoc = ConsumeToken();
1161
1162 // Parse the class-name.
1163 if (Tok.isNot(tok::identifier)) {
1164 Diag(Tok, diag::err_destructor_class_name);
1165 return true;
1166 }
1167
1168 // Parse the class-name (or template-name in a simple-template-id).
1169 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1170 SourceLocation ClassNameLoc = ConsumeToken();
1171
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001172 if (Tok.is(tok::less)) {
1173 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1174 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1175 EnteringContext, ObjectType, Result);
1176 }
1177
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001178 // Note that this is a destructor name.
1179 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
1180 CurScope, &SS);
1181 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001182 if (ObjectType)
1183 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1184 << ClassName;
1185 else
1186 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001187 return true;
1188 }
1189
1190 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001191 return false;
1192 }
1193
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001194 Diag(Tok, diag::err_expected_unqualified_id)
1195 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001196 return true;
1197}
1198
Douglas Gregor43c7bad2008-11-17 16:14:12 +00001199/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001200/// operator name (C++ [over.oper]). If successful, returns the
1201/// predefined identifier that corresponds to that overloaded
1202/// operator. Otherwise, returns NULL and does not consume any tokens.
1203///
1204/// operator-function-id: [C++ 13.5]
1205/// 'operator' operator
1206///
1207/// operator: one of
1208/// new delete new[] delete[]
1209/// + - * / % ^ & | ~
1210/// ! = < > += -= *= /= %=
1211/// ^= &= |= << >> >>= <<= == !=
1212/// <= >= && || ++ -- , ->* ->
1213/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +00001214OverloadedOperatorKind
1215Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +00001216 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +00001217 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001218
1219 OverloadedOperatorKind Op = OO_None;
1220 switch (NextToken().getKind()) {
1221 case tok::kw_new:
1222 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001223 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001224 if (Tok.is(tok::l_square)) {
1225 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +00001226 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001227 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
1228 Op = OO_Array_New;
1229 } else {
1230 Op = OO_New;
1231 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001232 if (EndLoc)
1233 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001234 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001235
1236 case tok::kw_delete:
1237 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001238 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001239 if (Tok.is(tok::l_square)) {
1240 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +00001241 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001242 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
1243 Op = OO_Array_Delete;
1244 } else {
1245 Op = OO_Delete;
1246 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001247 if (EndLoc)
1248 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001249 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001250
Douglas Gregor02bcd4c2008-11-10 13:38:07 +00001251#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001252 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +00001253#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001254#include "clang/Basic/OperatorKinds.def"
1255
1256 case tok::l_paren:
1257 ConsumeToken(); // 'operator'
1258 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +00001259 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001260 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001261 if (EndLoc)
1262 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001263 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001264
1265 case tok::l_square:
1266 ConsumeToken(); // 'operator'
1267 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +00001268 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001269 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001270 if (EndLoc)
1271 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001272 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001273
Douglas Gregored8d3222009-09-18 20:05:18 +00001274 case tok::code_completion: {
1275 // Code completion for the operator name.
1276 Actions.CodeCompleteOperatorName(CurScope);
1277
1278 // Consume the 'operator' token, then replace the code-completion token
1279 // with an 'operator' token and try again.
1280 SourceLocation OperatorLoc = ConsumeToken();
1281 Tok.setLocation(OperatorLoc);
1282 Tok.setKind(tok::kw_operator);
1283 return TryParseOperatorFunctionId(EndLoc);
1284 }
1285
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001286 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001287 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001288 }
1289
Douglas Gregor43c7bad2008-11-17 16:14:12 +00001290 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +00001291 Loc = ConsumeAnyToken(); // the operator itself
1292 if (EndLoc)
1293 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001294 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001295}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001296
1297/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
1298/// which expresses the name of a user-defined conversion operator
1299/// (C++ [class.conv.fct]p1). Returns the type that this operator is
1300/// specifying a conversion for, or NULL if there was an error.
1301///
1302/// conversion-function-id: [C++ 12.3.2]
1303/// operator conversion-type-id
1304///
1305/// conversion-type-id:
1306/// type-specifier-seq conversion-declarator[opt]
1307///
1308/// conversion-declarator:
1309/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +00001310Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001311 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1312 ConsumeToken(); // 'operator'
1313
1314 // Parse the type-specifier-seq.
1315 DeclSpec DS;
1316 if (ParseCXXTypeSpecifierSeq(DS))
1317 return 0;
1318
1319 // Parse the conversion-declarator, which is merely a sequence of
1320 // ptr-operators.
1321 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001322 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001323 if (EndLoc)
1324 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001325
1326 // Finish up the type.
1327 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001328 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001329 return 0;
1330 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001331 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001332}
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001333
1334/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1335/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001336///
Chris Lattner59232d32009-01-04 21:25:24 +00001337/// This method is called to parse the new expression after the optional :: has
1338/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1339/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001340///
1341/// new-expression:
1342/// '::'[opt] 'new' new-placement[opt] new-type-id
1343/// new-initializer[opt]
1344/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1345/// new-initializer[opt]
1346///
1347/// new-placement:
1348/// '(' expression-list ')'
1349///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001350/// new-type-id:
1351/// type-specifier-seq new-declarator[opt]
1352///
1353/// new-declarator:
1354/// ptr-operator new-declarator[opt]
1355/// direct-new-declarator
1356///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001357/// new-initializer:
1358/// '(' expression-list[opt] ')'
1359/// [C++0x] braced-init-list [TODO]
1360///
Chris Lattner59232d32009-01-04 21:25:24 +00001361Parser::OwningExprResult
1362Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1363 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1364 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001365
1366 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1367 // second form of new-expression. It can't be a new-type-id.
1368
Sebastian Redla55e52c2008-11-25 22:21:31 +00001369 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001370 SourceLocation PlacementLParen, PlacementRParen;
1371
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001372 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001373 DeclSpec DS;
1374 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001375 if (Tok.is(tok::l_paren)) {
1376 // If it turns out to be a placement, we change the type location.
1377 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001378 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1379 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001380 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001381 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001382
1383 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001384 if (PlacementRParen.isInvalid()) {
1385 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001386 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001387 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001388
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001389 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001390 // Reset the placement locations. There was no placement.
1391 PlacementLParen = PlacementRParen = SourceLocation();
1392 ParenTypeId = true;
1393 } else {
1394 // We still need the type.
1395 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001396 SourceLocation LParen = ConsumeParen();
1397 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001398 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001399 ParseDeclarator(DeclaratorInfo);
1400 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001401 ParenTypeId = true;
1402 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001403 if (ParseCXXTypeSpecifierSeq(DS))
1404 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001405 else {
1406 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001407 ParseDeclaratorInternal(DeclaratorInfo,
1408 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001409 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001410 ParenTypeId = false;
1411 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001412 }
1413 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001414 // A new-type-id is a simplified type-id, where essentially the
1415 // direct-declarator is replaced by a direct-new-declarator.
1416 if (ParseCXXTypeSpecifierSeq(DS))
1417 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001418 else {
1419 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001420 ParseDeclaratorInternal(DeclaratorInfo,
1421 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001422 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001423 ParenTypeId = false;
1424 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001425 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001426 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001427 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001428 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001429
Sebastian Redla55e52c2008-11-25 22:21:31 +00001430 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001431 SourceLocation ConstructorLParen, ConstructorRParen;
1432
1433 if (Tok.is(tok::l_paren)) {
1434 ConstructorLParen = ConsumeParen();
1435 if (Tok.isNot(tok::r_paren)) {
1436 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001437 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1438 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001439 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001440 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001441 }
1442 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001443 if (ConstructorRParen.isInvalid()) {
1444 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001445 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001446 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001447 }
1448
Sebastian Redlf53597f2009-03-15 17:47:39 +00001449 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1450 move_arg(PlacementArgs), PlacementRParen,
1451 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1452 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001453}
1454
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001455/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1456/// passed to ParseDeclaratorInternal.
1457///
1458/// direct-new-declarator:
1459/// '[' expression ']'
1460/// direct-new-declarator '[' constant-expression ']'
1461///
Chris Lattner59232d32009-01-04 21:25:24 +00001462void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001463 // Parse the array dimensions.
1464 bool first = true;
1465 while (Tok.is(tok::l_square)) {
1466 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001467 OwningExprResult Size(first ? ParseExpression()
1468 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001469 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001470 // Recover
1471 SkipUntil(tok::r_square);
1472 return;
1473 }
1474 first = false;
1475
Sebastian Redlab197ba2009-02-09 18:23:29 +00001476 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001477 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001478 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001479 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001480
Sebastian Redlab197ba2009-02-09 18:23:29 +00001481 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001482 return;
1483 }
1484}
1485
1486/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1487/// This ambiguity appears in the syntax of the C++ new operator.
1488///
1489/// new-expression:
1490/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1491/// new-initializer[opt]
1492///
1493/// new-placement:
1494/// '(' expression-list ')'
1495///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001496bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001497 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001498 // The '(' was already consumed.
1499 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001500 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001501 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001502 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001503 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001504 }
1505
1506 // It's not a type, it has to be an expression list.
1507 // Discard the comma locations - ActOnCXXNew has enough parameters.
1508 CommaLocsTy CommaLocs;
1509 return ParseExpressionList(PlacementArgs, CommaLocs);
1510}
1511
1512/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1513/// to free memory allocated by new.
1514///
Chris Lattner59232d32009-01-04 21:25:24 +00001515/// This method is called to parse the 'delete' expression after the optional
1516/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1517/// and "Start" is its location. Otherwise, "Start" is the location of the
1518/// 'delete' token.
1519///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001520/// delete-expression:
1521/// '::'[opt] 'delete' cast-expression
1522/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001523Parser::OwningExprResult
1524Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1525 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1526 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001527
1528 // Array delete?
1529 bool ArrayDelete = false;
1530 if (Tok.is(tok::l_square)) {
1531 ArrayDelete = true;
1532 SourceLocation LHS = ConsumeBracket();
1533 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1534 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001535 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001536 }
1537
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001538 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001539 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001540 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001541
Sebastian Redlf53597f2009-03-15 17:47:39 +00001542 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001543}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001544
Mike Stump1eb44332009-09-09 15:08:12 +00001545static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001546 switch(kind) {
1547 default: assert(false && "Not a known unary type trait.");
1548 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1549 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1550 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1551 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1552 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1553 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1554 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1555 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1556 case tok::kw___is_abstract: return UTT_IsAbstract;
1557 case tok::kw___is_class: return UTT_IsClass;
1558 case tok::kw___is_empty: return UTT_IsEmpty;
1559 case tok::kw___is_enum: return UTT_IsEnum;
1560 case tok::kw___is_pod: return UTT_IsPOD;
1561 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1562 case tok::kw___is_union: return UTT_IsUnion;
1563 }
1564}
1565
1566/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1567/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1568/// templates.
1569///
1570/// primary-expression:
1571/// [GNU] unary-type-trait '(' type-id ')'
1572///
Mike Stump1eb44332009-09-09 15:08:12 +00001573Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001574 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1575 SourceLocation Loc = ConsumeToken();
1576
1577 SourceLocation LParen = Tok.getLocation();
1578 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1579 return ExprError();
1580
1581 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1582 // there will be cryptic errors about mismatched parentheses and missing
1583 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001584 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001585
1586 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1587
Douglas Gregor809070a2009-02-18 17:45:20 +00001588 if (Ty.isInvalid())
1589 return ExprError();
1590
1591 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001592}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001593
1594/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1595/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1596/// based on the context past the parens.
1597Parser::OwningExprResult
1598Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1599 TypeTy *&CastTy,
1600 SourceLocation LParenLoc,
1601 SourceLocation &RParenLoc) {
1602 assert(getLang().CPlusPlus && "Should only be called for C++!");
1603 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1604 assert(isTypeIdInParens() && "Not a type-id!");
1605
1606 OwningExprResult Result(Actions, true);
1607 CastTy = 0;
1608
1609 // We need to disambiguate a very ugly part of the C++ syntax:
1610 //
1611 // (T())x; - type-id
1612 // (T())*x; - type-id
1613 // (T())/x; - expression
1614 // (T()); - expression
1615 //
1616 // The bad news is that we cannot use the specialized tentative parser, since
1617 // it can only verify that the thing inside the parens can be parsed as
1618 // type-id, it is not useful for determining the context past the parens.
1619 //
1620 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001621 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001622 //
1623 // It uses a scheme similar to parsing inline methods. The parenthesized
1624 // tokens are cached, the context that follows is determined (possibly by
1625 // parsing a cast-expression), and then we re-introduce the cached tokens
1626 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001627
Mike Stump1eb44332009-09-09 15:08:12 +00001628 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001629 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001630
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001631 // Store the tokens of the parentheses. We will parse them after we determine
1632 // the context that follows them.
1633 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1634 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001635 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1636 return ExprError();
1637 }
1638
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001639 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001640 ParseAs = CompoundLiteral;
1641 } else {
1642 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001643 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1644 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1645 NotCastExpr = true;
1646 } else {
1647 // Try parsing the cast-expression that may follow.
1648 // If it is not a cast-expression, NotCastExpr will be true and no token
1649 // will be consumed.
1650 Result = ParseCastExpression(false/*isUnaryExpression*/,
1651 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001652 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001653 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001654
1655 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1656 // an expression.
1657 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001658 }
1659
Mike Stump1eb44332009-09-09 15:08:12 +00001660 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001661 Toks.push_back(Tok);
1662 // Re-enter the stored parenthesized tokens into the token stream, so we may
1663 // parse them now.
1664 PP.EnterTokenStream(Toks.data(), Toks.size(),
1665 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1666 // Drop the current token and bring the first cached one. It's the same token
1667 // as when we entered this function.
1668 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001669
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001670 if (ParseAs >= CompoundLiteral) {
1671 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001672
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001673 // Match the ')'.
1674 if (Tok.is(tok::r_paren))
1675 RParenLoc = ConsumeParen();
1676 else
1677 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001678
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001679 if (ParseAs == CompoundLiteral) {
1680 ExprType = CompoundLiteral;
1681 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1682 }
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001684 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1685 assert(ParseAs == CastExpr);
1686
1687 if (Ty.isInvalid())
1688 return ExprError();
1689
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001690 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001691
1692 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001693 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001694 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001695 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001696 return move(Result);
1697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001699 // Not a compound literal, and not followed by a cast-expression.
1700 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001701
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001702 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001703 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001704 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1705 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1706
1707 // Match the ')'.
1708 if (Result.isInvalid()) {
1709 SkipUntil(tok::r_paren);
1710 return ExprError();
1711 }
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001713 if (Tok.is(tok::r_paren))
1714 RParenLoc = ConsumeParen();
1715 else
1716 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1717
1718 return move(Result);
1719}