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