blob: 2e0b6a55d5df466dddab3e9fe2e9687efdb2b3fd [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
Douglas Gregorae7902c2011-08-04 15:30:47 +000018#include "clang/Sema/Scope.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000020#include "llvm/Support/ErrorHandling.h"
21
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Richard Smithea698b32011-04-14 21:45:45 +000024static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
25 switch (Kind) {
26 case tok::kw_template: return 0;
27 case tok::kw_const_cast: return 1;
28 case tok::kw_dynamic_cast: return 2;
29 case tok::kw_reinterpret_cast: return 3;
30 case tok::kw_static_cast: return 4;
31 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000032 llvm_unreachable("Unknown type for digraph error message.");
Richard Smithea698b32011-04-14 21:45:45 +000033 }
34}
35
36// Are the two tokens adjacent in the same source file?
37static bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
38 SourceManager &SM = PP.getSourceManager();
39 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000040 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smithea698b32011-04-14 21:45:45 +000041 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
42}
43
44// Suggest fixit for "<::" after a cast.
45static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
46 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
47 // Pull '<:' and ':' off token stream.
48 if (!AtDigraph)
49 PP.Lex(DigraphToken);
50 PP.Lex(ColonToken);
51
52 SourceRange Range;
53 Range.setBegin(DigraphToken.getLocation());
54 Range.setEnd(ColonToken.getLocation());
55 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
56 << SelectDigraphErrorMessage(Kind)
57 << FixItHint::CreateReplacement(Range, "< ::");
58
59 // Update token information to reflect their change in token type.
60 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000061 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smithea698b32011-04-14 21:45:45 +000062 ColonToken.setLength(2);
63 DigraphToken.setKind(tok::less);
64 DigraphToken.setLength(1);
65
66 // Push new tokens back to token stream.
67 PP.EnterToken(ColonToken);
68 if (!AtDigraph)
69 PP.EnterToken(DigraphToken);
70}
71
Richard Trieu950be712011-09-19 19:01:00 +000072// Check for '<::' which should be '< ::' instead of '[:' when following
73// a template name.
74void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
75 bool EnteringContext,
76 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieuc11030e2011-09-20 20:03:50 +000077 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu950be712011-09-19 19:01:00 +000078 return;
79
80 Token SecondToken = GetLookAheadToken(2);
81 if (!SecondToken.is(tok::colon) || !AreTokensAdjacent(PP, Next, SecondToken))
82 return;
83
84 TemplateTy Template;
85 UnqualifiedId TemplateName;
86 TemplateName.setIdentifier(&II, Tok.getLocation());
87 bool MemberOfUnknownSpecialization;
88 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
89 TemplateName, ObjectType, EnteringContext,
90 Template, MemberOfUnknownSpecialization))
91 return;
92
93 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
94 /*AtDigraph*/false);
95}
96
Mike Stump1eb44332009-09-09 15:08:12 +000097/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000098///
99/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000100/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000101/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000102///
103/// '::'[opt] nested-name-specifier
104/// '::'
105///
106/// nested-name-specifier:
107/// type-name '::'
108/// namespace-name '::'
109/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000110/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000111///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000112///
Mike Stump1eb44332009-09-09 15:08:12 +0000113/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000114/// nested-name-specifier (or empty)
115///
Mike Stump1eb44332009-09-09 15:08:12 +0000116/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000117/// the "." or "->" of a member access expression, this parameter provides the
118/// type of the object whose members are being accessed.
119///
120/// \param EnteringContext whether we will be entering into the context of
121/// the nested-name-specifier after parsing it.
122///
Douglas Gregord4dca082010-02-24 18:44:31 +0000123/// \param MayBePseudoDestructor When non-NULL, points to a flag that
124/// indicates whether this nested-name-specifier may be part of a
125/// pseudo-destructor name. In this case, the flag will be set false
126/// if we don't actually end up parsing a destructor name. Moreorover,
127/// if we do end up determining that we are parsing a destructor name,
128/// the last component of the nested-name-specifier is not parsed as
129/// part of the scope specifier.
130
Douglas Gregorb10cd042010-02-21 18:36:56 +0000131/// member access expression, e.g., the \p T:: in \p p->T::m.
132///
John McCall9ba61662010-02-26 08:45:28 +0000133/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000134bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000135 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000136 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000137 bool *MayBePseudoDestructor,
138 bool IsTypename) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000139 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000140 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000142 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +0000143 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
144 Tok.getAnnotationRange(),
145 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000146 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000147 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000148 }
Chris Lattnere607e802009-01-04 21:14:15 +0000149
Douglas Gregor39a8de12009-02-25 19:37:18 +0000150 bool HasScopeSpecifier = false;
151
Chris Lattner5b454732009-01-05 03:55:46 +0000152 if (Tok.is(tok::coloncolon)) {
153 // ::new and ::delete aren't nested-name-specifiers.
154 tok::TokenKind NextKind = NextToken().getKind();
155 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
156 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattner55a7cef2009-01-05 00:13:00 +0000158 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000159 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
160 return true;
161
Douglas Gregor39a8de12009-02-25 19:37:18 +0000162 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000163 }
164
Douglas Gregord4dca082010-02-24 18:44:31 +0000165 bool CheckForDestructor = false;
166 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
167 CheckForDestructor = true;
168 *MayBePseudoDestructor = false;
169 }
170
David Blaikie42d6d0c2011-12-04 05:04:18 +0000171 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
172 DeclSpec DS(AttrFactory);
173 SourceLocation DeclLoc = Tok.getLocation();
174 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
175 if (Tok.isNot(tok::coloncolon)) {
176 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
177 return false;
178 }
179
180 SourceLocation CCLoc = ConsumeToken();
181 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
182 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
183
184 HasScopeSpecifier = true;
185 }
186
Douglas Gregor39a8de12009-02-25 19:37:18 +0000187 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000188 if (HasScopeSpecifier) {
189 // C++ [basic.lookup.classref]p5:
190 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000191 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000192 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000193 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000194 // the class-name-or-namespace-name is looked up in global scope as a
195 // class-name or namespace-name.
196 //
197 // To implement this, we clear out the object type as soon as we've
198 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000199 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000200
201 if (Tok.is(tok::code_completion)) {
202 // Code completion for a nested-name-specifier, where the code
203 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000204 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000205 // Include code completion token into the range of the scope otherwise
206 // when we try to annotate the scope tokens the dangling code completion
207 // token will cause assertion in
208 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000209 SS.setEndLoc(Tok.getLocation());
210 cutOffParsing();
211 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000212 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Douglas Gregor39a8de12009-02-25 19:37:18 +0000215 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000216 // nested-name-specifier 'template'[opt] simple-template-id '::'
217
218 // Parse the optional 'template' keyword, then make sure we have
219 // 'identifier <' after it.
220 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000221 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000222 // nested-name-specifier, since they aren't allowed to start with
223 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000224 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000225 break;
226
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000227 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000228 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000229
230 UnqualifiedId TemplateName;
231 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000232 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000233 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000234 ConsumeToken();
235 } else if (Tok.is(tok::kw_operator)) {
236 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000237 TemplateName)) {
238 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000239 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000240 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000241
Sean Hunte6252d12009-11-28 08:58:14 +0000242 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
243 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000244 Diag(TemplateName.getSourceRange().getBegin(),
245 diag::err_id_after_template_in_nested_name_spec)
246 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000247 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000248 break;
249 }
250 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000251 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000252 break;
253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000255 // If the next token is not '<', we have a qualified-id that refers
256 // to a template name, such as T::template apply, but is not a
257 // template-id.
258 if (Tok.isNot(tok::less)) {
259 TPA.Revert();
260 break;
261 }
262
263 // Commit to parsing the template-id.
264 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000265 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000266 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000267 TemplateKWLoc,
268 SS,
269 TemplateName,
270 ObjectType,
271 EnteringContext,
272 Template)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000273 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000274 TemplateKWLoc, false))
275 return true;
276 } else
John McCall9ba61662010-02-26 08:45:28 +0000277 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Chris Lattner77cf72a2009-06-26 03:47:46 +0000279 continue;
280 }
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Douglas Gregor39a8de12009-02-25 19:37:18 +0000282 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000283 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000284 //
285 // simple-template-id '::'
286 //
287 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000288 // right kind (it should name a type or be dependent), and then
289 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000290 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000291 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
292 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000293 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000294 }
295
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000296 // Consume the template-id token.
297 ConsumeToken();
298
299 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
300 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000301
David Blaikie6796fc12011-11-07 03:30:03 +0000302 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000303
304 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
305 TemplateId->getTemplateArgs(),
306 TemplateId->NumArgs);
307
308 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
309 /*FIXME:*/SourceLocation(),
310 SS,
311 TemplateId->Template,
312 TemplateId->TemplateNameLoc,
313 TemplateId->LAngleLoc,
314 TemplateArgsPtr,
315 TemplateId->RAngleLoc,
316 CCLoc,
317 EnteringContext)) {
318 SourceLocation StartLoc
319 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
320 : TemplateId->TemplateNameLoc;
321 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000322 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000323
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000324 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000325 }
326
Chris Lattner5c7f7862009-06-26 03:52:38 +0000327
328 // The rest of the nested-name-specifier possibilities start with
329 // tok::identifier.
330 if (Tok.isNot(tok::identifier))
331 break;
332
333 IdentifierInfo &II = *Tok.getIdentifierInfo();
334
335 // nested-name-specifier:
336 // type-name '::'
337 // namespace-name '::'
338 // nested-name-specifier identifier '::'
339 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000340
341 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
342 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000343 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000344 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
345 Tok.getLocation(),
346 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000347 EnteringContext) &&
348 // If the token after the colon isn't an identifier, it's still an
349 // error, but they probably meant something else strange so don't
350 // recover like this.
351 PP.LookAhead(1).is(tok::identifier)) {
352 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000353 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000354
355 // Recover as if the user wrote '::'.
356 Next.setKind(tok::coloncolon);
357 }
Chris Lattner46646492009-12-07 01:36:53 +0000358 }
359
Chris Lattner5c7f7862009-06-26 03:52:38 +0000360 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000361 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000362 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000363 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000364 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000365 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000366 }
367
Chris Lattner5c7f7862009-06-26 03:52:38 +0000368 // We have an identifier followed by a '::'. Lookup this name
369 // as the name in a nested-name-specifier.
370 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000371 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
372 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000373 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000375 HasScopeSpecifier = true;
376 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
377 ObjectType, EnteringContext, SS))
378 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
379
Chris Lattner5c7f7862009-06-26 03:52:38 +0000380 continue;
381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Richard Trieu950be712011-09-19 19:01:00 +0000383 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000384
Chris Lattner5c7f7862009-06-26 03:52:38 +0000385 // nested-name-specifier:
386 // type-name '<'
387 if (Next.is(tok::less)) {
388 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000389 UnqualifiedId TemplateName;
390 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000391 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000392 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000393 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000394 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000395 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000396 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000397 Template,
398 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000399 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000400 // with a template-id annotation. We do not permit the
401 // template-id to be translated into a type annotation,
402 // because some clients (e.g., the parsing of class template
403 // specializations) still want to see the original template-id
404 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000405 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000406 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000407 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000408 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000409 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000410 }
411
412 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000413 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000414 // We have something like t::getAs<T>, where getAs is a
415 // member of an unknown specialization. However, this will only
416 // parse correctly as a template, so suggest the keyword 'template'
417 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000418 unsigned DiagID = diag::err_missing_dependent_template_keyword;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000419 if (getLang().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000420 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000421
422 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000423 << II.getName()
424 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
425
Douglas Gregord6ab2322010-06-16 23:00:59 +0000426 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000427 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000428 Tok.getLocation(), SS,
429 TemplateName, ObjectType,
430 EnteringContext, Template)) {
431 // Consume the identifier.
432 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000433 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000434 SourceLocation(), false))
435 return true;
436 }
437 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000438 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000439
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000440 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000441 }
442 }
443
Douglas Gregor39a8de12009-02-25 19:37:18 +0000444 // We don't have any tokens that form the beginning of a
445 // nested-name-specifier, so we're done.
446 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Douglas Gregord4dca082010-02-24 18:44:31 +0000449 // Even if we didn't see any pieces of a nested-name-specifier, we
450 // still check whether there is a tilde in this position, which
451 // indicates a potential pseudo-destructor.
452 if (CheckForDestructor && Tok.is(tok::tilde))
453 *MayBePseudoDestructor = true;
454
John McCall9ba61662010-02-26 08:45:28 +0000455 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000456}
457
458/// ParseCXXIdExpression - Handle id-expression.
459///
460/// id-expression:
461/// unqualified-id
462/// qualified-id
463///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000464/// qualified-id:
465/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
466/// '::' identifier
467/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000468/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000469///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000470/// NOTE: The standard specifies that, for qualified-id, the parser does not
471/// expect:
472///
473/// '::' conversion-function-id
474/// '::' '~' class-name
475///
476/// This may cause a slight inconsistency on diagnostics:
477///
478/// class C {};
479/// namespace A {}
480/// void f() {
481/// :: A :: ~ C(); // Some Sema error about using destructor with a
482/// // namespace.
483/// :: ~ C(); // Some Parser error like 'unexpected ~'.
484/// }
485///
486/// We simplify the parser a bit and make it work like:
487///
488/// qualified-id:
489/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
490/// '::' unqualified-id
491///
492/// That way Sema can handle and report similar errors for namespaces and the
493/// global scope.
494///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000495/// The isAddressOfOperand parameter indicates that this id-expression is a
496/// direct operand of the address-of operator. This is, besides member contexts,
497/// the only place where a qualified-id naming a non-static class member may
498/// appear.
499///
John McCall60d7b3a2010-08-24 06:29:42 +0000500ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000501 // qualified-id:
502 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
503 // '::' unqualified-id
504 //
505 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000506 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000507
508 UnqualifiedId Name;
509 if (ParseUnqualifiedId(SS,
510 /*EnteringContext=*/false,
511 /*AllowDestructorName=*/false,
512 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000513 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000514 Name))
515 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000516
517 // This is only the direct operand of an & operator if it is not
518 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000519 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
520 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000521
Douglas Gregor23c94db2010-07-02 17:43:08 +0000522 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000523 isAddressOfOperand);
524
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000525}
526
Douglas Gregorae7902c2011-08-04 15:30:47 +0000527/// ParseLambdaExpression - Parse a C++0x lambda expression.
528///
529/// lambda-expression:
530/// lambda-introducer lambda-declarator[opt] compound-statement
531///
532/// lambda-introducer:
533/// '[' lambda-capture[opt] ']'
534///
535/// lambda-capture:
536/// capture-default
537/// capture-list
538/// capture-default ',' capture-list
539///
540/// capture-default:
541/// '&'
542/// '='
543///
544/// capture-list:
545/// capture
546/// capture-list ',' capture
547///
548/// capture:
549/// identifier
550/// '&' identifier
551/// 'this'
552///
553/// lambda-declarator:
554/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
555/// 'mutable'[opt] exception-specification[opt]
556/// trailing-return-type[opt]
557///
558ExprResult Parser::ParseLambdaExpression() {
559 // Parse lambda-introducer.
560 LambdaIntroducer Intro;
561
562 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
563 if (DiagID) {
564 Diag(Tok, DiagID.getValue());
565 SkipUntil(tok::r_square);
566 }
567
568 return ParseLambdaExpressionAfterIntroducer(Intro);
569}
570
571/// TryParseLambdaExpression - Use lookahead and potentially tentative
572/// parsing to determine if we are looking at a C++0x lambda expression, and parse
573/// it if we are.
574///
575/// If we are not looking at a lambda expression, returns ExprError().
576ExprResult Parser::TryParseLambdaExpression() {
577 assert(getLang().CPlusPlus0x
578 && Tok.is(tok::l_square)
579 && "Not at the start of a possible lambda expression.");
580
581 const Token Next = NextToken(), After = GetLookAheadToken(2);
582
583 // If lookahead indicates this is a lambda...
584 if (Next.is(tok::r_square) || // []
585 Next.is(tok::equal) || // [=
586 (Next.is(tok::amp) && // [&] or [&,
587 (After.is(tok::r_square) ||
588 After.is(tok::comma))) ||
589 (Next.is(tok::identifier) && // [identifier]
590 After.is(tok::r_square))) {
591 return ParseLambdaExpression();
592 }
593
594 // If lookahead indicates this is an Objective-C message...
595 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
596 return ExprError();
597 }
598
599 LambdaIntroducer Intro;
600 if (TryParseLambdaIntroducer(Intro))
601 return ExprError();
602 return ParseLambdaExpressionAfterIntroducer(Intro);
603}
604
605/// ParseLambdaExpression - Parse a lambda introducer.
606///
607/// Returns a DiagnosticID if it hit something unexpected.
608llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
609 typedef llvm::Optional<unsigned> DiagResult;
610
611 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000612 BalancedDelimiterTracker T(*this, tok::l_square);
613 T.consumeOpen();
614
615 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000616
617 bool first = true;
618
619 // Parse capture-default.
620 if (Tok.is(tok::amp) &&
621 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
622 Intro.Default = LCD_ByRef;
623 ConsumeToken();
624 first = false;
625 } else if (Tok.is(tok::equal)) {
626 Intro.Default = LCD_ByCopy;
627 ConsumeToken();
628 first = false;
629 }
630
631 while (Tok.isNot(tok::r_square)) {
632 if (!first) {
633 if (Tok.isNot(tok::comma))
634 return DiagResult(diag::err_expected_comma_or_rsquare);
635 ConsumeToken();
636 }
637
638 first = false;
639
640 // Parse capture.
641 LambdaCaptureKind Kind = LCK_ByCopy;
642 SourceLocation Loc;
643 IdentifierInfo* Id = 0;
644
645 if (Tok.is(tok::kw_this)) {
646 Kind = LCK_This;
647 Loc = ConsumeToken();
648 } else {
649 if (Tok.is(tok::amp)) {
650 Kind = LCK_ByRef;
651 ConsumeToken();
652 }
653
654 if (Tok.is(tok::identifier)) {
655 Id = Tok.getIdentifierInfo();
656 Loc = ConsumeToken();
657 } else if (Tok.is(tok::kw_this)) {
658 // FIXME: If we want to suggest a fixit here, will need to return more
659 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
660 // Clear()ed to prevent emission in case of tentative parsing?
661 return DiagResult(diag::err_this_captured_by_reference);
662 } else {
663 return DiagResult(diag::err_expected_capture);
664 }
665 }
666
667 Intro.addCapture(Kind, Loc, Id);
668 }
669
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000670 T.consumeClose();
671 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000672
673 return DiagResult();
674}
675
676/// TryParseLambdaExpression - Tentatively parse a lambda introducer.
677///
678/// Returns true if it hit something unexpected.
679bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
680 TentativeParsingAction PA(*this);
681
682 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
683
684 if (DiagID) {
685 PA.Revert();
686 return true;
687 }
688
689 PA.Commit();
690 return false;
691}
692
693/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
694/// expression.
695ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
696 LambdaIntroducer &Intro) {
Richard Smith7fe62082011-10-15 05:09:34 +0000697 Diag(Intro.Range.getBegin(), diag::warn_cxx98_compat_lambda);
698
Douglas Gregorae7902c2011-08-04 15:30:47 +0000699 // Parse lambda-declarator[opt].
700 DeclSpec DS(AttrFactory);
701 Declarator D(DS, Declarator::PrototypeContext);
702
703 if (Tok.is(tok::l_paren)) {
704 ParseScope PrototypeScope(this,
705 Scope::FunctionPrototypeScope |
706 Scope::DeclScope);
707
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000708 SourceLocation DeclLoc, DeclEndLoc;
709 BalancedDelimiterTracker T(*this, tok::l_paren);
710 T.consumeOpen();
711 DeclLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000712
713 // Parse parameter-declaration-clause.
714 ParsedAttributes Attr(AttrFactory);
715 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
716 SourceLocation EllipsisLoc;
717
718 if (Tok.isNot(tok::r_paren))
719 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
720
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000721 T.consumeClose();
722 DeclEndLoc = T.getCloseLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000723
724 // Parse 'mutable'[opt].
725 SourceLocation MutableLoc;
726 if (Tok.is(tok::kw_mutable)) {
727 MutableLoc = ConsumeToken();
728 DeclEndLoc = MutableLoc;
729 }
730
731 // Parse exception-specification[opt].
732 ExceptionSpecificationType ESpecType = EST_None;
733 SourceRange ESpecRange;
734 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
735 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
736 ExprResult NoexceptExpr;
737 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
738 DynamicExceptions,
739 DynamicExceptionRanges,
740 NoexceptExpr);
741
742 if (ESpecType != EST_None)
743 DeclEndLoc = ESpecRange.getEnd();
744
745 // Parse attribute-specifier[opt].
746 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
747
748 // Parse trailing-return-type[opt].
749 ParsedType TrailingReturnType;
750 if (Tok.is(tok::arrow)) {
751 SourceRange Range;
752 TrailingReturnType = ParseTrailingReturnType(Range).get();
753 if (Range.getEnd().isValid())
754 DeclEndLoc = Range.getEnd();
755 }
756
757 PrototypeScope.Exit();
758
759 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
760 /*isVariadic=*/EllipsisLoc.isValid(),
761 EllipsisLoc,
762 ParamInfo.data(), ParamInfo.size(),
763 DS.getTypeQualifiers(),
764 /*RefQualifierIsLValueRef=*/true,
765 /*RefQualifierLoc=*/SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +0000766 /*ConstQualifierLoc=*/SourceLocation(),
767 /*VolatileQualifierLoc=*/SourceLocation(),
Douglas Gregorae7902c2011-08-04 15:30:47 +0000768 MutableLoc,
769 ESpecType, ESpecRange.getBegin(),
770 DynamicExceptions.data(),
771 DynamicExceptionRanges.data(),
772 DynamicExceptions.size(),
773 NoexceptExpr.isUsable() ?
774 NoexceptExpr.get() : 0,
775 DeclLoc, DeclEndLoc, D,
776 TrailingReturnType),
777 Attr, DeclEndLoc);
778 }
779
780 // Parse compound-statement.
781 if (Tok.is(tok::l_brace)) {
782 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
783 // it.
784 ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
785 Scope::BreakScope | Scope::ContinueScope |
786 Scope::DeclScope);
787
788 StmtResult Stmt(ParseCompoundStatementBody());
789
790 BodyScope.Exit();
791 } else {
792 Diag(Tok, diag::err_expected_lambda_body);
793 }
794
795 return ExprEmpty();
796}
797
Reid Spencer5f016e22007-07-11 17:01:13 +0000798/// ParseCXXCasts - This handles the various ways to cast expressions to another
799/// type.
800///
801/// postfix-expression: [C++ 5.2p1]
802/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
803/// 'static_cast' '<' type-name '>' '(' expression ')'
804/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
805/// 'const_cast' '<' type-name '>' '(' expression ')'
806///
John McCall60d7b3a2010-08-24 06:29:42 +0000807ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 tok::TokenKind Kind = Tok.getKind();
809 const char *CastName = 0; // For error messages
810
811 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000812 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 case tok::kw_const_cast: CastName = "const_cast"; break;
814 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
815 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
816 case tok::kw_static_cast: CastName = "static_cast"; break;
817 }
818
819 SourceLocation OpLoc = ConsumeToken();
820 SourceLocation LAngleBracketLoc = Tok.getLocation();
821
Richard Smithea698b32011-04-14 21:45:45 +0000822 // Check for "<::" which is parsed as "[:". If found, fix token stream,
823 // diagnose error, suggest fix, and recover parsing.
824 Token Next = NextToken();
825 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
826 AreTokensAdjacent(PP, Tok, Next))
827 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
828
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000830 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000831
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000832 // Parse the common declaration-specifiers piece.
833 DeclSpec DS(AttrFactory);
834 ParseSpecifierQualifierList(DS);
835
836 // Parse the abstract-declarator, if present.
837 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
838 ParseDeclarator(DeclaratorInfo);
839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 SourceLocation RAngleBracketLoc = Tok.getLocation();
841
Chris Lattner1ab3b962008-11-18 07:48:38 +0000842 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000843 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000844
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000845 SourceLocation LParenLoc, RParenLoc;
846 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +0000847
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000848 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000849 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000850
John McCall60d7b3a2010-08-24 06:29:42 +0000851 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000853 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000854 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +0000855
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000856 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +0000857 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000858 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +0000859 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000860 T.getOpenLocation(), Result.take(),
861 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000862
Sebastian Redl20df9b72008-12-11 22:51:44 +0000863 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000864}
865
Sebastian Redlc42e1182008-11-11 11:37:55 +0000866/// ParseCXXTypeid - This handles the C++ typeid expression.
867///
868/// postfix-expression: [C++ 5.2p1]
869/// 'typeid' '(' expression ')'
870/// 'typeid' '(' type-id ')'
871///
John McCall60d7b3a2010-08-24 06:29:42 +0000872ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000873 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
874
875 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000876 SourceLocation LParenLoc, RParenLoc;
877 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000878
879 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000880 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000881 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000882 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000883
John McCall60d7b3a2010-08-24 06:29:42 +0000884 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000885
886 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000887 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000888
889 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000890 T.consumeClose();
891 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000892 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000893 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000894
895 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000896 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000897 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000898 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000899 // When typeid is applied to an expression other than an lvalue of a
900 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000901 // operand (Clause 5).
902 //
Mike Stump1eb44332009-09-09 15:08:12 +0000903 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000904 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000905 // we the expression is potentially potentially evaluated.
906 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000907 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000908 Result = ParseExpression();
909
910 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000911 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000912 SkipUntil(tok::r_paren);
913 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000914 T.consumeClose();
915 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000916 if (RParenLoc.isInvalid())
917 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000918
Sebastian Redlc42e1182008-11-11 11:37:55 +0000919 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000920 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000921 }
922 }
923
Sebastian Redl20df9b72008-12-11 22:51:44 +0000924 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000925}
926
Francois Pichet01b7c302010-09-08 12:20:18 +0000927/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
928///
929/// '__uuidof' '(' expression ')'
930/// '__uuidof' '(' type-id ')'
931///
932ExprResult Parser::ParseCXXUuidof() {
933 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
934
935 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000936 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +0000937
938 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000939 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +0000940 return ExprError();
941
942 ExprResult Result;
943
944 if (isTypeIdInParens()) {
945 TypeResult Ty = ParseTypeName();
946
947 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000948 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +0000949
950 if (Ty.isInvalid())
951 return ExprError();
952
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000953 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
954 Ty.get().getAsOpaquePtr(),
955 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +0000956 } else {
957 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
958 Result = ParseExpression();
959
960 // Match the ')'.
961 if (Result.isInvalid())
962 SkipUntil(tok::r_paren);
963 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000964 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +0000965
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000966 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
967 /*isType=*/false,
968 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +0000969 }
970 }
971
972 return move(Result);
973}
974
Douglas Gregord4dca082010-02-24 18:44:31 +0000975/// \brief Parse a C++ pseudo-destructor expression after the base,
976/// . or -> operator, and nested-name-specifier have already been
977/// parsed.
978///
979/// postfix-expression: [C++ 5.2]
980/// postfix-expression . pseudo-destructor-name
981/// postfix-expression -> pseudo-destructor-name
982///
983/// pseudo-destructor-name:
984/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
985/// ::[opt] nested-name-specifier template simple-template-id ::
986/// ~type-name
987/// ::[opt] nested-name-specifier[opt] ~type-name
988///
John McCall60d7b3a2010-08-24 06:29:42 +0000989ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000990Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
991 tok::TokenKind OpKind,
992 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000993 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000994 // We're parsing either a pseudo-destructor-name or a dependent
995 // member access that has the same form as a
996 // pseudo-destructor-name. We parse both in the same way and let
997 // the action model sort them out.
998 //
999 // Note that the ::[opt] nested-name-specifier[opt] has already
1000 // been parsed, and if there was a simple-template-id, it has
1001 // been coalesced into a template-id annotation token.
1002 UnqualifiedId FirstTypeName;
1003 SourceLocation CCLoc;
1004 if (Tok.is(tok::identifier)) {
1005 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1006 ConsumeToken();
1007 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1008 CCLoc = ConsumeToken();
1009 } else if (Tok.is(tok::annot_template_id)) {
1010 FirstTypeName.setTemplateId(
1011 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1012 ConsumeToken();
1013 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1014 CCLoc = ConsumeToken();
1015 } else {
1016 FirstTypeName.setIdentifier(0, SourceLocation());
1017 }
1018
1019 // Parse the tilde.
1020 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1021 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001022
1023 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1024 DeclSpec DS(AttrFactory);
1025 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
1026 if (DS.getTypeSpecType() == TST_error)
1027 return ExprError();
1028 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1029 OpKind, TildeLoc, DS,
1030 Tok.is(tok::l_paren));
1031 }
1032
Douglas Gregord4dca082010-02-24 18:44:31 +00001033 if (!Tok.is(tok::identifier)) {
1034 Diag(Tok, diag::err_destructor_tilde_identifier);
1035 return ExprError();
1036 }
1037
1038 // Parse the second type.
1039 UnqualifiedId SecondTypeName;
1040 IdentifierInfo *Name = Tok.getIdentifierInfo();
1041 SourceLocation NameLoc = ConsumeToken();
1042 SecondTypeName.setIdentifier(Name, NameLoc);
1043
1044 // If there is a '<', the second type name is a template-id. Parse
1045 // it as such.
1046 if (Tok.is(tok::less) &&
1047 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001048 SecondTypeName, /*AssumeTemplateName=*/true,
1049 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +00001050 return ExprError();
1051
John McCall9ae2f072010-08-23 23:25:46 +00001052 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1053 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001054 SS, FirstTypeName, CCLoc,
1055 TildeLoc, SecondTypeName,
1056 Tok.is(tok::l_paren));
1057}
1058
Reid Spencer5f016e22007-07-11 17:01:13 +00001059/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1060///
1061/// boolean-literal: [C++ 2.13.5]
1062/// 'true'
1063/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001064ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001065 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001066 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001067}
Chris Lattner50dd2892008-02-26 00:51:44 +00001068
1069/// ParseThrowExpression - This handles the C++ throw expression.
1070///
1071/// throw-expression: [C++ 15]
1072/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001073ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001074 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001075 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001076
Chris Lattner2a2819a2008-04-06 06:02:23 +00001077 // If the current token isn't the start of an assignment-expression,
1078 // then the expression is not present. This handles things like:
1079 // "C ? throw : (void)42", which is crazy but legal.
1080 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1081 case tok::semi:
1082 case tok::r_paren:
1083 case tok::r_square:
1084 case tok::r_brace:
1085 case tok::colon:
1086 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001087 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001088
Chris Lattner2a2819a2008-04-06 06:02:23 +00001089 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001090 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +00001091 if (Expr.isInvalid()) return move(Expr);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001092 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001093 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001094}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001095
1096/// ParseCXXThis - This handles the C++ 'this' pointer.
1097///
1098/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1099/// a non-lvalue expression whose value is the address of the object for which
1100/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001101ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001102 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1103 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001104 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001105}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001106
1107/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1108/// Can be interpreted either as function-style casting ("int(x)")
1109/// or class type construction ("ClassType(x,y,z)")
1110/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001111/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001112///
1113/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001114/// simple-type-specifier '(' expression-list[opt] ')'
1115/// [C++0x] simple-type-specifier braced-init-list
1116/// typename-specifier '(' expression-list[opt] ')'
1117/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001118///
John McCall60d7b3a2010-08-24 06:29:42 +00001119ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001120Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001121 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001122 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001123
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001124 assert((Tok.is(tok::l_paren) ||
1125 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1126 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001127
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001128 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001129
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001130 // FIXME: Convert to a proper type construct expression.
1131 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001132
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001133 } else {
1134 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1135
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001136 BalancedDelimiterTracker T(*this, tok::l_paren);
1137 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001138
1139 ExprVector Exprs(Actions);
1140 CommaLocsTy CommaLocs;
1141
1142 if (Tok.isNot(tok::r_paren)) {
1143 if (ParseExpressionList(Exprs, CommaLocs)) {
1144 SkipUntil(tok::r_paren);
1145 return ExprError();
1146 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001147 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001148
1149 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001150 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001151
1152 // TypeRep could be null, if it references an invalid typedef.
1153 if (!TypeRep)
1154 return ExprError();
1155
1156 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1157 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001158 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
1159 move_arg(Exprs),
1160 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001161 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001162}
1163
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001164/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001165///
1166/// condition:
1167/// expression
1168/// type-specifier-seq declarator '=' assignment-expression
1169/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1170/// '=' assignment-expression
1171///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001172/// \param ExprResult if the condition was parsed as an expression, the
1173/// parsed expression.
1174///
1175/// \param DeclResult if the condition was parsed as a declaration, the
1176/// parsed declaration.
1177///
Douglas Gregor586596f2010-05-06 17:25:47 +00001178/// \param Loc The location of the start of the statement that requires this
1179/// condition, e.g., the "for" in a for loop.
1180///
1181/// \param ConvertToBoolean Whether the condition expression should be
1182/// converted to a boolean value.
1183///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001184/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001185bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1186 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001187 SourceLocation Loc,
1188 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001189 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001190 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001191 cutOffParsing();
1192 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001193 }
1194
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001195 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001196 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001197 ExprOut = ParseExpression(); // expression
1198 DeclOut = 0;
1199 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001200 return true;
1201
1202 // If required, convert to a boolean value.
1203 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001204 ExprOut
1205 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1206 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001207 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001208
1209 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001210 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001211 ParseSpecifierQualifierList(DS);
1212
1213 // declarator
1214 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1215 ParseDeclarator(DeclaratorInfo);
1216
1217 // simple-asm-expr[opt]
1218 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001219 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001220 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001221 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001222 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001223 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001224 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001225 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001226 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001227 }
1228
1229 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001230 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001231
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001232 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001233 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001234 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001235 DeclOut = Dcl.get();
1236 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001237
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001238 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001239 if (isTokenEqualOrMistypedEqualEqual(
1240 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001241 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001242 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001243 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +00001244 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1245 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001246 } else {
1247 // FIXME: C++0x allows a braced-init-list
1248 Diag(Tok, diag::err_expected_equal_after_declarator);
1249 }
1250
Douglas Gregor586596f2010-05-06 17:25:47 +00001251 // FIXME: Build a reference to this declaration? Convert it to bool?
1252 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001253
1254 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001255
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001256 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001257}
1258
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001259/// \brief Determine whether the current token starts a C++
1260/// simple-type-specifier.
1261bool Parser::isCXXSimpleTypeSpecifier() const {
1262 switch (Tok.getKind()) {
1263 case tok::annot_typename:
1264 case tok::kw_short:
1265 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001266 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001267 case tok::kw_signed:
1268 case tok::kw_unsigned:
1269 case tok::kw_void:
1270 case tok::kw_char:
1271 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001272 case tok::kw_half:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001273 case tok::kw_float:
1274 case tok::kw_double:
1275 case tok::kw_wchar_t:
1276 case tok::kw_char16_t:
1277 case tok::kw_char32_t:
1278 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +00001279 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001280 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +00001281 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001282 return true;
1283
1284 default:
1285 break;
1286 }
1287
1288 return false;
1289}
1290
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001291/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1292/// This should only be called when the current token is known to be part of
1293/// simple-type-specifier.
1294///
1295/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001296/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001297/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1298/// char
1299/// wchar_t
1300/// bool
1301/// short
1302/// int
1303/// long
1304/// signed
1305/// unsigned
1306/// float
1307/// double
1308/// void
1309/// [GNU] typeof-specifier
1310/// [C++0x] auto [TODO]
1311///
1312/// type-name:
1313/// class-name
1314/// enum-name
1315/// typedef-name
1316///
1317void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1318 DS.SetRangeStart(Tok.getLocation());
1319 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001320 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001321 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001323 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001324 case tok::identifier: // foo::bar
1325 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001326 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001327 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001328 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001329
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001330 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001331 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001332 if (getTypeAnnotation(Tok))
1333 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1334 getTypeAnnotation(Tok));
1335 else
1336 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001337
1338 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1339 ConsumeToken();
1340
1341 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1342 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1343 // Objective-C interface. If we don't have Objective-C or a '<', this is
1344 // just a normal reference to a typedef name.
1345 if (Tok.is(tok::less) && getLang().ObjC1)
1346 ParseObjCProtocolQualifiers(DS);
1347
1348 DS.Finish(Diags, PP);
1349 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001350 }
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001352 // builtin types
1353 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001354 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001355 break;
1356 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001357 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001358 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001359 case tok::kw___int64:
1360 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1361 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001362 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001363 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001364 break;
1365 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001366 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001367 break;
1368 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001369 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001370 break;
1371 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001372 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001373 break;
1374 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001375 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001376 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001377 case tok::kw_half:
1378 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1379 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001380 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001381 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001382 break;
1383 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001384 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001385 break;
1386 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001387 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001388 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001389 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001390 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001391 break;
1392 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001393 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001394 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001395 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001396 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001397 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001399 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001400 // GNU typeof support.
1401 case tok::kw_typeof:
1402 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001403 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001404 return;
1405 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001406 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001407 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1408 else
1409 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001410 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001411 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001412}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001413
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001414/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1415/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1416/// e.g., "const short int". Note that the DeclSpec is *not* finished
1417/// by parsing the type-specifier-seq, because these sequences are
1418/// typically followed by some form of declarator. Returns true and
1419/// emits diagnostics if this is not a type-specifier-seq, false
1420/// otherwise.
1421///
1422/// type-specifier-seq: [C++ 8.1]
1423/// type-specifier type-specifier-seq[opt]
1424///
1425bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1426 DS.SetRangeStart(Tok.getLocation());
1427 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001428 unsigned DiagID;
1429 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001430
1431 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001432 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1433 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001434 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001435 return true;
1436 }
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Sebastian Redld9bafa72010-02-03 21:21:43 +00001438 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1439 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1440 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001441
Douglas Gregor396a9f22010-02-24 23:13:13 +00001442 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001443 return false;
1444}
1445
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001446/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1447/// some form.
1448///
1449/// This routine is invoked when a '<' is encountered after an identifier or
1450/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1451/// whether the unqualified-id is actually a template-id. This routine will
1452/// then parse the template arguments and form the appropriate template-id to
1453/// return to the caller.
1454///
1455/// \param SS the nested-name-specifier that precedes this template-id, if
1456/// we're actually parsing a qualified-id.
1457///
1458/// \param Name for constructor and destructor names, this is the actual
1459/// identifier that may be a template-name.
1460///
1461/// \param NameLoc the location of the class-name in a constructor or
1462/// destructor.
1463///
1464/// \param EnteringContext whether we're entering the scope of the
1465/// nested-name-specifier.
1466///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001467/// \param ObjectType if this unqualified-id occurs within a member access
1468/// expression, the type of the base object whose member is being accessed.
1469///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001470/// \param Id as input, describes the template-name or operator-function-id
1471/// that precedes the '<'. If template arguments were parsed successfully,
1472/// will be updated with the template-id.
1473///
Douglas Gregord4dca082010-02-24 18:44:31 +00001474/// \param AssumeTemplateId When true, this routine will assume that the name
1475/// refers to a template without performing name lookup to verify.
1476///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001477/// \returns true if a parse error occurred, false otherwise.
1478bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1479 IdentifierInfo *Name,
1480 SourceLocation NameLoc,
1481 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001482 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001483 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001484 bool AssumeTemplateId,
1485 SourceLocation TemplateKWLoc) {
1486 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1487 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001488
1489 TemplateTy Template;
1490 TemplateNameKind TNK = TNK_Non_template;
1491 switch (Id.getKind()) {
1492 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001493 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001494 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001495 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001496 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001497 Id, ObjectType, EnteringContext,
1498 Template);
1499 if (TNK == TNK_Non_template)
1500 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001501 } else {
1502 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001503 TNK = Actions.isTemplateName(getCurScope(), SS,
1504 TemplateKWLoc.isValid(), Id,
1505 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001506 MemberOfUnknownSpecialization);
1507
1508 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1509 ObjectType && IsTemplateArgumentList()) {
1510 // We have something like t->getAs<T>(), where getAs is a
1511 // member of an unknown specialization. However, this will only
1512 // parse correctly as a template, so suggest the keyword 'template'
1513 // before 'getAs' and treat this as a dependent template name.
1514 std::string Name;
1515 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1516 Name = Id.Identifier->getName();
1517 else {
1518 Name = "operator ";
1519 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1520 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1521 else
1522 Name += Id.Identifier->getName();
1523 }
1524 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1525 << Name
1526 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001527 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001528 SS, Id, ObjectType,
1529 EnteringContext, Template);
1530 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001531 return true;
1532 }
1533 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001534 break;
1535
Douglas Gregor014e88d2009-11-03 23:16:33 +00001536 case UnqualifiedId::IK_ConstructorName: {
1537 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001538 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001539 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001540 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1541 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001542 EnteringContext, Template,
1543 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001544 break;
1545 }
1546
Douglas Gregor014e88d2009-11-03 23:16:33 +00001547 case UnqualifiedId::IK_DestructorName: {
1548 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001549 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001550 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001551 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001552 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001553 TemplateName, ObjectType,
1554 EnteringContext, Template);
1555 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001556 return true;
1557 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001558 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1559 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001560 EnteringContext, Template,
1561 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001562
John McCallb3d87482010-08-24 05:47:05 +00001563 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001564 Diag(NameLoc, diag::err_destructor_template_id)
1565 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001566 return true;
1567 }
1568 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001569 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001570 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001571
1572 default:
1573 return false;
1574 }
1575
1576 if (TNK == TNK_Non_template)
1577 return false;
1578
1579 // Parse the enclosed template argument list.
1580 SourceLocation LAngleLoc, RAngleLoc;
1581 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001582 if (Tok.is(tok::less) &&
1583 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001584 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001585 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001586 RAngleLoc))
1587 return true;
1588
1589 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001590 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1591 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001592 // Form a parsed representation of the template-id to be stored in the
1593 // UnqualifiedId.
1594 TemplateIdAnnotation *TemplateId
1595 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1596
1597 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1598 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001599 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001600 TemplateId->TemplateNameLoc = Id.StartLocation;
1601 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001602 TemplateId->Name = 0;
1603 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1604 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001605 }
1606
Douglas Gregor059101f2011-03-02 00:47:37 +00001607 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001608 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001609 TemplateId->Kind = TNK;
1610 TemplateId->LAngleLoc = LAngleLoc;
1611 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001612 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001613 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001614 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001615 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001616
1617 Id.setTemplateId(TemplateId);
1618 return false;
1619 }
1620
1621 // Bundle the template arguments together.
1622 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001623 TemplateArgs.size());
1624
1625 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001626 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001627 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001628 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001629 RAngleLoc);
1630 if (Type.isInvalid())
1631 return true;
1632
1633 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1634 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1635 else
1636 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1637
1638 return false;
1639}
1640
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001641/// \brief Parse an operator-function-id or conversion-function-id as part
1642/// of a C++ unqualified-id.
1643///
1644/// This routine is responsible only for parsing the operator-function-id or
1645/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001646///
1647/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001648/// operator-function-id: [C++ 13.5]
1649/// 'operator' operator
1650///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001651/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001652/// new delete new[] delete[]
1653/// + - * / % ^ & | ~
1654/// ! = < > += -= *= /= %=
1655/// ^= &= |= << >> >>= <<= == !=
1656/// <= >= && || ++ -- , ->* ->
1657/// () []
1658///
1659/// conversion-function-id: [C++ 12.3.2]
1660/// operator conversion-type-id
1661///
1662/// conversion-type-id:
1663/// type-specifier-seq conversion-declarator[opt]
1664///
1665/// conversion-declarator:
1666/// ptr-operator conversion-declarator[opt]
1667/// \endcode
1668///
1669/// \param The nested-name-specifier that preceded this unqualified-id. If
1670/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1671///
1672/// \param EnteringContext whether we are entering the scope of the
1673/// nested-name-specifier.
1674///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001675/// \param ObjectType if this unqualified-id occurs within a member access
1676/// expression, the type of the base object whose member is being accessed.
1677///
1678/// \param Result on a successful parse, contains the parsed unqualified-id.
1679///
1680/// \returns true if parsing fails, false otherwise.
1681bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001682 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001683 UnqualifiedId &Result) {
1684 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1685
1686 // Consume the 'operator' keyword.
1687 SourceLocation KeywordLoc = ConsumeToken();
1688
1689 // Determine what kind of operator name we have.
1690 unsigned SymbolIdx = 0;
1691 SourceLocation SymbolLocations[3];
1692 OverloadedOperatorKind Op = OO_None;
1693 switch (Tok.getKind()) {
1694 case tok::kw_new:
1695 case tok::kw_delete: {
1696 bool isNew = Tok.getKind() == tok::kw_new;
1697 // Consume the 'new' or 'delete'.
1698 SymbolLocations[SymbolIdx++] = ConsumeToken();
1699 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001700 // Consume the '[' and ']'.
1701 BalancedDelimiterTracker T(*this, tok::l_square);
1702 T.consumeOpen();
1703 T.consumeClose();
1704 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001705 return true;
1706
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001707 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1708 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001709 Op = isNew? OO_Array_New : OO_Array_Delete;
1710 } else {
1711 Op = isNew? OO_New : OO_Delete;
1712 }
1713 break;
1714 }
1715
1716#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1717 case tok::Token: \
1718 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1719 Op = OO_##Name; \
1720 break;
1721#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1722#include "clang/Basic/OperatorKinds.def"
1723
1724 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001725 // Consume the '(' and ')'.
1726 BalancedDelimiterTracker T(*this, tok::l_paren);
1727 T.consumeOpen();
1728 T.consumeClose();
1729 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001730 return true;
1731
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001732 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1733 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001734 Op = OO_Call;
1735 break;
1736 }
1737
1738 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001739 // Consume the '[' and ']'.
1740 BalancedDelimiterTracker T(*this, tok::l_square);
1741 T.consumeOpen();
1742 T.consumeClose();
1743 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001744 return true;
1745
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001746 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1747 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001748 Op = OO_Subscript;
1749 break;
1750 }
1751
1752 case tok::code_completion: {
1753 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001754 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001755 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001756 // Don't try to parse any further.
1757 return true;
1758 }
1759
1760 default:
1761 break;
1762 }
1763
1764 if (Op != OO_None) {
1765 // We have parsed an operator-function-id.
1766 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1767 return false;
1768 }
Sean Hunt0486d742009-11-28 04:44:28 +00001769
1770 // Parse a literal-operator-id.
1771 //
1772 // literal-operator-id: [C++0x 13.5.8]
1773 // operator "" identifier
1774
1775 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
Richard Smith7fe62082011-10-15 05:09:34 +00001776 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001777 if (Tok.getLength() != 2)
1778 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1779 ConsumeStringToken();
1780
1781 if (Tok.isNot(tok::identifier)) {
1782 Diag(Tok.getLocation(), diag::err_expected_ident);
1783 return true;
1784 }
1785
1786 IdentifierInfo *II = Tok.getIdentifierInfo();
1787 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001788 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001789 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001790
1791 // Parse a conversion-function-id.
1792 //
1793 // conversion-function-id: [C++ 12.3.2]
1794 // operator conversion-type-id
1795 //
1796 // conversion-type-id:
1797 // type-specifier-seq conversion-declarator[opt]
1798 //
1799 // conversion-declarator:
1800 // ptr-operator conversion-declarator[opt]
1801
1802 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001803 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001804 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001805 return true;
1806
1807 // Parse the conversion-declarator, which is merely a sequence of
1808 // ptr-operators.
1809 Declarator D(DS, Declarator::TypeNameContext);
1810 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1811
1812 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001813 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001814 if (Ty.isInvalid())
1815 return true;
1816
1817 // Note that this is a conversion-function-id.
1818 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1819 D.getSourceRange().getEnd());
1820 return false;
1821}
1822
1823/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1824/// name of an entity.
1825///
1826/// \code
1827/// unqualified-id: [C++ expr.prim.general]
1828/// identifier
1829/// operator-function-id
1830/// conversion-function-id
1831/// [C++0x] literal-operator-id [TODO]
1832/// ~ class-name
1833/// template-id
1834///
1835/// \endcode
1836///
1837/// \param The nested-name-specifier that preceded this unqualified-id. If
1838/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1839///
1840/// \param EnteringContext whether we are entering the scope of the
1841/// nested-name-specifier.
1842///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001843/// \param AllowDestructorName whether we allow parsing of a destructor name.
1844///
1845/// \param AllowConstructorName whether we allow parsing a constructor name.
1846///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001847/// \param ObjectType if this unqualified-id occurs within a member access
1848/// expression, the type of the base object whose member is being accessed.
1849///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001850/// \param Result on a successful parse, contains the parsed unqualified-id.
1851///
1852/// \returns true if parsing fails, false otherwise.
1853bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1854 bool AllowDestructorName,
1855 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001856 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001857 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001858
1859 // Handle 'A::template B'. This is for template-ids which have not
1860 // already been annotated by ParseOptionalCXXScopeSpecifier().
1861 bool TemplateSpecified = false;
1862 SourceLocation TemplateKWLoc;
1863 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1864 (ObjectType || SS.isSet())) {
1865 TemplateSpecified = true;
1866 TemplateKWLoc = ConsumeToken();
1867 }
1868
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001869 // unqualified-id:
1870 // identifier
1871 // template-id (when it hasn't already been annotated)
1872 if (Tok.is(tok::identifier)) {
1873 // Consume the identifier.
1874 IdentifierInfo *Id = Tok.getIdentifierInfo();
1875 SourceLocation IdLoc = ConsumeToken();
1876
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001877 if (!getLang().CPlusPlus) {
1878 // If we're not in C++, only identifiers matter. Record the
1879 // identifier and return.
1880 Result.setIdentifier(Id, IdLoc);
1881 return false;
1882 }
1883
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001884 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001885 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001886 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001887 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001888 &SS, false, false,
1889 ParsedType(),
1890 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001891 IdLoc, IdLoc);
1892 } else {
1893 // We have parsed an identifier.
1894 Result.setIdentifier(Id, IdLoc);
1895 }
1896
1897 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001898 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001899 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001900 ObjectType, Result,
1901 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001902
1903 return false;
1904 }
1905
1906 // unqualified-id:
1907 // template-id (already parsed and annotated)
1908 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001909 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001910
1911 // If the template-name names the current class, then this is a constructor
1912 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001913 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001914 if (SS.isSet()) {
1915 // C++ [class.qual]p2 specifies that a qualified template-name
1916 // is taken as the constructor name where a constructor can be
1917 // declared. Thus, the template arguments are extraneous, so
1918 // complain about them and remove them entirely.
1919 Diag(TemplateId->TemplateNameLoc,
1920 diag::err_out_of_line_constructor_template_id)
1921 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001922 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001923 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1924 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1925 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001926 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001927 &SS, false, false,
1928 ParsedType(),
1929 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001930 TemplateId->TemplateNameLoc,
1931 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001932 ConsumeToken();
1933 return false;
1934 }
1935
1936 Result.setConstructorTemplateId(TemplateId);
1937 ConsumeToken();
1938 return false;
1939 }
1940
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001941 // We have already parsed a template-id; consume the annotation token as
1942 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001943 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001944 ConsumeToken();
1945 return false;
1946 }
1947
1948 // unqualified-id:
1949 // operator-function-id
1950 // conversion-function-id
1951 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001952 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001953 return true;
1954
Sean Hunte6252d12009-11-28 08:58:14 +00001955 // If we have an operator-function-id or a literal-operator-id and the next
1956 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001957 //
1958 // template-id:
1959 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001960 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1961 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001962 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001963 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1964 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001965 Result,
1966 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001967
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001968 return false;
1969 }
1970
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001971 if (getLang().CPlusPlus &&
1972 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001973 // C++ [expr.unary.op]p10:
1974 // There is an ambiguity in the unary-expression ~X(), where X is a
1975 // class-name. The ambiguity is resolved in favor of treating ~ as a
1976 // unary complement rather than treating ~X as referring to a destructor.
1977
1978 // Parse the '~'.
1979 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00001980
1981 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
1982 DeclSpec DS(AttrFactory);
1983 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
1984 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
1985 Result.setDestructorName(TildeLoc, Type, EndLoc);
1986 return false;
1987 }
1988 return true;
1989 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001990
1991 // Parse the class-name.
1992 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001993 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001994 return true;
1995 }
1996
1997 // Parse the class-name (or template-name in a simple-template-id).
1998 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1999 SourceLocation ClassNameLoc = ConsumeToken();
2000
Douglas Gregor0278e122010-05-05 05:58:24 +00002001 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002002 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002003 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00002004 EnteringContext, ObjectType, Result,
2005 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002006 }
2007
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002008 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002009 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2010 ClassNameLoc, getCurScope(),
2011 SS, ObjectType,
2012 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002013 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002014 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002015
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002016 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002017 return false;
2018 }
2019
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002020 Diag(Tok, diag::err_expected_unqualified_id)
2021 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002022 return true;
2023}
2024
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002025/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2026/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002027///
Chris Lattner59232d32009-01-04 21:25:24 +00002028/// This method is called to parse the new expression after the optional :: has
2029/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2030/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002031///
2032/// new-expression:
2033/// '::'[opt] 'new' new-placement[opt] new-type-id
2034/// new-initializer[opt]
2035/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2036/// new-initializer[opt]
2037///
2038/// new-placement:
2039/// '(' expression-list ')'
2040///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002041/// new-type-id:
2042/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002043/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002044///
2045/// new-declarator:
2046/// ptr-operator new-declarator[opt]
2047/// direct-new-declarator
2048///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002049/// new-initializer:
2050/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002051/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002052///
John McCall60d7b3a2010-08-24 06:29:42 +00002053ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002054Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2055 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2056 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002057
2058 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2059 // second form of new-expression. It can't be a new-type-id.
2060
Sebastian Redla55e52c2008-11-25 22:21:31 +00002061 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002062 SourceLocation PlacementLParen, PlacementRParen;
2063
Douglas Gregor4bd40312010-07-13 15:54:32 +00002064 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002065 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002066 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002067 if (Tok.is(tok::l_paren)) {
2068 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002069 BalancedDelimiterTracker T(*this, tok::l_paren);
2070 T.consumeOpen();
2071 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002072 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2073 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002074 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002075 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002076
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002077 T.consumeClose();
2078 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002079 if (PlacementRParen.isInvalid()) {
2080 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002081 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002082 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002083
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002084 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002085 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002086 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002087 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002088 } else {
2089 // We still need the type.
2090 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002091 BalancedDelimiterTracker T(*this, tok::l_paren);
2092 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002093 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002094 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002095 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002096 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002097 T.consumeClose();
2098 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002099 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002100 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002101 if (ParseCXXTypeSpecifierSeq(DS))
2102 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002103 else {
2104 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002105 ParseDeclaratorInternal(DeclaratorInfo,
2106 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002107 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002108 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002109 }
2110 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002111 // A new-type-id is a simplified type-id, where essentially the
2112 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002113 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002114 if (ParseCXXTypeSpecifierSeq(DS))
2115 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002116 else {
2117 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002118 ParseDeclaratorInternal(DeclaratorInfo,
2119 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002120 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002121 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002122 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002123 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002124 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002125 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002126
Sebastian Redla55e52c2008-11-25 22:21:31 +00002127 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002128 SourceLocation ConstructorLParen, ConstructorRParen;
2129
2130 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002131 BalancedDelimiterTracker T(*this, tok::l_paren);
2132 T.consumeOpen();
2133 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002134 if (Tok.isNot(tok::r_paren)) {
2135 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002136 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2137 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002138 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002139 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002140 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002141 T.consumeClose();
2142 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002143 if (ConstructorRParen.isInvalid()) {
2144 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002145 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002146 }
Richard Smith29e3a312011-10-15 03:38:41 +00002147 } else if (Tok.is(tok::l_brace) && getLang().CPlusPlus0x) {
Richard Smith7fe62082011-10-15 05:09:34 +00002148 Diag(Tok.getLocation(),
2149 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002150 // FIXME: Have to communicate the init-list to ActOnCXXNew.
2151 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002152 }
2153
Sebastian Redlf53597f2009-03-15 17:47:39 +00002154 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2155 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00002156 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002157 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002158}
2159
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002160/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2161/// passed to ParseDeclaratorInternal.
2162///
2163/// direct-new-declarator:
2164/// '[' expression ']'
2165/// direct-new-declarator '[' constant-expression ']'
2166///
Chris Lattner59232d32009-01-04 21:25:24 +00002167void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002168 // Parse the array dimensions.
2169 bool first = true;
2170 while (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002171 BalancedDelimiterTracker T(*this, tok::l_square);
2172 T.consumeOpen();
2173
John McCall60d7b3a2010-08-24 06:29:42 +00002174 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002175 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002176 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002177 // Recover
2178 SkipUntil(tok::r_square);
2179 return;
2180 }
2181 first = false;
2182
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002183 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002184
2185 ParsedAttributes attrs(AttrFactory);
2186 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002187 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002188 Size.release(),
2189 T.getOpenLocation(),
2190 T.getCloseLocation()),
2191 attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002192
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002193 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002194 return;
2195 }
2196}
2197
2198/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2199/// This ambiguity appears in the syntax of the C++ new operator.
2200///
2201/// new-expression:
2202/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2203/// new-initializer[opt]
2204///
2205/// new-placement:
2206/// '(' expression-list ')'
2207///
John McCallca0408f2010-08-23 06:44:23 +00002208bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002209 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002210 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002211 // The '(' was already consumed.
2212 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002213 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002214 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002215 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002216 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002217 }
2218
2219 // It's not a type, it has to be an expression list.
2220 // Discard the comma locations - ActOnCXXNew has enough parameters.
2221 CommaLocsTy CommaLocs;
2222 return ParseExpressionList(PlacementArgs, CommaLocs);
2223}
2224
2225/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2226/// to free memory allocated by new.
2227///
Chris Lattner59232d32009-01-04 21:25:24 +00002228/// This method is called to parse the 'delete' expression after the optional
2229/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2230/// and "Start" is its location. Otherwise, "Start" is the location of the
2231/// 'delete' token.
2232///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002233/// delete-expression:
2234/// '::'[opt] 'delete' cast-expression
2235/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002236ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002237Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2238 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2239 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002240
2241 // Array delete?
2242 bool ArrayDelete = false;
2243 if (Tok.is(tok::l_square)) {
2244 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002245 BalancedDelimiterTracker T(*this, tok::l_square);
2246
2247 T.consumeOpen();
2248 T.consumeClose();
2249 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002250 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002251 }
2252
John McCall60d7b3a2010-08-24 06:29:42 +00002253 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002254 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002255 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002256
John McCall9ae2f072010-08-23 23:25:46 +00002257 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002258}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002259
Mike Stump1eb44332009-09-09 15:08:12 +00002260static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002261 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002262 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002263 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002264 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002265 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002266 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002267 case tok::kw___has_trivial_constructor:
2268 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002269 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002270 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2271 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2272 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002273 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2274 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002275 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002276 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2277 case tok::kw___is_compound: return UTT_IsCompound;
2278 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002279 case tok::kw___is_empty: return UTT_IsEmpty;
2280 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002281 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002282 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2283 case tok::kw___is_function: return UTT_IsFunction;
2284 case tok::kw___is_fundamental: return UTT_IsFundamental;
2285 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00002286 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2287 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2288 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2289 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2290 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002291 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002292 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002293 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002294 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002295 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002296 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002297 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2298 case tok::kw___is_scalar: return UTT_IsScalar;
2299 case tok::kw___is_signed: return UTT_IsSigned;
2300 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2301 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002302 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002303 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002304 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2305 case tok::kw___is_void: return UTT_IsVoid;
2306 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002307 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002308}
2309
2310static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2311 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002312 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002313 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002314 case tok::kw___is_convertible: return BTT_IsConvertible;
2315 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002316 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002317 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002318 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002319}
2320
John Wiegley21ff2e52011-04-28 00:16:57 +00002321static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2322 switch(kind) {
2323 default: llvm_unreachable("Not a known binary type trait");
2324 case tok::kw___array_rank: return ATT_ArrayRank;
2325 case tok::kw___array_extent: return ATT_ArrayExtent;
2326 }
2327}
2328
John Wiegley55262202011-04-25 06:54:41 +00002329static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2330 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002331 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002332 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2333 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2334 }
2335}
2336
Sebastian Redl64b45f72009-01-05 20:52:13 +00002337/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2338/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2339/// templates.
2340///
2341/// primary-expression:
2342/// [GNU] unary-type-trait '(' type-id ')'
2343///
John McCall60d7b3a2010-08-24 06:29:42 +00002344ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002345 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2346 SourceLocation Loc = ConsumeToken();
2347
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002348 BalancedDelimiterTracker T(*this, tok::l_paren);
2349 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002350 return ExprError();
2351
2352 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2353 // there will be cryptic errors about mismatched parentheses and missing
2354 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002355 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002356
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002357 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002358
Douglas Gregor809070a2009-02-18 17:45:20 +00002359 if (Ty.isInvalid())
2360 return ExprError();
2361
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002362 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002363}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002364
Francois Pichet6ad6f282010-12-07 00:08:36 +00002365/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2366/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2367/// templates.
2368///
2369/// primary-expression:
2370/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2371///
2372ExprResult Parser::ParseBinaryTypeTrait() {
2373 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2374 SourceLocation Loc = ConsumeToken();
2375
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002376 BalancedDelimiterTracker T(*this, tok::l_paren);
2377 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002378 return ExprError();
2379
2380 TypeResult LhsTy = ParseTypeName();
2381 if (LhsTy.isInvalid()) {
2382 SkipUntil(tok::r_paren);
2383 return ExprError();
2384 }
2385
2386 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2387 SkipUntil(tok::r_paren);
2388 return ExprError();
2389 }
2390
2391 TypeResult RhsTy = ParseTypeName();
2392 if (RhsTy.isInvalid()) {
2393 SkipUntil(tok::r_paren);
2394 return ExprError();
2395 }
2396
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002397 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002398
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002399 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2400 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002401}
2402
John Wiegley21ff2e52011-04-28 00:16:57 +00002403/// ParseArrayTypeTrait - Parse the built-in array type-trait
2404/// pseudo-functions.
2405///
2406/// primary-expression:
2407/// [Embarcadero] '__array_rank' '(' type-id ')'
2408/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2409///
2410ExprResult Parser::ParseArrayTypeTrait() {
2411 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2412 SourceLocation Loc = ConsumeToken();
2413
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002414 BalancedDelimiterTracker T(*this, tok::l_paren);
2415 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002416 return ExprError();
2417
2418 TypeResult Ty = ParseTypeName();
2419 if (Ty.isInvalid()) {
2420 SkipUntil(tok::comma);
2421 SkipUntil(tok::r_paren);
2422 return ExprError();
2423 }
2424
2425 switch (ATT) {
2426 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002427 T.consumeClose();
2428 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2429 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002430 }
2431 case ATT_ArrayExtent: {
2432 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2433 SkipUntil(tok::r_paren);
2434 return ExprError();
2435 }
2436
2437 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002438 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002439
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002440 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2441 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002442 }
2443 default:
2444 break;
2445 }
2446 return ExprError();
2447}
2448
John Wiegley55262202011-04-25 06:54:41 +00002449/// ParseExpressionTrait - Parse built-in expression-trait
2450/// pseudo-functions like __is_lvalue_expr( xxx ).
2451///
2452/// primary-expression:
2453/// [Embarcadero] expression-trait '(' expression ')'
2454///
2455ExprResult Parser::ParseExpressionTrait() {
2456 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2457 SourceLocation Loc = ConsumeToken();
2458
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002459 BalancedDelimiterTracker T(*this, tok::l_paren);
2460 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002461 return ExprError();
2462
2463 ExprResult Expr = ParseExpression();
2464
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002465 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002466
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002467 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2468 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002469}
2470
2471
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002472/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2473/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2474/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002475ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002476Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002477 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002478 BalancedDelimiterTracker &Tracker) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002479 assert(getLang().CPlusPlus && "Should only be called for C++!");
2480 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2481 assert(isTypeIdInParens() && "Not a type-id!");
2482
John McCall60d7b3a2010-08-24 06:29:42 +00002483 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002484 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002485
2486 // We need to disambiguate a very ugly part of the C++ syntax:
2487 //
2488 // (T())x; - type-id
2489 // (T())*x; - type-id
2490 // (T())/x; - expression
2491 // (T()); - expression
2492 //
2493 // The bad news is that we cannot use the specialized tentative parser, since
2494 // it can only verify that the thing inside the parens can be parsed as
2495 // type-id, it is not useful for determining the context past the parens.
2496 //
2497 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002498 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002499 //
2500 // It uses a scheme similar to parsing inline methods. The parenthesized
2501 // tokens are cached, the context that follows is determined (possibly by
2502 // parsing a cast-expression), and then we re-introduce the cached tokens
2503 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002504
Mike Stump1eb44332009-09-09 15:08:12 +00002505 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002506 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002507
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002508 // Store the tokens of the parentheses. We will parse them after we determine
2509 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002510 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002511 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002512 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002513 return ExprError();
2514 }
2515
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002516 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002517 ParseAs = CompoundLiteral;
2518 } else {
2519 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002520 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2521 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2522 NotCastExpr = true;
2523 } else {
2524 // Try parsing the cast-expression that may follow.
2525 // If it is not a cast-expression, NotCastExpr will be true and no token
2526 // will be consumed.
2527 Result = ParseCastExpression(false/*isUnaryExpression*/,
2528 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002529 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002530 // type-id has priority.
2531 true/*isTypeCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002532 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002533
2534 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2535 // an expression.
2536 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002537 }
2538
Mike Stump1eb44332009-09-09 15:08:12 +00002539 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002540 Toks.push_back(Tok);
2541 // Re-enter the stored parenthesized tokens into the token stream, so we may
2542 // parse them now.
2543 PP.EnterTokenStream(Toks.data(), Toks.size(),
2544 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2545 // Drop the current token and bring the first cached one. It's the same token
2546 // as when we entered this function.
2547 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002548
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002549 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002550 // Parse the type declarator.
2551 DeclSpec DS(AttrFactory);
2552 ParseSpecifierQualifierList(DS);
2553 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2554 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002555
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002556 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002557 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002558
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002559 if (ParseAs == CompoundLiteral) {
2560 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002561 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002562 return ParseCompoundLiteralExpression(Ty.get(),
2563 Tracker.getOpenLocation(),
2564 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002565 }
Mike Stump1eb44332009-09-09 15:08:12 +00002566
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002567 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2568 assert(ParseAs == CastExpr);
2569
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002570 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002571 return ExprError();
2572
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002573 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002574 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002575 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2576 DeclaratorInfo, CastTy,
2577 Tracker.getCloseLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002578 return move(Result);
2579 }
Mike Stump1eb44332009-09-09 15:08:12 +00002580
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002581 // Not a compound literal, and not followed by a cast-expression.
2582 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002583
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002584 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002585 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002586 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002587 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2588 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002589
2590 // Match the ')'.
2591 if (Result.isInvalid()) {
2592 SkipUntil(tok::r_paren);
2593 return ExprError();
2594 }
Mike Stump1eb44332009-09-09 15:08:12 +00002595
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002596 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002597 return move(Result);
2598}