blob: d1c59894755210d9a60e06f2a2398b08ab4f3666 [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]
857 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000858 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000859 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000860 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000861 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000862 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000863 case tok::utf8_string_literal:
864 case tok::utf16_string_literal:
865 case tok::utf32_string_literal:
Richard Smithd67aea22012-03-06 03:21:47 +0000866 Res = ParseStringLiteralExpression(true);
John McCallb268a282010-08-23 23:25:46 +0000867 break;
Benjamin Kramere56f3932011-12-23 17:00:35 +0000868 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbourne91147592011-04-15 00:35:48 +0000869 Res = ParseGenericSelectionExpression();
870 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000871 case tok::kw___builtin_va_arg:
872 case tok::kw___builtin_offsetof:
873 case tok::kw___builtin_choose_expr:
Tanya Lattner55808c12011-06-04 00:47:47 +0000874 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redl90893182008-12-11 22:33:27 +0000875 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000876 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000877 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth904cb142011-07-08 04:59:44 +0000878
Douglas Gregora49ccfe2010-08-06 14:50:36 +0000879 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
880 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
881 // C++ [expr.unary] has:
882 // unary-expression:
883 // ++ cast-expression
884 // -- cast-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000885 SourceLocation SavedLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000886 Res = ParseCastExpression(!getLangOpts().CPlusPlus);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000887 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000888 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000889 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000890 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000891 case tok::amp: { // unary-expression: '&' cast-expression
892 // Special treatment because of member pointers
893 SourceLocation SavedLoc = ConsumeToken();
894 Res = ParseCastExpression(false, true);
895 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000896 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000897 return move(Res);
898 }
899
Chris Lattner81b576e2006-08-11 02:13:20 +0000900 case tok::star: // unary-expression: '*' cast-expression
901 case tok::plus: // unary-expression: '+' cast-expression
902 case tok::minus: // unary-expression: '-' cast-expression
903 case tok::tilde: // unary-expression: '~' cast-expression
904 case tok::exclaim: // unary-expression: '!' cast-expression
905 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000906 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000907 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000908 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000909 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000910 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000911 return move(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000912 }
913
Chris Lattnerc43926f2008-02-02 20:20:10 +0000914 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
915 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000916 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000917 SourceLocation SavedLoc = ConsumeToken();
918 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000919 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000920 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000921 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000922 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000923 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
924 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000925 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000926 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
927 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000928 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournee190dee2011-03-11 19:24:49 +0000929 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
930 return ParseUnaryExprOrTypeTraitExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000931 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000932 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000933 if (Tok.isNot(tok::identifier))
934 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000935
Chris Lattner9ba479b2011-02-18 21:16:39 +0000936 if (getCurScope()->getFnParent() == 0)
937 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
938
Chris Lattnereefa10e2007-05-28 06:56:27 +0000939 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000940 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
941 Tok.getLocation());
942 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Chris Lattner14a1b642006-10-15 22:33:58 +0000943 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000944 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000945 }
Chris Lattner29375652006-12-04 18:06:35 +0000946 case tok::kw_const_cast:
947 case tok::kw_dynamic_cast:
948 case tok::kw_reinterpret_cast:
949 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000950 Res = ParseCXXCasts();
John McCallb268a282010-08-23 23:25:46 +0000951 break;
Sebastian Redlc4704762008-11-11 11:37:55 +0000952 case tok::kw_typeid:
953 Res = ParseCXXTypeid();
John McCallb268a282010-08-23 23:25:46 +0000954 break;
Francois Pichet9f4f2072010-09-08 12:20:18 +0000955 case tok::kw___uuidof:
956 Res = ParseCXXUuidof();
957 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000958 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000959 Res = ParseCXXThis();
John McCallb268a282010-08-23 23:25:46 +0000960 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000961
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000962 case tok::annot_typename:
963 if (isStartOfObjCClassMessageMissingOpenBracket()) {
964 ParsedType Type = getTypeAnnotation(Tok);
965
966 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000967 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000968 DS.SetRangeStart(Tok.getLocation());
969 DS.SetRangeEnd(Tok.getLastLoc());
970
971 const char *PrevSpec = 0;
972 unsigned DiagID;
Nico Weber77430342010-11-22 10:30:56 +0000973 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
974 PrevSpec, DiagID, Type);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000975
976 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
977 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
978 if (Ty.isInvalid())
979 break;
980
981 ConsumeToken();
982 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
983 Ty.get(), 0);
984 break;
985 }
986 // Fall through
987
David Blaikie25896afb2012-01-24 05:47:35 +0000988 case tok::annot_decltype:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000989 case tok::kw_char:
990 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000991 case tok::kw_char16_t:
992 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000993 case tok::kw_bool:
994 case tok::kw_short:
995 case tok::kw_int:
996 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +0000997 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000998 case tok::kw___int128:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000999 case tok::kw_signed:
1000 case tok::kw_unsigned:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001001 case tok::kw_half:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001002 case tok::kw_float:
1003 case tok::kw_double:
1004 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +00001005 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +00001006 case tok::kw_typeof:
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001007 case tok::kw___vector: {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001008 if (!getLangOpts().CPlusPlus) {
Chris Lattner8a38aa82009-01-04 22:28:21 +00001009 Diag(Tok, diag::err_expected_expression);
1010 return ExprError();
1011 }
Eli Friedman6d692cc2009-06-11 00:33:41 +00001012
1013 if (SavedKind == tok::kw_typename) {
1014 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001015 // typename-specifier braced-init-list
John McCall1f476a12010-02-26 08:45:28 +00001016 if (TryAnnotateTypeOrScopeToken())
Eli Friedman6d692cc2009-06-11 00:33:41 +00001017 return ExprError();
1018 }
1019
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001020 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001021 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001022 //
John McCall084e83d2011-03-24 11:26:52 +00001023 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001024 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redl3da34892011-06-05 12:23:16 +00001025 if (Tok.isNot(tok::l_paren) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001026 (!getLangOpts().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl59b5e512008-12-11 21:36:32 +00001027 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1028 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001029
Richard Smith5d164bc2011-10-15 05:09:34 +00001030 if (Tok.is(tok::l_brace))
1031 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1032
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001033 Res = ParseCXXTypeConstructExpression(DS);
John McCallb268a282010-08-23 23:25:46 +00001034 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001035 }
1036
Douglas Gregor7df89f52010-02-05 19:11:37 +00001037 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001038 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1039 // (We can end up in this situation after tentative parsing.)
1040 if (TryAnnotateTypeOrScopeToken())
1041 return ExprError();
1042 if (!Tok.is(tok::annot_cxxscope))
1043 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001044 NotCastExpr, isTypeCast);
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001045
Douglas Gregor7df89f52010-02-05 19:11:37 +00001046 Token Next = NextToken();
1047 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001048 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001049 if (TemplateId->Kind == TNK_Type_template) {
1050 // We have a qualified template-id that we know refers to a
1051 // type, translate it into a type and continue parsing as a
1052 // cast expression.
1053 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001054 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1055 /*EnteringContext=*/false);
Douglas Gregore7c20652011-03-02 00:47:37 +00001056 AnnotateTemplateIdTokenAsType();
Douglas Gregor7df89f52010-02-05 19:11:37 +00001057 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001058 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001059 }
1060 }
1061
1062 // Parse as an id-expression.
1063 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001064 break;
Douglas Gregor7df89f52010-02-05 19:11:37 +00001065 }
1066
1067 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001068 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001069 if (TemplateId->Kind == TNK_Type_template) {
1070 // We have a template-id that we know refers to a type,
1071 // translate it into a type and continue parsing as a cast
1072 // expression.
1073 AnnotateTemplateIdTokenAsType();
1074 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001075 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001076 }
1077
1078 // Fall through to treat the template-id as an id-expression.
1079 }
1080
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001081 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001082 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001083 break;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001084
Chris Lattner122db262009-01-04 22:52:14 +00001085 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001086 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1087 // annotates the token, tail recurse.
1088 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001089 return ExprError();
1090 if (!Tok.is(tok::coloncolon))
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001091 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1092
Chris Lattner122db262009-01-04 22:52:14 +00001093 // ::new -> [C++] new-expression
1094 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001095 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +00001096 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001097 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +00001098 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001099 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001100
Chris Lattner9a8968b2009-01-04 23:23:14 +00001101 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001102 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +00001103 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +00001104 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +00001105
Sebastian Redlbd150f42008-11-21 19:14:01 +00001106 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001107 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001108
1109 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001110 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001111
Sebastian Redl22e3a932010-09-10 20:55:37 +00001112 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smithb15c11c2011-10-17 23:06:20 +00001113 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001114 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001115 BalancedDelimiterTracker T(*this, tok::l_paren);
1116
1117 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl22e3a932010-09-10 20:55:37 +00001118 return ExprError();
Richard Smith764d2fe2011-12-20 02:08:33 +00001119 // C++11 [expr.unary.noexcept]p1:
Sebastian Redle56be2f72010-09-10 21:57:27 +00001120 // The noexcept operator determines whether the evaluation of its operand,
1121 // which is an unevaluated operand, can throw an exception.
1122 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001123 ExprResult Result = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001124
1125 T.consumeClose();
1126
Sebastian Redl22e3a932010-09-10 20:55:37 +00001127 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001128 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1129 Result.take(), T.getCloseLocation());
Sebastian Redl22e3a932010-09-10 20:55:37 +00001130 return move(Result);
1131 }
1132
Chandler Carruth79803482011-04-23 10:47:20 +00001133 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001134 case tok::kw___is_class:
Eli Friedmanc96d4962009-08-15 21:55:26 +00001135 case tok::kw___is_empty:
Chandler Carruth79803482011-04-23 10:47:20 +00001136 case tok::kw___is_enum:
Sebastian Redl79eba1c2009-12-03 00:13:20 +00001137 case tok::kw___is_literal:
John Wiegley65497cc2011-04-27 23:09:49 +00001138 case tok::kw___is_arithmetic:
1139 case tok::kw___is_integral:
1140 case tok::kw___is_floating_point:
1141 case tok::kw___is_complete_type:
1142 case tok::kw___is_void:
1143 case tok::kw___is_array:
1144 case tok::kw___is_function:
1145 case tok::kw___is_reference:
1146 case tok::kw___is_lvalue_reference:
1147 case tok::kw___is_rvalue_reference:
1148 case tok::kw___is_fundamental:
1149 case tok::kw___is_object:
1150 case tok::kw___is_scalar:
1151 case tok::kw___is_compound:
1152 case tok::kw___is_pointer:
1153 case tok::kw___is_member_object_pointer:
1154 case tok::kw___is_member_function_pointer:
1155 case tok::kw___is_member_pointer:
1156 case tok::kw___is_const:
1157 case tok::kw___is_volatile:
1158 case tok::kw___is_standard_layout:
1159 case tok::kw___is_signed:
1160 case tok::kw___is_unsigned:
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00001161 case tok::kw___is_literal_type:
Chandler Carruth79803482011-04-23 10:47:20 +00001162 case tok::kw___is_pod:
1163 case tok::kw___is_polymorphic:
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +00001164 case tok::kw___is_trivial:
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001165 case tok::kw___is_trivially_copyable:
Chandler Carruth79803482011-04-23 10:47:20 +00001166 case tok::kw___is_union:
Douglas Gregordca70af2011-12-03 18:14:24 +00001167 case tok::kw___is_final:
Anders Carlssonfe63dc52009-04-16 00:08:20 +00001168 case tok::kw___has_trivial_constructor:
Douglas Gregor79f83ed2009-07-23 23:49:00 +00001169 case tok::kw___has_trivial_copy:
1170 case tok::kw___has_trivial_assign:
Anders Carlsson6dc35752009-04-17 02:34:54 +00001171 case tok::kw___has_trivial_destructor:
Sebastian Redl7dcb1552010-08-31 04:59:00 +00001172 case tok::kw___has_nothrow_assign:
1173 case tok::kw___has_nothrow_copy:
1174 case tok::kw___has_nothrow_constructor:
Sebastian Redlb469afb2010-09-02 23:19:42 +00001175 case tok::kw___has_virtual_destructor:
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001176 return ParseUnaryTypeTrait();
1177
Francois Pichet34b21132010-12-08 22:35:30 +00001178 case tok::kw___builtin_types_compatible_p:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001179 case tok::kw___is_base_of:
John Wiegley65497cc2011-04-27 23:09:49 +00001180 case tok::kw___is_same:
1181 case tok::kw___is_convertible:
Douglas Gregor8006e762011-01-27 20:28:01 +00001182 case tok::kw___is_convertible_to:
Douglas Gregor1be329d2012-02-23 07:33:15 +00001183 case tok::kw___is_trivially_assignable:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001184 return ParseBinaryTypeTrait();
1185
Douglas Gregor29c42f22012-02-24 07:38:34 +00001186 case tok::kw___is_trivially_constructible:
1187 return ParseTypeTrait();
1188
John Wiegley6242b6a2011-04-28 00:16:57 +00001189 case tok::kw___array_rank:
1190 case tok::kw___array_extent:
1191 return ParseArrayTypeTrait();
1192
John Wiegleyf9f65842011-04-25 06:54:41 +00001193 case tok::kw___is_lvalue_expr:
1194 case tok::kw___is_rvalue_expr:
1195 return ParseExpressionTrait();
1196
Chris Lattner644e1b72007-10-03 22:03:06 +00001197 case tok::at: {
1198 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001199 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +00001200 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001201 case tok::caret:
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001202 Res = ParseBlockLiteralExpression();
1203 break;
1204 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001205 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001206 cutOffParsing();
1207 return ExprError();
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001208 }
Chris Lattner6bf1db12008-12-12 19:20:14 +00001209 case tok::l_square:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001210 if (getLangOpts().CPlusPlus0x) {
1211 if (getLangOpts().ObjC1) {
Eli Friedmanc7c97142012-01-04 02:40:39 +00001212 // C++11 lambda expressions and Objective-C message sends both start with a
1213 // square bracket. There are three possibilities here:
1214 // we have a valid lambda expression, we have an invalid lambda
1215 // expression, or we have something that doesn't appear to be a lambda.
1216 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001217 Res = TryParseLambdaExpression();
Eli Friedmanc7c97142012-01-04 02:40:39 +00001218 if (!Res.isInvalid() && !Res.get())
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001219 Res = ParseObjCMessageExpression();
1220 break;
1221 }
1222 Res = ParseLambdaExpression();
1223 break;
1224 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001225 if (getLangOpts().ObjC1) {
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001226 Res = ParseObjCMessageExpression();
1227 break;
1228 }
1229 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +00001230 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001231 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001232 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +00001233 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001234
John McCallb268a282010-08-23 23:25:46 +00001235 // These can be followed by postfix-expr pieces.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001236 return ParsePostfixExpressionSuffix(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +00001237}
1238
James Dennett3d5e4592012-06-17 04:36:28 +00001239/// \brief Once the leading part of a postfix-expression is parsed, this
1240/// method parses any suffixes that apply.
Chris Lattner20c6a452006-08-12 17:40:43 +00001241///
James Dennett3d5e4592012-06-17 04:36:28 +00001242/// \verbatim
Chris Lattner20c6a452006-08-12 17:40:43 +00001243/// postfix-expression: [C99 6.5.2]
1244/// primary-expression
1245/// postfix-expression '[' expression ']'
Sebastian Redl3da34892011-06-05 12:23:16 +00001246/// postfix-expression '[' braced-init-list ']'
Chris Lattner20c6a452006-08-12 17:40:43 +00001247/// postfix-expression '(' argument-expression-list[opt] ')'
1248/// postfix-expression '.' identifier
1249/// postfix-expression '->' identifier
1250/// postfix-expression '++'
1251/// postfix-expression '--'
1252/// '(' type-name ')' '{' initializer-list '}'
1253/// '(' type-name ')' '{' initializer-list ',' '}'
1254///
1255/// argument-expression-list: [C99 6.5.2]
Douglas Gregor968f23a2011-01-03 19:31:53 +00001256/// argument-expression ...[opt]
1257/// argument-expression-list ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +00001258/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001259ExprResult
1260Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001261 // Now that the primary-expression piece of the postfix-expression has been
1262 // parsed, see if there are any postfix-expression pieces here.
1263 SourceLocation Loc;
1264 while (1) {
1265 switch (Tok.getKind()) {
Douglas Gregored0b69d2010-09-15 16:23:04 +00001266 case tok::code_completion:
1267 if (InMessageExpression)
1268 return move(LHS);
1269
Douglas Gregoreda7e542010-09-18 01:28:11 +00001270 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001271 cutOffParsing();
1272 return ExprError();
Douglas Gregored0b69d2010-09-15 16:23:04 +00001273
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001274 case tok::identifier:
1275 // If we see identifier: after an expression, and we're not already in a
1276 // message send, then this is probably a message send with a missing
1277 // opening bracket '['.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001278 if (getLangOpts().ObjC1 && !InMessageExpression &&
Douglas Gregord6d98002010-09-15 14:54:45 +00001279 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001280 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1281 ParsedType(), LHS.get());
1282 break;
1283 }
1284
1285 // Fall through; this isn't a message send.
1286
Chris Lattner20c6a452006-08-12 17:40:43 +00001287 default: // Not a postfix-expression suffix.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001288 return move(LHS);
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001289 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner47054fb2010-05-31 18:18:22 +00001290 // If we have a array postfix expression that starts on a new line and
1291 // Objective-C is enabled, it is highly likely that the user forgot a
1292 // semicolon after the base expression and that the array postfix-expr is
1293 // actually another message send. In this case, do some look-ahead to see
1294 // if the contents of the square brackets are obviously not a valid
1295 // expression and recover by pretending there is no suffix.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001296 if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
Chris Lattner47054fb2010-05-31 18:18:22 +00001297 isSimpleObjCMessageExpression())
Douglas Gregor990ccac2010-05-31 14:40:22 +00001298 return move(LHS);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001299
1300 // Reject array indices starting with a lambda-expression. '[[' is
1301 // reserved for attributes.
1302 if (CheckProhibitedCXX11Attribute())
1303 return ExprError();
1304
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001305 BalancedDelimiterTracker T(*this, tok::l_square);
1306 T.consumeOpen();
1307 Loc = T.getOpenLocation();
Sebastian Redl3da34892011-06-05 12:23:16 +00001308 ExprResult Idx;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001309 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001310 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00001311 Idx = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001312 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00001313 Idx = ParseExpression();
Sebastian Redl511ed552008-11-25 22:21:31 +00001314
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001315 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001316
1317 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCallb268a282010-08-23 23:25:46 +00001318 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1319 Idx.take(), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001320 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +00001321 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001322
Chris Lattner89c50c62006-08-11 06:41:18 +00001323 // Match the ']'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001324 T.consumeClose();
Chris Lattner89c50c62006-08-11 06:41:18 +00001325 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001326 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001327
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001328 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1329 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1330 // '(' argument-expression-list[opt] ')'
1331 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001332 InMessageExpressionRAIIObject InMessage(*this, false);
1333
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001334 Expr *ExecConfig = 0;
1335
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001336 BalancedDelimiterTracker PT(*this, tok::l_paren);
1337
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001338 if (OpKind == tok::lesslessless) {
1339 ExprVector ExecConfigExprs(Actions);
1340 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001341 SourceLocation OpenLoc = ConsumeToken();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001342
1343 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1344 LHS = ExprError();
1345 }
1346
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001347 SourceLocation CloseLoc = Tok.getLocation();
1348 if (Tok.is(tok::greatergreatergreater)) {
1349 ConsumeToken();
1350 } else if (LHS.isInvalid()) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001351 SkipUntil(tok::greatergreatergreater);
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001352 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001353 // There was an error closing the brackets
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001354 Diag(Tok, diag::err_expected_ggg);
1355 Diag(OpenLoc, diag::note_matching) << "<<<";
1356 SkipUntil(tok::greatergreatergreater);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001357 LHS = ExprError();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001358 }
1359
1360 if (!LHS.isInvalid()) {
1361 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1362 LHS = ExprError();
1363 else
1364 Loc = PrevTokLocation;
1365 }
1366
1367 if (!LHS.isInvalid()) {
1368 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001369 OpenLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001370 move_arg(ExecConfigExprs),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001371 CloseLoc);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001372 if (ECResult.isInvalid())
1373 LHS = ExprError();
1374 else
1375 ExecConfig = ECResult.get();
1376 }
1377 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001378 PT.consumeOpen();
1379 Loc = PT.getOpenLocation();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001380 }
1381
Sebastian Redl511ed552008-11-25 22:21:31 +00001382 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001383 CommaLocsTy CommaLocs;
Douglas Gregoreda7e542010-09-18 01:28:11 +00001384
Douglas Gregorcabea402009-09-22 15:41:20 +00001385 if (Tok.is(tok::code_completion)) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00001386 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1387 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001388 cutOffParsing();
1389 return ExprError();
Douglas Gregorcabea402009-09-22 15:41:20 +00001390 }
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001391
1392 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1393 if (Tok.isNot(tok::r_paren)) {
1394 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1395 LHS.get())) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001396 LHS = ExprError();
1397 }
Chris Lattner0c6c0342006-08-12 18:12:45 +00001398 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001399 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001400
Chris Lattner89c50c62006-08-11 06:41:18 +00001401 // Match the ')'.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001402 if (LHS.isInvalid()) {
1403 SkipUntil(tok::r_paren);
1404 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001405 PT.consumeClose();
Douglas Gregoreda7e542010-09-18 01:28:11 +00001406 LHS = ExprError();
1407 } else {
1408 assert((ArgExprs.size() == 0 ||
1409 ArgExprs.size()-1 == CommaLocs.size())&&
Chris Lattnere165d942006-08-24 04:40:38 +00001410 "Unexpected number of commas!");
John McCallb268a282010-08-23 23:25:46 +00001411 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001412 move_arg(ArgExprs), Tok.getLocation(),
1413 ExecConfig);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001414 PT.consumeClose();
Chris Lattnere165d942006-08-24 04:40:38 +00001415 }
Mike Stump11289f42009-09-09 15:08:12 +00001416
Chris Lattner89c50c62006-08-11 06:41:18 +00001417 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001418 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001419 case tok::arrow:
1420 case tok::period: {
1421 // postfix-expression: p-e '->' template[opt] id-expression
1422 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001423 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001424 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001425
Douglas Gregord8061562009-08-06 03:17:00 +00001426 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001427 ParsedType ObjectType;
Douglas Gregore610ada2010-02-24 18:44:31 +00001428 bool MayBePseudoDestructor = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001429 if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001430 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregore610ada2010-02-24 18:44:31 +00001431 OpLoc, OpKind, ObjectType,
1432 MayBePseudoDestructor);
Douglas Gregord8061562009-08-06 03:17:00 +00001433 if (LHS.isInvalid())
1434 break;
Douglas Gregore610ada2010-02-24 18:44:31 +00001435
Douglas Gregordf593fb2011-11-07 17:33:42 +00001436 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1437 /*EnteringContext=*/false,
Douglas Gregore610ada2010-02-24 18:44:31 +00001438 &MayBePseudoDestructor);
Douglas Gregor205a3612010-05-27 15:25:59 +00001439 if (SS.isNotEmpty())
John McCallba7bf592010-08-24 05:47:05 +00001440 ObjectType = ParsedType();
Douglas Gregord8061562009-08-06 03:17:00 +00001441 }
1442
Douglas Gregor2436e712009-09-17 21:32:03 +00001443 if (Tok.is(tok::code_completion)) {
1444 // Code completion for a member access expression.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001445 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor2436e712009-09-17 21:32:03 +00001446 OpLoc, OpKind == tok::arrow);
1447
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001448 cutOffParsing();
1449 return ExprError();
Douglas Gregor2436e712009-09-17 21:32:03 +00001450 }
1451
John McCallb268a282010-08-23 23:25:46 +00001452 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1453 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregore610ada2010-02-24 18:44:31 +00001454 ObjectType);
1455 break;
1456 }
1457
1458 // Either the action has told is that this cannot be a
1459 // pseudo-destructor expression (based on the type of base
1460 // expression), or we didn't see a '~' in the right place. We
1461 // can still parse a destructor name here, but in that case it
1462 // names a real destructor.
Francois Pichet64225792011-01-18 05:04:39 +00001463 // Allow explicit constructor calls in Microsoft mode.
1464 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001465 SourceLocation TemplateKWLoc;
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001466 UnqualifiedId Name;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001467 if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
Douglas Gregor36107ad2012-02-16 18:19:22 +00001468 // Objective-C++:
1469 // After a '.' in a member access expression, treat the keyword
1470 // 'class' as if it were an identifier.
1471 //
1472 // This hack allows property access to the 'class' method because it is
1473 // such a common method name. For other C++ keywords that are
1474 // Objective-C method names, one must use the message send syntax.
1475 IdentifierInfo *Id = Tok.getIdentifierInfo();
1476 SourceLocation Loc = ConsumeToken();
1477 Name.setIdentifier(Id, Loc);
1478 } else if (ParseUnqualifiedId(SS,
1479 /*EnteringContext=*/false,
1480 /*AllowDestructorName=*/true,
1481 /*AllowConstructorName=*/
David Blaikiebbafb8a2012-03-11 07:00:24 +00001482 getLangOpts().MicrosoftExt,
Douglas Gregor36107ad2012-02-16 18:19:22 +00001483 ObjectType, TemplateKWLoc, Name))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001484 LHS = ExprError();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001485
1486 if (!LHS.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001487 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001488 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001489 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1490 Tok.is(tok::l_paren));
Chris Lattner89c50c62006-08-11 06:41:18 +00001491 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001492 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001493 case tok::plusplus: // postfix-expression: postfix-expression '++'
1494 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001495 if (!LHS.isInvalid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001496 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCallb268a282010-08-23 23:25:46 +00001497 Tok.getKind(), LHS.take());
Sebastian Redl511ed552008-11-25 22:21:31 +00001498 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001499 ConsumeToken();
1500 break;
Chris Lattnerf8339772006-08-10 22:01:51 +00001501 }
1502 }
Chris Lattner52a99e52006-08-10 20:56:00 +00001503}
1504
Peter Collingbournee190dee2011-03-11 19:24:49 +00001505/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1506/// vec_step and we are at the start of an expression or a parenthesized
1507/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1508/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001509///
James Dennett3d5e4592012-06-17 04:36:28 +00001510/// \verbatim
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001511/// unary-expression: [C99 6.5.3]
1512/// 'sizeof' unary-expression
1513/// 'sizeof' '(' type-name ')'
1514/// [GNU] '__alignof' unary-expression
1515/// [GNU] '__alignof' '(' type-name ')'
1516/// [C++0x] 'alignof' '(' type-id ')'
1517///
1518/// [GNU] typeof-specifier:
1519/// typeof ( expressions )
1520/// typeof ( type-name )
1521/// [GNU/C++] typeof unary-expression
1522///
Peter Collingbournee190dee2011-03-11 19:24:49 +00001523/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1524/// vec_step ( expressions )
1525/// vec_step ( type-name )
James Dennett3d5e4592012-06-17 04:36:28 +00001526/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001527ExprResult
Peter Collingbournee190dee2011-03-11 19:24:49 +00001528Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1529 bool &isCastExpr,
1530 ParsedType &CastTy,
1531 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001532
1533 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournee190dee2011-03-11 19:24:49 +00001534 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1535 OpTok.is(tok::kw_vec_step)) &&
1536 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001537
John McCalldadc5752010-08-24 06:29:42 +00001538 ExprResult Operand;
Mike Stump11289f42009-09-09 15:08:12 +00001539
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001540 // If the operand doesn't start with an '(', it must be an expression.
1541 if (Tok.isNot(tok::l_paren)) {
1542 isCastExpr = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001543 if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001544 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1545 return ExprError();
1546 }
Mike Stump11289f42009-09-09 15:08:12 +00001547
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001548 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001549 } else {
1550 // If it starts with a '(', we know that it is either a parenthesized
1551 // type-name, or it is a unary-expression that starts with a compound
1552 // literal, or starts with a primary-expression that is a parenthesized
1553 // expression.
1554 ParenParseOption ExprType = CastExpr;
1555 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001556
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001557 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001558 false, CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001559 CastRange = SourceRange(LParenLoc, RParenLoc);
1560
1561 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1562 // a type.
1563 if (ExprType == CastExpr) {
1564 isCastExpr = true;
1565 return ExprEmpty();
1566 }
1567
David Blaikiebbafb8a2012-03-11 07:00:24 +00001568 if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
Douglas Gregor5dc05532010-07-28 18:22:12 +00001569 // GNU typeof in C requires the expression to be parenthesized. Not so for
1570 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1571 // the start of a unary-expression, but doesn't include any postfix
1572 // pieces. Parse these now if present.
John McCall3669c802010-08-24 23:41:43 +00001573 if (!Operand.isInvalid())
1574 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor5dc05532010-07-28 18:22:12 +00001575 }
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001576 }
1577
1578 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1579 isCastExpr = false;
1580 return move(Operand);
1581}
1582
Chris Lattner20c6a452006-08-12 17:40:43 +00001583
James Dennett3d5e4592012-06-17 04:36:28 +00001584/// \brief Parse a sizeof or alignof expression.
1585///
1586/// \verbatim
Chris Lattner81b576e2006-08-11 02:13:20 +00001587/// unary-expression: [C99 6.5.3]
1588/// 'sizeof' unary-expression
1589/// 'sizeof' '(' type-name ')'
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001590/// [C++0x] 'sizeof' '...' '(' identifier ')'
Chris Lattner81b576e2006-08-11 02:13:20 +00001591/// [GNU] '__alignof' unary-expression
1592/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +00001593/// [C++0x] 'alignof' '(' type-id ')'
James Dennett3d5e4592012-06-17 04:36:28 +00001594/// \endverbatim
Peter Collingbournee190dee2011-03-11 19:24:49 +00001595ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +00001596 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001597 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1598 "Not a sizeof/alignof/vec_step expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001599 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001600 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001601
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001602 // [C++0x] 'sizeof' '...' '(' identifier ')'
1603 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1604 SourceLocation EllipsisLoc = ConsumeToken();
1605 SourceLocation LParenLoc, RParenLoc;
1606 IdentifierInfo *Name = 0;
1607 SourceLocation NameLoc;
1608 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001609 BalancedDelimiterTracker T(*this, tok::l_paren);
1610 T.consumeOpen();
1611 LParenLoc = T.getOpenLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001612 if (Tok.is(tok::identifier)) {
1613 Name = Tok.getIdentifierInfo();
1614 NameLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001615 T.consumeClose();
1616 RParenLoc = T.getCloseLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001617 if (RParenLoc.isInvalid())
1618 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1619 } else {
1620 Diag(Tok, diag::err_expected_parameter_pack);
1621 SkipUntil(tok::r_paren);
1622 }
1623 } else if (Tok.is(tok::identifier)) {
1624 Name = Tok.getIdentifierInfo();
1625 NameLoc = ConsumeToken();
1626 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1627 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1628 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1629 << Name
1630 << FixItHint::CreateInsertion(LParenLoc, "(")
1631 << FixItHint::CreateInsertion(RParenLoc, ")");
1632 } else {
1633 Diag(Tok, diag::err_sizeof_parameter_pack);
1634 }
1635
1636 if (!Name)
1637 return ExprError();
1638
1639 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1640 OpTok.getLocation(),
1641 *Name, NameLoc,
1642 RParenLoc);
1643 }
Richard Smithb15c11c2011-10-17 23:06:20 +00001644
1645 if (OpTok.is(tok::kw_alignof))
1646 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1647
Eli Friedmane0afc982012-01-21 01:01:51 +00001648 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1649
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001650 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00001651 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001652 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001653 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1654 isCastExpr,
1655 CastTy,
1656 CastRange);
1657
1658 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1659 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1660 ExprKind = UETT_AlignOf;
1661 else if (OpTok.is(tok::kw_vec_step))
1662 ExprKind = UETT_VecStep;
Sebastian Redl90893182008-12-11 22:33:27 +00001663
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001664 if (isCastExpr)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001665 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1666 ExprKind,
1667 /*isType=*/true,
1668 CastTy.getAsOpaquePtr(),
1669 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001670
Chris Lattner26115ac2006-08-24 06:10:04 +00001671 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001672 if (!Operand.isInvalid())
Peter Collingbournee190dee2011-03-11 19:24:49 +00001673 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1674 ExprKind,
1675 /*isType=*/false,
1676 Operand.release(),
1677 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001678 return move(Operand);
Chris Lattner81b576e2006-08-11 02:13:20 +00001679}
1680
Chris Lattner11124352006-08-12 19:16:08 +00001681/// ParseBuiltinPrimaryExpression
1682///
James Dennett3d5e4592012-06-17 04:36:28 +00001683/// \verbatim
Chris Lattner11124352006-08-12 19:16:08 +00001684/// primary-expression: [C99 6.5.1]
1685/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1686/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1687/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1688/// assign-expr ')'
1689/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbourne62c21982011-11-05 03:47:48 +00001690/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001691///
Chris Lattner11124352006-08-12 19:16:08 +00001692/// [GNU] offsetof-member-designator:
1693/// [GNU] identifier
1694/// [GNU] offsetof-member-designator '.' identifier
1695/// [GNU] offsetof-member-designator '[' expression ']'
James Dennett3d5e4592012-06-17 04:36:28 +00001696/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001697ExprResult Parser::ParseBuiltinPrimaryExpression() {
1698 ExprResult Res;
Chris Lattner11124352006-08-12 19:16:08 +00001699 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1700
1701 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001702 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001703
1704 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001705 if (Tok.isNot(tok::l_paren))
1706 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1707 << BuiltinII);
1708
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001709 BalancedDelimiterTracker PT(*this, tok::l_paren);
1710 PT.consumeOpen();
1711
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001712 // TODO: Build AST.
1713
Chris Lattner11124352006-08-12 19:16:08 +00001714 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +00001715 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001716 case tok::kw___builtin_va_arg: {
John McCalldadc5752010-08-24 06:29:42 +00001717 ExprResult Expr(ParseAssignmentExpression());
Chris Lattner0be454e2006-08-12 19:30:51 +00001718
Chris Lattner6d7e6342006-08-15 03:41:14 +00001719 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001720 Expr = ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001721
Douglas Gregor220cac52009-02-18 17:45:20 +00001722 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001723
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001724 if (Tok.isNot(tok::r_paren)) {
1725 Diag(Tok, diag::err_expected_rparen);
Douglas Gregoreda7e542010-09-18 01:28:11 +00001726 Expr = ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001727 }
Douglas Gregoreda7e542010-09-18 01:28:11 +00001728
1729 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor220cac52009-02-18 17:45:20 +00001730 Res = ExprError();
1731 else
John McCallb268a282010-08-23 23:25:46 +00001732 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001733 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001734 }
Chris Lattner687d6092007-08-30 15:51:11 +00001735 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001736 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001737 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001738 if (Ty.isInvalid()) {
1739 SkipUntil(tok::r_paren);
1740 return ExprError();
1741 }
Mike Stump11289f42009-09-09 15:08:12 +00001742
Chris Lattner6d7e6342006-08-15 03:41:14 +00001743 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001744 return ExprError();
1745
Chris Lattner11124352006-08-12 19:16:08 +00001746 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001747 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001748 Diag(Tok, diag::err_expected_ident);
1749 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001750 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001751 }
Sebastian Redl90893182008-12-11 22:33:27 +00001752
Chris Lattner687d6092007-08-30 15:51:11 +00001753 // Keep track of the various subcomponents we see.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001754 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001755
John McCallfaf5fb42010-08-26 23:41:50 +00001756 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001757 Comps.back().isBrackets = false;
1758 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1759 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001760
Sebastian Redl511ed552008-11-25 22:21:31 +00001761 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001762 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001763 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001764 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallfaf5fb42010-08-26 23:41:50 +00001765 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001766 Comps.back().isBrackets = false;
1767 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001768
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001769 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001770 Diag(Tok, diag::err_expected_ident);
1771 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001772 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001773 }
1774 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1775 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001776
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001777 } else if (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001778 if (CheckProhibitedCXX11Attribute())
1779 return ExprError();
1780
Chris Lattner11124352006-08-12 19:16:08 +00001781 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallfaf5fb42010-08-26 23:41:50 +00001782 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001783 Comps.back().isBrackets = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001784 BalancedDelimiterTracker ST(*this, tok::l_square);
1785 ST.consumeOpen();
1786 Comps.back().LocStart = ST.getOpenLocation();
Chris Lattner11124352006-08-12 19:16:08 +00001787 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001788 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001789 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001790 return move(Res);
Chris Lattner11124352006-08-12 19:16:08 +00001791 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001792 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001793
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001794 ST.consumeClose();
1795 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman5e774b12009-06-27 20:38:33 +00001796 } else {
1797 if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001798 PT.consumeClose();
Douglas Gregor220cac52009-02-18 17:45:20 +00001799 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001800 } else if (Ty.isInvalid()) {
1801 Res = ExprError();
1802 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001803 PT.consumeClose();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001804 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001805 Ty.get(), &Comps[0], Comps.size(),
1806 PT.getCloseLocation());
Eli Friedman5e774b12009-06-27 20:38:33 +00001807 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001808 break;
Chris Lattner11124352006-08-12 19:16:08 +00001809 }
1810 }
1811 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001812 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001813 case tok::kw___builtin_choose_expr: {
John McCalldadc5752010-08-24 06:29:42 +00001814 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001815 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001816 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001817 return move(Cond);
Steve Naroff9efdabc2007-08-03 21:21:27 +00001818 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001819 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001820 return ExprError();
1821
John McCalldadc5752010-08-24 06:29:42 +00001822 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001823 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001824 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001825 return move(Expr1);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001826 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001827 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001828 return ExprError();
1829
John McCalldadc5752010-08-24 06:29:42 +00001830 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001831 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001832 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001833 return move(Expr2);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001834 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001835 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001836 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001837 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001838 }
John McCallb268a282010-08-23 23:25:46 +00001839 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1840 Expr2.take(), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001841 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001842 }
Tanya Lattner55808c12011-06-04 00:47:47 +00001843 case tok::kw___builtin_astype: {
1844 // The first argument is an expression to be converted, followed by a comma.
1845 ExprResult Expr(ParseAssignmentExpression());
1846 if (Expr.isInvalid()) {
1847 SkipUntil(tok::r_paren);
1848 return ExprError();
1849 }
1850
1851 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1852 tok::r_paren))
1853 return ExprError();
1854
1855 // Second argument is the type to bitcast to.
1856 TypeResult DestTy = ParseTypeName();
1857 if (DestTy.isInvalid())
1858 return ExprError();
1859
1860 // Attempt to consume the r-paren.
1861 if (Tok.isNot(tok::r_paren)) {
1862 Diag(Tok, diag::err_expected_rparen);
1863 SkipUntil(tok::r_paren);
1864 return ExprError();
1865 }
1866
1867 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1868 ConsumeParen());
1869 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001870 }
Chandler Carruth904cb142011-07-08 04:59:44 +00001871 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001872
John McCallb268a282010-08-23 23:25:46 +00001873 if (Res.isInvalid())
1874 return ExprError();
1875
Chris Lattner11124352006-08-12 19:16:08 +00001876 // These can be followed by postfix-expr pieces because they are
1877 // primary-expressions.
John McCallb268a282010-08-23 23:25:46 +00001878 return ParsePostfixExpressionSuffix(Res.take());
Chris Lattner11124352006-08-12 19:16:08 +00001879}
1880
Chris Lattner4add4e62006-08-11 01:33:00 +00001881/// ParseParenExpression - This parses the unit that starts with a '(' token,
1882/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001883/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1884/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001885///
James Dennett3d5e4592012-06-17 04:36:28 +00001886/// \verbatim
Chris Lattner4add4e62006-08-11 01:33:00 +00001887/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001888/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001889/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1890/// postfix-expression: [C99 6.5.2]
1891/// '(' type-name ')' '{' initializer-list '}'
1892/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001893/// cast-expression: [C99 6.5.4]
1894/// '(' type-name ')' cast-expression
John McCall31168b02011-06-15 23:02:42 +00001895/// [ARC] bridged-cast-expression
1896///
1897/// [ARC] bridged-cast-expression:
1898/// (__bridge type-name) cast-expression
1899/// (__bridge_transfer type-name) cast-expression
1900/// (__bridge_retained type-name) cast-expression
James Dennett3d5e4592012-06-17 04:36:28 +00001901/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001902ExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001903Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001904 bool isTypeCast, ParsedType &CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001905 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001906 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001907 BalancedDelimiterTracker T(*this, tok::l_paren);
1908 if (T.consumeOpen())
1909 return ExprError();
1910 SourceLocation OpenLoc = T.getOpenLocation();
1911
John McCalldadc5752010-08-24 06:29:42 +00001912 ExprResult Result(true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001913 bool isAmbiguousTypeId;
John McCallba7bf592010-08-24 05:47:05 +00001914 CastTy = ParsedType();
Sebastian Redl90893182008-12-11 22:33:27 +00001915
Douglas Gregor5e35d592010-09-14 23:59:36 +00001916 if (Tok.is(tok::code_completion)) {
1917 Actions.CodeCompleteOrdinaryName(getCurScope(),
1918 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1919 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001920 cutOffParsing();
Douglas Gregor5e35d592010-09-14 23:59:36 +00001921 return ExprError();
1922 }
John McCallc5e6b972011-04-06 02:35:25 +00001923
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001924 // Diagnose use of bridge casts in non-arc mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001925 bool BridgeCast = (getLangOpts().ObjC2 &&
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001926 (Tok.is(tok::kw___bridge) ||
1927 Tok.is(tok::kw___bridge_transfer) ||
1928 Tok.is(tok::kw___bridge_retained) ||
1929 Tok.is(tok::kw___bridge_retain)));
David Blaikiebbafb8a2012-03-11 07:00:24 +00001930 if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001931 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001932 SourceLocation BridgeKeywordLoc = ConsumeToken();
1933 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremenek084e1b42012-02-18 04:42:38 +00001934 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001935 << BridgeCastName
1936 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001937 BridgeCast = false;
1938 }
1939
John McCallc5e6b972011-04-06 02:35:25 +00001940 // None of these cases should fall through with an invalid Result
1941 // unless they've already reported an error.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001942 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001943 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall3abee492012-04-04 01:27:53 +00001944 Actions.ActOnStartStmtExpr();
1945
Richard Smithc202b282012-04-14 00:33:13 +00001946 StmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001947 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001948
Chris Lattner366727f2007-07-24 16:58:17 +00001949 // If the substmt parsed correctly, build the AST node.
John McCall3abee492012-04-04 01:27:53 +00001950 if (!Stmt.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001951 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
John McCall3abee492012-04-04 01:27:53 +00001952 } else {
1953 Actions.ActOnStmtExprError();
1954 }
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001955 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCall0c07bee2011-06-17 21:56:12 +00001956 tok::TokenKind tokenKind = Tok.getKind();
1957 SourceLocation BridgeKeywordLoc = ConsumeToken();
1958
John McCall31168b02011-06-15 23:02:42 +00001959 // Parse an Objective-C ARC ownership cast expression.
1960 ObjCBridgeCastKind Kind;
John McCall0c07bee2011-06-17 21:56:12 +00001961 if (tokenKind == tok::kw___bridge)
John McCall31168b02011-06-15 23:02:42 +00001962 Kind = OBC_Bridge;
John McCall0c07bee2011-06-17 21:56:12 +00001963 else if (tokenKind == tok::kw___bridge_transfer)
John McCall31168b02011-06-15 23:02:42 +00001964 Kind = OBC_BridgeTransfer;
John McCall0c07bee2011-06-17 21:56:12 +00001965 else if (tokenKind == tok::kw___bridge_retained)
John McCall31168b02011-06-15 23:02:42 +00001966 Kind = OBC_BridgeRetained;
John McCall0c07bee2011-06-17 21:56:12 +00001967 else {
1968 // As a hopefully temporary workaround, allow __bridge_retain as
1969 // a synonym for __bridge_retained, but only in system headers.
1970 assert(tokenKind == tok::kw___bridge_retain);
1971 Kind = OBC_BridgeRetained;
1972 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1973 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1974 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1975 "__bridge_retained");
1976 }
John McCall31168b02011-06-15 23:02:42 +00001977
John McCall31168b02011-06-15 23:02:42 +00001978 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001979 T.consumeClose();
1980 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001981 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCall31168b02011-06-15 23:02:42 +00001982
1983 if (Ty.isInvalid() || SubExpr.isInvalid())
1984 return ExprError();
1985
1986 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1987 BridgeKeywordLoc, Ty.get(),
1988 RParenLoc, SubExpr.get());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001989 } else if (ExprType >= CompoundLiteral &&
1990 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00001991
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001992 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00001993
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001994 // In C++, if the type-id is ambiguous we disambiguate based on context.
1995 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1996 // in which case we should treat it as type-id.
1997 // if stopIfCastExpr is false, we need to determine the context past the
1998 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001999 if (isAmbiguousTypeId && !stopIfCastExpr) {
2000 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
2001 RParenLoc = T.getCloseLocation();
2002 return res;
2003 }
Mike Stump11289f42009-09-09 15:08:12 +00002004
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002005 // Parse the type declarator.
2006 DeclSpec DS(AttrFactory);
2007 ParseSpecifierQualifierList(DS);
2008 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2009 ParseDeclarator(DeclaratorInfo);
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002010
Douglas Gregor3e972002010-09-15 23:19:31 +00002011 // If our type is followed by an identifier and either ':' or ']', then
2012 // this is probably an Objective-C message send where the leading '[' is
2013 // missing. Recover as if that were the case.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002014 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002015 !InMessageExpression && getLangOpts().ObjC1 &&
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002016 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
2017 TypeResult Ty;
2018 {
2019 InMessageExpressionRAIIObject InMessage(*this, false);
2020 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2021 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002022 Result = ParseObjCMessageExpressionBody(SourceLocation(),
2023 SourceLocation(),
2024 Ty.get(), 0);
2025 } else {
2026 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002027 T.consumeClose();
2028 RParenLoc = T.getCloseLocation();
Douglas Gregor3e972002010-09-15 23:19:31 +00002029 if (Tok.is(tok::l_brace)) {
2030 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002031 TypeResult Ty;
2032 {
2033 InMessageExpressionRAIIObject InMessage(*this, false);
2034 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2035 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002036 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +00002037 }
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00002038
Douglas Gregor3e972002010-09-15 23:19:31 +00002039 if (ExprType == CastExpr) {
2040 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb5d49352009-01-19 22:31:54 +00002041
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor3e972002010-09-15 23:19:31 +00002043 return ExprError();
2044
Douglas Gregor3e972002010-09-15 23:19:31 +00002045 // Note that this doesn't parse the subsequent cast-expression, it just
2046 // returns the parsed type to the callee.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002047 if (stopIfCastExpr) {
2048 TypeResult Ty;
2049 {
2050 InMessageExpressionRAIIObject InMessage(*this, false);
2051 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2052 }
2053 CastTy = Ty.get();
Douglas Gregor3e972002010-09-15 23:19:31 +00002054 return ExprResult();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002055 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002056
2057 // Reject the cast of super idiom in ObjC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002058 if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
Douglas Gregor3e972002010-09-15 23:19:31 +00002059 Tok.getIdentifierInfo() == Ident_super &&
2060 getCurScope()->isInObjcMethodScope() &&
2061 GetLookAheadToken(1).isNot(tok::period)) {
2062 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2063 << SourceRange(OpenLoc, RParenLoc);
2064 return ExprError();
2065 }
2066
2067 // Parse the cast-expression that follows it next.
2068 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002069 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2070 /*isAddressOfOperand=*/false,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002071 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002072 if (!Result.isInvalid()) {
2073 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2074 DeclaratorInfo, CastTy,
Douglas Gregor3e972002010-09-15 23:19:31 +00002075 RParenLoc, Result.take());
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002076 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002077 return move(Result);
2078 }
2079
2080 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2081 return ExprError();
2082 }
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002083 } else if (isTypeCast) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00002084 // Parse the expression-list.
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002085 InMessageExpressionRAIIObject InMessage(*this, false);
2086
Nate Begeman5ec4b312009-08-10 23:49:36 +00002087 ExprVector ArgExprs(Actions);
2088 CommaLocsTy CommaLocs;
2089
2090 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2091 ExprType = SimpleExpr;
Sebastian Redla9351792012-02-11 23:51:47 +00002092 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
2093 move_arg(ArgExprs));
Nate Begeman5ec4b312009-08-10 23:49:36 +00002094 }
Chris Lattner4add4e62006-08-11 01:33:00 +00002095 } else {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002096 InMessageExpressionRAIIObject InMessage(*this, false);
2097
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002098 Result = ParseExpression(MaybeTypeCast);
Chris Lattner4add4e62006-08-11 01:33:00 +00002099 ExprType = SimpleExpr;
John McCallc5e6b972011-04-06 02:35:25 +00002100
2101 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002102 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCallb268a282010-08-23 23:25:46 +00002103 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Chris Lattnerf8339772006-08-10 22:01:51 +00002104 }
Sebastian Redl90893182008-12-11 22:33:27 +00002105
Chris Lattner4564bc12006-08-10 23:14:52 +00002106 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00002107 if (Result.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +00002108 SkipUntil(tok::r_paren);
Chris Lattnerd8980502008-12-12 06:00:12 +00002109 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00002110 }
Mike Stump11289f42009-09-09 15:08:12 +00002111
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002112 T.consumeClose();
2113 RParenLoc = T.getCloseLocation();
Sebastian Redl90893182008-12-11 22:33:27 +00002114 return move(Result);
Chris Lattnerc951dae2006-08-10 04:23:57 +00002115}
Chris Lattnerd3e98952006-10-06 05:22:26 +00002116
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002117/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2118/// and we are at the left brace.
2119///
James Dennett3d5e4592012-06-17 04:36:28 +00002120/// \verbatim
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002121/// postfix-expression: [C99 6.5.2]
2122/// '(' type-name ')' '{' initializer-list '}'
2123/// '(' type-name ')' '{' initializer-list ',' '}'
James Dennett3d5e4592012-06-17 04:36:28 +00002124/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002125ExprResult
John McCallba7bf592010-08-24 05:47:05 +00002126Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002127 SourceLocation LParenLoc,
2128 SourceLocation RParenLoc) {
2129 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
David Blaikiebbafb8a2012-03-11 07:00:24 +00002130 if (!getLangOpts().C99) // Compound literals don't exist in C90.
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002131 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCalldadc5752010-08-24 06:29:42 +00002132 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002133 if (!Result.isInvalid() && Ty)
John McCallb268a282010-08-23 23:25:46 +00002134 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002135 return move(Result);
2136}
2137
Chris Lattnerd3e98952006-10-06 05:22:26 +00002138/// ParseStringLiteralExpression - This handles the various token types that
2139/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2140/// translation phase #6].
2141///
James Dennett3d5e4592012-06-17 04:36:28 +00002142/// \verbatim
Chris Lattnerd3e98952006-10-06 05:22:26 +00002143/// primary-expression: [C99 6.5.1]
2144/// string-literal
James Dennett3d5e4592012-06-17 04:36:28 +00002145/// \verbatim
Richard Smithd67aea22012-03-06 03:21:47 +00002146ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002147 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00002148
Chris Lattnerd3e98952006-10-06 05:22:26 +00002149 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2150 // considered to be strings for concatenation purposes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002151 SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00002152
Chris Lattnerd3e98952006-10-06 05:22:26 +00002153 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002154 StringToks.push_back(Tok);
2155 ConsumeStringToken();
2156 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00002157
2158 // Pass the set of string tokens, ready for concatenation, to the actions.
Richard Smithbcc22fc2012-03-09 08:00:36 +00002159 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(),
2160 AllowUserDefinedLiteral ? getCurScope() : 0);
Chris Lattnerd3e98952006-10-06 05:22:26 +00002161}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002162
Benjamin Kramere56f3932011-12-23 17:00:35 +00002163/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2164/// [C11 6.5.1.1].
Peter Collingbourne91147592011-04-15 00:35:48 +00002165///
James Dennett3d5e4592012-06-17 04:36:28 +00002166/// \verbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002167/// generic-selection:
2168/// _Generic ( assignment-expression , generic-assoc-list )
2169/// generic-assoc-list:
2170/// generic-association
2171/// generic-assoc-list , generic-association
2172/// generic-association:
2173/// type-name : assignment-expression
2174/// default : assignment-expression
James Dennett3d5e4592012-06-17 04:36:28 +00002175/// \endverbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002176ExprResult Parser::ParseGenericSelectionExpression() {
2177 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2178 SourceLocation KeyLoc = ConsumeToken();
2179
David Blaikiebbafb8a2012-03-11 07:00:24 +00002180 if (!getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +00002181 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbourne91147592011-04-15 00:35:48 +00002182
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002183 BalancedDelimiterTracker T(*this, tok::l_paren);
2184 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne91147592011-04-15 00:35:48 +00002185 return ExprError();
2186
2187 ExprResult ControllingExpr;
2188 {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002189 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbourne91147592011-04-15 00:35:48 +00002190 // not evaluated."
2191 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2192 ControllingExpr = ParseAssignmentExpression();
2193 if (ControllingExpr.isInvalid()) {
2194 SkipUntil(tok::r_paren);
2195 return ExprError();
2196 }
2197 }
2198
2199 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2200 SkipUntil(tok::r_paren);
2201 return ExprError();
2202 }
2203
2204 SourceLocation DefaultLoc;
2205 TypeVector Types(Actions);
2206 ExprVector Exprs(Actions);
2207 while (1) {
2208 ParsedType Ty;
2209 if (Tok.is(tok::kw_default)) {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002210 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbourne91147592011-04-15 00:35:48 +00002211 // generic association."
2212 if (!DefaultLoc.isInvalid()) {
2213 Diag(Tok, diag::err_duplicate_default_assoc);
2214 Diag(DefaultLoc, diag::note_previous_default_assoc);
2215 SkipUntil(tok::r_paren);
2216 return ExprError();
2217 }
2218 DefaultLoc = ConsumeToken();
2219 Ty = ParsedType();
2220 } else {
2221 ColonProtectionRAIIObject X(*this);
2222 TypeResult TR = ParseTypeName();
2223 if (TR.isInvalid()) {
2224 SkipUntil(tok::r_paren);
2225 return ExprError();
2226 }
2227 Ty = TR.release();
2228 }
2229 Types.push_back(Ty);
2230
2231 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2232 SkipUntil(tok::r_paren);
2233 return ExprError();
2234 }
2235
2236 // FIXME: These expressions should be parsed in a potentially potentially
2237 // evaluated context.
2238 ExprResult ER(ParseAssignmentExpression());
2239 if (ER.isInvalid()) {
2240 SkipUntil(tok::r_paren);
2241 return ExprError();
2242 }
2243 Exprs.push_back(ER.release());
2244
2245 if (Tok.isNot(tok::comma))
2246 break;
2247 ConsumeToken();
2248 }
2249
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002250 T.consumeClose();
2251 if (T.getCloseLocation().isInvalid())
Peter Collingbourne91147592011-04-15 00:35:48 +00002252 return ExprError();
2253
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002254 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2255 T.getCloseLocation(),
Peter Collingbourne91147592011-04-15 00:35:48 +00002256 ControllingExpr.release(),
2257 move_arg(Types), move_arg(Exprs));
2258}
2259
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002260/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2261///
James Dennett3d5e4592012-06-17 04:36:28 +00002262/// \verbatim
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002263/// argument-expression-list:
2264/// assignment-expression
2265/// argument-expression-list , assignment-expression
2266///
2267/// [C++] expression-list:
Sebastian Redl3da34892011-06-05 12:23:16 +00002268/// [C++] assignment-expression
2269/// [C++] expression-list , assignment-expression
2270///
2271/// [C++0x] expression-list:
2272/// [C++0x] initializer-list
2273///
2274/// [C++0x] initializer-list
2275/// [C++0x] initializer-clause ...[opt]
2276/// [C++0x] initializer-list , initializer-clause ...[opt]
2277///
2278/// [C++0x] initializer-clause:
2279/// [C++0x] assignment-expression
2280/// [C++0x] braced-init-list
James Dennett3d5e4592012-06-17 04:36:28 +00002281/// \endverbatim
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002282bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2283 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallfaf5fb42010-08-26 23:41:50 +00002284 void (Sema::*Completer)(Scope *S,
John McCall37ad5512010-08-23 06:44:23 +00002285 Expr *Data,
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002286 llvm::ArrayRef<Expr *> Args),
John McCall37ad5512010-08-23 06:44:23 +00002287 Expr *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002288 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00002289 if (Tok.is(tok::code_completion)) {
2290 if (Completer)
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002291 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor9c1f1bf2011-02-17 03:09:23 +00002292 else
2293 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002294 cutOffParsing();
2295 return true;
Douglas Gregorcabea402009-09-22 15:41:20 +00002296 }
Sebastian Redl3da34892011-06-05 12:23:16 +00002297
2298 ExprResult Expr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002299 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002300 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00002301 Expr = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00002302 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00002303 Expr = ParseAssignmentExpression();
2304
Douglas Gregor968f23a2011-01-03 19:31:53 +00002305 if (Tok.is(tok::ellipsis))
2306 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002307 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002308 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00002309
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002310 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002311
2312 if (Tok.isNot(tok::comma))
2313 return false;
2314 // Move to the next argument, remember where the comma was.
2315 CommaLocs.push_back(ConsumeToken());
2316 }
2317}
Steve Naroff0ac012832008-08-28 19:20:44 +00002318
Mike Stump82f071f2009-02-04 22:31:32 +00002319/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2320///
James Dennett3d5e4592012-06-17 04:36:28 +00002321/// \verbatim
Mike Stump82f071f2009-02-04 22:31:32 +00002322/// [clang] block-id:
2323/// [clang] specifier-qualifier-list block-declarator
James Dennett3d5e4592012-06-17 04:36:28 +00002324/// \endverbatim
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002325void Parser::ParseBlockId(SourceLocation CaretLoc) {
Douglas Gregor643c3302010-10-18 21:34:55 +00002326 if (Tok.is(tok::code_completion)) {
2327 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002328 return cutOffParsing();
Douglas Gregor643c3302010-10-18 21:34:55 +00002329 }
2330
Mike Stump82f071f2009-02-04 22:31:32 +00002331 // Parse the specifier-qualifier-list piece.
John McCall084e83d2011-03-24 11:26:52 +00002332 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002333 ParseSpecifierQualifierList(DS);
2334
2335 // Parse the block-declarator.
2336 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2337 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002338
Mike Stump56ed2ea2009-04-29 21:40:37 +00002339 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall084e83d2011-03-24 11:26:52 +00002340 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump56ed2ea2009-04-29 21:40:37 +00002341
John McCall53fa7142010-12-24 02:08:15 +00002342 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002343
Mike Stump82f071f2009-02-04 22:31:32 +00002344 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002345 Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
Mike Stump82f071f2009-02-04 22:31:32 +00002346}
2347
Steve Naroff0ac012832008-08-28 19:20:44 +00002348/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00002349/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00002350///
James Dennett3d5e4592012-06-17 04:36:28 +00002351/// \verbatim
Steve Naroff0ac012832008-08-28 19:20:44 +00002352/// block-literal:
2353/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00002354/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00002355/// [clang] block-args:
2356/// [clang] '(' parameter-list ')'
James Dennett3d5e4592012-06-17 04:36:28 +00002357/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002358ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00002359 assert(Tok.is(tok::caret) && "block literal starts with ^");
2360 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002361
Chris Lattnerf6801202009-03-05 07:32:12 +00002362 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2363 "block literal parsing");
2364
Mike Stump11289f42009-09-09 15:08:12 +00002365 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00002366 // argument decls, decls within the compound expression, etc. This also
2367 // allows determining whether a variable reference inside the block is
2368 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002369 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002370 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00002371
2372 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002373 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump11289f42009-09-09 15:08:12 +00002374
Steve Naroff0ac012832008-08-28 19:20:44 +00002375 // Parse the return type if present.
John McCall084e83d2011-03-24 11:26:52 +00002376 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002377 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002378 // FIXME: Since the return type isn't actually parsed, it can't be used to
2379 // fill ParamInfo with an initial valid range, so do it manually.
2380 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002381
Steve Naroff0ac012832008-08-28 19:20:44 +00002382 // If this block has arguments, parse them. There is no ambiguity here with
2383 // the expression case, because the expression case requires a parameter list.
2384 if (Tok.is(tok::l_paren)) {
2385 ParseParenDeclarator(ParamInfo);
2386 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002387 // SetIdentifier sets the source range end, but in this case we're past
2388 // that location.
2389 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00002390 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002391 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002392 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00002393 // If there was an error parsing the arguments, they may have
2394 // tried to use ^(x+y) which requires an argument list. Just
2395 // skip the whole block literal.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002396 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002397 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00002398 }
Mike Stump88788fe2009-04-29 19:03:13 +00002399
John McCall53fa7142010-12-24 02:08:15 +00002400 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002401
Mike Stump82f071f2009-02-04 22:31:32 +00002402 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002403 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Mike Stumpd73e4412009-04-14 18:24:37 +00002404 } else if (!Tok.is(tok::l_brace)) {
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002405 ParseBlockId(CaretLoc);
Steve Naroff0ac012832008-08-28 19:20:44 +00002406 } else {
2407 // Otherwise, pretend we saw (void).
John McCall084e83d2011-03-24 11:26:52 +00002408 ParsedAttributes attrs(AttrFactory);
2409 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002410 SourceLocation(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002411 0, 0, 0,
Douglas Gregor54992352011-01-26 03:43:54 +00002412 true, SourceLocation(),
Douglas Gregorad69e652011-07-13 21:47:47 +00002413 SourceLocation(),
Douglas Gregore248eea2011-10-19 06:04:55 +00002414 SourceLocation(),
2415 SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00002416 EST_None,
2417 SourceLocation(),
Richard Smith2331bbf2012-05-02 22:22:32 +00002418 0, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002419 CaretLoc, CaretLoc,
2420 ParamInfo),
John McCall084e83d2011-03-24 11:26:52 +00002421 attrs, CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00002422
John McCall53fa7142010-12-24 02:08:15 +00002423 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002424
Mike Stump82f071f2009-02-04 22:31:32 +00002425 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002426 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Steve Naroff0ac012832008-08-28 19:20:44 +00002427 }
2428
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002429
John McCalldadc5752010-08-24 06:29:42 +00002430 ExprResult Result(true);
Chris Lattner9eac9312009-03-27 04:18:06 +00002431 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002432 // Saw something like: ^expr
2433 Diag(Tok, diag::err_expected_expression);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002434 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002435 return ExprError();
2436 }
Mike Stump11289f42009-09-09 15:08:12 +00002437
John McCalldadc5752010-08-24 06:29:42 +00002438 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002439 BlockScope.Exit();
Chris Lattner9eac9312009-03-27 04:18:06 +00002440 if (!Stmt.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002441 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9eac9312009-03-27 04:18:06 +00002442 else
Douglas Gregor0be31a22010-07-02 17:43:08 +00002443 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002444 return move(Result);
Steve Naroff0ac012832008-08-28 19:20:44 +00002445}
Ted Kremeneke65b0862012-03-06 20:05:56 +00002446
2447/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
2448///
2449/// '__objc_yes'
2450/// '__objc_no'
2451ExprResult Parser::ParseObjCBoolLiteral() {
2452 tok::TokenKind Kind = Tok.getKind();
2453 return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
2454}