blob: 4c0f699b3f0a98f5bcde97b7c02bba2c75f1549e [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000017using namespace clang;
18
Mike Stump1eb44332009-09-09 15:08:12 +000019/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000020///
21/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +000022/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +000023/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-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 Gregor2dd078a2009-09-02 22:59:36 +000032/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000033///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000034///
Mike Stump1eb44332009-09-09 15:08:12 +000035/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +000036/// nested-name-specifier (or empty)
37///
Mike Stump1eb44332009-09-09 15:08:12 +000038/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-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 Gregor495c35d2009-08-25 22:51:20 +000046bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000047 Action::TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +000048 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000049 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000050 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +000051
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000052 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000053 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000054 SS.setRange(Tok.getAnnotationRange());
55 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000056 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000057 }
Chris Lattnere607e802009-01-04 21:14:15 +000058
Douglas Gregor39a8de12009-02-25 19:37:18 +000059 bool HasScopeSpecifier = false;
60
Chris Lattner5b454732009-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 Stump1eb44332009-09-09 15:08:12 +000066
Chris Lattner55a7cef2009-01-05 00:13:00 +000067 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000068 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000069 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000070 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000071 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000072 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000073 }
74
Douglas Gregor39a8de12009-02-25 19:37:18 +000075 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 if (HasScopeSpecifier) {
77 // C++ [basic.lookup.classref]p5:
78 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000079 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +000080 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000081 //
Douglas Gregor2dd078a2009-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;
Douglas Gregor81b747b2009-09-17 21:32:03 +000088
89 if (Tok.is(tok::code_completion)) {
90 // Code completion for a nested-name-specifier, where the code
91 // code completion token follows the '::'.
92 Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
93 ConsumeToken();
94 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +000095 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Douglas Gregor39a8de12009-02-25 19:37:18 +000097 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +000098 // nested-name-specifier 'template'[opt] simple-template-id '::'
99
100 // Parse the optional 'template' keyword, then make sure we have
101 // 'identifier <' after it.
102 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000103 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000104 // nested-name-specifier, since they aren't allowed to start with
105 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000106 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000107 break;
108
Chris Lattner77cf72a2009-06-26 03:47:46 +0000109 SourceLocation TemplateKWLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Chris Lattner77cf72a2009-06-26 03:47:46 +0000111 if (Tok.isNot(tok::identifier)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112 Diag(Tok.getLocation(),
Chris Lattner77cf72a2009-06-26 03:47:46 +0000113 diag::err_id_after_template_in_nested_name_spec)
114 << SourceRange(TemplateKWLoc);
115 break;
116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner77cf72a2009-06-26 03:47:46 +0000118 if (NextToken().isNot(tok::less)) {
119 Diag(NextToken().getLocation(),
120 diag::err_less_after_template_name_in_nested_name_spec)
121 << Tok.getIdentifierInfo()->getName()
122 << SourceRange(TemplateKWLoc, Tok.getLocation());
123 break;
124 }
Mike Stump1eb44332009-09-09 15:08:12 +0000125
126 TemplateTy Template
Chris Lattner77cf72a2009-06-26 03:47:46 +0000127 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
128 *Tok.getIdentifierInfo(),
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000129 Tok.getLocation(), SS,
130 ObjectType);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000131 if (!Template)
132 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000133 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
134 &SS, TemplateKWLoc, false))
135 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Chris Lattner77cf72a2009-06-26 03:47:46 +0000137 continue;
138 }
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Douglas Gregor39a8de12009-02-25 19:37:18 +0000140 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000141 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000142 //
143 // simple-template-id '::'
144 //
145 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000146 // right kind (it should name a type or be dependent), and then
147 // convert it into a type within the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000148 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000149 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
150
Mike Stump1eb44332009-09-09 15:08:12 +0000151 if (TemplateId->Kind == TNK_Type_template ||
Douglas Gregorc45c2322009-03-31 00:43:58 +0000152 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000153 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000154
Mike Stump1eb44332009-09-09 15:08:12 +0000155 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor39a8de12009-02-25 19:37:18 +0000156 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000157 Token TypeToken = Tok;
158 ConsumeToken();
159 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
160 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Douglas Gregor39a8de12009-02-25 19:37:18 +0000162 if (!HasScopeSpecifier) {
163 SS.setBeginLoc(TypeToken.getLocation());
164 HasScopeSpecifier = true;
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Douglas Gregor31a19b62009-04-01 21:51:26 +0000167 if (TypeToken.getAnnotationValue())
168 SS.setScopeRep(
Mike Stump1eb44332009-09-09 15:08:12 +0000169 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000170 TypeToken.getAnnotationValue(),
171 TypeToken.getAnnotationRange(),
172 CCLoc));
173 else
174 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000175 SS.setEndLoc(CCLoc);
176 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner67b9e832009-06-26 03:45:46 +0000179 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000180 }
181
Chris Lattner5c7f7862009-06-26 03:52:38 +0000182
183 // The rest of the nested-name-specifier possibilities start with
184 // tok::identifier.
185 if (Tok.isNot(tok::identifier))
186 break;
187
188 IdentifierInfo &II = *Tok.getIdentifierInfo();
189
190 // nested-name-specifier:
191 // type-name '::'
192 // namespace-name '::'
193 // nested-name-specifier identifier '::'
194 Token Next = NextToken();
195 if (Next.is(tok::coloncolon)) {
196 // We have an identifier followed by a '::'. Lookup this name
197 // as the name in a nested-name-specifier.
198 SourceLocation IdLoc = ConsumeToken();
199 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
200 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Chris Lattner5c7f7862009-06-26 03:52:38 +0000202 if (!HasScopeSpecifier) {
203 SS.setBeginLoc(IdLoc);
204 HasScopeSpecifier = true;
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Chris Lattner5c7f7862009-06-26 03:52:38 +0000207 if (SS.isInvalid())
208 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Chris Lattner5c7f7862009-06-26 03:52:38 +0000210 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000211 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000212 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000213 SS.setEndLoc(CCLoc);
214 continue;
215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Chris Lattner5c7f7862009-06-26 03:52:38 +0000217 // nested-name-specifier:
218 // type-name '<'
219 if (Next.is(tok::less)) {
220 TemplateTy Template;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000221 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, II,
222 Tok.getLocation(),
223 &SS,
224 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000225 EnteringContext,
226 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000227 // We have found a template name, so annotate this this token
228 // with a template-id annotation. We do not permit the
229 // template-id to be translated into a type annotation,
230 // because some clients (e.g., the parsing of class template
231 // specializations) still want to see the original template-id
232 // token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000233 if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(),
234 false))
235 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000236 continue;
237 }
238 }
239
Douglas Gregor39a8de12009-02-25 19:37:18 +0000240 // We don't have any tokens that form the beginning of a
241 // nested-name-specifier, so we're done.
242 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000243 }
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Douglas Gregor39a8de12009-02-25 19:37:18 +0000245 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000246}
247
248/// ParseCXXIdExpression - Handle id-expression.
249///
250/// id-expression:
251/// unqualified-id
252/// qualified-id
253///
254/// unqualified-id:
255/// identifier
256/// operator-function-id
257/// conversion-function-id [TODO]
258/// '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000259/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000260///
261/// qualified-id:
262/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
263/// '::' identifier
264/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000265/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000266///
267/// nested-name-specifier:
268/// type-name '::'
269/// namespace-name '::'
270/// nested-name-specifier identifier '::'
271/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
272///
273/// NOTE: The standard specifies that, for qualified-id, the parser does not
274/// expect:
275///
276/// '::' conversion-function-id
277/// '::' '~' class-name
278///
279/// This may cause a slight inconsistency on diagnostics:
280///
281/// class C {};
282/// namespace A {}
283/// void f() {
284/// :: A :: ~ C(); // Some Sema error about using destructor with a
285/// // namespace.
286/// :: ~ C(); // Some Parser error like 'unexpected ~'.
287/// }
288///
289/// We simplify the parser a bit and make it work like:
290///
291/// qualified-id:
292/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
293/// '::' unqualified-id
294///
295/// That way Sema can handle and report similar errors for namespaces and the
296/// global scope.
297///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000298/// The isAddressOfOperand parameter indicates that this id-expression is a
299/// direct operand of the address-of operator. This is, besides member contexts,
300/// the only place where a qualified-id naming a non-static class member may
301/// appear.
302///
303Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000304 // qualified-id:
305 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
306 // '::' unqualified-id
307 //
308 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000309 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000310
311 // unqualified-id:
312 // identifier
313 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000314 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000315 // '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000316 // template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000317 //
318 switch (Tok.getKind()) {
319 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000320 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000321
322 case tok::identifier: {
323 // Consume the identifier so that we can see if it is followed by a '('.
324 IdentifierInfo &II = *Tok.getIdentifierInfo();
325 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000326 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
327 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000328 }
329
330 case tok::kw_operator: {
331 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000332 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000333 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000334 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
335 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000336 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000337 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000338 Tok.is(tok::l_paren), SS,
339 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000340
Douglas Gregor2def4832008-11-17 20:34:05 +0000341 // We already complained about a bad conversion-function-id,
342 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000343 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000344 }
345
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000346 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +0000347 TemplateIdAnnotation *TemplateId
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000348 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
349 assert((TemplateId->Kind == TNK_Function_template ||
350 TemplateId->Kind == TNK_Dependent_template_name) &&
351 "A template type name is not an ID expression");
352
Mike Stump1eb44332009-09-09 15:08:12 +0000353 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000354 TemplateId->getTemplateArgs(),
355 TemplateId->getTemplateArgIsType(),
356 TemplateId->NumArgs);
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000358 OwningExprResult Result
359 = Actions.ActOnTemplateIdExpr(TemplateTy::make(TemplateId->Template),
360 TemplateId->TemplateNameLoc,
361 TemplateId->LAngleLoc,
362 TemplateArgsPtr,
363 TemplateId->getTemplateArgLocations(),
364 TemplateId->RAngleLoc);
365 ConsumeToken(); // Consume the template-id token
366 return move(Result);
367 }
368
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000369 } // switch.
370
371 assert(0 && "The switch was supposed to take care everything.");
372}
373
Reid Spencer5f016e22007-07-11 17:01:13 +0000374/// ParseCXXCasts - This handles the various ways to cast expressions to another
375/// type.
376///
377/// postfix-expression: [C++ 5.2p1]
378/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
379/// 'static_cast' '<' type-name '>' '(' expression ')'
380/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
381/// 'const_cast' '<' type-name '>' '(' expression ')'
382///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000383Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 tok::TokenKind Kind = Tok.getKind();
385 const char *CastName = 0; // For error messages
386
387 switch (Kind) {
388 default: assert(0 && "Unknown C++ cast!"); abort();
389 case tok::kw_const_cast: CastName = "const_cast"; break;
390 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
391 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
392 case tok::kw_static_cast: CastName = "static_cast"; break;
393 }
394
395 SourceLocation OpLoc = ConsumeToken();
396 SourceLocation LAngleBracketLoc = Tok.getLocation();
397
398 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000399 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000400
Douglas Gregor809070a2009-02-18 17:45:20 +0000401 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 SourceLocation RAngleBracketLoc = Tok.getLocation();
403
Chris Lattner1ab3b962008-11-18 07:48:38 +0000404 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000405 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000406
407 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
408
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000409 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
410 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000411
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000412 OwningExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000414 // Match the ')'.
415 if (Result.isInvalid())
416 SkipUntil(tok::r_paren);
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000418 if (Tok.is(tok::r_paren))
419 RParenLoc = ConsumeParen();
420 else
421 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000422
Douglas Gregor809070a2009-02-18 17:45:20 +0000423 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000424 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000425 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000426 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000427 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000428
Sebastian Redl20df9b72008-12-11 22:51:44 +0000429 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000430}
431
Sebastian Redlc42e1182008-11-11 11:37:55 +0000432/// ParseCXXTypeid - This handles the C++ typeid expression.
433///
434/// postfix-expression: [C++ 5.2p1]
435/// 'typeid' '(' expression ')'
436/// 'typeid' '(' type-id ')'
437///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000438Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000439 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
440
441 SourceLocation OpLoc = ConsumeToken();
442 SourceLocation LParenLoc = Tok.getLocation();
443 SourceLocation RParenLoc;
444
445 // typeid expressions are always parenthesized.
446 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
447 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000448 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000449
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000450 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000451
452 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000453 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000454
455 // Match the ')'.
456 MatchRHSPunctuation(tok::r_paren, LParenLoc);
457
Douglas Gregor809070a2009-02-18 17:45:20 +0000458 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000459 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000460
461 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000462 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000463 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000464 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +0000465 // When typeid is applied to an expression other than an lvalue of a
466 // polymorphic class type [...] The expression is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +0000467 // operand (Clause 5).
468 //
Mike Stump1eb44332009-09-09 15:08:12 +0000469 // Note that we can't tell whether the expression is an lvalue of a
Douglas Gregore0762c92009-06-19 23:52:42 +0000470 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000471 // we the expression is potentially potentially evaluated.
472 EnterExpressionEvaluationContext Unevaluated(Actions,
473 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000474 Result = ParseExpression();
475
476 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000477 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000478 SkipUntil(tok::r_paren);
479 else {
480 MatchRHSPunctuation(tok::r_paren, LParenLoc);
481
482 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000483 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000484 }
485 }
486
Sebastian Redl20df9b72008-12-11 22:51:44 +0000487 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000488}
489
Reid Spencer5f016e22007-07-11 17:01:13 +0000490/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
491///
492/// boolean-literal: [C++ 2.13.5]
493/// 'true'
494/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000495Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000497 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000498}
Chris Lattner50dd2892008-02-26 00:51:44 +0000499
500/// ParseThrowExpression - This handles the C++ throw expression.
501///
502/// throw-expression: [C++ 15]
503/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000504Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000505 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000506 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000507
Chris Lattner2a2819a2008-04-06 06:02:23 +0000508 // If the current token isn't the start of an assignment-expression,
509 // then the expression is not present. This handles things like:
510 // "C ? throw : (void)42", which is crazy but legal.
511 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
512 case tok::semi:
513 case tok::r_paren:
514 case tok::r_square:
515 case tok::r_brace:
516 case tok::colon:
517 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000518 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000519
Chris Lattner2a2819a2008-04-06 06:02:23 +0000520 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000521 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000522 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000523 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000524 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000525}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000526
527/// ParseCXXThis - This handles the C++ 'this' pointer.
528///
529/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
530/// a non-lvalue expression whose value is the address of the object for which
531/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000532Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000533 assert(Tok.is(tok::kw_this) && "Not 'this'!");
534 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000535 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000536}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000537
538/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
539/// Can be interpreted either as function-style casting ("int(x)")
540/// or class type construction ("ClassType(x,y,z)")
541/// or creation of a value-initialized type ("int()").
542///
543/// postfix-expression: [C++ 5.2p1]
544/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
545/// typename-specifier '(' expression-list[opt] ')' [TODO]
546///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000547Parser::OwningExprResult
548Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000549 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000550 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000551
552 assert(Tok.is(tok::l_paren) && "Expected '('!");
553 SourceLocation LParenLoc = ConsumeParen();
554
Sebastian Redla55e52c2008-11-25 22:21:31 +0000555 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000556 CommaLocsTy CommaLocs;
557
558 if (Tok.isNot(tok::r_paren)) {
559 if (ParseExpressionList(Exprs, CommaLocs)) {
560 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000561 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000562 }
563 }
564
565 // Match the ')'.
566 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
567
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000568 // TypeRep could be null, if it references an invalid typedef.
569 if (!TypeRep)
570 return ExprError();
571
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000572 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
573 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000574 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
575 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000576 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000577}
578
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000579/// ParseCXXCondition - if/switch/while/for condition expression.
580///
581/// condition:
582/// expression
583/// type-specifier-seq declarator '=' assignment-expression
584/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
585/// '=' assignment-expression
586///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000587Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000588 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000589 return ParseExpression(); // expression
590
591 SourceLocation StartLoc = Tok.getLocation();
592
593 // type-specifier-seq
594 DeclSpec DS;
595 ParseSpecifierQualifierList(DS);
596
597 // declarator
598 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
599 ParseDeclarator(DeclaratorInfo);
600
601 // simple-asm-expr[opt]
602 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000603 SourceLocation Loc;
604 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000605 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000606 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000607 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000608 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000609 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000610 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000611 }
612
613 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000614 if (Tok.is(tok::kw___attribute)) {
615 SourceLocation Loc;
616 AttributeList *AttrList = ParseAttributes(&Loc);
617 DeclaratorInfo.AddAttributes(AttrList, Loc);
618 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000619
620 // '=' assignment-expression
621 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000622 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000623 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000624 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000625 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000626 return ExprError();
627
Sebastian Redlf53597f2009-03-15 17:47:39 +0000628 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
629 DeclaratorInfo,EqualLoc,
630 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000631}
632
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000633/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
634/// This should only be called when the current token is known to be part of
635/// simple-type-specifier.
636///
637/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000638/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000639/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
640/// char
641/// wchar_t
642/// bool
643/// short
644/// int
645/// long
646/// signed
647/// unsigned
648/// float
649/// double
650/// void
651/// [GNU] typeof-specifier
652/// [C++0x] auto [TODO]
653///
654/// type-name:
655/// class-name
656/// enum-name
657/// typedef-name
658///
659void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
660 DS.SetRangeStart(Tok.getLocation());
661 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000662 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000663 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000665 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000666 case tok::identifier: // foo::bar
667 case tok::coloncolon: // ::foo::bar
668 assert(0 && "Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +0000669 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000670 assert(0 && "Not a simple-type-specifier token!");
671 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000672
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000673 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000674 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000675 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000676 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000677 break;
678 }
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000680 // builtin types
681 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000682 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000683 break;
684 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000685 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000686 break;
687 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000688 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000689 break;
690 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000691 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000692 break;
693 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000694 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000695 break;
696 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000697 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000698 break;
699 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000700 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000701 break;
702 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000703 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000704 break;
705 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000706 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000707 break;
708 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000709 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000710 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000711 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000712 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000713 break;
714 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000715 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000716 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000717 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000718 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000719 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000721 // GNU typeof support.
722 case tok::kw_typeof:
723 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000724 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000725 return;
726 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000727 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000728 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
729 else
730 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000731 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000732 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000733}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000734
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000735/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
736/// [dcl.name]), which is a non-empty sequence of type-specifiers,
737/// e.g., "const short int". Note that the DeclSpec is *not* finished
738/// by parsing the type-specifier-seq, because these sequences are
739/// typically followed by some form of declarator. Returns true and
740/// emits diagnostics if this is not a type-specifier-seq, false
741/// otherwise.
742///
743/// type-specifier-seq: [C++ 8.1]
744/// type-specifier type-specifier-seq[opt]
745///
746bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
747 DS.SetRangeStart(Tok.getLocation());
748 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000749 unsigned DiagID;
750 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000751
752 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000753 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000754 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000755 return true;
756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
John McCallfec54012009-08-03 20:12:06 +0000758 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000759
760 return false;
761}
762
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000763/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000764/// operator name (C++ [over.oper]). If successful, returns the
765/// predefined identifier that corresponds to that overloaded
766/// operator. Otherwise, returns NULL and does not consume any tokens.
767///
768/// operator-function-id: [C++ 13.5]
769/// 'operator' operator
770///
771/// operator: one of
772/// new delete new[] delete[]
773/// + - * / % ^ & | ~
774/// ! = < > += -= *= /= %=
775/// ^= &= |= << >> >>= <<= == !=
776/// <= >= && || ++ -- , ->* ->
777/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +0000778OverloadedOperatorKind
779Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000780 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +0000781 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000782
783 OverloadedOperatorKind Op = OO_None;
784 switch (NextToken().getKind()) {
785 case tok::kw_new:
786 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000787 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000788 if (Tok.is(tok::l_square)) {
789 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000790 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000791 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
792 Op = OO_Array_New;
793 } else {
794 Op = OO_New;
795 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000796 if (EndLoc)
797 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000798 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000799
800 case tok::kw_delete:
801 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000802 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000803 if (Tok.is(tok::l_square)) {
804 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000805 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000806 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
807 Op = OO_Array_Delete;
808 } else {
809 Op = OO_Delete;
810 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000811 if (EndLoc)
812 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000813 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000814
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000815#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000816 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000817#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000818#include "clang/Basic/OperatorKinds.def"
819
820 case tok::l_paren:
821 ConsumeToken(); // 'operator'
822 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +0000823 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000824 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000825 if (EndLoc)
826 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000827 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000828
829 case tok::l_square:
830 ConsumeToken(); // 'operator'
831 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000832 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000833 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000834 if (EndLoc)
835 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000836 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000837
838 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000839 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000840 }
841
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000842 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000843 Loc = ConsumeAnyToken(); // the operator itself
844 if (EndLoc)
845 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000846 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000847}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000848
849/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
850/// which expresses the name of a user-defined conversion operator
851/// (C++ [class.conv.fct]p1). Returns the type that this operator is
852/// specifying a conversion for, or NULL if there was an error.
853///
854/// conversion-function-id: [C++ 12.3.2]
855/// operator conversion-type-id
856///
857/// conversion-type-id:
858/// type-specifier-seq conversion-declarator[opt]
859///
860/// conversion-declarator:
861/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +0000862Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000863 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
864 ConsumeToken(); // 'operator'
865
866 // Parse the type-specifier-seq.
867 DeclSpec DS;
868 if (ParseCXXTypeSpecifierSeq(DS))
869 return 0;
870
871 // Parse the conversion-declarator, which is merely a sequence of
872 // ptr-operators.
873 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000874 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000875 if (EndLoc)
876 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000877
878 // Finish up the type.
879 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000880 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000881 return 0;
882 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000883 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000884}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000885
886/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
887/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +0000888///
Chris Lattner59232d32009-01-04 21:25:24 +0000889/// This method is called to parse the new expression after the optional :: has
890/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
891/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000892///
893/// new-expression:
894/// '::'[opt] 'new' new-placement[opt] new-type-id
895/// new-initializer[opt]
896/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
897/// new-initializer[opt]
898///
899/// new-placement:
900/// '(' expression-list ')'
901///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000902/// new-type-id:
903/// type-specifier-seq new-declarator[opt]
904///
905/// new-declarator:
906/// ptr-operator new-declarator[opt]
907/// direct-new-declarator
908///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000909/// new-initializer:
910/// '(' expression-list[opt] ')'
911/// [C++0x] braced-init-list [TODO]
912///
Chris Lattner59232d32009-01-04 21:25:24 +0000913Parser::OwningExprResult
914Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
915 assert(Tok.is(tok::kw_new) && "expected 'new' token");
916 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000917
918 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
919 // second form of new-expression. It can't be a new-type-id.
920
Sebastian Redla55e52c2008-11-25 22:21:31 +0000921 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000922 SourceLocation PlacementLParen, PlacementRParen;
923
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000924 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000925 DeclSpec DS;
926 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000927 if (Tok.is(tok::l_paren)) {
928 // If it turns out to be a placement, we change the type location.
929 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000930 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
931 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000932 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000933 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000934
935 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000936 if (PlacementRParen.isInvalid()) {
937 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000938 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000939 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000940
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000941 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000942 // Reset the placement locations. There was no placement.
943 PlacementLParen = PlacementRParen = SourceLocation();
944 ParenTypeId = true;
945 } else {
946 // We still need the type.
947 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000948 SourceLocation LParen = ConsumeParen();
949 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000950 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000951 ParseDeclarator(DeclaratorInfo);
952 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000953 ParenTypeId = true;
954 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000955 if (ParseCXXTypeSpecifierSeq(DS))
956 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000957 else {
958 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000959 ParseDeclaratorInternal(DeclaratorInfo,
960 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000961 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000962 ParenTypeId = false;
963 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000964 }
965 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000966 // A new-type-id is a simplified type-id, where essentially the
967 // direct-declarator is replaced by a direct-new-declarator.
968 if (ParseCXXTypeSpecifierSeq(DS))
969 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000970 else {
971 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000972 ParseDeclaratorInternal(DeclaratorInfo,
973 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000974 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000975 ParenTypeId = false;
976 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000977 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000978 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000979 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000980 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000981
Sebastian Redla55e52c2008-11-25 22:21:31 +0000982 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000983 SourceLocation ConstructorLParen, ConstructorRParen;
984
985 if (Tok.is(tok::l_paren)) {
986 ConstructorLParen = ConsumeParen();
987 if (Tok.isNot(tok::r_paren)) {
988 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000989 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
990 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000991 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000992 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000993 }
994 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000995 if (ConstructorRParen.isInvalid()) {
996 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000997 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000998 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000999 }
1000
Sebastian Redlf53597f2009-03-15 17:47:39 +00001001 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
1002 move_arg(PlacementArgs), PlacementRParen,
1003 ParenTypeId, DeclaratorInfo, ConstructorLParen,
1004 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001005}
1006
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001007/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
1008/// passed to ParseDeclaratorInternal.
1009///
1010/// direct-new-declarator:
1011/// '[' expression ']'
1012/// direct-new-declarator '[' constant-expression ']'
1013///
Chris Lattner59232d32009-01-04 21:25:24 +00001014void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001015 // Parse the array dimensions.
1016 bool first = true;
1017 while (Tok.is(tok::l_square)) {
1018 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001019 OwningExprResult Size(first ? ParseExpression()
1020 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001021 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001022 // Recover
1023 SkipUntil(tok::r_square);
1024 return;
1025 }
1026 first = false;
1027
Sebastian Redlab197ba2009-02-09 18:23:29 +00001028 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001029 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001030 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001031 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001032
Sebastian Redlab197ba2009-02-09 18:23:29 +00001033 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001034 return;
1035 }
1036}
1037
1038/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1039/// This ambiguity appears in the syntax of the C++ new operator.
1040///
1041/// new-expression:
1042/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1043/// new-initializer[opt]
1044///
1045/// new-placement:
1046/// '(' expression-list ')'
1047///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001048bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001049 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001050 // The '(' was already consumed.
1051 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001052 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001053 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001054 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001055 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001056 }
1057
1058 // It's not a type, it has to be an expression list.
1059 // Discard the comma locations - ActOnCXXNew has enough parameters.
1060 CommaLocsTy CommaLocs;
1061 return ParseExpressionList(PlacementArgs, CommaLocs);
1062}
1063
1064/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1065/// to free memory allocated by new.
1066///
Chris Lattner59232d32009-01-04 21:25:24 +00001067/// This method is called to parse the 'delete' expression after the optional
1068/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1069/// and "Start" is its location. Otherwise, "Start" is the location of the
1070/// 'delete' token.
1071///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001072/// delete-expression:
1073/// '::'[opt] 'delete' cast-expression
1074/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001075Parser::OwningExprResult
1076Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1077 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1078 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001079
1080 // Array delete?
1081 bool ArrayDelete = false;
1082 if (Tok.is(tok::l_square)) {
1083 ArrayDelete = true;
1084 SourceLocation LHS = ConsumeBracket();
1085 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1086 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001087 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001088 }
1089
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001090 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001091 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001092 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001093
Sebastian Redlf53597f2009-03-15 17:47:39 +00001094 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001095}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001096
Mike Stump1eb44332009-09-09 15:08:12 +00001097static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001098 switch(kind) {
1099 default: assert(false && "Not a known unary type trait.");
1100 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1101 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1102 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1103 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1104 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1105 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1106 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1107 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1108 case tok::kw___is_abstract: return UTT_IsAbstract;
1109 case tok::kw___is_class: return UTT_IsClass;
1110 case tok::kw___is_empty: return UTT_IsEmpty;
1111 case tok::kw___is_enum: return UTT_IsEnum;
1112 case tok::kw___is_pod: return UTT_IsPOD;
1113 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1114 case tok::kw___is_union: return UTT_IsUnion;
1115 }
1116}
1117
1118/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1119/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1120/// templates.
1121///
1122/// primary-expression:
1123/// [GNU] unary-type-trait '(' type-id ')'
1124///
Mike Stump1eb44332009-09-09 15:08:12 +00001125Parser::OwningExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001126 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1127 SourceLocation Loc = ConsumeToken();
1128
1129 SourceLocation LParen = Tok.getLocation();
1130 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1131 return ExprError();
1132
1133 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1134 // there will be cryptic errors about mismatched parentheses and missing
1135 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001136 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001137
1138 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1139
Douglas Gregor809070a2009-02-18 17:45:20 +00001140 if (Ty.isInvalid())
1141 return ExprError();
1142
1143 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001144}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001145
1146/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1147/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1148/// based on the context past the parens.
1149Parser::OwningExprResult
1150Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1151 TypeTy *&CastTy,
1152 SourceLocation LParenLoc,
1153 SourceLocation &RParenLoc) {
1154 assert(getLang().CPlusPlus && "Should only be called for C++!");
1155 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1156 assert(isTypeIdInParens() && "Not a type-id!");
1157
1158 OwningExprResult Result(Actions, true);
1159 CastTy = 0;
1160
1161 // We need to disambiguate a very ugly part of the C++ syntax:
1162 //
1163 // (T())x; - type-id
1164 // (T())*x; - type-id
1165 // (T())/x; - expression
1166 // (T()); - expression
1167 //
1168 // The bad news is that we cannot use the specialized tentative parser, since
1169 // it can only verify that the thing inside the parens can be parsed as
1170 // type-id, it is not useful for determining the context past the parens.
1171 //
1172 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001173 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001174 //
1175 // It uses a scheme similar to parsing inline methods. The parenthesized
1176 // tokens are cached, the context that follows is determined (possibly by
1177 // parsing a cast-expression), and then we re-introduce the cached tokens
1178 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001179
Mike Stump1eb44332009-09-09 15:08:12 +00001180 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001181 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001182
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001183 // Store the tokens of the parentheses. We will parse them after we determine
1184 // the context that follows them.
1185 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1186 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001187 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1188 return ExprError();
1189 }
1190
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001191 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001192 ParseAs = CompoundLiteral;
1193 } else {
1194 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001195 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1196 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1197 NotCastExpr = true;
1198 } else {
1199 // Try parsing the cast-expression that may follow.
1200 // If it is not a cast-expression, NotCastExpr will be true and no token
1201 // will be consumed.
1202 Result = ParseCastExpression(false/*isUnaryExpression*/,
1203 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001204 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001205 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001206
1207 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1208 // an expression.
1209 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001210 }
1211
Mike Stump1eb44332009-09-09 15:08:12 +00001212 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001213 Toks.push_back(Tok);
1214 // Re-enter the stored parenthesized tokens into the token stream, so we may
1215 // parse them now.
1216 PP.EnterTokenStream(Toks.data(), Toks.size(),
1217 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1218 // Drop the current token and bring the first cached one. It's the same token
1219 // as when we entered this function.
1220 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001221
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001222 if (ParseAs >= CompoundLiteral) {
1223 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001224
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001225 // Match the ')'.
1226 if (Tok.is(tok::r_paren))
1227 RParenLoc = ConsumeParen();
1228 else
1229 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001230
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001231 if (ParseAs == CompoundLiteral) {
1232 ExprType = CompoundLiteral;
1233 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1234 }
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001236 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1237 assert(ParseAs == CastExpr);
1238
1239 if (Ty.isInvalid())
1240 return ExprError();
1241
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001242 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001243
1244 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001245 if (!Result.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001246 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001247 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001248 return move(Result);
1249 }
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001251 // Not a compound literal, and not followed by a cast-expression.
1252 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001253
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001254 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001255 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001256 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1257 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1258
1259 // Match the ')'.
1260 if (Result.isInvalid()) {
1261 SkipUntil(tok::r_paren);
1262 return ExprError();
1263 }
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001265 if (Tok.is(tok::r_paren))
1266 RParenLoc = ConsumeParen();
1267 else
1268 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1269
1270 return move(Result);
1271}