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