blob: fef52a567f61b7538f040350022520ce28fcf4ab [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner29375652006-12-04 18:06:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner29375652006-12-04 18:06:35 +000015#include "clang/Parse/Parser.h"
Douglas Gregor94a32472011-01-11 00:33:19 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
Douglas Gregordb0b9f12011-08-04 15:30:47 +000018#include "clang/Sema/Scope.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor7861a802009-11-03 01:35:08 +000020#include "llvm/Support/ErrorHandling.h"
21
Chris Lattner29375652006-12-04 18:06:35 +000022using namespace clang;
23
Richard Smith55858492011-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:
David Blaikie83d382b2011-09-23 05:06:16 +000032 llvm_unreachable("Unknown type for digraph error message.");
Richard Smith55858492011-04-14 21:45:45 +000033 }
34}
35
36// Are the two tokens adjacent in the same source file?
37static bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
38 SourceManager &SM = PP.getSourceManager();
39 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000040 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smith55858492011-04-14 21:45:45 +000041 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
42}
43
44// Suggest fixit for "<::" after a cast.
45static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
46 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
47 // Pull '<:' and ':' off token stream.
48 if (!AtDigraph)
49 PP.Lex(DigraphToken);
50 PP.Lex(ColonToken);
51
52 SourceRange Range;
53 Range.setBegin(DigraphToken.getLocation());
54 Range.setEnd(ColonToken.getLocation());
55 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
56 << SelectDigraphErrorMessage(Kind)
57 << FixItHint::CreateReplacement(Range, "< ::");
58
59 // Update token information to reflect their change in token type.
60 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000061 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smith55858492011-04-14 21:45:45 +000062 ColonToken.setLength(2);
63 DigraphToken.setKind(tok::less);
64 DigraphToken.setLength(1);
65
66 // Push new tokens back to token stream.
67 PP.EnterToken(ColonToken);
68 if (!AtDigraph)
69 PP.EnterToken(DigraphToken);
70}
71
Richard Trieu01fc0012011-09-19 19:01:00 +000072// Check for '<::' which should be '< ::' instead of '[:' when following
73// a template name.
74void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
75 bool EnteringContext,
76 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieu02e25db2011-09-20 20:03:50 +000077 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu01fc0012011-09-19 19:01:00 +000078 return;
79
80 Token SecondToken = GetLookAheadToken(2);
81 if (!SecondToken.is(tok::colon) || !AreTokensAdjacent(PP, Next, SecondToken))
82 return;
83
84 TemplateTy Template;
85 UnqualifiedId TemplateName;
86 TemplateName.setIdentifier(&II, Tok.getLocation());
87 bool MemberOfUnknownSpecialization;
88 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
89 TemplateName, ObjectType, EnteringContext,
90 Template, MemberOfUnknownSpecialization))
91 return;
92
93 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
94 /*AtDigraph*/false);
95}
96
Mike Stump11289f42009-09-09 15:08:12 +000097/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +000098///
99/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000100/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000101/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000102///
103/// '::'[opt] nested-name-specifier
104/// '::'
105///
106/// nested-name-specifier:
107/// type-name '::'
108/// namespace-name '::'
109/// nested-name-specifier identifier '::'
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000110/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000111///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000112///
Mike Stump11289f42009-09-09 15:08:12 +0000113/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000114/// nested-name-specifier (or empty)
115///
Mike Stump11289f42009-09-09 15:08:12 +0000116/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000117/// the "." or "->" of a member access expression, this parameter provides the
118/// type of the object whose members are being accessed.
119///
120/// \param EnteringContext whether we will be entering into the context of
121/// the nested-name-specifier after parsing it.
122///
Douglas Gregore610ada2010-02-24 18:44:31 +0000123/// \param MayBePseudoDestructor When non-NULL, points to a flag that
124/// indicates whether this nested-name-specifier may be part of a
125/// pseudo-destructor name. In this case, the flag will be set false
126/// if we don't actually end up parsing a destructor name. Moreorover,
127/// if we do end up determining that we are parsing a destructor name,
128/// the last component of the nested-name-specifier is not parsed as
129/// part of the scope specifier.
130
Douglas Gregor90d554e2010-02-21 18:36:56 +0000131/// member access expression, e.g., the \p T:: in \p p->T::m.
132///
John McCall1f476a12010-02-26 08:45:28 +0000133/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000134bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000135 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000136 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000137 bool *MayBePseudoDestructor,
138 bool IsTypename) {
Argyrios Kyrtzidisace521a2008-11-26 21:41:52 +0000139 assert(getLang().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000140 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000141
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000142 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor869ad452011-02-24 17:54:50 +0000143 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
144 Tok.getAnnotationRange(),
145 SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000146 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +0000147 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000148 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000149
Douglas Gregor7f741122009-02-25 19:37:18 +0000150 bool HasScopeSpecifier = false;
151
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000152 if (Tok.is(tok::coloncolon)) {
153 // ::new and ::delete aren't nested-name-specifiers.
154 tok::TokenKind NextKind = NextToken().getKind();
155 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
156 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000157
Chris Lattner45ddec32009-01-05 00:13:00 +0000158 // '::' - Global scope qualifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000159 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
160 return true;
161
Douglas Gregor7f741122009-02-25 19:37:18 +0000162 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000163 }
164
Douglas Gregore610ada2010-02-24 18:44:31 +0000165 bool CheckForDestructor = false;
166 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
167 CheckForDestructor = true;
168 *MayBePseudoDestructor = false;
169 }
170
Douglas Gregor7f741122009-02-25 19:37:18 +0000171 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000172 if (HasScopeSpecifier) {
173 // C++ [basic.lookup.classref]p5:
174 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000175 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000176 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000177 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000178 // the class-name-or-namespace-name is looked up in global scope as a
179 // class-name or namespace-name.
180 //
181 // To implement this, we clear out the object type as soon as we've
182 // seen a leading '::' or part of a nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000183 ObjectType = ParsedType();
Douglas Gregor2436e712009-09-17 21:32:03 +0000184
185 if (Tok.is(tok::code_completion)) {
186 // Code completion for a nested-name-specifier, where the code
187 // code completion token follows the '::'.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000188 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidis7d94c922011-04-23 01:04:12 +0000189 // Include code completion token into the range of the scope otherwise
190 // when we try to annotate the scope tokens the dangling code completion
191 // token will cause assertion in
192 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000193 SS.setEndLoc(Tok.getLocation());
194 cutOffParsing();
195 return true;
Douglas Gregor2436e712009-09-17 21:32:03 +0000196 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000197 }
Mike Stump11289f42009-09-09 15:08:12 +0000198
Douglas Gregor7f741122009-02-25 19:37:18 +0000199 // nested-name-specifier:
Chris Lattner0eed3a62009-06-26 03:47:46 +0000200 // nested-name-specifier 'template'[opt] simple-template-id '::'
201
202 // Parse the optional 'template' keyword, then make sure we have
203 // 'identifier <' after it.
204 if (Tok.is(tok::kw_template)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000205 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000206 // nested-name-specifier, since they aren't allowed to start with
207 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000208 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000209 break;
210
Douglas Gregor120635b2009-11-11 16:39:34 +0000211 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000212 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000213
214 UnqualifiedId TemplateName;
215 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000216 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000217 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000218 ConsumeToken();
219 } else if (Tok.is(tok::kw_operator)) {
220 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000221 TemplateName)) {
222 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000223 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000224 }
Douglas Gregor71395fa2009-11-04 00:56:37 +0000225
Alexis Hunted0530f2009-11-28 08:58:14 +0000226 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
227 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000228 Diag(TemplateName.getSourceRange().getBegin(),
229 diag::err_id_after_template_in_nested_name_spec)
230 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000231 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000232 break;
233 }
234 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000235 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000236 break;
237 }
Mike Stump11289f42009-09-09 15:08:12 +0000238
Douglas Gregor120635b2009-11-11 16:39:34 +0000239 // If the next token is not '<', we have a qualified-id that refers
240 // to a template name, such as T::template apply, but is not a
241 // template-id.
242 if (Tok.isNot(tok::less)) {
243 TPA.Revert();
244 break;
245 }
246
247 // Commit to parsing the template-id.
248 TPA.Commit();
Douglas Gregorbb119652010-06-16 23:00:59 +0000249 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000250 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000251 TemplateKWLoc,
252 SS,
253 TemplateName,
254 ObjectType,
255 EnteringContext,
256 Template)) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000257 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorbb119652010-06-16 23:00:59 +0000258 TemplateKWLoc, false))
259 return true;
260 } else
John McCall1f476a12010-02-26 08:45:28 +0000261 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000262
Chris Lattner0eed3a62009-06-26 03:47:46 +0000263 continue;
264 }
Mike Stump11289f42009-09-09 15:08:12 +0000265
Douglas Gregor7f741122009-02-25 19:37:18 +0000266 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000267 // We have
Douglas Gregor7f741122009-02-25 19:37:18 +0000268 //
269 // simple-template-id '::'
270 //
271 // So we need to check whether the simple-template-id is of the
Douglas Gregorb67535d2009-03-31 00:43:58 +0000272 // right kind (it should name a type or be dependent), and then
273 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000274 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000275 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
276 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000277 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000278 }
279
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000280 // Consume the template-id token.
281 ConsumeToken();
282
283 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
284 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000285
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000286 if (!HasScopeSpecifier)
287 HasScopeSpecifier = true;
288
289 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
290 TemplateId->getTemplateArgs(),
291 TemplateId->NumArgs);
292
293 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
294 /*FIXME:*/SourceLocation(),
295 SS,
296 TemplateId->Template,
297 TemplateId->TemplateNameLoc,
298 TemplateId->LAngleLoc,
299 TemplateArgsPtr,
300 TemplateId->RAngleLoc,
301 CCLoc,
302 EnteringContext)) {
303 SourceLocation StartLoc
304 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
305 : TemplateId->TemplateNameLoc;
306 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner704edfb2009-06-26 03:45:46 +0000307 }
Argyrios Kyrtzidis13935672011-05-03 18:45:38 +0000308
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000309 continue;
Douglas Gregor7f741122009-02-25 19:37:18 +0000310 }
311
Chris Lattnere2355f72009-06-26 03:52:38 +0000312
313 // The rest of the nested-name-specifier possibilities start with
314 // tok::identifier.
315 if (Tok.isNot(tok::identifier))
316 break;
317
318 IdentifierInfo &II = *Tok.getIdentifierInfo();
319
320 // nested-name-specifier:
321 // type-name '::'
322 // namespace-name '::'
323 // nested-name-specifier identifier '::'
324 Token Next = NextToken();
Chris Lattner1c428032009-12-07 01:36:53 +0000325
326 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
327 // and emit a fixit hint for it.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000328 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000329 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
330 Tok.getLocation(),
331 Next.getLocation(), ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000332 EnteringContext) &&
333 // If the token after the colon isn't an identifier, it's still an
334 // error, but they probably meant something else strange so don't
335 // recover like this.
336 PP.LookAhead(1).is(tok::identifier)) {
337 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregora771f462010-03-31 17:46:05 +0000338 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000339
340 // Recover as if the user wrote '::'.
341 Next.setKind(tok::coloncolon);
342 }
Chris Lattner1c428032009-12-07 01:36:53 +0000343 }
344
Chris Lattnere2355f72009-06-26 03:52:38 +0000345 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000346 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000347 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000348 II, ObjectType)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000349 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000350 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000351 }
352
Chris Lattnere2355f72009-06-26 03:52:38 +0000353 // We have an identifier followed by a '::'. Lookup this name
354 // as the name in a nested-name-specifier.
355 SourceLocation IdLoc = ConsumeToken();
Chris Lattner1c428032009-12-07 01:36:53 +0000356 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
357 "NextToken() not working properly!");
Chris Lattnere2355f72009-06-26 03:52:38 +0000358 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000359
Douglas Gregor90c99722011-02-24 00:17:56 +0000360 HasScopeSpecifier = true;
361 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
362 ObjectType, EnteringContext, SS))
363 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
364
Chris Lattnere2355f72009-06-26 03:52:38 +0000365 continue;
366 }
Mike Stump11289f42009-09-09 15:08:12 +0000367
Richard Trieu01fc0012011-09-19 19:01:00 +0000368 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smith55858492011-04-14 21:45:45 +0000369
Chris Lattnere2355f72009-06-26 03:52:38 +0000370 // nested-name-specifier:
371 // type-name '<'
372 if (Next.is(tok::less)) {
373 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000374 UnqualifiedId TemplateName;
375 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000376 bool MemberOfUnknownSpecialization;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000377 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000378 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000379 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000380 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000381 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000382 Template,
383 MemberOfUnknownSpecialization)) {
Chris Lattnere2355f72009-06-26 03:52:38 +0000384 // We have found a template name, so annotate this this token
385 // with a template-id annotation. We do not permit the
386 // template-id to be translated into a type annotation,
387 // because some clients (e.g., the parsing of class template
388 // specializations) still want to see the original template-id
389 // token.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000390 ConsumeToken();
Douglas Gregore7c20652011-03-02 00:47:37 +0000391 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregor71395fa2009-11-04 00:56:37 +0000392 SourceLocation(), false))
John McCall1f476a12010-02-26 08:45:28 +0000393 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000394 continue;
Douglas Gregor20c38a72010-05-21 23:43:39 +0000395 }
396
397 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000398 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregor20c38a72010-05-21 23:43:39 +0000399 // We have something like t::getAs<T>, where getAs is a
400 // member of an unknown specialization. However, this will only
401 // parse correctly as a template, so suggest the keyword 'template'
402 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000403 unsigned DiagID = diag::err_missing_dependent_template_keyword;
Francois Pichet0706d202011-09-17 17:15:52 +0000404 if (getLang().MicrosoftExt)
Francois Pichet93921652011-04-22 08:25:24 +0000405 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000406
407 Diag(Tok.getLocation(), DiagID)
Douglas Gregor20c38a72010-05-21 23:43:39 +0000408 << II.getName()
409 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
410
Douglas Gregorbb119652010-06-16 23:00:59 +0000411 if (TemplateNameKind TNK
Douglas Gregor0be31a22010-07-02 17:43:08 +0000412 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000413 Tok.getLocation(), SS,
414 TemplateName, ObjectType,
415 EnteringContext, Template)) {
416 // Consume the identifier.
417 ConsumeToken();
Douglas Gregore7c20652011-03-02 00:47:37 +0000418 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorbb119652010-06-16 23:00:59 +0000419 SourceLocation(), false))
420 return true;
421 }
422 else
Douglas Gregor20c38a72010-05-21 23:43:39 +0000423 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000424
Douglas Gregor20c38a72010-05-21 23:43:39 +0000425 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000426 }
427 }
428
Douglas Gregor7f741122009-02-25 19:37:18 +0000429 // We don't have any tokens that form the beginning of a
430 // nested-name-specifier, so we're done.
431 break;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000432 }
Mike Stump11289f42009-09-09 15:08:12 +0000433
Douglas Gregore610ada2010-02-24 18:44:31 +0000434 // Even if we didn't see any pieces of a nested-name-specifier, we
435 // still check whether there is a tilde in this position, which
436 // indicates a potential pseudo-destructor.
437 if (CheckForDestructor && Tok.is(tok::tilde))
438 *MayBePseudoDestructor = true;
439
John McCall1f476a12010-02-26 08:45:28 +0000440 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000441}
442
443/// ParseCXXIdExpression - Handle id-expression.
444///
445/// id-expression:
446/// unqualified-id
447/// qualified-id
448///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000449/// qualified-id:
450/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
451/// '::' identifier
452/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000453/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000454///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000455/// NOTE: The standard specifies that, for qualified-id, the parser does not
456/// expect:
457///
458/// '::' conversion-function-id
459/// '::' '~' class-name
460///
461/// This may cause a slight inconsistency on diagnostics:
462///
463/// class C {};
464/// namespace A {}
465/// void f() {
466/// :: A :: ~ C(); // Some Sema error about using destructor with a
467/// // namespace.
468/// :: ~ C(); // Some Parser error like 'unexpected ~'.
469/// }
470///
471/// We simplify the parser a bit and make it work like:
472///
473/// qualified-id:
474/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
475/// '::' unqualified-id
476///
477/// That way Sema can handle and report similar errors for namespaces and the
478/// global scope.
479///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000480/// The isAddressOfOperand parameter indicates that this id-expression is a
481/// direct operand of the address-of operator. This is, besides member contexts,
482/// the only place where a qualified-id naming a non-static class member may
483/// appear.
484///
John McCalldadc5752010-08-24 06:29:42 +0000485ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000486 // qualified-id:
487 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
488 // '::' unqualified-id
489 //
490 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +0000491 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregora121b752009-11-03 16:56:39 +0000492
493 UnqualifiedId Name;
494 if (ParseUnqualifiedId(SS,
495 /*EnteringContext=*/false,
496 /*AllowDestructorName=*/false,
497 /*AllowConstructorName=*/false,
John McCallba7bf592010-08-24 05:47:05 +0000498 /*ObjectType=*/ ParsedType(),
Douglas Gregora121b752009-11-03 16:56:39 +0000499 Name))
500 return ExprError();
John McCalla9ee3252009-11-22 02:49:43 +0000501
502 // This is only the direct operand of an & operator if it is not
503 // followed by a postfix-expression suffix.
John McCall8d08b9b2010-08-27 09:08:28 +0000504 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
505 isAddressOfOperand = false;
Douglas Gregora121b752009-11-03 16:56:39 +0000506
Douglas Gregor0be31a22010-07-02 17:43:08 +0000507 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregora121b752009-11-03 16:56:39 +0000508 isAddressOfOperand);
509
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000510}
511
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000512/// ParseLambdaExpression - Parse a C++0x lambda expression.
513///
514/// lambda-expression:
515/// lambda-introducer lambda-declarator[opt] compound-statement
516///
517/// lambda-introducer:
518/// '[' lambda-capture[opt] ']'
519///
520/// lambda-capture:
521/// capture-default
522/// capture-list
523/// capture-default ',' capture-list
524///
525/// capture-default:
526/// '&'
527/// '='
528///
529/// capture-list:
530/// capture
531/// capture-list ',' capture
532///
533/// capture:
534/// identifier
535/// '&' identifier
536/// 'this'
537///
538/// lambda-declarator:
539/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
540/// 'mutable'[opt] exception-specification[opt]
541/// trailing-return-type[opt]
542///
543ExprResult Parser::ParseLambdaExpression() {
544 // Parse lambda-introducer.
545 LambdaIntroducer Intro;
546
547 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
548 if (DiagID) {
549 Diag(Tok, DiagID.getValue());
550 SkipUntil(tok::r_square);
551 }
552
553 return ParseLambdaExpressionAfterIntroducer(Intro);
554}
555
556/// TryParseLambdaExpression - Use lookahead and potentially tentative
557/// parsing to determine if we are looking at a C++0x lambda expression, and parse
558/// it if we are.
559///
560/// If we are not looking at a lambda expression, returns ExprError().
561ExprResult Parser::TryParseLambdaExpression() {
562 assert(getLang().CPlusPlus0x
563 && Tok.is(tok::l_square)
564 && "Not at the start of a possible lambda expression.");
565
566 const Token Next = NextToken(), After = GetLookAheadToken(2);
567
568 // If lookahead indicates this is a lambda...
569 if (Next.is(tok::r_square) || // []
570 Next.is(tok::equal) || // [=
571 (Next.is(tok::amp) && // [&] or [&,
572 (After.is(tok::r_square) ||
573 After.is(tok::comma))) ||
574 (Next.is(tok::identifier) && // [identifier]
575 After.is(tok::r_square))) {
576 return ParseLambdaExpression();
577 }
578
579 // If lookahead indicates this is an Objective-C message...
580 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
581 return ExprError();
582 }
583
584 LambdaIntroducer Intro;
585 if (TryParseLambdaIntroducer(Intro))
586 return ExprError();
587 return ParseLambdaExpressionAfterIntroducer(Intro);
588}
589
590/// ParseLambdaExpression - Parse a lambda introducer.
591///
592/// Returns a DiagnosticID if it hit something unexpected.
593llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
594 typedef llvm::Optional<unsigned> DiagResult;
595
596 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
597 Intro.Range.setBegin(ConsumeBracket());
598
599 bool first = true;
600
601 // Parse capture-default.
602 if (Tok.is(tok::amp) &&
603 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
604 Intro.Default = LCD_ByRef;
605 ConsumeToken();
606 first = false;
607 } else if (Tok.is(tok::equal)) {
608 Intro.Default = LCD_ByCopy;
609 ConsumeToken();
610 first = false;
611 }
612
613 while (Tok.isNot(tok::r_square)) {
614 if (!first) {
615 if (Tok.isNot(tok::comma))
616 return DiagResult(diag::err_expected_comma_or_rsquare);
617 ConsumeToken();
618 }
619
620 first = false;
621
622 // Parse capture.
623 LambdaCaptureKind Kind = LCK_ByCopy;
624 SourceLocation Loc;
625 IdentifierInfo* Id = 0;
626
627 if (Tok.is(tok::kw_this)) {
628 Kind = LCK_This;
629 Loc = ConsumeToken();
630 } else {
631 if (Tok.is(tok::amp)) {
632 Kind = LCK_ByRef;
633 ConsumeToken();
634 }
635
636 if (Tok.is(tok::identifier)) {
637 Id = Tok.getIdentifierInfo();
638 Loc = ConsumeToken();
639 } else if (Tok.is(tok::kw_this)) {
640 // FIXME: If we want to suggest a fixit here, will need to return more
641 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
642 // Clear()ed to prevent emission in case of tentative parsing?
643 return DiagResult(diag::err_this_captured_by_reference);
644 } else {
645 return DiagResult(diag::err_expected_capture);
646 }
647 }
648
649 Intro.addCapture(Kind, Loc, Id);
650 }
651
652 Intro.Range.setEnd(MatchRHSPunctuation(tok::r_square,
653 Intro.Range.getBegin()));
654
655 return DiagResult();
656}
657
658/// TryParseLambdaExpression - Tentatively parse a lambda introducer.
659///
660/// Returns true if it hit something unexpected.
661bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
662 TentativeParsingAction PA(*this);
663
664 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
665
666 if (DiagID) {
667 PA.Revert();
668 return true;
669 }
670
671 PA.Commit();
672 return false;
673}
674
675/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
676/// expression.
677ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
678 LambdaIntroducer &Intro) {
679 // Parse lambda-declarator[opt].
680 DeclSpec DS(AttrFactory);
681 Declarator D(DS, Declarator::PrototypeContext);
682
683 if (Tok.is(tok::l_paren)) {
684 ParseScope PrototypeScope(this,
685 Scope::FunctionPrototypeScope |
686 Scope::DeclScope);
687
688 SourceLocation DeclLoc = ConsumeParen(), DeclEndLoc;
689
690 // Parse parameter-declaration-clause.
691 ParsedAttributes Attr(AttrFactory);
692 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
693 SourceLocation EllipsisLoc;
694
695 if (Tok.isNot(tok::r_paren))
696 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
697
698 DeclEndLoc = MatchRHSPunctuation(tok::r_paren, DeclLoc);
699
700 // Parse 'mutable'[opt].
701 SourceLocation MutableLoc;
702 if (Tok.is(tok::kw_mutable)) {
703 MutableLoc = ConsumeToken();
704 DeclEndLoc = MutableLoc;
705 }
706
707 // Parse exception-specification[opt].
708 ExceptionSpecificationType ESpecType = EST_None;
709 SourceRange ESpecRange;
710 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
711 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
712 ExprResult NoexceptExpr;
713 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
714 DynamicExceptions,
715 DynamicExceptionRanges,
716 NoexceptExpr);
717
718 if (ESpecType != EST_None)
719 DeclEndLoc = ESpecRange.getEnd();
720
721 // Parse attribute-specifier[opt].
722 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
723
724 // Parse trailing-return-type[opt].
725 ParsedType TrailingReturnType;
726 if (Tok.is(tok::arrow)) {
727 SourceRange Range;
728 TrailingReturnType = ParseTrailingReturnType(Range).get();
729 if (Range.getEnd().isValid())
730 DeclEndLoc = Range.getEnd();
731 }
732
733 PrototypeScope.Exit();
734
735 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
736 /*isVariadic=*/EllipsisLoc.isValid(),
737 EllipsisLoc,
738 ParamInfo.data(), ParamInfo.size(),
739 DS.getTypeQualifiers(),
740 /*RefQualifierIsLValueRef=*/true,
741 /*RefQualifierLoc=*/SourceLocation(),
742 MutableLoc,
743 ESpecType, ESpecRange.getBegin(),
744 DynamicExceptions.data(),
745 DynamicExceptionRanges.data(),
746 DynamicExceptions.size(),
747 NoexceptExpr.isUsable() ?
748 NoexceptExpr.get() : 0,
749 DeclLoc, DeclEndLoc, D,
750 TrailingReturnType),
751 Attr, DeclEndLoc);
752 }
753
754 // Parse compound-statement.
755 if (Tok.is(tok::l_brace)) {
756 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
757 // it.
758 ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
759 Scope::BreakScope | Scope::ContinueScope |
760 Scope::DeclScope);
761
762 StmtResult Stmt(ParseCompoundStatementBody());
763
764 BodyScope.Exit();
765 } else {
766 Diag(Tok, diag::err_expected_lambda_body);
767 }
768
769 return ExprEmpty();
770}
771
Chris Lattner29375652006-12-04 18:06:35 +0000772/// ParseCXXCasts - This handles the various ways to cast expressions to another
773/// type.
774///
775/// postfix-expression: [C++ 5.2p1]
776/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
777/// 'static_cast' '<' type-name '>' '(' expression ')'
778/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
779/// 'const_cast' '<' type-name '>' '(' expression ')'
780///
John McCalldadc5752010-08-24 06:29:42 +0000781ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +0000782 tok::TokenKind Kind = Tok.getKind();
783 const char *CastName = 0; // For error messages
784
785 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +0000786 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +0000787 case tok::kw_const_cast: CastName = "const_cast"; break;
788 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
789 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
790 case tok::kw_static_cast: CastName = "static_cast"; break;
791 }
792
793 SourceLocation OpLoc = ConsumeToken();
794 SourceLocation LAngleBracketLoc = Tok.getLocation();
795
Richard Smith55858492011-04-14 21:45:45 +0000796 // Check for "<::" which is parsed as "[:". If found, fix token stream,
797 // diagnose error, suggest fix, and recover parsing.
798 Token Next = NextToken();
799 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
800 AreTokensAdjacent(PP, Tok, Next))
801 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
802
Chris Lattner29375652006-12-04 18:06:35 +0000803 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +0000804 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000805
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000806 // Parse the common declaration-specifiers piece.
807 DeclSpec DS(AttrFactory);
808 ParseSpecifierQualifierList(DS);
809
810 // Parse the abstract-declarator, if present.
811 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
812 ParseDeclarator(DeclaratorInfo);
813
Chris Lattner29375652006-12-04 18:06:35 +0000814 SourceLocation RAngleBracketLoc = Tok.getLocation();
815
Chris Lattner6d29c102008-11-18 07:48:38 +0000816 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +0000817 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +0000818
819 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
820
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000821 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
822 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000823
John McCalldadc5752010-08-24 06:29:42 +0000824 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +0000825
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000826 // Match the ')'.
Douglas Gregorf4f2ff72009-11-06 05:48:00 +0000827 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner29375652006-12-04 18:06:35 +0000828
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000829 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +0000830 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +0000831 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +0000832 RAngleBracketLoc,
John McCallb268a282010-08-23 23:25:46 +0000833 LParenLoc, Result.take(), RParenLoc);
Chris Lattner29375652006-12-04 18:06:35 +0000834
Sebastian Redld65cea82008-12-11 22:51:44 +0000835 return move(Result);
Chris Lattner29375652006-12-04 18:06:35 +0000836}
Bill Wendling4073ed52007-02-13 01:51:42 +0000837
Sebastian Redlc4704762008-11-11 11:37:55 +0000838/// ParseCXXTypeid - This handles the C++ typeid expression.
839///
840/// postfix-expression: [C++ 5.2p1]
841/// 'typeid' '(' expression ')'
842/// 'typeid' '(' type-id ')'
843///
John McCalldadc5752010-08-24 06:29:42 +0000844ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +0000845 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
846
847 SourceLocation OpLoc = ConsumeToken();
848 SourceLocation LParenLoc = Tok.getLocation();
849 SourceLocation RParenLoc;
850
851 // typeid expressions are always parenthesized.
852 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
853 "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +0000854 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +0000855
John McCalldadc5752010-08-24 06:29:42 +0000856 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +0000857
858 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +0000859 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +0000860
861 // Match the ')'.
Douglas Gregor4c7c1092010-09-08 23:14:30 +0000862 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +0000863
Douglas Gregor4c7c1092010-09-08 23:14:30 +0000864 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +0000865 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +0000866
867 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +0000868 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +0000869 } else {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000870 // C++0x [expr.typeid]p3:
Mike Stump11289f42009-09-09 15:08:12 +0000871 // When typeid is applied to an expression other than an lvalue of a
872 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000873 // operand (Clause 5).
874 //
Mike Stump11289f42009-09-09 15:08:12 +0000875 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000876 // polymorphic class type until after we've parsed the expression, so
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000877 // we the expression is potentially potentially evaluated.
878 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallfaf5fb42010-08-26 23:41:50 +0000879 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc4704762008-11-11 11:37:55 +0000880 Result = ParseExpression();
881
882 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000883 if (Result.isInvalid())
Sebastian Redlc4704762008-11-11 11:37:55 +0000884 SkipUntil(tok::r_paren);
885 else {
Douglas Gregor4c7c1092010-09-08 23:14:30 +0000886 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
887 if (RParenLoc.isInvalid())
888 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +0000889
Sebastian Redlc4704762008-11-11 11:37:55 +0000890 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000891 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +0000892 }
893 }
894
Sebastian Redld65cea82008-12-11 22:51:44 +0000895 return move(Result);
Sebastian Redlc4704762008-11-11 11:37:55 +0000896}
897
Francois Pichet9f4f2072010-09-08 12:20:18 +0000898/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
899///
900/// '__uuidof' '(' expression ')'
901/// '__uuidof' '(' type-id ')'
902///
903ExprResult Parser::ParseCXXUuidof() {
904 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
905
906 SourceLocation OpLoc = ConsumeToken();
907 SourceLocation LParenLoc = Tok.getLocation();
908 SourceLocation RParenLoc;
909
910 // __uuidof expressions are always parenthesized.
911 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
912 "__uuidof"))
913 return ExprError();
914
915 ExprResult Result;
916
917 if (isTypeIdInParens()) {
918 TypeResult Ty = ParseTypeName();
919
920 // Match the ')'.
921 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
922
923 if (Ty.isInvalid())
924 return ExprError();
925
926 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
927 Ty.get().getAsOpaquePtr(), RParenLoc);
928 } else {
929 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
930 Result = ParseExpression();
931
932 // Match the ')'.
933 if (Result.isInvalid())
934 SkipUntil(tok::r_paren);
935 else {
936 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
937
938 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
939 Result.release(), RParenLoc);
940 }
941 }
942
943 return move(Result);
944}
945
Douglas Gregore610ada2010-02-24 18:44:31 +0000946/// \brief Parse a C++ pseudo-destructor expression after the base,
947/// . or -> operator, and nested-name-specifier have already been
948/// parsed.
949///
950/// postfix-expression: [C++ 5.2]
951/// postfix-expression . pseudo-destructor-name
952/// postfix-expression -> pseudo-destructor-name
953///
954/// pseudo-destructor-name:
955/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
956/// ::[opt] nested-name-specifier template simple-template-id ::
957/// ~type-name
958/// ::[opt] nested-name-specifier[opt] ~type-name
959///
John McCalldadc5752010-08-24 06:29:42 +0000960ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +0000961Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
962 tok::TokenKind OpKind,
963 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000964 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000965 // We're parsing either a pseudo-destructor-name or a dependent
966 // member access that has the same form as a
967 // pseudo-destructor-name. We parse both in the same way and let
968 // the action model sort them out.
969 //
970 // Note that the ::[opt] nested-name-specifier[opt] has already
971 // been parsed, and if there was a simple-template-id, it has
972 // been coalesced into a template-id annotation token.
973 UnqualifiedId FirstTypeName;
974 SourceLocation CCLoc;
975 if (Tok.is(tok::identifier)) {
976 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
977 ConsumeToken();
978 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
979 CCLoc = ConsumeToken();
980 } else if (Tok.is(tok::annot_template_id)) {
981 FirstTypeName.setTemplateId(
982 (TemplateIdAnnotation *)Tok.getAnnotationValue());
983 ConsumeToken();
984 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
985 CCLoc = ConsumeToken();
986 } else {
987 FirstTypeName.setIdentifier(0, SourceLocation());
988 }
989
990 // Parse the tilde.
991 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
992 SourceLocation TildeLoc = ConsumeToken();
993 if (!Tok.is(tok::identifier)) {
994 Diag(Tok, diag::err_destructor_tilde_identifier);
995 return ExprError();
996 }
997
998 // Parse the second type.
999 UnqualifiedId SecondTypeName;
1000 IdentifierInfo *Name = Tok.getIdentifierInfo();
1001 SourceLocation NameLoc = ConsumeToken();
1002 SecondTypeName.setIdentifier(Name, NameLoc);
1003
1004 // If there is a '<', the second type name is a template-id. Parse
1005 // it as such.
1006 if (Tok.is(tok::less) &&
1007 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregorb22ee882010-05-05 05:58:24 +00001008 SecondTypeName, /*AssumeTemplateName=*/true,
1009 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregore610ada2010-02-24 18:44:31 +00001010 return ExprError();
1011
John McCallb268a282010-08-23 23:25:46 +00001012 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1013 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001014 SS, FirstTypeName, CCLoc,
1015 TildeLoc, SecondTypeName,
1016 Tok.is(tok::l_paren));
1017}
1018
Bill Wendling4073ed52007-02-13 01:51:42 +00001019/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1020///
1021/// boolean-literal: [C++ 2.13.5]
1022/// 'true'
1023/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001024ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001025 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001026 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001027}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001028
1029/// ParseThrowExpression - This handles the C++ throw expression.
1030///
1031/// throw-expression: [C++ 15]
1032/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001033ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001034 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001035 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001036
Chris Lattner65dd8432008-04-06 06:02:23 +00001037 // If the current token isn't the start of an assignment-expression,
1038 // then the expression is not present. This handles things like:
1039 // "C ? throw : (void)42", which is crazy but legal.
1040 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1041 case tok::semi:
1042 case tok::r_paren:
1043 case tok::r_square:
1044 case tok::r_brace:
1045 case tok::colon:
1046 case tok::comma:
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001047 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001048
Chris Lattner65dd8432008-04-06 06:02:23 +00001049 default:
John McCalldadc5752010-08-24 06:29:42 +00001050 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redld65cea82008-12-11 22:51:44 +00001051 if (Expr.isInvalid()) return move(Expr);
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001052 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001053 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001054}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001055
1056/// ParseCXXThis - This handles the C++ 'this' pointer.
1057///
1058/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1059/// a non-lvalue expression whose value is the address of the object for which
1060/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001061ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001062 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1063 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001064 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001065}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001066
1067/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1068/// Can be interpreted either as function-style casting ("int(x)")
1069/// or class type construction ("ClassType(x,y,z)")
1070/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001071/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001072///
1073/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001074/// simple-type-specifier '(' expression-list[opt] ')'
1075/// [C++0x] simple-type-specifier braced-init-list
1076/// typename-specifier '(' expression-list[opt] ')'
1077/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001078///
John McCalldadc5752010-08-24 06:29:42 +00001079ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001080Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001081 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001082 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001083
Sebastian Redl3da34892011-06-05 12:23:16 +00001084 assert((Tok.is(tok::l_paren) ||
1085 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1086 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001087
Sebastian Redl3da34892011-06-05 12:23:16 +00001088 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001089
Sebastian Redl3da34892011-06-05 12:23:16 +00001090 // FIXME: Convert to a proper type construct expression.
1091 return ParseBraceInitializer();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001092
Sebastian Redl3da34892011-06-05 12:23:16 +00001093 } else {
1094 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1095
1096 SourceLocation LParenLoc = ConsumeParen();
1097
1098 ExprVector Exprs(Actions);
1099 CommaLocsTy CommaLocs;
1100
1101 if (Tok.isNot(tok::r_paren)) {
1102 if (ParseExpressionList(Exprs, CommaLocs)) {
1103 SkipUntil(tok::r_paren);
1104 return ExprError();
1105 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001106 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001107
1108 // Match the ')'.
1109 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1110
1111 // TypeRep could be null, if it references an invalid typedef.
1112 if (!TypeRep)
1113 return ExprError();
1114
1115 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1116 "Unexpected number of commas!");
1117 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
1118 RParenLoc);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001119 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001120}
1121
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001122/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001123///
1124/// condition:
1125/// expression
1126/// type-specifier-seq declarator '=' assignment-expression
1127/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1128/// '=' assignment-expression
1129///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001130/// \param ExprResult if the condition was parsed as an expression, the
1131/// parsed expression.
1132///
1133/// \param DeclResult if the condition was parsed as a declaration, the
1134/// parsed declaration.
1135///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001136/// \param Loc The location of the start of the statement that requires this
1137/// condition, e.g., the "for" in a for loop.
1138///
1139/// \param ConvertToBoolean Whether the condition expression should be
1140/// converted to a boolean value.
1141///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001142/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001143bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1144 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001145 SourceLocation Loc,
1146 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001147 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001148 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001149 cutOffParsing();
1150 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001151 }
1152
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001153 if (!isCXXConditionDeclaration()) {
Douglas Gregore60e41a2010-05-06 17:25:47 +00001154 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001155 ExprOut = ParseExpression(); // expression
1156 DeclOut = 0;
1157 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001158 return true;
1159
1160 // If required, convert to a boolean value.
1161 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001162 ExprOut
1163 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1164 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001165 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001166
1167 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001168 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001169 ParseSpecifierQualifierList(DS);
1170
1171 // declarator
1172 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1173 ParseDeclarator(DeclaratorInfo);
1174
1175 // simple-asm-expr[opt]
1176 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001177 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001178 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001179 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001180 SkipUntil(tok::semi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001181 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001182 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001183 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001184 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001185 }
1186
1187 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001188 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001189
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001190 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001191 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001192 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001193 DeclOut = Dcl.get();
1194 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001195
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001196 // '=' assignment-expression
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001197 if (isTokenEqualOrMistypedEqualEqual(
1198 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001199 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001200 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001201 if (!AssignExpr.isInvalid())
Richard Smith30482bc2011-02-20 03:19:35 +00001202 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1203 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001204 } else {
1205 // FIXME: C++0x allows a braced-init-list
1206 Diag(Tok, diag::err_expected_equal_after_declarator);
1207 }
1208
Douglas Gregore60e41a2010-05-06 17:25:47 +00001209 // FIXME: Build a reference to this declaration? Convert it to bool?
1210 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001211
1212 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001213
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001214 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001215}
1216
Douglas Gregor8d4de672010-04-21 22:36:40 +00001217/// \brief Determine whether the current token starts a C++
1218/// simple-type-specifier.
1219bool Parser::isCXXSimpleTypeSpecifier() const {
1220 switch (Tok.getKind()) {
1221 case tok::annot_typename:
1222 case tok::kw_short:
1223 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00001224 case tok::kw___int64:
Douglas Gregor8d4de672010-04-21 22:36:40 +00001225 case tok::kw_signed:
1226 case tok::kw_unsigned:
1227 case tok::kw_void:
1228 case tok::kw_char:
1229 case tok::kw_int:
1230 case tok::kw_float:
1231 case tok::kw_double:
1232 case tok::kw_wchar_t:
1233 case tok::kw_char16_t:
1234 case tok::kw_char32_t:
1235 case tok::kw_bool:
Douglas Gregor19b7acf2011-04-27 05:41:15 +00001236 case tok::kw_decltype:
Douglas Gregor8d4de672010-04-21 22:36:40 +00001237 case tok::kw_typeof:
Alexis Hunt4a257072011-05-19 05:37:45 +00001238 case tok::kw___underlying_type:
Douglas Gregor8d4de672010-04-21 22:36:40 +00001239 return true;
1240
1241 default:
1242 break;
1243 }
1244
1245 return false;
1246}
1247
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001248/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1249/// This should only be called when the current token is known to be part of
1250/// simple-type-specifier.
1251///
1252/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001253/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001254/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1255/// char
1256/// wchar_t
1257/// bool
1258/// short
1259/// int
1260/// long
1261/// signed
1262/// unsigned
1263/// float
1264/// double
1265/// void
1266/// [GNU] typeof-specifier
1267/// [C++0x] auto [TODO]
1268///
1269/// type-name:
1270/// class-name
1271/// enum-name
1272/// typedef-name
1273///
1274void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1275 DS.SetRangeStart(Tok.getLocation());
1276 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001277 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001278 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001279
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001280 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001281 case tok::identifier: // foo::bar
1282 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001283 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001284 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001285 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001286
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001287 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001288 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001289 if (getTypeAnnotation(Tok))
1290 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1291 getTypeAnnotation(Tok));
1292 else
1293 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001294
1295 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1296 ConsumeToken();
1297
1298 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1299 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1300 // Objective-C interface. If we don't have Objective-C or a '<', this is
1301 // just a normal reference to a typedef name.
1302 if (Tok.is(tok::less) && getLang().ObjC1)
1303 ParseObjCProtocolQualifiers(DS);
1304
1305 DS.Finish(Diags, PP);
1306 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001307 }
Mike Stump11289f42009-09-09 15:08:12 +00001308
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001309 // builtin types
1310 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001311 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001312 break;
1313 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +00001314 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001315 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001316 case tok::kw___int64:
1317 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1318 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001319 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001320 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001321 break;
1322 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001323 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001324 break;
1325 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001326 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001327 break;
1328 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001329 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001330 break;
1331 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001332 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001333 break;
1334 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001335 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001336 break;
1337 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001338 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001339 break;
1340 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001341 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001342 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001343 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001344 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001345 break;
1346 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001347 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001348 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001349 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +00001350 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001351 break;
Mike Stump11289f42009-09-09 15:08:12 +00001352
Douglas Gregor8d4de672010-04-21 22:36:40 +00001353 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001354 // GNU typeof support.
1355 case tok::kw_typeof:
1356 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +00001357 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001358 return;
1359 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001360 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001361 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1362 else
1363 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001364 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +00001365 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001366}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001367
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001368/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1369/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1370/// e.g., "const short int". Note that the DeclSpec is *not* finished
1371/// by parsing the type-specifier-seq, because these sequences are
1372/// typically followed by some form of declarator. Returns true and
1373/// emits diagnostics if this is not a type-specifier-seq, false
1374/// otherwise.
1375///
1376/// type-specifier-seq: [C++ 8.1]
1377/// type-specifier type-specifier-seq[opt]
1378///
1379bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1380 DS.SetRangeStart(Tok.getLocation());
1381 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001382 unsigned DiagID;
1383 bool isInvalid = 0;
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001384
1385 // Parse one or more of the type specifiers.
Sebastian Redl2b372722010-02-03 21:21:43 +00001386 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1387 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky07e97c52010-11-03 17:52:57 +00001388 Diag(Tok, diag::err_expected_type);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001389 return true;
1390 }
Mike Stump11289f42009-09-09 15:08:12 +00001391
Sebastian Redl2b372722010-02-03 21:21:43 +00001392 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1393 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1394 {}
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001395
Douglas Gregor40d732f2010-02-24 23:13:13 +00001396 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001397 return false;
1398}
1399
Douglas Gregor7861a802009-11-03 01:35:08 +00001400/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1401/// some form.
1402///
1403/// This routine is invoked when a '<' is encountered after an identifier or
1404/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1405/// whether the unqualified-id is actually a template-id. This routine will
1406/// then parse the template arguments and form the appropriate template-id to
1407/// return to the caller.
1408///
1409/// \param SS the nested-name-specifier that precedes this template-id, if
1410/// we're actually parsing a qualified-id.
1411///
1412/// \param Name for constructor and destructor names, this is the actual
1413/// identifier that may be a template-name.
1414///
1415/// \param NameLoc the location of the class-name in a constructor or
1416/// destructor.
1417///
1418/// \param EnteringContext whether we're entering the scope of the
1419/// nested-name-specifier.
1420///
Douglas Gregor127ea592009-11-03 21:24:04 +00001421/// \param ObjectType if this unqualified-id occurs within a member access
1422/// expression, the type of the base object whose member is being accessed.
1423///
Douglas Gregor7861a802009-11-03 01:35:08 +00001424/// \param Id as input, describes the template-name or operator-function-id
1425/// that precedes the '<'. If template arguments were parsed successfully,
1426/// will be updated with the template-id.
1427///
Douglas Gregore610ada2010-02-24 18:44:31 +00001428/// \param AssumeTemplateId When true, this routine will assume that the name
1429/// refers to a template without performing name lookup to verify.
1430///
Douglas Gregor7861a802009-11-03 01:35:08 +00001431/// \returns true if a parse error occurred, false otherwise.
1432bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1433 IdentifierInfo *Name,
1434 SourceLocation NameLoc,
1435 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001436 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001437 UnqualifiedId &Id,
Douglas Gregorb22ee882010-05-05 05:58:24 +00001438 bool AssumeTemplateId,
1439 SourceLocation TemplateKWLoc) {
1440 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1441 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001442
1443 TemplateTy Template;
1444 TemplateNameKind TNK = TNK_Non_template;
1445 switch (Id.getKind()) {
1446 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001447 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001448 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001449 if (AssumeTemplateId) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001450 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregorbb119652010-06-16 23:00:59 +00001451 Id, ObjectType, EnteringContext,
1452 Template);
1453 if (TNK == TNK_Non_template)
1454 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001455 } else {
1456 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001457 TNK = Actions.isTemplateName(getCurScope(), SS,
1458 TemplateKWLoc.isValid(), Id,
1459 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001460 MemberOfUnknownSpecialization);
1461
1462 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1463 ObjectType && IsTemplateArgumentList()) {
1464 // We have something like t->getAs<T>(), where getAs is a
1465 // member of an unknown specialization. However, this will only
1466 // parse correctly as a template, so suggest the keyword 'template'
1467 // before 'getAs' and treat this as a dependent template name.
1468 std::string Name;
1469 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1470 Name = Id.Identifier->getName();
1471 else {
1472 Name = "operator ";
1473 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1474 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1475 else
1476 Name += Id.Identifier->getName();
1477 }
1478 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1479 << Name
1480 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor0be31a22010-07-02 17:43:08 +00001481 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001482 SS, Id, ObjectType,
1483 EnteringContext, Template);
1484 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001485 return true;
1486 }
1487 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001488 break;
1489
Douglas Gregor3cf81312009-11-03 23:16:33 +00001490 case UnqualifiedId::IK_ConstructorName: {
1491 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001492 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001493 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001494 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1495 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001496 EnteringContext, Template,
1497 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001498 break;
1499 }
1500
Douglas Gregor3cf81312009-11-03 23:16:33 +00001501 case UnqualifiedId::IK_DestructorName: {
1502 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001503 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001504 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001505 if (ObjectType) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001506 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregorbb119652010-06-16 23:00:59 +00001507 TemplateName, ObjectType,
1508 EnteringContext, Template);
1509 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001510 return true;
1511 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001512 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1513 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001514 EnteringContext, Template,
1515 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001516
John McCallba7bf592010-08-24 05:47:05 +00001517 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001518 Diag(NameLoc, diag::err_destructor_template_id)
1519 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001520 return true;
1521 }
1522 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001523 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001524 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001525
1526 default:
1527 return false;
1528 }
1529
1530 if (TNK == TNK_Non_template)
1531 return false;
1532
1533 // Parse the enclosed template argument list.
1534 SourceLocation LAngleLoc, RAngleLoc;
1535 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001536 if (Tok.is(tok::less) &&
1537 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001538 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001539 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001540 RAngleLoc))
1541 return true;
1542
1543 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001544 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1545 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001546 // Form a parsed representation of the template-id to be stored in the
1547 // UnqualifiedId.
1548 TemplateIdAnnotation *TemplateId
1549 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1550
1551 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1552 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001553 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001554 TemplateId->TemplateNameLoc = Id.StartLocation;
1555 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001556 TemplateId->Name = 0;
1557 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1558 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001559 }
1560
Douglas Gregore7c20652011-03-02 00:47:37 +00001561 TemplateId->SS = SS;
John McCall3e56fd42010-08-23 07:28:44 +00001562 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001563 TemplateId->Kind = TNK;
1564 TemplateId->LAngleLoc = LAngleLoc;
1565 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001566 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001567 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001568 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001569 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001570
1571 Id.setTemplateId(TemplateId);
1572 return false;
1573 }
1574
1575 // Bundle the template arguments together.
1576 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor7861a802009-11-03 01:35:08 +00001577 TemplateArgs.size());
1578
1579 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001580 TypeResult Type
Douglas Gregore7c20652011-03-02 00:47:37 +00001581 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001582 LAngleLoc, TemplateArgsPtr,
Douglas Gregor7861a802009-11-03 01:35:08 +00001583 RAngleLoc);
1584 if (Type.isInvalid())
1585 return true;
1586
1587 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1588 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1589 else
1590 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1591
1592 return false;
1593}
1594
Douglas Gregor71395fa2009-11-04 00:56:37 +00001595/// \brief Parse an operator-function-id or conversion-function-id as part
1596/// of a C++ unqualified-id.
1597///
1598/// This routine is responsible only for parsing the operator-function-id or
1599/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001600///
1601/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001602/// operator-function-id: [C++ 13.5]
1603/// 'operator' operator
1604///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001605/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001606/// new delete new[] delete[]
1607/// + - * / % ^ & | ~
1608/// ! = < > += -= *= /= %=
1609/// ^= &= |= << >> >>= <<= == !=
1610/// <= >= && || ++ -- , ->* ->
1611/// () []
1612///
1613/// conversion-function-id: [C++ 12.3.2]
1614/// operator conversion-type-id
1615///
1616/// conversion-type-id:
1617/// type-specifier-seq conversion-declarator[opt]
1618///
1619/// conversion-declarator:
1620/// ptr-operator conversion-declarator[opt]
1621/// \endcode
1622///
1623/// \param The nested-name-specifier that preceded this unqualified-id. If
1624/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1625///
1626/// \param EnteringContext whether we are entering the scope of the
1627/// nested-name-specifier.
1628///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001629/// \param ObjectType if this unqualified-id occurs within a member access
1630/// expression, the type of the base object whose member is being accessed.
1631///
1632/// \param Result on a successful parse, contains the parsed unqualified-id.
1633///
1634/// \returns true if parsing fails, false otherwise.
1635bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001636 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001637 UnqualifiedId &Result) {
1638 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1639
1640 // Consume the 'operator' keyword.
1641 SourceLocation KeywordLoc = ConsumeToken();
1642
1643 // Determine what kind of operator name we have.
1644 unsigned SymbolIdx = 0;
1645 SourceLocation SymbolLocations[3];
1646 OverloadedOperatorKind Op = OO_None;
1647 switch (Tok.getKind()) {
1648 case tok::kw_new:
1649 case tok::kw_delete: {
1650 bool isNew = Tok.getKind() == tok::kw_new;
1651 // Consume the 'new' or 'delete'.
1652 SymbolLocations[SymbolIdx++] = ConsumeToken();
1653 if (Tok.is(tok::l_square)) {
1654 // Consume the '['.
1655 SourceLocation LBracketLoc = ConsumeBracket();
1656 // Consume the ']'.
1657 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1658 LBracketLoc);
1659 if (RBracketLoc.isInvalid())
1660 return true;
1661
1662 SymbolLocations[SymbolIdx++] = LBracketLoc;
1663 SymbolLocations[SymbolIdx++] = RBracketLoc;
1664 Op = isNew? OO_Array_New : OO_Array_Delete;
1665 } else {
1666 Op = isNew? OO_New : OO_Delete;
1667 }
1668 break;
1669 }
1670
1671#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1672 case tok::Token: \
1673 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1674 Op = OO_##Name; \
1675 break;
1676#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1677#include "clang/Basic/OperatorKinds.def"
1678
1679 case tok::l_paren: {
1680 // Consume the '('.
1681 SourceLocation LParenLoc = ConsumeParen();
1682 // Consume the ')'.
1683 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1684 LParenLoc);
1685 if (RParenLoc.isInvalid())
1686 return true;
1687
1688 SymbolLocations[SymbolIdx++] = LParenLoc;
1689 SymbolLocations[SymbolIdx++] = RParenLoc;
1690 Op = OO_Call;
1691 break;
1692 }
1693
1694 case tok::l_square: {
1695 // Consume the '['.
1696 SourceLocation LBracketLoc = ConsumeBracket();
1697 // Consume the ']'.
1698 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1699 LBracketLoc);
1700 if (RBracketLoc.isInvalid())
1701 return true;
1702
1703 SymbolLocations[SymbolIdx++] = LBracketLoc;
1704 SymbolLocations[SymbolIdx++] = RBracketLoc;
1705 Op = OO_Subscript;
1706 break;
1707 }
1708
1709 case tok::code_completion: {
1710 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001711 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001712 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001713 // Don't try to parse any further.
1714 return true;
1715 }
1716
1717 default:
1718 break;
1719 }
1720
1721 if (Op != OO_None) {
1722 // We have parsed an operator-function-id.
1723 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1724 return false;
1725 }
Alexis Hunt34458502009-11-28 04:44:28 +00001726
1727 // Parse a literal-operator-id.
1728 //
1729 // literal-operator-id: [C++0x 13.5.8]
1730 // operator "" identifier
1731
1732 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1733 if (Tok.getLength() != 2)
1734 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1735 ConsumeStringToken();
1736
1737 if (Tok.isNot(tok::identifier)) {
1738 Diag(Tok.getLocation(), diag::err_expected_ident);
1739 return true;
1740 }
1741
1742 IdentifierInfo *II = Tok.getIdentifierInfo();
1743 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Alexis Hunt3d221f22009-11-29 07:34:05 +00001744 return false;
Alexis Hunt34458502009-11-28 04:44:28 +00001745 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00001746
1747 // Parse a conversion-function-id.
1748 //
1749 // conversion-function-id: [C++ 12.3.2]
1750 // operator conversion-type-id
1751 //
1752 // conversion-type-id:
1753 // type-specifier-seq conversion-declarator[opt]
1754 //
1755 // conversion-declarator:
1756 // ptr-operator conversion-declarator[opt]
1757
1758 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00001759 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00001760 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00001761 return true;
1762
1763 // Parse the conversion-declarator, which is merely a sequence of
1764 // ptr-operators.
1765 Declarator D(DS, Declarator::TypeNameContext);
1766 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1767
1768 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00001769 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00001770 if (Ty.isInvalid())
1771 return true;
1772
1773 // Note that this is a conversion-function-id.
1774 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1775 D.getSourceRange().getEnd());
1776 return false;
1777}
1778
1779/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1780/// name of an entity.
1781///
1782/// \code
1783/// unqualified-id: [C++ expr.prim.general]
1784/// identifier
1785/// operator-function-id
1786/// conversion-function-id
1787/// [C++0x] literal-operator-id [TODO]
1788/// ~ class-name
1789/// template-id
1790///
1791/// \endcode
1792///
1793/// \param The nested-name-specifier that preceded this unqualified-id. If
1794/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1795///
1796/// \param EnteringContext whether we are entering the scope of the
1797/// nested-name-specifier.
1798///
Douglas Gregor7861a802009-11-03 01:35:08 +00001799/// \param AllowDestructorName whether we allow parsing of a destructor name.
1800///
1801/// \param AllowConstructorName whether we allow parsing a constructor name.
1802///
Douglas Gregor127ea592009-11-03 21:24:04 +00001803/// \param ObjectType if this unqualified-id occurs within a member access
1804/// expression, the type of the base object whose member is being accessed.
1805///
Douglas Gregor7861a802009-11-03 01:35:08 +00001806/// \param Result on a successful parse, contains the parsed unqualified-id.
1807///
1808/// \returns true if parsing fails, false otherwise.
1809bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1810 bool AllowDestructorName,
1811 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00001812 ParsedType ObjectType,
Douglas Gregor7861a802009-11-03 01:35:08 +00001813 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001814
1815 // Handle 'A::template B'. This is for template-ids which have not
1816 // already been annotated by ParseOptionalCXXScopeSpecifier().
1817 bool TemplateSpecified = false;
1818 SourceLocation TemplateKWLoc;
1819 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1820 (ObjectType || SS.isSet())) {
1821 TemplateSpecified = true;
1822 TemplateKWLoc = ConsumeToken();
1823 }
1824
Douglas Gregor7861a802009-11-03 01:35:08 +00001825 // unqualified-id:
1826 // identifier
1827 // template-id (when it hasn't already been annotated)
1828 if (Tok.is(tok::identifier)) {
1829 // Consume the identifier.
1830 IdentifierInfo *Id = Tok.getIdentifierInfo();
1831 SourceLocation IdLoc = ConsumeToken();
1832
Douglas Gregor411e5ac2010-01-11 23:29:10 +00001833 if (!getLang().CPlusPlus) {
1834 // If we're not in C++, only identifiers matter. Record the
1835 // identifier and return.
1836 Result.setIdentifier(Id, IdLoc);
1837 return false;
1838 }
1839
Douglas Gregor7861a802009-11-03 01:35:08 +00001840 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001841 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001842 // We have parsed a constructor name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001843 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001844 &SS, false, false,
1845 ParsedType(),
1846 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor7861a802009-11-03 01:35:08 +00001847 IdLoc, IdLoc);
1848 } else {
1849 // We have parsed an identifier.
1850 Result.setIdentifier(Id, IdLoc);
1851 }
1852
1853 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00001854 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor7861a802009-11-03 01:35:08 +00001855 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregorb22ee882010-05-05 05:58:24 +00001856 ObjectType, Result,
1857 TemplateSpecified, TemplateKWLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00001858
1859 return false;
1860 }
1861
1862 // unqualified-id:
1863 // template-id (already parsed and annotated)
1864 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001865 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001866
1867 // If the template-name names the current class, then this is a constructor
1868 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001869 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001870 if (SS.isSet()) {
1871 // C++ [class.qual]p2 specifies that a qualified template-name
1872 // is taken as the constructor name where a constructor can be
1873 // declared. Thus, the template arguments are extraneous, so
1874 // complain about them and remove them entirely.
1875 Diag(TemplateId->TemplateNameLoc,
1876 diag::err_out_of_line_constructor_template_id)
1877 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00001878 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001879 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1880 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1881 TemplateId->TemplateNameLoc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001882 getCurScope(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001883 &SS, false, false,
1884 ParsedType(),
1885 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001886 TemplateId->TemplateNameLoc,
1887 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001888 ConsumeToken();
1889 return false;
1890 }
1891
1892 Result.setConstructorTemplateId(TemplateId);
1893 ConsumeToken();
1894 return false;
1895 }
1896
Douglas Gregor7861a802009-11-03 01:35:08 +00001897 // We have already parsed a template-id; consume the annotation token as
1898 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001899 Result.setTemplateId(TemplateId);
Douglas Gregor7861a802009-11-03 01:35:08 +00001900 ConsumeToken();
1901 return false;
1902 }
1903
1904 // unqualified-id:
1905 // operator-function-id
1906 // conversion-function-id
1907 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00001908 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00001909 return true;
1910
Alexis Hunted0530f2009-11-28 08:58:14 +00001911 // If we have an operator-function-id or a literal-operator-id and the next
1912 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00001913 //
1914 // template-id:
1915 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00001916 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1917 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00001918 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregor71395fa2009-11-04 00:56:37 +00001919 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1920 EnteringContext, ObjectType,
Douglas Gregorb22ee882010-05-05 05:58:24 +00001921 Result,
1922 TemplateSpecified, TemplateKWLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00001923
Douglas Gregor7861a802009-11-03 01:35:08 +00001924 return false;
1925 }
1926
Douglas Gregor411e5ac2010-01-11 23:29:10 +00001927 if (getLang().CPlusPlus &&
1928 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001929 // C++ [expr.unary.op]p10:
1930 // There is an ambiguity in the unary-expression ~X(), where X is a
1931 // class-name. The ambiguity is resolved in favor of treating ~ as a
1932 // unary complement rather than treating ~X as referring to a destructor.
1933
1934 // Parse the '~'.
1935 SourceLocation TildeLoc = ConsumeToken();
1936
1937 // Parse the class-name.
1938 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001939 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00001940 return true;
1941 }
1942
1943 // Parse the class-name (or template-name in a simple-template-id).
1944 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1945 SourceLocation ClassNameLoc = ConsumeToken();
1946
Douglas Gregorb22ee882010-05-05 05:58:24 +00001947 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00001948 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001949 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregorb22ee882010-05-05 05:58:24 +00001950 EnteringContext, ObjectType, Result,
1951 TemplateSpecified, TemplateKWLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001952 }
1953
Douglas Gregor7861a802009-11-03 01:35:08 +00001954 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00001955 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1956 ClassNameLoc, getCurScope(),
1957 SS, ObjectType,
1958 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00001959 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00001960 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00001961
Douglas Gregor7861a802009-11-03 01:35:08 +00001962 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00001963 return false;
1964 }
1965
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001966 Diag(Tok, diag::err_expected_unqualified_id)
1967 << getLang().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00001968 return true;
1969}
1970
Sebastian Redlbd150f42008-11-21 19:14:01 +00001971/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1972/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00001973///
Chris Lattner109faf22009-01-04 21:25:24 +00001974/// This method is called to parse the new expression after the optional :: has
1975/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1976/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00001977///
1978/// new-expression:
1979/// '::'[opt] 'new' new-placement[opt] new-type-id
1980/// new-initializer[opt]
1981/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1982/// new-initializer[opt]
1983///
1984/// new-placement:
1985/// '(' expression-list ')'
1986///
Sebastian Redl351bb782008-12-02 14:43:59 +00001987/// new-type-id:
1988/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00001989/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00001990///
1991/// new-declarator:
1992/// ptr-operator new-declarator[opt]
1993/// direct-new-declarator
1994///
Sebastian Redlbd150f42008-11-21 19:14:01 +00001995/// new-initializer:
1996/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001997/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00001998///
John McCalldadc5752010-08-24 06:29:42 +00001999ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002000Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2001 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2002 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002003
2004 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2005 // second form of new-expression. It can't be a new-type-id.
2006
Sebastian Redl511ed552008-11-25 22:21:31 +00002007 ExprVector PlacementArgs(Actions);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002008 SourceLocation PlacementLParen, PlacementRParen;
2009
Douglas Gregorf2753b32010-07-13 15:54:32 +00002010 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002011 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002012 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002013 if (Tok.is(tok::l_paren)) {
2014 // If it turns out to be a placement, we change the type location.
2015 PlacementLParen = ConsumeParen();
Sebastian Redl351bb782008-12-02 14:43:59 +00002016 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2017 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002018 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002019 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002020
2021 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redl351bb782008-12-02 14:43:59 +00002022 if (PlacementRParen.isInvalid()) {
2023 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002024 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002025 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002026
Sebastian Redl351bb782008-12-02 14:43:59 +00002027 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002028 // Reset the placement locations. There was no placement.
Douglas Gregorf2753b32010-07-13 15:54:32 +00002029 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002030 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002031 } else {
2032 // We still need the type.
2033 if (Tok.is(tok::l_paren)) {
Douglas Gregorf2753b32010-07-13 15:54:32 +00002034 TypeIdParens.setBegin(ConsumeParen());
Douglas Gregora3a020a2011-04-15 19:40:02 +00002035 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002036 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002037 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002038 ParseDeclarator(DeclaratorInfo);
Douglas Gregorf2753b32010-07-13 15:54:32 +00002039 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
2040 TypeIdParens.getBegin()));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002041 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002042 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002043 if (ParseCXXTypeSpecifierSeq(DS))
2044 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002045 else {
2046 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002047 ParseDeclaratorInternal(DeclaratorInfo,
2048 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002049 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002050 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002051 }
2052 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002053 // A new-type-id is a simplified type-id, where essentially the
2054 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002055 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002056 if (ParseCXXTypeSpecifierSeq(DS))
2057 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002058 else {
2059 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002060 ParseDeclaratorInternal(DeclaratorInfo,
2061 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002062 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002063 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002064 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002065 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002066 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002067 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002068
Sebastian Redl511ed552008-11-25 22:21:31 +00002069 ExprVector ConstructorArgs(Actions);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002070 SourceLocation ConstructorLParen, ConstructorRParen;
2071
2072 if (Tok.is(tok::l_paren)) {
2073 ConstructorLParen = ConsumeParen();
2074 if (Tok.isNot(tok::r_paren)) {
2075 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002076 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2077 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002078 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002079 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002080 }
2081 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redl351bb782008-12-02 14:43:59 +00002082 if (ConstructorRParen.isInvalid()) {
2083 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002084 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002085 }
Sebastian Redl3da34892011-06-05 12:23:16 +00002086 } else if (Tok.is(tok::l_brace)) {
2087 // FIXME: Have to communicate the init-list to ActOnCXXNew.
2088 ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002089 }
2090
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002091 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2092 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregorf2753b32010-07-13 15:54:32 +00002093 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002094 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002095}
2096
Sebastian Redlbd150f42008-11-21 19:14:01 +00002097/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2098/// passed to ParseDeclaratorInternal.
2099///
2100/// direct-new-declarator:
2101/// '[' expression ']'
2102/// direct-new-declarator '[' constant-expression ']'
2103///
Chris Lattner109faf22009-01-04 21:25:24 +00002104void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002105 // Parse the array dimensions.
2106 bool first = true;
2107 while (Tok.is(tok::l_square)) {
2108 SourceLocation LLoc = ConsumeBracket();
John McCalldadc5752010-08-24 06:29:42 +00002109 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002110 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002111 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002112 // Recover
2113 SkipUntil(tok::r_square);
2114 return;
2115 }
2116 first = false;
2117
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002118 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall084e83d2011-03-24 11:26:52 +00002119
2120 ParsedAttributes attrs(AttrFactory);
2121 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002122 /*static=*/false, /*star=*/false,
Douglas Gregor04318252009-07-06 15:59:29 +00002123 Size.release(), LLoc, RLoc),
John McCall084e83d2011-03-24 11:26:52 +00002124 attrs, RLoc);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002125
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002126 if (RLoc.isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002127 return;
2128 }
2129}
2130
2131/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2132/// This ambiguity appears in the syntax of the C++ new operator.
2133///
2134/// new-expression:
2135/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2136/// new-initializer[opt]
2137///
2138/// new-placement:
2139/// '(' expression-list ')'
2140///
John McCall37ad5512010-08-23 06:44:23 +00002141bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002142 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002143 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002144 // The '(' was already consumed.
2145 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002146 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002147 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002148 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002149 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002150 }
2151
2152 // It's not a type, it has to be an expression list.
2153 // Discard the comma locations - ActOnCXXNew has enough parameters.
2154 CommaLocsTy CommaLocs;
2155 return ParseExpressionList(PlacementArgs, CommaLocs);
2156}
2157
2158/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2159/// to free memory allocated by new.
2160///
Chris Lattner109faf22009-01-04 21:25:24 +00002161/// This method is called to parse the 'delete' expression after the optional
2162/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2163/// and "Start" is its location. Otherwise, "Start" is the location of the
2164/// 'delete' token.
2165///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002166/// delete-expression:
2167/// '::'[opt] 'delete' cast-expression
2168/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002169ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002170Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2171 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2172 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002173
2174 // Array delete?
2175 bool ArrayDelete = false;
2176 if (Tok.is(tok::l_square)) {
2177 ArrayDelete = true;
2178 SourceLocation LHS = ConsumeBracket();
2179 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
2180 if (RHS.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002181 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002182 }
2183
John McCalldadc5752010-08-24 06:29:42 +00002184 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002185 if (Operand.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002186 return move(Operand);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002187
John McCallb268a282010-08-23 23:25:46 +00002188 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002189}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002190
Mike Stump11289f42009-09-09 15:08:12 +00002191static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002192 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002193 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002194 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002195 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002196 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002197 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Alexis Huntf479f1b2011-05-09 18:22:59 +00002198 case tok::kw___has_trivial_constructor:
2199 return UTT_HasTrivialDefaultConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002200 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002201 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2202 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2203 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley65497cc2011-04-27 23:09:49 +00002204 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2205 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002206 case tok::kw___is_class: return UTT_IsClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002207 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2208 case tok::kw___is_compound: return UTT_IsCompound;
2209 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002210 case tok::kw___is_empty: return UTT_IsEmpty;
2211 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley65497cc2011-04-27 23:09:49 +00002212 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2213 case tok::kw___is_function: return UTT_IsFunction;
2214 case tok::kw___is_fundamental: return UTT_IsFundamental;
2215 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley65497cc2011-04-27 23:09:49 +00002216 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2217 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2218 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2219 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2220 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth79803482011-04-23 10:47:20 +00002221 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00002222 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002223 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley65497cc2011-04-27 23:09:49 +00002224 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002225 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley65497cc2011-04-27 23:09:49 +00002226 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley65497cc2011-04-27 23:09:49 +00002227 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2228 case tok::kw___is_scalar: return UTT_IsScalar;
2229 case tok::kw___is_signed: return UTT_IsSigned;
2230 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2231 case tok::kw___is_trivial: return UTT_IsTrivial;
Alexis Huntd9a5cc12011-05-13 00:31:07 +00002232 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002233 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley65497cc2011-04-27 23:09:49 +00002234 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2235 case tok::kw___is_void: return UTT_IsVoid;
2236 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002237 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002238}
2239
2240static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2241 switch(kind) {
Francois Pichet347c4c72010-12-07 00:55:57 +00002242 default: llvm_unreachable("Not a known binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00002243 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley65497cc2011-04-27 23:09:49 +00002244 case tok::kw___is_convertible: return BTT_IsConvertible;
2245 case tok::kw___is_same: return BTT_IsSame;
Francois Pichet34b21132010-12-08 22:35:30 +00002246 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor8006e762011-01-27 20:28:01 +00002247 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002248 }
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002249}
2250
John Wiegley6242b6a2011-04-28 00:16:57 +00002251static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2252 switch(kind) {
2253 default: llvm_unreachable("Not a known binary type trait");
2254 case tok::kw___array_rank: return ATT_ArrayRank;
2255 case tok::kw___array_extent: return ATT_ArrayExtent;
2256 }
2257}
2258
John Wiegleyf9f65842011-04-25 06:54:41 +00002259static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2260 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002261 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002262 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2263 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2264 }
2265}
2266
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002267/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2268/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2269/// templates.
2270///
2271/// primary-expression:
2272/// [GNU] unary-type-trait '(' type-id ')'
2273///
John McCalldadc5752010-08-24 06:29:42 +00002274ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002275 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2276 SourceLocation Loc = ConsumeToken();
2277
2278 SourceLocation LParen = Tok.getLocation();
2279 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2280 return ExprError();
2281
2282 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2283 // there will be cryptic errors about mismatched parentheses and missing
2284 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00002285 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002286
2287 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2288
Douglas Gregor220cac52009-02-18 17:45:20 +00002289 if (Ty.isInvalid())
2290 return ExprError();
2291
Douglas Gregor54e5b132010-09-09 16:14:44 +00002292 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002293}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002294
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002295/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2296/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2297/// templates.
2298///
2299/// primary-expression:
2300/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2301///
2302ExprResult Parser::ParseBinaryTypeTrait() {
2303 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2304 SourceLocation Loc = ConsumeToken();
2305
2306 SourceLocation LParen = Tok.getLocation();
2307 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2308 return ExprError();
2309
2310 TypeResult LhsTy = ParseTypeName();
2311 if (LhsTy.isInvalid()) {
2312 SkipUntil(tok::r_paren);
2313 return ExprError();
2314 }
2315
2316 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2317 SkipUntil(tok::r_paren);
2318 return ExprError();
2319 }
2320
2321 TypeResult RhsTy = ParseTypeName();
2322 if (RhsTy.isInvalid()) {
2323 SkipUntil(tok::r_paren);
2324 return ExprError();
2325 }
2326
2327 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2328
2329 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
2330}
2331
John Wiegley6242b6a2011-04-28 00:16:57 +00002332/// ParseArrayTypeTrait - Parse the built-in array type-trait
2333/// pseudo-functions.
2334///
2335/// primary-expression:
2336/// [Embarcadero] '__array_rank' '(' type-id ')'
2337/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2338///
2339ExprResult Parser::ParseArrayTypeTrait() {
2340 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2341 SourceLocation Loc = ConsumeToken();
2342
2343 SourceLocation LParen = Tok.getLocation();
2344 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2345 return ExprError();
2346
2347 TypeResult Ty = ParseTypeName();
2348 if (Ty.isInvalid()) {
2349 SkipUntil(tok::comma);
2350 SkipUntil(tok::r_paren);
2351 return ExprError();
2352 }
2353
2354 switch (ATT) {
2355 case ATT_ArrayRank: {
2356 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2357 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, RParen);
2358 }
2359 case ATT_ArrayExtent: {
2360 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2361 SkipUntil(tok::r_paren);
2362 return ExprError();
2363 }
2364
2365 ExprResult DimExpr = ParseExpression();
2366 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2367
2368 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), RParen);
2369 }
2370 default:
2371 break;
2372 }
2373 return ExprError();
2374}
2375
John Wiegleyf9f65842011-04-25 06:54:41 +00002376/// ParseExpressionTrait - Parse built-in expression-trait
2377/// pseudo-functions like __is_lvalue_expr( xxx ).
2378///
2379/// primary-expression:
2380/// [Embarcadero] expression-trait '(' expression ')'
2381///
2382ExprResult Parser::ParseExpressionTrait() {
2383 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2384 SourceLocation Loc = ConsumeToken();
2385
2386 SourceLocation LParen = Tok.getLocation();
2387 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2388 return ExprError();
2389
2390 ExprResult Expr = ParseExpression();
2391
2392 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2393
2394 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), RParen);
2395}
2396
2397
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002398/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2399/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2400/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002401ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002402Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002403 ParsedType &CastTy,
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002404 SourceLocation LParenLoc,
2405 SourceLocation &RParenLoc) {
2406 assert(getLang().CPlusPlus && "Should only be called for C++!");
2407 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2408 assert(isTypeIdInParens() && "Not a type-id!");
2409
John McCalldadc5752010-08-24 06:29:42 +00002410 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002411 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002412
2413 // We need to disambiguate a very ugly part of the C++ syntax:
2414 //
2415 // (T())x; - type-id
2416 // (T())*x; - type-id
2417 // (T())/x; - expression
2418 // (T()); - expression
2419 //
2420 // The bad news is that we cannot use the specialized tentative parser, since
2421 // it can only verify that the thing inside the parens can be parsed as
2422 // type-id, it is not useful for determining the context past the parens.
2423 //
2424 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002425 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002426 //
2427 // It uses a scheme similar to parsing inline methods. The parenthesized
2428 // tokens are cached, the context that follows is determined (possibly by
2429 // parsing a cast-expression), and then we re-introduce the cached tokens
2430 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002431
Mike Stump11289f42009-09-09 15:08:12 +00002432 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002433 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002434
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002435 // Store the tokens of the parentheses. We will parse them after we determine
2436 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002437 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002438 // We didn't find the ')' we expected.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002439 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2440 return ExprError();
2441 }
2442
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002443 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002444 ParseAs = CompoundLiteral;
2445 } else {
2446 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002447 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2448 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2449 NotCastExpr = true;
2450 } else {
2451 // Try parsing the cast-expression that may follow.
2452 // If it is not a cast-expression, NotCastExpr will be true and no token
2453 // will be consumed.
2454 Result = ParseCastExpression(false/*isUnaryExpression*/,
2455 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002456 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002457 // type-id has priority.
2458 true/*isTypeCast*/);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002459 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002460
2461 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2462 // an expression.
2463 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002464 }
2465
Mike Stump11289f42009-09-09 15:08:12 +00002466 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002467 Toks.push_back(Tok);
2468 // Re-enter the stored parenthesized tokens into the token stream, so we may
2469 // parse them now.
2470 PP.EnterTokenStream(Toks.data(), Toks.size(),
2471 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2472 // Drop the current token and bring the first cached one. It's the same token
2473 // as when we entered this function.
2474 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002475
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002476 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002477 // Parse the type declarator.
2478 DeclSpec DS(AttrFactory);
2479 ParseSpecifierQualifierList(DS);
2480 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2481 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002482
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002483 // Match the ')'.
2484 if (Tok.is(tok::r_paren))
2485 RParenLoc = ConsumeParen();
2486 else
2487 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002488
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002489 if (ParseAs == CompoundLiteral) {
2490 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002491 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002492 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2493 }
Mike Stump11289f42009-09-09 15:08:12 +00002494
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002495 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2496 assert(ParseAs == CastExpr);
2497
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002498 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002499 return ExprError();
2500
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002501 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002502 if (!Result.isInvalid())
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002503 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc,
2504 DeclaratorInfo, CastTy,
2505 RParenLoc, Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002506 return move(Result);
2507 }
Mike Stump11289f42009-09-09 15:08:12 +00002508
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002509 // Not a compound literal, and not followed by a cast-expression.
2510 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002511
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002512 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002513 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002514 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCallb268a282010-08-23 23:25:46 +00002515 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002516
2517 // Match the ')'.
2518 if (Result.isInvalid()) {
2519 SkipUntil(tok::r_paren);
2520 return ExprError();
2521 }
Mike Stump11289f42009-09-09 15:08:12 +00002522
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002523 if (Tok.is(tok::r_paren))
2524 RParenLoc = ConsumeParen();
2525 else
2526 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2527
2528 return move(Result);
2529}