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