blob: 6d31396cc0166d9d67e6b7562fa67f927fc0ffa3 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc951dae2006-08-10 04:23:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnercde626a2006-08-12 08:13:25 +000010// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000016// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
Chris Lattnercde626a2006-08-12 08:13:25 +000017// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
Chris Lattnerc951dae2006-08-10 04:23:57 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/Scope.h"
25#include "clang/Sema/ParsedTemplate.h"
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +000026#include "clang/Sema/TypoCorrection.h"
Chris Lattnerf6801202009-03-05 07:32:12 +000027#include "clang/Basic/PrettyStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000028#include "RAIIObjectsForParser.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattner834618d2006-11-03 07:48:41 +000030#include "llvm/ADT/SmallString.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000031using namespace clang;
32
Chris Lattnercde626a2006-08-12 08:13:25 +000033/// getBinOpPrecedence - Return the precedence of the specified binary operator
Chris Lattner8d72f2a2010-07-19 05:07:24 +000034/// token.
Mike Stump11289f42009-09-09 15:08:12 +000035static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregorcbb45d02009-02-25 23:02:36 +000036 bool GreaterThanIsOperator,
37 bool CPlusPlus0x) {
Chris Lattnercde626a2006-08-12 08:13:25 +000038 switch (Kind) {
Douglas Gregor8bf42052009-02-09 18:46:07 +000039 case tok::greater:
Douglas Gregorcbb45d02009-02-25 23:02:36 +000040 // C++ [temp.names]p3:
41 // [...] When parsing a template-argument-list, the first
42 // non-nested > is taken as the ending delimiter rather than a
43 // greater-than operator. [...]
Douglas Gregor8bf42052009-02-09 18:46:07 +000044 if (GreaterThanIsOperator)
45 return prec::Relational;
46 return prec::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +000047
Douglas Gregorcbb45d02009-02-25 23:02:36 +000048 case tok::greatergreater:
49 // C++0x [temp.names]p3:
50 //
51 // [...] Similarly, the first non-nested >> is treated as two
52 // consecutive but distinct > tokens, the first of which is
53 // taken as the end of the template-argument-list and completes
54 // the template-id. [...]
55 if (GreaterThanIsOperator || !CPlusPlus0x)
56 return prec::Shift;
57 return prec::Unknown;
58
Chris Lattnercde626a2006-08-12 08:13:25 +000059 default: return prec::Unknown;
60 case tok::comma: return prec::Comma;
61 case tok::equal:
62 case tok::starequal:
63 case tok::slashequal:
64 case tok::percentequal:
65 case tok::plusequal:
66 case tok::minusequal:
67 case tok::lesslessequal:
68 case tok::greatergreaterequal:
69 case tok::ampequal:
70 case tok::caretequal:
71 case tok::pipeequal: return prec::Assignment;
72 case tok::question: return prec::Conditional;
73 case tok::pipepipe: return prec::LogicalOr;
74 case tok::ampamp: return prec::LogicalAnd;
75 case tok::pipe: return prec::InclusiveOr;
76 case tok::caret: return prec::ExclusiveOr;
77 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +000078 case tok::exclaimequal:
79 case tok::equalequal: return prec::Equality;
80 case tok::lessequal:
81 case tok::less:
Douglas Gregor8bf42052009-02-09 18:46:07 +000082 case tok::greaterequal: return prec::Relational;
Douglas Gregorcbb45d02009-02-25 23:02:36 +000083 case tok::lessless: return prec::Shift;
Chris Lattnercde626a2006-08-12 08:13:25 +000084 case tok::plus:
85 case tok::minus: return prec::Additive;
86 case tok::percent:
87 case tok::slash:
88 case tok::star: return prec::Multiplicative;
Sebastian Redl112a97662009-02-07 00:15:38 +000089 case tok::periodstar:
90 case tok::arrowstar: return prec::PointerToMember;
Chris Lattnercde626a2006-08-12 08:13:25 +000091 }
92}
93
94
Chris Lattnerce7e21d2006-08-12 17:22:40 +000095/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +000096/// operators.
97///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000098/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
Sebastian Redl112a97662009-02-07 00:15:38 +0000107/// pm-expression: [C++ 5.5]
108/// cast-expression
109/// pm-expression '.*' cast-expression
110/// pm-expression '->*' cast-expression
111///
Chris Lattnercde626a2006-08-12 08:13:25 +0000112/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl112a97662009-02-07 00:15:38 +0000113/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000114/// cast-expression
115/// multiplicative-expression '*' cast-expression
116/// multiplicative-expression '/' cast-expression
117/// multiplicative-expression '%' cast-expression
118///
119/// additive-expression: [C99 6.5.6]
120/// multiplicative-expression
121/// additive-expression '+' multiplicative-expression
122/// additive-expression '-' multiplicative-expression
123///
124/// shift-expression: [C99 6.5.7]
125/// additive-expression
126/// shift-expression '<<' additive-expression
127/// shift-expression '>>' additive-expression
128///
129/// relational-expression: [C99 6.5.8]
130/// shift-expression
131/// relational-expression '<' shift-expression
132/// relational-expression '>' shift-expression
133/// relational-expression '<=' shift-expression
134/// relational-expression '>=' shift-expression
135///
136/// equality-expression: [C99 6.5.9]
137/// relational-expression
138/// equality-expression '==' relational-expression
139/// equality-expression '!=' relational-expression
140///
141/// AND-expression: [C99 6.5.10]
142/// equality-expression
143/// AND-expression '&' equality-expression
144///
145/// exclusive-OR-expression: [C99 6.5.11]
146/// AND-expression
147/// exclusive-OR-expression '^' AND-expression
148///
149/// inclusive-OR-expression: [C99 6.5.12]
150/// exclusive-OR-expression
151/// inclusive-OR-expression '|' exclusive-OR-expression
152///
153/// logical-AND-expression: [C99 6.5.13]
154/// inclusive-OR-expression
155/// logical-AND-expression '&&' inclusive-OR-expression
156///
157/// logical-OR-expression: [C99 6.5.14]
158/// logical-AND-expression
159/// logical-OR-expression '||' logical-AND-expression
160///
161/// conditional-expression: [C99 6.5.15]
162/// logical-OR-expression
163/// logical-OR-expression '?' expression ':' conditional-expression
164/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl1a99f442009-04-16 17:51:27 +0000165/// [C++] the third operand is an assignment-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000166///
167/// assignment-expression: [C99 6.5.16]
168/// conditional-expression
169/// unary-expression assignment-operator assignment-expression
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000170/// [C++] throw-expression [C++ 15]
Chris Lattnercde626a2006-08-12 08:13:25 +0000171///
172/// assignment-operator: one of
173/// = *= /= %= += -= <<= >>= &= ^= |=
174///
175/// expression: [C99 6.5.17]
Douglas Gregor968f23a2011-01-03 19:31:53 +0000176/// assignment-expression ...[opt]
177/// expression ',' assignment-expression ...[opt]
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000178ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
179 ExprResult LHS(ParseAssignmentExpression(isTypeCast));
Sebastian Redl90893182008-12-11 22:33:27 +0000180 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Chris Lattnercde626a2006-08-12 08:13:25 +0000181}
182
Mike Stump11289f42009-09-09 15:08:12 +0000183/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000184/// Current token is an Identifier and is not a 'try'. This
Chris Lattner644e1b72007-10-03 22:03:06 +0000185/// routine is necessary to disambiguate @try-statement from,
186/// for example, @encode-expression.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000187///
John McCalldadc5752010-08-24 06:29:42 +0000188ExprResult
Sebastian Redl90893182008-12-11 22:33:27 +0000189Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000190 ExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redl90893182008-12-11 22:33:27 +0000191 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000192}
193
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000194/// This routine is called when a leading '__extension__' is seen and
195/// consumed. This is necessary because the token gets consumed in the
196/// process of disambiguating between an expression and a declaration.
John McCalldadc5752010-08-24 06:29:42 +0000197ExprResult
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000198Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000199 ExprResult LHS(true);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000200 {
201 // Silence extension warnings in the sub-expression
202 ExtensionRAIIObject O(Diags);
203
204 LHS = ParseCastExpression(false);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000205 }
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000206
Douglas Gregor29d907d2010-09-17 22:25:06 +0000207 if (!LHS.isInvalid())
208 LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
209 LHS.take());
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000210
Douglas Gregor29d907d2010-09-17 22:25:06 +0000211 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000212}
213
Chris Lattner0c6c0342006-08-12 18:12:45 +0000214/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000215ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000216 if (Tok.is(tok::code_completion)) {
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000217 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000218 cutOffParsing();
219 return ExprError();
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000220 }
221
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000222 if (Tok.is(tok::kw_throw))
Sebastian Redld65cea82008-12-11 22:51:44 +0000223 return ParseThrowExpression();
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000224
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000225 ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
226 /*isAddressOfOperand=*/false,
227 isTypeCast);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000228 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000229}
230
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000231/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
232/// where part of an objc message send has already been parsed. In this case
233/// LBracLoc indicates the location of the '[' of the message send, and either
234/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
235/// message.
236///
237/// Since this handles full assignment-expression's, it handles postfix
238/// expressions and other binary operators for these expressions as well.
John McCalldadc5752010-08-24 06:29:42 +0000239ExprResult
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000240Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000241 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +0000242 ParsedType ReceiverType,
John McCallb268a282010-08-23 23:25:46 +0000243 Expr *ReceiverExpr) {
John McCalldadc5752010-08-24 06:29:42 +0000244 ExprResult R
John McCallb268a282010-08-23 23:25:46 +0000245 = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
246 ReceiverType, ReceiverExpr);
Douglas Gregoreda7e542010-09-18 01:28:11 +0000247 R = ParsePostfixExpressionSuffix(R);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000248 return ParseRHSOfBinaryExpression(R, prec::Assignment);
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000249}
250
251
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000252ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
Richard Smith764d2fe2011-12-20 02:08:33 +0000253 // C++03 [basic.def.odr]p2:
Mike Stump11289f42009-09-09 15:08:12 +0000254 // An expression is potentially evaluated unless it appears where an
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000255 // integral constant expression is required (see 5.19) [...].
Richard Smith764d2fe2011-12-20 02:08:33 +0000256 // C++98 and C++11 have no such rule, but this is only a defect in C++98.
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000257 EnterExpressionEvaluationContext Unevaluated(Actions,
Richard Smith764d2fe2011-12-20 02:08:33 +0000258 Sema::ConstantEvaluated);
Mike Stump11289f42009-09-09 15:08:12 +0000259
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000260 ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
Eli Friedmanc6237c62012-02-29 03:16:56 +0000261 ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
262 return Actions.ActOnConstantExpression(Res);
Chris Lattner3b561a32006-08-13 00:12:11 +0000263}
264
Chris Lattnercde626a2006-08-12 08:13:25 +0000265/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
266/// LHS and has a precedence of at least MinPrec.
John McCalldadc5752010-08-24 06:29:42 +0000267ExprResult
268Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000269 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
270 GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000271 getLangOpts().CPlusPlus0x);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000272 SourceLocation ColonLoc;
273
Chris Lattnercde626a2006-08-12 08:13:25 +0000274 while (1) {
275 // If this token has a lower precedence than we are allowed to parse (e.g.
276 // because we are called recursively, or because the token is not a binop),
277 // then we are done!
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000278 if (NextTokPrec < MinPrec)
Sebastian Redl90893182008-12-11 22:33:27 +0000279 return move(LHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000280
281 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000282 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000283 ConsumeToken();
Sebastian Redl112a97662009-02-07 00:15:38 +0000284
Chris Lattner96c3deb2006-08-12 17:13:08 +0000285 // Special case handling for the ternary operator.
John McCalldadc5752010-08-24 06:29:42 +0000286 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000287 if (NextTokPrec == prec::Conditional) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000288 if (Tok.isNot(tok::colon)) {
Chris Lattner244b96b2009-12-10 02:02:58 +0000289 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
290 ColonProtectionRAIIObject X(*this);
291
Chris Lattner96c3deb2006-08-12 17:13:08 +0000292 // Handle this production specially:
293 // logical-OR-expression '?' expression ':' conditional-expression
294 // In particular, the RHS of the '?' is 'expression', not
295 // 'logical-OR-expression' as we might expect.
296 TernaryMiddle = ParseExpression();
Douglas Gregorec06c122010-09-17 22:41:34 +0000297 if (TernaryMiddle.isInvalid()) {
298 LHS = ExprError();
299 TernaryMiddle = 0;
300 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000301 } else {
302 // Special case handling of "X ? Y : Z" where Y is empty:
303 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redlc13f2682008-12-09 20:22:58 +0000304 TernaryMiddle = 0;
Chris Lattner96c3deb2006-08-12 17:13:08 +0000305 Diag(Tok, diag::ext_gnu_conditional_expr);
306 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000307
Chris Lattner0151b7e2010-04-20 21:33:39 +0000308 if (Tok.is(tok::colon)) {
309 // Eat the colon.
310 ColonLoc = ConsumeToken();
311 } else {
Chandler Carruth0b5cf7c2011-07-26 05:19:46 +0000312 // Otherwise, we're missing a ':'. Assume that this was a typo that
313 // the user forgot. If we're not in a macro expansion, we can suggest
314 // a fixit hint. If there were two spaces before the current token,
Chris Lattnerfb585152010-05-24 22:31:37 +0000315 // suggest inserting the colon in between them, otherwise insert ": ".
316 SourceLocation FILoc = Tok.getLocation();
317 const char *FIText = ": ";
Argyrios Kyrtzidis3ea4adb2011-06-24 17:28:29 +0000318 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis1b07c342012-01-19 15:59:08 +0000319 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
320 assert(FILoc.isFileID());
Chris Lattnerfb585152010-05-24 22:31:37 +0000321 bool IsInvalid = false;
322 const char *SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000323 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000324 if (!IsInvalid && *SourcePtr == ' ') {
325 SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000326 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000327 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000328 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattnerfb585152010-05-24 22:31:37 +0000329 FIText = ":";
330 }
331 }
332 }
333
Ted Kremeneke6013652010-04-12 22:10:35 +0000334 Diag(Tok, diag::err_expected_colon)
Chris Lattnerfb585152010-05-24 22:31:37 +0000335 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner03c40412008-11-23 23:17:07 +0000336 Diag(OpToken, diag::note_matching) << "?";
Chris Lattner0151b7e2010-04-20 21:33:39 +0000337 ColonLoc = Tok.getLocation();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000338 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000339 }
Fariborz Jahanian9a14b842009-10-23 21:01:39 +0000340
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000341 // Code completion for the right-hand side of an assignment expression
342 // goes through a special hook that takes the left-hand side into account.
343 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000344 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000345 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000346 return ExprError();
347 }
348
Chris Lattner96c3deb2006-08-12 17:13:08 +0000349 // Parse another leaf here for the RHS of the operator.
Sebastian Redl1a99f442009-04-16 17:51:27 +0000350 // ParseCastExpression works here because all RHS expressions in C have it
351 // as a prefix, at least. However, in C++, an assignment-expression could
352 // be a throw-expression, which is not a valid cast-expression.
353 // Therefore we need some special-casing here.
354 // Also note that the third operand of the conditional operator is
Richard Smith9a6403a2012-02-26 23:40:27 +0000355 // an assignment-expression in C++, and in C++11, we can have a
Richard Smith5e0cac72012-03-01 02:59:17 +0000356 // braced-init-list on the RHS of an assignment. For better diagnostics,
357 // parse as if we were allowed braced-init-lists everywhere, and check that
358 // they only appear on the RHS of assignments later.
John McCalldadc5752010-08-24 06:29:42 +0000359 ExprResult RHS;
Richard Smithebcd2352012-03-01 07:10:06 +0000360 bool RHSIsInitList = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000361 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith9a6403a2012-02-26 23:40:27 +0000362 RHS = ParseBraceInitializer();
Richard Smithebcd2352012-03-01 07:10:06 +0000363 RHSIsInitList = true;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000364 } else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional)
Sebastian Redl1a99f442009-04-16 17:51:27 +0000365 RHS = ParseAssignmentExpression();
Richard Smith5e0cac72012-03-01 02:59:17 +0000366 else
Sebastian Redl1a99f442009-04-16 17:51:27 +0000367 RHS = ParseCastExpression(false);
Chris Lattnercde626a2006-08-12 08:13:25 +0000368
Douglas Gregor29d907d2010-09-17 22:25:06 +0000369 if (RHS.isInvalid())
370 LHS = ExprError();
371
Chris Lattnercde626a2006-08-12 08:13:25 +0000372 // Remember the precedence of this operator and get the precedence of the
373 // operator immediately to the right of the RHS.
Douglas Gregor8d4de672010-04-21 22:36:40 +0000374 prec::Level ThisPrec = NextTokPrec;
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000375 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000376 getLangOpts().CPlusPlus0x);
Chris Lattner89d53752006-08-12 17:18:19 +0000377
378 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000379 bool isRightAssoc = ThisPrec == prec::Conditional ||
380 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000381
382 // Get the precedence of the operator to the right of the RHS. If it binds
383 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000384 if (ThisPrec < NextTokPrec ||
385 (ThisPrec == NextTokPrec && isRightAssoc)) {
Richard Smithebcd2352012-03-01 07:10:06 +0000386 if (!RHS.isInvalid() && RHSIsInitList) {
387 Diag(Tok, diag::err_init_list_bin_op)
388 << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get());
389 RHS = ExprError();
Richard Smith5e0cac72012-03-01 02:59:17 +0000390 }
Chris Lattner89d53752006-08-12 17:18:19 +0000391 // If this is left-associative, only parse things on the RHS that bind
392 // more tightly than the current operator. If it is left-associative, it
393 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
394 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl511ed552008-11-25 22:21:31 +0000395 // The function takes ownership of the RHS.
Douglas Gregor29d907d2010-09-17 22:25:06 +0000396 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor8d4de672010-04-21 22:36:40 +0000397 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Richard Smithebcd2352012-03-01 07:10:06 +0000398 RHSIsInitList = false;
Douglas Gregor29d907d2010-09-17 22:25:06 +0000399
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000400 if (RHS.isInvalid())
Douglas Gregor29d907d2010-09-17 22:25:06 +0000401 LHS = ExprError();
Chris Lattnercde626a2006-08-12 08:13:25 +0000402
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000403 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000404 getLangOpts().CPlusPlus0x);
Chris Lattnercde626a2006-08-12 08:13:25 +0000405 }
406 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl511ed552008-11-25 22:21:31 +0000407
Richard Smithebcd2352012-03-01 07:10:06 +0000408 if (!RHS.isInvalid() && RHSIsInitList) {
Richard Smith5e0cac72012-03-01 02:59:17 +0000409 if (ThisPrec == prec::Assignment) {
410 Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
Richard Smithebcd2352012-03-01 07:10:06 +0000411 << Actions.getExprRange(RHS.get());
Richard Smith5e0cac72012-03-01 02:59:17 +0000412 } else {
413 Diag(OpToken, diag::err_init_list_bin_op)
Richard Smithebcd2352012-03-01 07:10:06 +0000414 << /*RHS*/1 << PP.getSpelling(OpToken)
415 << Actions.getExprRange(RHS.get());
Richard Smith5e0cac72012-03-01 02:59:17 +0000416 LHS = ExprError();
417 }
418 }
419
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000420 if (!LHS.isInvalid()) {
Chris Lattner319079c2007-08-31 05:01:50 +0000421 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor87f95b02009-02-26 21:00:50 +0000422 if (TernaryMiddle.isInvalid()) {
423 // If we're using '>>' as an operator within a template
424 // argument list (in C++98), suggest the addition of
425 // parentheses so that the code remains well-formed in C++0x.
426 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
427 SuggestParentheses(OpToken.getLocation(),
428 diag::warn_cxx0x_right_shift_in_template_arg,
429 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
430 Actions.getExprRange(RHS.get()).getEnd()));
431
Douglas Gregor0be31a22010-07-02 17:43:08 +0000432 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCallb268a282010-08-23 23:25:46 +0000433 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor87f95b02009-02-26 21:00:50 +0000434 } else
Steve Naroff83895f72007-09-16 03:34:24 +0000435 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000436 LHS.take(), TernaryMiddle.take(),
437 RHS.take());
Chris Lattner319079c2007-08-31 05:01:50 +0000438 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000439 }
440}
441
Chris Lattnereaf06592006-08-11 02:02:23 +0000442/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000443/// true, parse a unary-expression. isAddressOfOperand exists because an
444/// id-expression that is the operand of address-of gets special treatment
445/// due to member pointers.
Chris Lattnereaf06592006-08-11 02:02:23 +0000446///
John McCalldadc5752010-08-24 06:29:42 +0000447ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000448 bool isAddressOfOperand,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000449 TypeCastState isTypeCast) {
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000450 bool NotCastExpr;
John McCalldadc5752010-08-24 06:29:42 +0000451 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor29d907d2010-09-17 22:25:06 +0000452 isAddressOfOperand,
453 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +0000454 isTypeCast);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000455 if (NotCastExpr)
456 Diag(Tok, diag::err_expected_expression);
457 return move(Res);
458}
459
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000460namespace {
461class CastExpressionIdValidator : public CorrectionCandidateCallback {
462 public:
463 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
464 : AllowNonTypes(AllowNonTypes) {
465 WantTypeSpecifiers = AllowTypes;
466 }
467
468 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
469 NamedDecl *ND = candidate.getCorrectionDecl();
470 if (!ND)
471 return candidate.isKeyword();
472
473 if (isa<TypeDecl>(ND))
474 return WantTypeSpecifiers;
475 return AllowNonTypes;
476 }
477
478 private:
479 bool AllowNonTypes;
480};
481}
482
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000483/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
484/// true, parse a unary-expression. isAddressOfOperand exists because an
485/// id-expression that is the operand of address-of gets special treatment
486/// due to member pointers. NotCastExpr is set to true if the token is not the
487/// start of a cast-expression, and no diagnostic is emitted in this case.
488///
Chris Lattner4564bc12006-08-10 23:14:52 +0000489/// cast-expression: [C99 6.5.4]
490/// unary-expression
491/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000492///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000493/// unary-expression: [C99 6.5.3]
494/// postfix-expression
495/// '++' unary-expression
496/// '--' unary-expression
497/// unary-operator cast-expression
498/// 'sizeof' unary-expression
499/// 'sizeof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000500/// [C++11] 'sizeof' '...' '(' identifier ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000501/// [GNU] '__alignof' unary-expression
502/// [GNU] '__alignof' '(' type-name ')'
Richard Smithd67aea22012-03-06 03:21:47 +0000503/// [C++11] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000504/// [GNU] '&&' identifier
Richard Smithd67aea22012-03-06 03:21:47 +0000505/// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7]
Sebastian Redlbd150f42008-11-21 19:14:01 +0000506/// [C++] new-expression
507/// [C++] delete-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000508///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000509/// unary-operator: one of
510/// '&' '*' '+' '-' '~' '!'
511/// [GNU] '__extension__' '__real' '__imag'
512///
Chris Lattner52a99e52006-08-10 20:56:00 +0000513/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000514/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000515/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000516/// constant
517/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000518/// [C++] boolean-literal [C++ 2.13.5]
Richard Smithd67aea22012-03-06 03:21:47 +0000519/// [C++11] 'nullptr' [C++11 2.14.7]
520/// [C++11] user-defined-literal
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000521/// '(' expression ')'
Benjamin Kramere56f3932011-12-23 17:00:35 +0000522/// [C11] generic-selection
Chris Lattner52a99e52006-08-10 20:56:00 +0000523/// '__func__' [C99 6.4.2.2]
524/// [GNU] '__FUNCTION__'
525/// [GNU] '__PRETTY_FUNCTION__'
526/// [GNU] '(' compound-statement ')'
527/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
528/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
529/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
530/// assign-expr ')'
531/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000532/// [GNU] '__null'
Mike Stump11289f42009-09-09 15:08:12 +0000533/// [OBJC] '[' objc-message-expr ']'
Chris Lattnerb241a1b2008-01-25 18:58:06 +0000534/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump11289f42009-09-09 15:08:12 +0000535/// [OBJC] '@protocol' '(' identifier ')'
536/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000537/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000538/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000539/// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3]
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000540/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Richard Smithd67aea22012-03-06 03:21:47 +0000541/// [C++11] typename-specifier braced-init-list [C++11 5.2.3]
Bill Wendlinga6930032007-06-29 18:21:34 +0000542/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
543/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
544/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
545/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000546/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
547/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000548/// [C++] 'this' [C++ 9.3.2]
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000549/// [G++] unary-type-trait '(' type-id ')'
550/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley6242b6a2011-04-28 00:16:57 +0000551/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff0ac012832008-08-28 19:20:44 +0000552/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000553///
554/// constant: [C99 6.4.4]
555/// integer-constant
556/// floating-constant
557/// enumeration-constant -> identifier
558/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000559///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000560/// id-expression: [C++ 5.1]
561/// unqualified-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000562/// qualified-id
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000563///
564/// unqualified-id: [C++ 5.1]
565/// identifier
566/// operator-function-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000567/// conversion-function-id
568/// '~' class-name
569/// template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000570///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000571/// new-expression: [C++ 5.3.4]
572/// '::'[opt] 'new' new-placement[opt] new-type-id
573/// new-initializer[opt]
574/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
575/// new-initializer[opt]
576///
577/// delete-expression: [C++ 5.3.5]
578/// '::'[opt] 'delete' cast-expression
579/// '::'[opt] 'delete' '[' ']' cast-expression
580///
John Wiegley65497cc2011-04-27 23:09:49 +0000581/// [GNU/Embarcadero] unary-type-trait:
582/// '__is_arithmetic'
583/// '__is_floating_point'
584/// '__is_integral'
585/// '__is_lvalue_expr'
586/// '__is_rvalue_expr'
587/// '__is_complete_type'
588/// '__is_void'
589/// '__is_array'
590/// '__is_function'
591/// '__is_reference'
592/// '__is_lvalue_reference'
593/// '__is_rvalue_reference'
594/// '__is_fundamental'
595/// '__is_object'
596/// '__is_scalar'
597/// '__is_compound'
598/// '__is_pointer'
599/// '__is_member_object_pointer'
600/// '__is_member_function_pointer'
601/// '__is_member_pointer'
602/// '__is_const'
603/// '__is_volatile'
604/// '__is_trivial'
605/// '__is_standard_layout'
606/// '__is_signed'
607/// '__is_unsigned'
608///
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000609/// [GNU] unary-type-trait:
Sebastian Redl7dcb1552010-08-31 04:59:00 +0000610/// '__has_nothrow_assign'
611/// '__has_nothrow_copy'
612/// '__has_nothrow_constructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000613/// '__has_trivial_assign' [TODO]
614/// '__has_trivial_copy' [TODO]
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000615/// '__has_trivial_constructor'
Anders Carlsson6dc35752009-04-17 02:34:54 +0000616/// '__has_trivial_destructor'
Sebastian Redlb469afb2010-09-02 23:19:42 +0000617/// '__has_virtual_destructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000618/// '__is_abstract' [TODO]
619/// '__is_class'
620/// '__is_empty' [TODO]
621/// '__is_enum'
Douglas Gregordca70af2011-12-03 18:14:24 +0000622/// '__is_final'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000623/// '__is_pod'
624/// '__is_polymorphic'
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000625/// '__is_trivial'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000626/// '__is_union'
627///
Alexis Huntd9a5cc12011-05-13 00:31:07 +0000628/// [Clang] unary-type-trait:
629/// '__trivially_copyable'
630///
Douglas Gregor8006e762011-01-27 20:28:01 +0000631/// binary-type-trait:
632/// [GNU] '__is_base_of'
633/// [MS] '__is_convertible_to'
John Wiegley65497cc2011-04-27 23:09:49 +0000634/// '__is_convertible'
635/// '__is_same'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000636///
John Wiegley6242b6a2011-04-28 00:16:57 +0000637/// [Embarcadero] array-type-trait:
638/// '__array_rank'
639/// '__array_extent'
640///
John Wiegleyf9f65842011-04-25 06:54:41 +0000641/// [Embarcadero] expression-trait:
642/// '__is_lvalue_expr'
643/// '__is_rvalue_expr'
644///
John McCalldadc5752010-08-24 06:29:42 +0000645ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl22e3a932010-09-10 20:55:37 +0000646 bool isAddressOfOperand,
647 bool &NotCastExpr,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000648 TypeCastState isTypeCast) {
John McCalldadc5752010-08-24 06:29:42 +0000649 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000650 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000651 NotCastExpr = false;
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattner81b576e2006-08-11 02:13:20 +0000653 // This handles all of cast-expression, unary-expression, postfix-expression,
654 // and primary-expression. We handle them together like this for efficiency
655 // and to simplify handling of an expression starting with a '(' token: which
656 // may be one of a parenthesized expression, cast-expression, compound literal
657 // expression, or statement expression.
658 //
659 // If the parsed tokens consist of a primary-expression, the cases below
John McCallb268a282010-08-23 23:25:46 +0000660 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
661 // to handle the postfix expression suffixes. Cases that cannot be followed
662 // by postfix exprs should return without invoking
663 // ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000664 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000665 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000666 // If this expression is limited to being a unary-expression, the parent can
667 // not start a cast expression.
668 ParenParseOption ParenExprType =
David Blaikiebbafb8a2012-03-11 07:00:24 +0000669 (isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallba7bf592010-08-24 05:47:05 +0000670 ParsedType CastTy;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000671 SourceLocation RParenLoc;
Chris Lattner3c674cf2009-12-10 02:08:07 +0000672
673 {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000674 // The inside of the parens don't need to be a colon protected scope, and
675 // isn't immediately a message send.
Chris Lattner3c674cf2009-12-10 02:08:07 +0000676 ColonProtectionRAIIObject X(*this, false);
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000677
Chris Lattner3c674cf2009-12-10 02:08:07 +0000678 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000679 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner3c674cf2009-12-10 02:08:07 +0000680 }
Mike Stump11289f42009-09-09 15:08:12 +0000681
Chris Lattner81b576e2006-08-11 02:13:20 +0000682 switch (ParenExprType) {
683 case SimpleExpr: break; // Nothing else to do.
684 case CompoundStmt: break; // Nothing else to do.
685 case CompoundLiteral:
686 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
687 // postfix-expression exist, parse them now.
688 break;
689 case CastExpr:
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000690 // We have parsed the cast-expression and no postfix-expr pieces are
691 // following.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000692 return move(Res);
Chris Lattner81b576e2006-08-11 02:13:20 +0000693 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000694
John McCallb268a282010-08-23 23:25:46 +0000695 break;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000696 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000697
Chris Lattner52a99e52006-08-10 20:56:00 +0000698 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000699 case tok::numeric_constant:
700 // constant: integer-constant
701 // constant: floating-constant
Sebastian Redl59b5e512008-12-11 21:36:32 +0000702
Richard Smithbcc22fc2012-03-09 08:00:36 +0000703 Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000704 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000705 break;
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000706
Bill Wendling4073ed52007-02-13 01:51:42 +0000707 case tok::kw_true:
708 case tok::kw_false:
Sebastian Redld65cea82008-12-11 22:51:44 +0000709 return ParseCXXBoolLiteral();
Ted Kremeneke65b0862012-03-06 20:05:56 +0000710
711 case tok::kw___objc_yes:
712 case tok::kw___objc_no:
713 return ParseObjCBoolLiteral();
Bill Wendling4073ed52007-02-13 01:51:42 +0000714
Sebastian Redl576fd422009-05-10 18:38:11 +0000715 case tok::kw_nullptr:
Richard Smithb15c11c2011-10-17 23:06:20 +0000716 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl576fd422009-05-10 18:38:11 +0000717 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
718
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000719 case tok::annot_primary_expr:
720 assert(Res.get() == 0 && "Stray primary-expression annotation?");
721 Res = getExprAnnotation(Tok);
722 ConsumeToken();
723 break;
724
David Blaikie15a430a2011-12-04 05:04:18 +0000725 case tok::kw_decltype:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000726 case tok::identifier: { // primary-expression: identifier
727 // unqualified-id: identifier
728 // constant: enumeration-constant
Chris Lattnera8a3f732009-01-06 05:06:21 +0000729 // Turn a potentially qualified name into a annot_typename or
Chris Lattner122db262009-01-04 22:52:14 +0000730 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000731 if (getLangOpts().CPlusPlus) {
John McCall64fe2332010-01-07 19:29:58 +0000732 // Avoid the unnecessary parse-time lookup in the common case
733 // where the syntax forbids a type.
734 const Token &Next = NextToken();
735 if (Next.is(tok::coloncolon) ||
736 (!ColonIsSacred && Next.is(tok::colon)) ||
737 Next.is(tok::less) ||
Sebastian Redl867f2282011-12-22 18:58:29 +0000738 Next.is(tok::l_paren) ||
739 Next.is(tok::l_brace)) {
John McCall64fe2332010-01-07 19:29:58 +0000740 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
741 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +0000742 return ExprError();
743 if (!Tok.is(tok::identifier))
John McCall64fe2332010-01-07 19:29:58 +0000744 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
745 }
Chris Lattner9a8968b2009-01-04 23:23:14 +0000746 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000747
Chris Lattner55662902009-10-25 17:04:48 +0000748 // Consume the identifier so that we can see if it is followed by a '(' or
749 // '.'.
750 IdentifierInfo &II = *Tok.getIdentifierInfo();
751 SourceLocation ILoc = ConsumeToken();
752
Chris Lattnera36ec422010-04-11 08:28:14 +0000753 // Support 'Class.property' and 'super.property' notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000754 if (getLangOpts().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000755 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattnercd963182010-04-12 06:20:33 +0000756 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000757 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000758 ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000759
Douglas Gregor36107ad2012-02-16 18:19:22 +0000760 // Allow either an identifier or the keyword 'class' (in C++).
761 if (Tok.isNot(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000762 !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattner55662902009-10-25 17:04:48 +0000763 Diag(Tok, diag::err_expected_property_name);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000764 return ExprError();
765 }
766 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
767 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000768
769 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
770 ILoc, PropertyLoc);
John McCallb268a282010-08-23 23:25:46 +0000771 break;
Steve Naroff9527bbf2009-03-09 21:12:44 +0000772 }
John McCall8d08b9b2010-08-27 09:08:28 +0000773
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000774 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregored0b69d2010-09-15 16:23:04 +0000775 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000776 // that identifier, this is probably a message send with a missing open
Douglas Gregored0b69d2010-09-15 16:23:04 +0000777 // bracket. Treat it as such.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000778 if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000779 getCurScope()->isInObjcMethodScope() &&
Douglas Gregored0b69d2010-09-15 16:23:04 +0000780 ((Tok.is(tok::identifier) &&
781 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
782 Tok.is(tok::code_completion))) {
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000783 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
784 0);
785 break;
786 }
787
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000788 // If we have an Objective-C class name followed by an identifier
789 // and either ':' or ']', this is an Objective-C class message
790 // send that's missing the opening '['. Recovery
791 // appropriately. Also take this path if we're performing code
792 // completion after an Objective-C class name.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000793 if (getLangOpts().ObjC1 &&
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000794 ((Tok.is(tok::identifier) && !InMessageExpression) ||
795 Tok.is(tok::code_completion))) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000796 const Token& Next = NextToken();
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000797 if (Tok.is(tok::code_completion) ||
798 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif433c9e12010-09-17 10:21:45 +0000799 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
800 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000801 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000802 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000803 DS.SetRangeStart(ILoc);
804 DS.SetRangeEnd(ILoc);
805 const char *PrevSpec = 0;
806 unsigned DiagID;
Gabor Greif433c9e12010-09-17 10:21:45 +0000807 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000808
809 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
810 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
811 DeclaratorInfo);
812 if (Ty.isInvalid())
813 break;
814
815 Res = ParseObjCMessageExpressionBody(SourceLocation(),
816 SourceLocation(),
817 Ty.get(), 0);
818 break;
819 }
820 }
821
John McCall8d08b9b2010-08-27 09:08:28 +0000822 // Make sure to pass down the right value for isAddressOfOperand.
823 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
824 isAddressOfOperand = false;
Chris Lattner55662902009-10-25 17:04:48 +0000825
Chris Lattnerac18be92006-11-20 06:49:47 +0000826 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
827 // need to know whether or not this identifier is a function designator or
828 // not.
Douglas Gregora121b752009-11-03 16:56:39 +0000829 UnqualifiedId Name;
830 CXXScopeSpec ScopeSpec;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000831 SourceLocation TemplateKWLoc;
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000832 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
833 isTypeCast != IsTypeCast);
Douglas Gregora121b752009-11-03 16:56:39 +0000834 Name.setIdentifier(&II, ILoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000835 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
836 Name, Tok.is(tok::l_paren),
837 isAddressOfOperand, &Validator);
John McCallb268a282010-08-23 23:25:46 +0000838 break;
Chris Lattnerac18be92006-11-20 06:49:47 +0000839 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000840 case tok::char_constant: // constant: character-constant
Douglas Gregorfb65e592011-07-27 05:40:30 +0000841 case tok::wide_char_constant:
842 case tok::utf16_char_constant:
843 case tok::utf32_char_constant:
Richard Smithbcc22fc2012-03-09 08:00:36 +0000844 Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope());
Steve Naroffae4143e2007-04-26 20:39:23 +0000845 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000846 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000847 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
848 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
849 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000850 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000851 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000852 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000853 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000854 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000855 case tok::utf8_string_literal:
856 case tok::utf16_string_literal:
857 case tok::utf32_string_literal:
Richard Smithd67aea22012-03-06 03:21:47 +0000858 Res = ParseStringLiteralExpression(true);
John McCallb268a282010-08-23 23:25:46 +0000859 break;
Benjamin Kramere56f3932011-12-23 17:00:35 +0000860 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbourne91147592011-04-15 00:35:48 +0000861 Res = ParseGenericSelectionExpression();
862 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000863 case tok::kw___builtin_va_arg:
864 case tok::kw___builtin_offsetof:
865 case tok::kw___builtin_choose_expr:
Tanya Lattner55808c12011-06-04 00:47:47 +0000866 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redl90893182008-12-11 22:33:27 +0000867 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000868 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000869 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth904cb142011-07-08 04:59:44 +0000870
Douglas Gregora49ccfe2010-08-06 14:50:36 +0000871 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
872 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
873 // C++ [expr.unary] has:
874 // unary-expression:
875 // ++ cast-expression
876 // -- cast-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000877 SourceLocation SavedLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000878 Res = ParseCastExpression(!getLangOpts().CPlusPlus);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000879 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000880 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000881 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000882 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000883 case tok::amp: { // unary-expression: '&' cast-expression
884 // Special treatment because of member pointers
885 SourceLocation SavedLoc = ConsumeToken();
886 Res = ParseCastExpression(false, true);
887 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000888 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000889 return move(Res);
890 }
891
Chris Lattner81b576e2006-08-11 02:13:20 +0000892 case tok::star: // unary-expression: '*' cast-expression
893 case tok::plus: // unary-expression: '+' cast-expression
894 case tok::minus: // unary-expression: '-' cast-expression
895 case tok::tilde: // unary-expression: '~' cast-expression
896 case tok::exclaim: // unary-expression: '!' cast-expression
897 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000898 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000899 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000900 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000901 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000902 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000903 return move(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000904 }
905
Chris Lattnerc43926f2008-02-02 20:20:10 +0000906 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
907 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000908 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000909 SourceLocation SavedLoc = ConsumeToken();
910 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000911 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000912 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000913 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000914 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000915 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
916 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000917 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000918 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
919 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000920 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournee190dee2011-03-11 19:24:49 +0000921 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
922 return ParseUnaryExprOrTypeTraitExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000923 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000924 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000925 if (Tok.isNot(tok::identifier))
926 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000927
Chris Lattner9ba479b2011-02-18 21:16:39 +0000928 if (getCurScope()->getFnParent() == 0)
929 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
930
Chris Lattnereefa10e2007-05-28 06:56:27 +0000931 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000932 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
933 Tok.getLocation());
934 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Chris Lattner14a1b642006-10-15 22:33:58 +0000935 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000936 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000937 }
Chris Lattner29375652006-12-04 18:06:35 +0000938 case tok::kw_const_cast:
939 case tok::kw_dynamic_cast:
940 case tok::kw_reinterpret_cast:
941 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000942 Res = ParseCXXCasts();
John McCallb268a282010-08-23 23:25:46 +0000943 break;
Sebastian Redlc4704762008-11-11 11:37:55 +0000944 case tok::kw_typeid:
945 Res = ParseCXXTypeid();
John McCallb268a282010-08-23 23:25:46 +0000946 break;
Francois Pichet9f4f2072010-09-08 12:20:18 +0000947 case tok::kw___uuidof:
948 Res = ParseCXXUuidof();
949 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000950 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000951 Res = ParseCXXThis();
John McCallb268a282010-08-23 23:25:46 +0000952 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000953
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000954 case tok::annot_typename:
955 if (isStartOfObjCClassMessageMissingOpenBracket()) {
956 ParsedType Type = getTypeAnnotation(Tok);
957
958 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000959 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000960 DS.SetRangeStart(Tok.getLocation());
961 DS.SetRangeEnd(Tok.getLastLoc());
962
963 const char *PrevSpec = 0;
964 unsigned DiagID;
Nico Weber77430342010-11-22 10:30:56 +0000965 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
966 PrevSpec, DiagID, Type);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000967
968 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
969 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
970 if (Ty.isInvalid())
971 break;
972
973 ConsumeToken();
974 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
975 Ty.get(), 0);
976 break;
977 }
978 // Fall through
979
David Blaikie25896afb2012-01-24 05:47:35 +0000980 case tok::annot_decltype:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000981 case tok::kw_char:
982 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000983 case tok::kw_char16_t:
984 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000985 case tok::kw_bool:
986 case tok::kw_short:
987 case tok::kw_int:
988 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +0000989 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000990 case tok::kw___int128:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000991 case tok::kw_signed:
992 case tok::kw_unsigned:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000993 case tok::kw_half:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000994 case tok::kw_float:
995 case tok::kw_double:
996 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +0000997 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +0000998 case tok::kw_typeof:
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000999 case tok::kw___vector: {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001000 if (!getLangOpts().CPlusPlus) {
Chris Lattner8a38aa82009-01-04 22:28:21 +00001001 Diag(Tok, diag::err_expected_expression);
1002 return ExprError();
1003 }
Eli Friedman6d692cc2009-06-11 00:33:41 +00001004
1005 if (SavedKind == tok::kw_typename) {
1006 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001007 // typename-specifier braced-init-list
John McCall1f476a12010-02-26 08:45:28 +00001008 if (TryAnnotateTypeOrScopeToken())
Eli Friedman6d692cc2009-06-11 00:33:41 +00001009 return ExprError();
1010 }
1011
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001012 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00001013 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001014 //
John McCall084e83d2011-03-24 11:26:52 +00001015 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001016 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redl3da34892011-06-05 12:23:16 +00001017 if (Tok.isNot(tok::l_paren) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001018 (!getLangOpts().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl59b5e512008-12-11 21:36:32 +00001019 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1020 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001021
Richard Smith5d164bc2011-10-15 05:09:34 +00001022 if (Tok.is(tok::l_brace))
1023 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1024
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001025 Res = ParseCXXTypeConstructExpression(DS);
John McCallb268a282010-08-23 23:25:46 +00001026 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001027 }
1028
Douglas Gregor7df89f52010-02-05 19:11:37 +00001029 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001030 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1031 // (We can end up in this situation after tentative parsing.)
1032 if (TryAnnotateTypeOrScopeToken())
1033 return ExprError();
1034 if (!Tok.is(tok::annot_cxxscope))
1035 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001036 NotCastExpr, isTypeCast);
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001037
Douglas Gregor7df89f52010-02-05 19:11:37 +00001038 Token Next = NextToken();
1039 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001040 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001041 if (TemplateId->Kind == TNK_Type_template) {
1042 // We have a qualified template-id that we know refers to a
1043 // type, translate it into a type and continue parsing as a
1044 // cast expression.
1045 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001046 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1047 /*EnteringContext=*/false);
Douglas Gregore7c20652011-03-02 00:47:37 +00001048 AnnotateTemplateIdTokenAsType();
Douglas Gregor7df89f52010-02-05 19:11:37 +00001049 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001050 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001051 }
1052 }
1053
1054 // Parse as an id-expression.
1055 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001056 break;
Douglas Gregor7df89f52010-02-05 19:11:37 +00001057 }
1058
1059 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001060 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001061 if (TemplateId->Kind == TNK_Type_template) {
1062 // We have a template-id that we know refers to a type,
1063 // translate it into a type and continue parsing as a cast
1064 // expression.
1065 AnnotateTemplateIdTokenAsType();
1066 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001067 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001068 }
1069
1070 // Fall through to treat the template-id as an id-expression.
1071 }
1072
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001073 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001074 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001075 break;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001076
Chris Lattner122db262009-01-04 22:52:14 +00001077 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001078 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1079 // annotates the token, tail recurse.
1080 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001081 return ExprError();
1082 if (!Tok.is(tok::coloncolon))
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001083 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1084
Chris Lattner122db262009-01-04 22:52:14 +00001085 // ::new -> [C++] new-expression
1086 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001087 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +00001088 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001089 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +00001090 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001091 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001092
Chris Lattner9a8968b2009-01-04 23:23:14 +00001093 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001094 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +00001095 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +00001096 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +00001097
Sebastian Redlbd150f42008-11-21 19:14:01 +00001098 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001099 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001100
1101 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001102 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001103
Sebastian Redl22e3a932010-09-10 20:55:37 +00001104 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smithb15c11c2011-10-17 23:06:20 +00001105 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001106 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001107 BalancedDelimiterTracker T(*this, tok::l_paren);
1108
1109 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl22e3a932010-09-10 20:55:37 +00001110 return ExprError();
Richard Smith764d2fe2011-12-20 02:08:33 +00001111 // C++11 [expr.unary.noexcept]p1:
Sebastian Redle56be2f72010-09-10 21:57:27 +00001112 // The noexcept operator determines whether the evaluation of its operand,
1113 // which is an unevaluated operand, can throw an exception.
1114 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001115 ExprResult Result = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001116
1117 T.consumeClose();
1118
Sebastian Redl22e3a932010-09-10 20:55:37 +00001119 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001120 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1121 Result.take(), T.getCloseLocation());
Sebastian Redl22e3a932010-09-10 20:55:37 +00001122 return move(Result);
1123 }
1124
Chandler Carruth79803482011-04-23 10:47:20 +00001125 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001126 case tok::kw___is_class:
Eli Friedmanc96d4962009-08-15 21:55:26 +00001127 case tok::kw___is_empty:
Chandler Carruth79803482011-04-23 10:47:20 +00001128 case tok::kw___is_enum:
Sebastian Redl79eba1c2009-12-03 00:13:20 +00001129 case tok::kw___is_literal:
John Wiegley65497cc2011-04-27 23:09:49 +00001130 case tok::kw___is_arithmetic:
1131 case tok::kw___is_integral:
1132 case tok::kw___is_floating_point:
1133 case tok::kw___is_complete_type:
1134 case tok::kw___is_void:
1135 case tok::kw___is_array:
1136 case tok::kw___is_function:
1137 case tok::kw___is_reference:
1138 case tok::kw___is_lvalue_reference:
1139 case tok::kw___is_rvalue_reference:
1140 case tok::kw___is_fundamental:
1141 case tok::kw___is_object:
1142 case tok::kw___is_scalar:
1143 case tok::kw___is_compound:
1144 case tok::kw___is_pointer:
1145 case tok::kw___is_member_object_pointer:
1146 case tok::kw___is_member_function_pointer:
1147 case tok::kw___is_member_pointer:
1148 case tok::kw___is_const:
1149 case tok::kw___is_volatile:
1150 case tok::kw___is_standard_layout:
1151 case tok::kw___is_signed:
1152 case tok::kw___is_unsigned:
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00001153 case tok::kw___is_literal_type:
Chandler Carruth79803482011-04-23 10:47:20 +00001154 case tok::kw___is_pod:
1155 case tok::kw___is_polymorphic:
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +00001156 case tok::kw___is_trivial:
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001157 case tok::kw___is_trivially_copyable:
Chandler Carruth79803482011-04-23 10:47:20 +00001158 case tok::kw___is_union:
Douglas Gregordca70af2011-12-03 18:14:24 +00001159 case tok::kw___is_final:
Anders Carlssonfe63dc52009-04-16 00:08:20 +00001160 case tok::kw___has_trivial_constructor:
Douglas Gregor79f83ed2009-07-23 23:49:00 +00001161 case tok::kw___has_trivial_copy:
1162 case tok::kw___has_trivial_assign:
Anders Carlsson6dc35752009-04-17 02:34:54 +00001163 case tok::kw___has_trivial_destructor:
Sebastian Redl7dcb1552010-08-31 04:59:00 +00001164 case tok::kw___has_nothrow_assign:
1165 case tok::kw___has_nothrow_copy:
1166 case tok::kw___has_nothrow_constructor:
Sebastian Redlb469afb2010-09-02 23:19:42 +00001167 case tok::kw___has_virtual_destructor:
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001168 return ParseUnaryTypeTrait();
1169
Francois Pichet34b21132010-12-08 22:35:30 +00001170 case tok::kw___builtin_types_compatible_p:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001171 case tok::kw___is_base_of:
John Wiegley65497cc2011-04-27 23:09:49 +00001172 case tok::kw___is_same:
1173 case tok::kw___is_convertible:
Douglas Gregor8006e762011-01-27 20:28:01 +00001174 case tok::kw___is_convertible_to:
Douglas Gregor1be329d2012-02-23 07:33:15 +00001175 case tok::kw___is_trivially_assignable:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001176 return ParseBinaryTypeTrait();
1177
Douglas Gregor29c42f22012-02-24 07:38:34 +00001178 case tok::kw___is_trivially_constructible:
1179 return ParseTypeTrait();
1180
John Wiegley6242b6a2011-04-28 00:16:57 +00001181 case tok::kw___array_rank:
1182 case tok::kw___array_extent:
1183 return ParseArrayTypeTrait();
1184
John Wiegleyf9f65842011-04-25 06:54:41 +00001185 case tok::kw___is_lvalue_expr:
1186 case tok::kw___is_rvalue_expr:
1187 return ParseExpressionTrait();
1188
Chris Lattner644e1b72007-10-03 22:03:06 +00001189 case tok::at: {
1190 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001191 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +00001192 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001193 case tok::caret:
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001194 Res = ParseBlockLiteralExpression();
1195 break;
1196 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001197 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001198 cutOffParsing();
1199 return ExprError();
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001200 }
Chris Lattner6bf1db12008-12-12 19:20:14 +00001201 case tok::l_square:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001202 if (getLangOpts().CPlusPlus0x) {
1203 if (getLangOpts().ObjC1) {
Eli Friedmanc7c97142012-01-04 02:40:39 +00001204 // C++11 lambda expressions and Objective-C message sends both start with a
1205 // square bracket. There are three possibilities here:
1206 // we have a valid lambda expression, we have an invalid lambda
1207 // expression, or we have something that doesn't appear to be a lambda.
1208 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001209 Res = TryParseLambdaExpression();
Eli Friedmanc7c97142012-01-04 02:40:39 +00001210 if (!Res.isInvalid() && !Res.get())
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001211 Res = ParseObjCMessageExpression();
1212 break;
1213 }
1214 Res = ParseLambdaExpression();
1215 break;
1216 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001217 if (getLangOpts().ObjC1) {
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001218 Res = ParseObjCMessageExpression();
1219 break;
1220 }
1221 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +00001222 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001223 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001224 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +00001225 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001226
John McCallb268a282010-08-23 23:25:46 +00001227 // These can be followed by postfix-expr pieces.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001228 return ParsePostfixExpressionSuffix(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +00001229}
1230
1231/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
1232/// is parsed, this method parses any suffixes that apply.
1233///
1234/// postfix-expression: [C99 6.5.2]
1235/// primary-expression
1236/// postfix-expression '[' expression ']'
Sebastian Redl3da34892011-06-05 12:23:16 +00001237/// postfix-expression '[' braced-init-list ']'
Chris Lattner20c6a452006-08-12 17:40:43 +00001238/// postfix-expression '(' argument-expression-list[opt] ')'
1239/// postfix-expression '.' identifier
1240/// postfix-expression '->' identifier
1241/// postfix-expression '++'
1242/// postfix-expression '--'
1243/// '(' type-name ')' '{' initializer-list '}'
1244/// '(' type-name ')' '{' initializer-list ',' '}'
1245///
1246/// argument-expression-list: [C99 6.5.2]
Douglas Gregor968f23a2011-01-03 19:31:53 +00001247/// argument-expression ...[opt]
1248/// argument-expression-list ',' assignment-expression ...[opt]
Chris Lattner20c6a452006-08-12 17:40:43 +00001249///
John McCalldadc5752010-08-24 06:29:42 +00001250ExprResult
1251Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001252 // Now that the primary-expression piece of the postfix-expression has been
1253 // parsed, see if there are any postfix-expression pieces here.
1254 SourceLocation Loc;
1255 while (1) {
1256 switch (Tok.getKind()) {
Douglas Gregored0b69d2010-09-15 16:23:04 +00001257 case tok::code_completion:
1258 if (InMessageExpression)
1259 return move(LHS);
1260
Douglas Gregoreda7e542010-09-18 01:28:11 +00001261 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001262 cutOffParsing();
1263 return ExprError();
Douglas Gregored0b69d2010-09-15 16:23:04 +00001264
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001265 case tok::identifier:
1266 // If we see identifier: after an expression, and we're not already in a
1267 // message send, then this is probably a message send with a missing
1268 // opening bracket '['.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001269 if (getLangOpts().ObjC1 && !InMessageExpression &&
Douglas Gregord6d98002010-09-15 14:54:45 +00001270 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001271 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1272 ParsedType(), LHS.get());
1273 break;
1274 }
1275
1276 // Fall through; this isn't a message send.
1277
Chris Lattner20c6a452006-08-12 17:40:43 +00001278 default: // Not a postfix-expression suffix.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001279 return move(LHS);
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001280 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner47054fb2010-05-31 18:18:22 +00001281 // If we have a array postfix expression that starts on a new line and
1282 // Objective-C is enabled, it is highly likely that the user forgot a
1283 // semicolon after the base expression and that the array postfix-expr is
1284 // actually another message send. In this case, do some look-ahead to see
1285 // if the contents of the square brackets are obviously not a valid
1286 // expression and recover by pretending there is no suffix.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001287 if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
Chris Lattner47054fb2010-05-31 18:18:22 +00001288 isSimpleObjCMessageExpression())
Douglas Gregor990ccac2010-05-31 14:40:22 +00001289 return move(LHS);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001290
1291 // Reject array indices starting with a lambda-expression. '[[' is
1292 // reserved for attributes.
1293 if (CheckProhibitedCXX11Attribute())
1294 return ExprError();
1295
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001296 BalancedDelimiterTracker T(*this, tok::l_square);
1297 T.consumeOpen();
1298 Loc = T.getOpenLocation();
Sebastian Redl3da34892011-06-05 12:23:16 +00001299 ExprResult Idx;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001300 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001301 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00001302 Idx = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001303 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00001304 Idx = ParseExpression();
Sebastian Redl511ed552008-11-25 22:21:31 +00001305
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001306 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001307
1308 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCallb268a282010-08-23 23:25:46 +00001309 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1310 Idx.take(), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001311 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +00001312 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001313
Chris Lattner89c50c62006-08-11 06:41:18 +00001314 // Match the ']'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001315 T.consumeClose();
Chris Lattner89c50c62006-08-11 06:41:18 +00001316 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001317 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001318
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001319 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1320 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1321 // '(' argument-expression-list[opt] ')'
1322 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001323 InMessageExpressionRAIIObject InMessage(*this, false);
1324
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001325 Expr *ExecConfig = 0;
1326
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001327 BalancedDelimiterTracker PT(*this, tok::l_paren);
1328
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001329 if (OpKind == tok::lesslessless) {
1330 ExprVector ExecConfigExprs(Actions);
1331 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001332 SourceLocation OpenLoc = ConsumeToken();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001333
1334 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1335 LHS = ExprError();
1336 }
1337
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001338 SourceLocation CloseLoc = Tok.getLocation();
1339 if (Tok.is(tok::greatergreatergreater)) {
1340 ConsumeToken();
1341 } else if (LHS.isInvalid()) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001342 SkipUntil(tok::greatergreatergreater);
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001343 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001344 // There was an error closing the brackets
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001345 Diag(Tok, diag::err_expected_ggg);
1346 Diag(OpenLoc, diag::note_matching) << "<<<";
1347 SkipUntil(tok::greatergreatergreater);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001348 LHS = ExprError();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001349 }
1350
1351 if (!LHS.isInvalid()) {
1352 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1353 LHS = ExprError();
1354 else
1355 Loc = PrevTokLocation;
1356 }
1357
1358 if (!LHS.isInvalid()) {
1359 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001360 OpenLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001361 move_arg(ExecConfigExprs),
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001362 CloseLoc);
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001363 if (ECResult.isInvalid())
1364 LHS = ExprError();
1365 else
1366 ExecConfig = ECResult.get();
1367 }
1368 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001369 PT.consumeOpen();
1370 Loc = PT.getOpenLocation();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001371 }
1372
Sebastian Redl511ed552008-11-25 22:21:31 +00001373 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001374 CommaLocsTy CommaLocs;
Douglas Gregoreda7e542010-09-18 01:28:11 +00001375
Douglas Gregorcabea402009-09-22 15:41:20 +00001376 if (Tok.is(tok::code_completion)) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00001377 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1378 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001379 cutOffParsing();
1380 return ExprError();
Douglas Gregorcabea402009-09-22 15:41:20 +00001381 }
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001382
1383 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1384 if (Tok.isNot(tok::r_paren)) {
1385 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1386 LHS.get())) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001387 LHS = ExprError();
1388 }
Chris Lattner0c6c0342006-08-12 18:12:45 +00001389 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001390 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001391
Chris Lattner89c50c62006-08-11 06:41:18 +00001392 // Match the ')'.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001393 if (LHS.isInvalid()) {
1394 SkipUntil(tok::r_paren);
1395 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001396 PT.consumeClose();
Douglas Gregoreda7e542010-09-18 01:28:11 +00001397 LHS = ExprError();
1398 } else {
1399 assert((ArgExprs.size() == 0 ||
1400 ArgExprs.size()-1 == CommaLocs.size())&&
Chris Lattnere165d942006-08-24 04:40:38 +00001401 "Unexpected number of commas!");
John McCallb268a282010-08-23 23:25:46 +00001402 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001403 move_arg(ArgExprs), Tok.getLocation(),
1404 ExecConfig);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001405 PT.consumeClose();
Chris Lattnere165d942006-08-24 04:40:38 +00001406 }
Mike Stump11289f42009-09-09 15:08:12 +00001407
Chris Lattner89c50c62006-08-11 06:41:18 +00001408 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001409 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001410 case tok::arrow:
1411 case tok::period: {
1412 // postfix-expression: p-e '->' template[opt] id-expression
1413 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001414 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001415 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001416
Douglas Gregord8061562009-08-06 03:17:00 +00001417 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001418 ParsedType ObjectType;
Douglas Gregore610ada2010-02-24 18:44:31 +00001419 bool MayBePseudoDestructor = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001420 if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001421 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregore610ada2010-02-24 18:44:31 +00001422 OpLoc, OpKind, ObjectType,
1423 MayBePseudoDestructor);
Douglas Gregord8061562009-08-06 03:17:00 +00001424 if (LHS.isInvalid())
1425 break;
Douglas Gregore610ada2010-02-24 18:44:31 +00001426
Douglas Gregordf593fb2011-11-07 17:33:42 +00001427 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1428 /*EnteringContext=*/false,
Douglas Gregore610ada2010-02-24 18:44:31 +00001429 &MayBePseudoDestructor);
Douglas Gregor205a3612010-05-27 15:25:59 +00001430 if (SS.isNotEmpty())
John McCallba7bf592010-08-24 05:47:05 +00001431 ObjectType = ParsedType();
Douglas Gregord8061562009-08-06 03:17:00 +00001432 }
1433
Douglas Gregor2436e712009-09-17 21:32:03 +00001434 if (Tok.is(tok::code_completion)) {
1435 // Code completion for a member access expression.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001436 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor2436e712009-09-17 21:32:03 +00001437 OpLoc, OpKind == tok::arrow);
1438
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001439 cutOffParsing();
1440 return ExprError();
Douglas Gregor2436e712009-09-17 21:32:03 +00001441 }
1442
John McCallb268a282010-08-23 23:25:46 +00001443 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1444 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregore610ada2010-02-24 18:44:31 +00001445 ObjectType);
1446 break;
1447 }
1448
1449 // Either the action has told is that this cannot be a
1450 // pseudo-destructor expression (based on the type of base
1451 // expression), or we didn't see a '~' in the right place. We
1452 // can still parse a destructor name here, but in that case it
1453 // names a real destructor.
Francois Pichet64225792011-01-18 05:04:39 +00001454 // Allow explicit constructor calls in Microsoft mode.
1455 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001456 SourceLocation TemplateKWLoc;
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001457 UnqualifiedId Name;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001458 if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
Douglas Gregor36107ad2012-02-16 18:19:22 +00001459 // Objective-C++:
1460 // After a '.' in a member access expression, treat the keyword
1461 // 'class' as if it were an identifier.
1462 //
1463 // This hack allows property access to the 'class' method because it is
1464 // such a common method name. For other C++ keywords that are
1465 // Objective-C method names, one must use the message send syntax.
1466 IdentifierInfo *Id = Tok.getIdentifierInfo();
1467 SourceLocation Loc = ConsumeToken();
1468 Name.setIdentifier(Id, Loc);
1469 } else if (ParseUnqualifiedId(SS,
1470 /*EnteringContext=*/false,
1471 /*AllowDestructorName=*/true,
1472 /*AllowConstructorName=*/
David Blaikiebbafb8a2012-03-11 07:00:24 +00001473 getLangOpts().MicrosoftExt,
Douglas Gregor36107ad2012-02-16 18:19:22 +00001474 ObjectType, TemplateKWLoc, Name))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001475 LHS = ExprError();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001476
1477 if (!LHS.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001478 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001479 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001480 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1481 Tok.is(tok::l_paren));
Chris Lattner89c50c62006-08-11 06:41:18 +00001482 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001483 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001484 case tok::plusplus: // postfix-expression: postfix-expression '++'
1485 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001486 if (!LHS.isInvalid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001487 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCallb268a282010-08-23 23:25:46 +00001488 Tok.getKind(), LHS.take());
Sebastian Redl511ed552008-11-25 22:21:31 +00001489 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001490 ConsumeToken();
1491 break;
Chris Lattnerf8339772006-08-10 22:01:51 +00001492 }
1493 }
Chris Lattner52a99e52006-08-10 20:56:00 +00001494}
1495
Peter Collingbournee190dee2011-03-11 19:24:49 +00001496/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1497/// vec_step and we are at the start of an expression or a parenthesized
1498/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1499/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001500///
1501/// unary-expression: [C99 6.5.3]
1502/// 'sizeof' unary-expression
1503/// 'sizeof' '(' type-name ')'
1504/// [GNU] '__alignof' unary-expression
1505/// [GNU] '__alignof' '(' type-name ')'
1506/// [C++0x] 'alignof' '(' type-id ')'
1507///
1508/// [GNU] typeof-specifier:
1509/// typeof ( expressions )
1510/// typeof ( type-name )
1511/// [GNU/C++] typeof unary-expression
1512///
Peter Collingbournee190dee2011-03-11 19:24:49 +00001513/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1514/// vec_step ( expressions )
1515/// vec_step ( type-name )
1516///
John McCalldadc5752010-08-24 06:29:42 +00001517ExprResult
Peter Collingbournee190dee2011-03-11 19:24:49 +00001518Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1519 bool &isCastExpr,
1520 ParsedType &CastTy,
1521 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001522
1523 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournee190dee2011-03-11 19:24:49 +00001524 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1525 OpTok.is(tok::kw_vec_step)) &&
1526 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001527
John McCalldadc5752010-08-24 06:29:42 +00001528 ExprResult Operand;
Mike Stump11289f42009-09-09 15:08:12 +00001529
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001530 // If the operand doesn't start with an '(', it must be an expression.
1531 if (Tok.isNot(tok::l_paren)) {
1532 isCastExpr = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001533 if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001534 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1535 return ExprError();
1536 }
Mike Stump11289f42009-09-09 15:08:12 +00001537
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001538 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001539 } else {
1540 // If it starts with a '(', we know that it is either a parenthesized
1541 // type-name, or it is a unary-expression that starts with a compound
1542 // literal, or starts with a primary-expression that is a parenthesized
1543 // expression.
1544 ParenParseOption ExprType = CastExpr;
1545 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001546
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001547 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001548 false, CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001549 CastRange = SourceRange(LParenLoc, RParenLoc);
1550
1551 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1552 // a type.
1553 if (ExprType == CastExpr) {
1554 isCastExpr = true;
1555 return ExprEmpty();
1556 }
1557
David Blaikiebbafb8a2012-03-11 07:00:24 +00001558 if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
Douglas Gregor5dc05532010-07-28 18:22:12 +00001559 // GNU typeof in C requires the expression to be parenthesized. Not so for
1560 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1561 // the start of a unary-expression, but doesn't include any postfix
1562 // pieces. Parse these now if present.
John McCall3669c802010-08-24 23:41:43 +00001563 if (!Operand.isInvalid())
1564 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor5dc05532010-07-28 18:22:12 +00001565 }
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001566 }
1567
1568 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1569 isCastExpr = false;
1570 return move(Operand);
1571}
1572
Chris Lattner20c6a452006-08-12 17:40:43 +00001573
Peter Collingbournee190dee2011-03-11 19:24:49 +00001574/// ParseUnaryExprOrTypeTraitExpression - Parse a sizeof or alignof expression.
Chris Lattner81b576e2006-08-11 02:13:20 +00001575/// unary-expression: [C99 6.5.3]
1576/// 'sizeof' unary-expression
1577/// 'sizeof' '(' type-name ')'
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001578/// [C++0x] 'sizeof' '...' '(' identifier ')'
Chris Lattner81b576e2006-08-11 02:13:20 +00001579/// [GNU] '__alignof' unary-expression
1580/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +00001581/// [C++0x] 'alignof' '(' type-id ')'
Peter Collingbournee190dee2011-03-11 19:24:49 +00001582ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +00001583 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001584 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1585 "Not a sizeof/alignof/vec_step expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001586 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001587 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001588
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001589 // [C++0x] 'sizeof' '...' '(' identifier ')'
1590 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1591 SourceLocation EllipsisLoc = ConsumeToken();
1592 SourceLocation LParenLoc, RParenLoc;
1593 IdentifierInfo *Name = 0;
1594 SourceLocation NameLoc;
1595 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001596 BalancedDelimiterTracker T(*this, tok::l_paren);
1597 T.consumeOpen();
1598 LParenLoc = T.getOpenLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001599 if (Tok.is(tok::identifier)) {
1600 Name = Tok.getIdentifierInfo();
1601 NameLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001602 T.consumeClose();
1603 RParenLoc = T.getCloseLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001604 if (RParenLoc.isInvalid())
1605 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1606 } else {
1607 Diag(Tok, diag::err_expected_parameter_pack);
1608 SkipUntil(tok::r_paren);
1609 }
1610 } else if (Tok.is(tok::identifier)) {
1611 Name = Tok.getIdentifierInfo();
1612 NameLoc = ConsumeToken();
1613 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1614 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1615 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1616 << Name
1617 << FixItHint::CreateInsertion(LParenLoc, "(")
1618 << FixItHint::CreateInsertion(RParenLoc, ")");
1619 } else {
1620 Diag(Tok, diag::err_sizeof_parameter_pack);
1621 }
1622
1623 if (!Name)
1624 return ExprError();
1625
1626 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1627 OpTok.getLocation(),
1628 *Name, NameLoc,
1629 RParenLoc);
1630 }
Richard Smithb15c11c2011-10-17 23:06:20 +00001631
1632 if (OpTok.is(tok::kw_alignof))
1633 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1634
Eli Friedmane0afc982012-01-21 01:01:51 +00001635 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1636
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001637 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00001638 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001639 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001640 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1641 isCastExpr,
1642 CastTy,
1643 CastRange);
1644
1645 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1646 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1647 ExprKind = UETT_AlignOf;
1648 else if (OpTok.is(tok::kw_vec_step))
1649 ExprKind = UETT_VecStep;
Sebastian Redl90893182008-12-11 22:33:27 +00001650
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001651 if (isCastExpr)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001652 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1653 ExprKind,
1654 /*isType=*/true,
1655 CastTy.getAsOpaquePtr(),
1656 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001657
Chris Lattner26115ac2006-08-24 06:10:04 +00001658 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001659 if (!Operand.isInvalid())
Peter Collingbournee190dee2011-03-11 19:24:49 +00001660 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1661 ExprKind,
1662 /*isType=*/false,
1663 Operand.release(),
1664 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001665 return move(Operand);
Chris Lattner81b576e2006-08-11 02:13:20 +00001666}
1667
Chris Lattner11124352006-08-12 19:16:08 +00001668/// ParseBuiltinPrimaryExpression
1669///
1670/// primary-expression: [C99 6.5.1]
1671/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1672/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1673/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1674/// assign-expr ')'
1675/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbourne62c21982011-11-05 03:47:48 +00001676/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001677///
Chris Lattner11124352006-08-12 19:16:08 +00001678/// [GNU] offsetof-member-designator:
1679/// [GNU] identifier
1680/// [GNU] offsetof-member-designator '.' identifier
1681/// [GNU] offsetof-member-designator '[' expression ']'
1682///
John McCalldadc5752010-08-24 06:29:42 +00001683ExprResult Parser::ParseBuiltinPrimaryExpression() {
1684 ExprResult Res;
Chris Lattner11124352006-08-12 19:16:08 +00001685 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1686
1687 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001688 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001689
1690 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001691 if (Tok.isNot(tok::l_paren))
1692 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1693 << BuiltinII);
1694
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001695 BalancedDelimiterTracker PT(*this, tok::l_paren);
1696 PT.consumeOpen();
1697
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001698 // TODO: Build AST.
1699
Chris Lattner11124352006-08-12 19:16:08 +00001700 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +00001701 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001702 case tok::kw___builtin_va_arg: {
John McCalldadc5752010-08-24 06:29:42 +00001703 ExprResult Expr(ParseAssignmentExpression());
Chris Lattner0be454e2006-08-12 19:30:51 +00001704
Chris Lattner6d7e6342006-08-15 03:41:14 +00001705 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001706 Expr = ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001707
Douglas Gregor220cac52009-02-18 17:45:20 +00001708 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001709
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001710 if (Tok.isNot(tok::r_paren)) {
1711 Diag(Tok, diag::err_expected_rparen);
Douglas Gregoreda7e542010-09-18 01:28:11 +00001712 Expr = ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001713 }
Douglas Gregoreda7e542010-09-18 01:28:11 +00001714
1715 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor220cac52009-02-18 17:45:20 +00001716 Res = ExprError();
1717 else
John McCallb268a282010-08-23 23:25:46 +00001718 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001719 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001720 }
Chris Lattner687d6092007-08-30 15:51:11 +00001721 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001722 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001723 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001724 if (Ty.isInvalid()) {
1725 SkipUntil(tok::r_paren);
1726 return ExprError();
1727 }
Mike Stump11289f42009-09-09 15:08:12 +00001728
Chris Lattner6d7e6342006-08-15 03:41:14 +00001729 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001730 return ExprError();
1731
Chris Lattner11124352006-08-12 19:16:08 +00001732 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001733 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001734 Diag(Tok, diag::err_expected_ident);
1735 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001736 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001737 }
Sebastian Redl90893182008-12-11 22:33:27 +00001738
Chris Lattner687d6092007-08-30 15:51:11 +00001739 // Keep track of the various subcomponents we see.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001740 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001741
John McCallfaf5fb42010-08-26 23:41:50 +00001742 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001743 Comps.back().isBrackets = false;
1744 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1745 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001746
Sebastian Redl511ed552008-11-25 22:21:31 +00001747 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001748 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001749 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001750 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallfaf5fb42010-08-26 23:41:50 +00001751 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001752 Comps.back().isBrackets = false;
1753 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001754
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001755 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001756 Diag(Tok, diag::err_expected_ident);
1757 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001758 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001759 }
1760 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1761 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001762
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001763 } else if (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001764 if (CheckProhibitedCXX11Attribute())
1765 return ExprError();
1766
Chris Lattner11124352006-08-12 19:16:08 +00001767 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallfaf5fb42010-08-26 23:41:50 +00001768 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001769 Comps.back().isBrackets = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001770 BalancedDelimiterTracker ST(*this, tok::l_square);
1771 ST.consumeOpen();
1772 Comps.back().LocStart = ST.getOpenLocation();
Chris Lattner11124352006-08-12 19:16:08 +00001773 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001774 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001775 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001776 return move(Res);
Chris Lattner11124352006-08-12 19:16:08 +00001777 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001778 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001779
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001780 ST.consumeClose();
1781 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman5e774b12009-06-27 20:38:33 +00001782 } else {
1783 if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001784 PT.consumeClose();
Douglas Gregor220cac52009-02-18 17:45:20 +00001785 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001786 } else if (Ty.isInvalid()) {
1787 Res = ExprError();
1788 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001789 PT.consumeClose();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001790 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001791 Ty.get(), &Comps[0], Comps.size(),
1792 PT.getCloseLocation());
Eli Friedman5e774b12009-06-27 20:38:33 +00001793 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001794 break;
Chris Lattner11124352006-08-12 19:16:08 +00001795 }
1796 }
1797 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001798 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001799 case tok::kw___builtin_choose_expr: {
John McCalldadc5752010-08-24 06:29:42 +00001800 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001801 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001802 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001803 return move(Cond);
Steve Naroff9efdabc2007-08-03 21:21:27 +00001804 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001805 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001806 return ExprError();
1807
John McCalldadc5752010-08-24 06:29:42 +00001808 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001809 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001810 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001811 return move(Expr1);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001812 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001813 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001814 return ExprError();
1815
John McCalldadc5752010-08-24 06:29:42 +00001816 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001817 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001818 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001819 return move(Expr2);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001820 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001821 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001822 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001823 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001824 }
John McCallb268a282010-08-23 23:25:46 +00001825 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1826 Expr2.take(), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001827 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001828 }
Tanya Lattner55808c12011-06-04 00:47:47 +00001829 case tok::kw___builtin_astype: {
1830 // The first argument is an expression to be converted, followed by a comma.
1831 ExprResult Expr(ParseAssignmentExpression());
1832 if (Expr.isInvalid()) {
1833 SkipUntil(tok::r_paren);
1834 return ExprError();
1835 }
1836
1837 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1838 tok::r_paren))
1839 return ExprError();
1840
1841 // Second argument is the type to bitcast to.
1842 TypeResult DestTy = ParseTypeName();
1843 if (DestTy.isInvalid())
1844 return ExprError();
1845
1846 // Attempt to consume the r-paren.
1847 if (Tok.isNot(tok::r_paren)) {
1848 Diag(Tok, diag::err_expected_rparen);
1849 SkipUntil(tok::r_paren);
1850 return ExprError();
1851 }
1852
1853 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1854 ConsumeParen());
1855 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001856 }
Chandler Carruth904cb142011-07-08 04:59:44 +00001857 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001858
John McCallb268a282010-08-23 23:25:46 +00001859 if (Res.isInvalid())
1860 return ExprError();
1861
Chris Lattner11124352006-08-12 19:16:08 +00001862 // These can be followed by postfix-expr pieces because they are
1863 // primary-expressions.
John McCallb268a282010-08-23 23:25:46 +00001864 return ParsePostfixExpressionSuffix(Res.take());
Chris Lattner11124352006-08-12 19:16:08 +00001865}
1866
Chris Lattner4add4e62006-08-11 01:33:00 +00001867/// ParseParenExpression - This parses the unit that starts with a '(' token,
1868/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001869/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1870/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001871///
1872/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001873/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001874/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1875/// postfix-expression: [C99 6.5.2]
1876/// '(' type-name ')' '{' initializer-list '}'
1877/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001878/// cast-expression: [C99 6.5.4]
1879/// '(' type-name ')' cast-expression
John McCall31168b02011-06-15 23:02:42 +00001880/// [ARC] bridged-cast-expression
1881///
1882/// [ARC] bridged-cast-expression:
1883/// (__bridge type-name) cast-expression
1884/// (__bridge_transfer type-name) cast-expression
1885/// (__bridge_retained type-name) cast-expression
John McCalldadc5752010-08-24 06:29:42 +00001886ExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001887Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001888 bool isTypeCast, ParsedType &CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001889 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001890 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregor0db4ccd2009-02-09 21:04:56 +00001891 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001892 BalancedDelimiterTracker T(*this, tok::l_paren);
1893 if (T.consumeOpen())
1894 return ExprError();
1895 SourceLocation OpenLoc = T.getOpenLocation();
1896
John McCalldadc5752010-08-24 06:29:42 +00001897 ExprResult Result(true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001898 bool isAmbiguousTypeId;
John McCallba7bf592010-08-24 05:47:05 +00001899 CastTy = ParsedType();
Sebastian Redl90893182008-12-11 22:33:27 +00001900
Douglas Gregor5e35d592010-09-14 23:59:36 +00001901 if (Tok.is(tok::code_completion)) {
1902 Actions.CodeCompleteOrdinaryName(getCurScope(),
1903 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1904 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001905 cutOffParsing();
Douglas Gregor5e35d592010-09-14 23:59:36 +00001906 return ExprError();
1907 }
John McCallc5e6b972011-04-06 02:35:25 +00001908
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001909 // Diagnose use of bridge casts in non-arc mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001910 bool BridgeCast = (getLangOpts().ObjC2 &&
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001911 (Tok.is(tok::kw___bridge) ||
1912 Tok.is(tok::kw___bridge_transfer) ||
1913 Tok.is(tok::kw___bridge_retained) ||
1914 Tok.is(tok::kw___bridge_retain)));
David Blaikiebbafb8a2012-03-11 07:00:24 +00001915 if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001916 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001917 SourceLocation BridgeKeywordLoc = ConsumeToken();
1918 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremenek084e1b42012-02-18 04:42:38 +00001919 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001920 << BridgeCastName
1921 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001922 BridgeCast = false;
1923 }
1924
John McCallc5e6b972011-04-06 02:35:25 +00001925 // None of these cases should fall through with an invalid Result
1926 // unless they've already reported an error.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001927 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001928 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall3abee492012-04-04 01:27:53 +00001929 Actions.ActOnStartStmtExpr();
1930
Richard Smithc202b282012-04-14 00:33:13 +00001931 StmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001932 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001933
Chris Lattner366727f2007-07-24 16:58:17 +00001934 // If the substmt parsed correctly, build the AST node.
John McCall3abee492012-04-04 01:27:53 +00001935 if (!Stmt.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001936 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
John McCall3abee492012-04-04 01:27:53 +00001937 } else {
1938 Actions.ActOnStmtExprError();
1939 }
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001940 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCall0c07bee2011-06-17 21:56:12 +00001941 tok::TokenKind tokenKind = Tok.getKind();
1942 SourceLocation BridgeKeywordLoc = ConsumeToken();
1943
John McCall31168b02011-06-15 23:02:42 +00001944 // Parse an Objective-C ARC ownership cast expression.
1945 ObjCBridgeCastKind Kind;
John McCall0c07bee2011-06-17 21:56:12 +00001946 if (tokenKind == tok::kw___bridge)
John McCall31168b02011-06-15 23:02:42 +00001947 Kind = OBC_Bridge;
John McCall0c07bee2011-06-17 21:56:12 +00001948 else if (tokenKind == tok::kw___bridge_transfer)
John McCall31168b02011-06-15 23:02:42 +00001949 Kind = OBC_BridgeTransfer;
John McCall0c07bee2011-06-17 21:56:12 +00001950 else if (tokenKind == tok::kw___bridge_retained)
John McCall31168b02011-06-15 23:02:42 +00001951 Kind = OBC_BridgeRetained;
John McCall0c07bee2011-06-17 21:56:12 +00001952 else {
1953 // As a hopefully temporary workaround, allow __bridge_retain as
1954 // a synonym for __bridge_retained, but only in system headers.
1955 assert(tokenKind == tok::kw___bridge_retain);
1956 Kind = OBC_BridgeRetained;
1957 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1958 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1959 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1960 "__bridge_retained");
1961 }
John McCall31168b02011-06-15 23:02:42 +00001962
John McCall31168b02011-06-15 23:02:42 +00001963 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001964 T.consumeClose();
1965 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001966 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCall31168b02011-06-15 23:02:42 +00001967
1968 if (Ty.isInvalid() || SubExpr.isInvalid())
1969 return ExprError();
1970
1971 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1972 BridgeKeywordLoc, Ty.get(),
1973 RParenLoc, SubExpr.get());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001974 } else if (ExprType >= CompoundLiteral &&
1975 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00001976
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001977 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00001978
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001979 // In C++, if the type-id is ambiguous we disambiguate based on context.
1980 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1981 // in which case we should treat it as type-id.
1982 // if stopIfCastExpr is false, we need to determine the context past the
1983 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001984 if (isAmbiguousTypeId && !stopIfCastExpr) {
1985 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
1986 RParenLoc = T.getCloseLocation();
1987 return res;
1988 }
Mike Stump11289f42009-09-09 15:08:12 +00001989
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001990 // Parse the type declarator.
1991 DeclSpec DS(AttrFactory);
1992 ParseSpecifierQualifierList(DS);
1993 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1994 ParseDeclarator(DeclaratorInfo);
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001995
Douglas Gregor3e972002010-09-15 23:19:31 +00001996 // If our type is followed by an identifier and either ':' or ']', then
1997 // this is probably an Objective-C message send where the leading '[' is
1998 // missing. Recover as if that were the case.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001999 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002000 !InMessageExpression && getLangOpts().ObjC1 &&
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002001 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
2002 TypeResult Ty;
2003 {
2004 InMessageExpressionRAIIObject InMessage(*this, false);
2005 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2006 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002007 Result = ParseObjCMessageExpressionBody(SourceLocation(),
2008 SourceLocation(),
2009 Ty.get(), 0);
2010 } else {
2011 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002012 T.consumeClose();
2013 RParenLoc = T.getCloseLocation();
Douglas Gregor3e972002010-09-15 23:19:31 +00002014 if (Tok.is(tok::l_brace)) {
2015 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002016 TypeResult Ty;
2017 {
2018 InMessageExpressionRAIIObject InMessage(*this, false);
2019 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2020 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002021 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +00002022 }
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00002023
Douglas Gregor3e972002010-09-15 23:19:31 +00002024 if (ExprType == CastExpr) {
2025 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb5d49352009-01-19 22:31:54 +00002026
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002027 if (DeclaratorInfo.isInvalidType())
Douglas Gregor3e972002010-09-15 23:19:31 +00002028 return ExprError();
2029
Douglas Gregor3e972002010-09-15 23:19:31 +00002030 // Note that this doesn't parse the subsequent cast-expression, it just
2031 // returns the parsed type to the callee.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002032 if (stopIfCastExpr) {
2033 TypeResult Ty;
2034 {
2035 InMessageExpressionRAIIObject InMessage(*this, false);
2036 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2037 }
2038 CastTy = Ty.get();
Douglas Gregor3e972002010-09-15 23:19:31 +00002039 return ExprResult();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002040 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002041
2042 // Reject the cast of super idiom in ObjC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002043 if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
Douglas Gregor3e972002010-09-15 23:19:31 +00002044 Tok.getIdentifierInfo() == Ident_super &&
2045 getCurScope()->isInObjcMethodScope() &&
2046 GetLookAheadToken(1).isNot(tok::period)) {
2047 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2048 << SourceRange(OpenLoc, RParenLoc);
2049 return ExprError();
2050 }
2051
2052 // Parse the cast-expression that follows it next.
2053 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002054 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2055 /*isAddressOfOperand=*/false,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002056 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002057 if (!Result.isInvalid()) {
2058 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2059 DeclaratorInfo, CastTy,
Douglas Gregor3e972002010-09-15 23:19:31 +00002060 RParenLoc, Result.take());
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002061 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002062 return move(Result);
2063 }
2064
2065 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2066 return ExprError();
2067 }
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002068 } else if (isTypeCast) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00002069 // Parse the expression-list.
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002070 InMessageExpressionRAIIObject InMessage(*this, false);
2071
Nate Begeman5ec4b312009-08-10 23:49:36 +00002072 ExprVector ArgExprs(Actions);
2073 CommaLocsTy CommaLocs;
2074
2075 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2076 ExprType = SimpleExpr;
Sebastian Redla9351792012-02-11 23:51:47 +00002077 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
2078 move_arg(ArgExprs));
Nate Begeman5ec4b312009-08-10 23:49:36 +00002079 }
Chris Lattner4add4e62006-08-11 01:33:00 +00002080 } else {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002081 InMessageExpressionRAIIObject InMessage(*this, false);
2082
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002083 Result = ParseExpression(MaybeTypeCast);
Chris Lattner4add4e62006-08-11 01:33:00 +00002084 ExprType = SimpleExpr;
John McCallc5e6b972011-04-06 02:35:25 +00002085
2086 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002087 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCallb268a282010-08-23 23:25:46 +00002088 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Chris Lattnerf8339772006-08-10 22:01:51 +00002089 }
Sebastian Redl90893182008-12-11 22:33:27 +00002090
Chris Lattner4564bc12006-08-10 23:14:52 +00002091 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00002092 if (Result.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +00002093 SkipUntil(tok::r_paren);
Chris Lattnerd8980502008-12-12 06:00:12 +00002094 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00002095 }
Mike Stump11289f42009-09-09 15:08:12 +00002096
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002097 T.consumeClose();
2098 RParenLoc = T.getCloseLocation();
Sebastian Redl90893182008-12-11 22:33:27 +00002099 return move(Result);
Chris Lattnerc951dae2006-08-10 04:23:57 +00002100}
Chris Lattnerd3e98952006-10-06 05:22:26 +00002101
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002102/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2103/// and we are at the left brace.
2104///
2105/// postfix-expression: [C99 6.5.2]
2106/// '(' type-name ')' '{' initializer-list '}'
2107/// '(' type-name ')' '{' initializer-list ',' '}'
2108///
John McCalldadc5752010-08-24 06:29:42 +00002109ExprResult
John McCallba7bf592010-08-24 05:47:05 +00002110Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002111 SourceLocation LParenLoc,
2112 SourceLocation RParenLoc) {
2113 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
David Blaikiebbafb8a2012-03-11 07:00:24 +00002114 if (!getLangOpts().C99) // Compound literals don't exist in C90.
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002115 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCalldadc5752010-08-24 06:29:42 +00002116 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002117 if (!Result.isInvalid() && Ty)
John McCallb268a282010-08-23 23:25:46 +00002118 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002119 return move(Result);
2120}
2121
Chris Lattnerd3e98952006-10-06 05:22:26 +00002122/// ParseStringLiteralExpression - This handles the various token types that
2123/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2124/// translation phase #6].
2125///
2126/// primary-expression: [C99 6.5.1]
2127/// string-literal
Richard Smithd67aea22012-03-06 03:21:47 +00002128ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002129 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00002130
Chris Lattnerd3e98952006-10-06 05:22:26 +00002131 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2132 // considered to be strings for concatenation purposes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002133 SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00002134
Chris Lattnerd3e98952006-10-06 05:22:26 +00002135 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002136 StringToks.push_back(Tok);
2137 ConsumeStringToken();
2138 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00002139
2140 // Pass the set of string tokens, ready for concatenation, to the actions.
Richard Smithbcc22fc2012-03-09 08:00:36 +00002141 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(),
2142 AllowUserDefinedLiteral ? getCurScope() : 0);
Chris Lattnerd3e98952006-10-06 05:22:26 +00002143}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002144
Benjamin Kramere56f3932011-12-23 17:00:35 +00002145/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2146/// [C11 6.5.1.1].
Peter Collingbourne91147592011-04-15 00:35:48 +00002147///
2148/// generic-selection:
2149/// _Generic ( assignment-expression , generic-assoc-list )
2150/// generic-assoc-list:
2151/// generic-association
2152/// generic-assoc-list , generic-association
2153/// generic-association:
2154/// type-name : assignment-expression
2155/// default : assignment-expression
2156ExprResult Parser::ParseGenericSelectionExpression() {
2157 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2158 SourceLocation KeyLoc = ConsumeToken();
2159
David Blaikiebbafb8a2012-03-11 07:00:24 +00002160 if (!getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +00002161 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbourne91147592011-04-15 00:35:48 +00002162
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002163 BalancedDelimiterTracker T(*this, tok::l_paren);
2164 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne91147592011-04-15 00:35:48 +00002165 return ExprError();
2166
2167 ExprResult ControllingExpr;
2168 {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002169 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbourne91147592011-04-15 00:35:48 +00002170 // not evaluated."
2171 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2172 ControllingExpr = ParseAssignmentExpression();
2173 if (ControllingExpr.isInvalid()) {
2174 SkipUntil(tok::r_paren);
2175 return ExprError();
2176 }
2177 }
2178
2179 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2180 SkipUntil(tok::r_paren);
2181 return ExprError();
2182 }
2183
2184 SourceLocation DefaultLoc;
2185 TypeVector Types(Actions);
2186 ExprVector Exprs(Actions);
2187 while (1) {
2188 ParsedType Ty;
2189 if (Tok.is(tok::kw_default)) {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002190 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbourne91147592011-04-15 00:35:48 +00002191 // generic association."
2192 if (!DefaultLoc.isInvalid()) {
2193 Diag(Tok, diag::err_duplicate_default_assoc);
2194 Diag(DefaultLoc, diag::note_previous_default_assoc);
2195 SkipUntil(tok::r_paren);
2196 return ExprError();
2197 }
2198 DefaultLoc = ConsumeToken();
2199 Ty = ParsedType();
2200 } else {
2201 ColonProtectionRAIIObject X(*this);
2202 TypeResult TR = ParseTypeName();
2203 if (TR.isInvalid()) {
2204 SkipUntil(tok::r_paren);
2205 return ExprError();
2206 }
2207 Ty = TR.release();
2208 }
2209 Types.push_back(Ty);
2210
2211 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2212 SkipUntil(tok::r_paren);
2213 return ExprError();
2214 }
2215
2216 // FIXME: These expressions should be parsed in a potentially potentially
2217 // evaluated context.
2218 ExprResult ER(ParseAssignmentExpression());
2219 if (ER.isInvalid()) {
2220 SkipUntil(tok::r_paren);
2221 return ExprError();
2222 }
2223 Exprs.push_back(ER.release());
2224
2225 if (Tok.isNot(tok::comma))
2226 break;
2227 ConsumeToken();
2228 }
2229
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002230 T.consumeClose();
2231 if (T.getCloseLocation().isInvalid())
Peter Collingbourne91147592011-04-15 00:35:48 +00002232 return ExprError();
2233
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002234 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2235 T.getCloseLocation(),
Peter Collingbourne91147592011-04-15 00:35:48 +00002236 ControllingExpr.release(),
2237 move_arg(Types), move_arg(Exprs));
2238}
2239
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002240/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2241///
2242/// argument-expression-list:
2243/// assignment-expression
2244/// argument-expression-list , assignment-expression
2245///
2246/// [C++] expression-list:
Sebastian Redl3da34892011-06-05 12:23:16 +00002247/// [C++] assignment-expression
2248/// [C++] expression-list , assignment-expression
2249///
2250/// [C++0x] expression-list:
2251/// [C++0x] initializer-list
2252///
2253/// [C++0x] initializer-list
2254/// [C++0x] initializer-clause ...[opt]
2255/// [C++0x] initializer-list , initializer-clause ...[opt]
2256///
2257/// [C++0x] initializer-clause:
2258/// [C++0x] assignment-expression
2259/// [C++0x] braced-init-list
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002260///
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002261bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2262 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallfaf5fb42010-08-26 23:41:50 +00002263 void (Sema::*Completer)(Scope *S,
John McCall37ad5512010-08-23 06:44:23 +00002264 Expr *Data,
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002265 llvm::ArrayRef<Expr *> Args),
John McCall37ad5512010-08-23 06:44:23 +00002266 Expr *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002267 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00002268 if (Tok.is(tok::code_completion)) {
2269 if (Completer)
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002270 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor9c1f1bf2011-02-17 03:09:23 +00002271 else
2272 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002273 cutOffParsing();
2274 return true;
Douglas Gregorcabea402009-09-22 15:41:20 +00002275 }
Sebastian Redl3da34892011-06-05 12:23:16 +00002276
2277 ExprResult Expr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002278 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002279 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00002280 Expr = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00002281 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00002282 Expr = ParseAssignmentExpression();
2283
Douglas Gregor968f23a2011-01-03 19:31:53 +00002284 if (Tok.is(tok::ellipsis))
2285 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002286 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002287 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00002288
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002289 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002290
2291 if (Tok.isNot(tok::comma))
2292 return false;
2293 // Move to the next argument, remember where the comma was.
2294 CommaLocs.push_back(ConsumeToken());
2295 }
2296}
Steve Naroff0ac012832008-08-28 19:20:44 +00002297
Mike Stump82f071f2009-02-04 22:31:32 +00002298/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2299///
2300/// [clang] block-id:
2301/// [clang] specifier-qualifier-list block-declarator
2302///
2303void Parser::ParseBlockId() {
Douglas Gregor643c3302010-10-18 21:34:55 +00002304 if (Tok.is(tok::code_completion)) {
2305 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002306 return cutOffParsing();
Douglas Gregor643c3302010-10-18 21:34:55 +00002307 }
2308
Mike Stump82f071f2009-02-04 22:31:32 +00002309 // Parse the specifier-qualifier-list piece.
John McCall084e83d2011-03-24 11:26:52 +00002310 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002311 ParseSpecifierQualifierList(DS);
2312
2313 // Parse the block-declarator.
2314 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2315 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002316
Mike Stump56ed2ea2009-04-29 21:40:37 +00002317 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall084e83d2011-03-24 11:26:52 +00002318 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump56ed2ea2009-04-29 21:40:37 +00002319
John McCall53fa7142010-12-24 02:08:15 +00002320 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002321
Mike Stump82f071f2009-02-04 22:31:32 +00002322 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002323 Actions.ActOnBlockArguments(DeclaratorInfo, getCurScope());
Mike Stump82f071f2009-02-04 22:31:32 +00002324}
2325
Steve Naroff0ac012832008-08-28 19:20:44 +00002326/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00002327/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00002328///
2329/// block-literal:
2330/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00002331/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00002332/// [clang] block-args:
2333/// [clang] '(' parameter-list ')'
2334///
John McCalldadc5752010-08-24 06:29:42 +00002335ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00002336 assert(Tok.is(tok::caret) && "block literal starts with ^");
2337 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002338
Chris Lattnerf6801202009-03-05 07:32:12 +00002339 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2340 "block literal parsing");
2341
Mike Stump11289f42009-09-09 15:08:12 +00002342 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00002343 // argument decls, decls within the compound expression, etc. This also
2344 // allows determining whether a variable reference inside the block is
2345 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002346 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002347 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00002348
2349 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002350 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump11289f42009-09-09 15:08:12 +00002351
Steve Naroff0ac012832008-08-28 19:20:44 +00002352 // Parse the return type if present.
John McCall084e83d2011-03-24 11:26:52 +00002353 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002354 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002355 // FIXME: Since the return type isn't actually parsed, it can't be used to
2356 // fill ParamInfo with an initial valid range, so do it manually.
2357 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002358
Steve Naroff0ac012832008-08-28 19:20:44 +00002359 // If this block has arguments, parse them. There is no ambiguity here with
2360 // the expression case, because the expression case requires a parameter list.
2361 if (Tok.is(tok::l_paren)) {
2362 ParseParenDeclarator(ParamInfo);
2363 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002364 // SetIdentifier sets the source range end, but in this case we're past
2365 // that location.
2366 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00002367 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002368 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002369 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00002370 // If there was an error parsing the arguments, they may have
2371 // tried to use ^(x+y) which requires an argument list. Just
2372 // skip the whole block literal.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002373 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002374 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00002375 }
Mike Stump88788fe2009-04-29 19:03:13 +00002376
John McCall53fa7142010-12-24 02:08:15 +00002377 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002378
Mike Stump82f071f2009-02-04 22:31:32 +00002379 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002380 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Mike Stumpd73e4412009-04-14 18:24:37 +00002381 } else if (!Tok.is(tok::l_brace)) {
Mike Stump82f071f2009-02-04 22:31:32 +00002382 ParseBlockId();
Steve Naroff0ac012832008-08-28 19:20:44 +00002383 } else {
2384 // Otherwise, pretend we saw (void).
John McCall084e83d2011-03-24 11:26:52 +00002385 ParsedAttributes attrs(AttrFactory);
2386 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002387 SourceLocation(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002388 0, 0, 0,
Douglas Gregor54992352011-01-26 03:43:54 +00002389 true, SourceLocation(),
Douglas Gregorad69e652011-07-13 21:47:47 +00002390 SourceLocation(),
Douglas Gregore248eea2011-10-19 06:04:55 +00002391 SourceLocation(),
2392 SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00002393 EST_None,
2394 SourceLocation(),
Richard Smith2331bbf2012-05-02 22:22:32 +00002395 0, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002396 CaretLoc, CaretLoc,
2397 ParamInfo),
John McCall084e83d2011-03-24 11:26:52 +00002398 attrs, CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00002399
John McCall53fa7142010-12-24 02:08:15 +00002400 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002401
Mike Stump82f071f2009-02-04 22:31:32 +00002402 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002403 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Steve Naroff0ac012832008-08-28 19:20:44 +00002404 }
2405
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002406
John McCalldadc5752010-08-24 06:29:42 +00002407 ExprResult Result(true);
Chris Lattner9eac9312009-03-27 04:18:06 +00002408 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002409 // Saw something like: ^expr
2410 Diag(Tok, diag::err_expected_expression);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002411 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002412 return ExprError();
2413 }
Mike Stump11289f42009-09-09 15:08:12 +00002414
John McCalldadc5752010-08-24 06:29:42 +00002415 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002416 BlockScope.Exit();
Chris Lattner9eac9312009-03-27 04:18:06 +00002417 if (!Stmt.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002418 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9eac9312009-03-27 04:18:06 +00002419 else
Douglas Gregor0be31a22010-07-02 17:43:08 +00002420 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002421 return move(Result);
Steve Naroff0ac012832008-08-28 19:20:44 +00002422}
Ted Kremeneke65b0862012-03-06 20:05:56 +00002423
2424/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
2425///
2426/// '__objc_yes'
2427/// '__objc_no'
2428ExprResult Parser::ParseObjCBoolLiteral() {
2429 tok::TokenKind Kind = Tok.getKind();
2430 return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
2431}