1e5dd7070Spatrick //===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick ///
9e5dd7070Spatrick /// \file
10e5dd7070Spatrick /// Provides the Expression parsing implementation.
11e5dd7070Spatrick ///
12e5dd7070Spatrick /// Expressions in C99 basically consist of a bunch of binary operators with
13e5dd7070Spatrick /// unary operators and other random stuff at the leaves.
14e5dd7070Spatrick ///
15e5dd7070Spatrick /// In the C99 grammar, these unary operators bind tightest and are represented
16e5dd7070Spatrick /// as the 'cast-expression' production. Everything else is either a binary
17e5dd7070Spatrick /// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
18e5dd7070Spatrick /// handled by ParseCastExpression, the higher level pieces are handled by
19e5dd7070Spatrick /// ParseBinaryExpression.
20e5dd7070Spatrick ///
21e5dd7070Spatrick //===----------------------------------------------------------------------===//
22e5dd7070Spatrick
23e5dd7070Spatrick #include "clang/Parse/Parser.h"
24e5dd7070Spatrick #include "clang/AST/ASTContext.h"
25e5dd7070Spatrick #include "clang/AST/ExprCXX.h"
26e5dd7070Spatrick #include "clang/Basic/PrettyStackTrace.h"
27e5dd7070Spatrick #include "clang/Parse/RAIIObjectsForParser.h"
28e5dd7070Spatrick #include "clang/Sema/DeclSpec.h"
29e5dd7070Spatrick #include "clang/Sema/ParsedTemplate.h"
30e5dd7070Spatrick #include "clang/Sema/Scope.h"
31e5dd7070Spatrick #include "clang/Sema/TypoCorrection.h"
32e5dd7070Spatrick #include "llvm/ADT/SmallVector.h"
33*12c85518Srobert #include <optional>
34e5dd7070Spatrick using namespace clang;
35e5dd7070Spatrick
36e5dd7070Spatrick /// Simple precedence-based parser for binary/ternary operators.
37e5dd7070Spatrick ///
38e5dd7070Spatrick /// Note: we diverge from the C99 grammar when parsing the assignment-expression
39e5dd7070Spatrick /// production. C99 specifies that the LHS of an assignment operator should be
40e5dd7070Spatrick /// parsed as a unary-expression, but consistency dictates that it be a
41e5dd7070Spatrick /// conditional-expession. In practice, the important thing here is that the
42e5dd7070Spatrick /// LHS of an assignment has to be an l-value, which productions between
43e5dd7070Spatrick /// unary-expression and conditional-expression don't produce. Because we want
44e5dd7070Spatrick /// consistency, we parse the LHS as a conditional-expression, then check for
45e5dd7070Spatrick /// l-value-ness in semantic analysis stages.
46e5dd7070Spatrick ///
47e5dd7070Spatrick /// \verbatim
48e5dd7070Spatrick /// pm-expression: [C++ 5.5]
49e5dd7070Spatrick /// cast-expression
50e5dd7070Spatrick /// pm-expression '.*' cast-expression
51e5dd7070Spatrick /// pm-expression '->*' cast-expression
52e5dd7070Spatrick ///
53e5dd7070Spatrick /// multiplicative-expression: [C99 6.5.5]
54e5dd7070Spatrick /// Note: in C++, apply pm-expression instead of cast-expression
55e5dd7070Spatrick /// cast-expression
56e5dd7070Spatrick /// multiplicative-expression '*' cast-expression
57e5dd7070Spatrick /// multiplicative-expression '/' cast-expression
58e5dd7070Spatrick /// multiplicative-expression '%' cast-expression
59e5dd7070Spatrick ///
60e5dd7070Spatrick /// additive-expression: [C99 6.5.6]
61e5dd7070Spatrick /// multiplicative-expression
62e5dd7070Spatrick /// additive-expression '+' multiplicative-expression
63e5dd7070Spatrick /// additive-expression '-' multiplicative-expression
64e5dd7070Spatrick ///
65e5dd7070Spatrick /// shift-expression: [C99 6.5.7]
66e5dd7070Spatrick /// additive-expression
67e5dd7070Spatrick /// shift-expression '<<' additive-expression
68e5dd7070Spatrick /// shift-expression '>>' additive-expression
69e5dd7070Spatrick ///
70e5dd7070Spatrick /// compare-expression: [C++20 expr.spaceship]
71e5dd7070Spatrick /// shift-expression
72e5dd7070Spatrick /// compare-expression '<=>' shift-expression
73e5dd7070Spatrick ///
74e5dd7070Spatrick /// relational-expression: [C99 6.5.8]
75e5dd7070Spatrick /// compare-expression
76e5dd7070Spatrick /// relational-expression '<' compare-expression
77e5dd7070Spatrick /// relational-expression '>' compare-expression
78e5dd7070Spatrick /// relational-expression '<=' compare-expression
79e5dd7070Spatrick /// relational-expression '>=' compare-expression
80e5dd7070Spatrick ///
81e5dd7070Spatrick /// equality-expression: [C99 6.5.9]
82e5dd7070Spatrick /// relational-expression
83e5dd7070Spatrick /// equality-expression '==' relational-expression
84e5dd7070Spatrick /// equality-expression '!=' relational-expression
85e5dd7070Spatrick ///
86e5dd7070Spatrick /// AND-expression: [C99 6.5.10]
87e5dd7070Spatrick /// equality-expression
88e5dd7070Spatrick /// AND-expression '&' equality-expression
89e5dd7070Spatrick ///
90e5dd7070Spatrick /// exclusive-OR-expression: [C99 6.5.11]
91e5dd7070Spatrick /// AND-expression
92e5dd7070Spatrick /// exclusive-OR-expression '^' AND-expression
93e5dd7070Spatrick ///
94e5dd7070Spatrick /// inclusive-OR-expression: [C99 6.5.12]
95e5dd7070Spatrick /// exclusive-OR-expression
96e5dd7070Spatrick /// inclusive-OR-expression '|' exclusive-OR-expression
97e5dd7070Spatrick ///
98e5dd7070Spatrick /// logical-AND-expression: [C99 6.5.13]
99e5dd7070Spatrick /// inclusive-OR-expression
100e5dd7070Spatrick /// logical-AND-expression '&&' inclusive-OR-expression
101e5dd7070Spatrick ///
102e5dd7070Spatrick /// logical-OR-expression: [C99 6.5.14]
103e5dd7070Spatrick /// logical-AND-expression
104e5dd7070Spatrick /// logical-OR-expression '||' logical-AND-expression
105e5dd7070Spatrick ///
106e5dd7070Spatrick /// conditional-expression: [C99 6.5.15]
107e5dd7070Spatrick /// logical-OR-expression
108e5dd7070Spatrick /// logical-OR-expression '?' expression ':' conditional-expression
109e5dd7070Spatrick /// [GNU] logical-OR-expression '?' ':' conditional-expression
110e5dd7070Spatrick /// [C++] the third operand is an assignment-expression
111e5dd7070Spatrick ///
112e5dd7070Spatrick /// assignment-expression: [C99 6.5.16]
113e5dd7070Spatrick /// conditional-expression
114e5dd7070Spatrick /// unary-expression assignment-operator assignment-expression
115e5dd7070Spatrick /// [C++] throw-expression [C++ 15]
116e5dd7070Spatrick ///
117e5dd7070Spatrick /// assignment-operator: one of
118e5dd7070Spatrick /// = *= /= %= += -= <<= >>= &= ^= |=
119e5dd7070Spatrick ///
120e5dd7070Spatrick /// expression: [C99 6.5.17]
121e5dd7070Spatrick /// assignment-expression ...[opt]
122e5dd7070Spatrick /// expression ',' assignment-expression ...[opt]
123e5dd7070Spatrick /// \endverbatim
ParseExpression(TypeCastState isTypeCast)124e5dd7070Spatrick ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
125e5dd7070Spatrick ExprResult LHS(ParseAssignmentExpression(isTypeCast));
126e5dd7070Spatrick return ParseRHSOfBinaryExpression(LHS, prec::Comma);
127e5dd7070Spatrick }
128e5dd7070Spatrick
129e5dd7070Spatrick /// This routine is called when the '@' is seen and consumed.
130e5dd7070Spatrick /// Current token is an Identifier and is not a 'try'. This
131e5dd7070Spatrick /// routine is necessary to disambiguate \@try-statement from,
132e5dd7070Spatrick /// for example, \@encode-expression.
133e5dd7070Spatrick ///
134e5dd7070Spatrick ExprResult
ParseExpressionWithLeadingAt(SourceLocation AtLoc)135e5dd7070Spatrick Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
136e5dd7070Spatrick ExprResult LHS(ParseObjCAtExpression(AtLoc));
137e5dd7070Spatrick return ParseRHSOfBinaryExpression(LHS, prec::Comma);
138e5dd7070Spatrick }
139e5dd7070Spatrick
140e5dd7070Spatrick /// This routine is called when a leading '__extension__' is seen and
141e5dd7070Spatrick /// consumed. This is necessary because the token gets consumed in the
142e5dd7070Spatrick /// process of disambiguating between an expression and a declaration.
143e5dd7070Spatrick ExprResult
ParseExpressionWithLeadingExtension(SourceLocation ExtLoc)144e5dd7070Spatrick Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
145e5dd7070Spatrick ExprResult LHS(true);
146e5dd7070Spatrick {
147e5dd7070Spatrick // Silence extension warnings in the sub-expression
148e5dd7070Spatrick ExtensionRAIIObject O(Diags);
149e5dd7070Spatrick
150e5dd7070Spatrick LHS = ParseCastExpression(AnyCastExpr);
151e5dd7070Spatrick }
152e5dd7070Spatrick
153e5dd7070Spatrick if (!LHS.isInvalid())
154e5dd7070Spatrick LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
155e5dd7070Spatrick LHS.get());
156e5dd7070Spatrick
157e5dd7070Spatrick return ParseRHSOfBinaryExpression(LHS, prec::Comma);
158e5dd7070Spatrick }
159e5dd7070Spatrick
160e5dd7070Spatrick /// Parse an expr that doesn't include (top-level) commas.
ParseAssignmentExpression(TypeCastState isTypeCast)161e5dd7070Spatrick ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
162e5dd7070Spatrick if (Tok.is(tok::code_completion)) {
163a9ac8606Spatrick cutOffParsing();
164e5dd7070Spatrick Actions.CodeCompleteExpression(getCurScope(),
165e5dd7070Spatrick PreferredType.get(Tok.getLocation()));
166e5dd7070Spatrick return ExprError();
167e5dd7070Spatrick }
168e5dd7070Spatrick
169e5dd7070Spatrick if (Tok.is(tok::kw_throw))
170e5dd7070Spatrick return ParseThrowExpression();
171e5dd7070Spatrick if (Tok.is(tok::kw_co_yield))
172e5dd7070Spatrick return ParseCoyieldExpression();
173e5dd7070Spatrick
174e5dd7070Spatrick ExprResult LHS = ParseCastExpression(AnyCastExpr,
175e5dd7070Spatrick /*isAddressOfOperand=*/false,
176e5dd7070Spatrick isTypeCast);
177e5dd7070Spatrick return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
178e5dd7070Spatrick }
179e5dd7070Spatrick
180e5dd7070Spatrick /// Parse an assignment expression where part of an Objective-C message
181e5dd7070Spatrick /// send has already been parsed.
182e5dd7070Spatrick ///
183e5dd7070Spatrick /// In this case \p LBracLoc indicates the location of the '[' of the message
184e5dd7070Spatrick /// send, and either \p ReceiverName or \p ReceiverExpr is non-null indicating
185e5dd7070Spatrick /// the receiver of the message.
186e5dd7070Spatrick ///
187e5dd7070Spatrick /// Since this handles full assignment-expression's, it handles postfix
188e5dd7070Spatrick /// expressions and other binary operators for these expressions as well.
189e5dd7070Spatrick ExprResult
ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,SourceLocation SuperLoc,ParsedType ReceiverType,Expr * ReceiverExpr)190e5dd7070Spatrick Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
191e5dd7070Spatrick SourceLocation SuperLoc,
192e5dd7070Spatrick ParsedType ReceiverType,
193e5dd7070Spatrick Expr *ReceiverExpr) {
194e5dd7070Spatrick ExprResult R
195e5dd7070Spatrick = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
196e5dd7070Spatrick ReceiverType, ReceiverExpr);
197e5dd7070Spatrick R = ParsePostfixExpressionSuffix(R);
198e5dd7070Spatrick return ParseRHSOfBinaryExpression(R, prec::Assignment);
199e5dd7070Spatrick }
200e5dd7070Spatrick
201e5dd7070Spatrick ExprResult
ParseConstantExpressionInExprEvalContext(TypeCastState isTypeCast)202e5dd7070Spatrick Parser::ParseConstantExpressionInExprEvalContext(TypeCastState isTypeCast) {
203e5dd7070Spatrick assert(Actions.ExprEvalContexts.back().Context ==
204e5dd7070Spatrick Sema::ExpressionEvaluationContext::ConstantEvaluated &&
205e5dd7070Spatrick "Call this function only if your ExpressionEvaluationContext is "
206e5dd7070Spatrick "already ConstantEvaluated");
207e5dd7070Spatrick ExprResult LHS(ParseCastExpression(AnyCastExpr, false, isTypeCast));
208e5dd7070Spatrick ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
209e5dd7070Spatrick return Actions.ActOnConstantExpression(Res);
210e5dd7070Spatrick }
211e5dd7070Spatrick
ParseConstantExpression()212*12c85518Srobert ExprResult Parser::ParseConstantExpression() {
213e5dd7070Spatrick // C++03 [basic.def.odr]p2:
214e5dd7070Spatrick // An expression is potentially evaluated unless it appears where an
215e5dd7070Spatrick // integral constant expression is required (see 5.19) [...].
216e5dd7070Spatrick // C++98 and C++11 have no such rule, but this is only a defect in C++98.
217e5dd7070Spatrick EnterExpressionEvaluationContext ConstantEvaluated(
218e5dd7070Spatrick Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
219*12c85518Srobert return ParseConstantExpressionInExprEvalContext(NotTypeCast);
220e5dd7070Spatrick }
221e5dd7070Spatrick
ParseCaseExpression(SourceLocation CaseLoc)222e5dd7070Spatrick ExprResult Parser::ParseCaseExpression(SourceLocation CaseLoc) {
223e5dd7070Spatrick EnterExpressionEvaluationContext ConstantEvaluated(
224e5dd7070Spatrick Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
225e5dd7070Spatrick ExprResult LHS(ParseCastExpression(AnyCastExpr, false, NotTypeCast));
226e5dd7070Spatrick ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
227e5dd7070Spatrick return Actions.ActOnCaseExpr(CaseLoc, Res);
228e5dd7070Spatrick }
229e5dd7070Spatrick
230e5dd7070Spatrick /// Parse a constraint-expression.
231e5dd7070Spatrick ///
232e5dd7070Spatrick /// \verbatim
233e5dd7070Spatrick /// constraint-expression: C++2a[temp.constr.decl]p1
234e5dd7070Spatrick /// logical-or-expression
235e5dd7070Spatrick /// \endverbatim
ParseConstraintExpression()236e5dd7070Spatrick ExprResult Parser::ParseConstraintExpression() {
237e5dd7070Spatrick EnterExpressionEvaluationContext ConstantEvaluated(
238e5dd7070Spatrick Actions, Sema::ExpressionEvaluationContext::Unevaluated);
239e5dd7070Spatrick ExprResult LHS(ParseCastExpression(AnyCastExpr));
240e5dd7070Spatrick ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::LogicalOr));
241e5dd7070Spatrick if (Res.isUsable() && !Actions.CheckConstraintExpression(Res.get())) {
242e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(Res);
243e5dd7070Spatrick return ExprError();
244e5dd7070Spatrick }
245e5dd7070Spatrick return Res;
246e5dd7070Spatrick }
247e5dd7070Spatrick
248e5dd7070Spatrick /// \brief Parse a constraint-logical-and-expression.
249e5dd7070Spatrick ///
250e5dd7070Spatrick /// \verbatim
251e5dd7070Spatrick /// C++2a[temp.constr.decl]p1
252e5dd7070Spatrick /// constraint-logical-and-expression:
253e5dd7070Spatrick /// primary-expression
254e5dd7070Spatrick /// constraint-logical-and-expression '&&' primary-expression
255e5dd7070Spatrick ///
256e5dd7070Spatrick /// \endverbatim
257e5dd7070Spatrick ExprResult
ParseConstraintLogicalAndExpression(bool IsTrailingRequiresClause)258e5dd7070Spatrick Parser::ParseConstraintLogicalAndExpression(bool IsTrailingRequiresClause) {
259e5dd7070Spatrick EnterExpressionEvaluationContext ConstantEvaluated(
260e5dd7070Spatrick Actions, Sema::ExpressionEvaluationContext::Unevaluated);
261e5dd7070Spatrick bool NotPrimaryExpression = false;
262e5dd7070Spatrick auto ParsePrimary = [&] () {
263e5dd7070Spatrick ExprResult E = ParseCastExpression(PrimaryExprOnly,
264e5dd7070Spatrick /*isAddressOfOperand=*/false,
265e5dd7070Spatrick /*isTypeCast=*/NotTypeCast,
266e5dd7070Spatrick /*isVectorLiteral=*/false,
267e5dd7070Spatrick &NotPrimaryExpression);
268e5dd7070Spatrick if (E.isInvalid())
269e5dd7070Spatrick return ExprError();
270e5dd7070Spatrick auto RecoverFromNonPrimary = [&] (ExprResult E, bool Note) {
271e5dd7070Spatrick E = ParsePostfixExpressionSuffix(E);
272e5dd7070Spatrick // Use InclusiveOr, the precedence just after '&&' to not parse the
273e5dd7070Spatrick // next arguments to the logical and.
274e5dd7070Spatrick E = ParseRHSOfBinaryExpression(E, prec::InclusiveOr);
275e5dd7070Spatrick if (!E.isInvalid())
276e5dd7070Spatrick Diag(E.get()->getExprLoc(),
277e5dd7070Spatrick Note
278e5dd7070Spatrick ? diag::note_unparenthesized_non_primary_expr_in_requires_clause
279e5dd7070Spatrick : diag::err_unparenthesized_non_primary_expr_in_requires_clause)
280e5dd7070Spatrick << FixItHint::CreateInsertion(E.get()->getBeginLoc(), "(")
281e5dd7070Spatrick << FixItHint::CreateInsertion(
282e5dd7070Spatrick PP.getLocForEndOfToken(E.get()->getEndLoc()), ")")
283e5dd7070Spatrick << E.get()->getSourceRange();
284e5dd7070Spatrick return E;
285e5dd7070Spatrick };
286e5dd7070Spatrick
287e5dd7070Spatrick if (NotPrimaryExpression ||
288e5dd7070Spatrick // Check if the following tokens must be a part of a non-primary
289e5dd7070Spatrick // expression
290e5dd7070Spatrick getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
291e5dd7070Spatrick /*CPlusPlus11=*/true) > prec::LogicalAnd ||
292e5dd7070Spatrick // Postfix operators other than '(' (which will be checked for in
293e5dd7070Spatrick // CheckConstraintExpression).
294e5dd7070Spatrick Tok.isOneOf(tok::period, tok::plusplus, tok::minusminus) ||
295e5dd7070Spatrick (Tok.is(tok::l_square) && !NextToken().is(tok::l_square))) {
296e5dd7070Spatrick E = RecoverFromNonPrimary(E, /*Note=*/false);
297e5dd7070Spatrick if (E.isInvalid())
298e5dd7070Spatrick return ExprError();
299e5dd7070Spatrick NotPrimaryExpression = false;
300e5dd7070Spatrick }
301e5dd7070Spatrick bool PossibleNonPrimary;
302e5dd7070Spatrick bool IsConstraintExpr =
303e5dd7070Spatrick Actions.CheckConstraintExpression(E.get(), Tok, &PossibleNonPrimary,
304e5dd7070Spatrick IsTrailingRequiresClause);
305e5dd7070Spatrick if (!IsConstraintExpr || PossibleNonPrimary) {
306e5dd7070Spatrick // Atomic constraint might be an unparenthesized non-primary expression
307e5dd7070Spatrick // (such as a binary operator), in which case we might get here (e.g. in
308e5dd7070Spatrick // 'requires 0 + 1 && true' we would now be at '+', and parse and ignore
309e5dd7070Spatrick // the rest of the addition expression). Try to parse the rest of it here.
310e5dd7070Spatrick if (PossibleNonPrimary)
311e5dd7070Spatrick E = RecoverFromNonPrimary(E, /*Note=*/!IsConstraintExpr);
312e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(E);
313e5dd7070Spatrick return ExprError();
314e5dd7070Spatrick }
315e5dd7070Spatrick return E;
316e5dd7070Spatrick };
317e5dd7070Spatrick ExprResult LHS = ParsePrimary();
318e5dd7070Spatrick if (LHS.isInvalid())
319e5dd7070Spatrick return ExprError();
320e5dd7070Spatrick while (Tok.is(tok::ampamp)) {
321e5dd7070Spatrick SourceLocation LogicalAndLoc = ConsumeToken();
322e5dd7070Spatrick ExprResult RHS = ParsePrimary();
323e5dd7070Spatrick if (RHS.isInvalid()) {
324e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(LHS);
325e5dd7070Spatrick return ExprError();
326e5dd7070Spatrick }
327e5dd7070Spatrick ExprResult Op = Actions.ActOnBinOp(getCurScope(), LogicalAndLoc,
328e5dd7070Spatrick tok::ampamp, LHS.get(), RHS.get());
329e5dd7070Spatrick if (!Op.isUsable()) {
330e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(RHS);
331e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(LHS);
332e5dd7070Spatrick return ExprError();
333e5dd7070Spatrick }
334e5dd7070Spatrick LHS = Op;
335e5dd7070Spatrick }
336e5dd7070Spatrick return LHS;
337e5dd7070Spatrick }
338e5dd7070Spatrick
339e5dd7070Spatrick /// \brief Parse a constraint-logical-or-expression.
340e5dd7070Spatrick ///
341e5dd7070Spatrick /// \verbatim
342e5dd7070Spatrick /// C++2a[temp.constr.decl]p1
343e5dd7070Spatrick /// constraint-logical-or-expression:
344e5dd7070Spatrick /// constraint-logical-and-expression
345e5dd7070Spatrick /// constraint-logical-or-expression '||'
346e5dd7070Spatrick /// constraint-logical-and-expression
347e5dd7070Spatrick ///
348e5dd7070Spatrick /// \endverbatim
349e5dd7070Spatrick ExprResult
ParseConstraintLogicalOrExpression(bool IsTrailingRequiresClause)350e5dd7070Spatrick Parser::ParseConstraintLogicalOrExpression(bool IsTrailingRequiresClause) {
351e5dd7070Spatrick ExprResult LHS(ParseConstraintLogicalAndExpression(IsTrailingRequiresClause));
352e5dd7070Spatrick if (!LHS.isUsable())
353e5dd7070Spatrick return ExprError();
354e5dd7070Spatrick while (Tok.is(tok::pipepipe)) {
355e5dd7070Spatrick SourceLocation LogicalOrLoc = ConsumeToken();
356e5dd7070Spatrick ExprResult RHS =
357e5dd7070Spatrick ParseConstraintLogicalAndExpression(IsTrailingRequiresClause);
358e5dd7070Spatrick if (!RHS.isUsable()) {
359e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(LHS);
360e5dd7070Spatrick return ExprError();
361e5dd7070Spatrick }
362e5dd7070Spatrick ExprResult Op = Actions.ActOnBinOp(getCurScope(), LogicalOrLoc,
363e5dd7070Spatrick tok::pipepipe, LHS.get(), RHS.get());
364e5dd7070Spatrick if (!Op.isUsable()) {
365e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(RHS);
366e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(LHS);
367e5dd7070Spatrick return ExprError();
368e5dd7070Spatrick }
369e5dd7070Spatrick LHS = Op;
370e5dd7070Spatrick }
371e5dd7070Spatrick return LHS;
372e5dd7070Spatrick }
373e5dd7070Spatrick
isNotExpressionStart()374e5dd7070Spatrick bool Parser::isNotExpressionStart() {
375e5dd7070Spatrick tok::TokenKind K = Tok.getKind();
376e5dd7070Spatrick if (K == tok::l_brace || K == tok::r_brace ||
377e5dd7070Spatrick K == tok::kw_for || K == tok::kw_while ||
378e5dd7070Spatrick K == tok::kw_if || K == tok::kw_else ||
379e5dd7070Spatrick K == tok::kw_goto || K == tok::kw_try)
380e5dd7070Spatrick return true;
381e5dd7070Spatrick // If this is a decl-specifier, we can't be at the start of an expression.
382e5dd7070Spatrick return isKnownToBeDeclarationSpecifier();
383e5dd7070Spatrick }
384e5dd7070Spatrick
isFoldOperator(prec::Level Level) const385e5dd7070Spatrick bool Parser::isFoldOperator(prec::Level Level) const {
386e5dd7070Spatrick return Level > prec::Unknown && Level != prec::Conditional &&
387e5dd7070Spatrick Level != prec::Spaceship;
388e5dd7070Spatrick }
389e5dd7070Spatrick
isFoldOperator(tok::TokenKind Kind) const390e5dd7070Spatrick bool Parser::isFoldOperator(tok::TokenKind Kind) const {
391e5dd7070Spatrick return isFoldOperator(getBinOpPrecedence(Kind, GreaterThanIsOperator, true));
392e5dd7070Spatrick }
393e5dd7070Spatrick
394e5dd7070Spatrick /// Parse a binary expression that starts with \p LHS and has a
395e5dd7070Spatrick /// precedence of at least \p MinPrec.
396e5dd7070Spatrick ExprResult
ParseRHSOfBinaryExpression(ExprResult LHS,prec::Level MinPrec)397e5dd7070Spatrick Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
398e5dd7070Spatrick prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
399e5dd7070Spatrick GreaterThanIsOperator,
400e5dd7070Spatrick getLangOpts().CPlusPlus11);
401e5dd7070Spatrick SourceLocation ColonLoc;
402e5dd7070Spatrick
403e5dd7070Spatrick auto SavedType = PreferredType;
404*12c85518Srobert while (true) {
405e5dd7070Spatrick // Every iteration may rely on a preferred type for the whole expression.
406e5dd7070Spatrick PreferredType = SavedType;
407e5dd7070Spatrick // If this token has a lower precedence than we are allowed to parse (e.g.
408e5dd7070Spatrick // because we are called recursively, or because the token is not a binop),
409e5dd7070Spatrick // then we are done!
410e5dd7070Spatrick if (NextTokPrec < MinPrec)
411e5dd7070Spatrick return LHS;
412e5dd7070Spatrick
413e5dd7070Spatrick // Consume the operator, saving the operator token for error reporting.
414e5dd7070Spatrick Token OpToken = Tok;
415e5dd7070Spatrick ConsumeToken();
416e5dd7070Spatrick
417e5dd7070Spatrick if (OpToken.is(tok::caretcaret)) {
418e5dd7070Spatrick return ExprError(Diag(Tok, diag::err_opencl_logical_exclusive_or));
419e5dd7070Spatrick }
420e5dd7070Spatrick
421e5dd7070Spatrick // If we're potentially in a template-id, we may now be able to determine
422e5dd7070Spatrick // whether we're actually in one or not.
423e5dd7070Spatrick if (OpToken.isOneOf(tok::comma, tok::greater, tok::greatergreater,
424e5dd7070Spatrick tok::greatergreatergreater) &&
425e5dd7070Spatrick checkPotentialAngleBracketDelimiter(OpToken))
426e5dd7070Spatrick return ExprError();
427e5dd7070Spatrick
428e5dd7070Spatrick // Bail out when encountering a comma followed by a token which can't
429e5dd7070Spatrick // possibly be the start of an expression. For instance:
430e5dd7070Spatrick // int f() { return 1, }
431e5dd7070Spatrick // We can't do this before consuming the comma, because
432e5dd7070Spatrick // isNotExpressionStart() looks at the token stream.
433e5dd7070Spatrick if (OpToken.is(tok::comma) && isNotExpressionStart()) {
434e5dd7070Spatrick PP.EnterToken(Tok, /*IsReinject*/true);
435e5dd7070Spatrick Tok = OpToken;
436e5dd7070Spatrick return LHS;
437e5dd7070Spatrick }
438e5dd7070Spatrick
439e5dd7070Spatrick // If the next token is an ellipsis, then this is a fold-expression. Leave
440e5dd7070Spatrick // it alone so we can handle it in the paren expression.
441e5dd7070Spatrick if (isFoldOperator(NextTokPrec) && Tok.is(tok::ellipsis)) {
442e5dd7070Spatrick // FIXME: We can't check this via lookahead before we consume the token
443e5dd7070Spatrick // because that tickles a lexer bug.
444e5dd7070Spatrick PP.EnterToken(Tok, /*IsReinject*/true);
445e5dd7070Spatrick Tok = OpToken;
446e5dd7070Spatrick return LHS;
447e5dd7070Spatrick }
448e5dd7070Spatrick
449e5dd7070Spatrick // In Objective-C++, alternative operator tokens can be used as keyword args
450e5dd7070Spatrick // in message expressions. Unconsume the token so that it can reinterpreted
451e5dd7070Spatrick // as an identifier in ParseObjCMessageExpressionBody. i.e., we support:
452e5dd7070Spatrick // [foo meth:0 and:0];
453e5dd7070Spatrick // [foo not_eq];
454e5dd7070Spatrick if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
455e5dd7070Spatrick Tok.isOneOf(tok::colon, tok::r_square) &&
456e5dd7070Spatrick OpToken.getIdentifierInfo() != nullptr) {
457e5dd7070Spatrick PP.EnterToken(Tok, /*IsReinject*/true);
458e5dd7070Spatrick Tok = OpToken;
459e5dd7070Spatrick return LHS;
460e5dd7070Spatrick }
461e5dd7070Spatrick
462e5dd7070Spatrick // Special case handling for the ternary operator.
463e5dd7070Spatrick ExprResult TernaryMiddle(true);
464e5dd7070Spatrick if (NextTokPrec == prec::Conditional) {
465e5dd7070Spatrick if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
466e5dd7070Spatrick // Parse a braced-init-list here for error recovery purposes.
467e5dd7070Spatrick SourceLocation BraceLoc = Tok.getLocation();
468e5dd7070Spatrick TernaryMiddle = ParseBraceInitializer();
469e5dd7070Spatrick if (!TernaryMiddle.isInvalid()) {
470e5dd7070Spatrick Diag(BraceLoc, diag::err_init_list_bin_op)
471e5dd7070Spatrick << /*RHS*/ 1 << PP.getSpelling(OpToken)
472e5dd7070Spatrick << Actions.getExprRange(TernaryMiddle.get());
473e5dd7070Spatrick TernaryMiddle = ExprError();
474e5dd7070Spatrick }
475e5dd7070Spatrick } else if (Tok.isNot(tok::colon)) {
476e5dd7070Spatrick // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
477e5dd7070Spatrick ColonProtectionRAIIObject X(*this);
478e5dd7070Spatrick
479e5dd7070Spatrick // Handle this production specially:
480e5dd7070Spatrick // logical-OR-expression '?' expression ':' conditional-expression
481e5dd7070Spatrick // In particular, the RHS of the '?' is 'expression', not
482e5dd7070Spatrick // 'logical-OR-expression' as we might expect.
483e5dd7070Spatrick TernaryMiddle = ParseExpression();
484e5dd7070Spatrick } else {
485e5dd7070Spatrick // Special case handling of "X ? Y : Z" where Y is empty:
486e5dd7070Spatrick // logical-OR-expression '?' ':' conditional-expression [GNU]
487e5dd7070Spatrick TernaryMiddle = nullptr;
488e5dd7070Spatrick Diag(Tok, diag::ext_gnu_conditional_expr);
489e5dd7070Spatrick }
490e5dd7070Spatrick
491e5dd7070Spatrick if (TernaryMiddle.isInvalid()) {
492e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(LHS);
493e5dd7070Spatrick LHS = ExprError();
494e5dd7070Spatrick TernaryMiddle = nullptr;
495e5dd7070Spatrick }
496e5dd7070Spatrick
497e5dd7070Spatrick if (!TryConsumeToken(tok::colon, ColonLoc)) {
498e5dd7070Spatrick // Otherwise, we're missing a ':'. Assume that this was a typo that
499e5dd7070Spatrick // the user forgot. If we're not in a macro expansion, we can suggest
500e5dd7070Spatrick // a fixit hint. If there were two spaces before the current token,
501e5dd7070Spatrick // suggest inserting the colon in between them, otherwise insert ": ".
502e5dd7070Spatrick SourceLocation FILoc = Tok.getLocation();
503e5dd7070Spatrick const char *FIText = ": ";
504e5dd7070Spatrick const SourceManager &SM = PP.getSourceManager();
505e5dd7070Spatrick if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
506e5dd7070Spatrick assert(FILoc.isFileID());
507e5dd7070Spatrick bool IsInvalid = false;
508e5dd7070Spatrick const char *SourcePtr =
509e5dd7070Spatrick SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
510e5dd7070Spatrick if (!IsInvalid && *SourcePtr == ' ') {
511e5dd7070Spatrick SourcePtr =
512e5dd7070Spatrick SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
513e5dd7070Spatrick if (!IsInvalid && *SourcePtr == ' ') {
514e5dd7070Spatrick FILoc = FILoc.getLocWithOffset(-1);
515e5dd7070Spatrick FIText = ":";
516e5dd7070Spatrick }
517e5dd7070Spatrick }
518e5dd7070Spatrick }
519e5dd7070Spatrick
520e5dd7070Spatrick Diag(Tok, diag::err_expected)
521e5dd7070Spatrick << tok::colon << FixItHint::CreateInsertion(FILoc, FIText);
522e5dd7070Spatrick Diag(OpToken, diag::note_matching) << tok::question;
523e5dd7070Spatrick ColonLoc = Tok.getLocation();
524e5dd7070Spatrick }
525e5dd7070Spatrick }
526e5dd7070Spatrick
527e5dd7070Spatrick PreferredType.enterBinary(Actions, Tok.getLocation(), LHS.get(),
528e5dd7070Spatrick OpToken.getKind());
529e5dd7070Spatrick // Parse another leaf here for the RHS of the operator.
530e5dd7070Spatrick // ParseCastExpression works here because all RHS expressions in C have it
531e5dd7070Spatrick // as a prefix, at least. However, in C++, an assignment-expression could
532e5dd7070Spatrick // be a throw-expression, which is not a valid cast-expression.
533e5dd7070Spatrick // Therefore we need some special-casing here.
534e5dd7070Spatrick // Also note that the third operand of the conditional operator is
535e5dd7070Spatrick // an assignment-expression in C++, and in C++11, we can have a
536e5dd7070Spatrick // braced-init-list on the RHS of an assignment. For better diagnostics,
537e5dd7070Spatrick // parse as if we were allowed braced-init-lists everywhere, and check that
538e5dd7070Spatrick // they only appear on the RHS of assignments later.
539e5dd7070Spatrick ExprResult RHS;
540e5dd7070Spatrick bool RHSIsInitList = false;
541e5dd7070Spatrick if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
542e5dd7070Spatrick RHS = ParseBraceInitializer();
543e5dd7070Spatrick RHSIsInitList = true;
544e5dd7070Spatrick } else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional)
545e5dd7070Spatrick RHS = ParseAssignmentExpression();
546e5dd7070Spatrick else
547e5dd7070Spatrick RHS = ParseCastExpression(AnyCastExpr);
548e5dd7070Spatrick
549e5dd7070Spatrick if (RHS.isInvalid()) {
550e5dd7070Spatrick // FIXME: Errors generated by the delayed typo correction should be
551e5dd7070Spatrick // printed before errors from parsing the RHS, not after.
552e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(LHS);
553e5dd7070Spatrick if (TernaryMiddle.isUsable())
554e5dd7070Spatrick TernaryMiddle = Actions.CorrectDelayedTyposInExpr(TernaryMiddle);
555e5dd7070Spatrick LHS = ExprError();
556e5dd7070Spatrick }
557e5dd7070Spatrick
558e5dd7070Spatrick // Remember the precedence of this operator and get the precedence of the
559e5dd7070Spatrick // operator immediately to the right of the RHS.
560e5dd7070Spatrick prec::Level ThisPrec = NextTokPrec;
561e5dd7070Spatrick NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
562e5dd7070Spatrick getLangOpts().CPlusPlus11);
563e5dd7070Spatrick
564e5dd7070Spatrick // Assignment and conditional expressions are right-associative.
565e5dd7070Spatrick bool isRightAssoc = ThisPrec == prec::Conditional ||
566e5dd7070Spatrick ThisPrec == prec::Assignment;
567e5dd7070Spatrick
568e5dd7070Spatrick // Get the precedence of the operator to the right of the RHS. If it binds
569e5dd7070Spatrick // more tightly with RHS than we do, evaluate it completely first.
570e5dd7070Spatrick if (ThisPrec < NextTokPrec ||
571e5dd7070Spatrick (ThisPrec == NextTokPrec && isRightAssoc)) {
572e5dd7070Spatrick if (!RHS.isInvalid() && RHSIsInitList) {
573e5dd7070Spatrick Diag(Tok, diag::err_init_list_bin_op)
574e5dd7070Spatrick << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get());
575e5dd7070Spatrick RHS = ExprError();
576e5dd7070Spatrick }
577e5dd7070Spatrick // If this is left-associative, only parse things on the RHS that bind
578e5dd7070Spatrick // more tightly than the current operator. If it is left-associative, it
579e5dd7070Spatrick // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
580e5dd7070Spatrick // A=(B=(C=D)), where each paren is a level of recursion here.
581e5dd7070Spatrick // The function takes ownership of the RHS.
582e5dd7070Spatrick RHS = ParseRHSOfBinaryExpression(RHS,
583e5dd7070Spatrick static_cast<prec::Level>(ThisPrec + !isRightAssoc));
584e5dd7070Spatrick RHSIsInitList = false;
585e5dd7070Spatrick
586e5dd7070Spatrick if (RHS.isInvalid()) {
587e5dd7070Spatrick // FIXME: Errors generated by the delayed typo correction should be
588e5dd7070Spatrick // printed before errors from ParseRHSOfBinaryExpression, not after.
589e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(LHS);
590e5dd7070Spatrick if (TernaryMiddle.isUsable())
591e5dd7070Spatrick TernaryMiddle = Actions.CorrectDelayedTyposInExpr(TernaryMiddle);
592e5dd7070Spatrick LHS = ExprError();
593e5dd7070Spatrick }
594e5dd7070Spatrick
595e5dd7070Spatrick NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
596e5dd7070Spatrick getLangOpts().CPlusPlus11);
597e5dd7070Spatrick }
598e5dd7070Spatrick
599e5dd7070Spatrick if (!RHS.isInvalid() && RHSIsInitList) {
600e5dd7070Spatrick if (ThisPrec == prec::Assignment) {
601e5dd7070Spatrick Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
602e5dd7070Spatrick << Actions.getExprRange(RHS.get());
603e5dd7070Spatrick } else if (ColonLoc.isValid()) {
604e5dd7070Spatrick Diag(ColonLoc, diag::err_init_list_bin_op)
605e5dd7070Spatrick << /*RHS*/1 << ":"
606e5dd7070Spatrick << Actions.getExprRange(RHS.get());
607e5dd7070Spatrick LHS = ExprError();
608e5dd7070Spatrick } else {
609e5dd7070Spatrick Diag(OpToken, diag::err_init_list_bin_op)
610e5dd7070Spatrick << /*RHS*/1 << PP.getSpelling(OpToken)
611e5dd7070Spatrick << Actions.getExprRange(RHS.get());
612e5dd7070Spatrick LHS = ExprError();
613e5dd7070Spatrick }
614e5dd7070Spatrick }
615e5dd7070Spatrick
616e5dd7070Spatrick ExprResult OrigLHS = LHS;
617e5dd7070Spatrick if (!LHS.isInvalid()) {
618e5dd7070Spatrick // Combine the LHS and RHS into the LHS (e.g. build AST).
619e5dd7070Spatrick if (TernaryMiddle.isInvalid()) {
620e5dd7070Spatrick // If we're using '>>' as an operator within a template
621e5dd7070Spatrick // argument list (in C++98), suggest the addition of
622e5dd7070Spatrick // parentheses so that the code remains well-formed in C++0x.
623e5dd7070Spatrick if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
624e5dd7070Spatrick SuggestParentheses(OpToken.getLocation(),
625e5dd7070Spatrick diag::warn_cxx11_right_shift_in_template_arg,
626e5dd7070Spatrick SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
627e5dd7070Spatrick Actions.getExprRange(RHS.get()).getEnd()));
628e5dd7070Spatrick
629ec727ea7Spatrick ExprResult BinOp =
630ec727ea7Spatrick Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
631e5dd7070Spatrick OpToken.getKind(), LHS.get(), RHS.get());
632ec727ea7Spatrick if (BinOp.isInvalid())
633ec727ea7Spatrick BinOp = Actions.CreateRecoveryExpr(LHS.get()->getBeginLoc(),
634ec727ea7Spatrick RHS.get()->getEndLoc(),
635ec727ea7Spatrick {LHS.get(), RHS.get()});
636e5dd7070Spatrick
637ec727ea7Spatrick LHS = BinOp;
638e5dd7070Spatrick } else {
639ec727ea7Spatrick ExprResult CondOp = Actions.ActOnConditionalOp(
640ec727ea7Spatrick OpToken.getLocation(), ColonLoc, LHS.get(), TernaryMiddle.get(),
641e5dd7070Spatrick RHS.get());
642ec727ea7Spatrick if (CondOp.isInvalid()) {
643ec727ea7Spatrick std::vector<clang::Expr *> Args;
644ec727ea7Spatrick // TernaryMiddle can be null for the GNU conditional expr extension.
645ec727ea7Spatrick if (TernaryMiddle.get())
646ec727ea7Spatrick Args = {LHS.get(), TernaryMiddle.get(), RHS.get()};
647ec727ea7Spatrick else
648ec727ea7Spatrick Args = {LHS.get(), RHS.get()};
649ec727ea7Spatrick CondOp = Actions.CreateRecoveryExpr(LHS.get()->getBeginLoc(),
650ec727ea7Spatrick RHS.get()->getEndLoc(), Args);
651ec727ea7Spatrick }
652ec727ea7Spatrick
653ec727ea7Spatrick LHS = CondOp;
654e5dd7070Spatrick }
655e5dd7070Spatrick // In this case, ActOnBinOp or ActOnConditionalOp performed the
656e5dd7070Spatrick // CorrectDelayedTyposInExpr check.
657e5dd7070Spatrick if (!getLangOpts().CPlusPlus)
658e5dd7070Spatrick continue;
659e5dd7070Spatrick }
660e5dd7070Spatrick
661e5dd7070Spatrick // Ensure potential typos aren't left undiagnosed.
662e5dd7070Spatrick if (LHS.isInvalid()) {
663e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(OrigLHS);
664e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(TernaryMiddle);
665e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(RHS);
666e5dd7070Spatrick }
667e5dd7070Spatrick }
668e5dd7070Spatrick }
669e5dd7070Spatrick
670e5dd7070Spatrick /// Parse a cast-expression, unary-expression or primary-expression, based
671e5dd7070Spatrick /// on \p ExprType.
672e5dd7070Spatrick ///
673e5dd7070Spatrick /// \p isAddressOfOperand exists because an id-expression that is the
674e5dd7070Spatrick /// operand of address-of gets special treatment due to member pointers.
675e5dd7070Spatrick ///
ParseCastExpression(CastParseKind ParseKind,bool isAddressOfOperand,TypeCastState isTypeCast,bool isVectorLiteral,bool * NotPrimaryExpression)676e5dd7070Spatrick ExprResult Parser::ParseCastExpression(CastParseKind ParseKind,
677e5dd7070Spatrick bool isAddressOfOperand,
678e5dd7070Spatrick TypeCastState isTypeCast,
679e5dd7070Spatrick bool isVectorLiteral,
680e5dd7070Spatrick bool *NotPrimaryExpression) {
681e5dd7070Spatrick bool NotCastExpr;
682e5dd7070Spatrick ExprResult Res = ParseCastExpression(ParseKind,
683e5dd7070Spatrick isAddressOfOperand,
684e5dd7070Spatrick NotCastExpr,
685e5dd7070Spatrick isTypeCast,
686e5dd7070Spatrick isVectorLiteral,
687e5dd7070Spatrick NotPrimaryExpression);
688e5dd7070Spatrick if (NotCastExpr)
689e5dd7070Spatrick Diag(Tok, diag::err_expected_expression);
690e5dd7070Spatrick return Res;
691e5dd7070Spatrick }
692e5dd7070Spatrick
693e5dd7070Spatrick namespace {
694e5dd7070Spatrick class CastExpressionIdValidator final : public CorrectionCandidateCallback {
695e5dd7070Spatrick public:
CastExpressionIdValidator(Token Next,bool AllowTypes,bool AllowNonTypes)696e5dd7070Spatrick CastExpressionIdValidator(Token Next, bool AllowTypes, bool AllowNonTypes)
697e5dd7070Spatrick : NextToken(Next), AllowNonTypes(AllowNonTypes) {
698e5dd7070Spatrick WantTypeSpecifiers = WantFunctionLikeCasts = AllowTypes;
699e5dd7070Spatrick }
700e5dd7070Spatrick
ValidateCandidate(const TypoCorrection & candidate)701e5dd7070Spatrick bool ValidateCandidate(const TypoCorrection &candidate) override {
702e5dd7070Spatrick NamedDecl *ND = candidate.getCorrectionDecl();
703e5dd7070Spatrick if (!ND)
704e5dd7070Spatrick return candidate.isKeyword();
705e5dd7070Spatrick
706e5dd7070Spatrick if (isa<TypeDecl>(ND))
707e5dd7070Spatrick return WantTypeSpecifiers;
708e5dd7070Spatrick
709e5dd7070Spatrick if (!AllowNonTypes || !CorrectionCandidateCallback::ValidateCandidate(candidate))
710e5dd7070Spatrick return false;
711e5dd7070Spatrick
712e5dd7070Spatrick if (!NextToken.isOneOf(tok::equal, tok::arrow, tok::period))
713e5dd7070Spatrick return true;
714e5dd7070Spatrick
715e5dd7070Spatrick for (auto *C : candidate) {
716e5dd7070Spatrick NamedDecl *ND = C->getUnderlyingDecl();
717e5dd7070Spatrick if (isa<ValueDecl>(ND) && !isa<FunctionDecl>(ND))
718e5dd7070Spatrick return true;
719e5dd7070Spatrick }
720e5dd7070Spatrick return false;
721e5dd7070Spatrick }
722e5dd7070Spatrick
clone()723e5dd7070Spatrick std::unique_ptr<CorrectionCandidateCallback> clone() override {
724e5dd7070Spatrick return std::make_unique<CastExpressionIdValidator>(*this);
725e5dd7070Spatrick }
726e5dd7070Spatrick
727e5dd7070Spatrick private:
728e5dd7070Spatrick Token NextToken;
729e5dd7070Spatrick bool AllowNonTypes;
730e5dd7070Spatrick };
731e5dd7070Spatrick }
732e5dd7070Spatrick
733e5dd7070Spatrick /// Parse a cast-expression, or, if \pisUnaryExpression is true, parse
734e5dd7070Spatrick /// a unary-expression.
735e5dd7070Spatrick ///
736e5dd7070Spatrick /// \p isAddressOfOperand exists because an id-expression that is the operand
737e5dd7070Spatrick /// of address-of gets special treatment due to member pointers. NotCastExpr
738e5dd7070Spatrick /// is set to true if the token is not the start of a cast-expression, and no
739e5dd7070Spatrick /// diagnostic is emitted in this case and no tokens are consumed.
740e5dd7070Spatrick ///
741e5dd7070Spatrick /// \verbatim
742e5dd7070Spatrick /// cast-expression: [C99 6.5.4]
743e5dd7070Spatrick /// unary-expression
744e5dd7070Spatrick /// '(' type-name ')' cast-expression
745e5dd7070Spatrick ///
746e5dd7070Spatrick /// unary-expression: [C99 6.5.3]
747e5dd7070Spatrick /// postfix-expression
748e5dd7070Spatrick /// '++' unary-expression
749e5dd7070Spatrick /// '--' unary-expression
750e5dd7070Spatrick /// [Coro] 'co_await' cast-expression
751e5dd7070Spatrick /// unary-operator cast-expression
752e5dd7070Spatrick /// 'sizeof' unary-expression
753e5dd7070Spatrick /// 'sizeof' '(' type-name ')'
754e5dd7070Spatrick /// [C++11] 'sizeof' '...' '(' identifier ')'
755e5dd7070Spatrick /// [GNU] '__alignof' unary-expression
756e5dd7070Spatrick /// [GNU] '__alignof' '(' type-name ')'
757e5dd7070Spatrick /// [C11] '_Alignof' '(' type-name ')'
758e5dd7070Spatrick /// [C++11] 'alignof' '(' type-id ')'
759e5dd7070Spatrick /// [GNU] '&&' identifier
760e5dd7070Spatrick /// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7]
761e5dd7070Spatrick /// [C++] new-expression
762e5dd7070Spatrick /// [C++] delete-expression
763e5dd7070Spatrick ///
764e5dd7070Spatrick /// unary-operator: one of
765e5dd7070Spatrick /// '&' '*' '+' '-' '~' '!'
766e5dd7070Spatrick /// [GNU] '__extension__' '__real' '__imag'
767e5dd7070Spatrick ///
768e5dd7070Spatrick /// primary-expression: [C99 6.5.1]
769e5dd7070Spatrick /// [C99] identifier
770e5dd7070Spatrick /// [C++] id-expression
771e5dd7070Spatrick /// constant
772e5dd7070Spatrick /// string-literal
773e5dd7070Spatrick /// [C++] boolean-literal [C++ 2.13.5]
774e5dd7070Spatrick /// [C++11] 'nullptr' [C++11 2.14.7]
775e5dd7070Spatrick /// [C++11] user-defined-literal
776e5dd7070Spatrick /// '(' expression ')'
777e5dd7070Spatrick /// [C11] generic-selection
778e5dd7070Spatrick /// [C++2a] requires-expression
779e5dd7070Spatrick /// '__func__' [C99 6.4.2.2]
780e5dd7070Spatrick /// [GNU] '__FUNCTION__'
781e5dd7070Spatrick /// [MS] '__FUNCDNAME__'
782e5dd7070Spatrick /// [MS] 'L__FUNCTION__'
783e5dd7070Spatrick /// [MS] '__FUNCSIG__'
784e5dd7070Spatrick /// [MS] 'L__FUNCSIG__'
785e5dd7070Spatrick /// [GNU] '__PRETTY_FUNCTION__'
786e5dd7070Spatrick /// [GNU] '(' compound-statement ')'
787e5dd7070Spatrick /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
788e5dd7070Spatrick /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
789e5dd7070Spatrick /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
790e5dd7070Spatrick /// assign-expr ')'
791e5dd7070Spatrick /// [GNU] '__builtin_FILE' '(' ')'
792e5dd7070Spatrick /// [GNU] '__builtin_FUNCTION' '(' ')'
793e5dd7070Spatrick /// [GNU] '__builtin_LINE' '(' ')'
794e5dd7070Spatrick /// [CLANG] '__builtin_COLUMN' '(' ')'
795*12c85518Srobert /// [GNU] '__builtin_source_location' '(' ')'
796e5dd7070Spatrick /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
797e5dd7070Spatrick /// [GNU] '__null'
798e5dd7070Spatrick /// [OBJC] '[' objc-message-expr ']'
799e5dd7070Spatrick /// [OBJC] '\@selector' '(' objc-selector-arg ')'
800e5dd7070Spatrick /// [OBJC] '\@protocol' '(' identifier ')'
801e5dd7070Spatrick /// [OBJC] '\@encode' '(' type-name ')'
802e5dd7070Spatrick /// [OBJC] objc-string-literal
803e5dd7070Spatrick /// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
804e5dd7070Spatrick /// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3]
805e5dd7070Spatrick /// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
806e5dd7070Spatrick /// [C++11] typename-specifier braced-init-list [C++11 5.2.3]
807e5dd7070Spatrick /// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
808e5dd7070Spatrick /// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
809e5dd7070Spatrick /// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
810e5dd7070Spatrick /// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
811e5dd7070Spatrick /// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
812e5dd7070Spatrick /// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
813e5dd7070Spatrick /// [C++] 'this' [C++ 9.3.2]
814e5dd7070Spatrick /// [G++] unary-type-trait '(' type-id ')'
815e5dd7070Spatrick /// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
816e5dd7070Spatrick /// [EMBT] array-type-trait '(' type-id ',' integer ')'
817e5dd7070Spatrick /// [clang] '^' block-literal
818e5dd7070Spatrick ///
819e5dd7070Spatrick /// constant: [C99 6.4.4]
820e5dd7070Spatrick /// integer-constant
821e5dd7070Spatrick /// floating-constant
822e5dd7070Spatrick /// enumeration-constant -> identifier
823e5dd7070Spatrick /// character-constant
824e5dd7070Spatrick ///
825e5dd7070Spatrick /// id-expression: [C++ 5.1]
826e5dd7070Spatrick /// unqualified-id
827e5dd7070Spatrick /// qualified-id
828e5dd7070Spatrick ///
829e5dd7070Spatrick /// unqualified-id: [C++ 5.1]
830e5dd7070Spatrick /// identifier
831e5dd7070Spatrick /// operator-function-id
832e5dd7070Spatrick /// conversion-function-id
833e5dd7070Spatrick /// '~' class-name
834e5dd7070Spatrick /// template-id
835e5dd7070Spatrick ///
836e5dd7070Spatrick /// new-expression: [C++ 5.3.4]
837e5dd7070Spatrick /// '::'[opt] 'new' new-placement[opt] new-type-id
838e5dd7070Spatrick /// new-initializer[opt]
839e5dd7070Spatrick /// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
840e5dd7070Spatrick /// new-initializer[opt]
841e5dd7070Spatrick ///
842e5dd7070Spatrick /// delete-expression: [C++ 5.3.5]
843e5dd7070Spatrick /// '::'[opt] 'delete' cast-expression
844e5dd7070Spatrick /// '::'[opt] 'delete' '[' ']' cast-expression
845e5dd7070Spatrick ///
846e5dd7070Spatrick /// [GNU/Embarcadero] unary-type-trait:
847e5dd7070Spatrick /// '__is_arithmetic'
848e5dd7070Spatrick /// '__is_floating_point'
849e5dd7070Spatrick /// '__is_integral'
850e5dd7070Spatrick /// '__is_lvalue_expr'
851e5dd7070Spatrick /// '__is_rvalue_expr'
852e5dd7070Spatrick /// '__is_complete_type'
853e5dd7070Spatrick /// '__is_void'
854e5dd7070Spatrick /// '__is_array'
855e5dd7070Spatrick /// '__is_function'
856e5dd7070Spatrick /// '__is_reference'
857e5dd7070Spatrick /// '__is_lvalue_reference'
858e5dd7070Spatrick /// '__is_rvalue_reference'
859e5dd7070Spatrick /// '__is_fundamental'
860e5dd7070Spatrick /// '__is_object'
861e5dd7070Spatrick /// '__is_scalar'
862e5dd7070Spatrick /// '__is_compound'
863e5dd7070Spatrick /// '__is_pointer'
864e5dd7070Spatrick /// '__is_member_object_pointer'
865e5dd7070Spatrick /// '__is_member_function_pointer'
866e5dd7070Spatrick /// '__is_member_pointer'
867e5dd7070Spatrick /// '__is_const'
868e5dd7070Spatrick /// '__is_volatile'
869e5dd7070Spatrick /// '__is_trivial'
870e5dd7070Spatrick /// '__is_standard_layout'
871e5dd7070Spatrick /// '__is_signed'
872e5dd7070Spatrick /// '__is_unsigned'
873e5dd7070Spatrick ///
874e5dd7070Spatrick /// [GNU] unary-type-trait:
875e5dd7070Spatrick /// '__has_nothrow_assign'
876e5dd7070Spatrick /// '__has_nothrow_copy'
877e5dd7070Spatrick /// '__has_nothrow_constructor'
878e5dd7070Spatrick /// '__has_trivial_assign' [TODO]
879e5dd7070Spatrick /// '__has_trivial_copy' [TODO]
880e5dd7070Spatrick /// '__has_trivial_constructor'
881e5dd7070Spatrick /// '__has_trivial_destructor'
882e5dd7070Spatrick /// '__has_virtual_destructor'
883e5dd7070Spatrick /// '__is_abstract' [TODO]
884e5dd7070Spatrick /// '__is_class'
885e5dd7070Spatrick /// '__is_empty' [TODO]
886e5dd7070Spatrick /// '__is_enum'
887e5dd7070Spatrick /// '__is_final'
888e5dd7070Spatrick /// '__is_pod'
889e5dd7070Spatrick /// '__is_polymorphic'
890e5dd7070Spatrick /// '__is_sealed' [MS]
891e5dd7070Spatrick /// '__is_trivial'
892e5dd7070Spatrick /// '__is_union'
893e5dd7070Spatrick /// '__has_unique_object_representations'
894e5dd7070Spatrick ///
895e5dd7070Spatrick /// [Clang] unary-type-trait:
896e5dd7070Spatrick /// '__is_aggregate'
897e5dd7070Spatrick /// '__trivially_copyable'
898e5dd7070Spatrick ///
899e5dd7070Spatrick /// binary-type-trait:
900e5dd7070Spatrick /// [GNU] '__is_base_of'
901e5dd7070Spatrick /// [MS] '__is_convertible_to'
902e5dd7070Spatrick /// '__is_convertible'
903e5dd7070Spatrick /// '__is_same'
904e5dd7070Spatrick ///
905e5dd7070Spatrick /// [Embarcadero] array-type-trait:
906e5dd7070Spatrick /// '__array_rank'
907e5dd7070Spatrick /// '__array_extent'
908e5dd7070Spatrick ///
909e5dd7070Spatrick /// [Embarcadero] expression-trait:
910e5dd7070Spatrick /// '__is_lvalue_expr'
911e5dd7070Spatrick /// '__is_rvalue_expr'
912e5dd7070Spatrick /// \endverbatim
913e5dd7070Spatrick ///
ParseCastExpression(CastParseKind ParseKind,bool isAddressOfOperand,bool & NotCastExpr,TypeCastState isTypeCast,bool isVectorLiteral,bool * NotPrimaryExpression)914e5dd7070Spatrick ExprResult Parser::ParseCastExpression(CastParseKind ParseKind,
915e5dd7070Spatrick bool isAddressOfOperand,
916e5dd7070Spatrick bool &NotCastExpr,
917e5dd7070Spatrick TypeCastState isTypeCast,
918e5dd7070Spatrick bool isVectorLiteral,
919e5dd7070Spatrick bool *NotPrimaryExpression) {
920e5dd7070Spatrick ExprResult Res;
921e5dd7070Spatrick tok::TokenKind SavedKind = Tok.getKind();
922e5dd7070Spatrick auto SavedType = PreferredType;
923e5dd7070Spatrick NotCastExpr = false;
924e5dd7070Spatrick
925ec727ea7Spatrick // Are postfix-expression suffix operators permitted after this
926ec727ea7Spatrick // cast-expression? If not, and we find some, we'll parse them anyway and
927ec727ea7Spatrick // diagnose them.
928ec727ea7Spatrick bool AllowSuffix = true;
929ec727ea7Spatrick
930e5dd7070Spatrick // This handles all of cast-expression, unary-expression, postfix-expression,
931e5dd7070Spatrick // and primary-expression. We handle them together like this for efficiency
932e5dd7070Spatrick // and to simplify handling of an expression starting with a '(' token: which
933e5dd7070Spatrick // may be one of a parenthesized expression, cast-expression, compound literal
934e5dd7070Spatrick // expression, or statement expression.
935e5dd7070Spatrick //
936e5dd7070Spatrick // If the parsed tokens consist of a primary-expression, the cases below
937e5dd7070Spatrick // break out of the switch; at the end we call ParsePostfixExpressionSuffix
938e5dd7070Spatrick // to handle the postfix expression suffixes. Cases that cannot be followed
939ec727ea7Spatrick // by postfix exprs should set AllowSuffix to false.
940e5dd7070Spatrick switch (SavedKind) {
941e5dd7070Spatrick case tok::l_paren: {
942e5dd7070Spatrick // If this expression is limited to being a unary-expression, the paren can
943e5dd7070Spatrick // not start a cast expression.
944e5dd7070Spatrick ParenParseOption ParenExprType;
945e5dd7070Spatrick switch (ParseKind) {
946e5dd7070Spatrick case CastParseKind::UnaryExprOnly:
947*12c85518Srobert assert(getLangOpts().CPlusPlus && "not possible to get here in C");
948*12c85518Srobert [[fallthrough]];
949e5dd7070Spatrick case CastParseKind::AnyCastExpr:
950e5dd7070Spatrick ParenExprType = ParenParseOption::CastExpr;
951e5dd7070Spatrick break;
952e5dd7070Spatrick case CastParseKind::PrimaryExprOnly:
953e5dd7070Spatrick ParenExprType = FoldExpr;
954e5dd7070Spatrick break;
955e5dd7070Spatrick }
956e5dd7070Spatrick ParsedType CastTy;
957e5dd7070Spatrick SourceLocation RParenLoc;
958e5dd7070Spatrick Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
959e5dd7070Spatrick isTypeCast == IsTypeCast, CastTy, RParenLoc);
960e5dd7070Spatrick
961ec727ea7Spatrick // FIXME: What should we do if a vector literal is followed by a
962ec727ea7Spatrick // postfix-expression suffix? Usually postfix operators are permitted on
963ec727ea7Spatrick // literals.
964e5dd7070Spatrick if (isVectorLiteral)
965e5dd7070Spatrick return Res;
966e5dd7070Spatrick
967e5dd7070Spatrick switch (ParenExprType) {
968e5dd7070Spatrick case SimpleExpr: break; // Nothing else to do.
969e5dd7070Spatrick case CompoundStmt: break; // Nothing else to do.
970e5dd7070Spatrick case CompoundLiteral:
971e5dd7070Spatrick // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
972e5dd7070Spatrick // postfix-expression exist, parse them now.
973e5dd7070Spatrick break;
974e5dd7070Spatrick case CastExpr:
975e5dd7070Spatrick // We have parsed the cast-expression and no postfix-expr pieces are
976e5dd7070Spatrick // following.
977e5dd7070Spatrick return Res;
978e5dd7070Spatrick case FoldExpr:
979e5dd7070Spatrick // We only parsed a fold-expression. There might be postfix-expr pieces
980e5dd7070Spatrick // afterwards; parse them now.
981e5dd7070Spatrick break;
982e5dd7070Spatrick }
983e5dd7070Spatrick
984e5dd7070Spatrick break;
985e5dd7070Spatrick }
986e5dd7070Spatrick
987e5dd7070Spatrick // primary-expression
988e5dd7070Spatrick case tok::numeric_constant:
989e5dd7070Spatrick // constant: integer-constant
990e5dd7070Spatrick // constant: floating-constant
991e5dd7070Spatrick
992e5dd7070Spatrick Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope());
993e5dd7070Spatrick ConsumeToken();
994e5dd7070Spatrick break;
995e5dd7070Spatrick
996e5dd7070Spatrick case tok::kw_true:
997e5dd7070Spatrick case tok::kw_false:
998e5dd7070Spatrick Res = ParseCXXBoolLiteral();
999e5dd7070Spatrick break;
1000e5dd7070Spatrick
1001e5dd7070Spatrick case tok::kw___objc_yes:
1002e5dd7070Spatrick case tok::kw___objc_no:
1003ec727ea7Spatrick Res = ParseObjCBoolLiteral();
1004ec727ea7Spatrick break;
1005e5dd7070Spatrick
1006e5dd7070Spatrick case tok::kw_nullptr:
1007*12c85518Srobert if (getLangOpts().CPlusPlus)
1008e5dd7070Spatrick Diag(Tok, diag::warn_cxx98_compat_nullptr);
1009*12c85518Srobert else
1010*12c85518Srobert Diag(Tok, getLangOpts().C2x ? diag::warn_c17_compat_nullptr
1011*12c85518Srobert : diag::ext_c_nullptr);
1012*12c85518Srobert
1013ec727ea7Spatrick Res = Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
1014ec727ea7Spatrick break;
1015e5dd7070Spatrick
1016e5dd7070Spatrick case tok::annot_primary_expr:
1017a9ac8606Spatrick case tok::annot_overload_set:
1018e5dd7070Spatrick Res = getExprAnnotation(Tok);
1019a9ac8606Spatrick if (!Res.isInvalid() && Tok.getKind() == tok::annot_overload_set)
1020a9ac8606Spatrick Res = Actions.ActOnNameClassifiedAsOverloadSet(getCurScope(), Res.get());
1021e5dd7070Spatrick ConsumeAnnotationToken();
1022e5dd7070Spatrick if (!Res.isInvalid() && Tok.is(tok::less))
1023e5dd7070Spatrick checkPotentialAngleBracket(Res);
1024e5dd7070Spatrick break;
1025e5dd7070Spatrick
1026e5dd7070Spatrick case tok::annot_non_type:
1027e5dd7070Spatrick case tok::annot_non_type_dependent:
1028e5dd7070Spatrick case tok::annot_non_type_undeclared: {
1029e5dd7070Spatrick CXXScopeSpec SS;
1030e5dd7070Spatrick Token Replacement;
1031e5dd7070Spatrick Res = tryParseCXXIdExpression(SS, isAddressOfOperand, Replacement);
1032e5dd7070Spatrick assert(!Res.isUnset() &&
1033e5dd7070Spatrick "should not perform typo correction on annotation token");
1034e5dd7070Spatrick break;
1035e5dd7070Spatrick }
1036e5dd7070Spatrick
1037e5dd7070Spatrick case tok::kw___super:
1038e5dd7070Spatrick case tok::kw_decltype:
1039e5dd7070Spatrick // Annotate the token and tail recurse.
1040e5dd7070Spatrick if (TryAnnotateTypeOrScopeToken())
1041e5dd7070Spatrick return ExprError();
1042e5dd7070Spatrick assert(Tok.isNot(tok::kw_decltype) && Tok.isNot(tok::kw___super));
1043e5dd7070Spatrick return ParseCastExpression(ParseKind, isAddressOfOperand, isTypeCast,
1044e5dd7070Spatrick isVectorLiteral, NotPrimaryExpression);
1045e5dd7070Spatrick
1046*12c85518Srobert case tok::identifier:
1047*12c85518Srobert ParseIdentifier: { // primary-expression: identifier
1048e5dd7070Spatrick // unqualified-id: identifier
1049e5dd7070Spatrick // constant: enumeration-constant
1050e5dd7070Spatrick // Turn a potentially qualified name into a annot_typename or
1051e5dd7070Spatrick // annot_cxxscope if it would be valid. This handles things like x::y, etc.
1052e5dd7070Spatrick if (getLangOpts().CPlusPlus) {
1053e5dd7070Spatrick // Avoid the unnecessary parse-time lookup in the common case
1054e5dd7070Spatrick // where the syntax forbids a type.
1055e5dd7070Spatrick const Token &Next = NextToken();
1056e5dd7070Spatrick
1057e5dd7070Spatrick // If this identifier was reverted from a token ID, and the next token
1058e5dd7070Spatrick // is a parenthesis, this is likely to be a use of a type trait. Check
1059e5dd7070Spatrick // those tokens.
1060e5dd7070Spatrick if (Next.is(tok::l_paren) &&
1061e5dd7070Spatrick Tok.is(tok::identifier) &&
1062e5dd7070Spatrick Tok.getIdentifierInfo()->hasRevertedTokenIDToIdentifier()) {
1063e5dd7070Spatrick IdentifierInfo *II = Tok.getIdentifierInfo();
1064e5dd7070Spatrick // Build up the mapping of revertible type traits, for future use.
1065e5dd7070Spatrick if (RevertibleTypeTraits.empty()) {
1066e5dd7070Spatrick #define RTT_JOIN(X,Y) X##Y
1067e5dd7070Spatrick #define REVERTIBLE_TYPE_TRAIT(Name) \
1068e5dd7070Spatrick RevertibleTypeTraits[PP.getIdentifierInfo(#Name)] \
1069e5dd7070Spatrick = RTT_JOIN(tok::kw_,Name)
1070e5dd7070Spatrick
1071e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_abstract);
1072e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_aggregate);
1073e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_arithmetic);
1074e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_array);
1075e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_assignable);
1076e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_base_of);
1077*12c85518Srobert REVERTIBLE_TYPE_TRAIT(__is_bounded_array);
1078e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_class);
1079e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_complete_type);
1080e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_compound);
1081e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_const);
1082e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_constructible);
1083e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_convertible);
1084e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_convertible_to);
1085e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_destructible);
1086e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_empty);
1087e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_enum);
1088e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_floating_point);
1089e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_final);
1090e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_function);
1091e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_fundamental);
1092e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_integral);
1093e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_interface_class);
1094e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_literal);
1095e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_lvalue_expr);
1096e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_lvalue_reference);
1097e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_member_function_pointer);
1098e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_member_object_pointer);
1099e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_member_pointer);
1100e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_nothrow_assignable);
1101e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_nothrow_constructible);
1102e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_nothrow_destructible);
1103*12c85518Srobert REVERTIBLE_TYPE_TRAIT(__is_nullptr);
1104e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_object);
1105e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_pod);
1106e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_pointer);
1107e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_polymorphic);
1108e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_reference);
1109*12c85518Srobert REVERTIBLE_TYPE_TRAIT(__is_referenceable);
1110e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_rvalue_expr);
1111e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_rvalue_reference);
1112e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_same);
1113e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_scalar);
1114*12c85518Srobert REVERTIBLE_TYPE_TRAIT(__is_scoped_enum);
1115e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_sealed);
1116e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_signed);
1117e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_standard_layout);
1118e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_trivial);
1119e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_trivially_assignable);
1120e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_trivially_constructible);
1121e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_trivially_copyable);
1122*12c85518Srobert REVERTIBLE_TYPE_TRAIT(__is_unbounded_array);
1123e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_union);
1124e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_unsigned);
1125e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_void);
1126e5dd7070Spatrick REVERTIBLE_TYPE_TRAIT(__is_volatile);
1127*12c85518Srobert #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) \
1128*12c85518Srobert REVERTIBLE_TYPE_TRAIT(RTT_JOIN(__, Trait));
1129*12c85518Srobert #include "clang/Basic/TransformTypeTraits.def"
1130e5dd7070Spatrick #undef REVERTIBLE_TYPE_TRAIT
1131e5dd7070Spatrick #undef RTT_JOIN
1132e5dd7070Spatrick }
1133e5dd7070Spatrick
1134e5dd7070Spatrick // If we find that this is in fact the name of a type trait,
1135e5dd7070Spatrick // update the token kind in place and parse again to treat it as
1136e5dd7070Spatrick // the appropriate kind of type trait.
1137e5dd7070Spatrick llvm::SmallDenseMap<IdentifierInfo *, tok::TokenKind>::iterator Known
1138e5dd7070Spatrick = RevertibleTypeTraits.find(II);
1139e5dd7070Spatrick if (Known != RevertibleTypeTraits.end()) {
1140e5dd7070Spatrick Tok.setKind(Known->second);
1141e5dd7070Spatrick return ParseCastExpression(ParseKind, isAddressOfOperand,
1142e5dd7070Spatrick NotCastExpr, isTypeCast,
1143e5dd7070Spatrick isVectorLiteral, NotPrimaryExpression);
1144e5dd7070Spatrick }
1145e5dd7070Spatrick }
1146e5dd7070Spatrick
1147e5dd7070Spatrick if ((!ColonIsSacred && Next.is(tok::colon)) ||
1148e5dd7070Spatrick Next.isOneOf(tok::coloncolon, tok::less, tok::l_paren,
1149e5dd7070Spatrick tok::l_brace)) {
1150e5dd7070Spatrick // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1151e5dd7070Spatrick if (TryAnnotateTypeOrScopeToken())
1152e5dd7070Spatrick return ExprError();
1153e5dd7070Spatrick if (!Tok.is(tok::identifier))
1154e5dd7070Spatrick return ParseCastExpression(ParseKind, isAddressOfOperand,
1155e5dd7070Spatrick NotCastExpr, isTypeCast,
1156e5dd7070Spatrick isVectorLiteral,
1157e5dd7070Spatrick NotPrimaryExpression);
1158e5dd7070Spatrick }
1159e5dd7070Spatrick }
1160e5dd7070Spatrick
1161e5dd7070Spatrick // Consume the identifier so that we can see if it is followed by a '(' or
1162e5dd7070Spatrick // '.'.
1163e5dd7070Spatrick IdentifierInfo &II = *Tok.getIdentifierInfo();
1164e5dd7070Spatrick SourceLocation ILoc = ConsumeToken();
1165e5dd7070Spatrick
1166e5dd7070Spatrick // Support 'Class.property' and 'super.property' notation.
1167e5dd7070Spatrick if (getLangOpts().ObjC && Tok.is(tok::period) &&
1168e5dd7070Spatrick (Actions.getTypeName(II, ILoc, getCurScope()) ||
1169e5dd7070Spatrick // Allow the base to be 'super' if in an objc-method.
1170e5dd7070Spatrick (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
1171e5dd7070Spatrick ConsumeToken();
1172e5dd7070Spatrick
1173e5dd7070Spatrick if (Tok.is(tok::code_completion) && &II != Ident_super) {
1174a9ac8606Spatrick cutOffParsing();
1175e5dd7070Spatrick Actions.CodeCompleteObjCClassPropertyRefExpr(
1176e5dd7070Spatrick getCurScope(), II, ILoc, ExprStatementTokLoc == ILoc);
1177e5dd7070Spatrick return ExprError();
1178e5dd7070Spatrick }
1179e5dd7070Spatrick // Allow either an identifier or the keyword 'class' (in C++).
1180e5dd7070Spatrick if (Tok.isNot(tok::identifier) &&
1181e5dd7070Spatrick !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
1182e5dd7070Spatrick Diag(Tok, diag::err_expected_property_name);
1183e5dd7070Spatrick return ExprError();
1184e5dd7070Spatrick }
1185e5dd7070Spatrick IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
1186e5dd7070Spatrick SourceLocation PropertyLoc = ConsumeToken();
1187e5dd7070Spatrick
1188e5dd7070Spatrick Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
1189e5dd7070Spatrick ILoc, PropertyLoc);
1190e5dd7070Spatrick break;
1191e5dd7070Spatrick }
1192e5dd7070Spatrick
1193e5dd7070Spatrick // In an Objective-C method, if we have "super" followed by an identifier,
1194e5dd7070Spatrick // the token sequence is ill-formed. However, if there's a ':' or ']' after
1195e5dd7070Spatrick // that identifier, this is probably a message send with a missing open
1196e5dd7070Spatrick // bracket. Treat it as such.
1197e5dd7070Spatrick if (getLangOpts().ObjC && &II == Ident_super && !InMessageExpression &&
1198e5dd7070Spatrick getCurScope()->isInObjcMethodScope() &&
1199e5dd7070Spatrick ((Tok.is(tok::identifier) &&
1200e5dd7070Spatrick (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
1201e5dd7070Spatrick Tok.is(tok::code_completion))) {
1202e5dd7070Spatrick Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, nullptr,
1203e5dd7070Spatrick nullptr);
1204e5dd7070Spatrick break;
1205e5dd7070Spatrick }
1206e5dd7070Spatrick
1207e5dd7070Spatrick // If we have an Objective-C class name followed by an identifier
1208e5dd7070Spatrick // and either ':' or ']', this is an Objective-C class message
1209e5dd7070Spatrick // send that's missing the opening '['. Recovery
1210e5dd7070Spatrick // appropriately. Also take this path if we're performing code
1211e5dd7070Spatrick // completion after an Objective-C class name.
1212e5dd7070Spatrick if (getLangOpts().ObjC &&
1213e5dd7070Spatrick ((Tok.is(tok::identifier) && !InMessageExpression) ||
1214e5dd7070Spatrick Tok.is(tok::code_completion))) {
1215e5dd7070Spatrick const Token& Next = NextToken();
1216e5dd7070Spatrick if (Tok.is(tok::code_completion) ||
1217e5dd7070Spatrick Next.is(tok::colon) || Next.is(tok::r_square))
1218e5dd7070Spatrick if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
1219e5dd7070Spatrick if (Typ.get()->isObjCObjectOrInterfaceType()) {
1220e5dd7070Spatrick // Fake up a Declarator to use with ActOnTypeName.
1221e5dd7070Spatrick DeclSpec DS(AttrFactory);
1222e5dd7070Spatrick DS.SetRangeStart(ILoc);
1223e5dd7070Spatrick DS.SetRangeEnd(ILoc);
1224e5dd7070Spatrick const char *PrevSpec = nullptr;
1225e5dd7070Spatrick unsigned DiagID;
1226e5dd7070Spatrick DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ,
1227e5dd7070Spatrick Actions.getASTContext().getPrintingPolicy());
1228e5dd7070Spatrick
1229*12c85518Srobert Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
1230*12c85518Srobert DeclaratorContext::TypeName);
1231e5dd7070Spatrick TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
1232e5dd7070Spatrick DeclaratorInfo);
1233e5dd7070Spatrick if (Ty.isInvalid())
1234e5dd7070Spatrick break;
1235e5dd7070Spatrick
1236e5dd7070Spatrick Res = ParseObjCMessageExpressionBody(SourceLocation(),
1237e5dd7070Spatrick SourceLocation(),
1238e5dd7070Spatrick Ty.get(), nullptr);
1239e5dd7070Spatrick break;
1240e5dd7070Spatrick }
1241e5dd7070Spatrick }
1242e5dd7070Spatrick
1243e5dd7070Spatrick // Make sure to pass down the right value for isAddressOfOperand.
1244e5dd7070Spatrick if (isAddressOfOperand && isPostfixExpressionSuffixStart())
1245e5dd7070Spatrick isAddressOfOperand = false;
1246e5dd7070Spatrick
1247e5dd7070Spatrick // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
1248e5dd7070Spatrick // need to know whether or not this identifier is a function designator or
1249e5dd7070Spatrick // not.
1250e5dd7070Spatrick UnqualifiedId Name;
1251e5dd7070Spatrick CXXScopeSpec ScopeSpec;
1252e5dd7070Spatrick SourceLocation TemplateKWLoc;
1253e5dd7070Spatrick Token Replacement;
1254e5dd7070Spatrick CastExpressionIdValidator Validator(
1255e5dd7070Spatrick /*Next=*/Tok,
1256e5dd7070Spatrick /*AllowTypes=*/isTypeCast != NotTypeCast,
1257e5dd7070Spatrick /*AllowNonTypes=*/isTypeCast != IsTypeCast);
1258e5dd7070Spatrick Validator.IsAddressOfOperand = isAddressOfOperand;
1259e5dd7070Spatrick if (Tok.isOneOf(tok::periodstar, tok::arrowstar)) {
1260e5dd7070Spatrick Validator.WantExpressionKeywords = false;
1261e5dd7070Spatrick Validator.WantRemainingKeywords = false;
1262e5dd7070Spatrick } else {
1263e5dd7070Spatrick Validator.WantRemainingKeywords = Tok.isNot(tok::r_paren);
1264e5dd7070Spatrick }
1265e5dd7070Spatrick Name.setIdentifier(&II, ILoc);
1266e5dd7070Spatrick Res = Actions.ActOnIdExpression(
1267e5dd7070Spatrick getCurScope(), ScopeSpec, TemplateKWLoc, Name, Tok.is(tok::l_paren),
1268e5dd7070Spatrick isAddressOfOperand, &Validator,
1269e5dd7070Spatrick /*IsInlineAsmIdentifier=*/false,
1270e5dd7070Spatrick Tok.is(tok::r_paren) ? nullptr : &Replacement);
1271e5dd7070Spatrick if (!Res.isInvalid() && Res.isUnset()) {
1272e5dd7070Spatrick UnconsumeToken(Replacement);
1273e5dd7070Spatrick return ParseCastExpression(ParseKind, isAddressOfOperand,
1274e5dd7070Spatrick NotCastExpr, isTypeCast,
1275e5dd7070Spatrick /*isVectorLiteral=*/false,
1276e5dd7070Spatrick NotPrimaryExpression);
1277e5dd7070Spatrick }
1278e5dd7070Spatrick if (!Res.isInvalid() && Tok.is(tok::less))
1279e5dd7070Spatrick checkPotentialAngleBracket(Res);
1280e5dd7070Spatrick break;
1281e5dd7070Spatrick }
1282e5dd7070Spatrick case tok::char_constant: // constant: character-constant
1283e5dd7070Spatrick case tok::wide_char_constant:
1284e5dd7070Spatrick case tok::utf8_char_constant:
1285e5dd7070Spatrick case tok::utf16_char_constant:
1286e5dd7070Spatrick case tok::utf32_char_constant:
1287e5dd7070Spatrick Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope());
1288e5dd7070Spatrick ConsumeToken();
1289e5dd7070Spatrick break;
1290e5dd7070Spatrick case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
1291e5dd7070Spatrick case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
1292e5dd7070Spatrick case tok::kw___FUNCDNAME__: // primary-expression: __FUNCDNAME__ [MS]
1293e5dd7070Spatrick case tok::kw___FUNCSIG__: // primary-expression: __FUNCSIG__ [MS]
1294e5dd7070Spatrick case tok::kw_L__FUNCTION__: // primary-expression: L__FUNCTION__ [MS]
1295e5dd7070Spatrick case tok::kw_L__FUNCSIG__: // primary-expression: L__FUNCSIG__ [MS]
1296e5dd7070Spatrick case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
1297e5dd7070Spatrick Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
1298e5dd7070Spatrick ConsumeToken();
1299e5dd7070Spatrick break;
1300e5dd7070Spatrick case tok::string_literal: // primary-expression: string-literal
1301e5dd7070Spatrick case tok::wide_string_literal:
1302e5dd7070Spatrick case tok::utf8_string_literal:
1303e5dd7070Spatrick case tok::utf16_string_literal:
1304e5dd7070Spatrick case tok::utf32_string_literal:
1305e5dd7070Spatrick Res = ParseStringLiteralExpression(true);
1306e5dd7070Spatrick break;
1307e5dd7070Spatrick case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
1308e5dd7070Spatrick Res = ParseGenericSelectionExpression();
1309e5dd7070Spatrick break;
1310e5dd7070Spatrick case tok::kw___builtin_available:
1311ec727ea7Spatrick Res = ParseAvailabilityCheckExpr(Tok.getLocation());
1312ec727ea7Spatrick break;
1313e5dd7070Spatrick case tok::kw___builtin_va_arg:
1314e5dd7070Spatrick case tok::kw___builtin_offsetof:
1315e5dd7070Spatrick case tok::kw___builtin_choose_expr:
1316e5dd7070Spatrick case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
1317e5dd7070Spatrick case tok::kw___builtin_convertvector:
1318e5dd7070Spatrick case tok::kw___builtin_COLUMN:
1319e5dd7070Spatrick case tok::kw___builtin_FILE:
1320e5dd7070Spatrick case tok::kw___builtin_FUNCTION:
1321e5dd7070Spatrick case tok::kw___builtin_LINE:
1322*12c85518Srobert case tok::kw___builtin_source_location:
1323e5dd7070Spatrick if (NotPrimaryExpression)
1324e5dd7070Spatrick *NotPrimaryExpression = true;
1325ec727ea7Spatrick // This parses the complete suffix; we can return early.
1326e5dd7070Spatrick return ParseBuiltinPrimaryExpression();
1327e5dd7070Spatrick case tok::kw___null:
1328ec727ea7Spatrick Res = Actions.ActOnGNUNullExpr(ConsumeToken());
1329ec727ea7Spatrick break;
1330e5dd7070Spatrick
1331e5dd7070Spatrick case tok::plusplus: // unary-expression: '++' unary-expression [C99]
1332e5dd7070Spatrick case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
1333e5dd7070Spatrick if (NotPrimaryExpression)
1334e5dd7070Spatrick *NotPrimaryExpression = true;
1335e5dd7070Spatrick // C++ [expr.unary] has:
1336e5dd7070Spatrick // unary-expression:
1337e5dd7070Spatrick // ++ cast-expression
1338e5dd7070Spatrick // -- cast-expression
1339e5dd7070Spatrick Token SavedTok = Tok;
1340e5dd7070Spatrick ConsumeToken();
1341e5dd7070Spatrick
1342e5dd7070Spatrick PreferredType.enterUnary(Actions, Tok.getLocation(), SavedTok.getKind(),
1343e5dd7070Spatrick SavedTok.getLocation());
1344e5dd7070Spatrick // One special case is implicitly handled here: if the preceding tokens are
1345e5dd7070Spatrick // an ambiguous cast expression, such as "(T())++", then we recurse to
1346e5dd7070Spatrick // determine whether the '++' is prefix or postfix.
1347e5dd7070Spatrick Res = ParseCastExpression(getLangOpts().CPlusPlus ?
1348e5dd7070Spatrick UnaryExprOnly : AnyCastExpr,
1349e5dd7070Spatrick /*isAddressOfOperand*/false, NotCastExpr,
1350e5dd7070Spatrick NotTypeCast);
1351e5dd7070Spatrick if (NotCastExpr) {
1352e5dd7070Spatrick // If we return with NotCastExpr = true, we must not consume any tokens,
1353e5dd7070Spatrick // so put the token back where we found it.
1354e5dd7070Spatrick assert(Res.isInvalid());
1355e5dd7070Spatrick UnconsumeToken(SavedTok);
1356e5dd7070Spatrick return ExprError();
1357e5dd7070Spatrick }
1358ec727ea7Spatrick if (!Res.isInvalid()) {
1359ec727ea7Spatrick Expr *Arg = Res.get();
1360e5dd7070Spatrick Res = Actions.ActOnUnaryOp(getCurScope(), SavedTok.getLocation(),
1361ec727ea7Spatrick SavedKind, Arg);
1362ec727ea7Spatrick if (Res.isInvalid())
1363ec727ea7Spatrick Res = Actions.CreateRecoveryExpr(SavedTok.getLocation(),
1364ec727ea7Spatrick Arg->getEndLoc(), Arg);
1365ec727ea7Spatrick }
1366e5dd7070Spatrick return Res;
1367e5dd7070Spatrick }
1368e5dd7070Spatrick case tok::amp: { // unary-expression: '&' cast-expression
1369e5dd7070Spatrick if (NotPrimaryExpression)
1370e5dd7070Spatrick *NotPrimaryExpression = true;
1371e5dd7070Spatrick // Special treatment because of member pointers
1372e5dd7070Spatrick SourceLocation SavedLoc = ConsumeToken();
1373e5dd7070Spatrick PreferredType.enterUnary(Actions, Tok.getLocation(), tok::amp, SavedLoc);
1374*12c85518Srobert
1375*12c85518Srobert Res = ParseCastExpression(AnyCastExpr, /*isAddressOfOperand=*/true);
1376ec727ea7Spatrick if (!Res.isInvalid()) {
1377ec727ea7Spatrick Expr *Arg = Res.get();
1378ec727ea7Spatrick Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Arg);
1379ec727ea7Spatrick if (Res.isInvalid())
1380ec727ea7Spatrick Res = Actions.CreateRecoveryExpr(Tok.getLocation(), Arg->getEndLoc(),
1381ec727ea7Spatrick Arg);
1382ec727ea7Spatrick }
1383e5dd7070Spatrick return Res;
1384e5dd7070Spatrick }
1385e5dd7070Spatrick
1386e5dd7070Spatrick case tok::star: // unary-expression: '*' cast-expression
1387e5dd7070Spatrick case tok::plus: // unary-expression: '+' cast-expression
1388e5dd7070Spatrick case tok::minus: // unary-expression: '-' cast-expression
1389e5dd7070Spatrick case tok::tilde: // unary-expression: '~' cast-expression
1390e5dd7070Spatrick case tok::exclaim: // unary-expression: '!' cast-expression
1391e5dd7070Spatrick case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
1392e5dd7070Spatrick case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
1393e5dd7070Spatrick if (NotPrimaryExpression)
1394e5dd7070Spatrick *NotPrimaryExpression = true;
1395e5dd7070Spatrick SourceLocation SavedLoc = ConsumeToken();
1396e5dd7070Spatrick PreferredType.enterUnary(Actions, Tok.getLocation(), SavedKind, SavedLoc);
1397e5dd7070Spatrick Res = ParseCastExpression(AnyCastExpr);
1398ec727ea7Spatrick if (!Res.isInvalid()) {
1399ec727ea7Spatrick Expr *Arg = Res.get();
1400*12c85518Srobert Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Arg,
1401*12c85518Srobert isAddressOfOperand);
1402ec727ea7Spatrick if (Res.isInvalid())
1403ec727ea7Spatrick Res = Actions.CreateRecoveryExpr(SavedLoc, Arg->getEndLoc(), Arg);
1404ec727ea7Spatrick }
1405e5dd7070Spatrick return Res;
1406e5dd7070Spatrick }
1407e5dd7070Spatrick
1408e5dd7070Spatrick case tok::kw_co_await: { // unary-expression: 'co_await' cast-expression
1409e5dd7070Spatrick if (NotPrimaryExpression)
1410e5dd7070Spatrick *NotPrimaryExpression = true;
1411e5dd7070Spatrick SourceLocation CoawaitLoc = ConsumeToken();
1412e5dd7070Spatrick Res = ParseCastExpression(AnyCastExpr);
1413e5dd7070Spatrick if (!Res.isInvalid())
1414e5dd7070Spatrick Res = Actions.ActOnCoawaitExpr(getCurScope(), CoawaitLoc, Res.get());
1415e5dd7070Spatrick return Res;
1416e5dd7070Spatrick }
1417e5dd7070Spatrick
1418e5dd7070Spatrick case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
1419e5dd7070Spatrick // __extension__ silences extension warnings in the subexpression.
1420e5dd7070Spatrick if (NotPrimaryExpression)
1421e5dd7070Spatrick *NotPrimaryExpression = true;
1422e5dd7070Spatrick ExtensionRAIIObject O(Diags); // Use RAII to do this.
1423e5dd7070Spatrick SourceLocation SavedLoc = ConsumeToken();
1424e5dd7070Spatrick Res = ParseCastExpression(AnyCastExpr);
1425e5dd7070Spatrick if (!Res.isInvalid())
1426e5dd7070Spatrick Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
1427e5dd7070Spatrick return Res;
1428e5dd7070Spatrick }
1429e5dd7070Spatrick case tok::kw__Alignof: // unary-expression: '_Alignof' '(' type-name ')'
1430e5dd7070Spatrick if (!getLangOpts().C11)
1431e5dd7070Spatrick Diag(Tok, diag::ext_c11_feature) << Tok.getName();
1432*12c85518Srobert [[fallthrough]];
1433e5dd7070Spatrick case tok::kw_alignof: // unary-expression: 'alignof' '(' type-id ')'
1434e5dd7070Spatrick case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
1435e5dd7070Spatrick // unary-expression: '__alignof' '(' type-name ')'
1436e5dd7070Spatrick case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
1437e5dd7070Spatrick // unary-expression: 'sizeof' '(' type-name ')'
1438e5dd7070Spatrick case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
1439e5dd7070Spatrick // unary-expression: '__builtin_omp_required_simd_align' '(' type-name ')'
1440e5dd7070Spatrick case tok::kw___builtin_omp_required_simd_align:
1441e5dd7070Spatrick if (NotPrimaryExpression)
1442e5dd7070Spatrick *NotPrimaryExpression = true;
1443ec727ea7Spatrick AllowSuffix = false;
1444ec727ea7Spatrick Res = ParseUnaryExprOrTypeTraitExpression();
1445ec727ea7Spatrick break;
1446e5dd7070Spatrick case tok::ampamp: { // unary-expression: '&&' identifier
1447e5dd7070Spatrick if (NotPrimaryExpression)
1448e5dd7070Spatrick *NotPrimaryExpression = true;
1449e5dd7070Spatrick SourceLocation AmpAmpLoc = ConsumeToken();
1450e5dd7070Spatrick if (Tok.isNot(tok::identifier))
1451e5dd7070Spatrick return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
1452e5dd7070Spatrick
1453e5dd7070Spatrick if (getCurScope()->getFnParent() == nullptr)
1454e5dd7070Spatrick return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
1455e5dd7070Spatrick
1456e5dd7070Spatrick Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
1457e5dd7070Spatrick LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1458e5dd7070Spatrick Tok.getLocation());
1459e5dd7070Spatrick Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
1460e5dd7070Spatrick ConsumeToken();
1461ec727ea7Spatrick AllowSuffix = false;
1462ec727ea7Spatrick break;
1463e5dd7070Spatrick }
1464e5dd7070Spatrick case tok::kw_const_cast:
1465e5dd7070Spatrick case tok::kw_dynamic_cast:
1466e5dd7070Spatrick case tok::kw_reinterpret_cast:
1467e5dd7070Spatrick case tok::kw_static_cast:
1468ec727ea7Spatrick case tok::kw_addrspace_cast:
1469e5dd7070Spatrick if (NotPrimaryExpression)
1470e5dd7070Spatrick *NotPrimaryExpression = true;
1471e5dd7070Spatrick Res = ParseCXXCasts();
1472e5dd7070Spatrick break;
1473e5dd7070Spatrick case tok::kw___builtin_bit_cast:
1474e5dd7070Spatrick if (NotPrimaryExpression)
1475e5dd7070Spatrick *NotPrimaryExpression = true;
1476e5dd7070Spatrick Res = ParseBuiltinBitCast();
1477e5dd7070Spatrick break;
1478e5dd7070Spatrick case tok::kw_typeid:
1479e5dd7070Spatrick if (NotPrimaryExpression)
1480e5dd7070Spatrick *NotPrimaryExpression = true;
1481e5dd7070Spatrick Res = ParseCXXTypeid();
1482e5dd7070Spatrick break;
1483e5dd7070Spatrick case tok::kw___uuidof:
1484e5dd7070Spatrick if (NotPrimaryExpression)
1485e5dd7070Spatrick *NotPrimaryExpression = true;
1486e5dd7070Spatrick Res = ParseCXXUuidof();
1487e5dd7070Spatrick break;
1488e5dd7070Spatrick case tok::kw_this:
1489e5dd7070Spatrick Res = ParseCXXThis();
1490e5dd7070Spatrick break;
1491a9ac8606Spatrick case tok::kw___builtin_sycl_unique_stable_name:
1492a9ac8606Spatrick Res = ParseSYCLUniqueStableNameExpression();
1493ec727ea7Spatrick break;
1494a9ac8606Spatrick
1495e5dd7070Spatrick case tok::annot_typename:
1496e5dd7070Spatrick if (isStartOfObjCClassMessageMissingOpenBracket()) {
1497ec727ea7Spatrick TypeResult Type = getTypeAnnotation(Tok);
1498e5dd7070Spatrick
1499e5dd7070Spatrick // Fake up a Declarator to use with ActOnTypeName.
1500e5dd7070Spatrick DeclSpec DS(AttrFactory);
1501e5dd7070Spatrick DS.SetRangeStart(Tok.getLocation());
1502e5dd7070Spatrick DS.SetRangeEnd(Tok.getLastLoc());
1503e5dd7070Spatrick
1504e5dd7070Spatrick const char *PrevSpec = nullptr;
1505e5dd7070Spatrick unsigned DiagID;
1506e5dd7070Spatrick DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
1507e5dd7070Spatrick PrevSpec, DiagID, Type,
1508e5dd7070Spatrick Actions.getASTContext().getPrintingPolicy());
1509e5dd7070Spatrick
1510*12c85518Srobert Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
1511*12c85518Srobert DeclaratorContext::TypeName);
1512e5dd7070Spatrick TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1513e5dd7070Spatrick if (Ty.isInvalid())
1514e5dd7070Spatrick break;
1515e5dd7070Spatrick
1516e5dd7070Spatrick ConsumeAnnotationToken();
1517e5dd7070Spatrick Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1518e5dd7070Spatrick Ty.get(), nullptr);
1519e5dd7070Spatrick break;
1520e5dd7070Spatrick }
1521*12c85518Srobert [[fallthrough]];
1522e5dd7070Spatrick
1523e5dd7070Spatrick case tok::annot_decltype:
1524e5dd7070Spatrick case tok::kw_char:
1525e5dd7070Spatrick case tok::kw_wchar_t:
1526e5dd7070Spatrick case tok::kw_char8_t:
1527e5dd7070Spatrick case tok::kw_char16_t:
1528e5dd7070Spatrick case tok::kw_char32_t:
1529e5dd7070Spatrick case tok::kw_bool:
1530e5dd7070Spatrick case tok::kw_short:
1531e5dd7070Spatrick case tok::kw_int:
1532e5dd7070Spatrick case tok::kw_long:
1533e5dd7070Spatrick case tok::kw___int64:
1534e5dd7070Spatrick case tok::kw___int128:
1535ec727ea7Spatrick case tok::kw__ExtInt:
1536*12c85518Srobert case tok::kw__BitInt:
1537e5dd7070Spatrick case tok::kw_signed:
1538e5dd7070Spatrick case tok::kw_unsigned:
1539e5dd7070Spatrick case tok::kw_half:
1540e5dd7070Spatrick case tok::kw_float:
1541e5dd7070Spatrick case tok::kw_double:
1542ec727ea7Spatrick case tok::kw___bf16:
1543e5dd7070Spatrick case tok::kw__Float16:
1544e5dd7070Spatrick case tok::kw___float128:
1545*12c85518Srobert case tok::kw___ibm128:
1546e5dd7070Spatrick case tok::kw_void:
1547*12c85518Srobert case tok::kw_auto:
1548e5dd7070Spatrick case tok::kw_typename:
1549e5dd7070Spatrick case tok::kw_typeof:
1550e5dd7070Spatrick case tok::kw___vector:
1551e5dd7070Spatrick #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1552e5dd7070Spatrick #include "clang/Basic/OpenCLImageTypes.def"
1553e5dd7070Spatrick {
1554e5dd7070Spatrick if (!getLangOpts().CPlusPlus) {
1555e5dd7070Spatrick Diag(Tok, diag::err_expected_expression);
1556e5dd7070Spatrick return ExprError();
1557e5dd7070Spatrick }
1558e5dd7070Spatrick
1559e5dd7070Spatrick // Everything henceforth is a postfix-expression.
1560e5dd7070Spatrick if (NotPrimaryExpression)
1561e5dd7070Spatrick *NotPrimaryExpression = true;
1562e5dd7070Spatrick
1563e5dd7070Spatrick if (SavedKind == tok::kw_typename) {
1564e5dd7070Spatrick // postfix-expression: typename-specifier '(' expression-list[opt] ')'
1565e5dd7070Spatrick // typename-specifier braced-init-list
1566e5dd7070Spatrick if (TryAnnotateTypeOrScopeToken())
1567e5dd7070Spatrick return ExprError();
1568e5dd7070Spatrick
1569e5dd7070Spatrick if (!Actions.isSimpleTypeSpecifier(Tok.getKind()))
1570e5dd7070Spatrick // We are trying to parse a simple-type-specifier but might not get such
1571e5dd7070Spatrick // a token after error recovery.
1572e5dd7070Spatrick return ExprError();
1573e5dd7070Spatrick }
1574e5dd7070Spatrick
1575e5dd7070Spatrick // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
1576e5dd7070Spatrick // simple-type-specifier braced-init-list
1577e5dd7070Spatrick //
1578e5dd7070Spatrick DeclSpec DS(AttrFactory);
1579e5dd7070Spatrick
1580e5dd7070Spatrick ParseCXXSimpleTypeSpecifier(DS);
1581e5dd7070Spatrick if (Tok.isNot(tok::l_paren) &&
1582e5dd7070Spatrick (!getLangOpts().CPlusPlus11 || Tok.isNot(tok::l_brace)))
1583e5dd7070Spatrick return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1584e5dd7070Spatrick << DS.getSourceRange());
1585e5dd7070Spatrick
1586e5dd7070Spatrick if (Tok.is(tok::l_brace))
1587e5dd7070Spatrick Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1588e5dd7070Spatrick
1589e5dd7070Spatrick Res = ParseCXXTypeConstructExpression(DS);
1590e5dd7070Spatrick break;
1591e5dd7070Spatrick }
1592e5dd7070Spatrick
1593e5dd7070Spatrick case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
1594e5dd7070Spatrick // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1595e5dd7070Spatrick // (We can end up in this situation after tentative parsing.)
1596e5dd7070Spatrick if (TryAnnotateTypeOrScopeToken())
1597e5dd7070Spatrick return ExprError();
1598e5dd7070Spatrick if (!Tok.is(tok::annot_cxxscope))
1599e5dd7070Spatrick return ParseCastExpression(ParseKind, isAddressOfOperand, NotCastExpr,
1600e5dd7070Spatrick isTypeCast, isVectorLiteral,
1601e5dd7070Spatrick NotPrimaryExpression);
1602e5dd7070Spatrick
1603e5dd7070Spatrick Token Next = NextToken();
1604e5dd7070Spatrick if (Next.is(tok::annot_template_id)) {
1605e5dd7070Spatrick TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
1606e5dd7070Spatrick if (TemplateId->Kind == TNK_Type_template) {
1607e5dd7070Spatrick // We have a qualified template-id that we know refers to a
1608e5dd7070Spatrick // type, translate it into a type and continue parsing as a
1609e5dd7070Spatrick // cast expression.
1610e5dd7070Spatrick CXXScopeSpec SS;
1611ec727ea7Spatrick ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
1612*12c85518Srobert /*ObjectHasErrors=*/false,
1613e5dd7070Spatrick /*EnteringContext=*/false);
1614*12c85518Srobert AnnotateTemplateIdTokenAsType(SS, ImplicitTypenameContext::Yes);
1615e5dd7070Spatrick return ParseCastExpression(ParseKind, isAddressOfOperand, NotCastExpr,
1616e5dd7070Spatrick isTypeCast, isVectorLiteral,
1617e5dd7070Spatrick NotPrimaryExpression);
1618e5dd7070Spatrick }
1619e5dd7070Spatrick }
1620e5dd7070Spatrick
1621e5dd7070Spatrick // Parse as an id-expression.
1622e5dd7070Spatrick Res = ParseCXXIdExpression(isAddressOfOperand);
1623e5dd7070Spatrick break;
1624e5dd7070Spatrick }
1625e5dd7070Spatrick
1626e5dd7070Spatrick case tok::annot_template_id: { // [C++] template-id
1627e5dd7070Spatrick TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1628e5dd7070Spatrick if (TemplateId->Kind == TNK_Type_template) {
1629e5dd7070Spatrick // We have a template-id that we know refers to a type,
1630e5dd7070Spatrick // translate it into a type and continue parsing as a cast
1631e5dd7070Spatrick // expression.
1632e5dd7070Spatrick CXXScopeSpec SS;
1633*12c85518Srobert AnnotateTemplateIdTokenAsType(SS, ImplicitTypenameContext::Yes);
1634e5dd7070Spatrick return ParseCastExpression(ParseKind, isAddressOfOperand,
1635e5dd7070Spatrick NotCastExpr, isTypeCast, isVectorLiteral,
1636e5dd7070Spatrick NotPrimaryExpression);
1637e5dd7070Spatrick }
1638e5dd7070Spatrick
1639e5dd7070Spatrick // Fall through to treat the template-id as an id-expression.
1640*12c85518Srobert [[fallthrough]];
1641e5dd7070Spatrick }
1642e5dd7070Spatrick
1643e5dd7070Spatrick case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
1644e5dd7070Spatrick Res = ParseCXXIdExpression(isAddressOfOperand);
1645e5dd7070Spatrick break;
1646e5dd7070Spatrick
1647e5dd7070Spatrick case tok::coloncolon: {
1648e5dd7070Spatrick // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1649e5dd7070Spatrick // annotates the token, tail recurse.
1650e5dd7070Spatrick if (TryAnnotateTypeOrScopeToken())
1651e5dd7070Spatrick return ExprError();
1652e5dd7070Spatrick if (!Tok.is(tok::coloncolon))
1653e5dd7070Spatrick return ParseCastExpression(ParseKind, isAddressOfOperand, isTypeCast,
1654e5dd7070Spatrick isVectorLiteral, NotPrimaryExpression);
1655e5dd7070Spatrick
1656e5dd7070Spatrick // ::new -> [C++] new-expression
1657e5dd7070Spatrick // ::delete -> [C++] delete-expression
1658e5dd7070Spatrick SourceLocation CCLoc = ConsumeToken();
1659e5dd7070Spatrick if (Tok.is(tok::kw_new)) {
1660e5dd7070Spatrick if (NotPrimaryExpression)
1661e5dd7070Spatrick *NotPrimaryExpression = true;
1662ec727ea7Spatrick Res = ParseCXXNewExpression(true, CCLoc);
1663ec727ea7Spatrick AllowSuffix = false;
1664ec727ea7Spatrick break;
1665e5dd7070Spatrick }
1666e5dd7070Spatrick if (Tok.is(tok::kw_delete)) {
1667e5dd7070Spatrick if (NotPrimaryExpression)
1668e5dd7070Spatrick *NotPrimaryExpression = true;
1669ec727ea7Spatrick Res = ParseCXXDeleteExpression(true, CCLoc);
1670ec727ea7Spatrick AllowSuffix = false;
1671ec727ea7Spatrick break;
1672e5dd7070Spatrick }
1673e5dd7070Spatrick
1674e5dd7070Spatrick // This is not a type name or scope specifier, it is an invalid expression.
1675e5dd7070Spatrick Diag(CCLoc, diag::err_expected_expression);
1676e5dd7070Spatrick return ExprError();
1677e5dd7070Spatrick }
1678e5dd7070Spatrick
1679e5dd7070Spatrick case tok::kw_new: // [C++] new-expression
1680e5dd7070Spatrick if (NotPrimaryExpression)
1681e5dd7070Spatrick *NotPrimaryExpression = true;
1682ec727ea7Spatrick Res = ParseCXXNewExpression(false, Tok.getLocation());
1683ec727ea7Spatrick AllowSuffix = false;
1684ec727ea7Spatrick break;
1685e5dd7070Spatrick
1686e5dd7070Spatrick case tok::kw_delete: // [C++] delete-expression
1687e5dd7070Spatrick if (NotPrimaryExpression)
1688e5dd7070Spatrick *NotPrimaryExpression = true;
1689ec727ea7Spatrick Res = ParseCXXDeleteExpression(false, Tok.getLocation());
1690ec727ea7Spatrick AllowSuffix = false;
1691ec727ea7Spatrick break;
1692e5dd7070Spatrick
1693e5dd7070Spatrick case tok::kw_requires: // [C++2a] requires-expression
1694ec727ea7Spatrick Res = ParseRequiresExpression();
1695ec727ea7Spatrick AllowSuffix = false;
1696ec727ea7Spatrick break;
1697e5dd7070Spatrick
1698e5dd7070Spatrick case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
1699e5dd7070Spatrick if (NotPrimaryExpression)
1700e5dd7070Spatrick *NotPrimaryExpression = true;
1701e5dd7070Spatrick Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
1702e5dd7070Spatrick SourceLocation KeyLoc = ConsumeToken();
1703e5dd7070Spatrick BalancedDelimiterTracker T(*this, tok::l_paren);
1704e5dd7070Spatrick
1705e5dd7070Spatrick if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
1706e5dd7070Spatrick return ExprError();
1707e5dd7070Spatrick // C++11 [expr.unary.noexcept]p1:
1708e5dd7070Spatrick // The noexcept operator determines whether the evaluation of its operand,
1709e5dd7070Spatrick // which is an unevaluated operand, can throw an exception.
1710e5dd7070Spatrick EnterExpressionEvaluationContext Unevaluated(
1711e5dd7070Spatrick Actions, Sema::ExpressionEvaluationContext::Unevaluated);
1712ec727ea7Spatrick Res = ParseExpression();
1713e5dd7070Spatrick
1714e5dd7070Spatrick T.consumeClose();
1715e5dd7070Spatrick
1716ec727ea7Spatrick if (!Res.isInvalid())
1717ec727ea7Spatrick Res = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(), Res.get(),
1718ec727ea7Spatrick T.getCloseLocation());
1719ec727ea7Spatrick AllowSuffix = false;
1720ec727ea7Spatrick break;
1721e5dd7070Spatrick }
1722e5dd7070Spatrick
1723e5dd7070Spatrick #define TYPE_TRAIT(N,Spelling,K) \
1724e5dd7070Spatrick case tok::kw_##Spelling:
1725e5dd7070Spatrick #include "clang/Basic/TokenKinds.def"
1726ec727ea7Spatrick Res = ParseTypeTrait();
1727ec727ea7Spatrick break;
1728e5dd7070Spatrick
1729e5dd7070Spatrick case tok::kw___array_rank:
1730e5dd7070Spatrick case tok::kw___array_extent:
1731e5dd7070Spatrick if (NotPrimaryExpression)
1732e5dd7070Spatrick *NotPrimaryExpression = true;
1733ec727ea7Spatrick Res = ParseArrayTypeTrait();
1734ec727ea7Spatrick break;
1735e5dd7070Spatrick
1736e5dd7070Spatrick case tok::kw___is_lvalue_expr:
1737e5dd7070Spatrick case tok::kw___is_rvalue_expr:
1738e5dd7070Spatrick if (NotPrimaryExpression)
1739e5dd7070Spatrick *NotPrimaryExpression = true;
1740ec727ea7Spatrick Res = ParseExpressionTrait();
1741ec727ea7Spatrick break;
1742e5dd7070Spatrick
1743e5dd7070Spatrick case tok::at: {
1744e5dd7070Spatrick if (NotPrimaryExpression)
1745e5dd7070Spatrick *NotPrimaryExpression = true;
1746e5dd7070Spatrick SourceLocation AtLoc = ConsumeToken();
1747e5dd7070Spatrick return ParseObjCAtExpression(AtLoc);
1748e5dd7070Spatrick }
1749e5dd7070Spatrick case tok::caret:
1750e5dd7070Spatrick Res = ParseBlockLiteralExpression();
1751e5dd7070Spatrick break;
1752e5dd7070Spatrick case tok::code_completion: {
1753a9ac8606Spatrick cutOffParsing();
1754e5dd7070Spatrick Actions.CodeCompleteExpression(getCurScope(),
1755e5dd7070Spatrick PreferredType.get(Tok.getLocation()));
1756e5dd7070Spatrick return ExprError();
1757e5dd7070Spatrick }
1758*12c85518Srobert #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
1759*12c85518Srobert #include "clang/Basic/TransformTypeTraits.def"
1760*12c85518Srobert // HACK: libstdc++ uses some of the transform-type-traits as alias
1761*12c85518Srobert // templates, so we need to work around this.
1762*12c85518Srobert if (!NextToken().is(tok::l_paren)) {
1763*12c85518Srobert Tok.setKind(tok::identifier);
1764*12c85518Srobert Diag(Tok, diag::ext_keyword_as_ident)
1765*12c85518Srobert << Tok.getIdentifierInfo()->getName() << 0;
1766*12c85518Srobert goto ParseIdentifier;
1767*12c85518Srobert }
1768*12c85518Srobert goto ExpectedExpression;
1769e5dd7070Spatrick case tok::l_square:
1770e5dd7070Spatrick if (getLangOpts().CPlusPlus11) {
1771e5dd7070Spatrick if (getLangOpts().ObjC) {
1772e5dd7070Spatrick // C++11 lambda expressions and Objective-C message sends both start with a
1773e5dd7070Spatrick // square bracket. There are three possibilities here:
1774e5dd7070Spatrick // we have a valid lambda expression, we have an invalid lambda
1775e5dd7070Spatrick // expression, or we have something that doesn't appear to be a lambda.
1776e5dd7070Spatrick // If we're in the last case, we fall back to ParseObjCMessageExpression.
1777e5dd7070Spatrick Res = TryParseLambdaExpression();
1778e5dd7070Spatrick if (!Res.isInvalid() && !Res.get()) {
1779e5dd7070Spatrick // We assume Objective-C++ message expressions are not
1780e5dd7070Spatrick // primary-expressions.
1781e5dd7070Spatrick if (NotPrimaryExpression)
1782e5dd7070Spatrick *NotPrimaryExpression = true;
1783e5dd7070Spatrick Res = ParseObjCMessageExpression();
1784e5dd7070Spatrick }
1785e5dd7070Spatrick break;
1786e5dd7070Spatrick }
1787e5dd7070Spatrick Res = ParseLambdaExpression();
1788e5dd7070Spatrick break;
1789e5dd7070Spatrick }
1790e5dd7070Spatrick if (getLangOpts().ObjC) {
1791e5dd7070Spatrick Res = ParseObjCMessageExpression();
1792e5dd7070Spatrick break;
1793e5dd7070Spatrick }
1794*12c85518Srobert [[fallthrough]];
1795e5dd7070Spatrick default:
1796*12c85518Srobert ExpectedExpression:
1797e5dd7070Spatrick NotCastExpr = true;
1798e5dd7070Spatrick return ExprError();
1799e5dd7070Spatrick }
1800e5dd7070Spatrick
1801e5dd7070Spatrick // Check to see whether Res is a function designator only. If it is and we
1802e5dd7070Spatrick // are compiling for OpenCL, we need to return an error as this implies
1803e5dd7070Spatrick // that the address of the function is being taken, which is illegal in CL.
1804e5dd7070Spatrick
1805e5dd7070Spatrick if (ParseKind == PrimaryExprOnly)
1806e5dd7070Spatrick // This is strictly a primary-expression - no postfix-expr pieces should be
1807e5dd7070Spatrick // parsed.
1808e5dd7070Spatrick return Res;
1809e5dd7070Spatrick
1810ec727ea7Spatrick if (!AllowSuffix) {
1811ec727ea7Spatrick // FIXME: Don't parse a primary-expression suffix if we encountered a parse
1812ec727ea7Spatrick // error already.
1813ec727ea7Spatrick if (Res.isInvalid())
1814ec727ea7Spatrick return Res;
1815ec727ea7Spatrick
1816ec727ea7Spatrick switch (Tok.getKind()) {
1817ec727ea7Spatrick case tok::l_square:
1818ec727ea7Spatrick case tok::l_paren:
1819ec727ea7Spatrick case tok::plusplus:
1820ec727ea7Spatrick case tok::minusminus:
1821ec727ea7Spatrick // "expected ';'" or similar is probably the right diagnostic here. Let
1822ec727ea7Spatrick // the caller decide what to do.
1823ec727ea7Spatrick if (Tok.isAtStartOfLine())
1824ec727ea7Spatrick return Res;
1825ec727ea7Spatrick
1826*12c85518Srobert [[fallthrough]];
1827ec727ea7Spatrick case tok::period:
1828ec727ea7Spatrick case tok::arrow:
1829ec727ea7Spatrick break;
1830ec727ea7Spatrick
1831ec727ea7Spatrick default:
1832ec727ea7Spatrick return Res;
1833ec727ea7Spatrick }
1834ec727ea7Spatrick
1835ec727ea7Spatrick // This was a unary-expression for which a postfix-expression suffix is
1836ec727ea7Spatrick // not permitted by the grammar (eg, a sizeof expression or
1837ec727ea7Spatrick // new-expression or similar). Diagnose but parse the suffix anyway.
1838ec727ea7Spatrick Diag(Tok.getLocation(), diag::err_postfix_after_unary_requires_parens)
1839ec727ea7Spatrick << Tok.getKind() << Res.get()->getSourceRange()
1840ec727ea7Spatrick << FixItHint::CreateInsertion(Res.get()->getBeginLoc(), "(")
1841ec727ea7Spatrick << FixItHint::CreateInsertion(PP.getLocForEndOfToken(PrevTokLocation),
1842ec727ea7Spatrick ")");
1843ec727ea7Spatrick }
1844ec727ea7Spatrick
1845e5dd7070Spatrick // These can be followed by postfix-expr pieces.
1846e5dd7070Spatrick PreferredType = SavedType;
1847e5dd7070Spatrick Res = ParsePostfixExpressionSuffix(Res);
1848a9ac8606Spatrick if (getLangOpts().OpenCL &&
1849a9ac8606Spatrick !getActions().getOpenCLOptions().isAvailableOption(
1850a9ac8606Spatrick "__cl_clang_function_pointers", getLangOpts()))
1851e5dd7070Spatrick if (Expr *PostfixExpr = Res.get()) {
1852e5dd7070Spatrick QualType Ty = PostfixExpr->getType();
1853e5dd7070Spatrick if (!Ty.isNull() && Ty->isFunctionType()) {
1854e5dd7070Spatrick Diag(PostfixExpr->getExprLoc(),
1855e5dd7070Spatrick diag::err_opencl_taking_function_address_parser);
1856e5dd7070Spatrick return ExprError();
1857e5dd7070Spatrick }
1858e5dd7070Spatrick }
1859e5dd7070Spatrick
1860e5dd7070Spatrick return Res;
1861e5dd7070Spatrick }
1862e5dd7070Spatrick
1863e5dd7070Spatrick /// Once the leading part of a postfix-expression is parsed, this
1864e5dd7070Spatrick /// method parses any suffixes that apply.
1865e5dd7070Spatrick ///
1866e5dd7070Spatrick /// \verbatim
1867e5dd7070Spatrick /// postfix-expression: [C99 6.5.2]
1868e5dd7070Spatrick /// primary-expression
1869e5dd7070Spatrick /// postfix-expression '[' expression ']'
1870e5dd7070Spatrick /// postfix-expression '[' braced-init-list ']'
1871*12c85518Srobert /// postfix-expression '[' expression-list [opt] ']' [C++2b 12.4.5]
1872e5dd7070Spatrick /// postfix-expression '(' argument-expression-list[opt] ')'
1873e5dd7070Spatrick /// postfix-expression '.' identifier
1874e5dd7070Spatrick /// postfix-expression '->' identifier
1875e5dd7070Spatrick /// postfix-expression '++'
1876e5dd7070Spatrick /// postfix-expression '--'
1877e5dd7070Spatrick /// '(' type-name ')' '{' initializer-list '}'
1878e5dd7070Spatrick /// '(' type-name ')' '{' initializer-list ',' '}'
1879e5dd7070Spatrick ///
1880e5dd7070Spatrick /// argument-expression-list: [C99 6.5.2]
1881e5dd7070Spatrick /// argument-expression ...[opt]
1882e5dd7070Spatrick /// argument-expression-list ',' assignment-expression ...[opt]
1883e5dd7070Spatrick /// \endverbatim
1884e5dd7070Spatrick ExprResult
ParsePostfixExpressionSuffix(ExprResult LHS)1885e5dd7070Spatrick Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
1886e5dd7070Spatrick // Now that the primary-expression piece of the postfix-expression has been
1887e5dd7070Spatrick // parsed, see if there are any postfix-expression pieces here.
1888e5dd7070Spatrick SourceLocation Loc;
1889e5dd7070Spatrick auto SavedType = PreferredType;
1890*12c85518Srobert while (true) {
1891e5dd7070Spatrick // Each iteration relies on preferred type for the whole expression.
1892e5dd7070Spatrick PreferredType = SavedType;
1893e5dd7070Spatrick switch (Tok.getKind()) {
1894e5dd7070Spatrick case tok::code_completion:
1895e5dd7070Spatrick if (InMessageExpression)
1896e5dd7070Spatrick return LHS;
1897e5dd7070Spatrick
1898a9ac8606Spatrick cutOffParsing();
1899e5dd7070Spatrick Actions.CodeCompletePostfixExpression(
1900e5dd7070Spatrick getCurScope(), LHS, PreferredType.get(Tok.getLocation()));
1901e5dd7070Spatrick return ExprError();
1902e5dd7070Spatrick
1903e5dd7070Spatrick case tok::identifier:
1904e5dd7070Spatrick // If we see identifier: after an expression, and we're not already in a
1905e5dd7070Spatrick // message send, then this is probably a message send with a missing
1906e5dd7070Spatrick // opening bracket '['.
1907e5dd7070Spatrick if (getLangOpts().ObjC && !InMessageExpression &&
1908e5dd7070Spatrick (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
1909e5dd7070Spatrick LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1910e5dd7070Spatrick nullptr, LHS.get());
1911e5dd7070Spatrick break;
1912e5dd7070Spatrick }
1913e5dd7070Spatrick // Fall through; this isn't a message send.
1914*12c85518Srobert [[fallthrough]];
1915e5dd7070Spatrick
1916e5dd7070Spatrick default: // Not a postfix-expression suffix.
1917e5dd7070Spatrick return LHS;
1918e5dd7070Spatrick case tok::l_square: { // postfix-expression: p-e '[' expression ']'
1919e5dd7070Spatrick // If we have a array postfix expression that starts on a new line and
1920e5dd7070Spatrick // Objective-C is enabled, it is highly likely that the user forgot a
1921e5dd7070Spatrick // semicolon after the base expression and that the array postfix-expr is
1922e5dd7070Spatrick // actually another message send. In this case, do some look-ahead to see
1923e5dd7070Spatrick // if the contents of the square brackets are obviously not a valid
1924e5dd7070Spatrick // expression and recover by pretending there is no suffix.
1925e5dd7070Spatrick if (getLangOpts().ObjC && Tok.isAtStartOfLine() &&
1926e5dd7070Spatrick isSimpleObjCMessageExpression())
1927e5dd7070Spatrick return LHS;
1928e5dd7070Spatrick
1929e5dd7070Spatrick // Reject array indices starting with a lambda-expression. '[[' is
1930e5dd7070Spatrick // reserved for attributes.
1931e5dd7070Spatrick if (CheckProhibitedCXX11Attribute()) {
1932e5dd7070Spatrick (void)Actions.CorrectDelayedTyposInExpr(LHS);
1933e5dd7070Spatrick return ExprError();
1934e5dd7070Spatrick }
1935e5dd7070Spatrick BalancedDelimiterTracker T(*this, tok::l_square);
1936e5dd7070Spatrick T.consumeOpen();
1937e5dd7070Spatrick Loc = T.getOpenLocation();
1938*12c85518Srobert ExprResult Length, Stride;
1939ec727ea7Spatrick SourceLocation ColonLocFirst, ColonLocSecond;
1940*12c85518Srobert ExprVector ArgExprs;
1941*12c85518Srobert bool HasError = false;
1942e5dd7070Spatrick PreferredType.enterSubscript(Actions, Tok.getLocation(), LHS.get());
1943*12c85518Srobert
1944*12c85518Srobert // We try to parse a list of indexes in all language mode first
1945*12c85518Srobert // and, in we find 0 or one index, we try to parse an OpenMP array
1946*12c85518Srobert // section. This allow us to support C++2b multi dimensional subscript and
1947*12c85518Srobert // OpenMp sections in the same language mode.
1948*12c85518Srobert if (!getLangOpts().OpenMP || Tok.isNot(tok::colon)) {
1949*12c85518Srobert if (!getLangOpts().CPlusPlus2b) {
1950*12c85518Srobert ExprResult Idx;
1951e5dd7070Spatrick if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
1952e5dd7070Spatrick Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1953e5dd7070Spatrick Idx = ParseBraceInitializer();
1954*12c85518Srobert } else {
1955*12c85518Srobert Idx = ParseExpression(); // May be a comma expression
1956e5dd7070Spatrick }
1957*12c85518Srobert LHS = Actions.CorrectDelayedTyposInExpr(LHS);
1958*12c85518Srobert Idx = Actions.CorrectDelayedTyposInExpr(Idx);
1959*12c85518Srobert if (Idx.isInvalid()) {
1960*12c85518Srobert HasError = true;
1961*12c85518Srobert } else {
1962*12c85518Srobert ArgExprs.push_back(Idx.get());
1963*12c85518Srobert }
1964*12c85518Srobert } else if (Tok.isNot(tok::r_square)) {
1965*12c85518Srobert if (ParseExpressionList(ArgExprs)) {
1966*12c85518Srobert LHS = Actions.CorrectDelayedTyposInExpr(LHS);
1967*12c85518Srobert HasError = true;
1968*12c85518Srobert }
1969*12c85518Srobert }
1970*12c85518Srobert }
1971*12c85518Srobert
1972*12c85518Srobert if (ArgExprs.size() <= 1 && getLangOpts().OpenMP) {
1973*12c85518Srobert ColonProtectionRAIIObject RAII(*this);
1974e5dd7070Spatrick if (Tok.is(tok::colon)) {
1975e5dd7070Spatrick // Consume ':'
1976ec727ea7Spatrick ColonLocFirst = ConsumeToken();
1977ec727ea7Spatrick if (Tok.isNot(tok::r_square) &&
1978ec727ea7Spatrick (getLangOpts().OpenMP < 50 ||
1979*12c85518Srobert ((Tok.isNot(tok::colon) && getLangOpts().OpenMP >= 50)))) {
1980e5dd7070Spatrick Length = ParseExpression();
1981*12c85518Srobert Length = Actions.CorrectDelayedTyposInExpr(Length);
1982*12c85518Srobert }
1983e5dd7070Spatrick }
1984ec727ea7Spatrick if (getLangOpts().OpenMP >= 50 &&
1985ec727ea7Spatrick (OMPClauseKind == llvm::omp::Clause::OMPC_to ||
1986ec727ea7Spatrick OMPClauseKind == llvm::omp::Clause::OMPC_from) &&
1987ec727ea7Spatrick Tok.is(tok::colon)) {
1988ec727ea7Spatrick // Consume ':'
1989ec727ea7Spatrick ColonLocSecond = ConsumeToken();
1990ec727ea7Spatrick if (Tok.isNot(tok::r_square)) {
1991ec727ea7Spatrick Stride = ParseExpression();
1992ec727ea7Spatrick }
1993ec727ea7Spatrick }
1994*12c85518Srobert }
1995e5dd7070Spatrick
1996e5dd7070Spatrick SourceLocation RLoc = Tok.getLocation();
1997e5dd7070Spatrick LHS = Actions.CorrectDelayedTyposInExpr(LHS);
1998*12c85518Srobert
1999*12c85518Srobert if (!LHS.isInvalid() && !HasError && !Length.isInvalid() &&
2000ec727ea7Spatrick !Stride.isInvalid() && Tok.is(tok::r_square)) {
2001ec727ea7Spatrick if (ColonLocFirst.isValid() || ColonLocSecond.isValid()) {
2002ec727ea7Spatrick LHS = Actions.ActOnOMPArraySectionExpr(
2003*12c85518Srobert LHS.get(), Loc, ArgExprs.empty() ? nullptr : ArgExprs[0],
2004*12c85518Srobert ColonLocFirst, ColonLocSecond, Length.get(), Stride.get(), RLoc);
2005e5dd7070Spatrick } else {
2006e5dd7070Spatrick LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.get(), Loc,
2007*12c85518Srobert ArgExprs, RLoc);
2008e5dd7070Spatrick }
2009e5dd7070Spatrick } else {
2010e5dd7070Spatrick LHS = ExprError();
2011e5dd7070Spatrick }
2012e5dd7070Spatrick
2013e5dd7070Spatrick // Match the ']'.
2014e5dd7070Spatrick T.consumeClose();
2015e5dd7070Spatrick break;
2016e5dd7070Spatrick }
2017e5dd7070Spatrick
2018e5dd7070Spatrick case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
2019e5dd7070Spatrick case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
2020e5dd7070Spatrick // '(' argument-expression-list[opt] ')'
2021e5dd7070Spatrick tok::TokenKind OpKind = Tok.getKind();
2022e5dd7070Spatrick InMessageExpressionRAIIObject InMessage(*this, false);
2023e5dd7070Spatrick
2024e5dd7070Spatrick Expr *ExecConfig = nullptr;
2025e5dd7070Spatrick
2026e5dd7070Spatrick BalancedDelimiterTracker PT(*this, tok::l_paren);
2027e5dd7070Spatrick
2028e5dd7070Spatrick if (OpKind == tok::lesslessless) {
2029e5dd7070Spatrick ExprVector ExecConfigExprs;
2030e5dd7070Spatrick SourceLocation OpenLoc = ConsumeToken();
2031e5dd7070Spatrick
2032*12c85518Srobert if (ParseSimpleExpressionList(ExecConfigExprs)) {
2033e5dd7070Spatrick (void)Actions.CorrectDelayedTyposInExpr(LHS);
2034e5dd7070Spatrick LHS = ExprError();
2035e5dd7070Spatrick }
2036e5dd7070Spatrick
2037e5dd7070Spatrick SourceLocation CloseLoc;
2038e5dd7070Spatrick if (TryConsumeToken(tok::greatergreatergreater, CloseLoc)) {
2039e5dd7070Spatrick } else if (LHS.isInvalid()) {
2040e5dd7070Spatrick SkipUntil(tok::greatergreatergreater, StopAtSemi);
2041e5dd7070Spatrick } else {
2042e5dd7070Spatrick // There was an error closing the brackets
2043e5dd7070Spatrick Diag(Tok, diag::err_expected) << tok::greatergreatergreater;
2044e5dd7070Spatrick Diag(OpenLoc, diag::note_matching) << tok::lesslessless;
2045e5dd7070Spatrick SkipUntil(tok::greatergreatergreater, StopAtSemi);
2046e5dd7070Spatrick LHS = ExprError();
2047e5dd7070Spatrick }
2048e5dd7070Spatrick
2049e5dd7070Spatrick if (!LHS.isInvalid()) {
2050e5dd7070Spatrick if (ExpectAndConsume(tok::l_paren))
2051e5dd7070Spatrick LHS = ExprError();
2052e5dd7070Spatrick else
2053e5dd7070Spatrick Loc = PrevTokLocation;
2054e5dd7070Spatrick }
2055e5dd7070Spatrick
2056e5dd7070Spatrick if (!LHS.isInvalid()) {
2057e5dd7070Spatrick ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
2058e5dd7070Spatrick OpenLoc,
2059e5dd7070Spatrick ExecConfigExprs,
2060e5dd7070Spatrick CloseLoc);
2061e5dd7070Spatrick if (ECResult.isInvalid())
2062e5dd7070Spatrick LHS = ExprError();
2063e5dd7070Spatrick else
2064e5dd7070Spatrick ExecConfig = ECResult.get();
2065e5dd7070Spatrick }
2066e5dd7070Spatrick } else {
2067e5dd7070Spatrick PT.consumeOpen();
2068e5dd7070Spatrick Loc = PT.getOpenLocation();
2069e5dd7070Spatrick }
2070e5dd7070Spatrick
2071e5dd7070Spatrick ExprVector ArgExprs;
2072e5dd7070Spatrick auto RunSignatureHelp = [&]() -> QualType {
2073e5dd7070Spatrick QualType PreferredType = Actions.ProduceCallSignatureHelp(
2074*12c85518Srobert LHS.get(), ArgExprs, PT.getOpenLocation());
2075e5dd7070Spatrick CalledSignatureHelp = true;
2076e5dd7070Spatrick return PreferredType;
2077e5dd7070Spatrick };
2078e5dd7070Spatrick if (OpKind == tok::l_paren || !LHS.isInvalid()) {
2079e5dd7070Spatrick if (Tok.isNot(tok::r_paren)) {
2080*12c85518Srobert if (ParseExpressionList(ArgExprs, [&] {
2081e5dd7070Spatrick PreferredType.enterFunctionArgument(Tok.getLocation(),
2082e5dd7070Spatrick RunSignatureHelp);
2083e5dd7070Spatrick })) {
2084e5dd7070Spatrick (void)Actions.CorrectDelayedTyposInExpr(LHS);
2085e5dd7070Spatrick // If we got an error when parsing expression list, we don't call
2086e5dd7070Spatrick // the CodeCompleteCall handler inside the parser. So call it here
2087e5dd7070Spatrick // to make sure we get overload suggestions even when we are in the
2088e5dd7070Spatrick // middle of a parameter.
2089e5dd7070Spatrick if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
2090e5dd7070Spatrick RunSignatureHelp();
2091e5dd7070Spatrick LHS = ExprError();
2092e5dd7070Spatrick } else if (LHS.isInvalid()) {
2093e5dd7070Spatrick for (auto &E : ArgExprs)
2094e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(E);
2095e5dd7070Spatrick }
2096e5dd7070Spatrick }
2097e5dd7070Spatrick }
2098e5dd7070Spatrick
2099e5dd7070Spatrick // Match the ')'.
2100e5dd7070Spatrick if (LHS.isInvalid()) {
2101e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2102e5dd7070Spatrick } else if (Tok.isNot(tok::r_paren)) {
2103e5dd7070Spatrick bool HadDelayedTypo = false;
2104e5dd7070Spatrick if (Actions.CorrectDelayedTyposInExpr(LHS).get() != LHS.get())
2105e5dd7070Spatrick HadDelayedTypo = true;
2106e5dd7070Spatrick for (auto &E : ArgExprs)
2107e5dd7070Spatrick if (Actions.CorrectDelayedTyposInExpr(E).get() != E)
2108e5dd7070Spatrick HadDelayedTypo = true;
2109e5dd7070Spatrick // If there were delayed typos in the LHS or ArgExprs, call SkipUntil
2110e5dd7070Spatrick // instead of PT.consumeClose() to avoid emitting extra diagnostics for
2111e5dd7070Spatrick // the unmatched l_paren.
2112e5dd7070Spatrick if (HadDelayedTypo)
2113e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2114e5dd7070Spatrick else
2115e5dd7070Spatrick PT.consumeClose();
2116e5dd7070Spatrick LHS = ExprError();
2117e5dd7070Spatrick } else {
2118ec727ea7Spatrick Expr *Fn = LHS.get();
2119ec727ea7Spatrick SourceLocation RParLoc = Tok.getLocation();
2120ec727ea7Spatrick LHS = Actions.ActOnCallExpr(getCurScope(), Fn, Loc, ArgExprs, RParLoc,
2121e5dd7070Spatrick ExecConfig);
2122ec727ea7Spatrick if (LHS.isInvalid()) {
2123ec727ea7Spatrick ArgExprs.insert(ArgExprs.begin(), Fn);
2124ec727ea7Spatrick LHS =
2125ec727ea7Spatrick Actions.CreateRecoveryExpr(Fn->getBeginLoc(), RParLoc, ArgExprs);
2126ec727ea7Spatrick }
2127e5dd7070Spatrick PT.consumeClose();
2128e5dd7070Spatrick }
2129e5dd7070Spatrick
2130e5dd7070Spatrick break;
2131e5dd7070Spatrick }
2132e5dd7070Spatrick case tok::arrow:
2133e5dd7070Spatrick case tok::period: {
2134e5dd7070Spatrick // postfix-expression: p-e '->' template[opt] id-expression
2135e5dd7070Spatrick // postfix-expression: p-e '.' template[opt] id-expression
2136e5dd7070Spatrick tok::TokenKind OpKind = Tok.getKind();
2137e5dd7070Spatrick SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
2138e5dd7070Spatrick
2139e5dd7070Spatrick CXXScopeSpec SS;
2140e5dd7070Spatrick ParsedType ObjectType;
2141e5dd7070Spatrick bool MayBePseudoDestructor = false;
2142e5dd7070Spatrick Expr* OrigLHS = !LHS.isInvalid() ? LHS.get() : nullptr;
2143e5dd7070Spatrick
2144e5dd7070Spatrick PreferredType.enterMemAccess(Actions, Tok.getLocation(), OrigLHS);
2145e5dd7070Spatrick
2146e5dd7070Spatrick if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
2147e5dd7070Spatrick Expr *Base = OrigLHS;
2148e5dd7070Spatrick const Type* BaseType = Base->getType().getTypePtrOrNull();
2149e5dd7070Spatrick if (BaseType && Tok.is(tok::l_paren) &&
2150e5dd7070Spatrick (BaseType->isFunctionType() ||
2151e5dd7070Spatrick BaseType->isSpecificPlaceholderType(BuiltinType::BoundMember))) {
2152e5dd7070Spatrick Diag(OpLoc, diag::err_function_is_not_record)
2153e5dd7070Spatrick << OpKind << Base->getSourceRange()
2154e5dd7070Spatrick << FixItHint::CreateRemoval(OpLoc);
2155e5dd7070Spatrick return ParsePostfixExpressionSuffix(Base);
2156e5dd7070Spatrick }
2157e5dd7070Spatrick
2158ec727ea7Spatrick LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), Base, OpLoc,
2159ec727ea7Spatrick OpKind, ObjectType,
2160e5dd7070Spatrick MayBePseudoDestructor);
2161ec727ea7Spatrick if (LHS.isInvalid()) {
2162ec727ea7Spatrick // Clang will try to perform expression based completion as a
2163ec727ea7Spatrick // fallback, which is confusing in case of member references. So we
2164ec727ea7Spatrick // stop here without any completions.
2165ec727ea7Spatrick if (Tok.is(tok::code_completion)) {
2166ec727ea7Spatrick cutOffParsing();
2167ec727ea7Spatrick return ExprError();
2168ec727ea7Spatrick }
2169e5dd7070Spatrick break;
2170ec727ea7Spatrick }
2171ec727ea7Spatrick ParseOptionalCXXScopeSpecifier(
2172ec727ea7Spatrick SS, ObjectType, LHS.get() && LHS.get()->containsErrors(),
2173ec727ea7Spatrick /*EnteringContext=*/false, &MayBePseudoDestructor);
2174e5dd7070Spatrick if (SS.isNotEmpty())
2175e5dd7070Spatrick ObjectType = nullptr;
2176e5dd7070Spatrick }
2177e5dd7070Spatrick
2178e5dd7070Spatrick if (Tok.is(tok::code_completion)) {
2179e5dd7070Spatrick tok::TokenKind CorrectedOpKind =
2180e5dd7070Spatrick OpKind == tok::arrow ? tok::period : tok::arrow;
2181e5dd7070Spatrick ExprResult CorrectedLHS(/*Invalid=*/true);
2182e5dd7070Spatrick if (getLangOpts().CPlusPlus && OrigLHS) {
2183e5dd7070Spatrick // FIXME: Creating a TentativeAnalysisScope from outside Sema is a
2184e5dd7070Spatrick // hack.
2185e5dd7070Spatrick Sema::TentativeAnalysisScope Trap(Actions);
2186e5dd7070Spatrick CorrectedLHS = Actions.ActOnStartCXXMemberReference(
2187e5dd7070Spatrick getCurScope(), OrigLHS, OpLoc, CorrectedOpKind, ObjectType,
2188e5dd7070Spatrick MayBePseudoDestructor);
2189e5dd7070Spatrick }
2190e5dd7070Spatrick
2191e5dd7070Spatrick Expr *Base = LHS.get();
2192e5dd7070Spatrick Expr *CorrectedBase = CorrectedLHS.get();
2193e5dd7070Spatrick if (!CorrectedBase && !getLangOpts().CPlusPlus)
2194e5dd7070Spatrick CorrectedBase = Base;
2195e5dd7070Spatrick
2196e5dd7070Spatrick // Code completion for a member access expression.
2197a9ac8606Spatrick cutOffParsing();
2198e5dd7070Spatrick Actions.CodeCompleteMemberReferenceExpr(
2199e5dd7070Spatrick getCurScope(), Base, CorrectedBase, OpLoc, OpKind == tok::arrow,
2200e5dd7070Spatrick Base && ExprStatementTokLoc == Base->getBeginLoc(),
2201e5dd7070Spatrick PreferredType.get(Tok.getLocation()));
2202e5dd7070Spatrick
2203e5dd7070Spatrick return ExprError();
2204e5dd7070Spatrick }
2205e5dd7070Spatrick
2206e5dd7070Spatrick if (MayBePseudoDestructor && !LHS.isInvalid()) {
2207e5dd7070Spatrick LHS = ParseCXXPseudoDestructor(LHS.get(), OpLoc, OpKind, SS,
2208e5dd7070Spatrick ObjectType);
2209e5dd7070Spatrick break;
2210e5dd7070Spatrick }
2211e5dd7070Spatrick
2212e5dd7070Spatrick // Either the action has told us that this cannot be a
2213e5dd7070Spatrick // pseudo-destructor expression (based on the type of base
2214e5dd7070Spatrick // expression), or we didn't see a '~' in the right place. We
2215e5dd7070Spatrick // can still parse a destructor name here, but in that case it
2216e5dd7070Spatrick // names a real destructor.
2217e5dd7070Spatrick // Allow explicit constructor calls in Microsoft mode.
2218e5dd7070Spatrick // FIXME: Add support for explicit call of template constructor.
2219e5dd7070Spatrick SourceLocation TemplateKWLoc;
2220e5dd7070Spatrick UnqualifiedId Name;
2221e5dd7070Spatrick if (getLangOpts().ObjC && OpKind == tok::period &&
2222e5dd7070Spatrick Tok.is(tok::kw_class)) {
2223e5dd7070Spatrick // Objective-C++:
2224e5dd7070Spatrick // After a '.' in a member access expression, treat the keyword
2225e5dd7070Spatrick // 'class' as if it were an identifier.
2226e5dd7070Spatrick //
2227e5dd7070Spatrick // This hack allows property access to the 'class' method because it is
2228e5dd7070Spatrick // such a common method name. For other C++ keywords that are
2229e5dd7070Spatrick // Objective-C method names, one must use the message send syntax.
2230e5dd7070Spatrick IdentifierInfo *Id = Tok.getIdentifierInfo();
2231e5dd7070Spatrick SourceLocation Loc = ConsumeToken();
2232e5dd7070Spatrick Name.setIdentifier(Id, Loc);
2233ec727ea7Spatrick } else if (ParseUnqualifiedId(
2234ec727ea7Spatrick SS, ObjectType, LHS.get() && LHS.get()->containsErrors(),
2235e5dd7070Spatrick /*EnteringContext=*/false,
2236e5dd7070Spatrick /*AllowDestructorName=*/true,
2237e5dd7070Spatrick /*AllowConstructorName=*/
2238ec727ea7Spatrick getLangOpts().MicrosoftExt && SS.isNotEmpty(),
2239ec727ea7Spatrick /*AllowDeductionGuide=*/false, &TemplateKWLoc, Name)) {
2240e5dd7070Spatrick (void)Actions.CorrectDelayedTyposInExpr(LHS);
2241e5dd7070Spatrick LHS = ExprError();
2242e5dd7070Spatrick }
2243e5dd7070Spatrick
2244e5dd7070Spatrick if (!LHS.isInvalid())
2245e5dd7070Spatrick LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.get(), OpLoc,
2246e5dd7070Spatrick OpKind, SS, TemplateKWLoc, Name,
2247e5dd7070Spatrick CurParsedObjCImpl ? CurParsedObjCImpl->Dcl
2248e5dd7070Spatrick : nullptr);
2249ec727ea7Spatrick if (!LHS.isInvalid()) {
2250ec727ea7Spatrick if (Tok.is(tok::less))
2251e5dd7070Spatrick checkPotentialAngleBracket(LHS);
2252ec727ea7Spatrick } else if (OrigLHS && Name.isValid()) {
2253ec727ea7Spatrick // Preserve the LHS if the RHS is an invalid member.
2254ec727ea7Spatrick LHS = Actions.CreateRecoveryExpr(OrigLHS->getBeginLoc(),
2255ec727ea7Spatrick Name.getEndLoc(), {OrigLHS});
2256ec727ea7Spatrick }
2257e5dd7070Spatrick break;
2258e5dd7070Spatrick }
2259e5dd7070Spatrick case tok::plusplus: // postfix-expression: postfix-expression '++'
2260e5dd7070Spatrick case tok::minusminus: // postfix-expression: postfix-expression '--'
2261e5dd7070Spatrick if (!LHS.isInvalid()) {
2262ec727ea7Spatrick Expr *Arg = LHS.get();
2263e5dd7070Spatrick LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
2264ec727ea7Spatrick Tok.getKind(), Arg);
2265ec727ea7Spatrick if (LHS.isInvalid())
2266ec727ea7Spatrick LHS = Actions.CreateRecoveryExpr(Arg->getBeginLoc(),
2267ec727ea7Spatrick Tok.getLocation(), Arg);
2268e5dd7070Spatrick }
2269e5dd7070Spatrick ConsumeToken();
2270e5dd7070Spatrick break;
2271e5dd7070Spatrick }
2272e5dd7070Spatrick }
2273e5dd7070Spatrick }
2274e5dd7070Spatrick
2275e5dd7070Spatrick /// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
2276e5dd7070Spatrick /// vec_step and we are at the start of an expression or a parenthesized
2277e5dd7070Spatrick /// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
2278e5dd7070Spatrick /// expression (isCastExpr == false) or the type (isCastExpr == true).
2279e5dd7070Spatrick ///
2280e5dd7070Spatrick /// \verbatim
2281e5dd7070Spatrick /// unary-expression: [C99 6.5.3]
2282e5dd7070Spatrick /// 'sizeof' unary-expression
2283e5dd7070Spatrick /// 'sizeof' '(' type-name ')'
2284e5dd7070Spatrick /// [GNU] '__alignof' unary-expression
2285e5dd7070Spatrick /// [GNU] '__alignof' '(' type-name ')'
2286e5dd7070Spatrick /// [C11] '_Alignof' '(' type-name ')'
2287e5dd7070Spatrick /// [C++0x] 'alignof' '(' type-id ')'
2288e5dd7070Spatrick ///
2289e5dd7070Spatrick /// [GNU] typeof-specifier:
2290e5dd7070Spatrick /// typeof ( expressions )
2291e5dd7070Spatrick /// typeof ( type-name )
2292e5dd7070Spatrick /// [GNU/C++] typeof unary-expression
2293*12c85518Srobert /// [C2x] typeof-specifier:
2294*12c85518Srobert /// typeof '(' typeof-specifier-argument ')'
2295*12c85518Srobert /// typeof_unqual '(' typeof-specifier-argument ')'
2296*12c85518Srobert ///
2297*12c85518Srobert /// typeof-specifier-argument:
2298*12c85518Srobert /// expression
2299*12c85518Srobert /// type-name
2300e5dd7070Spatrick ///
2301e5dd7070Spatrick /// [OpenCL 1.1 6.11.12] vec_step built-in function:
2302e5dd7070Spatrick /// vec_step ( expressions )
2303e5dd7070Spatrick /// vec_step ( type-name )
2304e5dd7070Spatrick /// \endverbatim
2305e5dd7070Spatrick ExprResult
ParseExprAfterUnaryExprOrTypeTrait(const Token & OpTok,bool & isCastExpr,ParsedType & CastTy,SourceRange & CastRange)2306e5dd7070Spatrick Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
2307e5dd7070Spatrick bool &isCastExpr,
2308e5dd7070Spatrick ParsedType &CastTy,
2309e5dd7070Spatrick SourceRange &CastRange) {
2310e5dd7070Spatrick
2311*12c85518Srobert assert(OpTok.isOneOf(tok::kw_typeof, tok::kw_typeof_unqual, tok::kw_sizeof,
2312*12c85518Srobert tok::kw___alignof, tok::kw_alignof, tok::kw__Alignof,
2313*12c85518Srobert tok::kw_vec_step,
2314e5dd7070Spatrick tok::kw___builtin_omp_required_simd_align) &&
2315e5dd7070Spatrick "Not a typeof/sizeof/alignof/vec_step expression!");
2316e5dd7070Spatrick
2317e5dd7070Spatrick ExprResult Operand;
2318e5dd7070Spatrick
2319e5dd7070Spatrick // If the operand doesn't start with an '(', it must be an expression.
2320e5dd7070Spatrick if (Tok.isNot(tok::l_paren)) {
2321e5dd7070Spatrick // If construct allows a form without parenthesis, user may forget to put
2322e5dd7070Spatrick // pathenthesis around type name.
2323e5dd7070Spatrick if (OpTok.isOneOf(tok::kw_sizeof, tok::kw___alignof, tok::kw_alignof,
2324e5dd7070Spatrick tok::kw__Alignof)) {
2325e5dd7070Spatrick if (isTypeIdUnambiguously()) {
2326e5dd7070Spatrick DeclSpec DS(AttrFactory);
2327e5dd7070Spatrick ParseSpecifierQualifierList(DS);
2328*12c85518Srobert Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
2329*12c85518Srobert DeclaratorContext::TypeName);
2330e5dd7070Spatrick ParseDeclarator(DeclaratorInfo);
2331e5dd7070Spatrick
2332e5dd7070Spatrick SourceLocation LParenLoc = PP.getLocForEndOfToken(OpTok.getLocation());
2333e5dd7070Spatrick SourceLocation RParenLoc = PP.getLocForEndOfToken(PrevTokLocation);
2334a9ac8606Spatrick if (LParenLoc.isInvalid() || RParenLoc.isInvalid()) {
2335a9ac8606Spatrick Diag(OpTok.getLocation(),
2336a9ac8606Spatrick diag::err_expected_parentheses_around_typename)
2337a9ac8606Spatrick << OpTok.getName();
2338a9ac8606Spatrick } else {
2339e5dd7070Spatrick Diag(LParenLoc, diag::err_expected_parentheses_around_typename)
2340a9ac8606Spatrick << OpTok.getName() << FixItHint::CreateInsertion(LParenLoc, "(")
2341e5dd7070Spatrick << FixItHint::CreateInsertion(RParenLoc, ")");
2342a9ac8606Spatrick }
2343e5dd7070Spatrick isCastExpr = true;
2344e5dd7070Spatrick return ExprEmpty();
2345e5dd7070Spatrick }
2346e5dd7070Spatrick }
2347e5dd7070Spatrick
2348e5dd7070Spatrick isCastExpr = false;
2349*12c85518Srobert if (OpTok.isOneOf(tok::kw_typeof, tok::kw_typeof_unqual) &&
2350*12c85518Srobert !getLangOpts().CPlusPlus) {
2351e5dd7070Spatrick Diag(Tok, diag::err_expected_after) << OpTok.getIdentifierInfo()
2352e5dd7070Spatrick << tok::l_paren;
2353e5dd7070Spatrick return ExprError();
2354e5dd7070Spatrick }
2355e5dd7070Spatrick
2356e5dd7070Spatrick Operand = ParseCastExpression(UnaryExprOnly);
2357e5dd7070Spatrick } else {
2358e5dd7070Spatrick // If it starts with a '(', we know that it is either a parenthesized
2359e5dd7070Spatrick // type-name, or it is a unary-expression that starts with a compound
2360e5dd7070Spatrick // literal, or starts with a primary-expression that is a parenthesized
2361e5dd7070Spatrick // expression.
2362e5dd7070Spatrick ParenParseOption ExprType = CastExpr;
2363e5dd7070Spatrick SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
2364e5dd7070Spatrick
2365e5dd7070Spatrick Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
2366e5dd7070Spatrick false, CastTy, RParenLoc);
2367e5dd7070Spatrick CastRange = SourceRange(LParenLoc, RParenLoc);
2368e5dd7070Spatrick
2369e5dd7070Spatrick // If ParseParenExpression parsed a '(typename)' sequence only, then this is
2370e5dd7070Spatrick // a type.
2371e5dd7070Spatrick if (ExprType == CastExpr) {
2372e5dd7070Spatrick isCastExpr = true;
2373e5dd7070Spatrick return ExprEmpty();
2374e5dd7070Spatrick }
2375e5dd7070Spatrick
2376*12c85518Srobert if (getLangOpts().CPlusPlus ||
2377*12c85518Srobert !OpTok.isOneOf(tok::kw_typeof, tok::kw_typeof_unqual)) {
2378e5dd7070Spatrick // GNU typeof in C requires the expression to be parenthesized. Not so for
2379e5dd7070Spatrick // sizeof/alignof or in C++. Therefore, the parenthesized expression is
2380e5dd7070Spatrick // the start of a unary-expression, but doesn't include any postfix
2381e5dd7070Spatrick // pieces. Parse these now if present.
2382e5dd7070Spatrick if (!Operand.isInvalid())
2383e5dd7070Spatrick Operand = ParsePostfixExpressionSuffix(Operand.get());
2384e5dd7070Spatrick }
2385e5dd7070Spatrick }
2386e5dd7070Spatrick
2387e5dd7070Spatrick // If we get here, the operand to the typeof/sizeof/alignof was an expression.
2388e5dd7070Spatrick isCastExpr = false;
2389e5dd7070Spatrick return Operand;
2390e5dd7070Spatrick }
2391e5dd7070Spatrick
2392a9ac8606Spatrick /// Parse a __builtin_sycl_unique_stable_name expression. Accepts a type-id as
2393a9ac8606Spatrick /// a parameter.
ParseSYCLUniqueStableNameExpression()2394a9ac8606Spatrick ExprResult Parser::ParseSYCLUniqueStableNameExpression() {
2395a9ac8606Spatrick assert(Tok.is(tok::kw___builtin_sycl_unique_stable_name) &&
2396*12c85518Srobert "Not __builtin_sycl_unique_stable_name");
2397ec727ea7Spatrick
2398ec727ea7Spatrick SourceLocation OpLoc = ConsumeToken();
2399ec727ea7Spatrick BalancedDelimiterTracker T(*this, tok::l_paren);
2400ec727ea7Spatrick
2401a9ac8606Spatrick // __builtin_sycl_unique_stable_name expressions are always parenthesized.
2402ec727ea7Spatrick if (T.expectAndConsume(diag::err_expected_lparen_after,
2403a9ac8606Spatrick "__builtin_sycl_unique_stable_name"))
2404ec727ea7Spatrick return ExprError();
2405ec727ea7Spatrick
2406ec727ea7Spatrick TypeResult Ty = ParseTypeName();
2407ec727ea7Spatrick
2408a9ac8606Spatrick if (Ty.isInvalid()) {
2409a9ac8606Spatrick T.skipToEnd();
2410a9ac8606Spatrick return ExprError();
2411a9ac8606Spatrick }
2412a9ac8606Spatrick
2413a9ac8606Spatrick if (T.consumeClose())
2414ec727ea7Spatrick return ExprError();
2415ec727ea7Spatrick
2416a9ac8606Spatrick return Actions.ActOnSYCLUniqueStableNameExpr(OpLoc, T.getOpenLocation(),
2417ec727ea7Spatrick T.getCloseLocation(), Ty.get());
2418ec727ea7Spatrick }
2419ec727ea7Spatrick
2420e5dd7070Spatrick /// Parse a sizeof or alignof expression.
2421e5dd7070Spatrick ///
2422e5dd7070Spatrick /// \verbatim
2423e5dd7070Spatrick /// unary-expression: [C99 6.5.3]
2424e5dd7070Spatrick /// 'sizeof' unary-expression
2425e5dd7070Spatrick /// 'sizeof' '(' type-name ')'
2426e5dd7070Spatrick /// [C++11] 'sizeof' '...' '(' identifier ')'
2427e5dd7070Spatrick /// [GNU] '__alignof' unary-expression
2428e5dd7070Spatrick /// [GNU] '__alignof' '(' type-name ')'
2429e5dd7070Spatrick /// [C11] '_Alignof' '(' type-name ')'
2430e5dd7070Spatrick /// [C++11] 'alignof' '(' type-id ')'
2431e5dd7070Spatrick /// \endverbatim
ParseUnaryExprOrTypeTraitExpression()2432e5dd7070Spatrick ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
2433e5dd7070Spatrick assert(Tok.isOneOf(tok::kw_sizeof, tok::kw___alignof, tok::kw_alignof,
2434e5dd7070Spatrick tok::kw__Alignof, tok::kw_vec_step,
2435e5dd7070Spatrick tok::kw___builtin_omp_required_simd_align) &&
2436e5dd7070Spatrick "Not a sizeof/alignof/vec_step expression!");
2437e5dd7070Spatrick Token OpTok = Tok;
2438e5dd7070Spatrick ConsumeToken();
2439e5dd7070Spatrick
2440e5dd7070Spatrick // [C++11] 'sizeof' '...' '(' identifier ')'
2441e5dd7070Spatrick if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
2442e5dd7070Spatrick SourceLocation EllipsisLoc = ConsumeToken();
2443e5dd7070Spatrick SourceLocation LParenLoc, RParenLoc;
2444e5dd7070Spatrick IdentifierInfo *Name = nullptr;
2445e5dd7070Spatrick SourceLocation NameLoc;
2446e5dd7070Spatrick if (Tok.is(tok::l_paren)) {
2447e5dd7070Spatrick BalancedDelimiterTracker T(*this, tok::l_paren);
2448e5dd7070Spatrick T.consumeOpen();
2449e5dd7070Spatrick LParenLoc = T.getOpenLocation();
2450e5dd7070Spatrick if (Tok.is(tok::identifier)) {
2451e5dd7070Spatrick Name = Tok.getIdentifierInfo();
2452e5dd7070Spatrick NameLoc = ConsumeToken();
2453e5dd7070Spatrick T.consumeClose();
2454e5dd7070Spatrick RParenLoc = T.getCloseLocation();
2455e5dd7070Spatrick if (RParenLoc.isInvalid())
2456e5dd7070Spatrick RParenLoc = PP.getLocForEndOfToken(NameLoc);
2457e5dd7070Spatrick } else {
2458e5dd7070Spatrick Diag(Tok, diag::err_expected_parameter_pack);
2459e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2460e5dd7070Spatrick }
2461e5dd7070Spatrick } else if (Tok.is(tok::identifier)) {
2462e5dd7070Spatrick Name = Tok.getIdentifierInfo();
2463e5dd7070Spatrick NameLoc = ConsumeToken();
2464e5dd7070Spatrick LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
2465e5dd7070Spatrick RParenLoc = PP.getLocForEndOfToken(NameLoc);
2466e5dd7070Spatrick Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
2467e5dd7070Spatrick << Name
2468e5dd7070Spatrick << FixItHint::CreateInsertion(LParenLoc, "(")
2469e5dd7070Spatrick << FixItHint::CreateInsertion(RParenLoc, ")");
2470e5dd7070Spatrick } else {
2471e5dd7070Spatrick Diag(Tok, diag::err_sizeof_parameter_pack);
2472e5dd7070Spatrick }
2473e5dd7070Spatrick
2474e5dd7070Spatrick if (!Name)
2475e5dd7070Spatrick return ExprError();
2476e5dd7070Spatrick
2477e5dd7070Spatrick EnterExpressionEvaluationContext Unevaluated(
2478e5dd7070Spatrick Actions, Sema::ExpressionEvaluationContext::Unevaluated,
2479e5dd7070Spatrick Sema::ReuseLambdaContextDecl);
2480e5dd7070Spatrick
2481e5dd7070Spatrick return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
2482e5dd7070Spatrick OpTok.getLocation(),
2483e5dd7070Spatrick *Name, NameLoc,
2484e5dd7070Spatrick RParenLoc);
2485e5dd7070Spatrick }
2486e5dd7070Spatrick
2487e5dd7070Spatrick if (OpTok.isOneOf(tok::kw_alignof, tok::kw__Alignof))
2488e5dd7070Spatrick Diag(OpTok, diag::warn_cxx98_compat_alignof);
2489e5dd7070Spatrick
2490e5dd7070Spatrick EnterExpressionEvaluationContext Unevaluated(
2491e5dd7070Spatrick Actions, Sema::ExpressionEvaluationContext::Unevaluated,
2492e5dd7070Spatrick Sema::ReuseLambdaContextDecl);
2493e5dd7070Spatrick
2494e5dd7070Spatrick bool isCastExpr;
2495e5dd7070Spatrick ParsedType CastTy;
2496e5dd7070Spatrick SourceRange CastRange;
2497e5dd7070Spatrick ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
2498e5dd7070Spatrick isCastExpr,
2499e5dd7070Spatrick CastTy,
2500e5dd7070Spatrick CastRange);
2501e5dd7070Spatrick
2502e5dd7070Spatrick UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
2503e5dd7070Spatrick if (OpTok.isOneOf(tok::kw_alignof, tok::kw__Alignof))
2504e5dd7070Spatrick ExprKind = UETT_AlignOf;
2505e5dd7070Spatrick else if (OpTok.is(tok::kw___alignof))
2506e5dd7070Spatrick ExprKind = UETT_PreferredAlignOf;
2507e5dd7070Spatrick else if (OpTok.is(tok::kw_vec_step))
2508e5dd7070Spatrick ExprKind = UETT_VecStep;
2509e5dd7070Spatrick else if (OpTok.is(tok::kw___builtin_omp_required_simd_align))
2510e5dd7070Spatrick ExprKind = UETT_OpenMPRequiredSimdAlign;
2511e5dd7070Spatrick
2512e5dd7070Spatrick if (isCastExpr)
2513e5dd7070Spatrick return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
2514e5dd7070Spatrick ExprKind,
2515e5dd7070Spatrick /*IsType=*/true,
2516e5dd7070Spatrick CastTy.getAsOpaquePtr(),
2517e5dd7070Spatrick CastRange);
2518e5dd7070Spatrick
2519e5dd7070Spatrick if (OpTok.isOneOf(tok::kw_alignof, tok::kw__Alignof))
2520e5dd7070Spatrick Diag(OpTok, diag::ext_alignof_expr) << OpTok.getIdentifierInfo();
2521e5dd7070Spatrick
2522e5dd7070Spatrick // If we get here, the operand to the sizeof/alignof was an expression.
2523e5dd7070Spatrick if (!Operand.isInvalid())
2524e5dd7070Spatrick Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
2525e5dd7070Spatrick ExprKind,
2526e5dd7070Spatrick /*IsType=*/false,
2527e5dd7070Spatrick Operand.get(),
2528e5dd7070Spatrick CastRange);
2529e5dd7070Spatrick return Operand;
2530e5dd7070Spatrick }
2531e5dd7070Spatrick
2532e5dd7070Spatrick /// ParseBuiltinPrimaryExpression
2533e5dd7070Spatrick ///
2534e5dd7070Spatrick /// \verbatim
2535e5dd7070Spatrick /// primary-expression: [C99 6.5.1]
2536e5dd7070Spatrick /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
2537e5dd7070Spatrick /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
2538e5dd7070Spatrick /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
2539e5dd7070Spatrick /// assign-expr ')'
2540e5dd7070Spatrick /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
2541e5dd7070Spatrick /// [GNU] '__builtin_FILE' '(' ')'
2542e5dd7070Spatrick /// [GNU] '__builtin_FUNCTION' '(' ')'
2543e5dd7070Spatrick /// [GNU] '__builtin_LINE' '(' ')'
2544e5dd7070Spatrick /// [CLANG] '__builtin_COLUMN' '(' ')'
2545*12c85518Srobert /// [GNU] '__builtin_source_location' '(' ')'
2546e5dd7070Spatrick /// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
2547e5dd7070Spatrick ///
2548e5dd7070Spatrick /// [GNU] offsetof-member-designator:
2549e5dd7070Spatrick /// [GNU] identifier
2550e5dd7070Spatrick /// [GNU] offsetof-member-designator '.' identifier
2551e5dd7070Spatrick /// [GNU] offsetof-member-designator '[' expression ']'
2552e5dd7070Spatrick /// \endverbatim
ParseBuiltinPrimaryExpression()2553e5dd7070Spatrick ExprResult Parser::ParseBuiltinPrimaryExpression() {
2554e5dd7070Spatrick ExprResult Res;
2555e5dd7070Spatrick const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
2556e5dd7070Spatrick
2557e5dd7070Spatrick tok::TokenKind T = Tok.getKind();
2558e5dd7070Spatrick SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
2559e5dd7070Spatrick
2560e5dd7070Spatrick // All of these start with an open paren.
2561e5dd7070Spatrick if (Tok.isNot(tok::l_paren))
2562e5dd7070Spatrick return ExprError(Diag(Tok, diag::err_expected_after) << BuiltinII
2563e5dd7070Spatrick << tok::l_paren);
2564e5dd7070Spatrick
2565e5dd7070Spatrick BalancedDelimiterTracker PT(*this, tok::l_paren);
2566e5dd7070Spatrick PT.consumeOpen();
2567e5dd7070Spatrick
2568e5dd7070Spatrick // TODO: Build AST.
2569e5dd7070Spatrick
2570e5dd7070Spatrick switch (T) {
2571e5dd7070Spatrick default: llvm_unreachable("Not a builtin primary expression!");
2572e5dd7070Spatrick case tok::kw___builtin_va_arg: {
2573e5dd7070Spatrick ExprResult Expr(ParseAssignmentExpression());
2574e5dd7070Spatrick
2575e5dd7070Spatrick if (ExpectAndConsume(tok::comma)) {
2576e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2577e5dd7070Spatrick Expr = ExprError();
2578e5dd7070Spatrick }
2579e5dd7070Spatrick
2580e5dd7070Spatrick TypeResult Ty = ParseTypeName();
2581e5dd7070Spatrick
2582e5dd7070Spatrick if (Tok.isNot(tok::r_paren)) {
2583e5dd7070Spatrick Diag(Tok, diag::err_expected) << tok::r_paren;
2584e5dd7070Spatrick Expr = ExprError();
2585e5dd7070Spatrick }
2586e5dd7070Spatrick
2587e5dd7070Spatrick if (Expr.isInvalid() || Ty.isInvalid())
2588e5dd7070Spatrick Res = ExprError();
2589e5dd7070Spatrick else
2590e5dd7070Spatrick Res = Actions.ActOnVAArg(StartLoc, Expr.get(), Ty.get(), ConsumeParen());
2591e5dd7070Spatrick break;
2592e5dd7070Spatrick }
2593e5dd7070Spatrick case tok::kw___builtin_offsetof: {
2594e5dd7070Spatrick SourceLocation TypeLoc = Tok.getLocation();
2595*12c85518Srobert auto OOK = Sema::OffsetOfKind::OOK_Builtin;
2596*12c85518Srobert if (Tok.getLocation().isMacroID()) {
2597*12c85518Srobert StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics(
2598*12c85518Srobert Tok.getLocation(), PP.getSourceManager(), getLangOpts());
2599*12c85518Srobert if (MacroName == "offsetof")
2600*12c85518Srobert OOK = Sema::OffsetOfKind::OOK_Macro;
2601*12c85518Srobert }
2602*12c85518Srobert TypeResult Ty;
2603*12c85518Srobert {
2604*12c85518Srobert OffsetOfStateRAIIObject InOffsetof(*this, OOK);
2605*12c85518Srobert Ty = ParseTypeName();
2606e5dd7070Spatrick if (Ty.isInvalid()) {
2607e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2608e5dd7070Spatrick return ExprError();
2609e5dd7070Spatrick }
2610*12c85518Srobert }
2611e5dd7070Spatrick
2612e5dd7070Spatrick if (ExpectAndConsume(tok::comma)) {
2613e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2614e5dd7070Spatrick return ExprError();
2615e5dd7070Spatrick }
2616e5dd7070Spatrick
2617e5dd7070Spatrick // We must have at least one identifier here.
2618e5dd7070Spatrick if (Tok.isNot(tok::identifier)) {
2619e5dd7070Spatrick Diag(Tok, diag::err_expected) << tok::identifier;
2620e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2621e5dd7070Spatrick return ExprError();
2622e5dd7070Spatrick }
2623e5dd7070Spatrick
2624e5dd7070Spatrick // Keep track of the various subcomponents we see.
2625e5dd7070Spatrick SmallVector<Sema::OffsetOfComponent, 4> Comps;
2626e5dd7070Spatrick
2627e5dd7070Spatrick Comps.push_back(Sema::OffsetOfComponent());
2628e5dd7070Spatrick Comps.back().isBrackets = false;
2629e5dd7070Spatrick Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
2630e5dd7070Spatrick Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
2631e5dd7070Spatrick
2632e5dd7070Spatrick // FIXME: This loop leaks the index expressions on error.
2633*12c85518Srobert while (true) {
2634e5dd7070Spatrick if (Tok.is(tok::period)) {
2635e5dd7070Spatrick // offsetof-member-designator: offsetof-member-designator '.' identifier
2636e5dd7070Spatrick Comps.push_back(Sema::OffsetOfComponent());
2637e5dd7070Spatrick Comps.back().isBrackets = false;
2638e5dd7070Spatrick Comps.back().LocStart = ConsumeToken();
2639e5dd7070Spatrick
2640e5dd7070Spatrick if (Tok.isNot(tok::identifier)) {
2641e5dd7070Spatrick Diag(Tok, diag::err_expected) << tok::identifier;
2642e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2643e5dd7070Spatrick return ExprError();
2644e5dd7070Spatrick }
2645e5dd7070Spatrick Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
2646e5dd7070Spatrick Comps.back().LocEnd = ConsumeToken();
2647e5dd7070Spatrick } else if (Tok.is(tok::l_square)) {
2648e5dd7070Spatrick if (CheckProhibitedCXX11Attribute())
2649e5dd7070Spatrick return ExprError();
2650e5dd7070Spatrick
2651e5dd7070Spatrick // offsetof-member-designator: offsetof-member-design '[' expression ']'
2652e5dd7070Spatrick Comps.push_back(Sema::OffsetOfComponent());
2653e5dd7070Spatrick Comps.back().isBrackets = true;
2654e5dd7070Spatrick BalancedDelimiterTracker ST(*this, tok::l_square);
2655e5dd7070Spatrick ST.consumeOpen();
2656e5dd7070Spatrick Comps.back().LocStart = ST.getOpenLocation();
2657e5dd7070Spatrick Res = ParseExpression();
2658e5dd7070Spatrick if (Res.isInvalid()) {
2659e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2660e5dd7070Spatrick return Res;
2661e5dd7070Spatrick }
2662e5dd7070Spatrick Comps.back().U.E = Res.get();
2663e5dd7070Spatrick
2664e5dd7070Spatrick ST.consumeClose();
2665e5dd7070Spatrick Comps.back().LocEnd = ST.getCloseLocation();
2666e5dd7070Spatrick } else {
2667e5dd7070Spatrick if (Tok.isNot(tok::r_paren)) {
2668e5dd7070Spatrick PT.consumeClose();
2669e5dd7070Spatrick Res = ExprError();
2670e5dd7070Spatrick } else if (Ty.isInvalid()) {
2671e5dd7070Spatrick Res = ExprError();
2672e5dd7070Spatrick } else {
2673e5dd7070Spatrick PT.consumeClose();
2674e5dd7070Spatrick Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
2675e5dd7070Spatrick Ty.get(), Comps,
2676e5dd7070Spatrick PT.getCloseLocation());
2677e5dd7070Spatrick }
2678e5dd7070Spatrick break;
2679e5dd7070Spatrick }
2680e5dd7070Spatrick }
2681e5dd7070Spatrick break;
2682e5dd7070Spatrick }
2683e5dd7070Spatrick case tok::kw___builtin_choose_expr: {
2684e5dd7070Spatrick ExprResult Cond(ParseAssignmentExpression());
2685e5dd7070Spatrick if (Cond.isInvalid()) {
2686e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2687e5dd7070Spatrick return Cond;
2688e5dd7070Spatrick }
2689e5dd7070Spatrick if (ExpectAndConsume(tok::comma)) {
2690e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2691e5dd7070Spatrick return ExprError();
2692e5dd7070Spatrick }
2693e5dd7070Spatrick
2694e5dd7070Spatrick ExprResult Expr1(ParseAssignmentExpression());
2695e5dd7070Spatrick if (Expr1.isInvalid()) {
2696e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2697e5dd7070Spatrick return Expr1;
2698e5dd7070Spatrick }
2699e5dd7070Spatrick if (ExpectAndConsume(tok::comma)) {
2700e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2701e5dd7070Spatrick return ExprError();
2702e5dd7070Spatrick }
2703e5dd7070Spatrick
2704e5dd7070Spatrick ExprResult Expr2(ParseAssignmentExpression());
2705e5dd7070Spatrick if (Expr2.isInvalid()) {
2706e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2707e5dd7070Spatrick return Expr2;
2708e5dd7070Spatrick }
2709e5dd7070Spatrick if (Tok.isNot(tok::r_paren)) {
2710e5dd7070Spatrick Diag(Tok, diag::err_expected) << tok::r_paren;
2711e5dd7070Spatrick return ExprError();
2712e5dd7070Spatrick }
2713e5dd7070Spatrick Res = Actions.ActOnChooseExpr(StartLoc, Cond.get(), Expr1.get(),
2714e5dd7070Spatrick Expr2.get(), ConsumeParen());
2715e5dd7070Spatrick break;
2716e5dd7070Spatrick }
2717e5dd7070Spatrick case tok::kw___builtin_astype: {
2718e5dd7070Spatrick // The first argument is an expression to be converted, followed by a comma.
2719e5dd7070Spatrick ExprResult Expr(ParseAssignmentExpression());
2720e5dd7070Spatrick if (Expr.isInvalid()) {
2721e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2722e5dd7070Spatrick return ExprError();
2723e5dd7070Spatrick }
2724e5dd7070Spatrick
2725e5dd7070Spatrick if (ExpectAndConsume(tok::comma)) {
2726e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2727e5dd7070Spatrick return ExprError();
2728e5dd7070Spatrick }
2729e5dd7070Spatrick
2730e5dd7070Spatrick // Second argument is the type to bitcast to.
2731e5dd7070Spatrick TypeResult DestTy = ParseTypeName();
2732e5dd7070Spatrick if (DestTy.isInvalid())
2733e5dd7070Spatrick return ExprError();
2734e5dd7070Spatrick
2735e5dd7070Spatrick // Attempt to consume the r-paren.
2736e5dd7070Spatrick if (Tok.isNot(tok::r_paren)) {
2737e5dd7070Spatrick Diag(Tok, diag::err_expected) << tok::r_paren;
2738e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2739e5dd7070Spatrick return ExprError();
2740e5dd7070Spatrick }
2741e5dd7070Spatrick
2742e5dd7070Spatrick Res = Actions.ActOnAsTypeExpr(Expr.get(), DestTy.get(), StartLoc,
2743e5dd7070Spatrick ConsumeParen());
2744e5dd7070Spatrick break;
2745e5dd7070Spatrick }
2746e5dd7070Spatrick case tok::kw___builtin_convertvector: {
2747e5dd7070Spatrick // The first argument is an expression to be converted, followed by a comma.
2748e5dd7070Spatrick ExprResult Expr(ParseAssignmentExpression());
2749e5dd7070Spatrick if (Expr.isInvalid()) {
2750e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2751e5dd7070Spatrick return ExprError();
2752e5dd7070Spatrick }
2753e5dd7070Spatrick
2754e5dd7070Spatrick if (ExpectAndConsume(tok::comma)) {
2755e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2756e5dd7070Spatrick return ExprError();
2757e5dd7070Spatrick }
2758e5dd7070Spatrick
2759e5dd7070Spatrick // Second argument is the type to bitcast to.
2760e5dd7070Spatrick TypeResult DestTy = ParseTypeName();
2761e5dd7070Spatrick if (DestTy.isInvalid())
2762e5dd7070Spatrick return ExprError();
2763e5dd7070Spatrick
2764e5dd7070Spatrick // Attempt to consume the r-paren.
2765e5dd7070Spatrick if (Tok.isNot(tok::r_paren)) {
2766e5dd7070Spatrick Diag(Tok, diag::err_expected) << tok::r_paren;
2767e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2768e5dd7070Spatrick return ExprError();
2769e5dd7070Spatrick }
2770e5dd7070Spatrick
2771e5dd7070Spatrick Res = Actions.ActOnConvertVectorExpr(Expr.get(), DestTy.get(), StartLoc,
2772e5dd7070Spatrick ConsumeParen());
2773e5dd7070Spatrick break;
2774e5dd7070Spatrick }
2775e5dd7070Spatrick case tok::kw___builtin_COLUMN:
2776e5dd7070Spatrick case tok::kw___builtin_FILE:
2777e5dd7070Spatrick case tok::kw___builtin_FUNCTION:
2778*12c85518Srobert case tok::kw___builtin_LINE:
2779*12c85518Srobert case tok::kw___builtin_source_location: {
2780e5dd7070Spatrick // Attempt to consume the r-paren.
2781e5dd7070Spatrick if (Tok.isNot(tok::r_paren)) {
2782e5dd7070Spatrick Diag(Tok, diag::err_expected) << tok::r_paren;
2783e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
2784e5dd7070Spatrick return ExprError();
2785e5dd7070Spatrick }
2786e5dd7070Spatrick SourceLocExpr::IdentKind Kind = [&] {
2787e5dd7070Spatrick switch (T) {
2788e5dd7070Spatrick case tok::kw___builtin_FILE:
2789e5dd7070Spatrick return SourceLocExpr::File;
2790e5dd7070Spatrick case tok::kw___builtin_FUNCTION:
2791e5dd7070Spatrick return SourceLocExpr::Function;
2792e5dd7070Spatrick case tok::kw___builtin_LINE:
2793e5dd7070Spatrick return SourceLocExpr::Line;
2794e5dd7070Spatrick case tok::kw___builtin_COLUMN:
2795e5dd7070Spatrick return SourceLocExpr::Column;
2796*12c85518Srobert case tok::kw___builtin_source_location:
2797*12c85518Srobert return SourceLocExpr::SourceLocStruct;
2798e5dd7070Spatrick default:
2799e5dd7070Spatrick llvm_unreachable("invalid keyword");
2800e5dd7070Spatrick }
2801e5dd7070Spatrick }();
2802e5dd7070Spatrick Res = Actions.ActOnSourceLocExpr(Kind, StartLoc, ConsumeParen());
2803e5dd7070Spatrick break;
2804e5dd7070Spatrick }
2805e5dd7070Spatrick }
2806e5dd7070Spatrick
2807e5dd7070Spatrick if (Res.isInvalid())
2808e5dd7070Spatrick return ExprError();
2809e5dd7070Spatrick
2810e5dd7070Spatrick // These can be followed by postfix-expr pieces because they are
2811e5dd7070Spatrick // primary-expressions.
2812e5dd7070Spatrick return ParsePostfixExpressionSuffix(Res.get());
2813e5dd7070Spatrick }
2814e5dd7070Spatrick
tryParseOpenMPArrayShapingCastPart()2815ec727ea7Spatrick bool Parser::tryParseOpenMPArrayShapingCastPart() {
2816ec727ea7Spatrick assert(Tok.is(tok::l_square) && "Expected open bracket");
2817ec727ea7Spatrick bool ErrorFound = true;
2818ec727ea7Spatrick TentativeParsingAction TPA(*this);
2819ec727ea7Spatrick do {
2820ec727ea7Spatrick if (Tok.isNot(tok::l_square))
2821ec727ea7Spatrick break;
2822ec727ea7Spatrick // Consume '['
2823ec727ea7Spatrick ConsumeBracket();
2824ec727ea7Spatrick // Skip inner expression.
2825ec727ea7Spatrick while (!SkipUntil(tok::r_square, tok::annot_pragma_openmp_end,
2826ec727ea7Spatrick StopAtSemi | StopBeforeMatch))
2827ec727ea7Spatrick ;
2828ec727ea7Spatrick if (Tok.isNot(tok::r_square))
2829ec727ea7Spatrick break;
2830ec727ea7Spatrick // Consume ']'
2831ec727ea7Spatrick ConsumeBracket();
2832ec727ea7Spatrick // Found ')' - done.
2833ec727ea7Spatrick if (Tok.is(tok::r_paren)) {
2834ec727ea7Spatrick ErrorFound = false;
2835ec727ea7Spatrick break;
2836ec727ea7Spatrick }
2837ec727ea7Spatrick } while (Tok.isNot(tok::annot_pragma_openmp_end));
2838ec727ea7Spatrick TPA.Revert();
2839ec727ea7Spatrick return !ErrorFound;
2840ec727ea7Spatrick }
2841ec727ea7Spatrick
2842e5dd7070Spatrick /// ParseParenExpression - This parses the unit that starts with a '(' token,
2843e5dd7070Spatrick /// based on what is allowed by ExprType. The actual thing parsed is returned
2844e5dd7070Spatrick /// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
2845e5dd7070Spatrick /// not the parsed cast-expression.
2846e5dd7070Spatrick ///
2847e5dd7070Spatrick /// \verbatim
2848e5dd7070Spatrick /// primary-expression: [C99 6.5.1]
2849e5dd7070Spatrick /// '(' expression ')'
2850e5dd7070Spatrick /// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
2851e5dd7070Spatrick /// postfix-expression: [C99 6.5.2]
2852e5dd7070Spatrick /// '(' type-name ')' '{' initializer-list '}'
2853e5dd7070Spatrick /// '(' type-name ')' '{' initializer-list ',' '}'
2854e5dd7070Spatrick /// cast-expression: [C99 6.5.4]
2855e5dd7070Spatrick /// '(' type-name ')' cast-expression
2856e5dd7070Spatrick /// [ARC] bridged-cast-expression
2857e5dd7070Spatrick /// [ARC] bridged-cast-expression:
2858e5dd7070Spatrick /// (__bridge type-name) cast-expression
2859e5dd7070Spatrick /// (__bridge_transfer type-name) cast-expression
2860e5dd7070Spatrick /// (__bridge_retained type-name) cast-expression
2861e5dd7070Spatrick /// fold-expression: [C++1z]
2862e5dd7070Spatrick /// '(' cast-expression fold-operator '...' ')'
2863e5dd7070Spatrick /// '(' '...' fold-operator cast-expression ')'
2864e5dd7070Spatrick /// '(' cast-expression fold-operator '...'
2865e5dd7070Spatrick /// fold-operator cast-expression ')'
2866ec727ea7Spatrick /// [OPENMP] Array shaping operation
2867ec727ea7Spatrick /// '(' '[' expression ']' { '[' expression ']' } cast-expression
2868e5dd7070Spatrick /// \endverbatim
2869e5dd7070Spatrick ExprResult
ParseParenExpression(ParenParseOption & ExprType,bool stopIfCastExpr,bool isTypeCast,ParsedType & CastTy,SourceLocation & RParenLoc)2870e5dd7070Spatrick Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
2871e5dd7070Spatrick bool isTypeCast, ParsedType &CastTy,
2872e5dd7070Spatrick SourceLocation &RParenLoc) {
2873e5dd7070Spatrick assert(Tok.is(tok::l_paren) && "Not a paren expr!");
2874e5dd7070Spatrick ColonProtectionRAIIObject ColonProtection(*this, false);
2875e5dd7070Spatrick BalancedDelimiterTracker T(*this, tok::l_paren);
2876e5dd7070Spatrick if (T.consumeOpen())
2877e5dd7070Spatrick return ExprError();
2878e5dd7070Spatrick SourceLocation OpenLoc = T.getOpenLocation();
2879e5dd7070Spatrick
2880e5dd7070Spatrick PreferredType.enterParenExpr(Tok.getLocation(), OpenLoc);
2881e5dd7070Spatrick
2882e5dd7070Spatrick ExprResult Result(true);
2883e5dd7070Spatrick bool isAmbiguousTypeId;
2884e5dd7070Spatrick CastTy = nullptr;
2885e5dd7070Spatrick
2886e5dd7070Spatrick if (Tok.is(tok::code_completion)) {
2887a9ac8606Spatrick cutOffParsing();
2888e5dd7070Spatrick Actions.CodeCompleteExpression(
2889e5dd7070Spatrick getCurScope(), PreferredType.get(Tok.getLocation()),
2890e5dd7070Spatrick /*IsParenthesized=*/ExprType >= CompoundLiteral);
2891e5dd7070Spatrick return ExprError();
2892e5dd7070Spatrick }
2893e5dd7070Spatrick
2894e5dd7070Spatrick // Diagnose use of bridge casts in non-arc mode.
2895e5dd7070Spatrick bool BridgeCast = (getLangOpts().ObjC &&
2896e5dd7070Spatrick Tok.isOneOf(tok::kw___bridge,
2897e5dd7070Spatrick tok::kw___bridge_transfer,
2898e5dd7070Spatrick tok::kw___bridge_retained,
2899e5dd7070Spatrick tok::kw___bridge_retain));
2900e5dd7070Spatrick if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
2901e5dd7070Spatrick if (!TryConsumeToken(tok::kw___bridge)) {
2902e5dd7070Spatrick StringRef BridgeCastName = Tok.getName();
2903e5dd7070Spatrick SourceLocation BridgeKeywordLoc = ConsumeToken();
2904e5dd7070Spatrick if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
2905e5dd7070Spatrick Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
2906e5dd7070Spatrick << BridgeCastName
2907e5dd7070Spatrick << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
2908e5dd7070Spatrick }
2909e5dd7070Spatrick BridgeCast = false;
2910e5dd7070Spatrick }
2911e5dd7070Spatrick
2912e5dd7070Spatrick // None of these cases should fall through with an invalid Result
2913e5dd7070Spatrick // unless they've already reported an error.
2914e5dd7070Spatrick if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
2915*12c85518Srobert Diag(Tok, OpenLoc.isMacroID() ? diag::ext_gnu_statement_expr_macro
2916*12c85518Srobert : diag::ext_gnu_statement_expr);
2917e5dd7070Spatrick
2918a9ac8606Spatrick checkCompoundToken(OpenLoc, tok::l_paren, CompoundToken::StmtExprBegin);
2919a9ac8606Spatrick
2920e5dd7070Spatrick if (!getCurScope()->getFnParent() && !getCurScope()->getBlockParent()) {
2921e5dd7070Spatrick Result = ExprError(Diag(OpenLoc, diag::err_stmtexpr_file_scope));
2922e5dd7070Spatrick } else {
2923e5dd7070Spatrick // Find the nearest non-record decl context. Variables declared in a
2924e5dd7070Spatrick // statement expression behave as if they were declared in the enclosing
2925e5dd7070Spatrick // function, block, or other code construct.
2926e5dd7070Spatrick DeclContext *CodeDC = Actions.CurContext;
2927e5dd7070Spatrick while (CodeDC->isRecord() || isa<EnumDecl>(CodeDC)) {
2928e5dd7070Spatrick CodeDC = CodeDC->getParent();
2929e5dd7070Spatrick assert(CodeDC && !CodeDC->isFileContext() &&
2930e5dd7070Spatrick "statement expr not in code context");
2931e5dd7070Spatrick }
2932e5dd7070Spatrick Sema::ContextRAII SavedContext(Actions, CodeDC, /*NewThisContext=*/false);
2933e5dd7070Spatrick
2934e5dd7070Spatrick Actions.ActOnStartStmtExpr();
2935e5dd7070Spatrick
2936e5dd7070Spatrick StmtResult Stmt(ParseCompoundStatement(true));
2937e5dd7070Spatrick ExprType = CompoundStmt;
2938e5dd7070Spatrick
2939e5dd7070Spatrick // If the substmt parsed correctly, build the AST node.
2940e5dd7070Spatrick if (!Stmt.isInvalid()) {
2941e5dd7070Spatrick Result = Actions.ActOnStmtExpr(getCurScope(), OpenLoc, Stmt.get(),
2942e5dd7070Spatrick Tok.getLocation());
2943e5dd7070Spatrick } else {
2944e5dd7070Spatrick Actions.ActOnStmtExprError();
2945e5dd7070Spatrick }
2946e5dd7070Spatrick }
2947e5dd7070Spatrick } else if (ExprType >= CompoundLiteral && BridgeCast) {
2948e5dd7070Spatrick tok::TokenKind tokenKind = Tok.getKind();
2949e5dd7070Spatrick SourceLocation BridgeKeywordLoc = ConsumeToken();
2950e5dd7070Spatrick
2951e5dd7070Spatrick // Parse an Objective-C ARC ownership cast expression.
2952e5dd7070Spatrick ObjCBridgeCastKind Kind;
2953e5dd7070Spatrick if (tokenKind == tok::kw___bridge)
2954e5dd7070Spatrick Kind = OBC_Bridge;
2955e5dd7070Spatrick else if (tokenKind == tok::kw___bridge_transfer)
2956e5dd7070Spatrick Kind = OBC_BridgeTransfer;
2957e5dd7070Spatrick else if (tokenKind == tok::kw___bridge_retained)
2958e5dd7070Spatrick Kind = OBC_BridgeRetained;
2959e5dd7070Spatrick else {
2960e5dd7070Spatrick // As a hopefully temporary workaround, allow __bridge_retain as
2961e5dd7070Spatrick // a synonym for __bridge_retained, but only in system headers.
2962e5dd7070Spatrick assert(tokenKind == tok::kw___bridge_retain);
2963e5dd7070Spatrick Kind = OBC_BridgeRetained;
2964e5dd7070Spatrick if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
2965e5dd7070Spatrick Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
2966e5dd7070Spatrick << FixItHint::CreateReplacement(BridgeKeywordLoc,
2967e5dd7070Spatrick "__bridge_retained");
2968e5dd7070Spatrick }
2969e5dd7070Spatrick
2970e5dd7070Spatrick TypeResult Ty = ParseTypeName();
2971e5dd7070Spatrick T.consumeClose();
2972e5dd7070Spatrick ColonProtection.restore();
2973e5dd7070Spatrick RParenLoc = T.getCloseLocation();
2974e5dd7070Spatrick
2975e5dd7070Spatrick PreferredType.enterTypeCast(Tok.getLocation(), Ty.get().get());
2976e5dd7070Spatrick ExprResult SubExpr = ParseCastExpression(AnyCastExpr);
2977e5dd7070Spatrick
2978e5dd7070Spatrick if (Ty.isInvalid() || SubExpr.isInvalid())
2979e5dd7070Spatrick return ExprError();
2980e5dd7070Spatrick
2981e5dd7070Spatrick return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
2982e5dd7070Spatrick BridgeKeywordLoc, Ty.get(),
2983e5dd7070Spatrick RParenLoc, SubExpr.get());
2984e5dd7070Spatrick } else if (ExprType >= CompoundLiteral &&
2985e5dd7070Spatrick isTypeIdInParens(isAmbiguousTypeId)) {
2986e5dd7070Spatrick
2987e5dd7070Spatrick // Otherwise, this is a compound literal expression or cast expression.
2988e5dd7070Spatrick
2989e5dd7070Spatrick // In C++, if the type-id is ambiguous we disambiguate based on context.
2990e5dd7070Spatrick // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
2991e5dd7070Spatrick // in which case we should treat it as type-id.
2992e5dd7070Spatrick // if stopIfCastExpr is false, we need to determine the context past the
2993e5dd7070Spatrick // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
2994e5dd7070Spatrick if (isAmbiguousTypeId && !stopIfCastExpr) {
2995e5dd7070Spatrick ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T,
2996e5dd7070Spatrick ColonProtection);
2997e5dd7070Spatrick RParenLoc = T.getCloseLocation();
2998e5dd7070Spatrick return res;
2999e5dd7070Spatrick }
3000e5dd7070Spatrick
3001e5dd7070Spatrick // Parse the type declarator.
3002e5dd7070Spatrick DeclSpec DS(AttrFactory);
3003e5dd7070Spatrick ParseSpecifierQualifierList(DS);
3004*12c85518Srobert Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
3005*12c85518Srobert DeclaratorContext::TypeName);
3006e5dd7070Spatrick ParseDeclarator(DeclaratorInfo);
3007e5dd7070Spatrick
3008e5dd7070Spatrick // If our type is followed by an identifier and either ':' or ']', then
3009e5dd7070Spatrick // this is probably an Objective-C message send where the leading '[' is
3010e5dd7070Spatrick // missing. Recover as if that were the case.
3011e5dd7070Spatrick if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
3012e5dd7070Spatrick !InMessageExpression && getLangOpts().ObjC &&
3013e5dd7070Spatrick (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
3014e5dd7070Spatrick TypeResult Ty;
3015e5dd7070Spatrick {
3016e5dd7070Spatrick InMessageExpressionRAIIObject InMessage(*this, false);
3017e5dd7070Spatrick Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
3018e5dd7070Spatrick }
3019e5dd7070Spatrick Result = ParseObjCMessageExpressionBody(SourceLocation(),
3020e5dd7070Spatrick SourceLocation(),
3021e5dd7070Spatrick Ty.get(), nullptr);
3022e5dd7070Spatrick } else {
3023e5dd7070Spatrick // Match the ')'.
3024e5dd7070Spatrick T.consumeClose();
3025e5dd7070Spatrick ColonProtection.restore();
3026e5dd7070Spatrick RParenLoc = T.getCloseLocation();
3027e5dd7070Spatrick if (Tok.is(tok::l_brace)) {
3028e5dd7070Spatrick ExprType = CompoundLiteral;
3029e5dd7070Spatrick TypeResult Ty;
3030e5dd7070Spatrick {
3031e5dd7070Spatrick InMessageExpressionRAIIObject InMessage(*this, false);
3032e5dd7070Spatrick Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
3033e5dd7070Spatrick }
3034e5dd7070Spatrick return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
3035e5dd7070Spatrick }
3036e5dd7070Spatrick
3037e5dd7070Spatrick if (Tok.is(tok::l_paren)) {
3038e5dd7070Spatrick // This could be OpenCL vector Literals
3039e5dd7070Spatrick if (getLangOpts().OpenCL)
3040e5dd7070Spatrick {
3041e5dd7070Spatrick TypeResult Ty;
3042e5dd7070Spatrick {
3043e5dd7070Spatrick InMessageExpressionRAIIObject InMessage(*this, false);
3044e5dd7070Spatrick Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
3045e5dd7070Spatrick }
3046e5dd7070Spatrick if(Ty.isInvalid())
3047e5dd7070Spatrick {
3048e5dd7070Spatrick return ExprError();
3049e5dd7070Spatrick }
3050e5dd7070Spatrick QualType QT = Ty.get().get().getCanonicalType();
3051e5dd7070Spatrick if (QT->isVectorType())
3052e5dd7070Spatrick {
3053e5dd7070Spatrick // We parsed '(' vector-type-name ')' followed by '('
3054e5dd7070Spatrick
3055e5dd7070Spatrick // Parse the cast-expression that follows it next.
3056e5dd7070Spatrick // isVectorLiteral = true will make sure we don't parse any
3057e5dd7070Spatrick // Postfix expression yet
3058e5dd7070Spatrick Result = ParseCastExpression(/*isUnaryExpression=*/AnyCastExpr,
3059e5dd7070Spatrick /*isAddressOfOperand=*/false,
3060e5dd7070Spatrick /*isTypeCast=*/IsTypeCast,
3061e5dd7070Spatrick /*isVectorLiteral=*/true);
3062e5dd7070Spatrick
3063e5dd7070Spatrick if (!Result.isInvalid()) {
3064e5dd7070Spatrick Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
3065e5dd7070Spatrick DeclaratorInfo, CastTy,
3066e5dd7070Spatrick RParenLoc, Result.get());
3067e5dd7070Spatrick }
3068e5dd7070Spatrick
3069e5dd7070Spatrick // After we performed the cast we can check for postfix-expr pieces.
3070e5dd7070Spatrick if (!Result.isInvalid()) {
3071e5dd7070Spatrick Result = ParsePostfixExpressionSuffix(Result);
3072e5dd7070Spatrick }
3073e5dd7070Spatrick
3074e5dd7070Spatrick return Result;
3075e5dd7070Spatrick }
3076e5dd7070Spatrick }
3077e5dd7070Spatrick }
3078e5dd7070Spatrick
3079e5dd7070Spatrick if (ExprType == CastExpr) {
3080e5dd7070Spatrick // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
3081e5dd7070Spatrick
3082e5dd7070Spatrick if (DeclaratorInfo.isInvalidType())
3083e5dd7070Spatrick return ExprError();
3084e5dd7070Spatrick
3085e5dd7070Spatrick // Note that this doesn't parse the subsequent cast-expression, it just
3086e5dd7070Spatrick // returns the parsed type to the callee.
3087e5dd7070Spatrick if (stopIfCastExpr) {
3088e5dd7070Spatrick TypeResult Ty;
3089e5dd7070Spatrick {
3090e5dd7070Spatrick InMessageExpressionRAIIObject InMessage(*this, false);
3091e5dd7070Spatrick Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
3092e5dd7070Spatrick }
3093e5dd7070Spatrick CastTy = Ty.get();
3094e5dd7070Spatrick return ExprResult();
3095e5dd7070Spatrick }
3096e5dd7070Spatrick
3097e5dd7070Spatrick // Reject the cast of super idiom in ObjC.
3098e5dd7070Spatrick if (Tok.is(tok::identifier) && getLangOpts().ObjC &&
3099e5dd7070Spatrick Tok.getIdentifierInfo() == Ident_super &&
3100e5dd7070Spatrick getCurScope()->isInObjcMethodScope() &&
3101e5dd7070Spatrick GetLookAheadToken(1).isNot(tok::period)) {
3102e5dd7070Spatrick Diag(Tok.getLocation(), diag::err_illegal_super_cast)
3103e5dd7070Spatrick << SourceRange(OpenLoc, RParenLoc);
3104e5dd7070Spatrick return ExprError();
3105e5dd7070Spatrick }
3106e5dd7070Spatrick
3107e5dd7070Spatrick PreferredType.enterTypeCast(Tok.getLocation(), CastTy.get());
3108e5dd7070Spatrick // Parse the cast-expression that follows it next.
3109e5dd7070Spatrick // TODO: For cast expression with CastTy.
3110e5dd7070Spatrick Result = ParseCastExpression(/*isUnaryExpression=*/AnyCastExpr,
3111e5dd7070Spatrick /*isAddressOfOperand=*/false,
3112e5dd7070Spatrick /*isTypeCast=*/IsTypeCast);
3113e5dd7070Spatrick if (!Result.isInvalid()) {
3114e5dd7070Spatrick Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
3115e5dd7070Spatrick DeclaratorInfo, CastTy,
3116e5dd7070Spatrick RParenLoc, Result.get());
3117e5dd7070Spatrick }
3118e5dd7070Spatrick return Result;
3119e5dd7070Spatrick }
3120e5dd7070Spatrick
3121e5dd7070Spatrick Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
3122e5dd7070Spatrick return ExprError();
3123e5dd7070Spatrick }
3124e5dd7070Spatrick } else if (ExprType >= FoldExpr && Tok.is(tok::ellipsis) &&
3125e5dd7070Spatrick isFoldOperator(NextToken().getKind())) {
3126e5dd7070Spatrick ExprType = FoldExpr;
3127e5dd7070Spatrick return ParseFoldExpression(ExprResult(), T);
3128e5dd7070Spatrick } else if (isTypeCast) {
3129e5dd7070Spatrick // Parse the expression-list.
3130e5dd7070Spatrick InMessageExpressionRAIIObject InMessage(*this, false);
3131e5dd7070Spatrick ExprVector ArgExprs;
3132e5dd7070Spatrick
3133*12c85518Srobert if (!ParseSimpleExpressionList(ArgExprs)) {
3134e5dd7070Spatrick // FIXME: If we ever support comma expressions as operands to
3135e5dd7070Spatrick // fold-expressions, we'll need to allow multiple ArgExprs here.
3136e5dd7070Spatrick if (ExprType >= FoldExpr && ArgExprs.size() == 1 &&
3137e5dd7070Spatrick isFoldOperator(Tok.getKind()) && NextToken().is(tok::ellipsis)) {
3138e5dd7070Spatrick ExprType = FoldExpr;
3139e5dd7070Spatrick return ParseFoldExpression(ArgExprs[0], T);
3140e5dd7070Spatrick }
3141e5dd7070Spatrick
3142e5dd7070Spatrick ExprType = SimpleExpr;
3143e5dd7070Spatrick Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
3144e5dd7070Spatrick ArgExprs);
3145e5dd7070Spatrick }
3146ec727ea7Spatrick } else if (getLangOpts().OpenMP >= 50 && OpenMPDirectiveParsing &&
3147ec727ea7Spatrick ExprType == CastExpr && Tok.is(tok::l_square) &&
3148ec727ea7Spatrick tryParseOpenMPArrayShapingCastPart()) {
3149ec727ea7Spatrick bool ErrorFound = false;
3150ec727ea7Spatrick SmallVector<Expr *, 4> OMPDimensions;
3151ec727ea7Spatrick SmallVector<SourceRange, 4> OMPBracketsRanges;
3152ec727ea7Spatrick do {
3153ec727ea7Spatrick BalancedDelimiterTracker TS(*this, tok::l_square);
3154ec727ea7Spatrick TS.consumeOpen();
3155ec727ea7Spatrick ExprResult NumElements =
3156ec727ea7Spatrick Actions.CorrectDelayedTyposInExpr(ParseExpression());
3157ec727ea7Spatrick if (!NumElements.isUsable()) {
3158ec727ea7Spatrick ErrorFound = true;
3159ec727ea7Spatrick while (!SkipUntil(tok::r_square, tok::r_paren,
3160ec727ea7Spatrick StopAtSemi | StopBeforeMatch))
3161ec727ea7Spatrick ;
3162ec727ea7Spatrick }
3163ec727ea7Spatrick TS.consumeClose();
3164ec727ea7Spatrick OMPDimensions.push_back(NumElements.get());
3165ec727ea7Spatrick OMPBracketsRanges.push_back(TS.getRange());
3166ec727ea7Spatrick } while (Tok.isNot(tok::r_paren));
3167ec727ea7Spatrick // Match the ')'.
3168ec727ea7Spatrick T.consumeClose();
3169ec727ea7Spatrick RParenLoc = T.getCloseLocation();
3170ec727ea7Spatrick Result = Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
3171ec727ea7Spatrick if (ErrorFound) {
3172ec727ea7Spatrick Result = ExprError();
3173ec727ea7Spatrick } else if (!Result.isInvalid()) {
3174ec727ea7Spatrick Result = Actions.ActOnOMPArrayShapingExpr(
3175ec727ea7Spatrick Result.get(), OpenLoc, RParenLoc, OMPDimensions, OMPBracketsRanges);
3176ec727ea7Spatrick }
3177ec727ea7Spatrick return Result;
3178e5dd7070Spatrick } else {
3179e5dd7070Spatrick InMessageExpressionRAIIObject InMessage(*this, false);
3180e5dd7070Spatrick
3181e5dd7070Spatrick Result = ParseExpression(MaybeTypeCast);
3182*12c85518Srobert if (!getLangOpts().CPlusPlus && Result.isUsable()) {
3183e5dd7070Spatrick // Correct typos in non-C++ code earlier so that implicit-cast-like
3184e5dd7070Spatrick // expressions are parsed correctly.
3185e5dd7070Spatrick Result = Actions.CorrectDelayedTyposInExpr(Result);
3186e5dd7070Spatrick }
3187e5dd7070Spatrick
3188e5dd7070Spatrick if (ExprType >= FoldExpr && isFoldOperator(Tok.getKind()) &&
3189e5dd7070Spatrick NextToken().is(tok::ellipsis)) {
3190e5dd7070Spatrick ExprType = FoldExpr;
3191e5dd7070Spatrick return ParseFoldExpression(Result, T);
3192e5dd7070Spatrick }
3193e5dd7070Spatrick ExprType = SimpleExpr;
3194e5dd7070Spatrick
3195e5dd7070Spatrick // Don't build a paren expression unless we actually match a ')'.
3196e5dd7070Spatrick if (!Result.isInvalid() && Tok.is(tok::r_paren))
3197e5dd7070Spatrick Result =
3198e5dd7070Spatrick Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.get());
3199e5dd7070Spatrick }
3200e5dd7070Spatrick
3201e5dd7070Spatrick // Match the ')'.
3202e5dd7070Spatrick if (Result.isInvalid()) {
3203e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
3204e5dd7070Spatrick return ExprError();
3205e5dd7070Spatrick }
3206e5dd7070Spatrick
3207e5dd7070Spatrick T.consumeClose();
3208e5dd7070Spatrick RParenLoc = T.getCloseLocation();
3209e5dd7070Spatrick return Result;
3210e5dd7070Spatrick }
3211e5dd7070Spatrick
3212e5dd7070Spatrick /// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
3213e5dd7070Spatrick /// and we are at the left brace.
3214e5dd7070Spatrick ///
3215e5dd7070Spatrick /// \verbatim
3216e5dd7070Spatrick /// postfix-expression: [C99 6.5.2]
3217e5dd7070Spatrick /// '(' type-name ')' '{' initializer-list '}'
3218e5dd7070Spatrick /// '(' type-name ')' '{' initializer-list ',' '}'
3219e5dd7070Spatrick /// \endverbatim
3220e5dd7070Spatrick ExprResult
ParseCompoundLiteralExpression(ParsedType Ty,SourceLocation LParenLoc,SourceLocation RParenLoc)3221e5dd7070Spatrick Parser::ParseCompoundLiteralExpression(ParsedType Ty,
3222e5dd7070Spatrick SourceLocation LParenLoc,
3223e5dd7070Spatrick SourceLocation RParenLoc) {
3224e5dd7070Spatrick assert(Tok.is(tok::l_brace) && "Not a compound literal!");
3225e5dd7070Spatrick if (!getLangOpts().C99) // Compound literals don't exist in C90.
3226e5dd7070Spatrick Diag(LParenLoc, diag::ext_c99_compound_literal);
3227a9ac8606Spatrick PreferredType.enterTypeCast(Tok.getLocation(), Ty.get());
3228e5dd7070Spatrick ExprResult Result = ParseInitializer();
3229e5dd7070Spatrick if (!Result.isInvalid() && Ty)
3230e5dd7070Spatrick return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.get());
3231e5dd7070Spatrick return Result;
3232e5dd7070Spatrick }
3233e5dd7070Spatrick
3234e5dd7070Spatrick /// ParseStringLiteralExpression - This handles the various token types that
3235e5dd7070Spatrick /// form string literals, and also handles string concatenation [C99 5.1.1.2,
3236e5dd7070Spatrick /// translation phase #6].
3237e5dd7070Spatrick ///
3238e5dd7070Spatrick /// \verbatim
3239e5dd7070Spatrick /// primary-expression: [C99 6.5.1]
3240e5dd7070Spatrick /// string-literal
3241e5dd7070Spatrick /// \verbatim
ParseStringLiteralExpression(bool AllowUserDefinedLiteral)3242e5dd7070Spatrick ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
3243e5dd7070Spatrick assert(isTokenStringLiteral() && "Not a string literal!");
3244e5dd7070Spatrick
3245e5dd7070Spatrick // String concat. Note that keywords like __func__ and __FUNCTION__ are not
3246e5dd7070Spatrick // considered to be strings for concatenation purposes.
3247e5dd7070Spatrick SmallVector<Token, 4> StringToks;
3248e5dd7070Spatrick
3249e5dd7070Spatrick do {
3250e5dd7070Spatrick StringToks.push_back(Tok);
3251e5dd7070Spatrick ConsumeStringToken();
3252e5dd7070Spatrick } while (isTokenStringLiteral());
3253e5dd7070Spatrick
3254e5dd7070Spatrick // Pass the set of string tokens, ready for concatenation, to the actions.
3255e5dd7070Spatrick return Actions.ActOnStringLiteral(StringToks,
3256e5dd7070Spatrick AllowUserDefinedLiteral ? getCurScope()
3257e5dd7070Spatrick : nullptr);
3258e5dd7070Spatrick }
3259e5dd7070Spatrick
3260e5dd7070Spatrick /// ParseGenericSelectionExpression - Parse a C11 generic-selection
3261e5dd7070Spatrick /// [C11 6.5.1.1].
3262e5dd7070Spatrick ///
3263e5dd7070Spatrick /// \verbatim
3264e5dd7070Spatrick /// generic-selection:
3265e5dd7070Spatrick /// _Generic ( assignment-expression , generic-assoc-list )
3266e5dd7070Spatrick /// generic-assoc-list:
3267e5dd7070Spatrick /// generic-association
3268e5dd7070Spatrick /// generic-assoc-list , generic-association
3269e5dd7070Spatrick /// generic-association:
3270e5dd7070Spatrick /// type-name : assignment-expression
3271e5dd7070Spatrick /// default : assignment-expression
3272e5dd7070Spatrick /// \endverbatim
ParseGenericSelectionExpression()3273e5dd7070Spatrick ExprResult Parser::ParseGenericSelectionExpression() {
3274e5dd7070Spatrick assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
3275e5dd7070Spatrick if (!getLangOpts().C11)
3276e5dd7070Spatrick Diag(Tok, diag::ext_c11_feature) << Tok.getName();
3277e5dd7070Spatrick
3278e5dd7070Spatrick SourceLocation KeyLoc = ConsumeToken();
3279e5dd7070Spatrick BalancedDelimiterTracker T(*this, tok::l_paren);
3280e5dd7070Spatrick if (T.expectAndConsume())
3281e5dd7070Spatrick return ExprError();
3282e5dd7070Spatrick
3283e5dd7070Spatrick ExprResult ControllingExpr;
3284e5dd7070Spatrick {
3285e5dd7070Spatrick // C11 6.5.1.1p3 "The controlling expression of a generic selection is
3286e5dd7070Spatrick // not evaluated."
3287e5dd7070Spatrick EnterExpressionEvaluationContext Unevaluated(
3288e5dd7070Spatrick Actions, Sema::ExpressionEvaluationContext::Unevaluated);
3289e5dd7070Spatrick ControllingExpr =
3290e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
3291e5dd7070Spatrick if (ControllingExpr.isInvalid()) {
3292e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
3293e5dd7070Spatrick return ExprError();
3294e5dd7070Spatrick }
3295e5dd7070Spatrick }
3296e5dd7070Spatrick
3297e5dd7070Spatrick if (ExpectAndConsume(tok::comma)) {
3298e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
3299e5dd7070Spatrick return ExprError();
3300e5dd7070Spatrick }
3301e5dd7070Spatrick
3302e5dd7070Spatrick SourceLocation DefaultLoc;
3303*12c85518Srobert SmallVector<ParsedType, 12> Types;
3304e5dd7070Spatrick ExprVector Exprs;
3305e5dd7070Spatrick do {
3306e5dd7070Spatrick ParsedType Ty;
3307e5dd7070Spatrick if (Tok.is(tok::kw_default)) {
3308e5dd7070Spatrick // C11 6.5.1.1p2 "A generic selection shall have no more than one default
3309e5dd7070Spatrick // generic association."
3310e5dd7070Spatrick if (!DefaultLoc.isInvalid()) {
3311e5dd7070Spatrick Diag(Tok, diag::err_duplicate_default_assoc);
3312e5dd7070Spatrick Diag(DefaultLoc, diag::note_previous_default_assoc);
3313e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
3314e5dd7070Spatrick return ExprError();
3315e5dd7070Spatrick }
3316e5dd7070Spatrick DefaultLoc = ConsumeToken();
3317e5dd7070Spatrick Ty = nullptr;
3318e5dd7070Spatrick } else {
3319e5dd7070Spatrick ColonProtectionRAIIObject X(*this);
3320*12c85518Srobert TypeResult TR = ParseTypeName(nullptr, DeclaratorContext::Association);
3321e5dd7070Spatrick if (TR.isInvalid()) {
3322e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
3323e5dd7070Spatrick return ExprError();
3324e5dd7070Spatrick }
3325e5dd7070Spatrick Ty = TR.get();
3326e5dd7070Spatrick }
3327e5dd7070Spatrick Types.push_back(Ty);
3328e5dd7070Spatrick
3329e5dd7070Spatrick if (ExpectAndConsume(tok::colon)) {
3330e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
3331e5dd7070Spatrick return ExprError();
3332e5dd7070Spatrick }
3333e5dd7070Spatrick
3334e5dd7070Spatrick // FIXME: These expressions should be parsed in a potentially potentially
3335e5dd7070Spatrick // evaluated context.
3336e5dd7070Spatrick ExprResult ER(
3337e5dd7070Spatrick Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()));
3338e5dd7070Spatrick if (ER.isInvalid()) {
3339e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
3340e5dd7070Spatrick return ExprError();
3341e5dd7070Spatrick }
3342e5dd7070Spatrick Exprs.push_back(ER.get());
3343e5dd7070Spatrick } while (TryConsumeToken(tok::comma));
3344e5dd7070Spatrick
3345e5dd7070Spatrick T.consumeClose();
3346e5dd7070Spatrick if (T.getCloseLocation().isInvalid())
3347e5dd7070Spatrick return ExprError();
3348e5dd7070Spatrick
3349e5dd7070Spatrick return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
3350e5dd7070Spatrick T.getCloseLocation(),
3351e5dd7070Spatrick ControllingExpr.get(),
3352e5dd7070Spatrick Types, Exprs);
3353e5dd7070Spatrick }
3354e5dd7070Spatrick
3355e5dd7070Spatrick /// Parse A C++1z fold-expression after the opening paren and optional
3356e5dd7070Spatrick /// left-hand-side expression.
3357e5dd7070Spatrick ///
3358e5dd7070Spatrick /// \verbatim
3359e5dd7070Spatrick /// fold-expression:
3360e5dd7070Spatrick /// ( cast-expression fold-operator ... )
3361e5dd7070Spatrick /// ( ... fold-operator cast-expression )
3362e5dd7070Spatrick /// ( cast-expression fold-operator ... fold-operator cast-expression )
ParseFoldExpression(ExprResult LHS,BalancedDelimiterTracker & T)3363e5dd7070Spatrick ExprResult Parser::ParseFoldExpression(ExprResult LHS,
3364e5dd7070Spatrick BalancedDelimiterTracker &T) {
3365e5dd7070Spatrick if (LHS.isInvalid()) {
3366e5dd7070Spatrick T.skipToEnd();
3367e5dd7070Spatrick return true;
3368e5dd7070Spatrick }
3369e5dd7070Spatrick
3370e5dd7070Spatrick tok::TokenKind Kind = tok::unknown;
3371e5dd7070Spatrick SourceLocation FirstOpLoc;
3372e5dd7070Spatrick if (LHS.isUsable()) {
3373e5dd7070Spatrick Kind = Tok.getKind();
3374e5dd7070Spatrick assert(isFoldOperator(Kind) && "missing fold-operator");
3375e5dd7070Spatrick FirstOpLoc = ConsumeToken();
3376e5dd7070Spatrick }
3377e5dd7070Spatrick
3378e5dd7070Spatrick assert(Tok.is(tok::ellipsis) && "not a fold-expression");
3379e5dd7070Spatrick SourceLocation EllipsisLoc = ConsumeToken();
3380e5dd7070Spatrick
3381e5dd7070Spatrick ExprResult RHS;
3382e5dd7070Spatrick if (Tok.isNot(tok::r_paren)) {
3383e5dd7070Spatrick if (!isFoldOperator(Tok.getKind()))
3384e5dd7070Spatrick return Diag(Tok.getLocation(), diag::err_expected_fold_operator);
3385e5dd7070Spatrick
3386e5dd7070Spatrick if (Kind != tok::unknown && Tok.getKind() != Kind)
3387e5dd7070Spatrick Diag(Tok.getLocation(), diag::err_fold_operator_mismatch)
3388e5dd7070Spatrick << SourceRange(FirstOpLoc);
3389e5dd7070Spatrick Kind = Tok.getKind();
3390e5dd7070Spatrick ConsumeToken();
3391e5dd7070Spatrick
3392e5dd7070Spatrick RHS = ParseExpression();
3393e5dd7070Spatrick if (RHS.isInvalid()) {
3394e5dd7070Spatrick T.skipToEnd();
3395e5dd7070Spatrick return true;
3396e5dd7070Spatrick }
3397e5dd7070Spatrick }
3398e5dd7070Spatrick
3399e5dd7070Spatrick Diag(EllipsisLoc, getLangOpts().CPlusPlus17
3400e5dd7070Spatrick ? diag::warn_cxx14_compat_fold_expression
3401e5dd7070Spatrick : diag::ext_fold_expression);
3402e5dd7070Spatrick
3403e5dd7070Spatrick T.consumeClose();
3404a9ac8606Spatrick return Actions.ActOnCXXFoldExpr(getCurScope(), T.getOpenLocation(), LHS.get(),
3405a9ac8606Spatrick Kind, EllipsisLoc, RHS.get(),
3406a9ac8606Spatrick T.getCloseLocation());
3407e5dd7070Spatrick }
3408e5dd7070Spatrick
3409e5dd7070Spatrick /// ParseExpressionList - Used for C/C++ (argument-)expression-list.
3410e5dd7070Spatrick ///
3411e5dd7070Spatrick /// \verbatim
3412e5dd7070Spatrick /// argument-expression-list:
3413e5dd7070Spatrick /// assignment-expression
3414e5dd7070Spatrick /// argument-expression-list , assignment-expression
3415e5dd7070Spatrick ///
3416e5dd7070Spatrick /// [C++] expression-list:
3417e5dd7070Spatrick /// [C++] assignment-expression
3418e5dd7070Spatrick /// [C++] expression-list , assignment-expression
3419e5dd7070Spatrick ///
3420e5dd7070Spatrick /// [C++0x] expression-list:
3421e5dd7070Spatrick /// [C++0x] initializer-list
3422e5dd7070Spatrick ///
3423e5dd7070Spatrick /// [C++0x] initializer-list
3424e5dd7070Spatrick /// [C++0x] initializer-clause ...[opt]
3425e5dd7070Spatrick /// [C++0x] initializer-list , initializer-clause ...[opt]
3426e5dd7070Spatrick ///
3427e5dd7070Spatrick /// [C++0x] initializer-clause:
3428e5dd7070Spatrick /// [C++0x] assignment-expression
3429e5dd7070Spatrick /// [C++0x] braced-init-list
3430e5dd7070Spatrick /// \endverbatim
ParseExpressionList(SmallVectorImpl<Expr * > & Exprs,llvm::function_ref<void ()> ExpressionStarts,bool FailImmediatelyOnInvalidExpr,bool EarlyTypoCorrection)3431e5dd7070Spatrick bool Parser::ParseExpressionList(SmallVectorImpl<Expr *> &Exprs,
3432*12c85518Srobert llvm::function_ref<void()> ExpressionStarts,
3433*12c85518Srobert bool FailImmediatelyOnInvalidExpr,
3434*12c85518Srobert bool EarlyTypoCorrection) {
3435e5dd7070Spatrick bool SawError = false;
3436*12c85518Srobert while (true) {
3437e5dd7070Spatrick if (ExpressionStarts)
3438e5dd7070Spatrick ExpressionStarts();
3439e5dd7070Spatrick
3440e5dd7070Spatrick ExprResult Expr;
3441e5dd7070Spatrick if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3442e5dd7070Spatrick Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3443e5dd7070Spatrick Expr = ParseBraceInitializer();
3444e5dd7070Spatrick } else
3445e5dd7070Spatrick Expr = ParseAssignmentExpression();
3446e5dd7070Spatrick
3447*12c85518Srobert if (EarlyTypoCorrection)
3448*12c85518Srobert Expr = Actions.CorrectDelayedTyposInExpr(Expr);
3449*12c85518Srobert
3450e5dd7070Spatrick if (Tok.is(tok::ellipsis))
3451e5dd7070Spatrick Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
3452ec727ea7Spatrick else if (Tok.is(tok::code_completion)) {
3453ec727ea7Spatrick // There's nothing to suggest in here as we parsed a full expression.
3454ec727ea7Spatrick // Instead fail and propogate the error since caller might have something
3455ec727ea7Spatrick // the suggest, e.g. signature help in function call. Note that this is
3456ec727ea7Spatrick // performed before pushing the \p Expr, so that signature help can report
3457ec727ea7Spatrick // current argument correctly.
3458ec727ea7Spatrick SawError = true;
3459ec727ea7Spatrick cutOffParsing();
3460ec727ea7Spatrick break;
3461ec727ea7Spatrick }
3462e5dd7070Spatrick if (Expr.isInvalid()) {
3463e5dd7070Spatrick SawError = true;
3464*12c85518Srobert if (FailImmediatelyOnInvalidExpr)
3465*12c85518Srobert break;
3466*12c85518Srobert SkipUntil(tok::comma, tok::r_paren, StopBeforeMatch);
3467e5dd7070Spatrick } else {
3468e5dd7070Spatrick Exprs.push_back(Expr.get());
3469e5dd7070Spatrick }
3470e5dd7070Spatrick
3471e5dd7070Spatrick if (Tok.isNot(tok::comma))
3472e5dd7070Spatrick break;
3473e5dd7070Spatrick // Move to the next argument, remember where the comma was.
3474e5dd7070Spatrick Token Comma = Tok;
3475*12c85518Srobert ConsumeToken();
3476e5dd7070Spatrick checkPotentialAngleBracketDelimiter(Comma);
3477e5dd7070Spatrick }
3478e5dd7070Spatrick if (SawError) {
3479e5dd7070Spatrick // Ensure typos get diagnosed when errors were encountered while parsing the
3480e5dd7070Spatrick // expression list.
3481e5dd7070Spatrick for (auto &E : Exprs) {
3482e5dd7070Spatrick ExprResult Expr = Actions.CorrectDelayedTyposInExpr(E);
3483e5dd7070Spatrick if (Expr.isUsable()) E = Expr.get();
3484e5dd7070Spatrick }
3485e5dd7070Spatrick }
3486e5dd7070Spatrick return SawError;
3487e5dd7070Spatrick }
3488e5dd7070Spatrick
3489e5dd7070Spatrick /// ParseSimpleExpressionList - A simple comma-separated list of expressions,
3490e5dd7070Spatrick /// used for misc language extensions.
3491e5dd7070Spatrick ///
3492e5dd7070Spatrick /// \verbatim
3493e5dd7070Spatrick /// simple-expression-list:
3494e5dd7070Spatrick /// assignment-expression
3495e5dd7070Spatrick /// simple-expression-list , assignment-expression
3496e5dd7070Spatrick /// \endverbatim
ParseSimpleExpressionList(SmallVectorImpl<Expr * > & Exprs)3497*12c85518Srobert bool Parser::ParseSimpleExpressionList(SmallVectorImpl<Expr *> &Exprs) {
3498*12c85518Srobert while (true) {
3499e5dd7070Spatrick ExprResult Expr = ParseAssignmentExpression();
3500e5dd7070Spatrick if (Expr.isInvalid())
3501e5dd7070Spatrick return true;
3502e5dd7070Spatrick
3503e5dd7070Spatrick Exprs.push_back(Expr.get());
3504e5dd7070Spatrick
3505*12c85518Srobert // We might be parsing the LHS of a fold-expression. If we reached the fold
3506*12c85518Srobert // operator, stop.
3507*12c85518Srobert if (Tok.isNot(tok::comma) || NextToken().is(tok::ellipsis))
3508e5dd7070Spatrick return false;
3509e5dd7070Spatrick
3510e5dd7070Spatrick // Move to the next argument, remember where the comma was.
3511e5dd7070Spatrick Token Comma = Tok;
3512*12c85518Srobert ConsumeToken();
3513e5dd7070Spatrick checkPotentialAngleBracketDelimiter(Comma);
3514e5dd7070Spatrick }
3515e5dd7070Spatrick }
3516e5dd7070Spatrick
3517e5dd7070Spatrick /// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
3518e5dd7070Spatrick ///
3519e5dd7070Spatrick /// \verbatim
3520e5dd7070Spatrick /// [clang] block-id:
3521e5dd7070Spatrick /// [clang] specifier-qualifier-list block-declarator
3522e5dd7070Spatrick /// \endverbatim
ParseBlockId(SourceLocation CaretLoc)3523e5dd7070Spatrick void Parser::ParseBlockId(SourceLocation CaretLoc) {
3524e5dd7070Spatrick if (Tok.is(tok::code_completion)) {
3525a9ac8606Spatrick cutOffParsing();
3526e5dd7070Spatrick Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
3527a9ac8606Spatrick return;
3528e5dd7070Spatrick }
3529e5dd7070Spatrick
3530e5dd7070Spatrick // Parse the specifier-qualifier-list piece.
3531e5dd7070Spatrick DeclSpec DS(AttrFactory);
3532e5dd7070Spatrick ParseSpecifierQualifierList(DS);
3533e5dd7070Spatrick
3534e5dd7070Spatrick // Parse the block-declarator.
3535*12c85518Srobert Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
3536*12c85518Srobert DeclaratorContext::BlockLiteral);
3537a9ac8606Spatrick DeclaratorInfo.setFunctionDefinitionKind(FunctionDefinitionKind::Definition);
3538e5dd7070Spatrick ParseDeclarator(DeclaratorInfo);
3539e5dd7070Spatrick
3540e5dd7070Spatrick MaybeParseGNUAttributes(DeclaratorInfo);
3541e5dd7070Spatrick
3542e5dd7070Spatrick // Inform sema that we are starting a block.
3543e5dd7070Spatrick Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
3544e5dd7070Spatrick }
3545e5dd7070Spatrick
3546e5dd7070Spatrick /// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
3547e5dd7070Spatrick /// like ^(int x){ return x+1; }
3548e5dd7070Spatrick ///
3549e5dd7070Spatrick /// \verbatim
3550e5dd7070Spatrick /// block-literal:
3551e5dd7070Spatrick /// [clang] '^' block-args[opt] compound-statement
3552e5dd7070Spatrick /// [clang] '^' block-id compound-statement
3553e5dd7070Spatrick /// [clang] block-args:
3554e5dd7070Spatrick /// [clang] '(' parameter-list ')'
3555e5dd7070Spatrick /// \endverbatim
ParseBlockLiteralExpression()3556e5dd7070Spatrick ExprResult Parser::ParseBlockLiteralExpression() {
3557e5dd7070Spatrick assert(Tok.is(tok::caret) && "block literal starts with ^");
3558e5dd7070Spatrick SourceLocation CaretLoc = ConsumeToken();
3559e5dd7070Spatrick
3560e5dd7070Spatrick PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
3561e5dd7070Spatrick "block literal parsing");
3562e5dd7070Spatrick
3563e5dd7070Spatrick // Enter a scope to hold everything within the block. This includes the
3564e5dd7070Spatrick // argument decls, decls within the compound expression, etc. This also
3565e5dd7070Spatrick // allows determining whether a variable reference inside the block is
3566e5dd7070Spatrick // within or outside of the block.
3567e5dd7070Spatrick ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
3568e5dd7070Spatrick Scope::CompoundStmtScope | Scope::DeclScope);
3569e5dd7070Spatrick
3570e5dd7070Spatrick // Inform sema that we are starting a block.
3571e5dd7070Spatrick Actions.ActOnBlockStart(CaretLoc, getCurScope());
3572e5dd7070Spatrick
3573e5dd7070Spatrick // Parse the return type if present.
3574e5dd7070Spatrick DeclSpec DS(AttrFactory);
3575*12c85518Srobert Declarator ParamInfo(DS, ParsedAttributesView::none(),
3576*12c85518Srobert DeclaratorContext::BlockLiteral);
3577a9ac8606Spatrick ParamInfo.setFunctionDefinitionKind(FunctionDefinitionKind::Definition);
3578e5dd7070Spatrick // FIXME: Since the return type isn't actually parsed, it can't be used to
3579e5dd7070Spatrick // fill ParamInfo with an initial valid range, so do it manually.
3580e5dd7070Spatrick ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
3581e5dd7070Spatrick
3582e5dd7070Spatrick // If this block has arguments, parse them. There is no ambiguity here with
3583e5dd7070Spatrick // the expression case, because the expression case requires a parameter list.
3584e5dd7070Spatrick if (Tok.is(tok::l_paren)) {
3585e5dd7070Spatrick ParseParenDeclarator(ParamInfo);
3586e5dd7070Spatrick // Parse the pieces after the identifier as if we had "int(...)".
3587e5dd7070Spatrick // SetIdentifier sets the source range end, but in this case we're past
3588e5dd7070Spatrick // that location.
3589e5dd7070Spatrick SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
3590e5dd7070Spatrick ParamInfo.SetIdentifier(nullptr, CaretLoc);
3591e5dd7070Spatrick ParamInfo.SetRangeEnd(Tmp);
3592e5dd7070Spatrick if (ParamInfo.isInvalidType()) {
3593e5dd7070Spatrick // If there was an error parsing the arguments, they may have
3594e5dd7070Spatrick // tried to use ^(x+y) which requires an argument list. Just
3595e5dd7070Spatrick // skip the whole block literal.
3596e5dd7070Spatrick Actions.ActOnBlockError(CaretLoc, getCurScope());
3597e5dd7070Spatrick return ExprError();
3598e5dd7070Spatrick }
3599e5dd7070Spatrick
3600e5dd7070Spatrick MaybeParseGNUAttributes(ParamInfo);
3601e5dd7070Spatrick
3602e5dd7070Spatrick // Inform sema that we are starting a block.
3603e5dd7070Spatrick Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
3604e5dd7070Spatrick } else if (!Tok.is(tok::l_brace)) {
3605e5dd7070Spatrick ParseBlockId(CaretLoc);
3606e5dd7070Spatrick } else {
3607e5dd7070Spatrick // Otherwise, pretend we saw (void).
3608e5dd7070Spatrick SourceLocation NoLoc;
3609e5dd7070Spatrick ParamInfo.AddTypeInfo(
3610e5dd7070Spatrick DeclaratorChunk::getFunction(/*HasProto=*/true,
3611e5dd7070Spatrick /*IsAmbiguous=*/false,
3612e5dd7070Spatrick /*RParenLoc=*/NoLoc,
3613e5dd7070Spatrick /*ArgInfo=*/nullptr,
3614e5dd7070Spatrick /*NumParams=*/0,
3615e5dd7070Spatrick /*EllipsisLoc=*/NoLoc,
3616e5dd7070Spatrick /*RParenLoc=*/NoLoc,
3617e5dd7070Spatrick /*RefQualifierIsLvalueRef=*/true,
3618e5dd7070Spatrick /*RefQualifierLoc=*/NoLoc,
3619e5dd7070Spatrick /*MutableLoc=*/NoLoc, EST_None,
3620e5dd7070Spatrick /*ESpecRange=*/SourceRange(),
3621e5dd7070Spatrick /*Exceptions=*/nullptr,
3622e5dd7070Spatrick /*ExceptionRanges=*/nullptr,
3623e5dd7070Spatrick /*NumExceptions=*/0,
3624e5dd7070Spatrick /*NoexceptExpr=*/nullptr,
3625e5dd7070Spatrick /*ExceptionSpecTokens=*/nullptr,
3626*12c85518Srobert /*DeclsInPrototype=*/std::nullopt,
3627*12c85518Srobert CaretLoc, CaretLoc, ParamInfo),
3628e5dd7070Spatrick CaretLoc);
3629e5dd7070Spatrick
3630e5dd7070Spatrick MaybeParseGNUAttributes(ParamInfo);
3631e5dd7070Spatrick
3632e5dd7070Spatrick // Inform sema that we are starting a block.
3633e5dd7070Spatrick Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
3634e5dd7070Spatrick }
3635e5dd7070Spatrick
3636e5dd7070Spatrick
3637e5dd7070Spatrick ExprResult Result(true);
3638e5dd7070Spatrick if (!Tok.is(tok::l_brace)) {
3639e5dd7070Spatrick // Saw something like: ^expr
3640e5dd7070Spatrick Diag(Tok, diag::err_expected_expression);
3641e5dd7070Spatrick Actions.ActOnBlockError(CaretLoc, getCurScope());
3642e5dd7070Spatrick return ExprError();
3643e5dd7070Spatrick }
3644e5dd7070Spatrick
3645e5dd7070Spatrick StmtResult Stmt(ParseCompoundStatementBody());
3646e5dd7070Spatrick BlockScope.Exit();
3647e5dd7070Spatrick if (!Stmt.isInvalid())
3648e5dd7070Spatrick Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.get(), getCurScope());
3649e5dd7070Spatrick else
3650e5dd7070Spatrick Actions.ActOnBlockError(CaretLoc, getCurScope());
3651e5dd7070Spatrick return Result;
3652e5dd7070Spatrick }
3653e5dd7070Spatrick
3654e5dd7070Spatrick /// ParseObjCBoolLiteral - This handles the objective-c Boolean literals.
3655e5dd7070Spatrick ///
3656e5dd7070Spatrick /// '__objc_yes'
3657e5dd7070Spatrick /// '__objc_no'
ParseObjCBoolLiteral()3658e5dd7070Spatrick ExprResult Parser::ParseObjCBoolLiteral() {
3659e5dd7070Spatrick tok::TokenKind Kind = Tok.getKind();
3660e5dd7070Spatrick return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
3661e5dd7070Spatrick }
3662e5dd7070Spatrick
3663e5dd7070Spatrick /// Validate availability spec list, emitting diagnostics if necessary. Returns
3664e5dd7070Spatrick /// true if invalid.
CheckAvailabilitySpecList(Parser & P,ArrayRef<AvailabilitySpec> AvailSpecs)3665e5dd7070Spatrick static bool CheckAvailabilitySpecList(Parser &P,
3666e5dd7070Spatrick ArrayRef<AvailabilitySpec> AvailSpecs) {
3667e5dd7070Spatrick llvm::SmallSet<StringRef, 4> Platforms;
3668e5dd7070Spatrick bool HasOtherPlatformSpec = false;
3669e5dd7070Spatrick bool Valid = true;
3670e5dd7070Spatrick for (const auto &Spec : AvailSpecs) {
3671e5dd7070Spatrick if (Spec.isOtherPlatformSpec()) {
3672e5dd7070Spatrick if (HasOtherPlatformSpec) {
3673e5dd7070Spatrick P.Diag(Spec.getBeginLoc(), diag::err_availability_query_repeated_star);
3674e5dd7070Spatrick Valid = false;
3675e5dd7070Spatrick }
3676e5dd7070Spatrick
3677e5dd7070Spatrick HasOtherPlatformSpec = true;
3678e5dd7070Spatrick continue;
3679e5dd7070Spatrick }
3680e5dd7070Spatrick
3681e5dd7070Spatrick bool Inserted = Platforms.insert(Spec.getPlatform()).second;
3682e5dd7070Spatrick if (!Inserted) {
3683e5dd7070Spatrick // Rule out multiple version specs referring to the same platform.
3684e5dd7070Spatrick // For example, we emit an error for:
3685e5dd7070Spatrick // @available(macos 10.10, macos 10.11, *)
3686e5dd7070Spatrick StringRef Platform = Spec.getPlatform();
3687e5dd7070Spatrick P.Diag(Spec.getBeginLoc(), diag::err_availability_query_repeated_platform)
3688e5dd7070Spatrick << Spec.getEndLoc() << Platform;
3689e5dd7070Spatrick Valid = false;
3690e5dd7070Spatrick }
3691e5dd7070Spatrick }
3692e5dd7070Spatrick
3693e5dd7070Spatrick if (!HasOtherPlatformSpec) {
3694e5dd7070Spatrick SourceLocation InsertWildcardLoc = AvailSpecs.back().getEndLoc();
3695e5dd7070Spatrick P.Diag(InsertWildcardLoc, diag::err_availability_query_wildcard_required)
3696e5dd7070Spatrick << FixItHint::CreateInsertion(InsertWildcardLoc, ", *");
3697e5dd7070Spatrick return true;
3698e5dd7070Spatrick }
3699e5dd7070Spatrick
3700e5dd7070Spatrick return !Valid;
3701e5dd7070Spatrick }
3702e5dd7070Spatrick
3703e5dd7070Spatrick /// Parse availability query specification.
3704e5dd7070Spatrick ///
3705e5dd7070Spatrick /// availability-spec:
3706e5dd7070Spatrick /// '*'
3707e5dd7070Spatrick /// identifier version-tuple
ParseAvailabilitySpec()3708*12c85518Srobert std::optional<AvailabilitySpec> Parser::ParseAvailabilitySpec() {
3709e5dd7070Spatrick if (Tok.is(tok::star)) {
3710e5dd7070Spatrick return AvailabilitySpec(ConsumeToken());
3711e5dd7070Spatrick } else {
3712e5dd7070Spatrick // Parse the platform name.
3713e5dd7070Spatrick if (Tok.is(tok::code_completion)) {
3714e5dd7070Spatrick cutOffParsing();
3715a9ac8606Spatrick Actions.CodeCompleteAvailabilityPlatformName();
3716*12c85518Srobert return std::nullopt;
3717e5dd7070Spatrick }
3718e5dd7070Spatrick if (Tok.isNot(tok::identifier)) {
3719e5dd7070Spatrick Diag(Tok, diag::err_avail_query_expected_platform_name);
3720*12c85518Srobert return std::nullopt;
3721e5dd7070Spatrick }
3722e5dd7070Spatrick
3723e5dd7070Spatrick IdentifierLoc *PlatformIdentifier = ParseIdentifierLoc();
3724e5dd7070Spatrick SourceRange VersionRange;
3725e5dd7070Spatrick VersionTuple Version = ParseVersionTuple(VersionRange);
3726e5dd7070Spatrick
3727e5dd7070Spatrick if (Version.empty())
3728*12c85518Srobert return std::nullopt;
3729e5dd7070Spatrick
3730e5dd7070Spatrick StringRef GivenPlatform = PlatformIdentifier->Ident->getName();
3731e5dd7070Spatrick StringRef Platform =
3732e5dd7070Spatrick AvailabilityAttr::canonicalizePlatformName(GivenPlatform);
3733e5dd7070Spatrick
3734e5dd7070Spatrick if (AvailabilityAttr::getPrettyPlatformName(Platform).empty()) {
3735e5dd7070Spatrick Diag(PlatformIdentifier->Loc,
3736e5dd7070Spatrick diag::err_avail_query_unrecognized_platform_name)
3737e5dd7070Spatrick << GivenPlatform;
3738*12c85518Srobert return std::nullopt;
3739e5dd7070Spatrick }
3740e5dd7070Spatrick
3741e5dd7070Spatrick return AvailabilitySpec(Version, Platform, PlatformIdentifier->Loc,
3742e5dd7070Spatrick VersionRange.getEnd());
3743e5dd7070Spatrick }
3744e5dd7070Spatrick }
3745e5dd7070Spatrick
ParseAvailabilityCheckExpr(SourceLocation BeginLoc)3746e5dd7070Spatrick ExprResult Parser::ParseAvailabilityCheckExpr(SourceLocation BeginLoc) {
3747e5dd7070Spatrick assert(Tok.is(tok::kw___builtin_available) ||
3748e5dd7070Spatrick Tok.isObjCAtKeyword(tok::objc_available));
3749e5dd7070Spatrick
3750e5dd7070Spatrick // Eat the available or __builtin_available.
3751e5dd7070Spatrick ConsumeToken();
3752e5dd7070Spatrick
3753e5dd7070Spatrick BalancedDelimiterTracker Parens(*this, tok::l_paren);
3754e5dd7070Spatrick if (Parens.expectAndConsume())
3755e5dd7070Spatrick return ExprError();
3756e5dd7070Spatrick
3757e5dd7070Spatrick SmallVector<AvailabilitySpec, 4> AvailSpecs;
3758e5dd7070Spatrick bool HasError = false;
3759e5dd7070Spatrick while (true) {
3760*12c85518Srobert std::optional<AvailabilitySpec> Spec = ParseAvailabilitySpec();
3761e5dd7070Spatrick if (!Spec)
3762e5dd7070Spatrick HasError = true;
3763e5dd7070Spatrick else
3764e5dd7070Spatrick AvailSpecs.push_back(*Spec);
3765e5dd7070Spatrick
3766e5dd7070Spatrick if (!TryConsumeToken(tok::comma))
3767e5dd7070Spatrick break;
3768e5dd7070Spatrick }
3769e5dd7070Spatrick
3770e5dd7070Spatrick if (HasError) {
3771e5dd7070Spatrick SkipUntil(tok::r_paren, StopAtSemi);
3772e5dd7070Spatrick return ExprError();
3773e5dd7070Spatrick }
3774e5dd7070Spatrick
3775e5dd7070Spatrick CheckAvailabilitySpecList(*this, AvailSpecs);
3776e5dd7070Spatrick
3777e5dd7070Spatrick if (Parens.consumeClose())
3778e5dd7070Spatrick return ExprError();
3779e5dd7070Spatrick
3780e5dd7070Spatrick return Actions.ActOnObjCAvailabilityCheckExpr(AvailSpecs, BeginLoc,
3781e5dd7070Spatrick Parens.getCloseLocation());
3782e5dd7070Spatrick }
3783