blob: 79d2ec21ab09cd8b3a9e224b21cb0dd6bf443464 [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();
1022 if (!Tok.is(tok::identifier)) {
1023 Diag(Tok, diag::err_destructor_tilde_identifier);
1024 return ExprError();
1025 }
1026
1027 // Parse the second type.
1028 UnqualifiedId SecondTypeName;
1029 IdentifierInfo *Name = Tok.getIdentifierInfo();
1030 SourceLocation NameLoc = ConsumeToken();
1031 SecondTypeName.setIdentifier(Name, NameLoc);
1032
1033 // If there is a '<', the second type name is a template-id. Parse
1034 // it as such.
1035 if (Tok.is(tok::less) &&
1036 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001037 SecondTypeName, /*AssumeTemplateName=*/true,
1038 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +00001039 return ExprError();
1040
John McCall9ae2f072010-08-23 23:25:46 +00001041 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1042 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001043 SS, FirstTypeName, CCLoc,
1044 TildeLoc, SecondTypeName,
1045 Tok.is(tok::l_paren));
1046}
1047
Reid Spencer5f016e22007-07-11 17:01:13 +00001048/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1049///
1050/// boolean-literal: [C++ 2.13.5]
1051/// 'true'
1052/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001053ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001055 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001056}
Chris Lattner50dd2892008-02-26 00:51:44 +00001057
1058/// ParseThrowExpression - This handles the C++ throw expression.
1059///
1060/// throw-expression: [C++ 15]
1061/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001062ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001063 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001064 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001065
Chris Lattner2a2819a2008-04-06 06:02:23 +00001066 // If the current token isn't the start of an assignment-expression,
1067 // then the expression is not present. This handles things like:
1068 // "C ? throw : (void)42", which is crazy but legal.
1069 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1070 case tok::semi:
1071 case tok::r_paren:
1072 case tok::r_square:
1073 case tok::r_brace:
1074 case tok::colon:
1075 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001076 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001077
Chris Lattner2a2819a2008-04-06 06:02:23 +00001078 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001079 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +00001080 if (Expr.isInvalid()) return move(Expr);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001081 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001082 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001083}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001084
1085/// ParseCXXThis - This handles the C++ 'this' pointer.
1086///
1087/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1088/// a non-lvalue expression whose value is the address of the object for which
1089/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001090ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001091 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1092 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001093 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001094}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001095
1096/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1097/// Can be interpreted either as function-style casting ("int(x)")
1098/// or class type construction ("ClassType(x,y,z)")
1099/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001100/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001101///
1102/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001103/// simple-type-specifier '(' expression-list[opt] ')'
1104/// [C++0x] simple-type-specifier braced-init-list
1105/// typename-specifier '(' expression-list[opt] ')'
1106/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001107///
John McCall60d7b3a2010-08-24 06:29:42 +00001108ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001109Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001110 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001111 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001112
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001113 assert((Tok.is(tok::l_paren) ||
1114 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1115 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001116
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001117 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001118
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001119 // FIXME: Convert to a proper type construct expression.
1120 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001121
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001122 } else {
1123 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1124
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001125 BalancedDelimiterTracker T(*this, tok::l_paren);
1126 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001127
1128 ExprVector Exprs(Actions);
1129 CommaLocsTy CommaLocs;
1130
1131 if (Tok.isNot(tok::r_paren)) {
1132 if (ParseExpressionList(Exprs, CommaLocs)) {
1133 SkipUntil(tok::r_paren);
1134 return ExprError();
1135 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001136 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001137
1138 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001139 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001140
1141 // TypeRep could be null, if it references an invalid typedef.
1142 if (!TypeRep)
1143 return ExprError();
1144
1145 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1146 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001147 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
1148 move_arg(Exprs),
1149 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001150 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001151}
1152
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001153/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001154///
1155/// condition:
1156/// expression
1157/// type-specifier-seq declarator '=' assignment-expression
1158/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1159/// '=' assignment-expression
1160///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001161/// \param ExprResult if the condition was parsed as an expression, the
1162/// parsed expression.
1163///
1164/// \param DeclResult if the condition was parsed as a declaration, the
1165/// parsed declaration.
1166///
Douglas Gregor586596f2010-05-06 17:25:47 +00001167/// \param Loc The location of the start of the statement that requires this
1168/// condition, e.g., the "for" in a for loop.
1169///
1170/// \param ConvertToBoolean Whether the condition expression should be
1171/// converted to a boolean value.
1172///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001173/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001174bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1175 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001176 SourceLocation Loc,
1177 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001178 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001179 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001180 cutOffParsing();
1181 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001182 }
1183
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001184 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001185 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001186 ExprOut = ParseExpression(); // expression
1187 DeclOut = 0;
1188 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001189 return true;
1190
1191 // If required, convert to a boolean value.
1192 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001193 ExprOut
1194 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1195 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001196 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001197
1198 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001199 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001200 ParseSpecifierQualifierList(DS);
1201
1202 // declarator
1203 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1204 ParseDeclarator(DeclaratorInfo);
1205
1206 // simple-asm-expr[opt]
1207 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001208 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001209 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001210 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001211 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001212 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001213 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001214 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001215 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001216 }
1217
1218 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001219 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001220
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001221 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001222 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001223 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001224 DeclOut = Dcl.get();
1225 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001226
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001227 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001228 if (isTokenEqualOrMistypedEqualEqual(
1229 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001230 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001231 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001232 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +00001233 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1234 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001235 } else {
1236 // FIXME: C++0x allows a braced-init-list
1237 Diag(Tok, diag::err_expected_equal_after_declarator);
1238 }
1239
Douglas Gregor586596f2010-05-06 17:25:47 +00001240 // FIXME: Build a reference to this declaration? Convert it to bool?
1241 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001242
1243 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001244
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001245 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001246}
1247
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001248/// \brief Determine whether the current token starts a C++
1249/// simple-type-specifier.
1250bool Parser::isCXXSimpleTypeSpecifier() const {
1251 switch (Tok.getKind()) {
1252 case tok::annot_typename:
1253 case tok::kw_short:
1254 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001255 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001256 case tok::kw_signed:
1257 case tok::kw_unsigned:
1258 case tok::kw_void:
1259 case tok::kw_char:
1260 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001261 case tok::kw_half:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001262 case tok::kw_float:
1263 case tok::kw_double:
1264 case tok::kw_wchar_t:
1265 case tok::kw_char16_t:
1266 case tok::kw_char32_t:
1267 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +00001268 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001269 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +00001270 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001271 return true;
1272
1273 default:
1274 break;
1275 }
1276
1277 return false;
1278}
1279
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001280/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1281/// This should only be called when the current token is known to be part of
1282/// simple-type-specifier.
1283///
1284/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001285/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001286/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1287/// char
1288/// wchar_t
1289/// bool
1290/// short
1291/// int
1292/// long
1293/// signed
1294/// unsigned
1295/// float
1296/// double
1297/// void
1298/// [GNU] typeof-specifier
1299/// [C++0x] auto [TODO]
1300///
1301/// type-name:
1302/// class-name
1303/// enum-name
1304/// typedef-name
1305///
1306void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1307 DS.SetRangeStart(Tok.getLocation());
1308 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001309 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001310 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001312 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001313 case tok::identifier: // foo::bar
1314 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001315 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001316 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001317 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001318
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001319 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001320 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001321 if (getTypeAnnotation(Tok))
1322 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1323 getTypeAnnotation(Tok));
1324 else
1325 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001326
1327 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1328 ConsumeToken();
1329
1330 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1331 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1332 // Objective-C interface. If we don't have Objective-C or a '<', this is
1333 // just a normal reference to a typedef name.
1334 if (Tok.is(tok::less) && getLang().ObjC1)
1335 ParseObjCProtocolQualifiers(DS);
1336
1337 DS.Finish(Diags, PP);
1338 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001339 }
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001341 // builtin types
1342 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001343 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001344 break;
1345 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001346 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001347 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001348 case tok::kw___int64:
1349 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1350 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001351 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001352 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001353 break;
1354 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001355 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001356 break;
1357 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001358 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001359 break;
1360 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001361 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001362 break;
1363 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001364 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001365 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001366 case tok::kw_half:
1367 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1368 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001369 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001370 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001371 break;
1372 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001373 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001374 break;
1375 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001376 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001377 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001378 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001379 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001380 break;
1381 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001382 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001383 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001384 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001385 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001386 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001388 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001389 // GNU typeof support.
1390 case tok::kw_typeof:
1391 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001392 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001393 return;
1394 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001395 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001396 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1397 else
1398 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001399 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001400 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001401}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001402
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001403/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1404/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1405/// e.g., "const short int". Note that the DeclSpec is *not* finished
1406/// by parsing the type-specifier-seq, because these sequences are
1407/// typically followed by some form of declarator. Returns true and
1408/// emits diagnostics if this is not a type-specifier-seq, false
1409/// otherwise.
1410///
1411/// type-specifier-seq: [C++ 8.1]
1412/// type-specifier type-specifier-seq[opt]
1413///
1414bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1415 DS.SetRangeStart(Tok.getLocation());
1416 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001417 unsigned DiagID;
1418 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001419
1420 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001421 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1422 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001423 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001424 return true;
1425 }
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Sebastian Redld9bafa72010-02-03 21:21:43 +00001427 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1428 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1429 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001430
Douglas Gregor396a9f22010-02-24 23:13:13 +00001431 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001432 return false;
1433}
1434
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001435/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1436/// some form.
1437///
1438/// This routine is invoked when a '<' is encountered after an identifier or
1439/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1440/// whether the unqualified-id is actually a template-id. This routine will
1441/// then parse the template arguments and form the appropriate template-id to
1442/// return to the caller.
1443///
1444/// \param SS the nested-name-specifier that precedes this template-id, if
1445/// we're actually parsing a qualified-id.
1446///
1447/// \param Name for constructor and destructor names, this is the actual
1448/// identifier that may be a template-name.
1449///
1450/// \param NameLoc the location of the class-name in a constructor or
1451/// destructor.
1452///
1453/// \param EnteringContext whether we're entering the scope of the
1454/// nested-name-specifier.
1455///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001456/// \param ObjectType if this unqualified-id occurs within a member access
1457/// expression, the type of the base object whose member is being accessed.
1458///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001459/// \param Id as input, describes the template-name or operator-function-id
1460/// that precedes the '<'. If template arguments were parsed successfully,
1461/// will be updated with the template-id.
1462///
Douglas Gregord4dca082010-02-24 18:44:31 +00001463/// \param AssumeTemplateId When true, this routine will assume that the name
1464/// refers to a template without performing name lookup to verify.
1465///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001466/// \returns true if a parse error occurred, false otherwise.
1467bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1468 IdentifierInfo *Name,
1469 SourceLocation NameLoc,
1470 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001471 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001472 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001473 bool AssumeTemplateId,
1474 SourceLocation TemplateKWLoc) {
1475 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1476 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001477
1478 TemplateTy Template;
1479 TemplateNameKind TNK = TNK_Non_template;
1480 switch (Id.getKind()) {
1481 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001482 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001483 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001484 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001485 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001486 Id, ObjectType, EnteringContext,
1487 Template);
1488 if (TNK == TNK_Non_template)
1489 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001490 } else {
1491 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001492 TNK = Actions.isTemplateName(getCurScope(), SS,
1493 TemplateKWLoc.isValid(), Id,
1494 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001495 MemberOfUnknownSpecialization);
1496
1497 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1498 ObjectType && IsTemplateArgumentList()) {
1499 // We have something like t->getAs<T>(), where getAs is a
1500 // member of an unknown specialization. However, this will only
1501 // parse correctly as a template, so suggest the keyword 'template'
1502 // before 'getAs' and treat this as a dependent template name.
1503 std::string Name;
1504 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1505 Name = Id.Identifier->getName();
1506 else {
1507 Name = "operator ";
1508 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1509 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1510 else
1511 Name += Id.Identifier->getName();
1512 }
1513 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1514 << Name
1515 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001516 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001517 SS, Id, ObjectType,
1518 EnteringContext, Template);
1519 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001520 return true;
1521 }
1522 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001523 break;
1524
Douglas Gregor014e88d2009-11-03 23:16:33 +00001525 case UnqualifiedId::IK_ConstructorName: {
1526 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001527 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001528 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001529 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1530 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001531 EnteringContext, Template,
1532 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001533 break;
1534 }
1535
Douglas Gregor014e88d2009-11-03 23:16:33 +00001536 case UnqualifiedId::IK_DestructorName: {
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);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001540 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001541 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001542 TemplateName, ObjectType,
1543 EnteringContext, Template);
1544 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001545 return true;
1546 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001547 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1548 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001549 EnteringContext, Template,
1550 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001551
John McCallb3d87482010-08-24 05:47:05 +00001552 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001553 Diag(NameLoc, diag::err_destructor_template_id)
1554 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001555 return true;
1556 }
1557 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001558 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001559 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001560
1561 default:
1562 return false;
1563 }
1564
1565 if (TNK == TNK_Non_template)
1566 return false;
1567
1568 // Parse the enclosed template argument list.
1569 SourceLocation LAngleLoc, RAngleLoc;
1570 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001571 if (Tok.is(tok::less) &&
1572 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001573 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001574 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001575 RAngleLoc))
1576 return true;
1577
1578 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001579 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1580 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001581 // Form a parsed representation of the template-id to be stored in the
1582 // UnqualifiedId.
1583 TemplateIdAnnotation *TemplateId
1584 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1585
1586 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1587 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001588 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001589 TemplateId->TemplateNameLoc = Id.StartLocation;
1590 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001591 TemplateId->Name = 0;
1592 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1593 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001594 }
1595
Douglas Gregor059101f2011-03-02 00:47:37 +00001596 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001597 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001598 TemplateId->Kind = TNK;
1599 TemplateId->LAngleLoc = LAngleLoc;
1600 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001601 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001602 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001603 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001604 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001605
1606 Id.setTemplateId(TemplateId);
1607 return false;
1608 }
1609
1610 // Bundle the template arguments together.
1611 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001612 TemplateArgs.size());
1613
1614 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001615 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001616 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001617 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001618 RAngleLoc);
1619 if (Type.isInvalid())
1620 return true;
1621
1622 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1623 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1624 else
1625 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1626
1627 return false;
1628}
1629
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001630/// \brief Parse an operator-function-id or conversion-function-id as part
1631/// of a C++ unqualified-id.
1632///
1633/// This routine is responsible only for parsing the operator-function-id or
1634/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001635///
1636/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001637/// operator-function-id: [C++ 13.5]
1638/// 'operator' operator
1639///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001640/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001641/// new delete new[] delete[]
1642/// + - * / % ^ & | ~
1643/// ! = < > += -= *= /= %=
1644/// ^= &= |= << >> >>= <<= == !=
1645/// <= >= && || ++ -- , ->* ->
1646/// () []
1647///
1648/// conversion-function-id: [C++ 12.3.2]
1649/// operator conversion-type-id
1650///
1651/// conversion-type-id:
1652/// type-specifier-seq conversion-declarator[opt]
1653///
1654/// conversion-declarator:
1655/// ptr-operator conversion-declarator[opt]
1656/// \endcode
1657///
1658/// \param The nested-name-specifier that preceded this unqualified-id. If
1659/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1660///
1661/// \param EnteringContext whether we are entering the scope of the
1662/// nested-name-specifier.
1663///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001664/// \param ObjectType if this unqualified-id occurs within a member access
1665/// expression, the type of the base object whose member is being accessed.
1666///
1667/// \param Result on a successful parse, contains the parsed unqualified-id.
1668///
1669/// \returns true if parsing fails, false otherwise.
1670bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001671 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001672 UnqualifiedId &Result) {
1673 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1674
1675 // Consume the 'operator' keyword.
1676 SourceLocation KeywordLoc = ConsumeToken();
1677
1678 // Determine what kind of operator name we have.
1679 unsigned SymbolIdx = 0;
1680 SourceLocation SymbolLocations[3];
1681 OverloadedOperatorKind Op = OO_None;
1682 switch (Tok.getKind()) {
1683 case tok::kw_new:
1684 case tok::kw_delete: {
1685 bool isNew = Tok.getKind() == tok::kw_new;
1686 // Consume the 'new' or 'delete'.
1687 SymbolLocations[SymbolIdx++] = ConsumeToken();
1688 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001689 // Consume the '[' and ']'.
1690 BalancedDelimiterTracker T(*this, tok::l_square);
1691 T.consumeOpen();
1692 T.consumeClose();
1693 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001694 return true;
1695
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001696 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1697 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001698 Op = isNew? OO_Array_New : OO_Array_Delete;
1699 } else {
1700 Op = isNew? OO_New : OO_Delete;
1701 }
1702 break;
1703 }
1704
1705#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1706 case tok::Token: \
1707 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1708 Op = OO_##Name; \
1709 break;
1710#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1711#include "clang/Basic/OperatorKinds.def"
1712
1713 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001714 // Consume the '(' and ')'.
1715 BalancedDelimiterTracker T(*this, tok::l_paren);
1716 T.consumeOpen();
1717 T.consumeClose();
1718 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001719 return true;
1720
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001721 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1722 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001723 Op = OO_Call;
1724 break;
1725 }
1726
1727 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001728 // Consume the '[' and ']'.
1729 BalancedDelimiterTracker T(*this, tok::l_square);
1730 T.consumeOpen();
1731 T.consumeClose();
1732 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001733 return true;
1734
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001735 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1736 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001737 Op = OO_Subscript;
1738 break;
1739 }
1740
1741 case tok::code_completion: {
1742 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001743 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001744 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001745 // Don't try to parse any further.
1746 return true;
1747 }
1748
1749 default:
1750 break;
1751 }
1752
1753 if (Op != OO_None) {
1754 // We have parsed an operator-function-id.
1755 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1756 return false;
1757 }
Sean Hunt0486d742009-11-28 04:44:28 +00001758
1759 // Parse a literal-operator-id.
1760 //
1761 // literal-operator-id: [C++0x 13.5.8]
1762 // operator "" identifier
1763
1764 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
Richard Smith7fe62082011-10-15 05:09:34 +00001765 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001766 if (Tok.getLength() != 2)
1767 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1768 ConsumeStringToken();
1769
1770 if (Tok.isNot(tok::identifier)) {
1771 Diag(Tok.getLocation(), diag::err_expected_ident);
1772 return true;
1773 }
1774
1775 IdentifierInfo *II = Tok.getIdentifierInfo();
1776 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001777 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001778 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001779
1780 // Parse a conversion-function-id.
1781 //
1782 // conversion-function-id: [C++ 12.3.2]
1783 // operator conversion-type-id
1784 //
1785 // conversion-type-id:
1786 // type-specifier-seq conversion-declarator[opt]
1787 //
1788 // conversion-declarator:
1789 // ptr-operator conversion-declarator[opt]
1790
1791 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001792 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001793 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001794 return true;
1795
1796 // Parse the conversion-declarator, which is merely a sequence of
1797 // ptr-operators.
1798 Declarator D(DS, Declarator::TypeNameContext);
1799 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1800
1801 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001802 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001803 if (Ty.isInvalid())
1804 return true;
1805
1806 // Note that this is a conversion-function-id.
1807 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1808 D.getSourceRange().getEnd());
1809 return false;
1810}
1811
1812/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1813/// name of an entity.
1814///
1815/// \code
1816/// unqualified-id: [C++ expr.prim.general]
1817/// identifier
1818/// operator-function-id
1819/// conversion-function-id
1820/// [C++0x] literal-operator-id [TODO]
1821/// ~ class-name
1822/// template-id
1823///
1824/// \endcode
1825///
1826/// \param The nested-name-specifier that preceded this unqualified-id. If
1827/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1828///
1829/// \param EnteringContext whether we are entering the scope of the
1830/// nested-name-specifier.
1831///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001832/// \param AllowDestructorName whether we allow parsing of a destructor name.
1833///
1834/// \param AllowConstructorName whether we allow parsing a constructor name.
1835///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001836/// \param ObjectType if this unqualified-id occurs within a member access
1837/// expression, the type of the base object whose member is being accessed.
1838///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001839/// \param Result on a successful parse, contains the parsed unqualified-id.
1840///
1841/// \returns true if parsing fails, false otherwise.
1842bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1843 bool AllowDestructorName,
1844 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001845 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001846 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001847
1848 // Handle 'A::template B'. This is for template-ids which have not
1849 // already been annotated by ParseOptionalCXXScopeSpecifier().
1850 bool TemplateSpecified = false;
1851 SourceLocation TemplateKWLoc;
1852 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1853 (ObjectType || SS.isSet())) {
1854 TemplateSpecified = true;
1855 TemplateKWLoc = ConsumeToken();
1856 }
1857
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001858 // unqualified-id:
1859 // identifier
1860 // template-id (when it hasn't already been annotated)
1861 if (Tok.is(tok::identifier)) {
1862 // Consume the identifier.
1863 IdentifierInfo *Id = Tok.getIdentifierInfo();
1864 SourceLocation IdLoc = ConsumeToken();
1865
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001866 if (!getLang().CPlusPlus) {
1867 // If we're not in C++, only identifiers matter. Record the
1868 // identifier and return.
1869 Result.setIdentifier(Id, IdLoc);
1870 return false;
1871 }
1872
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001873 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001874 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001875 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001876 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001877 &SS, false, false,
1878 ParsedType(),
1879 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001880 IdLoc, IdLoc);
1881 } else {
1882 // We have parsed an identifier.
1883 Result.setIdentifier(Id, IdLoc);
1884 }
1885
1886 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001887 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001888 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001889 ObjectType, Result,
1890 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001891
1892 return false;
1893 }
1894
1895 // unqualified-id:
1896 // template-id (already parsed and annotated)
1897 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001898 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001899
1900 // If the template-name names the current class, then this is a constructor
1901 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001902 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001903 if (SS.isSet()) {
1904 // C++ [class.qual]p2 specifies that a qualified template-name
1905 // is taken as the constructor name where a constructor can be
1906 // declared. Thus, the template arguments are extraneous, so
1907 // complain about them and remove them entirely.
1908 Diag(TemplateId->TemplateNameLoc,
1909 diag::err_out_of_line_constructor_template_id)
1910 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001911 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001912 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1913 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1914 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001915 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001916 &SS, false, false,
1917 ParsedType(),
1918 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001919 TemplateId->TemplateNameLoc,
1920 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001921 ConsumeToken();
1922 return false;
1923 }
1924
1925 Result.setConstructorTemplateId(TemplateId);
1926 ConsumeToken();
1927 return false;
1928 }
1929
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001930 // We have already parsed a template-id; consume the annotation token as
1931 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001932 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001933 ConsumeToken();
1934 return false;
1935 }
1936
1937 // unqualified-id:
1938 // operator-function-id
1939 // conversion-function-id
1940 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001941 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001942 return true;
1943
Sean Hunte6252d12009-11-28 08:58:14 +00001944 // If we have an operator-function-id or a literal-operator-id and the next
1945 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001946 //
1947 // template-id:
1948 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001949 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1950 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001951 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001952 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1953 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001954 Result,
1955 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001956
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001957 return false;
1958 }
1959
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001960 if (getLang().CPlusPlus &&
1961 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001962 // C++ [expr.unary.op]p10:
1963 // There is an ambiguity in the unary-expression ~X(), where X is a
1964 // class-name. The ambiguity is resolved in favor of treating ~ as a
1965 // unary complement rather than treating ~X as referring to a destructor.
1966
1967 // Parse the '~'.
1968 SourceLocation TildeLoc = ConsumeToken();
1969
1970 // Parse the class-name.
1971 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001972 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001973 return true;
1974 }
1975
1976 // Parse the class-name (or template-name in a simple-template-id).
1977 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1978 SourceLocation ClassNameLoc = ConsumeToken();
1979
Douglas Gregor0278e122010-05-05 05:58:24 +00001980 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001981 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001982 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001983 EnteringContext, ObjectType, Result,
1984 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001985 }
1986
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001987 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001988 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1989 ClassNameLoc, getCurScope(),
1990 SS, ObjectType,
1991 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001992 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001993 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001994
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001995 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001996 return false;
1997 }
1998
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001999 Diag(Tok, diag::err_expected_unqualified_id)
2000 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002001 return true;
2002}
2003
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002004/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2005/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002006///
Chris Lattner59232d32009-01-04 21:25:24 +00002007/// This method is called to parse the new expression after the optional :: has
2008/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2009/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002010///
2011/// new-expression:
2012/// '::'[opt] 'new' new-placement[opt] new-type-id
2013/// new-initializer[opt]
2014/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2015/// new-initializer[opt]
2016///
2017/// new-placement:
2018/// '(' expression-list ')'
2019///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002020/// new-type-id:
2021/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002022/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002023///
2024/// new-declarator:
2025/// ptr-operator new-declarator[opt]
2026/// direct-new-declarator
2027///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002028/// new-initializer:
2029/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002030/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002031///
John McCall60d7b3a2010-08-24 06:29:42 +00002032ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002033Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2034 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2035 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002036
2037 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2038 // second form of new-expression. It can't be a new-type-id.
2039
Sebastian Redla55e52c2008-11-25 22:21:31 +00002040 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002041 SourceLocation PlacementLParen, PlacementRParen;
2042
Douglas Gregor4bd40312010-07-13 15:54:32 +00002043 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002044 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002045 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002046 if (Tok.is(tok::l_paren)) {
2047 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002048 BalancedDelimiterTracker T(*this, tok::l_paren);
2049 T.consumeOpen();
2050 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002051 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2052 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002053 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002054 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002055
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002056 T.consumeClose();
2057 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002058 if (PlacementRParen.isInvalid()) {
2059 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002060 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002061 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002062
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002063 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002064 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002065 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002066 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002067 } else {
2068 // We still need the type.
2069 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002070 BalancedDelimiterTracker T(*this, tok::l_paren);
2071 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002072 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002073 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002074 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002075 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002076 T.consumeClose();
2077 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002078 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002079 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002080 if (ParseCXXTypeSpecifierSeq(DS))
2081 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002082 else {
2083 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002084 ParseDeclaratorInternal(DeclaratorInfo,
2085 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002086 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002087 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002088 }
2089 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002090 // A new-type-id is a simplified type-id, where essentially the
2091 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002092 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002093 if (ParseCXXTypeSpecifierSeq(DS))
2094 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002095 else {
2096 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002097 ParseDeclaratorInternal(DeclaratorInfo,
2098 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002099 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002100 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002101 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002102 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002103 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002104 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002105
Sebastian Redla55e52c2008-11-25 22:21:31 +00002106 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002107 SourceLocation ConstructorLParen, ConstructorRParen;
2108
2109 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002110 BalancedDelimiterTracker T(*this, tok::l_paren);
2111 T.consumeOpen();
2112 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002113 if (Tok.isNot(tok::r_paren)) {
2114 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002115 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2116 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002117 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002118 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002119 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002120 T.consumeClose();
2121 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002122 if (ConstructorRParen.isInvalid()) {
2123 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 }
Richard Smith29e3a312011-10-15 03:38:41 +00002126 } else if (Tok.is(tok::l_brace) && getLang().CPlusPlus0x) {
Richard Smith7fe62082011-10-15 05:09:34 +00002127 Diag(Tok.getLocation(),
2128 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002129 // FIXME: Have to communicate the init-list to ActOnCXXNew.
2130 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002131 }
2132
Sebastian Redlf53597f2009-03-15 17:47:39 +00002133 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2134 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00002135 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002136 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002137}
2138
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002139/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2140/// passed to ParseDeclaratorInternal.
2141///
2142/// direct-new-declarator:
2143/// '[' expression ']'
2144/// direct-new-declarator '[' constant-expression ']'
2145///
Chris Lattner59232d32009-01-04 21:25:24 +00002146void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002147 // Parse the array dimensions.
2148 bool first = true;
2149 while (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002150 BalancedDelimiterTracker T(*this, tok::l_square);
2151 T.consumeOpen();
2152
John McCall60d7b3a2010-08-24 06:29:42 +00002153 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002154 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002155 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002156 // Recover
2157 SkipUntil(tok::r_square);
2158 return;
2159 }
2160 first = false;
2161
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002162 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002163
2164 ParsedAttributes attrs(AttrFactory);
2165 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002166 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002167 Size.release(),
2168 T.getOpenLocation(),
2169 T.getCloseLocation()),
2170 attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002171
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002172 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002173 return;
2174 }
2175}
2176
2177/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2178/// This ambiguity appears in the syntax of the C++ new operator.
2179///
2180/// new-expression:
2181/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2182/// new-initializer[opt]
2183///
2184/// new-placement:
2185/// '(' expression-list ')'
2186///
John McCallca0408f2010-08-23 06:44:23 +00002187bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002188 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002189 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002190 // The '(' was already consumed.
2191 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002192 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002193 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002194 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002195 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002196 }
2197
2198 // It's not a type, it has to be an expression list.
2199 // Discard the comma locations - ActOnCXXNew has enough parameters.
2200 CommaLocsTy CommaLocs;
2201 return ParseExpressionList(PlacementArgs, CommaLocs);
2202}
2203
2204/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2205/// to free memory allocated by new.
2206///
Chris Lattner59232d32009-01-04 21:25:24 +00002207/// This method is called to parse the 'delete' expression after the optional
2208/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2209/// and "Start" is its location. Otherwise, "Start" is the location of the
2210/// 'delete' token.
2211///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002212/// delete-expression:
2213/// '::'[opt] 'delete' cast-expression
2214/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002215ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002216Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2217 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2218 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002219
2220 // Array delete?
2221 bool ArrayDelete = false;
2222 if (Tok.is(tok::l_square)) {
2223 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002224 BalancedDelimiterTracker T(*this, tok::l_square);
2225
2226 T.consumeOpen();
2227 T.consumeClose();
2228 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002229 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002230 }
2231
John McCall60d7b3a2010-08-24 06:29:42 +00002232 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002233 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002234 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002235
John McCall9ae2f072010-08-23 23:25:46 +00002236 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002237}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002238
Mike Stump1eb44332009-09-09 15:08:12 +00002239static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002240 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002241 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002242 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002243 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002244 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002245 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002246 case tok::kw___has_trivial_constructor:
2247 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002248 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002249 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2250 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2251 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002252 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2253 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002254 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002255 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2256 case tok::kw___is_compound: return UTT_IsCompound;
2257 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002258 case tok::kw___is_empty: return UTT_IsEmpty;
2259 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002260 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002261 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2262 case tok::kw___is_function: return UTT_IsFunction;
2263 case tok::kw___is_fundamental: return UTT_IsFundamental;
2264 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00002265 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2266 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2267 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2268 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2269 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002270 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002271 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002272 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002273 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002274 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002275 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002276 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2277 case tok::kw___is_scalar: return UTT_IsScalar;
2278 case tok::kw___is_signed: return UTT_IsSigned;
2279 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2280 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002281 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002282 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002283 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2284 case tok::kw___is_void: return UTT_IsVoid;
2285 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002286 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002287}
2288
2289static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2290 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002291 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002292 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002293 case tok::kw___is_convertible: return BTT_IsConvertible;
2294 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002295 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002296 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002297 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002298}
2299
John Wiegley21ff2e52011-04-28 00:16:57 +00002300static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2301 switch(kind) {
2302 default: llvm_unreachable("Not a known binary type trait");
2303 case tok::kw___array_rank: return ATT_ArrayRank;
2304 case tok::kw___array_extent: return ATT_ArrayExtent;
2305 }
2306}
2307
John Wiegley55262202011-04-25 06:54:41 +00002308static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2309 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002310 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002311 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2312 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2313 }
2314}
2315
Sebastian Redl64b45f72009-01-05 20:52:13 +00002316/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2317/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2318/// templates.
2319///
2320/// primary-expression:
2321/// [GNU] unary-type-trait '(' type-id ')'
2322///
John McCall60d7b3a2010-08-24 06:29:42 +00002323ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002324 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2325 SourceLocation Loc = ConsumeToken();
2326
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002327 BalancedDelimiterTracker T(*this, tok::l_paren);
2328 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002329 return ExprError();
2330
2331 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2332 // there will be cryptic errors about mismatched parentheses and missing
2333 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002334 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002335
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002336 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002337
Douglas Gregor809070a2009-02-18 17:45:20 +00002338 if (Ty.isInvalid())
2339 return ExprError();
2340
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002341 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002342}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002343
Francois Pichet6ad6f282010-12-07 00:08:36 +00002344/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2345/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2346/// templates.
2347///
2348/// primary-expression:
2349/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2350///
2351ExprResult Parser::ParseBinaryTypeTrait() {
2352 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2353 SourceLocation Loc = ConsumeToken();
2354
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002355 BalancedDelimiterTracker T(*this, tok::l_paren);
2356 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002357 return ExprError();
2358
2359 TypeResult LhsTy = ParseTypeName();
2360 if (LhsTy.isInvalid()) {
2361 SkipUntil(tok::r_paren);
2362 return ExprError();
2363 }
2364
2365 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2366 SkipUntil(tok::r_paren);
2367 return ExprError();
2368 }
2369
2370 TypeResult RhsTy = ParseTypeName();
2371 if (RhsTy.isInvalid()) {
2372 SkipUntil(tok::r_paren);
2373 return ExprError();
2374 }
2375
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002376 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002377
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002378 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2379 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002380}
2381
John Wiegley21ff2e52011-04-28 00:16:57 +00002382/// ParseArrayTypeTrait - Parse the built-in array type-trait
2383/// pseudo-functions.
2384///
2385/// primary-expression:
2386/// [Embarcadero] '__array_rank' '(' type-id ')'
2387/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2388///
2389ExprResult Parser::ParseArrayTypeTrait() {
2390 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2391 SourceLocation Loc = ConsumeToken();
2392
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002393 BalancedDelimiterTracker T(*this, tok::l_paren);
2394 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002395 return ExprError();
2396
2397 TypeResult Ty = ParseTypeName();
2398 if (Ty.isInvalid()) {
2399 SkipUntil(tok::comma);
2400 SkipUntil(tok::r_paren);
2401 return ExprError();
2402 }
2403
2404 switch (ATT) {
2405 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002406 T.consumeClose();
2407 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2408 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002409 }
2410 case ATT_ArrayExtent: {
2411 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2412 SkipUntil(tok::r_paren);
2413 return ExprError();
2414 }
2415
2416 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002417 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002418
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002419 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2420 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002421 }
2422 default:
2423 break;
2424 }
2425 return ExprError();
2426}
2427
John Wiegley55262202011-04-25 06:54:41 +00002428/// ParseExpressionTrait - Parse built-in expression-trait
2429/// pseudo-functions like __is_lvalue_expr( xxx ).
2430///
2431/// primary-expression:
2432/// [Embarcadero] expression-trait '(' expression ')'
2433///
2434ExprResult Parser::ParseExpressionTrait() {
2435 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2436 SourceLocation Loc = ConsumeToken();
2437
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002438 BalancedDelimiterTracker T(*this, tok::l_paren);
2439 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002440 return ExprError();
2441
2442 ExprResult Expr = ParseExpression();
2443
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002444 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002445
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002446 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2447 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002448}
2449
2450
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002451/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2452/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2453/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002454ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002455Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002456 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002457 BalancedDelimiterTracker &Tracker) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002458 assert(getLang().CPlusPlus && "Should only be called for C++!");
2459 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2460 assert(isTypeIdInParens() && "Not a type-id!");
2461
John McCall60d7b3a2010-08-24 06:29:42 +00002462 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002463 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002464
2465 // We need to disambiguate a very ugly part of the C++ syntax:
2466 //
2467 // (T())x; - type-id
2468 // (T())*x; - type-id
2469 // (T())/x; - expression
2470 // (T()); - expression
2471 //
2472 // The bad news is that we cannot use the specialized tentative parser, since
2473 // it can only verify that the thing inside the parens can be parsed as
2474 // type-id, it is not useful for determining the context past the parens.
2475 //
2476 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002477 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002478 //
2479 // It uses a scheme similar to parsing inline methods. The parenthesized
2480 // tokens are cached, the context that follows is determined (possibly by
2481 // parsing a cast-expression), and then we re-introduce the cached tokens
2482 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002483
Mike Stump1eb44332009-09-09 15:08:12 +00002484 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002485 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002486
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002487 // Store the tokens of the parentheses. We will parse them after we determine
2488 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002489 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002490 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002491 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002492 return ExprError();
2493 }
2494
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002495 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002496 ParseAs = CompoundLiteral;
2497 } else {
2498 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002499 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2500 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2501 NotCastExpr = true;
2502 } else {
2503 // Try parsing the cast-expression that may follow.
2504 // If it is not a cast-expression, NotCastExpr will be true and no token
2505 // will be consumed.
2506 Result = ParseCastExpression(false/*isUnaryExpression*/,
2507 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002508 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002509 // type-id has priority.
2510 true/*isTypeCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002511 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002512
2513 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2514 // an expression.
2515 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002516 }
2517
Mike Stump1eb44332009-09-09 15:08:12 +00002518 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002519 Toks.push_back(Tok);
2520 // Re-enter the stored parenthesized tokens into the token stream, so we may
2521 // parse them now.
2522 PP.EnterTokenStream(Toks.data(), Toks.size(),
2523 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2524 // Drop the current token and bring the first cached one. It's the same token
2525 // as when we entered this function.
2526 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002527
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002528 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002529 // Parse the type declarator.
2530 DeclSpec DS(AttrFactory);
2531 ParseSpecifierQualifierList(DS);
2532 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2533 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002534
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002535 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002536 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002537
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002538 if (ParseAs == CompoundLiteral) {
2539 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002540 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002541 return ParseCompoundLiteralExpression(Ty.get(),
2542 Tracker.getOpenLocation(),
2543 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002544 }
Mike Stump1eb44332009-09-09 15:08:12 +00002545
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002546 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2547 assert(ParseAs == CastExpr);
2548
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002549 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002550 return ExprError();
2551
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002552 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002553 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002554 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2555 DeclaratorInfo, CastTy,
2556 Tracker.getCloseLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002557 return move(Result);
2558 }
Mike Stump1eb44332009-09-09 15:08:12 +00002559
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002560 // Not a compound literal, and not followed by a cast-expression.
2561 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002562
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002563 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002564 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002565 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002566 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2567 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002568
2569 // Match the ')'.
2570 if (Result.isInvalid()) {
2571 SkipUntil(tok::r_paren);
2572 return ExprError();
2573 }
Mike Stump1eb44332009-09-09 15:08:12 +00002574
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002575 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002576 return move(Result);
2577}