blob: 33348a9aaba1b892ffc7aab12973d90e439f73e7 [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(),
751 MutableLoc,
752 ESpecType, ESpecRange.getBegin(),
753 DynamicExceptions.data(),
754 DynamicExceptionRanges.data(),
755 DynamicExceptions.size(),
756 NoexceptExpr.isUsable() ?
757 NoexceptExpr.get() : 0,
758 DeclLoc, DeclEndLoc, D,
759 TrailingReturnType),
760 Attr, DeclEndLoc);
761 }
762
763 // Parse compound-statement.
764 if (Tok.is(tok::l_brace)) {
765 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
766 // it.
767 ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
768 Scope::BreakScope | Scope::ContinueScope |
769 Scope::DeclScope);
770
771 StmtResult Stmt(ParseCompoundStatementBody());
772
773 BodyScope.Exit();
774 } else {
775 Diag(Tok, diag::err_expected_lambda_body);
776 }
777
778 return ExprEmpty();
779}
780
Reid Spencer5f016e22007-07-11 17:01:13 +0000781/// ParseCXXCasts - This handles the various ways to cast expressions to another
782/// type.
783///
784/// postfix-expression: [C++ 5.2p1]
785/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
786/// 'static_cast' '<' type-name '>' '(' expression ')'
787/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
788/// 'const_cast' '<' type-name '>' '(' expression ')'
789///
John McCall60d7b3a2010-08-24 06:29:42 +0000790ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 tok::TokenKind Kind = Tok.getKind();
792 const char *CastName = 0; // For error messages
793
794 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000795 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 case tok::kw_const_cast: CastName = "const_cast"; break;
797 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
798 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
799 case tok::kw_static_cast: CastName = "static_cast"; break;
800 }
801
802 SourceLocation OpLoc = ConsumeToken();
803 SourceLocation LAngleBracketLoc = Tok.getLocation();
804
Richard Smithea698b32011-04-14 21:45:45 +0000805 // Check for "<::" which is parsed as "[:". If found, fix token stream,
806 // diagnose error, suggest fix, and recover parsing.
807 Token Next = NextToken();
808 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
809 AreTokensAdjacent(PP, Tok, Next))
810 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
811
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000813 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000814
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000815 // Parse the common declaration-specifiers piece.
816 DeclSpec DS(AttrFactory);
817 ParseSpecifierQualifierList(DS);
818
819 // Parse the abstract-declarator, if present.
820 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
821 ParseDeclarator(DeclaratorInfo);
822
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 SourceLocation RAngleBracketLoc = Tok.getLocation();
824
Chris Lattner1ab3b962008-11-18 07:48:38 +0000825 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000826 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000827
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000828 SourceLocation LParenLoc, RParenLoc;
829 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +0000830
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000831 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000832 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000833
John McCall60d7b3a2010-08-24 06:29:42 +0000834 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000836 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000837 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +0000838
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000839 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +0000840 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000841 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +0000842 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000843 T.getOpenLocation(), Result.take(),
844 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000845
Sebastian Redl20df9b72008-12-11 22:51:44 +0000846 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000847}
848
Sebastian Redlc42e1182008-11-11 11:37:55 +0000849/// ParseCXXTypeid - This handles the C++ typeid expression.
850///
851/// postfix-expression: [C++ 5.2p1]
852/// 'typeid' '(' expression ')'
853/// 'typeid' '(' type-id ')'
854///
John McCall60d7b3a2010-08-24 06:29:42 +0000855ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000856 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
857
858 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000859 SourceLocation LParenLoc, RParenLoc;
860 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000861
862 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000863 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000864 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000865 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000866
John McCall60d7b3a2010-08-24 06:29:42 +0000867 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000868
869 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000870 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000871
872 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000873 T.consumeClose();
874 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000875 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000876 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000877
878 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000879 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000880 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000881 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000882 // When typeid is applied to an expression other than an lvalue of a
883 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000884 // operand (Clause 5).
885 //
Mike Stump1eb44332009-09-09 15:08:12 +0000886 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000887 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000888 // we the expression is potentially potentially evaluated.
889 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000890 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000891 Result = ParseExpression();
892
893 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000894 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000895 SkipUntil(tok::r_paren);
896 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000897 T.consumeClose();
898 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000899 if (RParenLoc.isInvalid())
900 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000901
Sebastian Redlc42e1182008-11-11 11:37:55 +0000902 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000903 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000904 }
905 }
906
Sebastian Redl20df9b72008-12-11 22:51:44 +0000907 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000908}
909
Francois Pichet01b7c302010-09-08 12:20:18 +0000910/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
911///
912/// '__uuidof' '(' expression ')'
913/// '__uuidof' '(' type-id ')'
914///
915ExprResult Parser::ParseCXXUuidof() {
916 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
917
918 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000919 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +0000920
921 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000922 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +0000923 return ExprError();
924
925 ExprResult Result;
926
927 if (isTypeIdInParens()) {
928 TypeResult Ty = ParseTypeName();
929
930 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000931 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +0000932
933 if (Ty.isInvalid())
934 return ExprError();
935
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000936 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
937 Ty.get().getAsOpaquePtr(),
938 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +0000939 } else {
940 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
941 Result = ParseExpression();
942
943 // Match the ')'.
944 if (Result.isInvalid())
945 SkipUntil(tok::r_paren);
946 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000947 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +0000948
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000949 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
950 /*isType=*/false,
951 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +0000952 }
953 }
954
955 return move(Result);
956}
957
Douglas Gregord4dca082010-02-24 18:44:31 +0000958/// \brief Parse a C++ pseudo-destructor expression after the base,
959/// . or -> operator, and nested-name-specifier have already been
960/// parsed.
961///
962/// postfix-expression: [C++ 5.2]
963/// postfix-expression . pseudo-destructor-name
964/// postfix-expression -> pseudo-destructor-name
965///
966/// pseudo-destructor-name:
967/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
968/// ::[opt] nested-name-specifier template simple-template-id ::
969/// ~type-name
970/// ::[opt] nested-name-specifier[opt] ~type-name
971///
John McCall60d7b3a2010-08-24 06:29:42 +0000972ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000973Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
974 tok::TokenKind OpKind,
975 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000976 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000977 // We're parsing either a pseudo-destructor-name or a dependent
978 // member access that has the same form as a
979 // pseudo-destructor-name. We parse both in the same way and let
980 // the action model sort them out.
981 //
982 // Note that the ::[opt] nested-name-specifier[opt] has already
983 // been parsed, and if there was a simple-template-id, it has
984 // been coalesced into a template-id annotation token.
985 UnqualifiedId FirstTypeName;
986 SourceLocation CCLoc;
987 if (Tok.is(tok::identifier)) {
988 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
989 ConsumeToken();
990 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
991 CCLoc = ConsumeToken();
992 } else if (Tok.is(tok::annot_template_id)) {
993 FirstTypeName.setTemplateId(
994 (TemplateIdAnnotation *)Tok.getAnnotationValue());
995 ConsumeToken();
996 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
997 CCLoc = ConsumeToken();
998 } else {
999 FirstTypeName.setIdentifier(0, SourceLocation());
1000 }
1001
1002 // Parse the tilde.
1003 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1004 SourceLocation TildeLoc = ConsumeToken();
1005 if (!Tok.is(tok::identifier)) {
1006 Diag(Tok, diag::err_destructor_tilde_identifier);
1007 return ExprError();
1008 }
1009
1010 // Parse the second type.
1011 UnqualifiedId SecondTypeName;
1012 IdentifierInfo *Name = Tok.getIdentifierInfo();
1013 SourceLocation NameLoc = ConsumeToken();
1014 SecondTypeName.setIdentifier(Name, NameLoc);
1015
1016 // If there is a '<', the second type name is a template-id. Parse
1017 // it as such.
1018 if (Tok.is(tok::less) &&
1019 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001020 SecondTypeName, /*AssumeTemplateName=*/true,
1021 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +00001022 return ExprError();
1023
John McCall9ae2f072010-08-23 23:25:46 +00001024 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1025 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001026 SS, FirstTypeName, CCLoc,
1027 TildeLoc, SecondTypeName,
1028 Tok.is(tok::l_paren));
1029}
1030
Reid Spencer5f016e22007-07-11 17:01:13 +00001031/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1032///
1033/// boolean-literal: [C++ 2.13.5]
1034/// 'true'
1035/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001036ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001038 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001039}
Chris Lattner50dd2892008-02-26 00:51:44 +00001040
1041/// ParseThrowExpression - This handles the C++ throw expression.
1042///
1043/// throw-expression: [C++ 15]
1044/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001045ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001046 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001047 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001048
Chris Lattner2a2819a2008-04-06 06:02:23 +00001049 // If the current token isn't the start of an assignment-expression,
1050 // then the expression is not present. This handles things like:
1051 // "C ? throw : (void)42", which is crazy but legal.
1052 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1053 case tok::semi:
1054 case tok::r_paren:
1055 case tok::r_square:
1056 case tok::r_brace:
1057 case tok::colon:
1058 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001059 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001060
Chris Lattner2a2819a2008-04-06 06:02:23 +00001061 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001062 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +00001063 if (Expr.isInvalid()) return move(Expr);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001064 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001065 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001066}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001067
1068/// ParseCXXThis - This handles the C++ 'this' pointer.
1069///
1070/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1071/// a non-lvalue expression whose value is the address of the object for which
1072/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001073ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001074 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1075 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001076 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001077}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001078
1079/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1080/// Can be interpreted either as function-style casting ("int(x)")
1081/// or class type construction ("ClassType(x,y,z)")
1082/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001083/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001084///
1085/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001086/// simple-type-specifier '(' expression-list[opt] ')'
1087/// [C++0x] simple-type-specifier braced-init-list
1088/// typename-specifier '(' expression-list[opt] ')'
1089/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001090///
John McCall60d7b3a2010-08-24 06:29:42 +00001091ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001092Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001093 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001094 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001095
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001096 assert((Tok.is(tok::l_paren) ||
1097 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1098 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001099
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001100 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001101
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001102 // FIXME: Convert to a proper type construct expression.
1103 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001104
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001105 } else {
1106 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1107
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001108 BalancedDelimiterTracker T(*this, tok::l_paren);
1109 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001110
1111 ExprVector Exprs(Actions);
1112 CommaLocsTy CommaLocs;
1113
1114 if (Tok.isNot(tok::r_paren)) {
1115 if (ParseExpressionList(Exprs, CommaLocs)) {
1116 SkipUntil(tok::r_paren);
1117 return ExprError();
1118 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001119 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001120
1121 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001122 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001123
1124 // TypeRep could be null, if it references an invalid typedef.
1125 if (!TypeRep)
1126 return ExprError();
1127
1128 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1129 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001130 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
1131 move_arg(Exprs),
1132 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001133 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001134}
1135
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001136/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001137///
1138/// condition:
1139/// expression
1140/// type-specifier-seq declarator '=' assignment-expression
1141/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1142/// '=' assignment-expression
1143///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001144/// \param ExprResult if the condition was parsed as an expression, the
1145/// parsed expression.
1146///
1147/// \param DeclResult if the condition was parsed as a declaration, the
1148/// parsed declaration.
1149///
Douglas Gregor586596f2010-05-06 17:25:47 +00001150/// \param Loc The location of the start of the statement that requires this
1151/// condition, e.g., the "for" in a for loop.
1152///
1153/// \param ConvertToBoolean Whether the condition expression should be
1154/// converted to a boolean value.
1155///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001156/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001157bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1158 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001159 SourceLocation Loc,
1160 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001161 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001162 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001163 cutOffParsing();
1164 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001165 }
1166
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001167 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001168 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001169 ExprOut = ParseExpression(); // expression
1170 DeclOut = 0;
1171 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001172 return true;
1173
1174 // If required, convert to a boolean value.
1175 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001176 ExprOut
1177 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1178 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001179 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001180
1181 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001182 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001183 ParseSpecifierQualifierList(DS);
1184
1185 // declarator
1186 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1187 ParseDeclarator(DeclaratorInfo);
1188
1189 // simple-asm-expr[opt]
1190 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001191 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001192 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001193 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001194 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001195 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001196 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001197 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001198 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001199 }
1200
1201 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001202 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001203
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001204 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001205 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001206 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001207 DeclOut = Dcl.get();
1208 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001209
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001210 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001211 if (isTokenEqualOrMistypedEqualEqual(
1212 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001213 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001214 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001215 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +00001216 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1217 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001218 } else {
1219 // FIXME: C++0x allows a braced-init-list
1220 Diag(Tok, diag::err_expected_equal_after_declarator);
1221 }
1222
Douglas Gregor586596f2010-05-06 17:25:47 +00001223 // FIXME: Build a reference to this declaration? Convert it to bool?
1224 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001225
1226 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001227
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001228 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001229}
1230
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001231/// \brief Determine whether the current token starts a C++
1232/// simple-type-specifier.
1233bool Parser::isCXXSimpleTypeSpecifier() const {
1234 switch (Tok.getKind()) {
1235 case tok::annot_typename:
1236 case tok::kw_short:
1237 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001238 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001239 case tok::kw_signed:
1240 case tok::kw_unsigned:
1241 case tok::kw_void:
1242 case tok::kw_char:
1243 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001244 case tok::kw_half:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001245 case tok::kw_float:
1246 case tok::kw_double:
1247 case tok::kw_wchar_t:
1248 case tok::kw_char16_t:
1249 case tok::kw_char32_t:
1250 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +00001251 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001252 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +00001253 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001254 return true;
1255
1256 default:
1257 break;
1258 }
1259
1260 return false;
1261}
1262
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001263/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1264/// This should only be called when the current token is known to be part of
1265/// simple-type-specifier.
1266///
1267/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001268/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001269/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1270/// char
1271/// wchar_t
1272/// bool
1273/// short
1274/// int
1275/// long
1276/// signed
1277/// unsigned
1278/// float
1279/// double
1280/// void
1281/// [GNU] typeof-specifier
1282/// [C++0x] auto [TODO]
1283///
1284/// type-name:
1285/// class-name
1286/// enum-name
1287/// typedef-name
1288///
1289void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1290 DS.SetRangeStart(Tok.getLocation());
1291 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001292 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001293 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001295 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001296 case tok::identifier: // foo::bar
1297 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001298 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001299 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001300 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001301
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001302 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001303 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001304 if (getTypeAnnotation(Tok))
1305 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1306 getTypeAnnotation(Tok));
1307 else
1308 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001309
1310 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1311 ConsumeToken();
1312
1313 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1314 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1315 // Objective-C interface. If we don't have Objective-C or a '<', this is
1316 // just a normal reference to a typedef name.
1317 if (Tok.is(tok::less) && getLang().ObjC1)
1318 ParseObjCProtocolQualifiers(DS);
1319
1320 DS.Finish(Diags, PP);
1321 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001322 }
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001324 // builtin types
1325 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001326 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001327 break;
1328 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001329 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001330 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001331 case tok::kw___int64:
1332 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1333 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001334 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001335 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001336 break;
1337 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001338 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001339 break;
1340 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001341 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001342 break;
1343 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001344 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001345 break;
1346 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001347 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001348 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001349 case tok::kw_half:
1350 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1351 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001352 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001353 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001354 break;
1355 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001356 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001357 break;
1358 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001359 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001360 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001361 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001362 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001363 break;
1364 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001365 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001366 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001367 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001368 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001369 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001371 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001372 // GNU typeof support.
1373 case tok::kw_typeof:
1374 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001375 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001376 return;
1377 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001378 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001379 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1380 else
1381 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001382 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001383 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001384}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001385
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001386/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1387/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1388/// e.g., "const short int". Note that the DeclSpec is *not* finished
1389/// by parsing the type-specifier-seq, because these sequences are
1390/// typically followed by some form of declarator. Returns true and
1391/// emits diagnostics if this is not a type-specifier-seq, false
1392/// otherwise.
1393///
1394/// type-specifier-seq: [C++ 8.1]
1395/// type-specifier type-specifier-seq[opt]
1396///
1397bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1398 DS.SetRangeStart(Tok.getLocation());
1399 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001400 unsigned DiagID;
1401 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001402
1403 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001404 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1405 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001406 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001407 return true;
1408 }
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Sebastian Redld9bafa72010-02-03 21:21:43 +00001410 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1411 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1412 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001413
Douglas Gregor396a9f22010-02-24 23:13:13 +00001414 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001415 return false;
1416}
1417
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001418/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1419/// some form.
1420///
1421/// This routine is invoked when a '<' is encountered after an identifier or
1422/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1423/// whether the unqualified-id is actually a template-id. This routine will
1424/// then parse the template arguments and form the appropriate template-id to
1425/// return to the caller.
1426///
1427/// \param SS the nested-name-specifier that precedes this template-id, if
1428/// we're actually parsing a qualified-id.
1429///
1430/// \param Name for constructor and destructor names, this is the actual
1431/// identifier that may be a template-name.
1432///
1433/// \param NameLoc the location of the class-name in a constructor or
1434/// destructor.
1435///
1436/// \param EnteringContext whether we're entering the scope of the
1437/// nested-name-specifier.
1438///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001439/// \param ObjectType if this unqualified-id occurs within a member access
1440/// expression, the type of the base object whose member is being accessed.
1441///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001442/// \param Id as input, describes the template-name or operator-function-id
1443/// that precedes the '<'. If template arguments were parsed successfully,
1444/// will be updated with the template-id.
1445///
Douglas Gregord4dca082010-02-24 18:44:31 +00001446/// \param AssumeTemplateId When true, this routine will assume that the name
1447/// refers to a template without performing name lookup to verify.
1448///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001449/// \returns true if a parse error occurred, false otherwise.
1450bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1451 IdentifierInfo *Name,
1452 SourceLocation NameLoc,
1453 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001454 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001455 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001456 bool AssumeTemplateId,
1457 SourceLocation TemplateKWLoc) {
1458 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1459 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001460
1461 TemplateTy Template;
1462 TemplateNameKind TNK = TNK_Non_template;
1463 switch (Id.getKind()) {
1464 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001465 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001466 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001467 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001468 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001469 Id, ObjectType, EnteringContext,
1470 Template);
1471 if (TNK == TNK_Non_template)
1472 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001473 } else {
1474 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001475 TNK = Actions.isTemplateName(getCurScope(), SS,
1476 TemplateKWLoc.isValid(), Id,
1477 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001478 MemberOfUnknownSpecialization);
1479
1480 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1481 ObjectType && IsTemplateArgumentList()) {
1482 // We have something like t->getAs<T>(), where getAs is a
1483 // member of an unknown specialization. However, this will only
1484 // parse correctly as a template, so suggest the keyword 'template'
1485 // before 'getAs' and treat this as a dependent template name.
1486 std::string Name;
1487 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1488 Name = Id.Identifier->getName();
1489 else {
1490 Name = "operator ";
1491 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1492 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1493 else
1494 Name += Id.Identifier->getName();
1495 }
1496 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1497 << Name
1498 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001499 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001500 SS, Id, ObjectType,
1501 EnteringContext, Template);
1502 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001503 return true;
1504 }
1505 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001506 break;
1507
Douglas Gregor014e88d2009-11-03 23:16:33 +00001508 case UnqualifiedId::IK_ConstructorName: {
1509 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001510 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001511 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001512 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1513 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001514 EnteringContext, Template,
1515 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001516 break;
1517 }
1518
Douglas Gregor014e88d2009-11-03 23:16:33 +00001519 case UnqualifiedId::IK_DestructorName: {
1520 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001521 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001522 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001523 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001524 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001525 TemplateName, ObjectType,
1526 EnteringContext, Template);
1527 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001528 return true;
1529 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001530 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1531 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001532 EnteringContext, Template,
1533 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001534
John McCallb3d87482010-08-24 05:47:05 +00001535 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001536 Diag(NameLoc, diag::err_destructor_template_id)
1537 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001538 return true;
1539 }
1540 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001541 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001542 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001543
1544 default:
1545 return false;
1546 }
1547
1548 if (TNK == TNK_Non_template)
1549 return false;
1550
1551 // Parse the enclosed template argument list.
1552 SourceLocation LAngleLoc, RAngleLoc;
1553 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001554 if (Tok.is(tok::less) &&
1555 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001556 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001557 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001558 RAngleLoc))
1559 return true;
1560
1561 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001562 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1563 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001564 // Form a parsed representation of the template-id to be stored in the
1565 // UnqualifiedId.
1566 TemplateIdAnnotation *TemplateId
1567 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1568
1569 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1570 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001571 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001572 TemplateId->TemplateNameLoc = Id.StartLocation;
1573 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001574 TemplateId->Name = 0;
1575 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1576 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001577 }
1578
Douglas Gregor059101f2011-03-02 00:47:37 +00001579 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001580 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001581 TemplateId->Kind = TNK;
1582 TemplateId->LAngleLoc = LAngleLoc;
1583 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001584 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001585 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001586 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001587 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001588
1589 Id.setTemplateId(TemplateId);
1590 return false;
1591 }
1592
1593 // Bundle the template arguments together.
1594 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001595 TemplateArgs.size());
1596
1597 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001598 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001599 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001600 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001601 RAngleLoc);
1602 if (Type.isInvalid())
1603 return true;
1604
1605 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1606 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1607 else
1608 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1609
1610 return false;
1611}
1612
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001613/// \brief Parse an operator-function-id or conversion-function-id as part
1614/// of a C++ unqualified-id.
1615///
1616/// This routine is responsible only for parsing the operator-function-id or
1617/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001618///
1619/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001620/// operator-function-id: [C++ 13.5]
1621/// 'operator' operator
1622///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001623/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001624/// new delete new[] delete[]
1625/// + - * / % ^ & | ~
1626/// ! = < > += -= *= /= %=
1627/// ^= &= |= << >> >>= <<= == !=
1628/// <= >= && || ++ -- , ->* ->
1629/// () []
1630///
1631/// conversion-function-id: [C++ 12.3.2]
1632/// operator conversion-type-id
1633///
1634/// conversion-type-id:
1635/// type-specifier-seq conversion-declarator[opt]
1636///
1637/// conversion-declarator:
1638/// ptr-operator conversion-declarator[opt]
1639/// \endcode
1640///
1641/// \param The nested-name-specifier that preceded this unqualified-id. If
1642/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1643///
1644/// \param EnteringContext whether we are entering the scope of the
1645/// nested-name-specifier.
1646///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001647/// \param ObjectType if this unqualified-id occurs within a member access
1648/// expression, the type of the base object whose member is being accessed.
1649///
1650/// \param Result on a successful parse, contains the parsed unqualified-id.
1651///
1652/// \returns true if parsing fails, false otherwise.
1653bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001654 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001655 UnqualifiedId &Result) {
1656 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1657
1658 // Consume the 'operator' keyword.
1659 SourceLocation KeywordLoc = ConsumeToken();
1660
1661 // Determine what kind of operator name we have.
1662 unsigned SymbolIdx = 0;
1663 SourceLocation SymbolLocations[3];
1664 OverloadedOperatorKind Op = OO_None;
1665 switch (Tok.getKind()) {
1666 case tok::kw_new:
1667 case tok::kw_delete: {
1668 bool isNew = Tok.getKind() == tok::kw_new;
1669 // Consume the 'new' or 'delete'.
1670 SymbolLocations[SymbolIdx++] = ConsumeToken();
1671 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001672 // Consume the '[' and ']'.
1673 BalancedDelimiterTracker T(*this, tok::l_square);
1674 T.consumeOpen();
1675 T.consumeClose();
1676 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001677 return true;
1678
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001679 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1680 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001681 Op = isNew? OO_Array_New : OO_Array_Delete;
1682 } else {
1683 Op = isNew? OO_New : OO_Delete;
1684 }
1685 break;
1686 }
1687
1688#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1689 case tok::Token: \
1690 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1691 Op = OO_##Name; \
1692 break;
1693#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1694#include "clang/Basic/OperatorKinds.def"
1695
1696 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001697 // Consume the '(' and ')'.
1698 BalancedDelimiterTracker T(*this, tok::l_paren);
1699 T.consumeOpen();
1700 T.consumeClose();
1701 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001702 return true;
1703
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001704 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1705 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001706 Op = OO_Call;
1707 break;
1708 }
1709
1710 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001711 // Consume the '[' and ']'.
1712 BalancedDelimiterTracker T(*this, tok::l_square);
1713 T.consumeOpen();
1714 T.consumeClose();
1715 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001716 return true;
1717
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001718 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1719 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001720 Op = OO_Subscript;
1721 break;
1722 }
1723
1724 case tok::code_completion: {
1725 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001726 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001727 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001728 // Don't try to parse any further.
1729 return true;
1730 }
1731
1732 default:
1733 break;
1734 }
1735
1736 if (Op != OO_None) {
1737 // We have parsed an operator-function-id.
1738 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1739 return false;
1740 }
Sean Hunt0486d742009-11-28 04:44:28 +00001741
1742 // Parse a literal-operator-id.
1743 //
1744 // literal-operator-id: [C++0x 13.5.8]
1745 // operator "" identifier
1746
1747 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
Richard Smith7fe62082011-10-15 05:09:34 +00001748 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001749 if (Tok.getLength() != 2)
1750 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1751 ConsumeStringToken();
1752
1753 if (Tok.isNot(tok::identifier)) {
1754 Diag(Tok.getLocation(), diag::err_expected_ident);
1755 return true;
1756 }
1757
1758 IdentifierInfo *II = Tok.getIdentifierInfo();
1759 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001760 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001761 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001762
1763 // Parse a conversion-function-id.
1764 //
1765 // conversion-function-id: [C++ 12.3.2]
1766 // operator conversion-type-id
1767 //
1768 // conversion-type-id:
1769 // type-specifier-seq conversion-declarator[opt]
1770 //
1771 // conversion-declarator:
1772 // ptr-operator conversion-declarator[opt]
1773
1774 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001775 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001776 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001777 return true;
1778
1779 // Parse the conversion-declarator, which is merely a sequence of
1780 // ptr-operators.
1781 Declarator D(DS, Declarator::TypeNameContext);
1782 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1783
1784 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001785 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001786 if (Ty.isInvalid())
1787 return true;
1788
1789 // Note that this is a conversion-function-id.
1790 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1791 D.getSourceRange().getEnd());
1792 return false;
1793}
1794
1795/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1796/// name of an entity.
1797///
1798/// \code
1799/// unqualified-id: [C++ expr.prim.general]
1800/// identifier
1801/// operator-function-id
1802/// conversion-function-id
1803/// [C++0x] literal-operator-id [TODO]
1804/// ~ class-name
1805/// template-id
1806///
1807/// \endcode
1808///
1809/// \param The nested-name-specifier that preceded this unqualified-id. If
1810/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1811///
1812/// \param EnteringContext whether we are entering the scope of the
1813/// nested-name-specifier.
1814///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001815/// \param AllowDestructorName whether we allow parsing of a destructor name.
1816///
1817/// \param AllowConstructorName whether we allow parsing a constructor name.
1818///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001819/// \param ObjectType if this unqualified-id occurs within a member access
1820/// expression, the type of the base object whose member is being accessed.
1821///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001822/// \param Result on a successful parse, contains the parsed unqualified-id.
1823///
1824/// \returns true if parsing fails, false otherwise.
1825bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1826 bool AllowDestructorName,
1827 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001828 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001829 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001830
1831 // Handle 'A::template B'. This is for template-ids which have not
1832 // already been annotated by ParseOptionalCXXScopeSpecifier().
1833 bool TemplateSpecified = false;
1834 SourceLocation TemplateKWLoc;
1835 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1836 (ObjectType || SS.isSet())) {
1837 TemplateSpecified = true;
1838 TemplateKWLoc = ConsumeToken();
1839 }
1840
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001841 // unqualified-id:
1842 // identifier
1843 // template-id (when it hasn't already been annotated)
1844 if (Tok.is(tok::identifier)) {
1845 // Consume the identifier.
1846 IdentifierInfo *Id = Tok.getIdentifierInfo();
1847 SourceLocation IdLoc = ConsumeToken();
1848
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001849 if (!getLang().CPlusPlus) {
1850 // If we're not in C++, only identifiers matter. Record the
1851 // identifier and return.
1852 Result.setIdentifier(Id, IdLoc);
1853 return false;
1854 }
1855
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001856 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001857 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001858 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001859 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001860 &SS, false, false,
1861 ParsedType(),
1862 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001863 IdLoc, IdLoc);
1864 } else {
1865 // We have parsed an identifier.
1866 Result.setIdentifier(Id, IdLoc);
1867 }
1868
1869 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001870 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001871 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001872 ObjectType, Result,
1873 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001874
1875 return false;
1876 }
1877
1878 // unqualified-id:
1879 // template-id (already parsed and annotated)
1880 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001881 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001882
1883 // If the template-name names the current class, then this is a constructor
1884 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001885 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001886 if (SS.isSet()) {
1887 // C++ [class.qual]p2 specifies that a qualified template-name
1888 // is taken as the constructor name where a constructor can be
1889 // declared. Thus, the template arguments are extraneous, so
1890 // complain about them and remove them entirely.
1891 Diag(TemplateId->TemplateNameLoc,
1892 diag::err_out_of_line_constructor_template_id)
1893 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001894 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001895 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1896 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1897 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001898 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001899 &SS, false, false,
1900 ParsedType(),
1901 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001902 TemplateId->TemplateNameLoc,
1903 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001904 ConsumeToken();
1905 return false;
1906 }
1907
1908 Result.setConstructorTemplateId(TemplateId);
1909 ConsumeToken();
1910 return false;
1911 }
1912
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001913 // We have already parsed a template-id; consume the annotation token as
1914 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001915 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001916 ConsumeToken();
1917 return false;
1918 }
1919
1920 // unqualified-id:
1921 // operator-function-id
1922 // conversion-function-id
1923 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001924 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001925 return true;
1926
Sean Hunte6252d12009-11-28 08:58:14 +00001927 // If we have an operator-function-id or a literal-operator-id and the next
1928 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001929 //
1930 // template-id:
1931 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001932 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1933 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001934 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001935 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1936 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001937 Result,
1938 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001939
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001940 return false;
1941 }
1942
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001943 if (getLang().CPlusPlus &&
1944 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001945 // C++ [expr.unary.op]p10:
1946 // There is an ambiguity in the unary-expression ~X(), where X is a
1947 // class-name. The ambiguity is resolved in favor of treating ~ as a
1948 // unary complement rather than treating ~X as referring to a destructor.
1949
1950 // Parse the '~'.
1951 SourceLocation TildeLoc = ConsumeToken();
1952
1953 // Parse the class-name.
1954 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001955 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001956 return true;
1957 }
1958
1959 // Parse the class-name (or template-name in a simple-template-id).
1960 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1961 SourceLocation ClassNameLoc = ConsumeToken();
1962
Douglas Gregor0278e122010-05-05 05:58:24 +00001963 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001964 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001965 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001966 EnteringContext, ObjectType, Result,
1967 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001968 }
1969
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001970 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001971 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1972 ClassNameLoc, getCurScope(),
1973 SS, ObjectType,
1974 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001975 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001976 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001977
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001978 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001979 return false;
1980 }
1981
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001982 Diag(Tok, diag::err_expected_unqualified_id)
1983 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001984 return true;
1985}
1986
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001987/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1988/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001989///
Chris Lattner59232d32009-01-04 21:25:24 +00001990/// This method is called to parse the new expression after the optional :: has
1991/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1992/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001993///
1994/// new-expression:
1995/// '::'[opt] 'new' new-placement[opt] new-type-id
1996/// new-initializer[opt]
1997/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1998/// new-initializer[opt]
1999///
2000/// new-placement:
2001/// '(' expression-list ')'
2002///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002003/// new-type-id:
2004/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002005/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002006///
2007/// new-declarator:
2008/// ptr-operator new-declarator[opt]
2009/// direct-new-declarator
2010///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002011/// new-initializer:
2012/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002013/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002014///
John McCall60d7b3a2010-08-24 06:29:42 +00002015ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002016Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2017 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2018 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002019
2020 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2021 // second form of new-expression. It can't be a new-type-id.
2022
Sebastian Redla55e52c2008-11-25 22:21:31 +00002023 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002024 SourceLocation PlacementLParen, PlacementRParen;
2025
Douglas Gregor4bd40312010-07-13 15:54:32 +00002026 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002027 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002028 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002029 if (Tok.is(tok::l_paren)) {
2030 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002031 BalancedDelimiterTracker T(*this, tok::l_paren);
2032 T.consumeOpen();
2033 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002034 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2035 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002036 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002037 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002038
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002039 T.consumeClose();
2040 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002041 if (PlacementRParen.isInvalid()) {
2042 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002043 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002044 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002045
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002046 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002047 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002048 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002049 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002050 } else {
2051 // We still need the type.
2052 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002053 BalancedDelimiterTracker T(*this, tok::l_paren);
2054 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002055 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002056 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002057 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002058 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002059 T.consumeClose();
2060 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002061 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002062 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002063 if (ParseCXXTypeSpecifierSeq(DS))
2064 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002065 else {
2066 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002067 ParseDeclaratorInternal(DeclaratorInfo,
2068 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002069 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002070 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002071 }
2072 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002073 // A new-type-id is a simplified type-id, where essentially the
2074 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002075 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002076 if (ParseCXXTypeSpecifierSeq(DS))
2077 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002078 else {
2079 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002080 ParseDeclaratorInternal(DeclaratorInfo,
2081 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002082 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002083 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002084 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002085 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002086 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002087 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002088
Sebastian Redla55e52c2008-11-25 22:21:31 +00002089 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002090 SourceLocation ConstructorLParen, ConstructorRParen;
2091
2092 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002093 BalancedDelimiterTracker T(*this, tok::l_paren);
2094 T.consumeOpen();
2095 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002096 if (Tok.isNot(tok::r_paren)) {
2097 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002098 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2099 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002100 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002101 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002102 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002103 T.consumeClose();
2104 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002105 if (ConstructorRParen.isInvalid()) {
2106 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002107 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002108 }
Richard Smith29e3a312011-10-15 03:38:41 +00002109 } else if (Tok.is(tok::l_brace) && getLang().CPlusPlus0x) {
Richard Smith7fe62082011-10-15 05:09:34 +00002110 Diag(Tok.getLocation(),
2111 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002112 // FIXME: Have to communicate the init-list to ActOnCXXNew.
2113 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002114 }
2115
Sebastian Redlf53597f2009-03-15 17:47:39 +00002116 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2117 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00002118 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002119 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002120}
2121
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002122/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2123/// passed to ParseDeclaratorInternal.
2124///
2125/// direct-new-declarator:
2126/// '[' expression ']'
2127/// direct-new-declarator '[' constant-expression ']'
2128///
Chris Lattner59232d32009-01-04 21:25:24 +00002129void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002130 // Parse the array dimensions.
2131 bool first = true;
2132 while (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002133 BalancedDelimiterTracker T(*this, tok::l_square);
2134 T.consumeOpen();
2135
John McCall60d7b3a2010-08-24 06:29:42 +00002136 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002137 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002138 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002139 // Recover
2140 SkipUntil(tok::r_square);
2141 return;
2142 }
2143 first = false;
2144
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002145 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002146
2147 ParsedAttributes attrs(AttrFactory);
2148 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002149 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002150 Size.release(),
2151 T.getOpenLocation(),
2152 T.getCloseLocation()),
2153 attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002154
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002155 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002156 return;
2157 }
2158}
2159
2160/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2161/// This ambiguity appears in the syntax of the C++ new operator.
2162///
2163/// new-expression:
2164/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2165/// new-initializer[opt]
2166///
2167/// new-placement:
2168/// '(' expression-list ')'
2169///
John McCallca0408f2010-08-23 06:44:23 +00002170bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002171 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002172 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002173 // The '(' was already consumed.
2174 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002175 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002176 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002177 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002178 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002179 }
2180
2181 // It's not a type, it has to be an expression list.
2182 // Discard the comma locations - ActOnCXXNew has enough parameters.
2183 CommaLocsTy CommaLocs;
2184 return ParseExpressionList(PlacementArgs, CommaLocs);
2185}
2186
2187/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2188/// to free memory allocated by new.
2189///
Chris Lattner59232d32009-01-04 21:25:24 +00002190/// This method is called to parse the 'delete' expression after the optional
2191/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2192/// and "Start" is its location. Otherwise, "Start" is the location of the
2193/// 'delete' token.
2194///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002195/// delete-expression:
2196/// '::'[opt] 'delete' cast-expression
2197/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002198ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002199Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2200 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2201 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002202
2203 // Array delete?
2204 bool ArrayDelete = false;
2205 if (Tok.is(tok::l_square)) {
2206 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002207 BalancedDelimiterTracker T(*this, tok::l_square);
2208
2209 T.consumeOpen();
2210 T.consumeClose();
2211 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002212 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002213 }
2214
John McCall60d7b3a2010-08-24 06:29:42 +00002215 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002216 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002217 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002218
John McCall9ae2f072010-08-23 23:25:46 +00002219 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002220}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002221
Mike Stump1eb44332009-09-09 15:08:12 +00002222static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002223 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002224 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002225 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002226 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002227 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002228 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002229 case tok::kw___has_trivial_constructor:
2230 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002231 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002232 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2233 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2234 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002235 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2236 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002237 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002238 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2239 case tok::kw___is_compound: return UTT_IsCompound;
2240 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002241 case tok::kw___is_empty: return UTT_IsEmpty;
2242 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley20c0da72011-04-27 23:09:49 +00002243 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2244 case tok::kw___is_function: return UTT_IsFunction;
2245 case tok::kw___is_fundamental: return UTT_IsFundamental;
2246 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00002247 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2248 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2249 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2250 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2251 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002252 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002253 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002254 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002255 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002256 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002257 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002258 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2259 case tok::kw___is_scalar: return UTT_IsScalar;
2260 case tok::kw___is_signed: return UTT_IsSigned;
2261 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2262 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002263 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002264 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002265 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2266 case tok::kw___is_void: return UTT_IsVoid;
2267 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002268 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002269}
2270
2271static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2272 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002273 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002274 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002275 case tok::kw___is_convertible: return BTT_IsConvertible;
2276 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002277 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002278 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002279 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002280}
2281
John Wiegley21ff2e52011-04-28 00:16:57 +00002282static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2283 switch(kind) {
2284 default: llvm_unreachable("Not a known binary type trait");
2285 case tok::kw___array_rank: return ATT_ArrayRank;
2286 case tok::kw___array_extent: return ATT_ArrayExtent;
2287 }
2288}
2289
John Wiegley55262202011-04-25 06:54:41 +00002290static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2291 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002292 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002293 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2294 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2295 }
2296}
2297
Sebastian Redl64b45f72009-01-05 20:52:13 +00002298/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2299/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2300/// templates.
2301///
2302/// primary-expression:
2303/// [GNU] unary-type-trait '(' type-id ')'
2304///
John McCall60d7b3a2010-08-24 06:29:42 +00002305ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002306 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2307 SourceLocation Loc = ConsumeToken();
2308
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002309 BalancedDelimiterTracker T(*this, tok::l_paren);
2310 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002311 return ExprError();
2312
2313 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2314 // there will be cryptic errors about mismatched parentheses and missing
2315 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002316 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002317
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002318 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002319
Douglas Gregor809070a2009-02-18 17:45:20 +00002320 if (Ty.isInvalid())
2321 return ExprError();
2322
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002323 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002324}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002325
Francois Pichet6ad6f282010-12-07 00:08:36 +00002326/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2327/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2328/// templates.
2329///
2330/// primary-expression:
2331/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2332///
2333ExprResult Parser::ParseBinaryTypeTrait() {
2334 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2335 SourceLocation Loc = ConsumeToken();
2336
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002337 BalancedDelimiterTracker T(*this, tok::l_paren);
2338 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002339 return ExprError();
2340
2341 TypeResult LhsTy = ParseTypeName();
2342 if (LhsTy.isInvalid()) {
2343 SkipUntil(tok::r_paren);
2344 return ExprError();
2345 }
2346
2347 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2348 SkipUntil(tok::r_paren);
2349 return ExprError();
2350 }
2351
2352 TypeResult RhsTy = ParseTypeName();
2353 if (RhsTy.isInvalid()) {
2354 SkipUntil(tok::r_paren);
2355 return ExprError();
2356 }
2357
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002358 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002359
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002360 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2361 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002362}
2363
John Wiegley21ff2e52011-04-28 00:16:57 +00002364/// ParseArrayTypeTrait - Parse the built-in array type-trait
2365/// pseudo-functions.
2366///
2367/// primary-expression:
2368/// [Embarcadero] '__array_rank' '(' type-id ')'
2369/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2370///
2371ExprResult Parser::ParseArrayTypeTrait() {
2372 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2373 SourceLocation Loc = ConsumeToken();
2374
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002375 BalancedDelimiterTracker T(*this, tok::l_paren);
2376 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002377 return ExprError();
2378
2379 TypeResult Ty = ParseTypeName();
2380 if (Ty.isInvalid()) {
2381 SkipUntil(tok::comma);
2382 SkipUntil(tok::r_paren);
2383 return ExprError();
2384 }
2385
2386 switch (ATT) {
2387 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002388 T.consumeClose();
2389 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2390 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002391 }
2392 case ATT_ArrayExtent: {
2393 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2394 SkipUntil(tok::r_paren);
2395 return ExprError();
2396 }
2397
2398 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002399 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002400
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002401 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2402 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002403 }
2404 default:
2405 break;
2406 }
2407 return ExprError();
2408}
2409
John Wiegley55262202011-04-25 06:54:41 +00002410/// ParseExpressionTrait - Parse built-in expression-trait
2411/// pseudo-functions like __is_lvalue_expr( xxx ).
2412///
2413/// primary-expression:
2414/// [Embarcadero] expression-trait '(' expression ')'
2415///
2416ExprResult Parser::ParseExpressionTrait() {
2417 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2418 SourceLocation Loc = ConsumeToken();
2419
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002420 BalancedDelimiterTracker T(*this, tok::l_paren);
2421 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002422 return ExprError();
2423
2424 ExprResult Expr = ParseExpression();
2425
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002426 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002427
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002428 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2429 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002430}
2431
2432
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002433/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2434/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2435/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002436ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002437Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002438 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002439 BalancedDelimiterTracker &Tracker) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002440 assert(getLang().CPlusPlus && "Should only be called for C++!");
2441 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2442 assert(isTypeIdInParens() && "Not a type-id!");
2443
John McCall60d7b3a2010-08-24 06:29:42 +00002444 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002445 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002446
2447 // We need to disambiguate a very ugly part of the C++ syntax:
2448 //
2449 // (T())x; - type-id
2450 // (T())*x; - type-id
2451 // (T())/x; - expression
2452 // (T()); - expression
2453 //
2454 // The bad news is that we cannot use the specialized tentative parser, since
2455 // it can only verify that the thing inside the parens can be parsed as
2456 // type-id, it is not useful for determining the context past the parens.
2457 //
2458 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002459 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002460 //
2461 // It uses a scheme similar to parsing inline methods. The parenthesized
2462 // tokens are cached, the context that follows is determined (possibly by
2463 // parsing a cast-expression), and then we re-introduce the cached tokens
2464 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002465
Mike Stump1eb44332009-09-09 15:08:12 +00002466 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002467 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002468
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002469 // Store the tokens of the parentheses. We will parse them after we determine
2470 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002471 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002472 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002473 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002474 return ExprError();
2475 }
2476
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002477 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002478 ParseAs = CompoundLiteral;
2479 } else {
2480 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002481 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2482 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2483 NotCastExpr = true;
2484 } else {
2485 // Try parsing the cast-expression that may follow.
2486 // If it is not a cast-expression, NotCastExpr will be true and no token
2487 // will be consumed.
2488 Result = ParseCastExpression(false/*isUnaryExpression*/,
2489 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002490 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002491 // type-id has priority.
2492 true/*isTypeCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002493 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002494
2495 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2496 // an expression.
2497 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002498 }
2499
Mike Stump1eb44332009-09-09 15:08:12 +00002500 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002501 Toks.push_back(Tok);
2502 // Re-enter the stored parenthesized tokens into the token stream, so we may
2503 // parse them now.
2504 PP.EnterTokenStream(Toks.data(), Toks.size(),
2505 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2506 // Drop the current token and bring the first cached one. It's the same token
2507 // as when we entered this function.
2508 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002509
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002510 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002511 // Parse the type declarator.
2512 DeclSpec DS(AttrFactory);
2513 ParseSpecifierQualifierList(DS);
2514 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2515 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002516
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002517 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002518 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002519
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002520 if (ParseAs == CompoundLiteral) {
2521 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002522 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002523 return ParseCompoundLiteralExpression(Ty.get(),
2524 Tracker.getOpenLocation(),
2525 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002526 }
Mike Stump1eb44332009-09-09 15:08:12 +00002527
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002528 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2529 assert(ParseAs == CastExpr);
2530
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002531 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002532 return ExprError();
2533
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002534 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002535 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002536 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2537 DeclaratorInfo, CastTy,
2538 Tracker.getCloseLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002539 return move(Result);
2540 }
Mike Stump1eb44332009-09-09 15:08:12 +00002541
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002542 // Not a compound literal, and not followed by a cast-expression.
2543 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002544
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002545 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002546 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002547 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002548 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2549 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002550
2551 // Match the ')'.
2552 if (Result.isInvalid()) {
2553 SkipUntil(tok::r_paren);
2554 return ExprError();
2555 }
Mike Stump1eb44332009-09-09 15:08:12 +00002556
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002557 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002558 return move(Result);
2559}