blob: de0af318a5bcae4482fe23db6f1acccfe8fcec33 [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 Dennett3d5e4592012-06-17 04:36:28 +00009
10/// \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.
Chris Lattnerc951dae2006-08-10 04:23:57 +000021
22#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/Scope.h"
25#include "clang/Sema/ParsedTemplate.h"
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +000026#include "clang/Sema/TypoCorrection.h"
Chris Lattnerf6801202009-03-05 07:32:12 +000027#include "clang/Basic/PrettyStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000028#include "RAIIObjectsForParser.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattner834618d2006-11-03 07:48:41 +000030#include "llvm/ADT/SmallString.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000031using namespace clang;
32
James Dennett3d5e4592012-06-17 04:36:28 +000033/// \brief Return the precedence of the specified binary operator token.
Mike Stump11289f42009-09-09 15:08:12 +000034static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregorcbb45d02009-02-25 23:02:36 +000035 bool GreaterThanIsOperator,
36 bool CPlusPlus0x) {
Chris Lattnercde626a2006-08-12 08:13:25 +000037 switch (Kind) {
Douglas Gregor8bf42052009-02-09 18:46:07 +000038 case tok::greater:
Douglas Gregorcbb45d02009-02-25 23:02:36 +000039 // C++ [temp.names]p3:
40 // [...] When parsing a template-argument-list, the first
41 // non-nested > is taken as the ending delimiter rather than a
42 // greater-than operator. [...]
Douglas Gregor8bf42052009-02-09 18:46:07 +000043 if (GreaterThanIsOperator)
44 return prec::Relational;
45 return prec::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +000046
Douglas Gregorcbb45d02009-02-25 23:02:36 +000047 case tok::greatergreater:
48 // C++0x [temp.names]p3:
49 //
50 // [...] Similarly, the first non-nested >> is treated as two
51 // consecutive but distinct > tokens, the first of which is
52 // taken as the end of the template-argument-list and completes
53 // the template-id. [...]
54 if (GreaterThanIsOperator || !CPlusPlus0x)
55 return prec::Shift;
56 return prec::Unknown;
57
Chris Lattnercde626a2006-08-12 08:13:25 +000058 default: return prec::Unknown;
59 case tok::comma: return prec::Comma;
60 case tok::equal:
61 case tok::starequal:
62 case tok::slashequal:
63 case tok::percentequal:
64 case tok::plusequal:
65 case tok::minusequal:
66 case tok::lesslessequal:
67 case tok::greatergreaterequal:
68 case tok::ampequal:
69 case tok::caretequal:
70 case tok::pipeequal: return prec::Assignment;
71 case tok::question: return prec::Conditional;
72 case tok::pipepipe: return prec::LogicalOr;
73 case tok::ampamp: return prec::LogicalAnd;
74 case tok::pipe: return prec::InclusiveOr;
75 case tok::caret: return prec::ExclusiveOr;
76 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +000077 case tok::exclaimequal:
78 case tok::equalequal: return prec::Equality;
79 case tok::lessequal:
80 case tok::less:
Douglas Gregor8bf42052009-02-09 18:46:07 +000081 case tok::greaterequal: return prec::Relational;
Douglas Gregorcbb45d02009-02-25 23:02:36 +000082 case tok::lessless: return prec::Shift;
Chris Lattnercde626a2006-08-12 08:13:25 +000083 case tok::plus:
84 case tok::minus: return prec::Additive;
85 case tok::percent:
86 case tok::slash:
87 case tok::star: return prec::Multiplicative;
Sebastian Redl112a97662009-02-07 00:15:38 +000088 case tok::periodstar:
89 case tok::arrowstar: return prec::PointerToMember;
Chris Lattnercde626a2006-08-12 08:13:25 +000090 }
91}
92
93
James Dennett3d5e4592012-06-17 04:36:28 +000094/// \brief Simple precedence-based parser for binary/ternary operators.
Chris Lattnercde626a2006-08-12 08:13:25 +000095///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000096/// Note: we diverge from the C99 grammar when parsing the assignment-expression
97/// production. C99 specifies that the LHS of an assignment operator should be
98/// parsed as a unary-expression, but consistency dictates that it be a
99/// conditional-expession. In practice, the important thing here is that the
100/// LHS of an assignment has to be an l-value, which productions between
101/// unary-expression and conditional-expression don't produce. Because we want
102/// consistency, we parse the LHS as a conditional-expression, then check for
103/// l-value-ness in semantic analysis stages.
104///
James Dennett3d5e4592012-06-17 04:36:28 +0000105/// \verbatim
Sebastian Redl112a97662009-02-07 00:15:38 +0000106/// pm-expression: [C++ 5.5]
107/// cast-expression
108/// pm-expression '.*' cast-expression
109/// pm-expression '->*' cast-expression
110///
Chris Lattnercde626a2006-08-12 08:13:25 +0000111/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl112a97662009-02-07 00:15:38 +0000112/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000113/// cast-expression
114/// multiplicative-expression '*' cast-expression
115/// multiplicative-expression '/' cast-expression
116/// multiplicative-expression '%' cast-expression
117///
118/// additive-expression: [C99 6.5.6]
119/// multiplicative-expression
120/// additive-expression '+' multiplicative-expression
121/// additive-expression '-' multiplicative-expression
122///
123/// shift-expression: [C99 6.5.7]
124/// additive-expression
125/// shift-expression '<<' additive-expression
126/// shift-expression '>>' additive-expression
127///
128/// relational-expression: [C99 6.5.8]
129/// shift-expression
130/// relational-expression '<' shift-expression
131/// relational-expression '>' shift-expression
132/// relational-expression '<=' shift-expression
133/// relational-expression '>=' shift-expression
134///
135/// equality-expression: [C99 6.5.9]
136/// relational-expression
137/// equality-expression '==' relational-expression
138/// equality-expression '!=' relational-expression
139///
140/// AND-expression: [C99 6.5.10]
141/// equality-expression
142/// AND-expression '&' equality-expression
143///
144/// exclusive-OR-expression: [C99 6.5.11]
145/// AND-expression
146/// exclusive-OR-expression '^' AND-expression
147///
148/// inclusive-OR-expression: [C99 6.5.12]
149/// exclusive-OR-expression
150/// inclusive-OR-expression '|' exclusive-OR-expression
151///
152/// logical-AND-expression: [C99 6.5.13]
153/// inclusive-OR-expression
154/// logical-AND-expression '&&' inclusive-OR-expression
155///
156/// logical-OR-expression: [C99 6.5.14]
157/// logical-AND-expression
158/// logical-OR-expression '||' logical-AND-expression
159///
160/// conditional-expression: [C99 6.5.15]
161/// logical-OR-expression
162/// logical-OR-expression '?' expression ':' conditional-expression
163/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl1a99f442009-04-16 17:51:27 +0000164/// [C++] the third operand is an assignment-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000165///
166/// assignment-expression: [C99 6.5.16]
167/// conditional-expression
168/// unary-expression assignment-operator assignment-expression
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000169/// [C++] throw-expression [C++ 15]
Chris Lattnercde626a2006-08-12 08:13:25 +0000170///
171/// assignment-operator: one of
172/// = *= /= %= += -= <<= >>= &= ^= |=
173///
174/// expression: [C99 6.5.17]
Douglas Gregor968f23a2011-01-03 19:31:53 +0000175/// assignment-expression ...[opt]
176/// expression ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +0000177/// \endverbatim
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000178ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
179 ExprResult LHS(ParseAssignmentExpression(isTypeCast));
Sebastian Redl90893182008-12-11 22:33:27 +0000180 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Chris Lattnercde626a2006-08-12 08:13:25 +0000181}
182
Mike Stump11289f42009-09-09 15:08:12 +0000183/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000184/// Current token is an Identifier and is not a 'try'. This
James Dennettf44874f2012-06-15 06:52:33 +0000185/// routine is necessary to disambiguate \@try-statement from,
186/// for example, \@encode-expression.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000187///
John McCalldadc5752010-08-24 06:29:42 +0000188ExprResult
Sebastian Redl90893182008-12-11 22:33:27 +0000189Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000190 ExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redl90893182008-12-11 22:33:27 +0000191 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000192}
193
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000194/// This routine is called when a leading '__extension__' is seen and
195/// consumed. This is necessary because the token gets consumed in the
196/// process of disambiguating between an expression and a declaration.
John McCalldadc5752010-08-24 06:29:42 +0000197ExprResult
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000198Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000199 ExprResult LHS(true);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000200 {
201 // Silence extension warnings in the sub-expression
202 ExtensionRAIIObject O(Diags);
203
204 LHS = ParseCastExpression(false);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000205 }
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000206
Douglas Gregor29d907d2010-09-17 22:25:06 +0000207 if (!LHS.isInvalid())
208 LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
209 LHS.take());
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000210
Douglas Gregor29d907d2010-09-17 22:25:06 +0000211 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000212}
213
James Dennett3d5e4592012-06-17 04:36:28 +0000214/// \brief Parse an expr that doesn't include (top-level) commas.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000215ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000216 if (Tok.is(tok::code_completion)) {
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000217 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000218 cutOffParsing();
219 return ExprError();
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000220 }
221
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000222 if (Tok.is(tok::kw_throw))
Sebastian Redld65cea82008-12-11 22:51:44 +0000223 return ParseThrowExpression();
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000224
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000225 ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
226 /*isAddressOfOperand=*/false,
227 isTypeCast);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000228 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000229}
230
James Dennett3d5e4592012-06-17 04:36:28 +0000231/// \brief Parse an assignment expression where part of an Objective-C message
232/// send has already been parsed.
233///
234/// In this case \p LBracLoc indicates the location of the '[' of the message
235/// send, and either \p ReceiverName or \p ReceiverExpr is non-null indicating
236/// the receiver of the message.
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000237///
238/// Since this handles full assignment-expression's, it handles postfix
239/// expressions and other binary operators for these expressions as well.
John McCalldadc5752010-08-24 06:29:42 +0000240ExprResult
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000241Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000242 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +0000243 ParsedType ReceiverType,
John McCallb268a282010-08-23 23:25:46 +0000244 Expr *ReceiverExpr) {
John McCalldadc5752010-08-24 06:29:42 +0000245 ExprResult R
John McCallb268a282010-08-23 23:25:46 +0000246 = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
247 ReceiverType, ReceiverExpr);
Douglas Gregoreda7e542010-09-18 01:28:11 +0000248 R = ParsePostfixExpressionSuffix(R);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000249 return ParseRHSOfBinaryExpression(R, prec::Assignment);
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000250}
251
252
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000253ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
Richard Smith764d2fe2011-12-20 02:08:33 +0000254 // C++03 [basic.def.odr]p2:
Mike Stump11289f42009-09-09 15:08:12 +0000255 // An expression is potentially evaluated unless it appears where an
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000256 // integral constant expression is required (see 5.19) [...].
Richard Smith764d2fe2011-12-20 02:08:33 +0000257 // 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 +0000258 EnterExpressionEvaluationContext Unevaluated(Actions,
Richard Smith764d2fe2011-12-20 02:08:33 +0000259 Sema::ConstantEvaluated);
Mike Stump11289f42009-09-09 15:08:12 +0000260
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000261 ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
Eli Friedmanc6237c62012-02-29 03:16:56 +0000262 ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
263 return Actions.ActOnConstantExpression(Res);
Chris Lattner3b561a32006-08-13 00:12:11 +0000264}
265
James Dennett3d5e4592012-06-17 04:36:28 +0000266/// \brief Parse a binary expression that starts with \p LHS and has a
267/// precedence of at least \p MinPrec.
John McCalldadc5752010-08-24 06:29:42 +0000268ExprResult
269Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000270 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
271 GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000272 getLangOpts().CPlusPlus0x);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000273 SourceLocation ColonLoc;
274
Chris Lattnercde626a2006-08-12 08:13:25 +0000275 while (1) {
276 // If this token has a lower precedence than we are allowed to parse (e.g.
277 // because we are called recursively, or because the token is not a binop),
278 // then we are done!
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000279 if (NextTokPrec < MinPrec)
Sebastian Redl90893182008-12-11 22:33:27 +0000280 return move(LHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000281
282 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000283 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000284 ConsumeToken();
Sebastian Redl112a97662009-02-07 00:15:38 +0000285
Chris Lattner96c3deb2006-08-12 17:13:08 +0000286 // Special case handling for the ternary operator.
John McCalldadc5752010-08-24 06:29:42 +0000287 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000288 if (NextTokPrec == prec::Conditional) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000289 if (Tok.isNot(tok::colon)) {
Chris Lattner244b96b2009-12-10 02:02:58 +0000290 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
291 ColonProtectionRAIIObject X(*this);
292
Chris Lattner96c3deb2006-08-12 17:13:08 +0000293 // Handle this production specially:
294 // logical-OR-expression '?' expression ':' conditional-expression
295 // In particular, the RHS of the '?' is 'expression', not
296 // 'logical-OR-expression' as we might expect.
297 TernaryMiddle = ParseExpression();
Douglas Gregorec06c122010-09-17 22:41:34 +0000298 if (TernaryMiddle.isInvalid()) {
299 LHS = ExprError();
300 TernaryMiddle = 0;
301 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000302 } else {
303 // Special case handling of "X ? Y : Z" where Y is empty:
304 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redlc13f2682008-12-09 20:22:58 +0000305 TernaryMiddle = 0;
Chris Lattner96c3deb2006-08-12 17:13:08 +0000306 Diag(Tok, diag::ext_gnu_conditional_expr);
307 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000308
Chris Lattner0151b7e2010-04-20 21:33:39 +0000309 if (Tok.is(tok::colon)) {
310 // Eat the colon.
311 ColonLoc = ConsumeToken();
312 } else {
Chandler Carruth0b5cf7c2011-07-26 05:19:46 +0000313 // Otherwise, we're missing a ':'. Assume that this was a typo that
314 // the user forgot. If we're not in a macro expansion, we can suggest
315 // a fixit hint. If there were two spaces before the current token,
Chris Lattnerfb585152010-05-24 22:31:37 +0000316 // suggest inserting the colon in between them, otherwise insert ": ".
317 SourceLocation FILoc = Tok.getLocation();
318 const char *FIText = ": ";
Argyrios Kyrtzidis3ea4adb2011-06-24 17:28:29 +0000319 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis1b07c342012-01-19 15:59:08 +0000320 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
321 assert(FILoc.isFileID());
Chris Lattnerfb585152010-05-24 22:31:37 +0000322 bool IsInvalid = false;
323 const char *SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000324 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000325 if (!IsInvalid && *SourcePtr == ' ') {
326 SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000327 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000328 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000329 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattnerfb585152010-05-24 22:31:37 +0000330 FIText = ":";
331 }
332 }
333 }
334
Ted Kremeneke6013652010-04-12 22:10:35 +0000335 Diag(Tok, diag::err_expected_colon)
Chris Lattnerfb585152010-05-24 22:31:37 +0000336 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner03c40412008-11-23 23:17:07 +0000337 Diag(OpToken, diag::note_matching) << "?";
Chris Lattner0151b7e2010-04-20 21:33:39 +0000338 ColonLoc = Tok.getLocation();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000339 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000340 }
Fariborz Jahanian9a14b842009-10-23 21:01:39 +0000341
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000342 // Code completion for the right-hand side of an assignment expression
343 // goes through a special hook that takes the left-hand side into account.
344 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000345 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000346 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000347 return ExprError();
348 }
349
Chris Lattner96c3deb2006-08-12 17:13:08 +0000350 // Parse another leaf here for the RHS of the operator.
Sebastian Redl1a99f442009-04-16 17:51:27 +0000351 // ParseCastExpression works here because all RHS expressions in C have it
352 // as a prefix, at least. However, in C++, an assignment-expression could
353 // be a throw-expression, which is not a valid cast-expression.
354 // Therefore we need some special-casing here.
355 // Also note that the third operand of the conditional operator is
Richard Smith9a6403a2012-02-26 23:40:27 +0000356 // an assignment-expression in C++, and in C++11, we can have a
Richard Smith5e0cac72012-03-01 02:59:17 +0000357 // braced-init-list on the RHS of an assignment. For better diagnostics,
358 // parse as if we were allowed braced-init-lists everywhere, and check that
359 // they only appear on the RHS of assignments later.
John McCalldadc5752010-08-24 06:29:42 +0000360 ExprResult RHS;
Richard Smithebcd2352012-03-01 07:10:06 +0000361 bool RHSIsInitList = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000362 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith9a6403a2012-02-26 23:40:27 +0000363 RHS = ParseBraceInitializer();
Richard Smithebcd2352012-03-01 07:10:06 +0000364 RHSIsInitList = true;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000365 } else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional)
Sebastian Redl1a99f442009-04-16 17:51:27 +0000366 RHS = ParseAssignmentExpression();
Richard Smith5e0cac72012-03-01 02:59:17 +0000367 else
Sebastian Redl1a99f442009-04-16 17:51:27 +0000368 RHS = ParseCastExpression(false);
Chris Lattnercde626a2006-08-12 08:13:25 +0000369
Douglas Gregor29d907d2010-09-17 22:25:06 +0000370 if (RHS.isInvalid())
371 LHS = ExprError();
372
Chris Lattnercde626a2006-08-12 08:13:25 +0000373 // Remember the precedence of this operator and get the precedence of the
374 // operator immediately to the right of the RHS.
Douglas Gregor8d4de672010-04-21 22:36:40 +0000375 prec::Level ThisPrec = NextTokPrec;
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000376 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000377 getLangOpts().CPlusPlus0x);
Chris Lattner89d53752006-08-12 17:18:19 +0000378
379 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000380 bool isRightAssoc = ThisPrec == prec::Conditional ||
381 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000382
383 // Get the precedence of the operator to the right of the RHS. If it binds
384 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000385 if (ThisPrec < NextTokPrec ||
386 (ThisPrec == NextTokPrec && isRightAssoc)) {
Richard Smithebcd2352012-03-01 07:10:06 +0000387 if (!RHS.isInvalid() && RHSIsInitList) {
388 Diag(Tok, diag::err_init_list_bin_op)
389 << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get());
390 RHS = ExprError();
Richard Smith5e0cac72012-03-01 02:59:17 +0000391 }
Chris Lattner89d53752006-08-12 17:18:19 +0000392 // If this is left-associative, only parse things on the RHS that bind
393 // more tightly than the current operator. If it is left-associative, it
394 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
395 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl511ed552008-11-25 22:21:31 +0000396 // The function takes ownership of the RHS.
Douglas Gregor29d907d2010-09-17 22:25:06 +0000397 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor8d4de672010-04-21 22:36:40 +0000398 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Richard Smithebcd2352012-03-01 07:10:06 +0000399 RHSIsInitList = false;
Douglas Gregor29d907d2010-09-17 22:25:06 +0000400
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000401 if (RHS.isInvalid())
Douglas Gregor29d907d2010-09-17 22:25:06 +0000402 LHS = ExprError();
Chris Lattnercde626a2006-08-12 08:13:25 +0000403
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000404 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000405 getLangOpts().CPlusPlus0x);
Chris Lattnercde626a2006-08-12 08:13:25 +0000406 }
407 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl511ed552008-11-25 22:21:31 +0000408
Richard Smithebcd2352012-03-01 07:10:06 +0000409 if (!RHS.isInvalid() && RHSIsInitList) {
Richard Smith5e0cac72012-03-01 02:59:17 +0000410 if (ThisPrec == prec::Assignment) {
411 Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
Richard Smithebcd2352012-03-01 07:10:06 +0000412 << Actions.getExprRange(RHS.get());
Richard Smith5e0cac72012-03-01 02:59:17 +0000413 } else {
414 Diag(OpToken, diag::err_init_list_bin_op)
Richard Smithebcd2352012-03-01 07:10:06 +0000415 << /*RHS*/1 << PP.getSpelling(OpToken)
416 << Actions.getExprRange(RHS.get());
Richard Smith5e0cac72012-03-01 02:59:17 +0000417 LHS = ExprError();
418 }
419 }
420
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000421 if (!LHS.isInvalid()) {
Chris Lattner319079c2007-08-31 05:01:50 +0000422 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor87f95b02009-02-26 21:00:50 +0000423 if (TernaryMiddle.isInvalid()) {
424 // If we're using '>>' as an operator within a template
425 // argument list (in C++98), suggest the addition of
426 // parentheses so that the code remains well-formed in C++0x.
427 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
428 SuggestParentheses(OpToken.getLocation(),
429 diag::warn_cxx0x_right_shift_in_template_arg,
430 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
431 Actions.getExprRange(RHS.get()).getEnd()));
432
Douglas Gregor0be31a22010-07-02 17:43:08 +0000433 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCallb268a282010-08-23 23:25:46 +0000434 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor87f95b02009-02-26 21:00:50 +0000435 } else
Steve Naroff83895f72007-09-16 03:34:24 +0000436 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000437 LHS.take(), TernaryMiddle.take(),
438 RHS.take());
Chris Lattner319079c2007-08-31 05:01:50 +0000439 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000440 }
441}
442
James Dennett3d5e4592012-06-17 04:36:28 +0000443/// \brief Parse a cast-expression, or, if \p isUnaryExpression is true,
444/// parse a unary-expression.
445///
446/// \p isAddressOfOperand exists because an id-expression that is the
447/// operand of address-of gets special treatment due to member pointers.
Chris Lattnereaf06592006-08-11 02:02:23 +0000448///
John McCalldadc5752010-08-24 06:29:42 +0000449ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000450 bool isAddressOfOperand,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000451 TypeCastState isTypeCast) {
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000452 bool NotCastExpr;
John McCalldadc5752010-08-24 06:29:42 +0000453 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor29d907d2010-09-17 22:25:06 +0000454 isAddressOfOperand,
455 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +0000456 isTypeCast);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000457 if (NotCastExpr)
458 Diag(Tok, diag::err_expected_expression);
459 return move(Res);
460}
461
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000462namespace {
463class CastExpressionIdValidator : public CorrectionCandidateCallback {
464 public:
465 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
466 : AllowNonTypes(AllowNonTypes) {
467 WantTypeSpecifiers = AllowTypes;
468 }
469
470 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
471 NamedDecl *ND = candidate.getCorrectionDecl();
472 if (!ND)
473 return candidate.isKeyword();
474
475 if (isa<TypeDecl>(ND))
476 return WantTypeSpecifiers;
477 return AllowNonTypes;
478 }
479
480 private:
481 bool AllowNonTypes;
482};
483}
484
James Dennett3d5e4592012-06-17 04:36:28 +0000485/// \brief Parse a cast-expression, or, if \pisUnaryExpression is true, parse
486/// a unary-expression.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000487///
James Dennett3d5e4592012-06-17 04:36:28 +0000488/// \p isAddressOfOperand exists because an id-expression that is the operand
489/// of address-of gets special treatment due to member pointers. NotCastExpr
490/// is set to true if the token is not the start of a cast-expression, and no
491/// diagnostic is emitted in this case.
492///
493/// \verbatim
Chris Lattner4564bc12006-08-10 23:14:52 +0000494/// cast-expression: [C99 6.5.4]
495/// unary-expression
496/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000497///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000498/// unary-expression: [C99 6.5.3]
499/// postfix-expression
500/// '++' unary-expression
501/// '--' unary-expression
502/// unary-operator cast-expression
503/// 'sizeof' unary-expression
504/// 'sizeof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000505/// [C++11] 'sizeof' '...' '(' identifier ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000506/// [GNU] '__alignof' unary-expression
507/// [GNU] '__alignof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000508/// [C++11] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000509/// [GNU] '&&' identifier
Richard Smithd67aea22012-03-06 03:21:47 +0000510/// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7]
Sebastian Redlbd150f42008-11-21 19:14:01 +0000511/// [C++] new-expression
512/// [C++] delete-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000513///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000514/// unary-operator: one of
515/// '&' '*' '+' '-' '~' '!'
516/// [GNU] '__extension__' '__real' '__imag'
517///
Chris Lattner52a99e52006-08-10 20:56:00 +0000518/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000519/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000520/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000521/// constant
522/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000523/// [C++] boolean-literal [C++ 2.13.5]
Richard Smithd67aea22012-03-06 03:21:47 +0000524/// [C++11] 'nullptr' [C++11 2.14.7]
525/// [C++11] user-defined-literal
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000526/// '(' expression ')'
Benjamin Kramere56f3932011-12-23 17:00:35 +0000527/// [C11] generic-selection
Chris Lattner52a99e52006-08-10 20:56:00 +0000528/// '__func__' [C99 6.4.2.2]
529/// [GNU] '__FUNCTION__'
530/// [GNU] '__PRETTY_FUNCTION__'
531/// [GNU] '(' compound-statement ')'
532/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
533/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
534/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
535/// assign-expr ')'
536/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000537/// [GNU] '__null'
Mike Stump11289f42009-09-09 15:08:12 +0000538/// [OBJC] '[' objc-message-expr ']'
James Dennettf44874f2012-06-15 06:52:33 +0000539/// [OBJC] '\@selector' '(' objc-selector-arg ')'
540/// [OBJC] '\@protocol' '(' identifier ')'
541/// [OBJC] '\@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000542/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000543/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000544/// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3]
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000545/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000546/// [C++11] typename-specifier braced-init-list [C++11 5.2.3]
Bill Wendlinga6930032007-06-29 18:21:34 +0000547/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
548/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
549/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
550/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000551/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
552/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000553/// [C++] 'this' [C++ 9.3.2]
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000554/// [G++] unary-type-trait '(' type-id ')'
555/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley6242b6a2011-04-28 00:16:57 +0000556/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff0ac012832008-08-28 19:20:44 +0000557/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000558///
559/// constant: [C99 6.4.4]
560/// integer-constant
561/// floating-constant
562/// enumeration-constant -> identifier
563/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000564///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000565/// id-expression: [C++ 5.1]
566/// unqualified-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000567/// qualified-id
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000568///
569/// unqualified-id: [C++ 5.1]
570/// identifier
571/// operator-function-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000572/// conversion-function-id
573/// '~' class-name
574/// template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000575///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000576/// new-expression: [C++ 5.3.4]
577/// '::'[opt] 'new' new-placement[opt] new-type-id
578/// new-initializer[opt]
579/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
580/// new-initializer[opt]
581///
582/// delete-expression: [C++ 5.3.5]
583/// '::'[opt] 'delete' cast-expression
584/// '::'[opt] 'delete' '[' ']' cast-expression
585///
John Wiegley65497cc2011-04-27 23:09:49 +0000586/// [GNU/Embarcadero] unary-type-trait:
587/// '__is_arithmetic'
588/// '__is_floating_point'
589/// '__is_integral'
590/// '__is_lvalue_expr'
591/// '__is_rvalue_expr'
592/// '__is_complete_type'
593/// '__is_void'
594/// '__is_array'
595/// '__is_function'
596/// '__is_reference'
597/// '__is_lvalue_reference'
598/// '__is_rvalue_reference'
599/// '__is_fundamental'
600/// '__is_object'
601/// '__is_scalar'
602/// '__is_compound'
603/// '__is_pointer'
604/// '__is_member_object_pointer'
605/// '__is_member_function_pointer'
606/// '__is_member_pointer'
607/// '__is_const'
608/// '__is_volatile'
609/// '__is_trivial'
610/// '__is_standard_layout'
611/// '__is_signed'
612/// '__is_unsigned'
613///
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000614/// [GNU] unary-type-trait:
Sebastian Redl7dcb1552010-08-31 04:59:00 +0000615/// '__has_nothrow_assign'
616/// '__has_nothrow_copy'
617/// '__has_nothrow_constructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000618/// '__has_trivial_assign' [TODO]
619/// '__has_trivial_copy' [TODO]
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000620/// '__has_trivial_constructor'
Anders Carlsson6dc35752009-04-17 02:34:54 +0000621/// '__has_trivial_destructor'
Sebastian Redlb469afb2010-09-02 23:19:42 +0000622/// '__has_virtual_destructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000623/// '__is_abstract' [TODO]
624/// '__is_class'
625/// '__is_empty' [TODO]
626/// '__is_enum'
Douglas Gregordca70af2011-12-03 18:14:24 +0000627/// '__is_final'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000628/// '__is_pod'
629/// '__is_polymorphic'
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000630/// '__is_trivial'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000631/// '__is_union'
632///
Alexis Huntd9a5cc12011-05-13 00:31:07 +0000633/// [Clang] unary-type-trait:
634/// '__trivially_copyable'
635///
Douglas Gregor8006e762011-01-27 20:28:01 +0000636/// binary-type-trait:
637/// [GNU] '__is_base_of'
638/// [MS] '__is_convertible_to'
John Wiegley65497cc2011-04-27 23:09:49 +0000639/// '__is_convertible'
640/// '__is_same'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000641///
John Wiegley6242b6a2011-04-28 00:16:57 +0000642/// [Embarcadero] array-type-trait:
643/// '__array_rank'
644/// '__array_extent'
645///
John Wiegleyf9f65842011-04-25 06:54:41 +0000646/// [Embarcadero] expression-trait:
647/// '__is_lvalue_expr'
648/// '__is_rvalue_expr'
James Dennett3d5e4592012-06-17 04:36:28 +0000649/// \endverbatim
John Wiegleyf9f65842011-04-25 06:54:41 +0000650///
John McCalldadc5752010-08-24 06:29:42 +0000651ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl22e3a932010-09-10 20:55:37 +0000652 bool isAddressOfOperand,
653 bool &NotCastExpr,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000654 TypeCastState isTypeCast) {
John McCalldadc5752010-08-24 06:29:42 +0000655 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000656 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000657 NotCastExpr = false;
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattner81b576e2006-08-11 02:13:20 +0000659 // This handles all of cast-expression, unary-expression, postfix-expression,
660 // and primary-expression. We handle them together like this for efficiency
661 // and to simplify handling of an expression starting with a '(' token: which
662 // may be one of a parenthesized expression, cast-expression, compound literal
663 // expression, or statement expression.
664 //
665 // If the parsed tokens consist of a primary-expression, the cases below
John McCallb268a282010-08-23 23:25:46 +0000666 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
667 // to handle the postfix expression suffixes. Cases that cannot be followed
668 // by postfix exprs should return without invoking
669 // ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000670 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000671 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000672 // If this expression is limited to being a unary-expression, the parent can
673 // not start a cast expression.
674 ParenParseOption ParenExprType =
David Blaikiebbafb8a2012-03-11 07:00:24 +0000675 (isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallba7bf592010-08-24 05:47:05 +0000676 ParsedType CastTy;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000677 SourceLocation RParenLoc;
Chris Lattner3c674cf2009-12-10 02:08:07 +0000678
679 {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000680 // The inside of the parens don't need to be a colon protected scope, and
681 // isn't immediately a message send.
Chris Lattner3c674cf2009-12-10 02:08:07 +0000682 ColonProtectionRAIIObject X(*this, false);
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000683
Chris Lattner3c674cf2009-12-10 02:08:07 +0000684 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000685 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner3c674cf2009-12-10 02:08:07 +0000686 }
Mike Stump11289f42009-09-09 15:08:12 +0000687
Chris Lattner81b576e2006-08-11 02:13:20 +0000688 switch (ParenExprType) {
689 case SimpleExpr: break; // Nothing else to do.
690 case CompoundStmt: break; // Nothing else to do.
691 case CompoundLiteral:
692 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
693 // postfix-expression exist, parse them now.
694 break;
695 case CastExpr:
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000696 // We have parsed the cast-expression and no postfix-expr pieces are
697 // following.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000698 return move(Res);
Chris Lattner81b576e2006-08-11 02:13:20 +0000699 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000700
John McCallb268a282010-08-23 23:25:46 +0000701 break;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000702 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000703
Chris Lattner52a99e52006-08-10 20:56:00 +0000704 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000705 case tok::numeric_constant:
706 // constant: integer-constant
707 // constant: floating-constant
Sebastian Redl59b5e512008-12-11 21:36:32 +0000708
Richard Smithbcc22fc2012-03-09 08:00:36 +0000709 Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000710 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000711 break;
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000712
Bill Wendling4073ed52007-02-13 01:51:42 +0000713 case tok::kw_true:
714 case tok::kw_false:
Sebastian Redld65cea82008-12-11 22:51:44 +0000715 return ParseCXXBoolLiteral();
Ted Kremeneke65b0862012-03-06 20:05:56 +0000716
717 case tok::kw___objc_yes:
718 case tok::kw___objc_no:
719 return ParseObjCBoolLiteral();
Bill Wendling4073ed52007-02-13 01:51:42 +0000720
Sebastian Redl576fd422009-05-10 18:38:11 +0000721 case tok::kw_nullptr:
Richard Smithb15c11c2011-10-17 23:06:20 +0000722 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl576fd422009-05-10 18:38:11 +0000723 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
724
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000725 case tok::annot_primary_expr:
726 assert(Res.get() == 0 && "Stray primary-expression annotation?");
727 Res = getExprAnnotation(Tok);
728 ConsumeToken();
729 break;
730
David Blaikie15a430a2011-12-04 05:04:18 +0000731 case tok::kw_decltype:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000732 case tok::identifier: { // primary-expression: identifier
733 // unqualified-id: identifier
734 // constant: enumeration-constant
Chris Lattnera8a3f732009-01-06 05:06:21 +0000735 // Turn a potentially qualified name into a annot_typename or
Chris Lattner122db262009-01-04 22:52:14 +0000736 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000737 if (getLangOpts().CPlusPlus) {
John McCall64fe2332010-01-07 19:29:58 +0000738 // Avoid the unnecessary parse-time lookup in the common case
739 // where the syntax forbids a type.
740 const Token &Next = NextToken();
741 if (Next.is(tok::coloncolon) ||
742 (!ColonIsSacred && Next.is(tok::colon)) ||
743 Next.is(tok::less) ||
Sebastian Redl867f2282011-12-22 18:58:29 +0000744 Next.is(tok::l_paren) ||
745 Next.is(tok::l_brace)) {
John McCall64fe2332010-01-07 19:29:58 +0000746 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
747 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +0000748 return ExprError();
749 if (!Tok.is(tok::identifier))
John McCall64fe2332010-01-07 19:29:58 +0000750 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
751 }
Chris Lattner9a8968b2009-01-04 23:23:14 +0000752 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000753
Chris Lattner55662902009-10-25 17:04:48 +0000754 // Consume the identifier so that we can see if it is followed by a '(' or
755 // '.'.
756 IdentifierInfo &II = *Tok.getIdentifierInfo();
757 SourceLocation ILoc = ConsumeToken();
758
Chris Lattnera36ec422010-04-11 08:28:14 +0000759 // Support 'Class.property' and 'super.property' notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000760 if (getLangOpts().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000761 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattnercd963182010-04-12 06:20:33 +0000762 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000763 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000764 ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000765
Douglas Gregor36107ad2012-02-16 18:19:22 +0000766 // Allow either an identifier or the keyword 'class' (in C++).
767 if (Tok.isNot(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000768 !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattner55662902009-10-25 17:04:48 +0000769 Diag(Tok, diag::err_expected_property_name);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000770 return ExprError();
771 }
772 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
773 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000774
775 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
776 ILoc, PropertyLoc);
John McCallb268a282010-08-23 23:25:46 +0000777 break;
Steve Naroff9527bbf2009-03-09 21:12:44 +0000778 }
John McCall8d08b9b2010-08-27 09:08:28 +0000779
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000780 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregored0b69d2010-09-15 16:23:04 +0000781 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000782 // that identifier, this is probably a message send with a missing open
Douglas Gregored0b69d2010-09-15 16:23:04 +0000783 // bracket. Treat it as such.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000784 if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000785 getCurScope()->isInObjcMethodScope() &&
Douglas Gregored0b69d2010-09-15 16:23:04 +0000786 ((Tok.is(tok::identifier) &&
787 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
788 Tok.is(tok::code_completion))) {
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000789 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
790 0);
791 break;
792 }
793
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000794 // If we have an Objective-C class name followed by an identifier
795 // and either ':' or ']', this is an Objective-C class message
796 // send that's missing the opening '['. Recovery
797 // appropriately. Also take this path if we're performing code
798 // completion after an Objective-C class name.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000799 if (getLangOpts().ObjC1 &&
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000800 ((Tok.is(tok::identifier) && !InMessageExpression) ||
801 Tok.is(tok::code_completion))) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000802 const Token& Next = NextToken();
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000803 if (Tok.is(tok::code_completion) ||
804 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif433c9e12010-09-17 10:21:45 +0000805 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
806 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000807 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000808 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000809 DS.SetRangeStart(ILoc);
810 DS.SetRangeEnd(ILoc);
811 const char *PrevSpec = 0;
812 unsigned DiagID;
Gabor Greif433c9e12010-09-17 10:21:45 +0000813 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000814
815 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
816 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
817 DeclaratorInfo);
818 if (Ty.isInvalid())
819 break;
820
821 Res = ParseObjCMessageExpressionBody(SourceLocation(),
822 SourceLocation(),
823 Ty.get(), 0);
824 break;
825 }
826 }
827
John McCall8d08b9b2010-08-27 09:08:28 +0000828 // Make sure to pass down the right value for isAddressOfOperand.
829 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
830 isAddressOfOperand = false;
Chris Lattner55662902009-10-25 17:04:48 +0000831
Chris Lattnerac18be92006-11-20 06:49:47 +0000832 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
833 // need to know whether or not this identifier is a function designator or
834 // not.
Douglas Gregora121b752009-11-03 16:56:39 +0000835 UnqualifiedId Name;
836 CXXScopeSpec ScopeSpec;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000837 SourceLocation TemplateKWLoc;
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000838 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
839 isTypeCast != IsTypeCast);
Douglas Gregora121b752009-11-03 16:56:39 +0000840 Name.setIdentifier(&II, ILoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000841 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
842 Name, Tok.is(tok::l_paren),
843 isAddressOfOperand, &Validator);
John McCallb268a282010-08-23 23:25:46 +0000844 break;
Chris Lattnerac18be92006-11-20 06:49:47 +0000845 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000846 case tok::char_constant: // constant: character-constant
Douglas Gregorfb65e592011-07-27 05:40:30 +0000847 case tok::wide_char_constant:
848 case tok::utf16_char_constant:
849 case tok::utf32_char_constant:
Richard Smithbcc22fc2012-03-09 08:00:36 +0000850 Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope());
Steve Naroffae4143e2007-04-26 20:39:23 +0000851 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000852 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000853 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
854 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
855 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000856 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000857 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000858 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000859 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000860 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000861 case tok::utf8_string_literal:
862 case tok::utf16_string_literal:
863 case tok::utf32_string_literal:
Richard Smithd67aea22012-03-06 03:21:47 +0000864 Res = ParseStringLiteralExpression(true);
John McCallb268a282010-08-23 23:25:46 +0000865 break;
Benjamin Kramere56f3932011-12-23 17:00:35 +0000866 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbourne91147592011-04-15 00:35:48 +0000867 Res = ParseGenericSelectionExpression();
868 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000869 case tok::kw___builtin_va_arg:
870 case tok::kw___builtin_offsetof:
871 case tok::kw___builtin_choose_expr:
Tanya Lattner55808c12011-06-04 00:47:47 +0000872 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redl90893182008-12-11 22:33:27 +0000873 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000874 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000875 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth904cb142011-07-08 04:59:44 +0000876
Douglas Gregora49ccfe2010-08-06 14:50:36 +0000877 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
878 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
879 // C++ [expr.unary] has:
880 // unary-expression:
881 // ++ cast-expression
882 // -- cast-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000883 SourceLocation SavedLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000884 Res = ParseCastExpression(!getLangOpts().CPlusPlus);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000885 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000886 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000887 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000888 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000889 case tok::amp: { // unary-expression: '&' cast-expression
890 // Special treatment because of member pointers
891 SourceLocation SavedLoc = ConsumeToken();
892 Res = ParseCastExpression(false, true);
893 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000894 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000895 return move(Res);
896 }
897
Chris Lattner81b576e2006-08-11 02:13:20 +0000898 case tok::star: // unary-expression: '*' cast-expression
899 case tok::plus: // unary-expression: '+' cast-expression
900 case tok::minus: // unary-expression: '-' cast-expression
901 case tok::tilde: // unary-expression: '~' cast-expression
902 case tok::exclaim: // unary-expression: '!' cast-expression
903 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000904 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000905 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000906 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000907 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000908 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000909 return move(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000910 }
911
Chris Lattnerc43926f2008-02-02 20:20:10 +0000912 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
913 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000914 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000915 SourceLocation SavedLoc = ConsumeToken();
916 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000917 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000918 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000919 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000920 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000921 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
922 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000923 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000924 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
925 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000926 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournee190dee2011-03-11 19:24:49 +0000927 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
928 return ParseUnaryExprOrTypeTraitExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000929 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000930 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000931 if (Tok.isNot(tok::identifier))
932 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000933
Chris Lattner9ba479b2011-02-18 21:16:39 +0000934 if (getCurScope()->getFnParent() == 0)
935 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
936
Chris Lattnereefa10e2007-05-28 06:56:27 +0000937 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000938 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
939 Tok.getLocation());
940 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Chris Lattner14a1b642006-10-15 22:33:58 +0000941 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000942 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000943 }
Chris Lattner29375652006-12-04 18:06:35 +0000944 case tok::kw_const_cast:
945 case tok::kw_dynamic_cast:
946 case tok::kw_reinterpret_cast:
947 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000948 Res = ParseCXXCasts();
John McCallb268a282010-08-23 23:25:46 +0000949 break;
Sebastian Redlc4704762008-11-11 11:37:55 +0000950 case tok::kw_typeid:
951 Res = ParseCXXTypeid();
John McCallb268a282010-08-23 23:25:46 +0000952 break;
Francois Pichet9f4f2072010-09-08 12:20:18 +0000953 case tok::kw___uuidof:
954 Res = ParseCXXUuidof();
955 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000956 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000957 Res = ParseCXXThis();
John McCallb268a282010-08-23 23:25:46 +0000958 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000959
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000960 case tok::annot_typename:
961 if (isStartOfObjCClassMessageMissingOpenBracket()) {
962 ParsedType Type = getTypeAnnotation(Tok);
963
964 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000965 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000966 DS.SetRangeStart(Tok.getLocation());
967 DS.SetRangeEnd(Tok.getLastLoc());
968
969 const char *PrevSpec = 0;
970 unsigned DiagID;
Nico Weber77430342010-11-22 10:30:56 +0000971 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
972 PrevSpec, DiagID, Type);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000973
974 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
975 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
976 if (Ty.isInvalid())
977 break;
978
979 ConsumeToken();
980 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
981 Ty.get(), 0);
982 break;
983 }
984 // Fall through
985
David Blaikie25896afb2012-01-24 05:47:35 +0000986 case tok::annot_decltype:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000987 case tok::kw_char:
988 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000989 case tok::kw_char16_t:
990 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000991 case tok::kw_bool:
992 case tok::kw_short:
993 case tok::kw_int:
994 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +0000995 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000996 case tok::kw___int128:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000997 case tok::kw_signed:
998 case tok::kw_unsigned:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000999 case tok::kw_half:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001000 case tok::kw_float:
1001 case tok::kw_double:
1002 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +00001003 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +00001004 case tok::kw_typeof:
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001005 case tok::kw___vector: {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001006 if (!getLangOpts().CPlusPlus) {
Chris Lattner8a38aa82009-01-04 22:28:21 +00001007 Diag(Tok, diag::err_expected_expression);
1008 return ExprError();
1009 }
Eli Friedman6d692cc2009-06-11 00:33:41 +00001010
1011 if (SavedKind == tok::kw_typename) {
1012 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001013 // typename-specifier braced-init-list
John McCall1f476a12010-02-26 08:45:28 +00001014 if (TryAnnotateTypeOrScopeToken())
Eli Friedman6d692cc2009-06-11 00:33:41 +00001015 return ExprError();
1016 }
1017
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001018 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001019 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001020 //
John McCall084e83d2011-03-24 11:26:52 +00001021 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001022 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redl3da34892011-06-05 12:23:16 +00001023 if (Tok.isNot(tok::l_paren) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001024 (!getLangOpts().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl59b5e512008-12-11 21:36:32 +00001025 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1026 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001027
Richard Smith5d164bc2011-10-15 05:09:34 +00001028 if (Tok.is(tok::l_brace))
1029 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1030
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001031 Res = ParseCXXTypeConstructExpression(DS);
John McCallb268a282010-08-23 23:25:46 +00001032 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001033 }
1034
Douglas Gregor7df89f52010-02-05 19:11:37 +00001035 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001036 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1037 // (We can end up in this situation after tentative parsing.)
1038 if (TryAnnotateTypeOrScopeToken())
1039 return ExprError();
1040 if (!Tok.is(tok::annot_cxxscope))
1041 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001042 NotCastExpr, isTypeCast);
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001043
Douglas Gregor7df89f52010-02-05 19:11:37 +00001044 Token Next = NextToken();
1045 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001046 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001047 if (TemplateId->Kind == TNK_Type_template) {
1048 // We have a qualified template-id that we know refers to a
1049 // type, translate it into a type and continue parsing as a
1050 // cast expression.
1051 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001052 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1053 /*EnteringContext=*/false);
Douglas Gregore7c20652011-03-02 00:47:37 +00001054 AnnotateTemplateIdTokenAsType();
Douglas Gregor7df89f52010-02-05 19:11:37 +00001055 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001056 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001057 }
1058 }
1059
1060 // Parse as an id-expression.
1061 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001062 break;
Douglas Gregor7df89f52010-02-05 19:11:37 +00001063 }
1064
1065 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001066 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001067 if (TemplateId->Kind == TNK_Type_template) {
1068 // We have a template-id that we know refers to a type,
1069 // translate it into a type and continue parsing as a cast
1070 // expression.
1071 AnnotateTemplateIdTokenAsType();
1072 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001073 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001074 }
1075
1076 // Fall through to treat the template-id as an id-expression.
1077 }
1078
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001079 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001080 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001081 break;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001082
Chris Lattner122db262009-01-04 22:52:14 +00001083 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001084 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1085 // annotates the token, tail recurse.
1086 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001087 return ExprError();
1088 if (!Tok.is(tok::coloncolon))
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001089 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1090
Chris Lattner122db262009-01-04 22:52:14 +00001091 // ::new -> [C++] new-expression
1092 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001093 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +00001094 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001095 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +00001096 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001097 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001098
Chris Lattner9a8968b2009-01-04 23:23:14 +00001099 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001100 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +00001101 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +00001102 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +00001103
Sebastian Redlbd150f42008-11-21 19:14:01 +00001104 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001105 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001106
1107 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001108 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001109
Sebastian Redl22e3a932010-09-10 20:55:37 +00001110 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smithb15c11c2011-10-17 23:06:20 +00001111 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001112 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001113 BalancedDelimiterTracker T(*this, tok::l_paren);
1114
1115 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl22e3a932010-09-10 20:55:37 +00001116 return ExprError();
Richard Smith764d2fe2011-12-20 02:08:33 +00001117 // C++11 [expr.unary.noexcept]p1:
Sebastian Redle56be2f72010-09-10 21:57:27 +00001118 // The noexcept operator determines whether the evaluation of its operand,
1119 // which is an unevaluated operand, can throw an exception.
1120 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001121 ExprResult Result = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001122
1123 T.consumeClose();
1124
Sebastian Redl22e3a932010-09-10 20:55:37 +00001125 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001126 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1127 Result.take(), T.getCloseLocation());
Sebastian Redl22e3a932010-09-10 20:55:37 +00001128 return move(Result);
1129 }
1130
Chandler Carruth79803482011-04-23 10:47:20 +00001131 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001132 case tok::kw___is_class:
Eli Friedmanc96d4962009-08-15 21:55:26 +00001133 case tok::kw___is_empty:
Chandler Carruth79803482011-04-23 10:47:20 +00001134 case tok::kw___is_enum:
Sebastian Redl79eba1c2009-12-03 00:13:20 +00001135 case tok::kw___is_literal:
John Wiegley65497cc2011-04-27 23:09:49 +00001136 case tok::kw___is_arithmetic:
1137 case tok::kw___is_integral:
1138 case tok::kw___is_floating_point:
1139 case tok::kw___is_complete_type:
1140 case tok::kw___is_void:
1141 case tok::kw___is_array:
1142 case tok::kw___is_function:
1143 case tok::kw___is_reference:
1144 case tok::kw___is_lvalue_reference:
1145 case tok::kw___is_rvalue_reference:
1146 case tok::kw___is_fundamental:
1147 case tok::kw___is_object:
1148 case tok::kw___is_scalar:
1149 case tok::kw___is_compound:
1150 case tok::kw___is_pointer:
1151 case tok::kw___is_member_object_pointer:
1152 case tok::kw___is_member_function_pointer:
1153 case tok::kw___is_member_pointer:
1154 case tok::kw___is_const:
1155 case tok::kw___is_volatile:
1156 case tok::kw___is_standard_layout:
1157 case tok::kw___is_signed:
1158 case tok::kw___is_unsigned:
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00001159 case tok::kw___is_literal_type:
Chandler Carruth79803482011-04-23 10:47:20 +00001160 case tok::kw___is_pod:
1161 case tok::kw___is_polymorphic:
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +00001162 case tok::kw___is_trivial:
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001163 case tok::kw___is_trivially_copyable:
Chandler Carruth79803482011-04-23 10:47:20 +00001164 case tok::kw___is_union:
Douglas Gregordca70af2011-12-03 18:14:24 +00001165 case tok::kw___is_final:
Anders Carlssonfe63dc52009-04-16 00:08:20 +00001166 case tok::kw___has_trivial_constructor:
Douglas Gregor79f83ed2009-07-23 23:49:00 +00001167 case tok::kw___has_trivial_copy:
1168 case tok::kw___has_trivial_assign:
Anders Carlsson6dc35752009-04-17 02:34:54 +00001169 case tok::kw___has_trivial_destructor:
Sebastian Redl7dcb1552010-08-31 04:59:00 +00001170 case tok::kw___has_nothrow_assign:
1171 case tok::kw___has_nothrow_copy:
1172 case tok::kw___has_nothrow_constructor:
Sebastian Redlb469afb2010-09-02 23:19:42 +00001173 case tok::kw___has_virtual_destructor:
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001174 return ParseUnaryTypeTrait();
1175
Francois Pichet34b21132010-12-08 22:35:30 +00001176 case tok::kw___builtin_types_compatible_p:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001177 case tok::kw___is_base_of:
John Wiegley65497cc2011-04-27 23:09:49 +00001178 case tok::kw___is_same:
1179 case tok::kw___is_convertible:
Douglas Gregor8006e762011-01-27 20:28:01 +00001180 case tok::kw___is_convertible_to:
Douglas Gregor1be329d2012-02-23 07:33:15 +00001181 case tok::kw___is_trivially_assignable:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001182 return ParseBinaryTypeTrait();
1183
Douglas Gregor29c42f22012-02-24 07:38:34 +00001184 case tok::kw___is_trivially_constructible:
1185 return ParseTypeTrait();
1186
John Wiegley6242b6a2011-04-28 00:16:57 +00001187 case tok::kw___array_rank:
1188 case tok::kw___array_extent:
1189 return ParseArrayTypeTrait();
1190
John Wiegleyf9f65842011-04-25 06:54:41 +00001191 case tok::kw___is_lvalue_expr:
1192 case tok::kw___is_rvalue_expr:
1193 return ParseExpressionTrait();
1194
Chris Lattner644e1b72007-10-03 22:03:06 +00001195 case tok::at: {
1196 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001197 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +00001198 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001199 case tok::caret:
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001200 Res = ParseBlockLiteralExpression();
1201 break;
1202 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001203 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001204 cutOffParsing();
1205 return ExprError();
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001206 }
Chris Lattner6bf1db12008-12-12 19:20:14 +00001207 case tok::l_square:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001208 if (getLangOpts().CPlusPlus0x) {
1209 if (getLangOpts().ObjC1) {
Eli Friedmanc7c97142012-01-04 02:40:39 +00001210 // C++11 lambda expressions and Objective-C message sends both start with a
1211 // square bracket. There are three possibilities here:
1212 // we have a valid lambda expression, we have an invalid lambda
1213 // expression, or we have something that doesn't appear to be a lambda.
1214 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001215 Res = TryParseLambdaExpression();
Eli Friedmanc7c97142012-01-04 02:40:39 +00001216 if (!Res.isInvalid() && !Res.get())
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001217 Res = ParseObjCMessageExpression();
1218 break;
1219 }
1220 Res = ParseLambdaExpression();
1221 break;
1222 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001223 if (getLangOpts().ObjC1) {
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001224 Res = ParseObjCMessageExpression();
1225 break;
1226 }
1227 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +00001228 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001229 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001230 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +00001231 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001232
John McCallb268a282010-08-23 23:25:46 +00001233 // These can be followed by postfix-expr pieces.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001234 return ParsePostfixExpressionSuffix(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +00001235}
1236
James Dennett3d5e4592012-06-17 04:36:28 +00001237/// \brief Once the leading part of a postfix-expression is parsed, this
1238/// method parses any suffixes that apply.
Chris Lattner20c6a452006-08-12 17:40:43 +00001239///
James Dennett3d5e4592012-06-17 04:36:28 +00001240/// \verbatim
Chris Lattner20c6a452006-08-12 17:40:43 +00001241/// postfix-expression: [C99 6.5.2]
1242/// primary-expression
1243/// postfix-expression '[' expression ']'
Sebastian Redl3da34892011-06-05 12:23:16 +00001244/// postfix-expression '[' braced-init-list ']'
Chris Lattner20c6a452006-08-12 17:40:43 +00001245/// postfix-expression '(' argument-expression-list[opt] ')'
1246/// postfix-expression '.' identifier
1247/// postfix-expression '->' identifier
1248/// postfix-expression '++'
1249/// postfix-expression '--'
1250/// '(' type-name ')' '{' initializer-list '}'
1251/// '(' type-name ')' '{' initializer-list ',' '}'
1252///
1253/// argument-expression-list: [C99 6.5.2]
Douglas Gregor968f23a2011-01-03 19:31:53 +00001254/// argument-expression ...[opt]
1255/// argument-expression-list ',' assignment-expression ...[opt]
James Dennett3d5e4592012-06-17 04:36:28 +00001256/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001257ExprResult
1258Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001259 // Now that the primary-expression piece of the postfix-expression has been
1260 // parsed, see if there are any postfix-expression pieces here.
1261 SourceLocation Loc;
1262 while (1) {
1263 switch (Tok.getKind()) {
Douglas Gregored0b69d2010-09-15 16:23:04 +00001264 case tok::code_completion:
1265 if (InMessageExpression)
1266 return move(LHS);
1267
Douglas Gregoreda7e542010-09-18 01:28:11 +00001268 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001269 cutOffParsing();
1270 return ExprError();
Douglas Gregored0b69d2010-09-15 16:23:04 +00001271
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001272 case tok::identifier:
1273 // If we see identifier: after an expression, and we're not already in a
1274 // message send, then this is probably a message send with a missing
1275 // opening bracket '['.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001276 if (getLangOpts().ObjC1 && !InMessageExpression &&
Douglas Gregord6d98002010-09-15 14:54:45 +00001277 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001278 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1279 ParsedType(), LHS.get());
1280 break;
1281 }
1282
1283 // Fall through; this isn't a message send.
1284
Chris Lattner20c6a452006-08-12 17:40:43 +00001285 default: // Not a postfix-expression suffix.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001286 return move(LHS);
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001287 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner47054fb2010-05-31 18:18:22 +00001288 // If we have a array postfix expression that starts on a new line and
1289 // Objective-C is enabled, it is highly likely that the user forgot a
1290 // semicolon after the base expression and that the array postfix-expr is
1291 // actually another message send. In this case, do some look-ahead to see
1292 // if the contents of the square brackets are obviously not a valid
1293 // expression and recover by pretending there is no suffix.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001294 if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
Chris Lattner47054fb2010-05-31 18:18:22 +00001295 isSimpleObjCMessageExpression())
Douglas Gregor990ccac2010-05-31 14:40:22 +00001296 return move(LHS);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001297
1298 // Reject array indices starting with a lambda-expression. '[[' is
1299 // reserved for attributes.
1300 if (CheckProhibitedCXX11Attribute())
1301 return ExprError();
1302
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001303 BalancedDelimiterTracker T(*this, tok::l_square);
1304 T.consumeOpen();
1305 Loc = T.getOpenLocation();
Sebastian Redl3da34892011-06-05 12:23:16 +00001306 ExprResult Idx;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001307 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001308 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00001309 Idx = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001310 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00001311 Idx = ParseExpression();
Sebastian Redl511ed552008-11-25 22:21:31 +00001312
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001313 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001314
1315 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCallb268a282010-08-23 23:25:46 +00001316 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1317 Idx.take(), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001318 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +00001319 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001320
Chris Lattner89c50c62006-08-11 06:41:18 +00001321 // Match the ']'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001322 T.consumeClose();
Chris Lattner89c50c62006-08-11 06:41:18 +00001323 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001324 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001325
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001326 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1327 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1328 // '(' argument-expression-list[opt] ')'
1329 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001330 InMessageExpressionRAIIObject InMessage(*this, false);
1331
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001332 Expr *ExecConfig = 0;
1333
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001334 BalancedDelimiterTracker PT(*this, tok::l_paren);
1335
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001336 if (OpKind == tok::lesslessless) {
1337 ExprVector ExecConfigExprs(Actions);
1338 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001339 SourceLocation OpenLoc = ConsumeToken();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001340
1341 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1342 LHS = ExprError();
1343 }
1344
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001345 SourceLocation CloseLoc = Tok.getLocation();
1346 if (Tok.is(tok::greatergreatergreater)) {
1347 ConsumeToken();
1348 } else if (LHS.isInvalid()) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001349 SkipUntil(tok::greatergreatergreater);
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001350 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001351 // There was an error closing the brackets
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001352 Diag(Tok, diag::err_expected_ggg);
1353 Diag(OpenLoc, diag::note_matching) << "<<<";
1354 SkipUntil(tok::greatergreatergreater);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001355 LHS = ExprError();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001356 }
1357
1358 if (!LHS.isInvalid()) {
1359 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1360 LHS = ExprError();
1361 else
1362 Loc = PrevTokLocation;
1363 }
1364
1365 if (!LHS.isInvalid()) {
1366 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001367 OpenLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001368 move_arg(ExecConfigExprs),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001369 CloseLoc);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001370 if (ECResult.isInvalid())
1371 LHS = ExprError();
1372 else
1373 ExecConfig = ECResult.get();
1374 }
1375 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001376 PT.consumeOpen();
1377 Loc = PT.getOpenLocation();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001378 }
1379
Sebastian Redl511ed552008-11-25 22:21:31 +00001380 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001381 CommaLocsTy CommaLocs;
Douglas Gregoreda7e542010-09-18 01:28:11 +00001382
Douglas Gregorcabea402009-09-22 15:41:20 +00001383 if (Tok.is(tok::code_completion)) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00001384 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1385 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001386 cutOffParsing();
1387 return ExprError();
Douglas Gregorcabea402009-09-22 15:41:20 +00001388 }
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001389
1390 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1391 if (Tok.isNot(tok::r_paren)) {
1392 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1393 LHS.get())) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001394 LHS = ExprError();
1395 }
Chris Lattner0c6c0342006-08-12 18:12:45 +00001396 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001397 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001398
Chris Lattner89c50c62006-08-11 06:41:18 +00001399 // Match the ')'.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001400 if (LHS.isInvalid()) {
1401 SkipUntil(tok::r_paren);
1402 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001403 PT.consumeClose();
Douglas Gregoreda7e542010-09-18 01:28:11 +00001404 LHS = ExprError();
1405 } else {
1406 assert((ArgExprs.size() == 0 ||
1407 ArgExprs.size()-1 == CommaLocs.size())&&
Chris Lattnere165d942006-08-24 04:40:38 +00001408 "Unexpected number of commas!");
John McCallb268a282010-08-23 23:25:46 +00001409 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001410 move_arg(ArgExprs), Tok.getLocation(),
1411 ExecConfig);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001412 PT.consumeClose();
Chris Lattnere165d942006-08-24 04:40:38 +00001413 }
Mike Stump11289f42009-09-09 15:08:12 +00001414
Chris Lattner89c50c62006-08-11 06:41:18 +00001415 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001416 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001417 case tok::arrow:
1418 case tok::period: {
1419 // postfix-expression: p-e '->' template[opt] id-expression
1420 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001421 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001422 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001423
Douglas Gregord8061562009-08-06 03:17:00 +00001424 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001425 ParsedType ObjectType;
Douglas Gregore610ada2010-02-24 18:44:31 +00001426 bool MayBePseudoDestructor = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001427 if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001428 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregore610ada2010-02-24 18:44:31 +00001429 OpLoc, OpKind, ObjectType,
1430 MayBePseudoDestructor);
Douglas Gregord8061562009-08-06 03:17:00 +00001431 if (LHS.isInvalid())
1432 break;
Douglas Gregore610ada2010-02-24 18:44:31 +00001433
Douglas Gregordf593fb2011-11-07 17:33:42 +00001434 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1435 /*EnteringContext=*/false,
Douglas Gregore610ada2010-02-24 18:44:31 +00001436 &MayBePseudoDestructor);
Douglas Gregor205a3612010-05-27 15:25:59 +00001437 if (SS.isNotEmpty())
John McCallba7bf592010-08-24 05:47:05 +00001438 ObjectType = ParsedType();
Douglas Gregord8061562009-08-06 03:17:00 +00001439 }
1440
Douglas Gregor2436e712009-09-17 21:32:03 +00001441 if (Tok.is(tok::code_completion)) {
1442 // Code completion for a member access expression.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001443 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor2436e712009-09-17 21:32:03 +00001444 OpLoc, OpKind == tok::arrow);
1445
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001446 cutOffParsing();
1447 return ExprError();
Douglas Gregor2436e712009-09-17 21:32:03 +00001448 }
1449
John McCallb268a282010-08-23 23:25:46 +00001450 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1451 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregore610ada2010-02-24 18:44:31 +00001452 ObjectType);
1453 break;
1454 }
1455
1456 // Either the action has told is that this cannot be a
1457 // pseudo-destructor expression (based on the type of base
1458 // expression), or we didn't see a '~' in the right place. We
1459 // can still parse a destructor name here, but in that case it
1460 // names a real destructor.
Francois Pichet64225792011-01-18 05:04:39 +00001461 // Allow explicit constructor calls in Microsoft mode.
1462 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001463 SourceLocation TemplateKWLoc;
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001464 UnqualifiedId Name;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001465 if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
Douglas Gregor36107ad2012-02-16 18:19:22 +00001466 // Objective-C++:
1467 // After a '.' in a member access expression, treat the keyword
1468 // 'class' as if it were an identifier.
1469 //
1470 // This hack allows property access to the 'class' method because it is
1471 // such a common method name. For other C++ keywords that are
1472 // Objective-C method names, one must use the message send syntax.
1473 IdentifierInfo *Id = Tok.getIdentifierInfo();
1474 SourceLocation Loc = ConsumeToken();
1475 Name.setIdentifier(Id, Loc);
1476 } else if (ParseUnqualifiedId(SS,
1477 /*EnteringContext=*/false,
1478 /*AllowDestructorName=*/true,
1479 /*AllowConstructorName=*/
David Blaikiebbafb8a2012-03-11 07:00:24 +00001480 getLangOpts().MicrosoftExt,
Douglas Gregor36107ad2012-02-16 18:19:22 +00001481 ObjectType, TemplateKWLoc, Name))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001482 LHS = ExprError();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001483
1484 if (!LHS.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001485 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001486 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001487 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1488 Tok.is(tok::l_paren));
Chris Lattner89c50c62006-08-11 06:41:18 +00001489 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001490 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001491 case tok::plusplus: // postfix-expression: postfix-expression '++'
1492 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001493 if (!LHS.isInvalid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001494 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCallb268a282010-08-23 23:25:46 +00001495 Tok.getKind(), LHS.take());
Sebastian Redl511ed552008-11-25 22:21:31 +00001496 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001497 ConsumeToken();
1498 break;
Chris Lattnerf8339772006-08-10 22:01:51 +00001499 }
1500 }
Chris Lattner52a99e52006-08-10 20:56:00 +00001501}
1502
Peter Collingbournee190dee2011-03-11 19:24:49 +00001503/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1504/// vec_step and we are at the start of an expression or a parenthesized
1505/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1506/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001507///
James Dennett3d5e4592012-06-17 04:36:28 +00001508/// \verbatim
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001509/// unary-expression: [C99 6.5.3]
1510/// 'sizeof' unary-expression
1511/// 'sizeof' '(' type-name ')'
1512/// [GNU] '__alignof' unary-expression
1513/// [GNU] '__alignof' '(' type-name ')'
1514/// [C++0x] 'alignof' '(' type-id ')'
1515///
1516/// [GNU] typeof-specifier:
1517/// typeof ( expressions )
1518/// typeof ( type-name )
1519/// [GNU/C++] typeof unary-expression
1520///
Peter Collingbournee190dee2011-03-11 19:24:49 +00001521/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1522/// vec_step ( expressions )
1523/// vec_step ( type-name )
James Dennett3d5e4592012-06-17 04:36:28 +00001524/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001525ExprResult
Peter Collingbournee190dee2011-03-11 19:24:49 +00001526Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1527 bool &isCastExpr,
1528 ParsedType &CastTy,
1529 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001530
1531 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournee190dee2011-03-11 19:24:49 +00001532 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1533 OpTok.is(tok::kw_vec_step)) &&
1534 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001535
John McCalldadc5752010-08-24 06:29:42 +00001536 ExprResult Operand;
Mike Stump11289f42009-09-09 15:08:12 +00001537
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001538 // If the operand doesn't start with an '(', it must be an expression.
1539 if (Tok.isNot(tok::l_paren)) {
1540 isCastExpr = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001541 if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001542 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1543 return ExprError();
1544 }
Mike Stump11289f42009-09-09 15:08:12 +00001545
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001546 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001547 } else {
1548 // If it starts with a '(', we know that it is either a parenthesized
1549 // type-name, or it is a unary-expression that starts with a compound
1550 // literal, or starts with a primary-expression that is a parenthesized
1551 // expression.
1552 ParenParseOption ExprType = CastExpr;
1553 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001555 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001556 false, CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001557 CastRange = SourceRange(LParenLoc, RParenLoc);
1558
1559 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1560 // a type.
1561 if (ExprType == CastExpr) {
1562 isCastExpr = true;
1563 return ExprEmpty();
1564 }
1565
David Blaikiebbafb8a2012-03-11 07:00:24 +00001566 if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
Douglas Gregor5dc05532010-07-28 18:22:12 +00001567 // GNU typeof in C requires the expression to be parenthesized. Not so for
1568 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1569 // the start of a unary-expression, but doesn't include any postfix
1570 // pieces. Parse these now if present.
John McCall3669c802010-08-24 23:41:43 +00001571 if (!Operand.isInvalid())
1572 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor5dc05532010-07-28 18:22:12 +00001573 }
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001574 }
1575
1576 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1577 isCastExpr = false;
1578 return move(Operand);
1579}
1580
Chris Lattner20c6a452006-08-12 17:40:43 +00001581
James Dennett3d5e4592012-06-17 04:36:28 +00001582/// \brief Parse a sizeof or alignof expression.
1583///
1584/// \verbatim
Chris Lattner81b576e2006-08-11 02:13:20 +00001585/// unary-expression: [C99 6.5.3]
1586/// 'sizeof' unary-expression
1587/// 'sizeof' '(' type-name ')'
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001588/// [C++0x] 'sizeof' '...' '(' identifier ')'
Chris Lattner81b576e2006-08-11 02:13:20 +00001589/// [GNU] '__alignof' unary-expression
1590/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +00001591/// [C++0x] 'alignof' '(' type-id ')'
James Dennett3d5e4592012-06-17 04:36:28 +00001592/// \endverbatim
Peter Collingbournee190dee2011-03-11 19:24:49 +00001593ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +00001594 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001595 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1596 "Not a sizeof/alignof/vec_step expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001597 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001598 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001599
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001600 // [C++0x] 'sizeof' '...' '(' identifier ')'
1601 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1602 SourceLocation EllipsisLoc = ConsumeToken();
1603 SourceLocation LParenLoc, RParenLoc;
1604 IdentifierInfo *Name = 0;
1605 SourceLocation NameLoc;
1606 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001607 BalancedDelimiterTracker T(*this, tok::l_paren);
1608 T.consumeOpen();
1609 LParenLoc = T.getOpenLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001610 if (Tok.is(tok::identifier)) {
1611 Name = Tok.getIdentifierInfo();
1612 NameLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001613 T.consumeClose();
1614 RParenLoc = T.getCloseLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001615 if (RParenLoc.isInvalid())
1616 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1617 } else {
1618 Diag(Tok, diag::err_expected_parameter_pack);
1619 SkipUntil(tok::r_paren);
1620 }
1621 } else if (Tok.is(tok::identifier)) {
1622 Name = Tok.getIdentifierInfo();
1623 NameLoc = ConsumeToken();
1624 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1625 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1626 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1627 << Name
1628 << FixItHint::CreateInsertion(LParenLoc, "(")
1629 << FixItHint::CreateInsertion(RParenLoc, ")");
1630 } else {
1631 Diag(Tok, diag::err_sizeof_parameter_pack);
1632 }
1633
1634 if (!Name)
1635 return ExprError();
1636
1637 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1638 OpTok.getLocation(),
1639 *Name, NameLoc,
1640 RParenLoc);
1641 }
Richard Smithb15c11c2011-10-17 23:06:20 +00001642
1643 if (OpTok.is(tok::kw_alignof))
1644 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1645
Eli Friedmane0afc982012-01-21 01:01:51 +00001646 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1647
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001648 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00001649 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001650 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001651 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1652 isCastExpr,
1653 CastTy,
1654 CastRange);
1655
1656 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1657 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1658 ExprKind = UETT_AlignOf;
1659 else if (OpTok.is(tok::kw_vec_step))
1660 ExprKind = UETT_VecStep;
Sebastian Redl90893182008-12-11 22:33:27 +00001661
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001662 if (isCastExpr)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001663 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1664 ExprKind,
1665 /*isType=*/true,
1666 CastTy.getAsOpaquePtr(),
1667 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001668
Chris Lattner26115ac2006-08-24 06:10:04 +00001669 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001670 if (!Operand.isInvalid())
Peter Collingbournee190dee2011-03-11 19:24:49 +00001671 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1672 ExprKind,
1673 /*isType=*/false,
1674 Operand.release(),
1675 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001676 return move(Operand);
Chris Lattner81b576e2006-08-11 02:13:20 +00001677}
1678
Chris Lattner11124352006-08-12 19:16:08 +00001679/// ParseBuiltinPrimaryExpression
1680///
James Dennett3d5e4592012-06-17 04:36:28 +00001681/// \verbatim
Chris Lattner11124352006-08-12 19:16:08 +00001682/// primary-expression: [C99 6.5.1]
1683/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1684/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1685/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1686/// assign-expr ')'
1687/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbourne62c21982011-11-05 03:47:48 +00001688/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001689///
Chris Lattner11124352006-08-12 19:16:08 +00001690/// [GNU] offsetof-member-designator:
1691/// [GNU] identifier
1692/// [GNU] offsetof-member-designator '.' identifier
1693/// [GNU] offsetof-member-designator '[' expression ']'
James Dennett3d5e4592012-06-17 04:36:28 +00001694/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001695ExprResult Parser::ParseBuiltinPrimaryExpression() {
1696 ExprResult Res;
Chris Lattner11124352006-08-12 19:16:08 +00001697 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1698
1699 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001700 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001701
1702 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001703 if (Tok.isNot(tok::l_paren))
1704 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1705 << BuiltinII);
1706
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001707 BalancedDelimiterTracker PT(*this, tok::l_paren);
1708 PT.consumeOpen();
1709
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001710 // TODO: Build AST.
1711
Chris Lattner11124352006-08-12 19:16:08 +00001712 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +00001713 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001714 case tok::kw___builtin_va_arg: {
John McCalldadc5752010-08-24 06:29:42 +00001715 ExprResult Expr(ParseAssignmentExpression());
Chris Lattner0be454e2006-08-12 19:30:51 +00001716
Chris Lattner6d7e6342006-08-15 03:41:14 +00001717 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001718 Expr = ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001719
Douglas Gregor220cac52009-02-18 17:45:20 +00001720 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001721
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001722 if (Tok.isNot(tok::r_paren)) {
1723 Diag(Tok, diag::err_expected_rparen);
Douglas Gregoreda7e542010-09-18 01:28:11 +00001724 Expr = ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001725 }
Douglas Gregoreda7e542010-09-18 01:28:11 +00001726
1727 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor220cac52009-02-18 17:45:20 +00001728 Res = ExprError();
1729 else
John McCallb268a282010-08-23 23:25:46 +00001730 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001731 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001732 }
Chris Lattner687d6092007-08-30 15:51:11 +00001733 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001734 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001735 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001736 if (Ty.isInvalid()) {
1737 SkipUntil(tok::r_paren);
1738 return ExprError();
1739 }
Mike Stump11289f42009-09-09 15:08:12 +00001740
Chris Lattner6d7e6342006-08-15 03:41:14 +00001741 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001742 return ExprError();
1743
Chris Lattner11124352006-08-12 19:16:08 +00001744 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001745 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001746 Diag(Tok, diag::err_expected_ident);
1747 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001748 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001749 }
Sebastian Redl90893182008-12-11 22:33:27 +00001750
Chris Lattner687d6092007-08-30 15:51:11 +00001751 // Keep track of the various subcomponents we see.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001752 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001753
John McCallfaf5fb42010-08-26 23:41:50 +00001754 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001755 Comps.back().isBrackets = false;
1756 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1757 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001758
Sebastian Redl511ed552008-11-25 22:21:31 +00001759 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001760 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001761 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001762 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallfaf5fb42010-08-26 23:41:50 +00001763 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001764 Comps.back().isBrackets = false;
1765 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001766
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001767 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001768 Diag(Tok, diag::err_expected_ident);
1769 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001770 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001771 }
1772 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1773 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001774
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001775 } else if (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001776 if (CheckProhibitedCXX11Attribute())
1777 return ExprError();
1778
Chris Lattner11124352006-08-12 19:16:08 +00001779 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallfaf5fb42010-08-26 23:41:50 +00001780 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001781 Comps.back().isBrackets = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001782 BalancedDelimiterTracker ST(*this, tok::l_square);
1783 ST.consumeOpen();
1784 Comps.back().LocStart = ST.getOpenLocation();
Chris Lattner11124352006-08-12 19:16:08 +00001785 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001786 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001787 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001788 return move(Res);
Chris Lattner11124352006-08-12 19:16:08 +00001789 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001790 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001791
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001792 ST.consumeClose();
1793 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman5e774b12009-06-27 20:38:33 +00001794 } else {
1795 if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001796 PT.consumeClose();
Douglas Gregor220cac52009-02-18 17:45:20 +00001797 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001798 } else if (Ty.isInvalid()) {
1799 Res = ExprError();
1800 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001801 PT.consumeClose();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001802 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001803 Ty.get(), &Comps[0], Comps.size(),
1804 PT.getCloseLocation());
Eli Friedman5e774b12009-06-27 20:38:33 +00001805 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001806 break;
Chris Lattner11124352006-08-12 19:16:08 +00001807 }
1808 }
1809 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001810 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001811 case tok::kw___builtin_choose_expr: {
John McCalldadc5752010-08-24 06:29:42 +00001812 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001813 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001814 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001815 return move(Cond);
Steve Naroff9efdabc2007-08-03 21:21:27 +00001816 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001817 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001818 return ExprError();
1819
John McCalldadc5752010-08-24 06:29:42 +00001820 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001821 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001822 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001823 return move(Expr1);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001824 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001825 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001826 return ExprError();
1827
John McCalldadc5752010-08-24 06:29:42 +00001828 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001829 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001830 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001831 return move(Expr2);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001832 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001833 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001834 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001835 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001836 }
John McCallb268a282010-08-23 23:25:46 +00001837 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1838 Expr2.take(), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001839 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001840 }
Tanya Lattner55808c12011-06-04 00:47:47 +00001841 case tok::kw___builtin_astype: {
1842 // The first argument is an expression to be converted, followed by a comma.
1843 ExprResult Expr(ParseAssignmentExpression());
1844 if (Expr.isInvalid()) {
1845 SkipUntil(tok::r_paren);
1846 return ExprError();
1847 }
1848
1849 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1850 tok::r_paren))
1851 return ExprError();
1852
1853 // Second argument is the type to bitcast to.
1854 TypeResult DestTy = ParseTypeName();
1855 if (DestTy.isInvalid())
1856 return ExprError();
1857
1858 // Attempt to consume the r-paren.
1859 if (Tok.isNot(tok::r_paren)) {
1860 Diag(Tok, diag::err_expected_rparen);
1861 SkipUntil(tok::r_paren);
1862 return ExprError();
1863 }
1864
1865 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1866 ConsumeParen());
1867 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001868 }
Chandler Carruth904cb142011-07-08 04:59:44 +00001869 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001870
John McCallb268a282010-08-23 23:25:46 +00001871 if (Res.isInvalid())
1872 return ExprError();
1873
Chris Lattner11124352006-08-12 19:16:08 +00001874 // These can be followed by postfix-expr pieces because they are
1875 // primary-expressions.
John McCallb268a282010-08-23 23:25:46 +00001876 return ParsePostfixExpressionSuffix(Res.take());
Chris Lattner11124352006-08-12 19:16:08 +00001877}
1878
Chris Lattner4add4e62006-08-11 01:33:00 +00001879/// ParseParenExpression - This parses the unit that starts with a '(' token,
1880/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001881/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1882/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001883///
James Dennett3d5e4592012-06-17 04:36:28 +00001884/// \verbatim
Chris Lattner4add4e62006-08-11 01:33:00 +00001885/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001886/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001887/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1888/// postfix-expression: [C99 6.5.2]
1889/// '(' type-name ')' '{' initializer-list '}'
1890/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001891/// cast-expression: [C99 6.5.4]
1892/// '(' type-name ')' cast-expression
John McCall31168b02011-06-15 23:02:42 +00001893/// [ARC] bridged-cast-expression
1894///
1895/// [ARC] bridged-cast-expression:
1896/// (__bridge type-name) cast-expression
1897/// (__bridge_transfer type-name) cast-expression
1898/// (__bridge_retained type-name) cast-expression
James Dennett3d5e4592012-06-17 04:36:28 +00001899/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00001900ExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001901Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001902 bool isTypeCast, ParsedType &CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001903 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001904 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001905 BalancedDelimiterTracker T(*this, tok::l_paren);
1906 if (T.consumeOpen())
1907 return ExprError();
1908 SourceLocation OpenLoc = T.getOpenLocation();
1909
John McCalldadc5752010-08-24 06:29:42 +00001910 ExprResult Result(true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001911 bool isAmbiguousTypeId;
John McCallba7bf592010-08-24 05:47:05 +00001912 CastTy = ParsedType();
Sebastian Redl90893182008-12-11 22:33:27 +00001913
Douglas Gregor5e35d592010-09-14 23:59:36 +00001914 if (Tok.is(tok::code_completion)) {
1915 Actions.CodeCompleteOrdinaryName(getCurScope(),
1916 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1917 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001918 cutOffParsing();
Douglas Gregor5e35d592010-09-14 23:59:36 +00001919 return ExprError();
1920 }
John McCallc5e6b972011-04-06 02:35:25 +00001921
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001922 // Diagnose use of bridge casts in non-arc mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001923 bool BridgeCast = (getLangOpts().ObjC2 &&
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001924 (Tok.is(tok::kw___bridge) ||
1925 Tok.is(tok::kw___bridge_transfer) ||
1926 Tok.is(tok::kw___bridge_retained) ||
1927 Tok.is(tok::kw___bridge_retain)));
David Blaikiebbafb8a2012-03-11 07:00:24 +00001928 if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001929 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001930 SourceLocation BridgeKeywordLoc = ConsumeToken();
1931 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremenek084e1b42012-02-18 04:42:38 +00001932 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001933 << BridgeCastName
1934 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001935 BridgeCast = false;
1936 }
1937
John McCallc5e6b972011-04-06 02:35:25 +00001938 // None of these cases should fall through with an invalid Result
1939 // unless they've already reported an error.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001940 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001941 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall3abee492012-04-04 01:27:53 +00001942 Actions.ActOnStartStmtExpr();
1943
Richard Smithc202b282012-04-14 00:33:13 +00001944 StmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001945 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001946
Chris Lattner366727f2007-07-24 16:58:17 +00001947 // If the substmt parsed correctly, build the AST node.
John McCall3abee492012-04-04 01:27:53 +00001948 if (!Stmt.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001949 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
John McCall3abee492012-04-04 01:27:53 +00001950 } else {
1951 Actions.ActOnStmtExprError();
1952 }
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001953 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCall0c07bee2011-06-17 21:56:12 +00001954 tok::TokenKind tokenKind = Tok.getKind();
1955 SourceLocation BridgeKeywordLoc = ConsumeToken();
1956
John McCall31168b02011-06-15 23:02:42 +00001957 // Parse an Objective-C ARC ownership cast expression.
1958 ObjCBridgeCastKind Kind;
John McCall0c07bee2011-06-17 21:56:12 +00001959 if (tokenKind == tok::kw___bridge)
John McCall31168b02011-06-15 23:02:42 +00001960 Kind = OBC_Bridge;
John McCall0c07bee2011-06-17 21:56:12 +00001961 else if (tokenKind == tok::kw___bridge_transfer)
John McCall31168b02011-06-15 23:02:42 +00001962 Kind = OBC_BridgeTransfer;
John McCall0c07bee2011-06-17 21:56:12 +00001963 else if (tokenKind == tok::kw___bridge_retained)
John McCall31168b02011-06-15 23:02:42 +00001964 Kind = OBC_BridgeRetained;
John McCall0c07bee2011-06-17 21:56:12 +00001965 else {
1966 // As a hopefully temporary workaround, allow __bridge_retain as
1967 // a synonym for __bridge_retained, but only in system headers.
1968 assert(tokenKind == tok::kw___bridge_retain);
1969 Kind = OBC_BridgeRetained;
1970 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1971 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1972 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1973 "__bridge_retained");
1974 }
John McCall31168b02011-06-15 23:02:42 +00001975
John McCall31168b02011-06-15 23:02:42 +00001976 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001977 T.consumeClose();
1978 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001979 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCall31168b02011-06-15 23:02:42 +00001980
1981 if (Ty.isInvalid() || SubExpr.isInvalid())
1982 return ExprError();
1983
1984 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1985 BridgeKeywordLoc, Ty.get(),
1986 RParenLoc, SubExpr.get());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001987 } else if (ExprType >= CompoundLiteral &&
1988 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00001989
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001990 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00001991
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001992 // In C++, if the type-id is ambiguous we disambiguate based on context.
1993 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1994 // in which case we should treat it as type-id.
1995 // if stopIfCastExpr is false, we need to determine the context past the
1996 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001997 if (isAmbiguousTypeId && !stopIfCastExpr) {
1998 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
1999 RParenLoc = T.getCloseLocation();
2000 return res;
2001 }
Mike Stump11289f42009-09-09 15:08:12 +00002002
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002003 // Parse the type declarator.
2004 DeclSpec DS(AttrFactory);
2005 ParseSpecifierQualifierList(DS);
2006 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2007 ParseDeclarator(DeclaratorInfo);
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002008
Douglas Gregor3e972002010-09-15 23:19:31 +00002009 // If our type is followed by an identifier and either ':' or ']', then
2010 // this is probably an Objective-C message send where the leading '[' is
2011 // missing. Recover as if that were the case.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002012 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002013 !InMessageExpression && getLangOpts().ObjC1 &&
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002014 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
2015 TypeResult Ty;
2016 {
2017 InMessageExpressionRAIIObject InMessage(*this, false);
2018 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2019 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002020 Result = ParseObjCMessageExpressionBody(SourceLocation(),
2021 SourceLocation(),
2022 Ty.get(), 0);
2023 } else {
2024 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002025 T.consumeClose();
2026 RParenLoc = T.getCloseLocation();
Douglas Gregor3e972002010-09-15 23:19:31 +00002027 if (Tok.is(tok::l_brace)) {
2028 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002029 TypeResult Ty;
2030 {
2031 InMessageExpressionRAIIObject InMessage(*this, false);
2032 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2033 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002034 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +00002035 }
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00002036
Douglas Gregor3e972002010-09-15 23:19:31 +00002037 if (ExprType == CastExpr) {
2038 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb5d49352009-01-19 22:31:54 +00002039
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002040 if (DeclaratorInfo.isInvalidType())
Douglas Gregor3e972002010-09-15 23:19:31 +00002041 return ExprError();
2042
Douglas Gregor3e972002010-09-15 23:19:31 +00002043 // Note that this doesn't parse the subsequent cast-expression, it just
2044 // returns the parsed type to the callee.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002045 if (stopIfCastExpr) {
2046 TypeResult Ty;
2047 {
2048 InMessageExpressionRAIIObject InMessage(*this, false);
2049 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2050 }
2051 CastTy = Ty.get();
Douglas Gregor3e972002010-09-15 23:19:31 +00002052 return ExprResult();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002053 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002054
2055 // Reject the cast of super idiom in ObjC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002056 if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
Douglas Gregor3e972002010-09-15 23:19:31 +00002057 Tok.getIdentifierInfo() == Ident_super &&
2058 getCurScope()->isInObjcMethodScope() &&
2059 GetLookAheadToken(1).isNot(tok::period)) {
2060 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2061 << SourceRange(OpenLoc, RParenLoc);
2062 return ExprError();
2063 }
2064
2065 // Parse the cast-expression that follows it next.
2066 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002067 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2068 /*isAddressOfOperand=*/false,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002069 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002070 if (!Result.isInvalid()) {
2071 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2072 DeclaratorInfo, CastTy,
Douglas Gregor3e972002010-09-15 23:19:31 +00002073 RParenLoc, Result.take());
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002074 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002075 return move(Result);
2076 }
2077
2078 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2079 return ExprError();
2080 }
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002081 } else if (isTypeCast) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00002082 // Parse the expression-list.
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002083 InMessageExpressionRAIIObject InMessage(*this, false);
2084
Nate Begeman5ec4b312009-08-10 23:49:36 +00002085 ExprVector ArgExprs(Actions);
2086 CommaLocsTy CommaLocs;
2087
2088 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2089 ExprType = SimpleExpr;
Sebastian Redla9351792012-02-11 23:51:47 +00002090 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
2091 move_arg(ArgExprs));
Nate Begeman5ec4b312009-08-10 23:49:36 +00002092 }
Chris Lattner4add4e62006-08-11 01:33:00 +00002093 } else {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002094 InMessageExpressionRAIIObject InMessage(*this, false);
2095
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002096 Result = ParseExpression(MaybeTypeCast);
Chris Lattner4add4e62006-08-11 01:33:00 +00002097 ExprType = SimpleExpr;
John McCallc5e6b972011-04-06 02:35:25 +00002098
2099 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002100 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCallb268a282010-08-23 23:25:46 +00002101 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Chris Lattnerf8339772006-08-10 22:01:51 +00002102 }
Sebastian Redl90893182008-12-11 22:33:27 +00002103
Chris Lattner4564bc12006-08-10 23:14:52 +00002104 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00002105 if (Result.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +00002106 SkipUntil(tok::r_paren);
Chris Lattnerd8980502008-12-12 06:00:12 +00002107 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00002108 }
Mike Stump11289f42009-09-09 15:08:12 +00002109
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002110 T.consumeClose();
2111 RParenLoc = T.getCloseLocation();
Sebastian Redl90893182008-12-11 22:33:27 +00002112 return move(Result);
Chris Lattnerc951dae2006-08-10 04:23:57 +00002113}
Chris Lattnerd3e98952006-10-06 05:22:26 +00002114
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002115/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2116/// and we are at the left brace.
2117///
James Dennett3d5e4592012-06-17 04:36:28 +00002118/// \verbatim
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002119/// postfix-expression: [C99 6.5.2]
2120/// '(' type-name ')' '{' initializer-list '}'
2121/// '(' type-name ')' '{' initializer-list ',' '}'
James Dennett3d5e4592012-06-17 04:36:28 +00002122/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002123ExprResult
John McCallba7bf592010-08-24 05:47:05 +00002124Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002125 SourceLocation LParenLoc,
2126 SourceLocation RParenLoc) {
2127 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
David Blaikiebbafb8a2012-03-11 07:00:24 +00002128 if (!getLangOpts().C99) // Compound literals don't exist in C90.
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002129 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCalldadc5752010-08-24 06:29:42 +00002130 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002131 if (!Result.isInvalid() && Ty)
John McCallb268a282010-08-23 23:25:46 +00002132 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002133 return move(Result);
2134}
2135
Chris Lattnerd3e98952006-10-06 05:22:26 +00002136/// ParseStringLiteralExpression - This handles the various token types that
2137/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2138/// translation phase #6].
2139///
James Dennett3d5e4592012-06-17 04:36:28 +00002140/// \verbatim
Chris Lattnerd3e98952006-10-06 05:22:26 +00002141/// primary-expression: [C99 6.5.1]
2142/// string-literal
James Dennett3d5e4592012-06-17 04:36:28 +00002143/// \verbatim
Richard Smithd67aea22012-03-06 03:21:47 +00002144ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002145 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00002146
Chris Lattnerd3e98952006-10-06 05:22:26 +00002147 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2148 // considered to be strings for concatenation purposes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002149 SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00002150
Chris Lattnerd3e98952006-10-06 05:22:26 +00002151 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002152 StringToks.push_back(Tok);
2153 ConsumeStringToken();
2154 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00002155
2156 // Pass the set of string tokens, ready for concatenation, to the actions.
Richard Smithbcc22fc2012-03-09 08:00:36 +00002157 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(),
2158 AllowUserDefinedLiteral ? getCurScope() : 0);
Chris Lattnerd3e98952006-10-06 05:22:26 +00002159}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002160
Benjamin Kramere56f3932011-12-23 17:00:35 +00002161/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2162/// [C11 6.5.1.1].
Peter Collingbourne91147592011-04-15 00:35:48 +00002163///
James Dennett3d5e4592012-06-17 04:36:28 +00002164/// \verbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002165/// generic-selection:
2166/// _Generic ( assignment-expression , generic-assoc-list )
2167/// generic-assoc-list:
2168/// generic-association
2169/// generic-assoc-list , generic-association
2170/// generic-association:
2171/// type-name : assignment-expression
2172/// default : assignment-expression
James Dennett3d5e4592012-06-17 04:36:28 +00002173/// \endverbatim
Peter Collingbourne91147592011-04-15 00:35:48 +00002174ExprResult Parser::ParseGenericSelectionExpression() {
2175 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2176 SourceLocation KeyLoc = ConsumeToken();
2177
David Blaikiebbafb8a2012-03-11 07:00:24 +00002178 if (!getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +00002179 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbourne91147592011-04-15 00:35:48 +00002180
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002181 BalancedDelimiterTracker T(*this, tok::l_paren);
2182 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne91147592011-04-15 00:35:48 +00002183 return ExprError();
2184
2185 ExprResult ControllingExpr;
2186 {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002187 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbourne91147592011-04-15 00:35:48 +00002188 // not evaluated."
2189 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2190 ControllingExpr = ParseAssignmentExpression();
2191 if (ControllingExpr.isInvalid()) {
2192 SkipUntil(tok::r_paren);
2193 return ExprError();
2194 }
2195 }
2196
2197 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2198 SkipUntil(tok::r_paren);
2199 return ExprError();
2200 }
2201
2202 SourceLocation DefaultLoc;
2203 TypeVector Types(Actions);
2204 ExprVector Exprs(Actions);
2205 while (1) {
2206 ParsedType Ty;
2207 if (Tok.is(tok::kw_default)) {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002208 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbourne91147592011-04-15 00:35:48 +00002209 // generic association."
2210 if (!DefaultLoc.isInvalid()) {
2211 Diag(Tok, diag::err_duplicate_default_assoc);
2212 Diag(DefaultLoc, diag::note_previous_default_assoc);
2213 SkipUntil(tok::r_paren);
2214 return ExprError();
2215 }
2216 DefaultLoc = ConsumeToken();
2217 Ty = ParsedType();
2218 } else {
2219 ColonProtectionRAIIObject X(*this);
2220 TypeResult TR = ParseTypeName();
2221 if (TR.isInvalid()) {
2222 SkipUntil(tok::r_paren);
2223 return ExprError();
2224 }
2225 Ty = TR.release();
2226 }
2227 Types.push_back(Ty);
2228
2229 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2230 SkipUntil(tok::r_paren);
2231 return ExprError();
2232 }
2233
2234 // FIXME: These expressions should be parsed in a potentially potentially
2235 // evaluated context.
2236 ExprResult ER(ParseAssignmentExpression());
2237 if (ER.isInvalid()) {
2238 SkipUntil(tok::r_paren);
2239 return ExprError();
2240 }
2241 Exprs.push_back(ER.release());
2242
2243 if (Tok.isNot(tok::comma))
2244 break;
2245 ConsumeToken();
2246 }
2247
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002248 T.consumeClose();
2249 if (T.getCloseLocation().isInvalid())
Peter Collingbourne91147592011-04-15 00:35:48 +00002250 return ExprError();
2251
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002252 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2253 T.getCloseLocation(),
Peter Collingbourne91147592011-04-15 00:35:48 +00002254 ControllingExpr.release(),
2255 move_arg(Types), move_arg(Exprs));
2256}
2257
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002258/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2259///
James Dennett3d5e4592012-06-17 04:36:28 +00002260/// \verbatim
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002261/// argument-expression-list:
2262/// assignment-expression
2263/// argument-expression-list , assignment-expression
2264///
2265/// [C++] expression-list:
Sebastian Redl3da34892011-06-05 12:23:16 +00002266/// [C++] assignment-expression
2267/// [C++] expression-list , assignment-expression
2268///
2269/// [C++0x] expression-list:
2270/// [C++0x] initializer-list
2271///
2272/// [C++0x] initializer-list
2273/// [C++0x] initializer-clause ...[opt]
2274/// [C++0x] initializer-list , initializer-clause ...[opt]
2275///
2276/// [C++0x] initializer-clause:
2277/// [C++0x] assignment-expression
2278/// [C++0x] braced-init-list
James Dennett3d5e4592012-06-17 04:36:28 +00002279/// \endverbatim
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002280bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2281 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallfaf5fb42010-08-26 23:41:50 +00002282 void (Sema::*Completer)(Scope *S,
John McCall37ad5512010-08-23 06:44:23 +00002283 Expr *Data,
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002284 llvm::ArrayRef<Expr *> Args),
John McCall37ad5512010-08-23 06:44:23 +00002285 Expr *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002286 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00002287 if (Tok.is(tok::code_completion)) {
2288 if (Completer)
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002289 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor9c1f1bf2011-02-17 03:09:23 +00002290 else
2291 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002292 cutOffParsing();
2293 return true;
Douglas Gregorcabea402009-09-22 15:41:20 +00002294 }
Sebastian Redl3da34892011-06-05 12:23:16 +00002295
2296 ExprResult Expr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002297 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002298 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00002299 Expr = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00002300 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00002301 Expr = ParseAssignmentExpression();
2302
Douglas Gregor968f23a2011-01-03 19:31:53 +00002303 if (Tok.is(tok::ellipsis))
2304 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002305 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002306 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00002307
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002308 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002309
2310 if (Tok.isNot(tok::comma))
2311 return false;
2312 // Move to the next argument, remember where the comma was.
2313 CommaLocs.push_back(ConsumeToken());
2314 }
2315}
Steve Naroff0ac012832008-08-28 19:20:44 +00002316
Mike Stump82f071f2009-02-04 22:31:32 +00002317/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2318///
James Dennett3d5e4592012-06-17 04:36:28 +00002319/// \verbatim
Mike Stump82f071f2009-02-04 22:31:32 +00002320/// [clang] block-id:
2321/// [clang] specifier-qualifier-list block-declarator
James Dennett3d5e4592012-06-17 04:36:28 +00002322/// \endverbatim
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002323void Parser::ParseBlockId(SourceLocation CaretLoc) {
Douglas Gregor643c3302010-10-18 21:34:55 +00002324 if (Tok.is(tok::code_completion)) {
2325 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002326 return cutOffParsing();
Douglas Gregor643c3302010-10-18 21:34:55 +00002327 }
2328
Mike Stump82f071f2009-02-04 22:31:32 +00002329 // Parse the specifier-qualifier-list piece.
John McCall084e83d2011-03-24 11:26:52 +00002330 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002331 ParseSpecifierQualifierList(DS);
2332
2333 // Parse the block-declarator.
2334 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2335 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002336
Mike Stump56ed2ea2009-04-29 21:40:37 +00002337 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall084e83d2011-03-24 11:26:52 +00002338 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump56ed2ea2009-04-29 21:40:37 +00002339
John McCall53fa7142010-12-24 02:08:15 +00002340 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002341
Mike Stump82f071f2009-02-04 22:31:32 +00002342 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002343 Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
Mike Stump82f071f2009-02-04 22:31:32 +00002344}
2345
Steve Naroff0ac012832008-08-28 19:20:44 +00002346/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00002347/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00002348///
James Dennett3d5e4592012-06-17 04:36:28 +00002349/// \verbatim
Steve Naroff0ac012832008-08-28 19:20:44 +00002350/// block-literal:
2351/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00002352/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00002353/// [clang] block-args:
2354/// [clang] '(' parameter-list ')'
James Dennett3d5e4592012-06-17 04:36:28 +00002355/// \endverbatim
John McCalldadc5752010-08-24 06:29:42 +00002356ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00002357 assert(Tok.is(tok::caret) && "block literal starts with ^");
2358 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002359
Chris Lattnerf6801202009-03-05 07:32:12 +00002360 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2361 "block literal parsing");
2362
Mike Stump11289f42009-09-09 15:08:12 +00002363 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00002364 // argument decls, decls within the compound expression, etc. This also
2365 // allows determining whether a variable reference inside the block is
2366 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002367 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002368 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00002369
2370 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002371 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump11289f42009-09-09 15:08:12 +00002372
Steve Naroff0ac012832008-08-28 19:20:44 +00002373 // Parse the return type if present.
John McCall084e83d2011-03-24 11:26:52 +00002374 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002375 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002376 // FIXME: Since the return type isn't actually parsed, it can't be used to
2377 // fill ParamInfo with an initial valid range, so do it manually.
2378 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002379
Steve Naroff0ac012832008-08-28 19:20:44 +00002380 // If this block has arguments, parse them. There is no ambiguity here with
2381 // the expression case, because the expression case requires a parameter list.
2382 if (Tok.is(tok::l_paren)) {
2383 ParseParenDeclarator(ParamInfo);
2384 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002385 // SetIdentifier sets the source range end, but in this case we're past
2386 // that location.
2387 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00002388 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002389 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002390 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00002391 // If there was an error parsing the arguments, they may have
2392 // tried to use ^(x+y) which requires an argument list. Just
2393 // skip the whole block literal.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002394 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002395 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00002396 }
Mike Stump88788fe2009-04-29 19:03:13 +00002397
John McCall53fa7142010-12-24 02:08:15 +00002398 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002399
Mike Stump82f071f2009-02-04 22:31:32 +00002400 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002401 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Mike Stumpd73e4412009-04-14 18:24:37 +00002402 } else if (!Tok.is(tok::l_brace)) {
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002403 ParseBlockId(CaretLoc);
Steve Naroff0ac012832008-08-28 19:20:44 +00002404 } else {
2405 // Otherwise, pretend we saw (void).
John McCall084e83d2011-03-24 11:26:52 +00002406 ParsedAttributes attrs(AttrFactory);
2407 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002408 SourceLocation(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002409 0, 0, 0,
Douglas Gregor54992352011-01-26 03:43:54 +00002410 true, SourceLocation(),
Douglas Gregorad69e652011-07-13 21:47:47 +00002411 SourceLocation(),
Douglas Gregore248eea2011-10-19 06:04:55 +00002412 SourceLocation(),
2413 SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00002414 EST_None,
2415 SourceLocation(),
Richard Smith2331bbf2012-05-02 22:22:32 +00002416 0, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002417 CaretLoc, CaretLoc,
2418 ParamInfo),
John McCall084e83d2011-03-24 11:26:52 +00002419 attrs, CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00002420
John McCall53fa7142010-12-24 02:08:15 +00002421 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002422
Mike Stump82f071f2009-02-04 22:31:32 +00002423 // Inform sema that we are starting a block.
Douglas Gregor7efd007c2012-06-15 16:59:29 +00002424 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Steve Naroff0ac012832008-08-28 19:20:44 +00002425 }
2426
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002427
John McCalldadc5752010-08-24 06:29:42 +00002428 ExprResult Result(true);
Chris Lattner9eac9312009-03-27 04:18:06 +00002429 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002430 // Saw something like: ^expr
2431 Diag(Tok, diag::err_expected_expression);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002432 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002433 return ExprError();
2434 }
Mike Stump11289f42009-09-09 15:08:12 +00002435
John McCalldadc5752010-08-24 06:29:42 +00002436 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002437 BlockScope.Exit();
Chris Lattner9eac9312009-03-27 04:18:06 +00002438 if (!Stmt.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002439 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9eac9312009-03-27 04:18:06 +00002440 else
Douglas Gregor0be31a22010-07-02 17:43:08 +00002441 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002442 return move(Result);
Steve Naroff0ac012832008-08-28 19:20:44 +00002443}
Ted Kremeneke65b0862012-03-06 20:05:56 +00002444
2445/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
2446///
2447/// '__objc_yes'
2448/// '__objc_no'
2449ExprResult Parser::ParseObjCBoolLiteral() {
2450 tok::TokenKind Kind = Tok.getKind();
2451 return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
2452}