blob: c7be0d3ff2b943db776f3cd719782983275d81dd [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +00002//
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 Lattnerc951dae2006-08-10 04:23:57 +00007//
8//===----------------------------------------------------------------------===//
James Dennette37835e2012-06-19 21:02:26 +00009///
James Dennett3d5e4592012-06-17 04:36:28 +000010/// \file
11/// \brief Provides the Expression parsing implementation.
12///
13/// Expressions in C99 basically consist of a bunch of binary operators with
14/// unary operators and other random stuff at the leaves.
15///
16/// In the C99 grammar, these unary operators bind tightest and are represented
17/// as the 'cast-expression' production. Everything else is either a binary
18/// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
19/// handled by ParseCastExpression, the higher level pieces are handled by
20/// ParseBinaryExpression.
James Dennette37835e2012-06-19 21:02:26 +000021///
22//===----------------------------------------------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +000023
24#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/Scope.h"
27#include "clang/Sema/ParsedTemplate.h"
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +000028#include "clang/Sema/TypoCorrection.h"
Chris Lattnerf6801202009-03-05 07:32:12 +000029#include "clang/Basic/PrettyStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000030#include "RAIIObjectsForParser.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000031#include "llvm/ADT/SmallVector.h"
Chris Lattner834618d2006-11-03 07:48:41 +000032#include "llvm/ADT/SmallString.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000033using namespace clang;
34
James Dennett3d5e4592012-06-17 04:36:28 +000035/// \brief Return the precedence of the specified binary operator token.
Mike Stump11289f42009-09-09 15:08:12 +000036static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregorcbb45d02009-02-25 23:02:36 +000037 bool GreaterThanIsOperator,
38 bool CPlusPlus0x) {
Chris Lattnercde626a2006-08-12 08:13:25 +000039 switch (Kind) {
Douglas Gregor8bf42052009-02-09 18:46:07 +000040 case tok::greater:
Douglas Gregorcbb45d02009-02-25 23:02:36 +000041 // C++ [temp.names]p3:
42 // [...] When parsing a template-argument-list, the first
43 // non-nested > is taken as the ending delimiter rather than a
44 // greater-than operator. [...]
Douglas Gregor8bf42052009-02-09 18:46:07 +000045 if (GreaterThanIsOperator)
46 return prec::Relational;
47 return prec::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +000048
Douglas Gregorcbb45d02009-02-25 23:02:36 +000049 case tok::greatergreater:
50 // C++0x [temp.names]p3:
51 //
52 // [...] Similarly, the first non-nested >> is treated as two
53 // consecutive but distinct > tokens, the first of which is
54 // taken as the end of the template-argument-list and completes
55 // the template-id. [...]
56 if (GreaterThanIsOperator || !CPlusPlus0x)
57 return prec::Shift;
58 return prec::Unknown;
59
Chris Lattnercde626a2006-08-12 08:13:25 +000060 default: return prec::Unknown;
61 case tok::comma: return prec::Comma;
62 case tok::equal:
63 case tok::starequal:
64 case tok::slashequal:
65 case tok::percentequal:
66 case tok::plusequal:
67 case tok::minusequal:
68 case tok::lesslessequal:
69 case tok::greatergreaterequal:
70 case tok::ampequal:
71 case tok::caretequal:
72 case tok::pipeequal: return prec::Assignment;
73 case tok::question: return prec::Conditional;
74 case tok::pipepipe: return prec::LogicalOr;
75 case tok::ampamp: return prec::LogicalAnd;
76 case tok::pipe: return prec::InclusiveOr;
77 case tok::caret: return prec::ExclusiveOr;
78 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +000079 case tok::exclaimequal:
80 case tok::equalequal: return prec::Equality;
81 case tok::lessequal:
82 case tok::less:
Douglas Gregor8bf42052009-02-09 18:46:07 +000083 case tok::greaterequal: return prec::Relational;
Douglas Gregorcbb45d02009-02-25 23:02:36 +000084 case tok::lessless: return prec::Shift;
Chris Lattnercde626a2006-08-12 08:13:25 +000085 case tok::plus:
86 case tok::minus: return prec::Additive;
87 case tok::percent:
88 case tok::slash:
89 case tok::star: return prec::Multiplicative;
Sebastian Redl112a97662009-02-07 00:15:38 +000090 case tok::periodstar:
91 case tok::arrowstar: return prec::PointerToMember;
Chris Lattnercde626a2006-08-12 08:13:25 +000092 }
93}
94
95
James Dennett3d5e4592012-06-17 04:36:28 +000096/// \brief Simple precedence-based parser for binary/ternary operators.
Chris Lattnercde626a2006-08-12 08:13:25 +000097///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000098/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
James Dennett3d5e4592012-06-17 04:36:28 +0000107/// \verbatim
Sebastian Redl112a97662009-02-07 00:15:38 +0000108/// pm-expression: [C++ 5.5]
109/// cast-expression
110/// pm-expression '.*' cast-expression
111/// pm-expression '->*' cast-expression
112///
Chris Lattnercde626a2006-08-12 08:13:25 +0000113/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl112a97662009-02-07 00:15:38 +0000114/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000115/// cast-expression
116/// multiplicative-expression '*' cast-expression
117/// multiplicative-expression '/' cast-expression
118/// multiplicative-expression '%' cast-expression
119///
120/// additive-expression: [C99 6.5.6]
121/// multiplicative-expression
122/// additive-expression '+' multiplicative-expression
123/// additive-expression '-' multiplicative-expression
124///
125/// shift-expression: [C99 6.5.7]
126/// additive-expression
127/// shift-expression '<<' additive-expression
128/// shift-expression '>>' additive-expression
129///
130/// relational-expression: [C99 6.5.8]
131/// shift-expression
132/// relational-expression '<' shift-expression
133/// relational-expression '>' shift-expression
134/// relational-expression '<=' shift-expression
135/// relational-expression '>=' shift-expression
136///
137/// equality-expression: [C99 6.5.9]
138/// relational-expression
139/// equality-expression '==' relational-expression
140/// equality-expression '!=' relational-expression
141///
142/// AND-expression: [C99 6.5.10]
143/// equality-expression
144/// AND-expression '&' equality-expression
145///
146/// exclusive-OR-expression: [C99 6.5.11]
147/// AND-expression
148/// exclusive-OR-expression '^' AND-expression
149///
150/// inclusive-OR-expression: [C99 6.5.12]
151/// exclusive-OR-expression
152/// inclusive-OR-expression '|' exclusive-OR-expression
153///
154/// logical-AND-expression: [C99 6.5.13]
155/// inclusive-OR-expression
156/// logical-AND-expression '&&' inclusive-OR-expression
157///
158/// logical-OR-expression: [C99 6.5.14]
159/// logical-AND-expression
160/// logical-OR-expression '||' logical-AND-expression
161///
162/// conditional-expression: [C99 6.5.15]
163/// logical-OR-expression
164/// logical-OR-expression '?' expression ':' conditional-expression
165/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl1a99f442009-04-16 17:51:27 +0000166/// [C++] the third operand is an assignment-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000167///
168/// assignment-expression: [C99 6.5.16]
169/// conditional-expression
170/// unary-expression assignment-operator assignment-expression
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000171/// [C++] throw-expression [C++ 15]
Chris Lattnercde626a2006-08-12 08:13:25 +0000172///
173/// assignment-operator: one of
174/// = *= /= %= += -= <<= >>= &= ^= |=
175///
176/// expression: [C99 6.5.17]
Douglas Gregor968f23a2011-01-03 19:31:53 +0000177/// assignment-expression ...[opt]
178/// expression ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +0000179/// \endverbatim
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000180ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
181 ExprResult LHS(ParseAssignmentExpression(isTypeCast));
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000182 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Chris Lattnercde626a2006-08-12 08:13:25 +0000183}
184
Mike Stump11289f42009-09-09 15:08:12 +0000185/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000186/// Current token is an Identifier and is not a 'try'. This
James Dennettf44874f2012-06-15 06:52:33 +0000187/// routine is necessary to disambiguate \@try-statement from,
188/// for example, \@encode-expression.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000189///
John McCalldadc5752010-08-24 06:29:42 +0000190ExprResult
Sebastian Redl90893182008-12-11 22:33:27 +0000191Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000192 ExprResult LHS(ParseObjCAtExpression(AtLoc));
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000193 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000194}
195
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000196/// This routine is called when a leading '__extension__' is seen and
197/// consumed. This is necessary because the token gets consumed in the
198/// process of disambiguating between an expression and a declaration.
John McCalldadc5752010-08-24 06:29:42 +0000199ExprResult
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000200Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000201 ExprResult LHS(true);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000202 {
203 // Silence extension warnings in the sub-expression
204 ExtensionRAIIObject O(Diags);
205
206 LHS = ParseCastExpression(false);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000207 }
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000208
Douglas Gregor29d907d2010-09-17 22:25:06 +0000209 if (!LHS.isInvalid())
210 LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
211 LHS.take());
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000212
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000213 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000214}
215
James Dennett3d5e4592012-06-17 04:36:28 +0000216/// \brief Parse an expr that doesn't include (top-level) commas.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000217ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000218 if (Tok.is(tok::code_completion)) {
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000219 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000220 cutOffParsing();
221 return ExprError();
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000222 }
223
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000224 if (Tok.is(tok::kw_throw))
Sebastian Redld65cea82008-12-11 22:51:44 +0000225 return ParseThrowExpression();
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000226
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000227 ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
228 /*isAddressOfOperand=*/false,
229 isTypeCast);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000230 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000231}
232
James Dennett3d5e4592012-06-17 04:36:28 +0000233/// \brief Parse an assignment expression where part of an Objective-C message
234/// send has already been parsed.
235///
236/// In this case \p LBracLoc indicates the location of the '[' of the message
237/// send, and either \p ReceiverName or \p ReceiverExpr is non-null indicating
238/// the receiver of the message.
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000239///
240/// Since this handles full assignment-expression's, it handles postfix
241/// expressions and other binary operators for these expressions as well.
John McCalldadc5752010-08-24 06:29:42 +0000242ExprResult
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000243Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000244 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +0000245 ParsedType ReceiverType,
John McCallb268a282010-08-23 23:25:46 +0000246 Expr *ReceiverExpr) {
John McCalldadc5752010-08-24 06:29:42 +0000247 ExprResult R
John McCallb268a282010-08-23 23:25:46 +0000248 = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
249 ReceiverType, ReceiverExpr);
Douglas Gregoreda7e542010-09-18 01:28:11 +0000250 R = ParsePostfixExpressionSuffix(R);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000251 return ParseRHSOfBinaryExpression(R, prec::Assignment);
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000252}
253
254
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000255ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
Richard Smith764d2fe2011-12-20 02:08:33 +0000256 // C++03 [basic.def.odr]p2:
Mike Stump11289f42009-09-09 15:08:12 +0000257 // An expression is potentially evaluated unless it appears where an
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000258 // integral constant expression is required (see 5.19) [...].
Richard Smith764d2fe2011-12-20 02:08:33 +0000259 // C++98 and C++11 have no such rule, but this is only a defect in C++98.
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000260 EnterExpressionEvaluationContext Unevaluated(Actions,
Richard Smith764d2fe2011-12-20 02:08:33 +0000261 Sema::ConstantEvaluated);
Mike Stump11289f42009-09-09 15:08:12 +0000262
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000263 ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
Eli Friedmanc6237c62012-02-29 03:16:56 +0000264 ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
265 return Actions.ActOnConstantExpression(Res);
Chris Lattner3b561a32006-08-13 00:12:11 +0000266}
267
Richard Smith0875c532012-09-18 00:52:05 +0000268bool Parser::isNotExpressionStart() {
269 tok::TokenKind K = Tok.getKind();
270 if (K == tok::l_brace || K == tok::r_brace ||
271 K == tok::kw_for || K == tok::kw_while ||
272 K == tok::kw_if || K == tok::kw_else ||
273 K == tok::kw_goto || K == tok::kw_try)
274 return true;
275 // If this is a decl-specifier, we can't be at the start of an expression.
276 return isKnownToBeDeclarationSpecifier();
277}
278
James Dennett3d5e4592012-06-17 04:36:28 +0000279/// \brief Parse a binary expression that starts with \p LHS and has a
280/// precedence of at least \p MinPrec.
John McCalldadc5752010-08-24 06:29:42 +0000281ExprResult
282Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000283 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
284 GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000285 getLangOpts().CPlusPlus0x);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000286 SourceLocation ColonLoc;
287
Chris Lattnercde626a2006-08-12 08:13:25 +0000288 while (1) {
289 // If this token has a lower precedence than we are allowed to parse (e.g.
290 // because we are called recursively, or because the token is not a binop),
291 // then we are done!
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000292 if (NextTokPrec < MinPrec)
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000293 return LHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000294
295 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000296 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000297 ConsumeToken();
Sebastian Redl112a97662009-02-07 00:15:38 +0000298
Richard Smith0875c532012-09-18 00:52:05 +0000299 // Bail out when encountering a comma followed by a token which can't
300 // possibly be the start of an expression. For instance:
301 // int f() { return 1, }
302 // We can't do this before consuming the comma, because
303 // isNotExpressionStart() looks at the token stream.
304 if (OpToken.is(tok::comma) && isNotExpressionStart()) {
305 PP.EnterToken(Tok);
306 Tok = OpToken;
307 return LHS;
308 }
309
Chris Lattner96c3deb2006-08-12 17:13:08 +0000310 // Special case handling for the ternary operator.
John McCalldadc5752010-08-24 06:29:42 +0000311 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000312 if (NextTokPrec == prec::Conditional) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000313 if (Tok.isNot(tok::colon)) {
Chris Lattner244b96b2009-12-10 02:02:58 +0000314 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
315 ColonProtectionRAIIObject X(*this);
316
Chris Lattner96c3deb2006-08-12 17:13:08 +0000317 // Handle this production specially:
318 // logical-OR-expression '?' expression ':' conditional-expression
319 // In particular, the RHS of the '?' is 'expression', not
320 // 'logical-OR-expression' as we might expect.
321 TernaryMiddle = ParseExpression();
Douglas Gregorec06c122010-09-17 22:41:34 +0000322 if (TernaryMiddle.isInvalid()) {
323 LHS = ExprError();
324 TernaryMiddle = 0;
325 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000326 } else {
327 // Special case handling of "X ? Y : Z" where Y is empty:
328 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redlc13f2682008-12-09 20:22:58 +0000329 TernaryMiddle = 0;
Chris Lattner96c3deb2006-08-12 17:13:08 +0000330 Diag(Tok, diag::ext_gnu_conditional_expr);
331 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000332
Chris Lattner0151b7e2010-04-20 21:33:39 +0000333 if (Tok.is(tok::colon)) {
334 // Eat the colon.
335 ColonLoc = ConsumeToken();
336 } else {
Chandler Carruth0b5cf7c2011-07-26 05:19:46 +0000337 // Otherwise, we're missing a ':'. Assume that this was a typo that
338 // the user forgot. If we're not in a macro expansion, we can suggest
339 // a fixit hint. If there were two spaces before the current token,
Chris Lattnerfb585152010-05-24 22:31:37 +0000340 // suggest inserting the colon in between them, otherwise insert ": ".
341 SourceLocation FILoc = Tok.getLocation();
342 const char *FIText = ": ";
Argyrios Kyrtzidis3ea4adb2011-06-24 17:28:29 +0000343 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis1b07c342012-01-19 15:59:08 +0000344 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
345 assert(FILoc.isFileID());
Chris Lattnerfb585152010-05-24 22:31:37 +0000346 bool IsInvalid = false;
347 const char *SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000348 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000349 if (!IsInvalid && *SourcePtr == ' ') {
350 SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000351 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000352 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000353 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattnerfb585152010-05-24 22:31:37 +0000354 FIText = ":";
355 }
356 }
357 }
358
Ted Kremeneke6013652010-04-12 22:10:35 +0000359 Diag(Tok, diag::err_expected_colon)
Chris Lattnerfb585152010-05-24 22:31:37 +0000360 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner03c40412008-11-23 23:17:07 +0000361 Diag(OpToken, diag::note_matching) << "?";
Chris Lattner0151b7e2010-04-20 21:33:39 +0000362 ColonLoc = Tok.getLocation();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000363 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000364 }
Fariborz Jahanian9a14b842009-10-23 21:01:39 +0000365
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000366 // Code completion for the right-hand side of an assignment expression
367 // goes through a special hook that takes the left-hand side into account.
368 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000369 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000370 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000371 return ExprError();
372 }
373
Chris Lattner96c3deb2006-08-12 17:13:08 +0000374 // Parse another leaf here for the RHS of the operator.
Sebastian Redl1a99f442009-04-16 17:51:27 +0000375 // ParseCastExpression works here because all RHS expressions in C have it
376 // as a prefix, at least. However, in C++, an assignment-expression could
377 // be a throw-expression, which is not a valid cast-expression.
378 // Therefore we need some special-casing here.
379 // Also note that the third operand of the conditional operator is
Richard Smith9a6403a2012-02-26 23:40:27 +0000380 // an assignment-expression in C++, and in C++11, we can have a
Richard Smith5e0cac72012-03-01 02:59:17 +0000381 // braced-init-list on the RHS of an assignment. For better diagnostics,
382 // parse as if we were allowed braced-init-lists everywhere, and check that
383 // they only appear on the RHS of assignments later.
John McCalldadc5752010-08-24 06:29:42 +0000384 ExprResult RHS;
Richard Smithebcd2352012-03-01 07:10:06 +0000385 bool RHSIsInitList = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000386 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith9a6403a2012-02-26 23:40:27 +0000387 RHS = ParseBraceInitializer();
Richard Smithebcd2352012-03-01 07:10:06 +0000388 RHSIsInitList = true;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000389 } else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional)
Sebastian Redl1a99f442009-04-16 17:51:27 +0000390 RHS = ParseAssignmentExpression();
Richard Smith5e0cac72012-03-01 02:59:17 +0000391 else
Sebastian Redl1a99f442009-04-16 17:51:27 +0000392 RHS = ParseCastExpression(false);
Chris Lattnercde626a2006-08-12 08:13:25 +0000393
Douglas Gregor29d907d2010-09-17 22:25:06 +0000394 if (RHS.isInvalid())
395 LHS = ExprError();
396
Chris Lattnercde626a2006-08-12 08:13:25 +0000397 // Remember the precedence of this operator and get the precedence of the
398 // operator immediately to the right of the RHS.
Douglas Gregor8d4de672010-04-21 22:36:40 +0000399 prec::Level ThisPrec = NextTokPrec;
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000400 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000401 getLangOpts().CPlusPlus0x);
Chris Lattner89d53752006-08-12 17:18:19 +0000402
403 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000404 bool isRightAssoc = ThisPrec == prec::Conditional ||
405 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000406
407 // Get the precedence of the operator to the right of the RHS. If it binds
408 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000409 if (ThisPrec < NextTokPrec ||
410 (ThisPrec == NextTokPrec && isRightAssoc)) {
Richard Smithebcd2352012-03-01 07:10:06 +0000411 if (!RHS.isInvalid() && RHSIsInitList) {
412 Diag(Tok, diag::err_init_list_bin_op)
413 << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get());
414 RHS = ExprError();
Richard Smith5e0cac72012-03-01 02:59:17 +0000415 }
Chris Lattner89d53752006-08-12 17:18:19 +0000416 // If this is left-associative, only parse things on the RHS that bind
417 // more tightly than the current operator. If it is left-associative, it
418 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
419 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl511ed552008-11-25 22:21:31 +0000420 // The function takes ownership of the RHS.
Douglas Gregor29d907d2010-09-17 22:25:06 +0000421 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor8d4de672010-04-21 22:36:40 +0000422 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Richard Smithebcd2352012-03-01 07:10:06 +0000423 RHSIsInitList = false;
Douglas Gregor29d907d2010-09-17 22:25:06 +0000424
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000425 if (RHS.isInvalid())
Douglas Gregor29d907d2010-09-17 22:25:06 +0000426 LHS = ExprError();
Chris Lattnercde626a2006-08-12 08:13:25 +0000427
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000428 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000429 getLangOpts().CPlusPlus0x);
Chris Lattnercde626a2006-08-12 08:13:25 +0000430 }
431 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl511ed552008-11-25 22:21:31 +0000432
Richard Smithebcd2352012-03-01 07:10:06 +0000433 if (!RHS.isInvalid() && RHSIsInitList) {
Richard Smith5e0cac72012-03-01 02:59:17 +0000434 if (ThisPrec == prec::Assignment) {
435 Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
Richard Smithebcd2352012-03-01 07:10:06 +0000436 << Actions.getExprRange(RHS.get());
Richard Smith5e0cac72012-03-01 02:59:17 +0000437 } else {
438 Diag(OpToken, diag::err_init_list_bin_op)
Richard Smithebcd2352012-03-01 07:10:06 +0000439 << /*RHS*/1 << PP.getSpelling(OpToken)
440 << Actions.getExprRange(RHS.get());
Richard Smith5e0cac72012-03-01 02:59:17 +0000441 LHS = ExprError();
442 }
443 }
444
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000445 if (!LHS.isInvalid()) {
Chris Lattner319079c2007-08-31 05:01:50 +0000446 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor87f95b02009-02-26 21:00:50 +0000447 if (TernaryMiddle.isInvalid()) {
448 // If we're using '>>' as an operator within a template
449 // argument list (in C++98), suggest the addition of
450 // parentheses so that the code remains well-formed in C++0x.
451 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
452 SuggestParentheses(OpToken.getLocation(),
453 diag::warn_cxx0x_right_shift_in_template_arg,
454 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
455 Actions.getExprRange(RHS.get()).getEnd()));
456
Douglas Gregor0be31a22010-07-02 17:43:08 +0000457 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCallb268a282010-08-23 23:25:46 +0000458 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor87f95b02009-02-26 21:00:50 +0000459 } else
Steve Naroff83895f72007-09-16 03:34:24 +0000460 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000461 LHS.take(), TernaryMiddle.take(),
462 RHS.take());
Chris Lattner319079c2007-08-31 05:01:50 +0000463 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000464 }
465}
466
James Dennett3d5e4592012-06-17 04:36:28 +0000467/// \brief Parse a cast-expression, or, if \p isUnaryExpression is true,
468/// parse a unary-expression.
469///
470/// \p isAddressOfOperand exists because an id-expression that is the
471/// operand of address-of gets special treatment due to member pointers.
Chris Lattnereaf06592006-08-11 02:02:23 +0000472///
John McCalldadc5752010-08-24 06:29:42 +0000473ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000474 bool isAddressOfOperand,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000475 TypeCastState isTypeCast) {
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000476 bool NotCastExpr;
John McCalldadc5752010-08-24 06:29:42 +0000477 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor29d907d2010-09-17 22:25:06 +0000478 isAddressOfOperand,
479 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +0000480 isTypeCast);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000481 if (NotCastExpr)
482 Diag(Tok, diag::err_expected_expression);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000483 return Res;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000484}
485
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000486namespace {
487class CastExpressionIdValidator : public CorrectionCandidateCallback {
488 public:
489 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
490 : AllowNonTypes(AllowNonTypes) {
491 WantTypeSpecifiers = AllowTypes;
492 }
493
494 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
495 NamedDecl *ND = candidate.getCorrectionDecl();
496 if (!ND)
497 return candidate.isKeyword();
498
499 if (isa<TypeDecl>(ND))
500 return WantTypeSpecifiers;
501 return AllowNonTypes;
502 }
503
504 private:
505 bool AllowNonTypes;
506};
507}
508
James Dennett3d5e4592012-06-17 04:36:28 +0000509/// \brief Parse a cast-expression, or, if \pisUnaryExpression is true, parse
510/// a unary-expression.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000511///
James Dennett3d5e4592012-06-17 04:36:28 +0000512/// \p isAddressOfOperand exists because an id-expression that is the operand
513/// of address-of gets special treatment due to member pointers. NotCastExpr
514/// is set to true if the token is not the start of a cast-expression, and no
515/// diagnostic is emitted in this case.
516///
517/// \verbatim
Chris Lattner4564bc12006-08-10 23:14:52 +0000518/// cast-expression: [C99 6.5.4]
519/// unary-expression
520/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000521///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000522/// unary-expression: [C99 6.5.3]
523/// postfix-expression
524/// '++' unary-expression
525/// '--' unary-expression
526/// unary-operator cast-expression
527/// 'sizeof' unary-expression
528/// 'sizeof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000529/// [C++11] 'sizeof' '...' '(' identifier ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000530/// [GNU] '__alignof' unary-expression
531/// [GNU] '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +0000532/// [C11] '_Alignof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000533/// [C++11] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000534/// [GNU] '&&' identifier
Richard Smithd67aea22012-03-06 03:21:47 +0000535/// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7]
Sebastian Redlbd150f42008-11-21 19:14:01 +0000536/// [C++] new-expression
537/// [C++] delete-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000538///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000539/// unary-operator: one of
540/// '&' '*' '+' '-' '~' '!'
541/// [GNU] '__extension__' '__real' '__imag'
542///
Chris Lattner52a99e52006-08-10 20:56:00 +0000543/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000544/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000545/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000546/// constant
547/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000548/// [C++] boolean-literal [C++ 2.13.5]
Richard Smithd67aea22012-03-06 03:21:47 +0000549/// [C++11] 'nullptr' [C++11 2.14.7]
550/// [C++11] user-defined-literal
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000551/// '(' expression ')'
Benjamin Kramere56f3932011-12-23 17:00:35 +0000552/// [C11] generic-selection
Chris Lattner52a99e52006-08-10 20:56:00 +0000553/// '__func__' [C99 6.4.2.2]
554/// [GNU] '__FUNCTION__'
555/// [GNU] '__PRETTY_FUNCTION__'
556/// [GNU] '(' compound-statement ')'
557/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
558/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
559/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
560/// assign-expr ')'
561/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000562/// [GNU] '__null'
Mike Stump11289f42009-09-09 15:08:12 +0000563/// [OBJC] '[' objc-message-expr ']'
James Dennettf44874f2012-06-15 06:52:33 +0000564/// [OBJC] '\@selector' '(' objc-selector-arg ')'
565/// [OBJC] '\@protocol' '(' identifier ')'
566/// [OBJC] '\@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000567/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000568/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000569/// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3]
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000570/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000571/// [C++11] typename-specifier braced-init-list [C++11 5.2.3]
Bill Wendlinga6930032007-06-29 18:21:34 +0000572/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
573/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
574/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
575/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000576/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
577/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000578/// [C++] 'this' [C++ 9.3.2]
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000579/// [G++] unary-type-trait '(' type-id ')'
580/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley6242b6a2011-04-28 00:16:57 +0000581/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff0ac012832008-08-28 19:20:44 +0000582/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000583///
584/// constant: [C99 6.4.4]
585/// integer-constant
586/// floating-constant
587/// enumeration-constant -> identifier
588/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000589///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000590/// id-expression: [C++ 5.1]
591/// unqualified-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000592/// qualified-id
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000593///
594/// unqualified-id: [C++ 5.1]
595/// identifier
596/// operator-function-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000597/// conversion-function-id
598/// '~' class-name
599/// template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000600///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000601/// new-expression: [C++ 5.3.4]
602/// '::'[opt] 'new' new-placement[opt] new-type-id
603/// new-initializer[opt]
604/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
605/// new-initializer[opt]
606///
607/// delete-expression: [C++ 5.3.5]
608/// '::'[opt] 'delete' cast-expression
609/// '::'[opt] 'delete' '[' ']' cast-expression
610///
John Wiegley65497cc2011-04-27 23:09:49 +0000611/// [GNU/Embarcadero] unary-type-trait:
612/// '__is_arithmetic'
613/// '__is_floating_point'
614/// '__is_integral'
615/// '__is_lvalue_expr'
616/// '__is_rvalue_expr'
617/// '__is_complete_type'
618/// '__is_void'
619/// '__is_array'
620/// '__is_function'
621/// '__is_reference'
622/// '__is_lvalue_reference'
623/// '__is_rvalue_reference'
624/// '__is_fundamental'
625/// '__is_object'
626/// '__is_scalar'
627/// '__is_compound'
628/// '__is_pointer'
629/// '__is_member_object_pointer'
630/// '__is_member_function_pointer'
631/// '__is_member_pointer'
632/// '__is_const'
633/// '__is_volatile'
634/// '__is_trivial'
635/// '__is_standard_layout'
636/// '__is_signed'
637/// '__is_unsigned'
638///
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000639/// [GNU] unary-type-trait:
Sebastian Redl7dcb1552010-08-31 04:59:00 +0000640/// '__has_nothrow_assign'
641/// '__has_nothrow_copy'
642/// '__has_nothrow_constructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000643/// '__has_trivial_assign' [TODO]
644/// '__has_trivial_copy' [TODO]
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000645/// '__has_trivial_constructor'
Anders Carlsson6dc35752009-04-17 02:34:54 +0000646/// '__has_trivial_destructor'
Sebastian Redlb469afb2010-09-02 23:19:42 +0000647/// '__has_virtual_destructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000648/// '__is_abstract' [TODO]
649/// '__is_class'
650/// '__is_empty' [TODO]
651/// '__is_enum'
Douglas Gregordca70af2011-12-03 18:14:24 +0000652/// '__is_final'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000653/// '__is_pod'
654/// '__is_polymorphic'
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000655/// '__is_trivial'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000656/// '__is_union'
657///
Alexis Huntd9a5cc12011-05-13 00:31:07 +0000658/// [Clang] unary-type-trait:
659/// '__trivially_copyable'
660///
Douglas Gregor8006e762011-01-27 20:28:01 +0000661/// binary-type-trait:
662/// [GNU] '__is_base_of'
663/// [MS] '__is_convertible_to'
John Wiegley65497cc2011-04-27 23:09:49 +0000664/// '__is_convertible'
665/// '__is_same'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000666///
John Wiegley6242b6a2011-04-28 00:16:57 +0000667/// [Embarcadero] array-type-trait:
668/// '__array_rank'
669/// '__array_extent'
670///
John Wiegleyf9f65842011-04-25 06:54:41 +0000671/// [Embarcadero] expression-trait:
672/// '__is_lvalue_expr'
673/// '__is_rvalue_expr'
James Dennett3d5e4592012-06-17 04:36:28 +0000674/// \endverbatim
John Wiegleyf9f65842011-04-25 06:54:41 +0000675///
John McCalldadc5752010-08-24 06:29:42 +0000676ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl22e3a932010-09-10 20:55:37 +0000677 bool isAddressOfOperand,
678 bool &NotCastExpr,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000679 TypeCastState isTypeCast) {
John McCalldadc5752010-08-24 06:29:42 +0000680 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000681 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000682 NotCastExpr = false;
Mike Stump11289f42009-09-09 15:08:12 +0000683
Chris Lattner81b576e2006-08-11 02:13:20 +0000684 // This handles all of cast-expression, unary-expression, postfix-expression,
685 // and primary-expression. We handle them together like this for efficiency
686 // and to simplify handling of an expression starting with a '(' token: which
687 // may be one of a parenthesized expression, cast-expression, compound literal
688 // expression, or statement expression.
689 //
690 // If the parsed tokens consist of a primary-expression, the cases below
John McCallb268a282010-08-23 23:25:46 +0000691 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
692 // to handle the postfix expression suffixes. Cases that cannot be followed
693 // by postfix exprs should return without invoking
694 // ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000695 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000696 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000697 // If this expression is limited to being a unary-expression, the parent can
698 // not start a cast expression.
699 ParenParseOption ParenExprType =
David Blaikiebbafb8a2012-03-11 07:00:24 +0000700 (isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallba7bf592010-08-24 05:47:05 +0000701 ParsedType CastTy;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000702 SourceLocation RParenLoc;
Chris Lattner3c674cf2009-12-10 02:08:07 +0000703
704 {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000705 // The inside of the parens don't need to be a colon protected scope, and
706 // isn't immediately a message send.
Chris Lattner3c674cf2009-12-10 02:08:07 +0000707 ColonProtectionRAIIObject X(*this, false);
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000708
Chris Lattner3c674cf2009-12-10 02:08:07 +0000709 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000710 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner3c674cf2009-12-10 02:08:07 +0000711 }
Mike Stump11289f42009-09-09 15:08:12 +0000712
Chris Lattner81b576e2006-08-11 02:13:20 +0000713 switch (ParenExprType) {
714 case SimpleExpr: break; // Nothing else to do.
715 case CompoundStmt: break; // Nothing else to do.
716 case CompoundLiteral:
717 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
718 // postfix-expression exist, parse them now.
719 break;
720 case CastExpr:
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000721 // We have parsed the cast-expression and no postfix-expr pieces are
722 // following.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000723 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000724 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000725
John McCallb268a282010-08-23 23:25:46 +0000726 break;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000727 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000728
Chris Lattner52a99e52006-08-10 20:56:00 +0000729 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000730 case tok::numeric_constant:
731 // constant: integer-constant
732 // constant: floating-constant
Sebastian Redl59b5e512008-12-11 21:36:32 +0000733
Richard Smithbcc22fc2012-03-09 08:00:36 +0000734 Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000735 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000736 break;
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000737
Bill Wendling4073ed52007-02-13 01:51:42 +0000738 case tok::kw_true:
739 case tok::kw_false:
Sebastian Redld65cea82008-12-11 22:51:44 +0000740 return ParseCXXBoolLiteral();
Ted Kremeneke65b0862012-03-06 20:05:56 +0000741
742 case tok::kw___objc_yes:
743 case tok::kw___objc_no:
744 return ParseObjCBoolLiteral();
Bill Wendling4073ed52007-02-13 01:51:42 +0000745
Sebastian Redl576fd422009-05-10 18:38:11 +0000746 case tok::kw_nullptr:
Richard Smithb15c11c2011-10-17 23:06:20 +0000747 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl576fd422009-05-10 18:38:11 +0000748 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
749
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000750 case tok::annot_primary_expr:
751 assert(Res.get() == 0 && "Stray primary-expression annotation?");
752 Res = getExprAnnotation(Tok);
753 ConsumeToken();
754 break;
755
David Blaikie15a430a2011-12-04 05:04:18 +0000756 case tok::kw_decltype:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000757 case tok::identifier: { // primary-expression: identifier
758 // unqualified-id: identifier
759 // constant: enumeration-constant
Chris Lattnera8a3f732009-01-06 05:06:21 +0000760 // Turn a potentially qualified name into a annot_typename or
Chris Lattner122db262009-01-04 22:52:14 +0000761 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000762 if (getLangOpts().CPlusPlus) {
John McCall64fe2332010-01-07 19:29:58 +0000763 // Avoid the unnecessary parse-time lookup in the common case
764 // where the syntax forbids a type.
765 const Token &Next = NextToken();
Douglas Gregor8bea83a2012-08-30 20:04:43 +0000766
767 // If this identifier was reverted from a token ID, and the next token
768 // is a parenthesis, this is likely to be a use of a type trait. Check
769 // those tokens.
770 if (Next.is(tok::l_paren) &&
771 Tok.is(tok::identifier) &&
772 Tok.getIdentifierInfo()->hasRevertedTokenIDToIdentifier()) {
773 IdentifierInfo *II = Tok.getIdentifierInfo();
774 // Build up the mapping of revertable type traits, for future use.
775 if (RevertableTypeTraits.empty()) {
Douglas Gregor8bea83a2012-08-30 20:04:43 +0000776#define RTT_JOIN(X,Y) X##Y
777#define REVERTABLE_TYPE_TRAIT(Name) \
778 RevertableTypeTraits[PP.getIdentifierInfo(#Name)] \
779 = RTT_JOIN(tok::kw_,Name)
780
781 REVERTABLE_TYPE_TRAIT(__is_arithmetic);
782 REVERTABLE_TYPE_TRAIT(__is_convertible);
783 REVERTABLE_TYPE_TRAIT(__is_empty);
784 REVERTABLE_TYPE_TRAIT(__is_floating_point);
785 REVERTABLE_TYPE_TRAIT(__is_function);
786 REVERTABLE_TYPE_TRAIT(__is_fundamental);
787 REVERTABLE_TYPE_TRAIT(__is_integral);
788 REVERTABLE_TYPE_TRAIT(__is_member_function_pointer);
789 REVERTABLE_TYPE_TRAIT(__is_member_pointer);
790 REVERTABLE_TYPE_TRAIT(__is_pod);
791 REVERTABLE_TYPE_TRAIT(__is_pointer);
792 REVERTABLE_TYPE_TRAIT(__is_same);
793 REVERTABLE_TYPE_TRAIT(__is_scalar);
794 REVERTABLE_TYPE_TRAIT(__is_signed);
795 REVERTABLE_TYPE_TRAIT(__is_unsigned);
796 REVERTABLE_TYPE_TRAIT(__is_void);
797#undef REVERTABLE_TYPE_TRAIT
Douglas Gregor8bea83a2012-08-30 20:04:43 +0000798#undef RTT_JOIN
799 }
800
801 // If we find that this is in fact the name of a type trait,
802 // update the token kind in place and parse again to treat it as
803 // the appropriate kind of type trait.
804 llvm::SmallDenseMap<IdentifierInfo *, tok::TokenKind>::iterator Known
805 = RevertableTypeTraits.find(II);
806 if (Known != RevertableTypeTraits.end()) {
807 Tok.setKind(Known->second);
808 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
809 NotCastExpr, isTypeCast);
810 }
811 }
812
John McCall64fe2332010-01-07 19:29:58 +0000813 if (Next.is(tok::coloncolon) ||
814 (!ColonIsSacred && Next.is(tok::colon)) ||
815 Next.is(tok::less) ||
Sebastian Redl867f2282011-12-22 18:58:29 +0000816 Next.is(tok::l_paren) ||
817 Next.is(tok::l_brace)) {
John McCall64fe2332010-01-07 19:29:58 +0000818 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
819 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +0000820 return ExprError();
821 if (!Tok.is(tok::identifier))
John McCall64fe2332010-01-07 19:29:58 +0000822 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
823 }
Chris Lattner9a8968b2009-01-04 23:23:14 +0000824 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000825
Chris Lattner55662902009-10-25 17:04:48 +0000826 // Consume the identifier so that we can see if it is followed by a '(' or
827 // '.'.
828 IdentifierInfo &II = *Tok.getIdentifierInfo();
829 SourceLocation ILoc = ConsumeToken();
Douglas Gregor8bea83a2012-08-30 20:04:43 +0000830
Chris Lattnera36ec422010-04-11 08:28:14 +0000831 // Support 'Class.property' and 'super.property' notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000832 if (getLangOpts().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000833 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattnercd963182010-04-12 06:20:33 +0000834 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000835 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000836 ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000837
Douglas Gregor36107ad2012-02-16 18:19:22 +0000838 // Allow either an identifier or the keyword 'class' (in C++).
839 if (Tok.isNot(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000840 !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattner55662902009-10-25 17:04:48 +0000841 Diag(Tok, diag::err_expected_property_name);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000842 return ExprError();
843 }
844 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
845 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000846
847 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
848 ILoc, PropertyLoc);
John McCallb268a282010-08-23 23:25:46 +0000849 break;
Steve Naroff9527bbf2009-03-09 21:12:44 +0000850 }
John McCall8d08b9b2010-08-27 09:08:28 +0000851
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000852 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregored0b69d2010-09-15 16:23:04 +0000853 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000854 // that identifier, this is probably a message send with a missing open
Douglas Gregored0b69d2010-09-15 16:23:04 +0000855 // bracket. Treat it as such.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000856 if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000857 getCurScope()->isInObjcMethodScope() &&
Douglas Gregored0b69d2010-09-15 16:23:04 +0000858 ((Tok.is(tok::identifier) &&
859 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
860 Tok.is(tok::code_completion))) {
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000861 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
862 0);
863 break;
864 }
865
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000866 // If we have an Objective-C class name followed by an identifier
867 // and either ':' or ']', this is an Objective-C class message
868 // send that's missing the opening '['. Recovery
869 // appropriately. Also take this path if we're performing code
870 // completion after an Objective-C class name.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000871 if (getLangOpts().ObjC1 &&
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000872 ((Tok.is(tok::identifier) && !InMessageExpression) ||
873 Tok.is(tok::code_completion))) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000874 const Token& Next = NextToken();
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000875 if (Tok.is(tok::code_completion) ||
876 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif433c9e12010-09-17 10:21:45 +0000877 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
878 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000879 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000880 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000881 DS.SetRangeStart(ILoc);
882 DS.SetRangeEnd(ILoc);
883 const char *PrevSpec = 0;
884 unsigned DiagID;
Gabor Greif433c9e12010-09-17 10:21:45 +0000885 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000886
887 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
888 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
889 DeclaratorInfo);
890 if (Ty.isInvalid())
891 break;
892
893 Res = ParseObjCMessageExpressionBody(SourceLocation(),
894 SourceLocation(),
895 Ty.get(), 0);
896 break;
897 }
898 }
899
John McCall8d08b9b2010-08-27 09:08:28 +0000900 // Make sure to pass down the right value for isAddressOfOperand.
901 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
902 isAddressOfOperand = false;
Chris Lattner55662902009-10-25 17:04:48 +0000903
Chris Lattnerac18be92006-11-20 06:49:47 +0000904 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
905 // need to know whether or not this identifier is a function designator or
906 // not.
Douglas Gregora121b752009-11-03 16:56:39 +0000907 UnqualifiedId Name;
908 CXXScopeSpec ScopeSpec;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000909 SourceLocation TemplateKWLoc;
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000910 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
911 isTypeCast != IsTypeCast);
Douglas Gregora121b752009-11-03 16:56:39 +0000912 Name.setIdentifier(&II, ILoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000913 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
914 Name, Tok.is(tok::l_paren),
915 isAddressOfOperand, &Validator);
John McCallb268a282010-08-23 23:25:46 +0000916 break;
Chris Lattnerac18be92006-11-20 06:49:47 +0000917 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000918 case tok::char_constant: // constant: character-constant
Douglas Gregorfb65e592011-07-27 05:40:30 +0000919 case tok::wide_char_constant:
920 case tok::utf16_char_constant:
921 case tok::utf32_char_constant:
Richard Smithbcc22fc2012-03-09 08:00:36 +0000922 Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope());
Steve Naroffae4143e2007-04-26 20:39:23 +0000923 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000924 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000925 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
926 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Nico Weber3a691a32012-06-23 02:07:59 +0000927 case tok::kw_L__FUNCTION__: // primary-expression: L__FUNCTION__ [MS]
Chris Lattner52a99e52006-08-10 20:56:00 +0000928 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000929 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000930 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000931 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000932 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000933 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000934 case tok::utf8_string_literal:
935 case tok::utf16_string_literal:
936 case tok::utf32_string_literal:
Richard Smithd67aea22012-03-06 03:21:47 +0000937 Res = ParseStringLiteralExpression(true);
John McCallb268a282010-08-23 23:25:46 +0000938 break;
Benjamin Kramere56f3932011-12-23 17:00:35 +0000939 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbourne91147592011-04-15 00:35:48 +0000940 Res = ParseGenericSelectionExpression();
941 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000942 case tok::kw___builtin_va_arg:
943 case tok::kw___builtin_offsetof:
944 case tok::kw___builtin_choose_expr:
Tanya Lattner55808c12011-06-04 00:47:47 +0000945 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redl90893182008-12-11 22:33:27 +0000946 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000947 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000948 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth904cb142011-07-08 04:59:44 +0000949
Douglas Gregora49ccfe2010-08-06 14:50:36 +0000950 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
951 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
952 // C++ [expr.unary] has:
953 // unary-expression:
954 // ++ cast-expression
955 // -- cast-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000956 SourceLocation SavedLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000957 Res = ParseCastExpression(!getLangOpts().CPlusPlus);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000958 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000959 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000960 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000961 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000962 case tok::amp: { // unary-expression: '&' cast-expression
963 // Special treatment because of member pointers
964 SourceLocation SavedLoc = ConsumeToken();
965 Res = ParseCastExpression(false, true);
966 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000967 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000968 return Res;
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000969 }
970
Chris Lattner81b576e2006-08-11 02:13:20 +0000971 case tok::star: // unary-expression: '*' cast-expression
972 case tok::plus: // unary-expression: '+' cast-expression
973 case tok::minus: // unary-expression: '-' cast-expression
974 case tok::tilde: // unary-expression: '~' cast-expression
975 case tok::exclaim: // unary-expression: '!' cast-expression
976 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000977 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000978 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000979 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000980 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000981 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000982 return Res;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000983 }
984
Chris Lattnerc43926f2008-02-02 20:20:10 +0000985 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
986 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000987 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000988 SourceLocation SavedLoc = ConsumeToken();
989 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000990 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000991 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000992 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000993 }
Jordan Rose58d54722012-06-30 21:33:57 +0000994 case tok::kw__Alignof: // unary-expression: '_Alignof' '(' type-name ')'
995 if (!getLangOpts().C11)
996 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
997 // fallthrough
998 case tok::kw_alignof: // unary-expression: 'alignof' '(' type-id ')'
Chris Lattner81b576e2006-08-11 02:13:20 +0000999 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
1000 // unary-expression: '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +00001001 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
1002 // unary-expression: 'sizeof' '(' type-name ')'
Peter Collingbournee190dee2011-03-11 19:24:49 +00001003 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
1004 return ParseUnaryExprOrTypeTraitExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001005 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +00001006 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001007 if (Tok.isNot(tok::identifier))
1008 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001009
Chris Lattner9ba479b2011-02-18 21:16:39 +00001010 if (getCurScope()->getFnParent() == 0)
1011 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
1012
Chris Lattnereefa10e2007-05-28 06:56:27 +00001013 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001014 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1015 Tok.getLocation());
1016 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Chris Lattner14a1b642006-10-15 22:33:58 +00001017 ConsumeToken();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001018 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001019 }
Chris Lattner29375652006-12-04 18:06:35 +00001020 case tok::kw_const_cast:
1021 case tok::kw_dynamic_cast:
1022 case tok::kw_reinterpret_cast:
1023 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +00001024 Res = ParseCXXCasts();
John McCallb268a282010-08-23 23:25:46 +00001025 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001026 case tok::kw_typeid:
1027 Res = ParseCXXTypeid();
John McCallb268a282010-08-23 23:25:46 +00001028 break;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001029 case tok::kw___uuidof:
1030 Res = ParseCXXUuidof();
1031 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001032 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +00001033 Res = ParseCXXThis();
John McCallb268a282010-08-23 23:25:46 +00001034 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001035
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001036 case tok::annot_typename:
1037 if (isStartOfObjCClassMessageMissingOpenBracket()) {
1038 ParsedType Type = getTypeAnnotation(Tok);
1039
1040 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001041 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001042 DS.SetRangeStart(Tok.getLocation());
1043 DS.SetRangeEnd(Tok.getLastLoc());
1044
1045 const char *PrevSpec = 0;
1046 unsigned DiagID;
Nico Weber77430342010-11-22 10:30:56 +00001047 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
1048 PrevSpec, DiagID, Type);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001049
1050 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1051 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1052 if (Ty.isInvalid())
1053 break;
1054
1055 ConsumeToken();
1056 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1057 Ty.get(), 0);
1058 break;
1059 }
1060 // Fall through
1061
David Blaikie25896afb2012-01-24 05:47:35 +00001062 case tok::annot_decltype:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001063 case tok::kw_char:
1064 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001065 case tok::kw_char16_t:
1066 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001067 case tok::kw_bool:
1068 case tok::kw_short:
1069 case tok::kw_int:
1070 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00001071 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00001072 case tok::kw___int128:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001073 case tok::kw_signed:
1074 case tok::kw_unsigned:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001075 case tok::kw_half:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001076 case tok::kw_float:
1077 case tok::kw_double:
1078 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +00001079 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +00001080 case tok::kw_typeof:
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001081 case tok::kw___vector: {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001082 if (!getLangOpts().CPlusPlus) {
Chris Lattner8a38aa82009-01-04 22:28:21 +00001083 Diag(Tok, diag::err_expected_expression);
1084 return ExprError();
1085 }
Eli Friedman6d692cc2009-06-11 00:33:41 +00001086
1087 if (SavedKind == tok::kw_typename) {
1088 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001089 // typename-specifier braced-init-list
John McCall1f476a12010-02-26 08:45:28 +00001090 if (TryAnnotateTypeOrScopeToken())
Eli Friedman6d692cc2009-06-11 00:33:41 +00001091 return ExprError();
1092 }
1093
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001094 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001095 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001096 //
John McCall084e83d2011-03-24 11:26:52 +00001097 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001098 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redl3da34892011-06-05 12:23:16 +00001099 if (Tok.isNot(tok::l_paren) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001100 (!getLangOpts().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl59b5e512008-12-11 21:36:32 +00001101 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1102 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001103
Richard Smith5d164bc2011-10-15 05:09:34 +00001104 if (Tok.is(tok::l_brace))
1105 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1106
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001107 Res = ParseCXXTypeConstructExpression(DS);
John McCallb268a282010-08-23 23:25:46 +00001108 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001109 }
1110
Douglas Gregor7df89f52010-02-05 19:11:37 +00001111 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001112 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1113 // (We can end up in this situation after tentative parsing.)
1114 if (TryAnnotateTypeOrScopeToken())
1115 return ExprError();
1116 if (!Tok.is(tok::annot_cxxscope))
1117 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001118 NotCastExpr, isTypeCast);
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001119
Douglas Gregor7df89f52010-02-05 19:11:37 +00001120 Token Next = NextToken();
1121 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001122 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001123 if (TemplateId->Kind == TNK_Type_template) {
1124 // We have a qualified template-id that we know refers to a
1125 // type, translate it into a type and continue parsing as a
1126 // cast expression.
1127 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001128 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1129 /*EnteringContext=*/false);
Douglas Gregore7c20652011-03-02 00:47:37 +00001130 AnnotateTemplateIdTokenAsType();
Douglas Gregor7df89f52010-02-05 19:11:37 +00001131 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001132 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001133 }
1134 }
1135
1136 // Parse as an id-expression.
1137 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001138 break;
Douglas Gregor7df89f52010-02-05 19:11:37 +00001139 }
1140
1141 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001142 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001143 if (TemplateId->Kind == TNK_Type_template) {
1144 // We have a template-id that we know refers to a type,
1145 // translate it into a type and continue parsing as a cast
1146 // expression.
1147 AnnotateTemplateIdTokenAsType();
1148 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001149 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001150 }
1151
1152 // Fall through to treat the template-id as an id-expression.
1153 }
1154
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001155 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001156 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001157 break;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001158
Chris Lattner122db262009-01-04 22:52:14 +00001159 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001160 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1161 // annotates the token, tail recurse.
1162 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001163 return ExprError();
1164 if (!Tok.is(tok::coloncolon))
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001165 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1166
Chris Lattner122db262009-01-04 22:52:14 +00001167 // ::new -> [C++] new-expression
1168 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001169 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +00001170 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001171 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +00001172 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001173 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001174
Chris Lattner9a8968b2009-01-04 23:23:14 +00001175 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001176 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +00001177 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +00001178 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +00001179
Sebastian Redlbd150f42008-11-21 19:14:01 +00001180 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001181 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001182
1183 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001184 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001185
Sebastian Redl22e3a932010-09-10 20:55:37 +00001186 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smithb15c11c2011-10-17 23:06:20 +00001187 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001188 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001189 BalancedDelimiterTracker T(*this, tok::l_paren);
1190
1191 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl22e3a932010-09-10 20:55:37 +00001192 return ExprError();
Richard Smith764d2fe2011-12-20 02:08:33 +00001193 // C++11 [expr.unary.noexcept]p1:
Sebastian Redle56be2f72010-09-10 21:57:27 +00001194 // The noexcept operator determines whether the evaluation of its operand,
1195 // which is an unevaluated operand, can throw an exception.
1196 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001197 ExprResult Result = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001198
1199 T.consumeClose();
1200
Sebastian Redl22e3a932010-09-10 20:55:37 +00001201 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001202 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1203 Result.take(), T.getCloseLocation());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001204 return Result;
Sebastian Redl22e3a932010-09-10 20:55:37 +00001205 }
1206
Chandler Carruth79803482011-04-23 10:47:20 +00001207 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001208 case tok::kw___is_class:
Eli Friedmanc96d4962009-08-15 21:55:26 +00001209 case tok::kw___is_empty:
Chandler Carruth79803482011-04-23 10:47:20 +00001210 case tok::kw___is_enum:
John McCallbf4a7d72012-09-25 07:32:49 +00001211 case tok::kw___is_interface_class:
Sebastian Redl79eba1c2009-12-03 00:13:20 +00001212 case tok::kw___is_literal:
John Wiegley65497cc2011-04-27 23:09:49 +00001213 case tok::kw___is_arithmetic:
1214 case tok::kw___is_integral:
1215 case tok::kw___is_floating_point:
1216 case tok::kw___is_complete_type:
1217 case tok::kw___is_void:
1218 case tok::kw___is_array:
1219 case tok::kw___is_function:
1220 case tok::kw___is_reference:
1221 case tok::kw___is_lvalue_reference:
1222 case tok::kw___is_rvalue_reference:
1223 case tok::kw___is_fundamental:
1224 case tok::kw___is_object:
1225 case tok::kw___is_scalar:
1226 case tok::kw___is_compound:
1227 case tok::kw___is_pointer:
1228 case tok::kw___is_member_object_pointer:
1229 case tok::kw___is_member_function_pointer:
1230 case tok::kw___is_member_pointer:
1231 case tok::kw___is_const:
1232 case tok::kw___is_volatile:
1233 case tok::kw___is_standard_layout:
1234 case tok::kw___is_signed:
1235 case tok::kw___is_unsigned:
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00001236 case tok::kw___is_literal_type:
Chandler Carruth79803482011-04-23 10:47:20 +00001237 case tok::kw___is_pod:
1238 case tok::kw___is_polymorphic:
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +00001239 case tok::kw___is_trivial:
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001240 case tok::kw___is_trivially_copyable:
Chandler Carruth79803482011-04-23 10:47:20 +00001241 case tok::kw___is_union:
Douglas Gregordca70af2011-12-03 18:14:24 +00001242 case tok::kw___is_final:
Anders Carlssonfe63dc52009-04-16 00:08:20 +00001243 case tok::kw___has_trivial_constructor:
Douglas Gregor79f83ed2009-07-23 23:49:00 +00001244 case tok::kw___has_trivial_copy:
1245 case tok::kw___has_trivial_assign:
Anders Carlsson6dc35752009-04-17 02:34:54 +00001246 case tok::kw___has_trivial_destructor:
Sebastian Redl7dcb1552010-08-31 04:59:00 +00001247 case tok::kw___has_nothrow_assign:
1248 case tok::kw___has_nothrow_copy:
1249 case tok::kw___has_nothrow_constructor:
Sebastian Redlb469afb2010-09-02 23:19:42 +00001250 case tok::kw___has_virtual_destructor:
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001251 return ParseUnaryTypeTrait();
1252
Francois Pichet34b21132010-12-08 22:35:30 +00001253 case tok::kw___builtin_types_compatible_p:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001254 case tok::kw___is_base_of:
John Wiegley65497cc2011-04-27 23:09:49 +00001255 case tok::kw___is_same:
1256 case tok::kw___is_convertible:
Douglas Gregor8006e762011-01-27 20:28:01 +00001257 case tok::kw___is_convertible_to:
Douglas Gregor1be329d2012-02-23 07:33:15 +00001258 case tok::kw___is_trivially_assignable:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001259 return ParseBinaryTypeTrait();
1260
Douglas Gregor29c42f22012-02-24 07:38:34 +00001261 case tok::kw___is_trivially_constructible:
1262 return ParseTypeTrait();
1263
John Wiegley6242b6a2011-04-28 00:16:57 +00001264 case tok::kw___array_rank:
1265 case tok::kw___array_extent:
1266 return ParseArrayTypeTrait();
1267
John Wiegleyf9f65842011-04-25 06:54:41 +00001268 case tok::kw___is_lvalue_expr:
1269 case tok::kw___is_rvalue_expr:
1270 return ParseExpressionTrait();
1271
Chris Lattner644e1b72007-10-03 22:03:06 +00001272 case tok::at: {
1273 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001274 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +00001275 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001276 case tok::caret:
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001277 Res = ParseBlockLiteralExpression();
1278 break;
1279 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001280 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001281 cutOffParsing();
1282 return ExprError();
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001283 }
Chris Lattner6bf1db12008-12-12 19:20:14 +00001284 case tok::l_square:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001285 if (getLangOpts().CPlusPlus0x) {
1286 if (getLangOpts().ObjC1) {
Eli Friedmanc7c97142012-01-04 02:40:39 +00001287 // C++11 lambda expressions and Objective-C message sends both start with a
1288 // square bracket. There are three possibilities here:
1289 // we have a valid lambda expression, we have an invalid lambda
1290 // expression, or we have something that doesn't appear to be a lambda.
1291 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001292 Res = TryParseLambdaExpression();
Eli Friedmanc7c97142012-01-04 02:40:39 +00001293 if (!Res.isInvalid() && !Res.get())
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001294 Res = ParseObjCMessageExpression();
1295 break;
1296 }
1297 Res = ParseLambdaExpression();
1298 break;
1299 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001300 if (getLangOpts().ObjC1) {
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001301 Res = ParseObjCMessageExpression();
1302 break;
1303 }
1304 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +00001305 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001306 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001307 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +00001308 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001309
John McCallb268a282010-08-23 23:25:46 +00001310 // These can be followed by postfix-expr pieces.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001311 return ParsePostfixExpressionSuffix(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +00001312}
1313
James Dennett3d5e4592012-06-17 04:36:28 +00001314/// \brief Once the leading part of a postfix-expression is parsed, this
1315/// method parses any suffixes that apply.
Chris Lattner20c6a452006-08-12 17:40:43 +00001316///
James Dennett3d5e4592012-06-17 04:36:28 +00001317/// \verbatim
Chris Lattner20c6a452006-08-12 17:40:43 +00001318/// postfix-expression: [C99 6.5.2]
1319/// primary-expression
1320/// postfix-expression '[' expression ']'
Sebastian Redl3da34892011-06-05 12:23:16 +00001321/// postfix-expression '[' braced-init-list ']'
Chris Lattner20c6a452006-08-12 17:40:43 +00001322/// postfix-expression '(' argument-expression-list[opt] ')'
1323/// postfix-expression '.' identifier
1324/// postfix-expression '->' identifier
1325/// postfix-expression '++'
1326/// postfix-expression '--'
1327/// '(' type-name ')' '{' initializer-list '}'
1328/// '(' type-name ')' '{' initializer-list ',' '}'
1329///
1330/// argument-expression-list: [C99 6.5.2]
Douglas Gregor968f23a2011-01-03 19:31:53 +00001331/// argument-expression ...[opt]
1332/// argument-expression-list ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +00001333/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001334ExprResult
1335Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001336 // Now that the primary-expression piece of the postfix-expression has been
1337 // parsed, see if there are any postfix-expression pieces here.
1338 SourceLocation Loc;
1339 while (1) {
1340 switch (Tok.getKind()) {
Douglas Gregored0b69d2010-09-15 16:23:04 +00001341 case tok::code_completion:
1342 if (InMessageExpression)
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001343 return LHS;
Douglas Gregored0b69d2010-09-15 16:23:04 +00001344
Douglas Gregoreda7e542010-09-18 01:28:11 +00001345 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001346 cutOffParsing();
1347 return ExprError();
Douglas Gregored0b69d2010-09-15 16:23:04 +00001348
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001349 case tok::identifier:
1350 // If we see identifier: after an expression, and we're not already in a
1351 // message send, then this is probably a message send with a missing
1352 // opening bracket '['.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001353 if (getLangOpts().ObjC1 && !InMessageExpression &&
Douglas Gregord6d98002010-09-15 14:54:45 +00001354 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001355 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1356 ParsedType(), LHS.get());
1357 break;
1358 }
1359
1360 // Fall through; this isn't a message send.
1361
Chris Lattner20c6a452006-08-12 17:40:43 +00001362 default: // Not a postfix-expression suffix.
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001363 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001364 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner47054fb2010-05-31 18:18:22 +00001365 // If we have a array postfix expression that starts on a new line and
1366 // Objective-C is enabled, it is highly likely that the user forgot a
1367 // semicolon after the base expression and that the array postfix-expr is
1368 // actually another message send. In this case, do some look-ahead to see
1369 // if the contents of the square brackets are obviously not a valid
1370 // expression and recover by pretending there is no suffix.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001371 if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
Chris Lattner47054fb2010-05-31 18:18:22 +00001372 isSimpleObjCMessageExpression())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001373 return LHS;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001374
1375 // Reject array indices starting with a lambda-expression. '[[' is
1376 // reserved for attributes.
1377 if (CheckProhibitedCXX11Attribute())
1378 return ExprError();
1379
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001380 BalancedDelimiterTracker T(*this, tok::l_square);
1381 T.consumeOpen();
1382 Loc = T.getOpenLocation();
Sebastian Redl3da34892011-06-05 12:23:16 +00001383 ExprResult Idx;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001384 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001385 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00001386 Idx = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001387 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00001388 Idx = ParseExpression();
Sebastian Redl511ed552008-11-25 22:21:31 +00001389
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001390 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001391
1392 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCallb268a282010-08-23 23:25:46 +00001393 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1394 Idx.take(), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001395 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +00001396 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001397
Chris Lattner89c50c62006-08-11 06:41:18 +00001398 // Match the ']'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001399 T.consumeClose();
Chris Lattner89c50c62006-08-11 06:41:18 +00001400 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001401 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001402
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001403 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1404 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1405 // '(' argument-expression-list[opt] ')'
1406 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001407 InMessageExpressionRAIIObject InMessage(*this, false);
1408
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001409 Expr *ExecConfig = 0;
1410
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001411 BalancedDelimiterTracker PT(*this, tok::l_paren);
1412
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001413 if (OpKind == tok::lesslessless) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00001414 ExprVector ExecConfigExprs;
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001415 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001416 SourceLocation OpenLoc = ConsumeToken();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001417
1418 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1419 LHS = ExprError();
1420 }
1421
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001422 SourceLocation CloseLoc = Tok.getLocation();
1423 if (Tok.is(tok::greatergreatergreater)) {
1424 ConsumeToken();
1425 } else if (LHS.isInvalid()) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001426 SkipUntil(tok::greatergreatergreater);
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001427 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001428 // There was an error closing the brackets
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001429 Diag(Tok, diag::err_expected_ggg);
1430 Diag(OpenLoc, diag::note_matching) << "<<<";
1431 SkipUntil(tok::greatergreatergreater);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001432 LHS = ExprError();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001433 }
1434
1435 if (!LHS.isInvalid()) {
1436 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1437 LHS = ExprError();
1438 else
1439 Loc = PrevTokLocation;
1440 }
1441
1442 if (!LHS.isInvalid()) {
1443 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001444 OpenLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001445 ExecConfigExprs,
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001446 CloseLoc);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001447 if (ECResult.isInvalid())
1448 LHS = ExprError();
1449 else
1450 ExecConfig = ECResult.get();
1451 }
1452 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001453 PT.consumeOpen();
1454 Loc = PT.getOpenLocation();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001455 }
1456
Benjamin Kramerf0623432012-08-23 22:51:59 +00001457 ExprVector ArgExprs;
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001458 CommaLocsTy CommaLocs;
Douglas Gregoreda7e542010-09-18 01:28:11 +00001459
Douglas Gregorcabea402009-09-22 15:41:20 +00001460 if (Tok.is(tok::code_completion)) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00001461 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1462 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001463 cutOffParsing();
1464 return ExprError();
Douglas Gregorcabea402009-09-22 15:41:20 +00001465 }
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001466
1467 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1468 if (Tok.isNot(tok::r_paren)) {
1469 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1470 LHS.get())) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001471 LHS = ExprError();
1472 }
Chris Lattner0c6c0342006-08-12 18:12:45 +00001473 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001474 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001475
Chris Lattner89c50c62006-08-11 06:41:18 +00001476 // Match the ')'.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001477 if (LHS.isInvalid()) {
1478 SkipUntil(tok::r_paren);
1479 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001480 PT.consumeClose();
Douglas Gregoreda7e542010-09-18 01:28:11 +00001481 LHS = ExprError();
1482 } else {
1483 assert((ArgExprs.size() == 0 ||
1484 ArgExprs.size()-1 == CommaLocs.size())&&
Chris Lattnere165d942006-08-24 04:40:38 +00001485 "Unexpected number of commas!");
John McCallb268a282010-08-23 23:25:46 +00001486 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001487 ArgExprs, Tok.getLocation(),
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001488 ExecConfig);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001489 PT.consumeClose();
Chris Lattnere165d942006-08-24 04:40:38 +00001490 }
Mike Stump11289f42009-09-09 15:08:12 +00001491
Chris Lattner89c50c62006-08-11 06:41:18 +00001492 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001493 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001494 case tok::arrow:
1495 case tok::period: {
1496 // postfix-expression: p-e '->' template[opt] id-expression
1497 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001498 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001499 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001500
Douglas Gregord8061562009-08-06 03:17:00 +00001501 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001502 ParsedType ObjectType;
Douglas Gregore610ada2010-02-24 18:44:31 +00001503 bool MayBePseudoDestructor = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001504 if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001505 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregore610ada2010-02-24 18:44:31 +00001506 OpLoc, OpKind, ObjectType,
1507 MayBePseudoDestructor);
Douglas Gregord8061562009-08-06 03:17:00 +00001508 if (LHS.isInvalid())
1509 break;
Douglas Gregore610ada2010-02-24 18:44:31 +00001510
Douglas Gregordf593fb2011-11-07 17:33:42 +00001511 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1512 /*EnteringContext=*/false,
Douglas Gregore610ada2010-02-24 18:44:31 +00001513 &MayBePseudoDestructor);
Douglas Gregor205a3612010-05-27 15:25:59 +00001514 if (SS.isNotEmpty())
John McCallba7bf592010-08-24 05:47:05 +00001515 ObjectType = ParsedType();
Douglas Gregord8061562009-08-06 03:17:00 +00001516 }
1517
Douglas Gregor2436e712009-09-17 21:32:03 +00001518 if (Tok.is(tok::code_completion)) {
1519 // Code completion for a member access expression.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001520 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor2436e712009-09-17 21:32:03 +00001521 OpLoc, OpKind == tok::arrow);
1522
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001523 cutOffParsing();
1524 return ExprError();
Douglas Gregor2436e712009-09-17 21:32:03 +00001525 }
1526
John McCallb268a282010-08-23 23:25:46 +00001527 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1528 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregore610ada2010-02-24 18:44:31 +00001529 ObjectType);
1530 break;
1531 }
1532
1533 // Either the action has told is that this cannot be a
1534 // pseudo-destructor expression (based on the type of base
1535 // expression), or we didn't see a '~' in the right place. We
1536 // can still parse a destructor name here, but in that case it
1537 // names a real destructor.
Francois Pichet64225792011-01-18 05:04:39 +00001538 // Allow explicit constructor calls in Microsoft mode.
1539 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001540 SourceLocation TemplateKWLoc;
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001541 UnqualifiedId Name;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001542 if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
Douglas Gregor36107ad2012-02-16 18:19:22 +00001543 // Objective-C++:
1544 // After a '.' in a member access expression, treat the keyword
1545 // 'class' as if it were an identifier.
1546 //
1547 // This hack allows property access to the 'class' method because it is
1548 // such a common method name. For other C++ keywords that are
1549 // Objective-C method names, one must use the message send syntax.
1550 IdentifierInfo *Id = Tok.getIdentifierInfo();
1551 SourceLocation Loc = ConsumeToken();
1552 Name.setIdentifier(Id, Loc);
1553 } else if (ParseUnqualifiedId(SS,
1554 /*EnteringContext=*/false,
1555 /*AllowDestructorName=*/true,
1556 /*AllowConstructorName=*/
David Blaikiebbafb8a2012-03-11 07:00:24 +00001557 getLangOpts().MicrosoftExt,
Douglas Gregor36107ad2012-02-16 18:19:22 +00001558 ObjectType, TemplateKWLoc, Name))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001559 LHS = ExprError();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001560
1561 if (!LHS.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001562 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001563 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001564 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1565 Tok.is(tok::l_paren));
Chris Lattner89c50c62006-08-11 06:41:18 +00001566 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001567 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001568 case tok::plusplus: // postfix-expression: postfix-expression '++'
1569 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001570 if (!LHS.isInvalid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001571 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCallb268a282010-08-23 23:25:46 +00001572 Tok.getKind(), LHS.take());
Sebastian Redl511ed552008-11-25 22:21:31 +00001573 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001574 ConsumeToken();
1575 break;
Chris Lattnerf8339772006-08-10 22:01:51 +00001576 }
1577 }
Chris Lattner52a99e52006-08-10 20:56:00 +00001578}
1579
Peter Collingbournee190dee2011-03-11 19:24:49 +00001580/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1581/// vec_step and we are at the start of an expression or a parenthesized
1582/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1583/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001584///
James Dennett3d5e4592012-06-17 04:36:28 +00001585/// \verbatim
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001586/// unary-expression: [C99 6.5.3]
1587/// 'sizeof' unary-expression
1588/// 'sizeof' '(' type-name ')'
1589/// [GNU] '__alignof' unary-expression
1590/// [GNU] '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +00001591/// [C11] '_Alignof' '(' type-name ')'
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001592/// [C++0x] 'alignof' '(' type-id ')'
1593///
1594/// [GNU] typeof-specifier:
1595/// typeof ( expressions )
1596/// typeof ( type-name )
1597/// [GNU/C++] typeof unary-expression
1598///
Peter Collingbournee190dee2011-03-11 19:24:49 +00001599/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1600/// vec_step ( expressions )
1601/// vec_step ( type-name )
James Dennett3d5e4592012-06-17 04:36:28 +00001602/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001603ExprResult
Peter Collingbournee190dee2011-03-11 19:24:49 +00001604Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1605 bool &isCastExpr,
1606 ParsedType &CastTy,
1607 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001608
1609 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournee190dee2011-03-11 19:24:49 +00001610 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
Jordan Rose58d54722012-06-30 21:33:57 +00001611 OpTok.is(tok::kw__Alignof) || OpTok.is(tok::kw_vec_step)) &&
Peter Collingbournee190dee2011-03-11 19:24:49 +00001612 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001613
John McCalldadc5752010-08-24 06:29:42 +00001614 ExprResult Operand;
Mike Stump11289f42009-09-09 15:08:12 +00001615
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001616 // If the operand doesn't start with an '(', it must be an expression.
1617 if (Tok.isNot(tok::l_paren)) {
1618 isCastExpr = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001619 if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001620 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1621 return ExprError();
1622 }
Mike Stump11289f42009-09-09 15:08:12 +00001623
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001624 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001625 } else {
1626 // If it starts with a '(', we know that it is either a parenthesized
1627 // type-name, or it is a unary-expression that starts with a compound
1628 // literal, or starts with a primary-expression that is a parenthesized
1629 // expression.
1630 ParenParseOption ExprType = CastExpr;
1631 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001632
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001633 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001634 false, CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001635 CastRange = SourceRange(LParenLoc, RParenLoc);
1636
1637 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1638 // a type.
1639 if (ExprType == CastExpr) {
1640 isCastExpr = true;
1641 return ExprEmpty();
1642 }
1643
David Blaikiebbafb8a2012-03-11 07:00:24 +00001644 if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
Douglas Gregor5dc05532010-07-28 18:22:12 +00001645 // GNU typeof in C requires the expression to be parenthesized. Not so for
1646 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1647 // the start of a unary-expression, but doesn't include any postfix
1648 // pieces. Parse these now if present.
John McCall3669c802010-08-24 23:41:43 +00001649 if (!Operand.isInvalid())
1650 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor5dc05532010-07-28 18:22:12 +00001651 }
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001652 }
1653
1654 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1655 isCastExpr = false;
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001656 return Operand;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001657}
1658
Chris Lattner20c6a452006-08-12 17:40:43 +00001659
James Dennett3d5e4592012-06-17 04:36:28 +00001660/// \brief Parse a sizeof or alignof expression.
1661///
1662/// \verbatim
Chris Lattner81b576e2006-08-11 02:13:20 +00001663/// unary-expression: [C99 6.5.3]
1664/// 'sizeof' unary-expression
1665/// 'sizeof' '(' type-name ')'
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001666/// [C++0x] 'sizeof' '...' '(' identifier ')'
Chris Lattner81b576e2006-08-11 02:13:20 +00001667/// [GNU] '__alignof' unary-expression
1668/// [GNU] '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +00001669/// [C11] '_Alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +00001670/// [C++0x] 'alignof' '(' type-id ')'
James Dennett3d5e4592012-06-17 04:36:28 +00001671/// \endverbatim
Peter Collingbournee190dee2011-03-11 19:24:49 +00001672ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Jordan Rose58d54722012-06-30 21:33:57 +00001673 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) ||
1674 Tok.is(tok::kw_alignof) || Tok.is(tok::kw__Alignof) ||
1675 Tok.is(tok::kw_vec_step)) &&
Peter Collingbournee190dee2011-03-11 19:24:49 +00001676 "Not a sizeof/alignof/vec_step expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001677 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001678 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001679
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001680 // [C++0x] 'sizeof' '...' '(' identifier ')'
1681 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1682 SourceLocation EllipsisLoc = ConsumeToken();
1683 SourceLocation LParenLoc, RParenLoc;
1684 IdentifierInfo *Name = 0;
1685 SourceLocation NameLoc;
1686 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001687 BalancedDelimiterTracker T(*this, tok::l_paren);
1688 T.consumeOpen();
1689 LParenLoc = T.getOpenLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001690 if (Tok.is(tok::identifier)) {
1691 Name = Tok.getIdentifierInfo();
1692 NameLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001693 T.consumeClose();
1694 RParenLoc = T.getCloseLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001695 if (RParenLoc.isInvalid())
1696 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1697 } else {
1698 Diag(Tok, diag::err_expected_parameter_pack);
1699 SkipUntil(tok::r_paren);
1700 }
1701 } else if (Tok.is(tok::identifier)) {
1702 Name = Tok.getIdentifierInfo();
1703 NameLoc = ConsumeToken();
1704 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1705 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1706 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1707 << Name
1708 << FixItHint::CreateInsertion(LParenLoc, "(")
1709 << FixItHint::CreateInsertion(RParenLoc, ")");
1710 } else {
1711 Diag(Tok, diag::err_sizeof_parameter_pack);
1712 }
1713
1714 if (!Name)
1715 return ExprError();
1716
1717 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1718 OpTok.getLocation(),
1719 *Name, NameLoc,
1720 RParenLoc);
1721 }
Richard Smithb15c11c2011-10-17 23:06:20 +00001722
Jordan Rose58d54722012-06-30 21:33:57 +00001723 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof))
Richard Smithb15c11c2011-10-17 23:06:20 +00001724 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1725
Eli Friedman15681d62012-09-26 04:34:21 +00001726 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1727 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00001728
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001729 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00001730 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001731 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001732 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1733 isCastExpr,
1734 CastTy,
1735 CastRange);
1736
1737 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
Jordan Rose58d54722012-06-30 21:33:57 +00001738 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof) ||
1739 OpTok.is(tok::kw__Alignof))
Peter Collingbournee190dee2011-03-11 19:24:49 +00001740 ExprKind = UETT_AlignOf;
1741 else if (OpTok.is(tok::kw_vec_step))
1742 ExprKind = UETT_VecStep;
Sebastian Redl90893182008-12-11 22:33:27 +00001743
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001744 if (isCastExpr)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001745 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1746 ExprKind,
1747 /*isType=*/true,
1748 CastTy.getAsOpaquePtr(),
1749 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001750
Chris Lattner26115ac2006-08-24 06:10:04 +00001751 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001752 if (!Operand.isInvalid())
Peter Collingbournee190dee2011-03-11 19:24:49 +00001753 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1754 ExprKind,
1755 /*isType=*/false,
1756 Operand.release(),
1757 CastRange);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001758 return Operand;
Chris Lattner81b576e2006-08-11 02:13:20 +00001759}
1760
Chris Lattner11124352006-08-12 19:16:08 +00001761/// ParseBuiltinPrimaryExpression
1762///
James Dennett3d5e4592012-06-17 04:36:28 +00001763/// \verbatim
Chris Lattner11124352006-08-12 19:16:08 +00001764/// primary-expression: [C99 6.5.1]
1765/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1766/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1767/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1768/// assign-expr ')'
1769/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbourne62c21982011-11-05 03:47:48 +00001770/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001771///
Chris Lattner11124352006-08-12 19:16:08 +00001772/// [GNU] offsetof-member-designator:
1773/// [GNU] identifier
1774/// [GNU] offsetof-member-designator '.' identifier
1775/// [GNU] offsetof-member-designator '[' expression ']'
James Dennett3d5e4592012-06-17 04:36:28 +00001776/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001777ExprResult Parser::ParseBuiltinPrimaryExpression() {
1778 ExprResult Res;
Chris Lattner11124352006-08-12 19:16:08 +00001779 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1780
1781 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001782 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001783
1784 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001785 if (Tok.isNot(tok::l_paren))
1786 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1787 << BuiltinII);
1788
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001789 BalancedDelimiterTracker PT(*this, tok::l_paren);
1790 PT.consumeOpen();
1791
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001792 // TODO: Build AST.
1793
Chris Lattner11124352006-08-12 19:16:08 +00001794 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +00001795 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001796 case tok::kw___builtin_va_arg: {
John McCalldadc5752010-08-24 06:29:42 +00001797 ExprResult Expr(ParseAssignmentExpression());
Chris Lattner0be454e2006-08-12 19:30:51 +00001798
Chris Lattner6d7e6342006-08-15 03:41:14 +00001799 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001800 Expr = ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001801
Douglas Gregor220cac52009-02-18 17:45:20 +00001802 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001803
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001804 if (Tok.isNot(tok::r_paren)) {
1805 Diag(Tok, diag::err_expected_rparen);
Douglas Gregoreda7e542010-09-18 01:28:11 +00001806 Expr = ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001807 }
Douglas Gregoreda7e542010-09-18 01:28:11 +00001808
1809 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor220cac52009-02-18 17:45:20 +00001810 Res = ExprError();
1811 else
John McCallb268a282010-08-23 23:25:46 +00001812 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001813 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001814 }
Chris Lattner687d6092007-08-30 15:51:11 +00001815 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001816 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001817 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001818 if (Ty.isInvalid()) {
1819 SkipUntil(tok::r_paren);
1820 return ExprError();
1821 }
Mike Stump11289f42009-09-09 15:08:12 +00001822
Chris Lattner6d7e6342006-08-15 03:41:14 +00001823 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001824 return ExprError();
1825
Chris Lattner11124352006-08-12 19:16:08 +00001826 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001827 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001828 Diag(Tok, diag::err_expected_ident);
1829 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001830 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001831 }
Sebastian Redl90893182008-12-11 22:33:27 +00001832
Chris Lattner687d6092007-08-30 15:51:11 +00001833 // Keep track of the various subcomponents we see.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001834 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001835
John McCallfaf5fb42010-08-26 23:41:50 +00001836 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001837 Comps.back().isBrackets = false;
1838 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1839 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001840
Sebastian Redl511ed552008-11-25 22:21:31 +00001841 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001842 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001843 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001844 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallfaf5fb42010-08-26 23:41:50 +00001845 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001846 Comps.back().isBrackets = false;
1847 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001848
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001849 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001850 Diag(Tok, diag::err_expected_ident);
1851 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001852 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001853 }
1854 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1855 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001856
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001857 } else if (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001858 if (CheckProhibitedCXX11Attribute())
1859 return ExprError();
1860
Chris Lattner11124352006-08-12 19:16:08 +00001861 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallfaf5fb42010-08-26 23:41:50 +00001862 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001863 Comps.back().isBrackets = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001864 BalancedDelimiterTracker ST(*this, tok::l_square);
1865 ST.consumeOpen();
1866 Comps.back().LocStart = ST.getOpenLocation();
Chris Lattner11124352006-08-12 19:16:08 +00001867 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001868 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001869 SkipUntil(tok::r_paren);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001870 return Res;
Chris Lattner11124352006-08-12 19:16:08 +00001871 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001872 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001873
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001874 ST.consumeClose();
1875 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman5e774b12009-06-27 20:38:33 +00001876 } else {
1877 if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001878 PT.consumeClose();
Douglas Gregor220cac52009-02-18 17:45:20 +00001879 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001880 } else if (Ty.isInvalid()) {
1881 Res = ExprError();
1882 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001883 PT.consumeClose();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001884 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001885 Ty.get(), &Comps[0], Comps.size(),
1886 PT.getCloseLocation());
Eli Friedman5e774b12009-06-27 20:38:33 +00001887 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001888 break;
Chris Lattner11124352006-08-12 19:16:08 +00001889 }
1890 }
1891 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001892 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001893 case tok::kw___builtin_choose_expr: {
John McCalldadc5752010-08-24 06:29:42 +00001894 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001895 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001896 SkipUntil(tok::r_paren);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001897 return Cond;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001898 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001899 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001900 return ExprError();
1901
John McCalldadc5752010-08-24 06:29:42 +00001902 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001903 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001904 SkipUntil(tok::r_paren);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001905 return Expr1;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001906 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001907 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001908 return ExprError();
1909
John McCalldadc5752010-08-24 06:29:42 +00001910 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001911 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001912 SkipUntil(tok::r_paren);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001913 return Expr2;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001914 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001915 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001916 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001917 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001918 }
John McCallb268a282010-08-23 23:25:46 +00001919 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1920 Expr2.take(), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001921 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001922 }
Tanya Lattner55808c12011-06-04 00:47:47 +00001923 case tok::kw___builtin_astype: {
1924 // The first argument is an expression to be converted, followed by a comma.
1925 ExprResult Expr(ParseAssignmentExpression());
1926 if (Expr.isInvalid()) {
1927 SkipUntil(tok::r_paren);
1928 return ExprError();
1929 }
1930
1931 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1932 tok::r_paren))
1933 return ExprError();
1934
1935 // Second argument is the type to bitcast to.
1936 TypeResult DestTy = ParseTypeName();
1937 if (DestTy.isInvalid())
1938 return ExprError();
1939
1940 // Attempt to consume the r-paren.
1941 if (Tok.isNot(tok::r_paren)) {
1942 Diag(Tok, diag::err_expected_rparen);
1943 SkipUntil(tok::r_paren);
1944 return ExprError();
1945 }
1946
1947 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1948 ConsumeParen());
1949 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001950 }
Chandler Carruth904cb142011-07-08 04:59:44 +00001951 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001952
John McCallb268a282010-08-23 23:25:46 +00001953 if (Res.isInvalid())
1954 return ExprError();
1955
Chris Lattner11124352006-08-12 19:16:08 +00001956 // These can be followed by postfix-expr pieces because they are
1957 // primary-expressions.
John McCallb268a282010-08-23 23:25:46 +00001958 return ParsePostfixExpressionSuffix(Res.take());
Chris Lattner11124352006-08-12 19:16:08 +00001959}
1960
Chris Lattner4add4e62006-08-11 01:33:00 +00001961/// ParseParenExpression - This parses the unit that starts with a '(' token,
1962/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001963/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1964/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001965///
James Dennett3d5e4592012-06-17 04:36:28 +00001966/// \verbatim
Chris Lattner4add4e62006-08-11 01:33:00 +00001967/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001968/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001969/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1970/// postfix-expression: [C99 6.5.2]
1971/// '(' type-name ')' '{' initializer-list '}'
1972/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001973/// cast-expression: [C99 6.5.4]
1974/// '(' type-name ')' cast-expression
John McCall31168b02011-06-15 23:02:42 +00001975/// [ARC] bridged-cast-expression
1976///
1977/// [ARC] bridged-cast-expression:
1978/// (__bridge type-name) cast-expression
1979/// (__bridge_transfer type-name) cast-expression
1980/// (__bridge_retained type-name) cast-expression
James Dennett3d5e4592012-06-17 04:36:28 +00001981/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001982ExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001983Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001984 bool isTypeCast, ParsedType &CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001985 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001986 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001987 BalancedDelimiterTracker T(*this, tok::l_paren);
1988 if (T.consumeOpen())
1989 return ExprError();
1990 SourceLocation OpenLoc = T.getOpenLocation();
1991
John McCalldadc5752010-08-24 06:29:42 +00001992 ExprResult Result(true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001993 bool isAmbiguousTypeId;
John McCallba7bf592010-08-24 05:47:05 +00001994 CastTy = ParsedType();
Sebastian Redl90893182008-12-11 22:33:27 +00001995
Douglas Gregor5e35d592010-09-14 23:59:36 +00001996 if (Tok.is(tok::code_completion)) {
1997 Actions.CodeCompleteOrdinaryName(getCurScope(),
1998 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1999 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002000 cutOffParsing();
Douglas Gregor5e35d592010-09-14 23:59:36 +00002001 return ExprError();
2002 }
John McCallc5e6b972011-04-06 02:35:25 +00002003
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00002004 // Diagnose use of bridge casts in non-arc mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002005 bool BridgeCast = (getLangOpts().ObjC2 &&
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00002006 (Tok.is(tok::kw___bridge) ||
2007 Tok.is(tok::kw___bridge_transfer) ||
2008 Tok.is(tok::kw___bridge_retained) ||
2009 Tok.is(tok::kw___bridge_retain)));
David Blaikiebbafb8a2012-03-11 07:00:24 +00002010 if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00002011 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00002012 SourceLocation BridgeKeywordLoc = ConsumeToken();
2013 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremenek084e1b42012-02-18 04:42:38 +00002014 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00002015 << BridgeCastName
2016 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00002017 BridgeCast = false;
2018 }
2019
John McCallc5e6b972011-04-06 02:35:25 +00002020 // None of these cases should fall through with an invalid Result
2021 // unless they've already reported an error.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00002022 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00002023 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall3abee492012-04-04 01:27:53 +00002024 Actions.ActOnStartStmtExpr();
2025
Richard Smithc202b282012-04-14 00:33:13 +00002026 StmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4add4e62006-08-11 01:33:00 +00002027 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002028
Chris Lattner366727f2007-07-24 16:58:17 +00002029 // If the substmt parsed correctly, build the AST node.
John McCall3abee492012-04-04 01:27:53 +00002030 if (!Stmt.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00002031 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
John McCall3abee492012-04-04 01:27:53 +00002032 } else {
2033 Actions.ActOnStmtExprError();
2034 }
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00002035 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCall0c07bee2011-06-17 21:56:12 +00002036 tok::TokenKind tokenKind = Tok.getKind();
2037 SourceLocation BridgeKeywordLoc = ConsumeToken();
2038
John McCall31168b02011-06-15 23:02:42 +00002039 // Parse an Objective-C ARC ownership cast expression.
2040 ObjCBridgeCastKind Kind;
John McCall0c07bee2011-06-17 21:56:12 +00002041 if (tokenKind == tok::kw___bridge)
John McCall31168b02011-06-15 23:02:42 +00002042 Kind = OBC_Bridge;
John McCall0c07bee2011-06-17 21:56:12 +00002043 else if (tokenKind == tok::kw___bridge_transfer)
John McCall31168b02011-06-15 23:02:42 +00002044 Kind = OBC_BridgeTransfer;
John McCall0c07bee2011-06-17 21:56:12 +00002045 else if (tokenKind == tok::kw___bridge_retained)
John McCall31168b02011-06-15 23:02:42 +00002046 Kind = OBC_BridgeRetained;
John McCall0c07bee2011-06-17 21:56:12 +00002047 else {
2048 // As a hopefully temporary workaround, allow __bridge_retain as
2049 // a synonym for __bridge_retained, but only in system headers.
2050 assert(tokenKind == tok::kw___bridge_retain);
2051 Kind = OBC_BridgeRetained;
2052 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
2053 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
2054 << FixItHint::CreateReplacement(BridgeKeywordLoc,
2055 "__bridge_retained");
2056 }
John McCall31168b02011-06-15 23:02:42 +00002057
John McCall31168b02011-06-15 23:02:42 +00002058 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002059 T.consumeClose();
2060 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002061 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCall31168b02011-06-15 23:02:42 +00002062
2063 if (Ty.isInvalid() || SubExpr.isInvalid())
2064 return ExprError();
2065
2066 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
2067 BridgeKeywordLoc, Ty.get(),
2068 RParenLoc, SubExpr.get());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002069 } else if (ExprType >= CompoundLiteral &&
2070 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00002071
Chris Lattner6c3f05d2006-08-12 16:54:25 +00002072 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00002073
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002074 // In C++, if the type-id is ambiguous we disambiguate based on context.
2075 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
2076 // in which case we should treat it as type-id.
2077 // if stopIfCastExpr is false, we need to determine the context past the
2078 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002079 if (isAmbiguousTypeId && !stopIfCastExpr) {
2080 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
2081 RParenLoc = T.getCloseLocation();
2082 return res;
2083 }
Mike Stump11289f42009-09-09 15:08:12 +00002084
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002085 // Parse the type declarator.
2086 DeclSpec DS(AttrFactory);
2087 ParseSpecifierQualifierList(DS);
2088 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2089 ParseDeclarator(DeclaratorInfo);
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002090
Douglas Gregor3e972002010-09-15 23:19:31 +00002091 // If our type is followed by an identifier and either ':' or ']', then
2092 // this is probably an Objective-C message send where the leading '[' is
2093 // missing. Recover as if that were the case.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002094 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002095 !InMessageExpression && getLangOpts().ObjC1 &&
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002096 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
2097 TypeResult Ty;
2098 {
2099 InMessageExpressionRAIIObject InMessage(*this, false);
2100 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2101 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002102 Result = ParseObjCMessageExpressionBody(SourceLocation(),
2103 SourceLocation(),
2104 Ty.get(), 0);
2105 } else {
2106 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002107 T.consumeClose();
2108 RParenLoc = T.getCloseLocation();
Douglas Gregor3e972002010-09-15 23:19:31 +00002109 if (Tok.is(tok::l_brace)) {
2110 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002111 TypeResult Ty;
2112 {
2113 InMessageExpressionRAIIObject InMessage(*this, false);
2114 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2115 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002116 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +00002117 }
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00002118
Douglas Gregor3e972002010-09-15 23:19:31 +00002119 if (ExprType == CastExpr) {
2120 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb5d49352009-01-19 22:31:54 +00002121
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002122 if (DeclaratorInfo.isInvalidType())
Douglas Gregor3e972002010-09-15 23:19:31 +00002123 return ExprError();
2124
Douglas Gregor3e972002010-09-15 23:19:31 +00002125 // Note that this doesn't parse the subsequent cast-expression, it just
2126 // returns the parsed type to the callee.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002127 if (stopIfCastExpr) {
2128 TypeResult Ty;
2129 {
2130 InMessageExpressionRAIIObject InMessage(*this, false);
2131 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2132 }
2133 CastTy = Ty.get();
Douglas Gregor3e972002010-09-15 23:19:31 +00002134 return ExprResult();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002135 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002136
2137 // Reject the cast of super idiom in ObjC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002138 if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
Douglas Gregor3e972002010-09-15 23:19:31 +00002139 Tok.getIdentifierInfo() == Ident_super &&
2140 getCurScope()->isInObjcMethodScope() &&
2141 GetLookAheadToken(1).isNot(tok::period)) {
2142 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2143 << SourceRange(OpenLoc, RParenLoc);
2144 return ExprError();
2145 }
2146
2147 // Parse the cast-expression that follows it next.
2148 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002149 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2150 /*isAddressOfOperand=*/false,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002151 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002152 if (!Result.isInvalid()) {
2153 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2154 DeclaratorInfo, CastTy,
Douglas Gregor3e972002010-09-15 23:19:31 +00002155 RParenLoc, Result.take());
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002156 }
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002157 return Result;
Douglas Gregor3e972002010-09-15 23:19:31 +00002158 }
2159
2160 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2161 return ExprError();
2162 }
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002163 } else if (isTypeCast) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00002164 // Parse the expression-list.
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002165 InMessageExpressionRAIIObject InMessage(*this, false);
2166
Benjamin Kramerf0623432012-08-23 22:51:59 +00002167 ExprVector ArgExprs;
Nate Begeman5ec4b312009-08-10 23:49:36 +00002168 CommaLocsTy CommaLocs;
2169
2170 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2171 ExprType = SimpleExpr;
Sebastian Redla9351792012-02-11 23:51:47 +00002172 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002173 ArgExprs);
Nate Begeman5ec4b312009-08-10 23:49:36 +00002174 }
Chris Lattner4add4e62006-08-11 01:33:00 +00002175 } else {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002176 InMessageExpressionRAIIObject InMessage(*this, false);
2177
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002178 Result = ParseExpression(MaybeTypeCast);
Chris Lattner4add4e62006-08-11 01:33:00 +00002179 ExprType = SimpleExpr;
John McCallc5e6b972011-04-06 02:35:25 +00002180
2181 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002182 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCallb268a282010-08-23 23:25:46 +00002183 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Chris Lattnerf8339772006-08-10 22:01:51 +00002184 }
Sebastian Redl90893182008-12-11 22:33:27 +00002185
Chris Lattner4564bc12006-08-10 23:14:52 +00002186 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00002187 if (Result.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +00002188 SkipUntil(tok::r_paren);
Chris Lattnerd8980502008-12-12 06:00:12 +00002189 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00002190 }
Mike Stump11289f42009-09-09 15:08:12 +00002191
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002192 T.consumeClose();
2193 RParenLoc = T.getCloseLocation();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002194 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +00002195}
Chris Lattnerd3e98952006-10-06 05:22:26 +00002196
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002197/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2198/// and we are at the left brace.
2199///
James Dennett3d5e4592012-06-17 04:36:28 +00002200/// \verbatim
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002201/// postfix-expression: [C99 6.5.2]
2202/// '(' type-name ')' '{' initializer-list '}'
2203/// '(' type-name ')' '{' initializer-list ',' '}'
James Dennett3d5e4592012-06-17 04:36:28 +00002204/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002205ExprResult
John McCallba7bf592010-08-24 05:47:05 +00002206Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002207 SourceLocation LParenLoc,
2208 SourceLocation RParenLoc) {
2209 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
David Blaikiebbafb8a2012-03-11 07:00:24 +00002210 if (!getLangOpts().C99) // Compound literals don't exist in C90.
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002211 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCalldadc5752010-08-24 06:29:42 +00002212 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002213 if (!Result.isInvalid() && Ty)
John McCallb268a282010-08-23 23:25:46 +00002214 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002215 return Result;
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002216}
2217
Chris Lattnerd3e98952006-10-06 05:22:26 +00002218/// ParseStringLiteralExpression - This handles the various token types that
2219/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2220/// translation phase #6].
2221///
James Dennett3d5e4592012-06-17 04:36:28 +00002222/// \verbatim
Chris Lattnerd3e98952006-10-06 05:22:26 +00002223/// primary-expression: [C99 6.5.1]
2224/// string-literal
James Dennett3d5e4592012-06-17 04:36:28 +00002225/// \verbatim
Richard Smithd67aea22012-03-06 03:21:47 +00002226ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002227 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00002228
Chris Lattnerd3e98952006-10-06 05:22:26 +00002229 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2230 // considered to be strings for concatenation purposes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002231 SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00002232
Chris Lattnerd3e98952006-10-06 05:22:26 +00002233 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002234 StringToks.push_back(Tok);
2235 ConsumeStringToken();
2236 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00002237
2238 // Pass the set of string tokens, ready for concatenation, to the actions.
Richard Smithbcc22fc2012-03-09 08:00:36 +00002239 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(),
2240 AllowUserDefinedLiteral ? getCurScope() : 0);
Chris Lattnerd3e98952006-10-06 05:22:26 +00002241}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002242
Benjamin Kramere56f3932011-12-23 17:00:35 +00002243/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2244/// [C11 6.5.1.1].
Peter Collingbourne91147592011-04-15 00:35:48 +00002245///
James Dennett3d5e4592012-06-17 04:36:28 +00002246/// \verbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002247/// generic-selection:
2248/// _Generic ( assignment-expression , generic-assoc-list )
2249/// generic-assoc-list:
2250/// generic-association
2251/// generic-assoc-list , generic-association
2252/// generic-association:
2253/// type-name : assignment-expression
2254/// default : assignment-expression
James Dennett3d5e4592012-06-17 04:36:28 +00002255/// \endverbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002256ExprResult Parser::ParseGenericSelectionExpression() {
2257 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2258 SourceLocation KeyLoc = ConsumeToken();
2259
David Blaikiebbafb8a2012-03-11 07:00:24 +00002260 if (!getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +00002261 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbourne91147592011-04-15 00:35:48 +00002262
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002263 BalancedDelimiterTracker T(*this, tok::l_paren);
2264 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne91147592011-04-15 00:35:48 +00002265 return ExprError();
2266
2267 ExprResult ControllingExpr;
2268 {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002269 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbourne91147592011-04-15 00:35:48 +00002270 // not evaluated."
2271 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2272 ControllingExpr = ParseAssignmentExpression();
2273 if (ControllingExpr.isInvalid()) {
2274 SkipUntil(tok::r_paren);
2275 return ExprError();
2276 }
2277 }
2278
2279 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2280 SkipUntil(tok::r_paren);
2281 return ExprError();
2282 }
2283
2284 SourceLocation DefaultLoc;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002285 TypeVector Types;
2286 ExprVector Exprs;
Peter Collingbourne91147592011-04-15 00:35:48 +00002287 while (1) {
2288 ParsedType Ty;
2289 if (Tok.is(tok::kw_default)) {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002290 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbourne91147592011-04-15 00:35:48 +00002291 // generic association."
2292 if (!DefaultLoc.isInvalid()) {
2293 Diag(Tok, diag::err_duplicate_default_assoc);
2294 Diag(DefaultLoc, diag::note_previous_default_assoc);
2295 SkipUntil(tok::r_paren);
2296 return ExprError();
2297 }
2298 DefaultLoc = ConsumeToken();
2299 Ty = ParsedType();
2300 } else {
2301 ColonProtectionRAIIObject X(*this);
2302 TypeResult TR = ParseTypeName();
2303 if (TR.isInvalid()) {
2304 SkipUntil(tok::r_paren);
2305 return ExprError();
2306 }
2307 Ty = TR.release();
2308 }
2309 Types.push_back(Ty);
2310
2311 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2312 SkipUntil(tok::r_paren);
2313 return ExprError();
2314 }
2315
2316 // FIXME: These expressions should be parsed in a potentially potentially
2317 // evaluated context.
2318 ExprResult ER(ParseAssignmentExpression());
2319 if (ER.isInvalid()) {
2320 SkipUntil(tok::r_paren);
2321 return ExprError();
2322 }
2323 Exprs.push_back(ER.release());
2324
2325 if (Tok.isNot(tok::comma))
2326 break;
2327 ConsumeToken();
2328 }
2329
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002330 T.consumeClose();
2331 if (T.getCloseLocation().isInvalid())
Peter Collingbourne91147592011-04-15 00:35:48 +00002332 return ExprError();
2333
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002334 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2335 T.getCloseLocation(),
Peter Collingbourne91147592011-04-15 00:35:48 +00002336 ControllingExpr.release(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002337 Types, Exprs);
Peter Collingbourne91147592011-04-15 00:35:48 +00002338}
2339
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002340/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2341///
James Dennett3d5e4592012-06-17 04:36:28 +00002342/// \verbatim
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002343/// argument-expression-list:
2344/// assignment-expression
2345/// argument-expression-list , assignment-expression
2346///
2347/// [C++] expression-list:
Sebastian Redl3da34892011-06-05 12:23:16 +00002348/// [C++] assignment-expression
2349/// [C++] expression-list , assignment-expression
2350///
2351/// [C++0x] expression-list:
2352/// [C++0x] initializer-list
2353///
2354/// [C++0x] initializer-list
2355/// [C++0x] initializer-clause ...[opt]
2356/// [C++0x] initializer-list , initializer-clause ...[opt]
2357///
2358/// [C++0x] initializer-clause:
2359/// [C++0x] assignment-expression
2360/// [C++0x] braced-init-list
James Dennett3d5e4592012-06-17 04:36:28 +00002361/// \endverbatim
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002362bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2363 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallfaf5fb42010-08-26 23:41:50 +00002364 void (Sema::*Completer)(Scope *S,
John McCall37ad5512010-08-23 06:44:23 +00002365 Expr *Data,
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002366 llvm::ArrayRef<Expr *> Args),
John McCall37ad5512010-08-23 06:44:23 +00002367 Expr *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002368 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00002369 if (Tok.is(tok::code_completion)) {
2370 if (Completer)
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002371 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor9c1f1bf2011-02-17 03:09:23 +00002372 else
2373 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002374 cutOffParsing();
2375 return true;
Douglas Gregorcabea402009-09-22 15:41:20 +00002376 }
Sebastian Redl3da34892011-06-05 12:23:16 +00002377
2378 ExprResult Expr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002379 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002380 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00002381 Expr = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00002382 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00002383 Expr = ParseAssignmentExpression();
2384
Douglas Gregor968f23a2011-01-03 19:31:53 +00002385 if (Tok.is(tok::ellipsis))
2386 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002387 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002388 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00002389
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002390 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002391
2392 if (Tok.isNot(tok::comma))
2393 return false;
2394 // Move to the next argument, remember where the comma was.
2395 CommaLocs.push_back(ConsumeToken());
2396 }
2397}
Steve Naroff0ac012832008-08-28 19:20:44 +00002398
Mike Stump82f071f2009-02-04 22:31:32 +00002399/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2400///
James Dennett3d5e4592012-06-17 04:36:28 +00002401/// \verbatim
Mike Stump82f071f2009-02-04 22:31:32 +00002402/// [clang] block-id:
2403/// [clang] specifier-qualifier-list block-declarator
James Dennett3d5e4592012-06-17 04:36:28 +00002404/// \endverbatim
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002405void Parser::ParseBlockId(SourceLocation CaretLoc) {
Douglas Gregor643c3302010-10-18 21:34:55 +00002406 if (Tok.is(tok::code_completion)) {
2407 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002408 return cutOffParsing();
Douglas Gregor643c3302010-10-18 21:34:55 +00002409 }
2410
Mike Stump82f071f2009-02-04 22:31:32 +00002411 // Parse the specifier-qualifier-list piece.
John McCall084e83d2011-03-24 11:26:52 +00002412 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002413 ParseSpecifierQualifierList(DS);
2414
2415 // Parse the block-declarator.
2416 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2417 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002418
Mike Stump56ed2ea2009-04-29 21:40:37 +00002419 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall084e83d2011-03-24 11:26:52 +00002420 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump56ed2ea2009-04-29 21:40:37 +00002421
John McCall53fa7142010-12-24 02:08:15 +00002422 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002423
Mike Stump82f071f2009-02-04 22:31:32 +00002424 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002425 Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
Mike Stump82f071f2009-02-04 22:31:32 +00002426}
2427
Steve Naroff0ac012832008-08-28 19:20:44 +00002428/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00002429/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00002430///
James Dennett3d5e4592012-06-17 04:36:28 +00002431/// \verbatim
Steve Naroff0ac012832008-08-28 19:20:44 +00002432/// block-literal:
2433/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00002434/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00002435/// [clang] block-args:
2436/// [clang] '(' parameter-list ')'
James Dennett3d5e4592012-06-17 04:36:28 +00002437/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002438ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00002439 assert(Tok.is(tok::caret) && "block literal starts with ^");
2440 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002441
Chris Lattnerf6801202009-03-05 07:32:12 +00002442 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2443 "block literal parsing");
2444
Mike Stump11289f42009-09-09 15:08:12 +00002445 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00002446 // argument decls, decls within the compound expression, etc. This also
2447 // allows determining whether a variable reference inside the block is
2448 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002449 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002450 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00002451
2452 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002453 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump11289f42009-09-09 15:08:12 +00002454
Steve Naroff0ac012832008-08-28 19:20:44 +00002455 // Parse the return type if present.
John McCall084e83d2011-03-24 11:26:52 +00002456 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002457 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002458 // FIXME: Since the return type isn't actually parsed, it can't be used to
2459 // fill ParamInfo with an initial valid range, so do it manually.
2460 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002461
Steve Naroff0ac012832008-08-28 19:20:44 +00002462 // If this block has arguments, parse them. There is no ambiguity here with
2463 // the expression case, because the expression case requires a parameter list.
2464 if (Tok.is(tok::l_paren)) {
2465 ParseParenDeclarator(ParamInfo);
2466 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002467 // SetIdentifier sets the source range end, but in this case we're past
2468 // that location.
2469 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00002470 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002471 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002472 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00002473 // If there was an error parsing the arguments, they may have
2474 // tried to use ^(x+y) which requires an argument list. Just
2475 // skip the whole block literal.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002476 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002477 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00002478 }
Mike Stump88788fe2009-04-29 19:03:13 +00002479
John McCall53fa7142010-12-24 02:08:15 +00002480 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002481
Mike Stump82f071f2009-02-04 22:31:32 +00002482 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002483 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Mike Stumpd73e4412009-04-14 18:24:37 +00002484 } else if (!Tok.is(tok::l_brace)) {
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002485 ParseBlockId(CaretLoc);
Steve Naroff0ac012832008-08-28 19:20:44 +00002486 } else {
2487 // Otherwise, pretend we saw (void).
John McCall084e83d2011-03-24 11:26:52 +00002488 ParsedAttributes attrs(AttrFactory);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00002489 SourceLocation NoLoc;
2490 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(/*HasProto=*/true,
2491 /*IsAmbiguous=*/false,
2492 /*RParenLoc=*/NoLoc,
2493 /*ArgInfo=*/0,
2494 /*NumArgs=*/0,
2495 /*EllipsisLoc=*/NoLoc,
2496 /*RParenLoc=*/NoLoc,
2497 /*TypeQuals=*/0,
2498 /*RefQualifierIsLvalueRef=*/true,
2499 /*RefQualifierLoc=*/NoLoc,
2500 /*ConstQualifierLoc=*/NoLoc,
2501 /*VolatileQualifierLoc=*/NoLoc,
2502 /*MutableLoc=*/NoLoc,
2503 EST_None,
2504 /*ESpecLoc=*/NoLoc,
2505 /*Exceptions=*/0,
2506 /*ExceptionRanges=*/0,
2507 /*NumExceptions=*/0,
2508 /*NoexceptExpr=*/0,
2509 CaretLoc, CaretLoc,
2510 ParamInfo),
John McCall084e83d2011-03-24 11:26:52 +00002511 attrs, CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00002512
John McCall53fa7142010-12-24 02:08:15 +00002513 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002514
Mike Stump82f071f2009-02-04 22:31:32 +00002515 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002516 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Steve Naroff0ac012832008-08-28 19:20:44 +00002517 }
2518
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002519
John McCalldadc5752010-08-24 06:29:42 +00002520 ExprResult Result(true);
Chris Lattner9eac9312009-03-27 04:18:06 +00002521 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002522 // Saw something like: ^expr
2523 Diag(Tok, diag::err_expected_expression);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002524 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002525 return ExprError();
2526 }
Mike Stump11289f42009-09-09 15:08:12 +00002527
John McCalldadc5752010-08-24 06:29:42 +00002528 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002529 BlockScope.Exit();
Chris Lattner9eac9312009-03-27 04:18:06 +00002530 if (!Stmt.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002531 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9eac9312009-03-27 04:18:06 +00002532 else
Douglas Gregor0be31a22010-07-02 17:43:08 +00002533 Actions.ActOnBlockError(CaretLoc, getCurScope());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002534 return Result;
Steve Naroff0ac012832008-08-28 19:20:44 +00002535}
Ted Kremeneke65b0862012-03-06 20:05:56 +00002536
2537/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
2538///
2539/// '__objc_yes'
2540/// '__objc_no'
2541ExprResult Parser::ParseObjCBoolLiteral() {
2542 tok::TokenKind Kind = Tok.getKind();
2543 return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
2544}