blob: 88abce9c5e2fe45e744c2f18800a084ad30f9b35 [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:
32 assert(0 && "Unknown type for digraph error message.");
33 return -1;
34 }
35}
36
37// Are the two tokens adjacent in the same source file?
38static bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) {
39 SourceManager &SM = PP.getSourceManager();
40 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
41 SourceLocation FirstEnd = FirstLoc.getFileLocWithOffset(First.getLength());
42 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
43}
44
45// Suggest fixit for "<::" after a cast.
46static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
47 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
48 // Pull '<:' and ':' off token stream.
49 if (!AtDigraph)
50 PP.Lex(DigraphToken);
51 PP.Lex(ColonToken);
52
53 SourceRange Range;
54 Range.setBegin(DigraphToken.getLocation());
55 Range.setEnd(ColonToken.getLocation());
56 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
57 << SelectDigraphErrorMessage(Kind)
58 << FixItHint::CreateReplacement(Range, "< ::");
59
60 // Update token information to reflect their change in token type.
61 ColonToken.setKind(tok::coloncolon);
62 ColonToken.setLocation(ColonToken.getLocation().getFileLocWithOffset(-1));
63 ColonToken.setLength(2);
64 DigraphToken.setKind(tok::less);
65 DigraphToken.setLength(1);
66
67 // Push new tokens back to token stream.
68 PP.EnterToken(ColonToken);
69 if (!AtDigraph)
70 PP.EnterToken(DigraphToken);
71}
72
Richard Trieu950be712011-09-19 19:01:00 +000073// Check for '<::' which should be '< ::' instead of '[:' when following
74// a template name.
75void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
76 bool EnteringContext,
77 IdentifierInfo &II, CXXScopeSpec &SS) {
78 if (!Next.is(tok::l_square) || !Next.getLength() == 2)
79 return;
80
81 Token SecondToken = GetLookAheadToken(2);
82 if (!SecondToken.is(tok::colon) || !AreTokensAdjacent(PP, Next, SecondToken))
83 return;
84
85 TemplateTy Template;
86 UnqualifiedId TemplateName;
87 TemplateName.setIdentifier(&II, Tok.getLocation());
88 bool MemberOfUnknownSpecialization;
89 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
90 TemplateName, ObjectType, EnteringContext,
91 Template, MemberOfUnknownSpecialization))
92 return;
93
94 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
95 /*AtDigraph*/false);
96}
97
Mike Stump1eb44332009-09-09 15:08:12 +000098/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000099///
100/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000101/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000102/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000103///
104/// '::'[opt] nested-name-specifier
105/// '::'
106///
107/// nested-name-specifier:
108/// type-name '::'
109/// namespace-name '::'
110/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000111/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000112///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000113///
Mike Stump1eb44332009-09-09 15:08:12 +0000114/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000115/// nested-name-specifier (or empty)
116///
Mike Stump1eb44332009-09-09 15:08:12 +0000117/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000118/// the "." or "->" of a member access expression, this parameter provides the
119/// type of the object whose members are being accessed.
120///
121/// \param EnteringContext whether we will be entering into the context of
122/// the nested-name-specifier after parsing it.
123///
Douglas Gregord4dca082010-02-24 18:44:31 +0000124/// \param MayBePseudoDestructor When non-NULL, points to a flag that
125/// indicates whether this nested-name-specifier may be part of a
126/// pseudo-destructor name. In this case, the flag will be set false
127/// if we don't actually end up parsing a destructor name. Moreorover,
128/// if we do end up determining that we are parsing a destructor name,
129/// the last component of the nested-name-specifier is not parsed as
130/// part of the scope specifier.
131
Douglas Gregorb10cd042010-02-21 18:36:56 +0000132/// member access expression, e.g., the \p T:: in \p p->T::m.
133///
John McCall9ba61662010-02-26 08:45:28 +0000134/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000135bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000136 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000137 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000138 bool *MayBePseudoDestructor,
139 bool IsTypename) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000140 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000141 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000143 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +0000144 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
145 Tok.getAnnotationRange(),
146 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000147 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000148 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000149 }
Chris Lattnere607e802009-01-04 21:14:15 +0000150
Douglas Gregor39a8de12009-02-25 19:37:18 +0000151 bool HasScopeSpecifier = false;
152
Chris Lattner5b454732009-01-05 03:55:46 +0000153 if (Tok.is(tok::coloncolon)) {
154 // ::new and ::delete aren't nested-name-specifiers.
155 tok::TokenKind NextKind = NextToken().getKind();
156 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
157 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattner55a7cef2009-01-05 00:13:00 +0000159 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000160 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
161 return true;
162
Douglas Gregor39a8de12009-02-25 19:37:18 +0000163 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000164 }
165
Douglas Gregord4dca082010-02-24 18:44:31 +0000166 bool CheckForDestructor = false;
167 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
168 CheckForDestructor = true;
169 *MayBePseudoDestructor = false;
170 }
171
Douglas Gregor39a8de12009-02-25 19:37:18 +0000172 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000173 if (HasScopeSpecifier) {
174 // C++ [basic.lookup.classref]p5:
175 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000176 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000177 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000178 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000179 // the class-name-or-namespace-name is looked up in global scope as a
180 // class-name or namespace-name.
181 //
182 // To implement this, we clear out the object type as soon as we've
183 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000184 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000185
186 if (Tok.is(tok::code_completion)) {
187 // Code completion for a nested-name-specifier, where the code
188 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000189 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000190 // Include code completion token into the range of the scope otherwise
191 // when we try to annotate the scope tokens the dangling code completion
192 // token will cause assertion in
193 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000194 SS.setEndLoc(Tok.getLocation());
195 cutOffParsing();
196 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000197 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Douglas Gregor39a8de12009-02-25 19:37:18 +0000200 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000201 // nested-name-specifier 'template'[opt] simple-template-id '::'
202
203 // Parse the optional 'template' keyword, then make sure we have
204 // 'identifier <' after it.
205 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000206 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000207 // nested-name-specifier, since they aren't allowed to start with
208 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000209 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000210 break;
211
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000212 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000213 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000214
215 UnqualifiedId TemplateName;
216 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000217 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000218 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000219 ConsumeToken();
220 } else if (Tok.is(tok::kw_operator)) {
221 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000222 TemplateName)) {
223 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000224 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000225 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000226
Sean Hunte6252d12009-11-28 08:58:14 +0000227 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
228 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000229 Diag(TemplateName.getSourceRange().getBegin(),
230 diag::err_id_after_template_in_nested_name_spec)
231 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000232 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000233 break;
234 }
235 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000236 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000237 break;
238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000240 // If the next token is not '<', we have a qualified-id that refers
241 // to a template name, such as T::template apply, but is not a
242 // template-id.
243 if (Tok.isNot(tok::less)) {
244 TPA.Revert();
245 break;
246 }
247
248 // Commit to parsing the template-id.
249 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000250 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000251 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000252 TemplateKWLoc,
253 SS,
254 TemplateName,
255 ObjectType,
256 EnteringContext,
257 Template)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000258 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000259 TemplateKWLoc, false))
260 return true;
261 } else
John McCall9ba61662010-02-26 08:45:28 +0000262 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chris Lattner77cf72a2009-06-26 03:47:46 +0000264 continue;
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Douglas Gregor39a8de12009-02-25 19:37:18 +0000267 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000268 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000269 //
270 // simple-template-id '::'
271 //
272 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000273 // right kind (it should name a type or be dependent), and then
274 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000275 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000276 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
277 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000278 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000279 }
280
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000281 // Consume the template-id token.
282 ConsumeToken();
283
284 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
285 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000287 if (!HasScopeSpecifier)
288 HasScopeSpecifier = true;
289
290 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
291 TemplateId->getTemplateArgs(),
292 TemplateId->NumArgs);
293
294 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
295 /*FIXME:*/SourceLocation(),
296 SS,
297 TemplateId->Template,
298 TemplateId->TemplateNameLoc,
299 TemplateId->LAngleLoc,
300 TemplateArgsPtr,
301 TemplateId->RAngleLoc,
302 CCLoc,
303 EnteringContext)) {
304 SourceLocation StartLoc
305 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
306 : TemplateId->TemplateNameLoc;
307 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000308 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000309
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000310 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000311 }
312
Chris Lattner5c7f7862009-06-26 03:52:38 +0000313
314 // The rest of the nested-name-specifier possibilities start with
315 // tok::identifier.
316 if (Tok.isNot(tok::identifier))
317 break;
318
319 IdentifierInfo &II = *Tok.getIdentifierInfo();
320
321 // nested-name-specifier:
322 // type-name '::'
323 // namespace-name '::'
324 // nested-name-specifier identifier '::'
325 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000326
327 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
328 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000329 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000330 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
331 Tok.getLocation(),
332 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000333 EnteringContext) &&
334 // If the token after the colon isn't an identifier, it's still an
335 // error, but they probably meant something else strange so don't
336 // recover like this.
337 PP.LookAhead(1).is(tok::identifier)) {
338 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000339 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000340
341 // Recover as if the user wrote '::'.
342 Next.setKind(tok::coloncolon);
343 }
Chris Lattner46646492009-12-07 01:36:53 +0000344 }
345
Chris Lattner5c7f7862009-06-26 03:52:38 +0000346 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000347 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000348 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000349 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000350 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000351 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000352 }
353
Chris Lattner5c7f7862009-06-26 03:52:38 +0000354 // We have an identifier followed by a '::'. Lookup this name
355 // as the name in a nested-name-specifier.
356 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000357 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
358 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000359 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000361 HasScopeSpecifier = true;
362 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
363 ObjectType, EnteringContext, SS))
364 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
365
Chris Lattner5c7f7862009-06-26 03:52:38 +0000366 continue;
367 }
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Richard Trieu950be712011-09-19 19:01:00 +0000369 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000370
Chris Lattner5c7f7862009-06-26 03:52:38 +0000371 // nested-name-specifier:
372 // type-name '<'
373 if (Next.is(tok::less)) {
374 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000375 UnqualifiedId TemplateName;
376 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000377 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000378 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000379 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000380 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000381 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000382 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000383 Template,
384 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000385 // We have found a template name, so annotate this this token
386 // with a template-id annotation. We do not permit the
387 // template-id to be translated into a type annotation,
388 // because some clients (e.g., the parsing of class template
389 // specializations) still want to see the original template-id
390 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000391 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000392 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000393 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000394 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000395 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000396 }
397
398 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000399 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000400 // We have something like t::getAs<T>, where getAs is a
401 // member of an unknown specialization. However, this will only
402 // parse correctly as a template, so suggest the keyword 'template'
403 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000404 unsigned DiagID = diag::err_missing_dependent_template_keyword;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000405 if (getLang().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000406 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000407
408 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000409 << II.getName()
410 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
411
Douglas Gregord6ab2322010-06-16 23:00:59 +0000412 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000413 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000414 Tok.getLocation(), SS,
415 TemplateName, ObjectType,
416 EnteringContext, Template)) {
417 // Consume the identifier.
418 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000419 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000420 SourceLocation(), false))
421 return true;
422 }
423 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000424 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000425
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000426 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000427 }
428 }
429
Douglas Gregor39a8de12009-02-25 19:37:18 +0000430 // We don't have any tokens that form the beginning of a
431 // nested-name-specifier, so we're done.
432 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Douglas Gregord4dca082010-02-24 18:44:31 +0000435 // Even if we didn't see any pieces of a nested-name-specifier, we
436 // still check whether there is a tilde in this position, which
437 // indicates a potential pseudo-destructor.
438 if (CheckForDestructor && Tok.is(tok::tilde))
439 *MayBePseudoDestructor = true;
440
John McCall9ba61662010-02-26 08:45:28 +0000441 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000442}
443
444/// ParseCXXIdExpression - Handle id-expression.
445///
446/// id-expression:
447/// unqualified-id
448/// qualified-id
449///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000450/// qualified-id:
451/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
452/// '::' identifier
453/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000454/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000455///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000456/// NOTE: The standard specifies that, for qualified-id, the parser does not
457/// expect:
458///
459/// '::' conversion-function-id
460/// '::' '~' class-name
461///
462/// This may cause a slight inconsistency on diagnostics:
463///
464/// class C {};
465/// namespace A {}
466/// void f() {
467/// :: A :: ~ C(); // Some Sema error about using destructor with a
468/// // namespace.
469/// :: ~ C(); // Some Parser error like 'unexpected ~'.
470/// }
471///
472/// We simplify the parser a bit and make it work like:
473///
474/// qualified-id:
475/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
476/// '::' unqualified-id
477///
478/// That way Sema can handle and report similar errors for namespaces and the
479/// global scope.
480///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000481/// The isAddressOfOperand parameter indicates that this id-expression is a
482/// direct operand of the address-of operator. This is, besides member contexts,
483/// the only place where a qualified-id naming a non-static class member may
484/// appear.
485///
John McCall60d7b3a2010-08-24 06:29:42 +0000486ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000487 // qualified-id:
488 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
489 // '::' unqualified-id
490 //
491 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000492 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000493
494 UnqualifiedId Name;
495 if (ParseUnqualifiedId(SS,
496 /*EnteringContext=*/false,
497 /*AllowDestructorName=*/false,
498 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000499 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000500 Name))
501 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000502
503 // This is only the direct operand of an & operator if it is not
504 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000505 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
506 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000507
Douglas Gregor23c94db2010-07-02 17:43:08 +0000508 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000509 isAddressOfOperand);
510
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000511}
512
Douglas Gregorae7902c2011-08-04 15:30:47 +0000513/// ParseLambdaExpression - Parse a C++0x lambda expression.
514///
515/// lambda-expression:
516/// lambda-introducer lambda-declarator[opt] compound-statement
517///
518/// lambda-introducer:
519/// '[' lambda-capture[opt] ']'
520///
521/// lambda-capture:
522/// capture-default
523/// capture-list
524/// capture-default ',' capture-list
525///
526/// capture-default:
527/// '&'
528/// '='
529///
530/// capture-list:
531/// capture
532/// capture-list ',' capture
533///
534/// capture:
535/// identifier
536/// '&' identifier
537/// 'this'
538///
539/// lambda-declarator:
540/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
541/// 'mutable'[opt] exception-specification[opt]
542/// trailing-return-type[opt]
543///
544ExprResult Parser::ParseLambdaExpression() {
545 // Parse lambda-introducer.
546 LambdaIntroducer Intro;
547
548 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
549 if (DiagID) {
550 Diag(Tok, DiagID.getValue());
551 SkipUntil(tok::r_square);
552 }
553
554 return ParseLambdaExpressionAfterIntroducer(Intro);
555}
556
557/// TryParseLambdaExpression - Use lookahead and potentially tentative
558/// parsing to determine if we are looking at a C++0x lambda expression, and parse
559/// it if we are.
560///
561/// If we are not looking at a lambda expression, returns ExprError().
562ExprResult Parser::TryParseLambdaExpression() {
563 assert(getLang().CPlusPlus0x
564 && Tok.is(tok::l_square)
565 && "Not at the start of a possible lambda expression.");
566
567 const Token Next = NextToken(), After = GetLookAheadToken(2);
568
569 // If lookahead indicates this is a lambda...
570 if (Next.is(tok::r_square) || // []
571 Next.is(tok::equal) || // [=
572 (Next.is(tok::amp) && // [&] or [&,
573 (After.is(tok::r_square) ||
574 After.is(tok::comma))) ||
575 (Next.is(tok::identifier) && // [identifier]
576 After.is(tok::r_square))) {
577 return ParseLambdaExpression();
578 }
579
580 // If lookahead indicates this is an Objective-C message...
581 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
582 return ExprError();
583 }
584
585 LambdaIntroducer Intro;
586 if (TryParseLambdaIntroducer(Intro))
587 return ExprError();
588 return ParseLambdaExpressionAfterIntroducer(Intro);
589}
590
591/// ParseLambdaExpression - Parse a lambda introducer.
592///
593/// Returns a DiagnosticID if it hit something unexpected.
594llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
595 typedef llvm::Optional<unsigned> DiagResult;
596
597 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
598 Intro.Range.setBegin(ConsumeBracket());
599
600 bool first = true;
601
602 // Parse capture-default.
603 if (Tok.is(tok::amp) &&
604 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
605 Intro.Default = LCD_ByRef;
606 ConsumeToken();
607 first = false;
608 } else if (Tok.is(tok::equal)) {
609 Intro.Default = LCD_ByCopy;
610 ConsumeToken();
611 first = false;
612 }
613
614 while (Tok.isNot(tok::r_square)) {
615 if (!first) {
616 if (Tok.isNot(tok::comma))
617 return DiagResult(diag::err_expected_comma_or_rsquare);
618 ConsumeToken();
619 }
620
621 first = false;
622
623 // Parse capture.
624 LambdaCaptureKind Kind = LCK_ByCopy;
625 SourceLocation Loc;
626 IdentifierInfo* Id = 0;
627
628 if (Tok.is(tok::kw_this)) {
629 Kind = LCK_This;
630 Loc = ConsumeToken();
631 } else {
632 if (Tok.is(tok::amp)) {
633 Kind = LCK_ByRef;
634 ConsumeToken();
635 }
636
637 if (Tok.is(tok::identifier)) {
638 Id = Tok.getIdentifierInfo();
639 Loc = ConsumeToken();
640 } else if (Tok.is(tok::kw_this)) {
641 // FIXME: If we want to suggest a fixit here, will need to return more
642 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
643 // Clear()ed to prevent emission in case of tentative parsing?
644 return DiagResult(diag::err_this_captured_by_reference);
645 } else {
646 return DiagResult(diag::err_expected_capture);
647 }
648 }
649
650 Intro.addCapture(Kind, Loc, Id);
651 }
652
653 Intro.Range.setEnd(MatchRHSPunctuation(tok::r_square,
654 Intro.Range.getBegin()));
655
656 return DiagResult();
657}
658
659/// TryParseLambdaExpression - Tentatively parse a lambda introducer.
660///
661/// Returns true if it hit something unexpected.
662bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
663 TentativeParsingAction PA(*this);
664
665 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
666
667 if (DiagID) {
668 PA.Revert();
669 return true;
670 }
671
672 PA.Commit();
673 return false;
674}
675
676/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
677/// expression.
678ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
679 LambdaIntroducer &Intro) {
680 // Parse lambda-declarator[opt].
681 DeclSpec DS(AttrFactory);
682 Declarator D(DS, Declarator::PrototypeContext);
683
684 if (Tok.is(tok::l_paren)) {
685 ParseScope PrototypeScope(this,
686 Scope::FunctionPrototypeScope |
687 Scope::DeclScope);
688
689 SourceLocation DeclLoc = ConsumeParen(), DeclEndLoc;
690
691 // Parse parameter-declaration-clause.
692 ParsedAttributes Attr(AttrFactory);
693 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
694 SourceLocation EllipsisLoc;
695
696 if (Tok.isNot(tok::r_paren))
697 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
698
699 DeclEndLoc = MatchRHSPunctuation(tok::r_paren, DeclLoc);
700
701 // Parse 'mutable'[opt].
702 SourceLocation MutableLoc;
703 if (Tok.is(tok::kw_mutable)) {
704 MutableLoc = ConsumeToken();
705 DeclEndLoc = MutableLoc;
706 }
707
708 // Parse exception-specification[opt].
709 ExceptionSpecificationType ESpecType = EST_None;
710 SourceRange ESpecRange;
711 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
712 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
713 ExprResult NoexceptExpr;
714 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
715 DynamicExceptions,
716 DynamicExceptionRanges,
717 NoexceptExpr);
718
719 if (ESpecType != EST_None)
720 DeclEndLoc = ESpecRange.getEnd();
721
722 // Parse attribute-specifier[opt].
723 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
724
725 // Parse trailing-return-type[opt].
726 ParsedType TrailingReturnType;
727 if (Tok.is(tok::arrow)) {
728 SourceRange Range;
729 TrailingReturnType = ParseTrailingReturnType(Range).get();
730 if (Range.getEnd().isValid())
731 DeclEndLoc = Range.getEnd();
732 }
733
734 PrototypeScope.Exit();
735
736 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
737 /*isVariadic=*/EllipsisLoc.isValid(),
738 EllipsisLoc,
739 ParamInfo.data(), ParamInfo.size(),
740 DS.getTypeQualifiers(),
741 /*RefQualifierIsLValueRef=*/true,
742 /*RefQualifierLoc=*/SourceLocation(),
743 MutableLoc,
744 ESpecType, ESpecRange.getBegin(),
745 DynamicExceptions.data(),
746 DynamicExceptionRanges.data(),
747 DynamicExceptions.size(),
748 NoexceptExpr.isUsable() ?
749 NoexceptExpr.get() : 0,
750 DeclLoc, DeclEndLoc, D,
751 TrailingReturnType),
752 Attr, DeclEndLoc);
753 }
754
755 // Parse compound-statement.
756 if (Tok.is(tok::l_brace)) {
757 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
758 // it.
759 ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
760 Scope::BreakScope | Scope::ContinueScope |
761 Scope::DeclScope);
762
763 StmtResult Stmt(ParseCompoundStatementBody());
764
765 BodyScope.Exit();
766 } else {
767 Diag(Tok, diag::err_expected_lambda_body);
768 }
769
770 return ExprEmpty();
771}
772
Reid Spencer5f016e22007-07-11 17:01:13 +0000773/// ParseCXXCasts - This handles the various ways to cast expressions to another
774/// type.
775///
776/// postfix-expression: [C++ 5.2p1]
777/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
778/// 'static_cast' '<' type-name '>' '(' expression ')'
779/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
780/// 'const_cast' '<' type-name '>' '(' expression ')'
781///
John McCall60d7b3a2010-08-24 06:29:42 +0000782ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 tok::TokenKind Kind = Tok.getKind();
784 const char *CastName = 0; // For error messages
785
786 switch (Kind) {
787 default: assert(0 && "Unknown C++ cast!"); abort();
788 case tok::kw_const_cast: CastName = "const_cast"; break;
789 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
790 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
791 case tok::kw_static_cast: CastName = "static_cast"; break;
792 }
793
794 SourceLocation OpLoc = ConsumeToken();
795 SourceLocation LAngleBracketLoc = Tok.getLocation();
796
Richard Smithea698b32011-04-14 21:45:45 +0000797 // Check for "<::" which is parsed as "[:". If found, fix token stream,
798 // diagnose error, suggest fix, and recover parsing.
799 Token Next = NextToken();
800 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
801 AreTokensAdjacent(PP, Tok, Next))
802 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
803
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000805 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000806
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000807 // Parse the common declaration-specifiers piece.
808 DeclSpec DS(AttrFactory);
809 ParseSpecifierQualifierList(DS);
810
811 // Parse the abstract-declarator, if present.
812 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
813 ParseDeclarator(DeclaratorInfo);
814
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 SourceLocation RAngleBracketLoc = Tok.getLocation();
816
Chris Lattner1ab3b962008-11-18 07:48:38 +0000817 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000818 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000819
820 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
821
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000822 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
823 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000824
John McCall60d7b3a2010-08-24 06:29:42 +0000825 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000827 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000828 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000829
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000830 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +0000831 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000832 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +0000833 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000834 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000835
Sebastian Redl20df9b72008-12-11 22:51:44 +0000836 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000837}
838
Sebastian Redlc42e1182008-11-11 11:37:55 +0000839/// ParseCXXTypeid - This handles the C++ typeid expression.
840///
841/// postfix-expression: [C++ 5.2p1]
842/// 'typeid' '(' expression ')'
843/// 'typeid' '(' type-id ')'
844///
John McCall60d7b3a2010-08-24 06:29:42 +0000845ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000846 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
847
848 SourceLocation OpLoc = ConsumeToken();
849 SourceLocation LParenLoc = Tok.getLocation();
850 SourceLocation RParenLoc;
851
852 // typeid expressions are always parenthesized.
853 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
854 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000855 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000856
John McCall60d7b3a2010-08-24 06:29:42 +0000857 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000858
859 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000860 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000861
862 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000863 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000864
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000865 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000866 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000867
868 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000869 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000870 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000871 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000872 // When typeid is applied to an expression other than an lvalue of a
873 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000874 // operand (Clause 5).
875 //
Mike Stump1eb44332009-09-09 15:08:12 +0000876 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000877 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000878 // we the expression is potentially potentially evaluated.
879 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000880 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000881 Result = ParseExpression();
882
883 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000884 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000885 SkipUntil(tok::r_paren);
886 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000887 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
888 if (RParenLoc.isInvalid())
889 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000890
891 // If we are a foo<int> that identifies a single function, resolve it now...
892 Expr* e = Result.get();
893 if (e->getType() == Actions.Context.OverloadTy) {
894 ExprResult er =
895 Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
896 if (er.isUsable())
897 Result = er.release();
898 }
Sebastian Redlc42e1182008-11-11 11:37:55 +0000899 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000900 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000901 }
902 }
903
Sebastian Redl20df9b72008-12-11 22:51:44 +0000904 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000905}
906
Francois Pichet01b7c302010-09-08 12:20:18 +0000907/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
908///
909/// '__uuidof' '(' expression ')'
910/// '__uuidof' '(' type-id ')'
911///
912ExprResult Parser::ParseCXXUuidof() {
913 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
914
915 SourceLocation OpLoc = ConsumeToken();
916 SourceLocation LParenLoc = Tok.getLocation();
917 SourceLocation RParenLoc;
918
919 // __uuidof expressions are always parenthesized.
920 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
921 "__uuidof"))
922 return ExprError();
923
924 ExprResult Result;
925
926 if (isTypeIdInParens()) {
927 TypeResult Ty = ParseTypeName();
928
929 // Match the ')'.
930 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
931
932 if (Ty.isInvalid())
933 return ExprError();
934
935 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
936 Ty.get().getAsOpaquePtr(), RParenLoc);
937 } else {
938 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
939 Result = ParseExpression();
940
941 // Match the ')'.
942 if (Result.isInvalid())
943 SkipUntil(tok::r_paren);
944 else {
945 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
946
947 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
948 Result.release(), RParenLoc);
949 }
950 }
951
952 return move(Result);
953}
954
Douglas Gregord4dca082010-02-24 18:44:31 +0000955/// \brief Parse a C++ pseudo-destructor expression after the base,
956/// . or -> operator, and nested-name-specifier have already been
957/// parsed.
958///
959/// postfix-expression: [C++ 5.2]
960/// postfix-expression . pseudo-destructor-name
961/// postfix-expression -> pseudo-destructor-name
962///
963/// pseudo-destructor-name:
964/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
965/// ::[opt] nested-name-specifier template simple-template-id ::
966/// ~type-name
967/// ::[opt] nested-name-specifier[opt] ~type-name
968///
John McCall60d7b3a2010-08-24 06:29:42 +0000969ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000970Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
971 tok::TokenKind OpKind,
972 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000973 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000974 // We're parsing either a pseudo-destructor-name or a dependent
975 // member access that has the same form as a
976 // pseudo-destructor-name. We parse both in the same way and let
977 // the action model sort them out.
978 //
979 // Note that the ::[opt] nested-name-specifier[opt] has already
980 // been parsed, and if there was a simple-template-id, it has
981 // been coalesced into a template-id annotation token.
982 UnqualifiedId FirstTypeName;
983 SourceLocation CCLoc;
984 if (Tok.is(tok::identifier)) {
985 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
986 ConsumeToken();
987 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
988 CCLoc = ConsumeToken();
989 } else if (Tok.is(tok::annot_template_id)) {
990 FirstTypeName.setTemplateId(
991 (TemplateIdAnnotation *)Tok.getAnnotationValue());
992 ConsumeToken();
993 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
994 CCLoc = ConsumeToken();
995 } else {
996 FirstTypeName.setIdentifier(0, SourceLocation());
997 }
998
999 // Parse the tilde.
1000 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1001 SourceLocation TildeLoc = ConsumeToken();
1002 if (!Tok.is(tok::identifier)) {
1003 Diag(Tok, diag::err_destructor_tilde_identifier);
1004 return ExprError();
1005 }
1006
1007 // Parse the second type.
1008 UnqualifiedId SecondTypeName;
1009 IdentifierInfo *Name = Tok.getIdentifierInfo();
1010 SourceLocation NameLoc = ConsumeToken();
1011 SecondTypeName.setIdentifier(Name, NameLoc);
1012
1013 // If there is a '<', the second type name is a template-id. Parse
1014 // it as such.
1015 if (Tok.is(tok::less) &&
1016 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001017 SecondTypeName, /*AssumeTemplateName=*/true,
1018 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +00001019 return ExprError();
1020
John McCall9ae2f072010-08-23 23:25:46 +00001021 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1022 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001023 SS, FirstTypeName, CCLoc,
1024 TildeLoc, SecondTypeName,
1025 Tok.is(tok::l_paren));
1026}
1027
Reid Spencer5f016e22007-07-11 17:01:13 +00001028/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1029///
1030/// boolean-literal: [C++ 2.13.5]
1031/// 'true'
1032/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001033ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001035 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001036}
Chris Lattner50dd2892008-02-26 00:51:44 +00001037
1038/// ParseThrowExpression - This handles the C++ throw expression.
1039///
1040/// throw-expression: [C++ 15]
1041/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001042ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001043 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001044 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001045
Chris Lattner2a2819a2008-04-06 06:02:23 +00001046 // If the current token isn't the start of an assignment-expression,
1047 // then the expression is not present. This handles things like:
1048 // "C ? throw : (void)42", which is crazy but legal.
1049 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1050 case tok::semi:
1051 case tok::r_paren:
1052 case tok::r_square:
1053 case tok::r_brace:
1054 case tok::colon:
1055 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001056 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001057
Chris Lattner2a2819a2008-04-06 06:02:23 +00001058 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001059 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +00001060 if (Expr.isInvalid()) return move(Expr);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001061 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001062 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001063}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001064
1065/// ParseCXXThis - This handles the C++ 'this' pointer.
1066///
1067/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1068/// a non-lvalue expression whose value is the address of the object for which
1069/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001070ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001071 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1072 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001073 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001074}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001075
1076/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1077/// Can be interpreted either as function-style casting ("int(x)")
1078/// or class type construction ("ClassType(x,y,z)")
1079/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001080/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001081///
1082/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001083/// simple-type-specifier '(' expression-list[opt] ')'
1084/// [C++0x] simple-type-specifier braced-init-list
1085/// typename-specifier '(' expression-list[opt] ')'
1086/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001087///
John McCall60d7b3a2010-08-24 06:29:42 +00001088ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001089Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001090 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001091 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001092
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001093 assert((Tok.is(tok::l_paren) ||
1094 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1095 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001096
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001097 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001098
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001099 // FIXME: Convert to a proper type construct expression.
1100 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001101
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001102 } else {
1103 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1104
1105 SourceLocation LParenLoc = ConsumeParen();
1106
1107 ExprVector Exprs(Actions);
1108 CommaLocsTy CommaLocs;
1109
1110 if (Tok.isNot(tok::r_paren)) {
1111 if (ParseExpressionList(Exprs, CommaLocs)) {
1112 SkipUntil(tok::r_paren);
1113 return ExprError();
1114 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001115 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001116
1117 // Match the ')'.
1118 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1119
1120 // TypeRep could be null, if it references an invalid typedef.
1121 if (!TypeRep)
1122 return ExprError();
1123
1124 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1125 "Unexpected number of commas!");
1126 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
1127 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001128 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001129}
1130
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001131/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001132///
1133/// condition:
1134/// expression
1135/// type-specifier-seq declarator '=' assignment-expression
1136/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1137/// '=' assignment-expression
1138///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001139/// \param ExprResult if the condition was parsed as an expression, the
1140/// parsed expression.
1141///
1142/// \param DeclResult if the condition was parsed as a declaration, the
1143/// parsed declaration.
1144///
Douglas Gregor586596f2010-05-06 17:25:47 +00001145/// \param Loc The location of the start of the statement that requires this
1146/// condition, e.g., the "for" in a for loop.
1147///
1148/// \param ConvertToBoolean Whether the condition expression should be
1149/// converted to a boolean value.
1150///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001151/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001152bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1153 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001154 SourceLocation Loc,
1155 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001156 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001157 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001158 cutOffParsing();
1159 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001160 }
1161
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001162 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001163 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001164 ExprOut = ParseExpression(); // expression
1165 DeclOut = 0;
1166 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001167 return true;
1168
1169 // If required, convert to a boolean value.
1170 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001171 ExprOut
1172 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1173 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001174 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001175
1176 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001177 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001178 ParseSpecifierQualifierList(DS);
1179
1180 // declarator
1181 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1182 ParseDeclarator(DeclaratorInfo);
1183
1184 // simple-asm-expr[opt]
1185 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001186 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001187 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001188 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001189 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001190 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001191 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001192 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001193 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001194 }
1195
1196 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001197 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001198
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001199 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001200 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001201 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001202 DeclOut = Dcl.get();
1203 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001204
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001205 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001206 if (isTokenEqualOrMistypedEqualEqual(
1207 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001208 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001209 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001210 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +00001211 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1212 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001213 } else {
1214 // FIXME: C++0x allows a braced-init-list
1215 Diag(Tok, diag::err_expected_equal_after_declarator);
1216 }
1217
Douglas Gregor586596f2010-05-06 17:25:47 +00001218 // FIXME: Build a reference to this declaration? Convert it to bool?
1219 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001220
1221 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001222
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001223 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001224}
1225
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001226/// \brief Determine whether the current token starts a C++
1227/// simple-type-specifier.
1228bool Parser::isCXXSimpleTypeSpecifier() const {
1229 switch (Tok.getKind()) {
1230 case tok::annot_typename:
1231 case tok::kw_short:
1232 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001233 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001234 case tok::kw_signed:
1235 case tok::kw_unsigned:
1236 case tok::kw_void:
1237 case tok::kw_char:
1238 case tok::kw_int:
1239 case tok::kw_float:
1240 case tok::kw_double:
1241 case tok::kw_wchar_t:
1242 case tok::kw_char16_t:
1243 case tok::kw_char32_t:
1244 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +00001245 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001246 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +00001247 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001248 return true;
1249
1250 default:
1251 break;
1252 }
1253
1254 return false;
1255}
1256
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001257/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1258/// This should only be called when the current token is known to be part of
1259/// simple-type-specifier.
1260///
1261/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001262/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001263/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1264/// char
1265/// wchar_t
1266/// bool
1267/// short
1268/// int
1269/// long
1270/// signed
1271/// unsigned
1272/// float
1273/// double
1274/// void
1275/// [GNU] typeof-specifier
1276/// [C++0x] auto [TODO]
1277///
1278/// type-name:
1279/// class-name
1280/// enum-name
1281/// typedef-name
1282///
1283void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1284 DS.SetRangeStart(Tok.getLocation());
1285 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001286 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001287 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001289 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001290 case tok::identifier: // foo::bar
1291 case tok::coloncolon: // ::foo::bar
1292 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001293 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001294 assert(0 && "Not a simple-type-specifier token!");
1295 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +00001296
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001297 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001298 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001299 if (getTypeAnnotation(Tok))
1300 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1301 getTypeAnnotation(Tok));
1302 else
1303 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001304
1305 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1306 ConsumeToken();
1307
1308 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1309 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1310 // Objective-C interface. If we don't have Objective-C or a '<', this is
1311 // just a normal reference to a typedef name.
1312 if (Tok.is(tok::less) && getLang().ObjC1)
1313 ParseObjCProtocolQualifiers(DS);
1314
1315 DS.Finish(Diags, PP);
1316 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001317 }
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001319 // builtin types
1320 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001321 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001322 break;
1323 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001324 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001325 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001326 case tok::kw___int64:
1327 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1328 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001329 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001330 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001331 break;
1332 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001333 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001334 break;
1335 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001336 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001337 break;
1338 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001339 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001340 break;
1341 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001342 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001343 break;
1344 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001345 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001346 break;
1347 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001348 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001349 break;
1350 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001351 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001352 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001353 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001354 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001355 break;
1356 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001357 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001358 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001359 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001360 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001361 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001363 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001364 // GNU typeof support.
1365 case tok::kw_typeof:
1366 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001367 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001368 return;
1369 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001370 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001371 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1372 else
1373 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001374 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001375 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001376}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001377
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001378/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1379/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1380/// e.g., "const short int". Note that the DeclSpec is *not* finished
1381/// by parsing the type-specifier-seq, because these sequences are
1382/// typically followed by some form of declarator. Returns true and
1383/// emits diagnostics if this is not a type-specifier-seq, false
1384/// otherwise.
1385///
1386/// type-specifier-seq: [C++ 8.1]
1387/// type-specifier type-specifier-seq[opt]
1388///
1389bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1390 DS.SetRangeStart(Tok.getLocation());
1391 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001392 unsigned DiagID;
1393 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001394
1395 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001396 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1397 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001398 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001399 return true;
1400 }
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Sebastian Redld9bafa72010-02-03 21:21:43 +00001402 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1403 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1404 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001405
Douglas Gregor396a9f22010-02-24 23:13:13 +00001406 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001407 return false;
1408}
1409
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001410/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1411/// some form.
1412///
1413/// This routine is invoked when a '<' is encountered after an identifier or
1414/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1415/// whether the unqualified-id is actually a template-id. This routine will
1416/// then parse the template arguments and form the appropriate template-id to
1417/// return to the caller.
1418///
1419/// \param SS the nested-name-specifier that precedes this template-id, if
1420/// we're actually parsing a qualified-id.
1421///
1422/// \param Name for constructor and destructor names, this is the actual
1423/// identifier that may be a template-name.
1424///
1425/// \param NameLoc the location of the class-name in a constructor or
1426/// destructor.
1427///
1428/// \param EnteringContext whether we're entering the scope of the
1429/// nested-name-specifier.
1430///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001431/// \param ObjectType if this unqualified-id occurs within a member access
1432/// expression, the type of the base object whose member is being accessed.
1433///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001434/// \param Id as input, describes the template-name or operator-function-id
1435/// that precedes the '<'. If template arguments were parsed successfully,
1436/// will be updated with the template-id.
1437///
Douglas Gregord4dca082010-02-24 18:44:31 +00001438/// \param AssumeTemplateId When true, this routine will assume that the name
1439/// refers to a template without performing name lookup to verify.
1440///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001441/// \returns true if a parse error occurred, false otherwise.
1442bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1443 IdentifierInfo *Name,
1444 SourceLocation NameLoc,
1445 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001446 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001447 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001448 bool AssumeTemplateId,
1449 SourceLocation TemplateKWLoc) {
1450 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1451 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001452
1453 TemplateTy Template;
1454 TemplateNameKind TNK = TNK_Non_template;
1455 switch (Id.getKind()) {
1456 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001457 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001458 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001459 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001460 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001461 Id, ObjectType, EnteringContext,
1462 Template);
1463 if (TNK == TNK_Non_template)
1464 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001465 } else {
1466 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001467 TNK = Actions.isTemplateName(getCurScope(), SS,
1468 TemplateKWLoc.isValid(), Id,
1469 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001470 MemberOfUnknownSpecialization);
1471
1472 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1473 ObjectType && IsTemplateArgumentList()) {
1474 // We have something like t->getAs<T>(), where getAs is a
1475 // member of an unknown specialization. However, this will only
1476 // parse correctly as a template, so suggest the keyword 'template'
1477 // before 'getAs' and treat this as a dependent template name.
1478 std::string Name;
1479 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1480 Name = Id.Identifier->getName();
1481 else {
1482 Name = "operator ";
1483 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1484 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1485 else
1486 Name += Id.Identifier->getName();
1487 }
1488 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1489 << Name
1490 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001491 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001492 SS, Id, ObjectType,
1493 EnteringContext, Template);
1494 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001495 return true;
1496 }
1497 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001498 break;
1499
Douglas Gregor014e88d2009-11-03 23:16:33 +00001500 case UnqualifiedId::IK_ConstructorName: {
1501 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001502 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001503 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001504 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1505 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001506 EnteringContext, Template,
1507 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001508 break;
1509 }
1510
Douglas Gregor014e88d2009-11-03 23:16:33 +00001511 case UnqualifiedId::IK_DestructorName: {
1512 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001513 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001514 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001515 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001516 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001517 TemplateName, ObjectType,
1518 EnteringContext, Template);
1519 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001520 return true;
1521 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001522 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1523 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001524 EnteringContext, Template,
1525 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001526
John McCallb3d87482010-08-24 05:47:05 +00001527 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001528 Diag(NameLoc, diag::err_destructor_template_id)
1529 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001530 return true;
1531 }
1532 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001533 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001534 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001535
1536 default:
1537 return false;
1538 }
1539
1540 if (TNK == TNK_Non_template)
1541 return false;
1542
1543 // Parse the enclosed template argument list.
1544 SourceLocation LAngleLoc, RAngleLoc;
1545 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001546 if (Tok.is(tok::less) &&
1547 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001548 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001549 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001550 RAngleLoc))
1551 return true;
1552
1553 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001554 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1555 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001556 // Form a parsed representation of the template-id to be stored in the
1557 // UnqualifiedId.
1558 TemplateIdAnnotation *TemplateId
1559 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1560
1561 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1562 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001563 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001564 TemplateId->TemplateNameLoc = Id.StartLocation;
1565 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001566 TemplateId->Name = 0;
1567 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1568 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001569 }
1570
Douglas Gregor059101f2011-03-02 00:47:37 +00001571 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001572 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001573 TemplateId->Kind = TNK;
1574 TemplateId->LAngleLoc = LAngleLoc;
1575 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001576 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001577 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001578 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001579 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001580
1581 Id.setTemplateId(TemplateId);
1582 return false;
1583 }
1584
1585 // Bundle the template arguments together.
1586 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001587 TemplateArgs.size());
1588
1589 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001590 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001591 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001592 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001593 RAngleLoc);
1594 if (Type.isInvalid())
1595 return true;
1596
1597 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1598 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1599 else
1600 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1601
1602 return false;
1603}
1604
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001605/// \brief Parse an operator-function-id or conversion-function-id as part
1606/// of a C++ unqualified-id.
1607///
1608/// This routine is responsible only for parsing the operator-function-id or
1609/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001610///
1611/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001612/// operator-function-id: [C++ 13.5]
1613/// 'operator' operator
1614///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001615/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001616/// new delete new[] delete[]
1617/// + - * / % ^ & | ~
1618/// ! = < > += -= *= /= %=
1619/// ^= &= |= << >> >>= <<= == !=
1620/// <= >= && || ++ -- , ->* ->
1621/// () []
1622///
1623/// conversion-function-id: [C++ 12.3.2]
1624/// operator conversion-type-id
1625///
1626/// conversion-type-id:
1627/// type-specifier-seq conversion-declarator[opt]
1628///
1629/// conversion-declarator:
1630/// ptr-operator conversion-declarator[opt]
1631/// \endcode
1632///
1633/// \param The nested-name-specifier that preceded this unqualified-id. If
1634/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1635///
1636/// \param EnteringContext whether we are entering the scope of the
1637/// nested-name-specifier.
1638///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001639/// \param ObjectType if this unqualified-id occurs within a member access
1640/// expression, the type of the base object whose member is being accessed.
1641///
1642/// \param Result on a successful parse, contains the parsed unqualified-id.
1643///
1644/// \returns true if parsing fails, false otherwise.
1645bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001646 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001647 UnqualifiedId &Result) {
1648 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1649
1650 // Consume the 'operator' keyword.
1651 SourceLocation KeywordLoc = ConsumeToken();
1652
1653 // Determine what kind of operator name we have.
1654 unsigned SymbolIdx = 0;
1655 SourceLocation SymbolLocations[3];
1656 OverloadedOperatorKind Op = OO_None;
1657 switch (Tok.getKind()) {
1658 case tok::kw_new:
1659 case tok::kw_delete: {
1660 bool isNew = Tok.getKind() == tok::kw_new;
1661 // Consume the 'new' or 'delete'.
1662 SymbolLocations[SymbolIdx++] = ConsumeToken();
1663 if (Tok.is(tok::l_square)) {
1664 // Consume the '['.
1665 SourceLocation LBracketLoc = ConsumeBracket();
1666 // Consume the ']'.
1667 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1668 LBracketLoc);
1669 if (RBracketLoc.isInvalid())
1670 return true;
1671
1672 SymbolLocations[SymbolIdx++] = LBracketLoc;
1673 SymbolLocations[SymbolIdx++] = RBracketLoc;
1674 Op = isNew? OO_Array_New : OO_Array_Delete;
1675 } else {
1676 Op = isNew? OO_New : OO_Delete;
1677 }
1678 break;
1679 }
1680
1681#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1682 case tok::Token: \
1683 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1684 Op = OO_##Name; \
1685 break;
1686#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1687#include "clang/Basic/OperatorKinds.def"
1688
1689 case tok::l_paren: {
1690 // Consume the '('.
1691 SourceLocation LParenLoc = ConsumeParen();
1692 // Consume the ')'.
1693 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1694 LParenLoc);
1695 if (RParenLoc.isInvalid())
1696 return true;
1697
1698 SymbolLocations[SymbolIdx++] = LParenLoc;
1699 SymbolLocations[SymbolIdx++] = RParenLoc;
1700 Op = OO_Call;
1701 break;
1702 }
1703
1704 case tok::l_square: {
1705 // Consume the '['.
1706 SourceLocation LBracketLoc = ConsumeBracket();
1707 // Consume the ']'.
1708 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1709 LBracketLoc);
1710 if (RBracketLoc.isInvalid())
1711 return true;
1712
1713 SymbolLocations[SymbolIdx++] = LBracketLoc;
1714 SymbolLocations[SymbolIdx++] = RBracketLoc;
1715 Op = OO_Subscript;
1716 break;
1717 }
1718
1719 case tok::code_completion: {
1720 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001721 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001722 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001723 // Don't try to parse any further.
1724 return true;
1725 }
1726
1727 default:
1728 break;
1729 }
1730
1731 if (Op != OO_None) {
1732 // We have parsed an operator-function-id.
1733 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1734 return false;
1735 }
Sean Hunt0486d742009-11-28 04:44:28 +00001736
1737 // Parse a literal-operator-id.
1738 //
1739 // literal-operator-id: [C++0x 13.5.8]
1740 // operator "" identifier
1741
1742 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1743 if (Tok.getLength() != 2)
1744 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1745 ConsumeStringToken();
1746
1747 if (Tok.isNot(tok::identifier)) {
1748 Diag(Tok.getLocation(), diag::err_expected_ident);
1749 return true;
1750 }
1751
1752 IdentifierInfo *II = Tok.getIdentifierInfo();
1753 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001754 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001755 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001756
1757 // Parse a conversion-function-id.
1758 //
1759 // conversion-function-id: [C++ 12.3.2]
1760 // operator conversion-type-id
1761 //
1762 // conversion-type-id:
1763 // type-specifier-seq conversion-declarator[opt]
1764 //
1765 // conversion-declarator:
1766 // ptr-operator conversion-declarator[opt]
1767
1768 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001769 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001770 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001771 return true;
1772
1773 // Parse the conversion-declarator, which is merely a sequence of
1774 // ptr-operators.
1775 Declarator D(DS, Declarator::TypeNameContext);
1776 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1777
1778 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001779 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001780 if (Ty.isInvalid())
1781 return true;
1782
1783 // Note that this is a conversion-function-id.
1784 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1785 D.getSourceRange().getEnd());
1786 return false;
1787}
1788
1789/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1790/// name of an entity.
1791///
1792/// \code
1793/// unqualified-id: [C++ expr.prim.general]
1794/// identifier
1795/// operator-function-id
1796/// conversion-function-id
1797/// [C++0x] literal-operator-id [TODO]
1798/// ~ class-name
1799/// template-id
1800///
1801/// \endcode
1802///
1803/// \param The nested-name-specifier that preceded this unqualified-id. If
1804/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1805///
1806/// \param EnteringContext whether we are entering the scope of the
1807/// nested-name-specifier.
1808///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001809/// \param AllowDestructorName whether we allow parsing of a destructor name.
1810///
1811/// \param AllowConstructorName whether we allow parsing a constructor name.
1812///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001813/// \param ObjectType if this unqualified-id occurs within a member access
1814/// expression, the type of the base object whose member is being accessed.
1815///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001816/// \param Result on a successful parse, contains the parsed unqualified-id.
1817///
1818/// \returns true if parsing fails, false otherwise.
1819bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1820 bool AllowDestructorName,
1821 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001822 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001823 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001824
1825 // Handle 'A::template B'. This is for template-ids which have not
1826 // already been annotated by ParseOptionalCXXScopeSpecifier().
1827 bool TemplateSpecified = false;
1828 SourceLocation TemplateKWLoc;
1829 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1830 (ObjectType || SS.isSet())) {
1831 TemplateSpecified = true;
1832 TemplateKWLoc = ConsumeToken();
1833 }
1834
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001835 // unqualified-id:
1836 // identifier
1837 // template-id (when it hasn't already been annotated)
1838 if (Tok.is(tok::identifier)) {
1839 // Consume the identifier.
1840 IdentifierInfo *Id = Tok.getIdentifierInfo();
1841 SourceLocation IdLoc = ConsumeToken();
1842
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001843 if (!getLang().CPlusPlus) {
1844 // If we're not in C++, only identifiers matter. Record the
1845 // identifier and return.
1846 Result.setIdentifier(Id, IdLoc);
1847 return false;
1848 }
1849
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001850 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001851 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001852 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001853 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001854 &SS, false, false,
1855 ParsedType(),
1856 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001857 IdLoc, IdLoc);
1858 } else {
1859 // We have parsed an identifier.
1860 Result.setIdentifier(Id, IdLoc);
1861 }
1862
1863 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001864 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001865 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001866 ObjectType, Result,
1867 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001868
1869 return false;
1870 }
1871
1872 // unqualified-id:
1873 // template-id (already parsed and annotated)
1874 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001875 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001876
1877 // If the template-name names the current class, then this is a constructor
1878 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001879 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001880 if (SS.isSet()) {
1881 // C++ [class.qual]p2 specifies that a qualified template-name
1882 // is taken as the constructor name where a constructor can be
1883 // declared. Thus, the template arguments are extraneous, so
1884 // complain about them and remove them entirely.
1885 Diag(TemplateId->TemplateNameLoc,
1886 diag::err_out_of_line_constructor_template_id)
1887 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001888 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001889 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1890 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1891 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001892 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001893 &SS, false, false,
1894 ParsedType(),
1895 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001896 TemplateId->TemplateNameLoc,
1897 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001898 ConsumeToken();
1899 return false;
1900 }
1901
1902 Result.setConstructorTemplateId(TemplateId);
1903 ConsumeToken();
1904 return false;
1905 }
1906
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001907 // We have already parsed a template-id; consume the annotation token as
1908 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001909 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001910 ConsumeToken();
1911 return false;
1912 }
1913
1914 // unqualified-id:
1915 // operator-function-id
1916 // conversion-function-id
1917 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001918 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001919 return true;
1920
Sean Hunte6252d12009-11-28 08:58:14 +00001921 // If we have an operator-function-id or a literal-operator-id and the next
1922 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001923 //
1924 // template-id:
1925 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001926 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1927 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001928 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001929 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1930 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001931 Result,
1932 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001933
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001934 return false;
1935 }
1936
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001937 if (getLang().CPlusPlus &&
1938 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001939 // C++ [expr.unary.op]p10:
1940 // There is an ambiguity in the unary-expression ~X(), where X is a
1941 // class-name. The ambiguity is resolved in favor of treating ~ as a
1942 // unary complement rather than treating ~X as referring to a destructor.
1943
1944 // Parse the '~'.
1945 SourceLocation TildeLoc = ConsumeToken();
1946
1947 // Parse the class-name.
1948 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001949 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001950 return true;
1951 }
1952
1953 // Parse the class-name (or template-name in a simple-template-id).
1954 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1955 SourceLocation ClassNameLoc = ConsumeToken();
1956
Douglas Gregor0278e122010-05-05 05:58:24 +00001957 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001958 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001959 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001960 EnteringContext, ObjectType, Result,
1961 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001962 }
1963
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001964 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001965 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1966 ClassNameLoc, getCurScope(),
1967 SS, ObjectType,
1968 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001969 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001970 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001971
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001972 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001973 return false;
1974 }
1975
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001976 Diag(Tok, diag::err_expected_unqualified_id)
1977 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001978 return true;
1979}
1980
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001981/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1982/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001983///
Chris Lattner59232d32009-01-04 21:25:24 +00001984/// This method is called to parse the new expression after the optional :: has
1985/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1986/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001987///
1988/// new-expression:
1989/// '::'[opt] 'new' new-placement[opt] new-type-id
1990/// new-initializer[opt]
1991/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1992/// new-initializer[opt]
1993///
1994/// new-placement:
1995/// '(' expression-list ')'
1996///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001997/// new-type-id:
1998/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001999/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002000///
2001/// new-declarator:
2002/// ptr-operator new-declarator[opt]
2003/// direct-new-declarator
2004///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002005/// new-initializer:
2006/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002007/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002008///
John McCall60d7b3a2010-08-24 06:29:42 +00002009ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002010Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2011 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2012 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002013
2014 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2015 // second form of new-expression. It can't be a new-type-id.
2016
Sebastian Redla55e52c2008-11-25 22:21:31 +00002017 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002018 SourceLocation PlacementLParen, PlacementRParen;
2019
Douglas Gregor4bd40312010-07-13 15:54:32 +00002020 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002021 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002022 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002023 if (Tok.is(tok::l_paren)) {
2024 // If it turns out to be a placement, we change the type location.
2025 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002026 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2027 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002028 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002029 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002030
2031 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002032 if (PlacementRParen.isInvalid()) {
2033 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002034 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002035 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002036
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002037 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002038 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00002039 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002040 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002041 } else {
2042 // We still need the type.
2043 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00002044 TypeIdParens.setBegin(ConsumeParen());
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002045 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002046 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002047 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002048 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00002049 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
2050 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002051 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002052 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002053 if (ParseCXXTypeSpecifierSeq(DS))
2054 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002055 else {
2056 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002057 ParseDeclaratorInternal(DeclaratorInfo,
2058 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002059 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002060 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002061 }
2062 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002063 // A new-type-id is a simplified type-id, where essentially the
2064 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002065 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002066 if (ParseCXXTypeSpecifierSeq(DS))
2067 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002068 else {
2069 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002070 ParseDeclaratorInternal(DeclaratorInfo,
2071 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002072 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002073 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002074 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002075 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002076 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002077 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002078
Sebastian Redla55e52c2008-11-25 22:21:31 +00002079 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002080 SourceLocation ConstructorLParen, ConstructorRParen;
2081
2082 if (Tok.is(tok::l_paren)) {
2083 ConstructorLParen = ConsumeParen();
2084 if (Tok.isNot(tok::r_paren)) {
2085 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002086 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2087 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002088 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002089 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002090 }
2091 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002092 if (ConstructorRParen.isInvalid()) {
2093 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002094 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002095 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002096 } else if (Tok.is(tok::l_brace)) {
2097 // FIXME: Have to communicate the init-list to ActOnCXXNew.
2098 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002099 }
2100
Sebastian Redlf53597f2009-03-15 17:47:39 +00002101 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2102 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00002103 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002104 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002105}
2106
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002107/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2108/// passed to ParseDeclaratorInternal.
2109///
2110/// direct-new-declarator:
2111/// '[' expression ']'
2112/// direct-new-declarator '[' constant-expression ']'
2113///
Chris Lattner59232d32009-01-04 21:25:24 +00002114void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002115 // Parse the array dimensions.
2116 bool first = true;
2117 while (Tok.is(tok::l_square)) {
2118 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00002119 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002120 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002121 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002122 // Recover
2123 SkipUntil(tok::r_square);
2124 return;
2125 }
2126 first = false;
2127
Sebastian Redlab197ba2009-02-09 18:23:29 +00002128 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall0b7e6782011-03-24 11:26:52 +00002129
2130 ParsedAttributes attrs(AttrFactory);
2131 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002132 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002133 Size.release(), LLoc, RLoc),
John McCall0b7e6782011-03-24 11:26:52 +00002134 attrs, RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002135
Sebastian Redlab197ba2009-02-09 18:23:29 +00002136 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002137 return;
2138 }
2139}
2140
2141/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2142/// This ambiguity appears in the syntax of the C++ new operator.
2143///
2144/// new-expression:
2145/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2146/// new-initializer[opt]
2147///
2148/// new-placement:
2149/// '(' expression-list ')'
2150///
John McCallca0408f2010-08-23 06:44:23 +00002151bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002152 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002153 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002154 // The '(' was already consumed.
2155 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002156 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002157 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002158 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002159 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002160 }
2161
2162 // It's not a type, it has to be an expression list.
2163 // Discard the comma locations - ActOnCXXNew has enough parameters.
2164 CommaLocsTy CommaLocs;
2165 return ParseExpressionList(PlacementArgs, CommaLocs);
2166}
2167
2168/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2169/// to free memory allocated by new.
2170///
Chris Lattner59232d32009-01-04 21:25:24 +00002171/// This method is called to parse the 'delete' expression after the optional
2172/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2173/// and "Start" is its location. Otherwise, "Start" is the location of the
2174/// 'delete' token.
2175///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002176/// delete-expression:
2177/// '::'[opt] 'delete' cast-expression
2178/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002179ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002180Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2181 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2182 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002183
2184 // Array delete?
2185 bool ArrayDelete = false;
2186 if (Tok.is(tok::l_square)) {
2187 ArrayDelete = true;
2188 SourceLocation LHS = ConsumeBracket();
2189 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
2190 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002191 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002192 }
2193
John McCall60d7b3a2010-08-24 06:29:42 +00002194 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002195 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002196 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002197
John McCall9ae2f072010-08-23 23:25:46 +00002198 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002199}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002200
Mike Stump1eb44332009-09-09 15:08:12 +00002201static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002202 switch(kind) {
John Wiegley20c0da72011-04-27 23:09:49 +00002203 default: assert(false && "Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002204 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002205 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002206 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002207 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002208 case tok::kw___has_trivial_constructor:
2209 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002210 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002211 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2212 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2213 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002214 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2215 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002216 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002217 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2218 case tok::kw___is_compound: return UTT_IsCompound;
2219 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002220 case tok::kw___is_empty: return UTT_IsEmpty;
2221 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley20c0da72011-04-27 23:09:49 +00002222 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2223 case tok::kw___is_function: return UTT_IsFunction;
2224 case tok::kw___is_fundamental: return UTT_IsFundamental;
2225 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00002226 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2227 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2228 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2229 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2230 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002231 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002232 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002233 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002234 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002235 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002236 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002237 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2238 case tok::kw___is_scalar: return UTT_IsScalar;
2239 case tok::kw___is_signed: return UTT_IsSigned;
2240 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2241 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002242 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002243 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002244 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2245 case tok::kw___is_void: return UTT_IsVoid;
2246 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002247 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002248}
2249
2250static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2251 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002252 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002253 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002254 case tok::kw___is_convertible: return BTT_IsConvertible;
2255 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002256 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002257 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002258 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002259}
2260
John Wiegley21ff2e52011-04-28 00:16:57 +00002261static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2262 switch(kind) {
2263 default: llvm_unreachable("Not a known binary type trait");
2264 case tok::kw___array_rank: return ATT_ArrayRank;
2265 case tok::kw___array_extent: return ATT_ArrayExtent;
2266 }
2267}
2268
John Wiegley55262202011-04-25 06:54:41 +00002269static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2270 switch(kind) {
2271 default: assert(false && "Not a known unary expression trait.");
2272 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2273 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2274 }
2275}
2276
Sebastian Redl64b45f72009-01-05 20:52:13 +00002277/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2278/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2279/// templates.
2280///
2281/// primary-expression:
2282/// [GNU] unary-type-trait '(' type-id ')'
2283///
John McCall60d7b3a2010-08-24 06:29:42 +00002284ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002285 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2286 SourceLocation Loc = ConsumeToken();
2287
2288 SourceLocation LParen = Tok.getLocation();
2289 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2290 return ExprError();
2291
2292 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2293 // there will be cryptic errors about mismatched parentheses and missing
2294 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002295 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002296
2297 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2298
Douglas Gregor809070a2009-02-18 17:45:20 +00002299 if (Ty.isInvalid())
2300 return ExprError();
2301
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002302 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00002303}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002304
Francois Pichet6ad6f282010-12-07 00:08:36 +00002305/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2306/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2307/// templates.
2308///
2309/// primary-expression:
2310/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2311///
2312ExprResult Parser::ParseBinaryTypeTrait() {
2313 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2314 SourceLocation Loc = ConsumeToken();
2315
2316 SourceLocation LParen = Tok.getLocation();
2317 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2318 return ExprError();
2319
2320 TypeResult LhsTy = ParseTypeName();
2321 if (LhsTy.isInvalid()) {
2322 SkipUntil(tok::r_paren);
2323 return ExprError();
2324 }
2325
2326 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2327 SkipUntil(tok::r_paren);
2328 return ExprError();
2329 }
2330
2331 TypeResult RhsTy = ParseTypeName();
2332 if (RhsTy.isInvalid()) {
2333 SkipUntil(tok::r_paren);
2334 return ExprError();
2335 }
2336
2337 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2338
2339 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
2340}
2341
John Wiegley21ff2e52011-04-28 00:16:57 +00002342/// ParseArrayTypeTrait - Parse the built-in array type-trait
2343/// pseudo-functions.
2344///
2345/// primary-expression:
2346/// [Embarcadero] '__array_rank' '(' type-id ')'
2347/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2348///
2349ExprResult Parser::ParseArrayTypeTrait() {
2350 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2351 SourceLocation Loc = ConsumeToken();
2352
2353 SourceLocation LParen = Tok.getLocation();
2354 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2355 return ExprError();
2356
2357 TypeResult Ty = ParseTypeName();
2358 if (Ty.isInvalid()) {
2359 SkipUntil(tok::comma);
2360 SkipUntil(tok::r_paren);
2361 return ExprError();
2362 }
2363
2364 switch (ATT) {
2365 case ATT_ArrayRank: {
2366 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2367 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, RParen);
2368 }
2369 case ATT_ArrayExtent: {
2370 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2371 SkipUntil(tok::r_paren);
2372 return ExprError();
2373 }
2374
2375 ExprResult DimExpr = ParseExpression();
2376 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2377
2378 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), RParen);
2379 }
2380 default:
2381 break;
2382 }
2383 return ExprError();
2384}
2385
John Wiegley55262202011-04-25 06:54:41 +00002386/// ParseExpressionTrait - Parse built-in expression-trait
2387/// pseudo-functions like __is_lvalue_expr( xxx ).
2388///
2389/// primary-expression:
2390/// [Embarcadero] expression-trait '(' expression ')'
2391///
2392ExprResult Parser::ParseExpressionTrait() {
2393 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2394 SourceLocation Loc = ConsumeToken();
2395
2396 SourceLocation LParen = Tok.getLocation();
2397 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2398 return ExprError();
2399
2400 ExprResult Expr = ParseExpression();
2401
2402 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2403
2404 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), RParen);
2405}
2406
2407
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002408/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2409/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2410/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002411ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002412Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002413 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002414 SourceLocation LParenLoc,
2415 SourceLocation &RParenLoc) {
2416 assert(getLang().CPlusPlus && "Should only be called for C++!");
2417 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2418 assert(isTypeIdInParens() && "Not a type-id!");
2419
John McCall60d7b3a2010-08-24 06:29:42 +00002420 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002421 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002422
2423 // We need to disambiguate a very ugly part of the C++ syntax:
2424 //
2425 // (T())x; - type-id
2426 // (T())*x; - type-id
2427 // (T())/x; - expression
2428 // (T()); - expression
2429 //
2430 // The bad news is that we cannot use the specialized tentative parser, since
2431 // it can only verify that the thing inside the parens can be parsed as
2432 // type-id, it is not useful for determining the context past the parens.
2433 //
2434 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002435 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002436 //
2437 // It uses a scheme similar to parsing inline methods. The parenthesized
2438 // tokens are cached, the context that follows is determined (possibly by
2439 // parsing a cast-expression), and then we re-introduce the cached tokens
2440 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002441
Mike Stump1eb44332009-09-09 15:08:12 +00002442 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002443 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002444
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002445 // Store the tokens of the parentheses. We will parse them after we determine
2446 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002447 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002448 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002449 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2450 return ExprError();
2451 }
2452
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002453 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002454 ParseAs = CompoundLiteral;
2455 } else {
2456 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002457 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2458 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2459 NotCastExpr = true;
2460 } else {
2461 // Try parsing the cast-expression that may follow.
2462 // If it is not a cast-expression, NotCastExpr will be true and no token
2463 // will be consumed.
2464 Result = ParseCastExpression(false/*isUnaryExpression*/,
2465 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002466 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002467 // type-id has priority.
2468 true/*isTypeCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002469 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002470
2471 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2472 // an expression.
2473 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002474 }
2475
Mike Stump1eb44332009-09-09 15:08:12 +00002476 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002477 Toks.push_back(Tok);
2478 // Re-enter the stored parenthesized tokens into the token stream, so we may
2479 // parse them now.
2480 PP.EnterTokenStream(Toks.data(), Toks.size(),
2481 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2482 // Drop the current token and bring the first cached one. It's the same token
2483 // as when we entered this function.
2484 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002485
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002486 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002487 // Parse the type declarator.
2488 DeclSpec DS(AttrFactory);
2489 ParseSpecifierQualifierList(DS);
2490 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2491 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002492
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002493 // Match the ')'.
2494 if (Tok.is(tok::r_paren))
2495 RParenLoc = ConsumeParen();
2496 else
2497 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002498
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002499 if (ParseAs == CompoundLiteral) {
2500 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002501 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002502 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2503 }
Mike Stump1eb44332009-09-09 15:08:12 +00002504
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002505 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2506 assert(ParseAs == CastExpr);
2507
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002508 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002509 return ExprError();
2510
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002511 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002512 if (!Result.isInvalid())
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002513 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc,
2514 DeclaratorInfo, CastTy,
2515 RParenLoc, Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002516 return move(Result);
2517 }
Mike Stump1eb44332009-09-09 15:08:12 +00002518
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002519 // Not a compound literal, and not followed by a cast-expression.
2520 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002521
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002522 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002523 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002524 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002525 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002526
2527 // Match the ')'.
2528 if (Result.isInvalid()) {
2529 SkipUntil(tok::r_paren);
2530 return ExprError();
2531 }
Mike Stump1eb44332009-09-09 15:08:12 +00002532
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002533 if (Tok.is(tok::r_paren))
2534 RParenLoc = ConsumeParen();
2535 else
2536 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2537
2538 return move(Result);
2539}