blob: 992211dad759bced71133d97f747db2313a3c6c2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000018#include "llvm/Support/ErrorHandling.h"
19
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Mike Stump1eb44332009-09-09 15:08:12 +000022/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000023///
24/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000025/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000026/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000027///
28/// '::'[opt] nested-name-specifier
29/// '::'
30///
31/// nested-name-specifier:
32/// type-name '::'
33/// namespace-name '::'
34/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000035/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000036///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000037///
Mike Stump1eb44332009-09-09 15:08:12 +000038/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000039/// nested-name-specifier (or empty)
40///
Mike Stump1eb44332009-09-09 15:08:12 +000041/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000042/// the "." or "->" of a member access expression, this parameter provides the
43/// type of the object whose members are being accessed.
44///
45/// \param EnteringContext whether we will be entering into the context of
46/// the nested-name-specifier after parsing it.
47///
48/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000049bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000050 Action::TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +000051 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000052 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000053 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000054
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000055 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000056 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000057 SS.setRange(Tok.getAnnotationRange());
58 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000059 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000060 }
Chris Lattnere607e802009-01-04 21:14:15 +000061
Douglas Gregor39a8de12009-02-25 19:37:18 +000062 bool HasScopeSpecifier = false;
63
Chris Lattner5b454732009-01-05 03:55:46 +000064 if (Tok.is(tok::coloncolon)) {
65 // ::new and ::delete aren't nested-name-specifiers.
66 tok::TokenKind NextKind = NextToken().getKind();
67 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
68 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Chris Lattner55a7cef2009-01-05 00:13:00 +000070 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000071 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000072 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000073 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000074 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000075 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000076 }
77
Douglas Gregor39a8de12009-02-25 19:37:18 +000078 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000079 if (HasScopeSpecifier) {
80 // C++ [basic.lookup.classref]p5:
81 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000082 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000083 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000084 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000085 // the class-name-or-namespace-name is looked up in global scope as a
86 // class-name or namespace-name.
87 //
88 // To implement this, we clear out the object type as soon as we've
89 // seen a leading '::' or part of a nested-name-specifier.
90 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +000091
92 if (Tok.is(tok::code_completion)) {
93 // Code completion for a nested-name-specifier, where the code
94 // code completion token follows the '::'.
95 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
96 ConsumeToken();
97 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +000098 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Douglas Gregor39a8de12009-02-25 19:37:18 +0000100 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000101 // nested-name-specifier 'template'[opt] simple-template-id '::'
102
103 // Parse the optional 'template' keyword, then make sure we have
104 // 'identifier <' after it.
105 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000106 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000107 // nested-name-specifier, since they aren't allowed to start with
108 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000109 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000110 break;
111
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000112 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000113 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000114
115 UnqualifiedId TemplateName;
116 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000117 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000118 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000119 ConsumeToken();
120 } else if (Tok.is(tok::kw_operator)) {
121 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000122 TemplateName)) {
123 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000124 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000125 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000126
127 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId) {
128 Diag(TemplateName.getSourceRange().getBegin(),
129 diag::err_id_after_template_in_nested_name_spec)
130 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000131 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000132 break;
133 }
134 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000135 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000136 break;
137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000139 // If the next token is not '<', we have a qualified-id that refers
140 // to a template name, such as T::template apply, but is not a
141 // template-id.
142 if (Tok.isNot(tok::less)) {
143 TPA.Revert();
144 break;
145 }
146
147 // Commit to parsing the template-id.
148 TPA.Commit();
Mike Stump1eb44332009-09-09 15:08:12 +0000149 TemplateTy Template
Douglas Gregor014e88d2009-11-03 23:16:33 +0000150 = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
Douglas Gregora481edb2009-11-20 23:39:24 +0000151 ObjectType, EnteringContext);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000152 if (!Template)
153 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000154 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000155 &SS, TemplateName, TemplateKWLoc, false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000156 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattner77cf72a2009-06-26 03:47:46 +0000158 continue;
159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Douglas Gregor39a8de12009-02-25 19:37:18 +0000161 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000162 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000163 //
164 // simple-template-id '::'
165 //
166 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000167 // right kind (it should name a type or be dependent), and then
168 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000169 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000170 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
171
Mike Stump1eb44332009-09-09 15:08:12 +0000172 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000173 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000174 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000175
Mike Stump1eb44332009-09-09 15:08:12 +0000176 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000177 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000178 Token TypeToken = Tok;
179 ConsumeToken();
180 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
181 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Douglas Gregor39a8de12009-02-25 19:37:18 +0000183 if (!HasScopeSpecifier) {
184 SS.setBeginLoc(TypeToken.getLocation());
185 HasScopeSpecifier = true;
186 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Douglas Gregor31a19b62009-04-01 21:51:26 +0000188 if (TypeToken.getAnnotationValue())
189 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000190 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000191 TypeToken.getAnnotationValue(),
192 TypeToken.getAnnotationRange(),
193 CCLoc));
194 else
195 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000196 SS.setEndLoc(CCLoc);
197 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Chris Lattner67b9e832009-06-26 03:45:46 +0000200 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000201 }
202
Chris Lattner5c7f7862009-06-26 03:52:38 +0000203
204 // The rest of the nested-name-specifier possibilities start with
205 // tok::identifier.
206 if (Tok.isNot(tok::identifier))
207 break;
208
209 IdentifierInfo &II = *Tok.getIdentifierInfo();
210
211 // nested-name-specifier:
212 // type-name '::'
213 // namespace-name '::'
214 // nested-name-specifier identifier '::'
215 Token Next = NextToken();
216 if (Next.is(tok::coloncolon)) {
217 // We have an identifier followed by a '::'. Lookup this name
218 // as the name in a nested-name-specifier.
219 SourceLocation IdLoc = ConsumeToken();
220 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
221 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Chris Lattner5c7f7862009-06-26 03:52:38 +0000223 if (!HasScopeSpecifier) {
224 SS.setBeginLoc(IdLoc);
225 HasScopeSpecifier = true;
226 }
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattner5c7f7862009-06-26 03:52:38 +0000228 if (SS.isInvalid())
229 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Chris Lattner5c7f7862009-06-26 03:52:38 +0000231 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000232 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000233 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000234 SS.setEndLoc(CCLoc);
235 continue;
236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattner5c7f7862009-06-26 03:52:38 +0000238 // nested-name-specifier:
239 // type-name '<'
240 if (Next.is(tok::less)) {
241 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000242 UnqualifiedId TemplateName;
243 TemplateName.setIdentifier(&II, Tok.getLocation());
244 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
245 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000246 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000247 EnteringContext,
248 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000249 // We have found a template name, so annotate this this token
250 // with a template-id annotation. We do not permit the
251 // template-id to be translated into a type annotation,
252 // because some clients (e.g., the parsing of class template
253 // specializations) still want to see the original template-id
254 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000255 ConsumeToken();
256 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
257 SourceLocation(), false))
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000258 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000259 continue;
260 }
261 }
262
Douglas Gregor39a8de12009-02-25 19:37:18 +0000263 // We don't have any tokens that form the beginning of a
264 // nested-name-specifier, so we're done.
265 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Douglas Gregor39a8de12009-02-25 19:37:18 +0000268 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000269}
270
271/// ParseCXXIdExpression - Handle id-expression.
272///
273/// id-expression:
274/// unqualified-id
275/// qualified-id
276///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000277/// qualified-id:
278/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
279/// '::' identifier
280/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000281/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000282///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000283/// NOTE: The standard specifies that, for qualified-id, the parser does not
284/// expect:
285///
286/// '::' conversion-function-id
287/// '::' '~' class-name
288///
289/// This may cause a slight inconsistency on diagnostics:
290///
291/// class C {};
292/// namespace A {}
293/// void f() {
294/// :: A :: ~ C(); // Some Sema error about using destructor with a
295/// // namespace.
296/// :: ~ C(); // Some Parser error like 'unexpected ~'.
297/// }
298///
299/// We simplify the parser a bit and make it work like:
300///
301/// qualified-id:
302/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
303/// '::' unqualified-id
304///
305/// That way Sema can handle and report similar errors for namespaces and the
306/// global scope.
307///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000308/// The isAddressOfOperand parameter indicates that this id-expression is a
309/// direct operand of the address-of operator. This is, besides member contexts,
310/// the only place where a qualified-id naming a non-static class member may
311/// appear.
312///
313Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000314 // qualified-id:
315 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
316 // '::' unqualified-id
317 //
318 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000319 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000320
321 UnqualifiedId Name;
322 if (ParseUnqualifiedId(SS,
323 /*EnteringContext=*/false,
324 /*AllowDestructorName=*/false,
325 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000326 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000327 Name))
328 return ExprError();
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,
Douglas Gregora481edb2009-11-20 23:39:24 +0000777 TemplateName, ObjectType,
778 EnteringContext);
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000779 TNK = TNK_Dependent_template_name;
780 if (!Template.get())
781 return true;
782 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000783 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000784 EnteringContext, Template);
785
786 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
787 // The identifier following the destructor did not refer to a template
788 // or to a type. Complain.
789 if (ObjectType)
790 Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
791 << Name;
792 else
793 Diag(NameLoc, diag::err_destructor_class_name);
794 return true;
795 }
796 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000797 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000798 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000799
800 default:
801 return false;
802 }
803
804 if (TNK == TNK_Non_template)
805 return false;
806
807 // Parse the enclosed template argument list.
808 SourceLocation LAngleLoc, RAngleLoc;
809 TemplateArgList TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000810 if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
811 &SS, true, LAngleLoc,
812 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000813 RAngleLoc))
814 return true;
815
816 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
817 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) {
818 // Form a parsed representation of the template-id to be stored in the
819 // UnqualifiedId.
820 TemplateIdAnnotation *TemplateId
821 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
822
823 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
824 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000825 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000826 TemplateId->TemplateNameLoc = Id.StartLocation;
827 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +0000828 TemplateId->Name = 0;
829 TemplateId->Operator = Id.OperatorFunctionId.Operator;
830 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000831 }
832
833 TemplateId->Template = Template.getAs<void*>();
834 TemplateId->Kind = TNK;
835 TemplateId->LAngleLoc = LAngleLoc;
836 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +0000837 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000838 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +0000839 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000840 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000841
842 Id.setTemplateId(TemplateId);
843 return false;
844 }
845
846 // Bundle the template arguments together.
847 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000848 TemplateArgs.size());
849
850 // Constructor and destructor names.
851 Action::TypeResult Type
852 = Actions.ActOnTemplateIdType(Template, NameLoc,
853 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000854 RAngleLoc);
855 if (Type.isInvalid())
856 return true;
857
858 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
859 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
860 else
861 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
862
863 return false;
864}
865
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000866/// \brief Parse an operator-function-id or conversion-function-id as part
867/// of a C++ unqualified-id.
868///
869/// This routine is responsible only for parsing the operator-function-id or
870/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000871///
872/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000873/// operator-function-id: [C++ 13.5]
874/// 'operator' operator
875///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000876/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000877/// new delete new[] delete[]
878/// + - * / % ^ & | ~
879/// ! = < > += -= *= /= %=
880/// ^= &= |= << >> >>= <<= == !=
881/// <= >= && || ++ -- , ->* ->
882/// () []
883///
884/// conversion-function-id: [C++ 12.3.2]
885/// operator conversion-type-id
886///
887/// conversion-type-id:
888/// type-specifier-seq conversion-declarator[opt]
889///
890/// conversion-declarator:
891/// ptr-operator conversion-declarator[opt]
892/// \endcode
893///
894/// \param The nested-name-specifier that preceded this unqualified-id. If
895/// non-empty, then we are parsing the unqualified-id of a qualified-id.
896///
897/// \param EnteringContext whether we are entering the scope of the
898/// nested-name-specifier.
899///
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000900/// \param ObjectType if this unqualified-id occurs within a member access
901/// expression, the type of the base object whose member is being accessed.
902///
903/// \param Result on a successful parse, contains the parsed unqualified-id.
904///
905/// \returns true if parsing fails, false otherwise.
906bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
907 TypeTy *ObjectType,
908 UnqualifiedId &Result) {
909 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
910
911 // Consume the 'operator' keyword.
912 SourceLocation KeywordLoc = ConsumeToken();
913
914 // Determine what kind of operator name we have.
915 unsigned SymbolIdx = 0;
916 SourceLocation SymbolLocations[3];
917 OverloadedOperatorKind Op = OO_None;
918 switch (Tok.getKind()) {
919 case tok::kw_new:
920 case tok::kw_delete: {
921 bool isNew = Tok.getKind() == tok::kw_new;
922 // Consume the 'new' or 'delete'.
923 SymbolLocations[SymbolIdx++] = ConsumeToken();
924 if (Tok.is(tok::l_square)) {
925 // Consume the '['.
926 SourceLocation LBracketLoc = ConsumeBracket();
927 // Consume the ']'.
928 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
929 LBracketLoc);
930 if (RBracketLoc.isInvalid())
931 return true;
932
933 SymbolLocations[SymbolIdx++] = LBracketLoc;
934 SymbolLocations[SymbolIdx++] = RBracketLoc;
935 Op = isNew? OO_Array_New : OO_Array_Delete;
936 } else {
937 Op = isNew? OO_New : OO_Delete;
938 }
939 break;
940 }
941
942#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
943 case tok::Token: \
944 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
945 Op = OO_##Name; \
946 break;
947#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
948#include "clang/Basic/OperatorKinds.def"
949
950 case tok::l_paren: {
951 // Consume the '('.
952 SourceLocation LParenLoc = ConsumeParen();
953 // Consume the ')'.
954 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
955 LParenLoc);
956 if (RParenLoc.isInvalid())
957 return true;
958
959 SymbolLocations[SymbolIdx++] = LParenLoc;
960 SymbolLocations[SymbolIdx++] = RParenLoc;
961 Op = OO_Call;
962 break;
963 }
964
965 case tok::l_square: {
966 // Consume the '['.
967 SourceLocation LBracketLoc = ConsumeBracket();
968 // Consume the ']'.
969 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
970 LBracketLoc);
971 if (RBracketLoc.isInvalid())
972 return true;
973
974 SymbolLocations[SymbolIdx++] = LBracketLoc;
975 SymbolLocations[SymbolIdx++] = RBracketLoc;
976 Op = OO_Subscript;
977 break;
978 }
979
980 case tok::code_completion: {
981 // Code completion for the operator name.
982 Actions.CodeCompleteOperatorName(CurScope);
983
984 // Consume the operator token.
985 ConsumeToken();
986
987 // Don't try to parse any further.
988 return true;
989 }
990
991 default:
992 break;
993 }
994
995 if (Op != OO_None) {
996 // We have parsed an operator-function-id.
997 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
998 return false;
999 }
1000
1001 // Parse a conversion-function-id.
1002 //
1003 // conversion-function-id: [C++ 12.3.2]
1004 // operator conversion-type-id
1005 //
1006 // conversion-type-id:
1007 // type-specifier-seq conversion-declarator[opt]
1008 //
1009 // conversion-declarator:
1010 // ptr-operator conversion-declarator[opt]
1011
1012 // Parse the type-specifier-seq.
1013 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001014 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001015 return true;
1016
1017 // Parse the conversion-declarator, which is merely a sequence of
1018 // ptr-operators.
1019 Declarator D(DS, Declarator::TypeNameContext);
1020 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1021
1022 // Finish up the type.
1023 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1024 if (Ty.isInvalid())
1025 return true;
1026
1027 // Note that this is a conversion-function-id.
1028 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1029 D.getSourceRange().getEnd());
1030 return false;
1031}
1032
1033/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1034/// name of an entity.
1035///
1036/// \code
1037/// unqualified-id: [C++ expr.prim.general]
1038/// identifier
1039/// operator-function-id
1040/// conversion-function-id
1041/// [C++0x] literal-operator-id [TODO]
1042/// ~ class-name
1043/// template-id
1044///
1045/// \endcode
1046///
1047/// \param The nested-name-specifier that preceded this unqualified-id. If
1048/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1049///
1050/// \param EnteringContext whether we are entering the scope of the
1051/// nested-name-specifier.
1052///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001053/// \param AllowDestructorName whether we allow parsing of a destructor name.
1054///
1055/// \param AllowConstructorName whether we allow parsing a constructor name.
1056///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001057/// \param ObjectType if this unqualified-id occurs within a member access
1058/// expression, the type of the base object whose member is being accessed.
1059///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001060/// \param Result on a successful parse, contains the parsed unqualified-id.
1061///
1062/// \returns true if parsing fails, false otherwise.
1063bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1064 bool AllowDestructorName,
1065 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001066 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001067 UnqualifiedId &Result) {
1068 // unqualified-id:
1069 // identifier
1070 // template-id (when it hasn't already been annotated)
1071 if (Tok.is(tok::identifier)) {
1072 // Consume the identifier.
1073 IdentifierInfo *Id = Tok.getIdentifierInfo();
1074 SourceLocation IdLoc = ConsumeToken();
1075
1076 if (AllowConstructorName &&
1077 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1078 // We have parsed a constructor name.
1079 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1080 &SS, false),
1081 IdLoc, IdLoc);
1082 } else {
1083 // We have parsed an identifier.
1084 Result.setIdentifier(Id, IdLoc);
1085 }
1086
1087 // If the next token is a '<', we may have a template.
1088 if (Tok.is(tok::less))
1089 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001090 ObjectType, Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001091
1092 return false;
1093 }
1094
1095 // unqualified-id:
1096 // template-id (already parsed and annotated)
1097 if (Tok.is(tok::annot_template_id)) {
1098 // FIXME: Could this be a constructor name???
1099
1100 // We have already parsed a template-id; consume the annotation token as
1101 // our unqualified-id.
1102 Result.setTemplateId(
1103 static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue()));
1104 ConsumeToken();
1105 return false;
1106 }
1107
1108 // unqualified-id:
1109 // operator-function-id
1110 // conversion-function-id
1111 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001112 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001113 return true;
1114
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001115 // If we have an operator-function-id and the next token is a '<', we may
1116 // have a
1117 //
1118 // template-id:
1119 // operator-function-id < template-argument-list[opt] >
1120 if (Result.getKind() == UnqualifiedId::IK_OperatorFunctionId &&
1121 Tok.is(tok::less))
1122 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1123 EnteringContext, ObjectType,
1124 Result);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001125
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001126 return false;
1127 }
1128
1129 if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
1130 // C++ [expr.unary.op]p10:
1131 // There is an ambiguity in the unary-expression ~X(), where X is a
1132 // class-name. The ambiguity is resolved in favor of treating ~ as a
1133 // unary complement rather than treating ~X as referring to a destructor.
1134
1135 // Parse the '~'.
1136 SourceLocation TildeLoc = ConsumeToken();
1137
1138 // Parse the class-name.
1139 if (Tok.isNot(tok::identifier)) {
1140 Diag(Tok, diag::err_destructor_class_name);
1141 return true;
1142 }
1143
1144 // Parse the class-name (or template-name in a simple-template-id).
1145 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1146 SourceLocation ClassNameLoc = ConsumeToken();
1147
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001148 if (Tok.is(tok::less)) {
1149 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1150 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
1151 EnteringContext, ObjectType, Result);
1152 }
1153
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001154 // Note that this is a destructor name.
1155 Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc,
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001156 CurScope, &SS, false, ObjectType);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001157 if (!Ty) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001158 if (ObjectType)
1159 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
1160 << ClassName;
1161 else
1162 Diag(ClassNameLoc, diag::err_destructor_class_name);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001163 return true;
1164 }
1165
1166 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001167 return false;
1168 }
1169
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001170 Diag(Tok, diag::err_expected_unqualified_id)
1171 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001172 return true;
1173}
1174
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001175/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1176/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001177///
Chris Lattner59232d32009-01-04 21:25:24 +00001178/// This method is called to parse the new expression after the optional :: has
1179/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1180/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001181///
1182/// new-expression:
1183/// '::'[opt] 'new' new-placement[opt] new-type-id
1184/// new-initializer[opt]
1185/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1186/// new-initializer[opt]
1187///
1188/// new-placement:
1189/// '(' expression-list ')'
1190///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001191/// new-type-id:
1192/// type-specifier-seq new-declarator[opt]
1193///
1194/// new-declarator:
1195/// ptr-operator new-declarator[opt]
1196/// direct-new-declarator
1197///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001198/// new-initializer:
1199/// '(' expression-list[opt] ')'
1200/// [C++0x] braced-init-list [TODO]
1201///
Chris Lattner59232d32009-01-04 21:25:24 +00001202Parser::OwningExprResult
1203Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1204 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1205 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001206
1207 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1208 // second form of new-expression. It can't be a new-type-id.
1209
Sebastian Redla55e52c2008-11-25 22:21:31 +00001210 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001211 SourceLocation PlacementLParen, PlacementRParen;
1212
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001213 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001214 DeclSpec DS;
1215 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001216 if (Tok.is(tok::l_paren)) {
1217 // If it turns out to be a placement, we change the type location.
1218 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001219 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1220 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001221 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001222 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001223
1224 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001225 if (PlacementRParen.isInvalid()) {
1226 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001227 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001228 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001229
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001230 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001231 // Reset the placement locations. There was no placement.
1232 PlacementLParen = PlacementRParen = SourceLocation();
1233 ParenTypeId = true;
1234 } else {
1235 // We still need the type.
1236 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001237 SourceLocation LParen = ConsumeParen();
1238 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001239 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001240 ParseDeclarator(DeclaratorInfo);
1241 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001242 ParenTypeId = true;
1243 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001244 if (ParseCXXTypeSpecifierSeq(DS))
1245 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001246 else {
1247 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001248 ParseDeclaratorInternal(DeclaratorInfo,
1249 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001250 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001251 ParenTypeId = false;
1252 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001253 }
1254 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001255 // A new-type-id is a simplified type-id, where essentially the
1256 // direct-declarator is replaced by a direct-new-declarator.
1257 if (ParseCXXTypeSpecifierSeq(DS))
1258 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001259 else {
1260 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001261 ParseDeclaratorInternal(DeclaratorInfo,
1262 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001263 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001264 ParenTypeId = false;
1265 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001266 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001267 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001268 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001269 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001270
Sebastian Redla55e52c2008-11-25 22:21:31 +00001271 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001272 SourceLocation ConstructorLParen, ConstructorRParen;
1273
1274 if (Tok.is(tok::l_paren)) {
1275 ConstructorLParen = ConsumeParen();
1276 if (Tok.isNot(tok::r_paren)) {
1277 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001278 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1279 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001280 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001281 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001282 }
1283 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001284 if (ConstructorRParen.isInvalid()) {
1285 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001286 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001287 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001288 }
1289
Sebastian Redlf53597f2009-03-15 17:47:39 +00001290 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1291 move_arg(PlacementArgs), PlacementRParen,
1292 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1293 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001294}
1295
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001296/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1297/// passed to ParseDeclaratorInternal.
1298///
1299/// direct-new-declarator:
1300/// '[' expression ']'
1301/// direct-new-declarator '[' constant-expression ']'
1302///
Chris Lattner59232d32009-01-04 21:25:24 +00001303void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001304 // Parse the array dimensions.
1305 bool first = true;
1306 while (Tok.is(tok::l_square)) {
1307 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001308 OwningExprResult Size(first ? ParseExpression()
1309 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001310 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001311 // Recover
1312 SkipUntil(tok::r_square);
1313 return;
1314 }
1315 first = false;
1316
Sebastian Redlab197ba2009-02-09 18:23:29 +00001317 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001318 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001319 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001320 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001321
Sebastian Redlab197ba2009-02-09 18:23:29 +00001322 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001323 return;
1324 }
1325}
1326
1327/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1328/// This ambiguity appears in the syntax of the C++ new operator.
1329///
1330/// new-expression:
1331/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1332/// new-initializer[opt]
1333///
1334/// new-placement:
1335/// '(' expression-list ')'
1336///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001337bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001338 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001339 // The '(' was already consumed.
1340 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001341 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001342 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001343 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001344 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001345 }
1346
1347 // It's not a type, it has to be an expression list.
1348 // Discard the comma locations - ActOnCXXNew has enough parameters.
1349 CommaLocsTy CommaLocs;
1350 return ParseExpressionList(PlacementArgs, CommaLocs);
1351}
1352
1353/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1354/// to free memory allocated by new.
1355///
Chris Lattner59232d32009-01-04 21:25:24 +00001356/// This method is called to parse the 'delete' expression after the optional
1357/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1358/// and "Start" is its location. Otherwise, "Start" is the location of the
1359/// 'delete' token.
1360///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001361/// delete-expression:
1362/// '::'[opt] 'delete' cast-expression
1363/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001364Parser::OwningExprResult
1365Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1366 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1367 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001368
1369 // Array delete?
1370 bool ArrayDelete = false;
1371 if (Tok.is(tok::l_square)) {
1372 ArrayDelete = true;
1373 SourceLocation LHS = ConsumeBracket();
1374 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1375 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001376 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001377 }
1378
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001379 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001380 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001381 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001382
Sebastian Redlf53597f2009-03-15 17:47:39 +00001383 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001384}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001385
Mike Stump1eb44332009-09-09 15:08:12 +00001386static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001387 switch(kind) {
1388 default: assert(false && "Not a known unary type trait.");
1389 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1390 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1391 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1392 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1393 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1394 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1395 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1396 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1397 case tok::kw___is_abstract: return UTT_IsAbstract;
1398 case tok::kw___is_class: return UTT_IsClass;
1399 case tok::kw___is_empty: return UTT_IsEmpty;
1400 case tok::kw___is_enum: return UTT_IsEnum;
1401 case tok::kw___is_pod: return UTT_IsPOD;
1402 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1403 case tok::kw___is_union: return UTT_IsUnion;
1404 }
1405}
1406
1407/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1408/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1409/// templates.
1410///
1411/// primary-expression:
1412/// [GNU] unary-type-trait '(' type-id ')'
1413///
Mike Stump1eb44332009-09-09 15:08:12 +00001414Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001415 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1416 SourceLocation Loc = ConsumeToken();
1417
1418 SourceLocation LParen = Tok.getLocation();
1419 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1420 return ExprError();
1421
1422 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1423 // there will be cryptic errors about mismatched parentheses and missing
1424 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001425 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001426
1427 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1428
Douglas Gregor809070a2009-02-18 17:45:20 +00001429 if (Ty.isInvalid())
1430 return ExprError();
1431
1432 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001433}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001434
1435/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1436/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1437/// based on the context past the parens.
1438Parser::OwningExprResult
1439Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1440 TypeTy *&CastTy,
1441 SourceLocation LParenLoc,
1442 SourceLocation &RParenLoc) {
1443 assert(getLang().CPlusPlus && "Should only be called for C++!");
1444 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1445 assert(isTypeIdInParens() && "Not a type-id!");
1446
1447 OwningExprResult Result(Actions, true);
1448 CastTy = 0;
1449
1450 // We need to disambiguate a very ugly part of the C++ syntax:
1451 //
1452 // (T())x; - type-id
1453 // (T())*x; - type-id
1454 // (T())/x; - expression
1455 // (T()); - expression
1456 //
1457 // The bad news is that we cannot use the specialized tentative parser, since
1458 // it can only verify that the thing inside the parens can be parsed as
1459 // type-id, it is not useful for determining the context past the parens.
1460 //
1461 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001462 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001463 //
1464 // It uses a scheme similar to parsing inline methods. The parenthesized
1465 // tokens are cached, the context that follows is determined (possibly by
1466 // parsing a cast-expression), and then we re-introduce the cached tokens
1467 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001468
Mike Stump1eb44332009-09-09 15:08:12 +00001469 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001470 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001471
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001472 // Store the tokens of the parentheses. We will parse them after we determine
1473 // the context that follows them.
1474 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1475 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001476 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1477 return ExprError();
1478 }
1479
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001480 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001481 ParseAs = CompoundLiteral;
1482 } else {
1483 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001484 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1485 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1486 NotCastExpr = true;
1487 } else {
1488 // Try parsing the cast-expression that may follow.
1489 // If it is not a cast-expression, NotCastExpr will be true and no token
1490 // will be consumed.
1491 Result = ParseCastExpression(false/*isUnaryExpression*/,
1492 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001493 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001494 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001495
1496 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1497 // an expression.
1498 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001499 }
1500
Mike Stump1eb44332009-09-09 15:08:12 +00001501 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001502 Toks.push_back(Tok);
1503 // Re-enter the stored parenthesized tokens into the token stream, so we may
1504 // parse them now.
1505 PP.EnterTokenStream(Toks.data(), Toks.size(),
1506 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1507 // Drop the current token and bring the first cached one. It's the same token
1508 // as when we entered this function.
1509 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001510
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001511 if (ParseAs >= CompoundLiteral) {
1512 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001513
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001514 // Match the ')'.
1515 if (Tok.is(tok::r_paren))
1516 RParenLoc = ConsumeParen();
1517 else
1518 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001519
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001520 if (ParseAs == CompoundLiteral) {
1521 ExprType = CompoundLiteral;
1522 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1523 }
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001525 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1526 assert(ParseAs == CastExpr);
1527
1528 if (Ty.isInvalid())
1529 return ExprError();
1530
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001531 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001532
1533 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001534 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001535 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001536 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001537 return move(Result);
1538 }
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001540 // Not a compound literal, and not followed by a cast-expression.
1541 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001542
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001543 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001544 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001545 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1546 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1547
1548 // Match the ')'.
1549 if (Result.isInvalid()) {
1550 SkipUntil(tok::r_paren);
1551 return ExprError();
1552 }
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001554 if (Tok.is(tok::r_paren))
1555 RParenLoc = ConsumeParen();
1556 else
1557 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1558
1559 return move(Result);
1560}