blob: b7ccea53d5903f46fe648572398bf89c87ea49d9 [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"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000018#include "llvm/Support/ErrorHandling.h"
19
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Mike Stump1eb44332009-09-09 15:08:12 +000022/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000023///
24/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000025/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000026/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000027///
28/// '::'[opt] nested-name-specifier
29/// '::'
30///
31/// nested-name-specifier:
32/// type-name '::'
33/// namespace-name '::'
34/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000035/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000036///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000037///
Mike Stump1eb44332009-09-09 15:08:12 +000038/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000039/// nested-name-specifier (or empty)
40///
Mike Stump1eb44332009-09-09 15:08:12 +000041/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +000042/// the "." or "->" of a member access expression, this parameter provides the
43/// type of the object whose members are being accessed.
44///
45/// \param EnteringContext whether we will be entering into the context of
46/// the nested-name-specifier after parsing it.
47///
Douglas Gregord4dca082010-02-24 18:44:31 +000048/// \param MayBePseudoDestructor When non-NULL, points to a flag that
49/// indicates whether this nested-name-specifier may be part of a
50/// pseudo-destructor name. In this case, the flag will be set false
51/// if we don't actually end up parsing a destructor name. Moreorover,
52/// if we do end up determining that we are parsing a destructor name,
53/// the last component of the nested-name-specifier is not parsed as
54/// part of the scope specifier.
55
Douglas Gregorb10cd042010-02-21 18:36:56 +000056/// member access expression, e.g., the \p T:: in \p p->T::m.
57///
John McCall9ba61662010-02-26 08:45:28 +000058/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +000059bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000060 Action::TypeTy *ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +000061 bool EnteringContext,
Douglas Gregord4dca082010-02-24 18:44:31 +000062 bool *MayBePseudoDestructor) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000063 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000064 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000065
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000066 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000067 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000068 SS.setRange(Tok.getAnnotationRange());
69 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +000070 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000071 }
Chris Lattnere607e802009-01-04 21:14:15 +000072
Douglas Gregor39a8de12009-02-25 19:37:18 +000073 bool HasScopeSpecifier = false;
74
Chris Lattner5b454732009-01-05 03:55:46 +000075 if (Tok.is(tok::coloncolon)) {
76 // ::new and ::delete aren't nested-name-specifiers.
77 tok::TokenKind NextKind = NextToken().getKind();
78 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
79 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattner55a7cef2009-01-05 00:13:00 +000081 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000082 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000083 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000084 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000085 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000086 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000087 }
88
Douglas Gregord4dca082010-02-24 18:44:31 +000089 bool CheckForDestructor = false;
90 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
91 CheckForDestructor = true;
92 *MayBePseudoDestructor = false;
93 }
94
Douglas Gregor39a8de12009-02-25 19:37:18 +000095 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000096 if (HasScopeSpecifier) {
97 // C++ [basic.lookup.classref]p5:
98 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000099 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000100 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000101 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000102 // the class-name-or-namespace-name is looked up in global scope as a
103 // class-name or namespace-name.
104 //
105 // To implement this, we clear out the object type as soon as we've
106 // seen a leading '::' or part of a nested-name-specifier.
107 ObjectType = 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000108
109 if (Tok.is(tok::code_completion)) {
110 // Code completion for a nested-name-specifier, where the code
111 // code completion token follows the '::'.
112 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
113 ConsumeToken();
114 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Douglas Gregor39a8de12009-02-25 19:37:18 +0000117 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000118 // nested-name-specifier 'template'[opt] simple-template-id '::'
119
120 // Parse the optional 'template' keyword, then make sure we have
121 // 'identifier <' after it.
122 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000124 // nested-name-specifier, since they aren't allowed to start with
125 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000126 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000127 break;
128
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000129 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000130 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000131
132 UnqualifiedId TemplateName;
133 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000134 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000135 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000136 ConsumeToken();
137 } else if (Tok.is(tok::kw_operator)) {
138 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000139 TemplateName)) {
140 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000141 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000142 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000143
Sean Hunte6252d12009-11-28 08:58:14 +0000144 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
145 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000146 Diag(TemplateName.getSourceRange().getBegin(),
147 diag::err_id_after_template_in_nested_name_spec)
148 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000149 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000150 break;
151 }
152 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000153 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000154 break;
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000157 // If the next token is not '<', we have a qualified-id that refers
158 // to a template name, such as T::template apply, but is not a
159 // template-id.
160 if (Tok.isNot(tok::less)) {
161 TPA.Revert();
162 break;
163 }
164
165 // Commit to parsing the template-id.
166 TPA.Commit();
Mike Stump1eb44332009-09-09 15:08:12 +0000167 TemplateTy Template
Douglas Gregor014e88d2009-11-03 23:16:33 +0000168 = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName,
Douglas Gregora481edb2009-11-20 23:39:24 +0000169 ObjectType, EnteringContext);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000170 if (!Template)
John McCall9ba61662010-02-26 08:45:28 +0000171 return true;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000172 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000173 &SS, TemplateName, TemplateKWLoc, false))
John McCall9ba61662010-02-26 08:45:28 +0000174 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Chris Lattner77cf72a2009-06-26 03:47:46 +0000176 continue;
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Douglas Gregor39a8de12009-02-25 19:37:18 +0000179 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000180 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000181 //
182 // simple-template-id '::'
183 //
184 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000185 // right kind (it should name a type or be dependent), and then
186 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000187 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000188 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord4dca082010-02-24 18:44:31 +0000189 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
190 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000191 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000192 }
193
Mike Stump1eb44332009-09-09 15:08:12 +0000194 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000195 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000196 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000197
Mike Stump1eb44332009-09-09 15:08:12 +0000198 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000199 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000200 Token TypeToken = Tok;
201 ConsumeToken();
202 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
203 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Douglas Gregor39a8de12009-02-25 19:37:18 +0000205 if (!HasScopeSpecifier) {
206 SS.setBeginLoc(TypeToken.getLocation());
207 HasScopeSpecifier = true;
208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Douglas Gregor31a19b62009-04-01 21:51:26 +0000210 if (TypeToken.getAnnotationValue())
211 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000212 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000213 TypeToken.getAnnotationValue(),
214 TypeToken.getAnnotationRange(),
Douglas Gregoredc90502010-02-25 04:46:04 +0000215 CCLoc));
Douglas Gregor31a19b62009-04-01 21:51:26 +0000216 else
217 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000218 SS.setEndLoc(CCLoc);
219 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000220 }
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Chris Lattner67b9e832009-06-26 03:45:46 +0000222 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000223 }
224
Chris Lattner5c7f7862009-06-26 03:52:38 +0000225
226 // The rest of the nested-name-specifier possibilities start with
227 // tok::identifier.
228 if (Tok.isNot(tok::identifier))
229 break;
230
231 IdentifierInfo &II = *Tok.getIdentifierInfo();
232
233 // nested-name-specifier:
234 // type-name '::'
235 // namespace-name '::'
236 // nested-name-specifier identifier '::'
237 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000238
239 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
240 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000241 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregoredc90502010-02-25 04:46:04 +0000242 if (Actions.IsInvalidUnlessNestedName(CurScope, SS, II, ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000243 EnteringContext) &&
244 // If the token after the colon isn't an identifier, it's still an
245 // error, but they probably meant something else strange so don't
246 // recover like this.
247 PP.LookAhead(1).is(tok::identifier)) {
248 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000249 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000250
251 // Recover as if the user wrote '::'.
252 Next.setKind(tok::coloncolon);
253 }
Chris Lattner46646492009-12-07 01:36:53 +0000254 }
255
Chris Lattner5c7f7862009-06-26 03:52:38 +0000256 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000257 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
258 !Actions.isNonTypeNestedNameSpecifier(CurScope, SS, Tok.getLocation(),
259 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000260 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000261 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000262 }
263
Chris Lattner5c7f7862009-06-26 03:52:38 +0000264 // We have an identifier followed by a '::'. Lookup this name
265 // as the name in a nested-name-specifier.
266 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000267 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
268 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000269 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattner5c7f7862009-06-26 03:52:38 +0000271 if (!HasScopeSpecifier) {
272 SS.setBeginLoc(IdLoc);
273 HasScopeSpecifier = true;
274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattner5c7f7862009-06-26 03:52:38 +0000276 if (SS.isInvalid())
277 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Chris Lattner5c7f7862009-06-26 03:52:38 +0000279 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000280 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregoredc90502010-02-25 04:46:04 +0000281 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000282 SS.setEndLoc(CCLoc);
283 continue;
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner5c7f7862009-06-26 03:52:38 +0000286 // nested-name-specifier:
287 // type-name '<'
288 if (Next.is(tok::less)) {
289 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000290 UnqualifiedId TemplateName;
291 TemplateName.setIdentifier(&II, Tok.getLocation());
292 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS,
293 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000294 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000295 EnteringContext,
296 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000297 // We have found a template name, so annotate this this token
298 // with a template-id annotation. We do not permit the
299 // template-id to be translated into a type annotation,
300 // because some clients (e.g., the parsing of class template
301 // specializations) still want to see the original template-id
302 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000303 ConsumeToken();
304 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName,
305 SourceLocation(), false))
John McCall9ba61662010-02-26 08:45:28 +0000306 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000307 continue;
308 }
309 }
310
Douglas Gregor39a8de12009-02-25 19:37:18 +0000311 // We don't have any tokens that form the beginning of a
312 // nested-name-specifier, so we're done.
313 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Douglas Gregord4dca082010-02-24 18:44:31 +0000316 // Even if we didn't see any pieces of a nested-name-specifier, we
317 // still check whether there is a tilde in this position, which
318 // indicates a potential pseudo-destructor.
319 if (CheckForDestructor && Tok.is(tok::tilde))
320 *MayBePseudoDestructor = true;
321
John McCall9ba61662010-02-26 08:45:28 +0000322 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000323}
324
325/// ParseCXXIdExpression - Handle id-expression.
326///
327/// id-expression:
328/// unqualified-id
329/// qualified-id
330///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000331/// qualified-id:
332/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
333/// '::' identifier
334/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000335/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000336///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000337/// NOTE: The standard specifies that, for qualified-id, the parser does not
338/// expect:
339///
340/// '::' conversion-function-id
341/// '::' '~' class-name
342///
343/// This may cause a slight inconsistency on diagnostics:
344///
345/// class C {};
346/// namespace A {}
347/// void f() {
348/// :: A :: ~ C(); // Some Sema error about using destructor with a
349/// // namespace.
350/// :: ~ C(); // Some Parser error like 'unexpected ~'.
351/// }
352///
353/// We simplify the parser a bit and make it work like:
354///
355/// qualified-id:
356/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
357/// '::' unqualified-id
358///
359/// That way Sema can handle and report similar errors for namespaces and the
360/// global scope.
361///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000362/// The isAddressOfOperand parameter indicates that this id-expression is a
363/// direct operand of the address-of operator. This is, besides member contexts,
364/// the only place where a qualified-id naming a non-static class member may
365/// appear.
366///
367Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000368 // qualified-id:
369 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
370 // '::' unqualified-id
371 //
372 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000373 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000374
375 UnqualifiedId Name;
376 if (ParseUnqualifiedId(SS,
377 /*EnteringContext=*/false,
378 /*AllowDestructorName=*/false,
379 /*AllowConstructorName=*/false,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000380 /*ObjectType=*/0,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000381 Name))
382 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000383
384 // This is only the direct operand of an & operator if it is not
385 // followed by a postfix-expression suffix.
386 if (isAddressOfOperand) {
387 switch (Tok.getKind()) {
388 case tok::l_square:
389 case tok::l_paren:
390 case tok::arrow:
391 case tok::period:
392 case tok::plusplus:
393 case tok::minusminus:
394 isAddressOfOperand = false;
395 break;
396
397 default:
398 break;
399 }
400 }
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000401
402 return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren),
403 isAddressOfOperand);
404
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000405}
406
Reid Spencer5f016e22007-07-11 17:01:13 +0000407/// ParseCXXCasts - This handles the various ways to cast expressions to another
408/// type.
409///
410/// postfix-expression: [C++ 5.2p1]
411/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
412/// 'static_cast' '<' type-name '>' '(' expression ')'
413/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
414/// 'const_cast' '<' type-name '>' '(' expression ')'
415///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000416Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 tok::TokenKind Kind = Tok.getKind();
418 const char *CastName = 0; // For error messages
419
420 switch (Kind) {
421 default: assert(0 && "Unknown C++ cast!"); abort();
422 case tok::kw_const_cast: CastName = "const_cast"; break;
423 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
424 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
425 case tok::kw_static_cast: CastName = "static_cast"; break;
426 }
427
428 SourceLocation OpLoc = ConsumeToken();
429 SourceLocation LAngleBracketLoc = Tok.getLocation();
430
431 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000432 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000433
Douglas Gregor809070a2009-02-18 17:45:20 +0000434 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 SourceLocation RAngleBracketLoc = Tok.getLocation();
436
Chris Lattner1ab3b962008-11-18 07:48:38 +0000437 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000438 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000439
440 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
441
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000442 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
443 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000444
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000445 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000447 // Match the ')'.
Douglas Gregor27591ff2009-11-06 05:48:00 +0000448 RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000449
Douglas Gregor809070a2009-02-18 17:45:20 +0000450 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000451 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000452 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000453 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000454 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000455
Sebastian Redl20df9b72008-12-11 22:51:44 +0000456 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000457}
458
Sebastian Redlc42e1182008-11-11 11:37:55 +0000459/// ParseCXXTypeid - This handles the C++ typeid expression.
460///
461/// postfix-expression: [C++ 5.2p1]
462/// 'typeid' '(' expression ')'
463/// 'typeid' '(' type-id ')'
464///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000465Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000466 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
467
468 SourceLocation OpLoc = ConsumeToken();
469 SourceLocation LParenLoc = Tok.getLocation();
470 SourceLocation RParenLoc;
471
472 // typeid expressions are always parenthesized.
473 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
474 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000475 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000476
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000477 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000478
479 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000480 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000481
482 // Match the ')'.
483 MatchRHSPunctuation(tok::r_paren, LParenLoc);
484
Douglas Gregor809070a2009-02-18 17:45:20 +0000485 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000486 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000487
488 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000489 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000490 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000491 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000492 // When typeid is applied to an expression other than an lvalue of a
493 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000494 // operand (Clause 5).
495 //
Mike Stump1eb44332009-09-09 15:08:12 +0000496 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000497 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000498 // we the expression is potentially potentially evaluated.
499 EnterExpressionEvaluationContext Unevaluated(Actions,
500 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000501 Result = ParseExpression();
502
503 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000504 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000505 SkipUntil(tok::r_paren);
506 else {
507 MatchRHSPunctuation(tok::r_paren, LParenLoc);
508
509 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000510 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000511 }
512 }
513
Sebastian Redl20df9b72008-12-11 22:51:44 +0000514 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000515}
516
Douglas Gregord4dca082010-02-24 18:44:31 +0000517/// \brief Parse a C++ pseudo-destructor expression after the base,
518/// . or -> operator, and nested-name-specifier have already been
519/// parsed.
520///
521/// postfix-expression: [C++ 5.2]
522/// postfix-expression . pseudo-destructor-name
523/// postfix-expression -> pseudo-destructor-name
524///
525/// pseudo-destructor-name:
526/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
527/// ::[opt] nested-name-specifier template simple-template-id ::
528/// ~type-name
529/// ::[opt] nested-name-specifier[opt] ~type-name
530///
531Parser::OwningExprResult
532Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
533 tok::TokenKind OpKind,
534 CXXScopeSpec &SS,
535 Action::TypeTy *ObjectType) {
536 // We're parsing either a pseudo-destructor-name or a dependent
537 // member access that has the same form as a
538 // pseudo-destructor-name. We parse both in the same way and let
539 // the action model sort them out.
540 //
541 // Note that the ::[opt] nested-name-specifier[opt] has already
542 // been parsed, and if there was a simple-template-id, it has
543 // been coalesced into a template-id annotation token.
544 UnqualifiedId FirstTypeName;
545 SourceLocation CCLoc;
546 if (Tok.is(tok::identifier)) {
547 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
548 ConsumeToken();
549 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
550 CCLoc = ConsumeToken();
551 } else if (Tok.is(tok::annot_template_id)) {
552 FirstTypeName.setTemplateId(
553 (TemplateIdAnnotation *)Tok.getAnnotationValue());
554 ConsumeToken();
555 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
556 CCLoc = ConsumeToken();
557 } else {
558 FirstTypeName.setIdentifier(0, SourceLocation());
559 }
560
561 // Parse the tilde.
562 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
563 SourceLocation TildeLoc = ConsumeToken();
564 if (!Tok.is(tok::identifier)) {
565 Diag(Tok, diag::err_destructor_tilde_identifier);
566 return ExprError();
567 }
568
569 // Parse the second type.
570 UnqualifiedId SecondTypeName;
571 IdentifierInfo *Name = Tok.getIdentifierInfo();
572 SourceLocation NameLoc = ConsumeToken();
573 SecondTypeName.setIdentifier(Name, NameLoc);
574
575 // If there is a '<', the second type name is a template-id. Parse
576 // it as such.
577 if (Tok.is(tok::less) &&
578 ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +0000579 SecondTypeName, /*AssumeTemplateName=*/true,
580 /*TemplateKWLoc*/SourceLocation()))
Douglas Gregord4dca082010-02-24 18:44:31 +0000581 return ExprError();
582
583 return Actions.ActOnPseudoDestructorExpr(CurScope, move(Base), OpLoc, OpKind,
584 SS, FirstTypeName, CCLoc,
585 TildeLoc, SecondTypeName,
586 Tok.is(tok::l_paren));
587}
588
Reid Spencer5f016e22007-07-11 17:01:13 +0000589/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
590///
591/// boolean-literal: [C++ 2.13.5]
592/// 'true'
593/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000594Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000596 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000597}
Chris Lattner50dd2892008-02-26 00:51:44 +0000598
599/// ParseThrowExpression - This handles the C++ throw expression.
600///
601/// throw-expression: [C++ 15]
602/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000603Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000604 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000605 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000606
Chris Lattner2a2819a2008-04-06 06:02:23 +0000607 // If the current token isn't the start of an assignment-expression,
608 // then the expression is not present. This handles things like:
609 // "C ? throw : (void)42", which is crazy but legal.
610 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
611 case tok::semi:
612 case tok::r_paren:
613 case tok::r_square:
614 case tok::r_brace:
615 case tok::colon:
616 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000617 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000618
Chris Lattner2a2819a2008-04-06 06:02:23 +0000619 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000620 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000621 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000622 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000623 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000624}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000625
626/// ParseCXXThis - This handles the C++ 'this' pointer.
627///
628/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
629/// a non-lvalue expression whose value is the address of the object for which
630/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000631Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000632 assert(Tok.is(tok::kw_this) && "Not 'this'!");
633 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000634 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000635}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000636
637/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
638/// Can be interpreted either as function-style casting ("int(x)")
639/// or class type construction ("ClassType(x,y,z)")
640/// or creation of a value-initialized type ("int()").
641///
642/// postfix-expression: [C++ 5.2p1]
643/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
644/// typename-specifier '(' expression-list[opt] ')' [TODO]
645///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000646Parser::OwningExprResult
647Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000648 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000649 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000650
651 assert(Tok.is(tok::l_paren) && "Expected '('!");
652 SourceLocation LParenLoc = ConsumeParen();
653
Sebastian Redla55e52c2008-11-25 22:21:31 +0000654 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000655 CommaLocsTy CommaLocs;
656
657 if (Tok.isNot(tok::r_paren)) {
658 if (ParseExpressionList(Exprs, CommaLocs)) {
659 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000660 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000661 }
662 }
663
664 // Match the ')'.
665 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
666
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000667 // TypeRep could be null, if it references an invalid typedef.
668 if (!TypeRep)
669 return ExprError();
670
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000671 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
672 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000673 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
674 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000675 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000676}
677
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000678/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000679///
680/// condition:
681/// expression
682/// type-specifier-seq declarator '=' assignment-expression
683/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
684/// '=' assignment-expression
685///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000686/// \param ExprResult if the condition was parsed as an expression, the
687/// parsed expression.
688///
689/// \param DeclResult if the condition was parsed as a declaration, the
690/// parsed declaration.
691///
Douglas Gregor586596f2010-05-06 17:25:47 +0000692/// \param Loc The location of the start of the statement that requires this
693/// condition, e.g., the "for" in a for loop.
694///
695/// \param ConvertToBoolean Whether the condition expression should be
696/// converted to a boolean value.
697///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000698/// \returns true if there was a parsing, false otherwise.
699bool Parser::ParseCXXCondition(OwningExprResult &ExprResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000700 DeclPtrTy &DeclResult,
701 SourceLocation Loc,
702 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000703 if (Tok.is(tok::code_completion)) {
704 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Condition);
705 ConsumeToken();
706 }
707
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000708 if (!isCXXConditionDeclaration()) {
Douglas Gregor586596f2010-05-06 17:25:47 +0000709 // Parse the expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000710 ExprResult = ParseExpression(); // expression
711 DeclResult = DeclPtrTy();
Douglas Gregor586596f2010-05-06 17:25:47 +0000712 if (ExprResult.isInvalid())
713 return true;
714
715 // If required, convert to a boolean value.
716 if (ConvertToBoolean)
717 ExprResult
718 = Actions.ActOnBooleanCondition(CurScope, Loc, move(ExprResult));
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000719 return ExprResult.isInvalid();
720 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000721
722 // type-specifier-seq
723 DeclSpec DS;
724 ParseSpecifierQualifierList(DS);
725
726 // declarator
727 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
728 ParseDeclarator(DeclaratorInfo);
729
730 // simple-asm-expr[opt]
731 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000732 SourceLocation Loc;
733 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000734 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000735 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000736 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000737 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000738 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000739 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000740 }
741
742 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000743 if (Tok.is(tok::kw___attribute)) {
744 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000745 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000746 DeclaratorInfo.AddAttributes(AttrList, Loc);
747 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000748
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000749 // Type-check the declaration itself.
750 Action::DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(CurScope,
751 DeclaratorInfo);
752 DeclResult = Dcl.get();
753 ExprResult = ExprError();
754
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000755 // '=' assignment-expression
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000756 if (Tok.is(tok::equal)) {
757 SourceLocation EqualLoc = ConsumeToken();
758 OwningExprResult AssignExpr(ParseAssignmentExpression());
759 if (!AssignExpr.isInvalid())
760 Actions.AddInitializerToDecl(DeclResult, move(AssignExpr));
761 } else {
762 // FIXME: C++0x allows a braced-init-list
763 Diag(Tok, diag::err_expected_equal_after_declarator);
764 }
765
Douglas Gregor586596f2010-05-06 17:25:47 +0000766 // FIXME: Build a reference to this declaration? Convert it to bool?
767 // (This is currently handled by Sema).
768
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000769 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000770}
771
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000772/// \brief Determine whether the current token starts a C++
773/// simple-type-specifier.
774bool Parser::isCXXSimpleTypeSpecifier() const {
775 switch (Tok.getKind()) {
776 case tok::annot_typename:
777 case tok::kw_short:
778 case tok::kw_long:
779 case tok::kw_signed:
780 case tok::kw_unsigned:
781 case tok::kw_void:
782 case tok::kw_char:
783 case tok::kw_int:
784 case tok::kw_float:
785 case tok::kw_double:
786 case tok::kw_wchar_t:
787 case tok::kw_char16_t:
788 case tok::kw_char32_t:
789 case tok::kw_bool:
790 // FIXME: C++0x decltype support.
791 // GNU typeof support.
792 case tok::kw_typeof:
793 return true;
794
795 default:
796 break;
797 }
798
799 return false;
800}
801
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000802/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
803/// This should only be called when the current token is known to be part of
804/// simple-type-specifier.
805///
806/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000807/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000808/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
809/// char
810/// wchar_t
811/// bool
812/// short
813/// int
814/// long
815/// signed
816/// unsigned
817/// float
818/// double
819/// void
820/// [GNU] typeof-specifier
821/// [C++0x] auto [TODO]
822///
823/// type-name:
824/// class-name
825/// enum-name
826/// typedef-name
827///
828void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
829 DS.SetRangeStart(Tok.getLocation());
830 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000831 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000832 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000834 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000835 case tok::identifier: // foo::bar
836 case tok::coloncolon: // ::foo::bar
837 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000838 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000839 assert(0 && "Not a simple-type-specifier token!");
840 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000841
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000842 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000843 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000844 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000845 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000846 break;
847 }
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000849 // builtin types
850 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000851 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000852 break;
853 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000854 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000855 break;
856 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000857 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000858 break;
859 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000860 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000861 break;
862 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000863 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000864 break;
865 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000866 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000867 break;
868 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000869 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000870 break;
871 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000872 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000873 break;
874 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000875 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000876 break;
877 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000878 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000879 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000880 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000881 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000882 break;
883 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000884 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000885 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000886 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000887 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000888 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000890 // FIXME: C++0x decltype support.
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000891 // GNU typeof support.
892 case tok::kw_typeof:
893 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000894 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000895 return;
896 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000897 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000898 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
899 else
900 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000901 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000902 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000903}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000904
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000905/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
906/// [dcl.name]), which is a non-empty sequence of type-specifiers,
907/// e.g., "const short int". Note that the DeclSpec is *not* finished
908/// by parsing the type-specifier-seq, because these sequences are
909/// typically followed by some form of declarator. Returns true and
910/// emits diagnostics if this is not a type-specifier-seq, false
911/// otherwise.
912///
913/// type-specifier-seq: [C++ 8.1]
914/// type-specifier type-specifier-seq[opt]
915///
916bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
917 DS.SetRangeStart(Tok.getLocation());
918 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000919 unsigned DiagID;
920 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000921
922 // Parse one or more of the type specifiers.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000923 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
924 ParsedTemplateInfo(), /*SuppressDeclarations*/true)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000925 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000926 return true;
927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Sebastian Redld9bafa72010-02-03 21:21:43 +0000929 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
930 ParsedTemplateInfo(), /*SuppressDeclarations*/true))
931 {}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000932
Douglas Gregor396a9f22010-02-24 23:13:13 +0000933 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000934 return false;
935}
936
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000937/// \brief Finish parsing a C++ unqualified-id that is a template-id of
938/// some form.
939///
940/// This routine is invoked when a '<' is encountered after an identifier or
941/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
942/// whether the unqualified-id is actually a template-id. This routine will
943/// then parse the template arguments and form the appropriate template-id to
944/// return to the caller.
945///
946/// \param SS the nested-name-specifier that precedes this template-id, if
947/// we're actually parsing a qualified-id.
948///
949/// \param Name for constructor and destructor names, this is the actual
950/// identifier that may be a template-name.
951///
952/// \param NameLoc the location of the class-name in a constructor or
953/// destructor.
954///
955/// \param EnteringContext whether we're entering the scope of the
956/// nested-name-specifier.
957///
Douglas Gregor46df8cc2009-11-03 21:24:04 +0000958/// \param ObjectType if this unqualified-id occurs within a member access
959/// expression, the type of the base object whose member is being accessed.
960///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000961/// \param Id as input, describes the template-name or operator-function-id
962/// that precedes the '<'. If template arguments were parsed successfully,
963/// will be updated with the template-id.
964///
Douglas Gregord4dca082010-02-24 18:44:31 +0000965/// \param AssumeTemplateId When true, this routine will assume that the name
966/// refers to a template without performing name lookup to verify.
967///
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000968/// \returns true if a parse error occurred, false otherwise.
969bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
970 IdentifierInfo *Name,
971 SourceLocation NameLoc,
972 bool EnteringContext,
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000973 TypeTy *ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +0000974 UnqualifiedId &Id,
Douglas Gregor0278e122010-05-05 05:58:24 +0000975 bool AssumeTemplateId,
976 SourceLocation TemplateKWLoc) {
977 assert((AssumeTemplateId || Tok.is(tok::less)) &&
978 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000979
980 TemplateTy Template;
981 TemplateNameKind TNK = TNK_Non_template;
982 switch (Id.getKind()) {
983 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +0000984 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +0000985 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +0000986 if (AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +0000987 Template = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS,
Douglas Gregord4dca082010-02-24 18:44:31 +0000988 Id, ObjectType,
989 EnteringContext);
990 TNK = TNK_Dependent_template_name;
991 if (!Template.get())
992 return true;
993 } else
994 TNK = Actions.isTemplateName(CurScope, SS, Id, ObjectType,
995 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000996 break;
997
Douglas Gregor014e88d2009-11-03 23:16:33 +0000998 case UnqualifiedId::IK_ConstructorName: {
999 UnqualifiedId TemplateName;
1000 TemplateName.setIdentifier(Name, NameLoc);
1001 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
1002 EnteringContext, Template);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001003 break;
1004 }
1005
Douglas Gregor014e88d2009-11-03 23:16:33 +00001006 case UnqualifiedId::IK_DestructorName: {
1007 UnqualifiedId TemplateName;
1008 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001009 if (ObjectType) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001010 Template = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS,
Douglas Gregora481edb2009-11-20 23:39:24 +00001011 TemplateName, ObjectType,
1012 EnteringContext);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001013 TNK = TNK_Dependent_template_name;
1014 if (!Template.get())
1015 return true;
1016 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001017 TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001018 EnteringContext, Template);
1019
1020 if (TNK == TNK_Non_template && Id.DestructorName == 0) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001021 Diag(NameLoc, diag::err_destructor_template_id)
1022 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001023 return true;
1024 }
1025 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001026 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001027 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001028
1029 default:
1030 return false;
1031 }
1032
1033 if (TNK == TNK_Non_template)
1034 return false;
1035
1036 // Parse the enclosed template argument list.
1037 SourceLocation LAngleLoc, RAngleLoc;
1038 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001039 if (Tok.is(tok::less) &&
1040 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001041 &SS, true, LAngleLoc,
1042 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001043 RAngleLoc))
1044 return true;
1045
1046 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001047 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1048 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001049 // Form a parsed representation of the template-id to be stored in the
1050 // UnqualifiedId.
1051 TemplateIdAnnotation *TemplateId
1052 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
1053
1054 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1055 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001056 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001057 TemplateId->TemplateNameLoc = Id.StartLocation;
1058 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001059 TemplateId->Name = 0;
1060 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1061 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001062 }
1063
1064 TemplateId->Template = Template.getAs<void*>();
1065 TemplateId->Kind = TNK;
1066 TemplateId->LAngleLoc = LAngleLoc;
1067 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001068 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001069 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001070 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001071 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001072
1073 Id.setTemplateId(TemplateId);
1074 return false;
1075 }
1076
1077 // Bundle the template arguments together.
1078 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001079 TemplateArgs.size());
1080
1081 // Constructor and destructor names.
1082 Action::TypeResult Type
1083 = Actions.ActOnTemplateIdType(Template, NameLoc,
1084 LAngleLoc, TemplateArgsPtr,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001085 RAngleLoc);
1086 if (Type.isInvalid())
1087 return true;
1088
1089 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1090 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1091 else
1092 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1093
1094 return false;
1095}
1096
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001097/// \brief Parse an operator-function-id or conversion-function-id as part
1098/// of a C++ unqualified-id.
1099///
1100/// This routine is responsible only for parsing the operator-function-id or
1101/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001102///
1103/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001104/// operator-function-id: [C++ 13.5]
1105/// 'operator' operator
1106///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001107/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001108/// new delete new[] delete[]
1109/// + - * / % ^ & | ~
1110/// ! = < > += -= *= /= %=
1111/// ^= &= |= << >> >>= <<= == !=
1112/// <= >= && || ++ -- , ->* ->
1113/// () []
1114///
1115/// conversion-function-id: [C++ 12.3.2]
1116/// operator conversion-type-id
1117///
1118/// conversion-type-id:
1119/// type-specifier-seq conversion-declarator[opt]
1120///
1121/// conversion-declarator:
1122/// ptr-operator conversion-declarator[opt]
1123/// \endcode
1124///
1125/// \param The nested-name-specifier that preceded this unqualified-id. If
1126/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1127///
1128/// \param EnteringContext whether we are entering the scope of the
1129/// nested-name-specifier.
1130///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001131/// \param ObjectType if this unqualified-id occurs within a member access
1132/// expression, the type of the base object whose member is being accessed.
1133///
1134/// \param Result on a successful parse, contains the parsed unqualified-id.
1135///
1136/// \returns true if parsing fails, false otherwise.
1137bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
1138 TypeTy *ObjectType,
1139 UnqualifiedId &Result) {
1140 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1141
1142 // Consume the 'operator' keyword.
1143 SourceLocation KeywordLoc = ConsumeToken();
1144
1145 // Determine what kind of operator name we have.
1146 unsigned SymbolIdx = 0;
1147 SourceLocation SymbolLocations[3];
1148 OverloadedOperatorKind Op = OO_None;
1149 switch (Tok.getKind()) {
1150 case tok::kw_new:
1151 case tok::kw_delete: {
1152 bool isNew = Tok.getKind() == tok::kw_new;
1153 // Consume the 'new' or 'delete'.
1154 SymbolLocations[SymbolIdx++] = ConsumeToken();
1155 if (Tok.is(tok::l_square)) {
1156 // Consume the '['.
1157 SourceLocation LBracketLoc = ConsumeBracket();
1158 // Consume the ']'.
1159 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1160 LBracketLoc);
1161 if (RBracketLoc.isInvalid())
1162 return true;
1163
1164 SymbolLocations[SymbolIdx++] = LBracketLoc;
1165 SymbolLocations[SymbolIdx++] = RBracketLoc;
1166 Op = isNew? OO_Array_New : OO_Array_Delete;
1167 } else {
1168 Op = isNew? OO_New : OO_Delete;
1169 }
1170 break;
1171 }
1172
1173#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1174 case tok::Token: \
1175 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1176 Op = OO_##Name; \
1177 break;
1178#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1179#include "clang/Basic/OperatorKinds.def"
1180
1181 case tok::l_paren: {
1182 // Consume the '('.
1183 SourceLocation LParenLoc = ConsumeParen();
1184 // Consume the ')'.
1185 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren,
1186 LParenLoc);
1187 if (RParenLoc.isInvalid())
1188 return true;
1189
1190 SymbolLocations[SymbolIdx++] = LParenLoc;
1191 SymbolLocations[SymbolIdx++] = RParenLoc;
1192 Op = OO_Call;
1193 break;
1194 }
1195
1196 case tok::l_square: {
1197 // Consume the '['.
1198 SourceLocation LBracketLoc = ConsumeBracket();
1199 // Consume the ']'.
1200 SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square,
1201 LBracketLoc);
1202 if (RBracketLoc.isInvalid())
1203 return true;
1204
1205 SymbolLocations[SymbolIdx++] = LBracketLoc;
1206 SymbolLocations[SymbolIdx++] = RBracketLoc;
1207 Op = OO_Subscript;
1208 break;
1209 }
1210
1211 case tok::code_completion: {
1212 // Code completion for the operator name.
1213 Actions.CodeCompleteOperatorName(CurScope);
1214
1215 // Consume the operator token.
1216 ConsumeToken();
1217
1218 // Don't try to parse any further.
1219 return true;
1220 }
1221
1222 default:
1223 break;
1224 }
1225
1226 if (Op != OO_None) {
1227 // We have parsed an operator-function-id.
1228 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1229 return false;
1230 }
Sean Hunt0486d742009-11-28 04:44:28 +00001231
1232 // Parse a literal-operator-id.
1233 //
1234 // literal-operator-id: [C++0x 13.5.8]
1235 // operator "" identifier
1236
1237 if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) {
1238 if (Tok.getLength() != 2)
1239 Diag(Tok.getLocation(), diag::err_operator_string_not_empty);
1240 ConsumeStringToken();
1241
1242 if (Tok.isNot(tok::identifier)) {
1243 Diag(Tok.getLocation(), diag::err_expected_ident);
1244 return true;
1245 }
1246
1247 IdentifierInfo *II = Tok.getIdentifierInfo();
1248 Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken());
Sean Hunt3e518bd2009-11-29 07:34:05 +00001249 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00001250 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001251
1252 // Parse a conversion-function-id.
1253 //
1254 // conversion-function-id: [C++ 12.3.2]
1255 // operator conversion-type-id
1256 //
1257 // conversion-type-id:
1258 // type-specifier-seq conversion-declarator[opt]
1259 //
1260 // conversion-declarator:
1261 // ptr-operator conversion-declarator[opt]
1262
1263 // Parse the type-specifier-seq.
1264 DeclSpec DS;
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00001265 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001266 return true;
1267
1268 // Parse the conversion-declarator, which is merely a sequence of
1269 // ptr-operators.
1270 Declarator D(DS, Declarator::TypeNameContext);
1271 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
1272
1273 // Finish up the type.
1274 Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D);
1275 if (Ty.isInvalid())
1276 return true;
1277
1278 // Note that this is a conversion-function-id.
1279 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
1280 D.getSourceRange().getEnd());
1281 return false;
1282}
1283
1284/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
1285/// name of an entity.
1286///
1287/// \code
1288/// unqualified-id: [C++ expr.prim.general]
1289/// identifier
1290/// operator-function-id
1291/// conversion-function-id
1292/// [C++0x] literal-operator-id [TODO]
1293/// ~ class-name
1294/// template-id
1295///
1296/// \endcode
1297///
1298/// \param The nested-name-specifier that preceded this unqualified-id. If
1299/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1300///
1301/// \param EnteringContext whether we are entering the scope of the
1302/// nested-name-specifier.
1303///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001304/// \param AllowDestructorName whether we allow parsing of a destructor name.
1305///
1306/// \param AllowConstructorName whether we allow parsing a constructor name.
1307///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001308/// \param ObjectType if this unqualified-id occurs within a member access
1309/// expression, the type of the base object whose member is being accessed.
1310///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001311/// \param Result on a successful parse, contains the parsed unqualified-id.
1312///
1313/// \returns true if parsing fails, false otherwise.
1314bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
1315 bool AllowDestructorName,
1316 bool AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001317 TypeTy *ObjectType,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001318 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001319
1320 // Handle 'A::template B'. This is for template-ids which have not
1321 // already been annotated by ParseOptionalCXXScopeSpecifier().
1322 bool TemplateSpecified = false;
1323 SourceLocation TemplateKWLoc;
1324 if (getLang().CPlusPlus && Tok.is(tok::kw_template) &&
1325 (ObjectType || SS.isSet())) {
1326 TemplateSpecified = true;
1327 TemplateKWLoc = ConsumeToken();
1328 }
1329
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001330 // unqualified-id:
1331 // identifier
1332 // template-id (when it hasn't already been annotated)
1333 if (Tok.is(tok::identifier)) {
1334 // Consume the identifier.
1335 IdentifierInfo *Id = Tok.getIdentifierInfo();
1336 SourceLocation IdLoc = ConsumeToken();
1337
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001338 if (!getLang().CPlusPlus) {
1339 // If we're not in C++, only identifiers matter. Record the
1340 // identifier and return.
1341 Result.setIdentifier(Id, IdLoc);
1342 return false;
1343 }
1344
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001345 if (AllowConstructorName &&
1346 Actions.isCurrentClassName(*Id, CurScope, &SS)) {
1347 // We have parsed a constructor name.
1348 Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope,
1349 &SS, false),
1350 IdLoc, IdLoc);
1351 } else {
1352 // We have parsed an identifier.
1353 Result.setIdentifier(Id, IdLoc);
1354 }
1355
1356 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00001357 if (TemplateSpecified || Tok.is(tok::less))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001358 return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext,
Douglas Gregor0278e122010-05-05 05:58:24 +00001359 ObjectType, Result,
1360 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001361
1362 return false;
1363 }
1364
1365 // unqualified-id:
1366 // template-id (already parsed and annotated)
1367 if (Tok.is(tok::annot_template_id)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001368 TemplateIdAnnotation *TemplateId
1369 = static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue());
1370
1371 // If the template-name names the current class, then this is a constructor
1372 if (AllowConstructorName && TemplateId->Name &&
1373 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
1374 if (SS.isSet()) {
1375 // C++ [class.qual]p2 specifies that a qualified template-name
1376 // is taken as the constructor name where a constructor can be
1377 // declared. Thus, the template arguments are extraneous, so
1378 // complain about them and remove them entirely.
1379 Diag(TemplateId->TemplateNameLoc,
1380 diag::err_out_of_line_constructor_template_id)
1381 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00001382 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001383 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
1384 Result.setConstructorName(Actions.getTypeName(*TemplateId->Name,
1385 TemplateId->TemplateNameLoc,
1386 CurScope,
1387 &SS, false),
1388 TemplateId->TemplateNameLoc,
1389 TemplateId->RAngleLoc);
1390 TemplateId->Destroy();
1391 ConsumeToken();
1392 return false;
1393 }
1394
1395 Result.setConstructorTemplateId(TemplateId);
1396 ConsumeToken();
1397 return false;
1398 }
1399
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001400 // We have already parsed a template-id; consume the annotation token as
1401 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001402 Result.setTemplateId(TemplateId);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001403 ConsumeToken();
1404 return false;
1405 }
1406
1407 // unqualified-id:
1408 // operator-function-id
1409 // conversion-function-id
1410 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001411 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001412 return true;
1413
Sean Hunte6252d12009-11-28 08:58:14 +00001414 // If we have an operator-function-id or a literal-operator-id and the next
1415 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001416 //
1417 // template-id:
1418 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00001419 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1420 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00001421 (TemplateSpecified || Tok.is(tok::less)))
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001422 return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(),
1423 EnteringContext, ObjectType,
Douglas Gregor0278e122010-05-05 05:58:24 +00001424 Result,
1425 TemplateSpecified, TemplateKWLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001426
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001427 return false;
1428 }
1429
Douglas Gregorb862b8f2010-01-11 23:29:10 +00001430 if (getLang().CPlusPlus &&
1431 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001432 // C++ [expr.unary.op]p10:
1433 // There is an ambiguity in the unary-expression ~X(), where X is a
1434 // class-name. The ambiguity is resolved in favor of treating ~ as a
1435 // unary complement rather than treating ~X as referring to a destructor.
1436
1437 // Parse the '~'.
1438 SourceLocation TildeLoc = ConsumeToken();
1439
1440 // Parse the class-name.
1441 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001442 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001443 return true;
1444 }
1445
1446 // Parse the class-name (or template-name in a simple-template-id).
1447 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
1448 SourceLocation ClassNameLoc = ConsumeToken();
1449
Douglas Gregor0278e122010-05-05 05:58:24 +00001450 if (TemplateSpecified || Tok.is(tok::less)) {
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001451 Result.setDestructorName(TildeLoc, 0, ClassNameLoc);
1452 return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc,
Douglas Gregor0278e122010-05-05 05:58:24 +00001453 EnteringContext, ObjectType, Result,
1454 TemplateSpecified, TemplateKWLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001455 }
1456
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001457 // Note that this is a destructor name.
Douglas Gregor124b8782010-02-16 19:09:40 +00001458 Action::TypeTy *Ty = Actions.getDestructorName(TildeLoc, *ClassName,
1459 ClassNameLoc, CurScope,
1460 SS, ObjectType,
1461 EnteringContext);
1462 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001463 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00001464
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001465 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001466 return false;
1467 }
1468
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001469 Diag(Tok, diag::err_expected_unqualified_id)
1470 << getLang().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001471 return true;
1472}
1473
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001474/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
1475/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00001476///
Chris Lattner59232d32009-01-04 21:25:24 +00001477/// This method is called to parse the new expression after the optional :: has
1478/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
1479/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001480///
1481/// new-expression:
1482/// '::'[opt] 'new' new-placement[opt] new-type-id
1483/// new-initializer[opt]
1484/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1485/// new-initializer[opt]
1486///
1487/// new-placement:
1488/// '(' expression-list ')'
1489///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001490/// new-type-id:
1491/// type-specifier-seq new-declarator[opt]
1492///
1493/// new-declarator:
1494/// ptr-operator new-declarator[opt]
1495/// direct-new-declarator
1496///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001497/// new-initializer:
1498/// '(' expression-list[opt] ')'
1499/// [C++0x] braced-init-list [TODO]
1500///
Chris Lattner59232d32009-01-04 21:25:24 +00001501Parser::OwningExprResult
1502Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
1503 assert(Tok.is(tok::kw_new) && "expected 'new' token");
1504 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001505
1506 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
1507 // second form of new-expression. It can't be a new-type-id.
1508
Sebastian Redla55e52c2008-11-25 22:21:31 +00001509 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001510 SourceLocation PlacementLParen, PlacementRParen;
1511
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001512 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001513 DeclSpec DS;
1514 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001515 if (Tok.is(tok::l_paren)) {
1516 // If it turns out to be a placement, we change the type location.
1517 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001518 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
1519 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001520 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001521 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001522
1523 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001524 if (PlacementRParen.isInvalid()) {
1525 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001526 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001527 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001528
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001529 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001530 // Reset the placement locations. There was no placement.
1531 PlacementLParen = PlacementRParen = SourceLocation();
1532 ParenTypeId = true;
1533 } else {
1534 // We still need the type.
1535 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001536 SourceLocation LParen = ConsumeParen();
1537 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001538 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001539 ParseDeclarator(DeclaratorInfo);
1540 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001541 ParenTypeId = true;
1542 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001543 if (ParseCXXTypeSpecifierSeq(DS))
1544 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001545 else {
1546 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001547 ParseDeclaratorInternal(DeclaratorInfo,
1548 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001549 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001550 ParenTypeId = false;
1551 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001552 }
1553 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001554 // A new-type-id is a simplified type-id, where essentially the
1555 // direct-declarator is replaced by a direct-new-declarator.
1556 if (ParseCXXTypeSpecifierSeq(DS))
1557 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001558 else {
1559 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001560 ParseDeclaratorInternal(DeclaratorInfo,
1561 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001562 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001563 ParenTypeId = false;
1564 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00001565 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001566 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001567 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001568 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001569
Sebastian Redla55e52c2008-11-25 22:21:31 +00001570 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001571 SourceLocation ConstructorLParen, ConstructorRParen;
1572
1573 if (Tok.is(tok::l_paren)) {
1574 ConstructorLParen = ConsumeParen();
1575 if (Tok.isNot(tok::r_paren)) {
1576 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001577 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
1578 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001579 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001580 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001581 }
1582 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001583 if (ConstructorRParen.isInvalid()) {
1584 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00001585 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001586 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001587 }
1588
Sebastian Redlf53597f2009-03-15 17:47:39 +00001589 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1590 move_arg(PlacementArgs), PlacementRParen,
1591 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1592 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001593}
1594
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001595/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1596/// passed to ParseDeclaratorInternal.
1597///
1598/// direct-new-declarator:
1599/// '[' expression ']'
1600/// direct-new-declarator '[' constant-expression ']'
1601///
Chris Lattner59232d32009-01-04 21:25:24 +00001602void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001603 // Parse the array dimensions.
1604 bool first = true;
1605 while (Tok.is(tok::l_square)) {
1606 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001607 OwningExprResult Size(first ? ParseExpression()
1608 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001609 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001610 // Recover
1611 SkipUntil(tok::r_square);
1612 return;
1613 }
1614 first = false;
1615
Sebastian Redlab197ba2009-02-09 18:23:29 +00001616 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001617 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001618 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001619 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001620
Sebastian Redlab197ba2009-02-09 18:23:29 +00001621 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001622 return;
1623 }
1624}
1625
1626/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1627/// This ambiguity appears in the syntax of the C++ new operator.
1628///
1629/// new-expression:
1630/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1631/// new-initializer[opt]
1632///
1633/// new-placement:
1634/// '(' expression-list ')'
1635///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001636bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001637 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001638 // The '(' was already consumed.
1639 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001640 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001641 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001642 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001643 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001644 }
1645
1646 // It's not a type, it has to be an expression list.
1647 // Discard the comma locations - ActOnCXXNew has enough parameters.
1648 CommaLocsTy CommaLocs;
1649 return ParseExpressionList(PlacementArgs, CommaLocs);
1650}
1651
1652/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1653/// to free memory allocated by new.
1654///
Chris Lattner59232d32009-01-04 21:25:24 +00001655/// This method is called to parse the 'delete' expression after the optional
1656/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1657/// and "Start" is its location. Otherwise, "Start" is the location of the
1658/// 'delete' token.
1659///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001660/// delete-expression:
1661/// '::'[opt] 'delete' cast-expression
1662/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001663Parser::OwningExprResult
1664Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1665 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1666 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001667
1668 // Array delete?
1669 bool ArrayDelete = false;
1670 if (Tok.is(tok::l_square)) {
1671 ArrayDelete = true;
1672 SourceLocation LHS = ConsumeBracket();
1673 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1674 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001675 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001676 }
1677
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001678 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001679 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001680 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001681
Sebastian Redlf53597f2009-03-15 17:47:39 +00001682 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001683}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001684
Mike Stump1eb44332009-09-09 15:08:12 +00001685static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001686 switch(kind) {
1687 default: assert(false && "Not a known unary type trait.");
1688 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1689 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1690 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1691 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1692 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1693 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1694 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1695 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1696 case tok::kw___is_abstract: return UTT_IsAbstract;
1697 case tok::kw___is_class: return UTT_IsClass;
1698 case tok::kw___is_empty: return UTT_IsEmpty;
1699 case tok::kw___is_enum: return UTT_IsEnum;
1700 case tok::kw___is_pod: return UTT_IsPOD;
1701 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1702 case tok::kw___is_union: return UTT_IsUnion;
Sebastian Redlccf43502009-12-03 00:13:20 +00001703 case tok::kw___is_literal: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00001704 }
1705}
1706
1707/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1708/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1709/// templates.
1710///
1711/// primary-expression:
1712/// [GNU] unary-type-trait '(' type-id ')'
1713///
Mike Stump1eb44332009-09-09 15:08:12 +00001714Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001715 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1716 SourceLocation Loc = ConsumeToken();
1717
1718 SourceLocation LParen = Tok.getLocation();
1719 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1720 return ExprError();
1721
1722 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1723 // there will be cryptic errors about mismatched parentheses and missing
1724 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001725 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001726
1727 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1728
Douglas Gregor809070a2009-02-18 17:45:20 +00001729 if (Ty.isInvalid())
1730 return ExprError();
1731
1732 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001733}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001734
1735/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1736/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1737/// based on the context past the parens.
1738Parser::OwningExprResult
1739Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1740 TypeTy *&CastTy,
1741 SourceLocation LParenLoc,
1742 SourceLocation &RParenLoc) {
1743 assert(getLang().CPlusPlus && "Should only be called for C++!");
1744 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1745 assert(isTypeIdInParens() && "Not a type-id!");
1746
1747 OwningExprResult Result(Actions, true);
1748 CastTy = 0;
1749
1750 // We need to disambiguate a very ugly part of the C++ syntax:
1751 //
1752 // (T())x; - type-id
1753 // (T())*x; - type-id
1754 // (T())/x; - expression
1755 // (T()); - expression
1756 //
1757 // The bad news is that we cannot use the specialized tentative parser, since
1758 // it can only verify that the thing inside the parens can be parsed as
1759 // type-id, it is not useful for determining the context past the parens.
1760 //
1761 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001762 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001763 //
1764 // It uses a scheme similar to parsing inline methods. The parenthesized
1765 // tokens are cached, the context that follows is determined (possibly by
1766 // parsing a cast-expression), and then we re-introduce the cached tokens
1767 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001768
Mike Stump1eb44332009-09-09 15:08:12 +00001769 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001770 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001771
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001772 // Store the tokens of the parentheses. We will parse them after we determine
1773 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00001774 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001775 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001776 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1777 return ExprError();
1778 }
1779
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001780 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001781 ParseAs = CompoundLiteral;
1782 } else {
1783 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001784 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1785 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1786 NotCastExpr = true;
1787 } else {
1788 // Try parsing the cast-expression that may follow.
1789 // If it is not a cast-expression, NotCastExpr will be true and no token
1790 // will be consumed.
1791 Result = ParseCastExpression(false/*isUnaryExpression*/,
1792 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001793 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001794 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001795
1796 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1797 // an expression.
1798 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001799 }
1800
Mike Stump1eb44332009-09-09 15:08:12 +00001801 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001802 Toks.push_back(Tok);
1803 // Re-enter the stored parenthesized tokens into the token stream, so we may
1804 // parse them now.
1805 PP.EnterTokenStream(Toks.data(), Toks.size(),
1806 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1807 // Drop the current token and bring the first cached one. It's the same token
1808 // as when we entered this function.
1809 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001810
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001811 if (ParseAs >= CompoundLiteral) {
1812 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001813
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001814 // Match the ')'.
1815 if (Tok.is(tok::r_paren))
1816 RParenLoc = ConsumeParen();
1817 else
1818 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001819
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001820 if (ParseAs == CompoundLiteral) {
1821 ExprType = CompoundLiteral;
1822 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1823 }
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001825 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1826 assert(ParseAs == CastExpr);
1827
1828 if (Ty.isInvalid())
1829 return ExprError();
1830
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001831 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001832
1833 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001834 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001835 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001836 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001837 return move(Result);
1838 }
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001840 // Not a compound literal, and not followed by a cast-expression.
1841 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001842
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001843 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001844 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001845 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1846 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1847
1848 // Match the ')'.
1849 if (Result.isInvalid()) {
1850 SkipUntil(tok::r_paren);
1851 return ExprError();
1852 }
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001854 if (Tok.is(tok::r_paren))
1855 RParenLoc = ConsumeParen();
1856 else
1857 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1858
1859 return move(Result);
1860}