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