blob: 7ace1f63d953f35dd64facfd0e105ae800683655 [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"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000025#include "RAIIObjectsForParser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/Basic/PrettyStackTrace.h"
27#include "clang/Sema/DeclSpec.h"
28#include "clang/Sema/ParsedTemplate.h"
29#include "clang/Sema/Scope.h"
30#include "clang/Sema/TypoCorrection.h"
Chris Lattner834618d2006-11-03 07:48:41 +000031#include "llvm/ADT/SmallString.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000032#include "llvm/ADT/SmallVector.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000033using namespace clang;
34
James Dennett3d5e4592012-06-17 04:36:28 +000035/// \brief Simple precedence-based parser for binary/ternary operators.
Chris Lattnercde626a2006-08-12 08:13:25 +000036///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000037/// Note: we diverge from the C99 grammar when parsing the assignment-expression
38/// production. C99 specifies that the LHS of an assignment operator should be
39/// parsed as a unary-expression, but consistency dictates that it be a
40/// conditional-expession. In practice, the important thing here is that the
41/// LHS of an assignment has to be an l-value, which productions between
42/// unary-expression and conditional-expression don't produce. Because we want
43/// consistency, we parse the LHS as a conditional-expression, then check for
44/// l-value-ness in semantic analysis stages.
45///
James Dennett3d5e4592012-06-17 04:36:28 +000046/// \verbatim
Sebastian Redl112a97662009-02-07 00:15:38 +000047/// pm-expression: [C++ 5.5]
48/// cast-expression
49/// pm-expression '.*' cast-expression
50/// pm-expression '->*' cast-expression
51///
Chris Lattnercde626a2006-08-12 08:13:25 +000052/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl112a97662009-02-07 00:15:38 +000053/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattnercde626a2006-08-12 08:13:25 +000054/// cast-expression
55/// multiplicative-expression '*' cast-expression
56/// multiplicative-expression '/' cast-expression
57/// multiplicative-expression '%' cast-expression
58///
59/// additive-expression: [C99 6.5.6]
60/// multiplicative-expression
61/// additive-expression '+' multiplicative-expression
62/// additive-expression '-' multiplicative-expression
63///
64/// shift-expression: [C99 6.5.7]
65/// additive-expression
66/// shift-expression '<<' additive-expression
67/// shift-expression '>>' additive-expression
68///
69/// relational-expression: [C99 6.5.8]
70/// shift-expression
71/// relational-expression '<' shift-expression
72/// relational-expression '>' shift-expression
73/// relational-expression '<=' shift-expression
74/// relational-expression '>=' shift-expression
75///
76/// equality-expression: [C99 6.5.9]
77/// relational-expression
78/// equality-expression '==' relational-expression
79/// equality-expression '!=' relational-expression
80///
81/// AND-expression: [C99 6.5.10]
82/// equality-expression
83/// AND-expression '&' equality-expression
84///
85/// exclusive-OR-expression: [C99 6.5.11]
86/// AND-expression
87/// exclusive-OR-expression '^' AND-expression
88///
89/// inclusive-OR-expression: [C99 6.5.12]
90/// exclusive-OR-expression
91/// inclusive-OR-expression '|' exclusive-OR-expression
92///
93/// logical-AND-expression: [C99 6.5.13]
94/// inclusive-OR-expression
95/// logical-AND-expression '&&' inclusive-OR-expression
96///
97/// logical-OR-expression: [C99 6.5.14]
98/// logical-AND-expression
99/// logical-OR-expression '||' logical-AND-expression
100///
101/// conditional-expression: [C99 6.5.15]
102/// logical-OR-expression
103/// logical-OR-expression '?' expression ':' conditional-expression
104/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl1a99f442009-04-16 17:51:27 +0000105/// [C++] the third operand is an assignment-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000106///
107/// assignment-expression: [C99 6.5.16]
108/// conditional-expression
109/// unary-expression assignment-operator assignment-expression
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000110/// [C++] throw-expression [C++ 15]
Chris Lattnercde626a2006-08-12 08:13:25 +0000111///
112/// assignment-operator: one of
113/// = *= /= %= += -= <<= >>= &= ^= |=
114///
115/// expression: [C99 6.5.17]
Douglas Gregor968f23a2011-01-03 19:31:53 +0000116/// assignment-expression ...[opt]
117/// expression ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +0000118/// \endverbatim
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000119ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
120 ExprResult LHS(ParseAssignmentExpression(isTypeCast));
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000121 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Chris Lattnercde626a2006-08-12 08:13:25 +0000122}
123
Mike Stump11289f42009-09-09 15:08:12 +0000124/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000125/// Current token is an Identifier and is not a 'try'. This
James Dennettf44874f2012-06-15 06:52:33 +0000126/// routine is necessary to disambiguate \@try-statement from,
127/// for example, \@encode-expression.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000128///
John McCalldadc5752010-08-24 06:29:42 +0000129ExprResult
Sebastian Redl90893182008-12-11 22:33:27 +0000130Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000131 ExprResult LHS(ParseObjCAtExpression(AtLoc));
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000132 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000133}
134
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000135/// This routine is called when a leading '__extension__' is seen and
136/// consumed. This is necessary because the token gets consumed in the
137/// process of disambiguating between an expression and a declaration.
John McCalldadc5752010-08-24 06:29:42 +0000138ExprResult
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000139Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000140 ExprResult LHS(true);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000141 {
142 // Silence extension warnings in the sub-expression
143 ExtensionRAIIObject O(Diags);
144
145 LHS = ParseCastExpression(false);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000146 }
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000147
Douglas Gregor29d907d2010-09-17 22:25:06 +0000148 if (!LHS.isInvalid())
149 LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
150 LHS.take());
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000151
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000152 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000153}
154
James Dennett3d5e4592012-06-17 04:36:28 +0000155/// \brief Parse an expr that doesn't include (top-level) commas.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000156ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000157 if (Tok.is(tok::code_completion)) {
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000158 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000159 cutOffParsing();
160 return ExprError();
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000161 }
162
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000163 if (Tok.is(tok::kw_throw))
Sebastian Redld65cea82008-12-11 22:51:44 +0000164 return ParseThrowExpression();
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000165
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000166 ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
167 /*isAddressOfOperand=*/false,
168 isTypeCast);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000169 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000170}
171
James Dennett3d5e4592012-06-17 04:36:28 +0000172/// \brief Parse an assignment expression where part of an Objective-C message
173/// send has already been parsed.
174///
175/// In this case \p LBracLoc indicates the location of the '[' of the message
176/// send, and either \p ReceiverName or \p ReceiverExpr is non-null indicating
177/// the receiver of the message.
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000178///
179/// Since this handles full assignment-expression's, it handles postfix
180/// expressions and other binary operators for these expressions as well.
John McCalldadc5752010-08-24 06:29:42 +0000181ExprResult
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000182Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000183 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +0000184 ParsedType ReceiverType,
John McCallb268a282010-08-23 23:25:46 +0000185 Expr *ReceiverExpr) {
John McCalldadc5752010-08-24 06:29:42 +0000186 ExprResult R
John McCallb268a282010-08-23 23:25:46 +0000187 = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
188 ReceiverType, ReceiverExpr);
Douglas Gregoreda7e542010-09-18 01:28:11 +0000189 R = ParsePostfixExpressionSuffix(R);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000190 return ParseRHSOfBinaryExpression(R, prec::Assignment);
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000191}
192
193
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000194ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
Richard Smith764d2fe2011-12-20 02:08:33 +0000195 // C++03 [basic.def.odr]p2:
Mike Stump11289f42009-09-09 15:08:12 +0000196 // An expression is potentially evaluated unless it appears where an
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000197 // integral constant expression is required (see 5.19) [...].
Richard Smith764d2fe2011-12-20 02:08:33 +0000198 // 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 +0000199 EnterExpressionEvaluationContext Unevaluated(Actions,
Richard Smith764d2fe2011-12-20 02:08:33 +0000200 Sema::ConstantEvaluated);
Mike Stump11289f42009-09-09 15:08:12 +0000201
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000202 ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
Eli Friedmanc6237c62012-02-29 03:16:56 +0000203 ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
204 return Actions.ActOnConstantExpression(Res);
Chris Lattner3b561a32006-08-13 00:12:11 +0000205}
206
Richard Smith0875c532012-09-18 00:52:05 +0000207bool Parser::isNotExpressionStart() {
208 tok::TokenKind K = Tok.getKind();
209 if (K == tok::l_brace || K == tok::r_brace ||
210 K == tok::kw_for || K == tok::kw_while ||
211 K == tok::kw_if || K == tok::kw_else ||
212 K == tok::kw_goto || K == tok::kw_try)
213 return true;
214 // If this is a decl-specifier, we can't be at the start of an expression.
215 return isKnownToBeDeclarationSpecifier();
216}
217
James Dennett3d5e4592012-06-17 04:36:28 +0000218/// \brief Parse a binary expression that starts with \p LHS and has a
219/// precedence of at least \p MinPrec.
John McCalldadc5752010-08-24 06:29:42 +0000220ExprResult
221Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000222 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
223 GreaterThanIsOperator,
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000224 getLangOpts().CPlusPlus11);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000225 SourceLocation ColonLoc;
226
Chris Lattnercde626a2006-08-12 08:13:25 +0000227 while (1) {
228 // If this token has a lower precedence than we are allowed to parse (e.g.
229 // because we are called recursively, or because the token is not a binop),
230 // then we are done!
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000231 if (NextTokPrec < MinPrec)
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000232 return LHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000233
234 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000235 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000236 ConsumeToken();
Sebastian Redl112a97662009-02-07 00:15:38 +0000237
Richard Smith0875c532012-09-18 00:52:05 +0000238 // Bail out when encountering a comma followed by a token which can't
239 // possibly be the start of an expression. For instance:
240 // int f() { return 1, }
241 // We can't do this before consuming the comma, because
242 // isNotExpressionStart() looks at the token stream.
243 if (OpToken.is(tok::comma) && isNotExpressionStart()) {
244 PP.EnterToken(Tok);
245 Tok = OpToken;
246 return LHS;
247 }
248
Chris Lattner96c3deb2006-08-12 17:13:08 +0000249 // Special case handling for the ternary operator.
John McCalldadc5752010-08-24 06:29:42 +0000250 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000251 if (NextTokPrec == prec::Conditional) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000252 if (Tok.isNot(tok::colon)) {
Chris Lattner244b96b2009-12-10 02:02:58 +0000253 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
254 ColonProtectionRAIIObject X(*this);
255
Chris Lattner96c3deb2006-08-12 17:13:08 +0000256 // Handle this production specially:
257 // logical-OR-expression '?' expression ':' conditional-expression
258 // In particular, the RHS of the '?' is 'expression', not
259 // 'logical-OR-expression' as we might expect.
260 TernaryMiddle = ParseExpression();
Douglas Gregorec06c122010-09-17 22:41:34 +0000261 if (TernaryMiddle.isInvalid()) {
262 LHS = ExprError();
263 TernaryMiddle = 0;
264 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000265 } else {
266 // Special case handling of "X ? Y : Z" where Y is empty:
267 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redlc13f2682008-12-09 20:22:58 +0000268 TernaryMiddle = 0;
Chris Lattner96c3deb2006-08-12 17:13:08 +0000269 Diag(Tok, diag::ext_gnu_conditional_expr);
270 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000271
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000272 if (!TryConsumeToken(tok::colon, ColonLoc)) {
Chandler Carruth0b5cf7c2011-07-26 05:19:46 +0000273 // Otherwise, we're missing a ':'. Assume that this was a typo that
274 // the user forgot. If we're not in a macro expansion, we can suggest
275 // a fixit hint. If there were two spaces before the current token,
Chris Lattnerfb585152010-05-24 22:31:37 +0000276 // suggest inserting the colon in between them, otherwise insert ": ".
277 SourceLocation FILoc = Tok.getLocation();
278 const char *FIText = ": ";
Argyrios Kyrtzidis3ea4adb2011-06-24 17:28:29 +0000279 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis1b07c342012-01-19 15:59:08 +0000280 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
281 assert(FILoc.isFileID());
Chris Lattnerfb585152010-05-24 22:31:37 +0000282 bool IsInvalid = false;
283 const char *SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000284 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000285 if (!IsInvalid && *SourcePtr == ' ') {
286 SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000287 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000288 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000289 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattnerfb585152010-05-24 22:31:37 +0000290 FIText = ":";
291 }
292 }
293 }
294
Ted Kremeneke6013652010-04-12 22:10:35 +0000295 Diag(Tok, diag::err_expected_colon)
Chris Lattnerfb585152010-05-24 22:31:37 +0000296 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner03c40412008-11-23 23:17:07 +0000297 Diag(OpToken, diag::note_matching) << "?";
Chris Lattner0151b7e2010-04-20 21:33:39 +0000298 ColonLoc = Tok.getLocation();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000299 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000300 }
Fariborz Jahanian9a14b842009-10-23 21:01:39 +0000301
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000302 // Code completion for the right-hand side of an assignment expression
303 // goes through a special hook that takes the left-hand side into account.
304 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000305 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000306 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000307 return ExprError();
308 }
309
Chris Lattner96c3deb2006-08-12 17:13:08 +0000310 // Parse another leaf here for the RHS of the operator.
Sebastian Redl1a99f442009-04-16 17:51:27 +0000311 // ParseCastExpression works here because all RHS expressions in C have it
312 // as a prefix, at least. However, in C++, an assignment-expression could
313 // be a throw-expression, which is not a valid cast-expression.
314 // Therefore we need some special-casing here.
315 // Also note that the third operand of the conditional operator is
Richard Smith9a6403a2012-02-26 23:40:27 +0000316 // an assignment-expression in C++, and in C++11, we can have a
Richard Smith5e0cac72012-03-01 02:59:17 +0000317 // braced-init-list on the RHS of an assignment. For better diagnostics,
318 // parse as if we were allowed braced-init-lists everywhere, and check that
319 // they only appear on the RHS of assignments later.
John McCalldadc5752010-08-24 06:29:42 +0000320 ExprResult RHS;
Richard Smithebcd2352012-03-01 07:10:06 +0000321 bool RHSIsInitList = false;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000322 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith9a6403a2012-02-26 23:40:27 +0000323 RHS = ParseBraceInitializer();
Richard Smithebcd2352012-03-01 07:10:06 +0000324 RHSIsInitList = true;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000325 } else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional)
Sebastian Redl1a99f442009-04-16 17:51:27 +0000326 RHS = ParseAssignmentExpression();
Richard Smith5e0cac72012-03-01 02:59:17 +0000327 else
Sebastian Redl1a99f442009-04-16 17:51:27 +0000328 RHS = ParseCastExpression(false);
Chris Lattnercde626a2006-08-12 08:13:25 +0000329
Douglas Gregor29d907d2010-09-17 22:25:06 +0000330 if (RHS.isInvalid())
331 LHS = ExprError();
332
Chris Lattnercde626a2006-08-12 08:13:25 +0000333 // Remember the precedence of this operator and get the precedence of the
334 // operator immediately to the right of the RHS.
Douglas Gregor8d4de672010-04-21 22:36:40 +0000335 prec::Level ThisPrec = NextTokPrec;
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000336 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000337 getLangOpts().CPlusPlus11);
Chris Lattner89d53752006-08-12 17:18:19 +0000338
339 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000340 bool isRightAssoc = ThisPrec == prec::Conditional ||
341 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000342
343 // Get the precedence of the operator to the right of the RHS. If it binds
344 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000345 if (ThisPrec < NextTokPrec ||
346 (ThisPrec == NextTokPrec && isRightAssoc)) {
Richard Smithebcd2352012-03-01 07:10:06 +0000347 if (!RHS.isInvalid() && RHSIsInitList) {
348 Diag(Tok, diag::err_init_list_bin_op)
349 << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get());
350 RHS = ExprError();
Richard Smith5e0cac72012-03-01 02:59:17 +0000351 }
Chris Lattner89d53752006-08-12 17:18:19 +0000352 // If this is left-associative, only parse things on the RHS that bind
353 // more tightly than the current operator. If it is left-associative, it
354 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
355 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl511ed552008-11-25 22:21:31 +0000356 // The function takes ownership of the RHS.
Douglas Gregor29d907d2010-09-17 22:25:06 +0000357 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor8d4de672010-04-21 22:36:40 +0000358 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Richard Smithebcd2352012-03-01 07:10:06 +0000359 RHSIsInitList = false;
Douglas Gregor29d907d2010-09-17 22:25:06 +0000360
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000361 if (RHS.isInvalid())
Douglas Gregor29d907d2010-09-17 22:25:06 +0000362 LHS = ExprError();
Chris Lattnercde626a2006-08-12 08:13:25 +0000363
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000364 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000365 getLangOpts().CPlusPlus11);
Chris Lattnercde626a2006-08-12 08:13:25 +0000366 }
367 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl511ed552008-11-25 22:21:31 +0000368
Richard Smithebcd2352012-03-01 07:10:06 +0000369 if (!RHS.isInvalid() && RHSIsInitList) {
Richard Smith5e0cac72012-03-01 02:59:17 +0000370 if (ThisPrec == prec::Assignment) {
371 Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
Richard Smithebcd2352012-03-01 07:10:06 +0000372 << Actions.getExprRange(RHS.get());
Richard Smith5e0cac72012-03-01 02:59:17 +0000373 } else {
374 Diag(OpToken, diag::err_init_list_bin_op)
Richard Smithebcd2352012-03-01 07:10:06 +0000375 << /*RHS*/1 << PP.getSpelling(OpToken)
376 << Actions.getExprRange(RHS.get());
Richard Smith5e0cac72012-03-01 02:59:17 +0000377 LHS = ExprError();
378 }
379 }
380
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000381 if (!LHS.isInvalid()) {
Chris Lattner319079c2007-08-31 05:01:50 +0000382 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor87f95b02009-02-26 21:00:50 +0000383 if (TernaryMiddle.isInvalid()) {
384 // If we're using '>>' as an operator within a template
385 // argument list (in C++98), suggest the addition of
386 // parentheses so that the code remains well-formed in C++0x.
387 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
388 SuggestParentheses(OpToken.getLocation(),
Craig Topper3195e252013-07-14 17:02:30 +0000389 diag::warn_cxx11_right_shift_in_template_arg,
Douglas Gregor87f95b02009-02-26 21:00:50 +0000390 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
391 Actions.getExprRange(RHS.get()).getEnd()));
392
Douglas Gregor0be31a22010-07-02 17:43:08 +0000393 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCallb268a282010-08-23 23:25:46 +0000394 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor87f95b02009-02-26 21:00:50 +0000395 } else
Steve Naroff83895f72007-09-16 03:34:24 +0000396 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000397 LHS.take(), TernaryMiddle.take(),
398 RHS.take());
Chris Lattner319079c2007-08-31 05:01:50 +0000399 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000400 }
401}
402
James Dennett3d5e4592012-06-17 04:36:28 +0000403/// \brief Parse a cast-expression, or, if \p isUnaryExpression is true,
404/// parse a unary-expression.
405///
406/// \p isAddressOfOperand exists because an id-expression that is the
407/// operand of address-of gets special treatment due to member pointers.
Chris Lattnereaf06592006-08-11 02:02:23 +0000408///
John McCalldadc5752010-08-24 06:29:42 +0000409ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000410 bool isAddressOfOperand,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000411 TypeCastState isTypeCast) {
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000412 bool NotCastExpr;
John McCalldadc5752010-08-24 06:29:42 +0000413 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor29d907d2010-09-17 22:25:06 +0000414 isAddressOfOperand,
415 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +0000416 isTypeCast);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000417 if (NotCastExpr)
418 Diag(Tok, diag::err_expected_expression);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000419 return Res;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000420}
421
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000422namespace {
423class CastExpressionIdValidator : public CorrectionCandidateCallback {
424 public:
425 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
426 : AllowNonTypes(AllowNonTypes) {
427 WantTypeSpecifiers = AllowTypes;
428 }
429
430 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
431 NamedDecl *ND = candidate.getCorrectionDecl();
432 if (!ND)
433 return candidate.isKeyword();
434
435 if (isa<TypeDecl>(ND))
436 return WantTypeSpecifiers;
437 return AllowNonTypes;
438 }
439
440 private:
441 bool AllowNonTypes;
442};
443}
444
James Dennett3d5e4592012-06-17 04:36:28 +0000445/// \brief Parse a cast-expression, or, if \pisUnaryExpression is true, parse
446/// a unary-expression.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000447///
James Dennett3d5e4592012-06-17 04:36:28 +0000448/// \p isAddressOfOperand exists because an id-expression that is the operand
449/// of address-of gets special treatment due to member pointers. NotCastExpr
450/// is set to true if the token is not the start of a cast-expression, and no
451/// diagnostic is emitted in this case.
452///
453/// \verbatim
Chris Lattner4564bc12006-08-10 23:14:52 +0000454/// cast-expression: [C99 6.5.4]
455/// unary-expression
456/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000457///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000458/// unary-expression: [C99 6.5.3]
459/// postfix-expression
460/// '++' unary-expression
461/// '--' unary-expression
462/// unary-operator cast-expression
463/// 'sizeof' unary-expression
464/// 'sizeof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000465/// [C++11] 'sizeof' '...' '(' identifier ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000466/// [GNU] '__alignof' unary-expression
467/// [GNU] '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +0000468/// [C11] '_Alignof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000469/// [C++11] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000470/// [GNU] '&&' identifier
Richard Smithd67aea22012-03-06 03:21:47 +0000471/// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7]
Sebastian Redlbd150f42008-11-21 19:14:01 +0000472/// [C++] new-expression
473/// [C++] delete-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000474///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000475/// unary-operator: one of
476/// '&' '*' '+' '-' '~' '!'
477/// [GNU] '__extension__' '__real' '__imag'
478///
Chris Lattner52a99e52006-08-10 20:56:00 +0000479/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000480/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000481/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000482/// constant
483/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000484/// [C++] boolean-literal [C++ 2.13.5]
Richard Smithd67aea22012-03-06 03:21:47 +0000485/// [C++11] 'nullptr' [C++11 2.14.7]
486/// [C++11] user-defined-literal
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000487/// '(' expression ')'
Benjamin Kramere56f3932011-12-23 17:00:35 +0000488/// [C11] generic-selection
Chris Lattner52a99e52006-08-10 20:56:00 +0000489/// '__func__' [C99 6.4.2.2]
490/// [GNU] '__FUNCTION__'
David Majnemerbed356a2013-11-06 23:31:56 +0000491/// [MS] '__FUNCDNAME__'
492/// [MS] 'L__FUNCTION__'
Chris Lattner52a99e52006-08-10 20:56:00 +0000493/// [GNU] '__PRETTY_FUNCTION__'
494/// [GNU] '(' compound-statement ')'
495/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
496/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
497/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
498/// assign-expr ')'
499/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000500/// [GNU] '__null'
Mike Stump11289f42009-09-09 15:08:12 +0000501/// [OBJC] '[' objc-message-expr ']'
James Dennettf44874f2012-06-15 06:52:33 +0000502/// [OBJC] '\@selector' '(' objc-selector-arg ')'
503/// [OBJC] '\@protocol' '(' identifier ')'
504/// [OBJC] '\@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000505/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000506/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000507/// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3]
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000508/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000509/// [C++11] typename-specifier braced-init-list [C++11 5.2.3]
Bill Wendlinga6930032007-06-29 18:21:34 +0000510/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
511/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
512/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
513/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000514/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
515/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000516/// [C++] 'this' [C++ 9.3.2]
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000517/// [G++] unary-type-trait '(' type-id ')'
518/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley6242b6a2011-04-28 00:16:57 +0000519/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff0ac012832008-08-28 19:20:44 +0000520/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000521///
522/// constant: [C99 6.4.4]
523/// integer-constant
524/// floating-constant
525/// enumeration-constant -> identifier
526/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000527///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000528/// id-expression: [C++ 5.1]
529/// unqualified-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000530/// qualified-id
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000531///
532/// unqualified-id: [C++ 5.1]
533/// identifier
534/// operator-function-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000535/// conversion-function-id
536/// '~' class-name
537/// template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000538///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000539/// new-expression: [C++ 5.3.4]
540/// '::'[opt] 'new' new-placement[opt] new-type-id
541/// new-initializer[opt]
542/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
543/// new-initializer[opt]
544///
545/// delete-expression: [C++ 5.3.5]
546/// '::'[opt] 'delete' cast-expression
547/// '::'[opt] 'delete' '[' ']' cast-expression
548///
John Wiegley65497cc2011-04-27 23:09:49 +0000549/// [GNU/Embarcadero] unary-type-trait:
550/// '__is_arithmetic'
551/// '__is_floating_point'
552/// '__is_integral'
553/// '__is_lvalue_expr'
554/// '__is_rvalue_expr'
555/// '__is_complete_type'
556/// '__is_void'
557/// '__is_array'
558/// '__is_function'
559/// '__is_reference'
560/// '__is_lvalue_reference'
561/// '__is_rvalue_reference'
562/// '__is_fundamental'
563/// '__is_object'
564/// '__is_scalar'
565/// '__is_compound'
566/// '__is_pointer'
567/// '__is_member_object_pointer'
568/// '__is_member_function_pointer'
569/// '__is_member_pointer'
570/// '__is_const'
571/// '__is_volatile'
572/// '__is_trivial'
573/// '__is_standard_layout'
574/// '__is_signed'
575/// '__is_unsigned'
576///
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000577/// [GNU] unary-type-trait:
Sebastian Redl7dcb1552010-08-31 04:59:00 +0000578/// '__has_nothrow_assign'
579/// '__has_nothrow_copy'
580/// '__has_nothrow_constructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000581/// '__has_trivial_assign' [TODO]
582/// '__has_trivial_copy' [TODO]
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000583/// '__has_trivial_constructor'
Anders Carlsson6dc35752009-04-17 02:34:54 +0000584/// '__has_trivial_destructor'
Sebastian Redlb469afb2010-09-02 23:19:42 +0000585/// '__has_virtual_destructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000586/// '__is_abstract' [TODO]
587/// '__is_class'
588/// '__is_empty' [TODO]
589/// '__is_enum'
Douglas Gregordca70af2011-12-03 18:14:24 +0000590/// '__is_final'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000591/// '__is_pod'
592/// '__is_polymorphic'
David Majnemera5433082013-10-18 00:33:31 +0000593/// '__is_sealed' [MS]
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000594/// '__is_trivial'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000595/// '__is_union'
596///
Alexis Huntd9a5cc12011-05-13 00:31:07 +0000597/// [Clang] unary-type-trait:
598/// '__trivially_copyable'
599///
Douglas Gregor8006e762011-01-27 20:28:01 +0000600/// binary-type-trait:
601/// [GNU] '__is_base_of'
602/// [MS] '__is_convertible_to'
John Wiegley65497cc2011-04-27 23:09:49 +0000603/// '__is_convertible'
604/// '__is_same'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000605///
John Wiegley6242b6a2011-04-28 00:16:57 +0000606/// [Embarcadero] array-type-trait:
607/// '__array_rank'
608/// '__array_extent'
609///
John Wiegleyf9f65842011-04-25 06:54:41 +0000610/// [Embarcadero] expression-trait:
611/// '__is_lvalue_expr'
612/// '__is_rvalue_expr'
James Dennett3d5e4592012-06-17 04:36:28 +0000613/// \endverbatim
John Wiegleyf9f65842011-04-25 06:54:41 +0000614///
John McCalldadc5752010-08-24 06:29:42 +0000615ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl22e3a932010-09-10 20:55:37 +0000616 bool isAddressOfOperand,
617 bool &NotCastExpr,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000618 TypeCastState isTypeCast) {
John McCalldadc5752010-08-24 06:29:42 +0000619 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000620 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000621 NotCastExpr = false;
Mike Stump11289f42009-09-09 15:08:12 +0000622
Chris Lattner81b576e2006-08-11 02:13:20 +0000623 // This handles all of cast-expression, unary-expression, postfix-expression,
624 // and primary-expression. We handle them together like this for efficiency
625 // and to simplify handling of an expression starting with a '(' token: which
626 // may be one of a parenthesized expression, cast-expression, compound literal
627 // expression, or statement expression.
628 //
629 // If the parsed tokens consist of a primary-expression, the cases below
John McCallb268a282010-08-23 23:25:46 +0000630 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
631 // to handle the postfix expression suffixes. Cases that cannot be followed
632 // by postfix exprs should return without invoking
633 // ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000634 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000635 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000636 // If this expression is limited to being a unary-expression, the parent can
637 // not start a cast expression.
638 ParenParseOption ParenExprType =
David Blaikiebbafb8a2012-03-11 07:00:24 +0000639 (isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallba7bf592010-08-24 05:47:05 +0000640 ParsedType CastTy;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000641 SourceLocation RParenLoc;
Chris Lattner3c674cf2009-12-10 02:08:07 +0000642
643 {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000644 // The inside of the parens don't need to be a colon protected scope, and
645 // isn't immediately a message send.
Chris Lattner3c674cf2009-12-10 02:08:07 +0000646 ColonProtectionRAIIObject X(*this, false);
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000647
Chris Lattner3c674cf2009-12-10 02:08:07 +0000648 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000649 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner3c674cf2009-12-10 02:08:07 +0000650 }
Mike Stump11289f42009-09-09 15:08:12 +0000651
Chris Lattner81b576e2006-08-11 02:13:20 +0000652 switch (ParenExprType) {
653 case SimpleExpr: break; // Nothing else to do.
654 case CompoundStmt: break; // Nothing else to do.
655 case CompoundLiteral:
656 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
657 // postfix-expression exist, parse them now.
658 break;
659 case CastExpr:
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000660 // We have parsed the cast-expression and no postfix-expr pieces are
661 // following.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000662 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000663 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000664
John McCallb268a282010-08-23 23:25:46 +0000665 break;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000666 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000667
Chris Lattner52a99e52006-08-10 20:56:00 +0000668 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000669 case tok::numeric_constant:
670 // constant: integer-constant
671 // constant: floating-constant
Sebastian Redl59b5e512008-12-11 21:36:32 +0000672
Richard Smithbcc22fc2012-03-09 08:00:36 +0000673 Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000674 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000675 break;
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000676
Bill Wendling4073ed52007-02-13 01:51:42 +0000677 case tok::kw_true:
678 case tok::kw_false:
Sebastian Redld65cea82008-12-11 22:51:44 +0000679 return ParseCXXBoolLiteral();
Ted Kremeneke65b0862012-03-06 20:05:56 +0000680
681 case tok::kw___objc_yes:
682 case tok::kw___objc_no:
683 return ParseObjCBoolLiteral();
Bill Wendling4073ed52007-02-13 01:51:42 +0000684
Sebastian Redl576fd422009-05-10 18:38:11 +0000685 case tok::kw_nullptr:
Richard Smithb15c11c2011-10-17 23:06:20 +0000686 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl576fd422009-05-10 18:38:11 +0000687 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
688
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000689 case tok::annot_primary_expr:
690 assert(Res.get() == 0 && "Stray primary-expression annotation?");
691 Res = getExprAnnotation(Tok);
692 ConsumeToken();
693 break;
Richard Smith74aeef52013-04-26 16:15:35 +0000694
David Blaikie15a430a2011-12-04 05:04:18 +0000695 case tok::kw_decltype:
Richard Smith74aeef52013-04-26 16:15:35 +0000696 // Annotate the token and tail recurse.
697 if (TryAnnotateTypeOrScopeToken())
698 return ExprError();
699 assert(Tok.isNot(tok::kw_decltype));
700 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
701
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000702 case tok::identifier: { // primary-expression: identifier
703 // unqualified-id: identifier
704 // constant: enumeration-constant
Chris Lattnera8a3f732009-01-06 05:06:21 +0000705 // Turn a potentially qualified name into a annot_typename or
Chris Lattner122db262009-01-04 22:52:14 +0000706 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000707 if (getLangOpts().CPlusPlus) {
John McCall64fe2332010-01-07 19:29:58 +0000708 // Avoid the unnecessary parse-time lookup in the common case
709 // where the syntax forbids a type.
710 const Token &Next = NextToken();
Douglas Gregor8bea83a2012-08-30 20:04:43 +0000711
712 // If this identifier was reverted from a token ID, and the next token
713 // is a parenthesis, this is likely to be a use of a type trait. Check
714 // those tokens.
Alp Toker53358e42013-12-17 14:12:30 +0000715 if (Next.is(tok::l_paren) && Tok.is(tok::identifier) &&
716 Tok.getIdentifierInfo()->hasRevertedTokenIDToIdentifier() &&
717 TryIdentKeywordUpgrade())
718 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
719 NotCastExpr, isTypeCast);
Richard Smithb3d6c052013-08-12 02:53:18 +0000720
John McCall64fe2332010-01-07 19:29:58 +0000721 if (Next.is(tok::coloncolon) ||
722 (!ColonIsSacred && Next.is(tok::colon)) ||
723 Next.is(tok::less) ||
Sebastian Redl867f2282011-12-22 18:58:29 +0000724 Next.is(tok::l_paren) ||
725 Next.is(tok::l_brace)) {
John McCall64fe2332010-01-07 19:29:58 +0000726 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
727 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +0000728 return ExprError();
729 if (!Tok.is(tok::identifier))
John McCall64fe2332010-01-07 19:29:58 +0000730 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
731 }
Chris Lattner9a8968b2009-01-04 23:23:14 +0000732 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000733
Chris Lattner55662902009-10-25 17:04:48 +0000734 // Consume the identifier so that we can see if it is followed by a '(' or
735 // '.'.
736 IdentifierInfo &II = *Tok.getIdentifierInfo();
737 SourceLocation ILoc = ConsumeToken();
Douglas Gregor8bea83a2012-08-30 20:04:43 +0000738
Chris Lattnera36ec422010-04-11 08:28:14 +0000739 // Support 'Class.property' and 'super.property' notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000740 if (getLangOpts().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000741 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattnercd963182010-04-12 06:20:33 +0000742 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000743 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000744 ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000745
Douglas Gregor36107ad2012-02-16 18:19:22 +0000746 // Allow either an identifier or the keyword 'class' (in C++).
747 if (Tok.isNot(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000748 !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattner55662902009-10-25 17:04:48 +0000749 Diag(Tok, diag::err_expected_property_name);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000750 return ExprError();
751 }
752 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
753 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000754
755 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
756 ILoc, PropertyLoc);
John McCallb268a282010-08-23 23:25:46 +0000757 break;
Steve Naroff9527bbf2009-03-09 21:12:44 +0000758 }
John McCall8d08b9b2010-08-27 09:08:28 +0000759
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000760 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregored0b69d2010-09-15 16:23:04 +0000761 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000762 // that identifier, this is probably a message send with a missing open
Douglas Gregored0b69d2010-09-15 16:23:04 +0000763 // bracket. Treat it as such.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000764 if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000765 getCurScope()->isInObjcMethodScope() &&
Douglas Gregored0b69d2010-09-15 16:23:04 +0000766 ((Tok.is(tok::identifier) &&
767 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
768 Tok.is(tok::code_completion))) {
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000769 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
770 0);
771 break;
772 }
773
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000774 // If we have an Objective-C class name followed by an identifier
775 // and either ':' or ']', this is an Objective-C class message
776 // send that's missing the opening '['. Recovery
777 // appropriately. Also take this path if we're performing code
778 // completion after an Objective-C class name.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000779 if (getLangOpts().ObjC1 &&
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000780 ((Tok.is(tok::identifier) && !InMessageExpression) ||
781 Tok.is(tok::code_completion))) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000782 const Token& Next = NextToken();
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000783 if (Tok.is(tok::code_completion) ||
784 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif433c9e12010-09-17 10:21:45 +0000785 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
786 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000787 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000788 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000789 DS.SetRangeStart(ILoc);
790 DS.SetRangeEnd(ILoc);
791 const char *PrevSpec = 0;
792 unsigned DiagID;
Gabor Greif433c9e12010-09-17 10:21:45 +0000793 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000794
795 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
796 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
797 DeclaratorInfo);
798 if (Ty.isInvalid())
799 break;
800
801 Res = ParseObjCMessageExpressionBody(SourceLocation(),
802 SourceLocation(),
803 Ty.get(), 0);
804 break;
805 }
806 }
807
John McCall8d08b9b2010-08-27 09:08:28 +0000808 // Make sure to pass down the right value for isAddressOfOperand.
809 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
810 isAddressOfOperand = false;
Chris Lattner55662902009-10-25 17:04:48 +0000811
Chris Lattnerac18be92006-11-20 06:49:47 +0000812 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
813 // need to know whether or not this identifier is a function designator or
814 // not.
Douglas Gregora121b752009-11-03 16:56:39 +0000815 UnqualifiedId Name;
816 CXXScopeSpec ScopeSpec;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000817 SourceLocation TemplateKWLoc;
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000818 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
819 isTypeCast != IsTypeCast);
Douglas Gregora121b752009-11-03 16:56:39 +0000820 Name.setIdentifier(&II, ILoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000821 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
822 Name, Tok.is(tok::l_paren),
823 isAddressOfOperand, &Validator);
John McCallb268a282010-08-23 23:25:46 +0000824 break;
Chris Lattnerac18be92006-11-20 06:49:47 +0000825 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000826 case tok::char_constant: // constant: character-constant
Douglas Gregorfb65e592011-07-27 05:40:30 +0000827 case tok::wide_char_constant:
828 case tok::utf16_char_constant:
829 case tok::utf32_char_constant:
Richard Smithbcc22fc2012-03-09 08:00:36 +0000830 Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope());
Steve Naroffae4143e2007-04-26 20:39:23 +0000831 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000832 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000833 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
834 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
David Majnemerbed356a2013-11-06 23:31:56 +0000835 case tok::kw___FUNCDNAME__: // primary-expression: __FUNCDNAME__ [MS]
Nico Weber3a691a32012-06-23 02:07:59 +0000836 case tok::kw_L__FUNCTION__: // primary-expression: L__FUNCTION__ [MS]
Chris Lattner52a99e52006-08-10 20:56:00 +0000837 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000838 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000839 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000840 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000841 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000842 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000843 case tok::utf8_string_literal:
844 case tok::utf16_string_literal:
845 case tok::utf32_string_literal:
Richard Smithd67aea22012-03-06 03:21:47 +0000846 Res = ParseStringLiteralExpression(true);
John McCallb268a282010-08-23 23:25:46 +0000847 break;
Benjamin Kramere56f3932011-12-23 17:00:35 +0000848 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbourne91147592011-04-15 00:35:48 +0000849 Res = ParseGenericSelectionExpression();
850 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000851 case tok::kw___builtin_va_arg:
852 case tok::kw___builtin_offsetof:
853 case tok::kw___builtin_choose_expr:
Tanya Lattner55808c12011-06-04 00:47:47 +0000854 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Hal Finkelc4d7c822013-09-18 03:29:45 +0000855 case tok::kw___builtin_convertvector:
Sebastian Redl90893182008-12-11 22:33:27 +0000856 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000857 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000858 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth904cb142011-07-08 04:59:44 +0000859
Douglas Gregora49ccfe2010-08-06 14:50:36 +0000860 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
861 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
862 // C++ [expr.unary] has:
863 // unary-expression:
864 // ++ cast-expression
865 // -- cast-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000866 SourceLocation SavedLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000867 Res = ParseCastExpression(!getLangOpts().CPlusPlus);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000868 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000869 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000870 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000871 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000872 case tok::amp: { // unary-expression: '&' cast-expression
873 // Special treatment because of member pointers
874 SourceLocation SavedLoc = ConsumeToken();
875 Res = ParseCastExpression(false, true);
876 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000877 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000878 return Res;
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000879 }
880
Chris Lattner81b576e2006-08-11 02:13:20 +0000881 case tok::star: // unary-expression: '*' cast-expression
882 case tok::plus: // unary-expression: '+' cast-expression
883 case tok::minus: // unary-expression: '-' cast-expression
884 case tok::tilde: // unary-expression: '~' cast-expression
885 case tok::exclaim: // unary-expression: '!' cast-expression
886 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000887 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000888 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000889 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000890 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000891 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000892 return Res;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000893 }
894
Chris Lattnerc43926f2008-02-02 20:20:10 +0000895 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
896 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000897 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000898 SourceLocation SavedLoc = ConsumeToken();
899 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000900 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000901 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000902 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000903 }
Jordan Rose58d54722012-06-30 21:33:57 +0000904 case tok::kw__Alignof: // unary-expression: '_Alignof' '(' type-name ')'
905 if (!getLangOpts().C11)
906 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
907 // fallthrough
908 case tok::kw_alignof: // unary-expression: 'alignof' '(' type-id ')'
Chris Lattner81b576e2006-08-11 02:13:20 +0000909 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
910 // unary-expression: '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +0000911 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
912 // unary-expression: 'sizeof' '(' type-name ')'
Peter Collingbournee190dee2011-03-11 19:24:49 +0000913 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
914 return ParseUnaryExprOrTypeTraitExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000915 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000916 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000917 if (Tok.isNot(tok::identifier))
918 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000919
Chris Lattner9ba479b2011-02-18 21:16:39 +0000920 if (getCurScope()->getFnParent() == 0)
921 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
922
Chris Lattnereefa10e2007-05-28 06:56:27 +0000923 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000924 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
925 Tok.getLocation());
926 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Chris Lattner14a1b642006-10-15 22:33:58 +0000927 ConsumeToken();
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000928 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000929 }
Chris Lattner29375652006-12-04 18:06:35 +0000930 case tok::kw_const_cast:
931 case tok::kw_dynamic_cast:
932 case tok::kw_reinterpret_cast:
933 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000934 Res = ParseCXXCasts();
John McCallb268a282010-08-23 23:25:46 +0000935 break;
Sebastian Redlc4704762008-11-11 11:37:55 +0000936 case tok::kw_typeid:
937 Res = ParseCXXTypeid();
John McCallb268a282010-08-23 23:25:46 +0000938 break;
Francois Pichet9f4f2072010-09-08 12:20:18 +0000939 case tok::kw___uuidof:
940 Res = ParseCXXUuidof();
941 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000942 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000943 Res = ParseCXXThis();
John McCallb268a282010-08-23 23:25:46 +0000944 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000945
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000946 case tok::annot_typename:
947 if (isStartOfObjCClassMessageMissingOpenBracket()) {
948 ParsedType Type = getTypeAnnotation(Tok);
Alexander Kornienkob98f6e52013-02-01 18:28:04 +0000949
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000950 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000951 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000952 DS.SetRangeStart(Tok.getLocation());
953 DS.SetRangeEnd(Tok.getLastLoc());
954
955 const char *PrevSpec = 0;
956 unsigned DiagID;
Nico Weber77430342010-11-22 10:30:56 +0000957 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
958 PrevSpec, DiagID, Type);
Alexander Kornienkob98f6e52013-02-01 18:28:04 +0000959
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000960 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
961 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
962 if (Ty.isInvalid())
963 break;
964
965 ConsumeToken();
966 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
967 Ty.get(), 0);
968 break;
969 }
970 // Fall through
Alexander Kornienkob98f6e52013-02-01 18:28:04 +0000971
David Blaikie25896afb2012-01-24 05:47:35 +0000972 case tok::annot_decltype:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000973 case tok::kw_char:
974 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000975 case tok::kw_char16_t:
976 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000977 case tok::kw_bool:
978 case tok::kw_short:
979 case tok::kw_int:
980 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +0000981 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000982 case tok::kw___int128:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000983 case tok::kw_signed:
984 case tok::kw_unsigned:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000985 case tok::kw_half:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000986 case tok::kw_float:
987 case tok::kw_double:
988 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +0000989 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +0000990 case tok::kw_typeof:
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000991 case tok::kw___vector:
992 case tok::kw_image1d_t:
993 case tok::kw_image1d_array_t:
994 case tok::kw_image1d_buffer_t:
995 case tok::kw_image2d_t:
996 case tok::kw_image2d_array_t:
Alexander Kornienkob98f6e52013-02-01 18:28:04 +0000997 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +0000998 case tok::kw_sampler_t:
Alexander Kornienkob98f6e52013-02-01 18:28:04 +0000999 case tok::kw_event_t: {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001000 if (!getLangOpts().CPlusPlus) {
Chris Lattner8a38aa82009-01-04 22:28:21 +00001001 Diag(Tok, diag::err_expected_expression);
1002 return ExprError();
1003 }
Eli Friedman6d692cc2009-06-11 00:33:41 +00001004
1005 if (SavedKind == tok::kw_typename) {
1006 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001007 // typename-specifier braced-init-list
John McCall1f476a12010-02-26 08:45:28 +00001008 if (TryAnnotateTypeOrScopeToken())
Eli Friedman6d692cc2009-06-11 00:33:41 +00001009 return ExprError();
David Majnemer338a7702013-09-22 03:30:01 +00001010
1011 if (!Actions.isSimpleTypeSpecifier(Tok.getKind()))
1012 // We are trying to parse a simple-type-specifier but might not get such
1013 // a token after error recovery.
1014 return ExprError();
Eli Friedman6d692cc2009-06-11 00:33:41 +00001015 }
1016
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001017 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001018 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001019 //
John McCall084e83d2011-03-24 11:26:52 +00001020 DeclSpec DS(AttrFactory);
David Majnemera5e92552013-09-22 01:24:26 +00001021
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001022 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redl3da34892011-06-05 12:23:16 +00001023 if (Tok.isNot(tok::l_paren) &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001024 (!getLangOpts().CPlusPlus11 || Tok.isNot(tok::l_brace)))
Sebastian Redl59b5e512008-12-11 21:36:32 +00001025 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1026 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001027
Richard Smith5d164bc2011-10-15 05:09:34 +00001028 if (Tok.is(tok::l_brace))
1029 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1030
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001031 Res = ParseCXXTypeConstructExpression(DS);
John McCallb268a282010-08-23 23:25:46 +00001032 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001033 }
1034
Douglas Gregor7df89f52010-02-05 19:11:37 +00001035 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001036 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1037 // (We can end up in this situation after tentative parsing.)
1038 if (TryAnnotateTypeOrScopeToken())
1039 return ExprError();
1040 if (!Tok.is(tok::annot_cxxscope))
1041 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001042 NotCastExpr, isTypeCast);
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001043
Douglas Gregor7df89f52010-02-05 19:11:37 +00001044 Token Next = NextToken();
1045 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001046 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001047 if (TemplateId->Kind == TNK_Type_template) {
1048 // We have a qualified template-id that we know refers to a
1049 // type, translate it into a type and continue parsing as a
1050 // cast expression.
1051 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001052 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1053 /*EnteringContext=*/false);
Douglas Gregore7c20652011-03-02 00:47:37 +00001054 AnnotateTemplateIdTokenAsType();
Douglas Gregor7df89f52010-02-05 19:11:37 +00001055 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001056 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001057 }
1058 }
1059
1060 // Parse as an id-expression.
1061 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001062 break;
Douglas Gregor7df89f52010-02-05 19:11:37 +00001063 }
1064
1065 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001066 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001067 if (TemplateId->Kind == TNK_Type_template) {
1068 // We have a template-id that we know refers to a type,
1069 // translate it into a type and continue parsing as a cast
1070 // expression.
1071 AnnotateTemplateIdTokenAsType();
1072 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001073 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001074 }
1075
1076 // Fall through to treat the template-id as an id-expression.
1077 }
1078
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001079 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001080 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001081 break;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001082
Chris Lattner122db262009-01-04 22:52:14 +00001083 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001084 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1085 // annotates the token, tail recurse.
1086 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001087 return ExprError();
1088 if (!Tok.is(tok::coloncolon))
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001089 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1090
Chris Lattner122db262009-01-04 22:52:14 +00001091 // ::new -> [C++] new-expression
1092 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001093 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +00001094 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001095 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +00001096 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001097 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001098
Chris Lattner9a8968b2009-01-04 23:23:14 +00001099 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001100 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +00001101 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +00001102 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +00001103
Sebastian Redlbd150f42008-11-21 19:14:01 +00001104 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001105 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001106
1107 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001108 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001109
Sebastian Redl22e3a932010-09-10 20:55:37 +00001110 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smithb15c11c2011-10-17 23:06:20 +00001111 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001112 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001113 BalancedDelimiterTracker T(*this, tok::l_paren);
1114
1115 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl22e3a932010-09-10 20:55:37 +00001116 return ExprError();
Richard Smith764d2fe2011-12-20 02:08:33 +00001117 // C++11 [expr.unary.noexcept]p1:
Sebastian Redle56be2f72010-09-10 21:57:27 +00001118 // The noexcept operator determines whether the evaluation of its operand,
1119 // which is an unevaluated operand, can throw an exception.
1120 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001121 ExprResult Result = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001122
1123 T.consumeClose();
1124
Sebastian Redl22e3a932010-09-10 20:55:37 +00001125 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001126 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1127 Result.take(), T.getCloseLocation());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001128 return Result;
Sebastian Redl22e3a932010-09-10 20:55:37 +00001129 }
1130
Alp Toker40f9b1c2013-12-12 21:23:03 +00001131#define TYPE_TRAIT(N,Spelling,K) \
1132 case tok::kw_##Spelling:
1133#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00001134 return ParseTypeTrait();
1135
John Wiegley6242b6a2011-04-28 00:16:57 +00001136 case tok::kw___array_rank:
1137 case tok::kw___array_extent:
1138 return ParseArrayTypeTrait();
1139
John Wiegleyf9f65842011-04-25 06:54:41 +00001140 case tok::kw___is_lvalue_expr:
1141 case tok::kw___is_rvalue_expr:
1142 return ParseExpressionTrait();
1143
Chris Lattner644e1b72007-10-03 22:03:06 +00001144 case tok::at: {
1145 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001146 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +00001147 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001148 case tok::caret:
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001149 Res = ParseBlockLiteralExpression();
1150 break;
1151 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001152 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001153 cutOffParsing();
1154 return ExprError();
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001155 }
Chris Lattner6bf1db12008-12-12 19:20:14 +00001156 case tok::l_square:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001157 if (getLangOpts().CPlusPlus11) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001158 if (getLangOpts().ObjC1) {
Eli Friedmanc7c97142012-01-04 02:40:39 +00001159 // C++11 lambda expressions and Objective-C message sends both start with a
1160 // square bracket. There are three possibilities here:
1161 // we have a valid lambda expression, we have an invalid lambda
1162 // expression, or we have something that doesn't appear to be a lambda.
1163 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001164 Res = TryParseLambdaExpression();
Eli Friedmanc7c97142012-01-04 02:40:39 +00001165 if (!Res.isInvalid() && !Res.get())
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001166 Res = ParseObjCMessageExpression();
1167 break;
1168 }
1169 Res = ParseLambdaExpression();
1170 break;
1171 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001172 if (getLangOpts().ObjC1) {
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001173 Res = ParseObjCMessageExpression();
1174 break;
1175 }
1176 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +00001177 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001178 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001179 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +00001180 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001181
John McCallb268a282010-08-23 23:25:46 +00001182 // These can be followed by postfix-expr pieces.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001183 return ParsePostfixExpressionSuffix(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +00001184}
1185
James Dennett3d5e4592012-06-17 04:36:28 +00001186/// \brief Once the leading part of a postfix-expression is parsed, this
1187/// method parses any suffixes that apply.
Chris Lattner20c6a452006-08-12 17:40:43 +00001188///
James Dennett3d5e4592012-06-17 04:36:28 +00001189/// \verbatim
Chris Lattner20c6a452006-08-12 17:40:43 +00001190/// postfix-expression: [C99 6.5.2]
1191/// primary-expression
1192/// postfix-expression '[' expression ']'
Sebastian Redl3da34892011-06-05 12:23:16 +00001193/// postfix-expression '[' braced-init-list ']'
Chris Lattner20c6a452006-08-12 17:40:43 +00001194/// postfix-expression '(' argument-expression-list[opt] ')'
1195/// postfix-expression '.' identifier
1196/// postfix-expression '->' identifier
1197/// postfix-expression '++'
1198/// postfix-expression '--'
1199/// '(' type-name ')' '{' initializer-list '}'
1200/// '(' type-name ')' '{' initializer-list ',' '}'
1201///
1202/// argument-expression-list: [C99 6.5.2]
Douglas Gregor968f23a2011-01-03 19:31:53 +00001203/// argument-expression ...[opt]
1204/// argument-expression-list ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +00001205/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001206ExprResult
1207Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001208 // Now that the primary-expression piece of the postfix-expression has been
1209 // parsed, see if there are any postfix-expression pieces here.
1210 SourceLocation Loc;
1211 while (1) {
1212 switch (Tok.getKind()) {
Douglas Gregored0b69d2010-09-15 16:23:04 +00001213 case tok::code_completion:
1214 if (InMessageExpression)
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001215 return LHS;
Douglas Gregored0b69d2010-09-15 16:23:04 +00001216
Douglas Gregoreda7e542010-09-18 01:28:11 +00001217 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001218 cutOffParsing();
1219 return ExprError();
Douglas Gregored0b69d2010-09-15 16:23:04 +00001220
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001221 case tok::identifier:
1222 // If we see identifier: after an expression, and we're not already in a
1223 // message send, then this is probably a message send with a missing
1224 // opening bracket '['.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001225 if (getLangOpts().ObjC1 && !InMessageExpression &&
Douglas Gregord6d98002010-09-15 14:54:45 +00001226 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001227 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1228 ParsedType(), LHS.get());
1229 break;
1230 }
1231
1232 // Fall through; this isn't a message send.
1233
Chris Lattner20c6a452006-08-12 17:40:43 +00001234 default: // Not a postfix-expression suffix.
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001235 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001236 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner47054fb2010-05-31 18:18:22 +00001237 // If we have a array postfix expression that starts on a new line and
1238 // Objective-C is enabled, it is highly likely that the user forgot a
1239 // semicolon after the base expression and that the array postfix-expr is
1240 // actually another message send. In this case, do some look-ahead to see
1241 // if the contents of the square brackets are obviously not a valid
1242 // expression and recover by pretending there is no suffix.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001243 if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
Chris Lattner47054fb2010-05-31 18:18:22 +00001244 isSimpleObjCMessageExpression())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001245 return LHS;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001246
1247 // Reject array indices starting with a lambda-expression. '[[' is
1248 // reserved for attributes.
1249 if (CheckProhibitedCXX11Attribute())
1250 return ExprError();
1251
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001252 BalancedDelimiterTracker T(*this, tok::l_square);
1253 T.consumeOpen();
1254 Loc = T.getOpenLocation();
Sebastian Redl3da34892011-06-05 12:23:16 +00001255 ExprResult Idx;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001256 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001257 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00001258 Idx = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001259 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00001260 Idx = ParseExpression();
Sebastian Redl511ed552008-11-25 22:21:31 +00001261
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001262 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001263
1264 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCallb268a282010-08-23 23:25:46 +00001265 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1266 Idx.take(), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001267 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +00001268 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001269
Chris Lattner89c50c62006-08-11 06:41:18 +00001270 // Match the ']'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001271 T.consumeClose();
Chris Lattner89c50c62006-08-11 06:41:18 +00001272 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001273 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001274
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001275 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1276 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1277 // '(' argument-expression-list[opt] ')'
1278 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001279 InMessageExpressionRAIIObject InMessage(*this, false);
1280
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001281 Expr *ExecConfig = 0;
1282
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001283 BalancedDelimiterTracker PT(*this, tok::l_paren);
1284
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001285 if (OpKind == tok::lesslessless) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00001286 ExprVector ExecConfigExprs;
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001287 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001288 SourceLocation OpenLoc = ConsumeToken();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001289
Eli Friedman7a15c4a2013-08-13 23:38:34 +00001290 if (ParseSimpleExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001291 LHS = ExprError();
1292 }
1293
Alp Tokera3ebe6e2013-12-17 14:12:37 +00001294 SourceLocation CloseLoc;
1295 if (TryConsumeToken(tok::greatergreatergreater, CloseLoc)) {
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001296 } else if (LHS.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001297 SkipUntil(tok::greatergreatergreater, StopAtSemi);
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001298 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001299 // There was an error closing the brackets
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001300 Diag(Tok, diag::err_expected_ggg);
1301 Diag(OpenLoc, diag::note_matching) << "<<<";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001302 SkipUntil(tok::greatergreatergreater, StopAtSemi);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001303 LHS = ExprError();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001304 }
1305
1306 if (!LHS.isInvalid()) {
1307 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1308 LHS = ExprError();
1309 else
1310 Loc = PrevTokLocation;
1311 }
1312
1313 if (!LHS.isInvalid()) {
1314 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001315 OpenLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001316 ExecConfigExprs,
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001317 CloseLoc);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001318 if (ECResult.isInvalid())
1319 LHS = ExprError();
1320 else
1321 ExecConfig = ECResult.get();
1322 }
1323 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001324 PT.consumeOpen();
1325 Loc = PT.getOpenLocation();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001326 }
1327
Benjamin Kramerf0623432012-08-23 22:51:59 +00001328 ExprVector ArgExprs;
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001329 CommaLocsTy CommaLocs;
Douglas Gregoreda7e542010-09-18 01:28:11 +00001330
Douglas Gregorcabea402009-09-22 15:41:20 +00001331 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001332 Actions.CodeCompleteCall(getCurScope(), LHS.get(), None);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001333 cutOffParsing();
1334 return ExprError();
Douglas Gregorcabea402009-09-22 15:41:20 +00001335 }
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001336
1337 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1338 if (Tok.isNot(tok::r_paren)) {
1339 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1340 LHS.get())) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001341 LHS = ExprError();
1342 }
Chris Lattner0c6c0342006-08-12 18:12:45 +00001343 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001344 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001345
Chris Lattner89c50c62006-08-11 06:41:18 +00001346 // Match the ')'.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001347 if (LHS.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001348 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregoreda7e542010-09-18 01:28:11 +00001349 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001350 PT.consumeClose();
Douglas Gregoreda7e542010-09-18 01:28:11 +00001351 LHS = ExprError();
1352 } else {
1353 assert((ArgExprs.size() == 0 ||
1354 ArgExprs.size()-1 == CommaLocs.size())&&
Chris Lattnere165d942006-08-24 04:40:38 +00001355 "Unexpected number of commas!");
John McCallb268a282010-08-23 23:25:46 +00001356 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001357 ArgExprs, Tok.getLocation(),
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001358 ExecConfig);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001359 PT.consumeClose();
Chris Lattnere165d942006-08-24 04:40:38 +00001360 }
Mike Stump11289f42009-09-09 15:08:12 +00001361
Chris Lattner89c50c62006-08-11 06:41:18 +00001362 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001363 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001364 case tok::arrow:
1365 case tok::period: {
1366 // postfix-expression: p-e '->' template[opt] id-expression
1367 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001368 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001369 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001370
Douglas Gregord8061562009-08-06 03:17:00 +00001371 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001372 ParsedType ObjectType;
Douglas Gregore610ada2010-02-24 18:44:31 +00001373 bool MayBePseudoDestructor = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001374 if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
Kaelyn Uhrain638264e2013-07-12 21:43:02 +00001375 Expr *Base = LHS.take();
1376 const Type* BaseType = Base->getType().getTypePtrOrNull();
1377 if (BaseType && Tok.is(tok::l_paren) &&
1378 (BaseType->isFunctionType() ||
Benjamin Kramer5fc787f2013-10-10 12:24:40 +00001379 BaseType->isSpecificPlaceholderType(BuiltinType::BoundMember))) {
Kaelyn Uhrain638264e2013-07-12 21:43:02 +00001380 Diag(OpLoc, diag::err_function_is_not_record)
1381 << (OpKind == tok::arrow) << Base->getSourceRange()
1382 << FixItHint::CreateRemoval(OpLoc);
1383 return ParsePostfixExpressionSuffix(Base);
1384 }
1385
1386 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), Base,
Douglas Gregore610ada2010-02-24 18:44:31 +00001387 OpLoc, OpKind, ObjectType,
1388 MayBePseudoDestructor);
Douglas Gregord8061562009-08-06 03:17:00 +00001389 if (LHS.isInvalid())
1390 break;
Douglas Gregore610ada2010-02-24 18:44:31 +00001391
Douglas Gregordf593fb2011-11-07 17:33:42 +00001392 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1393 /*EnteringContext=*/false,
Douglas Gregore610ada2010-02-24 18:44:31 +00001394 &MayBePseudoDestructor);
Douglas Gregor205a3612010-05-27 15:25:59 +00001395 if (SS.isNotEmpty())
John McCallba7bf592010-08-24 05:47:05 +00001396 ObjectType = ParsedType();
Douglas Gregord8061562009-08-06 03:17:00 +00001397 }
1398
Douglas Gregor2436e712009-09-17 21:32:03 +00001399 if (Tok.is(tok::code_completion)) {
1400 // Code completion for a member access expression.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001401 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor2436e712009-09-17 21:32:03 +00001402 OpLoc, OpKind == tok::arrow);
1403
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001404 cutOffParsing();
1405 return ExprError();
Douglas Gregor2436e712009-09-17 21:32:03 +00001406 }
1407
John McCallb268a282010-08-23 23:25:46 +00001408 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1409 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregore610ada2010-02-24 18:44:31 +00001410 ObjectType);
1411 break;
1412 }
1413
1414 // Either the action has told is that this cannot be a
1415 // pseudo-destructor expression (based on the type of base
1416 // expression), or we didn't see a '~' in the right place. We
1417 // can still parse a destructor name here, but in that case it
1418 // names a real destructor.
Francois Pichet64225792011-01-18 05:04:39 +00001419 // Allow explicit constructor calls in Microsoft mode.
1420 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001421 SourceLocation TemplateKWLoc;
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001422 UnqualifiedId Name;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001423 if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
Douglas Gregor36107ad2012-02-16 18:19:22 +00001424 // Objective-C++:
1425 // After a '.' in a member access expression, treat the keyword
1426 // 'class' as if it were an identifier.
1427 //
1428 // This hack allows property access to the 'class' method because it is
1429 // such a common method name. For other C++ keywords that are
1430 // Objective-C method names, one must use the message send syntax.
1431 IdentifierInfo *Id = Tok.getIdentifierInfo();
1432 SourceLocation Loc = ConsumeToken();
1433 Name.setIdentifier(Id, Loc);
1434 } else if (ParseUnqualifiedId(SS,
1435 /*EnteringContext=*/false,
1436 /*AllowDestructorName=*/true,
1437 /*AllowConstructorName=*/
David Blaikiebbafb8a2012-03-11 07:00:24 +00001438 getLangOpts().MicrosoftExt,
Douglas Gregor36107ad2012-02-16 18:19:22 +00001439 ObjectType, TemplateKWLoc, Name))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001440 LHS = ExprError();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001441
1442 if (!LHS.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001443 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001444 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001445 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1446 Tok.is(tok::l_paren));
Chris Lattner89c50c62006-08-11 06:41:18 +00001447 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001448 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001449 case tok::plusplus: // postfix-expression: postfix-expression '++'
1450 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001451 if (!LHS.isInvalid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001452 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCallb268a282010-08-23 23:25:46 +00001453 Tok.getKind(), LHS.take());
Sebastian Redl511ed552008-11-25 22:21:31 +00001454 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001455 ConsumeToken();
1456 break;
Chris Lattnerf8339772006-08-10 22:01:51 +00001457 }
1458 }
Chris Lattner52a99e52006-08-10 20:56:00 +00001459}
1460
Peter Collingbournee190dee2011-03-11 19:24:49 +00001461/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1462/// vec_step and we are at the start of an expression or a parenthesized
1463/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1464/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001465///
James Dennett3d5e4592012-06-17 04:36:28 +00001466/// \verbatim
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001467/// unary-expression: [C99 6.5.3]
1468/// 'sizeof' unary-expression
1469/// 'sizeof' '(' type-name ')'
1470/// [GNU] '__alignof' unary-expression
1471/// [GNU] '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +00001472/// [C11] '_Alignof' '(' type-name ')'
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001473/// [C++0x] 'alignof' '(' type-id ')'
1474///
1475/// [GNU] typeof-specifier:
1476/// typeof ( expressions )
1477/// typeof ( type-name )
1478/// [GNU/C++] typeof unary-expression
1479///
Peter Collingbournee190dee2011-03-11 19:24:49 +00001480/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1481/// vec_step ( expressions )
1482/// vec_step ( type-name )
James Dennett3d5e4592012-06-17 04:36:28 +00001483/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001484ExprResult
Peter Collingbournee190dee2011-03-11 19:24:49 +00001485Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1486 bool &isCastExpr,
1487 ParsedType &CastTy,
1488 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001489
1490 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournee190dee2011-03-11 19:24:49 +00001491 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
Jordan Rose58d54722012-06-30 21:33:57 +00001492 OpTok.is(tok::kw__Alignof) || OpTok.is(tok::kw_vec_step)) &&
Peter Collingbournee190dee2011-03-11 19:24:49 +00001493 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001494
John McCalldadc5752010-08-24 06:29:42 +00001495 ExprResult Operand;
Mike Stump11289f42009-09-09 15:08:12 +00001496
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001497 // If the operand doesn't start with an '(', it must be an expression.
1498 if (Tok.isNot(tok::l_paren)) {
Serge Pavlovaa57a642013-10-08 16:56:30 +00001499 // If construct allows a form without parenthesis, user may forget to put
1500 // pathenthesis around type name.
1501 if (OpTok.is(tok::kw_sizeof) || OpTok.is(tok::kw___alignof) ||
1502 OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof)) {
1503 bool isAmbiguousTypeId;
1504 if (isTypeIdInParens(isAmbiguousTypeId)) {
1505 DeclSpec DS(AttrFactory);
1506 ParseSpecifierQualifierList(DS);
1507 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1508 ParseDeclarator(DeclaratorInfo);
1509
1510 SourceLocation LParenLoc = PP.getLocForEndOfToken(OpTok.getLocation());
1511 SourceLocation RParenLoc = PP.getLocForEndOfToken(PrevTokLocation);
David Majnemer767c1f82013-10-09 00:22:23 +00001512 Diag(LParenLoc, diag::err_expected_parentheses_around_typename)
Serge Pavlovaa57a642013-10-08 16:56:30 +00001513 << OpTok.getName()
1514 << FixItHint::CreateInsertion(LParenLoc, "(")
1515 << FixItHint::CreateInsertion(RParenLoc, ")");
1516 isCastExpr = true;
1517 return ExprEmpty();
1518 }
1519 }
1520
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001521 isCastExpr = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001522 if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001523 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1524 return ExprError();
1525 }
Mike Stump11289f42009-09-09 15:08:12 +00001526
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001527 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001528 } else {
1529 // If it starts with a '(', we know that it is either a parenthesized
1530 // type-name, or it is a unary-expression that starts with a compound
1531 // literal, or starts with a primary-expression that is a parenthesized
1532 // expression.
1533 ParenParseOption ExprType = CastExpr;
1534 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001535
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001536 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001537 false, CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001538 CastRange = SourceRange(LParenLoc, RParenLoc);
1539
1540 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1541 // a type.
1542 if (ExprType == CastExpr) {
1543 isCastExpr = true;
1544 return ExprEmpty();
1545 }
1546
David Blaikiebbafb8a2012-03-11 07:00:24 +00001547 if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
Douglas Gregor5dc05532010-07-28 18:22:12 +00001548 // GNU typeof in C requires the expression to be parenthesized. Not so for
1549 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1550 // the start of a unary-expression, but doesn't include any postfix
1551 // pieces. Parse these now if present.
John McCall3669c802010-08-24 23:41:43 +00001552 if (!Operand.isInvalid())
1553 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor5dc05532010-07-28 18:22:12 +00001554 }
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001555 }
1556
1557 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1558 isCastExpr = false;
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001559 return Operand;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001560}
1561
Chris Lattner20c6a452006-08-12 17:40:43 +00001562
James Dennett3d5e4592012-06-17 04:36:28 +00001563/// \brief Parse a sizeof or alignof expression.
1564///
1565/// \verbatim
Chris Lattner81b576e2006-08-11 02:13:20 +00001566/// unary-expression: [C99 6.5.3]
1567/// 'sizeof' unary-expression
1568/// 'sizeof' '(' type-name ')'
Richard Smith7dd5fe52013-01-29 10:18:18 +00001569/// [C++11] 'sizeof' '...' '(' identifier ')'
Chris Lattner81b576e2006-08-11 02:13:20 +00001570/// [GNU] '__alignof' unary-expression
1571/// [GNU] '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +00001572/// [C11] '_Alignof' '(' type-name ')'
Richard Smith7dd5fe52013-01-29 10:18:18 +00001573/// [C++11] 'alignof' '(' type-id ')'
James Dennett3d5e4592012-06-17 04:36:28 +00001574/// \endverbatim
Peter Collingbournee190dee2011-03-11 19:24:49 +00001575ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Jordan Rose58d54722012-06-30 21:33:57 +00001576 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) ||
1577 Tok.is(tok::kw_alignof) || Tok.is(tok::kw__Alignof) ||
1578 Tok.is(tok::kw_vec_step)) &&
Peter Collingbournee190dee2011-03-11 19:24:49 +00001579 "Not a sizeof/alignof/vec_step expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001580 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001581 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001582
Richard Smith7dd5fe52013-01-29 10:18:18 +00001583 // [C++11] 'sizeof' '...' '(' identifier ')'
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001584 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1585 SourceLocation EllipsisLoc = ConsumeToken();
1586 SourceLocation LParenLoc, RParenLoc;
1587 IdentifierInfo *Name = 0;
1588 SourceLocation NameLoc;
1589 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001590 BalancedDelimiterTracker T(*this, tok::l_paren);
1591 T.consumeOpen();
1592 LParenLoc = T.getOpenLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001593 if (Tok.is(tok::identifier)) {
1594 Name = Tok.getIdentifierInfo();
1595 NameLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001596 T.consumeClose();
1597 RParenLoc = T.getCloseLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001598 if (RParenLoc.isInvalid())
1599 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1600 } else {
1601 Diag(Tok, diag::err_expected_parameter_pack);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001602 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001603 }
1604 } else if (Tok.is(tok::identifier)) {
1605 Name = Tok.getIdentifierInfo();
1606 NameLoc = ConsumeToken();
1607 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1608 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1609 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1610 << Name
1611 << FixItHint::CreateInsertion(LParenLoc, "(")
1612 << FixItHint::CreateInsertion(RParenLoc, ")");
1613 } else {
1614 Diag(Tok, diag::err_sizeof_parameter_pack);
1615 }
1616
1617 if (!Name)
1618 return ExprError();
1619
Faisal Valife194d5a2013-10-31 15:58:51 +00001620 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1621 Sema::ReuseLambdaContextDecl);
1622
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001623 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1624 OpTok.getLocation(),
1625 *Name, NameLoc,
1626 RParenLoc);
1627 }
Richard Smithb15c11c2011-10-17 23:06:20 +00001628
Jordan Rose58d54722012-06-30 21:33:57 +00001629 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof))
Richard Smithb15c11c2011-10-17 23:06:20 +00001630 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1631
Eli Friedman15681d62012-09-26 04:34:21 +00001632 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1633 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00001634
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001635 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00001636 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001637 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001638 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1639 isCastExpr,
1640 CastTy,
1641 CastRange);
1642
1643 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
Jordan Rose58d54722012-06-30 21:33:57 +00001644 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof) ||
1645 OpTok.is(tok::kw__Alignof))
Peter Collingbournee190dee2011-03-11 19:24:49 +00001646 ExprKind = UETT_AlignOf;
1647 else if (OpTok.is(tok::kw_vec_step))
1648 ExprKind = UETT_VecStep;
Sebastian Redl90893182008-12-11 22:33:27 +00001649
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001650 if (isCastExpr)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001651 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1652 ExprKind,
1653 /*isType=*/true,
1654 CastTy.getAsOpaquePtr(),
1655 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001656
Richard Smith7dd5fe52013-01-29 10:18:18 +00001657 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof))
1658 Diag(OpTok, diag::ext_alignof_expr) << OpTok.getIdentifierInfo();
1659
Chris Lattner26115ac2006-08-24 06:10:04 +00001660 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001661 if (!Operand.isInvalid())
Peter Collingbournee190dee2011-03-11 19:24:49 +00001662 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1663 ExprKind,
1664 /*isType=*/false,
1665 Operand.release(),
1666 CastRange);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001667 return Operand;
Chris Lattner81b576e2006-08-11 02:13:20 +00001668}
1669
Chris Lattner11124352006-08-12 19:16:08 +00001670/// ParseBuiltinPrimaryExpression
1671///
James Dennett3d5e4592012-06-17 04:36:28 +00001672/// \verbatim
Chris Lattner11124352006-08-12 19:16:08 +00001673/// primary-expression: [C99 6.5.1]
1674/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1675/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1676/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1677/// assign-expr ')'
1678/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbourne62c21982011-11-05 03:47:48 +00001679/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001680///
Chris Lattner11124352006-08-12 19:16:08 +00001681/// [GNU] offsetof-member-designator:
1682/// [GNU] identifier
1683/// [GNU] offsetof-member-designator '.' identifier
1684/// [GNU] offsetof-member-designator '[' expression ']'
James Dennett3d5e4592012-06-17 04:36:28 +00001685/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001686ExprResult Parser::ParseBuiltinPrimaryExpression() {
1687 ExprResult Res;
Chris Lattner11124352006-08-12 19:16:08 +00001688 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1689
1690 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001691 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001692
1693 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001694 if (Tok.isNot(tok::l_paren))
1695 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1696 << BuiltinII);
1697
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001698 BalancedDelimiterTracker PT(*this, tok::l_paren);
1699 PT.consumeOpen();
1700
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001701 // TODO: Build AST.
1702
Chris Lattner11124352006-08-12 19:16:08 +00001703 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +00001704 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001705 case tok::kw___builtin_va_arg: {
John McCalldadc5752010-08-24 06:29:42 +00001706 ExprResult Expr(ParseAssignmentExpression());
Chris Lattner0be454e2006-08-12 19:30:51 +00001707
Chris Lattner6d7e6342006-08-15 03:41:14 +00001708 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001709 Expr = ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001710
Douglas Gregor220cac52009-02-18 17:45:20 +00001711 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001712
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001713 if (Tok.isNot(tok::r_paren)) {
1714 Diag(Tok, diag::err_expected_rparen);
Douglas Gregoreda7e542010-09-18 01:28:11 +00001715 Expr = ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001716 }
Douglas Gregoreda7e542010-09-18 01:28:11 +00001717
1718 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor220cac52009-02-18 17:45:20 +00001719 Res = ExprError();
1720 else
John McCallb268a282010-08-23 23:25:46 +00001721 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001722 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001723 }
Chris Lattner687d6092007-08-30 15:51:11 +00001724 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001725 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001726 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001727 if (Ty.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001728 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001729 return ExprError();
1730 }
Mike Stump11289f42009-09-09 15:08:12 +00001731
Chris Lattner6d7e6342006-08-15 03:41:14 +00001732 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001733 return ExprError();
1734
Chris Lattner11124352006-08-12 19:16:08 +00001735 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001736 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001737 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001738 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl90893182008-12-11 22:33:27 +00001739 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001740 }
Sebastian Redl90893182008-12-11 22:33:27 +00001741
Chris Lattner687d6092007-08-30 15:51:11 +00001742 // Keep track of the various subcomponents we see.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001743 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001744
John McCallfaf5fb42010-08-26 23:41:50 +00001745 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001746 Comps.back().isBrackets = false;
1747 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1748 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001749
Sebastian Redl511ed552008-11-25 22:21:31 +00001750 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001751 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001752 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001753 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallfaf5fb42010-08-26 23:41:50 +00001754 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001755 Comps.back().isBrackets = false;
1756 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001757
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001758 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001759 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001760 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl90893182008-12-11 22:33:27 +00001761 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001762 }
1763 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1764 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001765
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001766 } else if (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001767 if (CheckProhibitedCXX11Attribute())
1768 return ExprError();
1769
Chris Lattner11124352006-08-12 19:16:08 +00001770 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallfaf5fb42010-08-26 23:41:50 +00001771 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001772 Comps.back().isBrackets = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001773 BalancedDelimiterTracker ST(*this, tok::l_square);
1774 ST.consumeOpen();
1775 Comps.back().LocStart = ST.getOpenLocation();
Chris Lattner11124352006-08-12 19:16:08 +00001776 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001777 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001778 SkipUntil(tok::r_paren, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001779 return Res;
Chris Lattner11124352006-08-12 19:16:08 +00001780 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001781 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001782
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001783 ST.consumeClose();
1784 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman5e774b12009-06-27 20:38:33 +00001785 } else {
1786 if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001787 PT.consumeClose();
Douglas Gregor220cac52009-02-18 17:45:20 +00001788 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001789 } else if (Ty.isInvalid()) {
1790 Res = ExprError();
1791 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001792 PT.consumeClose();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001793 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001794 Ty.get(), &Comps[0], Comps.size(),
1795 PT.getCloseLocation());
Eli Friedman5e774b12009-06-27 20:38:33 +00001796 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001797 break;
Chris Lattner11124352006-08-12 19:16:08 +00001798 }
1799 }
1800 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001801 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001802 case tok::kw___builtin_choose_expr: {
John McCalldadc5752010-08-24 06:29:42 +00001803 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001804 if (Cond.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001805 SkipUntil(tok::r_paren, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001806 return Cond;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001807 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001808 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001809 return ExprError();
1810
John McCalldadc5752010-08-24 06:29:42 +00001811 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001812 if (Expr1.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001813 SkipUntil(tok::r_paren, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001814 return Expr1;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001815 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001816 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001817 return ExprError();
1818
John McCalldadc5752010-08-24 06:29:42 +00001819 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001820 if (Expr2.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001821 SkipUntil(tok::r_paren, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001822 return Expr2;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001823 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001824 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001825 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001826 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001827 }
John McCallb268a282010-08-23 23:25:46 +00001828 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1829 Expr2.take(), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001830 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001831 }
Tanya Lattner55808c12011-06-04 00:47:47 +00001832 case tok::kw___builtin_astype: {
1833 // The first argument is an expression to be converted, followed by a comma.
1834 ExprResult Expr(ParseAssignmentExpression());
1835 if (Expr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001836 SkipUntil(tok::r_paren, StopAtSemi);
Tanya Lattner55808c12011-06-04 00:47:47 +00001837 return ExprError();
1838 }
1839
1840 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1841 tok::r_paren))
1842 return ExprError();
1843
1844 // Second argument is the type to bitcast to.
1845 TypeResult DestTy = ParseTypeName();
1846 if (DestTy.isInvalid())
1847 return ExprError();
1848
1849 // Attempt to consume the r-paren.
1850 if (Tok.isNot(tok::r_paren)) {
1851 Diag(Tok, diag::err_expected_rparen);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001852 SkipUntil(tok::r_paren, StopAtSemi);
Tanya Lattner55808c12011-06-04 00:47:47 +00001853 return ExprError();
1854 }
1855
1856 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1857 ConsumeParen());
1858 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001859 }
Hal Finkelc4d7c822013-09-18 03:29:45 +00001860 case tok::kw___builtin_convertvector: {
1861 // The first argument is an expression to be converted, followed by a comma.
1862 ExprResult Expr(ParseAssignmentExpression());
1863 if (Expr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001864 SkipUntil(tok::r_paren, StopAtSemi);
Hal Finkelc4d7c822013-09-18 03:29:45 +00001865 return ExprError();
1866 }
1867
1868 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1869 tok::r_paren))
1870 return ExprError();
1871
1872 // Second argument is the type to bitcast to.
1873 TypeResult DestTy = ParseTypeName();
1874 if (DestTy.isInvalid())
1875 return ExprError();
1876
1877 // Attempt to consume the r-paren.
1878 if (Tok.isNot(tok::r_paren)) {
1879 Diag(Tok, diag::err_expected_rparen);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001880 SkipUntil(tok::r_paren, StopAtSemi);
Hal Finkelc4d7c822013-09-18 03:29:45 +00001881 return ExprError();
1882 }
1883
1884 Res = Actions.ActOnConvertVectorExpr(Expr.take(), DestTy.get(), StartLoc,
1885 ConsumeParen());
1886 break;
1887 }
Chandler Carruth904cb142011-07-08 04:59:44 +00001888 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001889
John McCallb268a282010-08-23 23:25:46 +00001890 if (Res.isInvalid())
1891 return ExprError();
1892
Chris Lattner11124352006-08-12 19:16:08 +00001893 // These can be followed by postfix-expr pieces because they are
1894 // primary-expressions.
John McCallb268a282010-08-23 23:25:46 +00001895 return ParsePostfixExpressionSuffix(Res.take());
Chris Lattner11124352006-08-12 19:16:08 +00001896}
1897
Chris Lattner4add4e62006-08-11 01:33:00 +00001898/// ParseParenExpression - This parses the unit that starts with a '(' token,
1899/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001900/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1901/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001902///
James Dennett3d5e4592012-06-17 04:36:28 +00001903/// \verbatim
Chris Lattner4add4e62006-08-11 01:33:00 +00001904/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001905/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001906/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1907/// postfix-expression: [C99 6.5.2]
1908/// '(' type-name ')' '{' initializer-list '}'
1909/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001910/// cast-expression: [C99 6.5.4]
1911/// '(' type-name ')' cast-expression
John McCall31168b02011-06-15 23:02:42 +00001912/// [ARC] bridged-cast-expression
1913///
1914/// [ARC] bridged-cast-expression:
1915/// (__bridge type-name) cast-expression
1916/// (__bridge_transfer type-name) cast-expression
1917/// (__bridge_retained type-name) cast-expression
James Dennett3d5e4592012-06-17 04:36:28 +00001918/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001919ExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001920Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001921 bool isTypeCast, ParsedType &CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001922 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001923 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001924 BalancedDelimiterTracker T(*this, tok::l_paren);
1925 if (T.consumeOpen())
1926 return ExprError();
1927 SourceLocation OpenLoc = T.getOpenLocation();
1928
John McCalldadc5752010-08-24 06:29:42 +00001929 ExprResult Result(true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001930 bool isAmbiguousTypeId;
John McCallba7bf592010-08-24 05:47:05 +00001931 CastTy = ParsedType();
Sebastian Redl90893182008-12-11 22:33:27 +00001932
Douglas Gregor5e35d592010-09-14 23:59:36 +00001933 if (Tok.is(tok::code_completion)) {
1934 Actions.CodeCompleteOrdinaryName(getCurScope(),
1935 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1936 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001937 cutOffParsing();
Douglas Gregor5e35d592010-09-14 23:59:36 +00001938 return ExprError();
1939 }
John McCallc5e6b972011-04-06 02:35:25 +00001940
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001941 // Diagnose use of bridge casts in non-arc mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001942 bool BridgeCast = (getLangOpts().ObjC2 &&
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001943 (Tok.is(tok::kw___bridge) ||
1944 Tok.is(tok::kw___bridge_transfer) ||
1945 Tok.is(tok::kw___bridge_retained) ||
1946 Tok.is(tok::kw___bridge_retain)));
David Blaikiebbafb8a2012-03-11 07:00:24 +00001947 if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanian896ae922013-04-02 23:48:59 +00001948 if (Tok.isNot(tok::kw___bridge)) {
1949 StringRef BridgeCastName = Tok.getName();
1950 SourceLocation BridgeKeywordLoc = ConsumeToken();
1951 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1952 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
1953 << BridgeCastName
1954 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
1955 }
1956 else
1957 ConsumeToken(); // consume __bridge
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001958 BridgeCast = false;
1959 }
1960
John McCallc5e6b972011-04-06 02:35:25 +00001961 // None of these cases should fall through with an invalid Result
1962 // unless they've already reported an error.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001963 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001964 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall3abee492012-04-04 01:27:53 +00001965 Actions.ActOnStartStmtExpr();
1966
Richard Smithc202b282012-04-14 00:33:13 +00001967 StmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001968 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001969
Chris Lattner366727f2007-07-24 16:58:17 +00001970 // If the substmt parsed correctly, build the AST node.
John McCall3abee492012-04-04 01:27:53 +00001971 if (!Stmt.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001972 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
John McCall3abee492012-04-04 01:27:53 +00001973 } else {
1974 Actions.ActOnStmtExprError();
1975 }
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001976 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCall0c07bee2011-06-17 21:56:12 +00001977 tok::TokenKind tokenKind = Tok.getKind();
1978 SourceLocation BridgeKeywordLoc = ConsumeToken();
1979
John McCall31168b02011-06-15 23:02:42 +00001980 // Parse an Objective-C ARC ownership cast expression.
1981 ObjCBridgeCastKind Kind;
John McCall0c07bee2011-06-17 21:56:12 +00001982 if (tokenKind == tok::kw___bridge)
John McCall31168b02011-06-15 23:02:42 +00001983 Kind = OBC_Bridge;
John McCall0c07bee2011-06-17 21:56:12 +00001984 else if (tokenKind == tok::kw___bridge_transfer)
John McCall31168b02011-06-15 23:02:42 +00001985 Kind = OBC_BridgeTransfer;
John McCall0c07bee2011-06-17 21:56:12 +00001986 else if (tokenKind == tok::kw___bridge_retained)
John McCall31168b02011-06-15 23:02:42 +00001987 Kind = OBC_BridgeRetained;
John McCall0c07bee2011-06-17 21:56:12 +00001988 else {
1989 // As a hopefully temporary workaround, allow __bridge_retain as
1990 // a synonym for __bridge_retained, but only in system headers.
1991 assert(tokenKind == tok::kw___bridge_retain);
1992 Kind = OBC_BridgeRetained;
1993 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1994 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1995 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1996 "__bridge_retained");
1997 }
John McCall31168b02011-06-15 23:02:42 +00001998
John McCall31168b02011-06-15 23:02:42 +00001999 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002000 T.consumeClose();
2001 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002002 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCall31168b02011-06-15 23:02:42 +00002003
2004 if (Ty.isInvalid() || SubExpr.isInvalid())
2005 return ExprError();
2006
2007 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
2008 BridgeKeywordLoc, Ty.get(),
2009 RParenLoc, SubExpr.get());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002010 } else if (ExprType >= CompoundLiteral &&
2011 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00002012
Chris Lattner6c3f05d2006-08-12 16:54:25 +00002013 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00002014
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002015 // In C++, if the type-id is ambiguous we disambiguate based on context.
2016 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
2017 // in which case we should treat it as type-id.
2018 // if stopIfCastExpr is false, we need to determine the context past the
2019 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002020 if (isAmbiguousTypeId && !stopIfCastExpr) {
2021 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
2022 RParenLoc = T.getCloseLocation();
2023 return res;
2024 }
Mike Stump11289f42009-09-09 15:08:12 +00002025
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002026 // Parse the type declarator.
2027 DeclSpec DS(AttrFactory);
2028 ParseSpecifierQualifierList(DS);
2029 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2030 ParseDeclarator(DeclaratorInfo);
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002031
Douglas Gregor3e972002010-09-15 23:19:31 +00002032 // If our type is followed by an identifier and either ':' or ']', then
2033 // this is probably an Objective-C message send where the leading '[' is
2034 // missing. Recover as if that were the case.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002035 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002036 !InMessageExpression && getLangOpts().ObjC1 &&
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002037 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
2038 TypeResult Ty;
2039 {
2040 InMessageExpressionRAIIObject InMessage(*this, false);
2041 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2042 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002043 Result = ParseObjCMessageExpressionBody(SourceLocation(),
2044 SourceLocation(),
2045 Ty.get(), 0);
2046 } else {
2047 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002048 T.consumeClose();
2049 RParenLoc = T.getCloseLocation();
Douglas Gregor3e972002010-09-15 23:19:31 +00002050 if (Tok.is(tok::l_brace)) {
2051 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002052 TypeResult Ty;
2053 {
2054 InMessageExpressionRAIIObject InMessage(*this, false);
2055 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2056 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002057 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +00002058 }
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00002059
Douglas Gregor3e972002010-09-15 23:19:31 +00002060 if (ExprType == CastExpr) {
2061 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb5d49352009-01-19 22:31:54 +00002062
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002063 if (DeclaratorInfo.isInvalidType())
Douglas Gregor3e972002010-09-15 23:19:31 +00002064 return ExprError();
2065
Douglas Gregor3e972002010-09-15 23:19:31 +00002066 // Note that this doesn't parse the subsequent cast-expression, it just
2067 // returns the parsed type to the callee.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002068 if (stopIfCastExpr) {
2069 TypeResult Ty;
2070 {
2071 InMessageExpressionRAIIObject InMessage(*this, false);
2072 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2073 }
2074 CastTy = Ty.get();
Douglas Gregor3e972002010-09-15 23:19:31 +00002075 return ExprResult();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002076 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002077
2078 // Reject the cast of super idiom in ObjC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002079 if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
Douglas Gregor3e972002010-09-15 23:19:31 +00002080 Tok.getIdentifierInfo() == Ident_super &&
2081 getCurScope()->isInObjcMethodScope() &&
2082 GetLookAheadToken(1).isNot(tok::period)) {
2083 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2084 << SourceRange(OpenLoc, RParenLoc);
2085 return ExprError();
2086 }
2087
2088 // Parse the cast-expression that follows it next.
2089 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002090 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2091 /*isAddressOfOperand=*/false,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002092 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002093 if (!Result.isInvalid()) {
2094 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2095 DeclaratorInfo, CastTy,
Douglas Gregor3e972002010-09-15 23:19:31 +00002096 RParenLoc, Result.take());
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002097 }
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002098 return Result;
Douglas Gregor3e972002010-09-15 23:19:31 +00002099 }
2100
2101 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2102 return ExprError();
2103 }
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002104 } else if (isTypeCast) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00002105 // Parse the expression-list.
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002106 InMessageExpressionRAIIObject InMessage(*this, false);
2107
Benjamin Kramerf0623432012-08-23 22:51:59 +00002108 ExprVector ArgExprs;
Nate Begeman5ec4b312009-08-10 23:49:36 +00002109 CommaLocsTy CommaLocs;
2110
Eli Friedman7a15c4a2013-08-13 23:38:34 +00002111 if (!ParseSimpleExpressionList(ArgExprs, CommaLocs)) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00002112 ExprType = SimpleExpr;
Sebastian Redla9351792012-02-11 23:51:47 +00002113 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002114 ArgExprs);
Nate Begeman5ec4b312009-08-10 23:49:36 +00002115 }
Chris Lattner4add4e62006-08-11 01:33:00 +00002116 } else {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002117 InMessageExpressionRAIIObject InMessage(*this, false);
2118
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002119 Result = ParseExpression(MaybeTypeCast);
Chris Lattner4add4e62006-08-11 01:33:00 +00002120 ExprType = SimpleExpr;
John McCallc5e6b972011-04-06 02:35:25 +00002121
2122 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002123 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCallb268a282010-08-23 23:25:46 +00002124 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Chris Lattnerf8339772006-08-10 22:01:51 +00002125 }
Sebastian Redl90893182008-12-11 22:33:27 +00002126
Chris Lattner4564bc12006-08-10 23:14:52 +00002127 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00002128 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002129 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerd8980502008-12-12 06:00:12 +00002130 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00002131 }
Mike Stump11289f42009-09-09 15:08:12 +00002132
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002133 T.consumeClose();
2134 RParenLoc = T.getCloseLocation();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002135 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +00002136}
Chris Lattnerd3e98952006-10-06 05:22:26 +00002137
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002138/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2139/// and we are at the left brace.
2140///
James Dennett3d5e4592012-06-17 04:36:28 +00002141/// \verbatim
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002142/// postfix-expression: [C99 6.5.2]
2143/// '(' type-name ')' '{' initializer-list '}'
2144/// '(' type-name ')' '{' initializer-list ',' '}'
James Dennett3d5e4592012-06-17 04:36:28 +00002145/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002146ExprResult
John McCallba7bf592010-08-24 05:47:05 +00002147Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002148 SourceLocation LParenLoc,
2149 SourceLocation RParenLoc) {
2150 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
David Blaikiebbafb8a2012-03-11 07:00:24 +00002151 if (!getLangOpts().C99) // Compound literals don't exist in C90.
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002152 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCalldadc5752010-08-24 06:29:42 +00002153 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002154 if (!Result.isInvalid() && Ty)
John McCallb268a282010-08-23 23:25:46 +00002155 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002156 return Result;
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002157}
2158
Chris Lattnerd3e98952006-10-06 05:22:26 +00002159/// ParseStringLiteralExpression - This handles the various token types that
2160/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2161/// translation phase #6].
2162///
James Dennett3d5e4592012-06-17 04:36:28 +00002163/// \verbatim
Chris Lattnerd3e98952006-10-06 05:22:26 +00002164/// primary-expression: [C99 6.5.1]
2165/// string-literal
James Dennett3d5e4592012-06-17 04:36:28 +00002166/// \verbatim
Richard Smithd67aea22012-03-06 03:21:47 +00002167ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002168 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00002169
Chris Lattnerd3e98952006-10-06 05:22:26 +00002170 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2171 // considered to be strings for concatenation purposes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002172 SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00002173
Chris Lattnerd3e98952006-10-06 05:22:26 +00002174 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002175 StringToks.push_back(Tok);
2176 ConsumeStringToken();
2177 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00002178
2179 // Pass the set of string tokens, ready for concatenation, to the actions.
Richard Smithbcc22fc2012-03-09 08:00:36 +00002180 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(),
2181 AllowUserDefinedLiteral ? getCurScope() : 0);
Chris Lattnerd3e98952006-10-06 05:22:26 +00002182}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002183
Benjamin Kramere56f3932011-12-23 17:00:35 +00002184/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2185/// [C11 6.5.1.1].
Peter Collingbourne91147592011-04-15 00:35:48 +00002186///
James Dennett3d5e4592012-06-17 04:36:28 +00002187/// \verbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002188/// generic-selection:
2189/// _Generic ( assignment-expression , generic-assoc-list )
2190/// generic-assoc-list:
2191/// generic-association
2192/// generic-assoc-list , generic-association
2193/// generic-association:
2194/// type-name : assignment-expression
2195/// default : assignment-expression
James Dennett3d5e4592012-06-17 04:36:28 +00002196/// \endverbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002197ExprResult Parser::ParseGenericSelectionExpression() {
2198 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2199 SourceLocation KeyLoc = ConsumeToken();
2200
David Blaikiebbafb8a2012-03-11 07:00:24 +00002201 if (!getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +00002202 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbourne91147592011-04-15 00:35:48 +00002203
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002204 BalancedDelimiterTracker T(*this, tok::l_paren);
2205 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne91147592011-04-15 00:35:48 +00002206 return ExprError();
2207
2208 ExprResult ControllingExpr;
2209 {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002210 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbourne91147592011-04-15 00:35:48 +00002211 // not evaluated."
2212 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2213 ControllingExpr = ParseAssignmentExpression();
2214 if (ControllingExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002215 SkipUntil(tok::r_paren, StopAtSemi);
Peter Collingbourne91147592011-04-15 00:35:48 +00002216 return ExprError();
2217 }
2218 }
2219
2220 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002221 SkipUntil(tok::r_paren, StopAtSemi);
Peter Collingbourne91147592011-04-15 00:35:48 +00002222 return ExprError();
2223 }
2224
2225 SourceLocation DefaultLoc;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002226 TypeVector Types;
2227 ExprVector Exprs;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00002228 do {
Peter Collingbourne91147592011-04-15 00:35:48 +00002229 ParsedType Ty;
2230 if (Tok.is(tok::kw_default)) {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002231 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbourne91147592011-04-15 00:35:48 +00002232 // generic association."
2233 if (!DefaultLoc.isInvalid()) {
2234 Diag(Tok, diag::err_duplicate_default_assoc);
2235 Diag(DefaultLoc, diag::note_previous_default_assoc);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002236 SkipUntil(tok::r_paren, StopAtSemi);
Peter Collingbourne91147592011-04-15 00:35:48 +00002237 return ExprError();
2238 }
2239 DefaultLoc = ConsumeToken();
2240 Ty = ParsedType();
2241 } else {
2242 ColonProtectionRAIIObject X(*this);
2243 TypeResult TR = ParseTypeName();
2244 if (TR.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002245 SkipUntil(tok::r_paren, StopAtSemi);
Peter Collingbourne91147592011-04-15 00:35:48 +00002246 return ExprError();
2247 }
2248 Ty = TR.release();
2249 }
2250 Types.push_back(Ty);
2251
2252 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002253 SkipUntil(tok::r_paren, StopAtSemi);
Peter Collingbourne91147592011-04-15 00:35:48 +00002254 return ExprError();
2255 }
2256
2257 // FIXME: These expressions should be parsed in a potentially potentially
2258 // evaluated context.
2259 ExprResult ER(ParseAssignmentExpression());
2260 if (ER.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002261 SkipUntil(tok::r_paren, StopAtSemi);
Peter Collingbourne91147592011-04-15 00:35:48 +00002262 return ExprError();
2263 }
2264 Exprs.push_back(ER.release());
Alp Tokera3ebe6e2013-12-17 14:12:37 +00002265 } while (TryConsumeToken(tok::comma));
Peter Collingbourne91147592011-04-15 00:35:48 +00002266
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002267 T.consumeClose();
2268 if (T.getCloseLocation().isInvalid())
Peter Collingbourne91147592011-04-15 00:35:48 +00002269 return ExprError();
2270
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002271 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2272 T.getCloseLocation(),
Peter Collingbourne91147592011-04-15 00:35:48 +00002273 ControllingExpr.release(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002274 Types, Exprs);
Peter Collingbourne91147592011-04-15 00:35:48 +00002275}
2276
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002277/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2278///
James Dennett3d5e4592012-06-17 04:36:28 +00002279/// \verbatim
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002280/// argument-expression-list:
2281/// assignment-expression
2282/// argument-expression-list , assignment-expression
2283///
2284/// [C++] expression-list:
Sebastian Redl3da34892011-06-05 12:23:16 +00002285/// [C++] assignment-expression
2286/// [C++] expression-list , assignment-expression
2287///
2288/// [C++0x] expression-list:
2289/// [C++0x] initializer-list
2290///
2291/// [C++0x] initializer-list
2292/// [C++0x] initializer-clause ...[opt]
2293/// [C++0x] initializer-list , initializer-clause ...[opt]
2294///
2295/// [C++0x] initializer-clause:
2296/// [C++0x] assignment-expression
2297/// [C++0x] braced-init-list
James Dennett3d5e4592012-06-17 04:36:28 +00002298/// \endverbatim
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002299bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002300 SmallVectorImpl<SourceLocation> &CommaLocs,
2301 void (Sema::*Completer)(Scope *S,
2302 Expr *Data,
2303 ArrayRef<Expr *> Args),
John McCall37ad5512010-08-23 06:44:23 +00002304 Expr *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002305 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00002306 if (Tok.is(tok::code_completion)) {
2307 if (Completer)
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002308 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor9c1f1bf2011-02-17 03:09:23 +00002309 else
2310 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002311 cutOffParsing();
2312 return true;
Douglas Gregorcabea402009-09-22 15:41:20 +00002313 }
Sebastian Redl3da34892011-06-05 12:23:16 +00002314
2315 ExprResult Expr;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002316 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002317 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00002318 Expr = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00002319 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00002320 Expr = ParseAssignmentExpression();
2321
Douglas Gregor968f23a2011-01-03 19:31:53 +00002322 if (Tok.is(tok::ellipsis))
2323 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002324 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002325 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00002326
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002327 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002328
2329 if (Tok.isNot(tok::comma))
2330 return false;
2331 // Move to the next argument, remember where the comma was.
2332 CommaLocs.push_back(ConsumeToken());
2333 }
2334}
Steve Naroff0ac012832008-08-28 19:20:44 +00002335
Eli Friedman7a15c4a2013-08-13 23:38:34 +00002336/// ParseSimpleExpressionList - A simple comma-separated list of expressions,
2337/// used for misc language extensions.
2338///
2339/// \verbatim
2340/// simple-expression-list:
2341/// assignment-expression
2342/// simple-expression-list , assignment-expression
2343/// \endverbatim
2344bool
2345Parser::ParseSimpleExpressionList(SmallVectorImpl<Expr*> &Exprs,
2346 SmallVectorImpl<SourceLocation> &CommaLocs) {
2347 while (1) {
2348 ExprResult Expr = ParseAssignmentExpression();
2349 if (Expr.isInvalid())
2350 return true;
2351
2352 Exprs.push_back(Expr.release());
2353
2354 if (Tok.isNot(tok::comma))
2355 return false;
2356
2357 // Move to the next argument, remember where the comma was.
2358 CommaLocs.push_back(ConsumeToken());
2359 }
2360}
2361
Mike Stump82f071f2009-02-04 22:31:32 +00002362/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2363///
James Dennett3d5e4592012-06-17 04:36:28 +00002364/// \verbatim
Mike Stump82f071f2009-02-04 22:31:32 +00002365/// [clang] block-id:
2366/// [clang] specifier-qualifier-list block-declarator
James Dennett3d5e4592012-06-17 04:36:28 +00002367/// \endverbatim
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002368void Parser::ParseBlockId(SourceLocation CaretLoc) {
Douglas Gregor643c3302010-10-18 21:34:55 +00002369 if (Tok.is(tok::code_completion)) {
2370 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002371 return cutOffParsing();
Douglas Gregor643c3302010-10-18 21:34:55 +00002372 }
2373
Mike Stump82f071f2009-02-04 22:31:32 +00002374 // Parse the specifier-qualifier-list piece.
John McCall084e83d2011-03-24 11:26:52 +00002375 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002376 ParseSpecifierQualifierList(DS);
2377
2378 // Parse the block-declarator.
2379 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2380 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002381
Mike Stump56ed2ea2009-04-29 21:40:37 +00002382 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall084e83d2011-03-24 11:26:52 +00002383 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump56ed2ea2009-04-29 21:40:37 +00002384
John McCall53fa7142010-12-24 02:08:15 +00002385 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002386
Mike Stump82f071f2009-02-04 22:31:32 +00002387 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002388 Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
Mike Stump82f071f2009-02-04 22:31:32 +00002389}
2390
Steve Naroff0ac012832008-08-28 19:20:44 +00002391/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00002392/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00002393///
James Dennett3d5e4592012-06-17 04:36:28 +00002394/// \verbatim
Steve Naroff0ac012832008-08-28 19:20:44 +00002395/// block-literal:
2396/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00002397/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00002398/// [clang] block-args:
2399/// [clang] '(' parameter-list ')'
James Dennett3d5e4592012-06-17 04:36:28 +00002400/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002401ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00002402 assert(Tok.is(tok::caret) && "block literal starts with ^");
2403 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002404
Chris Lattnerf6801202009-03-05 07:32:12 +00002405 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2406 "block literal parsing");
2407
Mike Stump11289f42009-09-09 15:08:12 +00002408 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00002409 // argument decls, decls within the compound expression, etc. This also
2410 // allows determining whether a variable reference inside the block is
2411 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002412 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002413 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00002414
2415 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002416 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump11289f42009-09-09 15:08:12 +00002417
Steve Naroff0ac012832008-08-28 19:20:44 +00002418 // Parse the return type if present.
John McCall084e83d2011-03-24 11:26:52 +00002419 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002420 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002421 // FIXME: Since the return type isn't actually parsed, it can't be used to
2422 // fill ParamInfo with an initial valid range, so do it manually.
2423 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002424
Steve Naroff0ac012832008-08-28 19:20:44 +00002425 // If this block has arguments, parse them. There is no ambiguity here with
2426 // the expression case, because the expression case requires a parameter list.
2427 if (Tok.is(tok::l_paren)) {
2428 ParseParenDeclarator(ParamInfo);
2429 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002430 // SetIdentifier sets the source range end, but in this case we're past
2431 // that location.
2432 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00002433 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002434 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002435 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00002436 // If there was an error parsing the arguments, they may have
2437 // tried to use ^(x+y) which requires an argument list. Just
2438 // skip the whole block literal.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002439 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002440 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00002441 }
Mike Stump88788fe2009-04-29 19:03:13 +00002442
John McCall53fa7142010-12-24 02:08:15 +00002443 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002444
Mike Stump82f071f2009-02-04 22:31:32 +00002445 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002446 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Mike Stumpd73e4412009-04-14 18:24:37 +00002447 } else if (!Tok.is(tok::l_brace)) {
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002448 ParseBlockId(CaretLoc);
Steve Naroff0ac012832008-08-28 19:20:44 +00002449 } else {
2450 // Otherwise, pretend we saw (void).
John McCall084e83d2011-03-24 11:26:52 +00002451 ParsedAttributes attrs(AttrFactory);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00002452 SourceLocation NoLoc;
2453 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(/*HasProto=*/true,
2454 /*IsAmbiguous=*/false,
2455 /*RParenLoc=*/NoLoc,
2456 /*ArgInfo=*/0,
2457 /*NumArgs=*/0,
2458 /*EllipsisLoc=*/NoLoc,
2459 /*RParenLoc=*/NoLoc,
2460 /*TypeQuals=*/0,
2461 /*RefQualifierIsLvalueRef=*/true,
2462 /*RefQualifierLoc=*/NoLoc,
2463 /*ConstQualifierLoc=*/NoLoc,
2464 /*VolatileQualifierLoc=*/NoLoc,
2465 /*MutableLoc=*/NoLoc,
2466 EST_None,
2467 /*ESpecLoc=*/NoLoc,
2468 /*Exceptions=*/0,
2469 /*ExceptionRanges=*/0,
2470 /*NumExceptions=*/0,
2471 /*NoexceptExpr=*/0,
2472 CaretLoc, CaretLoc,
2473 ParamInfo),
John McCall084e83d2011-03-24 11:26:52 +00002474 attrs, CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00002475
John McCall53fa7142010-12-24 02:08:15 +00002476 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002477
Mike Stump82f071f2009-02-04 22:31:32 +00002478 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002479 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Steve Naroff0ac012832008-08-28 19:20:44 +00002480 }
2481
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002482
John McCalldadc5752010-08-24 06:29:42 +00002483 ExprResult Result(true);
Chris Lattner9eac9312009-03-27 04:18:06 +00002484 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002485 // Saw something like: ^expr
2486 Diag(Tok, diag::err_expected_expression);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002487 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002488 return ExprError();
2489 }
Mike Stump11289f42009-09-09 15:08:12 +00002490
John McCalldadc5752010-08-24 06:29:42 +00002491 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002492 BlockScope.Exit();
Chris Lattner9eac9312009-03-27 04:18:06 +00002493 if (!Stmt.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002494 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9eac9312009-03-27 04:18:06 +00002495 else
Douglas Gregor0be31a22010-07-02 17:43:08 +00002496 Actions.ActOnBlockError(CaretLoc, getCurScope());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002497 return Result;
Steve Naroff0ac012832008-08-28 19:20:44 +00002498}
Ted Kremeneke65b0862012-03-06 20:05:56 +00002499
2500/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
2501///
2502/// '__objc_yes'
2503/// '__objc_no'
2504ExprResult Parser::ParseObjCBoolLiteral() {
2505 tok::TokenKind Kind = Tok.getKind();
2506 return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
2507}