blob: fe4bfc853eff85469db03f0a46bb36c7973399c6 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
Douglas Gregorae7902c2011-08-04 15:30:47 +000018#include "clang/Sema/Scope.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000020#include "llvm/Support/ErrorHandling.h"
21
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Richard Smithea698b32011-04-14 21:45:45 +000024static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
25 switch (Kind) {
26 case tok::kw_template: return 0;
27 case tok::kw_const_cast: return 1;
28 case tok::kw_dynamic_cast: return 2;
29 case tok::kw_reinterpret_cast: return 3;
30 case tok::kw_static_cast: return 4;
31 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000032 llvm_unreachable("Unknown type for digraph error message.");
Richard Smithea698b32011-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 Kyrtzidisa64ccef2011-09-19 20:40:19 +000040 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smithea698b32011-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 Kyrtzidisa64ccef2011-09-19 20:40:19 +000061 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smithea698b32011-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 Trieu950be712011-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 Trieuc11030e2011-09-20 20:03:50 +000077 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu950be712011-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 Stump1eb44332009-09-09 15:08:12 +000097/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000098///
99/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000100/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000101/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-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 Gregor2dd078a2009-09-02 22:59:36 +0000110/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000111///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000112///
Mike Stump1eb44332009-09-09 15:08:12 +0000113/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000114/// nested-name-specifier (or empty)
115///
Mike Stump1eb44332009-09-09 15:08:12 +0000116/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-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 Gregord4dca082010-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 Gregorb10cd042010-02-21 18:36:56 +0000131/// member access expression, e.g., the \p T:: in \p p->T::m.
132///
John McCall9ba61662010-02-26 08:45:28 +0000133/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000134bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000135 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000136 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000137 bool *MayBePseudoDestructor,
138 bool IsTypename) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000139 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000140 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000142 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +0000143 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
144 Tok.getAnnotationRange(),
145 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000146 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000147 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000148 }
Chris Lattnere607e802009-01-04 21:14:15 +0000149
Douglas Gregor39a8de12009-02-25 19:37:18 +0000150 bool HasScopeSpecifier = false;
151
Chris Lattner5b454732009-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 Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattner55a7cef2009-01-05 00:13:00 +0000158 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000159 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
160 return true;
161
Douglas Gregor39a8de12009-02-25 19:37:18 +0000162 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000163 }
164
Douglas Gregord4dca082010-02-24 18:44:31 +0000165 bool CheckForDestructor = false;
166 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
167 CheckForDestructor = true;
168 *MayBePseudoDestructor = false;
169 }
170
Douglas Gregor39a8de12009-02-25 19:37:18 +0000171 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000172 if (HasScopeSpecifier) {
173 // C++ [basic.lookup.classref]p5:
174 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000175 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000176 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000177 //
Douglas Gregor2dd078a2009-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 McCallb3d87482010-08-24 05:47:05 +0000183 ObjectType = ParsedType();
Douglas Gregor81b747b2009-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 Gregor23c94db2010-07-02 17:43:08 +0000188 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-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 Kyrtzidis7d100872011-09-04 03:32:15 +0000193 SS.setEndLoc(Tok.getLocation());
194 cutOffParsing();
195 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000196 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Douglas Gregor39a8de12009-02-25 19:37:18 +0000199 // nested-name-specifier:
Chris Lattner77cf72a2009-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 Gregor2dd078a2009-09-02 22:59:36 +0000205 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000206 // nested-name-specifier, since they aren't allowed to start with
207 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000208 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000209 break;
210
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000211 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000212 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000213
214 UnqualifiedId TemplateName;
215 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000216 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000217 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000218 ConsumeToken();
219 } else if (Tok.is(tok::kw_operator)) {
220 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000221 TemplateName)) {
222 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000223 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000224 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000225
Sean Hunte6252d12009-11-28 08:58:14 +0000226 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
227 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000228 Diag(TemplateName.getSourceRange().getBegin(),
229 diag::err_id_after_template_in_nested_name_spec)
230 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000231 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000232 break;
233 }
234 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000235 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000236 break;
237 }
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Douglas Gregor7bb87fc2009-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 Gregord6ab2322010-06-16 23:00:59 +0000249 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000250 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000251 TemplateKWLoc,
252 SS,
253 TemplateName,
254 ObjectType,
255 EnteringContext,
256 Template)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000257 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000258 TemplateKWLoc, false))
259 return true;
260 } else
John McCall9ba61662010-02-26 08:45:28 +0000261 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Chris Lattner77cf72a2009-06-26 03:47:46 +0000263 continue;
264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Douglas Gregor39a8de12009-02-25 19:37:18 +0000266 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000267 // We have
Douglas Gregor39a8de12009-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 Gregorc45c2322009-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 Kyrtzidis25a76762011-06-22 06:09:49 +0000274 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000275 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
276 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000277 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000278 }
279
Douglas Gregor6cd9d4a2011-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 Stump1eb44332009-09-09 15:08:12 +0000285
Douglas Gregor6cd9d4a2011-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 Lattner67b9e832009-06-26 03:45:46 +0000307 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000308
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000309 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000310 }
311
Chris Lattner5c7f7862009-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 Lattner46646492009-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 Gregorb10cd042010-02-21 18:36:56 +0000328 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000329 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
330 Tok.getLocation(),
331 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-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 Gregor849b2432010-03-31 17:46:05 +0000338 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000339
340 // Recover as if the user wrote '::'.
341 Next.setKind(tok::coloncolon);
342 }
Chris Lattner46646492009-12-07 01:36:53 +0000343 }
344
Chris Lattner5c7f7862009-06-26 03:52:38 +0000345 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000346 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000347 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000348 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000349 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000350 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000351 }
352
Chris Lattner5c7f7862009-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 Lattner46646492009-12-07 01:36:53 +0000356 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
357 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000358 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Douglas Gregor2e4c34a2011-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 Lattner5c7f7862009-06-26 03:52:38 +0000365 continue;
366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Richard Trieu950be712011-09-19 19:01:00 +0000368 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000369
Chris Lattner5c7f7862009-06-26 03:52:38 +0000370 // nested-name-specifier:
371 // type-name '<'
372 if (Next.is(tok::less)) {
373 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000374 UnqualifiedId TemplateName;
375 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000376 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000377 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000378 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000379 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000380 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000381 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000382 Template,
383 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-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 Gregorca1bdd72009-11-04 00:56:37 +0000390 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000391 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000392 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000393 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000394 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000395 }
396
397 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000398 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-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 Pichet4147d302011-03-27 19:41:34 +0000403 unsigned DiagID = diag::err_missing_dependent_template_keyword;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000404 if (getLang().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000405 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000406
407 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000408 << II.getName()
409 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
410
Douglas Gregord6ab2322010-06-16 23:00:59 +0000411 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000412 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000413 Tok.getLocation(), SS,
414 TemplateName, ObjectType,
415 EnteringContext, Template)) {
416 // Consume the identifier.
417 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000418 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000419 SourceLocation(), false))
420 return true;
421 }
422 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000423 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000424
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000425 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000426 }
427 }
428
Douglas Gregor39a8de12009-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 Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Douglas Gregord4dca082010-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 McCall9ba61662010-02-26 08:45:28 +0000440 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000441}
442
443/// ParseCXXIdExpression - Handle id-expression.
444///
445/// id-expression:
446/// unqualified-id
447/// qualified-id
448///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000449/// qualified-id:
450/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
451/// '::' identifier
452/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000453/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000454///
Argyrios Kyrtzidiseb83ecd2008-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 Redlebc07d52009-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 McCall60d7b3a2010-08-24 06:29:42 +0000485ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-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 McCallb3d87482010-08-24 05:47:05 +0000491 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000492
493 UnqualifiedId Name;
494 if (ParseUnqualifiedId(SS,
495 /*EnteringContext=*/false,
496 /*AllowDestructorName=*/false,
497 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000498 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000499 Name))
500 return ExprError();
John McCallb681b612009-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 McCall9c72c602010-08-27 09:08:28 +0000504 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
505 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000506
Douglas Gregor23c94db2010-07-02 17:43:08 +0000507 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000508 isAddressOfOperand);
509
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000510}
511
Douglas Gregorae7902c2011-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 '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000597 BalancedDelimiterTracker T(*this, tok::l_square);
598 T.consumeOpen();
599
600 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000601
602 bool first = true;
603
604 // Parse capture-default.
605 if (Tok.is(tok::amp) &&
606 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
607 Intro.Default = LCD_ByRef;
608 ConsumeToken();
609 first = false;
610 } else if (Tok.is(tok::equal)) {
611 Intro.Default = LCD_ByCopy;
612 ConsumeToken();
613 first = false;
614 }
615
616 while (Tok.isNot(tok::r_square)) {
617 if (!first) {
618 if (Tok.isNot(tok::comma))
619 return DiagResult(diag::err_expected_comma_or_rsquare);
620 ConsumeToken();
621 }
622
623 first = false;
624
625 // Parse capture.
626 LambdaCaptureKind Kind = LCK_ByCopy;
627 SourceLocation Loc;
628 IdentifierInfo* Id = 0;
629
630 if (Tok.is(tok::kw_this)) {
631 Kind = LCK_This;
632 Loc = ConsumeToken();
633 } else {
634 if (Tok.is(tok::amp)) {
635 Kind = LCK_ByRef;
636 ConsumeToken();
637 }
638
639 if (Tok.is(tok::identifier)) {
640 Id = Tok.getIdentifierInfo();
641 Loc = ConsumeToken();
642 } else if (Tok.is(tok::kw_this)) {
643 // FIXME: If we want to suggest a fixit here, will need to return more
644 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
645 // Clear()ed to prevent emission in case of tentative parsing?
646 return DiagResult(diag::err_this_captured_by_reference);
647 } else {
648 return DiagResult(diag::err_expected_capture);
649 }
650 }
651
652 Intro.addCapture(Kind, Loc, Id);
653 }
654
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000655 T.consumeClose();
656 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000657
658 return DiagResult();
659}
660
661/// TryParseLambdaExpression - Tentatively parse a lambda introducer.
662///
663/// Returns true if it hit something unexpected.
664bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
665 TentativeParsingAction PA(*this);
666
667 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
668
669 if (DiagID) {
670 PA.Revert();
671 return true;
672 }
673
674 PA.Commit();
675 return false;
676}
677
678/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
679/// expression.
680ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
681 LambdaIntroducer &Intro) {
Richard Smith7fe62082011-10-15 05:09:34 +0000682 Diag(Intro.Range.getBegin(), diag::warn_cxx98_compat_lambda);
683
Douglas Gregorae7902c2011-08-04 15:30:47 +0000684 // Parse lambda-declarator[opt].
685 DeclSpec DS(AttrFactory);
686 Declarator D(DS, Declarator::PrototypeContext);
687
688 if (Tok.is(tok::l_paren)) {
689 ParseScope PrototypeScope(this,
690 Scope::FunctionPrototypeScope |
691 Scope::DeclScope);
692
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000693 SourceLocation DeclLoc, DeclEndLoc;
694 BalancedDelimiterTracker T(*this, tok::l_paren);
695 T.consumeOpen();
696 DeclLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000697
698 // Parse parameter-declaration-clause.
699 ParsedAttributes Attr(AttrFactory);
700 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
701 SourceLocation EllipsisLoc;
702
703 if (Tok.isNot(tok::r_paren))
704 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
705
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000706 T.consumeClose();
707 DeclEndLoc = T.getCloseLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000708
709 // Parse 'mutable'[opt].
710 SourceLocation MutableLoc;
711 if (Tok.is(tok::kw_mutable)) {
712 MutableLoc = ConsumeToken();
713 DeclEndLoc = MutableLoc;
714 }
715
716 // Parse exception-specification[opt].
717 ExceptionSpecificationType ESpecType = EST_None;
718 SourceRange ESpecRange;
719 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
720 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
721 ExprResult NoexceptExpr;
722 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
723 DynamicExceptions,
724 DynamicExceptionRanges,
725 NoexceptExpr);
726
727 if (ESpecType != EST_None)
728 DeclEndLoc = ESpecRange.getEnd();
729
730 // Parse attribute-specifier[opt].
731 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
732
733 // Parse trailing-return-type[opt].
734 ParsedType TrailingReturnType;
735 if (Tok.is(tok::arrow)) {
736 SourceRange Range;
737 TrailingReturnType = ParseTrailingReturnType(Range).get();
738 if (Range.getEnd().isValid())
739 DeclEndLoc = Range.getEnd();
740 }
741
742 PrototypeScope.Exit();
743
744 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
745 /*isVariadic=*/EllipsisLoc.isValid(),
746 EllipsisLoc,
747 ParamInfo.data(), ParamInfo.size(),
748 DS.getTypeQualifiers(),
749 /*RefQualifierIsLValueRef=*/true,
750 /*RefQualifierLoc=*/SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +0000751 /*ConstQualifierLoc=*/SourceLocation(),
752 /*VolatileQualifierLoc=*/SourceLocation(),
Douglas Gregorae7902c2011-08-04 15:30:47 +0000753 MutableLoc,
754 ESpecType, ESpecRange.getBegin(),
755 DynamicExceptions.data(),
756 DynamicExceptionRanges.data(),
757 DynamicExceptions.size(),
758 NoexceptExpr.isUsable() ?
759 NoexceptExpr.get() : 0,
760 DeclLoc, DeclEndLoc, D,
761 TrailingReturnType),
762 Attr, DeclEndLoc);
763 }
764
765 // Parse compound-statement.
766 if (Tok.is(tok::l_brace)) {
767 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
768 // it.
769 ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
770 Scope::BreakScope | Scope::ContinueScope |
771 Scope::DeclScope);
772
773 StmtResult Stmt(ParseCompoundStatementBody());
774
775 BodyScope.Exit();
776 } else {
777 Diag(Tok, diag::err_expected_lambda_body);
778 }
779
780 return ExprEmpty();
781}
782
Reid Spencer5f016e22007-07-11 17:01:13 +0000783/// ParseCXXCasts - This handles the various ways to cast expressions to another
784/// type.
785///
786/// postfix-expression: [C++ 5.2p1]
787/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
788/// 'static_cast' '<' type-name '>' '(' expression ')'
789/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
790/// 'const_cast' '<' type-name '>' '(' expression ')'
791///
John McCall60d7b3a2010-08-24 06:29:42 +0000792ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 tok::TokenKind Kind = Tok.getKind();
794 const char *CastName = 0; // For error messages
795
796 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000797 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 case tok::kw_const_cast: CastName = "const_cast"; break;
799 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
800 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
801 case tok::kw_static_cast: CastName = "static_cast"; break;
802 }
803
804 SourceLocation OpLoc = ConsumeToken();
805 SourceLocation LAngleBracketLoc = Tok.getLocation();
806
Richard Smithea698b32011-04-14 21:45:45 +0000807 // Check for "<::" which is parsed as "[:". If found, fix token stream,
808 // diagnose error, suggest fix, and recover parsing.
809 Token Next = NextToken();
810 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
811 AreTokensAdjacent(PP, Tok, Next))
812 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
813
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000815 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000816
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000817 // Parse the common declaration-specifiers piece.
818 DeclSpec DS(AttrFactory);
819 ParseSpecifierQualifierList(DS);
820
821 // Parse the abstract-declarator, if present.
822 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
823 ParseDeclarator(DeclaratorInfo);
824
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 SourceLocation RAngleBracketLoc = Tok.getLocation();
826
Chris Lattner1ab3b962008-11-18 07:48:38 +0000827 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000828 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000829
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000830 SourceLocation LParenLoc, RParenLoc;
831 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +0000832
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000833 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000834 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000835
John McCall60d7b3a2010-08-24 06:29:42 +0000836 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000838 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000839 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +0000840
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000841 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +0000842 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000843 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +0000844 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000845 T.getOpenLocation(), Result.take(),
846 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000847
Sebastian Redl20df9b72008-12-11 22:51:44 +0000848 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000849}
850
Sebastian Redlc42e1182008-11-11 11:37:55 +0000851/// ParseCXXTypeid - This handles the C++ typeid expression.
852///
853/// postfix-expression: [C++ 5.2p1]
854/// 'typeid' '(' expression ')'
855/// 'typeid' '(' type-id ')'
856///
John McCall60d7b3a2010-08-24 06:29:42 +0000857ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000858 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
859
860 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000861 SourceLocation LParenLoc, RParenLoc;
862 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000863
864 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000865 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000866 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000867 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000868
John McCall60d7b3a2010-08-24 06:29:42 +0000869 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000870
871 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000872 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000873
874 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000875 T.consumeClose();
876 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000877 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000878 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000879
880 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000881 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000882 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000883 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000884 // When typeid is applied to an expression other than an lvalue of a
885 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000886 // operand (Clause 5).
887 //
Mike Stump1eb44332009-09-09 15:08:12 +0000888 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000889 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000890 // we the expression is potentially potentially evaluated.
891 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000892 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000893 Result = ParseExpression();
894
895 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000896 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000897 SkipUntil(tok::r_paren);
898 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000899 T.consumeClose();
900 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000901 if (RParenLoc.isInvalid())
902 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000903
Sebastian Redlc42e1182008-11-11 11:37:55 +0000904 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000905 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000906 }
907 }
908
Sebastian Redl20df9b72008-12-11 22:51:44 +0000909 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000910}
911
Francois Pichet01b7c302010-09-08 12:20:18 +0000912/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
913///
914/// '__uuidof' '(' expression ')'
915/// '__uuidof' '(' type-id ')'
916///
917ExprResult Parser::ParseCXXUuidof() {
918 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
919
920 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000921 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +0000922
923 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000924 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +0000925 return ExprError();
926
927 ExprResult Result;
928
929 if (isTypeIdInParens()) {
930 TypeResult Ty = ParseTypeName();
931
932 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000933 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +0000934
935 if (Ty.isInvalid())
936 return ExprError();
937
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000938 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
939 Ty.get().getAsOpaquePtr(),
940 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +0000941 } else {
942 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
943 Result = ParseExpression();
944
945 // Match the ')'.
946 if (Result.isInvalid())
947 SkipUntil(tok::r_paren);
948 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000949 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +0000950
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000951 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
952 /*isType=*/false,
953 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +0000954 }
955 }
956
957 return move(Result);
958}
959
Douglas Gregord4dca082010-02-24 18:44:31 +0000960/// \brief Parse a C++ pseudo-destructor expression after the base,
961/// . or -> operator, and nested-name-specifier have already been
962/// parsed.
963///
964/// postfix-expression: [C++ 5.2]
965/// postfix-expression . pseudo-destructor-name
966/// postfix-expression -> pseudo-destructor-name
967///
968/// pseudo-destructor-name:
969/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
970/// ::[opt] nested-name-specifier template simple-template-id ::
971/// ~type-name
972/// ::[opt] nested-name-specifier[opt] ~type-name
973///
John McCall60d7b3a2010-08-24 06:29:42 +0000974ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000975Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
976 tok::TokenKind OpKind,
977 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000978 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000979 // We're parsing either a pseudo-destructor-name or a dependent
980 // member access that has the same form as a
981 // pseudo-destructor-name. We parse both in the same way and let
982 // the action model sort them out.
983 //
984 // Note that the ::[opt] nested-name-specifier[opt] has already
985 // been parsed, and if there was a simple-template-id, it has
986 // been coalesced into a template-id annotation token.
987 UnqualifiedId FirstTypeName;
988 SourceLocation CCLoc;
989 if (Tok.is(tok::identifier)) {
990 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
991 ConsumeToken();
992 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
993 CCLoc = ConsumeToken();
994 } else if (Tok.is(tok::annot_template_id)) {
995 FirstTypeName.setTemplateId(
996 (TemplateIdAnnotation *)Tok.getAnnotationValue());
997 ConsumeToken();
998 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
999 CCLoc = ConsumeToken();
1000 } else {
1001 FirstTypeName.setIdentifier(0, SourceLocation());
1002 }
1003
1004 // Parse the tilde.
1005 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1006 SourceLocation TildeLoc = ConsumeToken();
1007 if (!Tok.is(tok::identifier)) {
1008 Diag(Tok, diag::err_destructor_tilde_identifier);
1009 return ExprError();
1010 }
1011
1012 // Parse the second type.
1013 UnqualifiedId SecondTypeName;
1014 IdentifierInfo *Name = Tok.getIdentifierInfo();
1015 SourceLocation NameLoc = ConsumeToken();
1016 SecondTypeName.setIdentifier(Name, NameLoc);
1017
1018 // If there is a '<', the second type name is a template-id. Parse
1019 // it as such.
1020 if (Tok.is(tok::less) &&
1021 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001022 SecondTypeName, /*AssumeTemplateName=*/true,
1023 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +00001024 return ExprError();
1025
John McCall9ae2f072010-08-23 23:25:46 +00001026 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1027 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001028 SS, FirstTypeName, CCLoc,
1029 TildeLoc, SecondTypeName,
1030 Tok.is(tok::l_paren));
1031}
1032
Reid Spencer5f016e22007-07-11 17:01:13 +00001033/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1034///
1035/// boolean-literal: [C++ 2.13.5]
1036/// 'true'
1037/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001038ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001040 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001041}
Chris Lattner50dd2892008-02-26 00:51:44 +00001042
1043/// ParseThrowExpression - This handles the C++ throw expression.
1044///
1045/// throw-expression: [C++ 15]
1046/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001047ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001048 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001049 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001050
Chris Lattner2a2819a2008-04-06 06:02:23 +00001051 // If the current token isn't the start of an assignment-expression,
1052 // then the expression is not present. This handles things like:
1053 // "C ? throw : (void)42", which is crazy but legal.
1054 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1055 case tok::semi:
1056 case tok::r_paren:
1057 case tok::r_square:
1058 case tok::r_brace:
1059 case tok::colon:
1060 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001061 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001062
Chris Lattner2a2819a2008-04-06 06:02:23 +00001063 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001064 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +00001065 if (Expr.isInvalid()) return move(Expr);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001066 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001067 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001068}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001069
1070/// ParseCXXThis - This handles the C++ 'this' pointer.
1071///
1072/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1073/// a non-lvalue expression whose value is the address of the object for which
1074/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001075ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001076 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1077 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001078 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001079}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001080
1081/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1082/// Can be interpreted either as function-style casting ("int(x)")
1083/// or class type construction ("ClassType(x,y,z)")
1084/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001085/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001086///
1087/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001088/// simple-type-specifier '(' expression-list[opt] ')'
1089/// [C++0x] simple-type-specifier braced-init-list
1090/// typename-specifier '(' expression-list[opt] ')'
1091/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001092///
John McCall60d7b3a2010-08-24 06:29:42 +00001093ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001094Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001095 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001096 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001097
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001098 assert((Tok.is(tok::l_paren) ||
1099 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1100 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001101
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001102 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001103
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001104 // FIXME: Convert to a proper type construct expression.
1105 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001106
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001107 } else {
1108 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1109
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001110 BalancedDelimiterTracker T(*this, tok::l_paren);
1111 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001112
1113 ExprVector Exprs(Actions);
1114 CommaLocsTy CommaLocs;
1115
1116 if (Tok.isNot(tok::r_paren)) {
1117 if (ParseExpressionList(Exprs, CommaLocs)) {
1118 SkipUntil(tok::r_paren);
1119 return ExprError();
1120 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001121 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001122
1123 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001124 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001125
1126 // TypeRep could be null, if it references an invalid typedef.
1127 if (!TypeRep)
1128 return ExprError();
1129
1130 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1131 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001132 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
1133 move_arg(Exprs),
1134 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001135 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001136}
1137
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001138/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001139///
1140/// condition:
1141/// expression
1142/// type-specifier-seq declarator '=' assignment-expression
1143/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1144/// '=' assignment-expression
1145///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001146/// \param ExprResult if the condition was parsed as an expression, the
1147/// parsed expression.
1148///
1149/// \param DeclResult if the condition was parsed as a declaration, the
1150/// parsed declaration.
1151///
Douglas Gregor586596f2010-05-06 17:25:47 +00001152/// \param Loc The location of the start of the statement that requires this
1153/// condition, e.g., the "for" in a for loop.
1154///
1155/// \param ConvertToBoolean Whether the condition expression should be
1156/// converted to a boolean value.
1157///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001158/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001159bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1160 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001161 SourceLocation Loc,
1162 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001163 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001164 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001165 cutOffParsing();
1166 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001167 }
1168
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001169 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001170 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001171 ExprOut = ParseExpression(); // expression
1172 DeclOut = 0;
1173 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001174 return true;
1175
1176 // If required, convert to a boolean value.
1177 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001178 ExprOut
1179 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1180 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001181 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001182
1183 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001184 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001185 ParseSpecifierQualifierList(DS);
1186
1187 // declarator
1188 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1189 ParseDeclarator(DeclaratorInfo);
1190
1191 // simple-asm-expr[opt]
1192 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001193 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001194 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001195 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001196 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001197 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001198 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001199 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001200 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001201 }
1202
1203 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001204 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001205
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001206 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001207 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001208 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001209 DeclOut = Dcl.get();
1210 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001211
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001212 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001213 if (isTokenEqualOrMistypedEqualEqual(
1214 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001215 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001216 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001217 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +00001218 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1219 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001220 } else {
1221 // FIXME: C++0x allows a braced-init-list
1222 Diag(Tok, diag::err_expected_equal_after_declarator);
1223 }
1224
Douglas Gregor586596f2010-05-06 17:25:47 +00001225 // FIXME: Build a reference to this declaration? Convert it to bool?
1226 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001227
1228 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001229
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001230 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001231}
1232
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001233/// \brief Determine whether the current token starts a C++
1234/// simple-type-specifier.
1235bool Parser::isCXXSimpleTypeSpecifier() const {
1236 switch (Tok.getKind()) {
1237 case tok::annot_typename:
1238 case tok::kw_short:
1239 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001240 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001241 case tok::kw_signed:
1242 case tok::kw_unsigned:
1243 case tok::kw_void:
1244 case tok::kw_char:
1245 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001246 case tok::kw_half:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001247 case tok::kw_float:
1248 case tok::kw_double:
1249 case tok::kw_wchar_t:
1250 case tok::kw_char16_t:
1251 case tok::kw_char32_t:
1252 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +00001253 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001254 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +00001255 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001256 return true;
1257
1258 default:
1259 break;
1260 }
1261
1262 return false;
1263}
1264
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001265/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1266/// This should only be called when the current token is known to be part of
1267/// simple-type-specifier.
1268///
1269/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001270/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001271/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1272/// char
1273/// wchar_t
1274/// bool
1275/// short
1276/// int
1277/// long
1278/// signed
1279/// unsigned
1280/// float
1281/// double
1282/// void
1283/// [GNU] typeof-specifier
1284/// [C++0x] auto [TODO]
1285///
1286/// type-name:
1287/// class-name
1288/// enum-name
1289/// typedef-name
1290///
1291void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1292 DS.SetRangeStart(Tok.getLocation());
1293 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001294 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001295 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001297 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001298 case tok::identifier: // foo::bar
1299 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001300 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001301 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001302 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001303
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001304 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001305 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001306 if (getTypeAnnotation(Tok))
1307 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1308 getTypeAnnotation(Tok));
1309 else
1310 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001311
1312 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1313 ConsumeToken();
1314
1315 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1316 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1317 // Objective-C interface. If we don't have Objective-C or a '<', this is
1318 // just a normal reference to a typedef name.
1319 if (Tok.is(tok::less) && getLang().ObjC1)
1320 ParseObjCProtocolQualifiers(DS);
1321
1322 DS.Finish(Diags, PP);
1323 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001324 }
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001326 // builtin types
1327 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001328 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001329 break;
1330 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001331 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001332 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001333 case tok::kw___int64:
1334 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1335 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001336 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001337 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001338 break;
1339 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001340 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001341 break;
1342 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001343 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001344 break;
1345 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001346 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001347 break;
1348 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001349 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001350 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001351 case tok::kw_half:
1352 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1353 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001354 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001355 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001356 break;
1357 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001358 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001359 break;
1360 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001361 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001362 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001363 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001364 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001365 break;
1366 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001367 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001368 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001369 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001370 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001371 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001373 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001374 // GNU typeof support.
1375 case tok::kw_typeof:
1376 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001377 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001378 return;
1379 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001380 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001381 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1382 else
1383 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001384 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001385 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001386}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001387
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001388/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1389/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1390/// e.g., "const short int". Note that the DeclSpec is *not* finished
1391/// by parsing the type-specifier-seq, because these sequences are
1392/// typically followed by some form of declarator. Returns true and
1393/// emits diagnostics if this is not a type-specifier-seq, false
1394/// otherwise.
1395///
1396/// type-specifier-seq: [C++ 8.1]
1397/// type-specifier type-specifier-seq[opt]
1398///
1399bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1400 DS.SetRangeStart(Tok.getLocation());
1401 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001402 unsigned DiagID;
1403 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001404
1405 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001406 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1407 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001408 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001409 return true;
1410 }
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Sebastian Redld9bafa72010-02-03 21:21:43 +00001412 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1413 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1414 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001415
Douglas Gregor396a9f22010-02-24 23:13:13 +00001416 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001417 return false;
1418}
1419
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001420/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1421/// some form.
1422///
1423/// This routine is invoked when a '<' is encountered after an identifier or
1424/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1425/// whether the unqualified-id is actually a template-id. This routine will
1426/// then parse the template arguments and form the appropriate template-id to
1427/// return to the caller.
1428///
1429/// \param SS the nested-name-specifier that precedes this template-id, if
1430/// we're actually parsing a qualified-id.
1431///
1432/// \param Name for constructor and destructor names, this is the actual
1433/// identifier that may be a template-name.
1434///
1435/// \param NameLoc the location of the class-name in a constructor or
1436/// destructor.
1437///
1438/// \param EnteringContext whether we're entering the scope of the
1439/// nested-name-specifier.
1440///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001441/// \param ObjectType if this unqualified-id occurs within a member access
1442/// expression, the type of the base object whose member is being accessed.
1443///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001444/// \param Id as input, describes the template-name or operator-function-id
1445/// that precedes the '<'. If template arguments were parsed successfully,
1446/// will be updated with the template-id.
1447///
Douglas Gregord4dca082010-02-24 18:44:31 +00001448/// \param AssumeTemplateId When true, this routine will assume that the name
1449/// refers to a template without performing name lookup to verify.
1450///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001451/// \returns true if a parse error occurred, false otherwise.
1452bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1453 IdentifierInfo *Name,
1454 SourceLocation NameLoc,
1455 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001456 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001457 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001458 bool AssumeTemplateId,
1459 SourceLocation TemplateKWLoc) {
1460 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1461 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001462
1463 TemplateTy Template;
1464 TemplateNameKind TNK = TNK_Non_template;
1465 switch (Id.getKind()) {
1466 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001467 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001468 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001469 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001470 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001471 Id, ObjectType, EnteringContext,
1472 Template);
1473 if (TNK == TNK_Non_template)
1474 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001475 } else {
1476 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001477 TNK = Actions.isTemplateName(getCurScope(), SS,
1478 TemplateKWLoc.isValid(), Id,
1479 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001480 MemberOfUnknownSpecialization);
1481
1482 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1483 ObjectType && IsTemplateArgumentList()) {
1484 // We have something like t->getAs<T>(), where getAs is a
1485 // member of an unknown specialization. However, this will only
1486 // parse correctly as a template, so suggest the keyword 'template'
1487 // before 'getAs' and treat this as a dependent template name.
1488 std::string Name;
1489 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1490 Name = Id.Identifier->getName();
1491 else {
1492 Name = "operator ";
1493 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1494 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1495 else
1496 Name += Id.Identifier->getName();
1497 }
1498 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1499 << Name
1500 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001501 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001502 SS, Id, ObjectType,
1503 EnteringContext, Template);
1504 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001505 return true;
1506 }
1507 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001508 break;
1509
Douglas Gregor014e88d2009-11-03 23:16:33 +00001510 case UnqualifiedId::IK_ConstructorName: {
1511 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001512 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001513 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001514 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1515 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001516 EnteringContext, Template,
1517 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001518 break;
1519 }
1520
Douglas Gregor014e88d2009-11-03 23:16:33 +00001521 case UnqualifiedId::IK_DestructorName: {
1522 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001523 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001524 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001525 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001526 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001527 TemplateName, ObjectType,
1528 EnteringContext, Template);
1529 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001530 return true;
1531 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001532 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1533 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001534 EnteringContext, Template,
1535 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001536
John McCallb3d87482010-08-24 05:47:05 +00001537 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001538 Diag(NameLoc, diag::err_destructor_template_id)
1539 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001540 return true;
1541 }
1542 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001543 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001544 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001545
1546 default:
1547 return false;
1548 }
1549
1550 if (TNK == TNK_Non_template)
1551 return false;
1552
1553 // Parse the enclosed template argument list.
1554 SourceLocation LAngleLoc, RAngleLoc;
1555 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001556 if (Tok.is(tok::less) &&
1557 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001558 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001559 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001560 RAngleLoc))
1561 return true;
1562
1563 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001564 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1565 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001566 // Form a parsed representation of the template-id to be stored in the
1567 // UnqualifiedId.
1568 TemplateIdAnnotation *TemplateId
1569 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1570
1571 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1572 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001573 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001574 TemplateId->TemplateNameLoc = Id.StartLocation;
1575 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001576 TemplateId->Name = 0;
1577 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1578 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001579 }
1580
Douglas Gregor059101f2011-03-02 00:47:37 +00001581 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001582 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001583 TemplateId->Kind = TNK;
1584 TemplateId->LAngleLoc = LAngleLoc;
1585 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001586 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001587 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001588 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001589 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001590
1591 Id.setTemplateId(TemplateId);
1592 return false;
1593 }
1594
1595 // Bundle the template arguments together.
1596 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001597 TemplateArgs.size());
1598
1599 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001600 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001601 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001602 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001603 RAngleLoc);
1604 if (Type.isInvalid())
1605 return true;
1606
1607 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1608 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1609 else
1610 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1611
1612 return false;
1613}
1614
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001615/// \brief Parse an operator-function-id or conversion-function-id as part
1616/// of a C++ unqualified-id.
1617///
1618/// This routine is responsible only for parsing the operator-function-id or
1619/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001620///
1621/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001622/// operator-function-id: [C++ 13.5]
1623/// 'operator' operator
1624///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001625/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001626/// new delete new[] delete[]
1627/// + - * / % ^ & | ~
1628/// ! = < > += -= *= /= %=
1629/// ^= &= |= << >> >>= <<= == !=
1630/// <= >= && || ++ -- , ->* ->
1631/// () []
1632///
1633/// conversion-function-id: [C++ 12.3.2]
1634/// operator conversion-type-id
1635///
1636/// conversion-type-id:
1637/// type-specifier-seq conversion-declarator[opt]
1638///
1639/// conversion-declarator:
1640/// ptr-operator conversion-declarator[opt]
1641/// \endcode
1642///
1643/// \param The nested-name-specifier that preceded this unqualified-id. If
1644/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1645///
1646/// \param EnteringContext whether we are entering the scope of the
1647/// nested-name-specifier.
1648///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001649/// \param ObjectType if this unqualified-id occurs within a member access
1650/// expression, the type of the base object whose member is being accessed.
1651///
1652/// \param Result on a successful parse, contains the parsed unqualified-id.
1653///
1654/// \returns true if parsing fails, false otherwise.
1655bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001656 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001657 UnqualifiedId &Result) {
1658 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1659
1660 // Consume the 'operator' keyword.
1661 SourceLocation KeywordLoc = ConsumeToken();
1662
1663 // Determine what kind of operator name we have.
1664 unsigned SymbolIdx = 0;
1665 SourceLocation SymbolLocations[3];
1666 OverloadedOperatorKind Op = OO_None;
1667 switch (Tok.getKind()) {
1668 case tok::kw_new:
1669 case tok::kw_delete: {
1670 bool isNew = Tok.getKind() == tok::kw_new;
1671 // Consume the 'new' or 'delete'.
1672 SymbolLocations[SymbolIdx++] = ConsumeToken();
1673 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001674 // Consume the '[' and ']'.
1675 BalancedDelimiterTracker T(*this, tok::l_square);
1676 T.consumeOpen();
1677 T.consumeClose();
1678 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001679 return true;
1680
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001681 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1682 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001683 Op = isNew? OO_Array_New : OO_Array_Delete;
1684 } else {
1685 Op = isNew? OO_New : OO_Delete;
1686 }
1687 break;
1688 }
1689
1690#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1691 case tok::Token: \
1692 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1693 Op = OO_##Name; \
1694 break;
1695#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1696#include "clang/Basic/OperatorKinds.def"
1697
1698 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001699 // Consume the '(' and ')'.
1700 BalancedDelimiterTracker T(*this, tok::l_paren);
1701 T.consumeOpen();
1702 T.consumeClose();
1703 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001704 return true;
1705
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001706 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1707 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001708 Op = OO_Call;
1709 break;
1710 }
1711
1712 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001713 // Consume the '[' and ']'.
1714 BalancedDelimiterTracker T(*this, tok::l_square);
1715 T.consumeOpen();
1716 T.consumeClose();
1717 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001718 return true;
1719
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001720 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1721 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001722 Op = OO_Subscript;
1723 break;
1724 }
1725
1726 case tok::code_completion: {
1727 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001728 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001729 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001730 // Don't try to parse any further.
1731 return true;
1732 }
1733
1734 default:
1735 break;
1736 }
1737
1738 if (Op != OO_None) {
1739 // We have parsed an operator-function-id.
1740 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1741 return false;
1742 }
Sean Hunt0486d742009-11-28 04:44:28 +00001743
1744 // Parse a literal-operator-id.
1745 //
1746 // literal-operator-id: [C++0x 13.5.8]
1747 // operator "" identifier
1748
1749 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
Richard Smith7fe62082011-10-15 05:09:34 +00001750 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001751 if (Tok.getLength() != 2)
1752 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1753 ConsumeStringToken();
1754
1755 if (Tok.isNot(tok::identifier)) {
1756 Diag(Tok.getLocation(), diag::err_expected_ident);
1757 return true;
1758 }
1759
1760 IdentifierInfo *II = Tok.getIdentifierInfo();
1761 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001762 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001763 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001764
1765 // Parse a conversion-function-id.
1766 //
1767 // conversion-function-id: [C++ 12.3.2]
1768 // operator conversion-type-id
1769 //
1770 // conversion-type-id:
1771 // type-specifier-seq conversion-declarator[opt]
1772 //
1773 // conversion-declarator:
1774 // ptr-operator conversion-declarator[opt]
1775
1776 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001777 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001778 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001779 return true;
1780
1781 // Parse the conversion-declarator, which is merely a sequence of
1782 // ptr-operators.
1783 Declarator D(DS, Declarator::TypeNameContext);
1784 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1785
1786 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001787 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001788 if (Ty.isInvalid())
1789 return true;
1790
1791 // Note that this is a conversion-function-id.
1792 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1793 D.getSourceRange().getEnd());
1794 return false;
1795}
1796
1797/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1798/// name of an entity.
1799///
1800/// \code
1801/// unqualified-id: [C++ expr.prim.general]
1802/// identifier
1803/// operator-function-id
1804/// conversion-function-id
1805/// [C++0x] literal-operator-id [TODO]
1806/// ~ class-name
1807/// template-id
1808///
1809/// \endcode
1810///
1811/// \param The nested-name-specifier that preceded this unqualified-id. If
1812/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1813///
1814/// \param EnteringContext whether we are entering the scope of the
1815/// nested-name-specifier.
1816///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001817/// \param AllowDestructorName whether we allow parsing of a destructor name.
1818///
1819/// \param AllowConstructorName whether we allow parsing a constructor name.
1820///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001821/// \param ObjectType if this unqualified-id occurs within a member access
1822/// expression, the type of the base object whose member is being accessed.
1823///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001824/// \param Result on a successful parse, contains the parsed unqualified-id.
1825///
1826/// \returns true if parsing fails, false otherwise.
1827bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1828 bool AllowDestructorName,
1829 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001830 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001831 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001832
1833 // Handle 'A::template B'. This is for template-ids which have not
1834 // already been annotated by ParseOptionalCXXScopeSpecifier().
1835 bool TemplateSpecified = false;
1836 SourceLocation TemplateKWLoc;
1837 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1838 (ObjectType || SS.isSet())) {
1839 TemplateSpecified = true;
1840 TemplateKWLoc = ConsumeToken();
1841 }
1842
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001843 // unqualified-id:
1844 // identifier
1845 // template-id (when it hasn't already been annotated)
1846 if (Tok.is(tok::identifier)) {
1847 // Consume the identifier.
1848 IdentifierInfo *Id = Tok.getIdentifierInfo();
1849 SourceLocation IdLoc = ConsumeToken();
1850
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001851 if (!getLang().CPlusPlus) {
1852 // If we're not in C++, only identifiers matter. Record the
1853 // identifier and return.
1854 Result.setIdentifier(Id, IdLoc);
1855 return false;
1856 }
1857
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001858 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001859 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001860 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001861 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001862 &SS, false, false,
1863 ParsedType(),
1864 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001865 IdLoc, IdLoc);
1866 } else {
1867 // We have parsed an identifier.
1868 Result.setIdentifier(Id, IdLoc);
1869 }
1870
1871 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001872 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001873 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001874 ObjectType, Result,
1875 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001876
1877 return false;
1878 }
1879
1880 // unqualified-id:
1881 // template-id (already parsed and annotated)
1882 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001883 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001884
1885 // If the template-name names the current class, then this is a constructor
1886 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001887 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001888 if (SS.isSet()) {
1889 // C++ [class.qual]p2 specifies that a qualified template-name
1890 // is taken as the constructor name where a constructor can be
1891 // declared. Thus, the template arguments are extraneous, so
1892 // complain about them and remove them entirely.
1893 Diag(TemplateId->TemplateNameLoc,
1894 diag::err_out_of_line_constructor_template_id)
1895 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001896 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001897 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1898 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1899 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001900 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001901 &SS, false, false,
1902 ParsedType(),
1903 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001904 TemplateId->TemplateNameLoc,
1905 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001906 ConsumeToken();
1907 return false;
1908 }
1909
1910 Result.setConstructorTemplateId(TemplateId);
1911 ConsumeToken();
1912 return false;
1913 }
1914
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001915 // We have already parsed a template-id; consume the annotation token as
1916 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001917 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001918 ConsumeToken();
1919 return false;
1920 }
1921
1922 // unqualified-id:
1923 // operator-function-id
1924 // conversion-function-id
1925 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001926 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001927 return true;
1928
Sean Hunte6252d12009-11-28 08:58:14 +00001929 // If we have an operator-function-id or a literal-operator-id and the next
1930 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001931 //
1932 // template-id:
1933 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001934 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1935 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001936 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001937 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1938 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001939 Result,
1940 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001941
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001942 return false;
1943 }
1944
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001945 if (getLang().CPlusPlus &&
1946 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001947 // C++ [expr.unary.op]p10:
1948 // There is an ambiguity in the unary-expression ~X(), where X is a
1949 // class-name. The ambiguity is resolved in favor of treating ~ as a
1950 // unary complement rather than treating ~X as referring to a destructor.
1951
1952 // Parse the '~'.
1953 SourceLocation TildeLoc = ConsumeToken();
1954
1955 // Parse the class-name.
1956 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001957 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001958 return true;
1959 }
1960
1961 // Parse the class-name (or template-name in a simple-template-id).
1962 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1963 SourceLocation ClassNameLoc = ConsumeToken();
1964
Douglas Gregor0278e122010-05-05 05:58:24 +00001965 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001966 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001967 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001968 EnteringContext, ObjectType, Result,
1969 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001970 }
1971
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001972 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001973 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1974 ClassNameLoc, getCurScope(),
1975 SS, ObjectType,
1976 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001977 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001978 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001979
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001980 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001981 return false;
1982 }
1983
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001984 Diag(Tok, diag::err_expected_unqualified_id)
1985 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001986 return true;
1987}
1988
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001989/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1990/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001991///
Chris Lattner59232d32009-01-04 21:25:24 +00001992/// This method is called to parse the new expression after the optional :: has
1993/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1994/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001995///
1996/// new-expression:
1997/// '::'[opt] 'new' new-placement[opt] new-type-id
1998/// new-initializer[opt]
1999/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2000/// new-initializer[opt]
2001///
2002/// new-placement:
2003/// '(' expression-list ')'
2004///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002005/// new-type-id:
2006/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002007/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002008///
2009/// new-declarator:
2010/// ptr-operator new-declarator[opt]
2011/// direct-new-declarator
2012///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002013/// new-initializer:
2014/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002015/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002016///
John McCall60d7b3a2010-08-24 06:29:42 +00002017ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002018Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2019 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2020 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002021
2022 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2023 // second form of new-expression. It can't be a new-type-id.
2024
Sebastian Redla55e52c2008-11-25 22:21:31 +00002025 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002026 SourceLocation PlacementLParen, PlacementRParen;
2027
Douglas Gregor4bd40312010-07-13 15:54:32 +00002028 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002029 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002030 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002031 if (Tok.is(tok::l_paren)) {
2032 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002033 BalancedDelimiterTracker T(*this, tok::l_paren);
2034 T.consumeOpen();
2035 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002036 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2037 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002038 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002039 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002040
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002041 T.consumeClose();
2042 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002043 if (PlacementRParen.isInvalid()) {
2044 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002045 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002046 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002047
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002048 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002049 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002050 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002051 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002052 } else {
2053 // We still need the type.
2054 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002055 BalancedDelimiterTracker T(*this, tok::l_paren);
2056 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002057 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002058 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002059 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002060 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002061 T.consumeClose();
2062 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002063 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002064 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002065 if (ParseCXXTypeSpecifierSeq(DS))
2066 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002067 else {
2068 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002069 ParseDeclaratorInternal(DeclaratorInfo,
2070 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002071 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002072 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002073 }
2074 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002075 // A new-type-id is a simplified type-id, where essentially the
2076 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002077 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002078 if (ParseCXXTypeSpecifierSeq(DS))
2079 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002080 else {
2081 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002082 ParseDeclaratorInternal(DeclaratorInfo,
2083 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002084 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002085 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002086 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002087 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002088 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002089 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002090
Sebastian Redla55e52c2008-11-25 22:21:31 +00002091 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002092 SourceLocation ConstructorLParen, ConstructorRParen;
2093
2094 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002095 BalancedDelimiterTracker T(*this, tok::l_paren);
2096 T.consumeOpen();
2097 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002098 if (Tok.isNot(tok::r_paren)) {
2099 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002100 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2101 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002102 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002103 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002104 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002105 T.consumeClose();
2106 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002107 if (ConstructorRParen.isInvalid()) {
2108 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002109 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002110 }
Richard Smith29e3a312011-10-15 03:38:41 +00002111 } else if (Tok.is(tok::l_brace) && getLang().CPlusPlus0x) {
Richard Smith7fe62082011-10-15 05:09:34 +00002112 Diag(Tok.getLocation(),
2113 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002114 // FIXME: Have to communicate the init-list to ActOnCXXNew.
2115 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002116 }
2117
Sebastian Redlf53597f2009-03-15 17:47:39 +00002118 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2119 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00002120 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002121 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002122}
2123
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002124/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2125/// passed to ParseDeclaratorInternal.
2126///
2127/// direct-new-declarator:
2128/// '[' expression ']'
2129/// direct-new-declarator '[' constant-expression ']'
2130///
Chris Lattner59232d32009-01-04 21:25:24 +00002131void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002132 // Parse the array dimensions.
2133 bool first = true;
2134 while (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002135 BalancedDelimiterTracker T(*this, tok::l_square);
2136 T.consumeOpen();
2137
John McCall60d7b3a2010-08-24 06:29:42 +00002138 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002139 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002140 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002141 // Recover
2142 SkipUntil(tok::r_square);
2143 return;
2144 }
2145 first = false;
2146
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002147 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002148
2149 ParsedAttributes attrs(AttrFactory);
2150 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002151 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002152 Size.release(),
2153 T.getOpenLocation(),
2154 T.getCloseLocation()),
2155 attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002156
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002157 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002158 return;
2159 }
2160}
2161
2162/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2163/// This ambiguity appears in the syntax of the C++ new operator.
2164///
2165/// new-expression:
2166/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2167/// new-initializer[opt]
2168///
2169/// new-placement:
2170/// '(' expression-list ')'
2171///
John McCallca0408f2010-08-23 06:44:23 +00002172bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002173 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002174 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002175 // The '(' was already consumed.
2176 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002177 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002178 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002179 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002180 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002181 }
2182
2183 // It's not a type, it has to be an expression list.
2184 // Discard the comma locations - ActOnCXXNew has enough parameters.
2185 CommaLocsTy CommaLocs;
2186 return ParseExpressionList(PlacementArgs, CommaLocs);
2187}
2188
2189/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2190/// to free memory allocated by new.
2191///
Chris Lattner59232d32009-01-04 21:25:24 +00002192/// This method is called to parse the 'delete' expression after the optional
2193/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2194/// and "Start" is its location. Otherwise, "Start" is the location of the
2195/// 'delete' token.
2196///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002197/// delete-expression:
2198/// '::'[opt] 'delete' cast-expression
2199/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002200ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002201Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2202 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2203 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002204
2205 // Array delete?
2206 bool ArrayDelete = false;
2207 if (Tok.is(tok::l_square)) {
2208 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002209 BalancedDelimiterTracker T(*this, tok::l_square);
2210
2211 T.consumeOpen();
2212 T.consumeClose();
2213 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002214 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002215 }
2216
John McCall60d7b3a2010-08-24 06:29:42 +00002217 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002218 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002219 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002220
John McCall9ae2f072010-08-23 23:25:46 +00002221 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002222}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002223
Mike Stump1eb44332009-09-09 15:08:12 +00002224static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002225 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002226 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002227 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002228 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002229 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002230 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002231 case tok::kw___has_trivial_constructor:
2232 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002233 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002234 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2235 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2236 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002237 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2238 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002239 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002240 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2241 case tok::kw___is_compound: return UTT_IsCompound;
2242 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002243 case tok::kw___is_empty: return UTT_IsEmpty;
2244 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley20c0da72011-04-27 23:09:49 +00002245 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2246 case tok::kw___is_function: return UTT_IsFunction;
2247 case tok::kw___is_fundamental: return UTT_IsFundamental;
2248 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00002249 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2250 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2251 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2252 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2253 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002254 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002255 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002256 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002257 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002258 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002259 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002260 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2261 case tok::kw___is_scalar: return UTT_IsScalar;
2262 case tok::kw___is_signed: return UTT_IsSigned;
2263 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2264 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002265 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002266 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002267 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2268 case tok::kw___is_void: return UTT_IsVoid;
2269 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002270 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002271}
2272
2273static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2274 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002275 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002276 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002277 case tok::kw___is_convertible: return BTT_IsConvertible;
2278 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002279 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002280 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002281 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002282}
2283
John Wiegley21ff2e52011-04-28 00:16:57 +00002284static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2285 switch(kind) {
2286 default: llvm_unreachable("Not a known binary type trait");
2287 case tok::kw___array_rank: return ATT_ArrayRank;
2288 case tok::kw___array_extent: return ATT_ArrayExtent;
2289 }
2290}
2291
John Wiegley55262202011-04-25 06:54:41 +00002292static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2293 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002294 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002295 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2296 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2297 }
2298}
2299
Sebastian Redl64b45f72009-01-05 20:52:13 +00002300/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2301/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2302/// templates.
2303///
2304/// primary-expression:
2305/// [GNU] unary-type-trait '(' type-id ')'
2306///
John McCall60d7b3a2010-08-24 06:29:42 +00002307ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002308 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2309 SourceLocation Loc = ConsumeToken();
2310
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002311 BalancedDelimiterTracker T(*this, tok::l_paren);
2312 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002313 return ExprError();
2314
2315 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2316 // there will be cryptic errors about mismatched parentheses and missing
2317 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002318 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002319
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002320 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002321
Douglas Gregor809070a2009-02-18 17:45:20 +00002322 if (Ty.isInvalid())
2323 return ExprError();
2324
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002325 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002326}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002327
Francois Pichet6ad6f282010-12-07 00:08:36 +00002328/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2329/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2330/// templates.
2331///
2332/// primary-expression:
2333/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2334///
2335ExprResult Parser::ParseBinaryTypeTrait() {
2336 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2337 SourceLocation Loc = ConsumeToken();
2338
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002339 BalancedDelimiterTracker T(*this, tok::l_paren);
2340 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002341 return ExprError();
2342
2343 TypeResult LhsTy = ParseTypeName();
2344 if (LhsTy.isInvalid()) {
2345 SkipUntil(tok::r_paren);
2346 return ExprError();
2347 }
2348
2349 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2350 SkipUntil(tok::r_paren);
2351 return ExprError();
2352 }
2353
2354 TypeResult RhsTy = ParseTypeName();
2355 if (RhsTy.isInvalid()) {
2356 SkipUntil(tok::r_paren);
2357 return ExprError();
2358 }
2359
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002360 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002361
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002362 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2363 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002364}
2365
John Wiegley21ff2e52011-04-28 00:16:57 +00002366/// ParseArrayTypeTrait - Parse the built-in array type-trait
2367/// pseudo-functions.
2368///
2369/// primary-expression:
2370/// [Embarcadero] '__array_rank' '(' type-id ')'
2371/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2372///
2373ExprResult Parser::ParseArrayTypeTrait() {
2374 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2375 SourceLocation Loc = ConsumeToken();
2376
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002377 BalancedDelimiterTracker T(*this, tok::l_paren);
2378 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002379 return ExprError();
2380
2381 TypeResult Ty = ParseTypeName();
2382 if (Ty.isInvalid()) {
2383 SkipUntil(tok::comma);
2384 SkipUntil(tok::r_paren);
2385 return ExprError();
2386 }
2387
2388 switch (ATT) {
2389 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002390 T.consumeClose();
2391 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2392 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002393 }
2394 case ATT_ArrayExtent: {
2395 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2396 SkipUntil(tok::r_paren);
2397 return ExprError();
2398 }
2399
2400 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002401 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002402
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002403 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2404 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002405 }
2406 default:
2407 break;
2408 }
2409 return ExprError();
2410}
2411
John Wiegley55262202011-04-25 06:54:41 +00002412/// ParseExpressionTrait - Parse built-in expression-trait
2413/// pseudo-functions like __is_lvalue_expr( xxx ).
2414///
2415/// primary-expression:
2416/// [Embarcadero] expression-trait '(' expression ')'
2417///
2418ExprResult Parser::ParseExpressionTrait() {
2419 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2420 SourceLocation Loc = ConsumeToken();
2421
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002422 BalancedDelimiterTracker T(*this, tok::l_paren);
2423 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002424 return ExprError();
2425
2426 ExprResult Expr = ParseExpression();
2427
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002428 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002429
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002430 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2431 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002432}
2433
2434
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002435/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2436/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2437/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002438ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002439Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002440 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002441 BalancedDelimiterTracker &Tracker) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002442 assert(getLang().CPlusPlus && "Should only be called for C++!");
2443 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2444 assert(isTypeIdInParens() && "Not a type-id!");
2445
John McCall60d7b3a2010-08-24 06:29:42 +00002446 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002447 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002448
2449 // We need to disambiguate a very ugly part of the C++ syntax:
2450 //
2451 // (T())x; - type-id
2452 // (T())*x; - type-id
2453 // (T())/x; - expression
2454 // (T()); - expression
2455 //
2456 // The bad news is that we cannot use the specialized tentative parser, since
2457 // it can only verify that the thing inside the parens can be parsed as
2458 // type-id, it is not useful for determining the context past the parens.
2459 //
2460 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002461 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002462 //
2463 // It uses a scheme similar to parsing inline methods. The parenthesized
2464 // tokens are cached, the context that follows is determined (possibly by
2465 // parsing a cast-expression), and then we re-introduce the cached tokens
2466 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002467
Mike Stump1eb44332009-09-09 15:08:12 +00002468 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002469 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002470
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002471 // Store the tokens of the parentheses. We will parse them after we determine
2472 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002473 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002474 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002475 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002476 return ExprError();
2477 }
2478
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002479 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002480 ParseAs = CompoundLiteral;
2481 } else {
2482 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002483 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2484 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2485 NotCastExpr = true;
2486 } else {
2487 // Try parsing the cast-expression that may follow.
2488 // If it is not a cast-expression, NotCastExpr will be true and no token
2489 // will be consumed.
2490 Result = ParseCastExpression(false/*isUnaryExpression*/,
2491 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002492 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002493 // type-id has priority.
2494 true/*isTypeCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002495 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002496
2497 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2498 // an expression.
2499 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002500 }
2501
Mike Stump1eb44332009-09-09 15:08:12 +00002502 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002503 Toks.push_back(Tok);
2504 // Re-enter the stored parenthesized tokens into the token stream, so we may
2505 // parse them now.
2506 PP.EnterTokenStream(Toks.data(), Toks.size(),
2507 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2508 // Drop the current token and bring the first cached one. It's the same token
2509 // as when we entered this function.
2510 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002511
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002512 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002513 // Parse the type declarator.
2514 DeclSpec DS(AttrFactory);
2515 ParseSpecifierQualifierList(DS);
2516 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2517 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002518
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002519 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002520 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002521
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002522 if (ParseAs == CompoundLiteral) {
2523 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002524 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002525 return ParseCompoundLiteralExpression(Ty.get(),
2526 Tracker.getOpenLocation(),
2527 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002528 }
Mike Stump1eb44332009-09-09 15:08:12 +00002529
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002530 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2531 assert(ParseAs == CastExpr);
2532
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002533 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002534 return ExprError();
2535
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002536 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002537 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002538 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2539 DeclaratorInfo, CastTy,
2540 Tracker.getCloseLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002541 return move(Result);
2542 }
Mike Stump1eb44332009-09-09 15:08:12 +00002543
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002544 // Not a compound literal, and not followed by a cast-expression.
2545 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002546
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002547 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002548 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002549 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002550 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2551 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002552
2553 // Match the ')'.
2554 if (Result.isInvalid()) {
2555 SkipUntil(tok::r_paren);
2556 return ExprError();
2557 }
Mike Stump1eb44332009-09-09 15:08:12 +00002558
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002559 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002560 return move(Result);
2561}