blob: d1c59894755210d9a60e06f2a2398b08ab4f3666 [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));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000182 return ParseRHSOfBinaryExpression(move(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));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000193 return ParseRHSOfBinaryExpression(move(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
Douglas Gregor200b2922010-09-17 22:25:06 +0000213 return ParseRHSOfBinaryExpression(move(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);
Douglas Gregor200b2922010-09-17 22:25:06 +0000230 return ParseRHSOfBinaryExpression(move(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
James Dennette30d3ff2012-06-17 04:36:28 +0000268/// \brief Parse a binary expression that starts with \p LHS and has a
269/// precedence of at least \p MinPrec.
John McCall60d7b3a2010-08-24 06:29:42 +0000270ExprResult
271Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000272 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
273 GreaterThanIsOperator,
David Blaikie4e4d0842012-03-11 07:00:24 +0000274 getLangOpts().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 SourceLocation ColonLoc;
276
277 while (1) {
278 // If this token has a lower precedence than we are allowed to parse (e.g.
279 // because we are called recursively, or because the token is not a binop),
280 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000281 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000282 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
284 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000285 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // Special case handling for the ternary operator.
John McCall60d7b3a2010-08-24 06:29:42 +0000289 ExprResult TernaryMiddle(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000291 if (Tok.isNot(tok::colon)) {
Chris Lattnera69d0ed2009-12-10 02:02:58 +0000292 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
293 ColonProtectionRAIIObject X(*this);
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // Handle this production specially:
296 // logical-OR-expression '?' expression ':' conditional-expression
297 // In particular, the RHS of the '?' is 'expression', not
298 // 'logical-OR-expression' as we might expect.
299 TernaryMiddle = ParseExpression();
Douglas Gregor94859892010-09-17 22:41:34 +0000300 if (TernaryMiddle.isInvalid()) {
301 LHS = ExprError();
302 TernaryMiddle = 0;
303 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 } else {
305 // Special case handling of "X ? Y : Z" where Y is empty:
306 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000307 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 Diag(Tok, diag::ext_gnu_conditional_expr);
309 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000310
Chris Lattnere5deae92010-04-20 21:33:39 +0000311 if (Tok.is(tok::colon)) {
312 // Eat the colon.
313 ColonLoc = ConsumeToken();
314 } else {
Chandler Carruthb00d37e2011-07-26 05:19:46 +0000315 // Otherwise, we're missing a ':'. Assume that this was a typo that
316 // the user forgot. If we're not in a macro expansion, we can suggest
317 // a fixit hint. If there were two spaces before the current token,
Chris Lattner24728822010-05-24 22:31:37 +0000318 // suggest inserting the colon in between them, otherwise insert ": ".
319 SourceLocation FILoc = Tok.getLocation();
320 const char *FIText = ": ";
Argyrios Kyrtzidisb5303aa2011-06-24 17:28:29 +0000321 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis69bda4c2012-01-19 15:59:08 +0000322 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
323 assert(FILoc.isFileID());
Chris Lattner24728822010-05-24 22:31:37 +0000324 bool IsInvalid = false;
325 const char *SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000326 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000327 if (!IsInvalid && *SourcePtr == ' ') {
328 SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000329 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000330 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000331 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattner24728822010-05-24 22:31:37 +0000332 FIText = ":";
333 }
334 }
335 }
336
Ted Kremenek987aa872010-04-12 22:10:35 +0000337 Diag(Tok, diag::err_expected_colon)
Chris Lattner24728822010-05-24 22:31:37 +0000338 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000339 Diag(OpToken, diag::note_matching) << "?";
Chris Lattnere5deae92010-04-20 21:33:39 +0000340 ColonLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 }
Fariborz Jahanianf071e9b2009-10-23 21:01:39 +0000343
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000344 // Code completion for the right-hand side of an assignment expression
345 // goes through a special hook that takes the left-hand side into account.
346 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000347 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000348 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000349 return ExprError();
350 }
351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000353 // ParseCastExpression works here because all RHS expressions in C have it
354 // as a prefix, at least. However, in C++, an assignment-expression could
355 // be a throw-expression, which is not a valid cast-expression.
356 // Therefore we need some special-casing here.
357 // Also note that the third operand of the conditional operator is
Richard Smithc56ab432012-02-26 23:40:27 +0000358 // an assignment-expression in C++, and in C++11, we can have a
Richard Smith5e4e58b2012-03-01 02:59:17 +0000359 // braced-init-list on the RHS of an assignment. For better diagnostics,
360 // parse as if we were allowed braced-init-lists everywhere, and check that
361 // they only appear on the RHS of assignments later.
John McCall60d7b3a2010-08-24 06:29:42 +0000362 ExprResult RHS;
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000363 bool RHSIsInitList = false;
David Blaikie4e4d0842012-03-11 07:00:24 +0000364 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smithc56ab432012-02-26 23:40:27 +0000365 RHS = ParseBraceInitializer();
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000366 RHSIsInitList = true;
David Blaikie4e4d0842012-03-11 07:00:24 +0000367 } else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional)
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000368 RHS = ParseAssignmentExpression();
Richard Smith5e4e58b2012-03-01 02:59:17 +0000369 else
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000370 RHS = ParseCastExpression(false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000371
Douglas Gregor200b2922010-09-17 22:25:06 +0000372 if (RHS.isInvalid())
373 LHS = ExprError();
374
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 // Remember the precedence of this operator and get the precedence of the
376 // operator immediately to the right of the RHS.
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000377 prec::Level ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000378 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikie4e4d0842012-03-11 07:00:24 +0000379 getLangOpts().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000380
381 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000382 bool isRightAssoc = ThisPrec == prec::Conditional ||
383 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000384
385 // Get the precedence of the operator to the right of the RHS. If it binds
386 // more tightly with RHS than we do, evaluate it completely first.
387 if (ThisPrec < NextTokPrec ||
388 (ThisPrec == NextTokPrec && isRightAssoc)) {
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000389 if (!RHS.isInvalid() && RHSIsInitList) {
390 Diag(Tok, diag::err_init_list_bin_op)
391 << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get());
392 RHS = ExprError();
Richard Smith5e4e58b2012-03-01 02:59:17 +0000393 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 // If this is left-associative, only parse things on the RHS that bind
395 // more tightly than the current operator. If it is left-associative, it
396 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
397 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000398 // The function takes ownership of the RHS.
Douglas Gregor200b2922010-09-17 22:25:06 +0000399 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000400 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000401 RHSIsInitList = false;
Douglas Gregor200b2922010-09-17 22:25:06 +0000402
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000403 if (RHS.isInvalid())
Douglas Gregor200b2922010-09-17 22:25:06 +0000404 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000405
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000406 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikie4e4d0842012-03-11 07:00:24 +0000407 getLangOpts().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 }
409 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000410
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000411 if (!RHS.isInvalid() && RHSIsInitList) {
Richard Smith5e4e58b2012-03-01 02:59:17 +0000412 if (ThisPrec == prec::Assignment) {
413 Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000414 << Actions.getExprRange(RHS.get());
Richard Smith5e4e58b2012-03-01 02:59:17 +0000415 } else {
416 Diag(OpToken, diag::err_init_list_bin_op)
Richard Smithf9b6f2c2012-03-01 07:10:06 +0000417 << /*RHS*/1 << PP.getSpelling(OpToken)
418 << Actions.getExprRange(RHS.get());
Richard Smith5e4e58b2012-03-01 02:59:17 +0000419 LHS = ExprError();
420 }
421 }
422
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000423 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000424 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000425 if (TernaryMiddle.isInvalid()) {
426 // If we're using '>>' as an operator within a template
427 // argument list (in C++98), suggest the addition of
428 // parentheses so that the code remains well-formed in C++0x.
429 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
430 SuggestParentheses(OpToken.getLocation(),
431 diag::warn_cxx0x_right_shift_in_template_arg,
432 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
433 Actions.getExprRange(RHS.get()).getEnd()));
434
Douglas Gregor23c94db2010-07-02 17:43:08 +0000435 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000436 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000437 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000438 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000439 LHS.take(), TernaryMiddle.take(),
440 RHS.take());
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000441 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 }
443}
444
James Dennette30d3ff2012-06-17 04:36:28 +0000445/// \brief Parse a cast-expression, or, if \p isUnaryExpression is true,
446/// parse a unary-expression.
447///
448/// \p isAddressOfOperand exists because an id-expression that is the
449/// operand of address-of gets special treatment due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000450///
John McCall60d7b3a2010-08-24 06:29:42 +0000451ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000452 bool isAddressOfOperand,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000453 TypeCastState isTypeCast) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000454 bool NotCastExpr;
John McCall60d7b3a2010-08-24 06:29:42 +0000455 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor200b2922010-09-17 22:25:06 +0000456 isAddressOfOperand,
457 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +0000458 isTypeCast);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000459 if (NotCastExpr)
460 Diag(Tok, diag::err_expected_expression);
461 return move(Res);
462}
463
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000464namespace {
465class CastExpressionIdValidator : public CorrectionCandidateCallback {
466 public:
467 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
468 : AllowNonTypes(AllowNonTypes) {
469 WantTypeSpecifiers = AllowTypes;
470 }
471
472 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
473 NamedDecl *ND = candidate.getCorrectionDecl();
474 if (!ND)
475 return candidate.isKeyword();
476
477 if (isa<TypeDecl>(ND))
478 return WantTypeSpecifiers;
479 return AllowNonTypes;
480 }
481
482 private:
483 bool AllowNonTypes;
484};
485}
486
James Dennette30d3ff2012-06-17 04:36:28 +0000487/// \brief Parse a cast-expression, or, if \pisUnaryExpression is true, parse
488/// a unary-expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000489///
James Dennette30d3ff2012-06-17 04:36:28 +0000490/// \p isAddressOfOperand exists because an id-expression that is the operand
491/// of address-of gets special treatment due to member pointers. NotCastExpr
492/// is set to true if the token is not the start of a cast-expression, and no
493/// diagnostic is emitted in this case.
494///
495/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +0000496/// cast-expression: [C99 6.5.4]
497/// unary-expression
498/// '(' type-name ')' cast-expression
499///
500/// unary-expression: [C99 6.5.3]
501/// postfix-expression
502/// '++' unary-expression
503/// '--' unary-expression
504/// unary-operator cast-expression
505/// 'sizeof' unary-expression
506/// 'sizeof' '(' type-name ')'
Richard Smith99831e42012-03-06 03:21:47 +0000507/// [C++11] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000508/// [GNU] '__alignof' unary-expression
509/// [GNU] '__alignof' '(' type-name ')'
Richard Smith99831e42012-03-06 03:21:47 +0000510/// [C++11] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000511/// [GNU] '&&' identifier
Richard Smith99831e42012-03-06 03:21:47 +0000512/// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7]
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000513/// [C++] new-expression
514/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000515///
516/// unary-operator: one of
517/// '&' '*' '+' '-' '~' '!'
518/// [GNU] '__extension__' '__real' '__imag'
519///
520/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000521/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000522/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000523/// constant
524/// string-literal
525/// [C++] boolean-literal [C++ 2.13.5]
Richard Smith99831e42012-03-06 03:21:47 +0000526/// [C++11] 'nullptr' [C++11 2.14.7]
527/// [C++11] user-defined-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000528/// '(' expression ')'
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000529/// [C11] generic-selection
Reid Spencer5f016e22007-07-11 17:01:13 +0000530/// '__func__' [C99 6.4.2.2]
531/// [GNU] '__FUNCTION__'
532/// [GNU] '__PRETTY_FUNCTION__'
533/// [GNU] '(' compound-statement ')'
534/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
535/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
536/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
537/// assign-expr ')'
538/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000539/// [GNU] '__null'
Mike Stump1eb44332009-09-09 15:08:12 +0000540/// [OBJC] '[' objc-message-expr ']'
James Dennett7a90c8b2012-06-15 06:52:33 +0000541/// [OBJC] '\@selector' '(' objc-selector-arg ')'
542/// [OBJC] '\@protocol' '(' identifier ')'
543/// [OBJC] '\@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000544/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000545/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smith99831e42012-03-06 03:21:47 +0000546/// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3]
Douglas Gregor2725ca82010-04-21 19:57:20 +0000547/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smith99831e42012-03-06 03:21:47 +0000548/// [C++11] typename-specifier braced-init-list [C++11 5.2.3]
Reid Spencer5f016e22007-07-11 17:01:13 +0000549/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
550/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
551/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
552/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000553/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
554/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000555/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000556/// [G++] unary-type-trait '(' type-id ')'
557/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley21ff2e52011-04-28 00:16:57 +0000558/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff296e8d52008-08-28 19:20:44 +0000559/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000560///
561/// constant: [C99 6.4.4]
562/// integer-constant
563/// floating-constant
564/// enumeration-constant -> identifier
565/// character-constant
566///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000567/// id-expression: [C++ 5.1]
568/// unqualified-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000569/// qualified-id
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000570///
571/// unqualified-id: [C++ 5.1]
572/// identifier
573/// operator-function-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000574/// conversion-function-id
575/// '~' class-name
576/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000577///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000578/// new-expression: [C++ 5.3.4]
579/// '::'[opt] 'new' new-placement[opt] new-type-id
580/// new-initializer[opt]
581/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
582/// new-initializer[opt]
583///
584/// delete-expression: [C++ 5.3.5]
585/// '::'[opt] 'delete' cast-expression
586/// '::'[opt] 'delete' '[' ']' cast-expression
587///
John Wiegley20c0da72011-04-27 23:09:49 +0000588/// [GNU/Embarcadero] unary-type-trait:
589/// '__is_arithmetic'
590/// '__is_floating_point'
591/// '__is_integral'
592/// '__is_lvalue_expr'
593/// '__is_rvalue_expr'
594/// '__is_complete_type'
595/// '__is_void'
596/// '__is_array'
597/// '__is_function'
598/// '__is_reference'
599/// '__is_lvalue_reference'
600/// '__is_rvalue_reference'
601/// '__is_fundamental'
602/// '__is_object'
603/// '__is_scalar'
604/// '__is_compound'
605/// '__is_pointer'
606/// '__is_member_object_pointer'
607/// '__is_member_function_pointer'
608/// '__is_member_pointer'
609/// '__is_const'
610/// '__is_volatile'
611/// '__is_trivial'
612/// '__is_standard_layout'
613/// '__is_signed'
614/// '__is_unsigned'
615///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000616/// [GNU] unary-type-trait:
Sebastian Redlc238f092010-08-31 04:59:00 +0000617/// '__has_nothrow_assign'
618/// '__has_nothrow_copy'
619/// '__has_nothrow_constructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000620/// '__has_trivial_assign' [TODO]
621/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000622/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000623/// '__has_trivial_destructor'
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000624/// '__has_virtual_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000625/// '__is_abstract' [TODO]
626/// '__is_class'
627/// '__is_empty' [TODO]
628/// '__is_enum'
Douglas Gregor5e9392b2011-12-03 18:14:24 +0000629/// '__is_final'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000630/// '__is_pod'
631/// '__is_polymorphic'
Chandler Carruthb7e95892011-04-23 10:47:28 +0000632/// '__is_trivial'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000633/// '__is_union'
634///
Sean Huntfeb375d2011-05-13 00:31:07 +0000635/// [Clang] unary-type-trait:
636/// '__trivially_copyable'
637///
Douglas Gregor9f361132011-01-27 20:28:01 +0000638/// binary-type-trait:
639/// [GNU] '__is_base_of'
640/// [MS] '__is_convertible_to'
John Wiegley20c0da72011-04-27 23:09:49 +0000641/// '__is_convertible'
642/// '__is_same'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000643///
John Wiegley21ff2e52011-04-28 00:16:57 +0000644/// [Embarcadero] array-type-trait:
645/// '__array_rank'
646/// '__array_extent'
647///
John Wiegley55262202011-04-25 06:54:41 +0000648/// [Embarcadero] expression-trait:
649/// '__is_lvalue_expr'
650/// '__is_rvalue_expr'
James Dennette30d3ff2012-06-17 04:36:28 +0000651/// \endverbatim
John Wiegley55262202011-04-25 06:54:41 +0000652///
John McCall60d7b3a2010-08-24 06:29:42 +0000653ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl02bc21a2010-09-10 20:55:37 +0000654 bool isAddressOfOperand,
655 bool &NotCastExpr,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000656 TypeCastState isTypeCast) {
John McCall60d7b3a2010-08-24 06:29:42 +0000657 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000659 NotCastExpr = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 // This handles all of cast-expression, unary-expression, postfix-expression,
662 // and primary-expression. We handle them together like this for efficiency
663 // and to simplify handling of an expression starting with a '(' token: which
664 // may be one of a parenthesized expression, cast-expression, compound literal
665 // expression, or statement expression.
666 //
667 // If the parsed tokens consist of a primary-expression, the cases below
John McCall9ae2f072010-08-23 23:25:46 +0000668 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
669 // to handle the postfix expression suffixes. Cases that cannot be followed
670 // by postfix exprs should return without invoking
671 // ParsePostfixExpressionSuffix.
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 switch (SavedKind) {
673 case tok::l_paren: {
674 // If this expression is limited to being a unary-expression, the parent can
675 // not start a cast expression.
676 ParenParseOption ParenExprType =
David Blaikie4e4d0842012-03-11 07:00:24 +0000677 (isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallb3d87482010-08-24 05:47:05 +0000678 ParsedType CastTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 SourceLocation RParenLoc;
Chris Lattner932dff72009-12-10 02:08:07 +0000680
681 {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000682 // The inside of the parens don't need to be a colon protected scope, and
683 // isn't immediately a message send.
Chris Lattner932dff72009-12-10 02:08:07 +0000684 ColonProtectionRAIIObject X(*this, false);
Douglas Gregor0fbda682010-09-15 14:51:05 +0000685
Chris Lattner932dff72009-12-10 02:08:07 +0000686 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000687 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner932dff72009-12-10 02:08:07 +0000688 }
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 switch (ParenExprType) {
691 case SimpleExpr: break; // Nothing else to do.
692 case CompoundStmt: break; // Nothing else to do.
693 case CompoundLiteral:
694 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
695 // postfix-expression exist, parse them now.
696 break;
697 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000698 // We have parsed the cast-expression and no postfix-expr pieces are
699 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000700 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000702
John McCall9ae2f072010-08-23 23:25:46 +0000703 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000705
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // primary-expression
707 case tok::numeric_constant:
708 // constant: integer-constant
709 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000710
Richard Smith36f5cfe2012-03-09 08:00:36 +0000711 Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000713 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000714
715 case tok::kw_true:
716 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000717 return ParseCXXBoolLiteral();
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000718
719 case tok::kw___objc_yes:
720 case tok::kw___objc_no:
721 return ParseObjCBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000722
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000723 case tok::kw_nullptr:
Richard Smith841804b2011-10-17 23:06:20 +0000724 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000725 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
726
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000727 case tok::annot_primary_expr:
728 assert(Res.get() == 0 && "Stray primary-expression annotation?");
729 Res = getExprAnnotation(Tok);
730 ConsumeToken();
731 break;
732
David Blaikie42d6d0c2011-12-04 05:04:18 +0000733 case tok::kw_decltype:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000734 case tok::identifier: { // primary-expression: identifier
735 // unqualified-id: identifier
736 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000737 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000738 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
David Blaikie4e4d0842012-03-11 07:00:24 +0000739 if (getLangOpts().CPlusPlus) {
John McCallb6727072010-01-07 19:29:58 +0000740 // Avoid the unnecessary parse-time lookup in the common case
741 // where the syntax forbids a type.
742 const Token &Next = NextToken();
743 if (Next.is(tok::coloncolon) ||
744 (!ColonIsSacred && Next.is(tok::colon)) ||
745 Next.is(tok::less) ||
Sebastian Redl62f13c92011-12-22 18:58:29 +0000746 Next.is(tok::l_paren) ||
747 Next.is(tok::l_brace)) {
John McCallb6727072010-01-07 19:29:58 +0000748 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
749 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000750 return ExprError();
751 if (!Tok.is(tok::identifier))
John McCallb6727072010-01-07 19:29:58 +0000752 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
753 }
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000754 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000755
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000756 // Consume the identifier so that we can see if it is followed by a '(' or
757 // '.'.
758 IdentifierInfo &II = *Tok.getIdentifierInfo();
759 SourceLocation ILoc = ConsumeToken();
760
Chris Lattnereb483eb2010-04-11 08:28:14 +0000761 // Support 'Class.property' and 'super.property' notation.
David Blaikie4e4d0842012-03-11 07:00:24 +0000762 if (getLangOpts().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000763 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattner236beab2010-04-12 06:20:33 +0000764 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000765 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000766 ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000767
Douglas Gregor8f70bda2012-02-16 18:19:22 +0000768 // Allow either an identifier or the keyword 'class' (in C++).
769 if (Tok.isNot(tok::identifier) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000770 !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000771 Diag(Tok, diag::err_expected_property_name);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000772 return ExprError();
773 }
774 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
775 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000776
777 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
778 ILoc, PropertyLoc);
John McCall9ae2f072010-08-23 23:25:46 +0000779 break;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000780 }
John McCall9c72c602010-08-27 09:08:28 +0000781
Douglas Gregorfa885c12010-09-15 15:09:43 +0000782 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregor78edf512010-09-15 16:23:04 +0000783 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregorfa885c12010-09-15 15:09:43 +0000784 // that identifier, this is probably a message send with a missing open
Douglas Gregor78edf512010-09-15 16:23:04 +0000785 // bracket. Treat it as such.
David Blaikie4e4d0842012-03-11 07:00:24 +0000786 if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregorfa885c12010-09-15 15:09:43 +0000787 getCurScope()->isInObjcMethodScope() &&
Douglas Gregor78edf512010-09-15 16:23:04 +0000788 ((Tok.is(tok::identifier) &&
789 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
790 Tok.is(tok::code_completion))) {
Douglas Gregorfa885c12010-09-15 15:09:43 +0000791 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
792 0);
793 break;
794 }
795
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000796 // If we have an Objective-C class name followed by an identifier
797 // and either ':' or ']', this is an Objective-C class message
798 // send that's missing the opening '['. Recovery
799 // appropriately. Also take this path if we're performing code
800 // completion after an Objective-C class name.
David Blaikie4e4d0842012-03-11 07:00:24 +0000801 if (getLangOpts().ObjC1 &&
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000802 ((Tok.is(tok::identifier) && !InMessageExpression) ||
803 Tok.is(tok::code_completion))) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000804 const Token& Next = NextToken();
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000805 if (Tok.is(tok::code_completion) ||
806 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif9fe871a2010-09-17 10:21:45 +0000807 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
808 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000809 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000810 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000811 DS.SetRangeStart(ILoc);
812 DS.SetRangeEnd(ILoc);
813 const char *PrevSpec = 0;
814 unsigned DiagID;
Gabor Greif9fe871a2010-09-17 10:21:45 +0000815 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregor9497a732010-09-16 01:51:54 +0000816
817 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
818 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
819 DeclaratorInfo);
820 if (Ty.isInvalid())
821 break;
822
823 Res = ParseObjCMessageExpressionBody(SourceLocation(),
824 SourceLocation(),
825 Ty.get(), 0);
826 break;
827 }
828 }
829
John McCall9c72c602010-08-27 09:08:28 +0000830 // Make sure to pass down the right value for isAddressOfOperand.
831 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
832 isAddressOfOperand = false;
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000833
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
835 // need to know whether or not this identifier is a function designator or
836 // not.
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000837 UnqualifiedId Name;
838 CXXScopeSpec ScopeSpec;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000839 SourceLocation TemplateKWLoc;
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000840 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
841 isTypeCast != IsTypeCast);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000842 Name.setIdentifier(&II, ILoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000843 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
844 Name, Tok.is(tok::l_paren),
845 isAddressOfOperand, &Validator);
John McCall9ae2f072010-08-23 23:25:46 +0000846 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 }
848 case tok::char_constant: // constant: character-constant
Douglas Gregor5cee1192011-07-27 05:40:30 +0000849 case tok::wide_char_constant:
850 case tok::utf16_char_constant:
851 case tok::utf32_char_constant:
Richard Smith36f5cfe2012-03-09 08:00:36 +0000852 Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000854 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
856 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
857 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000858 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000860 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 case tok::string_literal: // primary-expression: string-literal
862 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000863 case tok::utf8_string_literal:
864 case tok::utf16_string_literal:
865 case tok::utf32_string_literal:
Richard Smith99831e42012-03-06 03:21:47 +0000866 Res = ParseStringLiteralExpression(true);
John McCall9ae2f072010-08-23 23:25:46 +0000867 break;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000868 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbournef111d932011-04-15 00:35:48 +0000869 Res = ParseGenericSelectionExpression();
870 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 case tok::kw___builtin_va_arg:
872 case tok::kw___builtin_offsetof:
873 case tok::kw___builtin_choose_expr:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000874 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redld8c4e152008-12-11 22:33:27 +0000875 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000876 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000877 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth3c7fddd2011-07-08 04:59:44 +0000878
Douglas Gregord4206632010-08-06 14:50:36 +0000879 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
880 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
881 // C++ [expr.unary] has:
882 // unary-expression:
883 // ++ cast-expression
884 // -- cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 SourceLocation SavedLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +0000886 Res = ParseCastExpression(!getLangOpts().CPlusPlus);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000887 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000888 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000889 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000891 case tok::amp: { // unary-expression: '&' cast-expression
892 // Special treatment because of member pointers
893 SourceLocation SavedLoc = ConsumeToken();
894 Res = ParseCastExpression(false, true);
895 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000896 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redlebc07d52009-02-03 20:19:35 +0000897 return move(Res);
898 }
899
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 case tok::star: // unary-expression: '*' cast-expression
901 case tok::plus: // unary-expression: '+' cast-expression
902 case tok::minus: // unary-expression: '-' cast-expression
903 case tok::tilde: // unary-expression: '~' cast-expression
904 case tok::exclaim: // unary-expression: '!' cast-expression
905 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000906 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 SourceLocation SavedLoc = ConsumeToken();
908 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000909 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000910 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000911 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000912 }
913
Chris Lattner35080842008-02-02 20:20:10 +0000914 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
915 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000916 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000917 SourceLocation SavedLoc = ConsumeToken();
918 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000919 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000920 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000921 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 }
923 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
924 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000925 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
927 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000928 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000929 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
930 return ParseUnaryExprOrTypeTraitExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 case tok::ampamp: { // unary-expression: '&&' identifier
932 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000933 if (Tok.isNot(tok::identifier))
934 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000935
Chris Lattnerfebb5b82011-02-18 21:16:39 +0000936 if (getCurScope()->getFnParent() == 0)
937 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
938
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattner337e5502011-02-18 01:27:55 +0000940 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
941 Tok.getLocation());
942 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000944 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 }
946 case tok::kw_const_cast:
947 case tok::kw_dynamic_cast:
948 case tok::kw_reinterpret_cast:
949 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000950 Res = ParseCXXCasts();
John McCall9ae2f072010-08-23 23:25:46 +0000951 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000952 case tok::kw_typeid:
953 Res = ParseCXXTypeid();
John McCall9ae2f072010-08-23 23:25:46 +0000954 break;
Francois Pichet01b7c302010-09-08 12:20:18 +0000955 case tok::kw___uuidof:
956 Res = ParseCXXUuidof();
957 break;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000958 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000959 Res = ParseCXXThis();
John McCall9ae2f072010-08-23 23:25:46 +0000960 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000961
Douglas Gregor9497a732010-09-16 01:51:54 +0000962 case tok::annot_typename:
963 if (isStartOfObjCClassMessageMissingOpenBracket()) {
964 ParsedType Type = getTypeAnnotation(Tok);
965
966 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000967 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000968 DS.SetRangeStart(Tok.getLocation());
969 DS.SetRangeEnd(Tok.getLastLoc());
970
971 const char *PrevSpec = 0;
972 unsigned DiagID;
Nico Weber253e80b2010-11-22 10:30:56 +0000973 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
974 PrevSpec, DiagID, Type);
Douglas Gregor9497a732010-09-16 01:51:54 +0000975
976 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
977 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
978 if (Ty.isInvalid())
979 break;
980
981 ConsumeToken();
982 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
983 Ty.get(), 0);
984 break;
985 }
986 // Fall through
987
David Blaikie5e089fe2012-01-24 05:47:35 +0000988 case tok::annot_decltype:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000989 case tok::kw_char:
990 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000991 case tok::kw_char16_t:
992 case tok::kw_char32_t:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000993 case tok::kw_bool:
994 case tok::kw_short:
995 case tok::kw_int:
996 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000997 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000998 case tok::kw___int128:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000999 case tok::kw_signed:
1000 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001001 case tok::kw_half:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001002 case tok::kw_float:
1003 case tok::kw_double:
1004 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +00001005 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +00001006 case tok::kw_typeof:
Douglas Gregor9497a732010-09-16 01:51:54 +00001007 case tok::kw___vector: {
David Blaikie4e4d0842012-03-11 07:00:24 +00001008 if (!getLangOpts().CPlusPlus) {
Chris Lattner2dcaab32009-01-04 22:28:21 +00001009 Diag(Tok, diag::err_expected_expression);
1010 return ExprError();
1011 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +00001012
1013 if (SavedKind == tok::kw_typename) {
1014 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001015 // typename-specifier braced-init-list
John McCall9ba61662010-02-26 08:45:28 +00001016 if (TryAnnotateTypeOrScopeToken())
Eli Friedman2e0cdb42009-06-11 00:33:41 +00001017 return ExprError();
1018 }
1019
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001020 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001021 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001022 //
John McCall0b7e6782011-03-24 11:26:52 +00001023 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001024 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001025 if (Tok.isNot(tok::l_paren) &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001026 (!getLangOpts().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001027 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1028 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001029
Richard Smith7fe62082011-10-15 05:09:34 +00001030 if (Tok.is(tok::l_brace))
1031 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1032
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001033 Res = ParseCXXTypeConstructExpression(DS);
John McCall9ae2f072010-08-23 23:25:46 +00001034 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001035 }
1036
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001037 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor4074eef2010-04-23 02:08:13 +00001038 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1039 // (We can end up in this situation after tentative parsing.)
1040 if (TryAnnotateTypeOrScopeToken())
1041 return ExprError();
1042 if (!Tok.is(tok::annot_cxxscope))
1043 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001044 NotCastExpr, isTypeCast);
Douglas Gregor4074eef2010-04-23 02:08:13 +00001045
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001046 Token Next = NextToken();
1047 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001048 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001049 if (TemplateId->Kind == TNK_Type_template) {
1050 // We have a qualified template-id that we know refers to a
1051 // type, translate it into a type and continue parsing as a
1052 // cast expression.
1053 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001054 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1055 /*EnteringContext=*/false);
Douglas Gregor059101f2011-03-02 00:47:37 +00001056 AnnotateTemplateIdTokenAsType();
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001057 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001058 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001059 }
1060 }
1061
1062 // Parse as an id-expression.
1063 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001064 break;
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001065 }
1066
1067 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001068 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001069 if (TemplateId->Kind == TNK_Type_template) {
1070 // We have a template-id that we know refers to a type,
1071 // translate it into a type and continue parsing as a cast
1072 // expression.
1073 AnnotateTemplateIdTokenAsType();
1074 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001075 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001076 }
1077
1078 // Fall through to treat the template-id as an id-expression.
1079 }
1080
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001081 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redlebc07d52009-02-03 20:19:35 +00001082 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001083 break;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001084
Chris Lattner74ba4102009-01-04 22:52:14 +00001085 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +00001086 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1087 // annotates the token, tail recurse.
1088 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001089 return ExprError();
1090 if (!Tok.is(tok::coloncolon))
Sebastian Redlebc07d52009-02-03 20:19:35 +00001091 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1092
Chris Lattner74ba4102009-01-04 22:52:14 +00001093 // ::new -> [C++] new-expression
1094 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +00001095 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +00001096 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +00001097 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +00001098 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +00001099 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001101 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +00001102 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001103 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +00001104 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +00001105
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001106 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001107 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001108
1109 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001110 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001111
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001112 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smith841804b2011-10-17 23:06:20 +00001113 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001114 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001115 BalancedDelimiterTracker T(*this, tok::l_paren);
1116
1117 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001118 return ExprError();
Richard Smithf6702a32011-12-20 02:08:33 +00001119 // C++11 [expr.unary.noexcept]p1:
Sebastian Redlbd7c8492010-09-10 21:57:27 +00001120 // The noexcept operator determines whether the evaluation of its operand,
1121 // which is an unevaluated operand, can throw an exception.
1122 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001123 ExprResult Result = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001124
1125 T.consumeClose();
1126
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001127 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001128 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1129 Result.take(), T.getCloseLocation());
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001130 return move(Result);
1131 }
1132
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001133 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redl64b45f72009-01-05 20:52:13 +00001134 case tok::kw___is_class:
Eli Friedman1d954f62009-08-15 21:55:26 +00001135 case tok::kw___is_empty:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001136 case tok::kw___is_enum:
Sebastian Redlccf43502009-12-03 00:13:20 +00001137 case tok::kw___is_literal:
John Wiegley20c0da72011-04-27 23:09:49 +00001138 case tok::kw___is_arithmetic:
1139 case tok::kw___is_integral:
1140 case tok::kw___is_floating_point:
1141 case tok::kw___is_complete_type:
1142 case tok::kw___is_void:
1143 case tok::kw___is_array:
1144 case tok::kw___is_function:
1145 case tok::kw___is_reference:
1146 case tok::kw___is_lvalue_reference:
1147 case tok::kw___is_rvalue_reference:
1148 case tok::kw___is_fundamental:
1149 case tok::kw___is_object:
1150 case tok::kw___is_scalar:
1151 case tok::kw___is_compound:
1152 case tok::kw___is_pointer:
1153 case tok::kw___is_member_object_pointer:
1154 case tok::kw___is_member_function_pointer:
1155 case tok::kw___is_member_pointer:
1156 case tok::kw___is_const:
1157 case tok::kw___is_volatile:
1158 case tok::kw___is_standard_layout:
1159 case tok::kw___is_signed:
1160 case tok::kw___is_unsigned:
Chandler Carruth38402812011-04-24 02:49:28 +00001161 case tok::kw___is_literal_type:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001162 case tok::kw___is_pod:
1163 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +00001164 case tok::kw___is_trivial:
Sean Huntfeb375d2011-05-13 00:31:07 +00001165 case tok::kw___is_trivially_copyable:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001166 case tok::kw___is_union:
Douglas Gregor5e9392b2011-12-03 18:14:24 +00001167 case tok::kw___is_final:
Anders Carlsson347ba892009-04-16 00:08:20 +00001168 case tok::kw___has_trivial_constructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001169 case tok::kw___has_trivial_copy:
1170 case tok::kw___has_trivial_assign:
Anders Carlsson072abef2009-04-17 02:34:54 +00001171 case tok::kw___has_trivial_destructor:
Sebastian Redlc238f092010-08-31 04:59:00 +00001172 case tok::kw___has_nothrow_assign:
1173 case tok::kw___has_nothrow_copy:
1174 case tok::kw___has_nothrow_constructor:
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001175 case tok::kw___has_virtual_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +00001176 return ParseUnaryTypeTrait();
1177
Francois Pichetf1872372010-12-08 22:35:30 +00001178 case tok::kw___builtin_types_compatible_p:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001179 case tok::kw___is_base_of:
John Wiegley20c0da72011-04-27 23:09:49 +00001180 case tok::kw___is_same:
1181 case tok::kw___is_convertible:
Douglas Gregor9f361132011-01-27 20:28:01 +00001182 case tok::kw___is_convertible_to:
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00001183 case tok::kw___is_trivially_assignable:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001184 return ParseBinaryTypeTrait();
1185
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001186 case tok::kw___is_trivially_constructible:
1187 return ParseTypeTrait();
1188
John Wiegley21ff2e52011-04-28 00:16:57 +00001189 case tok::kw___array_rank:
1190 case tok::kw___array_extent:
1191 return ParseArrayTypeTrait();
1192
John Wiegley55262202011-04-25 06:54:41 +00001193 case tok::kw___is_lvalue_expr:
1194 case tok::kw___is_rvalue_expr:
1195 return ParseExpressionTrait();
1196
Chris Lattnerc97c2042007-10-03 22:03:06 +00001197 case tok::at: {
1198 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001199 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +00001200 }
Steve Naroff296e8d52008-08-28 19:20:44 +00001201 case tok::caret:
Chandler Carruthbb399022011-07-08 04:28:55 +00001202 Res = ParseBlockLiteralExpression();
1203 break;
1204 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001205 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001206 cutOffParsing();
1207 return ExprError();
Chandler Carruthbb399022011-07-08 04:28:55 +00001208 }
Chris Lattnerfdb548e2008-12-12 19:20:14 +00001209 case tok::l_square:
David Blaikie4e4d0842012-03-11 07:00:24 +00001210 if (getLangOpts().CPlusPlus0x) {
1211 if (getLangOpts().ObjC1) {
Eli Friedmandc3b7232012-01-04 02:40:39 +00001212 // C++11 lambda expressions and Objective-C message sends both start with a
1213 // square bracket. There are three possibilities here:
1214 // we have a valid lambda expression, we have an invalid lambda
1215 // expression, or we have something that doesn't appear to be a lambda.
1216 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregorae7902c2011-08-04 15:30:47 +00001217 Res = TryParseLambdaExpression();
Eli Friedmandc3b7232012-01-04 02:40:39 +00001218 if (!Res.isInvalid() && !Res.get())
Douglas Gregorae7902c2011-08-04 15:30:47 +00001219 Res = ParseObjCMessageExpression();
1220 break;
1221 }
1222 Res = ParseLambdaExpression();
1223 break;
1224 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001225 if (getLangOpts().ObjC1) {
Chandler Carruthbb399022011-07-08 04:28:55 +00001226 Res = ParseObjCMessageExpression();
1227 break;
1228 }
1229 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001231 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001232 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001234
John McCall9ae2f072010-08-23 23:25:46 +00001235 // These can be followed by postfix-expr pieces.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001236 return ParsePostfixExpressionSuffix(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001237}
1238
James Dennette30d3ff2012-06-17 04:36:28 +00001239/// \brief Once the leading part of a postfix-expression is parsed, this
1240/// method parses any suffixes that apply.
Reid Spencer5f016e22007-07-11 17:01:13 +00001241///
James Dennette30d3ff2012-06-17 04:36:28 +00001242/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001243/// postfix-expression: [C99 6.5.2]
1244/// primary-expression
1245/// postfix-expression '[' expression ']'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001246/// postfix-expression '[' braced-init-list ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001247/// postfix-expression '(' argument-expression-list[opt] ')'
1248/// postfix-expression '.' identifier
1249/// postfix-expression '->' identifier
1250/// postfix-expression '++'
1251/// postfix-expression '--'
1252/// '(' type-name ')' '{' initializer-list '}'
1253/// '(' type-name ')' '{' initializer-list ',' '}'
1254///
1255/// argument-expression-list: [C99 6.5.2]
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00001256/// argument-expression ...[opt]
1257/// argument-expression-list ',' assignment-expression ...[opt]
James Dennette30d3ff2012-06-17 04:36:28 +00001258/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00001259ExprResult
1260Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 // Now that the primary-expression piece of the postfix-expression has been
1262 // parsed, see if there are any postfix-expression pieces here.
1263 SourceLocation Loc;
1264 while (1) {
1265 switch (Tok.getKind()) {
Douglas Gregor78edf512010-09-15 16:23:04 +00001266 case tok::code_completion:
1267 if (InMessageExpression)
1268 return move(LHS);
1269
Douglas Gregorac5fd842010-09-18 01:28:11 +00001270 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001271 cutOffParsing();
1272 return ExprError();
Douglas Gregor78edf512010-09-15 16:23:04 +00001273
Douglas Gregor0fbda682010-09-15 14:51:05 +00001274 case tok::identifier:
1275 // If we see identifier: after an expression, and we're not already in a
1276 // message send, then this is probably a message send with a missing
1277 // opening bracket '['.
David Blaikie4e4d0842012-03-11 07:00:24 +00001278 if (getLangOpts().ObjC1 && !InMessageExpression &&
Douglas Gregorb65042d2010-09-15 14:54:45 +00001279 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001280 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1281 ParsedType(), LHS.get());
1282 break;
1283 }
1284
1285 // Fall through; this isn't a message send.
1286
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001288 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattnerc59cb382010-05-31 18:18:22 +00001290 // If we have a array postfix expression that starts on a new line and
1291 // Objective-C is enabled, it is highly likely that the user forgot a
1292 // semicolon after the base expression and that the array postfix-expr is
1293 // actually another message send. In this case, do some look-ahead to see
1294 // if the contents of the square brackets are obviously not a valid
1295 // expression and recover by pretending there is no suffix.
David Blaikie4e4d0842012-03-11 07:00:24 +00001296 if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
Chris Lattnerc59cb382010-05-31 18:18:22 +00001297 isSimpleObjCMessageExpression())
Douglas Gregor1b730e82010-05-31 14:40:22 +00001298 return move(LHS);
Richard Smith6ee326a2012-04-10 01:32:12 +00001299
1300 // Reject array indices starting with a lambda-expression. '[[' is
1301 // reserved for attributes.
1302 if (CheckProhibitedCXX11Attribute())
1303 return ExprError();
1304
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001305 BalancedDelimiterTracker T(*this, tok::l_square);
1306 T.consumeOpen();
1307 Loc = T.getOpenLocation();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001308 ExprResult Idx;
David Blaikie4e4d0842012-03-11 07:00:24 +00001309 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00001310 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001311 Idx = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001312 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001313 Idx = ParseExpression();
Sebastian Redla55e52c2008-11-25 22:21:31 +00001314
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001316
1317 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCall9ae2f072010-08-23 23:25:46 +00001318 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1319 Idx.take(), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001320 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001321 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001322
1323 // Match the ']'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001324 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001325 break;
1326 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001327
Peter Collingbournebf36e252011-02-09 21:12:02 +00001328 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1329 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1330 // '(' argument-expression-list[opt] ')'
1331 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregor0fbda682010-09-15 14:51:05 +00001332 InMessageExpressionRAIIObject InMessage(*this, false);
1333
Peter Collingbournebf36e252011-02-09 21:12:02 +00001334 Expr *ExecConfig = 0;
1335
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001336 BalancedDelimiterTracker PT(*this, tok::l_paren);
1337
Peter Collingbournebf36e252011-02-09 21:12:02 +00001338 if (OpKind == tok::lesslessless) {
1339 ExprVector ExecConfigExprs(Actions);
1340 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001341 SourceLocation OpenLoc = ConsumeToken();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001342
1343 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1344 LHS = ExprError();
1345 }
1346
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001347 SourceLocation CloseLoc = Tok.getLocation();
1348 if (Tok.is(tok::greatergreatergreater)) {
1349 ConsumeToken();
1350 } else if (LHS.isInvalid()) {
Peter Collingbournebf36e252011-02-09 21:12:02 +00001351 SkipUntil(tok::greatergreatergreater);
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001352 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001353 // There was an error closing the brackets
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001354 Diag(Tok, diag::err_expected_ggg);
1355 Diag(OpenLoc, diag::note_matching) << "<<<";
1356 SkipUntil(tok::greatergreatergreater);
Peter Collingbournebf36e252011-02-09 21:12:02 +00001357 LHS = ExprError();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001358 }
1359
1360 if (!LHS.isInvalid()) {
1361 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1362 LHS = ExprError();
1363 else
1364 Loc = PrevTokLocation;
1365 }
1366
1367 if (!LHS.isInvalid()) {
1368 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001369 OpenLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001370 move_arg(ExecConfigExprs),
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001371 CloseLoc);
Peter Collingbournebf36e252011-02-09 21:12:02 +00001372 if (ECResult.isInvalid())
1373 LHS = ExprError();
1374 else
1375 ExecConfig = ECResult.get();
1376 }
1377 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001378 PT.consumeOpen();
1379 Loc = PT.getOpenLocation();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001380 }
1381
Sebastian Redla55e52c2008-11-25 22:21:31 +00001382 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001383 CommaLocsTy CommaLocs;
Douglas Gregorac5fd842010-09-18 01:28:11 +00001384
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001385 if (Tok.is(tok::code_completion)) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00001386 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1387 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001388 cutOffParsing();
1389 return ExprError();
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001390 }
Peter Collingbournebf36e252011-02-09 21:12:02 +00001391
1392 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1393 if (Tok.isNot(tok::r_paren)) {
1394 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1395 LHS.get())) {
Peter Collingbournebf36e252011-02-09 21:12:02 +00001396 LHS = ExprError();
1397 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001398 }
1399 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001400
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 // Match the ')'.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001402 if (LHS.isInvalid()) {
1403 SkipUntil(tok::r_paren);
1404 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001405 PT.consumeClose();
Douglas Gregorac5fd842010-09-18 01:28:11 +00001406 LHS = ExprError();
1407 } else {
1408 assert((ArgExprs.size() == 0 ||
1409 ArgExprs.size()-1 == CommaLocs.size())&&
Reid Spencer5f016e22007-07-11 17:01:13 +00001410 "Unexpected number of commas!");
John McCall9ae2f072010-08-23 23:25:46 +00001411 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbournebf36e252011-02-09 21:12:02 +00001412 move_arg(ArgExprs), Tok.getLocation(),
1413 ExecConfig);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001414 PT.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001415 }
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 break;
1418 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001419 case tok::arrow:
1420 case tok::period: {
1421 // postfix-expression: p-e '->' template[opt] id-expression
1422 // postfix-expression: p-e '.' template[opt] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 tok::TokenKind OpKind = Tok.getKind();
1424 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001425
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001426 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001427 ParsedType ObjectType;
Douglas Gregord4dca082010-02-24 18:44:31 +00001428 bool MayBePseudoDestructor = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001429 if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
John McCall9ae2f072010-08-23 23:25:46 +00001430 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregord4dca082010-02-24 18:44:31 +00001431 OpLoc, OpKind, ObjectType,
1432 MayBePseudoDestructor);
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001433 if (LHS.isInvalid())
1434 break;
Douglas Gregord4dca082010-02-24 18:44:31 +00001435
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001436 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1437 /*EnteringContext=*/false,
Douglas Gregord4dca082010-02-24 18:44:31 +00001438 &MayBePseudoDestructor);
Douglas Gregor9f716e42010-05-27 15:25:59 +00001439 if (SS.isNotEmpty())
John McCallb3d87482010-08-24 05:47:05 +00001440 ObjectType = ParsedType();
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001441 }
1442
Douglas Gregor81b747b2009-09-17 21:32:03 +00001443 if (Tok.is(tok::code_completion)) {
1444 // Code completion for a member access expression.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001445 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor81b747b2009-09-17 21:32:03 +00001446 OpLoc, OpKind == tok::arrow);
1447
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001448 cutOffParsing();
1449 return ExprError();
Douglas Gregor81b747b2009-09-17 21:32:03 +00001450 }
1451
John McCall9ae2f072010-08-23 23:25:46 +00001452 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1453 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregord4dca082010-02-24 18:44:31 +00001454 ObjectType);
1455 break;
1456 }
1457
1458 // Either the action has told is that this cannot be a
1459 // pseudo-destructor expression (based on the type of base
1460 // expression), or we didn't see a '~' in the right place. We
1461 // can still parse a destructor name here, but in that case it
1462 // names a real destructor.
Francois Pichetdbee3412011-01-18 05:04:39 +00001463 // Allow explicit constructor calls in Microsoft mode.
1464 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001465 SourceLocation TemplateKWLoc;
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001466 UnqualifiedId Name;
David Blaikie4e4d0842012-03-11 07:00:24 +00001467 if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
Douglas Gregor8f70bda2012-02-16 18:19:22 +00001468 // Objective-C++:
1469 // After a '.' in a member access expression, treat the keyword
1470 // 'class' as if it were an identifier.
1471 //
1472 // This hack allows property access to the 'class' method because it is
1473 // such a common method name. For other C++ keywords that are
1474 // Objective-C method names, one must use the message send syntax.
1475 IdentifierInfo *Id = Tok.getIdentifierInfo();
1476 SourceLocation Loc = ConsumeToken();
1477 Name.setIdentifier(Id, Loc);
1478 } else if (ParseUnqualifiedId(SS,
1479 /*EnteringContext=*/false,
1480 /*AllowDestructorName=*/true,
1481 /*AllowConstructorName=*/
David Blaikie4e4d0842012-03-11 07:00:24 +00001482 getLangOpts().MicrosoftExt,
Douglas Gregor8f70bda2012-02-16 18:19:22 +00001483 ObjectType, TemplateKWLoc, Name))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001484 LHS = ExprError();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001485
1486 if (!LHS.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001487 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001488 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001489 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1490 Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 break;
1492 }
1493 case tok::plusplus: // postfix-expression: postfix-expression '++'
1494 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001495 if (!LHS.isInvalid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001496 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001497 Tok.getKind(), LHS.take());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001498 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 ConsumeToken();
1500 break;
1501 }
1502 }
1503}
1504
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001505/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1506/// vec_step and we are at the start of an expression or a parenthesized
1507/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1508/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001509///
James Dennette30d3ff2012-06-17 04:36:28 +00001510/// \verbatim
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001511/// unary-expression: [C99 6.5.3]
1512/// 'sizeof' unary-expression
1513/// 'sizeof' '(' type-name ')'
1514/// [GNU] '__alignof' unary-expression
1515/// [GNU] '__alignof' '(' type-name ')'
1516/// [C++0x] 'alignof' '(' type-id ')'
1517///
1518/// [GNU] typeof-specifier:
1519/// typeof ( expressions )
1520/// typeof ( type-name )
1521/// [GNU/C++] typeof unary-expression
1522///
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001523/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1524/// vec_step ( expressions )
1525/// vec_step ( type-name )
James Dennette30d3ff2012-06-17 04:36:28 +00001526/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00001527ExprResult
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001528Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1529 bool &isCastExpr,
1530 ParsedType &CastTy,
1531 SourceRange &CastRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00001532
1533 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001534 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1535 OpTok.is(tok::kw_vec_step)) &&
1536 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001537
John McCall60d7b3a2010-08-24 06:29:42 +00001538 ExprResult Operand;
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001540 // If the operand doesn't start with an '(', it must be an expression.
1541 if (Tok.isNot(tok::l_paren)) {
1542 isCastExpr = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001543 if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001544 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1545 return ExprError();
1546 }
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001548 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001549 } else {
1550 // If it starts with a '(', we know that it is either a parenthesized
1551 // type-name, or it is a unary-expression that starts with a compound
1552 // literal, or starts with a primary-expression that is a parenthesized
1553 // expression.
1554 ParenParseOption ExprType = CastExpr;
1555 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001557 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001558 false, CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001559 CastRange = SourceRange(LParenLoc, RParenLoc);
1560
1561 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1562 // a type.
1563 if (ExprType == CastExpr) {
1564 isCastExpr = true;
1565 return ExprEmpty();
1566 }
1567
David Blaikie4e4d0842012-03-11 07:00:24 +00001568 if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001569 // GNU typeof in C requires the expression to be parenthesized. Not so for
1570 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1571 // the start of a unary-expression, but doesn't include any postfix
1572 // pieces. Parse these now if present.
John McCall124300e2010-08-24 23:41:43 +00001573 if (!Operand.isInvalid())
1574 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001575 }
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001576 }
1577
1578 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1579 isCastExpr = false;
1580 return move(Operand);
1581}
1582
Reid Spencer5f016e22007-07-11 17:01:13 +00001583
James Dennette30d3ff2012-06-17 04:36:28 +00001584/// \brief Parse a sizeof or alignof expression.
1585///
1586/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001587/// unary-expression: [C99 6.5.3]
1588/// 'sizeof' unary-expression
1589/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +00001590/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +00001591/// [GNU] '__alignof' unary-expression
1592/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001593/// [C++0x] 'alignof' '(' type-id ')'
James Dennette30d3ff2012-06-17 04:36:28 +00001594/// \endverbatim
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001595ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001596 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001597 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1598 "Not a sizeof/alignof/vec_step expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001599 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001600 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Douglas Gregoree8aff02011-01-04 17:33:58 +00001602 // [C++0x] 'sizeof' '...' '(' identifier ')'
1603 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1604 SourceLocation EllipsisLoc = ConsumeToken();
1605 SourceLocation LParenLoc, RParenLoc;
1606 IdentifierInfo *Name = 0;
1607 SourceLocation NameLoc;
1608 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001609 BalancedDelimiterTracker T(*this, tok::l_paren);
1610 T.consumeOpen();
1611 LParenLoc = T.getOpenLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001612 if (Tok.is(tok::identifier)) {
1613 Name = Tok.getIdentifierInfo();
1614 NameLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001615 T.consumeClose();
1616 RParenLoc = T.getCloseLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001617 if (RParenLoc.isInvalid())
1618 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1619 } else {
1620 Diag(Tok, diag::err_expected_parameter_pack);
1621 SkipUntil(tok::r_paren);
1622 }
1623 } else if (Tok.is(tok::identifier)) {
1624 Name = Tok.getIdentifierInfo();
1625 NameLoc = ConsumeToken();
1626 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1627 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1628 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1629 << Name
1630 << FixItHint::CreateInsertion(LParenLoc, "(")
1631 << FixItHint::CreateInsertion(RParenLoc, ")");
1632 } else {
1633 Diag(Tok, diag::err_sizeof_parameter_pack);
1634 }
1635
1636 if (!Name)
1637 return ExprError();
1638
1639 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1640 OpTok.getLocation(),
1641 *Name, NameLoc,
1642 RParenLoc);
1643 }
Richard Smith841804b2011-10-17 23:06:20 +00001644
1645 if (OpTok.is(tok::kw_alignof))
1646 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1647
Eli Friedman71b8fb52012-01-21 01:01:51 +00001648 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1649
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001650 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00001651 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001652 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001653 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1654 isCastExpr,
1655 CastTy,
1656 CastRange);
1657
1658 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1659 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1660 ExprKind = UETT_AlignOf;
1661 else if (OpTok.is(tok::kw_vec_step))
1662 ExprKind = UETT_VecStep;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001663
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001664 if (isCastExpr)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001665 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1666 ExprKind,
1667 /*isType=*/true,
1668 CastTy.getAsOpaquePtr(),
1669 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001670
Reid Spencer5f016e22007-07-11 17:01:13 +00001671 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001672 if (!Operand.isInvalid())
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001673 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1674 ExprKind,
1675 /*isType=*/false,
1676 Operand.release(),
1677 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001678 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001679}
1680
1681/// ParseBuiltinPrimaryExpression
1682///
James Dennette30d3ff2012-06-17 04:36:28 +00001683/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001684/// primary-expression: [C99 6.5.1]
1685/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1686/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1687/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1688/// assign-expr ')'
1689/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbournea1606712011-11-05 03:47:48 +00001690/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001691///
Reid Spencer5f016e22007-07-11 17:01:13 +00001692/// [GNU] offsetof-member-designator:
1693/// [GNU] identifier
1694/// [GNU] offsetof-member-designator '.' identifier
1695/// [GNU] offsetof-member-designator '[' expression ']'
James Dennette30d3ff2012-06-17 04:36:28 +00001696/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00001697ExprResult Parser::ParseBuiltinPrimaryExpression() {
1698 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1700
1701 tok::TokenKind T = Tok.getKind();
1702 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1703
1704 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001705 if (Tok.isNot(tok::l_paren))
1706 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1707 << BuiltinII);
1708
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001709 BalancedDelimiterTracker PT(*this, tok::l_paren);
1710 PT.consumeOpen();
1711
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 // TODO: Build AST.
1713
1714 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001715 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001716 case tok::kw___builtin_va_arg: {
John McCall60d7b3a2010-08-24 06:29:42 +00001717 ExprResult Expr(ParseAssignmentExpression());
Reid Spencer5f016e22007-07-11 17:01:13 +00001718
1719 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001720 Expr = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001721
Douglas Gregor809070a2009-02-18 17:45:20 +00001722 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001723
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001724 if (Tok.isNot(tok::r_paren)) {
1725 Diag(Tok, diag::err_expected_rparen);
Douglas Gregorac5fd842010-09-18 01:28:11 +00001726 Expr = ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001727 }
Douglas Gregorac5fd842010-09-18 01:28:11 +00001728
1729 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor809070a2009-02-18 17:45:20 +00001730 Res = ExprError();
1731 else
John McCall9ae2f072010-08-23 23:25:46 +00001732 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001733 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001734 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001735 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001736 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001737 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001738 if (Ty.isInvalid()) {
1739 SkipUntil(tok::r_paren);
1740 return ExprError();
1741 }
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Reid Spencer5f016e22007-07-11 17:01:13 +00001743 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001744 return ExprError();
1745
Reid Spencer5f016e22007-07-11 17:01:13 +00001746 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001747 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001748 Diag(Tok, diag::err_expected_ident);
1749 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001750 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001751 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001752
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001753 // Keep track of the various subcomponents we see.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001754 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001755
John McCallf312b1e2010-08-26 23:41:50 +00001756 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001757 Comps.back().isBrackets = false;
1758 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1759 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001760
Sebastian Redla55e52c2008-11-25 22:21:31 +00001761 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001762 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001763 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallf312b1e2010-08-26 23:41:50 +00001765 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001766 Comps.back().isBrackets = false;
1767 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001768
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001769 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001770 Diag(Tok, diag::err_expected_ident);
1771 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001772 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001773 }
1774 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1775 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001776
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001777 } else if (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00001778 if (CheckProhibitedCXX11Attribute())
1779 return ExprError();
1780
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallf312b1e2010-08-26 23:41:50 +00001782 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001783 Comps.back().isBrackets = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001784 BalancedDelimiterTracker ST(*this, tok::l_square);
1785 ST.consumeOpen();
1786 Comps.back().LocStart = ST.getOpenLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001787 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001788 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001790 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001792 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001793
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001794 ST.consumeClose();
1795 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001796 } else {
1797 if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001798 PT.consumeClose();
Douglas Gregor809070a2009-02-18 17:45:20 +00001799 Res = ExprError();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001800 } else if (Ty.isInvalid()) {
1801 Res = ExprError();
1802 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001803 PT.consumeClose();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001804 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001805 Ty.get(), &Comps[0], Comps.size(),
1806 PT.getCloseLocation());
Eli Friedman309fe0d2009-06-27 20:38:33 +00001807 }
Chris Lattner6eb21092007-08-30 15:52:49 +00001808 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001809 }
1810 }
1811 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001812 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001813 case tok::kw___builtin_choose_expr: {
John McCall60d7b3a2010-08-24 06:29:42 +00001814 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001815 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001816 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001817 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001818 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001819 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001820 return ExprError();
1821
John McCall60d7b3a2010-08-24 06:29:42 +00001822 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001823 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001824 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001825 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001826 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001827 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001828 return ExprError();
1829
John McCall60d7b3a2010-08-24 06:29:42 +00001830 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001831 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001832 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001833 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001834 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001835 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001836 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001837 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001838 }
John McCall9ae2f072010-08-23 23:25:46 +00001839 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1840 Expr2.take(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001841 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001842 }
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001843 case tok::kw___builtin_astype: {
1844 // The first argument is an expression to be converted, followed by a comma.
1845 ExprResult Expr(ParseAssignmentExpression());
1846 if (Expr.isInvalid()) {
1847 SkipUntil(tok::r_paren);
1848 return ExprError();
1849 }
1850
1851 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1852 tok::r_paren))
1853 return ExprError();
1854
1855 // Second argument is the type to bitcast to.
1856 TypeResult DestTy = ParseTypeName();
1857 if (DestTy.isInvalid())
1858 return ExprError();
1859
1860 // Attempt to consume the r-paren.
1861 if (Tok.isNot(tok::r_paren)) {
1862 Diag(Tok, diag::err_expected_rparen);
1863 SkipUntil(tok::r_paren);
1864 return ExprError();
1865 }
1866
1867 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1868 ConsumeParen());
1869 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001870 }
Chandler Carruth3c7fddd2011-07-08 04:59:44 +00001871 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001872
John McCall9ae2f072010-08-23 23:25:46 +00001873 if (Res.isInvalid())
1874 return ExprError();
1875
Reid Spencer5f016e22007-07-11 17:01:13 +00001876 // These can be followed by postfix-expr pieces because they are
1877 // primary-expressions.
John McCall9ae2f072010-08-23 23:25:46 +00001878 return ParsePostfixExpressionSuffix(Res.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001879}
1880
1881/// ParseParenExpression - This parses the unit that starts with a '(' token,
1882/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001883/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1884/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001885///
James Dennette30d3ff2012-06-17 04:36:28 +00001886/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001887/// primary-expression: [C99 6.5.1]
1888/// '(' expression ')'
1889/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1890/// postfix-expression: [C99 6.5.2]
1891/// '(' type-name ')' '{' initializer-list '}'
1892/// '(' type-name ')' '{' initializer-list ',' '}'
1893/// cast-expression: [C99 6.5.4]
1894/// '(' type-name ')' cast-expression
John McCallf85e1932011-06-15 23:02:42 +00001895/// [ARC] bridged-cast-expression
1896///
1897/// [ARC] bridged-cast-expression:
1898/// (__bridge type-name) cast-expression
1899/// (__bridge_transfer type-name) cast-expression
1900/// (__bridge_retained type-name) cast-expression
James Dennette30d3ff2012-06-17 04:36:28 +00001901/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00001902ExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001903Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001904 bool isTypeCast, ParsedType &CastTy,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001905 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001906 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001907 BalancedDelimiterTracker T(*this, tok::l_paren);
1908 if (T.consumeOpen())
1909 return ExprError();
1910 SourceLocation OpenLoc = T.getOpenLocation();
1911
John McCall60d7b3a2010-08-24 06:29:42 +00001912 ExprResult Result(true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001913 bool isAmbiguousTypeId;
John McCallb3d87482010-08-24 05:47:05 +00001914 CastTy = ParsedType();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001915
Douglas Gregor02688102010-09-14 23:59:36 +00001916 if (Tok.is(tok::code_completion)) {
1917 Actions.CodeCompleteOrdinaryName(getCurScope(),
1918 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1919 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001920 cutOffParsing();
Douglas Gregor02688102010-09-14 23:59:36 +00001921 return ExprError();
1922 }
John McCallb3c49062011-04-06 02:35:25 +00001923
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001924 // Diagnose use of bridge casts in non-arc mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00001925 bool BridgeCast = (getLangOpts().ObjC2 &&
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001926 (Tok.is(tok::kw___bridge) ||
1927 Tok.is(tok::kw___bridge_transfer) ||
1928 Tok.is(tok::kw___bridge_retained) ||
1929 Tok.is(tok::kw___bridge_retain)));
David Blaikie4e4d0842012-03-11 07:00:24 +00001930 if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001931 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001932 SourceLocation BridgeKeywordLoc = ConsumeToken();
1933 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremeneke698a5c2012-02-18 04:42:38 +00001934 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001935 << BridgeCastName
1936 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001937 BridgeCast = false;
1938 }
1939
John McCallb3c49062011-04-06 02:35:25 +00001940 // None of these cases should fall through with an invalid Result
1941 // unless they've already reported an error.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001942 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001943 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall73f428c2012-04-04 01:27:53 +00001944 Actions.ActOnStartStmtExpr();
1945
Richard Smith534986f2012-04-14 00:33:13 +00001946 StmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001947 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001948
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001949 // If the substmt parsed correctly, build the AST node.
John McCall73f428c2012-04-04 01:27:53 +00001950 if (!Stmt.isInvalid()) {
John McCall9ae2f072010-08-23 23:25:46 +00001951 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
John McCall73f428c2012-04-04 01:27:53 +00001952 } else {
1953 Actions.ActOnStmtExprError();
1954 }
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001955 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCallb64915a2011-06-17 21:56:12 +00001956 tok::TokenKind tokenKind = Tok.getKind();
1957 SourceLocation BridgeKeywordLoc = ConsumeToken();
1958
John McCallf85e1932011-06-15 23:02:42 +00001959 // Parse an Objective-C ARC ownership cast expression.
1960 ObjCBridgeCastKind Kind;
John McCallb64915a2011-06-17 21:56:12 +00001961 if (tokenKind == tok::kw___bridge)
John McCallf85e1932011-06-15 23:02:42 +00001962 Kind = OBC_Bridge;
John McCallb64915a2011-06-17 21:56:12 +00001963 else if (tokenKind == tok::kw___bridge_transfer)
John McCallf85e1932011-06-15 23:02:42 +00001964 Kind = OBC_BridgeTransfer;
John McCallb64915a2011-06-17 21:56:12 +00001965 else if (tokenKind == tok::kw___bridge_retained)
John McCallf85e1932011-06-15 23:02:42 +00001966 Kind = OBC_BridgeRetained;
John McCallb64915a2011-06-17 21:56:12 +00001967 else {
1968 // As a hopefully temporary workaround, allow __bridge_retain as
1969 // a synonym for __bridge_retained, but only in system headers.
1970 assert(tokenKind == tok::kw___bridge_retain);
1971 Kind = OBC_BridgeRetained;
1972 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1973 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1974 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1975 "__bridge_retained");
1976 }
John McCallf85e1932011-06-15 23:02:42 +00001977
John McCallf85e1932011-06-15 23:02:42 +00001978 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001979 T.consumeClose();
1980 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001981 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCallf85e1932011-06-15 23:02:42 +00001982
1983 if (Ty.isInvalid() || SubExpr.isInvalid())
1984 return ExprError();
1985
1986 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1987 BridgeKeywordLoc, Ty.get(),
1988 RParenLoc, SubExpr.get());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001989 } else if (ExprType >= CompoundLiteral &&
1990 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Reid Spencer5f016e22007-07-11 17:01:13 +00001992 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001994 // In C++, if the type-id is ambiguous we disambiguate based on context.
1995 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1996 // in which case we should treat it as type-id.
1997 // if stopIfCastExpr is false, we need to determine the context past the
1998 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001999 if (isAmbiguousTypeId && !stopIfCastExpr) {
2000 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
2001 RParenLoc = T.getCloseLocation();
2002 return res;
2003 }
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002005 // Parse the type declarator.
2006 DeclSpec DS(AttrFactory);
2007 ParseSpecifierQualifierList(DS);
2008 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2009 ParseDeclarator(DeclaratorInfo);
Douglas Gregor0fbda682010-09-15 14:51:05 +00002010
Douglas Gregor77328d12010-09-15 23:19:31 +00002011 // If our type is followed by an identifier and either ':' or ']', then
2012 // this is probably an Objective-C message send where the leading '[' is
2013 // missing. Recover as if that were the case.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002014 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
David Blaikie4e4d0842012-03-11 07:00:24 +00002015 !InMessageExpression && getLangOpts().ObjC1 &&
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002016 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
2017 TypeResult Ty;
2018 {
2019 InMessageExpressionRAIIObject InMessage(*this, false);
2020 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2021 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002022 Result = ParseObjCMessageExpressionBody(SourceLocation(),
2023 SourceLocation(),
2024 Ty.get(), 0);
2025 } else {
2026 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002027 T.consumeClose();
2028 RParenLoc = T.getCloseLocation();
Douglas Gregor77328d12010-09-15 23:19:31 +00002029 if (Tok.is(tok::l_brace)) {
2030 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002031 TypeResult Ty;
2032 {
2033 InMessageExpressionRAIIObject InMessage(*this, false);
2034 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2035 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002036 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +00002037 }
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00002038
Douglas Gregor77328d12010-09-15 23:19:31 +00002039 if (ExprType == CastExpr) {
2040 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00002041
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor77328d12010-09-15 23:19:31 +00002043 return ExprError();
2044
Douglas Gregor77328d12010-09-15 23:19:31 +00002045 // Note that this doesn't parse the subsequent cast-expression, it just
2046 // returns the parsed type to the callee.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002047 if (stopIfCastExpr) {
2048 TypeResult Ty;
2049 {
2050 InMessageExpressionRAIIObject InMessage(*this, false);
2051 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2052 }
2053 CastTy = Ty.get();
Douglas Gregor77328d12010-09-15 23:19:31 +00002054 return ExprResult();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002055 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002056
2057 // Reject the cast of super idiom in ObjC.
David Blaikie4e4d0842012-03-11 07:00:24 +00002058 if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
Douglas Gregor77328d12010-09-15 23:19:31 +00002059 Tok.getIdentifierInfo() == Ident_super &&
2060 getCurScope()->isInObjcMethodScope() &&
2061 GetLookAheadToken(1).isNot(tok::period)) {
2062 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2063 << SourceRange(OpenLoc, RParenLoc);
2064 return ExprError();
2065 }
2066
2067 // Parse the cast-expression that follows it next.
2068 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002069 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2070 /*isAddressOfOperand=*/false,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002071 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002072 if (!Result.isInvalid()) {
2073 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2074 DeclaratorInfo, CastTy,
Douglas Gregor77328d12010-09-15 23:19:31 +00002075 RParenLoc, Result.take());
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002076 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002077 return move(Result);
2078 }
2079
2080 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2081 return ExprError();
2082 }
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002083 } else if (isTypeCast) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00002084 // Parse the expression-list.
Douglas Gregor0fbda682010-09-15 14:51:05 +00002085 InMessageExpressionRAIIObject InMessage(*this, false);
2086
Nate Begeman2ef13e52009-08-10 23:49:36 +00002087 ExprVector ArgExprs(Actions);
2088 CommaLocsTy CommaLocs;
2089
2090 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2091 ExprType = SimpleExpr;
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002092 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
2093 move_arg(ArgExprs));
Nate Begeman2ef13e52009-08-10 23:49:36 +00002094 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002095 } else {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002096 InMessageExpressionRAIIObject InMessage(*this, false);
2097
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002098 Result = ParseExpression(MaybeTypeCast);
Reid Spencer5f016e22007-07-11 17:01:13 +00002099 ExprType = SimpleExpr;
John McCallb3c49062011-04-06 02:35:25 +00002100
2101 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002102 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002103 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00002104 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00002105
Reid Spencer5f016e22007-07-11 17:01:13 +00002106 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00002107 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002108 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00002109 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 }
Mike Stump1eb44332009-09-09 15:08:12 +00002111
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002112 T.consumeClose();
2113 RParenLoc = T.getCloseLocation();
Sebastian Redld8c4e152008-12-11 22:33:27 +00002114 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00002115}
2116
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002117/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2118/// and we are at the left brace.
2119///
James Dennette30d3ff2012-06-17 04:36:28 +00002120/// \verbatim
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002121/// postfix-expression: [C99 6.5.2]
2122/// '(' type-name ')' '{' initializer-list '}'
2123/// '(' type-name ')' '{' initializer-list ',' '}'
James Dennette30d3ff2012-06-17 04:36:28 +00002124/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00002125ExprResult
John McCallb3d87482010-08-24 05:47:05 +00002126Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002127 SourceLocation LParenLoc,
2128 SourceLocation RParenLoc) {
2129 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
David Blaikie4e4d0842012-03-11 07:00:24 +00002130 if (!getLangOpts().C99) // Compound literals don't exist in C90.
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002131 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCall60d7b3a2010-08-24 06:29:42 +00002132 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002133 if (!Result.isInvalid() && Ty)
John McCall9ae2f072010-08-23 23:25:46 +00002134 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002135 return move(Result);
2136}
2137
Reid Spencer5f016e22007-07-11 17:01:13 +00002138/// ParseStringLiteralExpression - This handles the various token types that
2139/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2140/// translation phase #6].
2141///
James Dennette30d3ff2012-06-17 04:36:28 +00002142/// \verbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00002143/// primary-expression: [C99 6.5.1]
2144/// string-literal
James Dennette30d3ff2012-06-17 04:36:28 +00002145/// \verbatim
Richard Smith99831e42012-03-06 03:21:47 +00002146ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002147 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00002148
Reid Spencer5f016e22007-07-11 17:01:13 +00002149 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2150 // considered to be strings for concatenation purposes.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002151 SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00002152
Reid Spencer5f016e22007-07-11 17:01:13 +00002153 do {
2154 StringToks.push_back(Tok);
2155 ConsumeStringToken();
2156 } while (isTokenStringLiteral());
2157
2158 // Pass the set of string tokens, ready for concatenation, to the actions.
Richard Smith36f5cfe2012-03-09 08:00:36 +00002159 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(),
2160 AllowUserDefinedLiteral ? getCurScope() : 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00002161}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002162
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002163/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2164/// [C11 6.5.1.1].
Peter Collingbournef111d932011-04-15 00:35:48 +00002165///
James Dennette30d3ff2012-06-17 04:36:28 +00002166/// \verbatim
Peter Collingbournef111d932011-04-15 00:35:48 +00002167/// generic-selection:
2168/// _Generic ( assignment-expression , generic-assoc-list )
2169/// generic-assoc-list:
2170/// generic-association
2171/// generic-assoc-list , generic-association
2172/// generic-association:
2173/// type-name : assignment-expression
2174/// default : assignment-expression
James Dennette30d3ff2012-06-17 04:36:28 +00002175/// \endverbatim
Peter Collingbournef111d932011-04-15 00:35:48 +00002176ExprResult Parser::ParseGenericSelectionExpression() {
2177 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2178 SourceLocation KeyLoc = ConsumeToken();
2179
David Blaikie4e4d0842012-03-11 07:00:24 +00002180 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002181 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbournef111d932011-04-15 00:35:48 +00002182
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002183 BalancedDelimiterTracker T(*this, tok::l_paren);
2184 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbournef111d932011-04-15 00:35:48 +00002185 return ExprError();
2186
2187 ExprResult ControllingExpr;
2188 {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002189 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbournef111d932011-04-15 00:35:48 +00002190 // not evaluated."
2191 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2192 ControllingExpr = ParseAssignmentExpression();
2193 if (ControllingExpr.isInvalid()) {
2194 SkipUntil(tok::r_paren);
2195 return ExprError();
2196 }
2197 }
2198
2199 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2200 SkipUntil(tok::r_paren);
2201 return ExprError();
2202 }
2203
2204 SourceLocation DefaultLoc;
2205 TypeVector Types(Actions);
2206 ExprVector Exprs(Actions);
2207 while (1) {
2208 ParsedType Ty;
2209 if (Tok.is(tok::kw_default)) {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002210 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbournef111d932011-04-15 00:35:48 +00002211 // generic association."
2212 if (!DefaultLoc.isInvalid()) {
2213 Diag(Tok, diag::err_duplicate_default_assoc);
2214 Diag(DefaultLoc, diag::note_previous_default_assoc);
2215 SkipUntil(tok::r_paren);
2216 return ExprError();
2217 }
2218 DefaultLoc = ConsumeToken();
2219 Ty = ParsedType();
2220 } else {
2221 ColonProtectionRAIIObject X(*this);
2222 TypeResult TR = ParseTypeName();
2223 if (TR.isInvalid()) {
2224 SkipUntil(tok::r_paren);
2225 return ExprError();
2226 }
2227 Ty = TR.release();
2228 }
2229 Types.push_back(Ty);
2230
2231 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2232 SkipUntil(tok::r_paren);
2233 return ExprError();
2234 }
2235
2236 // FIXME: These expressions should be parsed in a potentially potentially
2237 // evaluated context.
2238 ExprResult ER(ParseAssignmentExpression());
2239 if (ER.isInvalid()) {
2240 SkipUntil(tok::r_paren);
2241 return ExprError();
2242 }
2243 Exprs.push_back(ER.release());
2244
2245 if (Tok.isNot(tok::comma))
2246 break;
2247 ConsumeToken();
2248 }
2249
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002250 T.consumeClose();
2251 if (T.getCloseLocation().isInvalid())
Peter Collingbournef111d932011-04-15 00:35:48 +00002252 return ExprError();
2253
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002254 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2255 T.getCloseLocation(),
Peter Collingbournef111d932011-04-15 00:35:48 +00002256 ControllingExpr.release(),
2257 move_arg(Types), move_arg(Exprs));
2258}
2259
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002260/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2261///
James Dennette30d3ff2012-06-17 04:36:28 +00002262/// \verbatim
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002263/// argument-expression-list:
2264/// assignment-expression
2265/// argument-expression-list , assignment-expression
2266///
2267/// [C++] expression-list:
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002268/// [C++] assignment-expression
2269/// [C++] expression-list , assignment-expression
2270///
2271/// [C++0x] expression-list:
2272/// [C++0x] initializer-list
2273///
2274/// [C++0x] initializer-list
2275/// [C++0x] initializer-clause ...[opt]
2276/// [C++0x] initializer-list , initializer-clause ...[opt]
2277///
2278/// [C++0x] initializer-clause:
2279/// [C++0x] assignment-expression
2280/// [C++0x] braced-init-list
James Dennette30d3ff2012-06-17 04:36:28 +00002281/// \endverbatim
Chris Lattner5f9e2722011-07-23 10:55:15 +00002282bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2283 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallf312b1e2010-08-26 23:41:50 +00002284 void (Sema::*Completer)(Scope *S,
John McCallca0408f2010-08-23 06:44:23 +00002285 Expr *Data,
Ahmed Charles13a140c2012-02-25 11:00:22 +00002286 llvm::ArrayRef<Expr *> Args),
John McCallca0408f2010-08-23 06:44:23 +00002287 Expr *Data) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002288 while (1) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002289 if (Tok.is(tok::code_completion)) {
2290 if (Completer)
Ahmed Charles13a140c2012-02-25 11:00:22 +00002291 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor4706e872011-02-17 03:09:23 +00002292 else
2293 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002294 cutOffParsing();
2295 return true;
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002296 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002297
2298 ExprResult Expr;
David Blaikie4e4d0842012-03-11 07:00:24 +00002299 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002300 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002301 Expr = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00002302 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002303 Expr = ParseAssignmentExpression();
2304
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002305 if (Tok.is(tok::ellipsis))
2306 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002307 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002308 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00002309
Sebastian Redleffa8d12008-12-10 00:02:53 +00002310 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002311
2312 if (Tok.isNot(tok::comma))
2313 return false;
2314 // Move to the next argument, remember where the comma was.
2315 CommaLocs.push_back(ConsumeToken());
2316 }
2317}
Steve Naroff296e8d52008-08-28 19:20:44 +00002318
Mike Stump98eb8a72009-02-04 22:31:32 +00002319/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2320///
James Dennette30d3ff2012-06-17 04:36:28 +00002321/// \verbatim
Mike Stump98eb8a72009-02-04 22:31:32 +00002322/// [clang] block-id:
2323/// [clang] specifier-qualifier-list block-declarator
James Dennette30d3ff2012-06-17 04:36:28 +00002324/// \endverbatim
Douglas Gregor03f1eb02012-06-15 16:59:29 +00002325void Parser::ParseBlockId(SourceLocation CaretLoc) {
Douglas Gregor75ab4142010-10-18 21:34:55 +00002326 if (Tok.is(tok::code_completion)) {
2327 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002328 return cutOffParsing();
Douglas Gregor75ab4142010-10-18 21:34:55 +00002329 }
2330
Mike Stump98eb8a72009-02-04 22:31:32 +00002331 // Parse the specifier-qualifier-list piece.
John McCall0b7e6782011-03-24 11:26:52 +00002332 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002333 ParseSpecifierQualifierList(DS);
2334
2335 // Parse the block-declarator.
2336 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2337 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002338
Mike Stump6c92fa72009-04-29 21:40:37 +00002339 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00002340 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump6c92fa72009-04-29 21:40:37 +00002341
John McCall7f040a92010-12-24 02:08:15 +00002342 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002343
Mike Stump98eb8a72009-02-04 22:31:32 +00002344 // Inform sema that we are starting a block.
Douglas Gregor03f1eb02012-06-15 16:59:29 +00002345 Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
Mike Stump98eb8a72009-02-04 22:31:32 +00002346}
2347
Steve Naroff296e8d52008-08-28 19:20:44 +00002348/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00002349/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00002350///
James Dennette30d3ff2012-06-17 04:36:28 +00002351/// \verbatim
Steve Naroff296e8d52008-08-28 19:20:44 +00002352/// block-literal:
2353/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00002354/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00002355/// [clang] block-args:
2356/// [clang] '(' parameter-list ')'
James Dennette30d3ff2012-06-17 04:36:28 +00002357/// \endverbatim
John McCall60d7b3a2010-08-24 06:29:42 +00002358ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00002359 assert(Tok.is(tok::caret) && "block literal starts with ^");
2360 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002361
Chris Lattner6b91f002009-03-05 07:32:12 +00002362 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2363 "block literal parsing");
2364
Mike Stump1eb44332009-09-09 15:08:12 +00002365 // Enter a scope to hold everything within the block. This includes the
Steve Naroff296e8d52008-08-28 19:20:44 +00002366 // argument decls, decls within the compound expression, etc. This also
2367 // allows determining whether a variable reference inside the block is
2368 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002369 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlab197ba2009-02-09 18:23:29 +00002370 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00002371
2372 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002373 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump1eb44332009-09-09 15:08:12 +00002374
Steve Naroff296e8d52008-08-28 19:20:44 +00002375 // Parse the return type if present.
John McCall0b7e6782011-03-24 11:26:52 +00002376 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002377 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002378 // FIXME: Since the return type isn't actually parsed, it can't be used to
2379 // fill ParamInfo with an initial valid range, so do it manually.
2380 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00002381
Steve Naroff296e8d52008-08-28 19:20:44 +00002382 // If this block has arguments, parse them. There is no ambiguity here with
2383 // the expression case, because the expression case requires a parameter list.
2384 if (Tok.is(tok::l_paren)) {
2385 ParseParenDeclarator(ParamInfo);
2386 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00002387 // SetIdentifier sets the source range end, but in this case we're past
2388 // that location.
2389 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00002390 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002391 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002392 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002393 // If there was an error parsing the arguments, they may have
2394 // tried to use ^(x+y) which requires an argument list. Just
2395 // skip the whole block literal.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002396 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002397 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00002398 }
Mike Stump19c30c02009-04-29 19:03:13 +00002399
John McCall7f040a92010-12-24 02:08:15 +00002400 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002401
Mike Stump98eb8a72009-02-04 22:31:32 +00002402 // Inform sema that we are starting a block.
Douglas Gregor03f1eb02012-06-15 16:59:29 +00002403 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Mike Stumpaa771a82009-04-14 18:24:37 +00002404 } else if (!Tok.is(tok::l_brace)) {
Douglas Gregor03f1eb02012-06-15 16:59:29 +00002405 ParseBlockId(CaretLoc);
Steve Naroff296e8d52008-08-28 19:20:44 +00002406 } else {
2407 // Otherwise, pretend we saw (void).
John McCall0b7e6782011-03-24 11:26:52 +00002408 ParsedAttributes attrs(AttrFactory);
2409 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002410 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002411 0, 0, 0,
Douglas Gregor83f51722011-01-26 03:43:54 +00002412 true, SourceLocation(),
Douglas Gregor90ebed02011-07-13 21:47:47 +00002413 SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +00002414 SourceLocation(),
2415 SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00002416 EST_None,
2417 SourceLocation(),
Richard Smitha058fd42012-05-02 22:22:32 +00002418 0, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002419 CaretLoc, CaretLoc,
2420 ParamInfo),
John McCall0b7e6782011-03-24 11:26:52 +00002421 attrs, CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00002422
John McCall7f040a92010-12-24 02:08:15 +00002423 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002424
Mike Stump98eb8a72009-02-04 22:31:32 +00002425 // Inform sema that we are starting a block.
Douglas Gregor03f1eb02012-06-15 16:59:29 +00002426 Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
Steve Naroff296e8d52008-08-28 19:20:44 +00002427 }
2428
Sebastian Redl1d922962008-12-13 15:32:12 +00002429
John McCall60d7b3a2010-08-24 06:29:42 +00002430 ExprResult Result(true);
Chris Lattner9af55002009-03-27 04:18:06 +00002431 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002432 // Saw something like: ^expr
2433 Diag(Tok, diag::err_expected_expression);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002434 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002435 return ExprError();
2436 }
Mike Stump1eb44332009-09-09 15:08:12 +00002437
John McCall60d7b3a2010-08-24 06:29:42 +00002438 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregorc9977d02011-03-16 17:05:57 +00002439 BlockScope.Exit();
Chris Lattner9af55002009-03-27 04:18:06 +00002440 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002441 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9af55002009-03-27 04:18:06 +00002442 else
Douglas Gregor23c94db2010-07-02 17:43:08 +00002443 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002444 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00002445}
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002446
2447/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
2448///
2449/// '__objc_yes'
2450/// '__objc_no'
2451ExprResult Parser::ParseObjCBoolLiteral() {
2452 tok::TokenKind Kind = Tok.getKind();
2453 return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
2454}