blob: 9b95641f46bd97e944cc66484f42da80fbeb3cc8 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
James Dennett38b06032012-06-19 21:02:26 +00009///
James Dennette30d3ff2012-06-17 04:36:28 +000010/// \file
11/// \brief Provides the Expression parsing implementation.
12///
13/// Expressions in C99 basically consist of a bunch of binary operators with
14/// unary operators and other random stuff at the leaves.
15///
16/// In the C99 grammar, these unary operators bind tightest and are represented
17/// as the 'cast-expression' production. Everything else is either a binary
18/// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
19/// handled by ParseCastExpression, the higher level pieces are handled by
20/// ParseBinaryExpression.
James Dennett38b06032012-06-19 21:02:26 +000021///
22//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000023
24#include "clang/Parse/Parser.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/Scope.h"
27#include "clang/Sema/ParsedTemplate.h"
Kaelyn Uhraincd78e612012-01-25 20:49:08 +000028#include "clang/Sema/TypoCorrection.h"
Chris Lattner6b91f002009-03-05 07:32:12 +000029#include "clang/Basic/PrettyStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000030#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000031#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/SmallString.h"
33using namespace clang;
34
James Dennette30d3ff2012-06-17 04:36:28 +000035/// \brief Return the precedence of the specified binary operator token.
Mike Stump1eb44332009-09-09 15:08:12 +000036static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregor3965b7b2009-02-25 23:02:36 +000037 bool GreaterThanIsOperator,
38 bool CPlusPlus0x) {
Reid Spencer5f016e22007-07-11 17:01:13 +000039 switch (Kind) {
Douglas Gregor55f6b142009-02-09 18:46:07 +000040 case tok::greater:
Douglas Gregor3965b7b2009-02-25 23:02:36 +000041 // C++ [temp.names]p3:
42 // [...] When parsing a template-argument-list, the first
43 // non-nested > is taken as the ending delimiter rather than a
44 // greater-than operator. [...]
Douglas Gregor55f6b142009-02-09 18:46:07 +000045 if (GreaterThanIsOperator)
46 return prec::Relational;
47 return prec::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +000048
Douglas Gregor3965b7b2009-02-25 23:02:36 +000049 case tok::greatergreater:
50 // C++0x [temp.names]p3:
51 //
52 // [...] Similarly, the first non-nested >> is treated as two
53 // consecutive but distinct > tokens, the first of which is
54 // taken as the end of the template-argument-list and completes
55 // the template-id. [...]
56 if (GreaterThanIsOperator || !CPlusPlus0x)
57 return prec::Shift;
58 return prec::Unknown;
59
Reid Spencer5f016e22007-07-11 17:01:13 +000060 default: return prec::Unknown;
61 case tok::comma: return prec::Comma;
62 case tok::equal:
63 case tok::starequal:
64 case tok::slashequal:
65 case tok::percentequal:
66 case tok::plusequal:
67 case tok::minusequal:
68 case tok::lesslessequal:
69 case tok::greatergreaterequal:
70 case tok::ampequal:
71 case tok::caretequal:
72 case tok::pipeequal: return prec::Assignment;
73 case tok::question: return prec::Conditional;
74 case tok::pipepipe: return prec::LogicalOr;
75 case tok::ampamp: return prec::LogicalAnd;
76 case tok::pipe: return prec::InclusiveOr;
77 case tok::caret: return prec::ExclusiveOr;
78 case tok::amp: return prec::And;
79 case tok::exclaimequal:
80 case tok::equalequal: return prec::Equality;
81 case tok::lessequal:
82 case tok::less:
Douglas Gregor55f6b142009-02-09 18:46:07 +000083 case tok::greaterequal: return prec::Relational;
Douglas Gregor3965b7b2009-02-25 23:02:36 +000084 case tok::lessless: return prec::Shift;
Reid Spencer5f016e22007-07-11 17:01:13 +000085 case tok::plus:
86 case tok::minus: return prec::Additive;
87 case tok::percent:
88 case tok::slash:
89 case tok::star: return prec::Multiplicative;
Sebastian Redl22460502009-02-07 00:15:38 +000090 case tok::periodstar:
91 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +000092 }
93}
94
95
James Dennette30d3ff2012-06-17 04:36:28 +000096/// \brief Simple precedence-based parser for binary/ternary operators.
Reid Spencer5f016e22007-07-11 17:01:13 +000097///
98/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
James Dennette30d3ff2012-06-17 04:36:28 +0000107/// \verbatim
Sebastian Redl22460502009-02-07 00:15:38 +0000108/// pm-expression: [C++ 5.5]
109/// cast-expression
110/// pm-expression '.*' cast-expression
111/// pm-expression '->*' cast-expression
112///
Reid Spencer5f016e22007-07-11 17:01:13 +0000113/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000114/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000115/// cast-expression
116/// multiplicative-expression '*' cast-expression
117/// multiplicative-expression '/' cast-expression
118/// multiplicative-expression '%' cast-expression
119///
120/// additive-expression: [C99 6.5.6]
121/// multiplicative-expression
122/// additive-expression '+' multiplicative-expression
123/// additive-expression '-' multiplicative-expression
124///
125/// shift-expression: [C99 6.5.7]
126/// additive-expression
127/// shift-expression '<<' additive-expression
128/// shift-expression '>>' additive-expression
129///
130/// relational-expression: [C99 6.5.8]
131/// shift-expression
132/// relational-expression '<' shift-expression
133/// relational-expression '>' shift-expression
134/// relational-expression '<=' shift-expression
135/// relational-expression '>=' shift-expression
136///
137/// equality-expression: [C99 6.5.9]
138/// relational-expression
139/// equality-expression '==' relational-expression
140/// equality-expression '!=' relational-expression
141///
142/// AND-expression: [C99 6.5.10]
143/// equality-expression
144/// AND-expression '&' equality-expression
145///
146/// exclusive-OR-expression: [C99 6.5.11]
147/// AND-expression
148/// exclusive-OR-expression '^' AND-expression
149///
150/// inclusive-OR-expression: [C99 6.5.12]
151/// exclusive-OR-expression
152/// inclusive-OR-expression '|' exclusive-OR-expression
153///
154/// logical-AND-expression: [C99 6.5.13]
155/// inclusive-OR-expression
156/// logical-AND-expression '&&' inclusive-OR-expression
157///
158/// logical-OR-expression: [C99 6.5.14]
159/// logical-AND-expression
160/// logical-OR-expression '||' logical-AND-expression
161///
162/// conditional-expression: [C99 6.5.15]
163/// logical-OR-expression
164/// logical-OR-expression '?' expression ':' conditional-expression
165/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000166/// [C++] the third operand is an assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000167///
168/// assignment-expression: [C99 6.5.16]
169/// conditional-expression
170/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000171/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000172///
173/// assignment-operator: one of
174/// = *= /= %= += -= <<= >>= &= ^= |=
175///
176/// expression: [C99 6.5.17]
Douglas Gregordcaa1ca2011-01-03 19:31:53 +0000177/// assignment-expression ...[opt]
178/// expression ',' assignment-expression ...[opt]
James Dennette30d3ff2012-06-17 04:36:28 +0000179/// \endverbatim
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000180ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
181 ExprResult LHS(ParseAssignmentExpression(isTypeCast));
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000182 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000183}
184
Mike Stump1eb44332009-09-09 15:08:12 +0000185/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000186/// Current token is an Identifier and is not a 'try'. This
James Dennett7a90c8b2012-06-15 06:52:33 +0000187/// routine is necessary to disambiguate \@try-statement from,
188/// for example, \@encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000189///
John McCall60d7b3a2010-08-24 06:29:42 +0000190ExprResult
Sebastian Redld8c4e152008-12-11 22:33:27 +0000191Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
John McCall60d7b3a2010-08-24 06:29:42 +0000192 ExprResult LHS(ParseObjCAtExpression(AtLoc));
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000193 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000194}
195
Eli Friedmanadf077f2009-01-27 08:43:38 +0000196/// This routine is called when a leading '__extension__' is seen and
197/// consumed. This is necessary because the token gets consumed in the
198/// process of disambiguating between an expression and a declaration.
John McCall60d7b3a2010-08-24 06:29:42 +0000199ExprResult
Eli Friedmanadf077f2009-01-27 08:43:38 +0000200Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
John McCall60d7b3a2010-08-24 06:29:42 +0000201 ExprResult LHS(true);
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000202 {
203 // Silence extension warnings in the sub-expression
204 ExtensionRAIIObject O(Diags);
205
206 LHS = ParseCastExpression(false);
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000207 }
Eli Friedmanadf077f2009-01-27 08:43:38 +0000208
Douglas Gregor200b2922010-09-17 22:25:06 +0000209 if (!LHS.isInvalid())
210 LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
211 LHS.take());
Eli Friedmanadf077f2009-01-27 08:43:38 +0000212
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000213 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
Eli Friedmanadf077f2009-01-27 08:43:38 +0000214}
215
James Dennette30d3ff2012-06-17 04:36:28 +0000216/// \brief Parse an expr that doesn't include (top-level) commas.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000217ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000218 if (Tok.is(tok::code_completion)) {
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000219 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000220 cutOffParsing();
221 return ExprError();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000222 }
223
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000224 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000225 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000226
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000227 ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
228 /*isAddressOfOperand=*/false,
229 isTypeCast);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000230 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000231}
232
James Dennette30d3ff2012-06-17 04:36:28 +0000233/// \brief Parse an assignment expression where part of an Objective-C message
234/// send has already been parsed.
235///
236/// In this case \p LBracLoc indicates the location of the '[' of the message
237/// send, and either \p ReceiverName or \p ReceiverExpr is non-null indicating
238/// the receiver of the message.
Chris Lattnerb93fb492008-06-02 21:31:07 +0000239///
240/// Since this handles full assignment-expression's, it handles postfix
241/// expressions and other binary operators for these expressions as well.
John McCall60d7b3a2010-08-24 06:29:42 +0000242ExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000243Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +0000244 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +0000245 ParsedType ReceiverType,
John McCall9ae2f072010-08-23 23:25:46 +0000246 Expr *ReceiverExpr) {
John McCall60d7b3a2010-08-24 06:29:42 +0000247 ExprResult R
John McCall9ae2f072010-08-23 23:25:46 +0000248 = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
249 ReceiverType, ReceiverExpr);
Douglas Gregorac5fd842010-09-18 01:28:11 +0000250 R = ParsePostfixExpressionSuffix(R);
Douglas Gregor200b2922010-09-17 22:25:06 +0000251 return ParseRHSOfBinaryExpression(R, prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000252}
253
254
Kaelyn Uhraine43fe992012-02-22 01:03:07 +0000255ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
Richard Smithf6702a32011-12-20 02:08:33 +0000256 // C++03 [basic.def.odr]p2:
Mike Stump1eb44332009-09-09 15:08:12 +0000257 // An expression is potentially evaluated unless it appears where an
Douglas Gregore0762c92009-06-19 23:52:42 +0000258 // integral constant expression is required (see 5.19) [...].
Richard Smithf6702a32011-12-20 02:08:33 +0000259 // C++98 and C++11 have no such rule, but this is only a defect in C++98.
Douglas Gregorac7610d2009-06-22 20:57:11 +0000260 EnterExpressionEvaluationContext Unevaluated(Actions,
Richard Smithf6702a32011-12-20 02:08:33 +0000261 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Kaelyn Uhraine43fe992012-02-22 01:03:07 +0000263 ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
Eli Friedmanac626012012-02-29 03:16:56 +0000264 ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
265 return Actions.ActOnConstantExpression(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000266}
267
Richard Smith4b082422012-09-18 00:52:05 +0000268bool Parser::isNotExpressionStart() {
269 tok::TokenKind K = Tok.getKind();
270 if (K == tok::l_brace || K == tok::r_brace ||
271 K == tok::kw_for || K == tok::kw_while ||
272 K == tok::kw_if || K == tok::kw_else ||
273 K == tok::kw_goto || K == tok::kw_try)
274 return true;
275 // If this is a decl-specifier, we can't be at the start of an expression.
276 return isKnownToBeDeclarationSpecifier();
277}
278
James Dennette30d3ff2012-06-17 04:36:28 +0000279/// \brief Parse a binary expression that starts with \p LHS and has a
280/// precedence of at least \p MinPrec.
John McCall60d7b3a2010-08-24 06:29:42 +0000281ExprResult
282Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000283 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
284 GreaterThanIsOperator,
David Blaikie4e4d0842012-03-11 07:00:24 +0000285 getLangOpts().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 SourceLocation ColonLoc;
287
288 while (1) {
289 // If this token has a lower precedence than we are allowed to parse (e.g.
290 // because we are called recursively, or because the token is not a binop),
291 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000292 if (NextTokPrec < MinPrec)
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000293 return LHS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000294
295 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000296 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000298
Richard Smith4b082422012-09-18 00:52:05 +0000299 // Bail out when encountering a comma followed by a token which can't
300 // possibly be the start of an expression. For instance:
301 // int f() { return 1, }
302 // We can't do this before consuming the comma, because
303 // isNotExpressionStart() looks at the token stream.
304 if (OpToken.is(tok::comma) && isNotExpressionStart()) {
305 PP.EnterToken(Tok);
306 Tok = OpToken;
307 return LHS;
308 }
309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // Special case handling for the ternary operator.
John McCall60d7b3a2010-08-24 06:29:42 +0000311 ExprResult TernaryMiddle(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000313 if (Tok.isNot(tok::colon)) {
Chris Lattnera69d0ed2009-12-10 02:02:58 +0000314 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
315 ColonProtectionRAIIObject X(*this);
316
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 // Handle this production specially:
318 // logical-OR-expression '?' expression ':' conditional-expression
319 // In particular, the RHS of the '?' is 'expression', not
320 // 'logical-OR-expression' as we might expect.
321 TernaryMiddle = ParseExpression();
Douglas Gregor94859892010-09-17 22:41:34 +0000322 if (TernaryMiddle.isInvalid()) {
323 LHS = ExprError();
324 TernaryMiddle = 0;
325 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 } else {
327 // Special case handling of "X ? Y : Z" where Y is empty:
328 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000329 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 Diag(Tok, diag::ext_gnu_conditional_expr);
331 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000332
Chris Lattnere5deae92010-04-20 21:33:39 +0000333 if (Tok.is(tok::colon)) {
334 // Eat the colon.
335 ColonLoc = ConsumeToken();
336 } else {
Chandler Carruthb00d37e2011-07-26 05:19:46 +0000337 // Otherwise, we're missing a ':'. Assume that this was a typo that
338 // the user forgot. If we're not in a macro expansion, we can suggest
339 // a fixit hint. If there were two spaces before the current token,
Chris Lattner24728822010-05-24 22:31:37 +0000340 // suggest inserting the colon in between them, otherwise insert ": ".
341 SourceLocation FILoc = Tok.getLocation();
342 const char *FIText = ": ";
Argyrios Kyrtzidisb5303aa2011-06-24 17:28:29 +0000343 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis69bda4c2012-01-19 15:59:08 +0000344 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
345 assert(FILoc.isFileID());
Chris Lattner24728822010-05-24 22:31:37 +0000346 bool IsInvalid = false;
347 const char *SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000348 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000349 if (!IsInvalid && *SourcePtr == ' ') {
350 SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000351 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000352 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000353 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattner24728822010-05-24 22:31:37 +0000354 FIText = ":";
355 }
356 }
357 }
358
Ted Kremenek987aa872010-04-12 22:10:35 +0000359 Diag(Tok, diag::err_expected_colon)
Chris Lattner24728822010-05-24 22:31:37 +0000360 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000361 Diag(OpToken, diag::note_matching) << "?";
Chris Lattnere5deae92010-04-20 21:33:39 +0000362 ColonLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 }
Fariborz Jahanianf071e9b2009-10-23 21:01:39 +0000365
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000366 // Code completion for the right-hand side of an assignment expression
367 // goes through a special hook that takes the left-hand side into account.
368 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000369 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000370 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000371 return ExprError();
372 }
373
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000375 // ParseCastExpression works here because all RHS expressions in C have it
376 // as a prefix, at least. However, in C++, an assignment-expression could
377 // be a throw-expression, which is not a valid cast-expression.
378 // Therefore we need some special-casing here.
379 // Also note that the third operand of the conditional operator is
Richard Smithc56ab432012-02-26 23:40:27 +0000380 // an assignment-expression in C++, and in C++11, we can have a
Richard Smith5e4e58b2012-03-01 02:59:17 +0000381 // braced-init-list on the RHS of an assignment. For better diagnostics,
382 // parse as if we were allowed braced-init-lists everywhere, and check that
383 // they only appear on the RHS of assignments later.
John McCall60d7b3a2010-08-24 06:29:42 +0000384 ExprResult RHS;
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000385 bool RHSIsInitList = false;
David Blaikie4e4d0842012-03-11 07:00:24 +0000386 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smithc56ab432012-02-26 23:40:27 +0000387 RHS = ParseBraceInitializer();
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000388 RHSIsInitList = true;
David Blaikie4e4d0842012-03-11 07:00:24 +0000389 } else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional)
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000390 RHS = ParseAssignmentExpression();
Richard Smith5e4e58b2012-03-01 02:59:17 +0000391 else
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000392 RHS = ParseCastExpression(false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000393
Douglas Gregor200b2922010-09-17 22:25:06 +0000394 if (RHS.isInvalid())
395 LHS = ExprError();
396
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 // Remember the precedence of this operator and get the precedence of the
398 // operator immediately to the right of the RHS.
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000399 prec::Level ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000400 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikie4e4d0842012-03-11 07:00:24 +0000401 getLangOpts().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000402
403 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000404 bool isRightAssoc = ThisPrec == prec::Conditional ||
405 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000406
407 // Get the precedence of the operator to the right of the RHS. If it binds
408 // more tightly with RHS than we do, evaluate it completely first.
409 if (ThisPrec < NextTokPrec ||
410 (ThisPrec == NextTokPrec && isRightAssoc)) {
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000411 if (!RHS.isInvalid() && RHSIsInitList) {
412 Diag(Tok, diag::err_init_list_bin_op)
413 << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get());
414 RHS = ExprError();
Richard Smith5e4e58b2012-03-01 02:59:17 +0000415 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 // If this is left-associative, only parse things on the RHS that bind
417 // more tightly than the current operator. If it is left-associative, it
418 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
419 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000420 // The function takes ownership of the RHS.
Douglas Gregor200b2922010-09-17 22:25:06 +0000421 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000422 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000423 RHSIsInitList = false;
Douglas Gregor200b2922010-09-17 22:25:06 +0000424
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000425 if (RHS.isInvalid())
Douglas Gregor200b2922010-09-17 22:25:06 +0000426 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000427
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000428 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikie4e4d0842012-03-11 07:00:24 +0000429 getLangOpts().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 }
431 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000432
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000433 if (!RHS.isInvalid() && RHSIsInitList) {
Richard Smith5e4e58b2012-03-01 02:59:17 +0000434 if (ThisPrec == prec::Assignment) {
435 Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000436 << Actions.getExprRange(RHS.get());
Richard Smith5e4e58b2012-03-01 02:59:17 +0000437 } else {
438 Diag(OpToken, diag::err_init_list_bin_op)
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000439 << /*RHS*/1 << PP.getSpelling(OpToken)
440 << Actions.getExprRange(RHS.get());
Richard Smith5e4e58b2012-03-01 02:59:17 +0000441 LHS = ExprError();
442 }
443 }
444
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000445 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000446 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000447 if (TernaryMiddle.isInvalid()) {
448 // If we're using '>>' as an operator within a template
449 // argument list (in C++98), suggest the addition of
450 // parentheses so that the code remains well-formed in C++0x.
451 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
452 SuggestParentheses(OpToken.getLocation(),
453 diag::warn_cxx0x_right_shift_in_template_arg,
454 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
455 Actions.getExprRange(RHS.get()).getEnd()));
456
Douglas Gregor23c94db2010-07-02 17:43:08 +0000457 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000458 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000459 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000460 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000461 LHS.take(), TernaryMiddle.take(),
462 RHS.take());
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000463 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 }
465}
466
James Dennette30d3ff2012-06-17 04:36:28 +0000467/// \brief Parse a cast-expression, or, if \p isUnaryExpression is true,
468/// parse a unary-expression.
469///
470/// \p isAddressOfOperand exists because an id-expression that is the
471/// operand of address-of gets special treatment due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000472///
John McCall60d7b3a2010-08-24 06:29:42 +0000473ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000474 bool isAddressOfOperand,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000475 TypeCastState isTypeCast) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000476 bool NotCastExpr;
John McCall60d7b3a2010-08-24 06:29:42 +0000477 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor200b2922010-09-17 22:25:06 +0000478 isAddressOfOperand,
479 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +0000480 isTypeCast);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000481 if (NotCastExpr)
482 Diag(Tok, diag::err_expected_expression);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000483 return Res;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000484}
485
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000486namespace {
487class CastExpressionIdValidator : public CorrectionCandidateCallback {
488 public:
489 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
490 : AllowNonTypes(AllowNonTypes) {
491 WantTypeSpecifiers = AllowTypes;
492 }
493
494 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
495 NamedDecl *ND = candidate.getCorrectionDecl();
496 if (!ND)
497 return candidate.isKeyword();
498
499 if (isa<TypeDecl>(ND))
500 return WantTypeSpecifiers;
501 return AllowNonTypes;
502 }
503
504 private:
505 bool AllowNonTypes;
506};
507}
508
James Dennette30d3ff2012-06-17 04:36:28 +0000509/// \brief Parse a cast-expression, or, if \pisUnaryExpression is true, parse
510/// a unary-expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000511///
James Dennette30d3ff2012-06-17 04:36:28 +0000512/// \p isAddressOfOperand exists because an id-expression that is the operand
513/// of address-of gets special treatment due to member pointers. NotCastExpr
514/// is set to true if the token is not the start of a cast-expression, and no
515/// diagnostic is emitted in this case.
516///
517/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +0000518/// cast-expression: [C99 6.5.4]
519/// unary-expression
520/// '(' type-name ')' cast-expression
521///
522/// unary-expression: [C99 6.5.3]
523/// postfix-expression
524/// '++' unary-expression
525/// '--' unary-expression
526/// unary-operator cast-expression
527/// 'sizeof' unary-expression
528/// 'sizeof' '(' type-name ')'
Richard Smith99831e42012-03-06 03:21:47 +0000529/// [C++11] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000530/// [GNU] '__alignof' unary-expression
531/// [GNU] '__alignof' '(' type-name ')'
Jordan Rosef70a8862012-06-30 21:33:57 +0000532/// [C11] '_Alignof' '(' type-name ')'
Richard Smith99831e42012-03-06 03:21:47 +0000533/// [C++11] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000534/// [GNU] '&&' identifier
Richard Smith99831e42012-03-06 03:21:47 +0000535/// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7]
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000536/// [C++] new-expression
537/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000538///
539/// unary-operator: one of
540/// '&' '*' '+' '-' '~' '!'
541/// [GNU] '__extension__' '__real' '__imag'
542///
543/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000544/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000545/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000546/// constant
547/// string-literal
548/// [C++] boolean-literal [C++ 2.13.5]
Richard Smith99831e42012-03-06 03:21:47 +0000549/// [C++11] 'nullptr' [C++11 2.14.7]
550/// [C++11] user-defined-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000551/// '(' expression ')'
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000552/// [C11] generic-selection
Reid Spencer5f016e22007-07-11 17:01:13 +0000553/// '__func__' [C99 6.4.2.2]
554/// [GNU] '__FUNCTION__'
555/// [GNU] '__PRETTY_FUNCTION__'
556/// [GNU] '(' compound-statement ')'
557/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
558/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
559/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
560/// assign-expr ')'
561/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000562/// [GNU] '__null'
Mike Stump1eb44332009-09-09 15:08:12 +0000563/// [OBJC] '[' objc-message-expr ']'
James Dennett7a90c8b2012-06-15 06:52:33 +0000564/// [OBJC] '\@selector' '(' objc-selector-arg ')'
565/// [OBJC] '\@protocol' '(' identifier ')'
566/// [OBJC] '\@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000567/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000568/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smith99831e42012-03-06 03:21:47 +0000569/// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3]
Douglas Gregor2725ca82010-04-21 19:57:20 +0000570/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smith99831e42012-03-06 03:21:47 +0000571/// [C++11] typename-specifier braced-init-list [C++11 5.2.3]
Reid Spencer5f016e22007-07-11 17:01:13 +0000572/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
573/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
574/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
575/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000576/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
577/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000578/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000579/// [G++] unary-type-trait '(' type-id ')'
580/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley21ff2e52011-04-28 00:16:57 +0000581/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff296e8d52008-08-28 19:20:44 +0000582/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000583///
584/// constant: [C99 6.4.4]
585/// integer-constant
586/// floating-constant
587/// enumeration-constant -> identifier
588/// character-constant
589///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000590/// id-expression: [C++ 5.1]
591/// unqualified-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000592/// qualified-id
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000593///
594/// unqualified-id: [C++ 5.1]
595/// identifier
596/// operator-function-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000597/// conversion-function-id
598/// '~' class-name
599/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000600///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000601/// new-expression: [C++ 5.3.4]
602/// '::'[opt] 'new' new-placement[opt] new-type-id
603/// new-initializer[opt]
604/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
605/// new-initializer[opt]
606///
607/// delete-expression: [C++ 5.3.5]
608/// '::'[opt] 'delete' cast-expression
609/// '::'[opt] 'delete' '[' ']' cast-expression
610///
John Wiegley20c0da72011-04-27 23:09:49 +0000611/// [GNU/Embarcadero] unary-type-trait:
612/// '__is_arithmetic'
613/// '__is_floating_point'
614/// '__is_integral'
615/// '__is_lvalue_expr'
616/// '__is_rvalue_expr'
617/// '__is_complete_type'
618/// '__is_void'
619/// '__is_array'
620/// '__is_function'
621/// '__is_reference'
622/// '__is_lvalue_reference'
623/// '__is_rvalue_reference'
624/// '__is_fundamental'
625/// '__is_object'
626/// '__is_scalar'
627/// '__is_compound'
628/// '__is_pointer'
629/// '__is_member_object_pointer'
630/// '__is_member_function_pointer'
631/// '__is_member_pointer'
632/// '__is_const'
633/// '__is_volatile'
634/// '__is_trivial'
635/// '__is_standard_layout'
636/// '__is_signed'
637/// '__is_unsigned'
638///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000639/// [GNU] unary-type-trait:
Sebastian Redlc238f092010-08-31 04:59:00 +0000640/// '__has_nothrow_assign'
641/// '__has_nothrow_copy'
642/// '__has_nothrow_constructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000643/// '__has_trivial_assign' [TODO]
644/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000645/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000646/// '__has_trivial_destructor'
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000647/// '__has_virtual_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000648/// '__is_abstract' [TODO]
649/// '__is_class'
650/// '__is_empty' [TODO]
651/// '__is_enum'
Douglas Gregor5e9392b2011-12-03 18:14:24 +0000652/// '__is_final'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000653/// '__is_pod'
654/// '__is_polymorphic'
Chandler Carruthb7e95892011-04-23 10:47:28 +0000655/// '__is_trivial'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000656/// '__is_union'
657///
Sean Huntfeb375d2011-05-13 00:31:07 +0000658/// [Clang] unary-type-trait:
659/// '__trivially_copyable'
660///
Douglas Gregor9f361132011-01-27 20:28:01 +0000661/// binary-type-trait:
662/// [GNU] '__is_base_of'
663/// [MS] '__is_convertible_to'
John Wiegley20c0da72011-04-27 23:09:49 +0000664/// '__is_convertible'
665/// '__is_same'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000666///
John Wiegley21ff2e52011-04-28 00:16:57 +0000667/// [Embarcadero] array-type-trait:
668/// '__array_rank'
669/// '__array_extent'
670///
John Wiegley55262202011-04-25 06:54:41 +0000671/// [Embarcadero] expression-trait:
672/// '__is_lvalue_expr'
673/// '__is_rvalue_expr'
James Dennette30d3ff2012-06-17 04:36:28 +0000674/// \endverbatim
John Wiegley55262202011-04-25 06:54:41 +0000675///
John McCall60d7b3a2010-08-24 06:29:42 +0000676ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl02bc21a2010-09-10 20:55:37 +0000677 bool isAddressOfOperand,
678 bool &NotCastExpr,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000679 TypeCastState isTypeCast) {
John McCall60d7b3a2010-08-24 06:29:42 +0000680 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000682 NotCastExpr = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 // This handles all of cast-expression, unary-expression, postfix-expression,
685 // and primary-expression. We handle them together like this for efficiency
686 // and to simplify handling of an expression starting with a '(' token: which
687 // may be one of a parenthesized expression, cast-expression, compound literal
688 // expression, or statement expression.
689 //
690 // If the parsed tokens consist of a primary-expression, the cases below
John McCall9ae2f072010-08-23 23:25:46 +0000691 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
692 // to handle the postfix expression suffixes. Cases that cannot be followed
693 // by postfix exprs should return without invoking
694 // ParsePostfixExpressionSuffix.
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 switch (SavedKind) {
696 case tok::l_paren: {
697 // If this expression is limited to being a unary-expression, the parent can
698 // not start a cast expression.
699 ParenParseOption ParenExprType =
David Blaikie4e4d0842012-03-11 07:00:24 +0000700 (isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallb3d87482010-08-24 05:47:05 +0000701 ParsedType CastTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 SourceLocation RParenLoc;
Chris Lattner932dff72009-12-10 02:08:07 +0000703
704 {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000705 // The inside of the parens don't need to be a colon protected scope, and
706 // isn't immediately a message send.
Chris Lattner932dff72009-12-10 02:08:07 +0000707 ColonProtectionRAIIObject X(*this, false);
Douglas Gregor0fbda682010-09-15 14:51:05 +0000708
Chris Lattner932dff72009-12-10 02:08:07 +0000709 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000710 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner932dff72009-12-10 02:08:07 +0000711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 switch (ParenExprType) {
714 case SimpleExpr: break; // Nothing else to do.
715 case CompoundStmt: break; // Nothing else to do.
716 case CompoundLiteral:
717 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
718 // postfix-expression exist, parse them now.
719 break;
720 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000721 // We have parsed the cast-expression and no postfix-expr pieces are
722 // following.
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000723 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000725
John McCall9ae2f072010-08-23 23:25:46 +0000726 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000728
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 // primary-expression
730 case tok::numeric_constant:
731 // constant: integer-constant
732 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000733
Richard Smith36f5cfe2012-03-09 08:00:36 +0000734 Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000736 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000737
738 case tok::kw_true:
739 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000740 return ParseCXXBoolLiteral();
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000741
742 case tok::kw___objc_yes:
743 case tok::kw___objc_no:
744 return ParseObjCBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000745
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000746 case tok::kw_nullptr:
Richard Smith841804b2011-10-17 23:06:20 +0000747 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000748 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
749
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000750 case tok::annot_primary_expr:
751 assert(Res.get() == 0 && "Stray primary-expression annotation?");
752 Res = getExprAnnotation(Tok);
753 ConsumeToken();
754 break;
755
David Blaikie42d6d0c2011-12-04 05:04:18 +0000756 case tok::kw_decltype:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000757 case tok::identifier: { // primary-expression: identifier
758 // unqualified-id: identifier
759 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000760 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000761 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
David Blaikie4e4d0842012-03-11 07:00:24 +0000762 if (getLangOpts().CPlusPlus) {
John McCallb6727072010-01-07 19:29:58 +0000763 // Avoid the unnecessary parse-time lookup in the common case
764 // where the syntax forbids a type.
765 const Token &Next = NextToken();
Douglas Gregord2959702012-08-30 20:04:43 +0000766
767 // If this identifier was reverted from a token ID, and the next token
768 // is a parenthesis, this is likely to be a use of a type trait. Check
769 // those tokens.
770 if (Next.is(tok::l_paren) &&
771 Tok.is(tok::identifier) &&
772 Tok.getIdentifierInfo()->hasRevertedTokenIDToIdentifier()) {
773 IdentifierInfo *II = Tok.getIdentifierInfo();
774 // Build up the mapping of revertable type traits, for future use.
775 if (RevertableTypeTraits.empty()) {
Douglas Gregord2959702012-08-30 20:04:43 +0000776#define RTT_JOIN(X,Y) X##Y
777#define REVERTABLE_TYPE_TRAIT(Name) \
778 RevertableTypeTraits[PP.getIdentifierInfo(#Name)] \
779 = RTT_JOIN(tok::kw_,Name)
780
781 REVERTABLE_TYPE_TRAIT(__is_arithmetic);
782 REVERTABLE_TYPE_TRAIT(__is_convertible);
783 REVERTABLE_TYPE_TRAIT(__is_empty);
784 REVERTABLE_TYPE_TRAIT(__is_floating_point);
785 REVERTABLE_TYPE_TRAIT(__is_function);
786 REVERTABLE_TYPE_TRAIT(__is_fundamental);
787 REVERTABLE_TYPE_TRAIT(__is_integral);
788 REVERTABLE_TYPE_TRAIT(__is_member_function_pointer);
789 REVERTABLE_TYPE_TRAIT(__is_member_pointer);
790 REVERTABLE_TYPE_TRAIT(__is_pod);
791 REVERTABLE_TYPE_TRAIT(__is_pointer);
792 REVERTABLE_TYPE_TRAIT(__is_same);
793 REVERTABLE_TYPE_TRAIT(__is_scalar);
794 REVERTABLE_TYPE_TRAIT(__is_signed);
795 REVERTABLE_TYPE_TRAIT(__is_unsigned);
796 REVERTABLE_TYPE_TRAIT(__is_void);
797#undef REVERTABLE_TYPE_TRAIT
Douglas Gregord2959702012-08-30 20:04:43 +0000798#undef RTT_JOIN
799 }
800
801 // If we find that this is in fact the name of a type trait,
802 // update the token kind in place and parse again to treat it as
803 // the appropriate kind of type trait.
804 llvm::SmallDenseMap<IdentifierInfo *, tok::TokenKind>::iterator Known
805 = RevertableTypeTraits.find(II);
806 if (Known != RevertableTypeTraits.end()) {
807 Tok.setKind(Known->second);
808 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
809 NotCastExpr, isTypeCast);
810 }
811 }
812
John McCallb6727072010-01-07 19:29:58 +0000813 if (Next.is(tok::coloncolon) ||
814 (!ColonIsSacred && Next.is(tok::colon)) ||
815 Next.is(tok::less) ||
Sebastian Redl62f13c92011-12-22 18:58:29 +0000816 Next.is(tok::l_paren) ||
817 Next.is(tok::l_brace)) {
John McCallb6727072010-01-07 19:29:58 +0000818 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
819 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000820 return ExprError();
821 if (!Tok.is(tok::identifier))
John McCallb6727072010-01-07 19:29:58 +0000822 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
823 }
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000824 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000825
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000826 // Consume the identifier so that we can see if it is followed by a '(' or
827 // '.'.
828 IdentifierInfo &II = *Tok.getIdentifierInfo();
829 SourceLocation ILoc = ConsumeToken();
Douglas Gregord2959702012-08-30 20:04:43 +0000830
Chris Lattnereb483eb2010-04-11 08:28:14 +0000831 // Support 'Class.property' and 'super.property' notation.
David Blaikie4e4d0842012-03-11 07:00:24 +0000832 if (getLangOpts().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000833 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattner236beab2010-04-12 06:20:33 +0000834 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000835 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000836 ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000837
Douglas Gregor8f70bda2012-02-16 18:19:22 +0000838 // Allow either an identifier or the keyword 'class' (in C++).
839 if (Tok.isNot(tok::identifier) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000840 !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000841 Diag(Tok, diag::err_expected_property_name);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000842 return ExprError();
843 }
844 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
845 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000846
847 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
848 ILoc, PropertyLoc);
John McCall9ae2f072010-08-23 23:25:46 +0000849 break;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000850 }
John McCall9c72c602010-08-27 09:08:28 +0000851
Douglas Gregorfa885c12010-09-15 15:09:43 +0000852 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregor78edf512010-09-15 16:23:04 +0000853 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregorfa885c12010-09-15 15:09:43 +0000854 // that identifier, this is probably a message send with a missing open
Douglas Gregor78edf512010-09-15 16:23:04 +0000855 // bracket. Treat it as such.
David Blaikie4e4d0842012-03-11 07:00:24 +0000856 if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregorfa885c12010-09-15 15:09:43 +0000857 getCurScope()->isInObjcMethodScope() &&
Douglas Gregor78edf512010-09-15 16:23:04 +0000858 ((Tok.is(tok::identifier) &&
859 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
860 Tok.is(tok::code_completion))) {
Douglas Gregorfa885c12010-09-15 15:09:43 +0000861 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
862 0);
863 break;
864 }
865
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000866 // If we have an Objective-C class name followed by an identifier
867 // and either ':' or ']', this is an Objective-C class message
868 // send that's missing the opening '['. Recovery
869 // appropriately. Also take this path if we're performing code
870 // completion after an Objective-C class name.
David Blaikie4e4d0842012-03-11 07:00:24 +0000871 if (getLangOpts().ObjC1 &&
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000872 ((Tok.is(tok::identifier) && !InMessageExpression) ||
873 Tok.is(tok::code_completion))) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000874 const Token& Next = NextToken();
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000875 if (Tok.is(tok::code_completion) ||
876 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif9fe871a2010-09-17 10:21:45 +0000877 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
878 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000879 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000880 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000881 DS.SetRangeStart(ILoc);
882 DS.SetRangeEnd(ILoc);
883 const char *PrevSpec = 0;
884 unsigned DiagID;
Gabor Greif9fe871a2010-09-17 10:21:45 +0000885 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregor9497a732010-09-16 01:51:54 +0000886
887 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
888 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
889 DeclaratorInfo);
890 if (Ty.isInvalid())
891 break;
892
893 Res = ParseObjCMessageExpressionBody(SourceLocation(),
894 SourceLocation(),
895 Ty.get(), 0);
896 break;
897 }
898 }
899
John McCall9c72c602010-08-27 09:08:28 +0000900 // Make sure to pass down the right value for isAddressOfOperand.
901 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
902 isAddressOfOperand = false;
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000903
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
905 // need to know whether or not this identifier is a function designator or
906 // not.
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000907 UnqualifiedId Name;
908 CXXScopeSpec ScopeSpec;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000909 SourceLocation TemplateKWLoc;
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000910 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
911 isTypeCast != IsTypeCast);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000912 Name.setIdentifier(&II, ILoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000913 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
914 Name, Tok.is(tok::l_paren),
915 isAddressOfOperand, &Validator);
John McCall9ae2f072010-08-23 23:25:46 +0000916 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 }
918 case tok::char_constant: // constant: character-constant
Douglas Gregor5cee1192011-07-27 05:40:30 +0000919 case tok::wide_char_constant:
920 case tok::utf16_char_constant:
921 case tok::utf32_char_constant:
Richard Smith36f5cfe2012-03-09 08:00:36 +0000922 Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000924 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
926 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Nico Weber28ad0632012-06-23 02:07:59 +0000927 case tok::kw_L__FUNCTION__: // primary-expression: L__FUNCTION__ [MS]
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000929 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000931 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 case tok::string_literal: // primary-expression: string-literal
933 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000934 case tok::utf8_string_literal:
935 case tok::utf16_string_literal:
936 case tok::utf32_string_literal:
Richard Smith99831e42012-03-06 03:21:47 +0000937 Res = ParseStringLiteralExpression(true);
John McCall9ae2f072010-08-23 23:25:46 +0000938 break;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000939 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbournef111d932011-04-15 00:35:48 +0000940 Res = ParseGenericSelectionExpression();
941 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 case tok::kw___builtin_va_arg:
943 case tok::kw___builtin_offsetof:
944 case tok::kw___builtin_choose_expr:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000945 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redld8c4e152008-12-11 22:33:27 +0000946 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000947 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000948 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth3c7fddd2011-07-08 04:59:44 +0000949
Douglas Gregord4206632010-08-06 14:50:36 +0000950 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
951 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
952 // C++ [expr.unary] has:
953 // unary-expression:
954 // ++ cast-expression
955 // -- cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 SourceLocation SavedLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +0000957 Res = ParseCastExpression(!getLangOpts().CPlusPlus);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000958 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000959 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000960 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000962 case tok::amp: { // unary-expression: '&' cast-expression
963 // Special treatment because of member pointers
964 SourceLocation SavedLoc = ConsumeToken();
965 Res = ParseCastExpression(false, true);
966 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000967 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000968 return Res;
Sebastian Redlebc07d52009-02-03 20:19:35 +0000969 }
970
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 case tok::star: // unary-expression: '*' cast-expression
972 case tok::plus: // unary-expression: '+' cast-expression
973 case tok::minus: // unary-expression: '-' cast-expression
974 case tok::tilde: // unary-expression: '~' cast-expression
975 case tok::exclaim: // unary-expression: '!' cast-expression
976 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000977 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 SourceLocation SavedLoc = ConsumeToken();
979 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000980 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000981 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000982 return Res;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000983 }
984
Chris Lattner35080842008-02-02 20:20:10 +0000985 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
986 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000987 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000988 SourceLocation SavedLoc = ConsumeToken();
989 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000990 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000991 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000992 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 }
Jordan Rosef70a8862012-06-30 21:33:57 +0000994 case tok::kw__Alignof: // unary-expression: '_Alignof' '(' type-name ')'
995 if (!getLangOpts().C11)
996 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
997 // fallthrough
998 case tok::kw_alignof: // unary-expression: 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
1000 // unary-expression: '__alignof' '(' type-name ')'
Jordan Rosef70a8862012-06-30 21:33:57 +00001001 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
1002 // unary-expression: 'sizeof' '(' type-name ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001003 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
1004 return ParseUnaryExprOrTypeTraitExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 case tok::ampamp: { // unary-expression: '&&' identifier
1006 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001007 if (Tok.isNot(tok::identifier))
1008 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +00001009
Chris Lattnerfebb5b82011-02-18 21:16:39 +00001010 if (getCurScope()->getFnParent() == 0)
1011 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
1012
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattner337e5502011-02-18 01:27:55 +00001014 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1015 Tok.getLocation());
1016 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 ConsumeToken();
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001018 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 }
1020 case tok::kw_const_cast:
1021 case tok::kw_dynamic_cast:
1022 case tok::kw_reinterpret_cast:
1023 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +00001024 Res = ParseCXXCasts();
John McCall9ae2f072010-08-23 23:25:46 +00001025 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001026 case tok::kw_typeid:
1027 Res = ParseCXXTypeid();
John McCall9ae2f072010-08-23 23:25:46 +00001028 break;
Francois Pichet01b7c302010-09-08 12:20:18 +00001029 case tok::kw___uuidof:
1030 Res = ParseCXXUuidof();
1031 break;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001032 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +00001033 Res = ParseCXXThis();
John McCall9ae2f072010-08-23 23:25:46 +00001034 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001035
Douglas Gregor9497a732010-09-16 01:51:54 +00001036 case tok::annot_typename:
1037 if (isStartOfObjCClassMessageMissingOpenBracket()) {
1038 ParsedType Type = getTypeAnnotation(Tok);
1039
1040 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +00001041 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +00001042 DS.SetRangeStart(Tok.getLocation());
1043 DS.SetRangeEnd(Tok.getLastLoc());
1044
1045 const char *PrevSpec = 0;
1046 unsigned DiagID;
Nico Weber253e80b2010-11-22 10:30:56 +00001047 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
1048 PrevSpec, DiagID, Type);
Douglas Gregor9497a732010-09-16 01:51:54 +00001049
1050 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1051 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1052 if (Ty.isInvalid())
1053 break;
1054
1055 ConsumeToken();
1056 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1057 Ty.get(), 0);
1058 break;
1059 }
1060 // Fall through
1061
David Blaikie5e089fe2012-01-24 05:47:35 +00001062 case tok::annot_decltype:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001063 case tok::kw_char:
1064 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001065 case tok::kw_char16_t:
1066 case tok::kw_char32_t:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001067 case tok::kw_bool:
1068 case tok::kw_short:
1069 case tok::kw_int:
1070 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00001071 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00001072 case tok::kw___int128:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001073 case tok::kw_signed:
1074 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001075 case tok::kw_half:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001076 case tok::kw_float:
1077 case tok::kw_double:
1078 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +00001079 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +00001080 case tok::kw_typeof:
Douglas Gregor9497a732010-09-16 01:51:54 +00001081 case tok::kw___vector: {
David Blaikie4e4d0842012-03-11 07:00:24 +00001082 if (!getLangOpts().CPlusPlus) {
Chris Lattner2dcaab32009-01-04 22:28:21 +00001083 Diag(Tok, diag::err_expected_expression);
1084 return ExprError();
1085 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +00001086
1087 if (SavedKind == tok::kw_typename) {
1088 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001089 // typename-specifier braced-init-list
John McCall9ba61662010-02-26 08:45:28 +00001090 if (TryAnnotateTypeOrScopeToken())
Eli Friedman2e0cdb42009-06-11 00:33:41 +00001091 return ExprError();
1092 }
1093
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001094 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001095 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001096 //
John McCall0b7e6782011-03-24 11:26:52 +00001097 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001098 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001099 if (Tok.isNot(tok::l_paren) &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001100 (!getLangOpts().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001101 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1102 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001103
Richard Smith7fe62082011-10-15 05:09:34 +00001104 if (Tok.is(tok::l_brace))
1105 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1106
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001107 Res = ParseCXXTypeConstructExpression(DS);
John McCall9ae2f072010-08-23 23:25:46 +00001108 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001109 }
1110
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001111 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor4074eef2010-04-23 02:08:13 +00001112 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1113 // (We can end up in this situation after tentative parsing.)
1114 if (TryAnnotateTypeOrScopeToken())
1115 return ExprError();
1116 if (!Tok.is(tok::annot_cxxscope))
1117 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001118 NotCastExpr, isTypeCast);
Douglas Gregor4074eef2010-04-23 02:08:13 +00001119
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001120 Token Next = NextToken();
1121 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001122 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001123 if (TemplateId->Kind == TNK_Type_template) {
1124 // We have a qualified template-id that we know refers to a
1125 // type, translate it into a type and continue parsing as a
1126 // cast expression.
1127 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001128 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1129 /*EnteringContext=*/false);
Douglas Gregor059101f2011-03-02 00:47:37 +00001130 AnnotateTemplateIdTokenAsType();
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001131 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001132 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001133 }
1134 }
1135
1136 // Parse as an id-expression.
1137 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001138 break;
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001139 }
1140
1141 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001142 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001143 if (TemplateId->Kind == TNK_Type_template) {
1144 // We have a template-id that we know refers to a type,
1145 // translate it into a type and continue parsing as a cast
1146 // expression.
1147 AnnotateTemplateIdTokenAsType();
1148 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001149 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001150 }
1151
1152 // Fall through to treat the template-id as an id-expression.
1153 }
1154
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001155 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redlebc07d52009-02-03 20:19:35 +00001156 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001157 break;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001158
Chris Lattner74ba4102009-01-04 22:52:14 +00001159 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +00001160 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1161 // annotates the token, tail recurse.
1162 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001163 return ExprError();
1164 if (!Tok.is(tok::coloncolon))
Sebastian Redlebc07d52009-02-03 20:19:35 +00001165 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1166
Chris Lattner74ba4102009-01-04 22:52:14 +00001167 // ::new -> [C++] new-expression
1168 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +00001169 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +00001170 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +00001171 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +00001172 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +00001173 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001175 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +00001176 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001177 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +00001178 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +00001179
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001180 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001181 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001182
1183 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001184 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001185
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001186 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smith841804b2011-10-17 23:06:20 +00001187 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001188 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001189 BalancedDelimiterTracker T(*this, tok::l_paren);
1190
1191 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001192 return ExprError();
Richard Smithf6702a32011-12-20 02:08:33 +00001193 // C++11 [expr.unary.noexcept]p1:
Sebastian Redlbd7c8492010-09-10 21:57:27 +00001194 // The noexcept operator determines whether the evaluation of its operand,
1195 // which is an unevaluated operand, can throw an exception.
1196 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001197 ExprResult Result = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001198
1199 T.consumeClose();
1200
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001201 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001202 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1203 Result.take(), T.getCloseLocation());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001204 return Result;
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001205 }
1206
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001207 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redl64b45f72009-01-05 20:52:13 +00001208 case tok::kw___is_class:
Eli Friedman1d954f62009-08-15 21:55:26 +00001209 case tok::kw___is_empty:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001210 case tok::kw___is_enum:
Sebastian Redlccf43502009-12-03 00:13:20 +00001211 case tok::kw___is_literal:
John Wiegley20c0da72011-04-27 23:09:49 +00001212 case tok::kw___is_arithmetic:
1213 case tok::kw___is_integral:
1214 case tok::kw___is_floating_point:
1215 case tok::kw___is_complete_type:
1216 case tok::kw___is_void:
1217 case tok::kw___is_array:
1218 case tok::kw___is_function:
1219 case tok::kw___is_reference:
1220 case tok::kw___is_lvalue_reference:
1221 case tok::kw___is_rvalue_reference:
1222 case tok::kw___is_fundamental:
1223 case tok::kw___is_object:
1224 case tok::kw___is_scalar:
1225 case tok::kw___is_compound:
1226 case tok::kw___is_pointer:
1227 case tok::kw___is_member_object_pointer:
1228 case tok::kw___is_member_function_pointer:
1229 case tok::kw___is_member_pointer:
1230 case tok::kw___is_const:
1231 case tok::kw___is_volatile:
1232 case tok::kw___is_standard_layout:
1233 case tok::kw___is_signed:
1234 case tok::kw___is_unsigned:
Chandler Carruth38402812011-04-24 02:49:28 +00001235 case tok::kw___is_literal_type:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001236 case tok::kw___is_pod:
1237 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +00001238 case tok::kw___is_trivial:
Sean Huntfeb375d2011-05-13 00:31:07 +00001239 case tok::kw___is_trivially_copyable:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001240 case tok::kw___is_union:
Douglas Gregor5e9392b2011-12-03 18:14:24 +00001241 case tok::kw___is_final:
Anders Carlsson347ba892009-04-16 00:08:20 +00001242 case tok::kw___has_trivial_constructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001243 case tok::kw___has_trivial_copy:
1244 case tok::kw___has_trivial_assign:
Anders Carlsson072abef2009-04-17 02:34:54 +00001245 case tok::kw___has_trivial_destructor:
Sebastian Redlc238f092010-08-31 04:59:00 +00001246 case tok::kw___has_nothrow_assign:
1247 case tok::kw___has_nothrow_copy:
1248 case tok::kw___has_nothrow_constructor:
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001249 case tok::kw___has_virtual_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +00001250 return ParseUnaryTypeTrait();
1251
Francois Pichetf1872372010-12-08 22:35:30 +00001252 case tok::kw___builtin_types_compatible_p:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001253 case tok::kw___is_base_of:
John Wiegley20c0da72011-04-27 23:09:49 +00001254 case tok::kw___is_same:
1255 case tok::kw___is_convertible:
Douglas Gregor9f361132011-01-27 20:28:01 +00001256 case tok::kw___is_convertible_to:
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00001257 case tok::kw___is_trivially_assignable:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001258 return ParseBinaryTypeTrait();
1259
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001260 case tok::kw___is_trivially_constructible:
1261 return ParseTypeTrait();
1262
John Wiegley21ff2e52011-04-28 00:16:57 +00001263 case tok::kw___array_rank:
1264 case tok::kw___array_extent:
1265 return ParseArrayTypeTrait();
1266
John Wiegley55262202011-04-25 06:54:41 +00001267 case tok::kw___is_lvalue_expr:
1268 case tok::kw___is_rvalue_expr:
1269 return ParseExpressionTrait();
1270
Chris Lattnerc97c2042007-10-03 22:03:06 +00001271 case tok::at: {
1272 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001273 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +00001274 }
Steve Naroff296e8d52008-08-28 19:20:44 +00001275 case tok::caret:
Chandler Carruthbb399022011-07-08 04:28:55 +00001276 Res = ParseBlockLiteralExpression();
1277 break;
1278 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001279 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001280 cutOffParsing();
1281 return ExprError();
Chandler Carruthbb399022011-07-08 04:28:55 +00001282 }
Chris Lattnerfdb548e2008-12-12 19:20:14 +00001283 case tok::l_square:
David Blaikie4e4d0842012-03-11 07:00:24 +00001284 if (getLangOpts().CPlusPlus0x) {
1285 if (getLangOpts().ObjC1) {
Eli Friedmandc3b7232012-01-04 02:40:39 +00001286 // C++11 lambda expressions and Objective-C message sends both start with a
1287 // square bracket. There are three possibilities here:
1288 // we have a valid lambda expression, we have an invalid lambda
1289 // expression, or we have something that doesn't appear to be a lambda.
1290 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregorae7902c2011-08-04 15:30:47 +00001291 Res = TryParseLambdaExpression();
Eli Friedmandc3b7232012-01-04 02:40:39 +00001292 if (!Res.isInvalid() && !Res.get())
Douglas Gregorae7902c2011-08-04 15:30:47 +00001293 Res = ParseObjCMessageExpression();
1294 break;
1295 }
1296 Res = ParseLambdaExpression();
1297 break;
1298 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001299 if (getLangOpts().ObjC1) {
Chandler Carruthbb399022011-07-08 04:28:55 +00001300 Res = ParseObjCMessageExpression();
1301 break;
1302 }
1303 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001305 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001306 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001308
John McCall9ae2f072010-08-23 23:25:46 +00001309 // These can be followed by postfix-expr pieces.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001310 return ParsePostfixExpressionSuffix(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001311}
1312
James Dennette30d3ff2012-06-17 04:36:28 +00001313/// \brief Once the leading part of a postfix-expression is parsed, this
1314/// method parses any suffixes that apply.
Reid Spencer5f016e22007-07-11 17:01:13 +00001315///
James Dennette30d3ff2012-06-17 04:36:28 +00001316/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001317/// postfix-expression: [C99 6.5.2]
1318/// primary-expression
1319/// postfix-expression '[' expression ']'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001320/// postfix-expression '[' braced-init-list ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001321/// postfix-expression '(' argument-expression-list[opt] ')'
1322/// postfix-expression '.' identifier
1323/// postfix-expression '->' identifier
1324/// postfix-expression '++'
1325/// postfix-expression '--'
1326/// '(' type-name ')' '{' initializer-list '}'
1327/// '(' type-name ')' '{' initializer-list ',' '}'
1328///
1329/// argument-expression-list: [C99 6.5.2]
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00001330/// argument-expression ...[opt]
1331/// argument-expression-list ',' assignment-expression ...[opt]
James Dennette30d3ff2012-06-17 04:36:28 +00001332/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00001333ExprResult
1334Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 // Now that the primary-expression piece of the postfix-expression has been
1336 // parsed, see if there are any postfix-expression pieces here.
1337 SourceLocation Loc;
1338 while (1) {
1339 switch (Tok.getKind()) {
Douglas Gregor78edf512010-09-15 16:23:04 +00001340 case tok::code_completion:
1341 if (InMessageExpression)
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001342 return LHS;
Douglas Gregor78edf512010-09-15 16:23:04 +00001343
Douglas Gregorac5fd842010-09-18 01:28:11 +00001344 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001345 cutOffParsing();
1346 return ExprError();
Douglas Gregor78edf512010-09-15 16:23:04 +00001347
Douglas Gregor0fbda682010-09-15 14:51:05 +00001348 case tok::identifier:
1349 // If we see identifier: after an expression, and we're not already in a
1350 // message send, then this is probably a message send with a missing
1351 // opening bracket '['.
David Blaikie4e4d0842012-03-11 07:00:24 +00001352 if (getLangOpts().ObjC1 && !InMessageExpression &&
Douglas Gregorb65042d2010-09-15 14:54:45 +00001353 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001354 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1355 ParsedType(), LHS.get());
1356 break;
1357 }
1358
1359 // Fall through; this isn't a message send.
1360
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 default: // Not a postfix-expression suffix.
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001362 return LHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattnerc59cb382010-05-31 18:18:22 +00001364 // If we have a array postfix expression that starts on a new line and
1365 // Objective-C is enabled, it is highly likely that the user forgot a
1366 // semicolon after the base expression and that the array postfix-expr is
1367 // actually another message send. In this case, do some look-ahead to see
1368 // if the contents of the square brackets are obviously not a valid
1369 // expression and recover by pretending there is no suffix.
David Blaikie4e4d0842012-03-11 07:00:24 +00001370 if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
Chris Lattnerc59cb382010-05-31 18:18:22 +00001371 isSimpleObjCMessageExpression())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001372 return LHS;
Richard Smith6ee326a2012-04-10 01:32:12 +00001373
1374 // Reject array indices starting with a lambda-expression. '[[' is
1375 // reserved for attributes.
1376 if (CheckProhibitedCXX11Attribute())
1377 return ExprError();
1378
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001379 BalancedDelimiterTracker T(*this, tok::l_square);
1380 T.consumeOpen();
1381 Loc = T.getOpenLocation();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001382 ExprResult Idx;
David Blaikie4e4d0842012-03-11 07:00:24 +00001383 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00001384 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001385 Idx = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001386 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001387 Idx = ParseExpression();
Sebastian Redla55e52c2008-11-25 22:21:31 +00001388
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001390
1391 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCall9ae2f072010-08-23 23:25:46 +00001392 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1393 Idx.take(), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001394 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001395 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001396
1397 // Match the ']'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001398 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 break;
1400 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001401
Peter Collingbournebf36e252011-02-09 21:12:02 +00001402 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1403 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1404 // '(' argument-expression-list[opt] ')'
1405 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregor0fbda682010-09-15 14:51:05 +00001406 InMessageExpressionRAIIObject InMessage(*this, false);
1407
Peter Collingbournebf36e252011-02-09 21:12:02 +00001408 Expr *ExecConfig = 0;
1409
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001410 BalancedDelimiterTracker PT(*this, tok::l_paren);
1411
Peter Collingbournebf36e252011-02-09 21:12:02 +00001412 if (OpKind == tok::lesslessless) {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001413 ExprVector ExecConfigExprs;
Peter Collingbournebf36e252011-02-09 21:12:02 +00001414 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001415 SourceLocation OpenLoc = ConsumeToken();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001416
1417 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1418 LHS = ExprError();
1419 }
1420
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001421 SourceLocation CloseLoc = Tok.getLocation();
1422 if (Tok.is(tok::greatergreatergreater)) {
1423 ConsumeToken();
1424 } else if (LHS.isInvalid()) {
Peter Collingbournebf36e252011-02-09 21:12:02 +00001425 SkipUntil(tok::greatergreatergreater);
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001426 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001427 // There was an error closing the brackets
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001428 Diag(Tok, diag::err_expected_ggg);
1429 Diag(OpenLoc, diag::note_matching) << "<<<";
1430 SkipUntil(tok::greatergreatergreater);
Peter Collingbournebf36e252011-02-09 21:12:02 +00001431 LHS = ExprError();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001432 }
1433
1434 if (!LHS.isInvalid()) {
1435 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1436 LHS = ExprError();
1437 else
1438 Loc = PrevTokLocation;
1439 }
1440
1441 if (!LHS.isInvalid()) {
1442 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001443 OpenLoc,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001444 ExecConfigExprs,
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001445 CloseLoc);
Peter Collingbournebf36e252011-02-09 21:12:02 +00001446 if (ECResult.isInvalid())
1447 LHS = ExprError();
1448 else
1449 ExecConfig = ECResult.get();
1450 }
1451 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001452 PT.consumeOpen();
1453 Loc = PT.getOpenLocation();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001454 }
1455
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001456 ExprVector ArgExprs;
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001457 CommaLocsTy CommaLocs;
Douglas Gregorac5fd842010-09-18 01:28:11 +00001458
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001459 if (Tok.is(tok::code_completion)) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00001460 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1461 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001462 cutOffParsing();
1463 return ExprError();
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001464 }
Peter Collingbournebf36e252011-02-09 21:12:02 +00001465
1466 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1467 if (Tok.isNot(tok::r_paren)) {
1468 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1469 LHS.get())) {
Peter Collingbournebf36e252011-02-09 21:12:02 +00001470 LHS = ExprError();
1471 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001472 }
1473 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001474
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 // Match the ')'.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001476 if (LHS.isInvalid()) {
1477 SkipUntil(tok::r_paren);
1478 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001479 PT.consumeClose();
Douglas Gregorac5fd842010-09-18 01:28:11 +00001480 LHS = ExprError();
1481 } else {
1482 assert((ArgExprs.size() == 0 ||
1483 ArgExprs.size()-1 == CommaLocs.size())&&
Reid Spencer5f016e22007-07-11 17:01:13 +00001484 "Unexpected number of commas!");
John McCall9ae2f072010-08-23 23:25:46 +00001485 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001486 ArgExprs, Tok.getLocation(),
Peter Collingbournebf36e252011-02-09 21:12:02 +00001487 ExecConfig);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001488 PT.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001489 }
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 break;
1492 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001493 case tok::arrow:
1494 case tok::period: {
1495 // postfix-expression: p-e '->' template[opt] id-expression
1496 // postfix-expression: p-e '.' template[opt] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001497 tok::TokenKind OpKind = Tok.getKind();
1498 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001499
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001500 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001501 ParsedType ObjectType;
Douglas Gregord4dca082010-02-24 18:44:31 +00001502 bool MayBePseudoDestructor = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001503 if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
John McCall9ae2f072010-08-23 23:25:46 +00001504 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregord4dca082010-02-24 18:44:31 +00001505 OpLoc, OpKind, ObjectType,
1506 MayBePseudoDestructor);
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001507 if (LHS.isInvalid())
1508 break;
Douglas Gregord4dca082010-02-24 18:44:31 +00001509
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001510 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1511 /*EnteringContext=*/false,
Douglas Gregord4dca082010-02-24 18:44:31 +00001512 &MayBePseudoDestructor);
Douglas Gregor9f716e42010-05-27 15:25:59 +00001513 if (SS.isNotEmpty())
John McCallb3d87482010-08-24 05:47:05 +00001514 ObjectType = ParsedType();
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001515 }
1516
Douglas Gregor81b747b2009-09-17 21:32:03 +00001517 if (Tok.is(tok::code_completion)) {
1518 // Code completion for a member access expression.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001519 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor81b747b2009-09-17 21:32:03 +00001520 OpLoc, OpKind == tok::arrow);
1521
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001522 cutOffParsing();
1523 return ExprError();
Douglas Gregor81b747b2009-09-17 21:32:03 +00001524 }
1525
John McCall9ae2f072010-08-23 23:25:46 +00001526 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1527 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregord4dca082010-02-24 18:44:31 +00001528 ObjectType);
1529 break;
1530 }
1531
1532 // Either the action has told is that this cannot be a
1533 // pseudo-destructor expression (based on the type of base
1534 // expression), or we didn't see a '~' in the right place. We
1535 // can still parse a destructor name here, but in that case it
1536 // names a real destructor.
Francois Pichetdbee3412011-01-18 05:04:39 +00001537 // Allow explicit constructor calls in Microsoft mode.
1538 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001539 SourceLocation TemplateKWLoc;
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001540 UnqualifiedId Name;
David Blaikie4e4d0842012-03-11 07:00:24 +00001541 if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
Douglas Gregor8f70bda2012-02-16 18:19:22 +00001542 // Objective-C++:
1543 // After a '.' in a member access expression, treat the keyword
1544 // 'class' as if it were an identifier.
1545 //
1546 // This hack allows property access to the 'class' method because it is
1547 // such a common method name. For other C++ keywords that are
1548 // Objective-C method names, one must use the message send syntax.
1549 IdentifierInfo *Id = Tok.getIdentifierInfo();
1550 SourceLocation Loc = ConsumeToken();
1551 Name.setIdentifier(Id, Loc);
1552 } else if (ParseUnqualifiedId(SS,
1553 /*EnteringContext=*/false,
1554 /*AllowDestructorName=*/true,
1555 /*AllowConstructorName=*/
David Blaikie4e4d0842012-03-11 07:00:24 +00001556 getLangOpts().MicrosoftExt,
Douglas Gregor8f70bda2012-02-16 18:19:22 +00001557 ObjectType, TemplateKWLoc, Name))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001558 LHS = ExprError();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001559
1560 if (!LHS.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001561 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001562 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001563 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1564 Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 break;
1566 }
1567 case tok::plusplus: // postfix-expression: postfix-expression '++'
1568 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001569 if (!LHS.isInvalid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001570 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001571 Tok.getKind(), LHS.take());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001572 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 ConsumeToken();
1574 break;
1575 }
1576 }
1577}
1578
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001579/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1580/// vec_step and we are at the start of an expression or a parenthesized
1581/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1582/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001583///
James Dennette30d3ff2012-06-17 04:36:28 +00001584/// \verbatim
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001585/// unary-expression: [C99 6.5.3]
1586/// 'sizeof' unary-expression
1587/// 'sizeof' '(' type-name ')'
1588/// [GNU] '__alignof' unary-expression
1589/// [GNU] '__alignof' '(' type-name ')'
Jordan Rosef70a8862012-06-30 21:33:57 +00001590/// [C11] '_Alignof' '(' type-name ')'
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001591/// [C++0x] 'alignof' '(' type-id ')'
1592///
1593/// [GNU] typeof-specifier:
1594/// typeof ( expressions )
1595/// typeof ( type-name )
1596/// [GNU/C++] typeof unary-expression
1597///
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001598/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1599/// vec_step ( expressions )
1600/// vec_step ( type-name )
James Dennette30d3ff2012-06-17 04:36:28 +00001601/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00001602ExprResult
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001603Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1604 bool &isCastExpr,
1605 ParsedType &CastTy,
1606 SourceRange &CastRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00001607
1608 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001609 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
Jordan Rosef70a8862012-06-30 21:33:57 +00001610 OpTok.is(tok::kw__Alignof) || OpTok.is(tok::kw_vec_step)) &&
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001611 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001612
John McCall60d7b3a2010-08-24 06:29:42 +00001613 ExprResult Operand;
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001615 // If the operand doesn't start with an '(', it must be an expression.
1616 if (Tok.isNot(tok::l_paren)) {
1617 isCastExpr = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001618 if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001619 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1620 return ExprError();
1621 }
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001623 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001624 } else {
1625 // If it starts with a '(', we know that it is either a parenthesized
1626 // type-name, or it is a unary-expression that starts with a compound
1627 // literal, or starts with a primary-expression that is a parenthesized
1628 // expression.
1629 ParenParseOption ExprType = CastExpr;
1630 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001632 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001633 false, CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001634 CastRange = SourceRange(LParenLoc, RParenLoc);
1635
1636 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1637 // a type.
1638 if (ExprType == CastExpr) {
1639 isCastExpr = true;
1640 return ExprEmpty();
1641 }
1642
David Blaikie4e4d0842012-03-11 07:00:24 +00001643 if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001644 // GNU typeof in C requires the expression to be parenthesized. Not so for
1645 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1646 // the start of a unary-expression, but doesn't include any postfix
1647 // pieces. Parse these now if present.
John McCall124300e2010-08-24 23:41:43 +00001648 if (!Operand.isInvalid())
1649 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001650 }
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001651 }
1652
1653 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1654 isCastExpr = false;
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001655 return Operand;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001656}
1657
Reid Spencer5f016e22007-07-11 17:01:13 +00001658
James Dennette30d3ff2012-06-17 04:36:28 +00001659/// \brief Parse a sizeof or alignof expression.
1660///
1661/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001662/// unary-expression: [C99 6.5.3]
1663/// 'sizeof' unary-expression
1664/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +00001665/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +00001666/// [GNU] '__alignof' unary-expression
1667/// [GNU] '__alignof' '(' type-name ')'
Jordan Rosef70a8862012-06-30 21:33:57 +00001668/// [C11] '_Alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001669/// [C++0x] 'alignof' '(' type-id ')'
James Dennette30d3ff2012-06-17 04:36:28 +00001670/// \endverbatim
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001671ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Jordan Rosef70a8862012-06-30 21:33:57 +00001672 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) ||
1673 Tok.is(tok::kw_alignof) || Tok.is(tok::kw__Alignof) ||
1674 Tok.is(tok::kw_vec_step)) &&
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001675 "Not a sizeof/alignof/vec_step expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001676 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001677 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Douglas Gregoree8aff02011-01-04 17:33:58 +00001679 // [C++0x] 'sizeof' '...' '(' identifier ')'
1680 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1681 SourceLocation EllipsisLoc = ConsumeToken();
1682 SourceLocation LParenLoc, RParenLoc;
1683 IdentifierInfo *Name = 0;
1684 SourceLocation NameLoc;
1685 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001686 BalancedDelimiterTracker T(*this, tok::l_paren);
1687 T.consumeOpen();
1688 LParenLoc = T.getOpenLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001689 if (Tok.is(tok::identifier)) {
1690 Name = Tok.getIdentifierInfo();
1691 NameLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001692 T.consumeClose();
1693 RParenLoc = T.getCloseLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001694 if (RParenLoc.isInvalid())
1695 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1696 } else {
1697 Diag(Tok, diag::err_expected_parameter_pack);
1698 SkipUntil(tok::r_paren);
1699 }
1700 } else if (Tok.is(tok::identifier)) {
1701 Name = Tok.getIdentifierInfo();
1702 NameLoc = ConsumeToken();
1703 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1704 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1705 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1706 << Name
1707 << FixItHint::CreateInsertion(LParenLoc, "(")
1708 << FixItHint::CreateInsertion(RParenLoc, ")");
1709 } else {
1710 Diag(Tok, diag::err_sizeof_parameter_pack);
1711 }
1712
1713 if (!Name)
1714 return ExprError();
1715
1716 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1717 OpTok.getLocation(),
1718 *Name, NameLoc,
1719 RParenLoc);
1720 }
Richard Smith841804b2011-10-17 23:06:20 +00001721
Jordan Rosef70a8862012-06-30 21:33:57 +00001722 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof))
Richard Smith841804b2011-10-17 23:06:20 +00001723 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1724
Eli Friedman71b8fb52012-01-21 01:01:51 +00001725 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1726
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001727 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00001728 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001729 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001730 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1731 isCastExpr,
1732 CastTy,
1733 CastRange);
1734
1735 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
Jordan Rosef70a8862012-06-30 21:33:57 +00001736 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof) ||
1737 OpTok.is(tok::kw__Alignof))
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001738 ExprKind = UETT_AlignOf;
1739 else if (OpTok.is(tok::kw_vec_step))
1740 ExprKind = UETT_VecStep;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001741
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001742 if (isCastExpr)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001743 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1744 ExprKind,
1745 /*isType=*/true,
1746 CastTy.getAsOpaquePtr(),
1747 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001748
Reid Spencer5f016e22007-07-11 17:01:13 +00001749 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001750 if (!Operand.isInvalid())
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001751 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1752 ExprKind,
1753 /*isType=*/false,
1754 Operand.release(),
1755 CastRange);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001756 return Operand;
Reid Spencer5f016e22007-07-11 17:01:13 +00001757}
1758
1759/// ParseBuiltinPrimaryExpression
1760///
James Dennette30d3ff2012-06-17 04:36:28 +00001761/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001762/// primary-expression: [C99 6.5.1]
1763/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1764/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1765/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1766/// assign-expr ')'
1767/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbournea1606712011-11-05 03:47:48 +00001768/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001769///
Reid Spencer5f016e22007-07-11 17:01:13 +00001770/// [GNU] offsetof-member-designator:
1771/// [GNU] identifier
1772/// [GNU] offsetof-member-designator '.' identifier
1773/// [GNU] offsetof-member-designator '[' expression ']'
James Dennette30d3ff2012-06-17 04:36:28 +00001774/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00001775ExprResult Parser::ParseBuiltinPrimaryExpression() {
1776 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1778
1779 tok::TokenKind T = Tok.getKind();
1780 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1781
1782 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001783 if (Tok.isNot(tok::l_paren))
1784 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1785 << BuiltinII);
1786
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001787 BalancedDelimiterTracker PT(*this, tok::l_paren);
1788 PT.consumeOpen();
1789
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 // TODO: Build AST.
1791
1792 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001793 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001794 case tok::kw___builtin_va_arg: {
John McCall60d7b3a2010-08-24 06:29:42 +00001795 ExprResult Expr(ParseAssignmentExpression());
Reid Spencer5f016e22007-07-11 17:01:13 +00001796
1797 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001798 Expr = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001799
Douglas Gregor809070a2009-02-18 17:45:20 +00001800 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001801
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001802 if (Tok.isNot(tok::r_paren)) {
1803 Diag(Tok, diag::err_expected_rparen);
Douglas Gregorac5fd842010-09-18 01:28:11 +00001804 Expr = ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001805 }
Douglas Gregorac5fd842010-09-18 01:28:11 +00001806
1807 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor809070a2009-02-18 17:45:20 +00001808 Res = ExprError();
1809 else
John McCall9ae2f072010-08-23 23:25:46 +00001810 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001812 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001813 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001814 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001815 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001816 if (Ty.isInvalid()) {
1817 SkipUntil(tok::r_paren);
1818 return ExprError();
1819 }
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Reid Spencer5f016e22007-07-11 17:01:13 +00001821 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001822 return ExprError();
1823
Reid Spencer5f016e22007-07-11 17:01:13 +00001824 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001825 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001826 Diag(Tok, diag::err_expected_ident);
1827 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001828 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001829 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001830
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001831 // Keep track of the various subcomponents we see.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001832 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001833
John McCallf312b1e2010-08-26 23:41:50 +00001834 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001835 Comps.back().isBrackets = false;
1836 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1837 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001838
Sebastian Redla55e52c2008-11-25 22:21:31 +00001839 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001840 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001841 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001842 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallf312b1e2010-08-26 23:41:50 +00001843 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001844 Comps.back().isBrackets = false;
1845 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001846
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001847 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001848 Diag(Tok, diag::err_expected_ident);
1849 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001850 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001851 }
1852 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1853 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001854
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001855 } else if (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00001856 if (CheckProhibitedCXX11Attribute())
1857 return ExprError();
1858
Reid Spencer5f016e22007-07-11 17:01:13 +00001859 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallf312b1e2010-08-26 23:41:50 +00001860 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001861 Comps.back().isBrackets = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001862 BalancedDelimiterTracker ST(*this, tok::l_square);
1863 ST.consumeOpen();
1864 Comps.back().LocStart = ST.getOpenLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001865 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001866 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001867 SkipUntil(tok::r_paren);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001868 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001869 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001870 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001871
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001872 ST.consumeClose();
1873 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001874 } else {
1875 if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001876 PT.consumeClose();
Douglas Gregor809070a2009-02-18 17:45:20 +00001877 Res = ExprError();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001878 } else if (Ty.isInvalid()) {
1879 Res = ExprError();
1880 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001881 PT.consumeClose();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001882 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001883 Ty.get(), &Comps[0], Comps.size(),
1884 PT.getCloseLocation());
Eli Friedman309fe0d2009-06-27 20:38:33 +00001885 }
Chris Lattner6eb21092007-08-30 15:52:49 +00001886 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001887 }
1888 }
1889 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001890 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001891 case tok::kw___builtin_choose_expr: {
John McCall60d7b3a2010-08-24 06:29:42 +00001892 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001893 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001894 SkipUntil(tok::r_paren);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001895 return Cond;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001896 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001898 return ExprError();
1899
John McCall60d7b3a2010-08-24 06:29:42 +00001900 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001901 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001902 SkipUntil(tok::r_paren);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001903 return Expr1;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001904 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001906 return ExprError();
1907
John McCall60d7b3a2010-08-24 06:29:42 +00001908 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001909 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001910 SkipUntil(tok::r_paren);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001911 return Expr2;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001912 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001913 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001914 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001915 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001916 }
John McCall9ae2f072010-08-23 23:25:46 +00001917 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1918 Expr2.take(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001919 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001920 }
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001921 case tok::kw___builtin_astype: {
1922 // The first argument is an expression to be converted, followed by a comma.
1923 ExprResult Expr(ParseAssignmentExpression());
1924 if (Expr.isInvalid()) {
1925 SkipUntil(tok::r_paren);
1926 return ExprError();
1927 }
1928
1929 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1930 tok::r_paren))
1931 return ExprError();
1932
1933 // Second argument is the type to bitcast to.
1934 TypeResult DestTy = ParseTypeName();
1935 if (DestTy.isInvalid())
1936 return ExprError();
1937
1938 // Attempt to consume the r-paren.
1939 if (Tok.isNot(tok::r_paren)) {
1940 Diag(Tok, diag::err_expected_rparen);
1941 SkipUntil(tok::r_paren);
1942 return ExprError();
1943 }
1944
1945 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1946 ConsumeParen());
1947 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001948 }
Chandler Carruth3c7fddd2011-07-08 04:59:44 +00001949 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001950
John McCall9ae2f072010-08-23 23:25:46 +00001951 if (Res.isInvalid())
1952 return ExprError();
1953
Reid Spencer5f016e22007-07-11 17:01:13 +00001954 // These can be followed by postfix-expr pieces because they are
1955 // primary-expressions.
John McCall9ae2f072010-08-23 23:25:46 +00001956 return ParsePostfixExpressionSuffix(Res.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001957}
1958
1959/// ParseParenExpression - This parses the unit that starts with a '(' token,
1960/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001961/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1962/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001963///
James Dennette30d3ff2012-06-17 04:36:28 +00001964/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001965/// primary-expression: [C99 6.5.1]
1966/// '(' expression ')'
1967/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1968/// postfix-expression: [C99 6.5.2]
1969/// '(' type-name ')' '{' initializer-list '}'
1970/// '(' type-name ')' '{' initializer-list ',' '}'
1971/// cast-expression: [C99 6.5.4]
1972/// '(' type-name ')' cast-expression
John McCallf85e1932011-06-15 23:02:42 +00001973/// [ARC] bridged-cast-expression
1974///
1975/// [ARC] bridged-cast-expression:
1976/// (__bridge type-name) cast-expression
1977/// (__bridge_transfer type-name) cast-expression
1978/// (__bridge_retained type-name) cast-expression
James Dennette30d3ff2012-06-17 04:36:28 +00001979/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00001980ExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001981Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001982 bool isTypeCast, ParsedType &CastTy,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001983 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001984 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001985 BalancedDelimiterTracker T(*this, tok::l_paren);
1986 if (T.consumeOpen())
1987 return ExprError();
1988 SourceLocation OpenLoc = T.getOpenLocation();
1989
John McCall60d7b3a2010-08-24 06:29:42 +00001990 ExprResult Result(true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001991 bool isAmbiguousTypeId;
John McCallb3d87482010-08-24 05:47:05 +00001992 CastTy = ParsedType();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001993
Douglas Gregor02688102010-09-14 23:59:36 +00001994 if (Tok.is(tok::code_completion)) {
1995 Actions.CodeCompleteOrdinaryName(getCurScope(),
1996 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1997 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001998 cutOffParsing();
Douglas Gregor02688102010-09-14 23:59:36 +00001999 return ExprError();
2000 }
John McCallb3c49062011-04-06 02:35:25 +00002001
Fariborz Jahanian00852e42011-12-19 21:06:15 +00002002 // Diagnose use of bridge casts in non-arc mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002003 bool BridgeCast = (getLangOpts().ObjC2 &&
Fariborz Jahanian00852e42011-12-19 21:06:15 +00002004 (Tok.is(tok::kw___bridge) ||
2005 Tok.is(tok::kw___bridge_transfer) ||
2006 Tok.is(tok::kw___bridge_retained) ||
2007 Tok.is(tok::kw___bridge_retain)));
David Blaikie4e4d0842012-03-11 07:00:24 +00002008 if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
Ted Kremenekd9d12e02011-12-20 01:03:40 +00002009 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian00852e42011-12-19 21:06:15 +00002010 SourceLocation BridgeKeywordLoc = ConsumeToken();
2011 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremeneke698a5c2012-02-18 04:42:38 +00002012 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekd9d12e02011-12-20 01:03:40 +00002013 << BridgeCastName
2014 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian00852e42011-12-19 21:06:15 +00002015 BridgeCast = false;
2016 }
2017
John McCallb3c49062011-04-06 02:35:25 +00002018 // None of these cases should fall through with an invalid Result
2019 // unless they've already reported an error.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00002020 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002021 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall73f428c2012-04-04 01:27:53 +00002022 Actions.ActOnStartStmtExpr();
2023
Richard Smith534986f2012-04-14 00:33:13 +00002024 StmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00002025 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002026
Chris Lattnerab18c4c2007-07-24 16:58:17 +00002027 // If the substmt parsed correctly, build the AST node.
John McCall73f428c2012-04-04 01:27:53 +00002028 if (!Stmt.isInvalid()) {
John McCall9ae2f072010-08-23 23:25:46 +00002029 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
John McCall73f428c2012-04-04 01:27:53 +00002030 } else {
2031 Actions.ActOnStmtExprError();
2032 }
Fariborz Jahanian00852e42011-12-19 21:06:15 +00002033 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCallb64915a2011-06-17 21:56:12 +00002034 tok::TokenKind tokenKind = Tok.getKind();
2035 SourceLocation BridgeKeywordLoc = ConsumeToken();
2036
John McCallf85e1932011-06-15 23:02:42 +00002037 // Parse an Objective-C ARC ownership cast expression.
2038 ObjCBridgeCastKind Kind;
John McCallb64915a2011-06-17 21:56:12 +00002039 if (tokenKind == tok::kw___bridge)
John McCallf85e1932011-06-15 23:02:42 +00002040 Kind = OBC_Bridge;
John McCallb64915a2011-06-17 21:56:12 +00002041 else if (tokenKind == tok::kw___bridge_transfer)
John McCallf85e1932011-06-15 23:02:42 +00002042 Kind = OBC_BridgeTransfer;
John McCallb64915a2011-06-17 21:56:12 +00002043 else if (tokenKind == tok::kw___bridge_retained)
John McCallf85e1932011-06-15 23:02:42 +00002044 Kind = OBC_BridgeRetained;
John McCallb64915a2011-06-17 21:56:12 +00002045 else {
2046 // As a hopefully temporary workaround, allow __bridge_retain as
2047 // a synonym for __bridge_retained, but only in system headers.
2048 assert(tokenKind == tok::kw___bridge_retain);
2049 Kind = OBC_BridgeRetained;
2050 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
2051 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
2052 << FixItHint::CreateReplacement(BridgeKeywordLoc,
2053 "__bridge_retained");
2054 }
John McCallf85e1932011-06-15 23:02:42 +00002055
John McCallf85e1932011-06-15 23:02:42 +00002056 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002057 T.consumeClose();
2058 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002059 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCallf85e1932011-06-15 23:02:42 +00002060
2061 if (Ty.isInvalid() || SubExpr.isInvalid())
2062 return ExprError();
2063
2064 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
2065 BridgeKeywordLoc, Ty.get(),
2066 RParenLoc, SubExpr.get());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002067 } else if (ExprType >= CompoundLiteral &&
2068 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Reid Spencer5f016e22007-07-11 17:01:13 +00002070 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002072 // In C++, if the type-id is ambiguous we disambiguate based on context.
2073 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
2074 // in which case we should treat it as type-id.
2075 // if stopIfCastExpr is false, we need to determine the context past the
2076 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002077 if (isAmbiguousTypeId && !stopIfCastExpr) {
2078 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
2079 RParenLoc = T.getCloseLocation();
2080 return res;
2081 }
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002083 // Parse the type declarator.
2084 DeclSpec DS(AttrFactory);
2085 ParseSpecifierQualifierList(DS);
2086 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2087 ParseDeclarator(DeclaratorInfo);
Douglas Gregor0fbda682010-09-15 14:51:05 +00002088
Douglas Gregor77328d12010-09-15 23:19:31 +00002089 // If our type is followed by an identifier and either ':' or ']', then
2090 // this is probably an Objective-C message send where the leading '[' is
2091 // missing. Recover as if that were the case.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002092 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
David Blaikie4e4d0842012-03-11 07:00:24 +00002093 !InMessageExpression && getLangOpts().ObjC1 &&
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002094 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
2095 TypeResult Ty;
2096 {
2097 InMessageExpressionRAIIObject InMessage(*this, false);
2098 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2099 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002100 Result = ParseObjCMessageExpressionBody(SourceLocation(),
2101 SourceLocation(),
2102 Ty.get(), 0);
2103 } else {
2104 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002105 T.consumeClose();
2106 RParenLoc = T.getCloseLocation();
Douglas Gregor77328d12010-09-15 23:19:31 +00002107 if (Tok.is(tok::l_brace)) {
2108 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002109 TypeResult Ty;
2110 {
2111 InMessageExpressionRAIIObject InMessage(*this, false);
2112 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2113 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002114 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +00002115 }
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00002116
Douglas Gregor77328d12010-09-15 23:19:31 +00002117 if (ExprType == CastExpr) {
2118 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00002119
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002120 if (DeclaratorInfo.isInvalidType())
Douglas Gregor77328d12010-09-15 23:19:31 +00002121 return ExprError();
2122
Douglas Gregor77328d12010-09-15 23:19:31 +00002123 // Note that this doesn't parse the subsequent cast-expression, it just
2124 // returns the parsed type to the callee.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002125 if (stopIfCastExpr) {
2126 TypeResult Ty;
2127 {
2128 InMessageExpressionRAIIObject InMessage(*this, false);
2129 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2130 }
2131 CastTy = Ty.get();
Douglas Gregor77328d12010-09-15 23:19:31 +00002132 return ExprResult();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002133 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002134
2135 // Reject the cast of super idiom in ObjC.
David Blaikie4e4d0842012-03-11 07:00:24 +00002136 if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
Douglas Gregor77328d12010-09-15 23:19:31 +00002137 Tok.getIdentifierInfo() == Ident_super &&
2138 getCurScope()->isInObjcMethodScope() &&
2139 GetLookAheadToken(1).isNot(tok::period)) {
2140 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2141 << SourceRange(OpenLoc, RParenLoc);
2142 return ExprError();
2143 }
2144
2145 // Parse the cast-expression that follows it next.
2146 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002147 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2148 /*isAddressOfOperand=*/false,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002149 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002150 if (!Result.isInvalid()) {
2151 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2152 DeclaratorInfo, CastTy,
Douglas Gregor77328d12010-09-15 23:19:31 +00002153 RParenLoc, Result.take());
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002154 }
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002155 return Result;
Douglas Gregor77328d12010-09-15 23:19:31 +00002156 }
2157
2158 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2159 return ExprError();
2160 }
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002161 } else if (isTypeCast) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00002162 // Parse the expression-list.
Douglas Gregor0fbda682010-09-15 14:51:05 +00002163 InMessageExpressionRAIIObject InMessage(*this, false);
2164
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002165 ExprVector ArgExprs;
Nate Begeman2ef13e52009-08-10 23:49:36 +00002166 CommaLocsTy CommaLocs;
2167
2168 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2169 ExprType = SimpleExpr;
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002170 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002171 ArgExprs);
Nate Begeman2ef13e52009-08-10 23:49:36 +00002172 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002173 } else {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002174 InMessageExpressionRAIIObject InMessage(*this, false);
2175
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002176 Result = ParseExpression(MaybeTypeCast);
Reid Spencer5f016e22007-07-11 17:01:13 +00002177 ExprType = SimpleExpr;
John McCallb3c49062011-04-06 02:35:25 +00002178
2179 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002180 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002181 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00002182 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00002183
Reid Spencer5f016e22007-07-11 17:01:13 +00002184 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00002185 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002186 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00002187 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00002188 }
Mike Stump1eb44332009-09-09 15:08:12 +00002189
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002190 T.consumeClose();
2191 RParenLoc = T.getCloseLocation();
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002192 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00002193}
2194
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002195/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2196/// and we are at the left brace.
2197///
James Dennette30d3ff2012-06-17 04:36:28 +00002198/// \verbatim
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002199/// postfix-expression: [C99 6.5.2]
2200/// '(' type-name ')' '{' initializer-list '}'
2201/// '(' type-name ')' '{' initializer-list ',' '}'
James Dennette30d3ff2012-06-17 04:36:28 +00002202/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00002203ExprResult
John McCallb3d87482010-08-24 05:47:05 +00002204Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002205 SourceLocation LParenLoc,
2206 SourceLocation RParenLoc) {
2207 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
David Blaikie4e4d0842012-03-11 07:00:24 +00002208 if (!getLangOpts().C99) // Compound literals don't exist in C90.
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002209 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCall60d7b3a2010-08-24 06:29:42 +00002210 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002211 if (!Result.isInvalid() && Ty)
John McCall9ae2f072010-08-23 23:25:46 +00002212 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002213 return Result;
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002214}
2215
Reid Spencer5f016e22007-07-11 17:01:13 +00002216/// ParseStringLiteralExpression - This handles the various token types that
2217/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2218/// translation phase #6].
2219///
James Dennette30d3ff2012-06-17 04:36:28 +00002220/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00002221/// primary-expression: [C99 6.5.1]
2222/// string-literal
James Dennette30d3ff2012-06-17 04:36:28 +00002223/// \verbatim
Richard Smith99831e42012-03-06 03:21:47 +00002224ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002225 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00002226
Reid Spencer5f016e22007-07-11 17:01:13 +00002227 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2228 // considered to be strings for concatenation purposes.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002229 SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00002230
Reid Spencer5f016e22007-07-11 17:01:13 +00002231 do {
2232 StringToks.push_back(Tok);
2233 ConsumeStringToken();
2234 } while (isTokenStringLiteral());
2235
2236 // Pass the set of string tokens, ready for concatenation, to the actions.
Richard Smith36f5cfe2012-03-09 08:00:36 +00002237 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(),
2238 AllowUserDefinedLiteral ? getCurScope() : 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00002239}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002240
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002241/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2242/// [C11 6.5.1.1].
Peter Collingbournef111d932011-04-15 00:35:48 +00002243///
James Dennette30d3ff2012-06-17 04:36:28 +00002244/// \verbatim
Peter Collingbournef111d932011-04-15 00:35:48 +00002245/// generic-selection:
2246/// _Generic ( assignment-expression , generic-assoc-list )
2247/// generic-assoc-list:
2248/// generic-association
2249/// generic-assoc-list , generic-association
2250/// generic-association:
2251/// type-name : assignment-expression
2252/// default : assignment-expression
James Dennette30d3ff2012-06-17 04:36:28 +00002253/// \endverbatim
Peter Collingbournef111d932011-04-15 00:35:48 +00002254ExprResult Parser::ParseGenericSelectionExpression() {
2255 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2256 SourceLocation KeyLoc = ConsumeToken();
2257
David Blaikie4e4d0842012-03-11 07:00:24 +00002258 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002259 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbournef111d932011-04-15 00:35:48 +00002260
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002261 BalancedDelimiterTracker T(*this, tok::l_paren);
2262 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbournef111d932011-04-15 00:35:48 +00002263 return ExprError();
2264
2265 ExprResult ControllingExpr;
2266 {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002267 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbournef111d932011-04-15 00:35:48 +00002268 // not evaluated."
2269 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2270 ControllingExpr = ParseAssignmentExpression();
2271 if (ControllingExpr.isInvalid()) {
2272 SkipUntil(tok::r_paren);
2273 return ExprError();
2274 }
2275 }
2276
2277 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2278 SkipUntil(tok::r_paren);
2279 return ExprError();
2280 }
2281
2282 SourceLocation DefaultLoc;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002283 TypeVector Types;
2284 ExprVector Exprs;
Peter Collingbournef111d932011-04-15 00:35:48 +00002285 while (1) {
2286 ParsedType Ty;
2287 if (Tok.is(tok::kw_default)) {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002288 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbournef111d932011-04-15 00:35:48 +00002289 // generic association."
2290 if (!DefaultLoc.isInvalid()) {
2291 Diag(Tok, diag::err_duplicate_default_assoc);
2292 Diag(DefaultLoc, diag::note_previous_default_assoc);
2293 SkipUntil(tok::r_paren);
2294 return ExprError();
2295 }
2296 DefaultLoc = ConsumeToken();
2297 Ty = ParsedType();
2298 } else {
2299 ColonProtectionRAIIObject X(*this);
2300 TypeResult TR = ParseTypeName();
2301 if (TR.isInvalid()) {
2302 SkipUntil(tok::r_paren);
2303 return ExprError();
2304 }
2305 Ty = TR.release();
2306 }
2307 Types.push_back(Ty);
2308
2309 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2310 SkipUntil(tok::r_paren);
2311 return ExprError();
2312 }
2313
2314 // FIXME: These expressions should be parsed in a potentially potentially
2315 // evaluated context.
2316 ExprResult ER(ParseAssignmentExpression());
2317 if (ER.isInvalid()) {
2318 SkipUntil(tok::r_paren);
2319 return ExprError();
2320 }
2321 Exprs.push_back(ER.release());
2322
2323 if (Tok.isNot(tok::comma))
2324 break;
2325 ConsumeToken();
2326 }
2327
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002328 T.consumeClose();
2329 if (T.getCloseLocation().isInvalid())
Peter Collingbournef111d932011-04-15 00:35:48 +00002330 return ExprError();
2331
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002332 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2333 T.getCloseLocation(),
Peter Collingbournef111d932011-04-15 00:35:48 +00002334 ControllingExpr.release(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002335 Types, Exprs);
Peter Collingbournef111d932011-04-15 00:35:48 +00002336}
2337
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002338/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2339///
James Dennette30d3ff2012-06-17 04:36:28 +00002340/// \verbatim
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002341/// argument-expression-list:
2342/// assignment-expression
2343/// argument-expression-list , assignment-expression
2344///
2345/// [C++] expression-list:
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002346/// [C++] assignment-expression
2347/// [C++] expression-list , assignment-expression
2348///
2349/// [C++0x] expression-list:
2350/// [C++0x] initializer-list
2351///
2352/// [C++0x] initializer-list
2353/// [C++0x] initializer-clause ...[opt]
2354/// [C++0x] initializer-list , initializer-clause ...[opt]
2355///
2356/// [C++0x] initializer-clause:
2357/// [C++0x] assignment-expression
2358/// [C++0x] braced-init-list
James Dennette30d3ff2012-06-17 04:36:28 +00002359/// \endverbatim
Chris Lattner5f9e2722011-07-23 10:55:15 +00002360bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2361 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallf312b1e2010-08-26 23:41:50 +00002362 void (Sema::*Completer)(Scope *S,
John McCallca0408f2010-08-23 06:44:23 +00002363 Expr *Data,
Ahmed Charles13a140c2012-02-25 11:00:22 +00002364 llvm::ArrayRef<Expr *> Args),
John McCallca0408f2010-08-23 06:44:23 +00002365 Expr *Data) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002366 while (1) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002367 if (Tok.is(tok::code_completion)) {
2368 if (Completer)
Ahmed Charles13a140c2012-02-25 11:00:22 +00002369 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor4706e872011-02-17 03:09:23 +00002370 else
2371 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002372 cutOffParsing();
2373 return true;
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002374 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002375
2376 ExprResult Expr;
David Blaikie4e4d0842012-03-11 07:00:24 +00002377 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002378 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002379 Expr = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00002380 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002381 Expr = ParseAssignmentExpression();
2382
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002383 if (Tok.is(tok::ellipsis))
2384 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002385 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002386 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00002387
Sebastian Redleffa8d12008-12-10 00:02:53 +00002388 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002389
2390 if (Tok.isNot(tok::comma))
2391 return false;
2392 // Move to the next argument, remember where the comma was.
2393 CommaLocs.push_back(ConsumeToken());
2394 }
2395}
Steve Naroff296e8d52008-08-28 19:20:44 +00002396
Mike Stump98eb8a72009-02-04 22:31:32 +00002397/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2398///
James Dennette30d3ff2012-06-17 04:36:28 +00002399/// \verbatim
Mike Stump98eb8a72009-02-04 22:31:32 +00002400/// [clang] block-id:
2401/// [clang] specifier-qualifier-list block-declarator
James Dennette30d3ff2012-06-17 04:36:28 +00002402/// \endverbatim
Douglas Gregor03f1eb02012-06-15 16:59:29 +00002403void Parser::ParseBlockId(SourceLocation CaretLoc) {
Douglas Gregor75ab4142010-10-18 21:34:55 +00002404 if (Tok.is(tok::code_completion)) {
2405 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002406 return cutOffParsing();
Douglas Gregor75ab4142010-10-18 21:34:55 +00002407 }
2408
Mike Stump98eb8a72009-02-04 22:31:32 +00002409 // Parse the specifier-qualifier-list piece.
John McCall0b7e6782011-03-24 11:26:52 +00002410 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002411 ParseSpecifierQualifierList(DS);
2412
2413 // Parse the block-declarator.
2414 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2415 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002416
Mike Stump6c92fa72009-04-29 21:40:37 +00002417 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00002418 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump6c92fa72009-04-29 21:40:37 +00002419
John McCall7f040a92010-12-24 02:08:15 +00002420 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002421
Mike Stump98eb8a72009-02-04 22:31:32 +00002422 // Inform sema that we are starting a block.
Douglas Gregor03f1eb02012-06-15 16:59:29 +00002423 Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
Mike Stump98eb8a72009-02-04 22:31:32 +00002424}
2425
Steve Naroff296e8d52008-08-28 19:20:44 +00002426/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00002427/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00002428///
James Dennette30d3ff2012-06-17 04:36:28 +00002429/// \verbatim
Steve Naroff296e8d52008-08-28 19:20:44 +00002430/// block-literal:
2431/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00002432/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00002433/// [clang] block-args:
2434/// [clang] '(' parameter-list ')'
James Dennette30d3ff2012-06-17 04:36:28 +00002435/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00002436ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00002437 assert(Tok.is(tok::caret) && "block literal starts with ^");
2438 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002439
Chris Lattner6b91f002009-03-05 07:32:12 +00002440 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2441 "block literal parsing");
2442
Mike Stump1eb44332009-09-09 15:08:12 +00002443 // Enter a scope to hold everything within the block. This includes the
Steve Naroff296e8d52008-08-28 19:20:44 +00002444 // argument decls, decls within the compound expression, etc. This also
2445 // allows determining whether a variable reference inside the block is
2446 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002447 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlab197ba2009-02-09 18:23:29 +00002448 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00002449
2450 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002451 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump1eb44332009-09-09 15:08:12 +00002452
Steve Naroff296e8d52008-08-28 19:20:44 +00002453 // Parse the return type if present.
John McCall0b7e6782011-03-24 11:26:52 +00002454 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002455 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002456 // FIXME: Since the return type isn't actually parsed, it can't be used to
2457 // fill ParamInfo with an initial valid range, so do it manually.
2458 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00002459
Steve Naroff296e8d52008-08-28 19:20:44 +00002460 // If this block has arguments, parse them. There is no ambiguity here with
2461 // the expression case, because the expression case requires a parameter list.
2462 if (Tok.is(tok::l_paren)) {
2463 ParseParenDeclarator(ParamInfo);
2464 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00002465 // SetIdentifier sets the source range end, but in this case we're past
2466 // that location.
2467 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00002468 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002469 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002470 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002471 // If there was an error parsing the arguments, they may have
2472 // tried to use ^(x+y) which requires an argument list. Just
2473 // skip the whole block literal.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002474 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002475 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00002476 }
Mike Stump19c30c02009-04-29 19:03:13 +00002477
John McCall7f040a92010-12-24 02:08:15 +00002478 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002479
Mike Stump98eb8a72009-02-04 22:31:32 +00002480 // Inform sema that we are starting a block.
Douglas Gregor03f1eb02012-06-15 16:59:29 +00002481 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Mike Stumpaa771a82009-04-14 18:24:37 +00002482 } else if (!Tok.is(tok::l_brace)) {
Douglas Gregor03f1eb02012-06-15 16:59:29 +00002483 ParseBlockId(CaretLoc);
Steve Naroff296e8d52008-08-28 19:20:44 +00002484 } else {
2485 // Otherwise, pretend we saw (void).
John McCall0b7e6782011-03-24 11:26:52 +00002486 ParsedAttributes attrs(AttrFactory);
Richard Smithb9c62612012-07-30 21:30:52 +00002487 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false, false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002488 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002489 0, 0, 0,
Douglas Gregor83f51722011-01-26 03:43:54 +00002490 true, SourceLocation(),
Douglas Gregor90ebed02011-07-13 21:47:47 +00002491 SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +00002492 SourceLocation(),
2493 SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00002494 EST_None,
2495 SourceLocation(),
Richard Smitha058fd42012-05-02 22:22:32 +00002496 0, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002497 CaretLoc, CaretLoc,
2498 ParamInfo),
John McCall0b7e6782011-03-24 11:26:52 +00002499 attrs, CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00002500
John McCall7f040a92010-12-24 02:08:15 +00002501 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002502
Mike Stump98eb8a72009-02-04 22:31:32 +00002503 // Inform sema that we are starting a block.
Douglas Gregor03f1eb02012-06-15 16:59:29 +00002504 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Steve Naroff296e8d52008-08-28 19:20:44 +00002505 }
2506
Sebastian Redl1d922962008-12-13 15:32:12 +00002507
John McCall60d7b3a2010-08-24 06:29:42 +00002508 ExprResult Result(true);
Chris Lattner9af55002009-03-27 04:18:06 +00002509 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002510 // Saw something like: ^expr
2511 Diag(Tok, diag::err_expected_expression);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002512 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002513 return ExprError();
2514 }
Mike Stump1eb44332009-09-09 15:08:12 +00002515
John McCall60d7b3a2010-08-24 06:29:42 +00002516 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregorc9977d02011-03-16 17:05:57 +00002517 BlockScope.Exit();
Chris Lattner9af55002009-03-27 04:18:06 +00002518 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002519 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9af55002009-03-27 04:18:06 +00002520 else
Douglas Gregor23c94db2010-07-02 17:43:08 +00002521 Actions.ActOnBlockError(CaretLoc, getCurScope());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002522 return Result;
Steve Naroff296e8d52008-08-28 19:20:44 +00002523}
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002524
2525/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
2526///
2527/// '__objc_yes'
2528/// '__objc_no'
2529ExprResult Parser::ParseObjCBoolLiteral() {
2530 tok::TokenKind Kind = Tok.getKind();
2531 return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
2532}