1*67e74705SXin Li //===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li ///
10*67e74705SXin Li /// \file
11*67e74705SXin Li /// \brief Provides the Expression parsing implementation.
12*67e74705SXin Li ///
13*67e74705SXin Li /// Expressions in C99 basically consist of a bunch of binary operators with
14*67e74705SXin Li /// unary operators and other random stuff at the leaves.
15*67e74705SXin Li ///
16*67e74705SXin Li /// In the C99 grammar, these unary operators bind tightest and are represented
17*67e74705SXin Li /// as the 'cast-expression' production. Everything else is either a binary
18*67e74705SXin Li /// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
19*67e74705SXin Li /// handled by ParseCastExpression, the higher level pieces are handled by
20*67e74705SXin Li /// ParseBinaryExpression.
21*67e74705SXin Li ///
22*67e74705SXin Li //===----------------------------------------------------------------------===//
23*67e74705SXin Li
24*67e74705SXin Li #include "clang/Parse/Parser.h"
25*67e74705SXin Li #include "RAIIObjectsForParser.h"
26*67e74705SXin Li #include "clang/AST/ASTContext.h"
27*67e74705SXin Li #include "clang/Basic/PrettyStackTrace.h"
28*67e74705SXin Li #include "clang/Sema/DeclSpec.h"
29*67e74705SXin Li #include "clang/Sema/ParsedTemplate.h"
30*67e74705SXin Li #include "clang/Sema/Scope.h"
31*67e74705SXin Li #include "clang/Sema/TypoCorrection.h"
32*67e74705SXin Li #include "llvm/ADT/SmallString.h"
33*67e74705SXin Li #include "llvm/ADT/SmallVector.h"
34*67e74705SXin Li using namespace clang;
35*67e74705SXin Li
36*67e74705SXin Li /// \brief Simple precedence-based parser for binary/ternary operators.
37*67e74705SXin Li ///
38*67e74705SXin Li /// Note: we diverge from the C99 grammar when parsing the assignment-expression
39*67e74705SXin Li /// production. C99 specifies that the LHS of an assignment operator should be
40*67e74705SXin Li /// parsed as a unary-expression, but consistency dictates that it be a
41*67e74705SXin Li /// conditional-expession. In practice, the important thing here is that the
42*67e74705SXin Li /// LHS of an assignment has to be an l-value, which productions between
43*67e74705SXin Li /// unary-expression and conditional-expression don't produce. Because we want
44*67e74705SXin Li /// consistency, we parse the LHS as a conditional-expression, then check for
45*67e74705SXin Li /// l-value-ness in semantic analysis stages.
46*67e74705SXin Li ///
47*67e74705SXin Li /// \verbatim
48*67e74705SXin Li /// pm-expression: [C++ 5.5]
49*67e74705SXin Li /// cast-expression
50*67e74705SXin Li /// pm-expression '.*' cast-expression
51*67e74705SXin Li /// pm-expression '->*' cast-expression
52*67e74705SXin Li ///
53*67e74705SXin Li /// multiplicative-expression: [C99 6.5.5]
54*67e74705SXin Li /// Note: in C++, apply pm-expression instead of cast-expression
55*67e74705SXin Li /// cast-expression
56*67e74705SXin Li /// multiplicative-expression '*' cast-expression
57*67e74705SXin Li /// multiplicative-expression '/' cast-expression
58*67e74705SXin Li /// multiplicative-expression '%' cast-expression
59*67e74705SXin Li ///
60*67e74705SXin Li /// additive-expression: [C99 6.5.6]
61*67e74705SXin Li /// multiplicative-expression
62*67e74705SXin Li /// additive-expression '+' multiplicative-expression
63*67e74705SXin Li /// additive-expression '-' multiplicative-expression
64*67e74705SXin Li ///
65*67e74705SXin Li /// shift-expression: [C99 6.5.7]
66*67e74705SXin Li /// additive-expression
67*67e74705SXin Li /// shift-expression '<<' additive-expression
68*67e74705SXin Li /// shift-expression '>>' additive-expression
69*67e74705SXin Li ///
70*67e74705SXin Li /// relational-expression: [C99 6.5.8]
71*67e74705SXin Li /// shift-expression
72*67e74705SXin Li /// relational-expression '<' shift-expression
73*67e74705SXin Li /// relational-expression '>' shift-expression
74*67e74705SXin Li /// relational-expression '<=' shift-expression
75*67e74705SXin Li /// relational-expression '>=' shift-expression
76*67e74705SXin Li ///
77*67e74705SXin Li /// equality-expression: [C99 6.5.9]
78*67e74705SXin Li /// relational-expression
79*67e74705SXin Li /// equality-expression '==' relational-expression
80*67e74705SXin Li /// equality-expression '!=' relational-expression
81*67e74705SXin Li ///
82*67e74705SXin Li /// AND-expression: [C99 6.5.10]
83*67e74705SXin Li /// equality-expression
84*67e74705SXin Li /// AND-expression '&' equality-expression
85*67e74705SXin Li ///
86*67e74705SXin Li /// exclusive-OR-expression: [C99 6.5.11]
87*67e74705SXin Li /// AND-expression
88*67e74705SXin Li /// exclusive-OR-expression '^' AND-expression
89*67e74705SXin Li ///
90*67e74705SXin Li /// inclusive-OR-expression: [C99 6.5.12]
91*67e74705SXin Li /// exclusive-OR-expression
92*67e74705SXin Li /// inclusive-OR-expression '|' exclusive-OR-expression
93*67e74705SXin Li ///
94*67e74705SXin Li /// logical-AND-expression: [C99 6.5.13]
95*67e74705SXin Li /// inclusive-OR-expression
96*67e74705SXin Li /// logical-AND-expression '&&' inclusive-OR-expression
97*67e74705SXin Li ///
98*67e74705SXin Li /// logical-OR-expression: [C99 6.5.14]
99*67e74705SXin Li /// logical-AND-expression
100*67e74705SXin Li /// logical-OR-expression '||' logical-AND-expression
101*67e74705SXin Li ///
102*67e74705SXin Li /// conditional-expression: [C99 6.5.15]
103*67e74705SXin Li /// logical-OR-expression
104*67e74705SXin Li /// logical-OR-expression '?' expression ':' conditional-expression
105*67e74705SXin Li /// [GNU] logical-OR-expression '?' ':' conditional-expression
106*67e74705SXin Li /// [C++] the third operand is an assignment-expression
107*67e74705SXin Li ///
108*67e74705SXin Li /// assignment-expression: [C99 6.5.16]
109*67e74705SXin Li /// conditional-expression
110*67e74705SXin Li /// unary-expression assignment-operator assignment-expression
111*67e74705SXin Li /// [C++] throw-expression [C++ 15]
112*67e74705SXin Li ///
113*67e74705SXin Li /// assignment-operator: one of
114*67e74705SXin Li /// = *= /= %= += -= <<= >>= &= ^= |=
115*67e74705SXin Li ///
116*67e74705SXin Li /// expression: [C99 6.5.17]
117*67e74705SXin Li /// assignment-expression ...[opt]
118*67e74705SXin Li /// expression ',' assignment-expression ...[opt]
119*67e74705SXin Li /// \endverbatim
ParseExpression(TypeCastState isTypeCast)120*67e74705SXin Li ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
121*67e74705SXin Li ExprResult LHS(ParseAssignmentExpression(isTypeCast));
122*67e74705SXin Li return ParseRHSOfBinaryExpression(LHS, prec::Comma);
123*67e74705SXin Li }
124*67e74705SXin Li
125*67e74705SXin Li /// This routine is called when the '@' is seen and consumed.
126*67e74705SXin Li /// Current token is an Identifier and is not a 'try'. This
127*67e74705SXin Li /// routine is necessary to disambiguate \@try-statement from,
128*67e74705SXin Li /// for example, \@encode-expression.
129*67e74705SXin Li ///
130*67e74705SXin Li ExprResult
ParseExpressionWithLeadingAt(SourceLocation AtLoc)131*67e74705SXin Li Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
132*67e74705SXin Li ExprResult LHS(ParseObjCAtExpression(AtLoc));
133*67e74705SXin Li return ParseRHSOfBinaryExpression(LHS, prec::Comma);
134*67e74705SXin Li }
135*67e74705SXin Li
136*67e74705SXin Li /// This routine is called when a leading '__extension__' is seen and
137*67e74705SXin Li /// consumed. This is necessary because the token gets consumed in the
138*67e74705SXin Li /// process of disambiguating between an expression and a declaration.
139*67e74705SXin Li ExprResult
ParseExpressionWithLeadingExtension(SourceLocation ExtLoc)140*67e74705SXin Li Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
141*67e74705SXin Li ExprResult LHS(true);
142*67e74705SXin Li {
143*67e74705SXin Li // Silence extension warnings in the sub-expression
144*67e74705SXin Li ExtensionRAIIObject O(Diags);
145*67e74705SXin Li
146*67e74705SXin Li LHS = ParseCastExpression(false);
147*67e74705SXin Li }
148*67e74705SXin Li
149*67e74705SXin Li if (!LHS.isInvalid())
150*67e74705SXin Li LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
151*67e74705SXin Li LHS.get());
152*67e74705SXin Li
153*67e74705SXin Li return ParseRHSOfBinaryExpression(LHS, prec::Comma);
154*67e74705SXin Li }
155*67e74705SXin Li
156*67e74705SXin Li /// \brief Parse an expr that doesn't include (top-level) commas.
ParseAssignmentExpression(TypeCastState isTypeCast)157*67e74705SXin Li ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
158*67e74705SXin Li if (Tok.is(tok::code_completion)) {
159*67e74705SXin Li Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
160*67e74705SXin Li cutOffParsing();
161*67e74705SXin Li return ExprError();
162*67e74705SXin Li }
163*67e74705SXin Li
164*67e74705SXin Li if (Tok.is(tok::kw_throw))
165*67e74705SXin Li return ParseThrowExpression();
166*67e74705SXin Li if (Tok.is(tok::kw_co_yield))
167*67e74705SXin Li return ParseCoyieldExpression();
168*67e74705SXin Li
169*67e74705SXin Li ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
170*67e74705SXin Li /*isAddressOfOperand=*/false,
171*67e74705SXin Li isTypeCast);
172*67e74705SXin Li return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
173*67e74705SXin Li }
174*67e74705SXin Li
175*67e74705SXin Li /// \brief Parse an assignment expression where part of an Objective-C message
176*67e74705SXin Li /// send has already been parsed.
177*67e74705SXin Li ///
178*67e74705SXin Li /// In this case \p LBracLoc indicates the location of the '[' of the message
179*67e74705SXin Li /// send, and either \p ReceiverName or \p ReceiverExpr is non-null indicating
180*67e74705SXin Li /// the receiver of the message.
181*67e74705SXin Li ///
182*67e74705SXin Li /// Since this handles full assignment-expression's, it handles postfix
183*67e74705SXin Li /// expressions and other binary operators for these expressions as well.
184*67e74705SXin Li ExprResult
ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,SourceLocation SuperLoc,ParsedType ReceiverType,Expr * ReceiverExpr)185*67e74705SXin Li Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
186*67e74705SXin Li SourceLocation SuperLoc,
187*67e74705SXin Li ParsedType ReceiverType,
188*67e74705SXin Li Expr *ReceiverExpr) {
189*67e74705SXin Li ExprResult R
190*67e74705SXin Li = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
191*67e74705SXin Li ReceiverType, ReceiverExpr);
192*67e74705SXin Li R = ParsePostfixExpressionSuffix(R);
193*67e74705SXin Li return ParseRHSOfBinaryExpression(R, prec::Assignment);
194*67e74705SXin Li }
195*67e74705SXin Li
196*67e74705SXin Li
ParseConstantExpression(TypeCastState isTypeCast)197*67e74705SXin Li ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
198*67e74705SXin Li // C++03 [basic.def.odr]p2:
199*67e74705SXin Li // An expression is potentially evaluated unless it appears where an
200*67e74705SXin Li // integral constant expression is required (see 5.19) [...].
201*67e74705SXin Li // C++98 and C++11 have no such rule, but this is only a defect in C++98.
202*67e74705SXin Li EnterExpressionEvaluationContext Unevaluated(Actions,
203*67e74705SXin Li Sema::ConstantEvaluated);
204*67e74705SXin Li
205*67e74705SXin Li ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
206*67e74705SXin Li ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
207*67e74705SXin Li return Actions.ActOnConstantExpression(Res);
208*67e74705SXin Li }
209*67e74705SXin Li
210*67e74705SXin Li /// \brief Parse a constraint-expression.
211*67e74705SXin Li ///
212*67e74705SXin Li /// \verbatim
213*67e74705SXin Li /// constraint-expression: [Concepts TS temp.constr.decl p1]
214*67e74705SXin Li /// logical-or-expression
215*67e74705SXin Li /// \endverbatim
ParseConstraintExpression()216*67e74705SXin Li ExprResult Parser::ParseConstraintExpression() {
217*67e74705SXin Li // FIXME: this may erroneously consume a function-body as the braced
218*67e74705SXin Li // initializer list of a compound literal
219*67e74705SXin Li //
220*67e74705SXin Li // FIXME: this may erroneously consume a parenthesized rvalue reference
221*67e74705SXin Li // declarator as a parenthesized address-of-label expression
222*67e74705SXin Li ExprResult LHS(ParseCastExpression(/*isUnaryExpression=*/false));
223*67e74705SXin Li ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::LogicalOr));
224*67e74705SXin Li
225*67e74705SXin Li return Res;
226*67e74705SXin Li }
227*67e74705SXin Li
isNotExpressionStart()228*67e74705SXin Li bool Parser::isNotExpressionStart() {
229*67e74705SXin Li tok::TokenKind K = Tok.getKind();
230*67e74705SXin Li if (K == tok::l_brace || K == tok::r_brace ||
231*67e74705SXin Li K == tok::kw_for || K == tok::kw_while ||
232*67e74705SXin Li K == tok::kw_if || K == tok::kw_else ||
233*67e74705SXin Li K == tok::kw_goto || K == tok::kw_try)
234*67e74705SXin Li return true;
235*67e74705SXin Li // If this is a decl-specifier, we can't be at the start of an expression.
236*67e74705SXin Li return isKnownToBeDeclarationSpecifier();
237*67e74705SXin Li }
238*67e74705SXin Li
isFoldOperator(prec::Level Level)239*67e74705SXin Li static bool isFoldOperator(prec::Level Level) {
240*67e74705SXin Li return Level > prec::Unknown && Level != prec::Conditional;
241*67e74705SXin Li }
isFoldOperator(tok::TokenKind Kind)242*67e74705SXin Li static bool isFoldOperator(tok::TokenKind Kind) {
243*67e74705SXin Li return isFoldOperator(getBinOpPrecedence(Kind, false, true));
244*67e74705SXin Li }
245*67e74705SXin Li
246*67e74705SXin Li /// \brief Parse a binary expression that starts with \p LHS and has a
247*67e74705SXin Li /// precedence of at least \p MinPrec.
248*67e74705SXin Li ExprResult
ParseRHSOfBinaryExpression(ExprResult LHS,prec::Level MinPrec)249*67e74705SXin Li Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
250*67e74705SXin Li prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
251*67e74705SXin Li GreaterThanIsOperator,
252*67e74705SXin Li getLangOpts().CPlusPlus11);
253*67e74705SXin Li SourceLocation ColonLoc;
254*67e74705SXin Li
255*67e74705SXin Li while (1) {
256*67e74705SXin Li // If this token has a lower precedence than we are allowed to parse (e.g.
257*67e74705SXin Li // because we are called recursively, or because the token is not a binop),
258*67e74705SXin Li // then we are done!
259*67e74705SXin Li if (NextTokPrec < MinPrec)
260*67e74705SXin Li return LHS;
261*67e74705SXin Li
262*67e74705SXin Li // Consume the operator, saving the operator token for error reporting.
263*67e74705SXin Li Token OpToken = Tok;
264*67e74705SXin Li ConsumeToken();
265*67e74705SXin Li
266*67e74705SXin Li if (OpToken.is(tok::caretcaret)) {
267*67e74705SXin Li return ExprError(Diag(Tok, diag::err_opencl_logical_exclusive_or));
268*67e74705SXin Li }
269*67e74705SXin Li // Bail out when encountering a comma followed by a token which can't
270*67e74705SXin Li // possibly be the start of an expression. For instance:
271*67e74705SXin Li // int f() { return 1, }
272*67e74705SXin Li // We can't do this before consuming the comma, because
273*67e74705SXin Li // isNotExpressionStart() looks at the token stream.
274*67e74705SXin Li if (OpToken.is(tok::comma) && isNotExpressionStart()) {
275*67e74705SXin Li PP.EnterToken(Tok);
276*67e74705SXin Li Tok = OpToken;
277*67e74705SXin Li return LHS;
278*67e74705SXin Li }
279*67e74705SXin Li
280*67e74705SXin Li // If the next token is an ellipsis, then this is a fold-expression. Leave
281*67e74705SXin Li // it alone so we can handle it in the paren expression.
282*67e74705SXin Li if (isFoldOperator(NextTokPrec) && Tok.is(tok::ellipsis)) {
283*67e74705SXin Li // FIXME: We can't check this via lookahead before we consume the token
284*67e74705SXin Li // because that tickles a lexer bug.
285*67e74705SXin Li PP.EnterToken(Tok);
286*67e74705SXin Li Tok = OpToken;
287*67e74705SXin Li return LHS;
288*67e74705SXin Li }
289*67e74705SXin Li
290*67e74705SXin Li // Special case handling for the ternary operator.
291*67e74705SXin Li ExprResult TernaryMiddle(true);
292*67e74705SXin Li if (NextTokPrec == prec::Conditional) {
293*67e74705SXin Li if (Tok.isNot(tok::colon)) {
294*67e74705SXin Li // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
295*67e74705SXin Li ColonProtectionRAIIObject X(*this);
296*67e74705SXin Li
297*67e74705SXin Li // Handle this production specially:
298*67e74705SXin Li // logical-OR-expression '?' expression ':' conditional-expression
299*67e74705SXin Li // In particular, the RHS of the '?' is 'expression', not
300*67e74705SXin Li // 'logical-OR-expression' as we might expect.
301*67e74705SXin Li TernaryMiddle = ParseExpression();
302*67e74705SXin Li if (TernaryMiddle.isInvalid()) {
303*67e74705SXin Li Actions.CorrectDelayedTyposInExpr(LHS);
304*67e74705SXin Li LHS = ExprError();
305*67e74705SXin Li TernaryMiddle = nullptr;
306*67e74705SXin Li }
307*67e74705SXin Li } else {
308*67e74705SXin Li // Special case handling of "X ? Y : Z" where Y is empty:
309*67e74705SXin Li // logical-OR-expression '?' ':' conditional-expression [GNU]
310*67e74705SXin Li TernaryMiddle = nullptr;
311*67e74705SXin Li Diag(Tok, diag::ext_gnu_conditional_expr);
312*67e74705SXin Li }
313*67e74705SXin Li
314*67e74705SXin Li if (!TryConsumeToken(tok::colon, ColonLoc)) {
315*67e74705SXin Li // Otherwise, we're missing a ':'. Assume that this was a typo that
316*67e74705SXin Li // the user forgot. If we're not in a macro expansion, we can suggest
317*67e74705SXin Li // a fixit hint. If there were two spaces before the current token,
318*67e74705SXin Li // suggest inserting the colon in between them, otherwise insert ": ".
319*67e74705SXin Li SourceLocation FILoc = Tok.getLocation();
320*67e74705SXin Li const char *FIText = ": ";
321*67e74705SXin Li const SourceManager &SM = PP.getSourceManager();
322*67e74705SXin Li if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
323*67e74705SXin Li assert(FILoc.isFileID());
324*67e74705SXin Li bool IsInvalid = false;
325*67e74705SXin Li const char *SourcePtr =
326*67e74705SXin Li SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
327*67e74705SXin Li if (!IsInvalid && *SourcePtr == ' ') {
328*67e74705SXin Li SourcePtr =
329*67e74705SXin Li SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
330*67e74705SXin Li if (!IsInvalid && *SourcePtr == ' ') {
331*67e74705SXin Li FILoc = FILoc.getLocWithOffset(-1);
332*67e74705SXin Li FIText = ":";
333*67e74705SXin Li }
334*67e74705SXin Li }
335*67e74705SXin Li }
336*67e74705SXin Li
337*67e74705SXin Li Diag(Tok, diag::err_expected)
338*67e74705SXin Li << tok::colon << FixItHint::CreateInsertion(FILoc, FIText);
339*67e74705SXin Li Diag(OpToken, diag::note_matching) << tok::question;
340*67e74705SXin Li ColonLoc = Tok.getLocation();
341*67e74705SXin Li }
342*67e74705SXin Li }
343*67e74705SXin Li
344*67e74705SXin Li // Code completion for the right-hand side of an assignment expression
345*67e74705SXin Li // goes through a special hook that takes the left-hand side into account.
346*67e74705SXin Li if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
347*67e74705SXin Li Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
348*67e74705SXin Li cutOffParsing();
349*67e74705SXin Li return ExprError();
350*67e74705SXin Li }
351*67e74705SXin Li
352*67e74705SXin Li // Parse another leaf here for the RHS of the operator.
353*67e74705SXin Li // ParseCastExpression works here because all RHS expressions in C have it
354*67e74705SXin Li // as a prefix, at least. However, in C++, an assignment-expression could
355*67e74705SXin Li // be a throw-expression, which is not a valid cast-expression.
356*67e74705SXin Li // Therefore we need some special-casing here.
357*67e74705SXin Li // Also note that the third operand of the conditional operator is
358*67e74705SXin Li // an assignment-expression in C++, and in C++11, we can have a
359*67e74705SXin Li // braced-init-list on the RHS of an assignment. For better diagnostics,
360*67e74705SXin Li // parse as if we were allowed braced-init-lists everywhere, and check that
361*67e74705SXin Li // they only appear on the RHS of assignments later.
362*67e74705SXin Li ExprResult RHS;
363*67e74705SXin Li bool RHSIsInitList = false;
364*67e74705SXin Li if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
365*67e74705SXin Li RHS = ParseBraceInitializer();
366*67e74705SXin Li RHSIsInitList = true;
367*67e74705SXin Li } else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional)
368*67e74705SXin Li RHS = ParseAssignmentExpression();
369*67e74705SXin Li else
370*67e74705SXin Li RHS = ParseCastExpression(false);
371*67e74705SXin Li
372*67e74705SXin Li if (RHS.isInvalid()) {
373*67e74705SXin Li // FIXME: Errors generated by the delayed typo correction should be
374*67e74705SXin Li // printed before errors from parsing the RHS, not after.
375*67e74705SXin Li Actions.CorrectDelayedTyposInExpr(LHS);
376*67e74705SXin Li if (TernaryMiddle.isUsable())
377*67e74705SXin Li TernaryMiddle = Actions.CorrectDelayedTyposInExpr(TernaryMiddle);
378*67e74705SXin Li LHS = ExprError();
379*67e74705SXin Li }
380*67e74705SXin Li
381*67e74705SXin Li // Remember the precedence of this operator and get the precedence of the
382*67e74705SXin Li // operator immediately to the right of the RHS.
383*67e74705SXin Li prec::Level ThisPrec = NextTokPrec;
384*67e74705SXin Li NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
385*67e74705SXin Li getLangOpts().CPlusPlus11);
386*67e74705SXin Li
387*67e74705SXin Li // Assignment and conditional expressions are right-associative.
388*67e74705SXin Li bool isRightAssoc = ThisPrec == prec::Conditional ||
389*67e74705SXin Li ThisPrec == prec::Assignment;
390*67e74705SXin Li
391*67e74705SXin Li // Get the precedence of the operator to the right of the RHS. If it binds
392*67e74705SXin Li // more tightly with RHS than we do, evaluate it completely first.
393*67e74705SXin Li if (ThisPrec < NextTokPrec ||
394*67e74705SXin Li (ThisPrec == NextTokPrec && isRightAssoc)) {
395*67e74705SXin Li if (!RHS.isInvalid() && RHSIsInitList) {
396*67e74705SXin Li Diag(Tok, diag::err_init_list_bin_op)
397*67e74705SXin Li << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get());
398*67e74705SXin Li RHS = ExprError();
399*67e74705SXin Li }
400*67e74705SXin Li // If this is left-associative, only parse things on the RHS that bind
401*67e74705SXin Li // more tightly than the current operator. If it is left-associative, it
402*67e74705SXin Li // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
403*67e74705SXin Li // A=(B=(C=D)), where each paren is a level of recursion here.
404*67e74705SXin Li // The function takes ownership of the RHS.
405*67e74705SXin Li RHS = ParseRHSOfBinaryExpression(RHS,
406*67e74705SXin Li static_cast<prec::Level>(ThisPrec + !isRightAssoc));
407*67e74705SXin Li RHSIsInitList = false;
408*67e74705SXin Li
409*67e74705SXin Li if (RHS.isInvalid()) {
410*67e74705SXin Li // FIXME: Errors generated by the delayed typo correction should be
411*67e74705SXin Li // printed before errors from ParseRHSOfBinaryExpression, not after.
412*67e74705SXin Li Actions.CorrectDelayedTyposInExpr(LHS);
413*67e74705SXin Li if (TernaryMiddle.isUsable())
414*67e74705SXin Li TernaryMiddle = Actions.CorrectDelayedTyposInExpr(TernaryMiddle);
415*67e74705SXin Li LHS = ExprError();
416*67e74705SXin Li }
417*67e74705SXin Li
418*67e74705SXin Li NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
419*67e74705SXin Li getLangOpts().CPlusPlus11);
420*67e74705SXin Li }
421*67e74705SXin Li
422*67e74705SXin Li if (!RHS.isInvalid() && RHSIsInitList) {
423*67e74705SXin Li if (ThisPrec == prec::Assignment) {
424*67e74705SXin Li Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
425*67e74705SXin Li << Actions.getExprRange(RHS.get());
426*67e74705SXin Li } else {
427*67e74705SXin Li Diag(OpToken, diag::err_init_list_bin_op)
428*67e74705SXin Li << /*RHS*/1 << PP.getSpelling(OpToken)
429*67e74705SXin Li << Actions.getExprRange(RHS.get());
430*67e74705SXin Li LHS = ExprError();
431*67e74705SXin Li }
432*67e74705SXin Li }
433*67e74705SXin Li
434*67e74705SXin Li ExprResult OrigLHS = LHS;
435*67e74705SXin Li if (!LHS.isInvalid()) {
436*67e74705SXin Li // Combine the LHS and RHS into the LHS (e.g. build AST).
437*67e74705SXin Li if (TernaryMiddle.isInvalid()) {
438*67e74705SXin Li // If we're using '>>' as an operator within a template
439*67e74705SXin Li // argument list (in C++98), suggest the addition of
440*67e74705SXin Li // parentheses so that the code remains well-formed in C++0x.
441*67e74705SXin Li if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
442*67e74705SXin Li SuggestParentheses(OpToken.getLocation(),
443*67e74705SXin Li diag::warn_cxx11_right_shift_in_template_arg,
444*67e74705SXin Li SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
445*67e74705SXin Li Actions.getExprRange(RHS.get()).getEnd()));
446*67e74705SXin Li
447*67e74705SXin Li LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
448*67e74705SXin Li OpToken.getKind(), LHS.get(), RHS.get());
449*67e74705SXin Li
450*67e74705SXin Li // In this case, ActOnBinOp performed the CorrectDelayedTyposInExpr check.
451*67e74705SXin Li if (!getLangOpts().CPlusPlus)
452*67e74705SXin Li continue;
453*67e74705SXin Li } else {
454*67e74705SXin Li LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
455*67e74705SXin Li LHS.get(), TernaryMiddle.get(),
456*67e74705SXin Li RHS.get());
457*67e74705SXin Li }
458*67e74705SXin Li }
459*67e74705SXin Li // Ensure potential typos aren't left undiagnosed.
460*67e74705SXin Li if (LHS.isInvalid()) {
461*67e74705SXin Li Actions.CorrectDelayedTyposInExpr(OrigLHS);
462*67e74705SXin Li Actions.CorrectDelayedTyposInExpr(TernaryMiddle);
463*67e74705SXin Li Actions.CorrectDelayedTyposInExpr(RHS);
464*67e74705SXin Li }
465*67e74705SXin Li }
466*67e74705SXin Li }
467*67e74705SXin Li
468*67e74705SXin Li /// \brief Parse a cast-expression, or, if \p isUnaryExpression is true,
469*67e74705SXin Li /// parse a unary-expression.
470*67e74705SXin Li ///
471*67e74705SXin Li /// \p isAddressOfOperand exists because an id-expression that is the
472*67e74705SXin Li /// operand of address-of gets special treatment due to member pointers.
473*67e74705SXin Li ///
ParseCastExpression(bool isUnaryExpression,bool isAddressOfOperand,TypeCastState isTypeCast)474*67e74705SXin Li ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
475*67e74705SXin Li bool isAddressOfOperand,
476*67e74705SXin Li TypeCastState isTypeCast) {
477*67e74705SXin Li bool NotCastExpr;
478*67e74705SXin Li ExprResult Res = ParseCastExpression(isUnaryExpression,
479*67e74705SXin Li isAddressOfOperand,
480*67e74705SXin Li NotCastExpr,
481*67e74705SXin Li isTypeCast);
482*67e74705SXin Li if (NotCastExpr)
483*67e74705SXin Li Diag(Tok, diag::err_expected_expression);
484*67e74705SXin Li return Res;
485*67e74705SXin Li }
486*67e74705SXin Li
487*67e74705SXin Li namespace {
488*67e74705SXin Li class CastExpressionIdValidator : public CorrectionCandidateCallback {
489*67e74705SXin Li public:
CastExpressionIdValidator(Token Next,bool AllowTypes,bool AllowNonTypes)490*67e74705SXin Li CastExpressionIdValidator(Token Next, bool AllowTypes, bool AllowNonTypes)
491*67e74705SXin Li : NextToken(Next), AllowNonTypes(AllowNonTypes) {
492*67e74705SXin Li WantTypeSpecifiers = WantFunctionLikeCasts = AllowTypes;
493*67e74705SXin Li }
494*67e74705SXin Li
ValidateCandidate(const TypoCorrection & candidate)495*67e74705SXin Li bool ValidateCandidate(const TypoCorrection &candidate) override {
496*67e74705SXin Li NamedDecl *ND = candidate.getCorrectionDecl();
497*67e74705SXin Li if (!ND)
498*67e74705SXin Li return candidate.isKeyword();
499*67e74705SXin Li
500*67e74705SXin Li if (isa<TypeDecl>(ND))
501*67e74705SXin Li return WantTypeSpecifiers;
502*67e74705SXin Li
503*67e74705SXin Li if (!AllowNonTypes || !CorrectionCandidateCallback::ValidateCandidate(candidate))
504*67e74705SXin Li return false;
505*67e74705SXin Li
506*67e74705SXin Li if (!NextToken.isOneOf(tok::equal, tok::arrow, tok::period))
507*67e74705SXin Li return true;
508*67e74705SXin Li
509*67e74705SXin Li for (auto *C : candidate) {
510*67e74705SXin Li NamedDecl *ND = C->getUnderlyingDecl();
511*67e74705SXin Li if (isa<ValueDecl>(ND) && !isa<FunctionDecl>(ND))
512*67e74705SXin Li return true;
513*67e74705SXin Li }
514*67e74705SXin Li return false;
515*67e74705SXin Li }
516*67e74705SXin Li
517*67e74705SXin Li private:
518*67e74705SXin Li Token NextToken;
519*67e74705SXin Li bool AllowNonTypes;
520*67e74705SXin Li };
521*67e74705SXin Li }
522*67e74705SXin Li
523*67e74705SXin Li /// \brief Parse a cast-expression, or, if \pisUnaryExpression is true, parse
524*67e74705SXin Li /// a unary-expression.
525*67e74705SXin Li ///
526*67e74705SXin Li /// \p isAddressOfOperand exists because an id-expression that is the operand
527*67e74705SXin Li /// of address-of gets special treatment due to member pointers. NotCastExpr
528*67e74705SXin Li /// is set to true if the token is not the start of a cast-expression, and no
529*67e74705SXin Li /// diagnostic is emitted in this case and no tokens are consumed.
530*67e74705SXin Li ///
531*67e74705SXin Li /// \verbatim
532*67e74705SXin Li /// cast-expression: [C99 6.5.4]
533*67e74705SXin Li /// unary-expression
534*67e74705SXin Li /// '(' type-name ')' cast-expression
535*67e74705SXin Li ///
536*67e74705SXin Li /// unary-expression: [C99 6.5.3]
537*67e74705SXin Li /// postfix-expression
538*67e74705SXin Li /// '++' unary-expression
539*67e74705SXin Li /// '--' unary-expression
540*67e74705SXin Li /// [Coro] 'co_await' cast-expression
541*67e74705SXin Li /// unary-operator cast-expression
542*67e74705SXin Li /// 'sizeof' unary-expression
543*67e74705SXin Li /// 'sizeof' '(' type-name ')'
544*67e74705SXin Li /// [C++11] 'sizeof' '...' '(' identifier ')'
545*67e74705SXin Li /// [GNU] '__alignof' unary-expression
546*67e74705SXin Li /// [GNU] '__alignof' '(' type-name ')'
547*67e74705SXin Li /// [C11] '_Alignof' '(' type-name ')'
548*67e74705SXin Li /// [C++11] 'alignof' '(' type-id ')'
549*67e74705SXin Li /// [GNU] '&&' identifier
550*67e74705SXin Li /// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7]
551*67e74705SXin Li /// [C++] new-expression
552*67e74705SXin Li /// [C++] delete-expression
553*67e74705SXin Li ///
554*67e74705SXin Li /// unary-operator: one of
555*67e74705SXin Li /// '&' '*' '+' '-' '~' '!'
556*67e74705SXin Li /// [GNU] '__extension__' '__real' '__imag'
557*67e74705SXin Li ///
558*67e74705SXin Li /// primary-expression: [C99 6.5.1]
559*67e74705SXin Li /// [C99] identifier
560*67e74705SXin Li /// [C++] id-expression
561*67e74705SXin Li /// constant
562*67e74705SXin Li /// string-literal
563*67e74705SXin Li /// [C++] boolean-literal [C++ 2.13.5]
564*67e74705SXin Li /// [C++11] 'nullptr' [C++11 2.14.7]
565*67e74705SXin Li /// [C++11] user-defined-literal
566*67e74705SXin Li /// '(' expression ')'
567*67e74705SXin Li /// [C11] generic-selection
568*67e74705SXin Li /// '__func__' [C99 6.4.2.2]
569*67e74705SXin Li /// [GNU] '__FUNCTION__'
570*67e74705SXin Li /// [MS] '__FUNCDNAME__'
571*67e74705SXin Li /// [MS] 'L__FUNCTION__'
572*67e74705SXin Li /// [GNU] '__PRETTY_FUNCTION__'
573*67e74705SXin Li /// [GNU] '(' compound-statement ')'
574*67e74705SXin Li /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
575*67e74705SXin Li /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
576*67e74705SXin Li /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
577*67e74705SXin Li /// assign-expr ')'
578*67e74705SXin Li /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
579*67e74705SXin Li /// [GNU] '__null'
580*67e74705SXin Li /// [OBJC] '[' objc-message-expr ']'
581*67e74705SXin Li /// [OBJC] '\@selector' '(' objc-selector-arg ')'
582*67e74705SXin Li /// [OBJC] '\@protocol' '(' identifier ')'
583*67e74705SXin Li /// [OBJC] '\@encode' '(' type-name ')'
584*67e74705SXin Li /// [OBJC] objc-string-literal
585*67e74705SXin Li /// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
586*67e74705SXin Li /// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3]
587*67e74705SXin Li /// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
588*67e74705SXin Li /// [C++11] typename-specifier braced-init-list [C++11 5.2.3]
589*67e74705SXin Li /// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
590*67e74705SXin Li /// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
591*67e74705SXin Li /// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
592*67e74705SXin Li /// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
593*67e74705SXin Li /// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
594*67e74705SXin Li /// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
595*67e74705SXin Li /// [C++] 'this' [C++ 9.3.2]
596*67e74705SXin Li /// [G++] unary-type-trait '(' type-id ')'
597*67e74705SXin Li /// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
598*67e74705SXin Li /// [EMBT] array-type-trait '(' type-id ',' integer ')'
599*67e74705SXin Li /// [clang] '^' block-literal
600*67e74705SXin Li ///
601*67e74705SXin Li /// constant: [C99 6.4.4]
602*67e74705SXin Li /// integer-constant
603*67e74705SXin Li /// floating-constant
604*67e74705SXin Li /// enumeration-constant -> identifier
605*67e74705SXin Li /// character-constant
606*67e74705SXin Li ///
607*67e74705SXin Li /// id-expression: [C++ 5.1]
608*67e74705SXin Li /// unqualified-id
609*67e74705SXin Li /// qualified-id
610*67e74705SXin Li ///
611*67e74705SXin Li /// unqualified-id: [C++ 5.1]
612*67e74705SXin Li /// identifier
613*67e74705SXin Li /// operator-function-id
614*67e74705SXin Li /// conversion-function-id
615*67e74705SXin Li /// '~' class-name
616*67e74705SXin Li /// template-id
617*67e74705SXin Li ///
618*67e74705SXin Li /// new-expression: [C++ 5.3.4]
619*67e74705SXin Li /// '::'[opt] 'new' new-placement[opt] new-type-id
620*67e74705SXin Li /// new-initializer[opt]
621*67e74705SXin Li /// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
622*67e74705SXin Li /// new-initializer[opt]
623*67e74705SXin Li ///
624*67e74705SXin Li /// delete-expression: [C++ 5.3.5]
625*67e74705SXin Li /// '::'[opt] 'delete' cast-expression
626*67e74705SXin Li /// '::'[opt] 'delete' '[' ']' cast-expression
627*67e74705SXin Li ///
628*67e74705SXin Li /// [GNU/Embarcadero] unary-type-trait:
629*67e74705SXin Li /// '__is_arithmetic'
630*67e74705SXin Li /// '__is_floating_point'
631*67e74705SXin Li /// '__is_integral'
632*67e74705SXin Li /// '__is_lvalue_expr'
633*67e74705SXin Li /// '__is_rvalue_expr'
634*67e74705SXin Li /// '__is_complete_type'
635*67e74705SXin Li /// '__is_void'
636*67e74705SXin Li /// '__is_array'
637*67e74705SXin Li /// '__is_function'
638*67e74705SXin Li /// '__is_reference'
639*67e74705SXin Li /// '__is_lvalue_reference'
640*67e74705SXin Li /// '__is_rvalue_reference'
641*67e74705SXin Li /// '__is_fundamental'
642*67e74705SXin Li /// '__is_object'
643*67e74705SXin Li /// '__is_scalar'
644*67e74705SXin Li /// '__is_compound'
645*67e74705SXin Li /// '__is_pointer'
646*67e74705SXin Li /// '__is_member_object_pointer'
647*67e74705SXin Li /// '__is_member_function_pointer'
648*67e74705SXin Li /// '__is_member_pointer'
649*67e74705SXin Li /// '__is_const'
650*67e74705SXin Li /// '__is_volatile'
651*67e74705SXin Li /// '__is_trivial'
652*67e74705SXin Li /// '__is_standard_layout'
653*67e74705SXin Li /// '__is_signed'
654*67e74705SXin Li /// '__is_unsigned'
655*67e74705SXin Li ///
656*67e74705SXin Li /// [GNU] unary-type-trait:
657*67e74705SXin Li /// '__has_nothrow_assign'
658*67e74705SXin Li /// '__has_nothrow_copy'
659*67e74705SXin Li /// '__has_nothrow_constructor'
660*67e74705SXin Li /// '__has_trivial_assign' [TODO]
661*67e74705SXin Li /// '__has_trivial_copy' [TODO]
662*67e74705SXin Li /// '__has_trivial_constructor'
663*67e74705SXin Li /// '__has_trivial_destructor'
664*67e74705SXin Li /// '__has_virtual_destructor'
665*67e74705SXin Li /// '__is_abstract' [TODO]
666*67e74705SXin Li /// '__is_class'
667*67e74705SXin Li /// '__is_empty' [TODO]
668*67e74705SXin Li /// '__is_enum'
669*67e74705SXin Li /// '__is_final'
670*67e74705SXin Li /// '__is_pod'
671*67e74705SXin Li /// '__is_polymorphic'
672*67e74705SXin Li /// '__is_sealed' [MS]
673*67e74705SXin Li /// '__is_trivial'
674*67e74705SXin Li /// '__is_union'
675*67e74705SXin Li ///
676*67e74705SXin Li /// [Clang] unary-type-trait:
677*67e74705SXin Li /// '__trivially_copyable'
678*67e74705SXin Li ///
679*67e74705SXin Li /// binary-type-trait:
680*67e74705SXin Li /// [GNU] '__is_base_of'
681*67e74705SXin Li /// [MS] '__is_convertible_to'
682*67e74705SXin Li /// '__is_convertible'
683*67e74705SXin Li /// '__is_same'
684*67e74705SXin Li ///
685*67e74705SXin Li /// [Embarcadero] array-type-trait:
686*67e74705SXin Li /// '__array_rank'
687*67e74705SXin Li /// '__array_extent'
688*67e74705SXin Li ///
689*67e74705SXin Li /// [Embarcadero] expression-trait:
690*67e74705SXin Li /// '__is_lvalue_expr'
691*67e74705SXin Li /// '__is_rvalue_expr'
692*67e74705SXin Li /// \endverbatim
693*67e74705SXin Li ///
ParseCastExpression(bool isUnaryExpression,bool isAddressOfOperand,bool & NotCastExpr,TypeCastState isTypeCast)694*67e74705SXin Li ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
695*67e74705SXin Li bool isAddressOfOperand,
696*67e74705SXin Li bool &NotCastExpr,
697*67e74705SXin Li TypeCastState isTypeCast) {
698*67e74705SXin Li ExprResult Res;
699*67e74705SXin Li tok::TokenKind SavedKind = Tok.getKind();
700*67e74705SXin Li NotCastExpr = false;
701*67e74705SXin Li
702*67e74705SXin Li // This handles all of cast-expression, unary-expression, postfix-expression,
703*67e74705SXin Li // and primary-expression. We handle them together like this for efficiency
704*67e74705SXin Li // and to simplify handling of an expression starting with a '(' token: which
705*67e74705SXin Li // may be one of a parenthesized expression, cast-expression, compound literal
706*67e74705SXin Li // expression, or statement expression.
707*67e74705SXin Li //
708*67e74705SXin Li // If the parsed tokens consist of a primary-expression, the cases below
709*67e74705SXin Li // break out of the switch; at the end we call ParsePostfixExpressionSuffix
710*67e74705SXin Li // to handle the postfix expression suffixes. Cases that cannot be followed
711*67e74705SXin Li // by postfix exprs should return without invoking
712*67e74705SXin Li // ParsePostfixExpressionSuffix.
713*67e74705SXin Li switch (SavedKind) {
714*67e74705SXin Li case tok::l_paren: {
715*67e74705SXin Li // If this expression is limited to being a unary-expression, the parent can
716*67e74705SXin Li // not start a cast expression.
717*67e74705SXin Li ParenParseOption ParenExprType =
718*67e74705SXin Li (isUnaryExpression && !getLangOpts().CPlusPlus) ? CompoundLiteral
719*67e74705SXin Li : CastExpr;
720*67e74705SXin Li ParsedType CastTy;
721*67e74705SXin Li SourceLocation RParenLoc;
722*67e74705SXin Li Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
723*67e74705SXin Li isTypeCast == IsTypeCast, CastTy, RParenLoc);
724*67e74705SXin Li
725*67e74705SXin Li switch (ParenExprType) {
726*67e74705SXin Li case SimpleExpr: break; // Nothing else to do.
727*67e74705SXin Li case CompoundStmt: break; // Nothing else to do.
728*67e74705SXin Li case CompoundLiteral:
729*67e74705SXin Li // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
730*67e74705SXin Li // postfix-expression exist, parse them now.
731*67e74705SXin Li break;
732*67e74705SXin Li case CastExpr:
733*67e74705SXin Li // We have parsed the cast-expression and no postfix-expr pieces are
734*67e74705SXin Li // following.
735*67e74705SXin Li return Res;
736*67e74705SXin Li }
737*67e74705SXin Li
738*67e74705SXin Li break;
739*67e74705SXin Li }
740*67e74705SXin Li
741*67e74705SXin Li // primary-expression
742*67e74705SXin Li case tok::numeric_constant:
743*67e74705SXin Li // constant: integer-constant
744*67e74705SXin Li // constant: floating-constant
745*67e74705SXin Li
746*67e74705SXin Li Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope());
747*67e74705SXin Li ConsumeToken();
748*67e74705SXin Li break;
749*67e74705SXin Li
750*67e74705SXin Li case tok::kw_true:
751*67e74705SXin Li case tok::kw_false:
752*67e74705SXin Li return ParseCXXBoolLiteral();
753*67e74705SXin Li
754*67e74705SXin Li case tok::kw___objc_yes:
755*67e74705SXin Li case tok::kw___objc_no:
756*67e74705SXin Li return ParseObjCBoolLiteral();
757*67e74705SXin Li
758*67e74705SXin Li case tok::kw_nullptr:
759*67e74705SXin Li Diag(Tok, diag::warn_cxx98_compat_nullptr);
760*67e74705SXin Li return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
761*67e74705SXin Li
762*67e74705SXin Li case tok::annot_primary_expr:
763*67e74705SXin Li assert(Res.get() == nullptr && "Stray primary-expression annotation?");
764*67e74705SXin Li Res = getExprAnnotation(Tok);
765*67e74705SXin Li ConsumeToken();
766*67e74705SXin Li break;
767*67e74705SXin Li
768*67e74705SXin Li case tok::kw___super:
769*67e74705SXin Li case tok::kw_decltype:
770*67e74705SXin Li // Annotate the token and tail recurse.
771*67e74705SXin Li if (TryAnnotateTypeOrScopeToken())
772*67e74705SXin Li return ExprError();
773*67e74705SXin Li assert(Tok.isNot(tok::kw_decltype) && Tok.isNot(tok::kw___super));
774*67e74705SXin Li return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
775*67e74705SXin Li
776*67e74705SXin Li case tok::identifier: { // primary-expression: identifier
777*67e74705SXin Li // unqualified-id: identifier
778*67e74705SXin Li // constant: enumeration-constant
779*67e74705SXin Li // Turn a potentially qualified name into a annot_typename or
780*67e74705SXin Li // annot_cxxscope if it would be valid. This handles things like x::y, etc.
781*67e74705SXin Li if (getLangOpts().CPlusPlus) {
782*67e74705SXin Li // Avoid the unnecessary parse-time lookup in the common case
783*67e74705SXin Li // where the syntax forbids a type.
784*67e74705SXin Li const Token &Next = NextToken();
785*67e74705SXin Li
786*67e74705SXin Li // If this identifier was reverted from a token ID, and the next token
787*67e74705SXin Li // is a parenthesis, this is likely to be a use of a type trait. Check
788*67e74705SXin Li // those tokens.
789*67e74705SXin Li if (Next.is(tok::l_paren) &&
790*67e74705SXin Li Tok.is(tok::identifier) &&
791*67e74705SXin Li Tok.getIdentifierInfo()->hasRevertedTokenIDToIdentifier()) {
792*67e74705SXin Li IdentifierInfo *II = Tok.getIdentifierInfo();
793*67e74705SXin Li // Build up the mapping of revertible type traits, for future use.
794*67e74705SXin Li if (RevertibleTypeTraits.empty()) {
795*67e74705SXin Li #define RTT_JOIN(X,Y) X##Y
796*67e74705SXin Li #define REVERTIBLE_TYPE_TRAIT(Name) \
797*67e74705SXin Li RevertibleTypeTraits[PP.getIdentifierInfo(#Name)] \
798*67e74705SXin Li = RTT_JOIN(tok::kw_,Name)
799*67e74705SXin Li
800*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_abstract);
801*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_arithmetic);
802*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_array);
803*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_assignable);
804*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_base_of);
805*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_class);
806*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_complete_type);
807*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_compound);
808*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_const);
809*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_constructible);
810*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_convertible);
811*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_convertible_to);
812*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_destructible);
813*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_empty);
814*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_enum);
815*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_floating_point);
816*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_final);
817*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_function);
818*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_fundamental);
819*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_integral);
820*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_interface_class);
821*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_literal);
822*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_lvalue_expr);
823*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_lvalue_reference);
824*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_member_function_pointer);
825*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_member_object_pointer);
826*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_member_pointer);
827*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_nothrow_assignable);
828*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_nothrow_constructible);
829*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_nothrow_destructible);
830*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_object);
831*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_pod);
832*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_pointer);
833*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_polymorphic);
834*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_reference);
835*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_rvalue_expr);
836*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_rvalue_reference);
837*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_same);
838*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_scalar);
839*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_sealed);
840*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_signed);
841*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_standard_layout);
842*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_trivial);
843*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_trivially_assignable);
844*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_trivially_constructible);
845*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_trivially_copyable);
846*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_union);
847*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_unsigned);
848*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_void);
849*67e74705SXin Li REVERTIBLE_TYPE_TRAIT(__is_volatile);
850*67e74705SXin Li #undef REVERTIBLE_TYPE_TRAIT
851*67e74705SXin Li #undef RTT_JOIN
852*67e74705SXin Li }
853*67e74705SXin Li
854*67e74705SXin Li // If we find that this is in fact the name of a type trait,
855*67e74705SXin Li // update the token kind in place and parse again to treat it as
856*67e74705SXin Li // the appropriate kind of type trait.
857*67e74705SXin Li llvm::SmallDenseMap<IdentifierInfo *, tok::TokenKind>::iterator Known
858*67e74705SXin Li = RevertibleTypeTraits.find(II);
859*67e74705SXin Li if (Known != RevertibleTypeTraits.end()) {
860*67e74705SXin Li Tok.setKind(Known->second);
861*67e74705SXin Li return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
862*67e74705SXin Li NotCastExpr, isTypeCast);
863*67e74705SXin Li }
864*67e74705SXin Li }
865*67e74705SXin Li
866*67e74705SXin Li if ((!ColonIsSacred && Next.is(tok::colon)) ||
867*67e74705SXin Li Next.isOneOf(tok::coloncolon, tok::less, tok::l_paren,
868*67e74705SXin Li tok::l_brace)) {
869*67e74705SXin Li // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
870*67e74705SXin Li if (TryAnnotateTypeOrScopeToken())
871*67e74705SXin Li return ExprError();
872*67e74705SXin Li if (!Tok.is(tok::identifier))
873*67e74705SXin Li return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
874*67e74705SXin Li }
875*67e74705SXin Li }
876*67e74705SXin Li
877*67e74705SXin Li // Consume the identifier so that we can see if it is followed by a '(' or
878*67e74705SXin Li // '.'.
879*67e74705SXin Li IdentifierInfo &II = *Tok.getIdentifierInfo();
880*67e74705SXin Li SourceLocation ILoc = ConsumeToken();
881*67e74705SXin Li
882*67e74705SXin Li // Support 'Class.property' and 'super.property' notation.
883*67e74705SXin Li if (getLangOpts().ObjC1 && Tok.is(tok::period) &&
884*67e74705SXin Li (Actions.getTypeName(II, ILoc, getCurScope()) ||
885*67e74705SXin Li // Allow the base to be 'super' if in an objc-method.
886*67e74705SXin Li (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
887*67e74705SXin Li ConsumeToken();
888*67e74705SXin Li
889*67e74705SXin Li // Allow either an identifier or the keyword 'class' (in C++).
890*67e74705SXin Li if (Tok.isNot(tok::identifier) &&
891*67e74705SXin Li !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
892*67e74705SXin Li Diag(Tok, diag::err_expected_property_name);
893*67e74705SXin Li return ExprError();
894*67e74705SXin Li }
895*67e74705SXin Li IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
896*67e74705SXin Li SourceLocation PropertyLoc = ConsumeToken();
897*67e74705SXin Li
898*67e74705SXin Li Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
899*67e74705SXin Li ILoc, PropertyLoc);
900*67e74705SXin Li break;
901*67e74705SXin Li }
902*67e74705SXin Li
903*67e74705SXin Li // In an Objective-C method, if we have "super" followed by an identifier,
904*67e74705SXin Li // the token sequence is ill-formed. However, if there's a ':' or ']' after
905*67e74705SXin Li // that identifier, this is probably a message send with a missing open
906*67e74705SXin Li // bracket. Treat it as such.
907*67e74705SXin Li if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression &&
908*67e74705SXin Li getCurScope()->isInObjcMethodScope() &&
909*67e74705SXin Li ((Tok.is(tok::identifier) &&
910*67e74705SXin Li (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
911*67e74705SXin Li Tok.is(tok::code_completion))) {
912*67e74705SXin Li Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, nullptr,
913*67e74705SXin Li nullptr);
914*67e74705SXin Li break;
915*67e74705SXin Li }
916*67e74705SXin Li
917*67e74705SXin Li // If we have an Objective-C class name followed by an identifier
918*67e74705SXin Li // and either ':' or ']', this is an Objective-C class message
919*67e74705SXin Li // send that's missing the opening '['. Recovery
920*67e74705SXin Li // appropriately. Also take this path if we're performing code
921*67e74705SXin Li // completion after an Objective-C class name.
922*67e74705SXin Li if (getLangOpts().ObjC1 &&
923*67e74705SXin Li ((Tok.is(tok::identifier) && !InMessageExpression) ||
924*67e74705SXin Li Tok.is(tok::code_completion))) {
925*67e74705SXin Li const Token& Next = NextToken();
926*67e74705SXin Li if (Tok.is(tok::code_completion) ||
927*67e74705SXin Li Next.is(tok::colon) || Next.is(tok::r_square))
928*67e74705SXin Li if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
929*67e74705SXin Li if (Typ.get()->isObjCObjectOrInterfaceType()) {
930*67e74705SXin Li // Fake up a Declarator to use with ActOnTypeName.
931*67e74705SXin Li DeclSpec DS(AttrFactory);
932*67e74705SXin Li DS.SetRangeStart(ILoc);
933*67e74705SXin Li DS.SetRangeEnd(ILoc);
934*67e74705SXin Li const char *PrevSpec = nullptr;
935*67e74705SXin Li unsigned DiagID;
936*67e74705SXin Li DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ,
937*67e74705SXin Li Actions.getASTContext().getPrintingPolicy());
938*67e74705SXin Li
939*67e74705SXin Li Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
940*67e74705SXin Li TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
941*67e74705SXin Li DeclaratorInfo);
942*67e74705SXin Li if (Ty.isInvalid())
943*67e74705SXin Li break;
944*67e74705SXin Li
945*67e74705SXin Li Res = ParseObjCMessageExpressionBody(SourceLocation(),
946*67e74705SXin Li SourceLocation(),
947*67e74705SXin Li Ty.get(), nullptr);
948*67e74705SXin Li break;
949*67e74705SXin Li }
950*67e74705SXin Li }
951*67e74705SXin Li
952*67e74705SXin Li // Make sure to pass down the right value for isAddressOfOperand.
953*67e74705SXin Li if (isAddressOfOperand && isPostfixExpressionSuffixStart())
954*67e74705SXin Li isAddressOfOperand = false;
955*67e74705SXin Li
956*67e74705SXin Li // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
957*67e74705SXin Li // need to know whether or not this identifier is a function designator or
958*67e74705SXin Li // not.
959*67e74705SXin Li UnqualifiedId Name;
960*67e74705SXin Li CXXScopeSpec ScopeSpec;
961*67e74705SXin Li SourceLocation TemplateKWLoc;
962*67e74705SXin Li Token Replacement;
963*67e74705SXin Li auto Validator = llvm::make_unique<CastExpressionIdValidator>(
964*67e74705SXin Li Tok, isTypeCast != NotTypeCast, isTypeCast != IsTypeCast);
965*67e74705SXin Li Validator->IsAddressOfOperand = isAddressOfOperand;
966*67e74705SXin Li if (Tok.isOneOf(tok::periodstar, tok::arrowstar)) {
967*67e74705SXin Li Validator->WantExpressionKeywords = false;
968*67e74705SXin Li Validator->WantRemainingKeywords = false;
969*67e74705SXin Li } else {
970*67e74705SXin Li Validator->WantRemainingKeywords = Tok.isNot(tok::r_paren);
971*67e74705SXin Li }
972*67e74705SXin Li Name.setIdentifier(&II, ILoc);
973*67e74705SXin Li Res = Actions.ActOnIdExpression(
974*67e74705SXin Li getCurScope(), ScopeSpec, TemplateKWLoc, Name, Tok.is(tok::l_paren),
975*67e74705SXin Li isAddressOfOperand, std::move(Validator),
976*67e74705SXin Li /*IsInlineAsmIdentifier=*/false,
977*67e74705SXin Li Tok.is(tok::r_paren) ? nullptr : &Replacement);
978*67e74705SXin Li if (!Res.isInvalid() && !Res.get()) {
979*67e74705SXin Li UnconsumeToken(Replacement);
980*67e74705SXin Li return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
981*67e74705SXin Li NotCastExpr, isTypeCast);
982*67e74705SXin Li }
983*67e74705SXin Li break;
984*67e74705SXin Li }
985*67e74705SXin Li case tok::char_constant: // constant: character-constant
986*67e74705SXin Li case tok::wide_char_constant:
987*67e74705SXin Li case tok::utf8_char_constant:
988*67e74705SXin Li case tok::utf16_char_constant:
989*67e74705SXin Li case tok::utf32_char_constant:
990*67e74705SXin Li Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope());
991*67e74705SXin Li ConsumeToken();
992*67e74705SXin Li break;
993*67e74705SXin Li case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
994*67e74705SXin Li case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
995*67e74705SXin Li case tok::kw___FUNCDNAME__: // primary-expression: __FUNCDNAME__ [MS]
996*67e74705SXin Li case tok::kw___FUNCSIG__: // primary-expression: __FUNCSIG__ [MS]
997*67e74705SXin Li case tok::kw_L__FUNCTION__: // primary-expression: L__FUNCTION__ [MS]
998*67e74705SXin Li case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
999*67e74705SXin Li Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
1000*67e74705SXin Li ConsumeToken();
1001*67e74705SXin Li break;
1002*67e74705SXin Li case tok::string_literal: // primary-expression: string-literal
1003*67e74705SXin Li case tok::wide_string_literal:
1004*67e74705SXin Li case tok::utf8_string_literal:
1005*67e74705SXin Li case tok::utf16_string_literal:
1006*67e74705SXin Li case tok::utf32_string_literal:
1007*67e74705SXin Li Res = ParseStringLiteralExpression(true);
1008*67e74705SXin Li break;
1009*67e74705SXin Li case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
1010*67e74705SXin Li Res = ParseGenericSelectionExpression();
1011*67e74705SXin Li break;
1012*67e74705SXin Li case tok::kw___builtin_va_arg:
1013*67e74705SXin Li case tok::kw___builtin_offsetof:
1014*67e74705SXin Li case tok::kw___builtin_choose_expr:
1015*67e74705SXin Li case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
1016*67e74705SXin Li case tok::kw___builtin_convertvector:
1017*67e74705SXin Li return ParseBuiltinPrimaryExpression();
1018*67e74705SXin Li case tok::kw___null:
1019*67e74705SXin Li return Actions.ActOnGNUNullExpr(ConsumeToken());
1020*67e74705SXin Li
1021*67e74705SXin Li case tok::plusplus: // unary-expression: '++' unary-expression [C99]
1022*67e74705SXin Li case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
1023*67e74705SXin Li // C++ [expr.unary] has:
1024*67e74705SXin Li // unary-expression:
1025*67e74705SXin Li // ++ cast-expression
1026*67e74705SXin Li // -- cast-expression
1027*67e74705SXin Li Token SavedTok = Tok;
1028*67e74705SXin Li ConsumeToken();
1029*67e74705SXin Li // One special case is implicitly handled here: if the preceding tokens are
1030*67e74705SXin Li // an ambiguous cast expression, such as "(T())++", then we recurse to
1031*67e74705SXin Li // determine whether the '++' is prefix or postfix.
1032*67e74705SXin Li Res = ParseCastExpression(!getLangOpts().CPlusPlus,
1033*67e74705SXin Li /*isAddressOfOperand*/false, NotCastExpr,
1034*67e74705SXin Li NotTypeCast);
1035*67e74705SXin Li if (NotCastExpr) {
1036*67e74705SXin Li // If we return with NotCastExpr = true, we must not consume any tokens,
1037*67e74705SXin Li // so put the token back where we found it.
1038*67e74705SXin Li assert(Res.isInvalid());
1039*67e74705SXin Li UnconsumeToken(SavedTok);
1040*67e74705SXin Li return ExprError();
1041*67e74705SXin Li }
1042*67e74705SXin Li if (!Res.isInvalid())
1043*67e74705SXin Li Res = Actions.ActOnUnaryOp(getCurScope(), SavedTok.getLocation(),
1044*67e74705SXin Li SavedKind, Res.get());
1045*67e74705SXin Li return Res;
1046*67e74705SXin Li }
1047*67e74705SXin Li case tok::amp: { // unary-expression: '&' cast-expression
1048*67e74705SXin Li // Special treatment because of member pointers
1049*67e74705SXin Li SourceLocation SavedLoc = ConsumeToken();
1050*67e74705SXin Li Res = ParseCastExpression(false, true);
1051*67e74705SXin Li if (!Res.isInvalid())
1052*67e74705SXin Li Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
1053*67e74705SXin Li return Res;
1054*67e74705SXin Li }
1055*67e74705SXin Li
1056*67e74705SXin Li case tok::star: // unary-expression: '*' cast-expression
1057*67e74705SXin Li case tok::plus: // unary-expression: '+' cast-expression
1058*67e74705SXin Li case tok::minus: // unary-expression: '-' cast-expression
1059*67e74705SXin Li case tok::tilde: // unary-expression: '~' cast-expression
1060*67e74705SXin Li case tok::exclaim: // unary-expression: '!' cast-expression
1061*67e74705SXin Li case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
1062*67e74705SXin Li case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
1063*67e74705SXin Li SourceLocation SavedLoc = ConsumeToken();
1064*67e74705SXin Li Res = ParseCastExpression(false);
1065*67e74705SXin Li if (!Res.isInvalid())
1066*67e74705SXin Li Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
1067*67e74705SXin Li return Res;
1068*67e74705SXin Li }
1069*67e74705SXin Li
1070*67e74705SXin Li case tok::kw_co_await: { // unary-expression: 'co_await' cast-expression
1071*67e74705SXin Li SourceLocation CoawaitLoc = ConsumeToken();
1072*67e74705SXin Li Res = ParseCastExpression(false);
1073*67e74705SXin Li if (!Res.isInvalid())
1074*67e74705SXin Li Res = Actions.ActOnCoawaitExpr(getCurScope(), CoawaitLoc, Res.get());
1075*67e74705SXin Li return Res;
1076*67e74705SXin Li }
1077*67e74705SXin Li
1078*67e74705SXin Li case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
1079*67e74705SXin Li // __extension__ silences extension warnings in the subexpression.
1080*67e74705SXin Li ExtensionRAIIObject O(Diags); // Use RAII to do this.
1081*67e74705SXin Li SourceLocation SavedLoc = ConsumeToken();
1082*67e74705SXin Li Res = ParseCastExpression(false);
1083*67e74705SXin Li if (!Res.isInvalid())
1084*67e74705SXin Li Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
1085*67e74705SXin Li return Res;
1086*67e74705SXin Li }
1087*67e74705SXin Li case tok::kw__Alignof: // unary-expression: '_Alignof' '(' type-name ')'
1088*67e74705SXin Li if (!getLangOpts().C11)
1089*67e74705SXin Li Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
1090*67e74705SXin Li // fallthrough
1091*67e74705SXin Li case tok::kw_alignof: // unary-expression: 'alignof' '(' type-id ')'
1092*67e74705SXin Li case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
1093*67e74705SXin Li // unary-expression: '__alignof' '(' type-name ')'
1094*67e74705SXin Li case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
1095*67e74705SXin Li // unary-expression: 'sizeof' '(' type-name ')'
1096*67e74705SXin Li case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
1097*67e74705SXin Li // unary-expression: '__builtin_omp_required_simd_align' '(' type-name ')'
1098*67e74705SXin Li case tok::kw___builtin_omp_required_simd_align:
1099*67e74705SXin Li return ParseUnaryExprOrTypeTraitExpression();
1100*67e74705SXin Li case tok::ampamp: { // unary-expression: '&&' identifier
1101*67e74705SXin Li SourceLocation AmpAmpLoc = ConsumeToken();
1102*67e74705SXin Li if (Tok.isNot(tok::identifier))
1103*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
1104*67e74705SXin Li
1105*67e74705SXin Li if (getCurScope()->getFnParent() == nullptr)
1106*67e74705SXin Li return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
1107*67e74705SXin Li
1108*67e74705SXin Li Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
1109*67e74705SXin Li LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1110*67e74705SXin Li Tok.getLocation());
1111*67e74705SXin Li Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
1112*67e74705SXin Li ConsumeToken();
1113*67e74705SXin Li return Res;
1114*67e74705SXin Li }
1115*67e74705SXin Li case tok::kw_const_cast:
1116*67e74705SXin Li case tok::kw_dynamic_cast:
1117*67e74705SXin Li case tok::kw_reinterpret_cast:
1118*67e74705SXin Li case tok::kw_static_cast:
1119*67e74705SXin Li Res = ParseCXXCasts();
1120*67e74705SXin Li break;
1121*67e74705SXin Li case tok::kw_typeid:
1122*67e74705SXin Li Res = ParseCXXTypeid();
1123*67e74705SXin Li break;
1124*67e74705SXin Li case tok::kw___uuidof:
1125*67e74705SXin Li Res = ParseCXXUuidof();
1126*67e74705SXin Li break;
1127*67e74705SXin Li case tok::kw_this:
1128*67e74705SXin Li Res = ParseCXXThis();
1129*67e74705SXin Li break;
1130*67e74705SXin Li
1131*67e74705SXin Li case tok::annot_typename:
1132*67e74705SXin Li if (isStartOfObjCClassMessageMissingOpenBracket()) {
1133*67e74705SXin Li ParsedType Type = getTypeAnnotation(Tok);
1134*67e74705SXin Li
1135*67e74705SXin Li // Fake up a Declarator to use with ActOnTypeName.
1136*67e74705SXin Li DeclSpec DS(AttrFactory);
1137*67e74705SXin Li DS.SetRangeStart(Tok.getLocation());
1138*67e74705SXin Li DS.SetRangeEnd(Tok.getLastLoc());
1139*67e74705SXin Li
1140*67e74705SXin Li const char *PrevSpec = nullptr;
1141*67e74705SXin Li unsigned DiagID;
1142*67e74705SXin Li DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
1143*67e74705SXin Li PrevSpec, DiagID, Type,
1144*67e74705SXin Li Actions.getASTContext().getPrintingPolicy());
1145*67e74705SXin Li
1146*67e74705SXin Li Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1147*67e74705SXin Li TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1148*67e74705SXin Li if (Ty.isInvalid())
1149*67e74705SXin Li break;
1150*67e74705SXin Li
1151*67e74705SXin Li ConsumeToken();
1152*67e74705SXin Li Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1153*67e74705SXin Li Ty.get(), nullptr);
1154*67e74705SXin Li break;
1155*67e74705SXin Li }
1156*67e74705SXin Li // Fall through
1157*67e74705SXin Li
1158*67e74705SXin Li case tok::annot_decltype:
1159*67e74705SXin Li case tok::kw_char:
1160*67e74705SXin Li case tok::kw_wchar_t:
1161*67e74705SXin Li case tok::kw_char16_t:
1162*67e74705SXin Li case tok::kw_char32_t:
1163*67e74705SXin Li case tok::kw_bool:
1164*67e74705SXin Li case tok::kw_short:
1165*67e74705SXin Li case tok::kw_int:
1166*67e74705SXin Li case tok::kw_long:
1167*67e74705SXin Li case tok::kw___int64:
1168*67e74705SXin Li case tok::kw___int128:
1169*67e74705SXin Li case tok::kw_signed:
1170*67e74705SXin Li case tok::kw_unsigned:
1171*67e74705SXin Li case tok::kw_half:
1172*67e74705SXin Li case tok::kw_float:
1173*67e74705SXin Li case tok::kw_double:
1174*67e74705SXin Li case tok::kw___float128:
1175*67e74705SXin Li case tok::kw_void:
1176*67e74705SXin Li case tok::kw_typename:
1177*67e74705SXin Li case tok::kw_typeof:
1178*67e74705SXin Li case tok::kw___vector:
1179*67e74705SXin Li #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1180*67e74705SXin Li #include "clang/Basic/OpenCLImageTypes.def"
1181*67e74705SXin Li {
1182*67e74705SXin Li if (!getLangOpts().CPlusPlus) {
1183*67e74705SXin Li Diag(Tok, diag::err_expected_expression);
1184*67e74705SXin Li return ExprError();
1185*67e74705SXin Li }
1186*67e74705SXin Li
1187*67e74705SXin Li if (SavedKind == tok::kw_typename) {
1188*67e74705SXin Li // postfix-expression: typename-specifier '(' expression-list[opt] ')'
1189*67e74705SXin Li // typename-specifier braced-init-list
1190*67e74705SXin Li if (TryAnnotateTypeOrScopeToken())
1191*67e74705SXin Li return ExprError();
1192*67e74705SXin Li
1193*67e74705SXin Li if (!Actions.isSimpleTypeSpecifier(Tok.getKind()))
1194*67e74705SXin Li // We are trying to parse a simple-type-specifier but might not get such
1195*67e74705SXin Li // a token after error recovery.
1196*67e74705SXin Li return ExprError();
1197*67e74705SXin Li }
1198*67e74705SXin Li
1199*67e74705SXin Li // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
1200*67e74705SXin Li // simple-type-specifier braced-init-list
1201*67e74705SXin Li //
1202*67e74705SXin Li DeclSpec DS(AttrFactory);
1203*67e74705SXin Li
1204*67e74705SXin Li ParseCXXSimpleTypeSpecifier(DS);
1205*67e74705SXin Li if (Tok.isNot(tok::l_paren) &&
1206*67e74705SXin Li (!getLangOpts().CPlusPlus11 || Tok.isNot(tok::l_brace)))
1207*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1208*67e74705SXin Li << DS.getSourceRange());
1209*67e74705SXin Li
1210*67e74705SXin Li if (Tok.is(tok::l_brace))
1211*67e74705SXin Li Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1212*67e74705SXin Li
1213*67e74705SXin Li Res = ParseCXXTypeConstructExpression(DS);
1214*67e74705SXin Li break;
1215*67e74705SXin Li }
1216*67e74705SXin Li
1217*67e74705SXin Li case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
1218*67e74705SXin Li // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1219*67e74705SXin Li // (We can end up in this situation after tentative parsing.)
1220*67e74705SXin Li if (TryAnnotateTypeOrScopeToken())
1221*67e74705SXin Li return ExprError();
1222*67e74705SXin Li if (!Tok.is(tok::annot_cxxscope))
1223*67e74705SXin Li return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
1224*67e74705SXin Li NotCastExpr, isTypeCast);
1225*67e74705SXin Li
1226*67e74705SXin Li Token Next = NextToken();
1227*67e74705SXin Li if (Next.is(tok::annot_template_id)) {
1228*67e74705SXin Li TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
1229*67e74705SXin Li if (TemplateId->Kind == TNK_Type_template) {
1230*67e74705SXin Li // We have a qualified template-id that we know refers to a
1231*67e74705SXin Li // type, translate it into a type and continue parsing as a
1232*67e74705SXin Li // cast expression.
1233*67e74705SXin Li CXXScopeSpec SS;
1234*67e74705SXin Li ParseOptionalCXXScopeSpecifier(SS, nullptr,
1235*67e74705SXin Li /*EnteringContext=*/false);
1236*67e74705SXin Li AnnotateTemplateIdTokenAsType();
1237*67e74705SXin Li return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
1238*67e74705SXin Li NotCastExpr, isTypeCast);
1239*67e74705SXin Li }
1240*67e74705SXin Li }
1241*67e74705SXin Li
1242*67e74705SXin Li // Parse as an id-expression.
1243*67e74705SXin Li Res = ParseCXXIdExpression(isAddressOfOperand);
1244*67e74705SXin Li break;
1245*67e74705SXin Li }
1246*67e74705SXin Li
1247*67e74705SXin Li case tok::annot_template_id: { // [C++] template-id
1248*67e74705SXin Li TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1249*67e74705SXin Li if (TemplateId->Kind == TNK_Type_template) {
1250*67e74705SXin Li // We have a template-id that we know refers to a type,
1251*67e74705SXin Li // translate it into a type and continue parsing as a cast
1252*67e74705SXin Li // expression.
1253*67e74705SXin Li AnnotateTemplateIdTokenAsType();
1254*67e74705SXin Li return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
1255*67e74705SXin Li NotCastExpr, isTypeCast);
1256*67e74705SXin Li }
1257*67e74705SXin Li
1258*67e74705SXin Li // Fall through to treat the template-id as an id-expression.
1259*67e74705SXin Li }
1260*67e74705SXin Li
1261*67e74705SXin Li case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
1262*67e74705SXin Li Res = ParseCXXIdExpression(isAddressOfOperand);
1263*67e74705SXin Li break;
1264*67e74705SXin Li
1265*67e74705SXin Li case tok::coloncolon: {
1266*67e74705SXin Li // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1267*67e74705SXin Li // annotates the token, tail recurse.
1268*67e74705SXin Li if (TryAnnotateTypeOrScopeToken())
1269*67e74705SXin Li return ExprError();
1270*67e74705SXin Li if (!Tok.is(tok::coloncolon))
1271*67e74705SXin Li return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1272*67e74705SXin Li
1273*67e74705SXin Li // ::new -> [C++] new-expression
1274*67e74705SXin Li // ::delete -> [C++] delete-expression
1275*67e74705SXin Li SourceLocation CCLoc = ConsumeToken();
1276*67e74705SXin Li if (Tok.is(tok::kw_new))
1277*67e74705SXin Li return ParseCXXNewExpression(true, CCLoc);
1278*67e74705SXin Li if (Tok.is(tok::kw_delete))
1279*67e74705SXin Li return ParseCXXDeleteExpression(true, CCLoc);
1280*67e74705SXin Li
1281*67e74705SXin Li // This is not a type name or scope specifier, it is an invalid expression.
1282*67e74705SXin Li Diag(CCLoc, diag::err_expected_expression);
1283*67e74705SXin Li return ExprError();
1284*67e74705SXin Li }
1285*67e74705SXin Li
1286*67e74705SXin Li case tok::kw_new: // [C++] new-expression
1287*67e74705SXin Li return ParseCXXNewExpression(false, Tok.getLocation());
1288*67e74705SXin Li
1289*67e74705SXin Li case tok::kw_delete: // [C++] delete-expression
1290*67e74705SXin Li return ParseCXXDeleteExpression(false, Tok.getLocation());
1291*67e74705SXin Li
1292*67e74705SXin Li case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
1293*67e74705SXin Li Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
1294*67e74705SXin Li SourceLocation KeyLoc = ConsumeToken();
1295*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
1296*67e74705SXin Li
1297*67e74705SXin Li if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
1298*67e74705SXin Li return ExprError();
1299*67e74705SXin Li // C++11 [expr.unary.noexcept]p1:
1300*67e74705SXin Li // The noexcept operator determines whether the evaluation of its operand,
1301*67e74705SXin Li // which is an unevaluated operand, can throw an exception.
1302*67e74705SXin Li EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1303*67e74705SXin Li ExprResult Result = ParseExpression();
1304*67e74705SXin Li
1305*67e74705SXin Li T.consumeClose();
1306*67e74705SXin Li
1307*67e74705SXin Li if (!Result.isInvalid())
1308*67e74705SXin Li Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1309*67e74705SXin Li Result.get(), T.getCloseLocation());
1310*67e74705SXin Li return Result;
1311*67e74705SXin Li }
1312*67e74705SXin Li
1313*67e74705SXin Li #define TYPE_TRAIT(N,Spelling,K) \
1314*67e74705SXin Li case tok::kw_##Spelling:
1315*67e74705SXin Li #include "clang/Basic/TokenKinds.def"
1316*67e74705SXin Li return ParseTypeTrait();
1317*67e74705SXin Li
1318*67e74705SXin Li case tok::kw___array_rank:
1319*67e74705SXin Li case tok::kw___array_extent:
1320*67e74705SXin Li return ParseArrayTypeTrait();
1321*67e74705SXin Li
1322*67e74705SXin Li case tok::kw___is_lvalue_expr:
1323*67e74705SXin Li case tok::kw___is_rvalue_expr:
1324*67e74705SXin Li return ParseExpressionTrait();
1325*67e74705SXin Li
1326*67e74705SXin Li case tok::at: {
1327*67e74705SXin Li SourceLocation AtLoc = ConsumeToken();
1328*67e74705SXin Li return ParseObjCAtExpression(AtLoc);
1329*67e74705SXin Li }
1330*67e74705SXin Li case tok::caret:
1331*67e74705SXin Li Res = ParseBlockLiteralExpression();
1332*67e74705SXin Li break;
1333*67e74705SXin Li case tok::code_completion: {
1334*67e74705SXin Li Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
1335*67e74705SXin Li cutOffParsing();
1336*67e74705SXin Li return ExprError();
1337*67e74705SXin Li }
1338*67e74705SXin Li case tok::l_square:
1339*67e74705SXin Li if (getLangOpts().CPlusPlus11) {
1340*67e74705SXin Li if (getLangOpts().ObjC1) {
1341*67e74705SXin Li // C++11 lambda expressions and Objective-C message sends both start with a
1342*67e74705SXin Li // square bracket. There are three possibilities here:
1343*67e74705SXin Li // we have a valid lambda expression, we have an invalid lambda
1344*67e74705SXin Li // expression, or we have something that doesn't appear to be a lambda.
1345*67e74705SXin Li // If we're in the last case, we fall back to ParseObjCMessageExpression.
1346*67e74705SXin Li Res = TryParseLambdaExpression();
1347*67e74705SXin Li if (!Res.isInvalid() && !Res.get())
1348*67e74705SXin Li Res = ParseObjCMessageExpression();
1349*67e74705SXin Li break;
1350*67e74705SXin Li }
1351*67e74705SXin Li Res = ParseLambdaExpression();
1352*67e74705SXin Li break;
1353*67e74705SXin Li }
1354*67e74705SXin Li if (getLangOpts().ObjC1) {
1355*67e74705SXin Li Res = ParseObjCMessageExpression();
1356*67e74705SXin Li break;
1357*67e74705SXin Li }
1358*67e74705SXin Li // FALL THROUGH.
1359*67e74705SXin Li default:
1360*67e74705SXin Li NotCastExpr = true;
1361*67e74705SXin Li return ExprError();
1362*67e74705SXin Li }
1363*67e74705SXin Li
1364*67e74705SXin Li // Check to see whether Res is a function designator only. If it is and we
1365*67e74705SXin Li // are compiling for OpenCL, we need to return an error as this implies
1366*67e74705SXin Li // that the address of the function is being taken, which is illegal in CL.
1367*67e74705SXin Li
1368*67e74705SXin Li // These can be followed by postfix-expr pieces.
1369*67e74705SXin Li Res = ParsePostfixExpressionSuffix(Res);
1370*67e74705SXin Li if (getLangOpts().OpenCL)
1371*67e74705SXin Li if (Expr *PostfixExpr = Res.get()) {
1372*67e74705SXin Li QualType Ty = PostfixExpr->getType();
1373*67e74705SXin Li if (!Ty.isNull() && Ty->isFunctionType()) {
1374*67e74705SXin Li Diag(PostfixExpr->getExprLoc(),
1375*67e74705SXin Li diag::err_opencl_taking_function_address_parser);
1376*67e74705SXin Li return ExprError();
1377*67e74705SXin Li }
1378*67e74705SXin Li }
1379*67e74705SXin Li
1380*67e74705SXin Li return Res;
1381*67e74705SXin Li }
1382*67e74705SXin Li
1383*67e74705SXin Li /// \brief Once the leading part of a postfix-expression is parsed, this
1384*67e74705SXin Li /// method parses any suffixes that apply.
1385*67e74705SXin Li ///
1386*67e74705SXin Li /// \verbatim
1387*67e74705SXin Li /// postfix-expression: [C99 6.5.2]
1388*67e74705SXin Li /// primary-expression
1389*67e74705SXin Li /// postfix-expression '[' expression ']'
1390*67e74705SXin Li /// postfix-expression '[' braced-init-list ']'
1391*67e74705SXin Li /// postfix-expression '(' argument-expression-list[opt] ')'
1392*67e74705SXin Li /// postfix-expression '.' identifier
1393*67e74705SXin Li /// postfix-expression '->' identifier
1394*67e74705SXin Li /// postfix-expression '++'
1395*67e74705SXin Li /// postfix-expression '--'
1396*67e74705SXin Li /// '(' type-name ')' '{' initializer-list '}'
1397*67e74705SXin Li /// '(' type-name ')' '{' initializer-list ',' '}'
1398*67e74705SXin Li ///
1399*67e74705SXin Li /// argument-expression-list: [C99 6.5.2]
1400*67e74705SXin Li /// argument-expression ...[opt]
1401*67e74705SXin Li /// argument-expression-list ',' assignment-expression ...[opt]
1402*67e74705SXin Li /// \endverbatim
1403*67e74705SXin Li ExprResult
ParsePostfixExpressionSuffix(ExprResult LHS)1404*67e74705SXin Li Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
1405*67e74705SXin Li // Now that the primary-expression piece of the postfix-expression has been
1406*67e74705SXin Li // parsed, see if there are any postfix-expression pieces here.
1407*67e74705SXin Li SourceLocation Loc;
1408*67e74705SXin Li while (1) {
1409*67e74705SXin Li switch (Tok.getKind()) {
1410*67e74705SXin Li case tok::code_completion:
1411*67e74705SXin Li if (InMessageExpression)
1412*67e74705SXin Li return LHS;
1413*67e74705SXin Li
1414*67e74705SXin Li Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
1415*67e74705SXin Li cutOffParsing();
1416*67e74705SXin Li return ExprError();
1417*67e74705SXin Li
1418*67e74705SXin Li case tok::identifier:
1419*67e74705SXin Li // If we see identifier: after an expression, and we're not already in a
1420*67e74705SXin Li // message send, then this is probably a message send with a missing
1421*67e74705SXin Li // opening bracket '['.
1422*67e74705SXin Li if (getLangOpts().ObjC1 && !InMessageExpression &&
1423*67e74705SXin Li (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
1424*67e74705SXin Li LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1425*67e74705SXin Li nullptr, LHS.get());
1426*67e74705SXin Li break;
1427*67e74705SXin Li }
1428*67e74705SXin Li
1429*67e74705SXin Li // Fall through; this isn't a message send.
1430*67e74705SXin Li
1431*67e74705SXin Li default: // Not a postfix-expression suffix.
1432*67e74705SXin Li return LHS;
1433*67e74705SXin Li case tok::l_square: { // postfix-expression: p-e '[' expression ']'
1434*67e74705SXin Li // If we have a array postfix expression that starts on a new line and
1435*67e74705SXin Li // Objective-C is enabled, it is highly likely that the user forgot a
1436*67e74705SXin Li // semicolon after the base expression and that the array postfix-expr is
1437*67e74705SXin Li // actually another message send. In this case, do some look-ahead to see
1438*67e74705SXin Li // if the contents of the square brackets are obviously not a valid
1439*67e74705SXin Li // expression and recover by pretending there is no suffix.
1440*67e74705SXin Li if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
1441*67e74705SXin Li isSimpleObjCMessageExpression())
1442*67e74705SXin Li return LHS;
1443*67e74705SXin Li
1444*67e74705SXin Li // Reject array indices starting with a lambda-expression. '[[' is
1445*67e74705SXin Li // reserved for attributes.
1446*67e74705SXin Li if (CheckProhibitedCXX11Attribute()) {
1447*67e74705SXin Li (void)Actions.CorrectDelayedTyposInExpr(LHS);
1448*67e74705SXin Li return ExprError();
1449*67e74705SXin Li }
1450*67e74705SXin Li
1451*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_square);
1452*67e74705SXin Li T.consumeOpen();
1453*67e74705SXin Li Loc = T.getOpenLocation();
1454*67e74705SXin Li ExprResult Idx, Length;
1455*67e74705SXin Li SourceLocation ColonLoc;
1456*67e74705SXin Li if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
1457*67e74705SXin Li Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1458*67e74705SXin Li Idx = ParseBraceInitializer();
1459*67e74705SXin Li } else if (getLangOpts().OpenMP) {
1460*67e74705SXin Li ColonProtectionRAIIObject RAII(*this);
1461*67e74705SXin Li // Parse [: or [ expr or [ expr :
1462*67e74705SXin Li if (!Tok.is(tok::colon)) {
1463*67e74705SXin Li // [ expr
1464*67e74705SXin Li Idx = ParseExpression();
1465*67e74705SXin Li }
1466*67e74705SXin Li if (Tok.is(tok::colon)) {
1467*67e74705SXin Li // Consume ':'
1468*67e74705SXin Li ColonLoc = ConsumeToken();
1469*67e74705SXin Li if (Tok.isNot(tok::r_square))
1470*67e74705SXin Li Length = ParseExpression();
1471*67e74705SXin Li }
1472*67e74705SXin Li } else
1473*67e74705SXin Li Idx = ParseExpression();
1474*67e74705SXin Li
1475*67e74705SXin Li SourceLocation RLoc = Tok.getLocation();
1476*67e74705SXin Li
1477*67e74705SXin Li ExprResult OrigLHS = LHS;
1478*67e74705SXin Li if (!LHS.isInvalid() && !Idx.isInvalid() && !Length.isInvalid() &&
1479*67e74705SXin Li Tok.is(tok::r_square)) {
1480*67e74705SXin Li if (ColonLoc.isValid()) {
1481*67e74705SXin Li LHS = Actions.ActOnOMPArraySectionExpr(LHS.get(), Loc, Idx.get(),
1482*67e74705SXin Li ColonLoc, Length.get(), RLoc);
1483*67e74705SXin Li } else {
1484*67e74705SXin Li LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.get(), Loc,
1485*67e74705SXin Li Idx.get(), RLoc);
1486*67e74705SXin Li }
1487*67e74705SXin Li } else {
1488*67e74705SXin Li LHS = ExprError();
1489*67e74705SXin Li }
1490*67e74705SXin Li if (LHS.isInvalid()) {
1491*67e74705SXin Li (void)Actions.CorrectDelayedTyposInExpr(OrigLHS);
1492*67e74705SXin Li (void)Actions.CorrectDelayedTyposInExpr(Idx);
1493*67e74705SXin Li (void)Actions.CorrectDelayedTyposInExpr(Length);
1494*67e74705SXin Li LHS = ExprError();
1495*67e74705SXin Li Idx = ExprError();
1496*67e74705SXin Li }
1497*67e74705SXin Li
1498*67e74705SXin Li // Match the ']'.
1499*67e74705SXin Li T.consumeClose();
1500*67e74705SXin Li break;
1501*67e74705SXin Li }
1502*67e74705SXin Li
1503*67e74705SXin Li case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1504*67e74705SXin Li case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1505*67e74705SXin Li // '(' argument-expression-list[opt] ')'
1506*67e74705SXin Li tok::TokenKind OpKind = Tok.getKind();
1507*67e74705SXin Li InMessageExpressionRAIIObject InMessage(*this, false);
1508*67e74705SXin Li
1509*67e74705SXin Li Expr *ExecConfig = nullptr;
1510*67e74705SXin Li
1511*67e74705SXin Li BalancedDelimiterTracker PT(*this, tok::l_paren);
1512*67e74705SXin Li
1513*67e74705SXin Li if (OpKind == tok::lesslessless) {
1514*67e74705SXin Li ExprVector ExecConfigExprs;
1515*67e74705SXin Li CommaLocsTy ExecConfigCommaLocs;
1516*67e74705SXin Li SourceLocation OpenLoc = ConsumeToken();
1517*67e74705SXin Li
1518*67e74705SXin Li if (ParseSimpleExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1519*67e74705SXin Li (void)Actions.CorrectDelayedTyposInExpr(LHS);
1520*67e74705SXin Li LHS = ExprError();
1521*67e74705SXin Li }
1522*67e74705SXin Li
1523*67e74705SXin Li SourceLocation CloseLoc;
1524*67e74705SXin Li if (TryConsumeToken(tok::greatergreatergreater, CloseLoc)) {
1525*67e74705SXin Li } else if (LHS.isInvalid()) {
1526*67e74705SXin Li SkipUntil(tok::greatergreatergreater, StopAtSemi);
1527*67e74705SXin Li } else {
1528*67e74705SXin Li // There was an error closing the brackets
1529*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::greatergreatergreater;
1530*67e74705SXin Li Diag(OpenLoc, diag::note_matching) << tok::lesslessless;
1531*67e74705SXin Li SkipUntil(tok::greatergreatergreater, StopAtSemi);
1532*67e74705SXin Li LHS = ExprError();
1533*67e74705SXin Li }
1534*67e74705SXin Li
1535*67e74705SXin Li if (!LHS.isInvalid()) {
1536*67e74705SXin Li if (ExpectAndConsume(tok::l_paren))
1537*67e74705SXin Li LHS = ExprError();
1538*67e74705SXin Li else
1539*67e74705SXin Li Loc = PrevTokLocation;
1540*67e74705SXin Li }
1541*67e74705SXin Li
1542*67e74705SXin Li if (!LHS.isInvalid()) {
1543*67e74705SXin Li ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
1544*67e74705SXin Li OpenLoc,
1545*67e74705SXin Li ExecConfigExprs,
1546*67e74705SXin Li CloseLoc);
1547*67e74705SXin Li if (ECResult.isInvalid())
1548*67e74705SXin Li LHS = ExprError();
1549*67e74705SXin Li else
1550*67e74705SXin Li ExecConfig = ECResult.get();
1551*67e74705SXin Li }
1552*67e74705SXin Li } else {
1553*67e74705SXin Li PT.consumeOpen();
1554*67e74705SXin Li Loc = PT.getOpenLocation();
1555*67e74705SXin Li }
1556*67e74705SXin Li
1557*67e74705SXin Li ExprVector ArgExprs;
1558*67e74705SXin Li CommaLocsTy CommaLocs;
1559*67e74705SXin Li
1560*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1561*67e74705SXin Li Actions.CodeCompleteCall(getCurScope(), LHS.get(), None);
1562*67e74705SXin Li cutOffParsing();
1563*67e74705SXin Li return ExprError();
1564*67e74705SXin Li }
1565*67e74705SXin Li
1566*67e74705SXin Li if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1567*67e74705SXin Li if (Tok.isNot(tok::r_paren)) {
1568*67e74705SXin Li if (ParseExpressionList(ArgExprs, CommaLocs, [&] {
1569*67e74705SXin Li Actions.CodeCompleteCall(getCurScope(), LHS.get(), ArgExprs);
1570*67e74705SXin Li })) {
1571*67e74705SXin Li (void)Actions.CorrectDelayedTyposInExpr(LHS);
1572*67e74705SXin Li LHS = ExprError();
1573*67e74705SXin Li } else if (LHS.isInvalid()) {
1574*67e74705SXin Li for (auto &E : ArgExprs)
1575*67e74705SXin Li Actions.CorrectDelayedTyposInExpr(E);
1576*67e74705SXin Li }
1577*67e74705SXin Li }
1578*67e74705SXin Li }
1579*67e74705SXin Li
1580*67e74705SXin Li // Match the ')'.
1581*67e74705SXin Li if (LHS.isInvalid()) {
1582*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
1583*67e74705SXin Li } else if (Tok.isNot(tok::r_paren)) {
1584*67e74705SXin Li bool HadDelayedTypo = false;
1585*67e74705SXin Li if (Actions.CorrectDelayedTyposInExpr(LHS).get() != LHS.get())
1586*67e74705SXin Li HadDelayedTypo = true;
1587*67e74705SXin Li for (auto &E : ArgExprs)
1588*67e74705SXin Li if (Actions.CorrectDelayedTyposInExpr(E).get() != E)
1589*67e74705SXin Li HadDelayedTypo = true;
1590*67e74705SXin Li // If there were delayed typos in the LHS or ArgExprs, call SkipUntil
1591*67e74705SXin Li // instead of PT.consumeClose() to avoid emitting extra diagnostics for
1592*67e74705SXin Li // the unmatched l_paren.
1593*67e74705SXin Li if (HadDelayedTypo)
1594*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
1595*67e74705SXin Li else
1596*67e74705SXin Li PT.consumeClose();
1597*67e74705SXin Li LHS = ExprError();
1598*67e74705SXin Li } else {
1599*67e74705SXin Li assert((ArgExprs.size() == 0 ||
1600*67e74705SXin Li ArgExprs.size()-1 == CommaLocs.size())&&
1601*67e74705SXin Li "Unexpected number of commas!");
1602*67e74705SXin Li LHS = Actions.ActOnCallExpr(getCurScope(), LHS.get(), Loc,
1603*67e74705SXin Li ArgExprs, Tok.getLocation(),
1604*67e74705SXin Li ExecConfig);
1605*67e74705SXin Li PT.consumeClose();
1606*67e74705SXin Li }
1607*67e74705SXin Li
1608*67e74705SXin Li break;
1609*67e74705SXin Li }
1610*67e74705SXin Li case tok::arrow:
1611*67e74705SXin Li case tok::period: {
1612*67e74705SXin Li // postfix-expression: p-e '->' template[opt] id-expression
1613*67e74705SXin Li // postfix-expression: p-e '.' template[opt] id-expression
1614*67e74705SXin Li tok::TokenKind OpKind = Tok.getKind();
1615*67e74705SXin Li SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
1616*67e74705SXin Li
1617*67e74705SXin Li CXXScopeSpec SS;
1618*67e74705SXin Li ParsedType ObjectType;
1619*67e74705SXin Li bool MayBePseudoDestructor = false;
1620*67e74705SXin Li if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
1621*67e74705SXin Li Expr *Base = LHS.get();
1622*67e74705SXin Li const Type* BaseType = Base->getType().getTypePtrOrNull();
1623*67e74705SXin Li if (BaseType && Tok.is(tok::l_paren) &&
1624*67e74705SXin Li (BaseType->isFunctionType() ||
1625*67e74705SXin Li BaseType->isSpecificPlaceholderType(BuiltinType::BoundMember))) {
1626*67e74705SXin Li Diag(OpLoc, diag::err_function_is_not_record)
1627*67e74705SXin Li << OpKind << Base->getSourceRange()
1628*67e74705SXin Li << FixItHint::CreateRemoval(OpLoc);
1629*67e74705SXin Li return ParsePostfixExpressionSuffix(Base);
1630*67e74705SXin Li }
1631*67e74705SXin Li
1632*67e74705SXin Li LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), Base,
1633*67e74705SXin Li OpLoc, OpKind, ObjectType,
1634*67e74705SXin Li MayBePseudoDestructor);
1635*67e74705SXin Li if (LHS.isInvalid())
1636*67e74705SXin Li break;
1637*67e74705SXin Li
1638*67e74705SXin Li ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1639*67e74705SXin Li /*EnteringContext=*/false,
1640*67e74705SXin Li &MayBePseudoDestructor);
1641*67e74705SXin Li if (SS.isNotEmpty())
1642*67e74705SXin Li ObjectType = nullptr;
1643*67e74705SXin Li }
1644*67e74705SXin Li
1645*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1646*67e74705SXin Li // Code completion for a member access expression.
1647*67e74705SXin Li Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
1648*67e74705SXin Li OpLoc, OpKind == tok::arrow);
1649*67e74705SXin Li
1650*67e74705SXin Li cutOffParsing();
1651*67e74705SXin Li return ExprError();
1652*67e74705SXin Li }
1653*67e74705SXin Li
1654*67e74705SXin Li if (MayBePseudoDestructor && !LHS.isInvalid()) {
1655*67e74705SXin Li LHS = ParseCXXPseudoDestructor(LHS.get(), OpLoc, OpKind, SS,
1656*67e74705SXin Li ObjectType);
1657*67e74705SXin Li break;
1658*67e74705SXin Li }
1659*67e74705SXin Li
1660*67e74705SXin Li // Either the action has told us that this cannot be a
1661*67e74705SXin Li // pseudo-destructor expression (based on the type of base
1662*67e74705SXin Li // expression), or we didn't see a '~' in the right place. We
1663*67e74705SXin Li // can still parse a destructor name here, but in that case it
1664*67e74705SXin Li // names a real destructor.
1665*67e74705SXin Li // Allow explicit constructor calls in Microsoft mode.
1666*67e74705SXin Li // FIXME: Add support for explicit call of template constructor.
1667*67e74705SXin Li SourceLocation TemplateKWLoc;
1668*67e74705SXin Li UnqualifiedId Name;
1669*67e74705SXin Li if (getLangOpts().ObjC2 && OpKind == tok::period &&
1670*67e74705SXin Li Tok.is(tok::kw_class)) {
1671*67e74705SXin Li // Objective-C++:
1672*67e74705SXin Li // After a '.' in a member access expression, treat the keyword
1673*67e74705SXin Li // 'class' as if it were an identifier.
1674*67e74705SXin Li //
1675*67e74705SXin Li // This hack allows property access to the 'class' method because it is
1676*67e74705SXin Li // such a common method name. For other C++ keywords that are
1677*67e74705SXin Li // Objective-C method names, one must use the message send syntax.
1678*67e74705SXin Li IdentifierInfo *Id = Tok.getIdentifierInfo();
1679*67e74705SXin Li SourceLocation Loc = ConsumeToken();
1680*67e74705SXin Li Name.setIdentifier(Id, Loc);
1681*67e74705SXin Li } else if (ParseUnqualifiedId(SS,
1682*67e74705SXin Li /*EnteringContext=*/false,
1683*67e74705SXin Li /*AllowDestructorName=*/true,
1684*67e74705SXin Li /*AllowConstructorName=*/
1685*67e74705SXin Li getLangOpts().MicrosoftExt,
1686*67e74705SXin Li ObjectType, TemplateKWLoc, Name)) {
1687*67e74705SXin Li (void)Actions.CorrectDelayedTyposInExpr(LHS);
1688*67e74705SXin Li LHS = ExprError();
1689*67e74705SXin Li }
1690*67e74705SXin Li
1691*67e74705SXin Li if (!LHS.isInvalid())
1692*67e74705SXin Li LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.get(), OpLoc,
1693*67e74705SXin Li OpKind, SS, TemplateKWLoc, Name,
1694*67e74705SXin Li CurParsedObjCImpl ? CurParsedObjCImpl->Dcl
1695*67e74705SXin Li : nullptr);
1696*67e74705SXin Li break;
1697*67e74705SXin Li }
1698*67e74705SXin Li case tok::plusplus: // postfix-expression: postfix-expression '++'
1699*67e74705SXin Li case tok::minusminus: // postfix-expression: postfix-expression '--'
1700*67e74705SXin Li if (!LHS.isInvalid()) {
1701*67e74705SXin Li LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
1702*67e74705SXin Li Tok.getKind(), LHS.get());
1703*67e74705SXin Li }
1704*67e74705SXin Li ConsumeToken();
1705*67e74705SXin Li break;
1706*67e74705SXin Li }
1707*67e74705SXin Li }
1708*67e74705SXin Li }
1709*67e74705SXin Li
1710*67e74705SXin Li /// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1711*67e74705SXin Li /// vec_step and we are at the start of an expression or a parenthesized
1712*67e74705SXin Li /// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1713*67e74705SXin Li /// expression (isCastExpr == false) or the type (isCastExpr == true).
1714*67e74705SXin Li ///
1715*67e74705SXin Li /// \verbatim
1716*67e74705SXin Li /// unary-expression: [C99 6.5.3]
1717*67e74705SXin Li /// 'sizeof' unary-expression
1718*67e74705SXin Li /// 'sizeof' '(' type-name ')'
1719*67e74705SXin Li /// [GNU] '__alignof' unary-expression
1720*67e74705SXin Li /// [GNU] '__alignof' '(' type-name ')'
1721*67e74705SXin Li /// [C11] '_Alignof' '(' type-name ')'
1722*67e74705SXin Li /// [C++0x] 'alignof' '(' type-id ')'
1723*67e74705SXin Li ///
1724*67e74705SXin Li /// [GNU] typeof-specifier:
1725*67e74705SXin Li /// typeof ( expressions )
1726*67e74705SXin Li /// typeof ( type-name )
1727*67e74705SXin Li /// [GNU/C++] typeof unary-expression
1728*67e74705SXin Li ///
1729*67e74705SXin Li /// [OpenCL 1.1 6.11.12] vec_step built-in function:
1730*67e74705SXin Li /// vec_step ( expressions )
1731*67e74705SXin Li /// vec_step ( type-name )
1732*67e74705SXin Li /// \endverbatim
1733*67e74705SXin Li ExprResult
ParseExprAfterUnaryExprOrTypeTrait(const Token & OpTok,bool & isCastExpr,ParsedType & CastTy,SourceRange & CastRange)1734*67e74705SXin Li Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1735*67e74705SXin Li bool &isCastExpr,
1736*67e74705SXin Li ParsedType &CastTy,
1737*67e74705SXin Li SourceRange &CastRange) {
1738*67e74705SXin Li
1739*67e74705SXin Li assert(OpTok.isOneOf(tok::kw_typeof, tok::kw_sizeof, tok::kw___alignof,
1740*67e74705SXin Li tok::kw_alignof, tok::kw__Alignof, tok::kw_vec_step,
1741*67e74705SXin Li tok::kw___builtin_omp_required_simd_align) &&
1742*67e74705SXin Li "Not a typeof/sizeof/alignof/vec_step expression!");
1743*67e74705SXin Li
1744*67e74705SXin Li ExprResult Operand;
1745*67e74705SXin Li
1746*67e74705SXin Li // If the operand doesn't start with an '(', it must be an expression.
1747*67e74705SXin Li if (Tok.isNot(tok::l_paren)) {
1748*67e74705SXin Li // If construct allows a form without parenthesis, user may forget to put
1749*67e74705SXin Li // pathenthesis around type name.
1750*67e74705SXin Li if (OpTok.isOneOf(tok::kw_sizeof, tok::kw___alignof, tok::kw_alignof,
1751*67e74705SXin Li tok::kw__Alignof)) {
1752*67e74705SXin Li if (isTypeIdUnambiguously()) {
1753*67e74705SXin Li DeclSpec DS(AttrFactory);
1754*67e74705SXin Li ParseSpecifierQualifierList(DS);
1755*67e74705SXin Li Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1756*67e74705SXin Li ParseDeclarator(DeclaratorInfo);
1757*67e74705SXin Li
1758*67e74705SXin Li SourceLocation LParenLoc = PP.getLocForEndOfToken(OpTok.getLocation());
1759*67e74705SXin Li SourceLocation RParenLoc = PP.getLocForEndOfToken(PrevTokLocation);
1760*67e74705SXin Li Diag(LParenLoc, diag::err_expected_parentheses_around_typename)
1761*67e74705SXin Li << OpTok.getName()
1762*67e74705SXin Li << FixItHint::CreateInsertion(LParenLoc, "(")
1763*67e74705SXin Li << FixItHint::CreateInsertion(RParenLoc, ")");
1764*67e74705SXin Li isCastExpr = true;
1765*67e74705SXin Li return ExprEmpty();
1766*67e74705SXin Li }
1767*67e74705SXin Li }
1768*67e74705SXin Li
1769*67e74705SXin Li isCastExpr = false;
1770*67e74705SXin Li if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
1771*67e74705SXin Li Diag(Tok, diag::err_expected_after) << OpTok.getIdentifierInfo()
1772*67e74705SXin Li << tok::l_paren;
1773*67e74705SXin Li return ExprError();
1774*67e74705SXin Li }
1775*67e74705SXin Li
1776*67e74705SXin Li Operand = ParseCastExpression(true/*isUnaryExpression*/);
1777*67e74705SXin Li } else {
1778*67e74705SXin Li // If it starts with a '(', we know that it is either a parenthesized
1779*67e74705SXin Li // type-name, or it is a unary-expression that starts with a compound
1780*67e74705SXin Li // literal, or starts with a primary-expression that is a parenthesized
1781*67e74705SXin Li // expression.
1782*67e74705SXin Li ParenParseOption ExprType = CastExpr;
1783*67e74705SXin Li SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
1784*67e74705SXin Li
1785*67e74705SXin Li Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
1786*67e74705SXin Li false, CastTy, RParenLoc);
1787*67e74705SXin Li CastRange = SourceRange(LParenLoc, RParenLoc);
1788*67e74705SXin Li
1789*67e74705SXin Li // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1790*67e74705SXin Li // a type.
1791*67e74705SXin Li if (ExprType == CastExpr) {
1792*67e74705SXin Li isCastExpr = true;
1793*67e74705SXin Li return ExprEmpty();
1794*67e74705SXin Li }
1795*67e74705SXin Li
1796*67e74705SXin Li if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
1797*67e74705SXin Li // GNU typeof in C requires the expression to be parenthesized. Not so for
1798*67e74705SXin Li // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1799*67e74705SXin Li // the start of a unary-expression, but doesn't include any postfix
1800*67e74705SXin Li // pieces. Parse these now if present.
1801*67e74705SXin Li if (!Operand.isInvalid())
1802*67e74705SXin Li Operand = ParsePostfixExpressionSuffix(Operand.get());
1803*67e74705SXin Li }
1804*67e74705SXin Li }
1805*67e74705SXin Li
1806*67e74705SXin Li // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1807*67e74705SXin Li isCastExpr = false;
1808*67e74705SXin Li return Operand;
1809*67e74705SXin Li }
1810*67e74705SXin Li
1811*67e74705SXin Li
1812*67e74705SXin Li /// \brief Parse a sizeof or alignof expression.
1813*67e74705SXin Li ///
1814*67e74705SXin Li /// \verbatim
1815*67e74705SXin Li /// unary-expression: [C99 6.5.3]
1816*67e74705SXin Li /// 'sizeof' unary-expression
1817*67e74705SXin Li /// 'sizeof' '(' type-name ')'
1818*67e74705SXin Li /// [C++11] 'sizeof' '...' '(' identifier ')'
1819*67e74705SXin Li /// [GNU] '__alignof' unary-expression
1820*67e74705SXin Li /// [GNU] '__alignof' '(' type-name ')'
1821*67e74705SXin Li /// [C11] '_Alignof' '(' type-name ')'
1822*67e74705SXin Li /// [C++11] 'alignof' '(' type-id ')'
1823*67e74705SXin Li /// \endverbatim
ParseUnaryExprOrTypeTraitExpression()1824*67e74705SXin Li ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
1825*67e74705SXin Li assert(Tok.isOneOf(tok::kw_sizeof, tok::kw___alignof, tok::kw_alignof,
1826*67e74705SXin Li tok::kw__Alignof, tok::kw_vec_step,
1827*67e74705SXin Li tok::kw___builtin_omp_required_simd_align) &&
1828*67e74705SXin Li "Not a sizeof/alignof/vec_step expression!");
1829*67e74705SXin Li Token OpTok = Tok;
1830*67e74705SXin Li ConsumeToken();
1831*67e74705SXin Li
1832*67e74705SXin Li // [C++11] 'sizeof' '...' '(' identifier ')'
1833*67e74705SXin Li if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1834*67e74705SXin Li SourceLocation EllipsisLoc = ConsumeToken();
1835*67e74705SXin Li SourceLocation LParenLoc, RParenLoc;
1836*67e74705SXin Li IdentifierInfo *Name = nullptr;
1837*67e74705SXin Li SourceLocation NameLoc;
1838*67e74705SXin Li if (Tok.is(tok::l_paren)) {
1839*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
1840*67e74705SXin Li T.consumeOpen();
1841*67e74705SXin Li LParenLoc = T.getOpenLocation();
1842*67e74705SXin Li if (Tok.is(tok::identifier)) {
1843*67e74705SXin Li Name = Tok.getIdentifierInfo();
1844*67e74705SXin Li NameLoc = ConsumeToken();
1845*67e74705SXin Li T.consumeClose();
1846*67e74705SXin Li RParenLoc = T.getCloseLocation();
1847*67e74705SXin Li if (RParenLoc.isInvalid())
1848*67e74705SXin Li RParenLoc = PP.getLocForEndOfToken(NameLoc);
1849*67e74705SXin Li } else {
1850*67e74705SXin Li Diag(Tok, diag::err_expected_parameter_pack);
1851*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
1852*67e74705SXin Li }
1853*67e74705SXin Li } else if (Tok.is(tok::identifier)) {
1854*67e74705SXin Li Name = Tok.getIdentifierInfo();
1855*67e74705SXin Li NameLoc = ConsumeToken();
1856*67e74705SXin Li LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1857*67e74705SXin Li RParenLoc = PP.getLocForEndOfToken(NameLoc);
1858*67e74705SXin Li Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1859*67e74705SXin Li << Name
1860*67e74705SXin Li << FixItHint::CreateInsertion(LParenLoc, "(")
1861*67e74705SXin Li << FixItHint::CreateInsertion(RParenLoc, ")");
1862*67e74705SXin Li } else {
1863*67e74705SXin Li Diag(Tok, diag::err_sizeof_parameter_pack);
1864*67e74705SXin Li }
1865*67e74705SXin Li
1866*67e74705SXin Li if (!Name)
1867*67e74705SXin Li return ExprError();
1868*67e74705SXin Li
1869*67e74705SXin Li EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1870*67e74705SXin Li Sema::ReuseLambdaContextDecl);
1871*67e74705SXin Li
1872*67e74705SXin Li return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1873*67e74705SXin Li OpTok.getLocation(),
1874*67e74705SXin Li *Name, NameLoc,
1875*67e74705SXin Li RParenLoc);
1876*67e74705SXin Li }
1877*67e74705SXin Li
1878*67e74705SXin Li if (OpTok.isOneOf(tok::kw_alignof, tok::kw__Alignof))
1879*67e74705SXin Li Diag(OpTok, diag::warn_cxx98_compat_alignof);
1880*67e74705SXin Li
1881*67e74705SXin Li EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1882*67e74705SXin Li Sema::ReuseLambdaContextDecl);
1883*67e74705SXin Li
1884*67e74705SXin Li bool isCastExpr;
1885*67e74705SXin Li ParsedType CastTy;
1886*67e74705SXin Li SourceRange CastRange;
1887*67e74705SXin Li ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1888*67e74705SXin Li isCastExpr,
1889*67e74705SXin Li CastTy,
1890*67e74705SXin Li CastRange);
1891*67e74705SXin Li
1892*67e74705SXin Li UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1893*67e74705SXin Li if (OpTok.isOneOf(tok::kw_alignof, tok::kw___alignof, tok::kw__Alignof))
1894*67e74705SXin Li ExprKind = UETT_AlignOf;
1895*67e74705SXin Li else if (OpTok.is(tok::kw_vec_step))
1896*67e74705SXin Li ExprKind = UETT_VecStep;
1897*67e74705SXin Li else if (OpTok.is(tok::kw___builtin_omp_required_simd_align))
1898*67e74705SXin Li ExprKind = UETT_OpenMPRequiredSimdAlign;
1899*67e74705SXin Li
1900*67e74705SXin Li if (isCastExpr)
1901*67e74705SXin Li return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1902*67e74705SXin Li ExprKind,
1903*67e74705SXin Li /*isType=*/true,
1904*67e74705SXin Li CastTy.getAsOpaquePtr(),
1905*67e74705SXin Li CastRange);
1906*67e74705SXin Li
1907*67e74705SXin Li if (OpTok.isOneOf(tok::kw_alignof, tok::kw__Alignof))
1908*67e74705SXin Li Diag(OpTok, diag::ext_alignof_expr) << OpTok.getIdentifierInfo();
1909*67e74705SXin Li
1910*67e74705SXin Li // If we get here, the operand to the sizeof/alignof was an expresion.
1911*67e74705SXin Li if (!Operand.isInvalid())
1912*67e74705SXin Li Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1913*67e74705SXin Li ExprKind,
1914*67e74705SXin Li /*isType=*/false,
1915*67e74705SXin Li Operand.get(),
1916*67e74705SXin Li CastRange);
1917*67e74705SXin Li return Operand;
1918*67e74705SXin Li }
1919*67e74705SXin Li
1920*67e74705SXin Li /// ParseBuiltinPrimaryExpression
1921*67e74705SXin Li ///
1922*67e74705SXin Li /// \verbatim
1923*67e74705SXin Li /// primary-expression: [C99 6.5.1]
1924*67e74705SXin Li /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1925*67e74705SXin Li /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1926*67e74705SXin Li /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1927*67e74705SXin Li /// assign-expr ')'
1928*67e74705SXin Li /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
1929*67e74705SXin Li /// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
1930*67e74705SXin Li ///
1931*67e74705SXin Li /// [GNU] offsetof-member-designator:
1932*67e74705SXin Li /// [GNU] identifier
1933*67e74705SXin Li /// [GNU] offsetof-member-designator '.' identifier
1934*67e74705SXin Li /// [GNU] offsetof-member-designator '[' expression ']'
1935*67e74705SXin Li /// \endverbatim
ParseBuiltinPrimaryExpression()1936*67e74705SXin Li ExprResult Parser::ParseBuiltinPrimaryExpression() {
1937*67e74705SXin Li ExprResult Res;
1938*67e74705SXin Li const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1939*67e74705SXin Li
1940*67e74705SXin Li tok::TokenKind T = Tok.getKind();
1941*67e74705SXin Li SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1942*67e74705SXin Li
1943*67e74705SXin Li // All of these start with an open paren.
1944*67e74705SXin Li if (Tok.isNot(tok::l_paren))
1945*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected_after) << BuiltinII
1946*67e74705SXin Li << tok::l_paren);
1947*67e74705SXin Li
1948*67e74705SXin Li BalancedDelimiterTracker PT(*this, tok::l_paren);
1949*67e74705SXin Li PT.consumeOpen();
1950*67e74705SXin Li
1951*67e74705SXin Li // TODO: Build AST.
1952*67e74705SXin Li
1953*67e74705SXin Li switch (T) {
1954*67e74705SXin Li default: llvm_unreachable("Not a builtin primary expression!");
1955*67e74705SXin Li case tok::kw___builtin_va_arg: {
1956*67e74705SXin Li ExprResult Expr(ParseAssignmentExpression());
1957*67e74705SXin Li
1958*67e74705SXin Li if (ExpectAndConsume(tok::comma)) {
1959*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
1960*67e74705SXin Li Expr = ExprError();
1961*67e74705SXin Li }
1962*67e74705SXin Li
1963*67e74705SXin Li TypeResult Ty = ParseTypeName();
1964*67e74705SXin Li
1965*67e74705SXin Li if (Tok.isNot(tok::r_paren)) {
1966*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::r_paren;
1967*67e74705SXin Li Expr = ExprError();
1968*67e74705SXin Li }
1969*67e74705SXin Li
1970*67e74705SXin Li if (Expr.isInvalid() || Ty.isInvalid())
1971*67e74705SXin Li Res = ExprError();
1972*67e74705SXin Li else
1973*67e74705SXin Li Res = Actions.ActOnVAArg(StartLoc, Expr.get(), Ty.get(), ConsumeParen());
1974*67e74705SXin Li break;
1975*67e74705SXin Li }
1976*67e74705SXin Li case tok::kw___builtin_offsetof: {
1977*67e74705SXin Li SourceLocation TypeLoc = Tok.getLocation();
1978*67e74705SXin Li TypeResult Ty = ParseTypeName();
1979*67e74705SXin Li if (Ty.isInvalid()) {
1980*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
1981*67e74705SXin Li return ExprError();
1982*67e74705SXin Li }
1983*67e74705SXin Li
1984*67e74705SXin Li if (ExpectAndConsume(tok::comma)) {
1985*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
1986*67e74705SXin Li return ExprError();
1987*67e74705SXin Li }
1988*67e74705SXin Li
1989*67e74705SXin Li // We must have at least one identifier here.
1990*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
1991*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier;
1992*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
1993*67e74705SXin Li return ExprError();
1994*67e74705SXin Li }
1995*67e74705SXin Li
1996*67e74705SXin Li // Keep track of the various subcomponents we see.
1997*67e74705SXin Li SmallVector<Sema::OffsetOfComponent, 4> Comps;
1998*67e74705SXin Li
1999*67e74705SXin Li Comps.push_back(Sema::OffsetOfComponent());
2000*67e74705SXin Li Comps.back().isBrackets = false;
2001*67e74705SXin Li Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
2002*67e74705SXin Li Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
2003*67e74705SXin Li
2004*67e74705SXin Li // FIXME: This loop leaks the index expressions on error.
2005*67e74705SXin Li while (1) {
2006*67e74705SXin Li if (Tok.is(tok::period)) {
2007*67e74705SXin Li // offsetof-member-designator: offsetof-member-designator '.' identifier
2008*67e74705SXin Li Comps.push_back(Sema::OffsetOfComponent());
2009*67e74705SXin Li Comps.back().isBrackets = false;
2010*67e74705SXin Li Comps.back().LocStart = ConsumeToken();
2011*67e74705SXin Li
2012*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
2013*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier;
2014*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2015*67e74705SXin Li return ExprError();
2016*67e74705SXin Li }
2017*67e74705SXin Li Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
2018*67e74705SXin Li Comps.back().LocEnd = ConsumeToken();
2019*67e74705SXin Li
2020*67e74705SXin Li } else if (Tok.is(tok::l_square)) {
2021*67e74705SXin Li if (CheckProhibitedCXX11Attribute())
2022*67e74705SXin Li return ExprError();
2023*67e74705SXin Li
2024*67e74705SXin Li // offsetof-member-designator: offsetof-member-design '[' expression ']'
2025*67e74705SXin Li Comps.push_back(Sema::OffsetOfComponent());
2026*67e74705SXin Li Comps.back().isBrackets = true;
2027*67e74705SXin Li BalancedDelimiterTracker ST(*this, tok::l_square);
2028*67e74705SXin Li ST.consumeOpen();
2029*67e74705SXin Li Comps.back().LocStart = ST.getOpenLocation();
2030*67e74705SXin Li Res = ParseExpression();
2031*67e74705SXin Li if (Res.isInvalid()) {
2032*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2033*67e74705SXin Li return Res;
2034*67e74705SXin Li }
2035*67e74705SXin Li Comps.back().U.E = Res.get();
2036*67e74705SXin Li
2037*67e74705SXin Li ST.consumeClose();
2038*67e74705SXin Li Comps.back().LocEnd = ST.getCloseLocation();
2039*67e74705SXin Li } else {
2040*67e74705SXin Li if (Tok.isNot(tok::r_paren)) {
2041*67e74705SXin Li PT.consumeClose();
2042*67e74705SXin Li Res = ExprError();
2043*67e74705SXin Li } else if (Ty.isInvalid()) {
2044*67e74705SXin Li Res = ExprError();
2045*67e74705SXin Li } else {
2046*67e74705SXin Li PT.consumeClose();
2047*67e74705SXin Li Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
2048*67e74705SXin Li Ty.get(), Comps,
2049*67e74705SXin Li PT.getCloseLocation());
2050*67e74705SXin Li }
2051*67e74705SXin Li break;
2052*67e74705SXin Li }
2053*67e74705SXin Li }
2054*67e74705SXin Li break;
2055*67e74705SXin Li }
2056*67e74705SXin Li case tok::kw___builtin_choose_expr: {
2057*67e74705SXin Li ExprResult Cond(ParseAssignmentExpression());
2058*67e74705SXin Li if (Cond.isInvalid()) {
2059*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2060*67e74705SXin Li return Cond;
2061*67e74705SXin Li }
2062*67e74705SXin Li if (ExpectAndConsume(tok::comma)) {
2063*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2064*67e74705SXin Li return ExprError();
2065*67e74705SXin Li }
2066*67e74705SXin Li
2067*67e74705SXin Li ExprResult Expr1(ParseAssignmentExpression());
2068*67e74705SXin Li if (Expr1.isInvalid()) {
2069*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2070*67e74705SXin Li return Expr1;
2071*67e74705SXin Li }
2072*67e74705SXin Li if (ExpectAndConsume(tok::comma)) {
2073*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2074*67e74705SXin Li return ExprError();
2075*67e74705SXin Li }
2076*67e74705SXin Li
2077*67e74705SXin Li ExprResult Expr2(ParseAssignmentExpression());
2078*67e74705SXin Li if (Expr2.isInvalid()) {
2079*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2080*67e74705SXin Li return Expr2;
2081*67e74705SXin Li }
2082*67e74705SXin Li if (Tok.isNot(tok::r_paren)) {
2083*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::r_paren;
2084*67e74705SXin Li return ExprError();
2085*67e74705SXin Li }
2086*67e74705SXin Li Res = Actions.ActOnChooseExpr(StartLoc, Cond.get(), Expr1.get(),
2087*67e74705SXin Li Expr2.get(), ConsumeParen());
2088*67e74705SXin Li break;
2089*67e74705SXin Li }
2090*67e74705SXin Li case tok::kw___builtin_astype: {
2091*67e74705SXin Li // The first argument is an expression to be converted, followed by a comma.
2092*67e74705SXin Li ExprResult Expr(ParseAssignmentExpression());
2093*67e74705SXin Li if (Expr.isInvalid()) {
2094*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2095*67e74705SXin Li return ExprError();
2096*67e74705SXin Li }
2097*67e74705SXin Li
2098*67e74705SXin Li if (ExpectAndConsume(tok::comma)) {
2099*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2100*67e74705SXin Li return ExprError();
2101*67e74705SXin Li }
2102*67e74705SXin Li
2103*67e74705SXin Li // Second argument is the type to bitcast to.
2104*67e74705SXin Li TypeResult DestTy = ParseTypeName();
2105*67e74705SXin Li if (DestTy.isInvalid())
2106*67e74705SXin Li return ExprError();
2107*67e74705SXin Li
2108*67e74705SXin Li // Attempt to consume the r-paren.
2109*67e74705SXin Li if (Tok.isNot(tok::r_paren)) {
2110*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::r_paren;
2111*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2112*67e74705SXin Li return ExprError();
2113*67e74705SXin Li }
2114*67e74705SXin Li
2115*67e74705SXin Li Res = Actions.ActOnAsTypeExpr(Expr.get(), DestTy.get(), StartLoc,
2116*67e74705SXin Li ConsumeParen());
2117*67e74705SXin Li break;
2118*67e74705SXin Li }
2119*67e74705SXin Li case tok::kw___builtin_convertvector: {
2120*67e74705SXin Li // The first argument is an expression to be converted, followed by a comma.
2121*67e74705SXin Li ExprResult Expr(ParseAssignmentExpression());
2122*67e74705SXin Li if (Expr.isInvalid()) {
2123*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2124*67e74705SXin Li return ExprError();
2125*67e74705SXin Li }
2126*67e74705SXin Li
2127*67e74705SXin Li if (ExpectAndConsume(tok::comma)) {
2128*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2129*67e74705SXin Li return ExprError();
2130*67e74705SXin Li }
2131*67e74705SXin Li
2132*67e74705SXin Li // Second argument is the type to bitcast to.
2133*67e74705SXin Li TypeResult DestTy = ParseTypeName();
2134*67e74705SXin Li if (DestTy.isInvalid())
2135*67e74705SXin Li return ExprError();
2136*67e74705SXin Li
2137*67e74705SXin Li // Attempt to consume the r-paren.
2138*67e74705SXin Li if (Tok.isNot(tok::r_paren)) {
2139*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::r_paren;
2140*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2141*67e74705SXin Li return ExprError();
2142*67e74705SXin Li }
2143*67e74705SXin Li
2144*67e74705SXin Li Res = Actions.ActOnConvertVectorExpr(Expr.get(), DestTy.get(), StartLoc,
2145*67e74705SXin Li ConsumeParen());
2146*67e74705SXin Li break;
2147*67e74705SXin Li }
2148*67e74705SXin Li }
2149*67e74705SXin Li
2150*67e74705SXin Li if (Res.isInvalid())
2151*67e74705SXin Li return ExprError();
2152*67e74705SXin Li
2153*67e74705SXin Li // These can be followed by postfix-expr pieces because they are
2154*67e74705SXin Li // primary-expressions.
2155*67e74705SXin Li return ParsePostfixExpressionSuffix(Res.get());
2156*67e74705SXin Li }
2157*67e74705SXin Li
2158*67e74705SXin Li /// ParseParenExpression - This parses the unit that starts with a '(' token,
2159*67e74705SXin Li /// based on what is allowed by ExprType. The actual thing parsed is returned
2160*67e74705SXin Li /// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
2161*67e74705SXin Li /// not the parsed cast-expression.
2162*67e74705SXin Li ///
2163*67e74705SXin Li /// \verbatim
2164*67e74705SXin Li /// primary-expression: [C99 6.5.1]
2165*67e74705SXin Li /// '(' expression ')'
2166*67e74705SXin Li /// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
2167*67e74705SXin Li /// postfix-expression: [C99 6.5.2]
2168*67e74705SXin Li /// '(' type-name ')' '{' initializer-list '}'
2169*67e74705SXin Li /// '(' type-name ')' '{' initializer-list ',' '}'
2170*67e74705SXin Li /// cast-expression: [C99 6.5.4]
2171*67e74705SXin Li /// '(' type-name ')' cast-expression
2172*67e74705SXin Li /// [ARC] bridged-cast-expression
2173*67e74705SXin Li /// [ARC] bridged-cast-expression:
2174*67e74705SXin Li /// (__bridge type-name) cast-expression
2175*67e74705SXin Li /// (__bridge_transfer type-name) cast-expression
2176*67e74705SXin Li /// (__bridge_retained type-name) cast-expression
2177*67e74705SXin Li /// fold-expression: [C++1z]
2178*67e74705SXin Li /// '(' cast-expression fold-operator '...' ')'
2179*67e74705SXin Li /// '(' '...' fold-operator cast-expression ')'
2180*67e74705SXin Li /// '(' cast-expression fold-operator '...'
2181*67e74705SXin Li /// fold-operator cast-expression ')'
2182*67e74705SXin Li /// \endverbatim
2183*67e74705SXin Li ExprResult
ParseParenExpression(ParenParseOption & ExprType,bool stopIfCastExpr,bool isTypeCast,ParsedType & CastTy,SourceLocation & RParenLoc)2184*67e74705SXin Li Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
2185*67e74705SXin Li bool isTypeCast, ParsedType &CastTy,
2186*67e74705SXin Li SourceLocation &RParenLoc) {
2187*67e74705SXin Li assert(Tok.is(tok::l_paren) && "Not a paren expr!");
2188*67e74705SXin Li ColonProtectionRAIIObject ColonProtection(*this, false);
2189*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
2190*67e74705SXin Li if (T.consumeOpen())
2191*67e74705SXin Li return ExprError();
2192*67e74705SXin Li SourceLocation OpenLoc = T.getOpenLocation();
2193*67e74705SXin Li
2194*67e74705SXin Li ExprResult Result(true);
2195*67e74705SXin Li bool isAmbiguousTypeId;
2196*67e74705SXin Li CastTy = nullptr;
2197*67e74705SXin Li
2198*67e74705SXin Li if (Tok.is(tok::code_completion)) {
2199*67e74705SXin Li Actions.CodeCompleteOrdinaryName(getCurScope(),
2200*67e74705SXin Li ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
2201*67e74705SXin Li : Sema::PCC_Expression);
2202*67e74705SXin Li cutOffParsing();
2203*67e74705SXin Li return ExprError();
2204*67e74705SXin Li }
2205*67e74705SXin Li
2206*67e74705SXin Li // Diagnose use of bridge casts in non-arc mode.
2207*67e74705SXin Li bool BridgeCast = (getLangOpts().ObjC2 &&
2208*67e74705SXin Li Tok.isOneOf(tok::kw___bridge,
2209*67e74705SXin Li tok::kw___bridge_transfer,
2210*67e74705SXin Li tok::kw___bridge_retained,
2211*67e74705SXin Li tok::kw___bridge_retain));
2212*67e74705SXin Li if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
2213*67e74705SXin Li if (!TryConsumeToken(tok::kw___bridge)) {
2214*67e74705SXin Li StringRef BridgeCastName = Tok.getName();
2215*67e74705SXin Li SourceLocation BridgeKeywordLoc = ConsumeToken();
2216*67e74705SXin Li if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
2217*67e74705SXin Li Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
2218*67e74705SXin Li << BridgeCastName
2219*67e74705SXin Li << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
2220*67e74705SXin Li }
2221*67e74705SXin Li BridgeCast = false;
2222*67e74705SXin Li }
2223*67e74705SXin Li
2224*67e74705SXin Li // None of these cases should fall through with an invalid Result
2225*67e74705SXin Li // unless they've already reported an error.
2226*67e74705SXin Li if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
2227*67e74705SXin Li Diag(Tok, diag::ext_gnu_statement_expr);
2228*67e74705SXin Li
2229*67e74705SXin Li if (!getCurScope()->getFnParent() && !getCurScope()->getBlockParent()) {
2230*67e74705SXin Li Result = ExprError(Diag(OpenLoc, diag::err_stmtexpr_file_scope));
2231*67e74705SXin Li } else {
2232*67e74705SXin Li // Find the nearest non-record decl context. Variables declared in a
2233*67e74705SXin Li // statement expression behave as if they were declared in the enclosing
2234*67e74705SXin Li // function, block, or other code construct.
2235*67e74705SXin Li DeclContext *CodeDC = Actions.CurContext;
2236*67e74705SXin Li while (CodeDC->isRecord() || isa<EnumDecl>(CodeDC)) {
2237*67e74705SXin Li CodeDC = CodeDC->getParent();
2238*67e74705SXin Li assert(CodeDC && !CodeDC->isFileContext() &&
2239*67e74705SXin Li "statement expr not in code context");
2240*67e74705SXin Li }
2241*67e74705SXin Li Sema::ContextRAII SavedContext(Actions, CodeDC, /*NewThisContext=*/false);
2242*67e74705SXin Li
2243*67e74705SXin Li Actions.ActOnStartStmtExpr();
2244*67e74705SXin Li
2245*67e74705SXin Li StmtResult Stmt(ParseCompoundStatement(true));
2246*67e74705SXin Li ExprType = CompoundStmt;
2247*67e74705SXin Li
2248*67e74705SXin Li // If the substmt parsed correctly, build the AST node.
2249*67e74705SXin Li if (!Stmt.isInvalid()) {
2250*67e74705SXin Li Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.get(), Tok.getLocation());
2251*67e74705SXin Li } else {
2252*67e74705SXin Li Actions.ActOnStmtExprError();
2253*67e74705SXin Li }
2254*67e74705SXin Li }
2255*67e74705SXin Li } else if (ExprType >= CompoundLiteral && BridgeCast) {
2256*67e74705SXin Li tok::TokenKind tokenKind = Tok.getKind();
2257*67e74705SXin Li SourceLocation BridgeKeywordLoc = ConsumeToken();
2258*67e74705SXin Li
2259*67e74705SXin Li // Parse an Objective-C ARC ownership cast expression.
2260*67e74705SXin Li ObjCBridgeCastKind Kind;
2261*67e74705SXin Li if (tokenKind == tok::kw___bridge)
2262*67e74705SXin Li Kind = OBC_Bridge;
2263*67e74705SXin Li else if (tokenKind == tok::kw___bridge_transfer)
2264*67e74705SXin Li Kind = OBC_BridgeTransfer;
2265*67e74705SXin Li else if (tokenKind == tok::kw___bridge_retained)
2266*67e74705SXin Li Kind = OBC_BridgeRetained;
2267*67e74705SXin Li else {
2268*67e74705SXin Li // As a hopefully temporary workaround, allow __bridge_retain as
2269*67e74705SXin Li // a synonym for __bridge_retained, but only in system headers.
2270*67e74705SXin Li assert(tokenKind == tok::kw___bridge_retain);
2271*67e74705SXin Li Kind = OBC_BridgeRetained;
2272*67e74705SXin Li if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
2273*67e74705SXin Li Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
2274*67e74705SXin Li << FixItHint::CreateReplacement(BridgeKeywordLoc,
2275*67e74705SXin Li "__bridge_retained");
2276*67e74705SXin Li }
2277*67e74705SXin Li
2278*67e74705SXin Li TypeResult Ty = ParseTypeName();
2279*67e74705SXin Li T.consumeClose();
2280*67e74705SXin Li ColonProtection.restore();
2281*67e74705SXin Li RParenLoc = T.getCloseLocation();
2282*67e74705SXin Li ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
2283*67e74705SXin Li
2284*67e74705SXin Li if (Ty.isInvalid() || SubExpr.isInvalid())
2285*67e74705SXin Li return ExprError();
2286*67e74705SXin Li
2287*67e74705SXin Li return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
2288*67e74705SXin Li BridgeKeywordLoc, Ty.get(),
2289*67e74705SXin Li RParenLoc, SubExpr.get());
2290*67e74705SXin Li } else if (ExprType >= CompoundLiteral &&
2291*67e74705SXin Li isTypeIdInParens(isAmbiguousTypeId)) {
2292*67e74705SXin Li
2293*67e74705SXin Li // Otherwise, this is a compound literal expression or cast expression.
2294*67e74705SXin Li
2295*67e74705SXin Li // In C++, if the type-id is ambiguous we disambiguate based on context.
2296*67e74705SXin Li // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
2297*67e74705SXin Li // in which case we should treat it as type-id.
2298*67e74705SXin Li // if stopIfCastExpr is false, we need to determine the context past the
2299*67e74705SXin Li // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
2300*67e74705SXin Li if (isAmbiguousTypeId && !stopIfCastExpr) {
2301*67e74705SXin Li ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T,
2302*67e74705SXin Li ColonProtection);
2303*67e74705SXin Li RParenLoc = T.getCloseLocation();
2304*67e74705SXin Li return res;
2305*67e74705SXin Li }
2306*67e74705SXin Li
2307*67e74705SXin Li // Parse the type declarator.
2308*67e74705SXin Li DeclSpec DS(AttrFactory);
2309*67e74705SXin Li ParseSpecifierQualifierList(DS);
2310*67e74705SXin Li Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2311*67e74705SXin Li ParseDeclarator(DeclaratorInfo);
2312*67e74705SXin Li
2313*67e74705SXin Li // If our type is followed by an identifier and either ':' or ']', then
2314*67e74705SXin Li // this is probably an Objective-C message send where the leading '[' is
2315*67e74705SXin Li // missing. Recover as if that were the case.
2316*67e74705SXin Li if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
2317*67e74705SXin Li !InMessageExpression && getLangOpts().ObjC1 &&
2318*67e74705SXin Li (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
2319*67e74705SXin Li TypeResult Ty;
2320*67e74705SXin Li {
2321*67e74705SXin Li InMessageExpressionRAIIObject InMessage(*this, false);
2322*67e74705SXin Li Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2323*67e74705SXin Li }
2324*67e74705SXin Li Result = ParseObjCMessageExpressionBody(SourceLocation(),
2325*67e74705SXin Li SourceLocation(),
2326*67e74705SXin Li Ty.get(), nullptr);
2327*67e74705SXin Li } else {
2328*67e74705SXin Li // Match the ')'.
2329*67e74705SXin Li T.consumeClose();
2330*67e74705SXin Li ColonProtection.restore();
2331*67e74705SXin Li RParenLoc = T.getCloseLocation();
2332*67e74705SXin Li if (Tok.is(tok::l_brace)) {
2333*67e74705SXin Li ExprType = CompoundLiteral;
2334*67e74705SXin Li TypeResult Ty;
2335*67e74705SXin Li {
2336*67e74705SXin Li InMessageExpressionRAIIObject InMessage(*this, false);
2337*67e74705SXin Li Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2338*67e74705SXin Li }
2339*67e74705SXin Li return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
2340*67e74705SXin Li }
2341*67e74705SXin Li
2342*67e74705SXin Li if (ExprType == CastExpr) {
2343*67e74705SXin Li // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
2344*67e74705SXin Li
2345*67e74705SXin Li if (DeclaratorInfo.isInvalidType())
2346*67e74705SXin Li return ExprError();
2347*67e74705SXin Li
2348*67e74705SXin Li // Note that this doesn't parse the subsequent cast-expression, it just
2349*67e74705SXin Li // returns the parsed type to the callee.
2350*67e74705SXin Li if (stopIfCastExpr) {
2351*67e74705SXin Li TypeResult Ty;
2352*67e74705SXin Li {
2353*67e74705SXin Li InMessageExpressionRAIIObject InMessage(*this, false);
2354*67e74705SXin Li Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2355*67e74705SXin Li }
2356*67e74705SXin Li CastTy = Ty.get();
2357*67e74705SXin Li return ExprResult();
2358*67e74705SXin Li }
2359*67e74705SXin Li
2360*67e74705SXin Li // Reject the cast of super idiom in ObjC.
2361*67e74705SXin Li if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
2362*67e74705SXin Li Tok.getIdentifierInfo() == Ident_super &&
2363*67e74705SXin Li getCurScope()->isInObjcMethodScope() &&
2364*67e74705SXin Li GetLookAheadToken(1).isNot(tok::period)) {
2365*67e74705SXin Li Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2366*67e74705SXin Li << SourceRange(OpenLoc, RParenLoc);
2367*67e74705SXin Li return ExprError();
2368*67e74705SXin Li }
2369*67e74705SXin Li
2370*67e74705SXin Li // Parse the cast-expression that follows it next.
2371*67e74705SXin Li // TODO: For cast expression with CastTy.
2372*67e74705SXin Li Result = ParseCastExpression(/*isUnaryExpression=*/false,
2373*67e74705SXin Li /*isAddressOfOperand=*/false,
2374*67e74705SXin Li /*isTypeCast=*/IsTypeCast);
2375*67e74705SXin Li if (!Result.isInvalid()) {
2376*67e74705SXin Li Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2377*67e74705SXin Li DeclaratorInfo, CastTy,
2378*67e74705SXin Li RParenLoc, Result.get());
2379*67e74705SXin Li }
2380*67e74705SXin Li return Result;
2381*67e74705SXin Li }
2382*67e74705SXin Li
2383*67e74705SXin Li Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2384*67e74705SXin Li return ExprError();
2385*67e74705SXin Li }
2386*67e74705SXin Li } else if (Tok.is(tok::ellipsis) &&
2387*67e74705SXin Li isFoldOperator(NextToken().getKind())) {
2388*67e74705SXin Li return ParseFoldExpression(ExprResult(), T);
2389*67e74705SXin Li } else if (isTypeCast) {
2390*67e74705SXin Li // Parse the expression-list.
2391*67e74705SXin Li InMessageExpressionRAIIObject InMessage(*this, false);
2392*67e74705SXin Li
2393*67e74705SXin Li ExprVector ArgExprs;
2394*67e74705SXin Li CommaLocsTy CommaLocs;
2395*67e74705SXin Li
2396*67e74705SXin Li if (!ParseSimpleExpressionList(ArgExprs, CommaLocs)) {
2397*67e74705SXin Li // FIXME: If we ever support comma expressions as operands to
2398*67e74705SXin Li // fold-expressions, we'll need to allow multiple ArgExprs here.
2399*67e74705SXin Li if (ArgExprs.size() == 1 && isFoldOperator(Tok.getKind()) &&
2400*67e74705SXin Li NextToken().is(tok::ellipsis))
2401*67e74705SXin Li return ParseFoldExpression(Result, T);
2402*67e74705SXin Li
2403*67e74705SXin Li ExprType = SimpleExpr;
2404*67e74705SXin Li Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
2405*67e74705SXin Li ArgExprs);
2406*67e74705SXin Li }
2407*67e74705SXin Li } else {
2408*67e74705SXin Li InMessageExpressionRAIIObject InMessage(*this, false);
2409*67e74705SXin Li
2410*67e74705SXin Li Result = ParseExpression(MaybeTypeCast);
2411*67e74705SXin Li if (!getLangOpts().CPlusPlus && MaybeTypeCast && Result.isUsable()) {
2412*67e74705SXin Li // Correct typos in non-C++ code earlier so that implicit-cast-like
2413*67e74705SXin Li // expressions are parsed correctly.
2414*67e74705SXin Li Result = Actions.CorrectDelayedTyposInExpr(Result);
2415*67e74705SXin Li }
2416*67e74705SXin Li ExprType = SimpleExpr;
2417*67e74705SXin Li
2418*67e74705SXin Li if (isFoldOperator(Tok.getKind()) && NextToken().is(tok::ellipsis))
2419*67e74705SXin Li return ParseFoldExpression(Result, T);
2420*67e74705SXin Li
2421*67e74705SXin Li // Don't build a paren expression unless we actually match a ')'.
2422*67e74705SXin Li if (!Result.isInvalid() && Tok.is(tok::r_paren))
2423*67e74705SXin Li Result =
2424*67e74705SXin Li Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.get());
2425*67e74705SXin Li }
2426*67e74705SXin Li
2427*67e74705SXin Li // Match the ')'.
2428*67e74705SXin Li if (Result.isInvalid()) {
2429*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2430*67e74705SXin Li return ExprError();
2431*67e74705SXin Li }
2432*67e74705SXin Li
2433*67e74705SXin Li T.consumeClose();
2434*67e74705SXin Li RParenLoc = T.getCloseLocation();
2435*67e74705SXin Li return Result;
2436*67e74705SXin Li }
2437*67e74705SXin Li
2438*67e74705SXin Li /// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2439*67e74705SXin Li /// and we are at the left brace.
2440*67e74705SXin Li ///
2441*67e74705SXin Li /// \verbatim
2442*67e74705SXin Li /// postfix-expression: [C99 6.5.2]
2443*67e74705SXin Li /// '(' type-name ')' '{' initializer-list '}'
2444*67e74705SXin Li /// '(' type-name ')' '{' initializer-list ',' '}'
2445*67e74705SXin Li /// \endverbatim
2446*67e74705SXin Li ExprResult
ParseCompoundLiteralExpression(ParsedType Ty,SourceLocation LParenLoc,SourceLocation RParenLoc)2447*67e74705SXin Li Parser::ParseCompoundLiteralExpression(ParsedType Ty,
2448*67e74705SXin Li SourceLocation LParenLoc,
2449*67e74705SXin Li SourceLocation RParenLoc) {
2450*67e74705SXin Li assert(Tok.is(tok::l_brace) && "Not a compound literal!");
2451*67e74705SXin Li if (!getLangOpts().C99) // Compound literals don't exist in C90.
2452*67e74705SXin Li Diag(LParenLoc, diag::ext_c99_compound_literal);
2453*67e74705SXin Li ExprResult Result = ParseInitializer();
2454*67e74705SXin Li if (!Result.isInvalid() && Ty)
2455*67e74705SXin Li return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.get());
2456*67e74705SXin Li return Result;
2457*67e74705SXin Li }
2458*67e74705SXin Li
2459*67e74705SXin Li /// ParseStringLiteralExpression - This handles the various token types that
2460*67e74705SXin Li /// form string literals, and also handles string concatenation [C99 5.1.1.2,
2461*67e74705SXin Li /// translation phase #6].
2462*67e74705SXin Li ///
2463*67e74705SXin Li /// \verbatim
2464*67e74705SXin Li /// primary-expression: [C99 6.5.1]
2465*67e74705SXin Li /// string-literal
2466*67e74705SXin Li /// \verbatim
ParseStringLiteralExpression(bool AllowUserDefinedLiteral)2467*67e74705SXin Li ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
2468*67e74705SXin Li assert(isTokenStringLiteral() && "Not a string literal!");
2469*67e74705SXin Li
2470*67e74705SXin Li // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2471*67e74705SXin Li // considered to be strings for concatenation purposes.
2472*67e74705SXin Li SmallVector<Token, 4> StringToks;
2473*67e74705SXin Li
2474*67e74705SXin Li do {
2475*67e74705SXin Li StringToks.push_back(Tok);
2476*67e74705SXin Li ConsumeStringToken();
2477*67e74705SXin Li } while (isTokenStringLiteral());
2478*67e74705SXin Li
2479*67e74705SXin Li // Pass the set of string tokens, ready for concatenation, to the actions.
2480*67e74705SXin Li return Actions.ActOnStringLiteral(StringToks,
2481*67e74705SXin Li AllowUserDefinedLiteral ? getCurScope()
2482*67e74705SXin Li : nullptr);
2483*67e74705SXin Li }
2484*67e74705SXin Li
2485*67e74705SXin Li /// ParseGenericSelectionExpression - Parse a C11 generic-selection
2486*67e74705SXin Li /// [C11 6.5.1.1].
2487*67e74705SXin Li ///
2488*67e74705SXin Li /// \verbatim
2489*67e74705SXin Li /// generic-selection:
2490*67e74705SXin Li /// _Generic ( assignment-expression , generic-assoc-list )
2491*67e74705SXin Li /// generic-assoc-list:
2492*67e74705SXin Li /// generic-association
2493*67e74705SXin Li /// generic-assoc-list , generic-association
2494*67e74705SXin Li /// generic-association:
2495*67e74705SXin Li /// type-name : assignment-expression
2496*67e74705SXin Li /// default : assignment-expression
2497*67e74705SXin Li /// \endverbatim
ParseGenericSelectionExpression()2498*67e74705SXin Li ExprResult Parser::ParseGenericSelectionExpression() {
2499*67e74705SXin Li assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2500*67e74705SXin Li SourceLocation KeyLoc = ConsumeToken();
2501*67e74705SXin Li
2502*67e74705SXin Li if (!getLangOpts().C11)
2503*67e74705SXin Li Diag(KeyLoc, diag::ext_c11_generic_selection);
2504*67e74705SXin Li
2505*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
2506*67e74705SXin Li if (T.expectAndConsume())
2507*67e74705SXin Li return ExprError();
2508*67e74705SXin Li
2509*67e74705SXin Li ExprResult ControllingExpr;
2510*67e74705SXin Li {
2511*67e74705SXin Li // C11 6.5.1.1p3 "The controlling expression of a generic selection is
2512*67e74705SXin Li // not evaluated."
2513*67e74705SXin Li EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2514*67e74705SXin Li ControllingExpr =
2515*67e74705SXin Li Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
2516*67e74705SXin Li if (ControllingExpr.isInvalid()) {
2517*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2518*67e74705SXin Li return ExprError();
2519*67e74705SXin Li }
2520*67e74705SXin Li }
2521*67e74705SXin Li
2522*67e74705SXin Li if (ExpectAndConsume(tok::comma)) {
2523*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2524*67e74705SXin Li return ExprError();
2525*67e74705SXin Li }
2526*67e74705SXin Li
2527*67e74705SXin Li SourceLocation DefaultLoc;
2528*67e74705SXin Li TypeVector Types;
2529*67e74705SXin Li ExprVector Exprs;
2530*67e74705SXin Li do {
2531*67e74705SXin Li ParsedType Ty;
2532*67e74705SXin Li if (Tok.is(tok::kw_default)) {
2533*67e74705SXin Li // C11 6.5.1.1p2 "A generic selection shall have no more than one default
2534*67e74705SXin Li // generic association."
2535*67e74705SXin Li if (!DefaultLoc.isInvalid()) {
2536*67e74705SXin Li Diag(Tok, diag::err_duplicate_default_assoc);
2537*67e74705SXin Li Diag(DefaultLoc, diag::note_previous_default_assoc);
2538*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2539*67e74705SXin Li return ExprError();
2540*67e74705SXin Li }
2541*67e74705SXin Li DefaultLoc = ConsumeToken();
2542*67e74705SXin Li Ty = nullptr;
2543*67e74705SXin Li } else {
2544*67e74705SXin Li ColonProtectionRAIIObject X(*this);
2545*67e74705SXin Li TypeResult TR = ParseTypeName();
2546*67e74705SXin Li if (TR.isInvalid()) {
2547*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2548*67e74705SXin Li return ExprError();
2549*67e74705SXin Li }
2550*67e74705SXin Li Ty = TR.get();
2551*67e74705SXin Li }
2552*67e74705SXin Li Types.push_back(Ty);
2553*67e74705SXin Li
2554*67e74705SXin Li if (ExpectAndConsume(tok::colon)) {
2555*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2556*67e74705SXin Li return ExprError();
2557*67e74705SXin Li }
2558*67e74705SXin Li
2559*67e74705SXin Li // FIXME: These expressions should be parsed in a potentially potentially
2560*67e74705SXin Li // evaluated context.
2561*67e74705SXin Li ExprResult ER(
2562*67e74705SXin Li Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()));
2563*67e74705SXin Li if (ER.isInvalid()) {
2564*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2565*67e74705SXin Li return ExprError();
2566*67e74705SXin Li }
2567*67e74705SXin Li Exprs.push_back(ER.get());
2568*67e74705SXin Li } while (TryConsumeToken(tok::comma));
2569*67e74705SXin Li
2570*67e74705SXin Li T.consumeClose();
2571*67e74705SXin Li if (T.getCloseLocation().isInvalid())
2572*67e74705SXin Li return ExprError();
2573*67e74705SXin Li
2574*67e74705SXin Li return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2575*67e74705SXin Li T.getCloseLocation(),
2576*67e74705SXin Li ControllingExpr.get(),
2577*67e74705SXin Li Types, Exprs);
2578*67e74705SXin Li }
2579*67e74705SXin Li
2580*67e74705SXin Li /// \brief Parse A C++1z fold-expression after the opening paren and optional
2581*67e74705SXin Li /// left-hand-side expression.
2582*67e74705SXin Li ///
2583*67e74705SXin Li /// \verbatim
2584*67e74705SXin Li /// fold-expression:
2585*67e74705SXin Li /// ( cast-expression fold-operator ... )
2586*67e74705SXin Li /// ( ... fold-operator cast-expression )
2587*67e74705SXin Li /// ( cast-expression fold-operator ... fold-operator cast-expression )
ParseFoldExpression(ExprResult LHS,BalancedDelimiterTracker & T)2588*67e74705SXin Li ExprResult Parser::ParseFoldExpression(ExprResult LHS,
2589*67e74705SXin Li BalancedDelimiterTracker &T) {
2590*67e74705SXin Li if (LHS.isInvalid()) {
2591*67e74705SXin Li T.skipToEnd();
2592*67e74705SXin Li return true;
2593*67e74705SXin Li }
2594*67e74705SXin Li
2595*67e74705SXin Li tok::TokenKind Kind = tok::unknown;
2596*67e74705SXin Li SourceLocation FirstOpLoc;
2597*67e74705SXin Li if (LHS.isUsable()) {
2598*67e74705SXin Li Kind = Tok.getKind();
2599*67e74705SXin Li assert(isFoldOperator(Kind) && "missing fold-operator");
2600*67e74705SXin Li FirstOpLoc = ConsumeToken();
2601*67e74705SXin Li }
2602*67e74705SXin Li
2603*67e74705SXin Li assert(Tok.is(tok::ellipsis) && "not a fold-expression");
2604*67e74705SXin Li SourceLocation EllipsisLoc = ConsumeToken();
2605*67e74705SXin Li
2606*67e74705SXin Li ExprResult RHS;
2607*67e74705SXin Li if (Tok.isNot(tok::r_paren)) {
2608*67e74705SXin Li if (!isFoldOperator(Tok.getKind()))
2609*67e74705SXin Li return Diag(Tok.getLocation(), diag::err_expected_fold_operator);
2610*67e74705SXin Li
2611*67e74705SXin Li if (Kind != tok::unknown && Tok.getKind() != Kind)
2612*67e74705SXin Li Diag(Tok.getLocation(), diag::err_fold_operator_mismatch)
2613*67e74705SXin Li << SourceRange(FirstOpLoc);
2614*67e74705SXin Li Kind = Tok.getKind();
2615*67e74705SXin Li ConsumeToken();
2616*67e74705SXin Li
2617*67e74705SXin Li RHS = ParseExpression();
2618*67e74705SXin Li if (RHS.isInvalid()) {
2619*67e74705SXin Li T.skipToEnd();
2620*67e74705SXin Li return true;
2621*67e74705SXin Li }
2622*67e74705SXin Li }
2623*67e74705SXin Li
2624*67e74705SXin Li Diag(EllipsisLoc, getLangOpts().CPlusPlus1z
2625*67e74705SXin Li ? diag::warn_cxx14_compat_fold_expression
2626*67e74705SXin Li : diag::ext_fold_expression);
2627*67e74705SXin Li
2628*67e74705SXin Li T.consumeClose();
2629*67e74705SXin Li return Actions.ActOnCXXFoldExpr(T.getOpenLocation(), LHS.get(), Kind,
2630*67e74705SXin Li EllipsisLoc, RHS.get(), T.getCloseLocation());
2631*67e74705SXin Li }
2632*67e74705SXin Li
2633*67e74705SXin Li /// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2634*67e74705SXin Li ///
2635*67e74705SXin Li /// \verbatim
2636*67e74705SXin Li /// argument-expression-list:
2637*67e74705SXin Li /// assignment-expression
2638*67e74705SXin Li /// argument-expression-list , assignment-expression
2639*67e74705SXin Li ///
2640*67e74705SXin Li /// [C++] expression-list:
2641*67e74705SXin Li /// [C++] assignment-expression
2642*67e74705SXin Li /// [C++] expression-list , assignment-expression
2643*67e74705SXin Li ///
2644*67e74705SXin Li /// [C++0x] expression-list:
2645*67e74705SXin Li /// [C++0x] initializer-list
2646*67e74705SXin Li ///
2647*67e74705SXin Li /// [C++0x] initializer-list
2648*67e74705SXin Li /// [C++0x] initializer-clause ...[opt]
2649*67e74705SXin Li /// [C++0x] initializer-list , initializer-clause ...[opt]
2650*67e74705SXin Li ///
2651*67e74705SXin Li /// [C++0x] initializer-clause:
2652*67e74705SXin Li /// [C++0x] assignment-expression
2653*67e74705SXin Li /// [C++0x] braced-init-list
2654*67e74705SXin Li /// \endverbatim
ParseExpressionList(SmallVectorImpl<Expr * > & Exprs,SmallVectorImpl<SourceLocation> & CommaLocs,std::function<void ()> Completer)2655*67e74705SXin Li bool Parser::ParseExpressionList(SmallVectorImpl<Expr *> &Exprs,
2656*67e74705SXin Li SmallVectorImpl<SourceLocation> &CommaLocs,
2657*67e74705SXin Li std::function<void()> Completer) {
2658*67e74705SXin Li bool SawError = false;
2659*67e74705SXin Li while (1) {
2660*67e74705SXin Li if (Tok.is(tok::code_completion)) {
2661*67e74705SXin Li if (Completer)
2662*67e74705SXin Li Completer();
2663*67e74705SXin Li else
2664*67e74705SXin Li Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
2665*67e74705SXin Li cutOffParsing();
2666*67e74705SXin Li return true;
2667*67e74705SXin Li }
2668*67e74705SXin Li
2669*67e74705SXin Li ExprResult Expr;
2670*67e74705SXin Li if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2671*67e74705SXin Li Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2672*67e74705SXin Li Expr = ParseBraceInitializer();
2673*67e74705SXin Li } else
2674*67e74705SXin Li Expr = ParseAssignmentExpression();
2675*67e74705SXin Li
2676*67e74705SXin Li if (Tok.is(tok::ellipsis))
2677*67e74705SXin Li Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
2678*67e74705SXin Li if (Expr.isInvalid()) {
2679*67e74705SXin Li SkipUntil(tok::comma, tok::r_paren, StopBeforeMatch);
2680*67e74705SXin Li SawError = true;
2681*67e74705SXin Li } else {
2682*67e74705SXin Li Exprs.push_back(Expr.get());
2683*67e74705SXin Li }
2684*67e74705SXin Li
2685*67e74705SXin Li if (Tok.isNot(tok::comma))
2686*67e74705SXin Li break;
2687*67e74705SXin Li // Move to the next argument, remember where the comma was.
2688*67e74705SXin Li CommaLocs.push_back(ConsumeToken());
2689*67e74705SXin Li }
2690*67e74705SXin Li if (SawError) {
2691*67e74705SXin Li // Ensure typos get diagnosed when errors were encountered while parsing the
2692*67e74705SXin Li // expression list.
2693*67e74705SXin Li for (auto &E : Exprs) {
2694*67e74705SXin Li ExprResult Expr = Actions.CorrectDelayedTyposInExpr(E);
2695*67e74705SXin Li if (Expr.isUsable()) E = Expr.get();
2696*67e74705SXin Li }
2697*67e74705SXin Li }
2698*67e74705SXin Li return SawError;
2699*67e74705SXin Li }
2700*67e74705SXin Li
2701*67e74705SXin Li /// ParseSimpleExpressionList - A simple comma-separated list of expressions,
2702*67e74705SXin Li /// used for misc language extensions.
2703*67e74705SXin Li ///
2704*67e74705SXin Li /// \verbatim
2705*67e74705SXin Li /// simple-expression-list:
2706*67e74705SXin Li /// assignment-expression
2707*67e74705SXin Li /// simple-expression-list , assignment-expression
2708*67e74705SXin Li /// \endverbatim
2709*67e74705SXin Li bool
ParseSimpleExpressionList(SmallVectorImpl<Expr * > & Exprs,SmallVectorImpl<SourceLocation> & CommaLocs)2710*67e74705SXin Li Parser::ParseSimpleExpressionList(SmallVectorImpl<Expr*> &Exprs,
2711*67e74705SXin Li SmallVectorImpl<SourceLocation> &CommaLocs) {
2712*67e74705SXin Li while (1) {
2713*67e74705SXin Li ExprResult Expr = ParseAssignmentExpression();
2714*67e74705SXin Li if (Expr.isInvalid())
2715*67e74705SXin Li return true;
2716*67e74705SXin Li
2717*67e74705SXin Li Exprs.push_back(Expr.get());
2718*67e74705SXin Li
2719*67e74705SXin Li if (Tok.isNot(tok::comma))
2720*67e74705SXin Li return false;
2721*67e74705SXin Li
2722*67e74705SXin Li // Move to the next argument, remember where the comma was.
2723*67e74705SXin Li CommaLocs.push_back(ConsumeToken());
2724*67e74705SXin Li }
2725*67e74705SXin Li }
2726*67e74705SXin Li
2727*67e74705SXin Li /// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2728*67e74705SXin Li ///
2729*67e74705SXin Li /// \verbatim
2730*67e74705SXin Li /// [clang] block-id:
2731*67e74705SXin Li /// [clang] specifier-qualifier-list block-declarator
2732*67e74705SXin Li /// \endverbatim
ParseBlockId(SourceLocation CaretLoc)2733*67e74705SXin Li void Parser::ParseBlockId(SourceLocation CaretLoc) {
2734*67e74705SXin Li if (Tok.is(tok::code_completion)) {
2735*67e74705SXin Li Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
2736*67e74705SXin Li return cutOffParsing();
2737*67e74705SXin Li }
2738*67e74705SXin Li
2739*67e74705SXin Li // Parse the specifier-qualifier-list piece.
2740*67e74705SXin Li DeclSpec DS(AttrFactory);
2741*67e74705SXin Li ParseSpecifierQualifierList(DS);
2742*67e74705SXin Li
2743*67e74705SXin Li // Parse the block-declarator.
2744*67e74705SXin Li Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2745*67e74705SXin Li ParseDeclarator(DeclaratorInfo);
2746*67e74705SXin Li
2747*67e74705SXin Li MaybeParseGNUAttributes(DeclaratorInfo);
2748*67e74705SXin Li
2749*67e74705SXin Li // Inform sema that we are starting a block.
2750*67e74705SXin Li Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
2751*67e74705SXin Li }
2752*67e74705SXin Li
2753*67e74705SXin Li /// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
2754*67e74705SXin Li /// like ^(int x){ return x+1; }
2755*67e74705SXin Li ///
2756*67e74705SXin Li /// \verbatim
2757*67e74705SXin Li /// block-literal:
2758*67e74705SXin Li /// [clang] '^' block-args[opt] compound-statement
2759*67e74705SXin Li /// [clang] '^' block-id compound-statement
2760*67e74705SXin Li /// [clang] block-args:
2761*67e74705SXin Li /// [clang] '(' parameter-list ')'
2762*67e74705SXin Li /// \endverbatim
ParseBlockLiteralExpression()2763*67e74705SXin Li ExprResult Parser::ParseBlockLiteralExpression() {
2764*67e74705SXin Li assert(Tok.is(tok::caret) && "block literal starts with ^");
2765*67e74705SXin Li SourceLocation CaretLoc = ConsumeToken();
2766*67e74705SXin Li
2767*67e74705SXin Li PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2768*67e74705SXin Li "block literal parsing");
2769*67e74705SXin Li
2770*67e74705SXin Li // Enter a scope to hold everything within the block. This includes the
2771*67e74705SXin Li // argument decls, decls within the compound expression, etc. This also
2772*67e74705SXin Li // allows determining whether a variable reference inside the block is
2773*67e74705SXin Li // within or outside of the block.
2774*67e74705SXin Li ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
2775*67e74705SXin Li Scope::DeclScope);
2776*67e74705SXin Li
2777*67e74705SXin Li // Inform sema that we are starting a block.
2778*67e74705SXin Li Actions.ActOnBlockStart(CaretLoc, getCurScope());
2779*67e74705SXin Li
2780*67e74705SXin Li // Parse the return type if present.
2781*67e74705SXin Li DeclSpec DS(AttrFactory);
2782*67e74705SXin Li Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
2783*67e74705SXin Li // FIXME: Since the return type isn't actually parsed, it can't be used to
2784*67e74705SXin Li // fill ParamInfo with an initial valid range, so do it manually.
2785*67e74705SXin Li ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
2786*67e74705SXin Li
2787*67e74705SXin Li // If this block has arguments, parse them. There is no ambiguity here with
2788*67e74705SXin Li // the expression case, because the expression case requires a parameter list.
2789*67e74705SXin Li if (Tok.is(tok::l_paren)) {
2790*67e74705SXin Li ParseParenDeclarator(ParamInfo);
2791*67e74705SXin Li // Parse the pieces after the identifier as if we had "int(...)".
2792*67e74705SXin Li // SetIdentifier sets the source range end, but in this case we're past
2793*67e74705SXin Li // that location.
2794*67e74705SXin Li SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
2795*67e74705SXin Li ParamInfo.SetIdentifier(nullptr, CaretLoc);
2796*67e74705SXin Li ParamInfo.SetRangeEnd(Tmp);
2797*67e74705SXin Li if (ParamInfo.isInvalidType()) {
2798*67e74705SXin Li // If there was an error parsing the arguments, they may have
2799*67e74705SXin Li // tried to use ^(x+y) which requires an argument list. Just
2800*67e74705SXin Li // skip the whole block literal.
2801*67e74705SXin Li Actions.ActOnBlockError(CaretLoc, getCurScope());
2802*67e74705SXin Li return ExprError();
2803*67e74705SXin Li }
2804*67e74705SXin Li
2805*67e74705SXin Li MaybeParseGNUAttributes(ParamInfo);
2806*67e74705SXin Li
2807*67e74705SXin Li // Inform sema that we are starting a block.
2808*67e74705SXin Li Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
2809*67e74705SXin Li } else if (!Tok.is(tok::l_brace)) {
2810*67e74705SXin Li ParseBlockId(CaretLoc);
2811*67e74705SXin Li } else {
2812*67e74705SXin Li // Otherwise, pretend we saw (void).
2813*67e74705SXin Li ParsedAttributes attrs(AttrFactory);
2814*67e74705SXin Li SourceLocation NoLoc;
2815*67e74705SXin Li ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(/*HasProto=*/true,
2816*67e74705SXin Li /*IsAmbiguous=*/false,
2817*67e74705SXin Li /*RParenLoc=*/NoLoc,
2818*67e74705SXin Li /*ArgInfo=*/nullptr,
2819*67e74705SXin Li /*NumArgs=*/0,
2820*67e74705SXin Li /*EllipsisLoc=*/NoLoc,
2821*67e74705SXin Li /*RParenLoc=*/NoLoc,
2822*67e74705SXin Li /*TypeQuals=*/0,
2823*67e74705SXin Li /*RefQualifierIsLvalueRef=*/true,
2824*67e74705SXin Li /*RefQualifierLoc=*/NoLoc,
2825*67e74705SXin Li /*ConstQualifierLoc=*/NoLoc,
2826*67e74705SXin Li /*VolatileQualifierLoc=*/NoLoc,
2827*67e74705SXin Li /*RestrictQualifierLoc=*/NoLoc,
2828*67e74705SXin Li /*MutableLoc=*/NoLoc,
2829*67e74705SXin Li EST_None,
2830*67e74705SXin Li /*ESpecRange=*/SourceRange(),
2831*67e74705SXin Li /*Exceptions=*/nullptr,
2832*67e74705SXin Li /*ExceptionRanges=*/nullptr,
2833*67e74705SXin Li /*NumExceptions=*/0,
2834*67e74705SXin Li /*NoexceptExpr=*/nullptr,
2835*67e74705SXin Li /*ExceptionSpecTokens=*/nullptr,
2836*67e74705SXin Li CaretLoc, CaretLoc,
2837*67e74705SXin Li ParamInfo),
2838*67e74705SXin Li attrs, CaretLoc);
2839*67e74705SXin Li
2840*67e74705SXin Li MaybeParseGNUAttributes(ParamInfo);
2841*67e74705SXin Li
2842*67e74705SXin Li // Inform sema that we are starting a block.
2843*67e74705SXin Li Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
2844*67e74705SXin Li }
2845*67e74705SXin Li
2846*67e74705SXin Li
2847*67e74705SXin Li ExprResult Result(true);
2848*67e74705SXin Li if (!Tok.is(tok::l_brace)) {
2849*67e74705SXin Li // Saw something like: ^expr
2850*67e74705SXin Li Diag(Tok, diag::err_expected_expression);
2851*67e74705SXin Li Actions.ActOnBlockError(CaretLoc, getCurScope());
2852*67e74705SXin Li return ExprError();
2853*67e74705SXin Li }
2854*67e74705SXin Li
2855*67e74705SXin Li StmtResult Stmt(ParseCompoundStatementBody());
2856*67e74705SXin Li BlockScope.Exit();
2857*67e74705SXin Li if (!Stmt.isInvalid())
2858*67e74705SXin Li Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.get(), getCurScope());
2859*67e74705SXin Li else
2860*67e74705SXin Li Actions.ActOnBlockError(CaretLoc, getCurScope());
2861*67e74705SXin Li return Result;
2862*67e74705SXin Li }
2863*67e74705SXin Li
2864*67e74705SXin Li /// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
2865*67e74705SXin Li ///
2866*67e74705SXin Li /// '__objc_yes'
2867*67e74705SXin Li /// '__objc_no'
ParseObjCBoolLiteral()2868*67e74705SXin Li ExprResult Parser::ParseObjCBoolLiteral() {
2869*67e74705SXin Li tok::TokenKind Kind = Tok.getKind();
2870*67e74705SXin Li return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
2871*67e74705SXin Li }
2872