blob: 8046f6b88c7a69d58069562b112d2ec44657e162 [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"
Eli Friedmandc3b7232012-01-04 02:40:39 +000017#include "clang/Basic/PrettyStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
Douglas Gregorae7902c2011-08-04 15:30:47 +000019#include "clang/Sema/Scope.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000021#include "llvm/Support/ErrorHandling.h"
22
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Richard Smithea698b32011-04-14 21:45:45 +000025static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
26 switch (Kind) {
27 case tok::kw_template: return 0;
28 case tok::kw_const_cast: return 1;
29 case tok::kw_dynamic_cast: return 2;
30 case tok::kw_reinterpret_cast: return 3;
31 case tok::kw_static_cast: return 4;
32 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000033 llvm_unreachable("Unknown type for digraph error message.");
Richard Smithea698b32011-04-14 21:45:45 +000034 }
35}
36
37// Are the two tokens adjacent in the same source file?
38static bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
39 SourceManager &SM = PP.getSourceManager();
40 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000041 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smithea698b32011-04-14 21:45:45 +000042 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
43}
44
45// Suggest fixit for "<::" after a cast.
46static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
47 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
48 // Pull '<:' and ':' off token stream.
49 if (!AtDigraph)
50 PP.Lex(DigraphToken);
51 PP.Lex(ColonToken);
52
53 SourceRange Range;
54 Range.setBegin(DigraphToken.getLocation());
55 Range.setEnd(ColonToken.getLocation());
56 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
57 << SelectDigraphErrorMessage(Kind)
58 << FixItHint::CreateReplacement(Range, "< ::");
59
60 // Update token information to reflect their change in token type.
61 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000062 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smithea698b32011-04-14 21:45:45 +000063 ColonToken.setLength(2);
64 DigraphToken.setKind(tok::less);
65 DigraphToken.setLength(1);
66
67 // Push new tokens back to token stream.
68 PP.EnterToken(ColonToken);
69 if (!AtDigraph)
70 PP.EnterToken(DigraphToken);
71}
72
Richard Trieu950be712011-09-19 19:01:00 +000073// Check for '<::' which should be '< ::' instead of '[:' when following
74// a template name.
75void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
76 bool EnteringContext,
77 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieuc11030e2011-09-20 20:03:50 +000078 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu950be712011-09-19 19:01:00 +000079 return;
80
81 Token SecondToken = GetLookAheadToken(2);
82 if (!SecondToken.is(tok::colon) || !AreTokensAdjacent(PP, Next, SecondToken))
83 return;
84
85 TemplateTy Template;
86 UnqualifiedId TemplateName;
87 TemplateName.setIdentifier(&II, Tok.getLocation());
88 bool MemberOfUnknownSpecialization;
89 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
90 TemplateName, ObjectType, EnteringContext,
91 Template, MemberOfUnknownSpecialization))
92 return;
93
94 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
95 /*AtDigraph*/false);
96}
97
Mike Stump1eb44332009-09-09 15:08:12 +000098/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000099///
100/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000101/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000102/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000103///
104/// '::'[opt] nested-name-specifier
105/// '::'
106///
107/// nested-name-specifier:
108/// type-name '::'
109/// namespace-name '::'
110/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000111/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000112///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000113///
Mike Stump1eb44332009-09-09 15:08:12 +0000114/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000115/// nested-name-specifier (or empty)
116///
Mike Stump1eb44332009-09-09 15:08:12 +0000117/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000118/// the "." or "->" of a member access expression, this parameter provides the
119/// type of the object whose members are being accessed.
120///
121/// \param EnteringContext whether we will be entering into the context of
122/// the nested-name-specifier after parsing it.
123///
Douglas Gregord4dca082010-02-24 18:44:31 +0000124/// \param MayBePseudoDestructor When non-NULL, points to a flag that
125/// indicates whether this nested-name-specifier may be part of a
126/// pseudo-destructor name. In this case, the flag will be set false
127/// if we don't actually end up parsing a destructor name. Moreorover,
128/// if we do end up determining that we are parsing a destructor name,
129/// the last component of the nested-name-specifier is not parsed as
130/// part of the scope specifier.
131
Douglas Gregorb10cd042010-02-21 18:36:56 +0000132/// member access expression, e.g., the \p T:: in \p p->T::m.
133///
John McCall9ba61662010-02-26 08:45:28 +0000134/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000135bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000136 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000137 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000138 bool *MayBePseudoDestructor,
139 bool IsTypename) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000140 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000141 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000143 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +0000144 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
145 Tok.getAnnotationRange(),
146 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000147 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000148 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000149 }
Chris Lattnere607e802009-01-04 21:14:15 +0000150
Douglas Gregor39a8de12009-02-25 19:37:18 +0000151 bool HasScopeSpecifier = false;
152
Chris Lattner5b454732009-01-05 03:55:46 +0000153 if (Tok.is(tok::coloncolon)) {
154 // ::new and ::delete aren't nested-name-specifiers.
155 tok::TokenKind NextKind = NextToken().getKind();
156 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
157 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattner55a7cef2009-01-05 00:13:00 +0000159 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000160 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
161 return true;
162
Douglas Gregor39a8de12009-02-25 19:37:18 +0000163 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000164 }
165
Douglas Gregord4dca082010-02-24 18:44:31 +0000166 bool CheckForDestructor = false;
167 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
168 CheckForDestructor = true;
169 *MayBePseudoDestructor = false;
170 }
171
David Blaikie42d6d0c2011-12-04 05:04:18 +0000172 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
173 DeclSpec DS(AttrFactory);
174 SourceLocation DeclLoc = Tok.getLocation();
175 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
176 if (Tok.isNot(tok::coloncolon)) {
177 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
178 return false;
179 }
180
181 SourceLocation CCLoc = ConsumeToken();
182 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
183 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
184
185 HasScopeSpecifier = true;
186 }
187
Douglas Gregor39a8de12009-02-25 19:37:18 +0000188 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000189 if (HasScopeSpecifier) {
190 // C++ [basic.lookup.classref]p5:
191 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000192 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000193 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000194 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000195 // the class-name-or-namespace-name is looked up in global scope as a
196 // class-name or namespace-name.
197 //
198 // To implement this, we clear out the object type as soon as we've
199 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000200 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000201
202 if (Tok.is(tok::code_completion)) {
203 // Code completion for a nested-name-specifier, where the code
204 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000205 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000206 // Include code completion token into the range of the scope otherwise
207 // when we try to annotate the scope tokens the dangling code completion
208 // token will cause assertion in
209 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000210 SS.setEndLoc(Tok.getLocation());
211 cutOffParsing();
212 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000213 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Douglas Gregor39a8de12009-02-25 19:37:18 +0000216 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000217 // nested-name-specifier 'template'[opt] simple-template-id '::'
218
219 // Parse the optional 'template' keyword, then make sure we have
220 // 'identifier <' after it.
221 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000222 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000223 // nested-name-specifier, since they aren't allowed to start with
224 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000225 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000226 break;
227
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000228 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000229 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000230
231 UnqualifiedId TemplateName;
232 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000233 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000234 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000235 ConsumeToken();
236 } else if (Tok.is(tok::kw_operator)) {
237 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000238 TemplateName)) {
239 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000240 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000241 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000242
Sean Hunte6252d12009-11-28 08:58:14 +0000243 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
244 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000245 Diag(TemplateName.getSourceRange().getBegin(),
246 diag::err_id_after_template_in_nested_name_spec)
247 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000248 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000249 break;
250 }
251 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000252 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000253 break;
254 }
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000256 // If the next token is not '<', we have a qualified-id that refers
257 // to a template name, such as T::template apply, but is not a
258 // template-id.
259 if (Tok.isNot(tok::less)) {
260 TPA.Revert();
261 break;
262 }
263
264 // Commit to parsing the template-id.
265 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000266 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000267 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000268 TemplateKWLoc,
269 SS,
270 TemplateName,
271 ObjectType,
272 EnteringContext,
273 Template)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000274 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000275 TemplateKWLoc, false))
276 return true;
277 } else
John McCall9ba61662010-02-26 08:45:28 +0000278 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattner77cf72a2009-06-26 03:47:46 +0000280 continue;
281 }
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Douglas Gregor39a8de12009-02-25 19:37:18 +0000283 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000284 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000285 //
286 // simple-template-id '::'
287 //
288 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000289 // right kind (it should name a type or be dependent), and then
290 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000291 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000292 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
293 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000294 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000295 }
296
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000297 // Consume the template-id token.
298 ConsumeToken();
299
300 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
301 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000302
David Blaikie6796fc12011-11-07 03:30:03 +0000303 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000304
305 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
306 TemplateId->getTemplateArgs(),
307 TemplateId->NumArgs);
308
309 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
310 /*FIXME:*/SourceLocation(),
311 SS,
312 TemplateId->Template,
313 TemplateId->TemplateNameLoc,
314 TemplateId->LAngleLoc,
315 TemplateArgsPtr,
316 TemplateId->RAngleLoc,
317 CCLoc,
318 EnteringContext)) {
319 SourceLocation StartLoc
320 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
321 : TemplateId->TemplateNameLoc;
322 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000323 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000324
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000325 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000326 }
327
Chris Lattner5c7f7862009-06-26 03:52:38 +0000328
329 // The rest of the nested-name-specifier possibilities start with
330 // tok::identifier.
331 if (Tok.isNot(tok::identifier))
332 break;
333
334 IdentifierInfo &II = *Tok.getIdentifierInfo();
335
336 // nested-name-specifier:
337 // type-name '::'
338 // namespace-name '::'
339 // nested-name-specifier identifier '::'
340 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000341
342 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
343 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000344 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000345 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
346 Tok.getLocation(),
347 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000348 EnteringContext) &&
349 // If the token after the colon isn't an identifier, it's still an
350 // error, but they probably meant something else strange so don't
351 // recover like this.
352 PP.LookAhead(1).is(tok::identifier)) {
353 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000354 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000355
356 // Recover as if the user wrote '::'.
357 Next.setKind(tok::coloncolon);
358 }
Chris Lattner46646492009-12-07 01:36:53 +0000359 }
360
Chris Lattner5c7f7862009-06-26 03:52:38 +0000361 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000362 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000363 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000364 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000365 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000366 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000367 }
368
Chris Lattner5c7f7862009-06-26 03:52:38 +0000369 // We have an identifier followed by a '::'. Lookup this name
370 // as the name in a nested-name-specifier.
371 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000372 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
373 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000374 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000376 HasScopeSpecifier = true;
377 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
378 ObjectType, EnteringContext, SS))
379 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
380
Chris Lattner5c7f7862009-06-26 03:52:38 +0000381 continue;
382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Richard Trieu950be712011-09-19 19:01:00 +0000384 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000385
Chris Lattner5c7f7862009-06-26 03:52:38 +0000386 // nested-name-specifier:
387 // type-name '<'
388 if (Next.is(tok::less)) {
389 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000390 UnqualifiedId TemplateName;
391 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000392 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000393 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000394 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000395 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000396 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000397 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000398 Template,
399 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000400 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000401 // with a template-id annotation. We do not permit the
402 // template-id to be translated into a type annotation,
403 // because some clients (e.g., the parsing of class template
404 // specializations) still want to see the original template-id
405 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000406 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000407 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000408 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000409 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000410 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000411 }
412
413 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000414 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000415 // We have something like t::getAs<T>, where getAs is a
416 // member of an unknown specialization. However, this will only
417 // parse correctly as a template, so suggest the keyword 'template'
418 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000419 unsigned DiagID = diag::err_missing_dependent_template_keyword;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000420 if (getLang().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000421 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000422
423 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000424 << II.getName()
425 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
426
Douglas Gregord6ab2322010-06-16 23:00:59 +0000427 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000428 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000429 Tok.getLocation(), SS,
430 TemplateName, ObjectType,
431 EnteringContext, Template)) {
432 // Consume the identifier.
433 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000434 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000435 SourceLocation(), false))
436 return true;
437 }
438 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000439 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000440
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000441 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000442 }
443 }
444
Douglas Gregor39a8de12009-02-25 19:37:18 +0000445 // We don't have any tokens that form the beginning of a
446 // nested-name-specifier, so we're done.
447 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Douglas Gregord4dca082010-02-24 18:44:31 +0000450 // Even if we didn't see any pieces of a nested-name-specifier, we
451 // still check whether there is a tilde in this position, which
452 // indicates a potential pseudo-destructor.
453 if (CheckForDestructor && Tok.is(tok::tilde))
454 *MayBePseudoDestructor = true;
455
John McCall9ba61662010-02-26 08:45:28 +0000456 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000457}
458
459/// ParseCXXIdExpression - Handle id-expression.
460///
461/// id-expression:
462/// unqualified-id
463/// qualified-id
464///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000465/// qualified-id:
466/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
467/// '::' identifier
468/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000469/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000470///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000471/// NOTE: The standard specifies that, for qualified-id, the parser does not
472/// expect:
473///
474/// '::' conversion-function-id
475/// '::' '~' class-name
476///
477/// This may cause a slight inconsistency on diagnostics:
478///
479/// class C {};
480/// namespace A {}
481/// void f() {
482/// :: A :: ~ C(); // Some Sema error about using destructor with a
483/// // namespace.
484/// :: ~ C(); // Some Parser error like 'unexpected ~'.
485/// }
486///
487/// We simplify the parser a bit and make it work like:
488///
489/// qualified-id:
490/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
491/// '::' unqualified-id
492///
493/// That way Sema can handle and report similar errors for namespaces and the
494/// global scope.
495///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000496/// The isAddressOfOperand parameter indicates that this id-expression is a
497/// direct operand of the address-of operator. This is, besides member contexts,
498/// the only place where a qualified-id naming a non-static class member may
499/// appear.
500///
John McCall60d7b3a2010-08-24 06:29:42 +0000501ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000502 // qualified-id:
503 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
504 // '::' unqualified-id
505 //
506 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000507 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000508
509 UnqualifiedId Name;
510 if (ParseUnqualifiedId(SS,
511 /*EnteringContext=*/false,
512 /*AllowDestructorName=*/false,
513 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000514 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000515 Name))
516 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000517
518 // This is only the direct operand of an & operator if it is not
519 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000520 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
521 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000522
Douglas Gregor23c94db2010-07-02 17:43:08 +0000523 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000524 isAddressOfOperand);
525
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000526}
527
Douglas Gregorae7902c2011-08-04 15:30:47 +0000528/// ParseLambdaExpression - Parse a C++0x lambda expression.
529///
530/// lambda-expression:
531/// lambda-introducer lambda-declarator[opt] compound-statement
532///
533/// lambda-introducer:
534/// '[' lambda-capture[opt] ']'
535///
536/// lambda-capture:
537/// capture-default
538/// capture-list
539/// capture-default ',' capture-list
540///
541/// capture-default:
542/// '&'
543/// '='
544///
545/// capture-list:
546/// capture
547/// capture-list ',' capture
548///
549/// capture:
550/// identifier
551/// '&' identifier
552/// 'this'
553///
554/// lambda-declarator:
555/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
556/// 'mutable'[opt] exception-specification[opt]
557/// trailing-return-type[opt]
558///
559ExprResult Parser::ParseLambdaExpression() {
560 // Parse lambda-introducer.
561 LambdaIntroducer Intro;
562
563 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
564 if (DiagID) {
565 Diag(Tok, DiagID.getValue());
566 SkipUntil(tok::r_square);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000567 SkipUntil(tok::l_brace);
568 SkipUntil(tok::r_brace);
569 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000570 }
571
572 return ParseLambdaExpressionAfterIntroducer(Intro);
573}
574
575/// TryParseLambdaExpression - Use lookahead and potentially tentative
576/// parsing to determine if we are looking at a C++0x lambda expression, and parse
577/// it if we are.
578///
579/// If we are not looking at a lambda expression, returns ExprError().
580ExprResult Parser::TryParseLambdaExpression() {
581 assert(getLang().CPlusPlus0x
582 && Tok.is(tok::l_square)
583 && "Not at the start of a possible lambda expression.");
584
585 const Token Next = NextToken(), After = GetLookAheadToken(2);
586
587 // If lookahead indicates this is a lambda...
588 if (Next.is(tok::r_square) || // []
589 Next.is(tok::equal) || // [=
590 (Next.is(tok::amp) && // [&] or [&,
591 (After.is(tok::r_square) ||
592 After.is(tok::comma))) ||
593 (Next.is(tok::identifier) && // [identifier]
594 After.is(tok::r_square))) {
595 return ParseLambdaExpression();
596 }
597
Eli Friedmandc3b7232012-01-04 02:40:39 +0000598 // If lookahead indicates an ObjC message send...
599 // [identifier identifier
Douglas Gregorae7902c2011-08-04 15:30:47 +0000600 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000601 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000602 }
603
Eli Friedmandc3b7232012-01-04 02:40:39 +0000604 // Here, we're stuck: lambda introducers and Objective-C message sends are
605 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
606 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
607 // writing two routines to parse a lambda introducer, just try to parse
608 // a lambda introducer first, and fall back if that fails.
609 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregorae7902c2011-08-04 15:30:47 +0000610 LambdaIntroducer Intro;
611 if (TryParseLambdaIntroducer(Intro))
Eli Friedmandc3b7232012-01-04 02:40:39 +0000612 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000613 return ParseLambdaExpressionAfterIntroducer(Intro);
614}
615
616/// ParseLambdaExpression - Parse a lambda introducer.
617///
618/// Returns a DiagnosticID if it hit something unexpected.
619llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
620 typedef llvm::Optional<unsigned> DiagResult;
621
622 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000623 BalancedDelimiterTracker T(*this, tok::l_square);
624 T.consumeOpen();
625
626 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000627
628 bool first = true;
629
630 // Parse capture-default.
631 if (Tok.is(tok::amp) &&
632 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
633 Intro.Default = LCD_ByRef;
634 ConsumeToken();
635 first = false;
636 } else if (Tok.is(tok::equal)) {
637 Intro.Default = LCD_ByCopy;
638 ConsumeToken();
639 first = false;
640 }
641
642 while (Tok.isNot(tok::r_square)) {
643 if (!first) {
644 if (Tok.isNot(tok::comma))
645 return DiagResult(diag::err_expected_comma_or_rsquare);
646 ConsumeToken();
647 }
648
649 first = false;
650
651 // Parse capture.
652 LambdaCaptureKind Kind = LCK_ByCopy;
653 SourceLocation Loc;
654 IdentifierInfo* Id = 0;
655
656 if (Tok.is(tok::kw_this)) {
657 Kind = LCK_This;
658 Loc = ConsumeToken();
659 } else {
660 if (Tok.is(tok::amp)) {
661 Kind = LCK_ByRef;
662 ConsumeToken();
663 }
664
665 if (Tok.is(tok::identifier)) {
666 Id = Tok.getIdentifierInfo();
667 Loc = ConsumeToken();
668 } else if (Tok.is(tok::kw_this)) {
669 // FIXME: If we want to suggest a fixit here, will need to return more
670 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
671 // Clear()ed to prevent emission in case of tentative parsing?
672 return DiagResult(diag::err_this_captured_by_reference);
673 } else {
674 return DiagResult(diag::err_expected_capture);
675 }
676 }
677
678 Intro.addCapture(Kind, Loc, Id);
679 }
680
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000681 T.consumeClose();
682 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000683
684 return DiagResult();
685}
686
687/// TryParseLambdaExpression - Tentatively parse a lambda introducer.
688///
689/// Returns true if it hit something unexpected.
690bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
691 TentativeParsingAction PA(*this);
692
693 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
694
695 if (DiagID) {
696 PA.Revert();
697 return true;
698 }
699
700 PA.Commit();
701 return false;
702}
703
704/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
705/// expression.
706ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
707 LambdaIntroducer &Intro) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000708 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
709 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
710
711 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
712 "lambda expression parsing");
713
Douglas Gregorae7902c2011-08-04 15:30:47 +0000714 // Parse lambda-declarator[opt].
715 DeclSpec DS(AttrFactory);
Eli Friedmanf88c4002012-01-04 04:41:38 +0000716 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000717
718 if (Tok.is(tok::l_paren)) {
719 ParseScope PrototypeScope(this,
720 Scope::FunctionPrototypeScope |
721 Scope::DeclScope);
722
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000723 SourceLocation DeclLoc, DeclEndLoc;
724 BalancedDelimiterTracker T(*this, tok::l_paren);
725 T.consumeOpen();
726 DeclLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000727
728 // Parse parameter-declaration-clause.
729 ParsedAttributes Attr(AttrFactory);
730 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
731 SourceLocation EllipsisLoc;
732
733 if (Tok.isNot(tok::r_paren))
734 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
735
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000736 T.consumeClose();
737 DeclEndLoc = T.getCloseLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000738
739 // Parse 'mutable'[opt].
740 SourceLocation MutableLoc;
741 if (Tok.is(tok::kw_mutable)) {
742 MutableLoc = ConsumeToken();
743 DeclEndLoc = MutableLoc;
744 }
745
746 // Parse exception-specification[opt].
747 ExceptionSpecificationType ESpecType = EST_None;
748 SourceRange ESpecRange;
749 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
750 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
751 ExprResult NoexceptExpr;
752 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
753 DynamicExceptions,
754 DynamicExceptionRanges,
755 NoexceptExpr);
756
757 if (ESpecType != EST_None)
758 DeclEndLoc = ESpecRange.getEnd();
759
760 // Parse attribute-specifier[opt].
761 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
762
763 // Parse trailing-return-type[opt].
764 ParsedType TrailingReturnType;
765 if (Tok.is(tok::arrow)) {
766 SourceRange Range;
767 TrailingReturnType = ParseTrailingReturnType(Range).get();
768 if (Range.getEnd().isValid())
769 DeclEndLoc = Range.getEnd();
770 }
771
772 PrototypeScope.Exit();
773
774 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
775 /*isVariadic=*/EllipsisLoc.isValid(),
776 EllipsisLoc,
777 ParamInfo.data(), ParamInfo.size(),
778 DS.getTypeQualifiers(),
779 /*RefQualifierIsLValueRef=*/true,
780 /*RefQualifierLoc=*/SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +0000781 /*ConstQualifierLoc=*/SourceLocation(),
782 /*VolatileQualifierLoc=*/SourceLocation(),
Douglas Gregorae7902c2011-08-04 15:30:47 +0000783 MutableLoc,
784 ESpecType, ESpecRange.getBegin(),
785 DynamicExceptions.data(),
786 DynamicExceptionRanges.data(),
787 DynamicExceptions.size(),
788 NoexceptExpr.isUsable() ?
789 NoexceptExpr.get() : 0,
790 DeclLoc, DeclEndLoc, D,
791 TrailingReturnType),
792 Attr, DeclEndLoc);
793 }
794
Eli Friedman906a7e12012-01-06 03:05:34 +0000795 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
796 // it.
797 ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
798 Scope::BreakScope | Scope::ContinueScope |
799 Scope::DeclScope);
800
Eli Friedmanec9ea722012-01-05 03:35:19 +0000801 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
802
Douglas Gregorae7902c2011-08-04 15:30:47 +0000803 // Parse compound-statement.
Eli Friedmandc3b7232012-01-04 02:40:39 +0000804 if (!Tok.is(tok::l_brace)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +0000805 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000806 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
807 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000808 }
809
Eli Friedmandc3b7232012-01-04 02:40:39 +0000810 StmtResult Stmt(ParseCompoundStatementBody());
811 BodyScope.Exit();
812
Eli Friedmandeeab902012-01-04 02:46:53 +0000813 if (!Stmt.isInvalid())
814 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(),
815 getCurScope());
Eli Friedmandc3b7232012-01-04 02:40:39 +0000816
Eli Friedmandeeab902012-01-04 02:46:53 +0000817 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
818 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000819}
820
Reid Spencer5f016e22007-07-11 17:01:13 +0000821/// ParseCXXCasts - This handles the various ways to cast expressions to another
822/// type.
823///
824/// postfix-expression: [C++ 5.2p1]
825/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
826/// 'static_cast' '<' type-name '>' '(' expression ')'
827/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
828/// 'const_cast' '<' type-name '>' '(' expression ')'
829///
John McCall60d7b3a2010-08-24 06:29:42 +0000830ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 tok::TokenKind Kind = Tok.getKind();
832 const char *CastName = 0; // For error messages
833
834 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000835 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 case tok::kw_const_cast: CastName = "const_cast"; break;
837 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
838 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
839 case tok::kw_static_cast: CastName = "static_cast"; break;
840 }
841
842 SourceLocation OpLoc = ConsumeToken();
843 SourceLocation LAngleBracketLoc = Tok.getLocation();
844
Richard Smithea698b32011-04-14 21:45:45 +0000845 // Check for "<::" which is parsed as "[:". If found, fix token stream,
846 // diagnose error, suggest fix, and recover parsing.
847 Token Next = NextToken();
848 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
849 AreTokensAdjacent(PP, Tok, Next))
850 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
851
Reid Spencer5f016e22007-07-11 17:01:13 +0000852 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000853 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000854
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000855 // Parse the common declaration-specifiers piece.
856 DeclSpec DS(AttrFactory);
857 ParseSpecifierQualifierList(DS);
858
859 // Parse the abstract-declarator, if present.
860 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
861 ParseDeclarator(DeclaratorInfo);
862
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 SourceLocation RAngleBracketLoc = Tok.getLocation();
864
Chris Lattner1ab3b962008-11-18 07:48:38 +0000865 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000866 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000867
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000868 SourceLocation LParenLoc, RParenLoc;
869 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +0000870
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000871 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000872 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000873
John McCall60d7b3a2010-08-24 06:29:42 +0000874 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000876 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000877 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +0000878
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000879 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +0000880 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000881 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +0000882 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000883 T.getOpenLocation(), Result.take(),
884 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000885
Sebastian Redl20df9b72008-12-11 22:51:44 +0000886 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000887}
888
Sebastian Redlc42e1182008-11-11 11:37:55 +0000889/// ParseCXXTypeid - This handles the C++ typeid expression.
890///
891/// postfix-expression: [C++ 5.2p1]
892/// 'typeid' '(' expression ')'
893/// 'typeid' '(' type-id ')'
894///
John McCall60d7b3a2010-08-24 06:29:42 +0000895ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000896 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
897
898 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000899 SourceLocation LParenLoc, RParenLoc;
900 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000901
902 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000903 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000904 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000905 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000906
John McCall60d7b3a2010-08-24 06:29:42 +0000907 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000908
909 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000910 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000911
912 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000913 T.consumeClose();
914 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000915 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000916 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000917
918 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000919 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000920 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000921 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000922 // When typeid is applied to an expression other than an lvalue of a
923 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000924 // operand (Clause 5).
925 //
Mike Stump1eb44332009-09-09 15:08:12 +0000926 // Note that we can't tell whether the expression is an lvalue of a
Eli Friedmanef331b72012-01-20 01:26:23 +0000927 // polymorphic class type until after we've parsed the expression; we
928 // speculatively assume the subexpression is unevaluated, and fix it up
929 // later.
930 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000931 Result = ParseExpression();
932
933 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000934 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000935 SkipUntil(tok::r_paren);
936 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000937 T.consumeClose();
938 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000939 if (RParenLoc.isInvalid())
940 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000941
Sebastian Redlc42e1182008-11-11 11:37:55 +0000942 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000943 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000944 }
945 }
946
Sebastian Redl20df9b72008-12-11 22:51:44 +0000947 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000948}
949
Francois Pichet01b7c302010-09-08 12:20:18 +0000950/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
951///
952/// '__uuidof' '(' expression ')'
953/// '__uuidof' '(' type-id ')'
954///
955ExprResult Parser::ParseCXXUuidof() {
956 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
957
958 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000959 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +0000960
961 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000962 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +0000963 return ExprError();
964
965 ExprResult Result;
966
967 if (isTypeIdInParens()) {
968 TypeResult Ty = ParseTypeName();
969
970 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000971 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +0000972
973 if (Ty.isInvalid())
974 return ExprError();
975
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000976 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
977 Ty.get().getAsOpaquePtr(),
978 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +0000979 } else {
980 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
981 Result = ParseExpression();
982
983 // Match the ')'.
984 if (Result.isInvalid())
985 SkipUntil(tok::r_paren);
986 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000987 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +0000988
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000989 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
990 /*isType=*/false,
991 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +0000992 }
993 }
994
995 return move(Result);
996}
997
Douglas Gregord4dca082010-02-24 18:44:31 +0000998/// \brief Parse a C++ pseudo-destructor expression after the base,
999/// . or -> operator, and nested-name-specifier have already been
1000/// parsed.
1001///
1002/// postfix-expression: [C++ 5.2]
1003/// postfix-expression . pseudo-destructor-name
1004/// postfix-expression -> pseudo-destructor-name
1005///
1006/// pseudo-destructor-name:
1007/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1008/// ::[opt] nested-name-specifier template simple-template-id ::
1009/// ~type-name
1010/// ::[opt] nested-name-specifier[opt] ~type-name
1011///
John McCall60d7b3a2010-08-24 06:29:42 +00001012ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +00001013Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1014 tok::TokenKind OpKind,
1015 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +00001016 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +00001017 // We're parsing either a pseudo-destructor-name or a dependent
1018 // member access that has the same form as a
1019 // pseudo-destructor-name. We parse both in the same way and let
1020 // the action model sort them out.
1021 //
1022 // Note that the ::[opt] nested-name-specifier[opt] has already
1023 // been parsed, and if there was a simple-template-id, it has
1024 // been coalesced into a template-id annotation token.
1025 UnqualifiedId FirstTypeName;
1026 SourceLocation CCLoc;
1027 if (Tok.is(tok::identifier)) {
1028 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1029 ConsumeToken();
1030 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1031 CCLoc = ConsumeToken();
1032 } else if (Tok.is(tok::annot_template_id)) {
1033 FirstTypeName.setTemplateId(
1034 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1035 ConsumeToken();
1036 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1037 CCLoc = ConsumeToken();
1038 } else {
1039 FirstTypeName.setIdentifier(0, SourceLocation());
1040 }
1041
1042 // Parse the tilde.
1043 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1044 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001045
1046 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1047 DeclSpec DS(AttrFactory);
Benjamin Kramer85c60db2011-12-18 12:18:02 +00001048 ParseDecltypeSpecifier(DS);
David Blaikie91ec7892011-12-16 16:03:09 +00001049 if (DS.getTypeSpecType() == TST_error)
1050 return ExprError();
1051 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1052 OpKind, TildeLoc, DS,
1053 Tok.is(tok::l_paren));
1054 }
1055
Douglas Gregord4dca082010-02-24 18:44:31 +00001056 if (!Tok.is(tok::identifier)) {
1057 Diag(Tok, diag::err_destructor_tilde_identifier);
1058 return ExprError();
1059 }
1060
1061 // Parse the second type.
1062 UnqualifiedId SecondTypeName;
1063 IdentifierInfo *Name = Tok.getIdentifierInfo();
1064 SourceLocation NameLoc = ConsumeToken();
1065 SecondTypeName.setIdentifier(Name, NameLoc);
1066
1067 // If there is a '<', the second type name is a template-id. Parse
1068 // it as such.
1069 if (Tok.is(tok::less) &&
1070 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001071 SecondTypeName, /*AssumeTemplateName=*/true,
1072 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +00001073 return ExprError();
1074
John McCall9ae2f072010-08-23 23:25:46 +00001075 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1076 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001077 SS, FirstTypeName, CCLoc,
1078 TildeLoc, SecondTypeName,
1079 Tok.is(tok::l_paren));
1080}
1081
Reid Spencer5f016e22007-07-11 17:01:13 +00001082/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1083///
1084/// boolean-literal: [C++ 2.13.5]
1085/// 'true'
1086/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001087ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001089 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001090}
Chris Lattner50dd2892008-02-26 00:51:44 +00001091
1092/// ParseThrowExpression - This handles the C++ throw expression.
1093///
1094/// throw-expression: [C++ 15]
1095/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001096ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001097 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001098 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001099
Chris Lattner2a2819a2008-04-06 06:02:23 +00001100 // If the current token isn't the start of an assignment-expression,
1101 // then the expression is not present. This handles things like:
1102 // "C ? throw : (void)42", which is crazy but legal.
1103 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1104 case tok::semi:
1105 case tok::r_paren:
1106 case tok::r_square:
1107 case tok::r_brace:
1108 case tok::colon:
1109 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001110 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001111
Chris Lattner2a2819a2008-04-06 06:02:23 +00001112 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001113 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +00001114 if (Expr.isInvalid()) return move(Expr);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001115 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001116 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001117}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001118
1119/// ParseCXXThis - This handles the C++ 'this' pointer.
1120///
1121/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1122/// a non-lvalue expression whose value is the address of the object for which
1123/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001124ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001125 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1126 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001127 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001128}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001129
1130/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1131/// Can be interpreted either as function-style casting ("int(x)")
1132/// or class type construction ("ClassType(x,y,z)")
1133/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001134/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001135///
1136/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001137/// simple-type-specifier '(' expression-list[opt] ')'
1138/// [C++0x] simple-type-specifier braced-init-list
1139/// typename-specifier '(' expression-list[opt] ')'
1140/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001141///
John McCall60d7b3a2010-08-24 06:29:42 +00001142ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001143Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001144 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001145 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001146
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001147 assert((Tok.is(tok::l_paren) ||
1148 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1149 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001150
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001151 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001152
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001153 // FIXME: Convert to a proper type construct expression.
1154 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001155
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001156 } else {
1157 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1158
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001159 BalancedDelimiterTracker T(*this, tok::l_paren);
1160 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001161
1162 ExprVector Exprs(Actions);
1163 CommaLocsTy CommaLocs;
1164
1165 if (Tok.isNot(tok::r_paren)) {
1166 if (ParseExpressionList(Exprs, CommaLocs)) {
1167 SkipUntil(tok::r_paren);
1168 return ExprError();
1169 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001170 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001171
1172 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001173 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001174
1175 // TypeRep could be null, if it references an invalid typedef.
1176 if (!TypeRep)
1177 return ExprError();
1178
1179 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1180 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001181 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
1182 move_arg(Exprs),
1183 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001184 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001185}
1186
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001187/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001188///
1189/// condition:
1190/// expression
1191/// type-specifier-seq declarator '=' assignment-expression
1192/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1193/// '=' assignment-expression
1194///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001195/// \param ExprResult if the condition was parsed as an expression, the
1196/// parsed expression.
1197///
1198/// \param DeclResult if the condition was parsed as a declaration, the
1199/// parsed declaration.
1200///
Douglas Gregor586596f2010-05-06 17:25:47 +00001201/// \param Loc The location of the start of the statement that requires this
1202/// condition, e.g., the "for" in a for loop.
1203///
1204/// \param ConvertToBoolean Whether the condition expression should be
1205/// converted to a boolean value.
1206///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001207/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001208bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1209 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001210 SourceLocation Loc,
1211 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001212 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001213 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001214 cutOffParsing();
1215 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001216 }
1217
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001218 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001219 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001220 ExprOut = ParseExpression(); // expression
1221 DeclOut = 0;
1222 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001223 return true;
1224
1225 // If required, convert to a boolean value.
1226 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001227 ExprOut
1228 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1229 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001230 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001231
1232 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001233 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001234 ParseSpecifierQualifierList(DS);
1235
1236 // declarator
1237 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1238 ParseDeclarator(DeclaratorInfo);
1239
1240 // simple-asm-expr[opt]
1241 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001242 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001243 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001244 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001245 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001246 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001247 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001248 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001249 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001250 }
1251
1252 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001253 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001254
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001255 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001256 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001257 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001258 DeclOut = Dcl.get();
1259 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001260
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001261 // '=' assignment-expression
Richard Trieud6c7c672012-01-18 22:54:52 +00001262 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001263 if (isTokenEqualOrEqualTypo()) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001264 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001265 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001266 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +00001267 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1268 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001269 } else {
1270 // FIXME: C++0x allows a braced-init-list
1271 Diag(Tok, diag::err_expected_equal_after_declarator);
1272 }
1273
Douglas Gregor586596f2010-05-06 17:25:47 +00001274 // FIXME: Build a reference to this declaration? Convert it to bool?
1275 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001276
1277 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001278
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001279 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001280}
1281
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001282/// \brief Determine whether the current token starts a C++
1283/// simple-type-specifier.
1284bool Parser::isCXXSimpleTypeSpecifier() const {
1285 switch (Tok.getKind()) {
1286 case tok::annot_typename:
1287 case tok::kw_short:
1288 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001289 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001290 case tok::kw_signed:
1291 case tok::kw_unsigned:
1292 case tok::kw_void:
1293 case tok::kw_char:
1294 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001295 case tok::kw_half:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001296 case tok::kw_float:
1297 case tok::kw_double:
1298 case tok::kw_wchar_t:
1299 case tok::kw_char16_t:
1300 case tok::kw_char32_t:
1301 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +00001302 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001303 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +00001304 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001305 return true;
1306
1307 default:
1308 break;
1309 }
1310
1311 return false;
1312}
1313
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001314/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1315/// This should only be called when the current token is known to be part of
1316/// simple-type-specifier.
1317///
1318/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001319/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001320/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1321/// char
1322/// wchar_t
1323/// bool
1324/// short
1325/// int
1326/// long
1327/// signed
1328/// unsigned
1329/// float
1330/// double
1331/// void
1332/// [GNU] typeof-specifier
1333/// [C++0x] auto [TODO]
1334///
1335/// type-name:
1336/// class-name
1337/// enum-name
1338/// typedef-name
1339///
1340void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1341 DS.SetRangeStart(Tok.getLocation());
1342 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001343 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001344 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001346 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001347 case tok::identifier: // foo::bar
1348 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001349 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001350 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001351 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001352
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001353 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001354 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001355 if (getTypeAnnotation(Tok))
1356 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1357 getTypeAnnotation(Tok));
1358 else
1359 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001360
1361 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1362 ConsumeToken();
1363
1364 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1365 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1366 // Objective-C interface. If we don't have Objective-C or a '<', this is
1367 // just a normal reference to a typedef name.
1368 if (Tok.is(tok::less) && getLang().ObjC1)
1369 ParseObjCProtocolQualifiers(DS);
1370
1371 DS.Finish(Diags, PP);
1372 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001373 }
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001375 // builtin types
1376 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001377 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001378 break;
1379 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001380 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001381 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001382 case tok::kw___int64:
1383 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1384 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001385 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001386 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001387 break;
1388 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001389 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001390 break;
1391 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001392 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001393 break;
1394 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001395 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001396 break;
1397 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001398 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001399 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001400 case tok::kw_half:
1401 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1402 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001403 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001404 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001405 break;
1406 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001407 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001408 break;
1409 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001410 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001411 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001412 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001413 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001414 break;
1415 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001416 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001417 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001418 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001419 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001420 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001422 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001423 // GNU typeof support.
1424 case tok::kw_typeof:
1425 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001426 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001427 return;
1428 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001429 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001430 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1431 else
1432 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001433 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001434 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001435}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001436
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001437/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1438/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1439/// e.g., "const short int". Note that the DeclSpec is *not* finished
1440/// by parsing the type-specifier-seq, because these sequences are
1441/// typically followed by some form of declarator. Returns true and
1442/// emits diagnostics if this is not a type-specifier-seq, false
1443/// otherwise.
1444///
1445/// type-specifier-seq: [C++ 8.1]
1446/// type-specifier type-specifier-seq[opt]
1447///
1448bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1449 DS.SetRangeStart(Tok.getLocation());
1450 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001451 unsigned DiagID;
1452 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001453
1454 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001455 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1456 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001457 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001458 return true;
1459 }
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Sebastian Redld9bafa72010-02-03 21:21:43 +00001461 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1462 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1463 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001464
Douglas Gregor396a9f22010-02-24 23:13:13 +00001465 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001466 return false;
1467}
1468
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001469/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1470/// some form.
1471///
1472/// This routine is invoked when a '<' is encountered after an identifier or
1473/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1474/// whether the unqualified-id is actually a template-id. This routine will
1475/// then parse the template arguments and form the appropriate template-id to
1476/// return to the caller.
1477///
1478/// \param SS the nested-name-specifier that precedes this template-id, if
1479/// we're actually parsing a qualified-id.
1480///
1481/// \param Name for constructor and destructor names, this is the actual
1482/// identifier that may be a template-name.
1483///
1484/// \param NameLoc the location of the class-name in a constructor or
1485/// destructor.
1486///
1487/// \param EnteringContext whether we're entering the scope of the
1488/// nested-name-specifier.
1489///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001490/// \param ObjectType if this unqualified-id occurs within a member access
1491/// expression, the type of the base object whose member is being accessed.
1492///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001493/// \param Id as input, describes the template-name or operator-function-id
1494/// that precedes the '<'. If template arguments were parsed successfully,
1495/// will be updated with the template-id.
1496///
Douglas Gregord4dca082010-02-24 18:44:31 +00001497/// \param AssumeTemplateId When true, this routine will assume that the name
1498/// refers to a template without performing name lookup to verify.
1499///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001500/// \returns true if a parse error occurred, false otherwise.
1501bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1502 IdentifierInfo *Name,
1503 SourceLocation NameLoc,
1504 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001505 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001506 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001507 bool AssumeTemplateId,
1508 SourceLocation TemplateKWLoc) {
1509 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1510 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001511
1512 TemplateTy Template;
1513 TemplateNameKind TNK = TNK_Non_template;
1514 switch (Id.getKind()) {
1515 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001516 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001517 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001518 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001519 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001520 Id, ObjectType, EnteringContext,
1521 Template);
1522 if (TNK == TNK_Non_template)
1523 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001524 } else {
1525 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001526 TNK = Actions.isTemplateName(getCurScope(), SS,
1527 TemplateKWLoc.isValid(), Id,
1528 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001529 MemberOfUnknownSpecialization);
1530
1531 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1532 ObjectType && IsTemplateArgumentList()) {
1533 // We have something like t->getAs<T>(), where getAs is a
1534 // member of an unknown specialization. However, this will only
1535 // parse correctly as a template, so suggest the keyword 'template'
1536 // before 'getAs' and treat this as a dependent template name.
1537 std::string Name;
1538 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1539 Name = Id.Identifier->getName();
1540 else {
1541 Name = "operator ";
1542 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1543 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1544 else
1545 Name += Id.Identifier->getName();
1546 }
1547 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1548 << Name
1549 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001550 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001551 SS, Id, ObjectType,
1552 EnteringContext, Template);
1553 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001554 return true;
1555 }
1556 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001557 break;
1558
Douglas Gregor014e88d2009-11-03 23:16:33 +00001559 case UnqualifiedId::IK_ConstructorName: {
1560 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001561 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001562 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001563 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1564 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001565 EnteringContext, Template,
1566 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001567 break;
1568 }
1569
Douglas Gregor014e88d2009-11-03 23:16:33 +00001570 case UnqualifiedId::IK_DestructorName: {
1571 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001572 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001573 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001574 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001575 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001576 TemplateName, ObjectType,
1577 EnteringContext, Template);
1578 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001579 return true;
1580 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001581 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1582 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001583 EnteringContext, Template,
1584 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001585
John McCallb3d87482010-08-24 05:47:05 +00001586 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001587 Diag(NameLoc, diag::err_destructor_template_id)
1588 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001589 return true;
1590 }
1591 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001592 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001593 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001594
1595 default:
1596 return false;
1597 }
1598
1599 if (TNK == TNK_Non_template)
1600 return false;
1601
1602 // Parse the enclosed template argument list.
1603 SourceLocation LAngleLoc, RAngleLoc;
1604 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001605 if (Tok.is(tok::less) &&
1606 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001607 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001608 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001609 RAngleLoc))
1610 return true;
1611
1612 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001613 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1614 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001615 // Form a parsed representation of the template-id to be stored in the
1616 // UnqualifiedId.
1617 TemplateIdAnnotation *TemplateId
1618 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1619
1620 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1621 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001622 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001623 TemplateId->TemplateNameLoc = Id.StartLocation;
1624 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001625 TemplateId->Name = 0;
1626 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1627 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001628 }
1629
Douglas Gregor059101f2011-03-02 00:47:37 +00001630 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001631 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001632 TemplateId->Kind = TNK;
1633 TemplateId->LAngleLoc = LAngleLoc;
1634 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001635 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001636 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001637 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001638 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001639
1640 Id.setTemplateId(TemplateId);
1641 return false;
1642 }
1643
1644 // Bundle the template arguments together.
1645 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001646 TemplateArgs.size());
1647
1648 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001649 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001650 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001651 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001652 RAngleLoc);
1653 if (Type.isInvalid())
1654 return true;
1655
1656 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1657 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1658 else
1659 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1660
1661 return false;
1662}
1663
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001664/// \brief Parse an operator-function-id or conversion-function-id as part
1665/// of a C++ unqualified-id.
1666///
1667/// This routine is responsible only for parsing the operator-function-id or
1668/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001669///
1670/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001671/// operator-function-id: [C++ 13.5]
1672/// 'operator' operator
1673///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001674/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001675/// new delete new[] delete[]
1676/// + - * / % ^ & | ~
1677/// ! = < > += -= *= /= %=
1678/// ^= &= |= << >> >>= <<= == !=
1679/// <= >= && || ++ -- , ->* ->
1680/// () []
1681///
1682/// conversion-function-id: [C++ 12.3.2]
1683/// operator conversion-type-id
1684///
1685/// conversion-type-id:
1686/// type-specifier-seq conversion-declarator[opt]
1687///
1688/// conversion-declarator:
1689/// ptr-operator conversion-declarator[opt]
1690/// \endcode
1691///
1692/// \param The nested-name-specifier that preceded this unqualified-id. If
1693/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1694///
1695/// \param EnteringContext whether we are entering the scope of the
1696/// nested-name-specifier.
1697///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001698/// \param ObjectType if this unqualified-id occurs within a member access
1699/// expression, the type of the base object whose member is being accessed.
1700///
1701/// \param Result on a successful parse, contains the parsed unqualified-id.
1702///
1703/// \returns true if parsing fails, false otherwise.
1704bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001705 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001706 UnqualifiedId &Result) {
1707 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1708
1709 // Consume the 'operator' keyword.
1710 SourceLocation KeywordLoc = ConsumeToken();
1711
1712 // Determine what kind of operator name we have.
1713 unsigned SymbolIdx = 0;
1714 SourceLocation SymbolLocations[3];
1715 OverloadedOperatorKind Op = OO_None;
1716 switch (Tok.getKind()) {
1717 case tok::kw_new:
1718 case tok::kw_delete: {
1719 bool isNew = Tok.getKind() == tok::kw_new;
1720 // Consume the 'new' or 'delete'.
1721 SymbolLocations[SymbolIdx++] = ConsumeToken();
1722 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001723 // Consume the '[' and ']'.
1724 BalancedDelimiterTracker T(*this, tok::l_square);
1725 T.consumeOpen();
1726 T.consumeClose();
1727 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001728 return true;
1729
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001730 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1731 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001732 Op = isNew? OO_Array_New : OO_Array_Delete;
1733 } else {
1734 Op = isNew? OO_New : OO_Delete;
1735 }
1736 break;
1737 }
1738
1739#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1740 case tok::Token: \
1741 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1742 Op = OO_##Name; \
1743 break;
1744#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1745#include "clang/Basic/OperatorKinds.def"
1746
1747 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001748 // Consume the '(' and ')'.
1749 BalancedDelimiterTracker T(*this, tok::l_paren);
1750 T.consumeOpen();
1751 T.consumeClose();
1752 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001753 return true;
1754
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001755 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1756 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001757 Op = OO_Call;
1758 break;
1759 }
1760
1761 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001762 // Consume the '[' and ']'.
1763 BalancedDelimiterTracker T(*this, tok::l_square);
1764 T.consumeOpen();
1765 T.consumeClose();
1766 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001767 return true;
1768
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001769 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1770 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001771 Op = OO_Subscript;
1772 break;
1773 }
1774
1775 case tok::code_completion: {
1776 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001777 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001778 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001779 // Don't try to parse any further.
1780 return true;
1781 }
1782
1783 default:
1784 break;
1785 }
1786
1787 if (Op != OO_None) {
1788 // We have parsed an operator-function-id.
1789 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1790 return false;
1791 }
Sean Hunt0486d742009-11-28 04:44:28 +00001792
1793 // Parse a literal-operator-id.
1794 //
1795 // literal-operator-id: [C++0x 13.5.8]
1796 // operator "" identifier
1797
1798 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
Richard Smith7fe62082011-10-15 05:09:34 +00001799 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001800 if (Tok.getLength() != 2)
1801 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1802 ConsumeStringToken();
1803
1804 if (Tok.isNot(tok::identifier)) {
1805 Diag(Tok.getLocation(), diag::err_expected_ident);
1806 return true;
1807 }
1808
1809 IdentifierInfo *II = Tok.getIdentifierInfo();
1810 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001811 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001812 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001813
1814 // Parse a conversion-function-id.
1815 //
1816 // conversion-function-id: [C++ 12.3.2]
1817 // operator conversion-type-id
1818 //
1819 // conversion-type-id:
1820 // type-specifier-seq conversion-declarator[opt]
1821 //
1822 // conversion-declarator:
1823 // ptr-operator conversion-declarator[opt]
1824
1825 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001826 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001827 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001828 return true;
1829
1830 // Parse the conversion-declarator, which is merely a sequence of
1831 // ptr-operators.
1832 Declarator D(DS, Declarator::TypeNameContext);
1833 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1834
1835 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001836 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001837 if (Ty.isInvalid())
1838 return true;
1839
1840 // Note that this is a conversion-function-id.
1841 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1842 D.getSourceRange().getEnd());
1843 return false;
1844}
1845
1846/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1847/// name of an entity.
1848///
1849/// \code
1850/// unqualified-id: [C++ expr.prim.general]
1851/// identifier
1852/// operator-function-id
1853/// conversion-function-id
1854/// [C++0x] literal-operator-id [TODO]
1855/// ~ class-name
1856/// template-id
1857///
1858/// \endcode
1859///
1860/// \param The nested-name-specifier that preceded this unqualified-id. If
1861/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1862///
1863/// \param EnteringContext whether we are entering the scope of the
1864/// nested-name-specifier.
1865///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001866/// \param AllowDestructorName whether we allow parsing of a destructor name.
1867///
1868/// \param AllowConstructorName whether we allow parsing a constructor name.
1869///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001870/// \param ObjectType if this unqualified-id occurs within a member access
1871/// expression, the type of the base object whose member is being accessed.
1872///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001873/// \param Result on a successful parse, contains the parsed unqualified-id.
1874///
1875/// \returns true if parsing fails, false otherwise.
1876bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1877 bool AllowDestructorName,
1878 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001879 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001880 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001881
1882 // Handle 'A::template B'. This is for template-ids which have not
1883 // already been annotated by ParseOptionalCXXScopeSpecifier().
1884 bool TemplateSpecified = false;
1885 SourceLocation TemplateKWLoc;
1886 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1887 (ObjectType || SS.isSet())) {
1888 TemplateSpecified = true;
1889 TemplateKWLoc = ConsumeToken();
1890 }
1891
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001892 // unqualified-id:
1893 // identifier
1894 // template-id (when it hasn't already been annotated)
1895 if (Tok.is(tok::identifier)) {
1896 // Consume the identifier.
1897 IdentifierInfo *Id = Tok.getIdentifierInfo();
1898 SourceLocation IdLoc = ConsumeToken();
1899
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001900 if (!getLang().CPlusPlus) {
1901 // If we're not in C++, only identifiers matter. Record the
1902 // identifier and return.
1903 Result.setIdentifier(Id, IdLoc);
1904 return false;
1905 }
1906
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001907 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001908 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001909 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001910 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001911 &SS, false, false,
1912 ParsedType(),
1913 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001914 IdLoc, IdLoc);
1915 } else {
1916 // We have parsed an identifier.
1917 Result.setIdentifier(Id, IdLoc);
1918 }
1919
1920 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001921 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001922 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001923 ObjectType, Result,
1924 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001925
1926 return false;
1927 }
1928
1929 // unqualified-id:
1930 // template-id (already parsed and annotated)
1931 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001932 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001933
1934 // If the template-name names the current class, then this is a constructor
1935 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001936 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001937 if (SS.isSet()) {
1938 // C++ [class.qual]p2 specifies that a qualified template-name
1939 // is taken as the constructor name where a constructor can be
1940 // declared. Thus, the template arguments are extraneous, so
1941 // complain about them and remove them entirely.
1942 Diag(TemplateId->TemplateNameLoc,
1943 diag::err_out_of_line_constructor_template_id)
1944 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001945 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001946 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1947 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1948 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001949 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001950 &SS, false, false,
1951 ParsedType(),
1952 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001953 TemplateId->TemplateNameLoc,
1954 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001955 ConsumeToken();
1956 return false;
1957 }
1958
1959 Result.setConstructorTemplateId(TemplateId);
1960 ConsumeToken();
1961 return false;
1962 }
1963
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001964 // We have already parsed a template-id; consume the annotation token as
1965 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001966 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001967 ConsumeToken();
1968 return false;
1969 }
1970
1971 // unqualified-id:
1972 // operator-function-id
1973 // conversion-function-id
1974 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001975 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001976 return true;
1977
Sean Hunte6252d12009-11-28 08:58:14 +00001978 // If we have an operator-function-id or a literal-operator-id and the next
1979 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001980 //
1981 // template-id:
1982 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001983 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1984 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001985 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001986 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1987 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001988 Result,
1989 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001990
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001991 return false;
1992 }
1993
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001994 if (getLang().CPlusPlus &&
1995 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001996 // C++ [expr.unary.op]p10:
1997 // There is an ambiguity in the unary-expression ~X(), where X is a
1998 // class-name. The ambiguity is resolved in favor of treating ~ as a
1999 // unary complement rather than treating ~X as referring to a destructor.
2000
2001 // Parse the '~'.
2002 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00002003
2004 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2005 DeclSpec DS(AttrFactory);
2006 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2007 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2008 Result.setDestructorName(TildeLoc, Type, EndLoc);
2009 return false;
2010 }
2011 return true;
2012 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002013
2014 // Parse the class-name.
2015 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00002016 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002017 return true;
2018 }
2019
2020 // Parse the class-name (or template-name in a simple-template-id).
2021 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2022 SourceLocation ClassNameLoc = ConsumeToken();
2023
Douglas Gregor0278e122010-05-05 05:58:24 +00002024 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002025 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002026 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00002027 EnteringContext, ObjectType, Result,
2028 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002029 }
2030
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002031 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002032 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2033 ClassNameLoc, getCurScope(),
2034 SS, ObjectType,
2035 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002036 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002037 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002038
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002039 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002040 return false;
2041 }
2042
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002043 Diag(Tok, diag::err_expected_unqualified_id)
2044 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002045 return true;
2046}
2047
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002048/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2049/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002050///
Chris Lattner59232d32009-01-04 21:25:24 +00002051/// This method is called to parse the new expression after the optional :: has
2052/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2053/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002054///
2055/// new-expression:
2056/// '::'[opt] 'new' new-placement[opt] new-type-id
2057/// new-initializer[opt]
2058/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2059/// new-initializer[opt]
2060///
2061/// new-placement:
2062/// '(' expression-list ')'
2063///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002064/// new-type-id:
2065/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002066/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002067///
2068/// new-declarator:
2069/// ptr-operator new-declarator[opt]
2070/// direct-new-declarator
2071///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002072/// new-initializer:
2073/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002074/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002075///
John McCall60d7b3a2010-08-24 06:29:42 +00002076ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002077Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2078 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2079 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002080
2081 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2082 // second form of new-expression. It can't be a new-type-id.
2083
Sebastian Redla55e52c2008-11-25 22:21:31 +00002084 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002085 SourceLocation PlacementLParen, PlacementRParen;
2086
Douglas Gregor4bd40312010-07-13 15:54:32 +00002087 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002088 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002089 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002090 if (Tok.is(tok::l_paren)) {
2091 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002092 BalancedDelimiterTracker T(*this, tok::l_paren);
2093 T.consumeOpen();
2094 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002095 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2096 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002097 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002098 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002099
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002100 T.consumeClose();
2101 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002102 if (PlacementRParen.isInvalid()) {
2103 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002104 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002105 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002106
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002107 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002108 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002109 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002110 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002111 } else {
2112 // We still need the type.
2113 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002114 BalancedDelimiterTracker T(*this, tok::l_paren);
2115 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002116 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002117 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002118 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002119 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002120 T.consumeClose();
2121 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002122 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002123 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002124 if (ParseCXXTypeSpecifierSeq(DS))
2125 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002126 else {
2127 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002128 ParseDeclaratorInternal(DeclaratorInfo,
2129 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002130 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002131 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002132 }
2133 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002134 // A new-type-id is a simplified type-id, where essentially the
2135 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002136 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002137 if (ParseCXXTypeSpecifierSeq(DS))
2138 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002139 else {
2140 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002141 ParseDeclaratorInternal(DeclaratorInfo,
2142 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002143 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002144 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002145 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002146 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002147 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002148 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002149
Sebastian Redla55e52c2008-11-25 22:21:31 +00002150 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002151 SourceLocation ConstructorLParen, ConstructorRParen;
2152
2153 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002154 BalancedDelimiterTracker T(*this, tok::l_paren);
2155 T.consumeOpen();
2156 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002157 if (Tok.isNot(tok::r_paren)) {
2158 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002159 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2160 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002161 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002162 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002163 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002164 T.consumeClose();
2165 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002166 if (ConstructorRParen.isInvalid()) {
2167 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002168 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002169 }
Richard Smith29e3a312011-10-15 03:38:41 +00002170 } else if (Tok.is(tok::l_brace) && getLang().CPlusPlus0x) {
Richard Smith7fe62082011-10-15 05:09:34 +00002171 Diag(Tok.getLocation(),
2172 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002173 // FIXME: Have to communicate the init-list to ActOnCXXNew.
2174 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002175 }
2176
Sebastian Redlf53597f2009-03-15 17:47:39 +00002177 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2178 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00002179 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002180 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002181}
2182
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002183/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2184/// passed to ParseDeclaratorInternal.
2185///
2186/// direct-new-declarator:
2187/// '[' expression ']'
2188/// direct-new-declarator '[' constant-expression ']'
2189///
Chris Lattner59232d32009-01-04 21:25:24 +00002190void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002191 // Parse the array dimensions.
2192 bool first = true;
2193 while (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002194 BalancedDelimiterTracker T(*this, tok::l_square);
2195 T.consumeOpen();
2196
John McCall60d7b3a2010-08-24 06:29:42 +00002197 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002198 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002199 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002200 // Recover
2201 SkipUntil(tok::r_square);
2202 return;
2203 }
2204 first = false;
2205
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002206 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002207
2208 ParsedAttributes attrs(AttrFactory);
2209 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002210 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002211 Size.release(),
2212 T.getOpenLocation(),
2213 T.getCloseLocation()),
2214 attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002215
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002216 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002217 return;
2218 }
2219}
2220
2221/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2222/// This ambiguity appears in the syntax of the C++ new operator.
2223///
2224/// new-expression:
2225/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2226/// new-initializer[opt]
2227///
2228/// new-placement:
2229/// '(' expression-list ')'
2230///
John McCallca0408f2010-08-23 06:44:23 +00002231bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002232 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002233 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002234 // The '(' was already consumed.
2235 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002236 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002237 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002238 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002239 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002240 }
2241
2242 // It's not a type, it has to be an expression list.
2243 // Discard the comma locations - ActOnCXXNew has enough parameters.
2244 CommaLocsTy CommaLocs;
2245 return ParseExpressionList(PlacementArgs, CommaLocs);
2246}
2247
2248/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2249/// to free memory allocated by new.
2250///
Chris Lattner59232d32009-01-04 21:25:24 +00002251/// This method is called to parse the 'delete' expression after the optional
2252/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2253/// and "Start" is its location. Otherwise, "Start" is the location of the
2254/// 'delete' token.
2255///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002256/// delete-expression:
2257/// '::'[opt] 'delete' cast-expression
2258/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002259ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002260Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2261 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2262 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002263
2264 // Array delete?
2265 bool ArrayDelete = false;
2266 if (Tok.is(tok::l_square)) {
2267 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002268 BalancedDelimiterTracker T(*this, tok::l_square);
2269
2270 T.consumeOpen();
2271 T.consumeClose();
2272 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002273 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002274 }
2275
John McCall60d7b3a2010-08-24 06:29:42 +00002276 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002277 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002278 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002279
John McCall9ae2f072010-08-23 23:25:46 +00002280 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002281}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002282
Mike Stump1eb44332009-09-09 15:08:12 +00002283static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002284 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002285 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002286 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002287 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002288 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002289 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002290 case tok::kw___has_trivial_constructor:
2291 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002292 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002293 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2294 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2295 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002296 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2297 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002298 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002299 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2300 case tok::kw___is_compound: return UTT_IsCompound;
2301 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002302 case tok::kw___is_empty: return UTT_IsEmpty;
2303 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002304 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002305 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2306 case tok::kw___is_function: return UTT_IsFunction;
2307 case tok::kw___is_fundamental: return UTT_IsFundamental;
2308 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00002309 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2310 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2311 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2312 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2313 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002314 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002315 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002316 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002317 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002318 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002319 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002320 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2321 case tok::kw___is_scalar: return UTT_IsScalar;
2322 case tok::kw___is_signed: return UTT_IsSigned;
2323 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2324 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002325 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002326 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002327 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2328 case tok::kw___is_void: return UTT_IsVoid;
2329 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002330 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002331}
2332
2333static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2334 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002335 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002336 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002337 case tok::kw___is_convertible: return BTT_IsConvertible;
2338 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002339 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002340 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002341 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002342}
2343
John Wiegley21ff2e52011-04-28 00:16:57 +00002344static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2345 switch(kind) {
2346 default: llvm_unreachable("Not a known binary type trait");
2347 case tok::kw___array_rank: return ATT_ArrayRank;
2348 case tok::kw___array_extent: return ATT_ArrayExtent;
2349 }
2350}
2351
John Wiegley55262202011-04-25 06:54:41 +00002352static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2353 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002354 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002355 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2356 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2357 }
2358}
2359
Sebastian Redl64b45f72009-01-05 20:52:13 +00002360/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2361/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2362/// templates.
2363///
2364/// primary-expression:
2365/// [GNU] unary-type-trait '(' type-id ')'
2366///
John McCall60d7b3a2010-08-24 06:29:42 +00002367ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002368 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2369 SourceLocation Loc = ConsumeToken();
2370
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002371 BalancedDelimiterTracker T(*this, tok::l_paren);
2372 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002373 return ExprError();
2374
2375 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2376 // there will be cryptic errors about mismatched parentheses and missing
2377 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002378 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002379
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002380 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002381
Douglas Gregor809070a2009-02-18 17:45:20 +00002382 if (Ty.isInvalid())
2383 return ExprError();
2384
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002385 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002386}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002387
Francois Pichet6ad6f282010-12-07 00:08:36 +00002388/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2389/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2390/// templates.
2391///
2392/// primary-expression:
2393/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2394///
2395ExprResult Parser::ParseBinaryTypeTrait() {
2396 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2397 SourceLocation Loc = ConsumeToken();
2398
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002399 BalancedDelimiterTracker T(*this, tok::l_paren);
2400 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002401 return ExprError();
2402
2403 TypeResult LhsTy = ParseTypeName();
2404 if (LhsTy.isInvalid()) {
2405 SkipUntil(tok::r_paren);
2406 return ExprError();
2407 }
2408
2409 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2410 SkipUntil(tok::r_paren);
2411 return ExprError();
2412 }
2413
2414 TypeResult RhsTy = ParseTypeName();
2415 if (RhsTy.isInvalid()) {
2416 SkipUntil(tok::r_paren);
2417 return ExprError();
2418 }
2419
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002420 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002421
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002422 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2423 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002424}
2425
John Wiegley21ff2e52011-04-28 00:16:57 +00002426/// ParseArrayTypeTrait - Parse the built-in array type-trait
2427/// pseudo-functions.
2428///
2429/// primary-expression:
2430/// [Embarcadero] '__array_rank' '(' type-id ')'
2431/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2432///
2433ExprResult Parser::ParseArrayTypeTrait() {
2434 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2435 SourceLocation Loc = ConsumeToken();
2436
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002437 BalancedDelimiterTracker T(*this, tok::l_paren);
2438 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002439 return ExprError();
2440
2441 TypeResult Ty = ParseTypeName();
2442 if (Ty.isInvalid()) {
2443 SkipUntil(tok::comma);
2444 SkipUntil(tok::r_paren);
2445 return ExprError();
2446 }
2447
2448 switch (ATT) {
2449 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002450 T.consumeClose();
2451 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2452 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002453 }
2454 case ATT_ArrayExtent: {
2455 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2456 SkipUntil(tok::r_paren);
2457 return ExprError();
2458 }
2459
2460 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002461 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002462
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002463 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2464 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002465 }
John Wiegley21ff2e52011-04-28 00:16:57 +00002466 }
David Blaikie30263482012-01-20 21:50:17 +00002467 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley21ff2e52011-04-28 00:16:57 +00002468}
2469
John Wiegley55262202011-04-25 06:54:41 +00002470/// ParseExpressionTrait - Parse built-in expression-trait
2471/// pseudo-functions like __is_lvalue_expr( xxx ).
2472///
2473/// primary-expression:
2474/// [Embarcadero] expression-trait '(' expression ')'
2475///
2476ExprResult Parser::ParseExpressionTrait() {
2477 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2478 SourceLocation Loc = ConsumeToken();
2479
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002480 BalancedDelimiterTracker T(*this, tok::l_paren);
2481 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002482 return ExprError();
2483
2484 ExprResult Expr = ParseExpression();
2485
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002486 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002487
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002488 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2489 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002490}
2491
2492
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002493/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2494/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2495/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002496ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002497Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002498 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002499 BalancedDelimiterTracker &Tracker) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002500 assert(getLang().CPlusPlus && "Should only be called for C++!");
2501 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2502 assert(isTypeIdInParens() && "Not a type-id!");
2503
John McCall60d7b3a2010-08-24 06:29:42 +00002504 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002505 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002506
2507 // We need to disambiguate a very ugly part of the C++ syntax:
2508 //
2509 // (T())x; - type-id
2510 // (T())*x; - type-id
2511 // (T())/x; - expression
2512 // (T()); - expression
2513 //
2514 // The bad news is that we cannot use the specialized tentative parser, since
2515 // it can only verify that the thing inside the parens can be parsed as
2516 // type-id, it is not useful for determining the context past the parens.
2517 //
2518 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002519 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002520 //
2521 // It uses a scheme similar to parsing inline methods. The parenthesized
2522 // tokens are cached, the context that follows is determined (possibly by
2523 // parsing a cast-expression), and then we re-introduce the cached tokens
2524 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002525
Mike Stump1eb44332009-09-09 15:08:12 +00002526 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002527 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002528
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002529 // Store the tokens of the parentheses. We will parse them after we determine
2530 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002531 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002532 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002533 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002534 return ExprError();
2535 }
2536
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002537 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002538 ParseAs = CompoundLiteral;
2539 } else {
2540 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002541 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2542 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2543 NotCastExpr = true;
2544 } else {
2545 // Try parsing the cast-expression that may follow.
2546 // If it is not a cast-expression, NotCastExpr will be true and no token
2547 // will be consumed.
2548 Result = ParseCastExpression(false/*isUnaryExpression*/,
2549 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002550 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002551 // type-id has priority.
2552 true/*isTypeCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002553 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002554
2555 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2556 // an expression.
2557 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002558 }
2559
Mike Stump1eb44332009-09-09 15:08:12 +00002560 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002561 Toks.push_back(Tok);
2562 // Re-enter the stored parenthesized tokens into the token stream, so we may
2563 // parse them now.
2564 PP.EnterTokenStream(Toks.data(), Toks.size(),
2565 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2566 // Drop the current token and bring the first cached one. It's the same token
2567 // as when we entered this function.
2568 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002569
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002570 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002571 // Parse the type declarator.
2572 DeclSpec DS(AttrFactory);
2573 ParseSpecifierQualifierList(DS);
2574 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2575 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002576
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002577 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002578 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002579
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002580 if (ParseAs == CompoundLiteral) {
2581 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002582 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002583 return ParseCompoundLiteralExpression(Ty.get(),
2584 Tracker.getOpenLocation(),
2585 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002586 }
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002588 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2589 assert(ParseAs == CastExpr);
2590
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002591 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002592 return ExprError();
2593
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002594 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002595 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002596 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2597 DeclaratorInfo, CastTy,
2598 Tracker.getCloseLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002599 return move(Result);
2600 }
Mike Stump1eb44332009-09-09 15:08:12 +00002601
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002602 // Not a compound literal, and not followed by a cast-expression.
2603 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002604
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002605 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002606 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002607 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002608 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2609 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002610
2611 // Match the ')'.
2612 if (Result.isInvalid()) {
2613 SkipUntil(tok::r_paren);
2614 return ExprError();
2615 }
Mike Stump1eb44332009-09-09 15:08:12 +00002616
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002617 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002618 return move(Result);
2619}