blob: 757d86e5d609f66de6bd6b57fd098a38b4576050 [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
Mike Stump1eb44332009-09-09 15:08:12 +000073/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000074///
75/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000076/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000077/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000078///
79/// '::'[opt] nested-name-specifier
80/// '::'
81///
82/// nested-name-specifier:
83/// type-name '::'
84/// namespace-name '::'
85/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000086/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000087///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000088///
Mike Stump1eb44332009-09-09 15:08:12 +000089/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000090/// nested-name-specifier (or empty)
91///
Mike Stump1eb44332009-09-09 15:08:12 +000092/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000093/// the "." or "->" of a member access expression, this parameter provides the
94/// type of the object whose members are being accessed.
95///
96/// \param EnteringContext whether we will be entering into the context of
97/// the nested-name-specifier after parsing it.
98///
Douglas Gregord4dca082010-02-24 18:44:31 +000099/// \param MayBePseudoDestructor When non-NULL, points to a flag that
100/// indicates whether this nested-name-specifier may be part of a
101/// pseudo-destructor name. In this case, the flag will be set false
102/// if we don't actually end up parsing a destructor name. Moreorover,
103/// if we do end up determining that we are parsing a destructor name,
104/// the last component of the nested-name-specifier is not parsed as
105/// part of the scope specifier.
106
Douglas Gregorb10cd042010-02-21 18:36:56 +0000107/// member access expression, e.g., the \p T:: in \p p->T::m.
108///
John McCall9ba61662010-02-26 08:45:28 +0000109/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000110bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000111 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000112 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000113 bool *MayBePseudoDestructor,
114 bool IsTypename) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000115 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000116 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000118 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc34348a2011-02-24 17:54:50 +0000119 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
120 Tok.getAnnotationRange(),
121 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000122 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000123 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000124 }
Chris Lattnere607e802009-01-04 21:14:15 +0000125
Douglas Gregor39a8de12009-02-25 19:37:18 +0000126 bool HasScopeSpecifier = false;
127
Chris Lattner5b454732009-01-05 03:55:46 +0000128 if (Tok.is(tok::coloncolon)) {
129 // ::new and ::delete aren't nested-name-specifiers.
130 tok::TokenKind NextKind = NextToken().getKind();
131 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
132 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattner55a7cef2009-01-05 00:13:00 +0000134 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000135 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
136 return true;
137
Douglas Gregor39a8de12009-02-25 19:37:18 +0000138 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000139 }
140
Douglas Gregord4dca082010-02-24 18:44:31 +0000141 bool CheckForDestructor = false;
142 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
143 CheckForDestructor = true;
144 *MayBePseudoDestructor = false;
145 }
146
Douglas Gregor39a8de12009-02-25 19:37:18 +0000147 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000148 if (HasScopeSpecifier) {
149 // C++ [basic.lookup.classref]p5:
150 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000151 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000152 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000153 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000154 // the class-name-or-namespace-name is looked up in global scope as a
155 // class-name or namespace-name.
156 //
157 // To implement this, we clear out the object type as soon as we've
158 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000159 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000160
161 if (Tok.is(tok::code_completion)) {
162 // Code completion for a nested-name-specifier, where the code
163 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000164 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000165 SourceLocation ccLoc = ConsumeCodeCompletionToken();
166 // Include code completion token into the range of the scope otherwise
167 // when we try to annotate the scope tokens the dangling code completion
168 // token will cause assertion in
169 // Preprocessor::AnnotatePreviousCachedTokens.
170 SS.setEndLoc(ccLoc);
Douglas Gregor81b747b2009-09-17 21:32:03 +0000171 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Douglas Gregor39a8de12009-02-25 19:37:18 +0000174 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000175 // nested-name-specifier 'template'[opt] simple-template-id '::'
176
177 // Parse the optional 'template' keyword, then make sure we have
178 // 'identifier <' after it.
179 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000180 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000181 // nested-name-specifier, since they aren't allowed to start with
182 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000183 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000184 break;
185
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000186 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000187 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000188
189 UnqualifiedId TemplateName;
190 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000191 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000192 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000193 ConsumeToken();
194 } else if (Tok.is(tok::kw_operator)) {
195 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000196 TemplateName)) {
197 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000198 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000199 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000200
Sean Hunte6252d12009-11-28 08:58:14 +0000201 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
202 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000203 Diag(TemplateName.getSourceRange().getBegin(),
204 diag::err_id_after_template_in_nested_name_spec)
205 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000206 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000207 break;
208 }
209 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000210 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000211 break;
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000214 // If the next token is not '<', we have a qualified-id that refers
215 // to a template name, such as T::template apply, but is not a
216 // template-id.
217 if (Tok.isNot(tok::less)) {
218 TPA.Revert();
219 break;
220 }
221
222 // Commit to parsing the template-id.
223 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000224 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000225 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000226 TemplateKWLoc,
227 SS,
228 TemplateName,
229 ObjectType,
230 EnteringContext,
231 Template)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000232 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000233 TemplateKWLoc, false))
234 return true;
235 } else
John McCall9ba61662010-02-26 08:45:28 +0000236 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattner77cf72a2009-06-26 03:47:46 +0000238 continue;
239 }
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Douglas Gregor39a8de12009-02-25 19:37:18 +0000241 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000242 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000243 //
244 // simple-template-id '::'
245 //
246 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000247 // right kind (it should name a type or be dependent), and then
248 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000249 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000250 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
251 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000252 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000253 }
254
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000255 // Consume the template-id token.
256 ConsumeToken();
257
258 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
259 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000261 if (!HasScopeSpecifier)
262 HasScopeSpecifier = true;
263
264 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
265 TemplateId->getTemplateArgs(),
266 TemplateId->NumArgs);
267
268 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
269 /*FIXME:*/SourceLocation(),
270 SS,
271 TemplateId->Template,
272 TemplateId->TemplateNameLoc,
273 TemplateId->LAngleLoc,
274 TemplateArgsPtr,
275 TemplateId->RAngleLoc,
276 CCLoc,
277 EnteringContext)) {
278 SourceLocation StartLoc
279 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
280 : TemplateId->TemplateNameLoc;
281 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000282 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000283
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000284 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000285 }
286
Chris Lattner5c7f7862009-06-26 03:52:38 +0000287
288 // The rest of the nested-name-specifier possibilities start with
289 // tok::identifier.
290 if (Tok.isNot(tok::identifier))
291 break;
292
293 IdentifierInfo &II = *Tok.getIdentifierInfo();
294
295 // nested-name-specifier:
296 // type-name '::'
297 // namespace-name '::'
298 // nested-name-specifier identifier '::'
299 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000300
301 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
302 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000303 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000304 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
305 Tok.getLocation(),
306 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000307 EnteringContext) &&
308 // If the token after the colon isn't an identifier, it's still an
309 // error, but they probably meant something else strange so don't
310 // recover like this.
311 PP.LookAhead(1).is(tok::identifier)) {
312 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000313 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000314
315 // Recover as if the user wrote '::'.
316 Next.setKind(tok::coloncolon);
317 }
Chris Lattner46646492009-12-07 01:36:53 +0000318 }
319
Chris Lattner5c7f7862009-06-26 03:52:38 +0000320 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000321 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000322 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000323 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000324 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000325 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000326 }
327
Chris Lattner5c7f7862009-06-26 03:52:38 +0000328 // We have an identifier followed by a '::'. Lookup this name
329 // as the name in a nested-name-specifier.
330 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000331 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
332 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000333 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000335 HasScopeSpecifier = true;
336 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
337 ObjectType, EnteringContext, SS))
338 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
339
Chris Lattner5c7f7862009-06-26 03:52:38 +0000340 continue;
341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Richard Smithea698b32011-04-14 21:45:45 +0000343 // Check for '<::' which should be '< ::' instead of '[:' when following
344 // a template name.
345 if (Next.is(tok::l_square) && Next.getLength() == 2) {
346 Token SecondToken = GetLookAheadToken(2);
347 if (SecondToken.is(tok::colon) &&
348 AreTokensAdjacent(PP, Next, SecondToken)) {
349 TemplateTy Template;
350 UnqualifiedId TemplateName;
351 TemplateName.setIdentifier(&II, Tok.getLocation());
352 bool MemberOfUnknownSpecialization;
353 if (Actions.isTemplateName(getCurScope(), SS,
354 /*hasTemplateKeyword=*/false,
355 TemplateName,
356 ObjectType,
357 EnteringContext,
358 Template,
359 MemberOfUnknownSpecialization)) {
360 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
361 /*AtDigraph*/false);
362 }
363 }
364 }
365
Chris Lattner5c7f7862009-06-26 03:52:38 +0000366 // nested-name-specifier:
367 // type-name '<'
368 if (Next.is(tok::less)) {
369 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000370 UnqualifiedId TemplateName;
371 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000372 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000373 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000374 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000375 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000376 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000377 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000378 Template,
379 MemberOfUnknownSpecialization)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000380 // We have found a template name, so annotate this this token
381 // with a template-id annotation. We do not permit the
382 // template-id to be translated into a type annotation,
383 // because some clients (e.g., the parsing of class template
384 // specializations) still want to see the original template-id
385 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000386 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000387 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000388 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000389 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000390 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000391 }
392
393 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000394 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000395 // We have something like t::getAs<T>, where getAs is a
396 // member of an unknown specialization. However, this will only
397 // parse correctly as a template, so suggest the keyword 'template'
398 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000399 unsigned DiagID = diag::err_missing_dependent_template_keyword;
400 if (getLang().Microsoft)
Francois Pichetcf320c62011-04-22 08:25:24 +0000401 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000402
403 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000404 << II.getName()
405 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
406
Douglas Gregord6ab2322010-06-16 23:00:59 +0000407 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000408 = Actions.ActOnDependentTemplateName(getCurScope(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000409 Tok.getLocation(), SS,
410 TemplateName, ObjectType,
411 EnteringContext, Template)) {
412 // Consume the identifier.
413 ConsumeToken();
Douglas Gregor059101f2011-03-02 00:47:37 +0000414 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
Douglas Gregord6ab2322010-06-16 23:00:59 +0000415 SourceLocation(), false))
416 return true;
417 }
418 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000419 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000420
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000421 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000422 }
423 }
424
Douglas Gregor39a8de12009-02-25 19:37:18 +0000425 // We don't have any tokens that form the beginning of a
426 // nested-name-specifier, so we're done.
427 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000428 }
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Douglas Gregord4dca082010-02-24 18:44:31 +0000430 // Even if we didn't see any pieces of a nested-name-specifier, we
431 // still check whether there is a tilde in this position, which
432 // indicates a potential pseudo-destructor.
433 if (CheckForDestructor && Tok.is(tok::tilde))
434 *MayBePseudoDestructor = true;
435
John McCall9ba61662010-02-26 08:45:28 +0000436 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000437}
438
439/// ParseCXXIdExpression - Handle id-expression.
440///
441/// id-expression:
442/// unqualified-id
443/// qualified-id
444///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000445/// qualified-id:
446/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
447/// '::' identifier
448/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000449/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000450///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000451/// NOTE: The standard specifies that, for qualified-id, the parser does not
452/// expect:
453///
454/// '::' conversion-function-id
455/// '::' '~' class-name
456///
457/// This may cause a slight inconsistency on diagnostics:
458///
459/// class C {};
460/// namespace A {}
461/// void f() {
462/// :: A :: ~ C(); // Some Sema error about using destructor with a
463/// // namespace.
464/// :: ~ C(); // Some Parser error like 'unexpected ~'.
465/// }
466///
467/// We simplify the parser a bit and make it work like:
468///
469/// qualified-id:
470/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
471/// '::' unqualified-id
472///
473/// That way Sema can handle and report similar errors for namespaces and the
474/// global scope.
475///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000476/// The isAddressOfOperand parameter indicates that this id-expression is a
477/// direct operand of the address-of operator. This is, besides member contexts,
478/// the only place where a qualified-id naming a non-static class member may
479/// appear.
480///
John McCall60d7b3a2010-08-24 06:29:42 +0000481ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000482 // qualified-id:
483 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
484 // '::' unqualified-id
485 //
486 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +0000487 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000488
489 UnqualifiedId Name;
490 if (ParseUnqualifiedId(SS,
491 /*EnteringContext=*/false,
492 /*AllowDestructorName=*/false,
493 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000494 /*ObjectType=*/ ParsedType(),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000495 Name))
496 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000497
498 // This is only the direct operand of an & operator if it is not
499 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000500 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
501 isAddressOfOperand = false;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000502
Douglas Gregor23c94db2010-07-02 17:43:08 +0000503 return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren),
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000504 isAddressOfOperand);
505
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000506}
507
Douglas Gregorae7902c2011-08-04 15:30:47 +0000508/// ParseLambdaExpression - Parse a C++0x lambda expression.
509///
510/// lambda-expression:
511/// lambda-introducer lambda-declarator[opt] compound-statement
512///
513/// lambda-introducer:
514/// '[' lambda-capture[opt] ']'
515///
516/// lambda-capture:
517/// capture-default
518/// capture-list
519/// capture-default ',' capture-list
520///
521/// capture-default:
522/// '&'
523/// '='
524///
525/// capture-list:
526/// capture
527/// capture-list ',' capture
528///
529/// capture:
530/// identifier
531/// '&' identifier
532/// 'this'
533///
534/// lambda-declarator:
535/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
536/// 'mutable'[opt] exception-specification[opt]
537/// trailing-return-type[opt]
538///
539ExprResult Parser::ParseLambdaExpression() {
540 // Parse lambda-introducer.
541 LambdaIntroducer Intro;
542
543 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
544 if (DiagID) {
545 Diag(Tok, DiagID.getValue());
546 SkipUntil(tok::r_square);
547 }
548
549 return ParseLambdaExpressionAfterIntroducer(Intro);
550}
551
552/// TryParseLambdaExpression - Use lookahead and potentially tentative
553/// parsing to determine if we are looking at a C++0x lambda expression, and parse
554/// it if we are.
555///
556/// If we are not looking at a lambda expression, returns ExprError().
557ExprResult Parser::TryParseLambdaExpression() {
558 assert(getLang().CPlusPlus0x
559 && Tok.is(tok::l_square)
560 && "Not at the start of a possible lambda expression.");
561
562 const Token Next = NextToken(), After = GetLookAheadToken(2);
563
564 // If lookahead indicates this is a lambda...
565 if (Next.is(tok::r_square) || // []
566 Next.is(tok::equal) || // [=
567 (Next.is(tok::amp) && // [&] or [&,
568 (After.is(tok::r_square) ||
569 After.is(tok::comma))) ||
570 (Next.is(tok::identifier) && // [identifier]
571 After.is(tok::r_square))) {
572 return ParseLambdaExpression();
573 }
574
575 // If lookahead indicates this is an Objective-C message...
576 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
577 return ExprError();
578 }
579
580 LambdaIntroducer Intro;
581 if (TryParseLambdaIntroducer(Intro))
582 return ExprError();
583 return ParseLambdaExpressionAfterIntroducer(Intro);
584}
585
586/// ParseLambdaExpression - Parse a lambda introducer.
587///
588/// Returns a DiagnosticID if it hit something unexpected.
589llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
590 typedef llvm::Optional<unsigned> DiagResult;
591
592 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
593 Intro.Range.setBegin(ConsumeBracket());
594
595 bool first = true;
596
597 // Parse capture-default.
598 if (Tok.is(tok::amp) &&
599 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
600 Intro.Default = LCD_ByRef;
601 ConsumeToken();
602 first = false;
603 } else if (Tok.is(tok::equal)) {
604 Intro.Default = LCD_ByCopy;
605 ConsumeToken();
606 first = false;
607 }
608
609 while (Tok.isNot(tok::r_square)) {
610 if (!first) {
611 if (Tok.isNot(tok::comma))
612 return DiagResult(diag::err_expected_comma_or_rsquare);
613 ConsumeToken();
614 }
615
616 first = false;
617
618 // Parse capture.
619 LambdaCaptureKind Kind = LCK_ByCopy;
620 SourceLocation Loc;
621 IdentifierInfo* Id = 0;
622
623 if (Tok.is(tok::kw_this)) {
624 Kind = LCK_This;
625 Loc = ConsumeToken();
626 } else {
627 if (Tok.is(tok::amp)) {
628 Kind = LCK_ByRef;
629 ConsumeToken();
630 }
631
632 if (Tok.is(tok::identifier)) {
633 Id = Tok.getIdentifierInfo();
634 Loc = ConsumeToken();
635 } else if (Tok.is(tok::kw_this)) {
636 // FIXME: If we want to suggest a fixit here, will need to return more
637 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
638 // Clear()ed to prevent emission in case of tentative parsing?
639 return DiagResult(diag::err_this_captured_by_reference);
640 } else {
641 return DiagResult(diag::err_expected_capture);
642 }
643 }
644
645 Intro.addCapture(Kind, Loc, Id);
646 }
647
648 Intro.Range.setEnd(MatchRHSPunctuation(tok::r_square,
649 Intro.Range.getBegin()));
650
651 return DiagResult();
652}
653
654/// TryParseLambdaExpression - Tentatively parse a lambda introducer.
655///
656/// Returns true if it hit something unexpected.
657bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
658 TentativeParsingAction PA(*this);
659
660 llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
661
662 if (DiagID) {
663 PA.Revert();
664 return true;
665 }
666
667 PA.Commit();
668 return false;
669}
670
671/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
672/// expression.
673ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
674 LambdaIntroducer &Intro) {
675 // Parse lambda-declarator[opt].
676 DeclSpec DS(AttrFactory);
677 Declarator D(DS, Declarator::PrototypeContext);
678
679 if (Tok.is(tok::l_paren)) {
680 ParseScope PrototypeScope(this,
681 Scope::FunctionPrototypeScope |
682 Scope::DeclScope);
683
684 SourceLocation DeclLoc = ConsumeParen(), DeclEndLoc;
685
686 // Parse parameter-declaration-clause.
687 ParsedAttributes Attr(AttrFactory);
688 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
689 SourceLocation EllipsisLoc;
690
691 if (Tok.isNot(tok::r_paren))
692 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
693
694 DeclEndLoc = MatchRHSPunctuation(tok::r_paren, DeclLoc);
695
696 // Parse 'mutable'[opt].
697 SourceLocation MutableLoc;
698 if (Tok.is(tok::kw_mutable)) {
699 MutableLoc = ConsumeToken();
700 DeclEndLoc = MutableLoc;
701 }
702
703 // Parse exception-specification[opt].
704 ExceptionSpecificationType ESpecType = EST_None;
705 SourceRange ESpecRange;
706 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
707 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
708 ExprResult NoexceptExpr;
709 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
710 DynamicExceptions,
711 DynamicExceptionRanges,
712 NoexceptExpr);
713
714 if (ESpecType != EST_None)
715 DeclEndLoc = ESpecRange.getEnd();
716
717 // Parse attribute-specifier[opt].
718 MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
719
720 // Parse trailing-return-type[opt].
721 ParsedType TrailingReturnType;
722 if (Tok.is(tok::arrow)) {
723 SourceRange Range;
724 TrailingReturnType = ParseTrailingReturnType(Range).get();
725 if (Range.getEnd().isValid())
726 DeclEndLoc = Range.getEnd();
727 }
728
729 PrototypeScope.Exit();
730
731 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
732 /*isVariadic=*/EllipsisLoc.isValid(),
733 EllipsisLoc,
734 ParamInfo.data(), ParamInfo.size(),
735 DS.getTypeQualifiers(),
736 /*RefQualifierIsLValueRef=*/true,
737 /*RefQualifierLoc=*/SourceLocation(),
738 MutableLoc,
739 ESpecType, ESpecRange.getBegin(),
740 DynamicExceptions.data(),
741 DynamicExceptionRanges.data(),
742 DynamicExceptions.size(),
743 NoexceptExpr.isUsable() ?
744 NoexceptExpr.get() : 0,
745 DeclLoc, DeclEndLoc, D,
746 TrailingReturnType),
747 Attr, DeclEndLoc);
748 }
749
750 // Parse compound-statement.
751 if (Tok.is(tok::l_brace)) {
752 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
753 // it.
754 ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
755 Scope::BreakScope | Scope::ContinueScope |
756 Scope::DeclScope);
757
758 StmtResult Stmt(ParseCompoundStatementBody());
759
760 BodyScope.Exit();
761 } else {
762 Diag(Tok, diag::err_expected_lambda_body);
763 }
764
765 return ExprEmpty();
766}
767
Reid Spencer5f016e22007-07-11 17:01:13 +0000768/// ParseCXXCasts - This handles the various ways to cast expressions to another
769/// type.
770///
771/// postfix-expression: [C++ 5.2p1]
772/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
773/// 'static_cast' '<' type-name '>' '(' expression ')'
774/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
775/// 'const_cast' '<' type-name '>' '(' expression ')'
776///
John McCall60d7b3a2010-08-24 06:29:42 +0000777ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 tok::TokenKind Kind = Tok.getKind();
779 const char *CastName = 0; // For error messages
780
781 switch (Kind) {
782 default: assert(0 && "Unknown C++ cast!"); abort();
783 case tok::kw_const_cast: CastName = "const_cast"; break;
784 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
785 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
786 case tok::kw_static_cast: CastName = "static_cast"; break;
787 }
788
789 SourceLocation OpLoc = ConsumeToken();
790 SourceLocation LAngleBracketLoc = Tok.getLocation();
791
Richard Smithea698b32011-04-14 21:45:45 +0000792 // Check for "<::" which is parsed as "[:". If found, fix token stream,
793 // diagnose error, suggest fix, and recover parsing.
794 Token Next = NextToken();
795 if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) &&
796 AreTokensAdjacent(PP, Tok, Next))
797 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
798
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000800 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000801
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000802 // Parse the common declaration-specifiers piece.
803 DeclSpec DS(AttrFactory);
804 ParseSpecifierQualifierList(DS);
805
806 // Parse the abstract-declarator, if present.
807 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
808 ParseDeclarator(DeclaratorInfo);
809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 SourceLocation RAngleBracketLoc = Tok.getLocation();
811
Chris Lattner1ab3b962008-11-18 07:48:38 +0000812 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000813 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000814
815 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
816
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000817 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
818 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000819
John McCall60d7b3a2010-08-24 06:29:42 +0000820 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000822 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000823 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000824
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000825 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +0000826 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000827 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +0000828 RAngleBracketLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000829 LParenLoc, Result.take(), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000830
Sebastian Redl20df9b72008-12-11 22:51:44 +0000831 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000832}
833
Sebastian Redlc42e1182008-11-11 11:37:55 +0000834/// ParseCXXTypeid - This handles the C++ typeid expression.
835///
836/// postfix-expression: [C++ 5.2p1]
837/// 'typeid' '(' expression ')'
838/// 'typeid' '(' type-id ')'
839///
John McCall60d7b3a2010-08-24 06:29:42 +0000840ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000841 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
842
843 SourceLocation OpLoc = ConsumeToken();
844 SourceLocation LParenLoc = Tok.getLocation();
845 SourceLocation RParenLoc;
846
847 // typeid expressions are always parenthesized.
848 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
849 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000850 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000851
John McCall60d7b3a2010-08-24 06:29:42 +0000852 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000853
854 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000855 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000856
857 // Match the ')'.
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000858 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000859
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000860 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000861 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000862
863 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000864 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000865 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000866 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000867 // When typeid is applied to an expression other than an lvalue of a
868 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000869 // operand (Clause 5).
870 //
Mike Stump1eb44332009-09-09 15:08:12 +0000871 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000872 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000873 // we the expression is potentially potentially evaluated.
874 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000875 Sema::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000876 Result = ParseExpression();
877
878 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000879 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000880 SkipUntil(tok::r_paren);
881 else {
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000882 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
883 if (RParenLoc.isInvalid())
884 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +0000885
886 // If we are a foo<int> that identifies a single function, resolve it now...
887 Expr* e = Result.get();
888 if (e->getType() == Actions.Context.OverloadTy) {
889 ExprResult er =
890 Actions.ResolveAndFixSingleFunctionTemplateSpecialization(e);
891 if (er.isUsable())
892 Result = er.release();
893 }
Sebastian Redlc42e1182008-11-11 11:37:55 +0000894 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000895 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000896 }
897 }
898
Sebastian Redl20df9b72008-12-11 22:51:44 +0000899 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000900}
901
Francois Pichet01b7c302010-09-08 12:20:18 +0000902/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
903///
904/// '__uuidof' '(' expression ')'
905/// '__uuidof' '(' type-id ')'
906///
907ExprResult Parser::ParseCXXUuidof() {
908 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
909
910 SourceLocation OpLoc = ConsumeToken();
911 SourceLocation LParenLoc = Tok.getLocation();
912 SourceLocation RParenLoc;
913
914 // __uuidof expressions are always parenthesized.
915 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
916 "__uuidof"))
917 return ExprError();
918
919 ExprResult Result;
920
921 if (isTypeIdInParens()) {
922 TypeResult Ty = ParseTypeName();
923
924 // Match the ')'.
925 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
926
927 if (Ty.isInvalid())
928 return ExprError();
929
930 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/true,
931 Ty.get().getAsOpaquePtr(), RParenLoc);
932 } else {
933 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
934 Result = ParseExpression();
935
936 // Match the ')'.
937 if (Result.isInvalid())
938 SkipUntil(tok::r_paren);
939 else {
940 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
941
942 Result = Actions.ActOnCXXUuidof(OpLoc, LParenLoc, /*isType=*/false,
943 Result.release(), RParenLoc);
944 }
945 }
946
947 return move(Result);
948}
949
Douglas Gregord4dca082010-02-24 18:44:31 +0000950/// \brief Parse a C++ pseudo-destructor expression after the base,
951/// . or -> operator, and nested-name-specifier have already been
952/// parsed.
953///
954/// postfix-expression: [C++ 5.2]
955/// postfix-expression . pseudo-destructor-name
956/// postfix-expression -> pseudo-destructor-name
957///
958/// pseudo-destructor-name:
959/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
960/// ::[opt] nested-name-specifier template simple-template-id ::
961/// ~type-name
962/// ::[opt] nested-name-specifier[opt] ~type-name
963///
John McCall60d7b3a2010-08-24 06:29:42 +0000964ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +0000965Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
966 tok::TokenKind OpKind,
967 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000968 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000969 // We're parsing either a pseudo-destructor-name or a dependent
970 // member access that has the same form as a
971 // pseudo-destructor-name. We parse both in the same way and let
972 // the action model sort them out.
973 //
974 // Note that the ::[opt] nested-name-specifier[opt] has already
975 // been parsed, and if there was a simple-template-id, it has
976 // been coalesced into a template-id annotation token.
977 UnqualifiedId FirstTypeName;
978 SourceLocation CCLoc;
979 if (Tok.is(tok::identifier)) {
980 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
981 ConsumeToken();
982 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
983 CCLoc = ConsumeToken();
984 } else if (Tok.is(tok::annot_template_id)) {
985 FirstTypeName.setTemplateId(
986 (TemplateIdAnnotation *)Tok.getAnnotationValue());
987 ConsumeToken();
988 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
989 CCLoc = ConsumeToken();
990 } else {
991 FirstTypeName.setIdentifier(0, SourceLocation());
992 }
993
994 // Parse the tilde.
995 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
996 SourceLocation TildeLoc = ConsumeToken();
997 if (!Tok.is(tok::identifier)) {
998 Diag(Tok, diag::err_destructor_tilde_identifier);
999 return ExprError();
1000 }
1001
1002 // Parse the second type.
1003 UnqualifiedId SecondTypeName;
1004 IdentifierInfo *Name = Tok.getIdentifierInfo();
1005 SourceLocation NameLoc = ConsumeToken();
1006 SecondTypeName.setIdentifier(Name, NameLoc);
1007
1008 // If there is a '<', the second type name is a template-id. Parse
1009 // it as such.
1010 if (Tok.is(tok::less) &&
1011 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001012 SecondTypeName, /*AssumeTemplateName=*/true,
1013 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +00001014 return ExprError();
1015
John McCall9ae2f072010-08-23 23:25:46 +00001016 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1017 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001018 SS, FirstTypeName, CCLoc,
1019 TildeLoc, SecondTypeName,
1020 Tok.is(tok::l_paren));
1021}
1022
Reid Spencer5f016e22007-07-11 17:01:13 +00001023/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1024///
1025/// boolean-literal: [C++ 2.13.5]
1026/// 'true'
1027/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001028ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001030 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001031}
Chris Lattner50dd2892008-02-26 00:51:44 +00001032
1033/// ParseThrowExpression - This handles the C++ throw expression.
1034///
1035/// throw-expression: [C++ 15]
1036/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001037ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001038 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001039 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001040
Chris Lattner2a2819a2008-04-06 06:02:23 +00001041 // If the current token isn't the start of an assignment-expression,
1042 // then the expression is not present. This handles things like:
1043 // "C ? throw : (void)42", which is crazy but legal.
1044 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1045 case tok::semi:
1046 case tok::r_paren:
1047 case tok::r_square:
1048 case tok::r_brace:
1049 case tok::colon:
1050 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001051 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001052
Chris Lattner2a2819a2008-04-06 06:02:23 +00001053 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001054 ExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +00001055 if (Expr.isInvalid()) return move(Expr);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001056 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001057 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001058}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001059
1060/// ParseCXXThis - This handles the C++ 'this' pointer.
1061///
1062/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1063/// a non-lvalue expression whose value is the address of the object for which
1064/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001065ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001066 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1067 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001068 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001069}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001070
1071/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1072/// Can be interpreted either as function-style casting ("int(x)")
1073/// or class type construction ("ClassType(x,y,z)")
1074/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001075/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001076///
1077/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001078/// simple-type-specifier '(' expression-list[opt] ')'
1079/// [C++0x] simple-type-specifier braced-init-list
1080/// typename-specifier '(' expression-list[opt] ')'
1081/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001082///
John McCall60d7b3a2010-08-24 06:29:42 +00001083ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001084Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001085 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001086 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001087
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001088 assert((Tok.is(tok::l_paren) ||
1089 (getLang().CPlusPlus0x && Tok.is(tok::l_brace)))
1090 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001091
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001092 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001093
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001094 // FIXME: Convert to a proper type construct expression.
1095 return ParseBraceInitializer();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001096
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001097 } else {
1098 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
1099
1100 SourceLocation LParenLoc = ConsumeParen();
1101
1102 ExprVector Exprs(Actions);
1103 CommaLocsTy CommaLocs;
1104
1105 if (Tok.isNot(tok::r_paren)) {
1106 if (ParseExpressionList(Exprs, CommaLocs)) {
1107 SkipUntil(tok::r_paren);
1108 return ExprError();
1109 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001110 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001111
1112 // Match the ')'.
1113 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1114
1115 // TypeRep could be null, if it references an invalid typedef.
1116 if (!TypeRep)
1117 return ExprError();
1118
1119 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1120 "Unexpected number of commas!");
1121 return Actions.ActOnCXXTypeConstructExpr(TypeRep, LParenLoc, move_arg(Exprs),
1122 RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001123 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001124}
1125
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001126/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001127///
1128/// condition:
1129/// expression
1130/// type-specifier-seq declarator '=' assignment-expression
1131/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1132/// '=' assignment-expression
1133///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001134/// \param ExprResult if the condition was parsed as an expression, the
1135/// parsed expression.
1136///
1137/// \param DeclResult if the condition was parsed as a declaration, the
1138/// parsed declaration.
1139///
Douglas Gregor586596f2010-05-06 17:25:47 +00001140/// \param Loc The location of the start of the statement that requires this
1141/// condition, e.g., the "for" in a for loop.
1142///
1143/// \param ConvertToBoolean Whether the condition expression should be
1144/// converted to a boolean value.
1145///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001146/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001147bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1148 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001149 SourceLocation Loc,
1150 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001151 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001152 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Douglas Gregordc845342010-05-25 05:58:43 +00001153 ConsumeCodeCompletionToken();
Douglas Gregor01dfea02010-01-10 23:08:15 +00001154 }
1155
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001156 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001157 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001158 ExprOut = ParseExpression(); // expression
1159 DeclOut = 0;
1160 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001161 return true;
1162
1163 // If required, convert to a boolean value.
1164 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001165 ExprOut
1166 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1167 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001168 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001169
1170 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001171 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001172 ParseSpecifierQualifierList(DS);
1173
1174 // declarator
1175 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1176 ParseDeclarator(DeclaratorInfo);
1177
1178 // simple-asm-expr[opt]
1179 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001180 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001181 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001182 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001183 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001184 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001185 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001186 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001187 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001188 }
1189
1190 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001191 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001192
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001193 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001194 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001195 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001196 DeclOut = Dcl.get();
1197 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001198
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001199 // '=' assignment-expression
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001200 if (isTokenEqualOrMistypedEqualEqual(
1201 diag::err_invalid_equalequal_after_declarator)) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001202 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001203 ExprResult AssignExpr(ParseAssignmentExpression());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001204 if (!AssignExpr.isInvalid())
Richard Smith34b41d92011-02-20 03:19:35 +00001205 Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false,
1206 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001207 } else {
1208 // FIXME: C++0x allows a braced-init-list
1209 Diag(Tok, diag::err_expected_equal_after_declarator);
1210 }
1211
Douglas Gregor586596f2010-05-06 17:25:47 +00001212 // FIXME: Build a reference to this declaration? Convert it to bool?
1213 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001214
1215 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001216
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001217 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001218}
1219
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001220/// \brief Determine whether the current token starts a C++
1221/// simple-type-specifier.
1222bool Parser::isCXXSimpleTypeSpecifier() const {
1223 switch (Tok.getKind()) {
1224 case tok::annot_typename:
1225 case tok::kw_short:
1226 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001227 case tok::kw___int64:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001228 case tok::kw_signed:
1229 case tok::kw_unsigned:
1230 case tok::kw_void:
1231 case tok::kw_char:
1232 case tok::kw_int:
1233 case tok::kw_float:
1234 case tok::kw_double:
1235 case tok::kw_wchar_t:
1236 case tok::kw_char16_t:
1237 case tok::kw_char32_t:
1238 case tok::kw_bool:
Douglas Gregord9d75e52011-04-27 05:41:15 +00001239 case tok::kw_decltype:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001240 case tok::kw_typeof:
Sean Huntdb5d44b2011-05-19 05:37:45 +00001241 case tok::kw___underlying_type:
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001242 return true;
1243
1244 default:
1245 break;
1246 }
1247
1248 return false;
1249}
1250
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001251/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1252/// This should only be called when the current token is known to be part of
1253/// simple-type-specifier.
1254///
1255/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001256/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001257/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1258/// char
1259/// wchar_t
1260/// bool
1261/// short
1262/// int
1263/// long
1264/// signed
1265/// unsigned
1266/// float
1267/// double
1268/// void
1269/// [GNU] typeof-specifier
1270/// [C++0x] auto [TODO]
1271///
1272/// type-name:
1273/// class-name
1274/// enum-name
1275/// typedef-name
1276///
1277void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1278 DS.SetRangeStart(Tok.getLocation());
1279 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001280 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001281 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001283 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001284 case tok::identifier: // foo::bar
1285 case tok::coloncolon: // ::foo::bar
1286 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001287 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001288 assert(0 && "Not a simple-type-specifier token!");
1289 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +00001290
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001291 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001292 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001293 if (getTypeAnnotation(Tok))
1294 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1295 getTypeAnnotation(Tok));
1296 else
1297 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001298
1299 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1300 ConsumeToken();
1301
1302 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1303 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1304 // Objective-C interface. If we don't have Objective-C or a '<', this is
1305 // just a normal reference to a typedef name.
1306 if (Tok.is(tok::less) && getLang().ObjC1)
1307 ParseObjCProtocolQualifiers(DS);
1308
1309 DS.Finish(Diags, PP);
1310 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001311 }
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001313 // builtin types
1314 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001315 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001316 break;
1317 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001318 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001319 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001320 case tok::kw___int64:
1321 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1322 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001323 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001324 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001325 break;
1326 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001327 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001328 break;
1329 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001330 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001331 break;
1332 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001333 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001334 break;
1335 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001336 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001337 break;
1338 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001339 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001340 break;
1341 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001342 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001343 break;
1344 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001345 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001346 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001347 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001348 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001349 break;
1350 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001351 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001352 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001353 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001354 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001355 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001357 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001358 // GNU typeof support.
1359 case tok::kw_typeof:
1360 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001361 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001362 return;
1363 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001364 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001365 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1366 else
1367 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001368 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001369 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001370}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001371
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001372/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1373/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1374/// e.g., "const short int". Note that the DeclSpec is *not* finished
1375/// by parsing the type-specifier-seq, because these sequences are
1376/// typically followed by some form of declarator. Returns true and
1377/// emits diagnostics if this is not a type-specifier-seq, false
1378/// otherwise.
1379///
1380/// type-specifier-seq: [C++ 8.1]
1381/// type-specifier type-specifier-seq[opt]
1382///
1383bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
1384 DS.SetRangeStart(Tok.getLocation());
1385 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001386 unsigned DiagID;
1387 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001388
1389 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001390 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1391 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Nick Lewycky9fa8e562010-11-03 17:52:57 +00001392 Diag(Tok, diag::err_expected_type);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001393 return true;
1394 }
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Sebastian Redld9bafa72010-02-03 21:21:43 +00001396 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1397 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
1398 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001399
Douglas Gregor396a9f22010-02-24 23:13:13 +00001400 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001401 return false;
1402}
1403
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001404/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1405/// some form.
1406///
1407/// This routine is invoked when a '<' is encountered after an identifier or
1408/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1409/// whether the unqualified-id is actually a template-id. This routine will
1410/// then parse the template arguments and form the appropriate template-id to
1411/// return to the caller.
1412///
1413/// \param SS the nested-name-specifier that precedes this template-id, if
1414/// we're actually parsing a qualified-id.
1415///
1416/// \param Name for constructor and destructor names, this is the actual
1417/// identifier that may be a template-name.
1418///
1419/// \param NameLoc the location of the class-name in a constructor or
1420/// destructor.
1421///
1422/// \param EnteringContext whether we're entering the scope of the
1423/// nested-name-specifier.
1424///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001425/// \param ObjectType if this unqualified-id occurs within a member access
1426/// expression, the type of the base object whose member is being accessed.
1427///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001428/// \param Id as input, describes the template-name or operator-function-id
1429/// that precedes the '<'. If template arguments were parsed successfully,
1430/// will be updated with the template-id.
1431///
Douglas Gregord4dca082010-02-24 18:44:31 +00001432/// \param AssumeTemplateId When true, this routine will assume that the name
1433/// refers to a template without performing name lookup to verify.
1434///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001435/// \returns true if a parse error occurred, false otherwise.
1436bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
1437 IdentifierInfo *Name,
1438 SourceLocation NameLoc,
1439 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001440 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001441 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +00001442 bool AssumeTemplateId,
1443 SourceLocation TemplateKWLoc) {
1444 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1445 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001446
1447 TemplateTy Template;
1448 TemplateNameKind TNK = TNK_Non_template;
1449 switch (Id.getKind()) {
1450 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001451 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001452 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001453 if (AssumeTemplateId) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001454 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001455 Id, ObjectType, EnteringContext,
1456 Template);
1457 if (TNK == TNK_Non_template)
1458 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001459 } else {
1460 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001461 TNK = Actions.isTemplateName(getCurScope(), SS,
1462 TemplateKWLoc.isValid(), Id,
1463 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001464 MemberOfUnknownSpecialization);
1465
1466 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1467 ObjectType && IsTemplateArgumentList()) {
1468 // We have something like t->getAs<T>(), where getAs is a
1469 // member of an unknown specialization. However, this will only
1470 // parse correctly as a template, so suggest the keyword 'template'
1471 // before 'getAs' and treat this as a dependent template name.
1472 std::string Name;
1473 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1474 Name = Id.Identifier->getName();
1475 else {
1476 Name = "operator ";
1477 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1478 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1479 else
1480 Name += Id.Identifier->getName();
1481 }
1482 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1483 << Name
1484 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001485 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001486 SS, Id, ObjectType,
1487 EnteringContext, Template);
1488 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001489 return true;
1490 }
1491 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001492 break;
1493
Douglas Gregor014e88d2009-11-03 23:16:33 +00001494 case UnqualifiedId::IK_ConstructorName: {
1495 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001496 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001497 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001498 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1499 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001500 EnteringContext, Template,
1501 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001502 break;
1503 }
1504
Douglas Gregor014e88d2009-11-03 23:16:33 +00001505 case UnqualifiedId::IK_DestructorName: {
1506 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001507 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001508 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001509 if (ObjectType) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001510 TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001511 TemplateName, ObjectType,
1512 EnteringContext, Template);
1513 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001514 return true;
1515 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001516 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1517 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001518 EnteringContext, Template,
1519 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001520
John McCallb3d87482010-08-24 05:47:05 +00001521 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001522 Diag(NameLoc, diag::err_destructor_template_id)
1523 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001524 return true;
1525 }
1526 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001527 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001528 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001529
1530 default:
1531 return false;
1532 }
1533
1534 if (TNK == TNK_Non_template)
1535 return false;
1536
1537 // Parse the enclosed template argument list.
1538 SourceLocation LAngleLoc, RAngleLoc;
1539 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001540 if (Tok.is(tok::less) &&
1541 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001542 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001543 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001544 RAngleLoc))
1545 return true;
1546
1547 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001548 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1549 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001550 // Form a parsed representation of the template-id to be stored in the
1551 // UnqualifiedId.
1552 TemplateIdAnnotation *TemplateId
1553 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1554
1555 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1556 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001557 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001558 TemplateId->TemplateNameLoc = Id.StartLocation;
1559 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001560 TemplateId->Name = 0;
1561 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1562 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001563 }
1564
Douglas Gregor059101f2011-03-02 00:47:37 +00001565 TemplateId->SS = SS;
John McCall2b5289b2010-08-23 07:28:44 +00001566 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001567 TemplateId->Kind = TNK;
1568 TemplateId->LAngleLoc = LAngleLoc;
1569 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001570 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001571 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001572 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001573 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001574
1575 Id.setTemplateId(TemplateId);
1576 return false;
1577 }
1578
1579 // Bundle the template arguments together.
1580 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001581 TemplateArgs.size());
1582
1583 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001584 TypeResult Type
Douglas Gregor059101f2011-03-02 00:47:37 +00001585 = Actions.ActOnTemplateIdType(SS, Template, NameLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001586 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001587 RAngleLoc);
1588 if (Type.isInvalid())
1589 return true;
1590
1591 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1592 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1593 else
1594 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1595
1596 return false;
1597}
1598
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001599/// \brief Parse an operator-function-id or conversion-function-id as part
1600/// of a C++ unqualified-id.
1601///
1602/// This routine is responsible only for parsing the operator-function-id or
1603/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001604///
1605/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001606/// operator-function-id: [C++ 13.5]
1607/// 'operator' operator
1608///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001609/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001610/// new delete new[] delete[]
1611/// + - * / % ^ & | ~
1612/// ! = < > += -= *= /= %=
1613/// ^= &= |= << >> >>= <<= == !=
1614/// <= >= && || ++ -- , ->* ->
1615/// () []
1616///
1617/// conversion-function-id: [C++ 12.3.2]
1618/// operator conversion-type-id
1619///
1620/// conversion-type-id:
1621/// type-specifier-seq conversion-declarator[opt]
1622///
1623/// conversion-declarator:
1624/// ptr-operator conversion-declarator[opt]
1625/// \endcode
1626///
1627/// \param The nested-name-specifier that preceded this unqualified-id. If
1628/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1629///
1630/// \param EnteringContext whether we are entering the scope of the
1631/// nested-name-specifier.
1632///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001633/// \param ObjectType if this unqualified-id occurs within a member access
1634/// expression, the type of the base object whose member is being accessed.
1635///
1636/// \param Result on a successful parse, contains the parsed unqualified-id.
1637///
1638/// \returns true if parsing fails, false otherwise.
1639bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001640 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001641 UnqualifiedId &Result) {
1642 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1643
1644 // Consume the 'operator' keyword.
1645 SourceLocation KeywordLoc = ConsumeToken();
1646
1647 // Determine what kind of operator name we have.
1648 unsigned SymbolIdx = 0;
1649 SourceLocation SymbolLocations[3];
1650 OverloadedOperatorKind Op = OO_None;
1651 switch (Tok.getKind()) {
1652 case tok::kw_new:
1653 case tok::kw_delete: {
1654 bool isNew = Tok.getKind() == tok::kw_new;
1655 // Consume the 'new' or 'delete'.
1656 SymbolLocations[SymbolIdx++] = ConsumeToken();
1657 if (Tok.is(tok::l_square)) {
1658 // Consume the '['.
1659 SourceLocation LBracketLoc = ConsumeBracket();
1660 // Consume the ']'.
1661 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1662 LBracketLoc);
1663 if (RBracketLoc.isInvalid())
1664 return true;
1665
1666 SymbolLocations[SymbolIdx++] = LBracketLoc;
1667 SymbolLocations[SymbolIdx++] = RBracketLoc;
1668 Op = isNew? OO_Array_New : OO_Array_Delete;
1669 } else {
1670 Op = isNew? OO_New : OO_Delete;
1671 }
1672 break;
1673 }
1674
1675#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1676 case tok::Token: \
1677 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1678 Op = OO_##Name; \
1679 break;
1680#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1681#include "clang/Basic/OperatorKinds.def"
1682
1683 case tok::l_paren: {
1684 // Consume the '('.
1685 SourceLocation LParenLoc = ConsumeParen();
1686 // Consume the ')'.
1687 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1688 LParenLoc);
1689 if (RParenLoc.isInvalid())
1690 return true;
1691
1692 SymbolLocations[SymbolIdx++] = LParenLoc;
1693 SymbolLocations[SymbolIdx++] = RParenLoc;
1694 Op = OO_Call;
1695 break;
1696 }
1697
1698 case tok::l_square: {
1699 // Consume the '['.
1700 SourceLocation LBracketLoc = ConsumeBracket();
1701 // Consume the ']'.
1702 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1703 LBracketLoc);
1704 if (RBracketLoc.isInvalid())
1705 return true;
1706
1707 SymbolLocations[SymbolIdx++] = LBracketLoc;
1708 SymbolLocations[SymbolIdx++] = RBracketLoc;
1709 Op = OO_Subscript;
1710 break;
1711 }
1712
1713 case tok::code_completion: {
1714 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001715 Actions.CodeCompleteOperatorName(getCurScope());
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001716
1717 // Consume the operator token.
Douglas Gregordc845342010-05-25 05:58:43 +00001718 ConsumeCodeCompletionToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001719
1720 // Don't try to parse any further.
1721 return true;
1722 }
1723
1724 default:
1725 break;
1726 }
1727
1728 if (Op != OO_None) {
1729 // We have parsed an operator-function-id.
1730 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1731 return false;
1732 }
Sean Hunt0486d742009-11-28 04:44:28 +00001733
1734 // Parse a literal-operator-id.
1735 //
1736 // literal-operator-id: [C++0x 13.5.8]
1737 // operator "" identifier
1738
1739 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1740 if (Tok.getLength() != 2)
1741 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1742 ConsumeStringToken();
1743
1744 if (Tok.isNot(tok::identifier)) {
1745 Diag(Tok.getLocation(), diag::err_expected_ident);
1746 return true;
1747 }
1748
1749 IdentifierInfo *II = Tok.getIdentifierInfo();
1750 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001751 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001752 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001753
1754 // Parse a conversion-function-id.
1755 //
1756 // conversion-function-id: [C++ 12.3.2]
1757 // operator conversion-type-id
1758 //
1759 // conversion-type-id:
1760 // type-specifier-seq conversion-declarator[opt]
1761 //
1762 // conversion-declarator:
1763 // ptr-operator conversion-declarator[opt]
1764
1765 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00001766 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001767 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001768 return true;
1769
1770 // Parse the conversion-declarator, which is merely a sequence of
1771 // ptr-operators.
1772 Declarator D(DS, Declarator::TypeNameContext);
1773 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1774
1775 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00001776 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001777 if (Ty.isInvalid())
1778 return true;
1779
1780 // Note that this is a conversion-function-id.
1781 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1782 D.getSourceRange().getEnd());
1783 return false;
1784}
1785
1786/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1787/// name of an entity.
1788///
1789/// \code
1790/// unqualified-id: [C++ expr.prim.general]
1791/// identifier
1792/// operator-function-id
1793/// conversion-function-id
1794/// [C++0x] literal-operator-id [TODO]
1795/// ~ class-name
1796/// template-id
1797///
1798/// \endcode
1799///
1800/// \param The nested-name-specifier that preceded this unqualified-id. If
1801/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1802///
1803/// \param EnteringContext whether we are entering the scope of the
1804/// nested-name-specifier.
1805///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001806/// \param AllowDestructorName whether we allow parsing of a destructor name.
1807///
1808/// \param AllowConstructorName whether we allow parsing a constructor name.
1809///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001810/// \param ObjectType if this unqualified-id occurs within a member access
1811/// expression, the type of the base object whose member is being accessed.
1812///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001813/// \param Result on a successful parse, contains the parsed unqualified-id.
1814///
1815/// \returns true if parsing fails, false otherwise.
1816bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1817 bool AllowDestructorName,
1818 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00001819 ParsedType ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001820 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001821
1822 // Handle 'A::template B'. This is for template-ids which have not
1823 // already been annotated by ParseOptionalCXXScopeSpecifier().
1824 bool TemplateSpecified = false;
1825 SourceLocation TemplateKWLoc;
1826 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1827 (ObjectType || SS.isSet())) {
1828 TemplateSpecified = true;
1829 TemplateKWLoc = ConsumeToken();
1830 }
1831
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001832 // unqualified-id:
1833 // identifier
1834 // template-id (when it hasn't already been annotated)
1835 if (Tok.is(tok::identifier)) {
1836 // Consume the identifier.
1837 IdentifierInfo *Id = Tok.getIdentifierInfo();
1838 SourceLocation IdLoc = ConsumeToken();
1839
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001840 if (!getLang().CPlusPlus) {
1841 // If we're not in C++, only identifiers matter. Record the
1842 // identifier and return.
1843 Result.setIdentifier(Id, IdLoc);
1844 return false;
1845 }
1846
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001847 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001848 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001849 // We have parsed a constructor name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001850 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001851 &SS, false, false,
1852 ParsedType(),
1853 /*NonTrivialTypeSourceInfo=*/true),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001854 IdLoc, IdLoc);
1855 } else {
1856 // We have parsed an identifier.
1857 Result.setIdentifier(Id, IdLoc);
1858 }
1859
1860 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001861 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001862 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001863 ObjectType, Result,
1864 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001865
1866 return false;
1867 }
1868
1869 // unqualified-id:
1870 // template-id (already parsed and annotated)
1871 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001872 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001873
1874 // If the template-name names the current class, then this is a constructor
1875 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001876 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001877 if (SS.isSet()) {
1878 // C++ [class.qual]p2 specifies that a qualified template-name
1879 // is taken as the constructor name where a constructor can be
1880 // declared. Thus, the template arguments are extraneous, so
1881 // complain about them and remove them entirely.
1882 Diag(TemplateId->TemplateNameLoc,
1883 diag::err_out_of_line_constructor_template_id)
1884 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001885 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001886 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1887 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1888 TemplateId->TemplateNameLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001889 getCurScope(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001890 &SS, false, false,
1891 ParsedType(),
1892 /*NontrivialTypeSourceInfo=*/true),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001893 TemplateId->TemplateNameLoc,
1894 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001895 ConsumeToken();
1896 return false;
1897 }
1898
1899 Result.setConstructorTemplateId(TemplateId);
1900 ConsumeToken();
1901 return false;
1902 }
1903
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001904 // We have already parsed a template-id; consume the annotation token as
1905 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001906 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001907 ConsumeToken();
1908 return false;
1909 }
1910
1911 // unqualified-id:
1912 // operator-function-id
1913 // conversion-function-id
1914 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001915 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001916 return true;
1917
Sean Hunte6252d12009-11-28 08:58:14 +00001918 // If we have an operator-function-id or a literal-operator-id and the next
1919 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001920 //
1921 // template-id:
1922 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001923 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1924 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001925 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001926 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1927 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001928 Result,
1929 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001930
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001931 return false;
1932 }
1933
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001934 if (getLang().CPlusPlus &&
1935 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001936 // C++ [expr.unary.op]p10:
1937 // There is an ambiguity in the unary-expression ~X(), where X is a
1938 // class-name. The ambiguity is resolved in favor of treating ~ as a
1939 // unary complement rather than treating ~X as referring to a destructor.
1940
1941 // Parse the '~'.
1942 SourceLocation TildeLoc = ConsumeToken();
1943
1944 // Parse the class-name.
1945 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001946 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001947 return true;
1948 }
1949
1950 // Parse the class-name (or template-name in a simple-template-id).
1951 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1952 SourceLocation ClassNameLoc = ConsumeToken();
1953
Douglas Gregor0278e122010-05-05 05:58:24 +00001954 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00001955 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001956 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001957 EnteringContext, ObjectType, Result,
1958 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001959 }
1960
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001961 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00001962 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1963 ClassNameLoc, getCurScope(),
1964 SS, ObjectType,
1965 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00001966 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001967 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001968
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001969 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001970 return false;
1971 }
1972
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001973 Diag(Tok, diag::err_expected_unqualified_id)
1974 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001975 return true;
1976}
1977
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001978/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1979/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001980///
Chris Lattner59232d32009-01-04 21:25:24 +00001981/// This method is called to parse the new expression after the optional :: has
1982/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1983/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001984///
1985/// new-expression:
1986/// '::'[opt] 'new' new-placement[opt] new-type-id
1987/// new-initializer[opt]
1988/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1989/// new-initializer[opt]
1990///
1991/// new-placement:
1992/// '(' expression-list ')'
1993///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001994/// new-type-id:
1995/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00001996/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001997///
1998/// new-declarator:
1999/// ptr-operator new-declarator[opt]
2000/// direct-new-declarator
2001///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002002/// new-initializer:
2003/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002004/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002005///
John McCall60d7b3a2010-08-24 06:29:42 +00002006ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002007Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2008 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2009 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002010
2011 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2012 // second form of new-expression. It can't be a new-type-id.
2013
Sebastian Redla55e52c2008-11-25 22:21:31 +00002014 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002015 SourceLocation PlacementLParen, PlacementRParen;
2016
Douglas Gregor4bd40312010-07-13 15:54:32 +00002017 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002018 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002019 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002020 if (Tok.is(tok::l_paren)) {
2021 // If it turns out to be a placement, we change the type location.
2022 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002023 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2024 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002025 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002026 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002027
2028 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002029 if (PlacementRParen.isInvalid()) {
2030 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002031 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002032 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002033
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002034 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002035 // Reset the placement locations. There was no placement.
Douglas Gregor4bd40312010-07-13 15:54:32 +00002036 TypeIdParens = SourceRange(PlacementLParen, PlacementRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002037 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002038 } else {
2039 // We still need the type.
2040 if (Tok.is(tok::l_paren)) {
Douglas Gregor4bd40312010-07-13 15:54:32 +00002041 TypeIdParens.setBegin(ConsumeParen());
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002042 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002043 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002044 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002045 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4bd40312010-07-13 15:54:32 +00002046 TypeIdParens.setEnd(MatchRHSPunctuation(tok::r_paren,
2047 TypeIdParens.getBegin()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002048 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002049 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002050 if (ParseCXXTypeSpecifierSeq(DS))
2051 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002052 else {
2053 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002054 ParseDeclaratorInternal(DeclaratorInfo,
2055 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002056 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002057 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002058 }
2059 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002060 // A new-type-id is a simplified type-id, where essentially the
2061 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002062 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002063 if (ParseCXXTypeSpecifierSeq(DS))
2064 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002065 else {
2066 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002067 ParseDeclaratorInternal(DeclaratorInfo,
2068 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002069 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002070 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002071 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002072 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002073 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002074 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002075
Sebastian Redla55e52c2008-11-25 22:21:31 +00002076 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002077 SourceLocation ConstructorLParen, ConstructorRParen;
2078
2079 if (Tok.is(tok::l_paren)) {
2080 ConstructorLParen = ConsumeParen();
2081 if (Tok.isNot(tok::r_paren)) {
2082 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002083 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2084 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002085 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002086 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002087 }
2088 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002089 if (ConstructorRParen.isInvalid()) {
2090 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002091 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002092 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002093 } else if (Tok.is(tok::l_brace)) {
2094 // FIXME: Have to communicate the init-list to ActOnCXXNew.
2095 ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002096 }
2097
Sebastian Redlf53597f2009-03-15 17:47:39 +00002098 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
2099 move_arg(PlacementArgs), PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +00002100 TypeIdParens, DeclaratorInfo, ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002101 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002102}
2103
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002104/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2105/// passed to ParseDeclaratorInternal.
2106///
2107/// direct-new-declarator:
2108/// '[' expression ']'
2109/// direct-new-declarator '[' constant-expression ']'
2110///
Chris Lattner59232d32009-01-04 21:25:24 +00002111void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002112 // Parse the array dimensions.
2113 bool first = true;
2114 while (Tok.is(tok::l_square)) {
2115 SourceLocation LLoc = ConsumeBracket();
John McCall60d7b3a2010-08-24 06:29:42 +00002116 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002117 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002118 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002119 // Recover
2120 SkipUntil(tok::r_square);
2121 return;
2122 }
2123 first = false;
2124
Sebastian Redlab197ba2009-02-09 18:23:29 +00002125 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
John McCall0b7e6782011-03-24 11:26:52 +00002126
2127 ParsedAttributes attrs(AttrFactory);
2128 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002129 /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002130 Size.release(), LLoc, RLoc),
John McCall0b7e6782011-03-24 11:26:52 +00002131 attrs, RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002132
Sebastian Redlab197ba2009-02-09 18:23:29 +00002133 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002134 return;
2135 }
2136}
2137
2138/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2139/// This ambiguity appears in the syntax of the C++ new operator.
2140///
2141/// new-expression:
2142/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2143/// new-initializer[opt]
2144///
2145/// new-placement:
2146/// '(' expression-list ')'
2147///
John McCallca0408f2010-08-23 06:44:23 +00002148bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002149 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002150 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002151 // The '(' was already consumed.
2152 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002153 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002154 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002155 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002156 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002157 }
2158
2159 // It's not a type, it has to be an expression list.
2160 // Discard the comma locations - ActOnCXXNew has enough parameters.
2161 CommaLocsTy CommaLocs;
2162 return ParseExpressionList(PlacementArgs, CommaLocs);
2163}
2164
2165/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2166/// to free memory allocated by new.
2167///
Chris Lattner59232d32009-01-04 21:25:24 +00002168/// This method is called to parse the 'delete' expression after the optional
2169/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2170/// and "Start" is its location. Otherwise, "Start" is the location of the
2171/// 'delete' token.
2172///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002173/// delete-expression:
2174/// '::'[opt] 'delete' cast-expression
2175/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002176ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002177Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2178 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2179 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002180
2181 // Array delete?
2182 bool ArrayDelete = false;
2183 if (Tok.is(tok::l_square)) {
2184 ArrayDelete = true;
2185 SourceLocation LHS = ConsumeBracket();
2186 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
2187 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002188 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002189 }
2190
John McCall60d7b3a2010-08-24 06:29:42 +00002191 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002192 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002193 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002194
John McCall9ae2f072010-08-23 23:25:46 +00002195 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002196}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002197
Mike Stump1eb44332009-09-09 15:08:12 +00002198static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002199 switch(kind) {
John Wiegley20c0da72011-04-27 23:09:49 +00002200 default: assert(false && "Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002201 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002202 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002203 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002204 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002205 case tok::kw___has_trivial_constructor:
2206 return UTT_HasTrivialDefaultConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002207 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002208 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2209 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2210 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002211 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2212 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002213 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002214 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2215 case tok::kw___is_compound: return UTT_IsCompound;
2216 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002217 case tok::kw___is_empty: return UTT_IsEmpty;
2218 case tok::kw___is_enum: return UTT_IsEnum;
John Wiegley20c0da72011-04-27 23:09:49 +00002219 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2220 case tok::kw___is_function: return UTT_IsFunction;
2221 case tok::kw___is_fundamental: return UTT_IsFundamental;
2222 case tok::kw___is_integral: return UTT_IsIntegral;
John Wiegley20c0da72011-04-27 23:09:49 +00002223 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2224 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2225 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2226 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2227 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002228 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002229 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002230 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002231 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002232 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002233 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002234 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2235 case tok::kw___is_scalar: return UTT_IsScalar;
2236 case tok::kw___is_signed: return UTT_IsSigned;
2237 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2238 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002239 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002240 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002241 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2242 case tok::kw___is_void: return UTT_IsVoid;
2243 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002244 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002245}
2246
2247static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2248 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002249 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002250 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002251 case tok::kw___is_convertible: return BTT_IsConvertible;
2252 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002253 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002254 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002255 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002256}
2257
John Wiegley21ff2e52011-04-28 00:16:57 +00002258static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2259 switch(kind) {
2260 default: llvm_unreachable("Not a known binary type trait");
2261 case tok::kw___array_rank: return ATT_ArrayRank;
2262 case tok::kw___array_extent: return ATT_ArrayExtent;
2263 }
2264}
2265
John Wiegley55262202011-04-25 06:54:41 +00002266static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2267 switch(kind) {
2268 default: assert(false && "Not a known unary expression trait.");
2269 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2270 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2271 }
2272}
2273
Sebastian Redl64b45f72009-01-05 20:52:13 +00002274/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2275/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2276/// templates.
2277///
2278/// primary-expression:
2279/// [GNU] unary-type-trait '(' type-id ')'
2280///
John McCall60d7b3a2010-08-24 06:29:42 +00002281ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002282 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2283 SourceLocation Loc = ConsumeToken();
2284
2285 SourceLocation LParen = Tok.getLocation();
2286 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2287 return ExprError();
2288
2289 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2290 // there will be cryptic errors about mismatched parentheses and missing
2291 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002292 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002293
2294 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2295
Douglas Gregor809070a2009-02-18 17:45:20 +00002296 if (Ty.isInvalid())
2297 return ExprError();
2298
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002299 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00002300}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002301
Francois Pichet6ad6f282010-12-07 00:08:36 +00002302/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2303/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2304/// templates.
2305///
2306/// primary-expression:
2307/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2308///
2309ExprResult Parser::ParseBinaryTypeTrait() {
2310 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2311 SourceLocation Loc = ConsumeToken();
2312
2313 SourceLocation LParen = Tok.getLocation();
2314 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2315 return ExprError();
2316
2317 TypeResult LhsTy = ParseTypeName();
2318 if (LhsTy.isInvalid()) {
2319 SkipUntil(tok::r_paren);
2320 return ExprError();
2321 }
2322
2323 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2324 SkipUntil(tok::r_paren);
2325 return ExprError();
2326 }
2327
2328 TypeResult RhsTy = ParseTypeName();
2329 if (RhsTy.isInvalid()) {
2330 SkipUntil(tok::r_paren);
2331 return ExprError();
2332 }
2333
2334 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2335
2336 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), RParen);
2337}
2338
John Wiegley21ff2e52011-04-28 00:16:57 +00002339/// ParseArrayTypeTrait - Parse the built-in array type-trait
2340/// pseudo-functions.
2341///
2342/// primary-expression:
2343/// [Embarcadero] '__array_rank' '(' type-id ')'
2344/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2345///
2346ExprResult Parser::ParseArrayTypeTrait() {
2347 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2348 SourceLocation Loc = ConsumeToken();
2349
2350 SourceLocation LParen = Tok.getLocation();
2351 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2352 return ExprError();
2353
2354 TypeResult Ty = ParseTypeName();
2355 if (Ty.isInvalid()) {
2356 SkipUntil(tok::comma);
2357 SkipUntil(tok::r_paren);
2358 return ExprError();
2359 }
2360
2361 switch (ATT) {
2362 case ATT_ArrayRank: {
2363 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2364 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, RParen);
2365 }
2366 case ATT_ArrayExtent: {
2367 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2368 SkipUntil(tok::r_paren);
2369 return ExprError();
2370 }
2371
2372 ExprResult DimExpr = ParseExpression();
2373 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2374
2375 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), RParen);
2376 }
2377 default:
2378 break;
2379 }
2380 return ExprError();
2381}
2382
John Wiegley55262202011-04-25 06:54:41 +00002383/// ParseExpressionTrait - Parse built-in expression-trait
2384/// pseudo-functions like __is_lvalue_expr( xxx ).
2385///
2386/// primary-expression:
2387/// [Embarcadero] expression-trait '(' expression ')'
2388///
2389ExprResult Parser::ParseExpressionTrait() {
2390 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2391 SourceLocation Loc = ConsumeToken();
2392
2393 SourceLocation LParen = Tok.getLocation();
2394 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
2395 return ExprError();
2396
2397 ExprResult Expr = ParseExpression();
2398
2399 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
2400
2401 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), RParen);
2402}
2403
2404
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002405/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2406/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2407/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002408ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002409Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002410 ParsedType &CastTy,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002411 SourceLocation LParenLoc,
2412 SourceLocation &RParenLoc) {
2413 assert(getLang().CPlusPlus && "Should only be called for C++!");
2414 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2415 assert(isTypeIdInParens() && "Not a type-id!");
2416
John McCall60d7b3a2010-08-24 06:29:42 +00002417 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002418 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002419
2420 // We need to disambiguate a very ugly part of the C++ syntax:
2421 //
2422 // (T())x; - type-id
2423 // (T())*x; - type-id
2424 // (T())/x; - expression
2425 // (T()); - expression
2426 //
2427 // The bad news is that we cannot use the specialized tentative parser, since
2428 // it can only verify that the thing inside the parens can be parsed as
2429 // type-id, it is not useful for determining the context past the parens.
2430 //
2431 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002432 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002433 //
2434 // It uses a scheme similar to parsing inline methods. The parenthesized
2435 // tokens are cached, the context that follows is determined (possibly by
2436 // parsing a cast-expression), and then we re-introduce the cached tokens
2437 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002438
Mike Stump1eb44332009-09-09 15:08:12 +00002439 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002440 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002441
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002442 // Store the tokens of the parentheses. We will parse them after we determine
2443 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002444 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002445 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002446 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2447 return ExprError();
2448 }
2449
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002450 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002451 ParseAs = CompoundLiteral;
2452 } else {
2453 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002454 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2455 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2456 NotCastExpr = true;
2457 } else {
2458 // Try parsing the cast-expression that may follow.
2459 // If it is not a cast-expression, NotCastExpr will be true and no token
2460 // will be consumed.
2461 Result = ParseCastExpression(false/*isUnaryExpression*/,
2462 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002463 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002464 // type-id has priority.
2465 true/*isTypeCast*/);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002466 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002467
2468 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2469 // an expression.
2470 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002471 }
2472
Mike Stump1eb44332009-09-09 15:08:12 +00002473 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002474 Toks.push_back(Tok);
2475 // Re-enter the stored parenthesized tokens into the token stream, so we may
2476 // parse them now.
2477 PP.EnterTokenStream(Toks.data(), Toks.size(),
2478 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2479 // Drop the current token and bring the first cached one. It's the same token
2480 // as when we entered this function.
2481 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002482
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002483 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002484 // Parse the type declarator.
2485 DeclSpec DS(AttrFactory);
2486 ParseSpecifierQualifierList(DS);
2487 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2488 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002489
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002490 // Match the ')'.
2491 if (Tok.is(tok::r_paren))
2492 RParenLoc = ConsumeParen();
2493 else
2494 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002495
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002496 if (ParseAs == CompoundLiteral) {
2497 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002498 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002499 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
2500 }
Mike Stump1eb44332009-09-09 15:08:12 +00002501
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002502 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2503 assert(ParseAs == CastExpr);
2504
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002505 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002506 return ExprError();
2507
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002508 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002509 if (!Result.isInvalid())
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002510 Result = Actions.ActOnCastExpr(getCurScope(), LParenLoc,
2511 DeclaratorInfo, CastTy,
2512 RParenLoc, Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002513 return move(Result);
2514 }
Mike Stump1eb44332009-09-09 15:08:12 +00002515
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002516 // Not a compound literal, and not followed by a cast-expression.
2517 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002518
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002519 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002520 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002521 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002522 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002523
2524 // Match the ')'.
2525 if (Result.isInvalid()) {
2526 SkipUntil(tok::r_paren);
2527 return ExprError();
2528 }
Mike Stump1eb44332009-09-09 15:08:12 +00002529
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002530 if (Tok.is(tok::r_paren))
2531 RParenLoc = ConsumeParen();
2532 else
2533 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2534
2535 return move(Result);
2536}