blob: dfc77e17d4959362f86fc1ba095cbd9db009868a [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
David Blaikie6796fc12011-11-07 03:30:03 +0000286 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000287
288 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
289 TemplateId->getTemplateArgs(),
290 TemplateId->NumArgs);
291
292 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
293 /*FIXME:*/SourceLocation(),
294 SS,
295 TemplateId->Template,
296 TemplateId->TemplateNameLoc,
297 TemplateId->LAngleLoc,
298 TemplateArgsPtr,
299 TemplateId->RAngleLoc,
300 CCLoc,
301 EnteringContext)) {
302 SourceLocation StartLoc
303 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
304 : TemplateId->TemplateNameLoc;
305 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000306 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000307
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000308 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000309 }
310
Chris Lattner5c7f7862009-06-26 03:52:38 +0000311
312 // The rest of the nested-name-specifier possibilities start with
313 // tok::identifier.
314 if (Tok.isNot(tok::identifier))
315 break;
316
317 IdentifierInfo &II = *Tok.getIdentifierInfo();
318
319 // nested-name-specifier:
320 // type-name '::'
321 // namespace-name '::'
322 // nested-name-specifier identifier '::'
323 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000324
325 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
326 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000327 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000328 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
329 Tok.getLocation(),
330 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000331 EnteringContext) &&
332 // If the token after the colon isn't an identifier, it's still an
333 // error, but they probably meant something else strange so don't
334 // recover like this.
335 PP.LookAhead(1).is(tok::identifier)) {
336 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000337 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000338
339 // Recover as if the user wrote '::'.
340 Next.setKind(tok::coloncolon);
341 }
Chris Lattner46646492009-12-07 01:36:53 +0000342 }
343
Chris Lattner5c7f7862009-06-26 03:52:38 +0000344 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000345 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000346 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000347 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000348 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000349 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000350 }
351
Chris Lattner5c7f7862009-06-26 03:52:38 +0000352 // We have an identifier followed by a '::'. Lookup this name
353 // as the name in a nested-name-specifier.
354 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000355 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
356 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000357 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000359 HasScopeSpecifier = true;
360 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
361 ObjectType, EnteringContext, SS))
362 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
363
Chris Lattner5c7f7862009-06-26 03:52:38 +0000364 continue;
365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Richard Trieu950be712011-09-19 19:01:00 +0000367 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000368
Chris Lattner5c7f7862009-06-26 03:52:38 +0000369 // nested-name-specifier:
370 // type-name '<'
371 if (Next.is(tok::less)) {
372 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000373 UnqualifiedId TemplateName;
374 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000375 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000376 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000377 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000378 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000379 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000380 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000381 Template,
382 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000383 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000384 // with a template-id annotation. We do not permit the
385 // template-id to be translated into a type annotation,
386 // because some clients (e.g., the parsing of class template
387 // specializations) still want to see the original template-id
388 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000389 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000390 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000391 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000392 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000393 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000394 }
395
396 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000397 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000398 // We have something like t::getAs<T>, where getAs is a
399 // member of an unknown specialization. However, this will only
400 // parse correctly as a template, so suggest the keyword 'template'
401 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000402 unsigned DiagID = diag::err_missing_dependent_template_keyword;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000403 if (getLang().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000404 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000405
406 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000407 << II.getName()
408 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
409
Douglas Gregord6ab2322010-06-16 23:00:59 +0000410 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000411 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000412 Tok.getLocation(), SS,
413 TemplateName, ObjectType,
414 EnteringContext, Template)) {
415 // Consume the identifier.
416 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000417 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000418 SourceLocation(), false))
419 return true;
420 }
421 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000422 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000423
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000424 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000425 }
426 }
427
Douglas Gregor39a8de12009-02-25 19:37:18 +0000428 // We don't have any tokens that form the beginning of a
429 // nested-name-specifier, so we're done.
430 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Douglas Gregord4dca082010-02-24 18:44:31 +0000433 // Even if we didn't see any pieces of a nested-name-specifier, we
434 // still check whether there is a tilde in this position, which
435 // indicates a potential pseudo-destructor.
436 if (CheckForDestructor && Tok.is(tok::tilde))
437 *MayBePseudoDestructor = true;
438
John McCall9ba61662010-02-26 08:45:28 +0000439 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000440}
441
442/// ParseCXXIdExpression - Handle id-expression.
443///
444/// id-expression:
445/// unqualified-id
446/// qualified-id
447///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000448/// qualified-id:
449/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
450/// '::' identifier
451/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000452/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000453///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000454/// NOTE: The standard specifies that, for qualified-id, the parser does not
455/// expect:
456///
457/// '::' conversion-function-id
458/// '::' '~' class-name
459///
460/// This may cause a slight inconsistency on diagnostics:
461///
462/// class C {};
463/// namespace A {}
464/// void f() {
465/// :: A :: ~ C(); // Some Sema error about using destructor with a
466/// // namespace.
467/// :: ~ C(); // Some Parser error like 'unexpected ~'.
468/// }
469///
470/// We simplify the parser a bit and make it work like:
471///
472/// qualified-id:
473/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
474/// '::' unqualified-id
475///
476/// That way Sema can handle and report similar errors for namespaces and the
477/// global scope.
478///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000479/// The isAddressOfOperand parameter indicates that this id-expression is a
480/// direct operand of the address-of operator. This is, besides member contexts,
481/// the only place where a qualified-id naming a non-static class member may
482/// appear.
483///
John McCall60d7b3a2010-08-24 06:29:42 +0000484ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000485 // qualified-id:
486 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
487 // '::' unqualified-id
488 //
489 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000490 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000491
492 UnqualifiedId Name;
493 if (ParseUnqualifiedId(SS,
494 /*EnteringContext=*/false,
495 /*AllowDestructorName=*/false,
496 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000497 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000498 Name))
499 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000500
501 // This is only the direct operand of an & operator if it is not
502 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000503 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
504 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000505
Douglas Gregor23c94db2010-07-02 17:43:08 +0000506 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000507 isAddressOfOperand);
508
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000509}
510
Douglas Gregorae7902c2011-08-04 15:30:47 +0000511/// ParseLambdaExpression - Parse a C++0x lambda expression.
512///
513/// lambda-expression:
514/// lambda-introducer lambda-declarator[opt] compound-statement
515///
516/// lambda-introducer:
517/// '[' lambda-capture[opt] ']'
518///
519/// lambda-capture:
520/// capture-default
521/// capture-list
522/// capture-default ',' capture-list
523///
524/// capture-default:
525/// '&'
526/// '='
527///
528/// capture-list:
529/// capture
530/// capture-list ',' capture
531///
532/// capture:
533/// identifier
534/// '&' identifier
535/// 'this'
536///
537/// lambda-declarator:
538/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
539/// 'mutable'[opt] exception-specification[opt]
540/// trailing-return-type[opt]
541///
542ExprResult Parser::ParseLambdaExpression() {
543 // Parse lambda-introducer.
544 LambdaIntroducer Intro;
545
546 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
547 if (DiagID) {
548 Diag(Tok, DiagID.getValue());
549 SkipUntil(tok::r_square);
550 }
551
552 return ParseLambdaExpressionAfterIntroducer(Intro);
553}
554
555/// TryParseLambdaExpression - Use lookahead and potentially tentative
556/// parsing to determine if we are looking at a C++0x lambda expression, and parse
557/// it if we are.
558///
559/// If we are not looking at a lambda expression, returns ExprError().
560ExprResult Parser::TryParseLambdaExpression() {
561 assert(getLang().CPlusPlus0x
562 && Tok.is(tok::l_square)
563 && "Not at the start of a possible lambda expression.");
564
565 const Token Next = NextToken(), After = GetLookAheadToken(2);
566
567 // If lookahead indicates this is a lambda...
568 if (Next.is(tok::r_square) || // []
569 Next.is(tok::equal) || // [=
570 (Next.is(tok::amp) && // [&] or [&,
571 (After.is(tok::r_square) ||
572 After.is(tok::comma))) ||
573 (Next.is(tok::identifier) && // [identifier]
574 After.is(tok::r_square))) {
575 return ParseLambdaExpression();
576 }
577
578 // If lookahead indicates this is an Objective-C message...
579 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
580 return ExprError();
581 }
582
583 LambdaIntroducer Intro;
584 if (TryParseLambdaIntroducer(Intro))
585 return ExprError();
586 return ParseLambdaExpressionAfterIntroducer(Intro);
587}
588
589/// ParseLambdaExpression - Parse a lambda introducer.
590///
591/// Returns a DiagnosticID if it hit something unexpected.
592llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
593 typedef llvm::Optional<unsigned> DiagResult;
594
595 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000596 BalancedDelimiterTracker T(*this, tok::l_square);
597 T.consumeOpen();
598
599 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000600
601 bool first = true;
602
603 // Parse capture-default.
604 if (Tok.is(tok::amp) &&
605 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
606 Intro.Default = LCD_ByRef;
607 ConsumeToken();
608 first = false;
609 } else if (Tok.is(tok::equal)) {
610 Intro.Default = LCD_ByCopy;
611 ConsumeToken();
612 first = false;
613 }
614
615 while (Tok.isNot(tok::r_square)) {
616 if (!first) {
617 if (Tok.isNot(tok::comma))
618 return DiagResult(diag::err_expected_comma_or_rsquare);
619 ConsumeToken();
620 }
621
622 first = false;
623
624 // Parse capture.
625 LambdaCaptureKind Kind = LCK_ByCopy;
626 SourceLocation Loc;
627 IdentifierInfo* Id = 0;
628
629 if (Tok.is(tok::kw_this)) {
630 Kind = LCK_This;
631 Loc = ConsumeToken();
632 } else {
633 if (Tok.is(tok::amp)) {
634 Kind = LCK_ByRef;
635 ConsumeToken();
636 }
637
638 if (Tok.is(tok::identifier)) {
639 Id = Tok.getIdentifierInfo();
640 Loc = ConsumeToken();
641 } else if (Tok.is(tok::kw_this)) {
642 // FIXME: If we want to suggest a fixit here, will need to return more
643 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
644 // Clear()ed to prevent emission in case of tentative parsing?
645 return DiagResult(diag::err_this_captured_by_reference);
646 } else {
647 return DiagResult(diag::err_expected_capture);
648 }
649 }
650
651 Intro.addCapture(Kind, Loc, Id);
652 }
653
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000654 T.consumeClose();
655 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000656
657 return DiagResult();
658}
659
660/// TryParseLambdaExpression - Tentatively parse a lambda introducer.
661///
662/// Returns true if it hit something unexpected.
663bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
664 TentativeParsingAction PA(*this);
665
666 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
667
668 if (DiagID) {
669 PA.Revert();
670 return true;
671 }
672
673 PA.Commit();
674 return false;
675}
676
677/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
678/// expression.
679ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
680 LambdaIntroducer &Intro) {
Richard Smith7fe62082011-10-15 05:09:34 +0000681 Diag(Intro.Range.getBegin(), diag::warn_cxx98_compat_lambda);
682
Douglas Gregorae7902c2011-08-04 15:30:47 +0000683 // Parse lambda-declarator[opt].
684 DeclSpec DS(AttrFactory);
685 Declarator D(DS, Declarator::PrototypeContext);
686
687 if (Tok.is(tok::l_paren)) {
688 ParseScope PrototypeScope(this,
689 Scope::FunctionPrototypeScope |
690 Scope::DeclScope);
691
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000692 SourceLocation DeclLoc, DeclEndLoc;
693 BalancedDelimiterTracker T(*this, tok::l_paren);
694 T.consumeOpen();
695 DeclLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000696
697 // Parse parameter-declaration-clause.
698 ParsedAttributes Attr(AttrFactory);
699 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
700 SourceLocation EllipsisLoc;
701
702 if (Tok.isNot(tok::r_paren))
703 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
704
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000705 T.consumeClose();
706 DeclEndLoc = T.getCloseLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000707
708 // Parse 'mutable'[opt].
709 SourceLocation MutableLoc;
710 if (Tok.is(tok::kw_mutable)) {
711 MutableLoc = ConsumeToken();
712 DeclEndLoc = MutableLoc;
713 }
714
715 // Parse exception-specification[opt].
716 ExceptionSpecificationType ESpecType = EST_None;
717 SourceRange ESpecRange;
718 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
719 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
720 ExprResult NoexceptExpr;
721 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
722 DynamicExceptions,
723 DynamicExceptionRanges,
724 NoexceptExpr);
725
726 if (ESpecType != EST_None)
727 DeclEndLoc = ESpecRange.getEnd();
728
729 // Parse attribute-specifier[opt].
730 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
731
732 // Parse trailing-return-type[opt].
733 ParsedType TrailingReturnType;
734 if (Tok.is(tok::arrow)) {
735 SourceRange Range;
736 TrailingReturnType = ParseTrailingReturnType(Range).get();
737 if (Range.getEnd().isValid())
738 DeclEndLoc = Range.getEnd();
739 }
740
741 PrototypeScope.Exit();
742
743 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
744 /*isVariadic=*/EllipsisLoc.isValid(),
745 EllipsisLoc,
746 ParamInfo.data(), ParamInfo.size(),
747 DS.getTypeQualifiers(),
748 /*RefQualifierIsLValueRef=*/true,
749 /*RefQualifierLoc=*/SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +0000750 /*ConstQualifierLoc=*/SourceLocation(),
751 /*VolatileQualifierLoc=*/SourceLocation(),
Douglas Gregorae7902c2011-08-04 15:30:47 +0000752 MutableLoc,
753 ESpecType, ESpecRange.getBegin(),
754 DynamicExceptions.data(),
755 DynamicExceptionRanges.data(),
756 DynamicExceptions.size(),
757 NoexceptExpr.isUsable() ?
758 NoexceptExpr.get() : 0,
759 DeclLoc, DeclEndLoc, D,
760 TrailingReturnType),
761 Attr, DeclEndLoc);
762 }
763
764 // Parse compound-statement.
765 if (Tok.is(tok::l_brace)) {
766 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
767 // it.
768 ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
769 Scope::BreakScope | Scope::ContinueScope |
770 Scope::DeclScope);
771
772 StmtResult Stmt(ParseCompoundStatementBody());
773
774 BodyScope.Exit();
775 } else {
776 Diag(Tok, diag::err_expected_lambda_body);
777 }
778
779 return ExprEmpty();
780}
781
Reid Spencer5f016e22007-07-11 17:01:13 +0000782/// ParseCXXCasts - This handles the various ways to cast expressions to another
783/// type.
784///
785/// postfix-expression: [C++ 5.2p1]
786/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
787/// 'static_cast' '<' type-name '>' '(' expression ')'
788/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
789/// 'const_cast' '<' type-name '>' '(' expression ')'
790///
John McCall60d7b3a2010-08-24 06:29:42 +0000791ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 tok::TokenKind Kind = Tok.getKind();
793 const char *CastName = 0; // For error messages
794
795 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000796 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 case tok::kw_const_cast: CastName = "const_cast"; break;
798 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
799 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
800 case tok::kw_static_cast: CastName = "static_cast"; break;
801 }
802
803 SourceLocation OpLoc = ConsumeToken();
804 SourceLocation LAngleBracketLoc = Tok.getLocation();
805
Richard Smithea698b32011-04-14 21:45:45 +0000806 // Check for "<::" which is parsed as "[:". If found, fix token stream,
807 // diagnose error, suggest fix, and recover parsing.
808 Token Next = NextToken();
809 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
810 AreTokensAdjacent(PP, Tok, Next))
811 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
812
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000814 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000815
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000816 // Parse the common declaration-specifiers piece.
817 DeclSpec DS(AttrFactory);
818 ParseSpecifierQualifierList(DS);
819
820 // Parse the abstract-declarator, if present.
821 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
822 ParseDeclarator(DeclaratorInfo);
823
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 SourceLocation RAngleBracketLoc = Tok.getLocation();
825
Chris Lattner1ab3b962008-11-18 07:48:38 +0000826 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000827 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000828
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000829 SourceLocation LParenLoc, RParenLoc;
830 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +0000831
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000832 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000833 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000834
John McCall60d7b3a2010-08-24 06:29:42 +0000835 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000837 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000838 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +0000839
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000840 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +0000841 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000842 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +0000843 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000844 T.getOpenLocation(), Result.take(),
845 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000846
Sebastian Redl20df9b72008-12-11 22:51:44 +0000847 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000848}
849
Sebastian Redlc42e1182008-11-11 11:37:55 +0000850/// ParseCXXTypeid - This handles the C++ typeid expression.
851///
852/// postfix-expression: [C++ 5.2p1]
853/// 'typeid' '(' expression ')'
854/// 'typeid' '(' type-id ')'
855///
John McCall60d7b3a2010-08-24 06:29:42 +0000856ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000857 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
858
859 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000860 SourceLocation LParenLoc, RParenLoc;
861 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000862
863 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000864 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000865 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000866 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000867
John McCall60d7b3a2010-08-24 06:29:42 +0000868 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000869
870 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000871 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000872
873 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000874 T.consumeClose();
875 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000876 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000877 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000878
879 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000880 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000881 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000882 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000883 // When typeid is applied to an expression other than an lvalue of a
884 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000885 // operand (Clause 5).
886 //
Mike Stump1eb44332009-09-09 15:08:12 +0000887 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000888 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000889 // we the expression is potentially potentially evaluated.
890 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000891 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000892 Result = ParseExpression();
893
894 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000895 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000896 SkipUntil(tok::r_paren);
897 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000898 T.consumeClose();
899 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000900 if (RParenLoc.isInvalid())
901 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000902
Sebastian Redlc42e1182008-11-11 11:37:55 +0000903 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000904 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000905 }
906 }
907
Sebastian Redl20df9b72008-12-11 22:51:44 +0000908 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000909}
910
Francois Pichet01b7c302010-09-08 12:20:18 +0000911/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
912///
913/// '__uuidof' '(' expression ')'
914/// '__uuidof' '(' type-id ')'
915///
916ExprResult Parser::ParseCXXUuidof() {
917 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
918
919 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000920 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +0000921
922 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000923 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +0000924 return ExprError();
925
926 ExprResult Result;
927
928 if (isTypeIdInParens()) {
929 TypeResult Ty = ParseTypeName();
930
931 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000932 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +0000933
934 if (Ty.isInvalid())
935 return ExprError();
936
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000937 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
938 Ty.get().getAsOpaquePtr(),
939 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +0000940 } else {
941 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
942 Result = ParseExpression();
943
944 // Match the ')'.
945 if (Result.isInvalid())
946 SkipUntil(tok::r_paren);
947 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000948 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +0000949
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000950 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
951 /*isType=*/false,
952 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +0000953 }
954 }
955
956 return move(Result);
957}
958
Douglas Gregord4dca082010-02-24 18:44:31 +0000959/// \brief Parse a C++ pseudo-destructor expression after the base,
960/// . or -> operator, and nested-name-specifier have already been
961/// parsed.
962///
963/// postfix-expression: [C++ 5.2]
964/// postfix-expression . pseudo-destructor-name
965/// postfix-expression -> pseudo-destructor-name
966///
967/// pseudo-destructor-name:
968/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
969/// ::[opt] nested-name-specifier template simple-template-id ::
970/// ~type-name
971/// ::[opt] nested-name-specifier[opt] ~type-name
972///
John McCall60d7b3a2010-08-24 06:29:42 +0000973ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000974Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
975 tok::TokenKind OpKind,
976 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000977 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000978 // We're parsing either a pseudo-destructor-name or a dependent
979 // member access that has the same form as a
980 // pseudo-destructor-name. We parse both in the same way and let
981 // the action model sort them out.
982 //
983 // Note that the ::[opt] nested-name-specifier[opt] has already
984 // been parsed, and if there was a simple-template-id, it has
985 // been coalesced into a template-id annotation token.
986 UnqualifiedId FirstTypeName;
987 SourceLocation CCLoc;
988 if (Tok.is(tok::identifier)) {
989 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
990 ConsumeToken();
991 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
992 CCLoc = ConsumeToken();
993 } else if (Tok.is(tok::annot_template_id)) {
994 FirstTypeName.setTemplateId(
995 (TemplateIdAnnotation *)Tok.getAnnotationValue());
996 ConsumeToken();
997 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
998 CCLoc = ConsumeToken();
999 } else {
1000 FirstTypeName.setIdentifier(0, SourceLocation());
1001 }
1002
1003 // Parse the tilde.
1004 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1005 SourceLocation TildeLoc = ConsumeToken();
1006 if (!Tok.is(tok::identifier)) {
1007 Diag(Tok, diag::err_destructor_tilde_identifier);
1008 return ExprError();
1009 }
1010
1011 // Parse the second type.
1012 UnqualifiedId SecondTypeName;
1013 IdentifierInfo *Name = Tok.getIdentifierInfo();
1014 SourceLocation NameLoc = ConsumeToken();
1015 SecondTypeName.setIdentifier(Name, NameLoc);
1016
1017 // If there is a '<', the second type name is a template-id. Parse
1018 // it as such.
1019 if (Tok.is(tok::less) &&
1020 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001021 SecondTypeName, /*AssumeTemplateName=*/true,
1022 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +00001023 return ExprError();
1024
John McCall9ae2f072010-08-23 23:25:46 +00001025 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1026 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001027 SS, FirstTypeName, CCLoc,
1028 TildeLoc, SecondTypeName,
1029 Tok.is(tok::l_paren));
1030}
1031
Reid Spencer5f016e22007-07-11 17:01:13 +00001032/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1033///
1034/// boolean-literal: [C++ 2.13.5]
1035/// 'true'
1036/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001037ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001039 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001040}
Chris Lattner50dd2892008-02-26 00:51:44 +00001041
1042/// ParseThrowExpression - This handles the C++ throw expression.
1043///
1044/// throw-expression: [C++ 15]
1045/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001046ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001047 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001048 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001049
Chris Lattner2a2819a2008-04-06 06:02:23 +00001050 // If the current token isn't the start of an assignment-expression,
1051 // then the expression is not present. This handles things like:
1052 // "C ? throw : (void)42", which is crazy but legal.
1053 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1054 case tok::semi:
1055 case tok::r_paren:
1056 case tok::r_square:
1057 case tok::r_brace:
1058 case tok::colon:
1059 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001060 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001061
Chris Lattner2a2819a2008-04-06 06:02:23 +00001062 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001063 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +00001064 if (Expr.isInvalid()) return move(Expr);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001065 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001066 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001067}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001068
1069/// ParseCXXThis - This handles the C++ 'this' pointer.
1070///
1071/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1072/// a non-lvalue expression whose value is the address of the object for which
1073/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001074ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001075 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1076 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001077 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001078}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001079
1080/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1081/// Can be interpreted either as function-style casting ("int(x)")
1082/// or class type construction ("ClassType(x,y,z)")
1083/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001084/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001085///
1086/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001087/// simple-type-specifier '(' expression-list[opt] ')'
1088/// [C++0x] simple-type-specifier braced-init-list
1089/// typename-specifier '(' expression-list[opt] ')'
1090/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001091///
John McCall60d7b3a2010-08-24 06:29:42 +00001092ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001093Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001094 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001095 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001096
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001097 assert((Tok.is(tok::l_paren) ||
1098 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1099 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001100
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001101 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001102
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001103 // FIXME: Convert to a proper type construct expression.
1104 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001105
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001106 } else {
1107 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1108
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001109 BalancedDelimiterTracker T(*this, tok::l_paren);
1110 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001111
1112 ExprVector Exprs(Actions);
1113 CommaLocsTy CommaLocs;
1114
1115 if (Tok.isNot(tok::r_paren)) {
1116 if (ParseExpressionList(Exprs, CommaLocs)) {
1117 SkipUntil(tok::r_paren);
1118 return ExprError();
1119 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001120 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001121
1122 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001123 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001124
1125 // TypeRep could be null, if it references an invalid typedef.
1126 if (!TypeRep)
1127 return ExprError();
1128
1129 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1130 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001131 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
1132 move_arg(Exprs),
1133 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001134 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001135}
1136
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001137/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001138///
1139/// condition:
1140/// expression
1141/// type-specifier-seq declarator '=' assignment-expression
1142/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1143/// '=' assignment-expression
1144///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001145/// \param ExprResult if the condition was parsed as an expression, the
1146/// parsed expression.
1147///
1148/// \param DeclResult if the condition was parsed as a declaration, the
1149/// parsed declaration.
1150///
Douglas Gregor586596f2010-05-06 17:25:47 +00001151/// \param Loc The location of the start of the statement that requires this
1152/// condition, e.g., the "for" in a for loop.
1153///
1154/// \param ConvertToBoolean Whether the condition expression should be
1155/// converted to a boolean value.
1156///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001157/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001158bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1159 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001160 SourceLocation Loc,
1161 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001162 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001163 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001164 cutOffParsing();
1165 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001166 }
1167
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001168 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001169 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001170 ExprOut = ParseExpression(); // expression
1171 DeclOut = 0;
1172 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001173 return true;
1174
1175 // If required, convert to a boolean value.
1176 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001177 ExprOut
1178 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1179 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001180 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001181
1182 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001183 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001184 ParseSpecifierQualifierList(DS);
1185
1186 // declarator
1187 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1188 ParseDeclarator(DeclaratorInfo);
1189
1190 // simple-asm-expr[opt]
1191 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001192 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001193 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001194 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001195 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001196 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001197 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001198 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001199 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001200 }
1201
1202 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001203 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001204
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001205 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001206 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001207 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001208 DeclOut = Dcl.get();
1209 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001210
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001211 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001212 if (isTokenEqualOrMistypedEqualEqual(
1213 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001214 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001215 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001216 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +00001217 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1218 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001219 } else {
1220 // FIXME: C++0x allows a braced-init-list
1221 Diag(Tok, diag::err_expected_equal_after_declarator);
1222 }
1223
Douglas Gregor586596f2010-05-06 17:25:47 +00001224 // FIXME: Build a reference to this declaration? Convert it to bool?
1225 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001226
1227 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001228
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001229 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001230}
1231
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001232/// \brief Determine whether the current token starts a C++
1233/// simple-type-specifier.
1234bool Parser::isCXXSimpleTypeSpecifier() const {
1235 switch (Tok.getKind()) {
1236 case tok::annot_typename:
1237 case tok::kw_short:
1238 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001239 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001240 case tok::kw_signed:
1241 case tok::kw_unsigned:
1242 case tok::kw_void:
1243 case tok::kw_char:
1244 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001245 case tok::kw_half:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001246 case tok::kw_float:
1247 case tok::kw_double:
1248 case tok::kw_wchar_t:
1249 case tok::kw_char16_t:
1250 case tok::kw_char32_t:
1251 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +00001252 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001253 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +00001254 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001255 return true;
1256
1257 default:
1258 break;
1259 }
1260
1261 return false;
1262}
1263
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001264/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1265/// This should only be called when the current token is known to be part of
1266/// simple-type-specifier.
1267///
1268/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001269/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001270/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1271/// char
1272/// wchar_t
1273/// bool
1274/// short
1275/// int
1276/// long
1277/// signed
1278/// unsigned
1279/// float
1280/// double
1281/// void
1282/// [GNU] typeof-specifier
1283/// [C++0x] auto [TODO]
1284///
1285/// type-name:
1286/// class-name
1287/// enum-name
1288/// typedef-name
1289///
1290void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1291 DS.SetRangeStart(Tok.getLocation());
1292 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001293 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001294 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001296 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001297 case tok::identifier: // foo::bar
1298 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001299 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001300 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001301 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001302
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001303 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001304 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001305 if (getTypeAnnotation(Tok))
1306 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1307 getTypeAnnotation(Tok));
1308 else
1309 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001310
1311 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1312 ConsumeToken();
1313
1314 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1315 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1316 // Objective-C interface. If we don't have Objective-C or a '<', this is
1317 // just a normal reference to a typedef name.
1318 if (Tok.is(tok::less) && getLang().ObjC1)
1319 ParseObjCProtocolQualifiers(DS);
1320
1321 DS.Finish(Diags, PP);
1322 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001323 }
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001325 // builtin types
1326 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001327 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001328 break;
1329 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001330 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001331 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001332 case tok::kw___int64:
1333 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1334 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001335 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001336 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001337 break;
1338 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001339 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001340 break;
1341 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001342 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001343 break;
1344 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001345 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001346 break;
1347 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001348 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001349 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001350 case tok::kw_half:
1351 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1352 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001353 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001354 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001355 break;
1356 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001357 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001358 break;
1359 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001360 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001361 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001362 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001363 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001364 break;
1365 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001366 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001367 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001368 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001369 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001370 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001372 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001373 // GNU typeof support.
1374 case tok::kw_typeof:
1375 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001376 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001377 return;
1378 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001379 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001380 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1381 else
1382 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001383 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001384 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001385}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001386
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001387/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1388/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1389/// e.g., "const short int". Note that the DeclSpec is *not* finished
1390/// by parsing the type-specifier-seq, because these sequences are
1391/// typically followed by some form of declarator. Returns true and
1392/// emits diagnostics if this is not a type-specifier-seq, false
1393/// otherwise.
1394///
1395/// type-specifier-seq: [C++ 8.1]
1396/// type-specifier type-specifier-seq[opt]
1397///
1398bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1399 DS.SetRangeStart(Tok.getLocation());
1400 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001401 unsigned DiagID;
1402 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001403
1404 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001405 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1406 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001407 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001408 return true;
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Sebastian Redld9bafa72010-02-03 21:21:43 +00001411 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1412 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1413 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001414
Douglas Gregor396a9f22010-02-24 23:13:13 +00001415 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001416 return false;
1417}
1418
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001419/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1420/// some form.
1421///
1422/// This routine is invoked when a '<' is encountered after an identifier or
1423/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1424/// whether the unqualified-id is actually a template-id. This routine will
1425/// then parse the template arguments and form the appropriate template-id to
1426/// return to the caller.
1427///
1428/// \param SS the nested-name-specifier that precedes this template-id, if
1429/// we're actually parsing a qualified-id.
1430///
1431/// \param Name for constructor and destructor names, this is the actual
1432/// identifier that may be a template-name.
1433///
1434/// \param NameLoc the location of the class-name in a constructor or
1435/// destructor.
1436///
1437/// \param EnteringContext whether we're entering the scope of the
1438/// nested-name-specifier.
1439///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001440/// \param ObjectType if this unqualified-id occurs within a member access
1441/// expression, the type of the base object whose member is being accessed.
1442///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001443/// \param Id as input, describes the template-name or operator-function-id
1444/// that precedes the '<'. If template arguments were parsed successfully,
1445/// will be updated with the template-id.
1446///
Douglas Gregord4dca082010-02-24 18:44:31 +00001447/// \param AssumeTemplateId When true, this routine will assume that the name
1448/// refers to a template without performing name lookup to verify.
1449///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001450/// \returns true if a parse error occurred, false otherwise.
1451bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1452 IdentifierInfo *Name,
1453 SourceLocation NameLoc,
1454 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001455 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001456 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001457 bool AssumeTemplateId,
1458 SourceLocation TemplateKWLoc) {
1459 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1460 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001461
1462 TemplateTy Template;
1463 TemplateNameKind TNK = TNK_Non_template;
1464 switch (Id.getKind()) {
1465 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001466 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001467 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001468 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001469 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001470 Id, ObjectType, EnteringContext,
1471 Template);
1472 if (TNK == TNK_Non_template)
1473 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001474 } else {
1475 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001476 TNK = Actions.isTemplateName(getCurScope(), SS,
1477 TemplateKWLoc.isValid(), Id,
1478 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001479 MemberOfUnknownSpecialization);
1480
1481 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1482 ObjectType && IsTemplateArgumentList()) {
1483 // We have something like t->getAs<T>(), where getAs is a
1484 // member of an unknown specialization. However, this will only
1485 // parse correctly as a template, so suggest the keyword 'template'
1486 // before 'getAs' and treat this as a dependent template name.
1487 std::string Name;
1488 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1489 Name = Id.Identifier->getName();
1490 else {
1491 Name = "operator ";
1492 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1493 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1494 else
1495 Name += Id.Identifier->getName();
1496 }
1497 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1498 << Name
1499 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001500 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001501 SS, Id, ObjectType,
1502 EnteringContext, Template);
1503 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001504 return true;
1505 }
1506 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001507 break;
1508
Douglas Gregor014e88d2009-11-03 23:16:33 +00001509 case UnqualifiedId::IK_ConstructorName: {
1510 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001511 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001512 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001513 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1514 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001515 EnteringContext, Template,
1516 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001517 break;
1518 }
1519
Douglas Gregor014e88d2009-11-03 23:16:33 +00001520 case UnqualifiedId::IK_DestructorName: {
1521 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001522 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001523 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001524 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001525 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001526 TemplateName, ObjectType,
1527 EnteringContext, Template);
1528 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001529 return true;
1530 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001531 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1532 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001533 EnteringContext, Template,
1534 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001535
John McCallb3d87482010-08-24 05:47:05 +00001536 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001537 Diag(NameLoc, diag::err_destructor_template_id)
1538 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001539 return true;
1540 }
1541 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001542 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001543 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001544
1545 default:
1546 return false;
1547 }
1548
1549 if (TNK == TNK_Non_template)
1550 return false;
1551
1552 // Parse the enclosed template argument list.
1553 SourceLocation LAngleLoc, RAngleLoc;
1554 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001555 if (Tok.is(tok::less) &&
1556 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001557 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001558 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001559 RAngleLoc))
1560 return true;
1561
1562 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001563 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1564 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001565 // Form a parsed representation of the template-id to be stored in the
1566 // UnqualifiedId.
1567 TemplateIdAnnotation *TemplateId
1568 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1569
1570 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1571 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001572 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001573 TemplateId->TemplateNameLoc = Id.StartLocation;
1574 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001575 TemplateId->Name = 0;
1576 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1577 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001578 }
1579
Douglas Gregor059101f2011-03-02 00:47:37 +00001580 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001581 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001582 TemplateId->Kind = TNK;
1583 TemplateId->LAngleLoc = LAngleLoc;
1584 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001585 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001586 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001587 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001588 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001589
1590 Id.setTemplateId(TemplateId);
1591 return false;
1592 }
1593
1594 // Bundle the template arguments together.
1595 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001596 TemplateArgs.size());
1597
1598 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001599 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001600 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001601 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001602 RAngleLoc);
1603 if (Type.isInvalid())
1604 return true;
1605
1606 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1607 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1608 else
1609 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1610
1611 return false;
1612}
1613
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001614/// \brief Parse an operator-function-id or conversion-function-id as part
1615/// of a C++ unqualified-id.
1616///
1617/// This routine is responsible only for parsing the operator-function-id or
1618/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001619///
1620/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001621/// operator-function-id: [C++ 13.5]
1622/// 'operator' operator
1623///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001624/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001625/// new delete new[] delete[]
1626/// + - * / % ^ & | ~
1627/// ! = < > += -= *= /= %=
1628/// ^= &= |= << >> >>= <<= == !=
1629/// <= >= && || ++ -- , ->* ->
1630/// () []
1631///
1632/// conversion-function-id: [C++ 12.3.2]
1633/// operator conversion-type-id
1634///
1635/// conversion-type-id:
1636/// type-specifier-seq conversion-declarator[opt]
1637///
1638/// conversion-declarator:
1639/// ptr-operator conversion-declarator[opt]
1640/// \endcode
1641///
1642/// \param The nested-name-specifier that preceded this unqualified-id. If
1643/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1644///
1645/// \param EnteringContext whether we are entering the scope of the
1646/// nested-name-specifier.
1647///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001648/// \param ObjectType if this unqualified-id occurs within a member access
1649/// expression, the type of the base object whose member is being accessed.
1650///
1651/// \param Result on a successful parse, contains the parsed unqualified-id.
1652///
1653/// \returns true if parsing fails, false otherwise.
1654bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001655 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001656 UnqualifiedId &Result) {
1657 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1658
1659 // Consume the 'operator' keyword.
1660 SourceLocation KeywordLoc = ConsumeToken();
1661
1662 // Determine what kind of operator name we have.
1663 unsigned SymbolIdx = 0;
1664 SourceLocation SymbolLocations[3];
1665 OverloadedOperatorKind Op = OO_None;
1666 switch (Tok.getKind()) {
1667 case tok::kw_new:
1668 case tok::kw_delete: {
1669 bool isNew = Tok.getKind() == tok::kw_new;
1670 // Consume the 'new' or 'delete'.
1671 SymbolLocations[SymbolIdx++] = ConsumeToken();
1672 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001673 // Consume the '[' and ']'.
1674 BalancedDelimiterTracker T(*this, tok::l_square);
1675 T.consumeOpen();
1676 T.consumeClose();
1677 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001678 return true;
1679
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001680 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1681 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001682 Op = isNew? OO_Array_New : OO_Array_Delete;
1683 } else {
1684 Op = isNew? OO_New : OO_Delete;
1685 }
1686 break;
1687 }
1688
1689#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1690 case tok::Token: \
1691 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1692 Op = OO_##Name; \
1693 break;
1694#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1695#include "clang/Basic/OperatorKinds.def"
1696
1697 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001698 // Consume the '(' and ')'.
1699 BalancedDelimiterTracker T(*this, tok::l_paren);
1700 T.consumeOpen();
1701 T.consumeClose();
1702 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001703 return true;
1704
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001705 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1706 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001707 Op = OO_Call;
1708 break;
1709 }
1710
1711 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001712 // Consume the '[' and ']'.
1713 BalancedDelimiterTracker T(*this, tok::l_square);
1714 T.consumeOpen();
1715 T.consumeClose();
1716 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001717 return true;
1718
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001719 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1720 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001721 Op = OO_Subscript;
1722 break;
1723 }
1724
1725 case tok::code_completion: {
1726 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001727 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001728 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001729 // Don't try to parse any further.
1730 return true;
1731 }
1732
1733 default:
1734 break;
1735 }
1736
1737 if (Op != OO_None) {
1738 // We have parsed an operator-function-id.
1739 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1740 return false;
1741 }
Sean Hunt0486d742009-11-28 04:44:28 +00001742
1743 // Parse a literal-operator-id.
1744 //
1745 // literal-operator-id: [C++0x 13.5.8]
1746 // operator "" identifier
1747
1748 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
Richard Smith7fe62082011-10-15 05:09:34 +00001749 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001750 if (Tok.getLength() != 2)
1751 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1752 ConsumeStringToken();
1753
1754 if (Tok.isNot(tok::identifier)) {
1755 Diag(Tok.getLocation(), diag::err_expected_ident);
1756 return true;
1757 }
1758
1759 IdentifierInfo *II = Tok.getIdentifierInfo();
1760 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001761 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001762 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001763
1764 // Parse a conversion-function-id.
1765 //
1766 // conversion-function-id: [C++ 12.3.2]
1767 // operator conversion-type-id
1768 //
1769 // conversion-type-id:
1770 // type-specifier-seq conversion-declarator[opt]
1771 //
1772 // conversion-declarator:
1773 // ptr-operator conversion-declarator[opt]
1774
1775 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001776 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001777 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001778 return true;
1779
1780 // Parse the conversion-declarator, which is merely a sequence of
1781 // ptr-operators.
1782 Declarator D(DS, Declarator::TypeNameContext);
1783 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1784
1785 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001786 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001787 if (Ty.isInvalid())
1788 return true;
1789
1790 // Note that this is a conversion-function-id.
1791 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1792 D.getSourceRange().getEnd());
1793 return false;
1794}
1795
1796/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1797/// name of an entity.
1798///
1799/// \code
1800/// unqualified-id: [C++ expr.prim.general]
1801/// identifier
1802/// operator-function-id
1803/// conversion-function-id
1804/// [C++0x] literal-operator-id [TODO]
1805/// ~ class-name
1806/// template-id
1807///
1808/// \endcode
1809///
1810/// \param The nested-name-specifier that preceded this unqualified-id. If
1811/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1812///
1813/// \param EnteringContext whether we are entering the scope of the
1814/// nested-name-specifier.
1815///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001816/// \param AllowDestructorName whether we allow parsing of a destructor name.
1817///
1818/// \param AllowConstructorName whether we allow parsing a constructor name.
1819///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001820/// \param ObjectType if this unqualified-id occurs within a member access
1821/// expression, the type of the base object whose member is being accessed.
1822///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001823/// \param Result on a successful parse, contains the parsed unqualified-id.
1824///
1825/// \returns true if parsing fails, false otherwise.
1826bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1827 bool AllowDestructorName,
1828 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001829 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001830 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001831
1832 // Handle 'A::template B'. This is for template-ids which have not
1833 // already been annotated by ParseOptionalCXXScopeSpecifier().
1834 bool TemplateSpecified = false;
1835 SourceLocation TemplateKWLoc;
1836 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1837 (ObjectType || SS.isSet())) {
1838 TemplateSpecified = true;
1839 TemplateKWLoc = ConsumeToken();
1840 }
1841
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001842 // unqualified-id:
1843 // identifier
1844 // template-id (when it hasn't already been annotated)
1845 if (Tok.is(tok::identifier)) {
1846 // Consume the identifier.
1847 IdentifierInfo *Id = Tok.getIdentifierInfo();
1848 SourceLocation IdLoc = ConsumeToken();
1849
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001850 if (!getLang().CPlusPlus) {
1851 // If we're not in C++, only identifiers matter. Record the
1852 // identifier and return.
1853 Result.setIdentifier(Id, IdLoc);
1854 return false;
1855 }
1856
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001857 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001858 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001859 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001860 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001861 &SS, false, false,
1862 ParsedType(),
1863 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001864 IdLoc, IdLoc);
1865 } else {
1866 // We have parsed an identifier.
1867 Result.setIdentifier(Id, IdLoc);
1868 }
1869
1870 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001871 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001872 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001873 ObjectType, Result,
1874 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001875
1876 return false;
1877 }
1878
1879 // unqualified-id:
1880 // template-id (already parsed and annotated)
1881 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001882 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001883
1884 // If the template-name names the current class, then this is a constructor
1885 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001886 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001887 if (SS.isSet()) {
1888 // C++ [class.qual]p2 specifies that a qualified template-name
1889 // is taken as the constructor name where a constructor can be
1890 // declared. Thus, the template arguments are extraneous, so
1891 // complain about them and remove them entirely.
1892 Diag(TemplateId->TemplateNameLoc,
1893 diag::err_out_of_line_constructor_template_id)
1894 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001895 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001896 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1897 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1898 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001899 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001900 &SS, false, false,
1901 ParsedType(),
1902 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001903 TemplateId->TemplateNameLoc,
1904 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001905 ConsumeToken();
1906 return false;
1907 }
1908
1909 Result.setConstructorTemplateId(TemplateId);
1910 ConsumeToken();
1911 return false;
1912 }
1913
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001914 // We have already parsed a template-id; consume the annotation token as
1915 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001916 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001917 ConsumeToken();
1918 return false;
1919 }
1920
1921 // unqualified-id:
1922 // operator-function-id
1923 // conversion-function-id
1924 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001925 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001926 return true;
1927
Sean Hunte6252d12009-11-28 08:58:14 +00001928 // If we have an operator-function-id or a literal-operator-id and the next
1929 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001930 //
1931 // template-id:
1932 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001933 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1934 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001935 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001936 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1937 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001938 Result,
1939 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001940
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001941 return false;
1942 }
1943
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001944 if (getLang().CPlusPlus &&
1945 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001946 // C++ [expr.unary.op]p10:
1947 // There is an ambiguity in the unary-expression ~X(), where X is a
1948 // class-name. The ambiguity is resolved in favor of treating ~ as a
1949 // unary complement rather than treating ~X as referring to a destructor.
1950
1951 // Parse the '~'.
1952 SourceLocation TildeLoc = ConsumeToken();
1953
1954 // Parse the class-name.
1955 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001956 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001957 return true;
1958 }
1959
1960 // Parse the class-name (or template-name in a simple-template-id).
1961 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1962 SourceLocation ClassNameLoc = ConsumeToken();
1963
Douglas Gregor0278e122010-05-05 05:58:24 +00001964 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001965 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001966 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001967 EnteringContext, ObjectType, Result,
1968 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001969 }
1970
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001971 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001972 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1973 ClassNameLoc, getCurScope(),
1974 SS, ObjectType,
1975 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001976 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001977 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001978
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001979 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001980 return false;
1981 }
1982
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001983 Diag(Tok, diag::err_expected_unqualified_id)
1984 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001985 return true;
1986}
1987
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001988/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1989/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001990///
Chris Lattner59232d32009-01-04 21:25:24 +00001991/// This method is called to parse the new expression after the optional :: has
1992/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1993/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001994///
1995/// new-expression:
1996/// '::'[opt] 'new' new-placement[opt] new-type-id
1997/// new-initializer[opt]
1998/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1999/// new-initializer[opt]
2000///
2001/// new-placement:
2002/// '(' expression-list ')'
2003///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002004/// new-type-id:
2005/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002006/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002007///
2008/// new-declarator:
2009/// ptr-operator new-declarator[opt]
2010/// direct-new-declarator
2011///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002012/// new-initializer:
2013/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002014/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002015///
John McCall60d7b3a2010-08-24 06:29:42 +00002016ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002017Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2018 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2019 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002020
2021 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2022 // second form of new-expression. It can't be a new-type-id.
2023
Sebastian Redla55e52c2008-11-25 22:21:31 +00002024 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002025 SourceLocation PlacementLParen, PlacementRParen;
2026
Douglas Gregor4bd40312010-07-13 15:54:32 +00002027 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002028 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002029 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002030 if (Tok.is(tok::l_paren)) {
2031 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002032 BalancedDelimiterTracker T(*this, tok::l_paren);
2033 T.consumeOpen();
2034 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002035 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2036 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002037 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002038 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002039
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002040 T.consumeClose();
2041 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002042 if (PlacementRParen.isInvalid()) {
2043 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002044 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002045 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002046
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002047 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002048 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002049 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002050 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002051 } else {
2052 // We still need the type.
2053 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002054 BalancedDelimiterTracker T(*this, tok::l_paren);
2055 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002056 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002057 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002058 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002059 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002060 T.consumeClose();
2061 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002062 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002063 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002064 if (ParseCXXTypeSpecifierSeq(DS))
2065 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002066 else {
2067 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002068 ParseDeclaratorInternal(DeclaratorInfo,
2069 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002070 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002071 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002072 }
2073 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002074 // A new-type-id is a simplified type-id, where essentially the
2075 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002076 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002077 if (ParseCXXTypeSpecifierSeq(DS))
2078 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002079 else {
2080 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002081 ParseDeclaratorInternal(DeclaratorInfo,
2082 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002083 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002084 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002085 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002086 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002087 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002088 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002089
Sebastian Redla55e52c2008-11-25 22:21:31 +00002090 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002091 SourceLocation ConstructorLParen, ConstructorRParen;
2092
2093 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002094 BalancedDelimiterTracker T(*this, tok::l_paren);
2095 T.consumeOpen();
2096 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002097 if (Tok.isNot(tok::r_paren)) {
2098 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002099 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2100 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002101 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002102 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002103 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002104 T.consumeClose();
2105 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002106 if (ConstructorRParen.isInvalid()) {
2107 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002108 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002109 }
Richard Smith29e3a312011-10-15 03:38:41 +00002110 } else if (Tok.is(tok::l_brace) && getLang().CPlusPlus0x) {
Richard Smith7fe62082011-10-15 05:09:34 +00002111 Diag(Tok.getLocation(),
2112 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002113 // FIXME: Have to communicate the init-list to ActOnCXXNew.
2114 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002115 }
2116
Sebastian Redlf53597f2009-03-15 17:47:39 +00002117 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2118 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00002119 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002120 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002121}
2122
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002123/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2124/// passed to ParseDeclaratorInternal.
2125///
2126/// direct-new-declarator:
2127/// '[' expression ']'
2128/// direct-new-declarator '[' constant-expression ']'
2129///
Chris Lattner59232d32009-01-04 21:25:24 +00002130void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002131 // Parse the array dimensions.
2132 bool first = true;
2133 while (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002134 BalancedDelimiterTracker T(*this, tok::l_square);
2135 T.consumeOpen();
2136
John McCall60d7b3a2010-08-24 06:29:42 +00002137 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002138 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002139 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002140 // Recover
2141 SkipUntil(tok::r_square);
2142 return;
2143 }
2144 first = false;
2145
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002146 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002147
2148 ParsedAttributes attrs(AttrFactory);
2149 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002150 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002151 Size.release(),
2152 T.getOpenLocation(),
2153 T.getCloseLocation()),
2154 attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002155
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002156 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002157 return;
2158 }
2159}
2160
2161/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2162/// This ambiguity appears in the syntax of the C++ new operator.
2163///
2164/// new-expression:
2165/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2166/// new-initializer[opt]
2167///
2168/// new-placement:
2169/// '(' expression-list ')'
2170///
John McCallca0408f2010-08-23 06:44:23 +00002171bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002172 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002173 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002174 // The '(' was already consumed.
2175 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002176 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002177 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002178 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002179 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002180 }
2181
2182 // It's not a type, it has to be an expression list.
2183 // Discard the comma locations - ActOnCXXNew has enough parameters.
2184 CommaLocsTy CommaLocs;
2185 return ParseExpressionList(PlacementArgs, CommaLocs);
2186}
2187
2188/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2189/// to free memory allocated by new.
2190///
Chris Lattner59232d32009-01-04 21:25:24 +00002191/// This method is called to parse the 'delete' expression after the optional
2192/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2193/// and "Start" is its location. Otherwise, "Start" is the location of the
2194/// 'delete' token.
2195///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002196/// delete-expression:
2197/// '::'[opt] 'delete' cast-expression
2198/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002199ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002200Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2201 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2202 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002203
2204 // Array delete?
2205 bool ArrayDelete = false;
2206 if (Tok.is(tok::l_square)) {
2207 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002208 BalancedDelimiterTracker T(*this, tok::l_square);
2209
2210 T.consumeOpen();
2211 T.consumeClose();
2212 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002213 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002214 }
2215
John McCall60d7b3a2010-08-24 06:29:42 +00002216 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002217 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002218 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002219
John McCall9ae2f072010-08-23 23:25:46 +00002220 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002221}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002222
Mike Stump1eb44332009-09-09 15:08:12 +00002223static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002224 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002225 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002226 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002227 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002228 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002229 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002230 case tok::kw___has_trivial_constructor:
2231 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002232 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002233 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2234 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2235 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002236 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2237 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002238 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002239 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2240 case tok::kw___is_compound: return UTT_IsCompound;
2241 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002242 case tok::kw___is_empty: return UTT_IsEmpty;
2243 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley20c0da72011-04-27 23:09:49 +00002244 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2245 case tok::kw___is_function: return UTT_IsFunction;
2246 case tok::kw___is_fundamental: return UTT_IsFundamental;
2247 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00002248 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2249 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2250 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2251 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2252 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002253 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002254 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002255 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002256 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002257 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002258 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002259 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2260 case tok::kw___is_scalar: return UTT_IsScalar;
2261 case tok::kw___is_signed: return UTT_IsSigned;
2262 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2263 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002264 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002265 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002266 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2267 case tok::kw___is_void: return UTT_IsVoid;
2268 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002269 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002270}
2271
2272static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2273 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002274 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002275 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002276 case tok::kw___is_convertible: return BTT_IsConvertible;
2277 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002278 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002279 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002280 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002281}
2282
John Wiegley21ff2e52011-04-28 00:16:57 +00002283static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2284 switch(kind) {
2285 default: llvm_unreachable("Not a known binary type trait");
2286 case tok::kw___array_rank: return ATT_ArrayRank;
2287 case tok::kw___array_extent: return ATT_ArrayExtent;
2288 }
2289}
2290
John Wiegley55262202011-04-25 06:54:41 +00002291static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2292 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002293 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002294 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2295 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2296 }
2297}
2298
Sebastian Redl64b45f72009-01-05 20:52:13 +00002299/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2300/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2301/// templates.
2302///
2303/// primary-expression:
2304/// [GNU] unary-type-trait '(' type-id ')'
2305///
John McCall60d7b3a2010-08-24 06:29:42 +00002306ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002307 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2308 SourceLocation Loc = ConsumeToken();
2309
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002310 BalancedDelimiterTracker T(*this, tok::l_paren);
2311 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002312 return ExprError();
2313
2314 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2315 // there will be cryptic errors about mismatched parentheses and missing
2316 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002317 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002318
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002319 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002320
Douglas Gregor809070a2009-02-18 17:45:20 +00002321 if (Ty.isInvalid())
2322 return ExprError();
2323
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002324 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002325}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002326
Francois Pichet6ad6f282010-12-07 00:08:36 +00002327/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2328/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2329/// templates.
2330///
2331/// primary-expression:
2332/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2333///
2334ExprResult Parser::ParseBinaryTypeTrait() {
2335 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2336 SourceLocation Loc = ConsumeToken();
2337
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002338 BalancedDelimiterTracker T(*this, tok::l_paren);
2339 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002340 return ExprError();
2341
2342 TypeResult LhsTy = ParseTypeName();
2343 if (LhsTy.isInvalid()) {
2344 SkipUntil(tok::r_paren);
2345 return ExprError();
2346 }
2347
2348 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2349 SkipUntil(tok::r_paren);
2350 return ExprError();
2351 }
2352
2353 TypeResult RhsTy = ParseTypeName();
2354 if (RhsTy.isInvalid()) {
2355 SkipUntil(tok::r_paren);
2356 return ExprError();
2357 }
2358
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002359 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002360
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002361 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2362 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002363}
2364
John Wiegley21ff2e52011-04-28 00:16:57 +00002365/// ParseArrayTypeTrait - Parse the built-in array type-trait
2366/// pseudo-functions.
2367///
2368/// primary-expression:
2369/// [Embarcadero] '__array_rank' '(' type-id ')'
2370/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2371///
2372ExprResult Parser::ParseArrayTypeTrait() {
2373 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2374 SourceLocation Loc = ConsumeToken();
2375
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002376 BalancedDelimiterTracker T(*this, tok::l_paren);
2377 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002378 return ExprError();
2379
2380 TypeResult Ty = ParseTypeName();
2381 if (Ty.isInvalid()) {
2382 SkipUntil(tok::comma);
2383 SkipUntil(tok::r_paren);
2384 return ExprError();
2385 }
2386
2387 switch (ATT) {
2388 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002389 T.consumeClose();
2390 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2391 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002392 }
2393 case ATT_ArrayExtent: {
2394 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2395 SkipUntil(tok::r_paren);
2396 return ExprError();
2397 }
2398
2399 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002400 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002401
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002402 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2403 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002404 }
2405 default:
2406 break;
2407 }
2408 return ExprError();
2409}
2410
John Wiegley55262202011-04-25 06:54:41 +00002411/// ParseExpressionTrait - Parse built-in expression-trait
2412/// pseudo-functions like __is_lvalue_expr( xxx ).
2413///
2414/// primary-expression:
2415/// [Embarcadero] expression-trait '(' expression ')'
2416///
2417ExprResult Parser::ParseExpressionTrait() {
2418 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2419 SourceLocation Loc = ConsumeToken();
2420
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002421 BalancedDelimiterTracker T(*this, tok::l_paren);
2422 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002423 return ExprError();
2424
2425 ExprResult Expr = ParseExpression();
2426
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002427 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002428
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002429 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2430 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002431}
2432
2433
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002434/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2435/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2436/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002437ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002438Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002439 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002440 BalancedDelimiterTracker &Tracker) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002441 assert(getLang().CPlusPlus && "Should only be called for C++!");
2442 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2443 assert(isTypeIdInParens() && "Not a type-id!");
2444
John McCall60d7b3a2010-08-24 06:29:42 +00002445 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002446 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002447
2448 // We need to disambiguate a very ugly part of the C++ syntax:
2449 //
2450 // (T())x; - type-id
2451 // (T())*x; - type-id
2452 // (T())/x; - expression
2453 // (T()); - expression
2454 //
2455 // The bad news is that we cannot use the specialized tentative parser, since
2456 // it can only verify that the thing inside the parens can be parsed as
2457 // type-id, it is not useful for determining the context past the parens.
2458 //
2459 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002460 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002461 //
2462 // It uses a scheme similar to parsing inline methods. The parenthesized
2463 // tokens are cached, the context that follows is determined (possibly by
2464 // parsing a cast-expression), and then we re-introduce the cached tokens
2465 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002466
Mike Stump1eb44332009-09-09 15:08:12 +00002467 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002468 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002469
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002470 // Store the tokens of the parentheses. We will parse them after we determine
2471 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002472 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002473 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002474 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002475 return ExprError();
2476 }
2477
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002478 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002479 ParseAs = CompoundLiteral;
2480 } else {
2481 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002482 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2483 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2484 NotCastExpr = true;
2485 } else {
2486 // Try parsing the cast-expression that may follow.
2487 // If it is not a cast-expression, NotCastExpr will be true and no token
2488 // will be consumed.
2489 Result = ParseCastExpression(false/*isUnaryExpression*/,
2490 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002491 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002492 // type-id has priority.
2493 true/*isTypeCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002494 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002495
2496 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2497 // an expression.
2498 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002499 }
2500
Mike Stump1eb44332009-09-09 15:08:12 +00002501 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002502 Toks.push_back(Tok);
2503 // Re-enter the stored parenthesized tokens into the token stream, so we may
2504 // parse them now.
2505 PP.EnterTokenStream(Toks.data(), Toks.size(),
2506 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2507 // Drop the current token and bring the first cached one. It's the same token
2508 // as when we entered this function.
2509 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002510
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002511 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002512 // Parse the type declarator.
2513 DeclSpec DS(AttrFactory);
2514 ParseSpecifierQualifierList(DS);
2515 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2516 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002517
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002518 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002519 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002520
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002521 if (ParseAs == CompoundLiteral) {
2522 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002523 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002524 return ParseCompoundLiteralExpression(Ty.get(),
2525 Tracker.getOpenLocation(),
2526 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002527 }
Mike Stump1eb44332009-09-09 15:08:12 +00002528
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002529 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2530 assert(ParseAs == CastExpr);
2531
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002532 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002533 return ExprError();
2534
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002535 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002536 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002537 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2538 DeclaratorInfo, CastTy,
2539 Tracker.getCloseLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002540 return move(Result);
2541 }
Mike Stump1eb44332009-09-09 15:08:12 +00002542
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002543 // Not a compound literal, and not followed by a cast-expression.
2544 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002545
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002546 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002547 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002548 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002549 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2550 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002551
2552 // Match the ')'.
2553 if (Result.isInvalid()) {
2554 SkipUntil(tok::r_paren);
2555 return ExprError();
2556 }
Mike Stump1eb44332009-09-09 15:08:12 +00002557
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002558 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002559 return move(Result);
2560}