blob: a7e7648c1f56d8bc86078f4cea0d03bfac4b973b [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
Chris Lattner77cf72a2009-06-26 03:47:46 +0000112 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000113
114 UnqualifiedId TemplateName;
115 if (Tok.is(tok::identifier)) {
116 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
117
118 // If the next token is not '<', we may have a stray 'template' keyword.
119 // Complain and suggest removing the template keyword, but otherwise
120 // allow parsing to continue.
121 if (NextToken().isNot(tok::less)) {
122 Diag(NextToken().getLocation(),
123 diag::err_less_after_template_name_in_nested_name_spec)
124 << Tok.getIdentifierInfo()->getName()
125 << CodeModificationHint::CreateRemoval(SourceRange(TemplateKWLoc));
126 break;
127 }
128
129 // Consume the identifier.
130 ConsumeToken();
131 } else if (Tok.is(tok::kw_operator)) {
132 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
133 TemplateName))
134 break;
135
136 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId) {
137 Diag(TemplateName.getSourceRange().getBegin(),
138 diag::err_id_after_template_in_nested_name_spec)
139 << TemplateName.getSourceRange();
140 break;
141 } else if (Tok.isNot(tok::less)) {
142 std::string OperatorName = "operator ";
143 OperatorName += getOperatorSpelling(
144 TemplateName.OperatorFunctionId.Operator);
145 Diag(Tok.getLocation(),
146 diag::err_less_after_template_name_in_nested_name_spec)
147 << OperatorName
148 << TemplateName.getSourceRange();
149 break;
150 }
151 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000152 Diag(Tok.getLocation(),
Chris Lattner77cf72a2009-06-26 03:47:46 +0000153 diag::err_id_after_template_in_nested_name_spec)
154 << SourceRange(TemplateKWLoc);
155 break;
156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Mike Stump1eb44332009-09-09 15:08:12 +0000158 TemplateTy Template
Douglas Gregor014e88d2009-11-03 23:16:33 +0000159 = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000160 ObjectType);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000161 if (!Template)
162 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000163 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000164 &SS, TemplateName, TemplateKWLoc, false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000165 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Chris Lattner77cf72a2009-06-26 03:47:46 +0000167 continue;
168 }
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Douglas Gregor39a8de12009-02-25 19:37:18 +0000170 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000171 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000172 //
173 // simple-template-id '::'
174 //
175 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000176 // right kind (it should name a type or be dependent), and then
177 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000178 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000179 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
180
Mike Stump1eb44332009-09-09 15:08:12 +0000181 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000182 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000183 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000184
Mike Stump1eb44332009-09-09 15:08:12 +0000185 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000186 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000187 Token TypeToken = Tok;
188 ConsumeToken();
189 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
190 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Douglas Gregor39a8de12009-02-25 19:37:18 +0000192 if (!HasScopeSpecifier) {
193 SS.setBeginLoc(TypeToken.getLocation());
194 HasScopeSpecifier = true;
195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Douglas Gregor31a19b62009-04-01 21:51:26 +0000197 if (TypeToken.getAnnotationValue())
198 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000199 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000200 TypeToken.getAnnotationValue(),
201 TypeToken.getAnnotationRange(),
202 CCLoc));
203 else
204 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000205 SS.setEndLoc(CCLoc);
206 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattner67b9e832009-06-26 03:45:46 +0000209 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000210 }
211
Chris Lattner5c7f7862009-06-26 03:52:38 +0000212
213 // The rest of the nested-name-specifier possibilities start with
214 // tok::identifier.
215 if (Tok.isNot(tok::identifier))
216 break;
217
218 IdentifierInfo &II = *Tok.getIdentifierInfo();
219
220 // nested-name-specifier:
221 // type-name '::'
222 // namespace-name '::'
223 // nested-name-specifier identifier '::'
224 Token Next = NextToken();
225 if (Next.is(tok::coloncolon)) {
226 // We have an identifier followed by a '::'. Lookup this name
227 // as the name in a nested-name-specifier.
228 SourceLocation IdLoc = ConsumeToken();
229 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
230 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Chris Lattner5c7f7862009-06-26 03:52:38 +0000232 if (!HasScopeSpecifier) {
233 SS.setBeginLoc(IdLoc);
234 HasScopeSpecifier = true;
235 }
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattner5c7f7862009-06-26 03:52:38 +0000237 if (SS.isInvalid())
238 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Chris Lattner5c7f7862009-06-26 03:52:38 +0000240 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000241 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000242 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000243 SS.setEndLoc(CCLoc);
244 continue;
245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Chris Lattner5c7f7862009-06-26 03:52:38 +0000247 // nested-name-specifier:
248 // type-name '<'
249 if (Next.is(tok::less)) {
250 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000251 UnqualifiedId TemplateName;
252 TemplateName.setIdentifier(&II, Tok.getLocation());
253 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
254 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000255 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000256 EnteringContext,
257 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000258 // We have found a template name, so annotate this this token
259 // with a template-id annotation. We do not permit the
260 // template-id to be translated into a type annotation,
261 // because some clients (e.g., the parsing of class template
262 // specializations) still want to see the original template-id
263 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000264 ConsumeToken();
265 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
266 SourceLocation(), false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000267 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000268 continue;
269 }
270 }
271
Douglas Gregor39a8de12009-02-25 19:37:18 +0000272 // We don't have any tokens that form the beginning of a
273 // nested-name-specifier, so we're done.
274 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000275 }
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Douglas Gregor39a8de12009-02-25 19:37:18 +0000277 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000278}
279
280/// ParseCXXIdExpression - Handle id-expression.
281///
282/// id-expression:
283/// unqualified-id
284/// qualified-id
285///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000286/// qualified-id:
287/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
288/// '::' identifier
289/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000290/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000291///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000292/// NOTE: The standard specifies that, for qualified-id, the parser does not
293/// expect:
294///
295/// '::' conversion-function-id
296/// '::' '~' class-name
297///
298/// This may cause a slight inconsistency on diagnostics:
299///
300/// class C {};
301/// namespace A {}
302/// void f() {
303/// :: A :: ~ C(); // Some Sema error about using destructor with a
304/// // namespace.
305/// :: ~ C(); // Some Parser error like 'unexpected ~'.
306/// }
307///
308/// We simplify the parser a bit and make it work like:
309///
310/// qualified-id:
311/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
312/// '::' unqualified-id
313///
314/// That way Sema can handle and report similar errors for namespaces and the
315/// global scope.
316///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000317/// The isAddressOfOperand parameter indicates that this id-expression is a
318/// direct operand of the address-of operator. This is, besides member contexts,
319/// the only place where a qualified-id naming a non-static class member may
320/// appear.
321///
322Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000323 // qualified-id:
324 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
325 // '::' unqualified-id
326 //
327 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000328 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000329
330 UnqualifiedId Name;
331 if (ParseUnqualifiedId(SS,
332 /*EnteringContext=*/false,
333 /*AllowDestructorName=*/false,
334 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000335 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000336 Name))
337 return ExprError();
338
339 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
340 isAddressOfOperand);
341
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000342}
343
Reid Spencer5f016e22007-07-11 17:01:13 +0000344/// ParseCXXCasts - This handles the various ways to cast expressions to another
345/// type.
346///
347/// postfix-expression: [C++ 5.2p1]
348/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
349/// 'static_cast' '<' type-name '>' '(' expression ')'
350/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
351/// 'const_cast' '<' type-name '>' '(' expression ')'
352///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000353Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 tok::TokenKind Kind = Tok.getKind();
355 const char *CastName = 0; // For error messages
356
357 switch (Kind) {
358 default: assert(0 && "Unknown C++ cast!"); abort();
359 case tok::kw_const_cast: CastName = "const_cast"; break;
360 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
361 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
362 case tok::kw_static_cast: CastName = "static_cast"; break;
363 }
364
365 SourceLocation OpLoc = ConsumeToken();
366 SourceLocation LAngleBracketLoc = Tok.getLocation();
367
368 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000369 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000370
Douglas Gregor809070a2009-02-18 17:45:20 +0000371 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 SourceLocation RAngleBracketLoc = Tok.getLocation();
373
Chris Lattner1ab3b962008-11-18 07:48:38 +0000374 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000375 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000376
377 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
378
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000379 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
380 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000381
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000382 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000384 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000385 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000386
Douglas Gregor809070a2009-02-18 17:45:20 +0000387 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000388 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000389 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000390 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000391 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000392
Sebastian Redl20df9b72008-12-11 22:51:44 +0000393 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000394}
395
Sebastian Redlc42e1182008-11-11 11:37:55 +0000396/// ParseCXXTypeid - This handles the C++ typeid expression.
397///
398/// postfix-expression: [C++ 5.2p1]
399/// 'typeid' '(' expression ')'
400/// 'typeid' '(' type-id ')'
401///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000402Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000403 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
404
405 SourceLocation OpLoc = ConsumeToken();
406 SourceLocation LParenLoc = Tok.getLocation();
407 SourceLocation RParenLoc;
408
409 // typeid expressions are always parenthesized.
410 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
411 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000412 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000413
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000414 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000415
416 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000417 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000418
419 // Match the ')'.
420 MatchRHSPunctuation(tok::r_paren, LParenLoc);
421
Douglas Gregor809070a2009-02-18 17:45:20 +0000422 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000423 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000424
425 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000426 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000427 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000428 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000429 // When typeid is applied to an expression other than an lvalue of a
430 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000431 // operand (Clause 5).
432 //
Mike Stump1eb44332009-09-09 15:08:12 +0000433 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000434 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000435 // we the expression is potentially potentially evaluated.
436 EnterExpressionEvaluationContext Unevaluated(Actions,
437 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000438 Result = ParseExpression();
439
440 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000441 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000442 SkipUntil(tok::r_paren);
443 else {
444 MatchRHSPunctuation(tok::r_paren, LParenLoc);
445
446 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000447 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000448 }
449 }
450
Sebastian Redl20df9b72008-12-11 22:51:44 +0000451 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000452}
453
Reid Spencer5f016e22007-07-11 17:01:13 +0000454/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
455///
456/// boolean-literal: [C++ 2.13.5]
457/// 'true'
458/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000459Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000461 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000462}
Chris Lattner50dd2892008-02-26 00:51:44 +0000463
464/// ParseThrowExpression - This handles the C++ throw expression.
465///
466/// throw-expression: [C++ 15]
467/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000468Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000469 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000470 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000471
Chris Lattner2a2819a2008-04-06 06:02:23 +0000472 // If the current token isn't the start of an assignment-expression,
473 // then the expression is not present. This handles things like:
474 // "C ? throw : (void)42", which is crazy but legal.
475 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
476 case tok::semi:
477 case tok::r_paren:
478 case tok::r_square:
479 case tok::r_brace:
480 case tok::colon:
481 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000482 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000483
Chris Lattner2a2819a2008-04-06 06:02:23 +0000484 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000485 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000486 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000487 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000488 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000489}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000490
491/// ParseCXXThis - This handles the C++ 'this' pointer.
492///
493/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
494/// a non-lvalue expression whose value is the address of the object for which
495/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000496Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000497 assert(Tok.is(tok::kw_this) && "Not 'this'!");
498 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000499 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000500}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000501
502/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
503/// Can be interpreted either as function-style casting ("int(x)")
504/// or class type construction ("ClassType(x,y,z)")
505/// or creation of a value-initialized type ("int()").
506///
507/// postfix-expression: [C++ 5.2p1]
508/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
509/// typename-specifier '(' expression-list[opt] ')' [TODO]
510///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000511Parser::OwningExprResult
512Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000513 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000514 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000515
516 assert(Tok.is(tok::l_paren) && "Expected '('!");
517 SourceLocation LParenLoc = ConsumeParen();
518
Sebastian Redla55e52c2008-11-25 22:21:31 +0000519 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000520 CommaLocsTy CommaLocs;
521
522 if (Tok.isNot(tok::r_paren)) {
523 if (ParseExpressionList(Exprs, CommaLocs)) {
524 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000525 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000526 }
527 }
528
529 // Match the ')'.
530 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
531
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000532 // TypeRep could be null, if it references an invalid typedef.
533 if (!TypeRep)
534 return ExprError();
535
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000536 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
537 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000538 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
539 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000540 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000541}
542
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000543/// ParseCXXCondition - if/switch/while/for condition expression.
544///
545/// condition:
546/// expression
547/// type-specifier-seq declarator '=' assignment-expression
548/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
549/// '=' assignment-expression
550///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000551Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000552 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000553 return ParseExpression(); // expression
554
555 SourceLocation StartLoc = Tok.getLocation();
556
557 // type-specifier-seq
558 DeclSpec DS;
559 ParseSpecifierQualifierList(DS);
560
561 // declarator
562 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
563 ParseDeclarator(DeclaratorInfo);
564
565 // simple-asm-expr[opt]
566 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000567 SourceLocation Loc;
568 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000569 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000570 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000571 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000572 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000573 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000574 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000575 }
576
577 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000578 if (Tok.is(tok::kw___attribute)) {
579 SourceLocation Loc;
580 AttributeList *AttrList = ParseAttributes(&Loc);
581 DeclaratorInfo.AddAttributes(AttrList, Loc);
582 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000583
584 // '=' assignment-expression
585 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000586 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000587 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000588 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000589 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000590 return ExprError();
591
Sebastian Redlf53597f2009-03-15 17:47:39 +0000592 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
593 DeclaratorInfo,EqualLoc,
594 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000595}
596
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000597/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
598/// This should only be called when the current token is known to be part of
599/// simple-type-specifier.
600///
601/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000602/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000603/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
604/// char
605/// wchar_t
606/// bool
607/// short
608/// int
609/// long
610/// signed
611/// unsigned
612/// float
613/// double
614/// void
615/// [GNU] typeof-specifier
616/// [C++0x] auto [TODO]
617///
618/// type-name:
619/// class-name
620/// enum-name
621/// typedef-name
622///
623void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
624 DS.SetRangeStart(Tok.getLocation());
625 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000626 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000627 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000629 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000630 case tok::identifier: // foo::bar
631 case tok::coloncolon: // ::foo::bar
632 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000633 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000634 assert(0 && "Not a simple-type-specifier token!");
635 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000636
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000637 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000638 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000639 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000640 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000641 break;
642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000644 // builtin types
645 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000646 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000647 break;
648 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000649 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000650 break;
651 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000652 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000653 break;
654 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000655 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000656 break;
657 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000658 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000659 break;
660 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000661 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000662 break;
663 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000664 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000665 break;
666 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000667 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000668 break;
669 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000670 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000671 break;
672 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000673 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000674 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000675 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000676 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000677 break;
678 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000679 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000680 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000681 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000682 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000683 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000685 // GNU typeof support.
686 case tok::kw_typeof:
687 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000688 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000689 return;
690 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000691 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000692 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
693 else
694 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000695 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000696 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000697}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000698
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000699/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
700/// [dcl.name]), which is a non-empty sequence of type-specifiers,
701/// e.g., "const short int". Note that the DeclSpec is *not* finished
702/// by parsing the type-specifier-seq, because these sequences are
703/// typically followed by some form of declarator. Returns true and
704/// emits diagnostics if this is not a type-specifier-seq, false
705/// otherwise.
706///
707/// type-specifier-seq: [C++ 8.1]
708/// type-specifier type-specifier-seq[opt]
709///
710bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
711 DS.SetRangeStart(Tok.getLocation());
712 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000713 unsigned DiagID;
714 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000715
716 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000717 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000718 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000719 return true;
720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721
John McCallfec54012009-08-03 20:12:06 +0000722 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000723
724 return false;
725}
726
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000727/// \brief Finish parsing a C++ unqualified-id that is a template-id of
728/// some form.
729///
730/// This routine is invoked when a '<' is encountered after an identifier or
731/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
732/// whether the unqualified-id is actually a template-id. This routine will
733/// then parse the template arguments and form the appropriate template-id to
734/// return to the caller.
735///
736/// \param SS the nested-name-specifier that precedes this template-id, if
737/// we're actually parsing a qualified-id.
738///
739/// \param Name for constructor and destructor names, this is the actual
740/// identifier that may be a template-name.
741///
742/// \param NameLoc the location of the class-name in a constructor or
743/// destructor.
744///
745/// \param EnteringContext whether we're entering the scope of the
746/// nested-name-specifier.
747///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000748/// \param ObjectType if this unqualified-id occurs within a member access
749/// expression, the type of the base object whose member is being accessed.
750///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000751/// \param Id as input, describes the template-name or operator-function-id
752/// that precedes the '<'. If template arguments were parsed successfully,
753/// will be updated with the template-id.
754///
755/// \returns true if a parse error occurred, false otherwise.
756bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
757 IdentifierInfo *Name,
758 SourceLocation NameLoc,
759 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000760 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000761 UnqualifiedId &Id) {
762 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
763
764 TemplateTy Template;
765 TemplateNameKind TNK = TNK_Non_template;
766 switch (Id.getKind()) {
767 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000768 case UnqualifiedId::IK_OperatorFunctionId:
769 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
770 Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000771 break;
772
Douglas Gregor014e88d2009-11-03 23:16:33 +0000773 case UnqualifiedId::IK_ConstructorName: {
774 UnqualifiedId TemplateName;
775 TemplateName.setIdentifier(Name, NameLoc);
776 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
777 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000778 break;
779 }
780
Douglas Gregor014e88d2009-11-03 23:16:33 +0000781 case UnqualifiedId::IK_DestructorName: {
782 UnqualifiedId TemplateName;
783 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000784 if (ObjectType) {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000785 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
786 TemplateName, ObjectType);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000787 TNK = TNK_Dependent_template_name;
788 if (!Template.get())
789 return true;
790 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000791 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000792 EnteringContext, Template);
793
794 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
795 // The identifier following the destructor did not refer to a template
796 // or to a type. Complain.
797 if (ObjectType)
798 Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
799 << Name;
800 else
801 Diag(NameLoc, diag::err_destructor_class_name);
802 return true;
803 }
804 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000805 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000806 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000807
808 default:
809 return false;
810 }
811
812 if (TNK == TNK_Non_template)
813 return false;
814
815 // Parse the enclosed template argument list.
816 SourceLocation LAngleLoc, RAngleLoc;
817 TemplateArgList TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000818 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
819 &SS, true, LAngleLoc,
820 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000821 RAngleLoc))
822 return true;
823
824 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
825 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) {
826 // Form a parsed representation of the template-id to be stored in the
827 // UnqualifiedId.
828 TemplateIdAnnotation *TemplateId
829 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
830
831 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
832 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000833 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000834 TemplateId->TemplateNameLoc = Id.StartLocation;
835 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000836 TemplateId->Name = 0;
837 TemplateId->Operator = Id.OperatorFunctionId.Operator;
838 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000839 }
840
841 TemplateId->Template = Template.getAs<void*>();
842 TemplateId->Kind = TNK;
843 TemplateId->LAngleLoc = LAngleLoc;
844 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +0000845 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000846 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +0000847 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000848 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000849
850 Id.setTemplateId(TemplateId);
851 return false;
852 }
853
854 // Bundle the template arguments together.
855 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000856 TemplateArgs.size());
857
858 // Constructor and destructor names.
859 Action::TypeResult Type
860 = Actions.ActOnTemplateIdType(Template, NameLoc,
861 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000862 RAngleLoc);
863 if (Type.isInvalid())
864 return true;
865
866 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
867 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
868 else
869 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
870
871 return false;
872}
873
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000874/// \brief Parse an operator-function-id or conversion-function-id as part
875/// of a C++ unqualified-id.
876///
877/// This routine is responsible only for parsing the operator-function-id or
878/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000879///
880/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000881/// operator-function-id: [C++ 13.5]
882/// 'operator' operator
883///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000884/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000885/// new delete new[] delete[]
886/// + - * / % ^ & | ~
887/// ! = < > += -= *= /= %=
888/// ^= &= |= << >> >>= <<= == !=
889/// <= >= && || ++ -- , ->* ->
890/// () []
891///
892/// conversion-function-id: [C++ 12.3.2]
893/// operator conversion-type-id
894///
895/// conversion-type-id:
896/// type-specifier-seq conversion-declarator[opt]
897///
898/// conversion-declarator:
899/// ptr-operator conversion-declarator[opt]
900/// \endcode
901///
902/// \param The nested-name-specifier that preceded this unqualified-id. If
903/// non-empty, then we are parsing the unqualified-id of a qualified-id.
904///
905/// \param EnteringContext whether we are entering the scope of the
906/// nested-name-specifier.
907///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000908/// \param ObjectType if this unqualified-id occurs within a member access
909/// expression, the type of the base object whose member is being accessed.
910///
911/// \param Result on a successful parse, contains the parsed unqualified-id.
912///
913/// \returns true if parsing fails, false otherwise.
914bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
915 TypeTy *ObjectType,
916 UnqualifiedId &Result) {
917 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
918
919 // Consume the 'operator' keyword.
920 SourceLocation KeywordLoc = ConsumeToken();
921
922 // Determine what kind of operator name we have.
923 unsigned SymbolIdx = 0;
924 SourceLocation SymbolLocations[3];
925 OverloadedOperatorKind Op = OO_None;
926 switch (Tok.getKind()) {
927 case tok::kw_new:
928 case tok::kw_delete: {
929 bool isNew = Tok.getKind() == tok::kw_new;
930 // Consume the 'new' or 'delete'.
931 SymbolLocations[SymbolIdx++] = ConsumeToken();
932 if (Tok.is(tok::l_square)) {
933 // Consume the '['.
934 SourceLocation LBracketLoc = ConsumeBracket();
935 // Consume the ']'.
936 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
937 LBracketLoc);
938 if (RBracketLoc.isInvalid())
939 return true;
940
941 SymbolLocations[SymbolIdx++] = LBracketLoc;
942 SymbolLocations[SymbolIdx++] = RBracketLoc;
943 Op = isNew? OO_Array_New : OO_Array_Delete;
944 } else {
945 Op = isNew? OO_New : OO_Delete;
946 }
947 break;
948 }
949
950#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
951 case tok::Token: \
952 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
953 Op = OO_##Name; \
954 break;
955#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
956#include "clang/Basic/OperatorKinds.def"
957
958 case tok::l_paren: {
959 // Consume the '('.
960 SourceLocation LParenLoc = ConsumeParen();
961 // Consume the ')'.
962 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
963 LParenLoc);
964 if (RParenLoc.isInvalid())
965 return true;
966
967 SymbolLocations[SymbolIdx++] = LParenLoc;
968 SymbolLocations[SymbolIdx++] = RParenLoc;
969 Op = OO_Call;
970 break;
971 }
972
973 case tok::l_square: {
974 // Consume the '['.
975 SourceLocation LBracketLoc = ConsumeBracket();
976 // Consume the ']'.
977 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
978 LBracketLoc);
979 if (RBracketLoc.isInvalid())
980 return true;
981
982 SymbolLocations[SymbolIdx++] = LBracketLoc;
983 SymbolLocations[SymbolIdx++] = RBracketLoc;
984 Op = OO_Subscript;
985 break;
986 }
987
988 case tok::code_completion: {
989 // Code completion for the operator name.
990 Actions.CodeCompleteOperatorName(CurScope);
991
992 // Consume the operator token.
993 ConsumeToken();
994
995 // Don't try to parse any further.
996 return true;
997 }
998
999 default:
1000 break;
1001 }
1002
1003 if (Op != OO_None) {
1004 // We have parsed an operator-function-id.
1005 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1006 return false;
1007 }
1008
1009 // Parse a conversion-function-id.
1010 //
1011 // conversion-function-id: [C++ 12.3.2]
1012 // operator conversion-type-id
1013 //
1014 // conversion-type-id:
1015 // type-specifier-seq conversion-declarator[opt]
1016 //
1017 // conversion-declarator:
1018 // ptr-operator conversion-declarator[opt]
1019
1020 // Parse the type-specifier-seq.
1021 DeclSpec DS;
1022 if (ParseCXXTypeSpecifierSeq(DS))
1023 return true;
1024
1025 // Parse the conversion-declarator, which is merely a sequence of
1026 // ptr-operators.
1027 Declarator D(DS, Declarator::TypeNameContext);
1028 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1029
1030 // Finish up the type.
1031 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1032 if (Ty.isInvalid())
1033 return true;
1034
1035 // Note that this is a conversion-function-id.
1036 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1037 D.getSourceRange().getEnd());
1038 return false;
1039}
1040
1041/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1042/// name of an entity.
1043///
1044/// \code
1045/// unqualified-id: [C++ expr.prim.general]
1046/// identifier
1047/// operator-function-id
1048/// conversion-function-id
1049/// [C++0x] literal-operator-id [TODO]
1050/// ~ class-name
1051/// template-id
1052///
1053/// \endcode
1054///
1055/// \param The nested-name-specifier that preceded this unqualified-id. If
1056/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1057///
1058/// \param EnteringContext whether we are entering the scope of the
1059/// nested-name-specifier.
1060///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001061/// \param AllowDestructorName whether we allow parsing of a destructor name.
1062///
1063/// \param AllowConstructorName whether we allow parsing a constructor name.
1064///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001065/// \param ObjectType if this unqualified-id occurs within a member access
1066/// expression, the type of the base object whose member is being accessed.
1067///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001068/// \param Result on a successful parse, contains the parsed unqualified-id.
1069///
1070/// \returns true if parsing fails, false otherwise.
1071bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1072 bool AllowDestructorName,
1073 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001074 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001075 UnqualifiedId &Result) {
1076 // unqualified-id:
1077 // identifier
1078 // template-id (when it hasn't already been annotated)
1079 if (Tok.is(tok::identifier)) {
1080 // Consume the identifier.
1081 IdentifierInfo *Id = Tok.getIdentifierInfo();
1082 SourceLocation IdLoc = ConsumeToken();
1083
1084 if (AllowConstructorName &&
1085 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1086 // We have parsed a constructor name.
1087 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1088 &SS, false),
1089 IdLoc, IdLoc);
1090 } else {
1091 // We have parsed an identifier.
1092 Result.setIdentifier(Id, IdLoc);
1093 }
1094
1095 // If the next token is a '<', we may have a template.
1096 if (Tok.is(tok::less))
1097 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001098 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001099
1100 return false;
1101 }
1102
1103 // unqualified-id:
1104 // template-id (already parsed and annotated)
1105 if (Tok.is(tok::annot_template_id)) {
1106 // FIXME: Could this be a constructor name???
1107
1108 // We have already parsed a template-id; consume the annotation token as
1109 // our unqualified-id.
1110 Result.setTemplateId(
1111 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1112 ConsumeToken();
1113 return false;
1114 }
1115
1116 // unqualified-id:
1117 // operator-function-id
1118 // conversion-function-id
1119 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001120 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001121 return true;
1122
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001123 // If we have an operator-function-id and the next token is a '<', we may
1124 // have a
1125 //
1126 // template-id:
1127 // operator-function-id < template-argument-list[opt] >
1128 if (Result.getKind() == UnqualifiedId::IK_OperatorFunctionId &&
1129 Tok.is(tok::less))
1130 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1131 EnteringContext, ObjectType,
1132 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001133
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001134 return false;
1135 }
1136
1137 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1138 // C++ [expr.unary.op]p10:
1139 // There is an ambiguity in the unary-expression ~X(), where X is a
1140 // class-name. The ambiguity is resolved in favor of treating ~ as a
1141 // unary complement rather than treating ~X as referring to a destructor.
1142
1143 // Parse the '~'.
1144 SourceLocation TildeLoc = ConsumeToken();
1145
1146 // Parse the class-name.
1147 if (Tok.isNot(tok::identifier)) {
1148 Diag(Tok, diag::err_destructor_class_name);
1149 return true;
1150 }
1151
1152 // Parse the class-name (or template-name in a simple-template-id).
1153 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1154 SourceLocation ClassNameLoc = ConsumeToken();
1155
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001156 if (Tok.is(tok::less)) {
1157 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1158 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1159 EnteringContext, ObjectType, Result);
1160 }
1161
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001162 // Note that this is a destructor name.
1163 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
1164 CurScope, &SS);
1165 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001166 if (ObjectType)
1167 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1168 << ClassName;
1169 else
1170 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001171 return true;
1172 }
1173
1174 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001175 return false;
1176 }
1177
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001178 Diag(Tok, diag::err_expected_unqualified_id)
1179 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001180 return true;
1181}
1182
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001183/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1184/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001185///
Chris Lattner59232d32009-01-04 21:25:24 +00001186/// This method is called to parse the new expression after the optional :: has
1187/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1188/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001189///
1190/// new-expression:
1191/// '::'[opt] 'new' new-placement[opt] new-type-id
1192/// new-initializer[opt]
1193/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1194/// new-initializer[opt]
1195///
1196/// new-placement:
1197/// '(' expression-list ')'
1198///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001199/// new-type-id:
1200/// type-specifier-seq new-declarator[opt]
1201///
1202/// new-declarator:
1203/// ptr-operator new-declarator[opt]
1204/// direct-new-declarator
1205///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001206/// new-initializer:
1207/// '(' expression-list[opt] ')'
1208/// [C++0x] braced-init-list [TODO]
1209///
Chris Lattner59232d32009-01-04 21:25:24 +00001210Parser::OwningExprResult
1211Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1212 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1213 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001214
1215 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1216 // second form of new-expression. It can't be a new-type-id.
1217
Sebastian Redla55e52c2008-11-25 22:21:31 +00001218 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001219 SourceLocation PlacementLParen, PlacementRParen;
1220
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001221 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001222 DeclSpec DS;
1223 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001224 if (Tok.is(tok::l_paren)) {
1225 // If it turns out to be a placement, we change the type location.
1226 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001227 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1228 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001229 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001230 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001231
1232 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001233 if (PlacementRParen.isInvalid()) {
1234 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001235 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001236 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001237
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001238 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001239 // Reset the placement locations. There was no placement.
1240 PlacementLParen = PlacementRParen = SourceLocation();
1241 ParenTypeId = true;
1242 } else {
1243 // We still need the type.
1244 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001245 SourceLocation LParen = ConsumeParen();
1246 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001247 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001248 ParseDeclarator(DeclaratorInfo);
1249 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001250 ParenTypeId = true;
1251 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001252 if (ParseCXXTypeSpecifierSeq(DS))
1253 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001254 else {
1255 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001256 ParseDeclaratorInternal(DeclaratorInfo,
1257 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001258 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001259 ParenTypeId = false;
1260 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001261 }
1262 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001263 // A new-type-id is a simplified type-id, where essentially the
1264 // direct-declarator is replaced by a direct-new-declarator.
1265 if (ParseCXXTypeSpecifierSeq(DS))
1266 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001267 else {
1268 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001269 ParseDeclaratorInternal(DeclaratorInfo,
1270 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001271 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001272 ParenTypeId = false;
1273 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001274 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001275 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001276 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001277 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001278
Sebastian Redla55e52c2008-11-25 22:21:31 +00001279 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001280 SourceLocation ConstructorLParen, ConstructorRParen;
1281
1282 if (Tok.is(tok::l_paren)) {
1283 ConstructorLParen = ConsumeParen();
1284 if (Tok.isNot(tok::r_paren)) {
1285 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001286 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1287 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001288 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001289 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001290 }
1291 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001292 if (ConstructorRParen.isInvalid()) {
1293 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001294 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001295 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001296 }
1297
Sebastian Redlf53597f2009-03-15 17:47:39 +00001298 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1299 move_arg(PlacementArgs), PlacementRParen,
1300 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1301 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001302}
1303
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001304/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1305/// passed to ParseDeclaratorInternal.
1306///
1307/// direct-new-declarator:
1308/// '[' expression ']'
1309/// direct-new-declarator '[' constant-expression ']'
1310///
Chris Lattner59232d32009-01-04 21:25:24 +00001311void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001312 // Parse the array dimensions.
1313 bool first = true;
1314 while (Tok.is(tok::l_square)) {
1315 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001316 OwningExprResult Size(first ? ParseExpression()
1317 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001318 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001319 // Recover
1320 SkipUntil(tok::r_square);
1321 return;
1322 }
1323 first = false;
1324
Sebastian Redlab197ba2009-02-09 18:23:29 +00001325 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001326 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001327 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001328 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001329
Sebastian Redlab197ba2009-02-09 18:23:29 +00001330 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001331 return;
1332 }
1333}
1334
1335/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1336/// This ambiguity appears in the syntax of the C++ new operator.
1337///
1338/// new-expression:
1339/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1340/// new-initializer[opt]
1341///
1342/// new-placement:
1343/// '(' expression-list ')'
1344///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001345bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001346 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001347 // The '(' was already consumed.
1348 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001349 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001350 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001351 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001352 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001353 }
1354
1355 // It's not a type, it has to be an expression list.
1356 // Discard the comma locations - ActOnCXXNew has enough parameters.
1357 CommaLocsTy CommaLocs;
1358 return ParseExpressionList(PlacementArgs, CommaLocs);
1359}
1360
1361/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1362/// to free memory allocated by new.
1363///
Chris Lattner59232d32009-01-04 21:25:24 +00001364/// This method is called to parse the 'delete' expression after the optional
1365/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1366/// and "Start" is its location. Otherwise, "Start" is the location of the
1367/// 'delete' token.
1368///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001369/// delete-expression:
1370/// '::'[opt] 'delete' cast-expression
1371/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001372Parser::OwningExprResult
1373Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1374 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1375 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001376
1377 // Array delete?
1378 bool ArrayDelete = false;
1379 if (Tok.is(tok::l_square)) {
1380 ArrayDelete = true;
1381 SourceLocation LHS = ConsumeBracket();
1382 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1383 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001384 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001385 }
1386
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001387 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001388 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001389 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001390
Sebastian Redlf53597f2009-03-15 17:47:39 +00001391 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001392}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001393
Mike Stump1eb44332009-09-09 15:08:12 +00001394static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001395 switch(kind) {
1396 default: assert(false && "Not a known unary type trait.");
1397 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1398 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1399 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1400 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1401 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1402 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1403 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1404 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1405 case tok::kw___is_abstract: return UTT_IsAbstract;
1406 case tok::kw___is_class: return UTT_IsClass;
1407 case tok::kw___is_empty: return UTT_IsEmpty;
1408 case tok::kw___is_enum: return UTT_IsEnum;
1409 case tok::kw___is_pod: return UTT_IsPOD;
1410 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1411 case tok::kw___is_union: return UTT_IsUnion;
1412 }
1413}
1414
1415/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1416/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1417/// templates.
1418///
1419/// primary-expression:
1420/// [GNU] unary-type-trait '(' type-id ')'
1421///
Mike Stump1eb44332009-09-09 15:08:12 +00001422Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001423 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1424 SourceLocation Loc = ConsumeToken();
1425
1426 SourceLocation LParen = Tok.getLocation();
1427 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1428 return ExprError();
1429
1430 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1431 // there will be cryptic errors about mismatched parentheses and missing
1432 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001433 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001434
1435 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1436
Douglas Gregor809070a2009-02-18 17:45:20 +00001437 if (Ty.isInvalid())
1438 return ExprError();
1439
1440 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001441}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001442
1443/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1444/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1445/// based on the context past the parens.
1446Parser::OwningExprResult
1447Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1448 TypeTy *&CastTy,
1449 SourceLocation LParenLoc,
1450 SourceLocation &RParenLoc) {
1451 assert(getLang().CPlusPlus && "Should only be called for C++!");
1452 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1453 assert(isTypeIdInParens() && "Not a type-id!");
1454
1455 OwningExprResult Result(Actions, true);
1456 CastTy = 0;
1457
1458 // We need to disambiguate a very ugly part of the C++ syntax:
1459 //
1460 // (T())x; - type-id
1461 // (T())*x; - type-id
1462 // (T())/x; - expression
1463 // (T()); - expression
1464 //
1465 // The bad news is that we cannot use the specialized tentative parser, since
1466 // it can only verify that the thing inside the parens can be parsed as
1467 // type-id, it is not useful for determining the context past the parens.
1468 //
1469 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001470 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001471 //
1472 // It uses a scheme similar to parsing inline methods. The parenthesized
1473 // tokens are cached, the context that follows is determined (possibly by
1474 // parsing a cast-expression), and then we re-introduce the cached tokens
1475 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001476
Mike Stump1eb44332009-09-09 15:08:12 +00001477 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001478 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001479
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001480 // Store the tokens of the parentheses. We will parse them after we determine
1481 // the context that follows them.
1482 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1483 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001484 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1485 return ExprError();
1486 }
1487
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001488 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001489 ParseAs = CompoundLiteral;
1490 } else {
1491 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001492 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1493 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1494 NotCastExpr = true;
1495 } else {
1496 // Try parsing the cast-expression that may follow.
1497 // If it is not a cast-expression, NotCastExpr will be true and no token
1498 // will be consumed.
1499 Result = ParseCastExpression(false/*isUnaryExpression*/,
1500 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001501 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001502 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001503
1504 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1505 // an expression.
1506 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001507 }
1508
Mike Stump1eb44332009-09-09 15:08:12 +00001509 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001510 Toks.push_back(Tok);
1511 // Re-enter the stored parenthesized tokens into the token stream, so we may
1512 // parse them now.
1513 PP.EnterTokenStream(Toks.data(), Toks.size(),
1514 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1515 // Drop the current token and bring the first cached one. It's the same token
1516 // as when we entered this function.
1517 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001518
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001519 if (ParseAs >= CompoundLiteral) {
1520 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001521
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001522 // Match the ')'.
1523 if (Tok.is(tok::r_paren))
1524 RParenLoc = ConsumeParen();
1525 else
1526 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001527
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001528 if (ParseAs == CompoundLiteral) {
1529 ExprType = CompoundLiteral;
1530 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1531 }
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001533 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1534 assert(ParseAs == CastExpr);
1535
1536 if (Ty.isInvalid())
1537 return ExprError();
1538
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001539 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001540
1541 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001542 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001543 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001544 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001545 return move(Result);
1546 }
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001548 // Not a compound literal, and not followed by a cast-expression.
1549 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001550
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001551 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001552 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001553 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1554 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1555
1556 // Match the ')'.
1557 if (Result.isInvalid()) {
1558 SkipUntil(tok::r_paren);
1559 return ExprError();
1560 }
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001562 if (Tok.is(tok::r_paren))
1563 RParenLoc = ConsumeParen();
1564 else
1565 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1566
1567 return move(Result);
1568}