blob: 6c4d6fbee80e80835265903569ca49f42698fba2 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc951dae2006-08-10 04:23:57 +00007//
8//===----------------------------------------------------------------------===//
James Dennette37835e2012-06-19 21:02:26 +00009///
James Dennett3d5e4592012-06-17 04:36:28 +000010/// \file
11/// \brief Provides the Expression parsing implementation.
12///
13/// Expressions in C99 basically consist of a bunch of binary operators with
14/// unary operators and other random stuff at the leaves.
15///
16/// In the C99 grammar, these unary operators bind tightest and are represented
17/// as the 'cast-expression' production. Everything else is either a binary
18/// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
19/// handled by ParseCastExpression, the higher level pieces are handled by
20/// ParseBinaryExpression.
James Dennette37835e2012-06-19 21:02:26 +000021///
22//===----------------------------------------------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +000023
24#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/Scope.h"
27#include "clang/Sema/ParsedTemplate.h"
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +000028#include "clang/Sema/TypoCorrection.h"
Chris Lattnerf6801202009-03-05 07:32:12 +000029#include "clang/Basic/PrettyStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000030#include "RAIIObjectsForParser.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000031#include "llvm/ADT/SmallVector.h"
Chris Lattner834618d2006-11-03 07:48:41 +000032#include "llvm/ADT/SmallString.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000033using namespace clang;
34
James Dennett3d5e4592012-06-17 04:36:28 +000035/// \brief Return the precedence of the specified binary operator token.
Mike Stump11289f42009-09-09 15:08:12 +000036static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregorcbb45d02009-02-25 23:02:36 +000037 bool GreaterThanIsOperator,
38 bool CPlusPlus0x) {
Chris Lattnercde626a2006-08-12 08:13:25 +000039 switch (Kind) {
Douglas Gregor8bf42052009-02-09 18:46:07 +000040 case tok::greater:
Douglas Gregorcbb45d02009-02-25 23:02:36 +000041 // C++ [temp.names]p3:
42 // [...] When parsing a template-argument-list, the first
43 // non-nested > is taken as the ending delimiter rather than a
44 // greater-than operator. [...]
Douglas Gregor8bf42052009-02-09 18:46:07 +000045 if (GreaterThanIsOperator)
46 return prec::Relational;
47 return prec::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +000048
Douglas Gregorcbb45d02009-02-25 23:02:36 +000049 case tok::greatergreater:
50 // C++0x [temp.names]p3:
51 //
52 // [...] Similarly, the first non-nested >> is treated as two
53 // consecutive but distinct > tokens, the first of which is
54 // taken as the end of the template-argument-list and completes
55 // the template-id. [...]
56 if (GreaterThanIsOperator || !CPlusPlus0x)
57 return prec::Shift;
58 return prec::Unknown;
59
Chris Lattnercde626a2006-08-12 08:13:25 +000060 default: return prec::Unknown;
61 case tok::comma: return prec::Comma;
62 case tok::equal:
63 case tok::starequal:
64 case tok::slashequal:
65 case tok::percentequal:
66 case tok::plusequal:
67 case tok::minusequal:
68 case tok::lesslessequal:
69 case tok::greatergreaterequal:
70 case tok::ampequal:
71 case tok::caretequal:
72 case tok::pipeequal: return prec::Assignment;
73 case tok::question: return prec::Conditional;
74 case tok::pipepipe: return prec::LogicalOr;
75 case tok::ampamp: return prec::LogicalAnd;
76 case tok::pipe: return prec::InclusiveOr;
77 case tok::caret: return prec::ExclusiveOr;
78 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +000079 case tok::exclaimequal:
80 case tok::equalequal: return prec::Equality;
81 case tok::lessequal:
82 case tok::less:
Douglas Gregor8bf42052009-02-09 18:46:07 +000083 case tok::greaterequal: return prec::Relational;
Douglas Gregorcbb45d02009-02-25 23:02:36 +000084 case tok::lessless: return prec::Shift;
Chris Lattnercde626a2006-08-12 08:13:25 +000085 case tok::plus:
86 case tok::minus: return prec::Additive;
87 case tok::percent:
88 case tok::slash:
89 case tok::star: return prec::Multiplicative;
Sebastian Redl112a97662009-02-07 00:15:38 +000090 case tok::periodstar:
91 case tok::arrowstar: return prec::PointerToMember;
Chris Lattnercde626a2006-08-12 08:13:25 +000092 }
93}
94
95
James Dennett3d5e4592012-06-17 04:36:28 +000096/// \brief Simple precedence-based parser for binary/ternary operators.
Chris Lattnercde626a2006-08-12 08:13:25 +000097///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000098/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
James Dennett3d5e4592012-06-17 04:36:28 +0000107/// \verbatim
Sebastian Redl112a97662009-02-07 00:15:38 +0000108/// pm-expression: [C++ 5.5]
109/// cast-expression
110/// pm-expression '.*' cast-expression
111/// pm-expression '->*' cast-expression
112///
Chris Lattnercde626a2006-08-12 08:13:25 +0000113/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl112a97662009-02-07 00:15:38 +0000114/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000115/// cast-expression
116/// multiplicative-expression '*' cast-expression
117/// multiplicative-expression '/' cast-expression
118/// multiplicative-expression '%' cast-expression
119///
120/// additive-expression: [C99 6.5.6]
121/// multiplicative-expression
122/// additive-expression '+' multiplicative-expression
123/// additive-expression '-' multiplicative-expression
124///
125/// shift-expression: [C99 6.5.7]
126/// additive-expression
127/// shift-expression '<<' additive-expression
128/// shift-expression '>>' additive-expression
129///
130/// relational-expression: [C99 6.5.8]
131/// shift-expression
132/// relational-expression '<' shift-expression
133/// relational-expression '>' shift-expression
134/// relational-expression '<=' shift-expression
135/// relational-expression '>=' shift-expression
136///
137/// equality-expression: [C99 6.5.9]
138/// relational-expression
139/// equality-expression '==' relational-expression
140/// equality-expression '!=' relational-expression
141///
142/// AND-expression: [C99 6.5.10]
143/// equality-expression
144/// AND-expression '&' equality-expression
145///
146/// exclusive-OR-expression: [C99 6.5.11]
147/// AND-expression
148/// exclusive-OR-expression '^' AND-expression
149///
150/// inclusive-OR-expression: [C99 6.5.12]
151/// exclusive-OR-expression
152/// inclusive-OR-expression '|' exclusive-OR-expression
153///
154/// logical-AND-expression: [C99 6.5.13]
155/// inclusive-OR-expression
156/// logical-AND-expression '&&' inclusive-OR-expression
157///
158/// logical-OR-expression: [C99 6.5.14]
159/// logical-AND-expression
160/// logical-OR-expression '||' logical-AND-expression
161///
162/// conditional-expression: [C99 6.5.15]
163/// logical-OR-expression
164/// logical-OR-expression '?' expression ':' conditional-expression
165/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl1a99f442009-04-16 17:51:27 +0000166/// [C++] the third operand is an assignment-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000167///
168/// assignment-expression: [C99 6.5.16]
169/// conditional-expression
170/// unary-expression assignment-operator assignment-expression
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000171/// [C++] throw-expression [C++ 15]
Chris Lattnercde626a2006-08-12 08:13:25 +0000172///
173/// assignment-operator: one of
174/// = *= /= %= += -= <<= >>= &= ^= |=
175///
176/// expression: [C99 6.5.17]
Douglas Gregor968f23a2011-01-03 19:31:53 +0000177/// assignment-expression ...[opt]
178/// expression ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +0000179/// \endverbatim
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000180ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
181 ExprResult LHS(ParseAssignmentExpression(isTypeCast));
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000182 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Chris Lattnercde626a2006-08-12 08:13:25 +0000183}
184
Mike Stump11289f42009-09-09 15:08:12 +0000185/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000186/// Current token is an Identifier and is not a 'try'. This
James Dennettf44874f2012-06-15 06:52:33 +0000187/// routine is necessary to disambiguate \@try-statement from,
188/// for example, \@encode-expression.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000189///
John McCalldadc5752010-08-24 06:29:42 +0000190ExprResult
Sebastian Redl90893182008-12-11 22:33:27 +0000191Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000192 ExprResult LHS(ParseObjCAtExpression(AtLoc));
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000193 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000194}
195
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000196/// This routine is called when a leading '__extension__' is seen and
197/// consumed. This is necessary because the token gets consumed in the
198/// process of disambiguating between an expression and a declaration.
John McCalldadc5752010-08-24 06:29:42 +0000199ExprResult
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000200Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000201 ExprResult LHS(true);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000202 {
203 // Silence extension warnings in the sub-expression
204 ExtensionRAIIObject O(Diags);
205
206 LHS = ParseCastExpression(false);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000207 }
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000208
Douglas Gregor29d907d2010-09-17 22:25:06 +0000209 if (!LHS.isInvalid())
210 LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
211 LHS.take());
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000212
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000213 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000214}
215
James Dennett3d5e4592012-06-17 04:36:28 +0000216/// \brief Parse an expr that doesn't include (top-level) commas.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000217ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000218 if (Tok.is(tok::code_completion)) {
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000219 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000220 cutOffParsing();
221 return ExprError();
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000222 }
223
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000224 if (Tok.is(tok::kw_throw))
Sebastian Redld65cea82008-12-11 22:51:44 +0000225 return ParseThrowExpression();
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000226
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000227 ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
228 /*isAddressOfOperand=*/false,
229 isTypeCast);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000230 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000231}
232
James Dennett3d5e4592012-06-17 04:36:28 +0000233/// \brief Parse an assignment expression where part of an Objective-C message
234/// send has already been parsed.
235///
236/// In this case \p LBracLoc indicates the location of the '[' of the message
237/// send, and either \p ReceiverName or \p ReceiverExpr is non-null indicating
238/// the receiver of the message.
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000239///
240/// Since this handles full assignment-expression's, it handles postfix
241/// expressions and other binary operators for these expressions as well.
John McCalldadc5752010-08-24 06:29:42 +0000242ExprResult
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000243Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000244 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +0000245 ParsedType ReceiverType,
John McCallb268a282010-08-23 23:25:46 +0000246 Expr *ReceiverExpr) {
John McCalldadc5752010-08-24 06:29:42 +0000247 ExprResult R
John McCallb268a282010-08-23 23:25:46 +0000248 = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
249 ReceiverType, ReceiverExpr);
Douglas Gregoreda7e542010-09-18 01:28:11 +0000250 R = ParsePostfixExpressionSuffix(R);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000251 return ParseRHSOfBinaryExpression(R, prec::Assignment);
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000252}
253
254
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000255ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
Richard Smith764d2fe2011-12-20 02:08:33 +0000256 // C++03 [basic.def.odr]p2:
Mike Stump11289f42009-09-09 15:08:12 +0000257 // An expression is potentially evaluated unless it appears where an
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000258 // integral constant expression is required (see 5.19) [...].
Richard Smith764d2fe2011-12-20 02:08:33 +0000259 // C++98 and C++11 have no such rule, but this is only a defect in C++98.
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000260 EnterExpressionEvaluationContext Unevaluated(Actions,
Richard Smith764d2fe2011-12-20 02:08:33 +0000261 Sema::ConstantEvaluated);
Mike Stump11289f42009-09-09 15:08:12 +0000262
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000263 ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
Eli Friedmanc6237c62012-02-29 03:16:56 +0000264 ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
265 return Actions.ActOnConstantExpression(Res);
Chris Lattner3b561a32006-08-13 00:12:11 +0000266}
267
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)
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000282 return 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);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000461 return Res;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000462}
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 ')'
Jordan Rose58d54722012-06-30 21:33:57 +0000510/// [C11] '_Alignof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000511/// [C++11] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000512/// [GNU] '&&' identifier
Richard Smithd67aea22012-03-06 03:21:47 +0000513/// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7]
Sebastian Redlbd150f42008-11-21 19:14:01 +0000514/// [C++] new-expression
515/// [C++] delete-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000516///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000517/// unary-operator: one of
518/// '&' '*' '+' '-' '~' '!'
519/// [GNU] '__extension__' '__real' '__imag'
520///
Chris Lattner52a99e52006-08-10 20:56:00 +0000521/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000522/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000523/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000524/// constant
525/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000526/// [C++] boolean-literal [C++ 2.13.5]
Richard Smithd67aea22012-03-06 03:21:47 +0000527/// [C++11] 'nullptr' [C++11 2.14.7]
528/// [C++11] user-defined-literal
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000529/// '(' expression ')'
Benjamin Kramere56f3932011-12-23 17:00:35 +0000530/// [C11] generic-selection
Chris Lattner52a99e52006-08-10 20:56:00 +0000531/// '__func__' [C99 6.4.2.2]
532/// [GNU] '__FUNCTION__'
533/// [GNU] '__PRETTY_FUNCTION__'
534/// [GNU] '(' compound-statement ')'
535/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
536/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
537/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
538/// assign-expr ')'
539/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000540/// [GNU] '__null'
Mike Stump11289f42009-09-09 15:08:12 +0000541/// [OBJC] '[' objc-message-expr ']'
James Dennettf44874f2012-06-15 06:52:33 +0000542/// [OBJC] '\@selector' '(' objc-selector-arg ')'
543/// [OBJC] '\@protocol' '(' identifier ')'
544/// [OBJC] '\@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000545/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000546/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000547/// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3]
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000548/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000549/// [C++11] typename-specifier braced-init-list [C++11 5.2.3]
Bill Wendlinga6930032007-06-29 18:21:34 +0000550/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
551/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
552/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
553/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000554/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
555/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000556/// [C++] 'this' [C++ 9.3.2]
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000557/// [G++] unary-type-trait '(' type-id ')'
558/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley6242b6a2011-04-28 00:16:57 +0000559/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff0ac012832008-08-28 19:20:44 +0000560/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000561///
562/// constant: [C99 6.4.4]
563/// integer-constant
564/// floating-constant
565/// enumeration-constant -> identifier
566/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000567///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000568/// id-expression: [C++ 5.1]
569/// unqualified-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000570/// qualified-id
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000571///
572/// unqualified-id: [C++ 5.1]
573/// identifier
574/// operator-function-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000575/// conversion-function-id
576/// '~' class-name
577/// template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000578///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000579/// new-expression: [C++ 5.3.4]
580/// '::'[opt] 'new' new-placement[opt] new-type-id
581/// new-initializer[opt]
582/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
583/// new-initializer[opt]
584///
585/// delete-expression: [C++ 5.3.5]
586/// '::'[opt] 'delete' cast-expression
587/// '::'[opt] 'delete' '[' ']' cast-expression
588///
John Wiegley65497cc2011-04-27 23:09:49 +0000589/// [GNU/Embarcadero] unary-type-trait:
590/// '__is_arithmetic'
591/// '__is_floating_point'
592/// '__is_integral'
593/// '__is_lvalue_expr'
594/// '__is_rvalue_expr'
595/// '__is_complete_type'
596/// '__is_void'
597/// '__is_array'
598/// '__is_function'
599/// '__is_reference'
600/// '__is_lvalue_reference'
601/// '__is_rvalue_reference'
602/// '__is_fundamental'
603/// '__is_object'
604/// '__is_scalar'
605/// '__is_compound'
606/// '__is_pointer'
607/// '__is_member_object_pointer'
608/// '__is_member_function_pointer'
609/// '__is_member_pointer'
610/// '__is_const'
611/// '__is_volatile'
612/// '__is_trivial'
613/// '__is_standard_layout'
614/// '__is_signed'
615/// '__is_unsigned'
616///
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000617/// [GNU] unary-type-trait:
Sebastian Redl7dcb1552010-08-31 04:59:00 +0000618/// '__has_nothrow_assign'
619/// '__has_nothrow_copy'
620/// '__has_nothrow_constructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000621/// '__has_trivial_assign' [TODO]
622/// '__has_trivial_copy' [TODO]
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000623/// '__has_trivial_constructor'
Anders Carlsson6dc35752009-04-17 02:34:54 +0000624/// '__has_trivial_destructor'
Sebastian Redlb469afb2010-09-02 23:19:42 +0000625/// '__has_virtual_destructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000626/// '__is_abstract' [TODO]
627/// '__is_class'
628/// '__is_empty' [TODO]
629/// '__is_enum'
Douglas Gregordca70af2011-12-03 18:14:24 +0000630/// '__is_final'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000631/// '__is_pod'
632/// '__is_polymorphic'
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000633/// '__is_trivial'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000634/// '__is_union'
635///
Alexis Huntd9a5cc12011-05-13 00:31:07 +0000636/// [Clang] unary-type-trait:
637/// '__trivially_copyable'
638///
Douglas Gregor8006e762011-01-27 20:28:01 +0000639/// binary-type-trait:
640/// [GNU] '__is_base_of'
641/// [MS] '__is_convertible_to'
John Wiegley65497cc2011-04-27 23:09:49 +0000642/// '__is_convertible'
643/// '__is_same'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000644///
John Wiegley6242b6a2011-04-28 00:16:57 +0000645/// [Embarcadero] array-type-trait:
646/// '__array_rank'
647/// '__array_extent'
648///
John Wiegleyf9f65842011-04-25 06:54:41 +0000649/// [Embarcadero] expression-trait:
650/// '__is_lvalue_expr'
651/// '__is_rvalue_expr'
James Dennett3d5e4592012-06-17 04:36:28 +0000652/// \endverbatim
John Wiegleyf9f65842011-04-25 06:54:41 +0000653///
John McCalldadc5752010-08-24 06:29:42 +0000654ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl22e3a932010-09-10 20:55:37 +0000655 bool isAddressOfOperand,
656 bool &NotCastExpr,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000657 TypeCastState isTypeCast) {
John McCalldadc5752010-08-24 06:29:42 +0000658 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000659 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000660 NotCastExpr = false;
Mike Stump11289f42009-09-09 15:08:12 +0000661
Chris Lattner81b576e2006-08-11 02:13:20 +0000662 // This handles all of cast-expression, unary-expression, postfix-expression,
663 // and primary-expression. We handle them together like this for efficiency
664 // and to simplify handling of an expression starting with a '(' token: which
665 // may be one of a parenthesized expression, cast-expression, compound literal
666 // expression, or statement expression.
667 //
668 // If the parsed tokens consist of a primary-expression, the cases below
John McCallb268a282010-08-23 23:25:46 +0000669 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
670 // to handle the postfix expression suffixes. Cases that cannot be followed
671 // by postfix exprs should return without invoking
672 // ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000673 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000674 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000675 // If this expression is limited to being a unary-expression, the parent can
676 // not start a cast expression.
677 ParenParseOption ParenExprType =
David Blaikiebbafb8a2012-03-11 07:00:24 +0000678 (isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallba7bf592010-08-24 05:47:05 +0000679 ParsedType CastTy;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000680 SourceLocation RParenLoc;
Chris Lattner3c674cf2009-12-10 02:08:07 +0000681
682 {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000683 // The inside of the parens don't need to be a colon protected scope, and
684 // isn't immediately a message send.
Chris Lattner3c674cf2009-12-10 02:08:07 +0000685 ColonProtectionRAIIObject X(*this, false);
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000686
Chris Lattner3c674cf2009-12-10 02:08:07 +0000687 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000688 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner3c674cf2009-12-10 02:08:07 +0000689 }
Mike Stump11289f42009-09-09 15:08:12 +0000690
Chris Lattner81b576e2006-08-11 02:13:20 +0000691 switch (ParenExprType) {
692 case SimpleExpr: break; // Nothing else to do.
693 case CompoundStmt: break; // Nothing else to do.
694 case CompoundLiteral:
695 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
696 // postfix-expression exist, parse them now.
697 break;
698 case CastExpr:
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000699 // We have parsed the cast-expression and no postfix-expr pieces are
700 // following.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000701 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000702 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000703
John McCallb268a282010-08-23 23:25:46 +0000704 break;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000705 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000706
Chris Lattner52a99e52006-08-10 20:56:00 +0000707 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000708 case tok::numeric_constant:
709 // constant: integer-constant
710 // constant: floating-constant
Sebastian Redl59b5e512008-12-11 21:36:32 +0000711
Richard Smithbcc22fc2012-03-09 08:00:36 +0000712 Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000713 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000714 break;
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000715
Bill Wendling4073ed52007-02-13 01:51:42 +0000716 case tok::kw_true:
717 case tok::kw_false:
Sebastian Redld65cea82008-12-11 22:51:44 +0000718 return ParseCXXBoolLiteral();
Ted Kremeneke65b0862012-03-06 20:05:56 +0000719
720 case tok::kw___objc_yes:
721 case tok::kw___objc_no:
722 return ParseObjCBoolLiteral();
Bill Wendling4073ed52007-02-13 01:51:42 +0000723
Sebastian Redl576fd422009-05-10 18:38:11 +0000724 case tok::kw_nullptr:
Richard Smithb15c11c2011-10-17 23:06:20 +0000725 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl576fd422009-05-10 18:38:11 +0000726 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
727
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000728 case tok::annot_primary_expr:
729 assert(Res.get() == 0 && "Stray primary-expression annotation?");
730 Res = getExprAnnotation(Tok);
731 ConsumeToken();
732 break;
733
David Blaikie15a430a2011-12-04 05:04:18 +0000734 case tok::kw_decltype:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000735 case tok::identifier: { // primary-expression: identifier
736 // unqualified-id: identifier
737 // constant: enumeration-constant
Chris Lattnera8a3f732009-01-06 05:06:21 +0000738 // Turn a potentially qualified name into a annot_typename or
Chris Lattner122db262009-01-04 22:52:14 +0000739 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000740 if (getLangOpts().CPlusPlus) {
John McCall64fe2332010-01-07 19:29:58 +0000741 // Avoid the unnecessary parse-time lookup in the common case
742 // where the syntax forbids a type.
743 const Token &Next = NextToken();
744 if (Next.is(tok::coloncolon) ||
745 (!ColonIsSacred && Next.is(tok::colon)) ||
746 Next.is(tok::less) ||
Sebastian Redl867f2282011-12-22 18:58:29 +0000747 Next.is(tok::l_paren) ||
748 Next.is(tok::l_brace)) {
John McCall64fe2332010-01-07 19:29:58 +0000749 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
750 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +0000751 return ExprError();
752 if (!Tok.is(tok::identifier))
John McCall64fe2332010-01-07 19:29:58 +0000753 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
754 }
Chris Lattner9a8968b2009-01-04 23:23:14 +0000755 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000756
Chris Lattner55662902009-10-25 17:04:48 +0000757 // Consume the identifier so that we can see if it is followed by a '(' or
758 // '.'.
759 IdentifierInfo &II = *Tok.getIdentifierInfo();
760 SourceLocation ILoc = ConsumeToken();
761
Chris Lattnera36ec422010-04-11 08:28:14 +0000762 // Support 'Class.property' and 'super.property' notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000763 if (getLangOpts().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000764 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattnercd963182010-04-12 06:20:33 +0000765 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000766 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000767 ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000768
Douglas Gregor36107ad2012-02-16 18:19:22 +0000769 // Allow either an identifier or the keyword 'class' (in C++).
770 if (Tok.isNot(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000771 !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattner55662902009-10-25 17:04:48 +0000772 Diag(Tok, diag::err_expected_property_name);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000773 return ExprError();
774 }
775 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
776 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000777
778 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
779 ILoc, PropertyLoc);
John McCallb268a282010-08-23 23:25:46 +0000780 break;
Steve Naroff9527bbf2009-03-09 21:12:44 +0000781 }
John McCall8d08b9b2010-08-27 09:08:28 +0000782
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000783 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregored0b69d2010-09-15 16:23:04 +0000784 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000785 // that identifier, this is probably a message send with a missing open
Douglas Gregored0b69d2010-09-15 16:23:04 +0000786 // bracket. Treat it as such.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000787 if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000788 getCurScope()->isInObjcMethodScope() &&
Douglas Gregored0b69d2010-09-15 16:23:04 +0000789 ((Tok.is(tok::identifier) &&
790 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
791 Tok.is(tok::code_completion))) {
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000792 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
793 0);
794 break;
795 }
796
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000797 // If we have an Objective-C class name followed by an identifier
798 // and either ':' or ']', this is an Objective-C class message
799 // send that's missing the opening '['. Recovery
800 // appropriately. Also take this path if we're performing code
801 // completion after an Objective-C class name.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000802 if (getLangOpts().ObjC1 &&
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000803 ((Tok.is(tok::identifier) && !InMessageExpression) ||
804 Tok.is(tok::code_completion))) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000805 const Token& Next = NextToken();
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000806 if (Tok.is(tok::code_completion) ||
807 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif433c9e12010-09-17 10:21:45 +0000808 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
809 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000810 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000811 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000812 DS.SetRangeStart(ILoc);
813 DS.SetRangeEnd(ILoc);
814 const char *PrevSpec = 0;
815 unsigned DiagID;
Gabor Greif433c9e12010-09-17 10:21:45 +0000816 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000817
818 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
819 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
820 DeclaratorInfo);
821 if (Ty.isInvalid())
822 break;
823
824 Res = ParseObjCMessageExpressionBody(SourceLocation(),
825 SourceLocation(),
826 Ty.get(), 0);
827 break;
828 }
829 }
830
John McCall8d08b9b2010-08-27 09:08:28 +0000831 // Make sure to pass down the right value for isAddressOfOperand.
832 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
833 isAddressOfOperand = false;
Chris Lattner55662902009-10-25 17:04:48 +0000834
Chris Lattnerac18be92006-11-20 06:49:47 +0000835 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
836 // need to know whether or not this identifier is a function designator or
837 // not.
Douglas Gregora121b752009-11-03 16:56:39 +0000838 UnqualifiedId Name;
839 CXXScopeSpec ScopeSpec;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000840 SourceLocation TemplateKWLoc;
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000841 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
842 isTypeCast != IsTypeCast);
Douglas Gregora121b752009-11-03 16:56:39 +0000843 Name.setIdentifier(&II, ILoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000844 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
845 Name, Tok.is(tok::l_paren),
846 isAddressOfOperand, &Validator);
John McCallb268a282010-08-23 23:25:46 +0000847 break;
Chris Lattnerac18be92006-11-20 06:49:47 +0000848 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000849 case tok::char_constant: // constant: character-constant
Douglas Gregorfb65e592011-07-27 05:40:30 +0000850 case tok::wide_char_constant:
851 case tok::utf16_char_constant:
852 case tok::utf32_char_constant:
Richard Smithbcc22fc2012-03-09 08:00:36 +0000853 Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope());
Steve Naroffae4143e2007-04-26 20:39:23 +0000854 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000855 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000856 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
857 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Nico Weber3a691a32012-06-23 02:07:59 +0000858 case tok::kw_L__FUNCTION__: // primary-expression: L__FUNCTION__ [MS]
Chris Lattner52a99e52006-08-10 20:56:00 +0000859 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000860 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000861 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000862 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000863 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000864 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000865 case tok::utf8_string_literal:
866 case tok::utf16_string_literal:
867 case tok::utf32_string_literal:
Richard Smithd67aea22012-03-06 03:21:47 +0000868 Res = ParseStringLiteralExpression(true);
John McCallb268a282010-08-23 23:25:46 +0000869 break;
Benjamin Kramere56f3932011-12-23 17:00:35 +0000870 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbourne91147592011-04-15 00:35:48 +0000871 Res = ParseGenericSelectionExpression();
872 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000873 case tok::kw___builtin_va_arg:
874 case tok::kw___builtin_offsetof:
875 case tok::kw___builtin_choose_expr:
Tanya Lattner55808c12011-06-04 00:47:47 +0000876 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redl90893182008-12-11 22:33:27 +0000877 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000878 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000879 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth904cb142011-07-08 04:59:44 +0000880
Douglas Gregora49ccfe2010-08-06 14:50:36 +0000881 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
882 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
883 // C++ [expr.unary] has:
884 // unary-expression:
885 // ++ cast-expression
886 // -- cast-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000887 SourceLocation SavedLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000888 Res = ParseCastExpression(!getLangOpts().CPlusPlus);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000889 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000890 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000891 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000892 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000893 case tok::amp: { // unary-expression: '&' cast-expression
894 // Special treatment because of member pointers
895 SourceLocation SavedLoc = ConsumeToken();
896 Res = ParseCastExpression(false, true);
897 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000898 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000899 return Res;
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000900 }
901
Chris Lattner81b576e2006-08-11 02:13:20 +0000902 case tok::star: // unary-expression: '*' cast-expression
903 case tok::plus: // unary-expression: '+' cast-expression
904 case tok::minus: // unary-expression: '-' cast-expression
905 case tok::tilde: // unary-expression: '~' cast-expression
906 case tok::exclaim: // unary-expression: '!' cast-expression
907 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000908 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000909 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000910 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000911 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000912 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000913 return Res;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000914 }
915
Chris Lattnerc43926f2008-02-02 20:20:10 +0000916 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
917 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000918 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000919 SourceLocation SavedLoc = ConsumeToken();
920 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000921 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000922 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000923 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000924 }
Jordan Rose58d54722012-06-30 21:33:57 +0000925 case tok::kw__Alignof: // unary-expression: '_Alignof' '(' type-name ')'
926 if (!getLangOpts().C11)
927 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
928 // fallthrough
929 case tok::kw_alignof: // unary-expression: 'alignof' '(' type-id ')'
Chris Lattner81b576e2006-08-11 02:13:20 +0000930 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
931 // unary-expression: '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +0000932 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
933 // unary-expression: 'sizeof' '(' type-name ')'
Peter Collingbournee190dee2011-03-11 19:24:49 +0000934 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
935 return ParseUnaryExprOrTypeTraitExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000936 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000937 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000938 if (Tok.isNot(tok::identifier))
939 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000940
Chris Lattner9ba479b2011-02-18 21:16:39 +0000941 if (getCurScope()->getFnParent() == 0)
942 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
943
Chris Lattnereefa10e2007-05-28 06:56:27 +0000944 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000945 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
946 Tok.getLocation());
947 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Chris Lattner14a1b642006-10-15 22:33:58 +0000948 ConsumeToken();
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000949 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000950 }
Chris Lattner29375652006-12-04 18:06:35 +0000951 case tok::kw_const_cast:
952 case tok::kw_dynamic_cast:
953 case tok::kw_reinterpret_cast:
954 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000955 Res = ParseCXXCasts();
John McCallb268a282010-08-23 23:25:46 +0000956 break;
Sebastian Redlc4704762008-11-11 11:37:55 +0000957 case tok::kw_typeid:
958 Res = ParseCXXTypeid();
John McCallb268a282010-08-23 23:25:46 +0000959 break;
Francois Pichet9f4f2072010-09-08 12:20:18 +0000960 case tok::kw___uuidof:
961 Res = ParseCXXUuidof();
962 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000963 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000964 Res = ParseCXXThis();
John McCallb268a282010-08-23 23:25:46 +0000965 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000966
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000967 case tok::annot_typename:
968 if (isStartOfObjCClassMessageMissingOpenBracket()) {
969 ParsedType Type = getTypeAnnotation(Tok);
970
971 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000972 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000973 DS.SetRangeStart(Tok.getLocation());
974 DS.SetRangeEnd(Tok.getLastLoc());
975
976 const char *PrevSpec = 0;
977 unsigned DiagID;
Nico Weber77430342010-11-22 10:30:56 +0000978 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
979 PrevSpec, DiagID, Type);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000980
981 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
982 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
983 if (Ty.isInvalid())
984 break;
985
986 ConsumeToken();
987 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
988 Ty.get(), 0);
989 break;
990 }
991 // Fall through
992
David Blaikie25896afb2012-01-24 05:47:35 +0000993 case tok::annot_decltype:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000994 case tok::kw_char:
995 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000996 case tok::kw_char16_t:
997 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000998 case tok::kw_bool:
999 case tok::kw_short:
1000 case tok::kw_int:
1001 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00001002 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00001003 case tok::kw___int128:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001004 case tok::kw_signed:
1005 case tok::kw_unsigned:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001006 case tok::kw_half:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001007 case tok::kw_float:
1008 case tok::kw_double:
1009 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +00001010 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +00001011 case tok::kw_typeof:
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001012 case tok::kw___vector: {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001013 if (!getLangOpts().CPlusPlus) {
Chris Lattner8a38aa82009-01-04 22:28:21 +00001014 Diag(Tok, diag::err_expected_expression);
1015 return ExprError();
1016 }
Eli Friedman6d692cc2009-06-11 00:33:41 +00001017
1018 if (SavedKind == tok::kw_typename) {
1019 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001020 // typename-specifier braced-init-list
John McCall1f476a12010-02-26 08:45:28 +00001021 if (TryAnnotateTypeOrScopeToken())
Eli Friedman6d692cc2009-06-11 00:33:41 +00001022 return ExprError();
1023 }
1024
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001025 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001026 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001027 //
John McCall084e83d2011-03-24 11:26:52 +00001028 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001029 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redl3da34892011-06-05 12:23:16 +00001030 if (Tok.isNot(tok::l_paren) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001031 (!getLangOpts().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl59b5e512008-12-11 21:36:32 +00001032 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1033 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001034
Richard Smith5d164bc2011-10-15 05:09:34 +00001035 if (Tok.is(tok::l_brace))
1036 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1037
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001038 Res = ParseCXXTypeConstructExpression(DS);
John McCallb268a282010-08-23 23:25:46 +00001039 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001040 }
1041
Douglas Gregor7df89f52010-02-05 19:11:37 +00001042 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001043 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1044 // (We can end up in this situation after tentative parsing.)
1045 if (TryAnnotateTypeOrScopeToken())
1046 return ExprError();
1047 if (!Tok.is(tok::annot_cxxscope))
1048 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001049 NotCastExpr, isTypeCast);
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001050
Douglas Gregor7df89f52010-02-05 19:11:37 +00001051 Token Next = NextToken();
1052 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001053 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001054 if (TemplateId->Kind == TNK_Type_template) {
1055 // We have a qualified template-id that we know refers to a
1056 // type, translate it into a type and continue parsing as a
1057 // cast expression.
1058 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001059 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1060 /*EnteringContext=*/false);
Douglas Gregore7c20652011-03-02 00:47:37 +00001061 AnnotateTemplateIdTokenAsType();
Douglas Gregor7df89f52010-02-05 19:11:37 +00001062 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001063 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001064 }
1065 }
1066
1067 // Parse as an id-expression.
1068 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001069 break;
Douglas Gregor7df89f52010-02-05 19:11:37 +00001070 }
1071
1072 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001073 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001074 if (TemplateId->Kind == TNK_Type_template) {
1075 // We have a template-id that we know refers to a type,
1076 // translate it into a type and continue parsing as a cast
1077 // expression.
1078 AnnotateTemplateIdTokenAsType();
1079 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001080 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001081 }
1082
1083 // Fall through to treat the template-id as an id-expression.
1084 }
1085
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001086 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001087 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001088 break;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001089
Chris Lattner122db262009-01-04 22:52:14 +00001090 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001091 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1092 // annotates the token, tail recurse.
1093 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001094 return ExprError();
1095 if (!Tok.is(tok::coloncolon))
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001096 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1097
Chris Lattner122db262009-01-04 22:52:14 +00001098 // ::new -> [C++] new-expression
1099 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001100 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +00001101 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001102 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +00001103 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001104 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001105
Chris Lattner9a8968b2009-01-04 23:23:14 +00001106 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001107 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +00001108 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +00001109 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +00001110
Sebastian Redlbd150f42008-11-21 19:14:01 +00001111 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001112 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001113
1114 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001115 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001116
Sebastian Redl22e3a932010-09-10 20:55:37 +00001117 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smithb15c11c2011-10-17 23:06:20 +00001118 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001119 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001120 BalancedDelimiterTracker T(*this, tok::l_paren);
1121
1122 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl22e3a932010-09-10 20:55:37 +00001123 return ExprError();
Richard Smith764d2fe2011-12-20 02:08:33 +00001124 // C++11 [expr.unary.noexcept]p1:
Sebastian Redle56be2f72010-09-10 21:57:27 +00001125 // The noexcept operator determines whether the evaluation of its operand,
1126 // which is an unevaluated operand, can throw an exception.
1127 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001128 ExprResult Result = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001129
1130 T.consumeClose();
1131
Sebastian Redl22e3a932010-09-10 20:55:37 +00001132 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001133 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1134 Result.take(), T.getCloseLocation());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001135 return Result;
Sebastian Redl22e3a932010-09-10 20:55:37 +00001136 }
1137
Chandler Carruth79803482011-04-23 10:47:20 +00001138 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001139 case tok::kw___is_class:
Eli Friedmanc96d4962009-08-15 21:55:26 +00001140 case tok::kw___is_empty:
Chandler Carruth79803482011-04-23 10:47:20 +00001141 case tok::kw___is_enum:
Sebastian Redl79eba1c2009-12-03 00:13:20 +00001142 case tok::kw___is_literal:
John Wiegley65497cc2011-04-27 23:09:49 +00001143 case tok::kw___is_arithmetic:
1144 case tok::kw___is_integral:
1145 case tok::kw___is_floating_point:
1146 case tok::kw___is_complete_type:
1147 case tok::kw___is_void:
1148 case tok::kw___is_array:
1149 case tok::kw___is_function:
1150 case tok::kw___is_reference:
1151 case tok::kw___is_lvalue_reference:
1152 case tok::kw___is_rvalue_reference:
1153 case tok::kw___is_fundamental:
1154 case tok::kw___is_object:
1155 case tok::kw___is_scalar:
1156 case tok::kw___is_compound:
1157 case tok::kw___is_pointer:
1158 case tok::kw___is_member_object_pointer:
1159 case tok::kw___is_member_function_pointer:
1160 case tok::kw___is_member_pointer:
1161 case tok::kw___is_const:
1162 case tok::kw___is_volatile:
1163 case tok::kw___is_standard_layout:
1164 case tok::kw___is_signed:
1165 case tok::kw___is_unsigned:
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00001166 case tok::kw___is_literal_type:
Chandler Carruth79803482011-04-23 10:47:20 +00001167 case tok::kw___is_pod:
1168 case tok::kw___is_polymorphic:
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +00001169 case tok::kw___is_trivial:
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001170 case tok::kw___is_trivially_copyable:
Chandler Carruth79803482011-04-23 10:47:20 +00001171 case tok::kw___is_union:
Douglas Gregordca70af2011-12-03 18:14:24 +00001172 case tok::kw___is_final:
Anders Carlssonfe63dc52009-04-16 00:08:20 +00001173 case tok::kw___has_trivial_constructor:
Douglas Gregor79f83ed2009-07-23 23:49:00 +00001174 case tok::kw___has_trivial_copy:
1175 case tok::kw___has_trivial_assign:
Anders Carlsson6dc35752009-04-17 02:34:54 +00001176 case tok::kw___has_trivial_destructor:
Sebastian Redl7dcb1552010-08-31 04:59:00 +00001177 case tok::kw___has_nothrow_assign:
1178 case tok::kw___has_nothrow_copy:
1179 case tok::kw___has_nothrow_constructor:
Sebastian Redlb469afb2010-09-02 23:19:42 +00001180 case tok::kw___has_virtual_destructor:
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001181 return ParseUnaryTypeTrait();
1182
Francois Pichet34b21132010-12-08 22:35:30 +00001183 case tok::kw___builtin_types_compatible_p:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001184 case tok::kw___is_base_of:
John Wiegley65497cc2011-04-27 23:09:49 +00001185 case tok::kw___is_same:
1186 case tok::kw___is_convertible:
Douglas Gregor8006e762011-01-27 20:28:01 +00001187 case tok::kw___is_convertible_to:
Douglas Gregor1be329d2012-02-23 07:33:15 +00001188 case tok::kw___is_trivially_assignable:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001189 return ParseBinaryTypeTrait();
1190
Douglas Gregor29c42f22012-02-24 07:38:34 +00001191 case tok::kw___is_trivially_constructible:
1192 return ParseTypeTrait();
1193
John Wiegley6242b6a2011-04-28 00:16:57 +00001194 case tok::kw___array_rank:
1195 case tok::kw___array_extent:
1196 return ParseArrayTypeTrait();
1197
John Wiegleyf9f65842011-04-25 06:54:41 +00001198 case tok::kw___is_lvalue_expr:
1199 case tok::kw___is_rvalue_expr:
1200 return ParseExpressionTrait();
1201
Chris Lattner644e1b72007-10-03 22:03:06 +00001202 case tok::at: {
1203 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001204 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +00001205 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001206 case tok::caret:
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001207 Res = ParseBlockLiteralExpression();
1208 break;
1209 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001210 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001211 cutOffParsing();
1212 return ExprError();
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001213 }
Chris Lattner6bf1db12008-12-12 19:20:14 +00001214 case tok::l_square:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001215 if (getLangOpts().CPlusPlus0x) {
1216 if (getLangOpts().ObjC1) {
Eli Friedmanc7c97142012-01-04 02:40:39 +00001217 // C++11 lambda expressions and Objective-C message sends both start with a
1218 // square bracket. There are three possibilities here:
1219 // we have a valid lambda expression, we have an invalid lambda
1220 // expression, or we have something that doesn't appear to be a lambda.
1221 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001222 Res = TryParseLambdaExpression();
Eli Friedmanc7c97142012-01-04 02:40:39 +00001223 if (!Res.isInvalid() && !Res.get())
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001224 Res = ParseObjCMessageExpression();
1225 break;
1226 }
1227 Res = ParseLambdaExpression();
1228 break;
1229 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001230 if (getLangOpts().ObjC1) {
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001231 Res = ParseObjCMessageExpression();
1232 break;
1233 }
1234 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +00001235 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001236 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001237 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +00001238 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001239
John McCallb268a282010-08-23 23:25:46 +00001240 // These can be followed by postfix-expr pieces.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001241 return ParsePostfixExpressionSuffix(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +00001242}
1243
James Dennett3d5e4592012-06-17 04:36:28 +00001244/// \brief Once the leading part of a postfix-expression is parsed, this
1245/// method parses any suffixes that apply.
Chris Lattner20c6a452006-08-12 17:40:43 +00001246///
James Dennett3d5e4592012-06-17 04:36:28 +00001247/// \verbatim
Chris Lattner20c6a452006-08-12 17:40:43 +00001248/// postfix-expression: [C99 6.5.2]
1249/// primary-expression
1250/// postfix-expression '[' expression ']'
Sebastian Redl3da34892011-06-05 12:23:16 +00001251/// postfix-expression '[' braced-init-list ']'
Chris Lattner20c6a452006-08-12 17:40:43 +00001252/// postfix-expression '(' argument-expression-list[opt] ')'
1253/// postfix-expression '.' identifier
1254/// postfix-expression '->' identifier
1255/// postfix-expression '++'
1256/// postfix-expression '--'
1257/// '(' type-name ')' '{' initializer-list '}'
1258/// '(' type-name ')' '{' initializer-list ',' '}'
1259///
1260/// argument-expression-list: [C99 6.5.2]
Douglas Gregor968f23a2011-01-03 19:31:53 +00001261/// argument-expression ...[opt]
1262/// argument-expression-list ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +00001263/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001264ExprResult
1265Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001266 // Now that the primary-expression piece of the postfix-expression has been
1267 // parsed, see if there are any postfix-expression pieces here.
1268 SourceLocation Loc;
1269 while (1) {
1270 switch (Tok.getKind()) {
Douglas Gregored0b69d2010-09-15 16:23:04 +00001271 case tok::code_completion:
1272 if (InMessageExpression)
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001273 return LHS;
Douglas Gregored0b69d2010-09-15 16:23:04 +00001274
Douglas Gregoreda7e542010-09-18 01:28:11 +00001275 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001276 cutOffParsing();
1277 return ExprError();
Douglas Gregored0b69d2010-09-15 16:23:04 +00001278
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001279 case tok::identifier:
1280 // If we see identifier: after an expression, and we're not already in a
1281 // message send, then this is probably a message send with a missing
1282 // opening bracket '['.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001283 if (getLangOpts().ObjC1 && !InMessageExpression &&
Douglas Gregord6d98002010-09-15 14:54:45 +00001284 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001285 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1286 ParsedType(), LHS.get());
1287 break;
1288 }
1289
1290 // Fall through; this isn't a message send.
1291
Chris Lattner20c6a452006-08-12 17:40:43 +00001292 default: // Not a postfix-expression suffix.
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001293 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001294 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner47054fb2010-05-31 18:18:22 +00001295 // If we have a array postfix expression that starts on a new line and
1296 // Objective-C is enabled, it is highly likely that the user forgot a
1297 // semicolon after the base expression and that the array postfix-expr is
1298 // actually another message send. In this case, do some look-ahead to see
1299 // if the contents of the square brackets are obviously not a valid
1300 // expression and recover by pretending there is no suffix.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001301 if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
Chris Lattner47054fb2010-05-31 18:18:22 +00001302 isSimpleObjCMessageExpression())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001303 return LHS;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001304
1305 // Reject array indices starting with a lambda-expression. '[[' is
1306 // reserved for attributes.
1307 if (CheckProhibitedCXX11Attribute())
1308 return ExprError();
1309
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001310 BalancedDelimiterTracker T(*this, tok::l_square);
1311 T.consumeOpen();
1312 Loc = T.getOpenLocation();
Sebastian Redl3da34892011-06-05 12:23:16 +00001313 ExprResult Idx;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001314 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001315 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00001316 Idx = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001317 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00001318 Idx = ParseExpression();
Sebastian Redl511ed552008-11-25 22:21:31 +00001319
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001320 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001321
1322 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCallb268a282010-08-23 23:25:46 +00001323 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1324 Idx.take(), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001325 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +00001326 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001327
Chris Lattner89c50c62006-08-11 06:41:18 +00001328 // Match the ']'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001329 T.consumeClose();
Chris Lattner89c50c62006-08-11 06:41:18 +00001330 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001331 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001332
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001333 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1334 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1335 // '(' argument-expression-list[opt] ')'
1336 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001337 InMessageExpressionRAIIObject InMessage(*this, false);
1338
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001339 Expr *ExecConfig = 0;
1340
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001341 BalancedDelimiterTracker PT(*this, tok::l_paren);
1342
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001343 if (OpKind == tok::lesslessless) {
1344 ExprVector ExecConfigExprs(Actions);
1345 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001346 SourceLocation OpenLoc = ConsumeToken();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001347
1348 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1349 LHS = ExprError();
1350 }
1351
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001352 SourceLocation CloseLoc = Tok.getLocation();
1353 if (Tok.is(tok::greatergreatergreater)) {
1354 ConsumeToken();
1355 } else if (LHS.isInvalid()) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001356 SkipUntil(tok::greatergreatergreater);
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001357 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001358 // There was an error closing the brackets
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001359 Diag(Tok, diag::err_expected_ggg);
1360 Diag(OpenLoc, diag::note_matching) << "<<<";
1361 SkipUntil(tok::greatergreatergreater);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001362 LHS = ExprError();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001363 }
1364
1365 if (!LHS.isInvalid()) {
1366 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1367 LHS = ExprError();
1368 else
1369 Loc = PrevTokLocation;
1370 }
1371
1372 if (!LHS.isInvalid()) {
1373 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001374 OpenLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001375 ExecConfigExprs,
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001376 CloseLoc);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001377 if (ECResult.isInvalid())
1378 LHS = ExprError();
1379 else
1380 ExecConfig = ECResult.get();
1381 }
1382 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001383 PT.consumeOpen();
1384 Loc = PT.getOpenLocation();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001385 }
1386
Sebastian Redl511ed552008-11-25 22:21:31 +00001387 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001388 CommaLocsTy CommaLocs;
Douglas Gregoreda7e542010-09-18 01:28:11 +00001389
Douglas Gregorcabea402009-09-22 15:41:20 +00001390 if (Tok.is(tok::code_completion)) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00001391 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1392 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001393 cutOffParsing();
1394 return ExprError();
Douglas Gregorcabea402009-09-22 15:41:20 +00001395 }
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001396
1397 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1398 if (Tok.isNot(tok::r_paren)) {
1399 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1400 LHS.get())) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001401 LHS = ExprError();
1402 }
Chris Lattner0c6c0342006-08-12 18:12:45 +00001403 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001404 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001405
Chris Lattner89c50c62006-08-11 06:41:18 +00001406 // Match the ')'.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001407 if (LHS.isInvalid()) {
1408 SkipUntil(tok::r_paren);
1409 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001410 PT.consumeClose();
Douglas Gregoreda7e542010-09-18 01:28:11 +00001411 LHS = ExprError();
1412 } else {
1413 assert((ArgExprs.size() == 0 ||
1414 ArgExprs.size()-1 == CommaLocs.size())&&
Chris Lattnere165d942006-08-24 04:40:38 +00001415 "Unexpected number of commas!");
John McCallb268a282010-08-23 23:25:46 +00001416 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001417 ArgExprs, Tok.getLocation(),
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001418 ExecConfig);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001419 PT.consumeClose();
Chris Lattnere165d942006-08-24 04:40:38 +00001420 }
Mike Stump11289f42009-09-09 15:08:12 +00001421
Chris Lattner89c50c62006-08-11 06:41:18 +00001422 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001423 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001424 case tok::arrow:
1425 case tok::period: {
1426 // postfix-expression: p-e '->' template[opt] id-expression
1427 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001428 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001429 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001430
Douglas Gregord8061562009-08-06 03:17:00 +00001431 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001432 ParsedType ObjectType;
Douglas Gregore610ada2010-02-24 18:44:31 +00001433 bool MayBePseudoDestructor = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001434 if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001435 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregore610ada2010-02-24 18:44:31 +00001436 OpLoc, OpKind, ObjectType,
1437 MayBePseudoDestructor);
Douglas Gregord8061562009-08-06 03:17:00 +00001438 if (LHS.isInvalid())
1439 break;
Douglas Gregore610ada2010-02-24 18:44:31 +00001440
Douglas Gregordf593fb2011-11-07 17:33:42 +00001441 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1442 /*EnteringContext=*/false,
Douglas Gregore610ada2010-02-24 18:44:31 +00001443 &MayBePseudoDestructor);
Douglas Gregor205a3612010-05-27 15:25:59 +00001444 if (SS.isNotEmpty())
John McCallba7bf592010-08-24 05:47:05 +00001445 ObjectType = ParsedType();
Douglas Gregord8061562009-08-06 03:17:00 +00001446 }
1447
Douglas Gregor2436e712009-09-17 21:32:03 +00001448 if (Tok.is(tok::code_completion)) {
1449 // Code completion for a member access expression.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001450 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor2436e712009-09-17 21:32:03 +00001451 OpLoc, OpKind == tok::arrow);
1452
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001453 cutOffParsing();
1454 return ExprError();
Douglas Gregor2436e712009-09-17 21:32:03 +00001455 }
1456
John McCallb268a282010-08-23 23:25:46 +00001457 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1458 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregore610ada2010-02-24 18:44:31 +00001459 ObjectType);
1460 break;
1461 }
1462
1463 // Either the action has told is that this cannot be a
1464 // pseudo-destructor expression (based on the type of base
1465 // expression), or we didn't see a '~' in the right place. We
1466 // can still parse a destructor name here, but in that case it
1467 // names a real destructor.
Francois Pichet64225792011-01-18 05:04:39 +00001468 // Allow explicit constructor calls in Microsoft mode.
1469 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001470 SourceLocation TemplateKWLoc;
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001471 UnqualifiedId Name;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001472 if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
Douglas Gregor36107ad2012-02-16 18:19:22 +00001473 // Objective-C++:
1474 // After a '.' in a member access expression, treat the keyword
1475 // 'class' as if it were an identifier.
1476 //
1477 // This hack allows property access to the 'class' method because it is
1478 // such a common method name. For other C++ keywords that are
1479 // Objective-C method names, one must use the message send syntax.
1480 IdentifierInfo *Id = Tok.getIdentifierInfo();
1481 SourceLocation Loc = ConsumeToken();
1482 Name.setIdentifier(Id, Loc);
1483 } else if (ParseUnqualifiedId(SS,
1484 /*EnteringContext=*/false,
1485 /*AllowDestructorName=*/true,
1486 /*AllowConstructorName=*/
David Blaikiebbafb8a2012-03-11 07:00:24 +00001487 getLangOpts().MicrosoftExt,
Douglas Gregor36107ad2012-02-16 18:19:22 +00001488 ObjectType, TemplateKWLoc, Name))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001489 LHS = ExprError();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001490
1491 if (!LHS.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001492 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001493 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001494 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1495 Tok.is(tok::l_paren));
Chris Lattner89c50c62006-08-11 06:41:18 +00001496 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001497 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001498 case tok::plusplus: // postfix-expression: postfix-expression '++'
1499 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001500 if (!LHS.isInvalid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001501 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCallb268a282010-08-23 23:25:46 +00001502 Tok.getKind(), LHS.take());
Sebastian Redl511ed552008-11-25 22:21:31 +00001503 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001504 ConsumeToken();
1505 break;
Chris Lattnerf8339772006-08-10 22:01:51 +00001506 }
1507 }
Chris Lattner52a99e52006-08-10 20:56:00 +00001508}
1509
Peter Collingbournee190dee2011-03-11 19:24:49 +00001510/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1511/// vec_step and we are at the start of an expression or a parenthesized
1512/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1513/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001514///
James Dennett3d5e4592012-06-17 04:36:28 +00001515/// \verbatim
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001516/// unary-expression: [C99 6.5.3]
1517/// 'sizeof' unary-expression
1518/// 'sizeof' '(' type-name ')'
1519/// [GNU] '__alignof' unary-expression
1520/// [GNU] '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +00001521/// [C11] '_Alignof' '(' type-name ')'
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001522/// [C++0x] 'alignof' '(' type-id ')'
1523///
1524/// [GNU] typeof-specifier:
1525/// typeof ( expressions )
1526/// typeof ( type-name )
1527/// [GNU/C++] typeof unary-expression
1528///
Peter Collingbournee190dee2011-03-11 19:24:49 +00001529/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1530/// vec_step ( expressions )
1531/// vec_step ( type-name )
James Dennett3d5e4592012-06-17 04:36:28 +00001532/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001533ExprResult
Peter Collingbournee190dee2011-03-11 19:24:49 +00001534Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1535 bool &isCastExpr,
1536 ParsedType &CastTy,
1537 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001538
1539 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournee190dee2011-03-11 19:24:49 +00001540 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
Jordan Rose58d54722012-06-30 21:33:57 +00001541 OpTok.is(tok::kw__Alignof) || OpTok.is(tok::kw_vec_step)) &&
Peter Collingbournee190dee2011-03-11 19:24:49 +00001542 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001543
John McCalldadc5752010-08-24 06:29:42 +00001544 ExprResult Operand;
Mike Stump11289f42009-09-09 15:08:12 +00001545
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001546 // If the operand doesn't start with an '(', it must be an expression.
1547 if (Tok.isNot(tok::l_paren)) {
1548 isCastExpr = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001549 if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001550 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1551 return ExprError();
1552 }
Mike Stump11289f42009-09-09 15:08:12 +00001553
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001554 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001555 } else {
1556 // If it starts with a '(', we know that it is either a parenthesized
1557 // type-name, or it is a unary-expression that starts with a compound
1558 // literal, or starts with a primary-expression that is a parenthesized
1559 // expression.
1560 ParenParseOption ExprType = CastExpr;
1561 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001562
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001563 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001564 false, CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001565 CastRange = SourceRange(LParenLoc, RParenLoc);
1566
1567 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1568 // a type.
1569 if (ExprType == CastExpr) {
1570 isCastExpr = true;
1571 return ExprEmpty();
1572 }
1573
David Blaikiebbafb8a2012-03-11 07:00:24 +00001574 if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
Douglas Gregor5dc05532010-07-28 18:22:12 +00001575 // GNU typeof in C requires the expression to be parenthesized. Not so for
1576 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1577 // the start of a unary-expression, but doesn't include any postfix
1578 // pieces. Parse these now if present.
John McCall3669c802010-08-24 23:41:43 +00001579 if (!Operand.isInvalid())
1580 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor5dc05532010-07-28 18:22:12 +00001581 }
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001582 }
1583
1584 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1585 isCastExpr = false;
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001586 return Operand;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001587}
1588
Chris Lattner20c6a452006-08-12 17:40:43 +00001589
James Dennett3d5e4592012-06-17 04:36:28 +00001590/// \brief Parse a sizeof or alignof expression.
1591///
1592/// \verbatim
Chris Lattner81b576e2006-08-11 02:13:20 +00001593/// unary-expression: [C99 6.5.3]
1594/// 'sizeof' unary-expression
1595/// 'sizeof' '(' type-name ')'
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001596/// [C++0x] 'sizeof' '...' '(' identifier ')'
Chris Lattner81b576e2006-08-11 02:13:20 +00001597/// [GNU] '__alignof' unary-expression
1598/// [GNU] '__alignof' '(' type-name ')'
Jordan Rose58d54722012-06-30 21:33:57 +00001599/// [C11] '_Alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +00001600/// [C++0x] 'alignof' '(' type-id ')'
James Dennett3d5e4592012-06-17 04:36:28 +00001601/// \endverbatim
Peter Collingbournee190dee2011-03-11 19:24:49 +00001602ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Jordan Rose58d54722012-06-30 21:33:57 +00001603 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) ||
1604 Tok.is(tok::kw_alignof) || Tok.is(tok::kw__Alignof) ||
1605 Tok.is(tok::kw_vec_step)) &&
Peter Collingbournee190dee2011-03-11 19:24:49 +00001606 "Not a sizeof/alignof/vec_step expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001607 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001608 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001609
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001610 // [C++0x] 'sizeof' '...' '(' identifier ')'
1611 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1612 SourceLocation EllipsisLoc = ConsumeToken();
1613 SourceLocation LParenLoc, RParenLoc;
1614 IdentifierInfo *Name = 0;
1615 SourceLocation NameLoc;
1616 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001617 BalancedDelimiterTracker T(*this, tok::l_paren);
1618 T.consumeOpen();
1619 LParenLoc = T.getOpenLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001620 if (Tok.is(tok::identifier)) {
1621 Name = Tok.getIdentifierInfo();
1622 NameLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001623 T.consumeClose();
1624 RParenLoc = T.getCloseLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001625 if (RParenLoc.isInvalid())
1626 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1627 } else {
1628 Diag(Tok, diag::err_expected_parameter_pack);
1629 SkipUntil(tok::r_paren);
1630 }
1631 } else if (Tok.is(tok::identifier)) {
1632 Name = Tok.getIdentifierInfo();
1633 NameLoc = ConsumeToken();
1634 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1635 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1636 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1637 << Name
1638 << FixItHint::CreateInsertion(LParenLoc, "(")
1639 << FixItHint::CreateInsertion(RParenLoc, ")");
1640 } else {
1641 Diag(Tok, diag::err_sizeof_parameter_pack);
1642 }
1643
1644 if (!Name)
1645 return ExprError();
1646
1647 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1648 OpTok.getLocation(),
1649 *Name, NameLoc,
1650 RParenLoc);
1651 }
Richard Smithb15c11c2011-10-17 23:06:20 +00001652
Jordan Rose58d54722012-06-30 21:33:57 +00001653 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof))
Richard Smithb15c11c2011-10-17 23:06:20 +00001654 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1655
Eli Friedmane0afc982012-01-21 01:01:51 +00001656 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1657
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001658 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00001659 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001660 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001661 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1662 isCastExpr,
1663 CastTy,
1664 CastRange);
1665
1666 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
Jordan Rose58d54722012-06-30 21:33:57 +00001667 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof) ||
1668 OpTok.is(tok::kw__Alignof))
Peter Collingbournee190dee2011-03-11 19:24:49 +00001669 ExprKind = UETT_AlignOf;
1670 else if (OpTok.is(tok::kw_vec_step))
1671 ExprKind = UETT_VecStep;
Sebastian Redl90893182008-12-11 22:33:27 +00001672
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001673 if (isCastExpr)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001674 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1675 ExprKind,
1676 /*isType=*/true,
1677 CastTy.getAsOpaquePtr(),
1678 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001679
Chris Lattner26115ac2006-08-24 06:10:04 +00001680 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001681 if (!Operand.isInvalid())
Peter Collingbournee190dee2011-03-11 19:24:49 +00001682 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1683 ExprKind,
1684 /*isType=*/false,
1685 Operand.release(),
1686 CastRange);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001687 return Operand;
Chris Lattner81b576e2006-08-11 02:13:20 +00001688}
1689
Chris Lattner11124352006-08-12 19:16:08 +00001690/// ParseBuiltinPrimaryExpression
1691///
James Dennett3d5e4592012-06-17 04:36:28 +00001692/// \verbatim
Chris Lattner11124352006-08-12 19:16:08 +00001693/// primary-expression: [C99 6.5.1]
1694/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1695/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1696/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1697/// assign-expr ')'
1698/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbourne62c21982011-11-05 03:47:48 +00001699/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001700///
Chris Lattner11124352006-08-12 19:16:08 +00001701/// [GNU] offsetof-member-designator:
1702/// [GNU] identifier
1703/// [GNU] offsetof-member-designator '.' identifier
1704/// [GNU] offsetof-member-designator '[' expression ']'
James Dennett3d5e4592012-06-17 04:36:28 +00001705/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001706ExprResult Parser::ParseBuiltinPrimaryExpression() {
1707 ExprResult Res;
Chris Lattner11124352006-08-12 19:16:08 +00001708 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1709
1710 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001711 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001712
1713 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001714 if (Tok.isNot(tok::l_paren))
1715 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1716 << BuiltinII);
1717
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001718 BalancedDelimiterTracker PT(*this, tok::l_paren);
1719 PT.consumeOpen();
1720
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001721 // TODO: Build AST.
1722
Chris Lattner11124352006-08-12 19:16:08 +00001723 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +00001724 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001725 case tok::kw___builtin_va_arg: {
John McCalldadc5752010-08-24 06:29:42 +00001726 ExprResult Expr(ParseAssignmentExpression());
Chris Lattner0be454e2006-08-12 19:30:51 +00001727
Chris Lattner6d7e6342006-08-15 03:41:14 +00001728 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001729 Expr = ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001730
Douglas Gregor220cac52009-02-18 17:45:20 +00001731 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001732
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001733 if (Tok.isNot(tok::r_paren)) {
1734 Diag(Tok, diag::err_expected_rparen);
Douglas Gregoreda7e542010-09-18 01:28:11 +00001735 Expr = ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001736 }
Douglas Gregoreda7e542010-09-18 01:28:11 +00001737
1738 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor220cac52009-02-18 17:45:20 +00001739 Res = ExprError();
1740 else
John McCallb268a282010-08-23 23:25:46 +00001741 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001742 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001743 }
Chris Lattner687d6092007-08-30 15:51:11 +00001744 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001745 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001746 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001747 if (Ty.isInvalid()) {
1748 SkipUntil(tok::r_paren);
1749 return ExprError();
1750 }
Mike Stump11289f42009-09-09 15:08:12 +00001751
Chris Lattner6d7e6342006-08-15 03:41:14 +00001752 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001753 return ExprError();
1754
Chris Lattner11124352006-08-12 19:16:08 +00001755 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001756 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001757 Diag(Tok, diag::err_expected_ident);
1758 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001759 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001760 }
Sebastian Redl90893182008-12-11 22:33:27 +00001761
Chris Lattner687d6092007-08-30 15:51:11 +00001762 // Keep track of the various subcomponents we see.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001763 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001764
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().U.IdentInfo = Tok.getIdentifierInfo();
1768 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001769
Sebastian Redl511ed552008-11-25 22:21:31 +00001770 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001771 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001772 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001773 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallfaf5fb42010-08-26 23:41:50 +00001774 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001775 Comps.back().isBrackets = false;
1776 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001777
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001778 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001779 Diag(Tok, diag::err_expected_ident);
1780 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001781 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001782 }
1783 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1784 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001785
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001786 } else if (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001787 if (CheckProhibitedCXX11Attribute())
1788 return ExprError();
1789
Chris Lattner11124352006-08-12 19:16:08 +00001790 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallfaf5fb42010-08-26 23:41:50 +00001791 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001792 Comps.back().isBrackets = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001793 BalancedDelimiterTracker ST(*this, tok::l_square);
1794 ST.consumeOpen();
1795 Comps.back().LocStart = ST.getOpenLocation();
Chris Lattner11124352006-08-12 19:16:08 +00001796 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001797 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001798 SkipUntil(tok::r_paren);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001799 return Res;
Chris Lattner11124352006-08-12 19:16:08 +00001800 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001801 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001802
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001803 ST.consumeClose();
1804 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman5e774b12009-06-27 20:38:33 +00001805 } else {
1806 if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001807 PT.consumeClose();
Douglas Gregor220cac52009-02-18 17:45:20 +00001808 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001809 } else if (Ty.isInvalid()) {
1810 Res = ExprError();
1811 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001812 PT.consumeClose();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001813 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001814 Ty.get(), &Comps[0], Comps.size(),
1815 PT.getCloseLocation());
Eli Friedman5e774b12009-06-27 20:38:33 +00001816 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001817 break;
Chris Lattner11124352006-08-12 19:16:08 +00001818 }
1819 }
1820 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001821 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001822 case tok::kw___builtin_choose_expr: {
John McCalldadc5752010-08-24 06:29:42 +00001823 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001824 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001825 SkipUntil(tok::r_paren);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001826 return Cond;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001827 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001828 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001829 return ExprError();
1830
John McCalldadc5752010-08-24 06:29:42 +00001831 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001832 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001833 SkipUntil(tok::r_paren);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001834 return Expr1;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001835 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001836 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001837 return ExprError();
1838
John McCalldadc5752010-08-24 06:29:42 +00001839 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001840 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001841 SkipUntil(tok::r_paren);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001842 return Expr2;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001843 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001844 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001845 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001846 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001847 }
John McCallb268a282010-08-23 23:25:46 +00001848 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1849 Expr2.take(), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001850 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001851 }
Tanya Lattner55808c12011-06-04 00:47:47 +00001852 case tok::kw___builtin_astype: {
1853 // The first argument is an expression to be converted, followed by a comma.
1854 ExprResult Expr(ParseAssignmentExpression());
1855 if (Expr.isInvalid()) {
1856 SkipUntil(tok::r_paren);
1857 return ExprError();
1858 }
1859
1860 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1861 tok::r_paren))
1862 return ExprError();
1863
1864 // Second argument is the type to bitcast to.
1865 TypeResult DestTy = ParseTypeName();
1866 if (DestTy.isInvalid())
1867 return ExprError();
1868
1869 // Attempt to consume the r-paren.
1870 if (Tok.isNot(tok::r_paren)) {
1871 Diag(Tok, diag::err_expected_rparen);
1872 SkipUntil(tok::r_paren);
1873 return ExprError();
1874 }
1875
1876 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1877 ConsumeParen());
1878 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001879 }
Chandler Carruth904cb142011-07-08 04:59:44 +00001880 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001881
John McCallb268a282010-08-23 23:25:46 +00001882 if (Res.isInvalid())
1883 return ExprError();
1884
Chris Lattner11124352006-08-12 19:16:08 +00001885 // These can be followed by postfix-expr pieces because they are
1886 // primary-expressions.
John McCallb268a282010-08-23 23:25:46 +00001887 return ParsePostfixExpressionSuffix(Res.take());
Chris Lattner11124352006-08-12 19:16:08 +00001888}
1889
Chris Lattner4add4e62006-08-11 01:33:00 +00001890/// ParseParenExpression - This parses the unit that starts with a '(' token,
1891/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001892/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1893/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001894///
James Dennett3d5e4592012-06-17 04:36:28 +00001895/// \verbatim
Chris Lattner4add4e62006-08-11 01:33:00 +00001896/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001897/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001898/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1899/// postfix-expression: [C99 6.5.2]
1900/// '(' type-name ')' '{' initializer-list '}'
1901/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001902/// cast-expression: [C99 6.5.4]
1903/// '(' type-name ')' cast-expression
John McCall31168b02011-06-15 23:02:42 +00001904/// [ARC] bridged-cast-expression
1905///
1906/// [ARC] bridged-cast-expression:
1907/// (__bridge type-name) cast-expression
1908/// (__bridge_transfer type-name) cast-expression
1909/// (__bridge_retained type-name) cast-expression
James Dennett3d5e4592012-06-17 04:36:28 +00001910/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001911ExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001912Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001913 bool isTypeCast, ParsedType &CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001914 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001915 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001916 BalancedDelimiterTracker T(*this, tok::l_paren);
1917 if (T.consumeOpen())
1918 return ExprError();
1919 SourceLocation OpenLoc = T.getOpenLocation();
1920
John McCalldadc5752010-08-24 06:29:42 +00001921 ExprResult Result(true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001922 bool isAmbiguousTypeId;
John McCallba7bf592010-08-24 05:47:05 +00001923 CastTy = ParsedType();
Sebastian Redl90893182008-12-11 22:33:27 +00001924
Douglas Gregor5e35d592010-09-14 23:59:36 +00001925 if (Tok.is(tok::code_completion)) {
1926 Actions.CodeCompleteOrdinaryName(getCurScope(),
1927 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1928 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001929 cutOffParsing();
Douglas Gregor5e35d592010-09-14 23:59:36 +00001930 return ExprError();
1931 }
John McCallc5e6b972011-04-06 02:35:25 +00001932
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001933 // Diagnose use of bridge casts in non-arc mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001934 bool BridgeCast = (getLangOpts().ObjC2 &&
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001935 (Tok.is(tok::kw___bridge) ||
1936 Tok.is(tok::kw___bridge_transfer) ||
1937 Tok.is(tok::kw___bridge_retained) ||
1938 Tok.is(tok::kw___bridge_retain)));
David Blaikiebbafb8a2012-03-11 07:00:24 +00001939 if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001940 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001941 SourceLocation BridgeKeywordLoc = ConsumeToken();
1942 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremenek084e1b42012-02-18 04:42:38 +00001943 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001944 << BridgeCastName
1945 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001946 BridgeCast = false;
1947 }
1948
John McCallc5e6b972011-04-06 02:35:25 +00001949 // None of these cases should fall through with an invalid Result
1950 // unless they've already reported an error.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001951 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001952 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall3abee492012-04-04 01:27:53 +00001953 Actions.ActOnStartStmtExpr();
1954
Richard Smithc202b282012-04-14 00:33:13 +00001955 StmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001956 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001957
Chris Lattner366727f2007-07-24 16:58:17 +00001958 // If the substmt parsed correctly, build the AST node.
John McCall3abee492012-04-04 01:27:53 +00001959 if (!Stmt.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001960 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
John McCall3abee492012-04-04 01:27:53 +00001961 } else {
1962 Actions.ActOnStmtExprError();
1963 }
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001964 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCall0c07bee2011-06-17 21:56:12 +00001965 tok::TokenKind tokenKind = Tok.getKind();
1966 SourceLocation BridgeKeywordLoc = ConsumeToken();
1967
John McCall31168b02011-06-15 23:02:42 +00001968 // Parse an Objective-C ARC ownership cast expression.
1969 ObjCBridgeCastKind Kind;
John McCall0c07bee2011-06-17 21:56:12 +00001970 if (tokenKind == tok::kw___bridge)
John McCall31168b02011-06-15 23:02:42 +00001971 Kind = OBC_Bridge;
John McCall0c07bee2011-06-17 21:56:12 +00001972 else if (tokenKind == tok::kw___bridge_transfer)
John McCall31168b02011-06-15 23:02:42 +00001973 Kind = OBC_BridgeTransfer;
John McCall0c07bee2011-06-17 21:56:12 +00001974 else if (tokenKind == tok::kw___bridge_retained)
John McCall31168b02011-06-15 23:02:42 +00001975 Kind = OBC_BridgeRetained;
John McCall0c07bee2011-06-17 21:56:12 +00001976 else {
1977 // As a hopefully temporary workaround, allow __bridge_retain as
1978 // a synonym for __bridge_retained, but only in system headers.
1979 assert(tokenKind == tok::kw___bridge_retain);
1980 Kind = OBC_BridgeRetained;
1981 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1982 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1983 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1984 "__bridge_retained");
1985 }
John McCall31168b02011-06-15 23:02:42 +00001986
John McCall31168b02011-06-15 23:02:42 +00001987 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001988 T.consumeClose();
1989 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001990 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCall31168b02011-06-15 23:02:42 +00001991
1992 if (Ty.isInvalid() || SubExpr.isInvalid())
1993 return ExprError();
1994
1995 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1996 BridgeKeywordLoc, Ty.get(),
1997 RParenLoc, SubExpr.get());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001998 } else if (ExprType >= CompoundLiteral &&
1999 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00002000
Chris Lattner6c3f05d2006-08-12 16:54:25 +00002001 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00002002
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002003 // In C++, if the type-id is ambiguous we disambiguate based on context.
2004 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
2005 // in which case we should treat it as type-id.
2006 // if stopIfCastExpr is false, we need to determine the context past the
2007 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002008 if (isAmbiguousTypeId && !stopIfCastExpr) {
2009 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
2010 RParenLoc = T.getCloseLocation();
2011 return res;
2012 }
Mike Stump11289f42009-09-09 15:08:12 +00002013
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002014 // Parse the type declarator.
2015 DeclSpec DS(AttrFactory);
2016 ParseSpecifierQualifierList(DS);
2017 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2018 ParseDeclarator(DeclaratorInfo);
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002019
Douglas Gregor3e972002010-09-15 23:19:31 +00002020 // If our type is followed by an identifier and either ':' or ']', then
2021 // this is probably an Objective-C message send where the leading '[' is
2022 // missing. Recover as if that were the case.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002023 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002024 !InMessageExpression && getLangOpts().ObjC1 &&
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002025 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
2026 TypeResult Ty;
2027 {
2028 InMessageExpressionRAIIObject InMessage(*this, false);
2029 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2030 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002031 Result = ParseObjCMessageExpressionBody(SourceLocation(),
2032 SourceLocation(),
2033 Ty.get(), 0);
2034 } else {
2035 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002036 T.consumeClose();
2037 RParenLoc = T.getCloseLocation();
Douglas Gregor3e972002010-09-15 23:19:31 +00002038 if (Tok.is(tok::l_brace)) {
2039 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002040 TypeResult Ty;
2041 {
2042 InMessageExpressionRAIIObject InMessage(*this, false);
2043 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2044 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002045 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +00002046 }
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00002047
Douglas Gregor3e972002010-09-15 23:19:31 +00002048 if (ExprType == CastExpr) {
2049 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb5d49352009-01-19 22:31:54 +00002050
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002051 if (DeclaratorInfo.isInvalidType())
Douglas Gregor3e972002010-09-15 23:19:31 +00002052 return ExprError();
2053
Douglas Gregor3e972002010-09-15 23:19:31 +00002054 // Note that this doesn't parse the subsequent cast-expression, it just
2055 // returns the parsed type to the callee.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002056 if (stopIfCastExpr) {
2057 TypeResult Ty;
2058 {
2059 InMessageExpressionRAIIObject InMessage(*this, false);
2060 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2061 }
2062 CastTy = Ty.get();
Douglas Gregor3e972002010-09-15 23:19:31 +00002063 return ExprResult();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002064 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002065
2066 // Reject the cast of super idiom in ObjC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002067 if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
Douglas Gregor3e972002010-09-15 23:19:31 +00002068 Tok.getIdentifierInfo() == Ident_super &&
2069 getCurScope()->isInObjcMethodScope() &&
2070 GetLookAheadToken(1).isNot(tok::period)) {
2071 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2072 << SourceRange(OpenLoc, RParenLoc);
2073 return ExprError();
2074 }
2075
2076 // Parse the cast-expression that follows it next.
2077 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002078 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2079 /*isAddressOfOperand=*/false,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002080 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002081 if (!Result.isInvalid()) {
2082 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2083 DeclaratorInfo, CastTy,
Douglas Gregor3e972002010-09-15 23:19:31 +00002084 RParenLoc, Result.take());
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002085 }
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002086 return Result;
Douglas Gregor3e972002010-09-15 23:19:31 +00002087 }
2088
2089 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2090 return ExprError();
2091 }
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002092 } else if (isTypeCast) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00002093 // Parse the expression-list.
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002094 InMessageExpressionRAIIObject InMessage(*this, false);
2095
Nate Begeman5ec4b312009-08-10 23:49:36 +00002096 ExprVector ArgExprs(Actions);
2097 CommaLocsTy CommaLocs;
2098
2099 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2100 ExprType = SimpleExpr;
Sebastian Redla9351792012-02-11 23:51:47 +00002101 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002102 ArgExprs);
Nate Begeman5ec4b312009-08-10 23:49:36 +00002103 }
Chris Lattner4add4e62006-08-11 01:33:00 +00002104 } else {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002105 InMessageExpressionRAIIObject InMessage(*this, false);
2106
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002107 Result = ParseExpression(MaybeTypeCast);
Chris Lattner4add4e62006-08-11 01:33:00 +00002108 ExprType = SimpleExpr;
John McCallc5e6b972011-04-06 02:35:25 +00002109
2110 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002111 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCallb268a282010-08-23 23:25:46 +00002112 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Chris Lattnerf8339772006-08-10 22:01:51 +00002113 }
Sebastian Redl90893182008-12-11 22:33:27 +00002114
Chris Lattner4564bc12006-08-10 23:14:52 +00002115 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00002116 if (Result.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +00002117 SkipUntil(tok::r_paren);
Chris Lattnerd8980502008-12-12 06:00:12 +00002118 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00002119 }
Mike Stump11289f42009-09-09 15:08:12 +00002120
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002121 T.consumeClose();
2122 RParenLoc = T.getCloseLocation();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002123 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +00002124}
Chris Lattnerd3e98952006-10-06 05:22:26 +00002125
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002126/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2127/// and we are at the left brace.
2128///
James Dennett3d5e4592012-06-17 04:36:28 +00002129/// \verbatim
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002130/// postfix-expression: [C99 6.5.2]
2131/// '(' type-name ')' '{' initializer-list '}'
2132/// '(' type-name ')' '{' initializer-list ',' '}'
James Dennett3d5e4592012-06-17 04:36:28 +00002133/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002134ExprResult
John McCallba7bf592010-08-24 05:47:05 +00002135Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002136 SourceLocation LParenLoc,
2137 SourceLocation RParenLoc) {
2138 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
David Blaikiebbafb8a2012-03-11 07:00:24 +00002139 if (!getLangOpts().C99) // Compound literals don't exist in C90.
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002140 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCalldadc5752010-08-24 06:29:42 +00002141 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002142 if (!Result.isInvalid() && Ty)
John McCallb268a282010-08-23 23:25:46 +00002143 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002144 return Result;
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002145}
2146
Chris Lattnerd3e98952006-10-06 05:22:26 +00002147/// ParseStringLiteralExpression - This handles the various token types that
2148/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2149/// translation phase #6].
2150///
James Dennett3d5e4592012-06-17 04:36:28 +00002151/// \verbatim
Chris Lattnerd3e98952006-10-06 05:22:26 +00002152/// primary-expression: [C99 6.5.1]
2153/// string-literal
James Dennett3d5e4592012-06-17 04:36:28 +00002154/// \verbatim
Richard Smithd67aea22012-03-06 03:21:47 +00002155ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002156 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00002157
Chris Lattnerd3e98952006-10-06 05:22:26 +00002158 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2159 // considered to be strings for concatenation purposes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002160 SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00002161
Chris Lattnerd3e98952006-10-06 05:22:26 +00002162 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002163 StringToks.push_back(Tok);
2164 ConsumeStringToken();
2165 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00002166
2167 // Pass the set of string tokens, ready for concatenation, to the actions.
Richard Smithbcc22fc2012-03-09 08:00:36 +00002168 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(),
2169 AllowUserDefinedLiteral ? getCurScope() : 0);
Chris Lattnerd3e98952006-10-06 05:22:26 +00002170}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002171
Benjamin Kramere56f3932011-12-23 17:00:35 +00002172/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2173/// [C11 6.5.1.1].
Peter Collingbourne91147592011-04-15 00:35:48 +00002174///
James Dennett3d5e4592012-06-17 04:36:28 +00002175/// \verbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002176/// generic-selection:
2177/// _Generic ( assignment-expression , generic-assoc-list )
2178/// generic-assoc-list:
2179/// generic-association
2180/// generic-assoc-list , generic-association
2181/// generic-association:
2182/// type-name : assignment-expression
2183/// default : assignment-expression
James Dennett3d5e4592012-06-17 04:36:28 +00002184/// \endverbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002185ExprResult Parser::ParseGenericSelectionExpression() {
2186 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2187 SourceLocation KeyLoc = ConsumeToken();
2188
David Blaikiebbafb8a2012-03-11 07:00:24 +00002189 if (!getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +00002190 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbourne91147592011-04-15 00:35:48 +00002191
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002192 BalancedDelimiterTracker T(*this, tok::l_paren);
2193 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne91147592011-04-15 00:35:48 +00002194 return ExprError();
2195
2196 ExprResult ControllingExpr;
2197 {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002198 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbourne91147592011-04-15 00:35:48 +00002199 // not evaluated."
2200 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2201 ControllingExpr = ParseAssignmentExpression();
2202 if (ControllingExpr.isInvalid()) {
2203 SkipUntil(tok::r_paren);
2204 return ExprError();
2205 }
2206 }
2207
2208 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2209 SkipUntil(tok::r_paren);
2210 return ExprError();
2211 }
2212
2213 SourceLocation DefaultLoc;
2214 TypeVector Types(Actions);
2215 ExprVector Exprs(Actions);
2216 while (1) {
2217 ParsedType Ty;
2218 if (Tok.is(tok::kw_default)) {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002219 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbourne91147592011-04-15 00:35:48 +00002220 // generic association."
2221 if (!DefaultLoc.isInvalid()) {
2222 Diag(Tok, diag::err_duplicate_default_assoc);
2223 Diag(DefaultLoc, diag::note_previous_default_assoc);
2224 SkipUntil(tok::r_paren);
2225 return ExprError();
2226 }
2227 DefaultLoc = ConsumeToken();
2228 Ty = ParsedType();
2229 } else {
2230 ColonProtectionRAIIObject X(*this);
2231 TypeResult TR = ParseTypeName();
2232 if (TR.isInvalid()) {
2233 SkipUntil(tok::r_paren);
2234 return ExprError();
2235 }
2236 Ty = TR.release();
2237 }
2238 Types.push_back(Ty);
2239
2240 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2241 SkipUntil(tok::r_paren);
2242 return ExprError();
2243 }
2244
2245 // FIXME: These expressions should be parsed in a potentially potentially
2246 // evaluated context.
2247 ExprResult ER(ParseAssignmentExpression());
2248 if (ER.isInvalid()) {
2249 SkipUntil(tok::r_paren);
2250 return ExprError();
2251 }
2252 Exprs.push_back(ER.release());
2253
2254 if (Tok.isNot(tok::comma))
2255 break;
2256 ConsumeToken();
2257 }
2258
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002259 T.consumeClose();
2260 if (T.getCloseLocation().isInvalid())
Peter Collingbourne91147592011-04-15 00:35:48 +00002261 return ExprError();
2262
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002263 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2264 T.getCloseLocation(),
Peter Collingbourne91147592011-04-15 00:35:48 +00002265 ControllingExpr.release(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002266 Types, Exprs);
Peter Collingbourne91147592011-04-15 00:35:48 +00002267}
2268
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002269/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2270///
James Dennett3d5e4592012-06-17 04:36:28 +00002271/// \verbatim
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002272/// argument-expression-list:
2273/// assignment-expression
2274/// argument-expression-list , assignment-expression
2275///
2276/// [C++] expression-list:
Sebastian Redl3da34892011-06-05 12:23:16 +00002277/// [C++] assignment-expression
2278/// [C++] expression-list , assignment-expression
2279///
2280/// [C++0x] expression-list:
2281/// [C++0x] initializer-list
2282///
2283/// [C++0x] initializer-list
2284/// [C++0x] initializer-clause ...[opt]
2285/// [C++0x] initializer-list , initializer-clause ...[opt]
2286///
2287/// [C++0x] initializer-clause:
2288/// [C++0x] assignment-expression
2289/// [C++0x] braced-init-list
James Dennett3d5e4592012-06-17 04:36:28 +00002290/// \endverbatim
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002291bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2292 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallfaf5fb42010-08-26 23:41:50 +00002293 void (Sema::*Completer)(Scope *S,
John McCall37ad5512010-08-23 06:44:23 +00002294 Expr *Data,
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002295 llvm::ArrayRef<Expr *> Args),
John McCall37ad5512010-08-23 06:44:23 +00002296 Expr *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002297 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00002298 if (Tok.is(tok::code_completion)) {
2299 if (Completer)
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002300 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor9c1f1bf2011-02-17 03:09:23 +00002301 else
2302 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002303 cutOffParsing();
2304 return true;
Douglas Gregorcabea402009-09-22 15:41:20 +00002305 }
Sebastian Redl3da34892011-06-05 12:23:16 +00002306
2307 ExprResult Expr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002308 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002309 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00002310 Expr = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00002311 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00002312 Expr = ParseAssignmentExpression();
2313
Douglas Gregor968f23a2011-01-03 19:31:53 +00002314 if (Tok.is(tok::ellipsis))
2315 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002316 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002317 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00002318
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002319 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002320
2321 if (Tok.isNot(tok::comma))
2322 return false;
2323 // Move to the next argument, remember where the comma was.
2324 CommaLocs.push_back(ConsumeToken());
2325 }
2326}
Steve Naroff0ac012832008-08-28 19:20:44 +00002327
Mike Stump82f071f2009-02-04 22:31:32 +00002328/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2329///
James Dennett3d5e4592012-06-17 04:36:28 +00002330/// \verbatim
Mike Stump82f071f2009-02-04 22:31:32 +00002331/// [clang] block-id:
2332/// [clang] specifier-qualifier-list block-declarator
James Dennett3d5e4592012-06-17 04:36:28 +00002333/// \endverbatim
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002334void Parser::ParseBlockId(SourceLocation CaretLoc) {
Douglas Gregor643c3302010-10-18 21:34:55 +00002335 if (Tok.is(tok::code_completion)) {
2336 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002337 return cutOffParsing();
Douglas Gregor643c3302010-10-18 21:34:55 +00002338 }
2339
Mike Stump82f071f2009-02-04 22:31:32 +00002340 // Parse the specifier-qualifier-list piece.
John McCall084e83d2011-03-24 11:26:52 +00002341 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002342 ParseSpecifierQualifierList(DS);
2343
2344 // Parse the block-declarator.
2345 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2346 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002347
Mike Stump56ed2ea2009-04-29 21:40:37 +00002348 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall084e83d2011-03-24 11:26:52 +00002349 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump56ed2ea2009-04-29 21:40:37 +00002350
John McCall53fa7142010-12-24 02:08:15 +00002351 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002352
Mike Stump82f071f2009-02-04 22:31:32 +00002353 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002354 Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
Mike Stump82f071f2009-02-04 22:31:32 +00002355}
2356
Steve Naroff0ac012832008-08-28 19:20:44 +00002357/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00002358/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00002359///
James Dennett3d5e4592012-06-17 04:36:28 +00002360/// \verbatim
Steve Naroff0ac012832008-08-28 19:20:44 +00002361/// block-literal:
2362/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00002363/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00002364/// [clang] block-args:
2365/// [clang] '(' parameter-list ')'
James Dennett3d5e4592012-06-17 04:36:28 +00002366/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002367ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00002368 assert(Tok.is(tok::caret) && "block literal starts with ^");
2369 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002370
Chris Lattnerf6801202009-03-05 07:32:12 +00002371 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2372 "block literal parsing");
2373
Mike Stump11289f42009-09-09 15:08:12 +00002374 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00002375 // argument decls, decls within the compound expression, etc. This also
2376 // allows determining whether a variable reference inside the block is
2377 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002378 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002379 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00002380
2381 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002382 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump11289f42009-09-09 15:08:12 +00002383
Steve Naroff0ac012832008-08-28 19:20:44 +00002384 // Parse the return type if present.
John McCall084e83d2011-03-24 11:26:52 +00002385 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002386 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002387 // FIXME: Since the return type isn't actually parsed, it can't be used to
2388 // fill ParamInfo with an initial valid range, so do it manually.
2389 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002390
Steve Naroff0ac012832008-08-28 19:20:44 +00002391 // If this block has arguments, parse them. There is no ambiguity here with
2392 // the expression case, because the expression case requires a parameter list.
2393 if (Tok.is(tok::l_paren)) {
2394 ParseParenDeclarator(ParamInfo);
2395 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002396 // SetIdentifier sets the source range end, but in this case we're past
2397 // that location.
2398 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00002399 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002400 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002401 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00002402 // If there was an error parsing the arguments, they may have
2403 // tried to use ^(x+y) which requires an argument list. Just
2404 // skip the whole block literal.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002405 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002406 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00002407 }
Mike Stump88788fe2009-04-29 19:03:13 +00002408
John McCall53fa7142010-12-24 02:08:15 +00002409 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002410
Mike Stump82f071f2009-02-04 22:31:32 +00002411 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002412 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Mike Stumpd73e4412009-04-14 18:24:37 +00002413 } else if (!Tok.is(tok::l_brace)) {
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002414 ParseBlockId(CaretLoc);
Steve Naroff0ac012832008-08-28 19:20:44 +00002415 } else {
2416 // Otherwise, pretend we saw (void).
John McCall084e83d2011-03-24 11:26:52 +00002417 ParsedAttributes attrs(AttrFactory);
Richard Smith943c4402012-07-30 21:30:52 +00002418 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false, false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002419 SourceLocation(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002420 0, 0, 0,
Douglas Gregor54992352011-01-26 03:43:54 +00002421 true, SourceLocation(),
Douglas Gregorad69e652011-07-13 21:47:47 +00002422 SourceLocation(),
Douglas Gregore248eea2011-10-19 06:04:55 +00002423 SourceLocation(),
2424 SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00002425 EST_None,
2426 SourceLocation(),
Richard Smith2331bbf2012-05-02 22:22:32 +00002427 0, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002428 CaretLoc, CaretLoc,
2429 ParamInfo),
John McCall084e83d2011-03-24 11:26:52 +00002430 attrs, CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00002431
John McCall53fa7142010-12-24 02:08:15 +00002432 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002433
Mike Stump82f071f2009-02-04 22:31:32 +00002434 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002435 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Steve Naroff0ac012832008-08-28 19:20:44 +00002436 }
2437
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002438
John McCalldadc5752010-08-24 06:29:42 +00002439 ExprResult Result(true);
Chris Lattner9eac9312009-03-27 04:18:06 +00002440 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002441 // Saw something like: ^expr
2442 Diag(Tok, diag::err_expected_expression);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002443 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002444 return ExprError();
2445 }
Mike Stump11289f42009-09-09 15:08:12 +00002446
John McCalldadc5752010-08-24 06:29:42 +00002447 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002448 BlockScope.Exit();
Chris Lattner9eac9312009-03-27 04:18:06 +00002449 if (!Stmt.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002450 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9eac9312009-03-27 04:18:06 +00002451 else
Douglas Gregor0be31a22010-07-02 17:43:08 +00002452 Actions.ActOnBlockError(CaretLoc, getCurScope());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002453 return Result;
Steve Naroff0ac012832008-08-28 19:20:44 +00002454}
Ted Kremeneke65b0862012-03-06 20:05:56 +00002455
2456/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
2457///
2458/// '__objc_yes'
2459/// '__objc_no'
2460ExprResult Parser::ParseObjCBoolLiteral() {
2461 tok::TokenKind Kind = Tok.getKind();
2462 return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
2463}