clang 24.0.0git
SemaOpenACCAtomic.cpp
Go to the documentation of this file.
1//== SemaOpenACCAtomic.cpp - Semantic Analysis for OpenACC Atomic Construct===//
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/// \file
9/// This file implements semantic analysis for the OpenACC atomic construct.
10///
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/ExprCXX.h"
16
17#include <optional>
18
19using namespace clang;
20
21namespace {
22
23class AtomicOperandChecker {
24 SemaOpenACC &SemaRef;
25 OpenACCAtomicKind AtKind;
26 SourceLocation AtomicDirLoc;
27 StmtResult AssocStmt;
28
29 // Do a diagnostic, which sets the correct error, then displays passed note.
30 bool DiagnoseInvalidAtomic(SourceLocation Loc, PartialDiagnostic NoteDiag) {
31 SemaRef.Diag(AtomicDirLoc, diag::err_acc_invalid_atomic)
32 << (AtKind != OpenACCAtomicKind::None) << AtKind;
33 SemaRef.Diag(Loc, NoteDiag);
34 return true;
35 }
36
37 // Create a replacement recovery expr in case we find an error here. This
38 // allows us to ignore this during template instantiation so we only get a
39 // single error.
40 StmtResult getRecoveryExpr() {
41 if (!AssocStmt.isUsable())
42 return AssocStmt;
43
44 if (!SemaRef.getASTContext().getLangOpts().RecoveryAST)
45 return StmtError();
46
47 Expr *E = dyn_cast<Expr>(AssocStmt.get());
48
49 // DeclStmt can't be wrapped in a RecoveryExpr, and dropping it causes
50 // template instantiation to not be able to find the declaration, so we have
51 // to continue representing it as itself.
52 if (isa<DeclStmt>(AssocStmt.get()))
53 return AssocStmt;
54
55 QualType T = E ? E->getType() : SemaRef.getASTContext().DependentTy;
56
57 return RecoveryExpr::Create(SemaRef.getASTContext(), T,
58 AssocStmt.get()->getBeginLoc(),
59 AssocStmt.get()->getEndLoc(),
60 E ? ArrayRef<Expr *>{E} : ArrayRef<Expr *>{});
61 }
62
63 // OpenACC 3.3 2.12: 'expr' is an expression with scalar type.
64 bool CheckOperandExpr(const Expr *E, PartialDiagnostic PD) {
65 QualType ExprTy = E->getType();
66
67 // Scalar allowed, plus we allow instantiation dependent to support
68 // templates.
69 if (ExprTy->isInstantiationDependentType() || ExprTy->isScalarType())
70 return false;
71
72 return DiagnoseInvalidAtomic(E->getExprLoc(),
73 PD << diag::OACCLValScalar::Scalar << ExprTy);
74 }
75
76 // OpenACC 3.3 2.12: 'x' and 'v' (as applicable) are boht l-value expressoins
77 // with scalar type.
78 bool CheckOperandVariable(const Expr *E, PartialDiagnostic PD) {
79 if (CheckOperandExpr(E, PD))
80 return true;
81
82 if (E->isLValue())
83 return false;
84
85 return DiagnoseInvalidAtomic(E->getExprLoc(),
86 PD << diag::OACCLValScalar::LVal);
87 }
88
89 Expr *RequireExpr(Stmt *Stmt, PartialDiagnostic ExpectedNote) {
90 if (Expr *E = dyn_cast<Expr>(Stmt))
91 return E->IgnoreImpCasts();
92
93 DiagnoseInvalidAtomic(Stmt->getBeginLoc(), ExpectedNote);
94 return nullptr;
95 }
96
97 // A struct to hold the return the inner components of any operands, which
98 // allows for compound checking.
99 struct BinaryOpInfo {
100 const Expr *FoundExpr = nullptr;
101 const Expr *LHS = nullptr;
102 const Expr *RHS = nullptr;
103 BinaryOperatorKind Operator;
104 };
105
106 struct UnaryOpInfo {
107 const Expr *FoundExpr = nullptr;
108 const Expr *SubExpr = nullptr;
109 UnaryOperatorKind Operator;
110
111 bool IsIncrementOp() {
112 return Operator == UO_PostInc || Operator == UO_PreInc;
113 }
114 };
115
116 std::optional<UnaryOpInfo> GetUnaryOperatorInfo(const Expr *E) {
117 // If this is a simple unary operator, just return its details.
118 if (const auto *UO = dyn_cast<UnaryOperator>(E))
119 return UnaryOpInfo{UO, UO->getSubExpr()->IgnoreImpCasts(),
120 UO->getOpcode()};
121
122 // This might be an overloaded operator or a dependent context, so make sure
123 // we can get as many details out of this as we can.
124 if (const auto *OpCall = dyn_cast<CXXOperatorCallExpr>(E)) {
125 UnaryOpInfo Inf;
126 Inf.FoundExpr = OpCall;
127
128 switch (OpCall->getOperator()) {
129 default:
130 return std::nullopt;
131 case OO_PlusPlus:
132 Inf.Operator = OpCall->getNumArgs() == 1 ? UO_PreInc : UO_PostInc;
133 break;
134 case OO_MinusMinus:
135 Inf.Operator = OpCall->getNumArgs() == 1 ? UO_PreDec : UO_PostDec;
136 break;
137 case OO_Amp:
138 Inf.Operator = UO_AddrOf;
139 break;
140 case OO_Star:
141 Inf.Operator = UO_Deref;
142 break;
143 case OO_Plus:
144 Inf.Operator = UO_Plus;
145 break;
146 case OO_Minus:
147 Inf.Operator = UO_Minus;
148 break;
149 case OO_Tilde:
150 Inf.Operator = UO_Not;
151 break;
152 case OO_Exclaim:
153 Inf.Operator = UO_LNot;
154 break;
155 case OO_Coawait:
156 Inf.Operator = UO_Coawait;
157 break;
158 }
159
160 // Some of the above can be both binary and unary operations, so make sure
161 // we get the right one.
162 if (Inf.Operator != UO_PostInc && Inf.Operator != UO_PostDec &&
163 OpCall->getNumArgs() != 1)
164 return std::nullopt;
165
166 Inf.SubExpr = OpCall->getArg(0);
167 return Inf;
168 }
169 return std::nullopt;
170 }
171
172 // Get a normalized version of a binary operator.
173 std::optional<BinaryOpInfo> GetBinaryOperatorInfo(const Expr *E) {
174 if (const auto *BO = dyn_cast<BinaryOperator>(E))
175 return BinaryOpInfo{BO, BO->getLHS()->IgnoreImpCasts(),
176 BO->getRHS()->IgnoreImpCasts(), BO->getOpcode()};
177
178 // In case this is an operator-call, which allows us to support overloaded
179 // operators and dependent expression.
180 if (const auto *OpCall = dyn_cast<CXXOperatorCallExpr>(E)) {
181 BinaryOpInfo Inf;
182 Inf.FoundExpr = OpCall;
183
184 switch (OpCall->getOperator()) {
185 default:
186 return std::nullopt;
187 case OO_Plus:
188 Inf.Operator = BO_Add;
189 break;
190 case OO_Minus:
191 Inf.Operator = BO_Sub;
192 break;
193 case OO_Star:
194 Inf.Operator = BO_Mul;
195 break;
196 case OO_Slash:
197 Inf.Operator = BO_Div;
198 break;
199 case OO_Percent:
200 Inf.Operator = BO_Rem;
201 break;
202 case OO_Caret:
203 Inf.Operator = BO_Xor;
204 break;
205 case OO_Amp:
206 Inf.Operator = BO_And;
207 break;
208 case OO_Pipe:
209 Inf.Operator = BO_Or;
210 break;
211 case OO_Equal:
212 Inf.Operator = BO_Assign;
213 break;
214 case OO_Spaceship:
215 Inf.Operator = BO_Cmp;
216 break;
217 case OO_Less:
218 Inf.Operator = BO_LT;
219 break;
220 case OO_Greater:
221 Inf.Operator = BO_GT;
222 break;
223 case OO_PlusEqual:
224 Inf.Operator = BO_AddAssign;
225 break;
226 case OO_MinusEqual:
227 Inf.Operator = BO_SubAssign;
228 break;
229 case OO_StarEqual:
230 Inf.Operator = BO_MulAssign;
231 break;
232 case OO_SlashEqual:
233 Inf.Operator = BO_DivAssign;
234 break;
235 case OO_PercentEqual:
236 Inf.Operator = BO_RemAssign;
237 break;
238 case OO_CaretEqual:
239 Inf.Operator = BO_XorAssign;
240 break;
241 case OO_AmpEqual:
242 Inf.Operator = BO_AndAssign;
243 break;
244 case OO_PipeEqual:
245 Inf.Operator = BO_OrAssign;
246 break;
247 case OO_LessLess:
248 Inf.Operator = BO_Shl;
249 break;
250 case OO_GreaterGreater:
251 Inf.Operator = BO_Shr;
252 break;
253 case OO_LessLessEqual:
254 Inf.Operator = BO_ShlAssign;
255 break;
256 case OO_GreaterGreaterEqual:
257 Inf.Operator = BO_ShrAssign;
258 break;
259 case OO_EqualEqual:
260 Inf.Operator = BO_EQ;
261 break;
262 case OO_ExclaimEqual:
263 Inf.Operator = BO_NE;
264 break;
265 case OO_LessEqual:
266 Inf.Operator = BO_LE;
267 break;
268 case OO_GreaterEqual:
269 Inf.Operator = BO_GE;
270 break;
271 case OO_AmpAmp:
272 Inf.Operator = BO_LAnd;
273 break;
274 case OO_PipePipe:
275 Inf.Operator = BO_LOr;
276 break;
277 case OO_Comma:
278 Inf.Operator = BO_Comma;
279 break;
280 case OO_ArrowStar:
281 Inf.Operator = BO_PtrMemI;
282 break;
283 }
284
285 // This isn't a binary operator unless there are two arguments.
286 if (OpCall->getNumArgs() != 2)
287 return std::nullopt;
288
289 // Callee is the call-operator, so we only need to extract the two
290 // arguments here.
291 Inf.LHS = OpCall->getArg(0)->IgnoreImpCasts();
292 Inf.RHS = OpCall->getArg(1)->IgnoreImpCasts();
293 return Inf;
294 }
295
296 return std::nullopt;
297 }
298
299 // Checks a required assignment operation, but don't check the LHS or RHS,
300 // callers have to do that here.
301 std::optional<BinaryOpInfo> CheckAssignment(const Expr *E) {
302 std::optional<BinaryOpInfo> Inf = GetBinaryOperatorInfo(E);
303
304 if (!Inf) {
305 DiagnoseInvalidAtomic(E->getExprLoc(),
306 SemaRef.PDiag(diag::note_acc_atomic_expr_must_be)
307 << diag::OACCAtomicExpr::Assign);
308 return std::nullopt;
309 }
310
311 if (Inf->Operator != BO_Assign) {
312 DiagnoseInvalidAtomic(Inf->FoundExpr->getExprLoc(),
313 SemaRef.PDiag(diag::note_acc_atomic_expr_must_be)
314 << diag::OACCAtomicExpr::Assign);
315 return std::nullopt;
316 }
317
318 // Assignment always requires an lvalue/scalar on the LHS.
319 if (CheckOperandVariable(
320 Inf->LHS, SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
321 << /*left=*/0 << diag::OACCAtomicOpKind::Assign))
322 return std::nullopt;
323
324 return Inf;
325 }
326
327 struct IDACInfo {
328 bool Failed = false;
329 enum ExprKindTy {
330 Invalid,
331 // increment/decrement ops.
332 Unary,
333 // v = x
334 SimpleAssign,
335 // x = expr
336 ExprAssign,
337 // x binop= expr
338 CompoundAssign,
339 // x = x binop expr
340 // x = expr binop x
341 AssignBinOp
342 } ExprKind;
343
344 // The variable referred to as 'x' in all of the grammar, such that it is
345 // needed in compound statement checking of capture to check between the two
346 // expressions.
347 const Expr *X_Var = nullptr;
348
349 static IDACInfo Fail() { return IDACInfo{true, Invalid, nullptr}; };
350 };
351
352 // Helper for CheckIncDecAssignCompoundAssign, does checks for inc/dec.
353 IDACInfo CheckIncDec(UnaryOpInfo Inf) {
354
356 DiagnoseInvalidAtomic(
357 Inf.FoundExpr->getExprLoc(),
358 SemaRef.PDiag(diag::note_acc_atomic_unsupported_unary_operator));
359 return IDACInfo::Fail();
360 }
361 bool Failed = CheckOperandVariable(
362 Inf.SubExpr,
363 SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
364 << /*none=*/2
365 << (Inf.IsIncrementOp() ? diag::OACCAtomicOpKind::Inc
366 : diag::OACCAtomicOpKind::Dec));
367 // For increment/decrements, the subexpr is the 'x' (x++, ++x, etc).
368 return IDACInfo{Failed, IDACInfo::Unary, Inf.SubExpr};
369 }
370
371 enum class SimpleAssignKind { None, Var, Expr };
372
373 // Check an assignment, and ensure the RHS is either x binop expr or expr
374 // binop x.
375 // If AllowSimpleAssign, also allows v = x;
376 IDACInfo CheckAssignmentWithBinOpOnRHS(BinaryOpInfo AssignInf,
377 SimpleAssignKind SAK) {
378 PartialDiagnostic PD =
379 SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
380 << /*left=*/0 << diag::OACCAtomicOpKind::Assign;
381 if (CheckOperandVariable(AssignInf.LHS, PD))
382 return IDACInfo::Fail();
383
384 std::optional<BinaryOpInfo> BinInf = GetBinaryOperatorInfo(AssignInf.RHS);
385
386 if (!BinInf) {
387
388 // Capture in a compound statement allows v = x assignment. So make sure
389 // we permit that here.
390 if (SAK != SimpleAssignKind::None) {
391 PartialDiagnostic PD =
392 SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
393 << /*right=*/1 << diag::OACCAtomicOpKind::Assign;
394 if (SAK == SimpleAssignKind::Var) {
395 // In the var version, everywhere we allow v = x;, X is the RHS.
396 return IDACInfo{CheckOperandVariable(AssignInf.RHS, PD),
397 IDACInfo::SimpleAssign, AssignInf.RHS};
398 }
399 assert(SAK == SimpleAssignKind::Expr);
400 // In the expression version, supported by v=x; x = expr;, we need to
401 // set to the LHS here.
402 return IDACInfo{CheckOperandExpr(AssignInf.RHS, PD),
403 IDACInfo::ExprAssign, AssignInf.LHS};
404 }
405
406 DiagnoseInvalidAtomic(
407 AssignInf.RHS->getExprLoc(),
408 SemaRef.PDiag(diag::note_acc_atomic_expected_binop));
409
410 return IDACInfo::Fail();
411 }
412 switch (BinInf->Operator) {
413 default:
414 DiagnoseInvalidAtomic(
415 BinInf->FoundExpr->getExprLoc(),
416 SemaRef.PDiag(diag::note_acc_atomic_unsupported_binary_operator));
417 return IDACInfo::Fail();
418 // binop is one of +, *, -, /, &, ^, |, <<, or >>
419 case BO_Add:
420 case BO_Mul:
421 case BO_Sub:
422 case BO_Div:
423 case BO_And:
424 case BO_Xor:
425 case BO_Or:
426 case BO_Shl:
427 case BO_Shr:
428 // Handle these outside of the switch.
429 break;
430 }
431
432 llvm::FoldingSetNodeID LHS_ID, InnerLHS_ID, InnerRHS_ID;
433 AssignInf.LHS->Profile(LHS_ID, SemaRef.getASTContext(),
434 /*Canonical=*/true);
435 BinInf->LHS->Profile(InnerLHS_ID, SemaRef.getASTContext(),
436 /*Canonical=*/true);
437
438 // This is X = X binop expr;
439 // Check the RHS is an expression.
440 if (LHS_ID == InnerLHS_ID)
441 return IDACInfo{
442 CheckOperandExpr(
443 BinInf->RHS,
444 SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
445 << /*right=*/1 << diag::OACCAtomicOpKind::CompoundAssign),
446 IDACInfo::AssignBinOp, AssignInf.LHS};
447
448 BinInf->RHS->Profile(InnerRHS_ID, SemaRef.getASTContext(),
449 /*Canonical=*/true);
450 // This is X = expr binop X;
451 // Check the LHS is an expression
452 if (LHS_ID == InnerRHS_ID)
453 return IDACInfo{
454 CheckOperandExpr(
455 BinInf->LHS,
456 SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
457 << /*left=*/0 << diag::OACCAtomicOpKind::CompoundAssign),
458 IDACInfo::AssignBinOp, AssignInf.LHS};
459
460 // If nothing matches, error out.
461 DiagnoseInvalidAtomic(BinInf->FoundExpr->getExprLoc(),
462 SemaRef.PDiag(diag::note_acc_atomic_mismatch_operand)
463 << AssignInf.LHS << BinInf->LHS << BinInf->RHS);
464 return IDACInfo::Fail();
465 }
466
467 // Ensures that the expression is an increment/decrement, an assignment, or a
468 // compound assignment. If its an assignment, allows the x binop expr/x binop
469 // expr syntax. If it is a compound-assignment, allows any expr on the RHS.
470 IDACInfo CheckIncDecAssignCompoundAssign(const Expr *E,
471 SimpleAssignKind SAK) {
472 std::optional<UnaryOpInfo> UInf = GetUnaryOperatorInfo(E);
473
474 // If this is a unary operator, only increment/decrement are allowed, so get
475 // unary operator, then check everything we can.
476 if (UInf)
477 return CheckIncDec(*UInf);
478
479 std::optional<BinaryOpInfo> BinInf = GetBinaryOperatorInfo(E);
480
481 // Unary or binary operator were the only choices, so error here.
482 if (!BinInf) {
483 DiagnoseInvalidAtomic(E->getExprLoc(),
484 SemaRef.PDiag(diag::note_acc_atomic_expr_must_be)
485 << diag::OACCAtomicExpr::UnaryCompAssign);
486 return IDACInfo::Fail();
487 }
488
489 switch (BinInf->Operator) {
490 default:
491 DiagnoseInvalidAtomic(
492 BinInf->FoundExpr->getExprLoc(),
493 SemaRef.PDiag(
494 diag::note_acc_atomic_unsupported_compound_binary_operator));
495 return IDACInfo::Fail();
496 case BO_Assign:
497 return CheckAssignmentWithBinOpOnRHS(*BinInf, SAK);
498 case BO_AddAssign:
499 case BO_MulAssign:
500 case BO_SubAssign:
501 case BO_DivAssign:
502 case BO_AndAssign:
503 case BO_XorAssign:
504 case BO_OrAssign:
505 case BO_ShlAssign:
506 case BO_ShrAssign: {
507 PartialDiagnostic LPD =
508 SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
509 << /*left=*/0 << diag::OACCAtomicOpKind::CompoundAssign;
510 PartialDiagnostic RPD =
511 SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
512 << /*right=*/1 << diag::OACCAtomicOpKind::CompoundAssign;
513 // nothing to do other than check the variable expressions.
514 // success or failure
515 bool Failed = CheckOperandVariable(BinInf->LHS, LPD) ||
516 CheckOperandExpr(BinInf->RHS, RPD);
517
518 return IDACInfo{Failed, IDACInfo::CompoundAssign, BinInf->LHS};
519 }
520 }
521 llvm_unreachable("all binary operator kinds should be checked above");
522 }
523
524 StmtResult CheckRead() {
525 Expr *AssocExpr = RequireExpr(
526 AssocStmt.get(), SemaRef.PDiag(diag::note_acc_atomic_expr_must_be)
527 << diag::OACCAtomicExpr::Assign);
528
529 if (!AssocExpr)
530 return getRecoveryExpr();
531
532 std::optional<BinaryOpInfo> AssignRes = CheckAssignment(AssocExpr);
533 if (!AssignRes)
534 return getRecoveryExpr();
535
536 PartialDiagnostic PD =
537 SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
538 << /*right=*/1 << diag::OACCAtomicOpKind::Assign;
539
540 // Finally, check the RHS.
541 if (CheckOperandVariable(AssignRes->RHS, PD))
542 return getRecoveryExpr();
543
544 return AssocStmt;
545 }
546
547 StmtResult CheckWrite() {
548 Expr *AssocExpr = RequireExpr(
549 AssocStmt.get(), SemaRef.PDiag(diag::note_acc_atomic_expr_must_be)
550 << diag::OACCAtomicExpr::Assign);
551
552 if (!AssocExpr)
553 return getRecoveryExpr();
554
555 std::optional<BinaryOpInfo> AssignRes = CheckAssignment(AssocExpr);
556 if (!AssignRes)
557 return getRecoveryExpr();
558
559 PartialDiagnostic PD =
560 SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
561 << /*right=*/1 << diag::OACCAtomicOpKind::Assign;
562
563 // Finally, check the RHS.
564 if (CheckOperandExpr(AssignRes->RHS, PD))
565 return getRecoveryExpr();
566
567 return AssocStmt;
568 }
569
570 StmtResult CheckUpdate() {
571 Expr *AssocExpr = RequireExpr(
572 AssocStmt.get(), SemaRef.PDiag(diag::note_acc_atomic_expr_must_be)
573 << diag::OACCAtomicExpr::UnaryCompAssign);
574
575 if (!AssocExpr ||
576 CheckIncDecAssignCompoundAssign(AssocExpr, SimpleAssignKind::None)
577 .Failed)
578 return getRecoveryExpr();
579
580 return AssocStmt;
581 }
582
583 const Expr *IgnoreBeforeCompare(const Expr *E) {
585 SemaRef.getASTContext());
586 }
587
588 bool CheckVarRefsSame(IDACInfo::ExprKindTy FirstKind, const Expr *FirstX,
589 IDACInfo::ExprKindTy SecondKind, const Expr *SecondX) {
590 llvm::FoldingSetNodeID First_ID, Second_ID;
591 FirstX->Profile(First_ID, SemaRef.getASTContext(), /*Canonical=*/true);
592 SecondX->Profile(Second_ID, SemaRef.getASTContext(), /*Canonical=*/true);
593
594 if (First_ID == Second_ID)
595 return false;
596
597 PartialDiagnostic PD =
598 SemaRef.PDiag(diag::note_acc_atomic_mismatch_compound_operand)
599 << FirstKind << FirstX << SecondKind << SecondX;
600
601 return DiagnoseInvalidAtomic(SecondX->getExprLoc(), PD);
602 }
603
604 StmtResult CheckCapture() {
605 if (const auto *CmpdStmt = dyn_cast<CompoundStmt>(AssocStmt.get())) {
606 auto *const *BodyItr = CmpdStmt->body().begin();
607 PartialDiagnostic PD = SemaRef.PDiag(diag::note_acc_atomic_expr_must_be)
608 << diag::OACCAtomicExpr::UnaryCompAssign;
609 // If we don't have at least 1 statement, error.
610 if (BodyItr == CmpdStmt->body().end()) {
611 DiagnoseInvalidAtomic(CmpdStmt->getBeginLoc(), PD);
612 return getRecoveryExpr();
613 }
614
615 // First Expr can be inc/dec, assign, or compound assign.
616 Expr *FirstExpr = RequireExpr(*BodyItr, PD);
617 if (!FirstExpr)
618 return getRecoveryExpr();
619
620 IDACInfo FirstExprResults =
621 CheckIncDecAssignCompoundAssign(FirstExpr, SimpleAssignKind::Var);
622 if (FirstExprResults.Failed)
623 return getRecoveryExpr();
624
625 ++BodyItr;
626
627 // If we don't have second statement, error.
628 if (BodyItr == CmpdStmt->body().end()) {
629 DiagnoseInvalidAtomic(CmpdStmt->getEndLoc(), PD);
630 return getRecoveryExpr();
631 }
632
633 Expr *SecondExpr = RequireExpr(*BodyItr, PD);
634 if (!SecondExpr)
635 return getRecoveryExpr();
636
637 assert(FirstExprResults.ExprKind != IDACInfo::Invalid);
638
639 switch (FirstExprResults.ExprKind) {
640 case IDACInfo::Invalid:
641 case IDACInfo::ExprAssign:
642 llvm_unreachable("Should have error'ed out by now");
643 case IDACInfo::Unary:
644 case IDACInfo::CompoundAssign:
645 case IDACInfo::AssignBinOp: {
646 // Everything but simple-assign can only be followed by a simple
647 // assignment.
648 std::optional<BinaryOpInfo> AssignRes = CheckAssignment(SecondExpr);
649 if (!AssignRes)
650 return getRecoveryExpr();
651
652 PartialDiagnostic PD =
653 SemaRef.PDiag(diag::note_acc_atomic_operand_lvalue_scalar)
654 << /*right=*/1 << diag::OACCAtomicOpKind::Assign;
655
656 if (CheckOperandVariable(AssignRes->RHS, PD))
657 return getRecoveryExpr();
658
659 if (CheckVarRefsSame(FirstExprResults.ExprKind,
660 IgnoreBeforeCompare(FirstExprResults.X_Var),
661 IDACInfo::SimpleAssign,
662 IgnoreBeforeCompare(AssignRes->RHS)))
663 return getRecoveryExpr();
664 break;
665 }
666 case IDACInfo::SimpleAssign: {
667 // If the first was v = x, anything but simple expression is allowed.
668 IDACInfo SecondExprResults =
669 CheckIncDecAssignCompoundAssign(SecondExpr, SimpleAssignKind::Expr);
670 if (SecondExprResults.Failed)
671 return getRecoveryExpr();
672
673 if (CheckVarRefsSame(FirstExprResults.ExprKind,
674 IgnoreBeforeCompare(FirstExprResults.X_Var),
675 SecondExprResults.ExprKind,
676 IgnoreBeforeCompare(SecondExprResults.X_Var)))
677 return getRecoveryExpr();
678 break;
679 }
680 }
681 ++BodyItr;
682 if (BodyItr != CmpdStmt->body().end()) {
683 DiagnoseInvalidAtomic(
684 (*BodyItr)->getBeginLoc(),
685 SemaRef.PDiag(diag::note_acc_atomic_too_many_stmts));
686 return getRecoveryExpr();
687 }
688 } else {
689 // This check doesn't need to happen if it is a compound stmt.
690 Expr *AssocExpr = RequireExpr(
691 AssocStmt.get(), SemaRef.PDiag(diag::note_acc_atomic_expr_must_be)
692 << diag::OACCAtomicExpr::Assign);
693 if (!AssocExpr)
694 return getRecoveryExpr();
695
696 // First, we require an assignment.
697 std::optional<BinaryOpInfo> AssignRes = CheckAssignment(AssocExpr);
698
699 if (!AssignRes)
700 return getRecoveryExpr();
701
702 if (CheckIncDecAssignCompoundAssign(AssignRes->RHS,
703 SimpleAssignKind::None)
704 .Failed)
705 return getRecoveryExpr();
706 }
707
708 return AssocStmt;
709 }
710
711public:
712 AtomicOperandChecker(SemaOpenACC &S, OpenACCAtomicKind AtKind,
713 SourceLocation DirLoc, StmtResult AssocStmt)
714 : SemaRef(S), AtKind(AtKind), AtomicDirLoc(DirLoc), AssocStmt(AssocStmt) {
715 }
716
717 StmtResult Check() {
718
719 switch (AtKind) {
720 case OpenACCAtomicKind::Read:
721 return CheckRead();
722 case OpenACCAtomicKind::Write:
723 return CheckWrite();
724 case OpenACCAtomicKind::None:
725 case OpenACCAtomicKind::Update:
726 return CheckUpdate();
727 case OpenACCAtomicKind::Capture:
728 return CheckCapture();
729 }
730 llvm_unreachable("Unhandled atomic kind?");
731 }
732};
733} // namespace
734
736 OpenACCAtomicKind AtKind,
737 StmtResult AssocStmt) {
738 if (!AssocStmt.isUsable())
739 return AssocStmt;
740
741 if (isa<RecoveryExpr>(AssocStmt.get()))
742 return AssocStmt;
743
744 AtomicOperandChecker Checker{*this, AtKind, AtomicDirLoc, AssocStmt};
745 return Checker.Check();
746}
Defines the clang::Expr interface and subclasses for C++ expressions.
This file declares semantic analysis for OpenACC constructs and clauses.
PtrTy get() const
Definition Ownership.h:171
bool isUsable() const
Definition Ownership.h:169
Expr * IgnoreParenNoopCasts(const ASTContext &Ctx) LLVM_READONLY
Skip past any parentheses and casts which do not change the value (including ptr->int casts of the sa...
Definition Expr.cpp:3157
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition Expr.cpp:3130
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition Expr.h:286
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:283
QualType getType() const
Definition Expr.h:146
static RecoveryExpr * Create(ASTContext &Ctx, QualType T, SourceLocation BeginLoc, SourceLocation EndLoc, ArrayRef< Expr * > SubExprs)
Definition Expr.cpp:5514
StmtResult CheckAtomicAssociatedStmt(SourceLocation AtomicDirLoc, OpenACCAtomicKind AtKind, StmtResult AssocStmt)
Called to check the form of the atomic construct which has some fairly sizable restrictions.
Encodes a location in the source.
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, bool Canonical, bool ProfileLambdaExpr=false) const
Produce a unique representation of the given statement.
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
bool isIncrementDecrementOp() const
Definition Expr.h:2410
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
OpenACCAtomicKind
StmtResult StmtError()
Definition Ownership.h:266
ActionResult< Stmt * > StmtResult
Definition Ownership.h:250