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