blob: b2ecc9e827f1db8c9cebfc5b4bd6ebda7450ebbc [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000018#include "llvm/Support/ErrorHandling.h"
19
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Mike Stump1eb44332009-09-09 15:08:12 +000022/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000023///
24/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000025/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000026/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000027///
28/// '::'[opt] nested-name-specifier
29/// '::'
30///
31/// nested-name-specifier:
32/// type-name '::'
33/// namespace-name '::'
34/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000035/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000036///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000037///
Mike Stump1eb44332009-09-09 15:08:12 +000038/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000039/// nested-name-specifier (or empty)
40///
Mike Stump1eb44332009-09-09 15:08:12 +000041/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000042/// the "." or "->" of a member access expression, this parameter provides the
43/// type of the object whose members are being accessed.
44///
45/// \param EnteringContext whether we will be entering into the context of
46/// the nested-name-specifier after parsing it.
47///
48/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000049bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000050 Action::TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +000051 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000052 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000053 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000054
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000055 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000056 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000057 SS.setRange(Tok.getAnnotationRange());
58 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000059 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000060 }
Chris Lattnere607e802009-01-04 21:14:15 +000061
Douglas Gregor39a8de12009-02-25 19:37:18 +000062 bool HasScopeSpecifier = false;
63
Chris Lattner5b454732009-01-05 03:55:46 +000064 if (Tok.is(tok::coloncolon)) {
65 // ::new and ::delete aren't nested-name-specifiers.
66 tok::TokenKind NextKind = NextToken().getKind();
67 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
68 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Chris Lattner55a7cef2009-01-05 00:13:00 +000070 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000071 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000072 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000073 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000074 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000075 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000076 }
77
Douglas Gregor39a8de12009-02-25 19:37:18 +000078 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000079 if (HasScopeSpecifier) {
80 // C++ [basic.lookup.classref]p5:
81 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000082 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000083 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000084 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000085 // the class-name-or-namespace-name is looked up in global scope as a
86 // class-name or namespace-name.
87 //
88 // To implement this, we clear out the object type as soon as we've
89 // seen a leading '::' or part of a nested-name-specifier.
90 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +000091
92 if (Tok.is(tok::code_completion)) {
93 // Code completion for a nested-name-specifier, where the code
94 // code completion token follows the '::'.
95 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
96 ConsumeToken();
97 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +000098 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Douglas Gregor39a8de12009-02-25 19:37:18 +0000100 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000101 // nested-name-specifier 'template'[opt] simple-template-id '::'
102
103 // Parse the optional 'template' keyword, then make sure we have
104 // 'identifier <' after it.
105 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000106 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000107 // nested-name-specifier, since they aren't allowed to start with
108 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000109 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000110 break;
111
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000112 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000113 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000114
115 UnqualifiedId TemplateName;
116 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000117 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000118 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000119 ConsumeToken();
120 } else if (Tok.is(tok::kw_operator)) {
121 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000122 TemplateName)) {
123 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000124 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000125 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000126
127 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId) {
128 Diag(TemplateName.getSourceRange().getBegin(),
129 diag::err_id_after_template_in_nested_name_spec)
130 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000131 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000132 break;
133 }
134 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000135 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000136 break;
137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000139 // If the next token is not '<', we have a qualified-id that refers
140 // to a template name, such as T::template apply, but is not a
141 // template-id.
142 if (Tok.isNot(tok::less)) {
143 TPA.Revert();
144 break;
145 }
146
147 // Commit to parsing the template-id.
148 TPA.Commit();
Mike Stump1eb44332009-09-09 15:08:12 +0000149 TemplateTy Template
Douglas Gregor014e88d2009-11-03 23:16:33 +0000150 = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000151 ObjectType);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000152 if (!Template)
153 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000154 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000155 &SS, TemplateName, TemplateKWLoc, false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000156 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattner77cf72a2009-06-26 03:47:46 +0000158 continue;
159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Douglas Gregor39a8de12009-02-25 19:37:18 +0000161 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000162 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000163 //
164 // simple-template-id '::'
165 //
166 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000167 // right kind (it should name a type or be dependent), and then
168 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000169 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000170 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
171
Mike Stump1eb44332009-09-09 15:08:12 +0000172 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000173 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000174 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000175
Mike Stump1eb44332009-09-09 15:08:12 +0000176 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000177 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000178 Token TypeToken = Tok;
179 ConsumeToken();
180 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
181 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Douglas Gregor39a8de12009-02-25 19:37:18 +0000183 if (!HasScopeSpecifier) {
184 SS.setBeginLoc(TypeToken.getLocation());
185 HasScopeSpecifier = true;
186 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Douglas Gregor31a19b62009-04-01 21:51:26 +0000188 if (TypeToken.getAnnotationValue())
189 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000190 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000191 TypeToken.getAnnotationValue(),
192 TypeToken.getAnnotationRange(),
193 CCLoc));
194 else
195 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000196 SS.setEndLoc(CCLoc);
197 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Chris Lattner67b9e832009-06-26 03:45:46 +0000200 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000201 }
202
Chris Lattner5c7f7862009-06-26 03:52:38 +0000203
204 // The rest of the nested-name-specifier possibilities start with
205 // tok::identifier.
206 if (Tok.isNot(tok::identifier))
207 break;
208
209 IdentifierInfo &II = *Tok.getIdentifierInfo();
210
211 // nested-name-specifier:
212 // type-name '::'
213 // namespace-name '::'
214 // nested-name-specifier identifier '::'
215 Token Next = NextToken();
216 if (Next.is(tok::coloncolon)) {
217 // We have an identifier followed by a '::'. Lookup this name
218 // as the name in a nested-name-specifier.
219 SourceLocation IdLoc = ConsumeToken();
220 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
221 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Chris Lattner5c7f7862009-06-26 03:52:38 +0000223 if (!HasScopeSpecifier) {
224 SS.setBeginLoc(IdLoc);
225 HasScopeSpecifier = true;
226 }
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattner5c7f7862009-06-26 03:52:38 +0000228 if (SS.isInvalid())
229 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Chris Lattner5c7f7862009-06-26 03:52:38 +0000231 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000232 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000233 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000234 SS.setEndLoc(CCLoc);
235 continue;
236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattner5c7f7862009-06-26 03:52:38 +0000238 // nested-name-specifier:
239 // type-name '<'
240 if (Next.is(tok::less)) {
241 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000242 UnqualifiedId TemplateName;
243 TemplateName.setIdentifier(&II, Tok.getLocation());
244 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
245 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000246 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000247 EnteringContext,
248 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000249 // We have found a template name, so annotate this this token
250 // with a template-id annotation. We do not permit the
251 // template-id to be translated into a type annotation,
252 // because some clients (e.g., the parsing of class template
253 // specializations) still want to see the original template-id
254 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000255 ConsumeToken();
256 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
257 SourceLocation(), false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000258 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000259 continue;
260 }
261 }
262
Douglas Gregor39a8de12009-02-25 19:37:18 +0000263 // We don't have any tokens that form the beginning of a
264 // nested-name-specifier, so we're done.
265 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Douglas Gregor39a8de12009-02-25 19:37:18 +0000268 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000269}
270
271/// ParseCXXIdExpression - Handle id-expression.
272///
273/// id-expression:
274/// unqualified-id
275/// qualified-id
276///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000277/// qualified-id:
278/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
279/// '::' identifier
280/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000281/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000282///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000283/// NOTE: The standard specifies that, for qualified-id, the parser does not
284/// expect:
285///
286/// '::' conversion-function-id
287/// '::' '~' class-name
288///
289/// This may cause a slight inconsistency on diagnostics:
290///
291/// class C {};
292/// namespace A {}
293/// void f() {
294/// :: A :: ~ C(); // Some Sema error about using destructor with a
295/// // namespace.
296/// :: ~ C(); // Some Parser error like 'unexpected ~'.
297/// }
298///
299/// We simplify the parser a bit and make it work like:
300///
301/// qualified-id:
302/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
303/// '::' unqualified-id
304///
305/// That way Sema can handle and report similar errors for namespaces and the
306/// global scope.
307///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000308/// The isAddressOfOperand parameter indicates that this id-expression is a
309/// direct operand of the address-of operator. This is, besides member contexts,
310/// the only place where a qualified-id naming a non-static class member may
311/// appear.
312///
313Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000314 // qualified-id:
315 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
316 // '::' unqualified-id
317 //
318 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000319 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000320
321 UnqualifiedId Name;
322 if (ParseUnqualifiedId(SS,
323 /*EnteringContext=*/false,
324 /*AllowDestructorName=*/false,
325 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000326 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000327 Name))
328 return ExprError();
329
330 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
331 isAddressOfOperand);
332
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000333}
334
Reid Spencer5f016e22007-07-11 17:01:13 +0000335/// ParseCXXCasts - This handles the various ways to cast expressions to another
336/// type.
337///
338/// postfix-expression: [C++ 5.2p1]
339/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
340/// 'static_cast' '<' type-name '>' '(' expression ')'
341/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
342/// 'const_cast' '<' type-name '>' '(' expression ')'
343///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000344Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 tok::TokenKind Kind = Tok.getKind();
346 const char *CastName = 0; // For error messages
347
348 switch (Kind) {
349 default: assert(0 && "Unknown C++ cast!"); abort();
350 case tok::kw_const_cast: CastName = "const_cast"; break;
351 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
352 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
353 case tok::kw_static_cast: CastName = "static_cast"; break;
354 }
355
356 SourceLocation OpLoc = ConsumeToken();
357 SourceLocation LAngleBracketLoc = Tok.getLocation();
358
359 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000360 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000361
Douglas Gregor809070a2009-02-18 17:45:20 +0000362 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 SourceLocation RAngleBracketLoc = Tok.getLocation();
364
Chris Lattner1ab3b962008-11-18 07:48:38 +0000365 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000366 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000367
368 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
369
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000370 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
371 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000372
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000373 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000375 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000376 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000377
Douglas Gregor809070a2009-02-18 17:45:20 +0000378 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000379 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000380 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000381 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000382 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000383
Sebastian Redl20df9b72008-12-11 22:51:44 +0000384 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000385}
386
Sebastian Redlc42e1182008-11-11 11:37:55 +0000387/// ParseCXXTypeid - This handles the C++ typeid expression.
388///
389/// postfix-expression: [C++ 5.2p1]
390/// 'typeid' '(' expression ')'
391/// 'typeid' '(' type-id ')'
392///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000393Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000394 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
395
396 SourceLocation OpLoc = ConsumeToken();
397 SourceLocation LParenLoc = Tok.getLocation();
398 SourceLocation RParenLoc;
399
400 // typeid expressions are always parenthesized.
401 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
402 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000403 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000404
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000405 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000406
407 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000408 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000409
410 // Match the ')'.
411 MatchRHSPunctuation(tok::r_paren, LParenLoc);
412
Douglas Gregor809070a2009-02-18 17:45:20 +0000413 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000414 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000415
416 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000417 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000418 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000419 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000420 // When typeid is applied to an expression other than an lvalue of a
421 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000422 // operand (Clause 5).
423 //
Mike Stump1eb44332009-09-09 15:08:12 +0000424 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000425 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000426 // we the expression is potentially potentially evaluated.
427 EnterExpressionEvaluationContext Unevaluated(Actions,
428 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000429 Result = ParseExpression();
430
431 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000432 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000433 SkipUntil(tok::r_paren);
434 else {
435 MatchRHSPunctuation(tok::r_paren, LParenLoc);
436
437 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000438 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000439 }
440 }
441
Sebastian Redl20df9b72008-12-11 22:51:44 +0000442 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000443}
444
Reid Spencer5f016e22007-07-11 17:01:13 +0000445/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
446///
447/// boolean-literal: [C++ 2.13.5]
448/// 'true'
449/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000450Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000452 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000453}
Chris Lattner50dd2892008-02-26 00:51:44 +0000454
455/// ParseThrowExpression - This handles the C++ throw expression.
456///
457/// throw-expression: [C++ 15]
458/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000459Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000460 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000461 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000462
Chris Lattner2a2819a2008-04-06 06:02:23 +0000463 // If the current token isn't the start of an assignment-expression,
464 // then the expression is not present. This handles things like:
465 // "C ? throw : (void)42", which is crazy but legal.
466 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
467 case tok::semi:
468 case tok::r_paren:
469 case tok::r_square:
470 case tok::r_brace:
471 case tok::colon:
472 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000473 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000474
Chris Lattner2a2819a2008-04-06 06:02:23 +0000475 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000476 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000477 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000478 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000479 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000480}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000481
482/// ParseCXXThis - This handles the C++ 'this' pointer.
483///
484/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
485/// a non-lvalue expression whose value is the address of the object for which
486/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000487Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000488 assert(Tok.is(tok::kw_this) && "Not 'this'!");
489 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000490 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000491}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000492
493/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
494/// Can be interpreted either as function-style casting ("int(x)")
495/// or class type construction ("ClassType(x,y,z)")
496/// or creation of a value-initialized type ("int()").
497///
498/// postfix-expression: [C++ 5.2p1]
499/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
500/// typename-specifier '(' expression-list[opt] ')' [TODO]
501///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000502Parser::OwningExprResult
503Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000504 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000505 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000506
507 assert(Tok.is(tok::l_paren) && "Expected '('!");
508 SourceLocation LParenLoc = ConsumeParen();
509
Sebastian Redla55e52c2008-11-25 22:21:31 +0000510 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000511 CommaLocsTy CommaLocs;
512
513 if (Tok.isNot(tok::r_paren)) {
514 if (ParseExpressionList(Exprs, CommaLocs)) {
515 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000516 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000517 }
518 }
519
520 // Match the ')'.
521 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
522
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000523 // TypeRep could be null, if it references an invalid typedef.
524 if (!TypeRep)
525 return ExprError();
526
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000527 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
528 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000529 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
530 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000531 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000532}
533
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000534/// ParseCXXCondition - if/switch/while/for condition expression.
535///
536/// condition:
537/// expression
538/// type-specifier-seq declarator '=' assignment-expression
539/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
540/// '=' assignment-expression
541///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000542Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000543 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000544 return ParseExpression(); // expression
545
546 SourceLocation StartLoc = Tok.getLocation();
547
548 // type-specifier-seq
549 DeclSpec DS;
550 ParseSpecifierQualifierList(DS);
551
552 // declarator
553 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
554 ParseDeclarator(DeclaratorInfo);
555
556 // simple-asm-expr[opt]
557 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000558 SourceLocation Loc;
559 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000560 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000561 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000562 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000563 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000564 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000565 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000566 }
567
568 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000569 if (Tok.is(tok::kw___attribute)) {
570 SourceLocation Loc;
571 AttributeList *AttrList = ParseAttributes(&Loc);
572 DeclaratorInfo.AddAttributes(AttrList, Loc);
573 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000574
575 // '=' assignment-expression
576 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000577 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000578 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000579 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000580 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000581 return ExprError();
582
Sebastian Redlf53597f2009-03-15 17:47:39 +0000583 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
584 DeclaratorInfo,EqualLoc,
585 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000586}
587
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000588/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
589/// This should only be called when the current token is known to be part of
590/// simple-type-specifier.
591///
592/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000593/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000594/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
595/// char
596/// wchar_t
597/// bool
598/// short
599/// int
600/// long
601/// signed
602/// unsigned
603/// float
604/// double
605/// void
606/// [GNU] typeof-specifier
607/// [C++0x] auto [TODO]
608///
609/// type-name:
610/// class-name
611/// enum-name
612/// typedef-name
613///
614void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
615 DS.SetRangeStart(Tok.getLocation());
616 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000617 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000618 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000620 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000621 case tok::identifier: // foo::bar
622 case tok::coloncolon: // ::foo::bar
623 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000624 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000625 assert(0 && "Not a simple-type-specifier token!");
626 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000627
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000628 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000629 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000630 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000631 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000632 break;
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000635 // builtin types
636 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000637 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000638 break;
639 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000640 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000641 break;
642 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000643 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000644 break;
645 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000646 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000647 break;
648 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000649 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000650 break;
651 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000652 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000653 break;
654 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000655 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000656 break;
657 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000658 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000659 break;
660 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000661 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000662 break;
663 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000664 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000665 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000666 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000667 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000668 break;
669 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000670 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000671 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000672 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000673 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000674 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000676 // GNU typeof support.
677 case tok::kw_typeof:
678 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000679 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000680 return;
681 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000682 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000683 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
684 else
685 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000686 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000687 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000688}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000689
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000690/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
691/// [dcl.name]), which is a non-empty sequence of type-specifiers,
692/// e.g., "const short int". Note that the DeclSpec is *not* finished
693/// by parsing the type-specifier-seq, because these sequences are
694/// typically followed by some form of declarator. Returns true and
695/// emits diagnostics if this is not a type-specifier-seq, false
696/// otherwise.
697///
698/// type-specifier-seq: [C++ 8.1]
699/// type-specifier type-specifier-seq[opt]
700///
701bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
702 DS.SetRangeStart(Tok.getLocation());
703 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000704 unsigned DiagID;
705 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000706
707 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000708 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000709 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000710 return true;
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
John McCallfec54012009-08-03 20:12:06 +0000713 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000714
715 return false;
716}
717
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000718/// \brief Finish parsing a C++ unqualified-id that is a template-id of
719/// some form.
720///
721/// This routine is invoked when a '<' is encountered after an identifier or
722/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
723/// whether the unqualified-id is actually a template-id. This routine will
724/// then parse the template arguments and form the appropriate template-id to
725/// return to the caller.
726///
727/// \param SS the nested-name-specifier that precedes this template-id, if
728/// we're actually parsing a qualified-id.
729///
730/// \param Name for constructor and destructor names, this is the actual
731/// identifier that may be a template-name.
732///
733/// \param NameLoc the location of the class-name in a constructor or
734/// destructor.
735///
736/// \param EnteringContext whether we're entering the scope of the
737/// nested-name-specifier.
738///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000739/// \param ObjectType if this unqualified-id occurs within a member access
740/// expression, the type of the base object whose member is being accessed.
741///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000742/// \param Id as input, describes the template-name or operator-function-id
743/// that precedes the '<'. If template arguments were parsed successfully,
744/// will be updated with the template-id.
745///
746/// \returns true if a parse error occurred, false otherwise.
747bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
748 IdentifierInfo *Name,
749 SourceLocation NameLoc,
750 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000751 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000752 UnqualifiedId &Id) {
753 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
754
755 TemplateTy Template;
756 TemplateNameKind TNK = TNK_Non_template;
757 switch (Id.getKind()) {
758 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000759 case UnqualifiedId::IK_OperatorFunctionId:
760 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType, EnteringContext,
761 Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000762 break;
763
Douglas Gregor014e88d2009-11-03 23:16:33 +0000764 case UnqualifiedId::IK_ConstructorName: {
765 UnqualifiedId TemplateName;
766 TemplateName.setIdentifier(Name, NameLoc);
767 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
768 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000769 break;
770 }
771
Douglas Gregor014e88d2009-11-03 23:16:33 +0000772 case UnqualifiedId::IK_DestructorName: {
773 UnqualifiedId TemplateName;
774 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000775 if (ObjectType) {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000776 Template = Actions.ActOnDependentTemplateName(SourceLocation(), SS,
777 TemplateName, ObjectType);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000778 TNK = TNK_Dependent_template_name;
779 if (!Template.get())
780 return true;
781 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000782 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000783 EnteringContext, Template);
784
785 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
786 // The identifier following the destructor did not refer to a template
787 // or to a type. Complain.
788 if (ObjectType)
789 Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
790 << Name;
791 else
792 Diag(NameLoc, diag::err_destructor_class_name);
793 return true;
794 }
795 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000796 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000797 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000798
799 default:
800 return false;
801 }
802
803 if (TNK == TNK_Non_template)
804 return false;
805
806 // Parse the enclosed template argument list.
807 SourceLocation LAngleLoc, RAngleLoc;
808 TemplateArgList TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000809 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
810 &SS, true, LAngleLoc,
811 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000812 RAngleLoc))
813 return true;
814
815 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
816 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) {
817 // Form a parsed representation of the template-id to be stored in the
818 // UnqualifiedId.
819 TemplateIdAnnotation *TemplateId
820 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
821
822 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
823 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000824 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000825 TemplateId->TemplateNameLoc = Id.StartLocation;
826 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000827 TemplateId->Name = 0;
828 TemplateId->Operator = Id.OperatorFunctionId.Operator;
829 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000830 }
831
832 TemplateId->Template = Template.getAs<void*>();
833 TemplateId->Kind = TNK;
834 TemplateId->LAngleLoc = LAngleLoc;
835 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +0000836 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000837 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +0000838 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000839 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000840
841 Id.setTemplateId(TemplateId);
842 return false;
843 }
844
845 // Bundle the template arguments together.
846 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000847 TemplateArgs.size());
848
849 // Constructor and destructor names.
850 Action::TypeResult Type
851 = Actions.ActOnTemplateIdType(Template, NameLoc,
852 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000853 RAngleLoc);
854 if (Type.isInvalid())
855 return true;
856
857 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
858 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
859 else
860 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
861
862 return false;
863}
864
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000865/// \brief Parse an operator-function-id or conversion-function-id as part
866/// of a C++ unqualified-id.
867///
868/// This routine is responsible only for parsing the operator-function-id or
869/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000870///
871/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000872/// operator-function-id: [C++ 13.5]
873/// 'operator' operator
874///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000875/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000876/// new delete new[] delete[]
877/// + - * / % ^ & | ~
878/// ! = < > += -= *= /= %=
879/// ^= &= |= << >> >>= <<= == !=
880/// <= >= && || ++ -- , ->* ->
881/// () []
882///
883/// conversion-function-id: [C++ 12.3.2]
884/// operator conversion-type-id
885///
886/// conversion-type-id:
887/// type-specifier-seq conversion-declarator[opt]
888///
889/// conversion-declarator:
890/// ptr-operator conversion-declarator[opt]
891/// \endcode
892///
893/// \param The nested-name-specifier that preceded this unqualified-id. If
894/// non-empty, then we are parsing the unqualified-id of a qualified-id.
895///
896/// \param EnteringContext whether we are entering the scope of the
897/// nested-name-specifier.
898///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000899/// \param ObjectType if this unqualified-id occurs within a member access
900/// expression, the type of the base object whose member is being accessed.
901///
902/// \param Result on a successful parse, contains the parsed unqualified-id.
903///
904/// \returns true if parsing fails, false otherwise.
905bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
906 TypeTy *ObjectType,
907 UnqualifiedId &Result) {
908 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
909
910 // Consume the 'operator' keyword.
911 SourceLocation KeywordLoc = ConsumeToken();
912
913 // Determine what kind of operator name we have.
914 unsigned SymbolIdx = 0;
915 SourceLocation SymbolLocations[3];
916 OverloadedOperatorKind Op = OO_None;
917 switch (Tok.getKind()) {
918 case tok::kw_new:
919 case tok::kw_delete: {
920 bool isNew = Tok.getKind() == tok::kw_new;
921 // Consume the 'new' or 'delete'.
922 SymbolLocations[SymbolIdx++] = ConsumeToken();
923 if (Tok.is(tok::l_square)) {
924 // Consume the '['.
925 SourceLocation LBracketLoc = ConsumeBracket();
926 // Consume the ']'.
927 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
928 LBracketLoc);
929 if (RBracketLoc.isInvalid())
930 return true;
931
932 SymbolLocations[SymbolIdx++] = LBracketLoc;
933 SymbolLocations[SymbolIdx++] = RBracketLoc;
934 Op = isNew? OO_Array_New : OO_Array_Delete;
935 } else {
936 Op = isNew? OO_New : OO_Delete;
937 }
938 break;
939 }
940
941#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
942 case tok::Token: \
943 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
944 Op = OO_##Name; \
945 break;
946#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
947#include "clang/Basic/OperatorKinds.def"
948
949 case tok::l_paren: {
950 // Consume the '('.
951 SourceLocation LParenLoc = ConsumeParen();
952 // Consume the ')'.
953 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
954 LParenLoc);
955 if (RParenLoc.isInvalid())
956 return true;
957
958 SymbolLocations[SymbolIdx++] = LParenLoc;
959 SymbolLocations[SymbolIdx++] = RParenLoc;
960 Op = OO_Call;
961 break;
962 }
963
964 case tok::l_square: {
965 // Consume the '['.
966 SourceLocation LBracketLoc = ConsumeBracket();
967 // Consume the ']'.
968 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
969 LBracketLoc);
970 if (RBracketLoc.isInvalid())
971 return true;
972
973 SymbolLocations[SymbolIdx++] = LBracketLoc;
974 SymbolLocations[SymbolIdx++] = RBracketLoc;
975 Op = OO_Subscript;
976 break;
977 }
978
979 case tok::code_completion: {
980 // Code completion for the operator name.
981 Actions.CodeCompleteOperatorName(CurScope);
982
983 // Consume the operator token.
984 ConsumeToken();
985
986 // Don't try to parse any further.
987 return true;
988 }
989
990 default:
991 break;
992 }
993
994 if (Op != OO_None) {
995 // We have parsed an operator-function-id.
996 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
997 return false;
998 }
999
1000 // Parse a conversion-function-id.
1001 //
1002 // conversion-function-id: [C++ 12.3.2]
1003 // operator conversion-type-id
1004 //
1005 // conversion-type-id:
1006 // type-specifier-seq conversion-declarator[opt]
1007 //
1008 // conversion-declarator:
1009 // ptr-operator conversion-declarator[opt]
1010
1011 // Parse the type-specifier-seq.
1012 DeclSpec DS;
1013 if (ParseCXXTypeSpecifierSeq(DS))
1014 return true;
1015
1016 // Parse the conversion-declarator, which is merely a sequence of
1017 // ptr-operators.
1018 Declarator D(DS, Declarator::TypeNameContext);
1019 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1020
1021 // Finish up the type.
1022 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1023 if (Ty.isInvalid())
1024 return true;
1025
1026 // Note that this is a conversion-function-id.
1027 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1028 D.getSourceRange().getEnd());
1029 return false;
1030}
1031
1032/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1033/// name of an entity.
1034///
1035/// \code
1036/// unqualified-id: [C++ expr.prim.general]
1037/// identifier
1038/// operator-function-id
1039/// conversion-function-id
1040/// [C++0x] literal-operator-id [TODO]
1041/// ~ class-name
1042/// template-id
1043///
1044/// \endcode
1045///
1046/// \param The nested-name-specifier that preceded this unqualified-id. If
1047/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1048///
1049/// \param EnteringContext whether we are entering the scope of the
1050/// nested-name-specifier.
1051///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001052/// \param AllowDestructorName whether we allow parsing of a destructor name.
1053///
1054/// \param AllowConstructorName whether we allow parsing a constructor name.
1055///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001056/// \param ObjectType if this unqualified-id occurs within a member access
1057/// expression, the type of the base object whose member is being accessed.
1058///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001059/// \param Result on a successful parse, contains the parsed unqualified-id.
1060///
1061/// \returns true if parsing fails, false otherwise.
1062bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1063 bool AllowDestructorName,
1064 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001065 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001066 UnqualifiedId &Result) {
1067 // unqualified-id:
1068 // identifier
1069 // template-id (when it hasn't already been annotated)
1070 if (Tok.is(tok::identifier)) {
1071 // Consume the identifier.
1072 IdentifierInfo *Id = Tok.getIdentifierInfo();
1073 SourceLocation IdLoc = ConsumeToken();
1074
1075 if (AllowConstructorName &&
1076 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1077 // We have parsed a constructor name.
1078 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1079 &SS, false),
1080 IdLoc, IdLoc);
1081 } else {
1082 // We have parsed an identifier.
1083 Result.setIdentifier(Id, IdLoc);
1084 }
1085
1086 // If the next token is a '<', we may have a template.
1087 if (Tok.is(tok::less))
1088 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001089 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001090
1091 return false;
1092 }
1093
1094 // unqualified-id:
1095 // template-id (already parsed and annotated)
1096 if (Tok.is(tok::annot_template_id)) {
1097 // FIXME: Could this be a constructor name???
1098
1099 // We have already parsed a template-id; consume the annotation token as
1100 // our unqualified-id.
1101 Result.setTemplateId(
1102 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1103 ConsumeToken();
1104 return false;
1105 }
1106
1107 // unqualified-id:
1108 // operator-function-id
1109 // conversion-function-id
1110 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001111 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001112 return true;
1113
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001114 // If we have an operator-function-id and the next token is a '<', we may
1115 // have a
1116 //
1117 // template-id:
1118 // operator-function-id < template-argument-list[opt] >
1119 if (Result.getKind() == UnqualifiedId::IK_OperatorFunctionId &&
1120 Tok.is(tok::less))
1121 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1122 EnteringContext, ObjectType,
1123 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001124
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001125 return false;
1126 }
1127
1128 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1129 // C++ [expr.unary.op]p10:
1130 // There is an ambiguity in the unary-expression ~X(), where X is a
1131 // class-name. The ambiguity is resolved in favor of treating ~ as a
1132 // unary complement rather than treating ~X as referring to a destructor.
1133
1134 // Parse the '~'.
1135 SourceLocation TildeLoc = ConsumeToken();
1136
1137 // Parse the class-name.
1138 if (Tok.isNot(tok::identifier)) {
1139 Diag(Tok, diag::err_destructor_class_name);
1140 return true;
1141 }
1142
1143 // Parse the class-name (or template-name in a simple-template-id).
1144 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1145 SourceLocation ClassNameLoc = ConsumeToken();
1146
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001147 if (Tok.is(tok::less)) {
1148 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1149 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1150 EnteringContext, ObjectType, Result);
1151 }
1152
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001153 // Note that this is a destructor name.
1154 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
1155 CurScope, &SS);
1156 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001157 if (ObjectType)
1158 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1159 << ClassName;
1160 else
1161 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001162 return true;
1163 }
1164
1165 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001166 return false;
1167 }
1168
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001169 Diag(Tok, diag::err_expected_unqualified_id)
1170 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001171 return true;
1172}
1173
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001174/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1175/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001176///
Chris Lattner59232d32009-01-04 21:25:24 +00001177/// This method is called to parse the new expression after the optional :: has
1178/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1179/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001180///
1181/// new-expression:
1182/// '::'[opt] 'new' new-placement[opt] new-type-id
1183/// new-initializer[opt]
1184/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1185/// new-initializer[opt]
1186///
1187/// new-placement:
1188/// '(' expression-list ')'
1189///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001190/// new-type-id:
1191/// type-specifier-seq new-declarator[opt]
1192///
1193/// new-declarator:
1194/// ptr-operator new-declarator[opt]
1195/// direct-new-declarator
1196///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001197/// new-initializer:
1198/// '(' expression-list[opt] ')'
1199/// [C++0x] braced-init-list [TODO]
1200///
Chris Lattner59232d32009-01-04 21:25:24 +00001201Parser::OwningExprResult
1202Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1203 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1204 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001205
1206 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1207 // second form of new-expression. It can't be a new-type-id.
1208
Sebastian Redla55e52c2008-11-25 22:21:31 +00001209 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001210 SourceLocation PlacementLParen, PlacementRParen;
1211
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001212 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001213 DeclSpec DS;
1214 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001215 if (Tok.is(tok::l_paren)) {
1216 // If it turns out to be a placement, we change the type location.
1217 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001218 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1219 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001220 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001221 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001222
1223 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001224 if (PlacementRParen.isInvalid()) {
1225 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001226 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001227 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001228
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001229 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001230 // Reset the placement locations. There was no placement.
1231 PlacementLParen = PlacementRParen = SourceLocation();
1232 ParenTypeId = true;
1233 } else {
1234 // We still need the type.
1235 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001236 SourceLocation LParen = ConsumeParen();
1237 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001238 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001239 ParseDeclarator(DeclaratorInfo);
1240 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001241 ParenTypeId = true;
1242 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001243 if (ParseCXXTypeSpecifierSeq(DS))
1244 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001245 else {
1246 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001247 ParseDeclaratorInternal(DeclaratorInfo,
1248 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001249 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001250 ParenTypeId = false;
1251 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001252 }
1253 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001254 // A new-type-id is a simplified type-id, where essentially the
1255 // direct-declarator is replaced by a direct-new-declarator.
1256 if (ParseCXXTypeSpecifierSeq(DS))
1257 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001258 else {
1259 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001260 ParseDeclaratorInternal(DeclaratorInfo,
1261 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001262 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001263 ParenTypeId = false;
1264 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001265 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001266 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001267 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001268 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001269
Sebastian Redla55e52c2008-11-25 22:21:31 +00001270 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001271 SourceLocation ConstructorLParen, ConstructorRParen;
1272
1273 if (Tok.is(tok::l_paren)) {
1274 ConstructorLParen = ConsumeParen();
1275 if (Tok.isNot(tok::r_paren)) {
1276 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001277 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1278 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001279 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001280 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001281 }
1282 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001283 if (ConstructorRParen.isInvalid()) {
1284 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001285 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001286 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001287 }
1288
Sebastian Redlf53597f2009-03-15 17:47:39 +00001289 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1290 move_arg(PlacementArgs), PlacementRParen,
1291 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1292 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001293}
1294
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001295/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1296/// passed to ParseDeclaratorInternal.
1297///
1298/// direct-new-declarator:
1299/// '[' expression ']'
1300/// direct-new-declarator '[' constant-expression ']'
1301///
Chris Lattner59232d32009-01-04 21:25:24 +00001302void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001303 // Parse the array dimensions.
1304 bool first = true;
1305 while (Tok.is(tok::l_square)) {
1306 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001307 OwningExprResult Size(first ? ParseExpression()
1308 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001309 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001310 // Recover
1311 SkipUntil(tok::r_square);
1312 return;
1313 }
1314 first = false;
1315
Sebastian Redlab197ba2009-02-09 18:23:29 +00001316 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001317 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001318 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001319 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001320
Sebastian Redlab197ba2009-02-09 18:23:29 +00001321 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001322 return;
1323 }
1324}
1325
1326/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1327/// This ambiguity appears in the syntax of the C++ new operator.
1328///
1329/// new-expression:
1330/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1331/// new-initializer[opt]
1332///
1333/// new-placement:
1334/// '(' expression-list ')'
1335///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001336bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001337 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001338 // The '(' was already consumed.
1339 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001340 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001341 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001342 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001343 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001344 }
1345
1346 // It's not a type, it has to be an expression list.
1347 // Discard the comma locations - ActOnCXXNew has enough parameters.
1348 CommaLocsTy CommaLocs;
1349 return ParseExpressionList(PlacementArgs, CommaLocs);
1350}
1351
1352/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1353/// to free memory allocated by new.
1354///
Chris Lattner59232d32009-01-04 21:25:24 +00001355/// This method is called to parse the 'delete' expression after the optional
1356/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1357/// and "Start" is its location. Otherwise, "Start" is the location of the
1358/// 'delete' token.
1359///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001360/// delete-expression:
1361/// '::'[opt] 'delete' cast-expression
1362/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001363Parser::OwningExprResult
1364Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1365 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1366 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001367
1368 // Array delete?
1369 bool ArrayDelete = false;
1370 if (Tok.is(tok::l_square)) {
1371 ArrayDelete = true;
1372 SourceLocation LHS = ConsumeBracket();
1373 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1374 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001375 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001376 }
1377
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001378 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001379 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001380 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001381
Sebastian Redlf53597f2009-03-15 17:47:39 +00001382 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001383}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001384
Mike Stump1eb44332009-09-09 15:08:12 +00001385static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001386 switch(kind) {
1387 default: assert(false && "Not a known unary type trait.");
1388 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1389 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1390 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1391 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1392 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1393 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1394 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1395 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1396 case tok::kw___is_abstract: return UTT_IsAbstract;
1397 case tok::kw___is_class: return UTT_IsClass;
1398 case tok::kw___is_empty: return UTT_IsEmpty;
1399 case tok::kw___is_enum: return UTT_IsEnum;
1400 case tok::kw___is_pod: return UTT_IsPOD;
1401 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1402 case tok::kw___is_union: return UTT_IsUnion;
1403 }
1404}
1405
1406/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1407/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1408/// templates.
1409///
1410/// primary-expression:
1411/// [GNU] unary-type-trait '(' type-id ')'
1412///
Mike Stump1eb44332009-09-09 15:08:12 +00001413Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001414 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1415 SourceLocation Loc = ConsumeToken();
1416
1417 SourceLocation LParen = Tok.getLocation();
1418 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1419 return ExprError();
1420
1421 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1422 // there will be cryptic errors about mismatched parentheses and missing
1423 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001424 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001425
1426 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1427
Douglas Gregor809070a2009-02-18 17:45:20 +00001428 if (Ty.isInvalid())
1429 return ExprError();
1430
1431 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001432}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001433
1434/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1435/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1436/// based on the context past the parens.
1437Parser::OwningExprResult
1438Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1439 TypeTy *&CastTy,
1440 SourceLocation LParenLoc,
1441 SourceLocation &RParenLoc) {
1442 assert(getLang().CPlusPlus && "Should only be called for C++!");
1443 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1444 assert(isTypeIdInParens() && "Not a type-id!");
1445
1446 OwningExprResult Result(Actions, true);
1447 CastTy = 0;
1448
1449 // We need to disambiguate a very ugly part of the C++ syntax:
1450 //
1451 // (T())x; - type-id
1452 // (T())*x; - type-id
1453 // (T())/x; - expression
1454 // (T()); - expression
1455 //
1456 // The bad news is that we cannot use the specialized tentative parser, since
1457 // it can only verify that the thing inside the parens can be parsed as
1458 // type-id, it is not useful for determining the context past the parens.
1459 //
1460 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001461 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001462 //
1463 // It uses a scheme similar to parsing inline methods. The parenthesized
1464 // tokens are cached, the context that follows is determined (possibly by
1465 // parsing a cast-expression), and then we re-introduce the cached tokens
1466 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001467
Mike Stump1eb44332009-09-09 15:08:12 +00001468 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001469 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001470
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001471 // Store the tokens of the parentheses. We will parse them after we determine
1472 // the context that follows them.
1473 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1474 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001475 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1476 return ExprError();
1477 }
1478
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001479 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001480 ParseAs = CompoundLiteral;
1481 } else {
1482 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001483 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1484 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1485 NotCastExpr = true;
1486 } else {
1487 // Try parsing the cast-expression that may follow.
1488 // If it is not a cast-expression, NotCastExpr will be true and no token
1489 // will be consumed.
1490 Result = ParseCastExpression(false/*isUnaryExpression*/,
1491 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001492 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001493 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001494
1495 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1496 // an expression.
1497 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001498 }
1499
Mike Stump1eb44332009-09-09 15:08:12 +00001500 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001501 Toks.push_back(Tok);
1502 // Re-enter the stored parenthesized tokens into the token stream, so we may
1503 // parse them now.
1504 PP.EnterTokenStream(Toks.data(), Toks.size(),
1505 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1506 // Drop the current token and bring the first cached one. It's the same token
1507 // as when we entered this function.
1508 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001509
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001510 if (ParseAs >= CompoundLiteral) {
1511 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001512
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001513 // Match the ')'.
1514 if (Tok.is(tok::r_paren))
1515 RParenLoc = ConsumeParen();
1516 else
1517 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001518
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001519 if (ParseAs == CompoundLiteral) {
1520 ExprType = CompoundLiteral;
1521 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1522 }
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001524 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1525 assert(ParseAs == CastExpr);
1526
1527 if (Ty.isInvalid())
1528 return ExprError();
1529
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001530 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001531
1532 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001533 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001534 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001535 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001536 return move(Result);
1537 }
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001539 // Not a compound literal, and not followed by a cast-expression.
1540 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001541
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001542 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001543 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001544 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1545 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1546
1547 // Match the ')'.
1548 if (Result.isInvalid()) {
1549 SkipUntil(tok::r_paren);
1550 return ExprError();
1551 }
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001553 if (Tok.is(tok::r_paren))
1554 RParenLoc = ConsumeParen();
1555 else
1556 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1557
1558 return move(Result);
1559}