blob: a68ed6a8037026ac4393d2cdaf13784fa6a5c8aa [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner29375652006-12-04 18:06:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner29375652006-12-04 18:06:35 +000015#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattner29375652006-12-04 18:06:35 +000017using namespace clang;
18
Mike Stump11289f42009-09-09 15:08:12 +000019/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +000020///
21/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +000022/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +000023/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000024///
25/// '::'[opt] nested-name-specifier
26/// '::'
27///
28/// nested-name-specifier:
29/// type-name '::'
30/// namespace-name '::'
31/// nested-name-specifier identifier '::'
Douglas Gregorb7bfe792009-09-02 22:59:36 +000032/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000033///
Douglas Gregorb7bfe792009-09-02 22:59:36 +000034///
Mike Stump11289f42009-09-09 15:08:12 +000035/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +000036/// nested-name-specifier (or empty)
37///
Mike Stump11289f42009-09-09 15:08:12 +000038/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-09-02 22:59:36 +000039/// the "." or "->" of a member access expression, this parameter provides the
40/// type of the object whose members are being accessed.
41///
42/// \param EnteringContext whether we will be entering into the context of
43/// the nested-name-specifier after parsing it.
44///
45/// \returns true if a scope specifier was parsed.
Douglas Gregore861bac2009-08-25 22:51:20 +000046bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregorb7bfe792009-09-02 22:59:36 +000047 Action::TypeTy *ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +000048 bool EnteringContext) {
Argyrios Kyrtzidisace521a2008-11-26 21:41:52 +000049 assert(getLang().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +000050 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +000051
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000052 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregorc23500e2009-03-26 23:56:24 +000053 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000054 SS.setRange(Tok.getAnnotationRange());
55 ConsumeToken();
Argyrios Kyrtzidisace521a2008-11-26 21:41:52 +000056 return true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000057 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +000058
Douglas Gregor7f741122009-02-25 19:37:18 +000059 bool HasScopeSpecifier = false;
60
Chris Lattner8a7d10d2009-01-05 03:55:46 +000061 if (Tok.is(tok::coloncolon)) {
62 // ::new and ::delete aren't nested-name-specifiers.
63 tok::TokenKind NextKind = NextToken().getKind();
64 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
65 return false;
Mike Stump11289f42009-09-09 15:08:12 +000066
Chris Lattner45ddec32009-01-05 00:13:00 +000067 // '::' - Global scope qualifier.
Chris Lattner6b87b9d2009-01-05 02:07:19 +000068 SourceLocation CCLoc = ConsumeToken();
Chris Lattner6b87b9d2009-01-05 02:07:19 +000069 SS.setBeginLoc(CCLoc);
Douglas Gregorc23500e2009-03-26 23:56:24 +000070 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner6b87b9d2009-01-05 02:07:19 +000071 SS.setEndLoc(CCLoc);
Douglas Gregor7f741122009-02-25 19:37:18 +000072 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000073 }
74
Douglas Gregor7f741122009-02-25 19:37:18 +000075 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +000076 if (HasScopeSpecifier) {
77 // C++ [basic.lookup.classref]p5:
78 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +000079 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +000080 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +000081 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +000082 // the class-name-or-namespace-name is looked up in global scope as a
83 // class-name or namespace-name.
84 //
85 // To implement this, we clear out the object type as soon as we've
86 // seen a leading '::' or part of a nested-name-specifier.
87 ObjectType = 0;
88 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Douglas Gregor7f741122009-02-25 19:37:18 +000090 // nested-name-specifier:
Chris Lattner0eed3a62009-06-26 03:47:46 +000091 // nested-name-specifier 'template'[opt] simple-template-id '::'
92
93 // Parse the optional 'template' keyword, then make sure we have
94 // 'identifier <' after it.
95 if (Tok.is(tok::kw_template)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +000096 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +000097 // nested-name-specifier, since they aren't allowed to start with
98 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +000099 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000100 break;
101
Chris Lattner0eed3a62009-06-26 03:47:46 +0000102 SourceLocation TemplateKWLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattner0eed3a62009-06-26 03:47:46 +0000104 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +0000105 Diag(Tok.getLocation(),
Chris Lattner0eed3a62009-06-26 03:47:46 +0000106 diag::err_id_after_template_in_nested_name_spec)
107 << SourceRange(TemplateKWLoc);
108 break;
109 }
Mike Stump11289f42009-09-09 15:08:12 +0000110
Chris Lattner0eed3a62009-06-26 03:47:46 +0000111 if (NextToken().isNot(tok::less)) {
112 Diag(NextToken().getLocation(),
113 diag::err_less_after_template_name_in_nested_name_spec)
114 << Tok.getIdentifierInfo()->getName()
115 << SourceRange(TemplateKWLoc, Tok.getLocation());
116 break;
117 }
Mike Stump11289f42009-09-09 15:08:12 +0000118
119 TemplateTy Template
Chris Lattner0eed3a62009-06-26 03:47:46 +0000120 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
121 *Tok.getIdentifierInfo(),
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000122 Tok.getLocation(), SS,
123 ObjectType);
Eli Friedman2624be42009-08-29 04:08:08 +0000124 if (!Template)
125 break;
Chris Lattner5558e9f2009-06-26 04:27:47 +0000126 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
127 &SS, TemplateKWLoc, false))
128 break;
Mike Stump11289f42009-09-09 15:08:12 +0000129
Chris Lattner0eed3a62009-06-26 03:47:46 +0000130 continue;
131 }
Mike Stump11289f42009-09-09 15:08:12 +0000132
Douglas Gregor7f741122009-02-25 19:37:18 +0000133 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000134 // We have
Douglas Gregor7f741122009-02-25 19:37:18 +0000135 //
136 // simple-template-id '::'
137 //
138 // So we need to check whether the simple-template-id is of the
Douglas Gregorb67535d2009-03-31 00:43:58 +0000139 // right kind (it should name a type or be dependent), and then
140 // convert it into a type within the nested-name-specifier.
Mike Stump11289f42009-09-09 15:08:12 +0000141 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000142 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
143
Mike Stump11289f42009-09-09 15:08:12 +0000144 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorb67535d2009-03-31 00:43:58 +0000145 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000146 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor7f741122009-02-25 19:37:18 +0000147
Mike Stump11289f42009-09-09 15:08:12 +0000148 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor7f741122009-02-25 19:37:18 +0000149 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor7f741122009-02-25 19:37:18 +0000150 Token TypeToken = Tok;
151 ConsumeToken();
152 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
153 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000154
Douglas Gregor7f741122009-02-25 19:37:18 +0000155 if (!HasScopeSpecifier) {
156 SS.setBeginLoc(TypeToken.getLocation());
157 HasScopeSpecifier = true;
158 }
Mike Stump11289f42009-09-09 15:08:12 +0000159
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000160 if (TypeToken.getAnnotationValue())
161 SS.setScopeRep(
Mike Stump11289f42009-09-09 15:08:12 +0000162 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000163 TypeToken.getAnnotationValue(),
164 TypeToken.getAnnotationRange(),
165 CCLoc));
166 else
167 SS.setScopeRep(0);
Douglas Gregor7f741122009-02-25 19:37:18 +0000168 SS.setEndLoc(CCLoc);
169 continue;
Chris Lattner704edfb2009-06-26 03:45:46 +0000170 }
Mike Stump11289f42009-09-09 15:08:12 +0000171
Chris Lattner704edfb2009-06-26 03:45:46 +0000172 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor7f741122009-02-25 19:37:18 +0000173 }
174
Chris Lattnere2355f72009-06-26 03:52:38 +0000175
176 // The rest of the nested-name-specifier possibilities start with
177 // tok::identifier.
178 if (Tok.isNot(tok::identifier))
179 break;
180
181 IdentifierInfo &II = *Tok.getIdentifierInfo();
182
183 // nested-name-specifier:
184 // type-name '::'
185 // namespace-name '::'
186 // nested-name-specifier identifier '::'
187 Token Next = NextToken();
188 if (Next.is(tok::coloncolon)) {
189 // We have an identifier followed by a '::'. Lookup this name
190 // as the name in a nested-name-specifier.
191 SourceLocation IdLoc = ConsumeToken();
192 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
193 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000194
Chris Lattnere2355f72009-06-26 03:52:38 +0000195 if (!HasScopeSpecifier) {
196 SS.setBeginLoc(IdLoc);
197 HasScopeSpecifier = true;
198 }
Mike Stump11289f42009-09-09 15:08:12 +0000199
Chris Lattnere2355f72009-06-26 03:52:38 +0000200 if (SS.isInvalid())
201 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000202
Chris Lattnere2355f72009-06-26 03:52:38 +0000203 SS.setScopeRep(
Douglas Gregore861bac2009-08-25 22:51:20 +0000204 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000205 ObjectType, EnteringContext));
Chris Lattnere2355f72009-06-26 03:52:38 +0000206 SS.setEndLoc(CCLoc);
207 continue;
208 }
Mike Stump11289f42009-09-09 15:08:12 +0000209
Chris Lattnere2355f72009-06-26 03:52:38 +0000210 // nested-name-specifier:
211 // type-name '<'
212 if (Next.is(tok::less)) {
213 TemplateTy Template;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000214 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, II,
215 Tok.getLocation(),
216 &SS,
217 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000218 EnteringContext,
219 Template)) {
Chris Lattnere2355f72009-06-26 03:52:38 +0000220 // We have found a template name, so annotate this this token
221 // with a template-id annotation. We do not permit the
222 // template-id to be translated into a type annotation,
223 // because some clients (e.g., the parsing of class template
224 // specializations) still want to see the original template-id
225 // token.
Chris Lattner5558e9f2009-06-26 04:27:47 +0000226 if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(),
227 false))
228 break;
Chris Lattnere2355f72009-06-26 03:52:38 +0000229 continue;
230 }
231 }
232
Douglas Gregor7f741122009-02-25 19:37:18 +0000233 // We don't have any tokens that form the beginning of a
234 // nested-name-specifier, so we're done.
235 break;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000236 }
Mike Stump11289f42009-09-09 15:08:12 +0000237
Douglas Gregor7f741122009-02-25 19:37:18 +0000238 return HasScopeSpecifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000239}
240
241/// ParseCXXIdExpression - Handle id-expression.
242///
243/// id-expression:
244/// unqualified-id
245/// qualified-id
246///
247/// unqualified-id:
248/// identifier
249/// operator-function-id
250/// conversion-function-id [TODO]
251/// '~' class-name [TODO]
Douglas Gregora727cb92009-06-30 22:34:41 +0000252/// template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000253///
254/// qualified-id:
255/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
256/// '::' identifier
257/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000258/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000259///
260/// nested-name-specifier:
261/// type-name '::'
262/// namespace-name '::'
263/// nested-name-specifier identifier '::'
264/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
265///
266/// NOTE: The standard specifies that, for qualified-id, the parser does not
267/// expect:
268///
269/// '::' conversion-function-id
270/// '::' '~' class-name
271///
272/// This may cause a slight inconsistency on diagnostics:
273///
274/// class C {};
275/// namespace A {}
276/// void f() {
277/// :: A :: ~ C(); // Some Sema error about using destructor with a
278/// // namespace.
279/// :: ~ C(); // Some Parser error like 'unexpected ~'.
280/// }
281///
282/// We simplify the parser a bit and make it work like:
283///
284/// qualified-id:
285/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
286/// '::' unqualified-id
287///
288/// That way Sema can handle and report similar errors for namespaces and the
289/// global scope.
290///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000291/// The isAddressOfOperand parameter indicates that this id-expression is a
292/// direct operand of the address-of operator. This is, besides member contexts,
293/// the only place where a qualified-id naming a non-static class member may
294/// appear.
295///
296Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000297 // qualified-id:
298 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
299 // '::' unqualified-id
300 //
301 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000302 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000303
304 // unqualified-id:
305 // identifier
306 // operator-function-id
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000307 // conversion-function-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000308 // '~' class-name [TODO]
Douglas Gregora727cb92009-06-30 22:34:41 +0000309 // template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000310 //
311 switch (Tok.getKind()) {
312 default:
Sebastian Redld65cea82008-12-11 22:51:44 +0000313 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000314
315 case tok::identifier: {
316 // Consume the identifier so that we can see if it is followed by a '('.
317 IdentifierInfo &II = *Tok.getIdentifierInfo();
318 SourceLocation L = ConsumeToken();
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000319 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
320 &SS, isAddressOfOperand);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000321 }
322
323 case tok::kw_operator: {
324 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattnerb5134c02009-01-05 01:24:05 +0000325 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlffbcf962009-01-18 18:53:16 +0000326 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000327 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
328 isAddressOfOperand);
Chris Lattnerb5134c02009-01-05 01:24:05 +0000329 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlffbcf962009-01-18 18:53:16 +0000330 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000331 Tok.is(tok::l_paren), SS,
332 isAddressOfOperand);
Sebastian Redld65cea82008-12-11 22:51:44 +0000333
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000334 // We already complained about a bad conversion-function-id,
335 // above.
Sebastian Redld65cea82008-12-11 22:51:44 +0000336 return ExprError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000337 }
338
Douglas Gregora727cb92009-06-30 22:34:41 +0000339 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +0000340 TemplateIdAnnotation *TemplateId
Douglas Gregora727cb92009-06-30 22:34:41 +0000341 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
342 assert((TemplateId->Kind == TNK_Function_template ||
343 TemplateId->Kind == TNK_Dependent_template_name) &&
344 "A template type name is not an ID expression");
345
Mike Stump11289f42009-09-09 15:08:12 +0000346 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregora727cb92009-06-30 22:34:41 +0000347 TemplateId->getTemplateArgs(),
348 TemplateId->getTemplateArgIsType(),
349 TemplateId->NumArgs);
Mike Stump11289f42009-09-09 15:08:12 +0000350
Douglas Gregora727cb92009-06-30 22:34:41 +0000351 OwningExprResult Result
352 = Actions.ActOnTemplateIdExpr(TemplateTy::make(TemplateId->Template),
353 TemplateId->TemplateNameLoc,
354 TemplateId->LAngleLoc,
355 TemplateArgsPtr,
356 TemplateId->getTemplateArgLocations(),
357 TemplateId->RAngleLoc);
358 ConsumeToken(); // Consume the template-id token
359 return move(Result);
360 }
361
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000362 } // switch.
363
364 assert(0 && "The switch was supposed to take care everything.");
365}
366
Chris Lattner29375652006-12-04 18:06:35 +0000367/// ParseCXXCasts - This handles the various ways to cast expressions to another
368/// type.
369///
370/// postfix-expression: [C++ 5.2p1]
371/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
372/// 'static_cast' '<' type-name '>' '(' expression ')'
373/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
374/// 'const_cast' '<' type-name '>' '(' expression ')'
375///
Sebastian Redld65cea82008-12-11 22:51:44 +0000376Parser::OwningExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +0000377 tok::TokenKind Kind = Tok.getKind();
378 const char *CastName = 0; // For error messages
379
380 switch (Kind) {
381 default: assert(0 && "Unknown C++ cast!"); abort();
382 case tok::kw_const_cast: CastName = "const_cast"; break;
383 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
384 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
385 case tok::kw_static_cast: CastName = "static_cast"; break;
386 }
387
388 SourceLocation OpLoc = ConsumeToken();
389 SourceLocation LAngleBracketLoc = Tok.getLocation();
390
391 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +0000392 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000393
Douglas Gregor220cac52009-02-18 17:45:20 +0000394 TypeResult CastTy = ParseTypeName();
Chris Lattner29375652006-12-04 18:06:35 +0000395 SourceLocation RAngleBracketLoc = Tok.getLocation();
396
Chris Lattner6d29c102008-11-18 07:48:38 +0000397 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +0000398 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +0000399
400 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
401
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000402 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
403 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +0000404
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000405 OwningExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +0000406
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000407 // Match the ')'.
408 if (Result.isInvalid())
409 SkipUntil(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +0000410
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +0000411 if (Tok.is(tok::r_paren))
412 RParenLoc = ConsumeParen();
413 else
414 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner29375652006-12-04 18:06:35 +0000415
Douglas Gregor220cac52009-02-18 17:45:20 +0000416 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregore200adc2008-10-27 19:41:14 +0000417 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000418 LAngleBracketLoc, CastTy.get(),
Douglas Gregor220cac52009-02-18 17:45:20 +0000419 RAngleBracketLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000420 LParenLoc, move(Result), RParenLoc);
Chris Lattner29375652006-12-04 18:06:35 +0000421
Sebastian Redld65cea82008-12-11 22:51:44 +0000422 return move(Result);
Chris Lattner29375652006-12-04 18:06:35 +0000423}
Bill Wendling4073ed52007-02-13 01:51:42 +0000424
Sebastian Redlc4704762008-11-11 11:37:55 +0000425/// ParseCXXTypeid - This handles the C++ typeid expression.
426///
427/// postfix-expression: [C++ 5.2p1]
428/// 'typeid' '(' expression ')'
429/// 'typeid' '(' type-id ')'
430///
Sebastian Redld65cea82008-12-11 22:51:44 +0000431Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +0000432 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
433
434 SourceLocation OpLoc = ConsumeToken();
435 SourceLocation LParenLoc = Tok.getLocation();
436 SourceLocation RParenLoc;
437
438 // typeid expressions are always parenthesized.
439 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
440 "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +0000441 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +0000442
Sebastian Redlc13f2682008-12-09 20:22:58 +0000443 OwningExprResult Result(Actions);
Sebastian Redlc4704762008-11-11 11:37:55 +0000444
445 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +0000446 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +0000447
448 // Match the ')'.
449 MatchRHSPunctuation(tok::r_paren, LParenLoc);
450
Douglas Gregor220cac52009-02-18 17:45:20 +0000451 if (Ty.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +0000452 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +0000453
454 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor220cac52009-02-18 17:45:20 +0000455 Ty.get(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +0000456 } else {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000457 // C++0x [expr.typeid]p3:
Mike Stump11289f42009-09-09 15:08:12 +0000458 // When typeid is applied to an expression other than an lvalue of a
459 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000460 // operand (Clause 5).
461 //
Mike Stump11289f42009-09-09 15:08:12 +0000462 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000463 // polymorphic class type until after we've parsed the expression, so
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000464 // we the expression is potentially potentially evaluated.
465 EnterExpressionEvaluationContext Unevaluated(Actions,
466 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc4704762008-11-11 11:37:55 +0000467 Result = ParseExpression();
468
469 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000470 if (Result.isInvalid())
Sebastian Redlc4704762008-11-11 11:37:55 +0000471 SkipUntil(tok::r_paren);
472 else {
473 MatchRHSPunctuation(tok::r_paren, LParenLoc);
474
475 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000476 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +0000477 }
478 }
479
Sebastian Redld65cea82008-12-11 22:51:44 +0000480 return move(Result);
Sebastian Redlc4704762008-11-11 11:37:55 +0000481}
482
Bill Wendling4073ed52007-02-13 01:51:42 +0000483/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
484///
485/// boolean-literal: [C++ 2.13.5]
486/// 'true'
487/// 'false'
Sebastian Redld65cea82008-12-11 22:51:44 +0000488Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +0000489 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000490 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +0000491}
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000492
493/// ParseThrowExpression - This handles the C++ throw expression.
494///
495/// throw-expression: [C++ 15]
496/// 'throw' assignment-expression[opt]
Sebastian Redld65cea82008-12-11 22:51:44 +0000497Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000498 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000499 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +0000500
Chris Lattner65dd8432008-04-06 06:02:23 +0000501 // If the current token isn't the start of an assignment-expression,
502 // then the expression is not present. This handles things like:
503 // "C ? throw : (void)42", which is crazy but legal.
504 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
505 case tok::semi:
506 case tok::r_paren:
507 case tok::r_square:
508 case tok::r_brace:
509 case tok::colon:
510 case tok::comma:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000511 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000512
Chris Lattner65dd8432008-04-06 06:02:23 +0000513 default:
Sebastian Redl59b5e512008-12-11 21:36:32 +0000514 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redld65cea82008-12-11 22:51:44 +0000515 if (Expr.isInvalid()) return move(Expr);
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000516 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner65dd8432008-04-06 06:02:23 +0000517 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000518}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000519
520/// ParseCXXThis - This handles the C++ 'this' pointer.
521///
522/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
523/// a non-lvalue expression whose value is the address of the object for which
524/// the function is called.
Sebastian Redld65cea82008-12-11 22:51:44 +0000525Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000526 assert(Tok.is(tok::kw_this) && "Not 'this'!");
527 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000528 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000529}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000530
531/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
532/// Can be interpreted either as function-style casting ("int(x)")
533/// or class type construction ("ClassType(x,y,z)")
534/// or creation of a value-initialized type ("int()").
535///
536/// postfix-expression: [C++ 5.2p1]
537/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
538/// typename-specifier '(' expression-list[opt] ')' [TODO]
539///
Sebastian Redld65cea82008-12-11 22:51:44 +0000540Parser::OwningExprResult
541Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000542 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregorf8298252009-01-26 22:44:13 +0000543 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000544
545 assert(Tok.is(tok::l_paren) && "Expected '('!");
546 SourceLocation LParenLoc = ConsumeParen();
547
Sebastian Redl511ed552008-11-25 22:21:31 +0000548 ExprVector Exprs(Actions);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000549 CommaLocsTy CommaLocs;
550
551 if (Tok.isNot(tok::r_paren)) {
552 if (ParseExpressionList(Exprs, CommaLocs)) {
553 SkipUntil(tok::r_paren);
Sebastian Redld65cea82008-12-11 22:51:44 +0000554 return ExprError();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000555 }
556 }
557
558 // Match the ')'.
559 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
560
Sebastian Redl955a0672009-07-29 13:50:23 +0000561 // TypeRep could be null, if it references an invalid typedef.
562 if (!TypeRep)
563 return ExprError();
564
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000565 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
566 "Unexpected number of commas!");
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000567 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
568 LParenLoc, move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000569 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000570}
571
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000572/// ParseCXXCondition - if/switch/while/for condition expression.
573///
574/// condition:
575/// expression
576/// type-specifier-seq declarator '=' assignment-expression
577/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
578/// '=' assignment-expression
579///
Sebastian Redl59b5e512008-12-11 21:36:32 +0000580Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidis71f3e192008-10-05 15:03:47 +0000581 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000582 return ParseExpression(); // expression
583
584 SourceLocation StartLoc = Tok.getLocation();
585
586 // type-specifier-seq
587 DeclSpec DS;
588 ParseSpecifierQualifierList(DS);
589
590 // declarator
591 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
592 ParseDeclarator(DeclaratorInfo);
593
594 // simple-asm-expr[opt]
595 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000596 SourceLocation Loc;
597 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000598 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000599 SkipUntil(tok::semi);
Sebastian Redl59b5e512008-12-11 21:36:32 +0000600 return ExprError();
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000601 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000602 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000603 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000604 }
605
606 // If attributes are present, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000607 if (Tok.is(tok::kw___attribute)) {
608 SourceLocation Loc;
609 AttributeList *AttrList = ParseAttributes(&Loc);
610 DeclaratorInfo.AddAttributes(AttrList, Loc);
611 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000612
613 // '=' assignment-expression
614 if (Tok.isNot(tok::equal))
Sebastian Redl59b5e512008-12-11 21:36:32 +0000615 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000616 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000617 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000618 if (AssignExpr.isInvalid())
Sebastian Redl59b5e512008-12-11 21:36:32 +0000619 return ExprError();
620
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000621 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
622 DeclaratorInfo,EqualLoc,
623 move(AssignExpr));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000624}
625
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000626/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
627/// This should only be called when the current token is known to be part of
628/// simple-type-specifier.
629///
630/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000631/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000632/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
633/// char
634/// wchar_t
635/// bool
636/// short
637/// int
638/// long
639/// signed
640/// unsigned
641/// float
642/// double
643/// void
644/// [GNU] typeof-specifier
645/// [C++0x] auto [TODO]
646///
647/// type-name:
648/// class-name
649/// enum-name
650/// typedef-name
651///
652void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
653 DS.SetRangeStart(Tok.getLocation());
654 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000655 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000656 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000657
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000658 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +0000659 case tok::identifier: // foo::bar
660 case tok::coloncolon: // ::foo::bar
661 assert(0 && "Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +0000662 default:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000663 assert(0 && "Not a simple-type-specifier token!");
664 abort();
Chris Lattner45ddec32009-01-05 00:13:00 +0000665
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000666 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +0000667 case tok::annot_typename: {
John McCall49bfce42009-08-03 20:12:06 +0000668 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000669 Tok.getAnnotationValue());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000670 break;
671 }
Mike Stump11289f42009-09-09 15:08:12 +0000672
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000673 // builtin types
674 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +0000675 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000676 break;
677 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +0000678 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000679 break;
680 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +0000681 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000682 break;
683 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +0000684 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000685 break;
686 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +0000687 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000688 break;
689 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +0000690 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000691 break;
692 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +0000693 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000694 break;
695 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +0000696 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000697 break;
698 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +0000699 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000700 break;
701 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +0000702 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000703 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000704 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +0000705 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000706 break;
707 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +0000708 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000709 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000710 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +0000711 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000712 break;
Mike Stump11289f42009-09-09 15:08:12 +0000713
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000714 // GNU typeof support.
715 case tok::kw_typeof:
716 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +0000717 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000718 return;
719 }
Chris Lattnera8a3f732009-01-06 05:06:21 +0000720 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000721 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
722 else
723 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000724 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +0000725 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000726}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000727
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000728/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
729/// [dcl.name]), which is a non-empty sequence of type-specifiers,
730/// e.g., "const short int". Note that the DeclSpec is *not* finished
731/// by parsing the type-specifier-seq, because these sequences are
732/// typically followed by some form of declarator. Returns true and
733/// emits diagnostics if this is not a type-specifier-seq, false
734/// otherwise.
735///
736/// type-specifier-seq: [C++ 8.1]
737/// type-specifier type-specifier-seq[opt]
738///
739bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
740 DS.SetRangeStart(Tok.getLocation());
741 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000742 unsigned DiagID;
743 bool isInvalid = 0;
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000744
745 // Parse one or more of the type specifiers.
John McCall49bfce42009-08-03 20:12:06 +0000746 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000747 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000748 return true;
749 }
Mike Stump11289f42009-09-09 15:08:12 +0000750
John McCall49bfce42009-08-03 20:12:06 +0000751 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000752
753 return false;
754}
755
Douglas Gregord69246b2008-11-17 16:14:12 +0000756/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000757/// operator name (C++ [over.oper]). If successful, returns the
758/// predefined identifier that corresponds to that overloaded
759/// operator. Otherwise, returns NULL and does not consume any tokens.
760///
761/// operator-function-id: [C++ 13.5]
762/// 'operator' operator
763///
764/// operator: one of
765/// new delete new[] delete[]
766/// + - * / % ^ & | ~
767/// ! = < > += -= *= /= %=
768/// ^= &= |= << >> >>= <<= == !=
769/// <= >= && || ++ -- , ->* ->
770/// () []
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000771OverloadedOperatorKind
772Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis56fa31b2008-11-07 15:54:02 +0000773 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000774 SourceLocation Loc;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000775
776 OverloadedOperatorKind Op = OO_None;
777 switch (NextToken().getKind()) {
778 case tok::kw_new:
779 ConsumeToken(); // 'operator'
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000780 Loc = ConsumeToken(); // 'new'
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000781 if (Tok.is(tok::l_square)) {
782 ConsumeBracket(); // '['
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000783 Loc = Tok.getLocation();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000784 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
785 Op = OO_Array_New;
786 } else {
787 Op = OO_New;
788 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000789 if (EndLoc)
790 *EndLoc = Loc;
Douglas Gregor163c5852008-11-18 14:39:36 +0000791 return Op;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000792
793 case tok::kw_delete:
794 ConsumeToken(); // 'operator'
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000795 Loc = ConsumeToken(); // 'delete'
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000796 if (Tok.is(tok::l_square)) {
797 ConsumeBracket(); // '['
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000798 Loc = Tok.getLocation();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000799 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
800 Op = OO_Array_Delete;
801 } else {
802 Op = OO_Delete;
803 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000804 if (EndLoc)
805 *EndLoc = Loc;
Douglas Gregor163c5852008-11-18 14:39:36 +0000806 return Op;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000807
Douglas Gregor6cf08062008-11-10 13:38:07 +0000808#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000809 case tok::Token: Op = OO_##Name; break;
Douglas Gregor6cf08062008-11-10 13:38:07 +0000810#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000811#include "clang/Basic/OperatorKinds.def"
812
813 case tok::l_paren:
814 ConsumeToken(); // 'operator'
815 ConsumeParen(); // '('
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000816 Loc = Tok.getLocation();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000817 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000818 if (EndLoc)
819 *EndLoc = Loc;
Douglas Gregor163c5852008-11-18 14:39:36 +0000820 return OO_Call;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000821
822 case tok::l_square:
823 ConsumeToken(); // 'operator'
824 ConsumeBracket(); // '['
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000825 Loc = Tok.getLocation();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000826 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000827 if (EndLoc)
828 *EndLoc = Loc;
Douglas Gregor163c5852008-11-18 14:39:36 +0000829 return OO_Subscript;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000830
831 default:
Douglas Gregor163c5852008-11-18 14:39:36 +0000832 return OO_None;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000833 }
834
Douglas Gregord69246b2008-11-17 16:14:12 +0000835 ConsumeToken(); // 'operator'
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000836 Loc = ConsumeAnyToken(); // the operator itself
837 if (EndLoc)
838 *EndLoc = Loc;
Douglas Gregor163c5852008-11-18 14:39:36 +0000839 return Op;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000840}
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000841
842/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
843/// which expresses the name of a user-defined conversion operator
844/// (C++ [class.conv.fct]p1). Returns the type that this operator is
845/// specifying a conversion for, or NULL if there was an error.
846///
847/// conversion-function-id: [C++ 12.3.2]
848/// operator conversion-type-id
849///
850/// conversion-type-id:
851/// type-specifier-seq conversion-declarator[opt]
852///
853/// conversion-declarator:
854/// ptr-operator conversion-declarator[opt]
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000855Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000856 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
857 ConsumeToken(); // 'operator'
858
859 // Parse the type-specifier-seq.
860 DeclSpec DS;
861 if (ParseCXXTypeSpecifierSeq(DS))
862 return 0;
863
864 // Parse the conversion-declarator, which is merely a sequence of
865 // ptr-operators.
866 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000867 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000868 if (EndLoc)
869 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000870
871 // Finish up the type.
872 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregorf8298252009-01-26 22:44:13 +0000873 if (Result.isInvalid())
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000874 return 0;
875 else
Douglas Gregorf8298252009-01-26 22:44:13 +0000876 return Result.get();
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000877}
Sebastian Redlbd150f42008-11-21 19:14:01 +0000878
879/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
880/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +0000881///
Chris Lattner109faf22009-01-04 21:25:24 +0000882/// This method is called to parse the new expression after the optional :: has
883/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
884/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +0000885///
886/// new-expression:
887/// '::'[opt] 'new' new-placement[opt] new-type-id
888/// new-initializer[opt]
889/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
890/// new-initializer[opt]
891///
892/// new-placement:
893/// '(' expression-list ')'
894///
Sebastian Redl351bb782008-12-02 14:43:59 +0000895/// new-type-id:
896/// type-specifier-seq new-declarator[opt]
897///
898/// new-declarator:
899/// ptr-operator new-declarator[opt]
900/// direct-new-declarator
901///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000902/// new-initializer:
903/// '(' expression-list[opt] ')'
904/// [C++0x] braced-init-list [TODO]
905///
Chris Lattner109faf22009-01-04 21:25:24 +0000906Parser::OwningExprResult
907Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
908 assert(Tok.is(tok::kw_new) && "expected 'new' token");
909 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +0000910
911 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
912 // second form of new-expression. It can't be a new-type-id.
913
Sebastian Redl511ed552008-11-25 22:21:31 +0000914 ExprVector PlacementArgs(Actions);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000915 SourceLocation PlacementLParen, PlacementRParen;
916
Sebastian Redlbd150f42008-11-21 19:14:01 +0000917 bool ParenTypeId;
Sebastian Redl351bb782008-12-02 14:43:59 +0000918 DeclSpec DS;
919 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000920 if (Tok.is(tok::l_paren)) {
921 // If it turns out to be a placement, we change the type location.
922 PlacementLParen = ConsumeParen();
Sebastian Redl351bb782008-12-02 14:43:59 +0000923 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
924 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +0000925 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +0000926 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000927
928 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redl351bb782008-12-02 14:43:59 +0000929 if (PlacementRParen.isInvalid()) {
930 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +0000931 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +0000932 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000933
Sebastian Redl351bb782008-12-02 14:43:59 +0000934 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +0000935 // Reset the placement locations. There was no placement.
936 PlacementLParen = PlacementRParen = SourceLocation();
937 ParenTypeId = true;
938 } else {
939 // We still need the type.
940 if (Tok.is(tok::l_paren)) {
Sebastian Redl351bb782008-12-02 14:43:59 +0000941 SourceLocation LParen = ConsumeParen();
942 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000943 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +0000944 ParseDeclarator(DeclaratorInfo);
945 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000946 ParenTypeId = true;
947 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +0000948 if (ParseCXXTypeSpecifierSeq(DS))
949 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000950 else {
951 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +0000952 ParseDeclaratorInternal(DeclaratorInfo,
953 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000954 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000955 ParenTypeId = false;
956 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000957 }
958 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +0000959 // A new-type-id is a simplified type-id, where essentially the
960 // direct-declarator is replaced by a direct-new-declarator.
961 if (ParseCXXTypeSpecifierSeq(DS))
962 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000963 else {
964 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +0000965 ParseDeclaratorInternal(DeclaratorInfo,
966 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000967 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000968 ParenTypeId = false;
969 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +0000970 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl351bb782008-12-02 14:43:59 +0000971 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +0000972 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +0000973 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000974
Sebastian Redl511ed552008-11-25 22:21:31 +0000975 ExprVector ConstructorArgs(Actions);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000976 SourceLocation ConstructorLParen, ConstructorRParen;
977
978 if (Tok.is(tok::l_paren)) {
979 ConstructorLParen = ConsumeParen();
980 if (Tok.isNot(tok::r_paren)) {
981 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +0000982 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
983 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +0000984 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +0000985 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000986 }
987 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redl351bb782008-12-02 14:43:59 +0000988 if (ConstructorRParen.isInvalid()) {
989 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +0000990 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +0000991 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000992 }
993
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000994 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
995 move_arg(PlacementArgs), PlacementRParen,
996 ParenTypeId, DeclaratorInfo, ConstructorLParen,
997 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000998}
999
Sebastian Redlbd150f42008-11-21 19:14:01 +00001000/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1001/// passed to ParseDeclaratorInternal.
1002///
1003/// direct-new-declarator:
1004/// '[' expression ']'
1005/// direct-new-declarator '[' constant-expression ']'
1006///
Chris Lattner109faf22009-01-04 21:25:24 +00001007void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001008 // Parse the array dimensions.
1009 bool first = true;
1010 while (Tok.is(tok::l_square)) {
1011 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001012 OwningExprResult Size(first ? ParseExpression()
1013 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001014 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001015 // Recover
1016 SkipUntil(tok::r_square);
1017 return;
1018 }
1019 first = false;
1020
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001021 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001022 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor04318252009-07-06 15:59:29 +00001023 Size.release(), LLoc, RLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001024 RLoc);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001025
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001026 if (RLoc.isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00001027 return;
1028 }
1029}
1030
1031/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1032/// This ambiguity appears in the syntax of the C++ new operator.
1033///
1034/// new-expression:
1035/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1036/// new-initializer[opt]
1037///
1038/// new-placement:
1039/// '(' expression-list ')'
1040///
Sebastian Redl351bb782008-12-02 14:43:59 +00001041bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00001042 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001043 // The '(' was already consumed.
1044 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00001045 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001046 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00001047 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00001048 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00001049 }
1050
1051 // It's not a type, it has to be an expression list.
1052 // Discard the comma locations - ActOnCXXNew has enough parameters.
1053 CommaLocsTy CommaLocs;
1054 return ParseExpressionList(PlacementArgs, CommaLocs);
1055}
1056
1057/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1058/// to free memory allocated by new.
1059///
Chris Lattner109faf22009-01-04 21:25:24 +00001060/// This method is called to parse the 'delete' expression after the optional
1061/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1062/// and "Start" is its location. Otherwise, "Start" is the location of the
1063/// 'delete' token.
1064///
Sebastian Redlbd150f42008-11-21 19:14:01 +00001065/// delete-expression:
1066/// '::'[opt] 'delete' cast-expression
1067/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001068Parser::OwningExprResult
1069Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1070 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1071 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00001072
1073 // Array delete?
1074 bool ArrayDelete = false;
1075 if (Tok.is(tok::l_square)) {
1076 ArrayDelete = true;
1077 SourceLocation LHS = ConsumeBracket();
1078 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1079 if (RHS.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001080 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00001081 }
1082
Sebastian Redl59b5e512008-12-11 21:36:32 +00001083 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001084 if (Operand.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001085 return move(Operand);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001086
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001087 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001088}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001089
Mike Stump11289f42009-09-09 15:08:12 +00001090static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001091 switch(kind) {
1092 default: assert(false && "Not a known unary type trait.");
1093 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1094 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1095 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1096 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1097 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1098 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1099 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1100 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1101 case tok::kw___is_abstract: return UTT_IsAbstract;
1102 case tok::kw___is_class: return UTT_IsClass;
1103 case tok::kw___is_empty: return UTT_IsEmpty;
1104 case tok::kw___is_enum: return UTT_IsEnum;
1105 case tok::kw___is_pod: return UTT_IsPOD;
1106 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1107 case tok::kw___is_union: return UTT_IsUnion;
1108 }
1109}
1110
1111/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1112/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1113/// templates.
1114///
1115/// primary-expression:
1116/// [GNU] unary-type-trait '(' type-id ')'
1117///
Mike Stump11289f42009-09-09 15:08:12 +00001118Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001119 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1120 SourceLocation Loc = ConsumeToken();
1121
1122 SourceLocation LParen = Tok.getLocation();
1123 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1124 return ExprError();
1125
1126 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1127 // there will be cryptic errors about mismatched parentheses and missing
1128 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00001129 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001130
1131 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1132
Douglas Gregor220cac52009-02-18 17:45:20 +00001133 if (Ty.isInvalid())
1134 return ExprError();
1135
1136 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001137}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001138
1139/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1140/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1141/// based on the context past the parens.
1142Parser::OwningExprResult
1143Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1144 TypeTy *&CastTy,
1145 SourceLocation LParenLoc,
1146 SourceLocation &RParenLoc) {
1147 assert(getLang().CPlusPlus && "Should only be called for C++!");
1148 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1149 assert(isTypeIdInParens() && "Not a type-id!");
1150
1151 OwningExprResult Result(Actions, true);
1152 CastTy = 0;
1153
1154 // We need to disambiguate a very ugly part of the C++ syntax:
1155 //
1156 // (T())x; - type-id
1157 // (T())*x; - type-id
1158 // (T())/x; - expression
1159 // (T()); - expression
1160 //
1161 // The bad news is that we cannot use the specialized tentative parser, since
1162 // it can only verify that the thing inside the parens can be parsed as
1163 // type-id, it is not useful for determining the context past the parens.
1164 //
1165 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00001166 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001167 //
1168 // It uses a scheme similar to parsing inline methods. The parenthesized
1169 // tokens are cached, the context that follows is determined (possibly by
1170 // parsing a cast-expression), and then we re-introduce the cached tokens
1171 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001172
Mike Stump11289f42009-09-09 15:08:12 +00001173 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001174 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001175
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001176 // Store the tokens of the parentheses. We will parse them after we determine
1177 // the context that follows them.
1178 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1179 // We didn't find the ')' we expected.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001180 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1181 return ExprError();
1182 }
1183
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001184 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001185 ParseAs = CompoundLiteral;
1186 } else {
1187 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00001188 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1189 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1190 NotCastExpr = true;
1191 } else {
1192 // Try parsing the cast-expression that may follow.
1193 // If it is not a cast-expression, NotCastExpr will be true and no token
1194 // will be consumed.
1195 Result = ParseCastExpression(false/*isUnaryExpression*/,
1196 false/*isAddressofOperand*/,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001197 NotCastExpr, false);
Eli Friedmancf7530f2009-05-25 19:41:42 +00001198 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001199
1200 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1201 // an expression.
1202 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001203 }
1204
Mike Stump11289f42009-09-09 15:08:12 +00001205 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001206 Toks.push_back(Tok);
1207 // Re-enter the stored parenthesized tokens into the token stream, so we may
1208 // parse them now.
1209 PP.EnterTokenStream(Toks.data(), Toks.size(),
1210 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1211 // Drop the current token and bring the first cached one. It's the same token
1212 // as when we entered this function.
1213 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001214
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001215 if (ParseAs >= CompoundLiteral) {
1216 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001217
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001218 // Match the ')'.
1219 if (Tok.is(tok::r_paren))
1220 RParenLoc = ConsumeParen();
1221 else
1222 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001223
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001224 if (ParseAs == CompoundLiteral) {
1225 ExprType = CompoundLiteral;
1226 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1227 }
Mike Stump11289f42009-09-09 15:08:12 +00001228
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001229 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1230 assert(ParseAs == CastExpr);
1231
1232 if (Ty.isInvalid())
1233 return ExprError();
1234
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001235 CastTy = Ty.get();
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001236
1237 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001238 if (!Result.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001239 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001240 move(Result));
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001241 return move(Result);
1242 }
Mike Stump11289f42009-09-09 15:08:12 +00001243
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001244 // Not a compound literal, and not followed by a cast-expression.
1245 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001246
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001247 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00001248 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001249 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1250 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1251
1252 // Match the ')'.
1253 if (Result.isInvalid()) {
1254 SkipUntil(tok::r_paren);
1255 return ExprError();
1256 }
Mike Stump11289f42009-09-09 15:08:12 +00001257
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001258 if (Tok.is(tok::r_paren))
1259 RParenLoc = ConsumeParen();
1260 else
1261 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1262
1263 return move(Result);
1264}