blob: 840402530b0d39d538d4773094bb22d28a88c85e [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc951dae2006-08-10 04:23:57 +00007//
8//===----------------------------------------------------------------------===//
James Dennette37835e2012-06-19 21:02:26 +00009///
James Dennett3d5e4592012-06-17 04:36:28 +000010/// \file
11/// \brief Provides the Expression parsing implementation.
12///
13/// Expressions in C99 basically consist of a bunch of binary operators with
14/// unary operators and other random stuff at the leaves.
15///
16/// In the C99 grammar, these unary operators bind tightest and are represented
17/// as the 'cast-expression' production. Everything else is either a binary
18/// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
19/// handled by ParseCastExpression, the higher level pieces are handled by
20/// ParseBinaryExpression.
James Dennette37835e2012-06-19 21:02:26 +000021///
22//===----------------------------------------------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +000023
24#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/Scope.h"
27#include "clang/Sema/ParsedTemplate.h"
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +000028#include "clang/Sema/TypoCorrection.h"
Chris Lattnerf6801202009-03-05 07:32:12 +000029#include "clang/Basic/PrettyStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000030#include "RAIIObjectsForParser.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000031#include "llvm/ADT/SmallVector.h"
Chris Lattner834618d2006-11-03 07:48:41 +000032#include "llvm/ADT/SmallString.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000033using namespace clang;
34
James Dennett3d5e4592012-06-17 04:36:28 +000035/// \brief Return the precedence of the specified binary operator token.
Mike Stump11289f42009-09-09 15:08:12 +000036static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregorcbb45d02009-02-25 23:02:36 +000037 bool GreaterThanIsOperator,
38 bool CPlusPlus0x) {
Chris Lattnercde626a2006-08-12 08:13:25 +000039 switch (Kind) {
Douglas Gregor8bf42052009-02-09 18:46:07 +000040 case tok::greater:
Douglas Gregorcbb45d02009-02-25 23:02:36 +000041 // C++ [temp.names]p3:
42 // [...] When parsing a template-argument-list, the first
43 // non-nested > is taken as the ending delimiter rather than a
44 // greater-than operator. [...]
Douglas Gregor8bf42052009-02-09 18:46:07 +000045 if (GreaterThanIsOperator)
46 return prec::Relational;
47 return prec::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +000048
Douglas Gregorcbb45d02009-02-25 23:02:36 +000049 case tok::greatergreater:
50 // C++0x [temp.names]p3:
51 //
52 // [...] Similarly, the first non-nested >> is treated as two
53 // consecutive but distinct > tokens, the first of which is
54 // taken as the end of the template-argument-list and completes
55 // the template-id. [...]
56 if (GreaterThanIsOperator || !CPlusPlus0x)
57 return prec::Shift;
58 return prec::Unknown;
59
Chris Lattnercde626a2006-08-12 08:13:25 +000060 default: return prec::Unknown;
61 case tok::comma: return prec::Comma;
62 case tok::equal:
63 case tok::starequal:
64 case tok::slashequal:
65 case tok::percentequal:
66 case tok::plusequal:
67 case tok::minusequal:
68 case tok::lesslessequal:
69 case tok::greatergreaterequal:
70 case tok::ampequal:
71 case tok::caretequal:
72 case tok::pipeequal: return prec::Assignment;
73 case tok::question: return prec::Conditional;
74 case tok::pipepipe: return prec::LogicalOr;
75 case tok::ampamp: return prec::LogicalAnd;
76 case tok::pipe: return prec::InclusiveOr;
77 case tok::caret: return prec::ExclusiveOr;
78 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +000079 case tok::exclaimequal:
80 case tok::equalequal: return prec::Equality;
81 case tok::lessequal:
82 case tok::less:
Douglas Gregor8bf42052009-02-09 18:46:07 +000083 case tok::greaterequal: return prec::Relational;
Douglas Gregorcbb45d02009-02-25 23:02:36 +000084 case tok::lessless: return prec::Shift;
Chris Lattnercde626a2006-08-12 08:13:25 +000085 case tok::plus:
86 case tok::minus: return prec::Additive;
87 case tok::percent:
88 case tok::slash:
89 case tok::star: return prec::Multiplicative;
Sebastian Redl112a97662009-02-07 00:15:38 +000090 case tok::periodstar:
91 case tok::arrowstar: return prec::PointerToMember;
Chris Lattnercde626a2006-08-12 08:13:25 +000092 }
93}
94
95
James Dennett3d5e4592012-06-17 04:36:28 +000096/// \brief Simple precedence-based parser for binary/ternary operators.
Chris Lattnercde626a2006-08-12 08:13:25 +000097///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000098/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
James Dennett3d5e4592012-06-17 04:36:28 +0000107/// \verbatim
Sebastian Redl112a97662009-02-07 00:15:38 +0000108/// pm-expression: [C++ 5.5]
109/// cast-expression
110/// pm-expression '.*' cast-expression
111/// pm-expression '->*' cast-expression
112///
Chris Lattnercde626a2006-08-12 08:13:25 +0000113/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl112a97662009-02-07 00:15:38 +0000114/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000115/// cast-expression
116/// multiplicative-expression '*' cast-expression
117/// multiplicative-expression '/' cast-expression
118/// multiplicative-expression '%' cast-expression
119///
120/// additive-expression: [C99 6.5.6]
121/// multiplicative-expression
122/// additive-expression '+' multiplicative-expression
123/// additive-expression '-' multiplicative-expression
124///
125/// shift-expression: [C99 6.5.7]
126/// additive-expression
127/// shift-expression '<<' additive-expression
128/// shift-expression '>>' additive-expression
129///
130/// relational-expression: [C99 6.5.8]
131/// shift-expression
132/// relational-expression '<' shift-expression
133/// relational-expression '>' shift-expression
134/// relational-expression '<=' shift-expression
135/// relational-expression '>=' shift-expression
136///
137/// equality-expression: [C99 6.5.9]
138/// relational-expression
139/// equality-expression '==' relational-expression
140/// equality-expression '!=' relational-expression
141///
142/// AND-expression: [C99 6.5.10]
143/// equality-expression
144/// AND-expression '&' equality-expression
145///
146/// exclusive-OR-expression: [C99 6.5.11]
147/// AND-expression
148/// exclusive-OR-expression '^' AND-expression
149///
150/// inclusive-OR-expression: [C99 6.5.12]
151/// exclusive-OR-expression
152/// inclusive-OR-expression '|' exclusive-OR-expression
153///
154/// logical-AND-expression: [C99 6.5.13]
155/// inclusive-OR-expression
156/// logical-AND-expression '&&' inclusive-OR-expression
157///
158/// logical-OR-expression: [C99 6.5.14]
159/// logical-AND-expression
160/// logical-OR-expression '||' logical-AND-expression
161///
162/// conditional-expression: [C99 6.5.15]
163/// logical-OR-expression
164/// logical-OR-expression '?' expression ':' conditional-expression
165/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl1a99f442009-04-16 17:51:27 +0000166/// [C++] the third operand is an assignment-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000167///
168/// assignment-expression: [C99 6.5.16]
169/// conditional-expression
170/// unary-expression assignment-operator assignment-expression
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000171/// [C++] throw-expression [C++ 15]
Chris Lattnercde626a2006-08-12 08:13:25 +0000172///
173/// assignment-operator: one of
174/// = *= /= %= += -= <<= >>= &= ^= |=
175///
176/// expression: [C99 6.5.17]
Douglas Gregor968f23a2011-01-03 19:31:53 +0000177/// assignment-expression ...[opt]
178/// expression ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +0000179/// \endverbatim
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000180ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
181 ExprResult LHS(ParseAssignmentExpression(isTypeCast));
Sebastian Redl90893182008-12-11 22:33:27 +0000182 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Chris Lattnercde626a2006-08-12 08:13:25 +0000183}
184
Mike Stump11289f42009-09-09 15:08:12 +0000185/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000186/// Current token is an Identifier and is not a 'try'. This
James Dennettf44874f2012-06-15 06:52:33 +0000187/// routine is necessary to disambiguate \@try-statement from,
188/// for example, \@encode-expression.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000189///
John McCalldadc5752010-08-24 06:29:42 +0000190ExprResult
Sebastian Redl90893182008-12-11 22:33:27 +0000191Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000192 ExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redl90893182008-12-11 22:33:27 +0000193 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000194}
195
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000196/// This routine is called when a leading '__extension__' is seen and
197/// consumed. This is necessary because the token gets consumed in the
198/// process of disambiguating between an expression and a declaration.
John McCalldadc5752010-08-24 06:29:42 +0000199ExprResult
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000200Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000201 ExprResult LHS(true);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000202 {
203 // Silence extension warnings in the sub-expression
204 ExtensionRAIIObject O(Diags);
205
206 LHS = ParseCastExpression(false);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000207 }
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000208
Douglas Gregor29d907d2010-09-17 22:25:06 +0000209 if (!LHS.isInvalid())
210 LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
211 LHS.take());
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000212
Douglas Gregor29d907d2010-09-17 22:25:06 +0000213 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000214}
215
James Dennett3d5e4592012-06-17 04:36:28 +0000216/// \brief Parse an expr that doesn't include (top-level) commas.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000217ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000218 if (Tok.is(tok::code_completion)) {
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000219 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000220 cutOffParsing();
221 return ExprError();
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000222 }
223
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000224 if (Tok.is(tok::kw_throw))
Sebastian Redld65cea82008-12-11 22:51:44 +0000225 return ParseThrowExpression();
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000226
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000227 ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
228 /*isAddressOfOperand=*/false,
229 isTypeCast);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000230 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000231}
232
James Dennett3d5e4592012-06-17 04:36:28 +0000233/// \brief Parse an assignment expression where part of an Objective-C message
234/// send has already been parsed.
235///
236/// In this case \p LBracLoc indicates the location of the '[' of the message
237/// send, and either \p ReceiverName or \p ReceiverExpr is non-null indicating
238/// the receiver of the message.
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000239///
240/// Since this handles full assignment-expression's, it handles postfix
241/// expressions and other binary operators for these expressions as well.
John McCalldadc5752010-08-24 06:29:42 +0000242ExprResult
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000243Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000244 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +0000245 ParsedType ReceiverType,
John McCallb268a282010-08-23 23:25:46 +0000246 Expr *ReceiverExpr) {
John McCalldadc5752010-08-24 06:29:42 +0000247 ExprResult R
John McCallb268a282010-08-23 23:25:46 +0000248 = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
249 ReceiverType, ReceiverExpr);
Douglas Gregoreda7e542010-09-18 01:28:11 +0000250 R = ParsePostfixExpressionSuffix(R);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000251 return ParseRHSOfBinaryExpression(R, prec::Assignment);
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000252}
253
254
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000255ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
Richard Smith764d2fe2011-12-20 02:08:33 +0000256 // C++03 [basic.def.odr]p2:
Mike Stump11289f42009-09-09 15:08:12 +0000257 // An expression is potentially evaluated unless it appears where an
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000258 // integral constant expression is required (see 5.19) [...].
Richard Smith764d2fe2011-12-20 02:08:33 +0000259 // C++98 and C++11 have no such rule, but this is only a defect in C++98.
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000260 EnterExpressionEvaluationContext Unevaluated(Actions,
Richard Smith764d2fe2011-12-20 02:08:33 +0000261 Sema::ConstantEvaluated);
Mike Stump11289f42009-09-09 15:08:12 +0000262
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000263 ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
Eli Friedmanc6237c62012-02-29 03:16:56 +0000264 ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
265 return Actions.ActOnConstantExpression(Res);
Chris Lattner3b561a32006-08-13 00:12:11 +0000266}
267
James Dennett3d5e4592012-06-17 04:36:28 +0000268/// \brief Parse a binary expression that starts with \p LHS and has a
269/// precedence of at least \p MinPrec.
John McCalldadc5752010-08-24 06:29:42 +0000270ExprResult
271Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000272 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
273 GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000274 getLangOpts().CPlusPlus0x);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000275 SourceLocation ColonLoc;
276
Chris Lattnercde626a2006-08-12 08:13:25 +0000277 while (1) {
278 // If this token has a lower precedence than we are allowed to parse (e.g.
279 // because we are called recursively, or because the token is not a binop),
280 // then we are done!
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000281 if (NextTokPrec < MinPrec)
Sebastian Redl90893182008-12-11 22:33:27 +0000282 return move(LHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000283
284 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000285 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000286 ConsumeToken();
Sebastian Redl112a97662009-02-07 00:15:38 +0000287
Chris Lattner96c3deb2006-08-12 17:13:08 +0000288 // Special case handling for the ternary operator.
John McCalldadc5752010-08-24 06:29:42 +0000289 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000290 if (NextTokPrec == prec::Conditional) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000291 if (Tok.isNot(tok::colon)) {
Chris Lattner244b96b2009-12-10 02:02:58 +0000292 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
293 ColonProtectionRAIIObject X(*this);
294
Chris Lattner96c3deb2006-08-12 17:13:08 +0000295 // Handle this production specially:
296 // logical-OR-expression '?' expression ':' conditional-expression
297 // In particular, the RHS of the '?' is 'expression', not
298 // 'logical-OR-expression' as we might expect.
299 TernaryMiddle = ParseExpression();
Douglas Gregorec06c122010-09-17 22:41:34 +0000300 if (TernaryMiddle.isInvalid()) {
301 LHS = ExprError();
302 TernaryMiddle = 0;
303 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000304 } else {
305 // Special case handling of "X ? Y : Z" where Y is empty:
306 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redlc13f2682008-12-09 20:22:58 +0000307 TernaryMiddle = 0;
Chris Lattner96c3deb2006-08-12 17:13:08 +0000308 Diag(Tok, diag::ext_gnu_conditional_expr);
309 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000310
Chris Lattner0151b7e2010-04-20 21:33:39 +0000311 if (Tok.is(tok::colon)) {
312 // Eat the colon.
313 ColonLoc = ConsumeToken();
314 } else {
Chandler Carruth0b5cf7c2011-07-26 05:19:46 +0000315 // Otherwise, we're missing a ':'. Assume that this was a typo that
316 // the user forgot. If we're not in a macro expansion, we can suggest
317 // a fixit hint. If there were two spaces before the current token,
Chris Lattnerfb585152010-05-24 22:31:37 +0000318 // suggest inserting the colon in between them, otherwise insert ": ".
319 SourceLocation FILoc = Tok.getLocation();
320 const char *FIText = ": ";
Argyrios Kyrtzidis3ea4adb2011-06-24 17:28:29 +0000321 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis1b07c342012-01-19 15:59:08 +0000322 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
323 assert(FILoc.isFileID());
Chris Lattnerfb585152010-05-24 22:31:37 +0000324 bool IsInvalid = false;
325 const char *SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000326 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000327 if (!IsInvalid && *SourcePtr == ' ') {
328 SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000329 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000330 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000331 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattnerfb585152010-05-24 22:31:37 +0000332 FIText = ":";
333 }
334 }
335 }
336
Ted Kremeneke6013652010-04-12 22:10:35 +0000337 Diag(Tok, diag::err_expected_colon)
Chris Lattnerfb585152010-05-24 22:31:37 +0000338 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner03c40412008-11-23 23:17:07 +0000339 Diag(OpToken, diag::note_matching) << "?";
Chris Lattner0151b7e2010-04-20 21:33:39 +0000340 ColonLoc = Tok.getLocation();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000341 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000342 }
Fariborz Jahanian9a14b842009-10-23 21:01:39 +0000343
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000344 // Code completion for the right-hand side of an assignment expression
345 // goes through a special hook that takes the left-hand side into account.
346 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000347 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000348 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000349 return ExprError();
350 }
351
Chris Lattner96c3deb2006-08-12 17:13:08 +0000352 // Parse another leaf here for the RHS of the operator.
Sebastian Redl1a99f442009-04-16 17:51:27 +0000353 // ParseCastExpression works here because all RHS expressions in C have it
354 // as a prefix, at least. However, in C++, an assignment-expression could
355 // be a throw-expression, which is not a valid cast-expression.
356 // Therefore we need some special-casing here.
357 // Also note that the third operand of the conditional operator is
Richard Smith9a6403a2012-02-26 23:40:27 +0000358 // an assignment-expression in C++, and in C++11, we can have a
Richard Smith5e0cac72012-03-01 02:59:17 +0000359 // braced-init-list on the RHS of an assignment. For better diagnostics,
360 // parse as if we were allowed braced-init-lists everywhere, and check that
361 // they only appear on the RHS of assignments later.
John McCalldadc5752010-08-24 06:29:42 +0000362 ExprResult RHS;
Richard Smithebcd2352012-03-01 07:10:06 +0000363 bool RHSIsInitList = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000364 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith9a6403a2012-02-26 23:40:27 +0000365 RHS = ParseBraceInitializer();
Richard Smithebcd2352012-03-01 07:10:06 +0000366 RHSIsInitList = true;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000367 } else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional)
Sebastian Redl1a99f442009-04-16 17:51:27 +0000368 RHS = ParseAssignmentExpression();
Richard Smith5e0cac72012-03-01 02:59:17 +0000369 else
Sebastian Redl1a99f442009-04-16 17:51:27 +0000370 RHS = ParseCastExpression(false);
Chris Lattnercde626a2006-08-12 08:13:25 +0000371
Douglas Gregor29d907d2010-09-17 22:25:06 +0000372 if (RHS.isInvalid())
373 LHS = ExprError();
374
Chris Lattnercde626a2006-08-12 08:13:25 +0000375 // Remember the precedence of this operator and get the precedence of the
376 // operator immediately to the right of the RHS.
Douglas Gregor8d4de672010-04-21 22:36:40 +0000377 prec::Level ThisPrec = NextTokPrec;
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000378 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000379 getLangOpts().CPlusPlus0x);
Chris Lattner89d53752006-08-12 17:18:19 +0000380
381 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000382 bool isRightAssoc = ThisPrec == prec::Conditional ||
383 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000384
385 // Get the precedence of the operator to the right of the RHS. If it binds
386 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000387 if (ThisPrec < NextTokPrec ||
388 (ThisPrec == NextTokPrec && isRightAssoc)) {
Richard Smithebcd2352012-03-01 07:10:06 +0000389 if (!RHS.isInvalid() && RHSIsInitList) {
390 Diag(Tok, diag::err_init_list_bin_op)
391 << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get());
392 RHS = ExprError();
Richard Smith5e0cac72012-03-01 02:59:17 +0000393 }
Chris Lattner89d53752006-08-12 17:18:19 +0000394 // If this is left-associative, only parse things on the RHS that bind
395 // more tightly than the current operator. If it is left-associative, it
396 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
397 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl511ed552008-11-25 22:21:31 +0000398 // The function takes ownership of the RHS.
Douglas Gregor29d907d2010-09-17 22:25:06 +0000399 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor8d4de672010-04-21 22:36:40 +0000400 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Richard Smithebcd2352012-03-01 07:10:06 +0000401 RHSIsInitList = false;
Douglas Gregor29d907d2010-09-17 22:25:06 +0000402
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000403 if (RHS.isInvalid())
Douglas Gregor29d907d2010-09-17 22:25:06 +0000404 LHS = ExprError();
Chris Lattnercde626a2006-08-12 08:13:25 +0000405
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000406 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000407 getLangOpts().CPlusPlus0x);
Chris Lattnercde626a2006-08-12 08:13:25 +0000408 }
409 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl511ed552008-11-25 22:21:31 +0000410
Richard Smithebcd2352012-03-01 07:10:06 +0000411 if (!RHS.isInvalid() && RHSIsInitList) {
Richard Smith5e0cac72012-03-01 02:59:17 +0000412 if (ThisPrec == prec::Assignment) {
413 Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
Richard Smithebcd2352012-03-01 07:10:06 +0000414 << Actions.getExprRange(RHS.get());
Richard Smith5e0cac72012-03-01 02:59:17 +0000415 } else {
416 Diag(OpToken, diag::err_init_list_bin_op)
Richard Smithebcd2352012-03-01 07:10:06 +0000417 << /*RHS*/1 << PP.getSpelling(OpToken)
418 << Actions.getExprRange(RHS.get());
Richard Smith5e0cac72012-03-01 02:59:17 +0000419 LHS = ExprError();
420 }
421 }
422
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000423 if (!LHS.isInvalid()) {
Chris Lattner319079c2007-08-31 05:01:50 +0000424 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor87f95b02009-02-26 21:00:50 +0000425 if (TernaryMiddle.isInvalid()) {
426 // If we're using '>>' as an operator within a template
427 // argument list (in C++98), suggest the addition of
428 // parentheses so that the code remains well-formed in C++0x.
429 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
430 SuggestParentheses(OpToken.getLocation(),
431 diag::warn_cxx0x_right_shift_in_template_arg,
432 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
433 Actions.getExprRange(RHS.get()).getEnd()));
434
Douglas Gregor0be31a22010-07-02 17:43:08 +0000435 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCallb268a282010-08-23 23:25:46 +0000436 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor87f95b02009-02-26 21:00:50 +0000437 } else
Steve Naroff83895f72007-09-16 03:34:24 +0000438 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000439 LHS.take(), TernaryMiddle.take(),
440 RHS.take());
Chris Lattner319079c2007-08-31 05:01:50 +0000441 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000442 }
443}
444
James Dennett3d5e4592012-06-17 04:36:28 +0000445/// \brief Parse a cast-expression, or, if \p isUnaryExpression is true,
446/// parse a unary-expression.
447///
448/// \p isAddressOfOperand exists because an id-expression that is the
449/// operand of address-of gets special treatment due to member pointers.
Chris Lattnereaf06592006-08-11 02:02:23 +0000450///
John McCalldadc5752010-08-24 06:29:42 +0000451ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000452 bool isAddressOfOperand,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000453 TypeCastState isTypeCast) {
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000454 bool NotCastExpr;
John McCalldadc5752010-08-24 06:29:42 +0000455 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor29d907d2010-09-17 22:25:06 +0000456 isAddressOfOperand,
457 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +0000458 isTypeCast);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000459 if (NotCastExpr)
460 Diag(Tok, diag::err_expected_expression);
461 return move(Res);
462}
463
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000464namespace {
465class CastExpressionIdValidator : public CorrectionCandidateCallback {
466 public:
467 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
468 : AllowNonTypes(AllowNonTypes) {
469 WantTypeSpecifiers = AllowTypes;
470 }
471
472 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
473 NamedDecl *ND = candidate.getCorrectionDecl();
474 if (!ND)
475 return candidate.isKeyword();
476
477 if (isa<TypeDecl>(ND))
478 return WantTypeSpecifiers;
479 return AllowNonTypes;
480 }
481
482 private:
483 bool AllowNonTypes;
484};
485}
486
James Dennett3d5e4592012-06-17 04:36:28 +0000487/// \brief Parse a cast-expression, or, if \pisUnaryExpression is true, parse
488/// a unary-expression.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000489///
James Dennett3d5e4592012-06-17 04:36:28 +0000490/// \p isAddressOfOperand exists because an id-expression that is the operand
491/// of address-of gets special treatment due to member pointers. NotCastExpr
492/// is set to true if the token is not the start of a cast-expression, and no
493/// diagnostic is emitted in this case.
494///
495/// \verbatim
Chris Lattner4564bc12006-08-10 23:14:52 +0000496/// cast-expression: [C99 6.5.4]
497/// unary-expression
498/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000499///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000500/// unary-expression: [C99 6.5.3]
501/// postfix-expression
502/// '++' unary-expression
503/// '--' unary-expression
504/// unary-operator cast-expression
505/// 'sizeof' unary-expression
506/// 'sizeof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000507/// [C++11] 'sizeof' '...' '(' identifier ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000508/// [GNU] '__alignof' unary-expression
509/// [GNU] '__alignof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000510/// [C++11] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000511/// [GNU] '&&' identifier
Richard Smithd67aea22012-03-06 03:21:47 +0000512/// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7]
Sebastian Redlbd150f42008-11-21 19:14:01 +0000513/// [C++] new-expression
514/// [C++] delete-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000515///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000516/// unary-operator: one of
517/// '&' '*' '+' '-' '~' '!'
518/// [GNU] '__extension__' '__real' '__imag'
519///
Chris Lattner52a99e52006-08-10 20:56:00 +0000520/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000521/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000522/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000523/// constant
524/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000525/// [C++] boolean-literal [C++ 2.13.5]
Richard Smithd67aea22012-03-06 03:21:47 +0000526/// [C++11] 'nullptr' [C++11 2.14.7]
527/// [C++11] user-defined-literal
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000528/// '(' expression ')'
Benjamin Kramere56f3932011-12-23 17:00:35 +0000529/// [C11] generic-selection
Chris Lattner52a99e52006-08-10 20:56:00 +0000530/// '__func__' [C99 6.4.2.2]
531/// [GNU] '__FUNCTION__'
532/// [GNU] '__PRETTY_FUNCTION__'
533/// [GNU] '(' compound-statement ')'
534/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
535/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
536/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
537/// assign-expr ')'
538/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000539/// [GNU] '__null'
Mike Stump11289f42009-09-09 15:08:12 +0000540/// [OBJC] '[' objc-message-expr ']'
James Dennettf44874f2012-06-15 06:52:33 +0000541/// [OBJC] '\@selector' '(' objc-selector-arg ')'
542/// [OBJC] '\@protocol' '(' identifier ')'
543/// [OBJC] '\@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000544/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000545/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000546/// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3]
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000547/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000548/// [C++11] typename-specifier braced-init-list [C++11 5.2.3]
Bill Wendlinga6930032007-06-29 18:21:34 +0000549/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
550/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
551/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
552/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000553/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
554/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000555/// [C++] 'this' [C++ 9.3.2]
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000556/// [G++] unary-type-trait '(' type-id ')'
557/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley6242b6a2011-04-28 00:16:57 +0000558/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff0ac012832008-08-28 19:20:44 +0000559/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000560///
561/// constant: [C99 6.4.4]
562/// integer-constant
563/// floating-constant
564/// enumeration-constant -> identifier
565/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000566///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000567/// id-expression: [C++ 5.1]
568/// unqualified-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000569/// qualified-id
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000570///
571/// unqualified-id: [C++ 5.1]
572/// identifier
573/// operator-function-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000574/// conversion-function-id
575/// '~' class-name
576/// template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000577///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000578/// new-expression: [C++ 5.3.4]
579/// '::'[opt] 'new' new-placement[opt] new-type-id
580/// new-initializer[opt]
581/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
582/// new-initializer[opt]
583///
584/// delete-expression: [C++ 5.3.5]
585/// '::'[opt] 'delete' cast-expression
586/// '::'[opt] 'delete' '[' ']' cast-expression
587///
John Wiegley65497cc2011-04-27 23:09:49 +0000588/// [GNU/Embarcadero] unary-type-trait:
589/// '__is_arithmetic'
590/// '__is_floating_point'
591/// '__is_integral'
592/// '__is_lvalue_expr'
593/// '__is_rvalue_expr'
594/// '__is_complete_type'
595/// '__is_void'
596/// '__is_array'
597/// '__is_function'
598/// '__is_reference'
599/// '__is_lvalue_reference'
600/// '__is_rvalue_reference'
601/// '__is_fundamental'
602/// '__is_object'
603/// '__is_scalar'
604/// '__is_compound'
605/// '__is_pointer'
606/// '__is_member_object_pointer'
607/// '__is_member_function_pointer'
608/// '__is_member_pointer'
609/// '__is_const'
610/// '__is_volatile'
611/// '__is_trivial'
612/// '__is_standard_layout'
613/// '__is_signed'
614/// '__is_unsigned'
615///
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000616/// [GNU] unary-type-trait:
Sebastian Redl7dcb1552010-08-31 04:59:00 +0000617/// '__has_nothrow_assign'
618/// '__has_nothrow_copy'
619/// '__has_nothrow_constructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000620/// '__has_trivial_assign' [TODO]
621/// '__has_trivial_copy' [TODO]
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000622/// '__has_trivial_constructor'
Anders Carlsson6dc35752009-04-17 02:34:54 +0000623/// '__has_trivial_destructor'
Sebastian Redlb469afb2010-09-02 23:19:42 +0000624/// '__has_virtual_destructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000625/// '__is_abstract' [TODO]
626/// '__is_class'
627/// '__is_empty' [TODO]
628/// '__is_enum'
Douglas Gregordca70af2011-12-03 18:14:24 +0000629/// '__is_final'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000630/// '__is_pod'
631/// '__is_polymorphic'
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000632/// '__is_trivial'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000633/// '__is_union'
634///
Alexis Huntd9a5cc12011-05-13 00:31:07 +0000635/// [Clang] unary-type-trait:
636/// '__trivially_copyable'
637///
Douglas Gregor8006e762011-01-27 20:28:01 +0000638/// binary-type-trait:
639/// [GNU] '__is_base_of'
640/// [MS] '__is_convertible_to'
John Wiegley65497cc2011-04-27 23:09:49 +0000641/// '__is_convertible'
642/// '__is_same'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000643///
John Wiegley6242b6a2011-04-28 00:16:57 +0000644/// [Embarcadero] array-type-trait:
645/// '__array_rank'
646/// '__array_extent'
647///
John Wiegleyf9f65842011-04-25 06:54:41 +0000648/// [Embarcadero] expression-trait:
649/// '__is_lvalue_expr'
650/// '__is_rvalue_expr'
James Dennett3d5e4592012-06-17 04:36:28 +0000651/// \endverbatim
John Wiegleyf9f65842011-04-25 06:54:41 +0000652///
John McCalldadc5752010-08-24 06:29:42 +0000653ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl22e3a932010-09-10 20:55:37 +0000654 bool isAddressOfOperand,
655 bool &NotCastExpr,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000656 TypeCastState isTypeCast) {
John McCalldadc5752010-08-24 06:29:42 +0000657 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000658 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000659 NotCastExpr = false;
Mike Stump11289f42009-09-09 15:08:12 +0000660
Chris Lattner81b576e2006-08-11 02:13:20 +0000661 // This handles all of cast-expression, unary-expression, postfix-expression,
662 // and primary-expression. We handle them together like this for efficiency
663 // and to simplify handling of an expression starting with a '(' token: which
664 // may be one of a parenthesized expression, cast-expression, compound literal
665 // expression, or statement expression.
666 //
667 // If the parsed tokens consist of a primary-expression, the cases below
John McCallb268a282010-08-23 23:25:46 +0000668 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
669 // to handle the postfix expression suffixes. Cases that cannot be followed
670 // by postfix exprs should return without invoking
671 // ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000672 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000673 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000674 // If this expression is limited to being a unary-expression, the parent can
675 // not start a cast expression.
676 ParenParseOption ParenExprType =
David Blaikiebbafb8a2012-03-11 07:00:24 +0000677 (isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallba7bf592010-08-24 05:47:05 +0000678 ParsedType CastTy;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000679 SourceLocation RParenLoc;
Chris Lattner3c674cf2009-12-10 02:08:07 +0000680
681 {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000682 // The inside of the parens don't need to be a colon protected scope, and
683 // isn't immediately a message send.
Chris Lattner3c674cf2009-12-10 02:08:07 +0000684 ColonProtectionRAIIObject X(*this, false);
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000685
Chris Lattner3c674cf2009-12-10 02:08:07 +0000686 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000687 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner3c674cf2009-12-10 02:08:07 +0000688 }
Mike Stump11289f42009-09-09 15:08:12 +0000689
Chris Lattner81b576e2006-08-11 02:13:20 +0000690 switch (ParenExprType) {
691 case SimpleExpr: break; // Nothing else to do.
692 case CompoundStmt: break; // Nothing else to do.
693 case CompoundLiteral:
694 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
695 // postfix-expression exist, parse them now.
696 break;
697 case CastExpr:
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000698 // We have parsed the cast-expression and no postfix-expr pieces are
699 // following.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000700 return move(Res);
Chris Lattner81b576e2006-08-11 02:13:20 +0000701 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000702
John McCallb268a282010-08-23 23:25:46 +0000703 break;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000704 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000705
Chris Lattner52a99e52006-08-10 20:56:00 +0000706 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000707 case tok::numeric_constant:
708 // constant: integer-constant
709 // constant: floating-constant
Sebastian Redl59b5e512008-12-11 21:36:32 +0000710
Richard Smithbcc22fc2012-03-09 08:00:36 +0000711 Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000712 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000713 break;
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000714
Bill Wendling4073ed52007-02-13 01:51:42 +0000715 case tok::kw_true:
716 case tok::kw_false:
Sebastian Redld65cea82008-12-11 22:51:44 +0000717 return ParseCXXBoolLiteral();
Ted Kremeneke65b0862012-03-06 20:05:56 +0000718
719 case tok::kw___objc_yes:
720 case tok::kw___objc_no:
721 return ParseObjCBoolLiteral();
Bill Wendling4073ed52007-02-13 01:51:42 +0000722
Sebastian Redl576fd422009-05-10 18:38:11 +0000723 case tok::kw_nullptr:
Richard Smithb15c11c2011-10-17 23:06:20 +0000724 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl576fd422009-05-10 18:38:11 +0000725 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
726
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000727 case tok::annot_primary_expr:
728 assert(Res.get() == 0 && "Stray primary-expression annotation?");
729 Res = getExprAnnotation(Tok);
730 ConsumeToken();
731 break;
732
David Blaikie15a430a2011-12-04 05:04:18 +0000733 case tok::kw_decltype:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000734 case tok::identifier: { // primary-expression: identifier
735 // unqualified-id: identifier
736 // constant: enumeration-constant
Chris Lattnera8a3f732009-01-06 05:06:21 +0000737 // Turn a potentially qualified name into a annot_typename or
Chris Lattner122db262009-01-04 22:52:14 +0000738 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000739 if (getLangOpts().CPlusPlus) {
John McCall64fe2332010-01-07 19:29:58 +0000740 // Avoid the unnecessary parse-time lookup in the common case
741 // where the syntax forbids a type.
742 const Token &Next = NextToken();
743 if (Next.is(tok::coloncolon) ||
744 (!ColonIsSacred && Next.is(tok::colon)) ||
745 Next.is(tok::less) ||
Sebastian Redl867f2282011-12-22 18:58:29 +0000746 Next.is(tok::l_paren) ||
747 Next.is(tok::l_brace)) {
John McCall64fe2332010-01-07 19:29:58 +0000748 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
749 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +0000750 return ExprError();
751 if (!Tok.is(tok::identifier))
John McCall64fe2332010-01-07 19:29:58 +0000752 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
753 }
Chris Lattner9a8968b2009-01-04 23:23:14 +0000754 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000755
Chris Lattner55662902009-10-25 17:04:48 +0000756 // Consume the identifier so that we can see if it is followed by a '(' or
757 // '.'.
758 IdentifierInfo &II = *Tok.getIdentifierInfo();
759 SourceLocation ILoc = ConsumeToken();
760
Chris Lattnera36ec422010-04-11 08:28:14 +0000761 // Support 'Class.property' and 'super.property' notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000762 if (getLangOpts().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000763 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattnercd963182010-04-12 06:20:33 +0000764 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000765 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000766 ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000767
Douglas Gregor36107ad2012-02-16 18:19:22 +0000768 // Allow either an identifier or the keyword 'class' (in C++).
769 if (Tok.isNot(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000770 !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattner55662902009-10-25 17:04:48 +0000771 Diag(Tok, diag::err_expected_property_name);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000772 return ExprError();
773 }
774 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
775 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000776
777 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
778 ILoc, PropertyLoc);
John McCallb268a282010-08-23 23:25:46 +0000779 break;
Steve Naroff9527bbf2009-03-09 21:12:44 +0000780 }
John McCall8d08b9b2010-08-27 09:08:28 +0000781
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000782 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregored0b69d2010-09-15 16:23:04 +0000783 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000784 // that identifier, this is probably a message send with a missing open
Douglas Gregored0b69d2010-09-15 16:23:04 +0000785 // bracket. Treat it as such.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000786 if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000787 getCurScope()->isInObjcMethodScope() &&
Douglas Gregored0b69d2010-09-15 16:23:04 +0000788 ((Tok.is(tok::identifier) &&
789 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
790 Tok.is(tok::code_completion))) {
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000791 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
792 0);
793 break;
794 }
795
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000796 // If we have an Objective-C class name followed by an identifier
797 // and either ':' or ']', this is an Objective-C class message
798 // send that's missing the opening '['. Recovery
799 // appropriately. Also take this path if we're performing code
800 // completion after an Objective-C class name.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000801 if (getLangOpts().ObjC1 &&
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000802 ((Tok.is(tok::identifier) && !InMessageExpression) ||
803 Tok.is(tok::code_completion))) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000804 const Token& Next = NextToken();
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000805 if (Tok.is(tok::code_completion) ||
806 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif433c9e12010-09-17 10:21:45 +0000807 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
808 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000809 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000810 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000811 DS.SetRangeStart(ILoc);
812 DS.SetRangeEnd(ILoc);
813 const char *PrevSpec = 0;
814 unsigned DiagID;
Gabor Greif433c9e12010-09-17 10:21:45 +0000815 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000816
817 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
818 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
819 DeclaratorInfo);
820 if (Ty.isInvalid())
821 break;
822
823 Res = ParseObjCMessageExpressionBody(SourceLocation(),
824 SourceLocation(),
825 Ty.get(), 0);
826 break;
827 }
828 }
829
John McCall8d08b9b2010-08-27 09:08:28 +0000830 // Make sure to pass down the right value for isAddressOfOperand.
831 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
832 isAddressOfOperand = false;
Chris Lattner55662902009-10-25 17:04:48 +0000833
Chris Lattnerac18be92006-11-20 06:49:47 +0000834 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
835 // need to know whether or not this identifier is a function designator or
836 // not.
Douglas Gregora121b752009-11-03 16:56:39 +0000837 UnqualifiedId Name;
838 CXXScopeSpec ScopeSpec;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000839 SourceLocation TemplateKWLoc;
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000840 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
841 isTypeCast != IsTypeCast);
Douglas Gregora121b752009-11-03 16:56:39 +0000842 Name.setIdentifier(&II, ILoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000843 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
844 Name, Tok.is(tok::l_paren),
845 isAddressOfOperand, &Validator);
John McCallb268a282010-08-23 23:25:46 +0000846 break;
Chris Lattnerac18be92006-11-20 06:49:47 +0000847 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000848 case tok::char_constant: // constant: character-constant
Douglas Gregorfb65e592011-07-27 05:40:30 +0000849 case tok::wide_char_constant:
850 case tok::utf16_char_constant:
851 case tok::utf32_char_constant:
Richard Smithbcc22fc2012-03-09 08:00:36 +0000852 Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope());
Steve Naroffae4143e2007-04-26 20:39:23 +0000853 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000854 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000855 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
856 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Nico Weber3a691a32012-06-23 02:07:59 +0000857 case tok::kw_L__FUNCTION__: // primary-expression: L__FUNCTION__ [MS]
Chris Lattner52a99e52006-08-10 20:56:00 +0000858 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000859 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000860 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000861 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000862 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000863 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000864 case tok::utf8_string_literal:
865 case tok::utf16_string_literal:
866 case tok::utf32_string_literal:
Richard Smithd67aea22012-03-06 03:21:47 +0000867 Res = ParseStringLiteralExpression(true);
John McCallb268a282010-08-23 23:25:46 +0000868 break;
Benjamin Kramere56f3932011-12-23 17:00:35 +0000869 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbourne91147592011-04-15 00:35:48 +0000870 Res = ParseGenericSelectionExpression();
871 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000872 case tok::kw___builtin_va_arg:
873 case tok::kw___builtin_offsetof:
874 case tok::kw___builtin_choose_expr:
Tanya Lattner55808c12011-06-04 00:47:47 +0000875 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redl90893182008-12-11 22:33:27 +0000876 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000877 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000878 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth904cb142011-07-08 04:59:44 +0000879
Douglas Gregora49ccfe2010-08-06 14:50:36 +0000880 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
881 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
882 // C++ [expr.unary] has:
883 // unary-expression:
884 // ++ cast-expression
885 // -- cast-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000886 SourceLocation SavedLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000887 Res = ParseCastExpression(!getLangOpts().CPlusPlus);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000888 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000889 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000890 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000891 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000892 case tok::amp: { // unary-expression: '&' cast-expression
893 // Special treatment because of member pointers
894 SourceLocation SavedLoc = ConsumeToken();
895 Res = ParseCastExpression(false, true);
896 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000897 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000898 return move(Res);
899 }
900
Chris Lattner81b576e2006-08-11 02:13:20 +0000901 case tok::star: // unary-expression: '*' cast-expression
902 case tok::plus: // unary-expression: '+' cast-expression
903 case tok::minus: // unary-expression: '-' cast-expression
904 case tok::tilde: // unary-expression: '~' cast-expression
905 case tok::exclaim: // unary-expression: '!' cast-expression
906 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000907 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000908 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000909 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000910 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000911 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000912 return move(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000913 }
914
Chris Lattnerc43926f2008-02-02 20:20:10 +0000915 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
916 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000917 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000918 SourceLocation SavedLoc = ConsumeToken();
919 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000920 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000921 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000922 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000923 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000924 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
925 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000926 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000927 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
928 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000929 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournee190dee2011-03-11 19:24:49 +0000930 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
931 return ParseUnaryExprOrTypeTraitExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000932 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000933 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000934 if (Tok.isNot(tok::identifier))
935 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000936
Chris Lattner9ba479b2011-02-18 21:16:39 +0000937 if (getCurScope()->getFnParent() == 0)
938 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
939
Chris Lattnereefa10e2007-05-28 06:56:27 +0000940 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000941 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
942 Tok.getLocation());
943 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Chris Lattner14a1b642006-10-15 22:33:58 +0000944 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000945 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000946 }
Chris Lattner29375652006-12-04 18:06:35 +0000947 case tok::kw_const_cast:
948 case tok::kw_dynamic_cast:
949 case tok::kw_reinterpret_cast:
950 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000951 Res = ParseCXXCasts();
John McCallb268a282010-08-23 23:25:46 +0000952 break;
Sebastian Redlc4704762008-11-11 11:37:55 +0000953 case tok::kw_typeid:
954 Res = ParseCXXTypeid();
John McCallb268a282010-08-23 23:25:46 +0000955 break;
Francois Pichet9f4f2072010-09-08 12:20:18 +0000956 case tok::kw___uuidof:
957 Res = ParseCXXUuidof();
958 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000959 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000960 Res = ParseCXXThis();
John McCallb268a282010-08-23 23:25:46 +0000961 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000962
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000963 case tok::annot_typename:
964 if (isStartOfObjCClassMessageMissingOpenBracket()) {
965 ParsedType Type = getTypeAnnotation(Tok);
966
967 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000968 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000969 DS.SetRangeStart(Tok.getLocation());
970 DS.SetRangeEnd(Tok.getLastLoc());
971
972 const char *PrevSpec = 0;
973 unsigned DiagID;
Nico Weber77430342010-11-22 10:30:56 +0000974 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
975 PrevSpec, DiagID, Type);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000976
977 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
978 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
979 if (Ty.isInvalid())
980 break;
981
982 ConsumeToken();
983 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
984 Ty.get(), 0);
985 break;
986 }
987 // Fall through
988
David Blaikie25896afb2012-01-24 05:47:35 +0000989 case tok::annot_decltype:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000990 case tok::kw_char:
991 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000992 case tok::kw_char16_t:
993 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000994 case tok::kw_bool:
995 case tok::kw_short:
996 case tok::kw_int:
997 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +0000998 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000999 case tok::kw___int128:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001000 case tok::kw_signed:
1001 case tok::kw_unsigned:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001002 case tok::kw_half:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001003 case tok::kw_float:
1004 case tok::kw_double:
1005 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +00001006 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +00001007 case tok::kw_typeof:
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001008 case tok::kw___vector: {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001009 if (!getLangOpts().CPlusPlus) {
Chris Lattner8a38aa82009-01-04 22:28:21 +00001010 Diag(Tok, diag::err_expected_expression);
1011 return ExprError();
1012 }
Eli Friedman6d692cc2009-06-11 00:33:41 +00001013
1014 if (SavedKind == tok::kw_typename) {
1015 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001016 // typename-specifier braced-init-list
John McCall1f476a12010-02-26 08:45:28 +00001017 if (TryAnnotateTypeOrScopeToken())
Eli Friedman6d692cc2009-06-11 00:33:41 +00001018 return ExprError();
1019 }
1020
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001021 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001022 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001023 //
John McCall084e83d2011-03-24 11:26:52 +00001024 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001025 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redl3da34892011-06-05 12:23:16 +00001026 if (Tok.isNot(tok::l_paren) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001027 (!getLangOpts().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl59b5e512008-12-11 21:36:32 +00001028 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1029 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001030
Richard Smith5d164bc2011-10-15 05:09:34 +00001031 if (Tok.is(tok::l_brace))
1032 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1033
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001034 Res = ParseCXXTypeConstructExpression(DS);
John McCallb268a282010-08-23 23:25:46 +00001035 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001036 }
1037
Douglas Gregor7df89f52010-02-05 19:11:37 +00001038 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001039 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1040 // (We can end up in this situation after tentative parsing.)
1041 if (TryAnnotateTypeOrScopeToken())
1042 return ExprError();
1043 if (!Tok.is(tok::annot_cxxscope))
1044 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001045 NotCastExpr, isTypeCast);
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001046
Douglas Gregor7df89f52010-02-05 19:11:37 +00001047 Token Next = NextToken();
1048 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001049 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001050 if (TemplateId->Kind == TNK_Type_template) {
1051 // We have a qualified template-id that we know refers to a
1052 // type, translate it into a type and continue parsing as a
1053 // cast expression.
1054 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001055 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1056 /*EnteringContext=*/false);
Douglas Gregore7c20652011-03-02 00:47:37 +00001057 AnnotateTemplateIdTokenAsType();
Douglas Gregor7df89f52010-02-05 19:11:37 +00001058 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001059 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001060 }
1061 }
1062
1063 // Parse as an id-expression.
1064 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001065 break;
Douglas Gregor7df89f52010-02-05 19:11:37 +00001066 }
1067
1068 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001069 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001070 if (TemplateId->Kind == TNK_Type_template) {
1071 // We have a template-id that we know refers to a type,
1072 // translate it into a type and continue parsing as a cast
1073 // expression.
1074 AnnotateTemplateIdTokenAsType();
1075 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001076 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001077 }
1078
1079 // Fall through to treat the template-id as an id-expression.
1080 }
1081
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001082 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001083 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001084 break;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001085
Chris Lattner122db262009-01-04 22:52:14 +00001086 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001087 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1088 // annotates the token, tail recurse.
1089 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001090 return ExprError();
1091 if (!Tok.is(tok::coloncolon))
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001092 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1093
Chris Lattner122db262009-01-04 22:52:14 +00001094 // ::new -> [C++] new-expression
1095 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001096 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +00001097 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001098 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +00001099 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001100 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001101
Chris Lattner9a8968b2009-01-04 23:23:14 +00001102 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001103 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +00001104 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +00001105 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +00001106
Sebastian Redlbd150f42008-11-21 19:14:01 +00001107 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001108 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001109
1110 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001111 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001112
Sebastian Redl22e3a932010-09-10 20:55:37 +00001113 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smithb15c11c2011-10-17 23:06:20 +00001114 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001115 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001116 BalancedDelimiterTracker T(*this, tok::l_paren);
1117
1118 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl22e3a932010-09-10 20:55:37 +00001119 return ExprError();
Richard Smith764d2fe2011-12-20 02:08:33 +00001120 // C++11 [expr.unary.noexcept]p1:
Sebastian Redle56be2f72010-09-10 21:57:27 +00001121 // The noexcept operator determines whether the evaluation of its operand,
1122 // which is an unevaluated operand, can throw an exception.
1123 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001124 ExprResult Result = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001125
1126 T.consumeClose();
1127
Sebastian Redl22e3a932010-09-10 20:55:37 +00001128 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001129 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1130 Result.take(), T.getCloseLocation());
Sebastian Redl22e3a932010-09-10 20:55:37 +00001131 return move(Result);
1132 }
1133
Chandler Carruth79803482011-04-23 10:47:20 +00001134 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001135 case tok::kw___is_class:
Eli Friedmanc96d4962009-08-15 21:55:26 +00001136 case tok::kw___is_empty:
Chandler Carruth79803482011-04-23 10:47:20 +00001137 case tok::kw___is_enum:
Sebastian Redl79eba1c2009-12-03 00:13:20 +00001138 case tok::kw___is_literal:
John Wiegley65497cc2011-04-27 23:09:49 +00001139 case tok::kw___is_arithmetic:
1140 case tok::kw___is_integral:
1141 case tok::kw___is_floating_point:
1142 case tok::kw___is_complete_type:
1143 case tok::kw___is_void:
1144 case tok::kw___is_array:
1145 case tok::kw___is_function:
1146 case tok::kw___is_reference:
1147 case tok::kw___is_lvalue_reference:
1148 case tok::kw___is_rvalue_reference:
1149 case tok::kw___is_fundamental:
1150 case tok::kw___is_object:
1151 case tok::kw___is_scalar:
1152 case tok::kw___is_compound:
1153 case tok::kw___is_pointer:
1154 case tok::kw___is_member_object_pointer:
1155 case tok::kw___is_member_function_pointer:
1156 case tok::kw___is_member_pointer:
1157 case tok::kw___is_const:
1158 case tok::kw___is_volatile:
1159 case tok::kw___is_standard_layout:
1160 case tok::kw___is_signed:
1161 case tok::kw___is_unsigned:
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00001162 case tok::kw___is_literal_type:
Chandler Carruth79803482011-04-23 10:47:20 +00001163 case tok::kw___is_pod:
1164 case tok::kw___is_polymorphic:
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +00001165 case tok::kw___is_trivial:
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001166 case tok::kw___is_trivially_copyable:
Chandler Carruth79803482011-04-23 10:47:20 +00001167 case tok::kw___is_union:
Douglas Gregordca70af2011-12-03 18:14:24 +00001168 case tok::kw___is_final:
Anders Carlssonfe63dc52009-04-16 00:08:20 +00001169 case tok::kw___has_trivial_constructor:
Douglas Gregor79f83ed2009-07-23 23:49:00 +00001170 case tok::kw___has_trivial_copy:
1171 case tok::kw___has_trivial_assign:
Anders Carlsson6dc35752009-04-17 02:34:54 +00001172 case tok::kw___has_trivial_destructor:
Sebastian Redl7dcb1552010-08-31 04:59:00 +00001173 case tok::kw___has_nothrow_assign:
1174 case tok::kw___has_nothrow_copy:
1175 case tok::kw___has_nothrow_constructor:
Sebastian Redlb469afb2010-09-02 23:19:42 +00001176 case tok::kw___has_virtual_destructor:
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001177 return ParseUnaryTypeTrait();
1178
Francois Pichet34b21132010-12-08 22:35:30 +00001179 case tok::kw___builtin_types_compatible_p:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001180 case tok::kw___is_base_of:
John Wiegley65497cc2011-04-27 23:09:49 +00001181 case tok::kw___is_same:
1182 case tok::kw___is_convertible:
Douglas Gregor8006e762011-01-27 20:28:01 +00001183 case tok::kw___is_convertible_to:
Douglas Gregor1be329d2012-02-23 07:33:15 +00001184 case tok::kw___is_trivially_assignable:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001185 return ParseBinaryTypeTrait();
1186
Douglas Gregor29c42f22012-02-24 07:38:34 +00001187 case tok::kw___is_trivially_constructible:
1188 return ParseTypeTrait();
1189
John Wiegley6242b6a2011-04-28 00:16:57 +00001190 case tok::kw___array_rank:
1191 case tok::kw___array_extent:
1192 return ParseArrayTypeTrait();
1193
John Wiegleyf9f65842011-04-25 06:54:41 +00001194 case tok::kw___is_lvalue_expr:
1195 case tok::kw___is_rvalue_expr:
1196 return ParseExpressionTrait();
1197
Chris Lattner644e1b72007-10-03 22:03:06 +00001198 case tok::at: {
1199 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001200 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +00001201 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001202 case tok::caret:
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001203 Res = ParseBlockLiteralExpression();
1204 break;
1205 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001206 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001207 cutOffParsing();
1208 return ExprError();
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001209 }
Chris Lattner6bf1db12008-12-12 19:20:14 +00001210 case tok::l_square:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001211 if (getLangOpts().CPlusPlus0x) {
1212 if (getLangOpts().ObjC1) {
Eli Friedmanc7c97142012-01-04 02:40:39 +00001213 // C++11 lambda expressions and Objective-C message sends both start with a
1214 // square bracket. There are three possibilities here:
1215 // we have a valid lambda expression, we have an invalid lambda
1216 // expression, or we have something that doesn't appear to be a lambda.
1217 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001218 Res = TryParseLambdaExpression();
Eli Friedmanc7c97142012-01-04 02:40:39 +00001219 if (!Res.isInvalid() && !Res.get())
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001220 Res = ParseObjCMessageExpression();
1221 break;
1222 }
1223 Res = ParseLambdaExpression();
1224 break;
1225 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001226 if (getLangOpts().ObjC1) {
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001227 Res = ParseObjCMessageExpression();
1228 break;
1229 }
1230 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +00001231 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001232 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001233 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +00001234 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001235
John McCallb268a282010-08-23 23:25:46 +00001236 // These can be followed by postfix-expr pieces.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001237 return ParsePostfixExpressionSuffix(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +00001238}
1239
James Dennett3d5e4592012-06-17 04:36:28 +00001240/// \brief Once the leading part of a postfix-expression is parsed, this
1241/// method parses any suffixes that apply.
Chris Lattner20c6a452006-08-12 17:40:43 +00001242///
James Dennett3d5e4592012-06-17 04:36:28 +00001243/// \verbatim
Chris Lattner20c6a452006-08-12 17:40:43 +00001244/// postfix-expression: [C99 6.5.2]
1245/// primary-expression
1246/// postfix-expression '[' expression ']'
Sebastian Redl3da34892011-06-05 12:23:16 +00001247/// postfix-expression '[' braced-init-list ']'
Chris Lattner20c6a452006-08-12 17:40:43 +00001248/// postfix-expression '(' argument-expression-list[opt] ')'
1249/// postfix-expression '.' identifier
1250/// postfix-expression '->' identifier
1251/// postfix-expression '++'
1252/// postfix-expression '--'
1253/// '(' type-name ')' '{' initializer-list '}'
1254/// '(' type-name ')' '{' initializer-list ',' '}'
1255///
1256/// argument-expression-list: [C99 6.5.2]
Douglas Gregor968f23a2011-01-03 19:31:53 +00001257/// argument-expression ...[opt]
1258/// argument-expression-list ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +00001259/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001260ExprResult
1261Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001262 // Now that the primary-expression piece of the postfix-expression has been
1263 // parsed, see if there are any postfix-expression pieces here.
1264 SourceLocation Loc;
1265 while (1) {
1266 switch (Tok.getKind()) {
Douglas Gregored0b69d2010-09-15 16:23:04 +00001267 case tok::code_completion:
1268 if (InMessageExpression)
1269 return move(LHS);
1270
Douglas Gregoreda7e542010-09-18 01:28:11 +00001271 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001272 cutOffParsing();
1273 return ExprError();
Douglas Gregored0b69d2010-09-15 16:23:04 +00001274
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001275 case tok::identifier:
1276 // If we see identifier: after an expression, and we're not already in a
1277 // message send, then this is probably a message send with a missing
1278 // opening bracket '['.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001279 if (getLangOpts().ObjC1 && !InMessageExpression &&
Douglas Gregord6d98002010-09-15 14:54:45 +00001280 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001281 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1282 ParsedType(), LHS.get());
1283 break;
1284 }
1285
1286 // Fall through; this isn't a message send.
1287
Chris Lattner20c6a452006-08-12 17:40:43 +00001288 default: // Not a postfix-expression suffix.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001289 return move(LHS);
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001290 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner47054fb2010-05-31 18:18:22 +00001291 // If we have a array postfix expression that starts on a new line and
1292 // Objective-C is enabled, it is highly likely that the user forgot a
1293 // semicolon after the base expression and that the array postfix-expr is
1294 // actually another message send. In this case, do some look-ahead to see
1295 // if the contents of the square brackets are obviously not a valid
1296 // expression and recover by pretending there is no suffix.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001297 if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
Chris Lattner47054fb2010-05-31 18:18:22 +00001298 isSimpleObjCMessageExpression())
Douglas Gregor990ccac2010-05-31 14:40:22 +00001299 return move(LHS);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001300
1301 // Reject array indices starting with a lambda-expression. '[[' is
1302 // reserved for attributes.
1303 if (CheckProhibitedCXX11Attribute())
1304 return ExprError();
1305
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001306 BalancedDelimiterTracker T(*this, tok::l_square);
1307 T.consumeOpen();
1308 Loc = T.getOpenLocation();
Sebastian Redl3da34892011-06-05 12:23:16 +00001309 ExprResult Idx;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001310 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001311 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00001312 Idx = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001313 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00001314 Idx = ParseExpression();
Sebastian Redl511ed552008-11-25 22:21:31 +00001315
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001316 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001317
1318 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCallb268a282010-08-23 23:25:46 +00001319 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1320 Idx.take(), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001321 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +00001322 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001323
Chris Lattner89c50c62006-08-11 06:41:18 +00001324 // Match the ']'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001325 T.consumeClose();
Chris Lattner89c50c62006-08-11 06:41:18 +00001326 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001327 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001328
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001329 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1330 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1331 // '(' argument-expression-list[opt] ')'
1332 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001333 InMessageExpressionRAIIObject InMessage(*this, false);
1334
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001335 Expr *ExecConfig = 0;
1336
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001337 BalancedDelimiterTracker PT(*this, tok::l_paren);
1338
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001339 if (OpKind == tok::lesslessless) {
1340 ExprVector ExecConfigExprs(Actions);
1341 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001342 SourceLocation OpenLoc = ConsumeToken();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001343
1344 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1345 LHS = ExprError();
1346 }
1347
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001348 SourceLocation CloseLoc = Tok.getLocation();
1349 if (Tok.is(tok::greatergreatergreater)) {
1350 ConsumeToken();
1351 } else if (LHS.isInvalid()) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001352 SkipUntil(tok::greatergreatergreater);
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001353 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001354 // There was an error closing the brackets
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001355 Diag(Tok, diag::err_expected_ggg);
1356 Diag(OpenLoc, diag::note_matching) << "<<<";
1357 SkipUntil(tok::greatergreatergreater);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001358 LHS = ExprError();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001359 }
1360
1361 if (!LHS.isInvalid()) {
1362 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1363 LHS = ExprError();
1364 else
1365 Loc = PrevTokLocation;
1366 }
1367
1368 if (!LHS.isInvalid()) {
1369 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001370 OpenLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001371 move_arg(ExecConfigExprs),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001372 CloseLoc);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001373 if (ECResult.isInvalid())
1374 LHS = ExprError();
1375 else
1376 ExecConfig = ECResult.get();
1377 }
1378 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001379 PT.consumeOpen();
1380 Loc = PT.getOpenLocation();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001381 }
1382
Sebastian Redl511ed552008-11-25 22:21:31 +00001383 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001384 CommaLocsTy CommaLocs;
Douglas Gregoreda7e542010-09-18 01:28:11 +00001385
Douglas Gregorcabea402009-09-22 15:41:20 +00001386 if (Tok.is(tok::code_completion)) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00001387 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1388 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001389 cutOffParsing();
1390 return ExprError();
Douglas Gregorcabea402009-09-22 15:41:20 +00001391 }
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001392
1393 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1394 if (Tok.isNot(tok::r_paren)) {
1395 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1396 LHS.get())) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001397 LHS = ExprError();
1398 }
Chris Lattner0c6c0342006-08-12 18:12:45 +00001399 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001400 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001401
Chris Lattner89c50c62006-08-11 06:41:18 +00001402 // Match the ')'.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001403 if (LHS.isInvalid()) {
1404 SkipUntil(tok::r_paren);
1405 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001406 PT.consumeClose();
Douglas Gregoreda7e542010-09-18 01:28:11 +00001407 LHS = ExprError();
1408 } else {
1409 assert((ArgExprs.size() == 0 ||
1410 ArgExprs.size()-1 == CommaLocs.size())&&
Chris Lattnere165d942006-08-24 04:40:38 +00001411 "Unexpected number of commas!");
John McCallb268a282010-08-23 23:25:46 +00001412 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001413 move_arg(ArgExprs), Tok.getLocation(),
1414 ExecConfig);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001415 PT.consumeClose();
Chris Lattnere165d942006-08-24 04:40:38 +00001416 }
Mike Stump11289f42009-09-09 15:08:12 +00001417
Chris Lattner89c50c62006-08-11 06:41:18 +00001418 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001419 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001420 case tok::arrow:
1421 case tok::period: {
1422 // postfix-expression: p-e '->' template[opt] id-expression
1423 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001424 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001425 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001426
Douglas Gregord8061562009-08-06 03:17:00 +00001427 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001428 ParsedType ObjectType;
Douglas Gregore610ada2010-02-24 18:44:31 +00001429 bool MayBePseudoDestructor = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001430 if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001431 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregore610ada2010-02-24 18:44:31 +00001432 OpLoc, OpKind, ObjectType,
1433 MayBePseudoDestructor);
Douglas Gregord8061562009-08-06 03:17:00 +00001434 if (LHS.isInvalid())
1435 break;
Douglas Gregore610ada2010-02-24 18:44:31 +00001436
Douglas Gregordf593fb2011-11-07 17:33:42 +00001437 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1438 /*EnteringContext=*/false,
Douglas Gregore610ada2010-02-24 18:44:31 +00001439 &MayBePseudoDestructor);
Douglas Gregor205a3612010-05-27 15:25:59 +00001440 if (SS.isNotEmpty())
John McCallba7bf592010-08-24 05:47:05 +00001441 ObjectType = ParsedType();
Douglas Gregord8061562009-08-06 03:17:00 +00001442 }
1443
Douglas Gregor2436e712009-09-17 21:32:03 +00001444 if (Tok.is(tok::code_completion)) {
1445 // Code completion for a member access expression.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001446 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor2436e712009-09-17 21:32:03 +00001447 OpLoc, OpKind == tok::arrow);
1448
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001449 cutOffParsing();
1450 return ExprError();
Douglas Gregor2436e712009-09-17 21:32:03 +00001451 }
1452
John McCallb268a282010-08-23 23:25:46 +00001453 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1454 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregore610ada2010-02-24 18:44:31 +00001455 ObjectType);
1456 break;
1457 }
1458
1459 // Either the action has told is that this cannot be a
1460 // pseudo-destructor expression (based on the type of base
1461 // expression), or we didn't see a '~' in the right place. We
1462 // can still parse a destructor name here, but in that case it
1463 // names a real destructor.
Francois Pichet64225792011-01-18 05:04:39 +00001464 // Allow explicit constructor calls in Microsoft mode.
1465 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001466 SourceLocation TemplateKWLoc;
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001467 UnqualifiedId Name;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001468 if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
Douglas Gregor36107ad2012-02-16 18:19:22 +00001469 // Objective-C++:
1470 // After a '.' in a member access expression, treat the keyword
1471 // 'class' as if it were an identifier.
1472 //
1473 // This hack allows property access to the 'class' method because it is
1474 // such a common method name. For other C++ keywords that are
1475 // Objective-C method names, one must use the message send syntax.
1476 IdentifierInfo *Id = Tok.getIdentifierInfo();
1477 SourceLocation Loc = ConsumeToken();
1478 Name.setIdentifier(Id, Loc);
1479 } else if (ParseUnqualifiedId(SS,
1480 /*EnteringContext=*/false,
1481 /*AllowDestructorName=*/true,
1482 /*AllowConstructorName=*/
David Blaikiebbafb8a2012-03-11 07:00:24 +00001483 getLangOpts().MicrosoftExt,
Douglas Gregor36107ad2012-02-16 18:19:22 +00001484 ObjectType, TemplateKWLoc, Name))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001485 LHS = ExprError();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001486
1487 if (!LHS.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001488 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001489 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001490 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1491 Tok.is(tok::l_paren));
Chris Lattner89c50c62006-08-11 06:41:18 +00001492 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001493 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001494 case tok::plusplus: // postfix-expression: postfix-expression '++'
1495 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001496 if (!LHS.isInvalid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001497 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCallb268a282010-08-23 23:25:46 +00001498 Tok.getKind(), LHS.take());
Sebastian Redl511ed552008-11-25 22:21:31 +00001499 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001500 ConsumeToken();
1501 break;
Chris Lattnerf8339772006-08-10 22:01:51 +00001502 }
1503 }
Chris Lattner52a99e52006-08-10 20:56:00 +00001504}
1505
Peter Collingbournee190dee2011-03-11 19:24:49 +00001506/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1507/// vec_step and we are at the start of an expression or a parenthesized
1508/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1509/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001510///
James Dennett3d5e4592012-06-17 04:36:28 +00001511/// \verbatim
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001512/// unary-expression: [C99 6.5.3]
1513/// 'sizeof' unary-expression
1514/// 'sizeof' '(' type-name ')'
1515/// [GNU] '__alignof' unary-expression
1516/// [GNU] '__alignof' '(' type-name ')'
1517/// [C++0x] 'alignof' '(' type-id ')'
1518///
1519/// [GNU] typeof-specifier:
1520/// typeof ( expressions )
1521/// typeof ( type-name )
1522/// [GNU/C++] typeof unary-expression
1523///
Peter Collingbournee190dee2011-03-11 19:24:49 +00001524/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1525/// vec_step ( expressions )
1526/// vec_step ( type-name )
James Dennett3d5e4592012-06-17 04:36:28 +00001527/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001528ExprResult
Peter Collingbournee190dee2011-03-11 19:24:49 +00001529Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1530 bool &isCastExpr,
1531 ParsedType &CastTy,
1532 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001533
1534 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournee190dee2011-03-11 19:24:49 +00001535 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1536 OpTok.is(tok::kw_vec_step)) &&
1537 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001538
John McCalldadc5752010-08-24 06:29:42 +00001539 ExprResult Operand;
Mike Stump11289f42009-09-09 15:08:12 +00001540
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001541 // If the operand doesn't start with an '(', it must be an expression.
1542 if (Tok.isNot(tok::l_paren)) {
1543 isCastExpr = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001544 if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001545 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1546 return ExprError();
1547 }
Mike Stump11289f42009-09-09 15:08:12 +00001548
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001549 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001550 } else {
1551 // If it starts with a '(', we know that it is either a parenthesized
1552 // type-name, or it is a unary-expression that starts with a compound
1553 // literal, or starts with a primary-expression that is a parenthesized
1554 // expression.
1555 ParenParseOption ExprType = CastExpr;
1556 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001557
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001558 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001559 false, CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001560 CastRange = SourceRange(LParenLoc, RParenLoc);
1561
1562 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1563 // a type.
1564 if (ExprType == CastExpr) {
1565 isCastExpr = true;
1566 return ExprEmpty();
1567 }
1568
David Blaikiebbafb8a2012-03-11 07:00:24 +00001569 if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
Douglas Gregor5dc05532010-07-28 18:22:12 +00001570 // GNU typeof in C requires the expression to be parenthesized. Not so for
1571 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1572 // the start of a unary-expression, but doesn't include any postfix
1573 // pieces. Parse these now if present.
John McCall3669c802010-08-24 23:41:43 +00001574 if (!Operand.isInvalid())
1575 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor5dc05532010-07-28 18:22:12 +00001576 }
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001577 }
1578
1579 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1580 isCastExpr = false;
1581 return move(Operand);
1582}
1583
Chris Lattner20c6a452006-08-12 17:40:43 +00001584
James Dennett3d5e4592012-06-17 04:36:28 +00001585/// \brief Parse a sizeof or alignof expression.
1586///
1587/// \verbatim
Chris Lattner81b576e2006-08-11 02:13:20 +00001588/// unary-expression: [C99 6.5.3]
1589/// 'sizeof' unary-expression
1590/// 'sizeof' '(' type-name ')'
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001591/// [C++0x] 'sizeof' '...' '(' identifier ')'
Chris Lattner81b576e2006-08-11 02:13:20 +00001592/// [GNU] '__alignof' unary-expression
1593/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +00001594/// [C++0x] 'alignof' '(' type-id ')'
James Dennett3d5e4592012-06-17 04:36:28 +00001595/// \endverbatim
Peter Collingbournee190dee2011-03-11 19:24:49 +00001596ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +00001597 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001598 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1599 "Not a sizeof/alignof/vec_step expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001600 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001601 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001602
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001603 // [C++0x] 'sizeof' '...' '(' identifier ')'
1604 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1605 SourceLocation EllipsisLoc = ConsumeToken();
1606 SourceLocation LParenLoc, RParenLoc;
1607 IdentifierInfo *Name = 0;
1608 SourceLocation NameLoc;
1609 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001610 BalancedDelimiterTracker T(*this, tok::l_paren);
1611 T.consumeOpen();
1612 LParenLoc = T.getOpenLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001613 if (Tok.is(tok::identifier)) {
1614 Name = Tok.getIdentifierInfo();
1615 NameLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001616 T.consumeClose();
1617 RParenLoc = T.getCloseLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001618 if (RParenLoc.isInvalid())
1619 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1620 } else {
1621 Diag(Tok, diag::err_expected_parameter_pack);
1622 SkipUntil(tok::r_paren);
1623 }
1624 } else if (Tok.is(tok::identifier)) {
1625 Name = Tok.getIdentifierInfo();
1626 NameLoc = ConsumeToken();
1627 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1628 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1629 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1630 << Name
1631 << FixItHint::CreateInsertion(LParenLoc, "(")
1632 << FixItHint::CreateInsertion(RParenLoc, ")");
1633 } else {
1634 Diag(Tok, diag::err_sizeof_parameter_pack);
1635 }
1636
1637 if (!Name)
1638 return ExprError();
1639
1640 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1641 OpTok.getLocation(),
1642 *Name, NameLoc,
1643 RParenLoc);
1644 }
Richard Smithb15c11c2011-10-17 23:06:20 +00001645
1646 if (OpTok.is(tok::kw_alignof))
1647 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1648
Eli Friedmane0afc982012-01-21 01:01:51 +00001649 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1650
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001651 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00001652 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001653 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001654 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1655 isCastExpr,
1656 CastTy,
1657 CastRange);
1658
1659 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1660 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1661 ExprKind = UETT_AlignOf;
1662 else if (OpTok.is(tok::kw_vec_step))
1663 ExprKind = UETT_VecStep;
Sebastian Redl90893182008-12-11 22:33:27 +00001664
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001665 if (isCastExpr)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001666 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1667 ExprKind,
1668 /*isType=*/true,
1669 CastTy.getAsOpaquePtr(),
1670 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001671
Chris Lattner26115ac2006-08-24 06:10:04 +00001672 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001673 if (!Operand.isInvalid())
Peter Collingbournee190dee2011-03-11 19:24:49 +00001674 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1675 ExprKind,
1676 /*isType=*/false,
1677 Operand.release(),
1678 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001679 return move(Operand);
Chris Lattner81b576e2006-08-11 02:13:20 +00001680}
1681
Chris Lattner11124352006-08-12 19:16:08 +00001682/// ParseBuiltinPrimaryExpression
1683///
James Dennett3d5e4592012-06-17 04:36:28 +00001684/// \verbatim
Chris Lattner11124352006-08-12 19:16:08 +00001685/// primary-expression: [C99 6.5.1]
1686/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1687/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1688/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1689/// assign-expr ')'
1690/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbourne62c21982011-11-05 03:47:48 +00001691/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001692///
Chris Lattner11124352006-08-12 19:16:08 +00001693/// [GNU] offsetof-member-designator:
1694/// [GNU] identifier
1695/// [GNU] offsetof-member-designator '.' identifier
1696/// [GNU] offsetof-member-designator '[' expression ']'
James Dennett3d5e4592012-06-17 04:36:28 +00001697/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001698ExprResult Parser::ParseBuiltinPrimaryExpression() {
1699 ExprResult Res;
Chris Lattner11124352006-08-12 19:16:08 +00001700 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1701
1702 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001703 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001704
1705 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001706 if (Tok.isNot(tok::l_paren))
1707 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1708 << BuiltinII);
1709
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001710 BalancedDelimiterTracker PT(*this, tok::l_paren);
1711 PT.consumeOpen();
1712
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001713 // TODO: Build AST.
1714
Chris Lattner11124352006-08-12 19:16:08 +00001715 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +00001716 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001717 case tok::kw___builtin_va_arg: {
John McCalldadc5752010-08-24 06:29:42 +00001718 ExprResult Expr(ParseAssignmentExpression());
Chris Lattner0be454e2006-08-12 19:30:51 +00001719
Chris Lattner6d7e6342006-08-15 03:41:14 +00001720 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001721 Expr = ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001722
Douglas Gregor220cac52009-02-18 17:45:20 +00001723 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001724
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001725 if (Tok.isNot(tok::r_paren)) {
1726 Diag(Tok, diag::err_expected_rparen);
Douglas Gregoreda7e542010-09-18 01:28:11 +00001727 Expr = ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001728 }
Douglas Gregoreda7e542010-09-18 01:28:11 +00001729
1730 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor220cac52009-02-18 17:45:20 +00001731 Res = ExprError();
1732 else
John McCallb268a282010-08-23 23:25:46 +00001733 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001734 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001735 }
Chris Lattner687d6092007-08-30 15:51:11 +00001736 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001737 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001738 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001739 if (Ty.isInvalid()) {
1740 SkipUntil(tok::r_paren);
1741 return ExprError();
1742 }
Mike Stump11289f42009-09-09 15:08:12 +00001743
Chris Lattner6d7e6342006-08-15 03:41:14 +00001744 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001745 return ExprError();
1746
Chris Lattner11124352006-08-12 19:16:08 +00001747 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001748 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001749 Diag(Tok, diag::err_expected_ident);
1750 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001751 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001752 }
Sebastian Redl90893182008-12-11 22:33:27 +00001753
Chris Lattner687d6092007-08-30 15:51:11 +00001754 // Keep track of the various subcomponents we see.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001755 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001756
John McCallfaf5fb42010-08-26 23:41:50 +00001757 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001758 Comps.back().isBrackets = false;
1759 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1760 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001761
Sebastian Redl511ed552008-11-25 22:21:31 +00001762 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001763 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001764 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001765 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallfaf5fb42010-08-26 23:41:50 +00001766 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001767 Comps.back().isBrackets = false;
1768 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001769
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001770 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001771 Diag(Tok, diag::err_expected_ident);
1772 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001773 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001774 }
1775 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1776 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001777
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001778 } else if (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001779 if (CheckProhibitedCXX11Attribute())
1780 return ExprError();
1781
Chris Lattner11124352006-08-12 19:16:08 +00001782 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallfaf5fb42010-08-26 23:41:50 +00001783 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001784 Comps.back().isBrackets = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001785 BalancedDelimiterTracker ST(*this, tok::l_square);
1786 ST.consumeOpen();
1787 Comps.back().LocStart = ST.getOpenLocation();
Chris Lattner11124352006-08-12 19:16:08 +00001788 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001789 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001790 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001791 return move(Res);
Chris Lattner11124352006-08-12 19:16:08 +00001792 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001793 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001794
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001795 ST.consumeClose();
1796 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman5e774b12009-06-27 20:38:33 +00001797 } else {
1798 if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001799 PT.consumeClose();
Douglas Gregor220cac52009-02-18 17:45:20 +00001800 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001801 } else if (Ty.isInvalid()) {
1802 Res = ExprError();
1803 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001804 PT.consumeClose();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001805 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001806 Ty.get(), &Comps[0], Comps.size(),
1807 PT.getCloseLocation());
Eli Friedman5e774b12009-06-27 20:38:33 +00001808 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001809 break;
Chris Lattner11124352006-08-12 19:16:08 +00001810 }
1811 }
1812 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001813 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001814 case tok::kw___builtin_choose_expr: {
John McCalldadc5752010-08-24 06:29:42 +00001815 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001816 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001817 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001818 return move(Cond);
Steve Naroff9efdabc2007-08-03 21:21:27 +00001819 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001820 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001821 return ExprError();
1822
John McCalldadc5752010-08-24 06:29:42 +00001823 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001824 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001825 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001826 return move(Expr1);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001827 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001828 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001829 return ExprError();
1830
John McCalldadc5752010-08-24 06:29:42 +00001831 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001832 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001833 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001834 return move(Expr2);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001835 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001836 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001837 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001838 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001839 }
John McCallb268a282010-08-23 23:25:46 +00001840 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1841 Expr2.take(), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001842 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001843 }
Tanya Lattner55808c12011-06-04 00:47:47 +00001844 case tok::kw___builtin_astype: {
1845 // The first argument is an expression to be converted, followed by a comma.
1846 ExprResult Expr(ParseAssignmentExpression());
1847 if (Expr.isInvalid()) {
1848 SkipUntil(tok::r_paren);
1849 return ExprError();
1850 }
1851
1852 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1853 tok::r_paren))
1854 return ExprError();
1855
1856 // Second argument is the type to bitcast to.
1857 TypeResult DestTy = ParseTypeName();
1858 if (DestTy.isInvalid())
1859 return ExprError();
1860
1861 // Attempt to consume the r-paren.
1862 if (Tok.isNot(tok::r_paren)) {
1863 Diag(Tok, diag::err_expected_rparen);
1864 SkipUntil(tok::r_paren);
1865 return ExprError();
1866 }
1867
1868 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1869 ConsumeParen());
1870 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001871 }
Chandler Carruth904cb142011-07-08 04:59:44 +00001872 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001873
John McCallb268a282010-08-23 23:25:46 +00001874 if (Res.isInvalid())
1875 return ExprError();
1876
Chris Lattner11124352006-08-12 19:16:08 +00001877 // These can be followed by postfix-expr pieces because they are
1878 // primary-expressions.
John McCallb268a282010-08-23 23:25:46 +00001879 return ParsePostfixExpressionSuffix(Res.take());
Chris Lattner11124352006-08-12 19:16:08 +00001880}
1881
Chris Lattner4add4e62006-08-11 01:33:00 +00001882/// ParseParenExpression - This parses the unit that starts with a '(' token,
1883/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001884/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1885/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001886///
James Dennett3d5e4592012-06-17 04:36:28 +00001887/// \verbatim
Chris Lattner4add4e62006-08-11 01:33:00 +00001888/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001889/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001890/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1891/// postfix-expression: [C99 6.5.2]
1892/// '(' type-name ')' '{' initializer-list '}'
1893/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001894/// cast-expression: [C99 6.5.4]
1895/// '(' type-name ')' cast-expression
John McCall31168b02011-06-15 23:02:42 +00001896/// [ARC] bridged-cast-expression
1897///
1898/// [ARC] bridged-cast-expression:
1899/// (__bridge type-name) cast-expression
1900/// (__bridge_transfer type-name) cast-expression
1901/// (__bridge_retained type-name) cast-expression
James Dennett3d5e4592012-06-17 04:36:28 +00001902/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001903ExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001904Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001905 bool isTypeCast, ParsedType &CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001906 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001907 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001908 BalancedDelimiterTracker T(*this, tok::l_paren);
1909 if (T.consumeOpen())
1910 return ExprError();
1911 SourceLocation OpenLoc = T.getOpenLocation();
1912
John McCalldadc5752010-08-24 06:29:42 +00001913 ExprResult Result(true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001914 bool isAmbiguousTypeId;
John McCallba7bf592010-08-24 05:47:05 +00001915 CastTy = ParsedType();
Sebastian Redl90893182008-12-11 22:33:27 +00001916
Douglas Gregor5e35d592010-09-14 23:59:36 +00001917 if (Tok.is(tok::code_completion)) {
1918 Actions.CodeCompleteOrdinaryName(getCurScope(),
1919 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1920 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001921 cutOffParsing();
Douglas Gregor5e35d592010-09-14 23:59:36 +00001922 return ExprError();
1923 }
John McCallc5e6b972011-04-06 02:35:25 +00001924
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001925 // Diagnose use of bridge casts in non-arc mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001926 bool BridgeCast = (getLangOpts().ObjC2 &&
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001927 (Tok.is(tok::kw___bridge) ||
1928 Tok.is(tok::kw___bridge_transfer) ||
1929 Tok.is(tok::kw___bridge_retained) ||
1930 Tok.is(tok::kw___bridge_retain)));
David Blaikiebbafb8a2012-03-11 07:00:24 +00001931 if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001932 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001933 SourceLocation BridgeKeywordLoc = ConsumeToken();
1934 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremenek084e1b42012-02-18 04:42:38 +00001935 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001936 << BridgeCastName
1937 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001938 BridgeCast = false;
1939 }
1940
John McCallc5e6b972011-04-06 02:35:25 +00001941 // None of these cases should fall through with an invalid Result
1942 // unless they've already reported an error.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001943 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001944 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall3abee492012-04-04 01:27:53 +00001945 Actions.ActOnStartStmtExpr();
1946
Richard Smithc202b282012-04-14 00:33:13 +00001947 StmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001948 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001949
Chris Lattner366727f2007-07-24 16:58:17 +00001950 // If the substmt parsed correctly, build the AST node.
John McCall3abee492012-04-04 01:27:53 +00001951 if (!Stmt.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001952 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
John McCall3abee492012-04-04 01:27:53 +00001953 } else {
1954 Actions.ActOnStmtExprError();
1955 }
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001956 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCall0c07bee2011-06-17 21:56:12 +00001957 tok::TokenKind tokenKind = Tok.getKind();
1958 SourceLocation BridgeKeywordLoc = ConsumeToken();
1959
John McCall31168b02011-06-15 23:02:42 +00001960 // Parse an Objective-C ARC ownership cast expression.
1961 ObjCBridgeCastKind Kind;
John McCall0c07bee2011-06-17 21:56:12 +00001962 if (tokenKind == tok::kw___bridge)
John McCall31168b02011-06-15 23:02:42 +00001963 Kind = OBC_Bridge;
John McCall0c07bee2011-06-17 21:56:12 +00001964 else if (tokenKind == tok::kw___bridge_transfer)
John McCall31168b02011-06-15 23:02:42 +00001965 Kind = OBC_BridgeTransfer;
John McCall0c07bee2011-06-17 21:56:12 +00001966 else if (tokenKind == tok::kw___bridge_retained)
John McCall31168b02011-06-15 23:02:42 +00001967 Kind = OBC_BridgeRetained;
John McCall0c07bee2011-06-17 21:56:12 +00001968 else {
1969 // As a hopefully temporary workaround, allow __bridge_retain as
1970 // a synonym for __bridge_retained, but only in system headers.
1971 assert(tokenKind == tok::kw___bridge_retain);
1972 Kind = OBC_BridgeRetained;
1973 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1974 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1975 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1976 "__bridge_retained");
1977 }
John McCall31168b02011-06-15 23:02:42 +00001978
John McCall31168b02011-06-15 23:02:42 +00001979 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001980 T.consumeClose();
1981 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001982 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCall31168b02011-06-15 23:02:42 +00001983
1984 if (Ty.isInvalid() || SubExpr.isInvalid())
1985 return ExprError();
1986
1987 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1988 BridgeKeywordLoc, Ty.get(),
1989 RParenLoc, SubExpr.get());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001990 } else if (ExprType >= CompoundLiteral &&
1991 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00001992
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001993 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00001994
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001995 // In C++, if the type-id is ambiguous we disambiguate based on context.
1996 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1997 // in which case we should treat it as type-id.
1998 // if stopIfCastExpr is false, we need to determine the context past the
1999 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002000 if (isAmbiguousTypeId && !stopIfCastExpr) {
2001 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
2002 RParenLoc = T.getCloseLocation();
2003 return res;
2004 }
Mike Stump11289f42009-09-09 15:08:12 +00002005
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002006 // Parse the type declarator.
2007 DeclSpec DS(AttrFactory);
2008 ParseSpecifierQualifierList(DS);
2009 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2010 ParseDeclarator(DeclaratorInfo);
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002011
Douglas Gregor3e972002010-09-15 23:19:31 +00002012 // If our type is followed by an identifier and either ':' or ']', then
2013 // this is probably an Objective-C message send where the leading '[' is
2014 // missing. Recover as if that were the case.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002015 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002016 !InMessageExpression && getLangOpts().ObjC1 &&
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002017 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
2018 TypeResult Ty;
2019 {
2020 InMessageExpressionRAIIObject InMessage(*this, false);
2021 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2022 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002023 Result = ParseObjCMessageExpressionBody(SourceLocation(),
2024 SourceLocation(),
2025 Ty.get(), 0);
2026 } else {
2027 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002028 T.consumeClose();
2029 RParenLoc = T.getCloseLocation();
Douglas Gregor3e972002010-09-15 23:19:31 +00002030 if (Tok.is(tok::l_brace)) {
2031 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002032 TypeResult Ty;
2033 {
2034 InMessageExpressionRAIIObject InMessage(*this, false);
2035 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2036 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002037 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +00002038 }
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00002039
Douglas Gregor3e972002010-09-15 23:19:31 +00002040 if (ExprType == CastExpr) {
2041 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb5d49352009-01-19 22:31:54 +00002042
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002043 if (DeclaratorInfo.isInvalidType())
Douglas Gregor3e972002010-09-15 23:19:31 +00002044 return ExprError();
2045
Douglas Gregor3e972002010-09-15 23:19:31 +00002046 // Note that this doesn't parse the subsequent cast-expression, it just
2047 // returns the parsed type to the callee.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002048 if (stopIfCastExpr) {
2049 TypeResult Ty;
2050 {
2051 InMessageExpressionRAIIObject InMessage(*this, false);
2052 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2053 }
2054 CastTy = Ty.get();
Douglas Gregor3e972002010-09-15 23:19:31 +00002055 return ExprResult();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002056 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002057
2058 // Reject the cast of super idiom in ObjC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002059 if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
Douglas Gregor3e972002010-09-15 23:19:31 +00002060 Tok.getIdentifierInfo() == Ident_super &&
2061 getCurScope()->isInObjcMethodScope() &&
2062 GetLookAheadToken(1).isNot(tok::period)) {
2063 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2064 << SourceRange(OpenLoc, RParenLoc);
2065 return ExprError();
2066 }
2067
2068 // Parse the cast-expression that follows it next.
2069 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002070 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2071 /*isAddressOfOperand=*/false,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002072 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002073 if (!Result.isInvalid()) {
2074 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2075 DeclaratorInfo, CastTy,
Douglas Gregor3e972002010-09-15 23:19:31 +00002076 RParenLoc, Result.take());
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002077 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002078 return move(Result);
2079 }
2080
2081 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2082 return ExprError();
2083 }
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002084 } else if (isTypeCast) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00002085 // Parse the expression-list.
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002086 InMessageExpressionRAIIObject InMessage(*this, false);
2087
Nate Begeman5ec4b312009-08-10 23:49:36 +00002088 ExprVector ArgExprs(Actions);
2089 CommaLocsTy CommaLocs;
2090
2091 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2092 ExprType = SimpleExpr;
Sebastian Redla9351792012-02-11 23:51:47 +00002093 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
2094 move_arg(ArgExprs));
Nate Begeman5ec4b312009-08-10 23:49:36 +00002095 }
Chris Lattner4add4e62006-08-11 01:33:00 +00002096 } else {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002097 InMessageExpressionRAIIObject InMessage(*this, false);
2098
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002099 Result = ParseExpression(MaybeTypeCast);
Chris Lattner4add4e62006-08-11 01:33:00 +00002100 ExprType = SimpleExpr;
John McCallc5e6b972011-04-06 02:35:25 +00002101
2102 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002103 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCallb268a282010-08-23 23:25:46 +00002104 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Chris Lattnerf8339772006-08-10 22:01:51 +00002105 }
Sebastian Redl90893182008-12-11 22:33:27 +00002106
Chris Lattner4564bc12006-08-10 23:14:52 +00002107 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00002108 if (Result.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +00002109 SkipUntil(tok::r_paren);
Chris Lattnerd8980502008-12-12 06:00:12 +00002110 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00002111 }
Mike Stump11289f42009-09-09 15:08:12 +00002112
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002113 T.consumeClose();
2114 RParenLoc = T.getCloseLocation();
Sebastian Redl90893182008-12-11 22:33:27 +00002115 return move(Result);
Chris Lattnerc951dae2006-08-10 04:23:57 +00002116}
Chris Lattnerd3e98952006-10-06 05:22:26 +00002117
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002118/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2119/// and we are at the left brace.
2120///
James Dennett3d5e4592012-06-17 04:36:28 +00002121/// \verbatim
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002122/// postfix-expression: [C99 6.5.2]
2123/// '(' type-name ')' '{' initializer-list '}'
2124/// '(' type-name ')' '{' initializer-list ',' '}'
James Dennett3d5e4592012-06-17 04:36:28 +00002125/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002126ExprResult
John McCallba7bf592010-08-24 05:47:05 +00002127Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002128 SourceLocation LParenLoc,
2129 SourceLocation RParenLoc) {
2130 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
David Blaikiebbafb8a2012-03-11 07:00:24 +00002131 if (!getLangOpts().C99) // Compound literals don't exist in C90.
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002132 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCalldadc5752010-08-24 06:29:42 +00002133 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002134 if (!Result.isInvalid() && Ty)
John McCallb268a282010-08-23 23:25:46 +00002135 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002136 return move(Result);
2137}
2138
Chris Lattnerd3e98952006-10-06 05:22:26 +00002139/// ParseStringLiteralExpression - This handles the various token types that
2140/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2141/// translation phase #6].
2142///
James Dennett3d5e4592012-06-17 04:36:28 +00002143/// \verbatim
Chris Lattnerd3e98952006-10-06 05:22:26 +00002144/// primary-expression: [C99 6.5.1]
2145/// string-literal
James Dennett3d5e4592012-06-17 04:36:28 +00002146/// \verbatim
Richard Smithd67aea22012-03-06 03:21:47 +00002147ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002148 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00002149
Chris Lattnerd3e98952006-10-06 05:22:26 +00002150 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2151 // considered to be strings for concatenation purposes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002152 SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00002153
Chris Lattnerd3e98952006-10-06 05:22:26 +00002154 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002155 StringToks.push_back(Tok);
2156 ConsumeStringToken();
2157 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00002158
2159 // Pass the set of string tokens, ready for concatenation, to the actions.
Richard Smithbcc22fc2012-03-09 08:00:36 +00002160 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(),
2161 AllowUserDefinedLiteral ? getCurScope() : 0);
Chris Lattnerd3e98952006-10-06 05:22:26 +00002162}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002163
Benjamin Kramere56f3932011-12-23 17:00:35 +00002164/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2165/// [C11 6.5.1.1].
Peter Collingbourne91147592011-04-15 00:35:48 +00002166///
James Dennett3d5e4592012-06-17 04:36:28 +00002167/// \verbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002168/// generic-selection:
2169/// _Generic ( assignment-expression , generic-assoc-list )
2170/// generic-assoc-list:
2171/// generic-association
2172/// generic-assoc-list , generic-association
2173/// generic-association:
2174/// type-name : assignment-expression
2175/// default : assignment-expression
James Dennett3d5e4592012-06-17 04:36:28 +00002176/// \endverbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002177ExprResult Parser::ParseGenericSelectionExpression() {
2178 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2179 SourceLocation KeyLoc = ConsumeToken();
2180
David Blaikiebbafb8a2012-03-11 07:00:24 +00002181 if (!getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +00002182 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbourne91147592011-04-15 00:35:48 +00002183
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002184 BalancedDelimiterTracker T(*this, tok::l_paren);
2185 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne91147592011-04-15 00:35:48 +00002186 return ExprError();
2187
2188 ExprResult ControllingExpr;
2189 {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002190 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbourne91147592011-04-15 00:35:48 +00002191 // not evaluated."
2192 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2193 ControllingExpr = ParseAssignmentExpression();
2194 if (ControllingExpr.isInvalid()) {
2195 SkipUntil(tok::r_paren);
2196 return ExprError();
2197 }
2198 }
2199
2200 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2201 SkipUntil(tok::r_paren);
2202 return ExprError();
2203 }
2204
2205 SourceLocation DefaultLoc;
2206 TypeVector Types(Actions);
2207 ExprVector Exprs(Actions);
2208 while (1) {
2209 ParsedType Ty;
2210 if (Tok.is(tok::kw_default)) {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002211 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbourne91147592011-04-15 00:35:48 +00002212 // generic association."
2213 if (!DefaultLoc.isInvalid()) {
2214 Diag(Tok, diag::err_duplicate_default_assoc);
2215 Diag(DefaultLoc, diag::note_previous_default_assoc);
2216 SkipUntil(tok::r_paren);
2217 return ExprError();
2218 }
2219 DefaultLoc = ConsumeToken();
2220 Ty = ParsedType();
2221 } else {
2222 ColonProtectionRAIIObject X(*this);
2223 TypeResult TR = ParseTypeName();
2224 if (TR.isInvalid()) {
2225 SkipUntil(tok::r_paren);
2226 return ExprError();
2227 }
2228 Ty = TR.release();
2229 }
2230 Types.push_back(Ty);
2231
2232 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2233 SkipUntil(tok::r_paren);
2234 return ExprError();
2235 }
2236
2237 // FIXME: These expressions should be parsed in a potentially potentially
2238 // evaluated context.
2239 ExprResult ER(ParseAssignmentExpression());
2240 if (ER.isInvalid()) {
2241 SkipUntil(tok::r_paren);
2242 return ExprError();
2243 }
2244 Exprs.push_back(ER.release());
2245
2246 if (Tok.isNot(tok::comma))
2247 break;
2248 ConsumeToken();
2249 }
2250
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002251 T.consumeClose();
2252 if (T.getCloseLocation().isInvalid())
Peter Collingbourne91147592011-04-15 00:35:48 +00002253 return ExprError();
2254
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002255 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2256 T.getCloseLocation(),
Peter Collingbourne91147592011-04-15 00:35:48 +00002257 ControllingExpr.release(),
2258 move_arg(Types), move_arg(Exprs));
2259}
2260
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002261/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2262///
James Dennett3d5e4592012-06-17 04:36:28 +00002263/// \verbatim
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002264/// argument-expression-list:
2265/// assignment-expression
2266/// argument-expression-list , assignment-expression
2267///
2268/// [C++] expression-list:
Sebastian Redl3da34892011-06-05 12:23:16 +00002269/// [C++] assignment-expression
2270/// [C++] expression-list , assignment-expression
2271///
2272/// [C++0x] expression-list:
2273/// [C++0x] initializer-list
2274///
2275/// [C++0x] initializer-list
2276/// [C++0x] initializer-clause ...[opt]
2277/// [C++0x] initializer-list , initializer-clause ...[opt]
2278///
2279/// [C++0x] initializer-clause:
2280/// [C++0x] assignment-expression
2281/// [C++0x] braced-init-list
James Dennett3d5e4592012-06-17 04:36:28 +00002282/// \endverbatim
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002283bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2284 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallfaf5fb42010-08-26 23:41:50 +00002285 void (Sema::*Completer)(Scope *S,
John McCall37ad5512010-08-23 06:44:23 +00002286 Expr *Data,
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002287 llvm::ArrayRef<Expr *> Args),
John McCall37ad5512010-08-23 06:44:23 +00002288 Expr *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002289 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00002290 if (Tok.is(tok::code_completion)) {
2291 if (Completer)
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002292 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor9c1f1bf2011-02-17 03:09:23 +00002293 else
2294 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002295 cutOffParsing();
2296 return true;
Douglas Gregorcabea402009-09-22 15:41:20 +00002297 }
Sebastian Redl3da34892011-06-05 12:23:16 +00002298
2299 ExprResult Expr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002300 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002301 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00002302 Expr = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00002303 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00002304 Expr = ParseAssignmentExpression();
2305
Douglas Gregor968f23a2011-01-03 19:31:53 +00002306 if (Tok.is(tok::ellipsis))
2307 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002308 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002309 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00002310
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002311 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002312
2313 if (Tok.isNot(tok::comma))
2314 return false;
2315 // Move to the next argument, remember where the comma was.
2316 CommaLocs.push_back(ConsumeToken());
2317 }
2318}
Steve Naroff0ac012832008-08-28 19:20:44 +00002319
Mike Stump82f071f2009-02-04 22:31:32 +00002320/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2321///
James Dennett3d5e4592012-06-17 04:36:28 +00002322/// \verbatim
Mike Stump82f071f2009-02-04 22:31:32 +00002323/// [clang] block-id:
2324/// [clang] specifier-qualifier-list block-declarator
James Dennett3d5e4592012-06-17 04:36:28 +00002325/// \endverbatim
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002326void Parser::ParseBlockId(SourceLocation CaretLoc) {
Douglas Gregor643c3302010-10-18 21:34:55 +00002327 if (Tok.is(tok::code_completion)) {
2328 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002329 return cutOffParsing();
Douglas Gregor643c3302010-10-18 21:34:55 +00002330 }
2331
Mike Stump82f071f2009-02-04 22:31:32 +00002332 // Parse the specifier-qualifier-list piece.
John McCall084e83d2011-03-24 11:26:52 +00002333 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002334 ParseSpecifierQualifierList(DS);
2335
2336 // Parse the block-declarator.
2337 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2338 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002339
Mike Stump56ed2ea2009-04-29 21:40:37 +00002340 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall084e83d2011-03-24 11:26:52 +00002341 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump56ed2ea2009-04-29 21:40:37 +00002342
John McCall53fa7142010-12-24 02:08:15 +00002343 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002344
Mike Stump82f071f2009-02-04 22:31:32 +00002345 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002346 Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
Mike Stump82f071f2009-02-04 22:31:32 +00002347}
2348
Steve Naroff0ac012832008-08-28 19:20:44 +00002349/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00002350/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00002351///
James Dennett3d5e4592012-06-17 04:36:28 +00002352/// \verbatim
Steve Naroff0ac012832008-08-28 19:20:44 +00002353/// block-literal:
2354/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00002355/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00002356/// [clang] block-args:
2357/// [clang] '(' parameter-list ')'
James Dennett3d5e4592012-06-17 04:36:28 +00002358/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002359ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00002360 assert(Tok.is(tok::caret) && "block literal starts with ^");
2361 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002362
Chris Lattnerf6801202009-03-05 07:32:12 +00002363 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2364 "block literal parsing");
2365
Mike Stump11289f42009-09-09 15:08:12 +00002366 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00002367 // argument decls, decls within the compound expression, etc. This also
2368 // allows determining whether a variable reference inside the block is
2369 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002370 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002371 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00002372
2373 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002374 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump11289f42009-09-09 15:08:12 +00002375
Steve Naroff0ac012832008-08-28 19:20:44 +00002376 // Parse the return type if present.
John McCall084e83d2011-03-24 11:26:52 +00002377 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002378 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002379 // FIXME: Since the return type isn't actually parsed, it can't be used to
2380 // fill ParamInfo with an initial valid range, so do it manually.
2381 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002382
Steve Naroff0ac012832008-08-28 19:20:44 +00002383 // If this block has arguments, parse them. There is no ambiguity here with
2384 // the expression case, because the expression case requires a parameter list.
2385 if (Tok.is(tok::l_paren)) {
2386 ParseParenDeclarator(ParamInfo);
2387 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002388 // SetIdentifier sets the source range end, but in this case we're past
2389 // that location.
2390 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00002391 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002392 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002393 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00002394 // If there was an error parsing the arguments, they may have
2395 // tried to use ^(x+y) which requires an argument list. Just
2396 // skip the whole block literal.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002397 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002398 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00002399 }
Mike Stump88788fe2009-04-29 19:03:13 +00002400
John McCall53fa7142010-12-24 02:08:15 +00002401 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002402
Mike Stump82f071f2009-02-04 22:31:32 +00002403 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002404 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Mike Stumpd73e4412009-04-14 18:24:37 +00002405 } else if (!Tok.is(tok::l_brace)) {
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002406 ParseBlockId(CaretLoc);
Steve Naroff0ac012832008-08-28 19:20:44 +00002407 } else {
2408 // Otherwise, pretend we saw (void).
John McCall084e83d2011-03-24 11:26:52 +00002409 ParsedAttributes attrs(AttrFactory);
2410 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002411 SourceLocation(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002412 0, 0, 0,
Douglas Gregor54992352011-01-26 03:43:54 +00002413 true, SourceLocation(),
Douglas Gregorad69e652011-07-13 21:47:47 +00002414 SourceLocation(),
Douglas Gregore248eea2011-10-19 06:04:55 +00002415 SourceLocation(),
2416 SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00002417 EST_None,
2418 SourceLocation(),
Richard Smith2331bbf2012-05-02 22:22:32 +00002419 0, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002420 CaretLoc, CaretLoc,
2421 ParamInfo),
John McCall084e83d2011-03-24 11:26:52 +00002422 attrs, CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00002423
John McCall53fa7142010-12-24 02:08:15 +00002424 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002425
Mike Stump82f071f2009-02-04 22:31:32 +00002426 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002427 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Steve Naroff0ac012832008-08-28 19:20:44 +00002428 }
2429
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002430
John McCalldadc5752010-08-24 06:29:42 +00002431 ExprResult Result(true);
Chris Lattner9eac9312009-03-27 04:18:06 +00002432 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002433 // Saw something like: ^expr
2434 Diag(Tok, diag::err_expected_expression);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002435 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002436 return ExprError();
2437 }
Mike Stump11289f42009-09-09 15:08:12 +00002438
John McCalldadc5752010-08-24 06:29:42 +00002439 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002440 BlockScope.Exit();
Chris Lattner9eac9312009-03-27 04:18:06 +00002441 if (!Stmt.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002442 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9eac9312009-03-27 04:18:06 +00002443 else
Douglas Gregor0be31a22010-07-02 17:43:08 +00002444 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002445 return move(Result);
Steve Naroff0ac012832008-08-28 19:20:44 +00002446}
Ted Kremeneke65b0862012-03-06 20:05:56 +00002447
2448/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
2449///
2450/// '__objc_yes'
2451/// '__objc_no'
2452ExprResult Parser::ParseObjCBoolLiteral() {
2453 tok::TokenKind Kind = Tok.getKind();
2454 return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
2455}