blob: 1faeebc37cd4edd0390832d4e12990f2644a5259 [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"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
Douglas Gregorae7902c2011-08-04 15:30:47 +000018#include "clang/Sema/Scope.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000020#include "llvm/Support/ErrorHandling.h"
21
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Richard Smithea698b32011-04-14 21:45:45 +000024static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
25 switch (Kind) {
26 case tok::kw_template: return 0;
27 case tok::kw_const_cast: return 1;
28 case tok::kw_dynamic_cast: return 2;
29 case tok::kw_reinterpret_cast: return 3;
30 case tok::kw_static_cast: return 4;
31 default:
32 assert(0 && "Unknown type for digraph error message.");
33 return -1;
34 }
35}
36
37// Are the two tokens adjacent in the same source file?
38static bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
39 SourceManager &SM = PP.getSourceManager();
40 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
41 SourceLocation FirstEnd = FirstLoc.getFileLocWithOffset(First.getLength());
42 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
43}
44
45// Suggest fixit for "<::" after a cast.
46static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
47 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
48 // Pull '<:' and ':' off token stream.
49 if (!AtDigraph)
50 PP.Lex(DigraphToken);
51 PP.Lex(ColonToken);
52
53 SourceRange Range;
54 Range.setBegin(DigraphToken.getLocation());
55 Range.setEnd(ColonToken.getLocation());
56 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
57 << SelectDigraphErrorMessage(Kind)
58 << FixItHint::CreateReplacement(Range, "< ::");
59
60 // Update token information to reflect their change in token type.
61 ColonToken.setKind(tok::coloncolon);
62 ColonToken.setLocation(ColonToken.getLocation().getFileLocWithOffset(-1));
63 ColonToken.setLength(2);
64 DigraphToken.setKind(tok::less);
65 DigraphToken.setLength(1);
66
67 // Push new tokens back to token stream.
68 PP.EnterToken(ColonToken);
69 if (!AtDigraph)
70 PP.EnterToken(DigraphToken);
71}
72
Mike Stump1eb44332009-09-09 15:08:12 +000073/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000074///
75/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000076/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000077/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000078///
79/// '::'[opt] nested-name-specifier
80/// '::'
81///
82/// nested-name-specifier:
83/// type-name '::'
84/// namespace-name '::'
85/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000086/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000087///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000088///
Mike Stump1eb44332009-09-09 15:08:12 +000089/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000090/// nested-name-specifier (or empty)
91///
Mike Stump1eb44332009-09-09 15:08:12 +000092/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000093/// the "." or "->" of a member access expression, this parameter provides the
94/// type of the object whose members are being accessed.
95///
96/// \param EnteringContext whether we will be entering into the context of
97/// the nested-name-specifier after parsing it.
98///
Douglas Gregord4dca082010-02-24 18:44:31 +000099/// \param MayBePseudoDestructor When non-NULL, points to a flag that
100/// indicates whether this nested-name-specifier may be part of a
101/// pseudo-destructor name. In this case, the flag will be set false
102/// if we don't actually end up parsing a destructor name. Moreorover,
103/// if we do end up determining that we are parsing a destructor name,
104/// the last component of the nested-name-specifier is not parsed as
105/// part of the scope specifier.
106
Douglas Gregorb10cd042010-02-21 18:36:56 +0000107/// member access expression, e.g., the \p T:: in \p p->T::m.
108///
John McCall9ba61662010-02-26 08:45:28 +0000109/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000110bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000111 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000112 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000113 bool *MayBePseudoDestructor,
114 bool IsTypename) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000115 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000116 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000118 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +0000119 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
120 Tok.getAnnotationRange(),
121 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000122 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000123 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000124 }
Chris Lattnere607e802009-01-04 21:14:15 +0000125
Douglas Gregor39a8de12009-02-25 19:37:18 +0000126 bool HasScopeSpecifier = false;
127
Chris Lattner5b454732009-01-05 03:55:46 +0000128 if (Tok.is(tok::coloncolon)) {
129 // ::new and ::delete aren't nested-name-specifiers.
130 tok::TokenKind NextKind = NextToken().getKind();
131 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
132 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattner55a7cef2009-01-05 00:13:00 +0000134 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000135 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
136 return true;
137
Douglas Gregor39a8de12009-02-25 19:37:18 +0000138 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000139 }
140
Douglas Gregord4dca082010-02-24 18:44:31 +0000141 bool CheckForDestructor = false;
142 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
143 CheckForDestructor = true;
144 *MayBePseudoDestructor = false;
145 }
146
Douglas Gregor39a8de12009-02-25 19:37:18 +0000147 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000148 if (HasScopeSpecifier) {
149 // C++ [basic.lookup.classref]p5:
150 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000151 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000152 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000153 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000154 // the class-name-or-namespace-name is looked up in global scope as a
155 // class-name or namespace-name.
156 //
157 // To implement this, we clear out the object type as soon as we've
158 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000159 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000160
161 if (Tok.is(tok::code_completion)) {
162 // Code completion for a nested-name-specifier, where the code
163 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000164 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000165 // Include code completion token into the range of the scope otherwise
166 // when we try to annotate the scope tokens the dangling code completion
167 // token will cause assertion in
168 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000169 SS.setEndLoc(Tok.getLocation());
170 cutOffParsing();
171 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000172 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000173 }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Douglas Gregor39a8de12009-02-25 19:37:18 +0000175 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000176 // nested-name-specifier 'template'[opt] simple-template-id '::'
177
178 // Parse the optional 'template' keyword, then make sure we have
179 // 'identifier <' after it.
180 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000181 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000182 // nested-name-specifier, since they aren't allowed to start with
183 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000184 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000185 break;
186
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000187 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000188 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000189
190 UnqualifiedId TemplateName;
191 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000192 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000193 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000194 ConsumeToken();
195 } else if (Tok.is(tok::kw_operator)) {
196 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000197 TemplateName)) {
198 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000199 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000200 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000201
Sean Hunte6252d12009-11-28 08:58:14 +0000202 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
203 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000204 Diag(TemplateName.getSourceRange().getBegin(),
205 diag::err_id_after_template_in_nested_name_spec)
206 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000207 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000208 break;
209 }
210 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000211 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000212 break;
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000215 // If the next token is not '<', we have a qualified-id that refers
216 // to a template name, such as T::template apply, but is not a
217 // template-id.
218 if (Tok.isNot(tok::less)) {
219 TPA.Revert();
220 break;
221 }
222
223 // Commit to parsing the template-id.
224 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000225 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000226 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000227 TemplateKWLoc,
228 SS,
229 TemplateName,
230 ObjectType,
231 EnteringContext,
232 Template)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000233 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000234 TemplateKWLoc, false))
235 return true;
236 } else
John McCall9ba61662010-02-26 08:45:28 +0000237 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Chris Lattner77cf72a2009-06-26 03:47:46 +0000239 continue;
240 }
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Douglas Gregor39a8de12009-02-25 19:37:18 +0000242 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000243 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000244 //
245 // simple-template-id '::'
246 //
247 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000248 // right kind (it should name a type or be dependent), and then
249 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000250 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000251 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
252 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000253 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000254 }
255
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000256 // Consume the template-id token.
257 ConsumeToken();
258
259 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
260 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000262 if (!HasScopeSpecifier)
263 HasScopeSpecifier = true;
264
265 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
266 TemplateId->getTemplateArgs(),
267 TemplateId->NumArgs);
268
269 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
270 /*FIXME:*/SourceLocation(),
271 SS,
272 TemplateId->Template,
273 TemplateId->TemplateNameLoc,
274 TemplateId->LAngleLoc,
275 TemplateArgsPtr,
276 TemplateId->RAngleLoc,
277 CCLoc,
278 EnteringContext)) {
279 SourceLocation StartLoc
280 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
281 : TemplateId->TemplateNameLoc;
282 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000283 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000284
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000285 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000286 }
287
Chris Lattner5c7f7862009-06-26 03:52:38 +0000288
289 // The rest of the nested-name-specifier possibilities start with
290 // tok::identifier.
291 if (Tok.isNot(tok::identifier))
292 break;
293
294 IdentifierInfo &II = *Tok.getIdentifierInfo();
295
296 // nested-name-specifier:
297 // type-name '::'
298 // namespace-name '::'
299 // nested-name-specifier identifier '::'
300 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000301
302 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
303 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000304 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000305 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
306 Tok.getLocation(),
307 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000308 EnteringContext) &&
309 // If the token after the colon isn't an identifier, it's still an
310 // error, but they probably meant something else strange so don't
311 // recover like this.
312 PP.LookAhead(1).is(tok::identifier)) {
313 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000314 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000315
316 // Recover as if the user wrote '::'.
317 Next.setKind(tok::coloncolon);
318 }
Chris Lattner46646492009-12-07 01:36:53 +0000319 }
320
Chris Lattner5c7f7862009-06-26 03:52:38 +0000321 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000322 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000323 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000324 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000325 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000326 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000327 }
328
Chris Lattner5c7f7862009-06-26 03:52:38 +0000329 // We have an identifier followed by a '::'. Lookup this name
330 // as the name in a nested-name-specifier.
331 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000332 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
333 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000334 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000336 HasScopeSpecifier = true;
337 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
338 ObjectType, EnteringContext, SS))
339 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
340
Chris Lattner5c7f7862009-06-26 03:52:38 +0000341 continue;
342 }
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Richard Smithea698b32011-04-14 21:45:45 +0000344 // Check for '<::' which should be '< ::' instead of '[:' when following
345 // a template name.
346 if (Next.is(tok::l_square) && Next.getLength() == 2) {
347 Token SecondToken = GetLookAheadToken(2);
348 if (SecondToken.is(tok::colon) &&
349 AreTokensAdjacent(PP, Next, SecondToken)) {
350 TemplateTy Template;
351 UnqualifiedId TemplateName;
352 TemplateName.setIdentifier(&II, Tok.getLocation());
353 bool MemberOfUnknownSpecialization;
354 if (Actions.isTemplateName(getCurScope(), SS,
355 /*hasTemplateKeyword=*/false,
356 TemplateName,
357 ObjectType,
358 EnteringContext,
359 Template,
360 MemberOfUnknownSpecialization)) {
361 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
362 /*AtDigraph*/false);
363 }
364 }
365 }
366
Chris Lattner5c7f7862009-06-26 03:52:38 +0000367 // nested-name-specifier:
368 // type-name '<'
369 if (Next.is(tok::less)) {
370 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000371 UnqualifiedId TemplateName;
372 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000373 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000374 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000375 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000376 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000377 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000378 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000379 Template,
380 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000381 // We have found a template name, so annotate this this token
382 // with a template-id annotation. We do not permit the
383 // template-id to be translated into a type annotation,
384 // because some clients (e.g., the parsing of class template
385 // specializations) still want to see the original template-id
386 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000387 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000388 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000389 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000390 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000391 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000392 }
393
394 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000395 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000396 // We have something like t::getAs<T>, where getAs is a
397 // member of an unknown specialization. However, this will only
398 // parse correctly as a template, so suggest the keyword 'template'
399 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000400 unsigned DiagID = diag::err_missing_dependent_template_keyword;
401 if (getLang().Microsoft)
Francois Pichetcf320c62011-04-22 08:25:24 +0000402 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000403
404 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000405 << II.getName()
406 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
407
Douglas Gregord6ab2322010-06-16 23:00:59 +0000408 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000409 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000410 Tok.getLocation(), SS,
411 TemplateName, ObjectType,
412 EnteringContext, Template)) {
413 // Consume the identifier.
414 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000415 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000416 SourceLocation(), false))
417 return true;
418 }
419 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000420 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000421
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000422 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000423 }
424 }
425
Douglas Gregor39a8de12009-02-25 19:37:18 +0000426 // We don't have any tokens that form the beginning of a
427 // nested-name-specifier, so we're done.
428 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Douglas Gregord4dca082010-02-24 18:44:31 +0000431 // Even if we didn't see any pieces of a nested-name-specifier, we
432 // still check whether there is a tilde in this position, which
433 // indicates a potential pseudo-destructor.
434 if (CheckForDestructor && Tok.is(tok::tilde))
435 *MayBePseudoDestructor = true;
436
John McCall9ba61662010-02-26 08:45:28 +0000437 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000438}
439
440/// ParseCXXIdExpression - Handle id-expression.
441///
442/// id-expression:
443/// unqualified-id
444/// qualified-id
445///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000446/// qualified-id:
447/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
448/// '::' identifier
449/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000450/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000451///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000452/// NOTE: The standard specifies that, for qualified-id, the parser does not
453/// expect:
454///
455/// '::' conversion-function-id
456/// '::' '~' class-name
457///
458/// This may cause a slight inconsistency on diagnostics:
459///
460/// class C {};
461/// namespace A {}
462/// void f() {
463/// :: A :: ~ C(); // Some Sema error about using destructor with a
464/// // namespace.
465/// :: ~ C(); // Some Parser error like 'unexpected ~'.
466/// }
467///
468/// We simplify the parser a bit and make it work like:
469///
470/// qualified-id:
471/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
472/// '::' unqualified-id
473///
474/// That way Sema can handle and report similar errors for namespaces and the
475/// global scope.
476///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000477/// The isAddressOfOperand parameter indicates that this id-expression is a
478/// direct operand of the address-of operator. This is, besides member contexts,
479/// the only place where a qualified-id naming a non-static class member may
480/// appear.
481///
John McCall60d7b3a2010-08-24 06:29:42 +0000482ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000483 // qualified-id:
484 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
485 // '::' unqualified-id
486 //
487 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000488 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000489
490 UnqualifiedId Name;
491 if (ParseUnqualifiedId(SS,
492 /*EnteringContext=*/false,
493 /*AllowDestructorName=*/false,
494 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000495 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000496 Name))
497 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000498
499 // This is only the direct operand of an & operator if it is not
500 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000501 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
502 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000503
Douglas Gregor23c94db2010-07-02 17:43:08 +0000504 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000505 isAddressOfOperand);
506
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000507}
508
Douglas Gregorae7902c2011-08-04 15:30:47 +0000509/// ParseLambdaExpression - Parse a C++0x lambda expression.
510///
511/// lambda-expression:
512/// lambda-introducer lambda-declarator[opt] compound-statement
513///
514/// lambda-introducer:
515/// '[' lambda-capture[opt] ']'
516///
517/// lambda-capture:
518/// capture-default
519/// capture-list
520/// capture-default ',' capture-list
521///
522/// capture-default:
523/// '&'
524/// '='
525///
526/// capture-list:
527/// capture
528/// capture-list ',' capture
529///
530/// capture:
531/// identifier
532/// '&' identifier
533/// 'this'
534///
535/// lambda-declarator:
536/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
537/// 'mutable'[opt] exception-specification[opt]
538/// trailing-return-type[opt]
539///
540ExprResult Parser::ParseLambdaExpression() {
541 // Parse lambda-introducer.
542 LambdaIntroducer Intro;
543
544 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
545 if (DiagID) {
546 Diag(Tok, DiagID.getValue());
547 SkipUntil(tok::r_square);
548 }
549
550 return ParseLambdaExpressionAfterIntroducer(Intro);
551}
552
553/// TryParseLambdaExpression - Use lookahead and potentially tentative
554/// parsing to determine if we are looking at a C++0x lambda expression, and parse
555/// it if we are.
556///
557/// If we are not looking at a lambda expression, returns ExprError().
558ExprResult Parser::TryParseLambdaExpression() {
559 assert(getLang().CPlusPlus0x
560 && Tok.is(tok::l_square)
561 && "Not at the start of a possible lambda expression.");
562
563 const Token Next = NextToken(), After = GetLookAheadToken(2);
564
565 // If lookahead indicates this is a lambda...
566 if (Next.is(tok::r_square) || // []
567 Next.is(tok::equal) || // [=
568 (Next.is(tok::amp) && // [&] or [&,
569 (After.is(tok::r_square) ||
570 After.is(tok::comma))) ||
571 (Next.is(tok::identifier) && // [identifier]
572 After.is(tok::r_square))) {
573 return ParseLambdaExpression();
574 }
575
576 // If lookahead indicates this is an Objective-C message...
577 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
578 return ExprError();
579 }
580
581 LambdaIntroducer Intro;
582 if (TryParseLambdaIntroducer(Intro))
583 return ExprError();
584 return ParseLambdaExpressionAfterIntroducer(Intro);
585}
586
587/// ParseLambdaExpression - Parse a lambda introducer.
588///
589/// Returns a DiagnosticID if it hit something unexpected.
590llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
591 typedef llvm::Optional<unsigned> DiagResult;
592
593 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
594 Intro.Range.setBegin(ConsumeBracket());
595
596 bool first = true;
597
598 // Parse capture-default.
599 if (Tok.is(tok::amp) &&
600 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
601 Intro.Default = LCD_ByRef;
602 ConsumeToken();
603 first = false;
604 } else if (Tok.is(tok::equal)) {
605 Intro.Default = LCD_ByCopy;
606 ConsumeToken();
607 first = false;
608 }
609
610 while (Tok.isNot(tok::r_square)) {
611 if (!first) {
612 if (Tok.isNot(tok::comma))
613 return DiagResult(diag::err_expected_comma_or_rsquare);
614 ConsumeToken();
615 }
616
617 first = false;
618
619 // Parse capture.
620 LambdaCaptureKind Kind = LCK_ByCopy;
621 SourceLocation Loc;
622 IdentifierInfo* Id = 0;
623
624 if (Tok.is(tok::kw_this)) {
625 Kind = LCK_This;
626 Loc = ConsumeToken();
627 } else {
628 if (Tok.is(tok::amp)) {
629 Kind = LCK_ByRef;
630 ConsumeToken();
631 }
632
633 if (Tok.is(tok::identifier)) {
634 Id = Tok.getIdentifierInfo();
635 Loc = ConsumeToken();
636 } else if (Tok.is(tok::kw_this)) {
637 // FIXME: If we want to suggest a fixit here, will need to return more
638 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
639 // Clear()ed to prevent emission in case of tentative parsing?
640 return DiagResult(diag::err_this_captured_by_reference);
641 } else {
642 return DiagResult(diag::err_expected_capture);
643 }
644 }
645
646 Intro.addCapture(Kind, Loc, Id);
647 }
648
649 Intro.Range.setEnd(MatchRHSPunctuation(tok::r_square,
650 Intro.Range.getBegin()));
651
652 return DiagResult();
653}
654
655/// TryParseLambdaExpression - Tentatively parse a lambda introducer.
656///
657/// Returns true if it hit something unexpected.
658bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
659 TentativeParsingAction PA(*this);
660
661 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
662
663 if (DiagID) {
664 PA.Revert();
665 return true;
666 }
667
668 PA.Commit();
669 return false;
670}
671
672/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
673/// expression.
674ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
675 LambdaIntroducer &Intro) {
676 // Parse lambda-declarator[opt].
677 DeclSpec DS(AttrFactory);
678 Declarator D(DS, Declarator::PrototypeContext);
679
680 if (Tok.is(tok::l_paren)) {
681 ParseScope PrototypeScope(this,
682 Scope::FunctionPrototypeScope |
683 Scope::DeclScope);
684
685 SourceLocation DeclLoc = ConsumeParen(), DeclEndLoc;
686
687 // Parse parameter-declaration-clause.
688 ParsedAttributes Attr(AttrFactory);
689 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
690 SourceLocation EllipsisLoc;
691
692 if (Tok.isNot(tok::r_paren))
693 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
694
695 DeclEndLoc = MatchRHSPunctuation(tok::r_paren, DeclLoc);
696
697 // Parse 'mutable'[opt].
698 SourceLocation MutableLoc;
699 if (Tok.is(tok::kw_mutable)) {
700 MutableLoc = ConsumeToken();
701 DeclEndLoc = MutableLoc;
702 }
703
704 // Parse exception-specification[opt].
705 ExceptionSpecificationType ESpecType = EST_None;
706 SourceRange ESpecRange;
707 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
708 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
709 ExprResult NoexceptExpr;
710 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
711 DynamicExceptions,
712 DynamicExceptionRanges,
713 NoexceptExpr);
714
715 if (ESpecType != EST_None)
716 DeclEndLoc = ESpecRange.getEnd();
717
718 // Parse attribute-specifier[opt].
719 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
720
721 // Parse trailing-return-type[opt].
722 ParsedType TrailingReturnType;
723 if (Tok.is(tok::arrow)) {
724 SourceRange Range;
725 TrailingReturnType = ParseTrailingReturnType(Range).get();
726 if (Range.getEnd().isValid())
727 DeclEndLoc = Range.getEnd();
728 }
729
730 PrototypeScope.Exit();
731
732 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
733 /*isVariadic=*/EllipsisLoc.isValid(),
734 EllipsisLoc,
735 ParamInfo.data(), ParamInfo.size(),
736 DS.getTypeQualifiers(),
737 /*RefQualifierIsLValueRef=*/true,
738 /*RefQualifierLoc=*/SourceLocation(),
739 MutableLoc,
740 ESpecType, ESpecRange.getBegin(),
741 DynamicExceptions.data(),
742 DynamicExceptionRanges.data(),
743 DynamicExceptions.size(),
744 NoexceptExpr.isUsable() ?
745 NoexceptExpr.get() : 0,
746 DeclLoc, DeclEndLoc, D,
747 TrailingReturnType),
748 Attr, DeclEndLoc);
749 }
750
751 // Parse compound-statement.
752 if (Tok.is(tok::l_brace)) {
753 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
754 // it.
755 ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
756 Scope::BreakScope | Scope::ContinueScope |
757 Scope::DeclScope);
758
759 StmtResult Stmt(ParseCompoundStatementBody());
760
761 BodyScope.Exit();
762 } else {
763 Diag(Tok, diag::err_expected_lambda_body);
764 }
765
766 return ExprEmpty();
767}
768
Reid Spencer5f016e22007-07-11 17:01:13 +0000769/// ParseCXXCasts - This handles the various ways to cast expressions to another
770/// type.
771///
772/// postfix-expression: [C++ 5.2p1]
773/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
774/// 'static_cast' '<' type-name '>' '(' expression ')'
775/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
776/// 'const_cast' '<' type-name '>' '(' expression ')'
777///
John McCall60d7b3a2010-08-24 06:29:42 +0000778ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 tok::TokenKind Kind = Tok.getKind();
780 const char *CastName = 0; // For error messages
781
782 switch (Kind) {
783 default: assert(0 && "Unknown C++ cast!"); abort();
784 case tok::kw_const_cast: CastName = "const_cast"; break;
785 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
786 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
787 case tok::kw_static_cast: CastName = "static_cast"; break;
788 }
789
790 SourceLocation OpLoc = ConsumeToken();
791 SourceLocation LAngleBracketLoc = Tok.getLocation();
792
Richard Smithea698b32011-04-14 21:45:45 +0000793 // Check for "<::" which is parsed as "[:". If found, fix token stream,
794 // diagnose error, suggest fix, and recover parsing.
795 Token Next = NextToken();
796 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
797 AreTokensAdjacent(PP, Tok, Next))
798 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
799
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000801 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000802
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000803 // Parse the common declaration-specifiers piece.
804 DeclSpec DS(AttrFactory);
805 ParseSpecifierQualifierList(DS);
806
807 // Parse the abstract-declarator, if present.
808 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
809 ParseDeclarator(DeclaratorInfo);
810
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 SourceLocation RAngleBracketLoc = Tok.getLocation();
812
Chris Lattner1ab3b962008-11-18 07:48:38 +0000813 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000814 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000815
816 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
817
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000818 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
819 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000820
John McCall60d7b3a2010-08-24 06:29:42 +0000821 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000823 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000824 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000825
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000826 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +0000827 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000828 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +0000829 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000830 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000831
Sebastian Redl20df9b72008-12-11 22:51:44 +0000832 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000833}
834
Sebastian Redlc42e1182008-11-11 11:37:55 +0000835/// ParseCXXTypeid - This handles the C++ typeid expression.
836///
837/// postfix-expression: [C++ 5.2p1]
838/// 'typeid' '(' expression ')'
839/// 'typeid' '(' type-id ')'
840///
John McCall60d7b3a2010-08-24 06:29:42 +0000841ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000842 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
843
844 SourceLocation OpLoc = ConsumeToken();
845 SourceLocation LParenLoc = Tok.getLocation();
846 SourceLocation RParenLoc;
847
848 // typeid expressions are always parenthesized.
849 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
850 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000851 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000852
John McCall60d7b3a2010-08-24 06:29:42 +0000853 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000854
855 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000856 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000857
858 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000859 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000860
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000861 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000862 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000863
864 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000865 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000866 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000867 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000868 // When typeid is applied to an expression other than an lvalue of a
869 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000870 // operand (Clause 5).
871 //
Mike Stump1eb44332009-09-09 15:08:12 +0000872 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000873 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000874 // we the expression is potentially potentially evaluated.
875 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000876 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000877 Result = ParseExpression();
878
879 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000880 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000881 SkipUntil(tok::r_paren);
882 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000883 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
884 if (RParenLoc.isInvalid())
885 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000886
887 // If we are a foo<int> that identifies a single function, resolve it now...
888 Expr* e = Result.get();
889 if (e->getType() == Actions.Context.OverloadTy) {
890 ExprResult er =
891 Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
892 if (er.isUsable())
893 Result = er.release();
894 }
Sebastian Redlc42e1182008-11-11 11:37:55 +0000895 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000896 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000897 }
898 }
899
Sebastian Redl20df9b72008-12-11 22:51:44 +0000900 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000901}
902
Francois Pichet01b7c302010-09-08 12:20:18 +0000903/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
904///
905/// '__uuidof' '(' expression ')'
906/// '__uuidof' '(' type-id ')'
907///
908ExprResult Parser::ParseCXXUuidof() {
909 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
910
911 SourceLocation OpLoc = ConsumeToken();
912 SourceLocation LParenLoc = Tok.getLocation();
913 SourceLocation RParenLoc;
914
915 // __uuidof expressions are always parenthesized.
916 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
917 "__uuidof"))
918 return ExprError();
919
920 ExprResult Result;
921
922 if (isTypeIdInParens()) {
923 TypeResult Ty = ParseTypeName();
924
925 // Match the ')'.
926 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
927
928 if (Ty.isInvalid())
929 return ExprError();
930
931 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
932 Ty.get().getAsOpaquePtr(), RParenLoc);
933 } else {
934 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
935 Result = ParseExpression();
936
937 // Match the ')'.
938 if (Result.isInvalid())
939 SkipUntil(tok::r_paren);
940 else {
941 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
942
943 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
944 Result.release(), RParenLoc);
945 }
946 }
947
948 return move(Result);
949}
950
Douglas Gregord4dca082010-02-24 18:44:31 +0000951/// \brief Parse a C++ pseudo-destructor expression after the base,
952/// . or -> operator, and nested-name-specifier have already been
953/// parsed.
954///
955/// postfix-expression: [C++ 5.2]
956/// postfix-expression . pseudo-destructor-name
957/// postfix-expression -> pseudo-destructor-name
958///
959/// pseudo-destructor-name:
960/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
961/// ::[opt] nested-name-specifier template simple-template-id ::
962/// ~type-name
963/// ::[opt] nested-name-specifier[opt] ~type-name
964///
John McCall60d7b3a2010-08-24 06:29:42 +0000965ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000966Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
967 tok::TokenKind OpKind,
968 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000969 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000970 // We're parsing either a pseudo-destructor-name or a dependent
971 // member access that has the same form as a
972 // pseudo-destructor-name. We parse both in the same way and let
973 // the action model sort them out.
974 //
975 // Note that the ::[opt] nested-name-specifier[opt] has already
976 // been parsed, and if there was a simple-template-id, it has
977 // been coalesced into a template-id annotation token.
978 UnqualifiedId FirstTypeName;
979 SourceLocation CCLoc;
980 if (Tok.is(tok::identifier)) {
981 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
982 ConsumeToken();
983 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
984 CCLoc = ConsumeToken();
985 } else if (Tok.is(tok::annot_template_id)) {
986 FirstTypeName.setTemplateId(
987 (TemplateIdAnnotation *)Tok.getAnnotationValue());
988 ConsumeToken();
989 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
990 CCLoc = ConsumeToken();
991 } else {
992 FirstTypeName.setIdentifier(0, SourceLocation());
993 }
994
995 // Parse the tilde.
996 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
997 SourceLocation TildeLoc = ConsumeToken();
998 if (!Tok.is(tok::identifier)) {
999 Diag(Tok, diag::err_destructor_tilde_identifier);
1000 return ExprError();
1001 }
1002
1003 // Parse the second type.
1004 UnqualifiedId SecondTypeName;
1005 IdentifierInfo *Name = Tok.getIdentifierInfo();
1006 SourceLocation NameLoc = ConsumeToken();
1007 SecondTypeName.setIdentifier(Name, NameLoc);
1008
1009 // If there is a '<', the second type name is a template-id. Parse
1010 // it as such.
1011 if (Tok.is(tok::less) &&
1012 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001013 SecondTypeName, /*AssumeTemplateName=*/true,
1014 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +00001015 return ExprError();
1016
John McCall9ae2f072010-08-23 23:25:46 +00001017 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1018 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001019 SS, FirstTypeName, CCLoc,
1020 TildeLoc, SecondTypeName,
1021 Tok.is(tok::l_paren));
1022}
1023
Reid Spencer5f016e22007-07-11 17:01:13 +00001024/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1025///
1026/// boolean-literal: [C++ 2.13.5]
1027/// 'true'
1028/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001029ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001031 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001032}
Chris Lattner50dd2892008-02-26 00:51:44 +00001033
1034/// ParseThrowExpression - This handles the C++ throw expression.
1035///
1036/// throw-expression: [C++ 15]
1037/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001038ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001039 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001040 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001041
Chris Lattner2a2819a2008-04-06 06:02:23 +00001042 // If the current token isn't the start of an assignment-expression,
1043 // then the expression is not present. This handles things like:
1044 // "C ? throw : (void)42", which is crazy but legal.
1045 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1046 case tok::semi:
1047 case tok::r_paren:
1048 case tok::r_square:
1049 case tok::r_brace:
1050 case tok::colon:
1051 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001052 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001053
Chris Lattner2a2819a2008-04-06 06:02:23 +00001054 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001055 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +00001056 if (Expr.isInvalid()) return move(Expr);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001057 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001058 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001059}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001060
1061/// ParseCXXThis - This handles the C++ 'this' pointer.
1062///
1063/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1064/// a non-lvalue expression whose value is the address of the object for which
1065/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001066ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001067 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1068 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001069 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001070}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001071
1072/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1073/// Can be interpreted either as function-style casting ("int(x)")
1074/// or class type construction ("ClassType(x,y,z)")
1075/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001076/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001077///
1078/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001079/// simple-type-specifier '(' expression-list[opt] ')'
1080/// [C++0x] simple-type-specifier braced-init-list
1081/// typename-specifier '(' expression-list[opt] ')'
1082/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001083///
John McCall60d7b3a2010-08-24 06:29:42 +00001084ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001085Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001086 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001087 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001088
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001089 assert((Tok.is(tok::l_paren) ||
1090 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1091 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001092
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001093 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001094
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001095 // FIXME: Convert to a proper type construct expression.
1096 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001097
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001098 } else {
1099 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1100
1101 SourceLocation LParenLoc = ConsumeParen();
1102
1103 ExprVector Exprs(Actions);
1104 CommaLocsTy CommaLocs;
1105
1106 if (Tok.isNot(tok::r_paren)) {
1107 if (ParseExpressionList(Exprs, CommaLocs)) {
1108 SkipUntil(tok::r_paren);
1109 return ExprError();
1110 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001111 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001112
1113 // Match the ')'.
1114 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1115
1116 // TypeRep could be null, if it references an invalid typedef.
1117 if (!TypeRep)
1118 return ExprError();
1119
1120 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1121 "Unexpected number of commas!");
1122 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
1123 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001124 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001125}
1126
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001127/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001128///
1129/// condition:
1130/// expression
1131/// type-specifier-seq declarator '=' assignment-expression
1132/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1133/// '=' assignment-expression
1134///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001135/// \param ExprResult if the condition was parsed as an expression, the
1136/// parsed expression.
1137///
1138/// \param DeclResult if the condition was parsed as a declaration, the
1139/// parsed declaration.
1140///
Douglas Gregor586596f2010-05-06 17:25:47 +00001141/// \param Loc The location of the start of the statement that requires this
1142/// condition, e.g., the "for" in a for loop.
1143///
1144/// \param ConvertToBoolean Whether the condition expression should be
1145/// converted to a boolean value.
1146///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001147/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001148bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1149 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001150 SourceLocation Loc,
1151 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001152 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001153 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001154 cutOffParsing();
1155 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001156 }
1157
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001158 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001159 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001160 ExprOut = ParseExpression(); // expression
1161 DeclOut = 0;
1162 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001163 return true;
1164
1165 // If required, convert to a boolean value.
1166 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001167 ExprOut
1168 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1169 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001170 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001171
1172 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001173 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001174 ParseSpecifierQualifierList(DS);
1175
1176 // declarator
1177 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1178 ParseDeclarator(DeclaratorInfo);
1179
1180 // simple-asm-expr[opt]
1181 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001182 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001183 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001184 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001185 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001186 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001187 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001188 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001189 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001190 }
1191
1192 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001193 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001194
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001195 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001196 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001197 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001198 DeclOut = Dcl.get();
1199 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001200
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001201 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001202 if (isTokenEqualOrMistypedEqualEqual(
1203 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001204 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001205 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001206 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +00001207 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1208 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001209 } else {
1210 // FIXME: C++0x allows a braced-init-list
1211 Diag(Tok, diag::err_expected_equal_after_declarator);
1212 }
1213
Douglas Gregor586596f2010-05-06 17:25:47 +00001214 // FIXME: Build a reference to this declaration? Convert it to bool?
1215 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001216
1217 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001218
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001219 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001220}
1221
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001222/// \brief Determine whether the current token starts a C++
1223/// simple-type-specifier.
1224bool Parser::isCXXSimpleTypeSpecifier() const {
1225 switch (Tok.getKind()) {
1226 case tok::annot_typename:
1227 case tok::kw_short:
1228 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001229 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001230 case tok::kw_signed:
1231 case tok::kw_unsigned:
1232 case tok::kw_void:
1233 case tok::kw_char:
1234 case tok::kw_int:
1235 case tok::kw_float:
1236 case tok::kw_double:
1237 case tok::kw_wchar_t:
1238 case tok::kw_char16_t:
1239 case tok::kw_char32_t:
1240 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +00001241 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001242 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +00001243 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001244 return true;
1245
1246 default:
1247 break;
1248 }
1249
1250 return false;
1251}
1252
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001253/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1254/// This should only be called when the current token is known to be part of
1255/// simple-type-specifier.
1256///
1257/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001258/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001259/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1260/// char
1261/// wchar_t
1262/// bool
1263/// short
1264/// int
1265/// long
1266/// signed
1267/// unsigned
1268/// float
1269/// double
1270/// void
1271/// [GNU] typeof-specifier
1272/// [C++0x] auto [TODO]
1273///
1274/// type-name:
1275/// class-name
1276/// enum-name
1277/// typedef-name
1278///
1279void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1280 DS.SetRangeStart(Tok.getLocation());
1281 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001282 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001283 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001285 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001286 case tok::identifier: // foo::bar
1287 case tok::coloncolon: // ::foo::bar
1288 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001289 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001290 assert(0 && "Not a simple-type-specifier token!");
1291 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +00001292
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001293 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001294 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001295 if (getTypeAnnotation(Tok))
1296 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1297 getTypeAnnotation(Tok));
1298 else
1299 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001300
1301 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1302 ConsumeToken();
1303
1304 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1305 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1306 // Objective-C interface. If we don't have Objective-C or a '<', this is
1307 // just a normal reference to a typedef name.
1308 if (Tok.is(tok::less) && getLang().ObjC1)
1309 ParseObjCProtocolQualifiers(DS);
1310
1311 DS.Finish(Diags, PP);
1312 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001313 }
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001315 // builtin types
1316 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001317 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001318 break;
1319 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001320 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001321 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001322 case tok::kw___int64:
1323 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1324 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001325 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001326 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001327 break;
1328 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001329 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001330 break;
1331 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001332 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001333 break;
1334 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001335 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001336 break;
1337 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001338 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001339 break;
1340 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001341 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001342 break;
1343 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001344 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001345 break;
1346 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001347 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001348 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001349 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001350 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001351 break;
1352 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001353 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001354 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001355 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001356 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001357 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001359 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001360 // GNU typeof support.
1361 case tok::kw_typeof:
1362 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001363 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001364 return;
1365 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001366 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001367 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1368 else
1369 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001370 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001371 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001372}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001373
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001374/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1375/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1376/// e.g., "const short int". Note that the DeclSpec is *not* finished
1377/// by parsing the type-specifier-seq, because these sequences are
1378/// typically followed by some form of declarator. Returns true and
1379/// emits diagnostics if this is not a type-specifier-seq, false
1380/// otherwise.
1381///
1382/// type-specifier-seq: [C++ 8.1]
1383/// type-specifier type-specifier-seq[opt]
1384///
1385bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1386 DS.SetRangeStart(Tok.getLocation());
1387 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001388 unsigned DiagID;
1389 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001390
1391 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001392 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1393 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001394 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001395 return true;
1396 }
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Sebastian Redld9bafa72010-02-03 21:21:43 +00001398 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1399 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1400 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001401
Douglas Gregor396a9f22010-02-24 23:13:13 +00001402 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001403 return false;
1404}
1405
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001406/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1407/// some form.
1408///
1409/// This routine is invoked when a '<' is encountered after an identifier or
1410/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1411/// whether the unqualified-id is actually a template-id. This routine will
1412/// then parse the template arguments and form the appropriate template-id to
1413/// return to the caller.
1414///
1415/// \param SS the nested-name-specifier that precedes this template-id, if
1416/// we're actually parsing a qualified-id.
1417///
1418/// \param Name for constructor and destructor names, this is the actual
1419/// identifier that may be a template-name.
1420///
1421/// \param NameLoc the location of the class-name in a constructor or
1422/// destructor.
1423///
1424/// \param EnteringContext whether we're entering the scope of the
1425/// nested-name-specifier.
1426///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001427/// \param ObjectType if this unqualified-id occurs within a member access
1428/// expression, the type of the base object whose member is being accessed.
1429///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001430/// \param Id as input, describes the template-name or operator-function-id
1431/// that precedes the '<'. If template arguments were parsed successfully,
1432/// will be updated with the template-id.
1433///
Douglas Gregord4dca082010-02-24 18:44:31 +00001434/// \param AssumeTemplateId When true, this routine will assume that the name
1435/// refers to a template without performing name lookup to verify.
1436///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001437/// \returns true if a parse error occurred, false otherwise.
1438bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1439 IdentifierInfo *Name,
1440 SourceLocation NameLoc,
1441 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001442 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001443 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001444 bool AssumeTemplateId,
1445 SourceLocation TemplateKWLoc) {
1446 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1447 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001448
1449 TemplateTy Template;
1450 TemplateNameKind TNK = TNK_Non_template;
1451 switch (Id.getKind()) {
1452 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001453 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001454 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001455 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001456 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001457 Id, ObjectType, EnteringContext,
1458 Template);
1459 if (TNK == TNK_Non_template)
1460 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001461 } else {
1462 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001463 TNK = Actions.isTemplateName(getCurScope(), SS,
1464 TemplateKWLoc.isValid(), Id,
1465 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001466 MemberOfUnknownSpecialization);
1467
1468 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1469 ObjectType && IsTemplateArgumentList()) {
1470 // We have something like t->getAs<T>(), where getAs is a
1471 // member of an unknown specialization. However, this will only
1472 // parse correctly as a template, so suggest the keyword 'template'
1473 // before 'getAs' and treat this as a dependent template name.
1474 std::string Name;
1475 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1476 Name = Id.Identifier->getName();
1477 else {
1478 Name = "operator ";
1479 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1480 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1481 else
1482 Name += Id.Identifier->getName();
1483 }
1484 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1485 << Name
1486 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001487 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001488 SS, Id, ObjectType,
1489 EnteringContext, Template);
1490 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001491 return true;
1492 }
1493 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001494 break;
1495
Douglas Gregor014e88d2009-11-03 23:16:33 +00001496 case UnqualifiedId::IK_ConstructorName: {
1497 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001498 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001499 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001500 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1501 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001502 EnteringContext, Template,
1503 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001504 break;
1505 }
1506
Douglas Gregor014e88d2009-11-03 23:16:33 +00001507 case UnqualifiedId::IK_DestructorName: {
1508 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001509 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001510 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001511 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001512 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001513 TemplateName, ObjectType,
1514 EnteringContext, Template);
1515 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001516 return true;
1517 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001518 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1519 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001520 EnteringContext, Template,
1521 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001522
John McCallb3d87482010-08-24 05:47:05 +00001523 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001524 Diag(NameLoc, diag::err_destructor_template_id)
1525 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001526 return true;
1527 }
1528 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001529 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001530 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001531
1532 default:
1533 return false;
1534 }
1535
1536 if (TNK == TNK_Non_template)
1537 return false;
1538
1539 // Parse the enclosed template argument list.
1540 SourceLocation LAngleLoc, RAngleLoc;
1541 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001542 if (Tok.is(tok::less) &&
1543 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001544 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001545 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001546 RAngleLoc))
1547 return true;
1548
1549 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001550 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1551 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001552 // Form a parsed representation of the template-id to be stored in the
1553 // UnqualifiedId.
1554 TemplateIdAnnotation *TemplateId
1555 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1556
1557 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1558 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001559 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001560 TemplateId->TemplateNameLoc = Id.StartLocation;
1561 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001562 TemplateId->Name = 0;
1563 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1564 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001565 }
1566
Douglas Gregor059101f2011-03-02 00:47:37 +00001567 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001568 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001569 TemplateId->Kind = TNK;
1570 TemplateId->LAngleLoc = LAngleLoc;
1571 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001572 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001573 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001574 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001575 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001576
1577 Id.setTemplateId(TemplateId);
1578 return false;
1579 }
1580
1581 // Bundle the template arguments together.
1582 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001583 TemplateArgs.size());
1584
1585 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001586 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001587 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001588 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001589 RAngleLoc);
1590 if (Type.isInvalid())
1591 return true;
1592
1593 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1594 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1595 else
1596 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1597
1598 return false;
1599}
1600
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001601/// \brief Parse an operator-function-id or conversion-function-id as part
1602/// of a C++ unqualified-id.
1603///
1604/// This routine is responsible only for parsing the operator-function-id or
1605/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001606///
1607/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001608/// operator-function-id: [C++ 13.5]
1609/// 'operator' operator
1610///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001611/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001612/// new delete new[] delete[]
1613/// + - * / % ^ & | ~
1614/// ! = < > += -= *= /= %=
1615/// ^= &= |= << >> >>= <<= == !=
1616/// <= >= && || ++ -- , ->* ->
1617/// () []
1618///
1619/// conversion-function-id: [C++ 12.3.2]
1620/// operator conversion-type-id
1621///
1622/// conversion-type-id:
1623/// type-specifier-seq conversion-declarator[opt]
1624///
1625/// conversion-declarator:
1626/// ptr-operator conversion-declarator[opt]
1627/// \endcode
1628///
1629/// \param The nested-name-specifier that preceded this unqualified-id. If
1630/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1631///
1632/// \param EnteringContext whether we are entering the scope of the
1633/// nested-name-specifier.
1634///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001635/// \param ObjectType if this unqualified-id occurs within a member access
1636/// expression, the type of the base object whose member is being accessed.
1637///
1638/// \param Result on a successful parse, contains the parsed unqualified-id.
1639///
1640/// \returns true if parsing fails, false otherwise.
1641bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001642 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001643 UnqualifiedId &Result) {
1644 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1645
1646 // Consume the 'operator' keyword.
1647 SourceLocation KeywordLoc = ConsumeToken();
1648
1649 // Determine what kind of operator name we have.
1650 unsigned SymbolIdx = 0;
1651 SourceLocation SymbolLocations[3];
1652 OverloadedOperatorKind Op = OO_None;
1653 switch (Tok.getKind()) {
1654 case tok::kw_new:
1655 case tok::kw_delete: {
1656 bool isNew = Tok.getKind() == tok::kw_new;
1657 // Consume the 'new' or 'delete'.
1658 SymbolLocations[SymbolIdx++] = ConsumeToken();
1659 if (Tok.is(tok::l_square)) {
1660 // Consume the '['.
1661 SourceLocation LBracketLoc = ConsumeBracket();
1662 // Consume the ']'.
1663 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1664 LBracketLoc);
1665 if (RBracketLoc.isInvalid())
1666 return true;
1667
1668 SymbolLocations[SymbolIdx++] = LBracketLoc;
1669 SymbolLocations[SymbolIdx++] = RBracketLoc;
1670 Op = isNew? OO_Array_New : OO_Array_Delete;
1671 } else {
1672 Op = isNew? OO_New : OO_Delete;
1673 }
1674 break;
1675 }
1676
1677#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1678 case tok::Token: \
1679 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1680 Op = OO_##Name; \
1681 break;
1682#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1683#include "clang/Basic/OperatorKinds.def"
1684
1685 case tok::l_paren: {
1686 // Consume the '('.
1687 SourceLocation LParenLoc = ConsumeParen();
1688 // Consume the ')'.
1689 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1690 LParenLoc);
1691 if (RParenLoc.isInvalid())
1692 return true;
1693
1694 SymbolLocations[SymbolIdx++] = LParenLoc;
1695 SymbolLocations[SymbolIdx++] = RParenLoc;
1696 Op = OO_Call;
1697 break;
1698 }
1699
1700 case tok::l_square: {
1701 // Consume the '['.
1702 SourceLocation LBracketLoc = ConsumeBracket();
1703 // Consume the ']'.
1704 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1705 LBracketLoc);
1706 if (RBracketLoc.isInvalid())
1707 return true;
1708
1709 SymbolLocations[SymbolIdx++] = LBracketLoc;
1710 SymbolLocations[SymbolIdx++] = RBracketLoc;
1711 Op = OO_Subscript;
1712 break;
1713 }
1714
1715 case tok::code_completion: {
1716 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001717 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001718 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001719 // Don't try to parse any further.
1720 return true;
1721 }
1722
1723 default:
1724 break;
1725 }
1726
1727 if (Op != OO_None) {
1728 // We have parsed an operator-function-id.
1729 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1730 return false;
1731 }
Sean Hunt0486d742009-11-28 04:44:28 +00001732
1733 // Parse a literal-operator-id.
1734 //
1735 // literal-operator-id: [C++0x 13.5.8]
1736 // operator "" identifier
1737
1738 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1739 if (Tok.getLength() != 2)
1740 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1741 ConsumeStringToken();
1742
1743 if (Tok.isNot(tok::identifier)) {
1744 Diag(Tok.getLocation(), diag::err_expected_ident);
1745 return true;
1746 }
1747
1748 IdentifierInfo *II = Tok.getIdentifierInfo();
1749 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001750 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001751 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001752
1753 // Parse a conversion-function-id.
1754 //
1755 // conversion-function-id: [C++ 12.3.2]
1756 // operator conversion-type-id
1757 //
1758 // conversion-type-id:
1759 // type-specifier-seq conversion-declarator[opt]
1760 //
1761 // conversion-declarator:
1762 // ptr-operator conversion-declarator[opt]
1763
1764 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001765 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001766 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001767 return true;
1768
1769 // Parse the conversion-declarator, which is merely a sequence of
1770 // ptr-operators.
1771 Declarator D(DS, Declarator::TypeNameContext);
1772 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1773
1774 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001775 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001776 if (Ty.isInvalid())
1777 return true;
1778
1779 // Note that this is a conversion-function-id.
1780 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1781 D.getSourceRange().getEnd());
1782 return false;
1783}
1784
1785/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1786/// name of an entity.
1787///
1788/// \code
1789/// unqualified-id: [C++ expr.prim.general]
1790/// identifier
1791/// operator-function-id
1792/// conversion-function-id
1793/// [C++0x] literal-operator-id [TODO]
1794/// ~ class-name
1795/// template-id
1796///
1797/// \endcode
1798///
1799/// \param The nested-name-specifier that preceded this unqualified-id. If
1800/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1801///
1802/// \param EnteringContext whether we are entering the scope of the
1803/// nested-name-specifier.
1804///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001805/// \param AllowDestructorName whether we allow parsing of a destructor name.
1806///
1807/// \param AllowConstructorName whether we allow parsing a constructor name.
1808///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001809/// \param ObjectType if this unqualified-id occurs within a member access
1810/// expression, the type of the base object whose member is being accessed.
1811///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001812/// \param Result on a successful parse, contains the parsed unqualified-id.
1813///
1814/// \returns true if parsing fails, false otherwise.
1815bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1816 bool AllowDestructorName,
1817 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001818 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001819 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001820
1821 // Handle 'A::template B'. This is for template-ids which have not
1822 // already been annotated by ParseOptionalCXXScopeSpecifier().
1823 bool TemplateSpecified = false;
1824 SourceLocation TemplateKWLoc;
1825 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1826 (ObjectType || SS.isSet())) {
1827 TemplateSpecified = true;
1828 TemplateKWLoc = ConsumeToken();
1829 }
1830
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001831 // unqualified-id:
1832 // identifier
1833 // template-id (when it hasn't already been annotated)
1834 if (Tok.is(tok::identifier)) {
1835 // Consume the identifier.
1836 IdentifierInfo *Id = Tok.getIdentifierInfo();
1837 SourceLocation IdLoc = ConsumeToken();
1838
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001839 if (!getLang().CPlusPlus) {
1840 // If we're not in C++, only identifiers matter. Record the
1841 // identifier and return.
1842 Result.setIdentifier(Id, IdLoc);
1843 return false;
1844 }
1845
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001846 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001847 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001848 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001849 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001850 &SS, false, false,
1851 ParsedType(),
1852 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001853 IdLoc, IdLoc);
1854 } else {
1855 // We have parsed an identifier.
1856 Result.setIdentifier(Id, IdLoc);
1857 }
1858
1859 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001860 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001861 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001862 ObjectType, Result,
1863 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001864
1865 return false;
1866 }
1867
1868 // unqualified-id:
1869 // template-id (already parsed and annotated)
1870 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001871 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001872
1873 // If the template-name names the current class, then this is a constructor
1874 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001875 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001876 if (SS.isSet()) {
1877 // C++ [class.qual]p2 specifies that a qualified template-name
1878 // is taken as the constructor name where a constructor can be
1879 // declared. Thus, the template arguments are extraneous, so
1880 // complain about them and remove them entirely.
1881 Diag(TemplateId->TemplateNameLoc,
1882 diag::err_out_of_line_constructor_template_id)
1883 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001884 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001885 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1886 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1887 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001888 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001889 &SS, false, false,
1890 ParsedType(),
1891 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001892 TemplateId->TemplateNameLoc,
1893 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001894 ConsumeToken();
1895 return false;
1896 }
1897
1898 Result.setConstructorTemplateId(TemplateId);
1899 ConsumeToken();
1900 return false;
1901 }
1902
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001903 // We have already parsed a template-id; consume the annotation token as
1904 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001905 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001906 ConsumeToken();
1907 return false;
1908 }
1909
1910 // unqualified-id:
1911 // operator-function-id
1912 // conversion-function-id
1913 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001914 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001915 return true;
1916
Sean Hunte6252d12009-11-28 08:58:14 +00001917 // If we have an operator-function-id or a literal-operator-id and the next
1918 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001919 //
1920 // template-id:
1921 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001922 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1923 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001924 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001925 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1926 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001927 Result,
1928 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001929
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001930 return false;
1931 }
1932
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001933 if (getLang().CPlusPlus &&
1934 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001935 // C++ [expr.unary.op]p10:
1936 // There is an ambiguity in the unary-expression ~X(), where X is a
1937 // class-name. The ambiguity is resolved in favor of treating ~ as a
1938 // unary complement rather than treating ~X as referring to a destructor.
1939
1940 // Parse the '~'.
1941 SourceLocation TildeLoc = ConsumeToken();
1942
1943 // Parse the class-name.
1944 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001945 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001946 return true;
1947 }
1948
1949 // Parse the class-name (or template-name in a simple-template-id).
1950 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1951 SourceLocation ClassNameLoc = ConsumeToken();
1952
Douglas Gregor0278e122010-05-05 05:58:24 +00001953 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001954 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001955 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001956 EnteringContext, ObjectType, Result,
1957 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001958 }
1959
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001960 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001961 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1962 ClassNameLoc, getCurScope(),
1963 SS, ObjectType,
1964 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001965 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001966 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001967
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001968 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001969 return false;
1970 }
1971
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001972 Diag(Tok, diag::err_expected_unqualified_id)
1973 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001974 return true;
1975}
1976
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001977/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1978/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001979///
Chris Lattner59232d32009-01-04 21:25:24 +00001980/// This method is called to parse the new expression after the optional :: has
1981/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1982/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001983///
1984/// new-expression:
1985/// '::'[opt] 'new' new-placement[opt] new-type-id
1986/// new-initializer[opt]
1987/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1988/// new-initializer[opt]
1989///
1990/// new-placement:
1991/// '(' expression-list ')'
1992///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001993/// new-type-id:
1994/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001995/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001996///
1997/// new-declarator:
1998/// ptr-operator new-declarator[opt]
1999/// direct-new-declarator
2000///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002001/// new-initializer:
2002/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002003/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002004///
John McCall60d7b3a2010-08-24 06:29:42 +00002005ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002006Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2007 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2008 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002009
2010 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2011 // second form of new-expression. It can't be a new-type-id.
2012
Sebastian Redla55e52c2008-11-25 22:21:31 +00002013 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002014 SourceLocation PlacementLParen, PlacementRParen;
2015
Douglas Gregor4bd40312010-07-13 15:54:32 +00002016 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002017 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002018 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002019 if (Tok.is(tok::l_paren)) {
2020 // If it turns out to be a placement, we change the type location.
2021 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002022 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2023 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002024 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002025 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002026
2027 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002028 if (PlacementRParen.isInvalid()) {
2029 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002030 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002031 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002032
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002033 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002034 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00002035 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002036 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002037 } else {
2038 // We still need the type.
2039 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00002040 TypeIdParens.setBegin(ConsumeParen());
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002041 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002042 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002043 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002044 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00002045 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
2046 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002047 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002048 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002049 if (ParseCXXTypeSpecifierSeq(DS))
2050 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002051 else {
2052 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002053 ParseDeclaratorInternal(DeclaratorInfo,
2054 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002055 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002056 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002057 }
2058 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002059 // A new-type-id is a simplified type-id, where essentially the
2060 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002061 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002062 if (ParseCXXTypeSpecifierSeq(DS))
2063 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002064 else {
2065 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002066 ParseDeclaratorInternal(DeclaratorInfo,
2067 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002068 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002069 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002070 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002071 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002072 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002073 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002074
Sebastian Redla55e52c2008-11-25 22:21:31 +00002075 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002076 SourceLocation ConstructorLParen, ConstructorRParen;
2077
2078 if (Tok.is(tok::l_paren)) {
2079 ConstructorLParen = ConsumeParen();
2080 if (Tok.isNot(tok::r_paren)) {
2081 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002082 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2083 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002084 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002085 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002086 }
2087 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002088 if (ConstructorRParen.isInvalid()) {
2089 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002090 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002091 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002092 } else if (Tok.is(tok::l_brace)) {
2093 // FIXME: Have to communicate the init-list to ActOnCXXNew.
2094 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002095 }
2096
Sebastian Redlf53597f2009-03-15 17:47:39 +00002097 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2098 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00002099 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002100 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002101}
2102
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002103/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2104/// passed to ParseDeclaratorInternal.
2105///
2106/// direct-new-declarator:
2107/// '[' expression ']'
2108/// direct-new-declarator '[' constant-expression ']'
2109///
Chris Lattner59232d32009-01-04 21:25:24 +00002110void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002111 // Parse the array dimensions.
2112 bool first = true;
2113 while (Tok.is(tok::l_square)) {
2114 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00002115 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002116 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002117 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002118 // Recover
2119 SkipUntil(tok::r_square);
2120 return;
2121 }
2122 first = false;
2123
Sebastian Redlab197ba2009-02-09 18:23:29 +00002124 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall0b7e6782011-03-24 11:26:52 +00002125
2126 ParsedAttributes attrs(AttrFactory);
2127 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002128 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002129 Size.release(), LLoc, RLoc),
John McCall0b7e6782011-03-24 11:26:52 +00002130 attrs, RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002131
Sebastian Redlab197ba2009-02-09 18:23:29 +00002132 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002133 return;
2134 }
2135}
2136
2137/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2138/// This ambiguity appears in the syntax of the C++ new operator.
2139///
2140/// new-expression:
2141/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2142/// new-initializer[opt]
2143///
2144/// new-placement:
2145/// '(' expression-list ')'
2146///
John McCallca0408f2010-08-23 06:44:23 +00002147bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002148 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002149 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002150 // The '(' was already consumed.
2151 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002152 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002153 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002154 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002155 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002156 }
2157
2158 // It's not a type, it has to be an expression list.
2159 // Discard the comma locations - ActOnCXXNew has enough parameters.
2160 CommaLocsTy CommaLocs;
2161 return ParseExpressionList(PlacementArgs, CommaLocs);
2162}
2163
2164/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2165/// to free memory allocated by new.
2166///
Chris Lattner59232d32009-01-04 21:25:24 +00002167/// This method is called to parse the 'delete' expression after the optional
2168/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2169/// and "Start" is its location. Otherwise, "Start" is the location of the
2170/// 'delete' token.
2171///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002172/// delete-expression:
2173/// '::'[opt] 'delete' cast-expression
2174/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002175ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002176Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2177 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2178 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002179
2180 // Array delete?
2181 bool ArrayDelete = false;
2182 if (Tok.is(tok::l_square)) {
2183 ArrayDelete = true;
2184 SourceLocation LHS = ConsumeBracket();
2185 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
2186 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002187 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002188 }
2189
John McCall60d7b3a2010-08-24 06:29:42 +00002190 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002191 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002192 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002193
John McCall9ae2f072010-08-23 23:25:46 +00002194 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002195}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002196
Mike Stump1eb44332009-09-09 15:08:12 +00002197static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002198 switch(kind) {
John Wiegley20c0da72011-04-27 23:09:49 +00002199 default: assert(false && "Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002200 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002201 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002202 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002203 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002204 case tok::kw___has_trivial_constructor:
2205 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002206 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002207 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2208 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2209 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002210 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2211 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002212 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002213 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2214 case tok::kw___is_compound: return UTT_IsCompound;
2215 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002216 case tok::kw___is_empty: return UTT_IsEmpty;
2217 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley20c0da72011-04-27 23:09:49 +00002218 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2219 case tok::kw___is_function: return UTT_IsFunction;
2220 case tok::kw___is_fundamental: return UTT_IsFundamental;
2221 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00002222 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2223 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2224 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2225 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2226 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002227 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002228 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002229 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002230 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002231 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002232 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002233 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2234 case tok::kw___is_scalar: return UTT_IsScalar;
2235 case tok::kw___is_signed: return UTT_IsSigned;
2236 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2237 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002238 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002239 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002240 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2241 case tok::kw___is_void: return UTT_IsVoid;
2242 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002243 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002244}
2245
2246static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2247 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002248 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002249 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002250 case tok::kw___is_convertible: return BTT_IsConvertible;
2251 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002252 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002253 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002254 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002255}
2256
John Wiegley21ff2e52011-04-28 00:16:57 +00002257static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2258 switch(kind) {
2259 default: llvm_unreachable("Not a known binary type trait");
2260 case tok::kw___array_rank: return ATT_ArrayRank;
2261 case tok::kw___array_extent: return ATT_ArrayExtent;
2262 }
2263}
2264
John Wiegley55262202011-04-25 06:54:41 +00002265static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2266 switch(kind) {
2267 default: assert(false && "Not a known unary expression trait.");
2268 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2269 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2270 }
2271}
2272
Sebastian Redl64b45f72009-01-05 20:52:13 +00002273/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2274/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2275/// templates.
2276///
2277/// primary-expression:
2278/// [GNU] unary-type-trait '(' type-id ')'
2279///
John McCall60d7b3a2010-08-24 06:29:42 +00002280ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002281 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2282 SourceLocation Loc = ConsumeToken();
2283
2284 SourceLocation LParen = Tok.getLocation();
2285 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2286 return ExprError();
2287
2288 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2289 // there will be cryptic errors about mismatched parentheses and missing
2290 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002291 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002292
2293 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2294
Douglas Gregor809070a2009-02-18 17:45:20 +00002295 if (Ty.isInvalid())
2296 return ExprError();
2297
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002298 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00002299}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002300
Francois Pichet6ad6f282010-12-07 00:08:36 +00002301/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2302/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2303/// templates.
2304///
2305/// primary-expression:
2306/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2307///
2308ExprResult Parser::ParseBinaryTypeTrait() {
2309 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2310 SourceLocation Loc = ConsumeToken();
2311
2312 SourceLocation LParen = Tok.getLocation();
2313 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2314 return ExprError();
2315
2316 TypeResult LhsTy = ParseTypeName();
2317 if (LhsTy.isInvalid()) {
2318 SkipUntil(tok::r_paren);
2319 return ExprError();
2320 }
2321
2322 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2323 SkipUntil(tok::r_paren);
2324 return ExprError();
2325 }
2326
2327 TypeResult RhsTy = ParseTypeName();
2328 if (RhsTy.isInvalid()) {
2329 SkipUntil(tok::r_paren);
2330 return ExprError();
2331 }
2332
2333 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2334
2335 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
2336}
2337
John Wiegley21ff2e52011-04-28 00:16:57 +00002338/// ParseArrayTypeTrait - Parse the built-in array type-trait
2339/// pseudo-functions.
2340///
2341/// primary-expression:
2342/// [Embarcadero] '__array_rank' '(' type-id ')'
2343/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2344///
2345ExprResult Parser::ParseArrayTypeTrait() {
2346 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2347 SourceLocation Loc = ConsumeToken();
2348
2349 SourceLocation LParen = Tok.getLocation();
2350 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2351 return ExprError();
2352
2353 TypeResult Ty = ParseTypeName();
2354 if (Ty.isInvalid()) {
2355 SkipUntil(tok::comma);
2356 SkipUntil(tok::r_paren);
2357 return ExprError();
2358 }
2359
2360 switch (ATT) {
2361 case ATT_ArrayRank: {
2362 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2363 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, RParen);
2364 }
2365 case ATT_ArrayExtent: {
2366 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2367 SkipUntil(tok::r_paren);
2368 return ExprError();
2369 }
2370
2371 ExprResult DimExpr = ParseExpression();
2372 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2373
2374 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), RParen);
2375 }
2376 default:
2377 break;
2378 }
2379 return ExprError();
2380}
2381
John Wiegley55262202011-04-25 06:54:41 +00002382/// ParseExpressionTrait - Parse built-in expression-trait
2383/// pseudo-functions like __is_lvalue_expr( xxx ).
2384///
2385/// primary-expression:
2386/// [Embarcadero] expression-trait '(' expression ')'
2387///
2388ExprResult Parser::ParseExpressionTrait() {
2389 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2390 SourceLocation Loc = ConsumeToken();
2391
2392 SourceLocation LParen = Tok.getLocation();
2393 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2394 return ExprError();
2395
2396 ExprResult Expr = ParseExpression();
2397
2398 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2399
2400 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), RParen);
2401}
2402
2403
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002404/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2405/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2406/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002407ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002408Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002409 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002410 SourceLocation LParenLoc,
2411 SourceLocation &RParenLoc) {
2412 assert(getLang().CPlusPlus && "Should only be called for C++!");
2413 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2414 assert(isTypeIdInParens() && "Not a type-id!");
2415
John McCall60d7b3a2010-08-24 06:29:42 +00002416 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002417 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002418
2419 // We need to disambiguate a very ugly part of the C++ syntax:
2420 //
2421 // (T())x; - type-id
2422 // (T())*x; - type-id
2423 // (T())/x; - expression
2424 // (T()); - expression
2425 //
2426 // The bad news is that we cannot use the specialized tentative parser, since
2427 // it can only verify that the thing inside the parens can be parsed as
2428 // type-id, it is not useful for determining the context past the parens.
2429 //
2430 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002431 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002432 //
2433 // It uses a scheme similar to parsing inline methods. The parenthesized
2434 // tokens are cached, the context that follows is determined (possibly by
2435 // parsing a cast-expression), and then we re-introduce the cached tokens
2436 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002437
Mike Stump1eb44332009-09-09 15:08:12 +00002438 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002439 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002440
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002441 // Store the tokens of the parentheses. We will parse them after we determine
2442 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002443 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002444 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002445 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2446 return ExprError();
2447 }
2448
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002449 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002450 ParseAs = CompoundLiteral;
2451 } else {
2452 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002453 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2454 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2455 NotCastExpr = true;
2456 } else {
2457 // Try parsing the cast-expression that may follow.
2458 // If it is not a cast-expression, NotCastExpr will be true and no token
2459 // will be consumed.
2460 Result = ParseCastExpression(false/*isUnaryExpression*/,
2461 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002462 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002463 // type-id has priority.
2464 true/*isTypeCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002465 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002466
2467 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2468 // an expression.
2469 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002470 }
2471
Mike Stump1eb44332009-09-09 15:08:12 +00002472 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002473 Toks.push_back(Tok);
2474 // Re-enter the stored parenthesized tokens into the token stream, so we may
2475 // parse them now.
2476 PP.EnterTokenStream(Toks.data(), Toks.size(),
2477 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2478 // Drop the current token and bring the first cached one. It's the same token
2479 // as when we entered this function.
2480 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002481
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002482 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002483 // Parse the type declarator.
2484 DeclSpec DS(AttrFactory);
2485 ParseSpecifierQualifierList(DS);
2486 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2487 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002488
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002489 // Match the ')'.
2490 if (Tok.is(tok::r_paren))
2491 RParenLoc = ConsumeParen();
2492 else
2493 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002494
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002495 if (ParseAs == CompoundLiteral) {
2496 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002497 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002498 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2499 }
Mike Stump1eb44332009-09-09 15:08:12 +00002500
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002501 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2502 assert(ParseAs == CastExpr);
2503
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002504 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002505 return ExprError();
2506
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002507 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002508 if (!Result.isInvalid())
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002509 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc,
2510 DeclaratorInfo, CastTy,
2511 RParenLoc, Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002512 return move(Result);
2513 }
Mike Stump1eb44332009-09-09 15:08:12 +00002514
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002515 // Not a compound literal, and not followed by a cast-expression.
2516 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002517
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002518 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002519 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002520 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002521 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002522
2523 // Match the ')'.
2524 if (Result.isInvalid()) {
2525 SkipUntil(tok::r_paren);
2526 return ExprError();
2527 }
Mike Stump1eb44332009-09-09 15:08:12 +00002528
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002529 if (Tok.is(tok::r_paren))
2530 RParenLoc = ConsumeParen();
2531 else
2532 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2533
2534 return move(Result);
2535}