1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
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 OpenMP directives and
10 /// clauses.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "TreeTransform.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/ASTMutationListener.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclOpenMP.h"
21 #include "clang/AST/OpenMPClause.h"
22 #include "clang/AST/StmtCXX.h"
23 #include "clang/AST/StmtOpenMP.h"
24 #include "clang/AST/StmtVisitor.h"
25 #include "clang/AST/TypeOrdering.h"
26 #include "clang/Basic/DiagnosticSema.h"
27 #include "clang/Basic/OpenMPKinds.h"
28 #include "clang/Basic/PartialDiagnostic.h"
29 #include "clang/Basic/TargetInfo.h"
30 #include "clang/Sema/Initialization.h"
31 #include "clang/Sema/Lookup.h"
32 #include "clang/Sema/Scope.h"
33 #include "clang/Sema/ScopeInfo.h"
34 #include "clang/Sema/SemaInternal.h"
35 #include "llvm/ADT/IndexedMap.h"
36 #include "llvm/ADT/PointerEmbeddedInt.h"
37 #include "llvm/ADT/STLExtras.h"
38 #include "llvm/ADT/StringExtras.h"
39 #include "llvm/Frontend/OpenMP/OMPConstants.h"
40 #include <set>
41 
42 using namespace clang;
43 using namespace llvm::omp;
44 
45 //===----------------------------------------------------------------------===//
46 // Stack of data-sharing attributes for variables
47 //===----------------------------------------------------------------------===//
48 
49 static const Expr *checkMapClauseExpressionBase(
50     Sema &SemaRef, Expr *E,
51     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
52     OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose);
53 
54 namespace {
55 /// Default data sharing attributes, which can be applied to directive.
56 enum DefaultDataSharingAttributes {
57   DSA_unspecified = 0,       /// Data sharing attribute not specified.
58   DSA_none = 1 << 0,         /// Default data sharing attribute 'none'.
59   DSA_shared = 1 << 1,       /// Default data sharing attribute 'shared'.
60   DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'.
61 };
62 
63 /// Stack for tracking declarations used in OpenMP directives and
64 /// clauses and their data-sharing attributes.
65 class DSAStackTy {
66 public:
67   struct DSAVarData {
68     OpenMPDirectiveKind DKind = OMPD_unknown;
69     OpenMPClauseKind CKind = OMPC_unknown;
70     unsigned Modifier = 0;
71     const Expr *RefExpr = nullptr;
72     DeclRefExpr *PrivateCopy = nullptr;
73     SourceLocation ImplicitDSALoc;
74     bool AppliedToPointee = false;
75     DSAVarData() = default;
76     DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
77                const Expr *RefExpr, DeclRefExpr *PrivateCopy,
78                SourceLocation ImplicitDSALoc, unsigned Modifier,
79                bool AppliedToPointee)
80         : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr),
81           PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc),
82           AppliedToPointee(AppliedToPointee) {}
83   };
84   using OperatorOffsetTy =
85       llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
86   using DoacrossDependMapTy =
87       llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
88   /// Kind of the declaration used in the uses_allocators clauses.
89   enum class UsesAllocatorsDeclKind {
90     /// Predefined allocator
91     PredefinedAllocator,
92     /// User-defined allocator
93     UserDefinedAllocator,
94     /// The declaration that represent allocator trait
95     AllocatorTrait,
96   };
97 
98 private:
99   struct DSAInfo {
100     OpenMPClauseKind Attributes = OMPC_unknown;
101     unsigned Modifier = 0;
102     /// Pointer to a reference expression and a flag which shows that the
103     /// variable is marked as lastprivate(true) or not (false).
104     llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
105     DeclRefExpr *PrivateCopy = nullptr;
106     /// true if the attribute is applied to the pointee, not the variable
107     /// itself.
108     bool AppliedToPointee = false;
109   };
110   using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
111   using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
112   using LCDeclInfo = std::pair<unsigned, VarDecl *>;
113   using LoopControlVariablesMapTy =
114       llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
115   /// Struct that associates a component with the clause kind where they are
116   /// found.
117   struct MappedExprComponentTy {
118     OMPClauseMappableExprCommon::MappableExprComponentLists Components;
119     OpenMPClauseKind Kind = OMPC_unknown;
120   };
121   using MappedExprComponentsTy =
122       llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
123   using CriticalsWithHintsTy =
124       llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
125   struct ReductionData {
126     using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
127     SourceRange ReductionRange;
128     llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
129     ReductionData() = default;
130     void set(BinaryOperatorKind BO, SourceRange RR) {
131       ReductionRange = RR;
132       ReductionOp = BO;
133     }
134     void set(const Expr *RefExpr, SourceRange RR) {
135       ReductionRange = RR;
136       ReductionOp = RefExpr;
137     }
138   };
139   using DeclReductionMapTy =
140       llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
141   struct DefaultmapInfo {
142     OpenMPDefaultmapClauseModifier ImplicitBehavior =
143         OMPC_DEFAULTMAP_MODIFIER_unknown;
144     SourceLocation SLoc;
145     DefaultmapInfo() = default;
146     DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc)
147         : ImplicitBehavior(M), SLoc(Loc) {}
148   };
149 
150   struct SharingMapTy {
151     DeclSAMapTy SharingMap;
152     DeclReductionMapTy ReductionMap;
153     UsedRefMapTy AlignedMap;
154     UsedRefMapTy NontemporalMap;
155     MappedExprComponentsTy MappedExprComponents;
156     LoopControlVariablesMapTy LCVMap;
157     DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
158     SourceLocation DefaultAttrLoc;
159     DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown];
160     OpenMPDirectiveKind Directive = OMPD_unknown;
161     DeclarationNameInfo DirectiveName;
162     Scope *CurScope = nullptr;
163     DeclContext *Context = nullptr;
164     SourceLocation ConstructLoc;
165     /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
166     /// get the data (loop counters etc.) about enclosing loop-based construct.
167     /// This data is required during codegen.
168     DoacrossDependMapTy DoacrossDepends;
169     /// First argument (Expr *) contains optional argument of the
170     /// 'ordered' clause, the second one is true if the regions has 'ordered'
171     /// clause, false otherwise.
172     llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
173     unsigned AssociatedLoops = 1;
174     bool HasMutipleLoops = false;
175     const Decl *PossiblyLoopCounter = nullptr;
176     bool NowaitRegion = false;
177     bool CancelRegion = false;
178     bool LoopStart = false;
179     bool BodyComplete = false;
180     SourceLocation PrevScanLocation;
181     SourceLocation PrevOrderedLocation;
182     SourceLocation InnerTeamsRegionLoc;
183     /// Reference to the taskgroup task_reduction reference expression.
184     Expr *TaskgroupReductionRef = nullptr;
185     llvm::DenseSet<QualType> MappedClassesQualTypes;
186     SmallVector<Expr *, 4> InnerUsedAllocators;
187     llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates;
188     /// List of globals marked as declare target link in this target region
189     /// (isOpenMPTargetExecutionDirective(Directive) == true).
190     llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
191     /// List of decls used in inclusive/exclusive clauses of the scan directive.
192     llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
193     llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind>
194         UsesAllocatorsDecls;
195     Expr *DeclareMapperVar = nullptr;
196     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
197                  Scope *CurScope, SourceLocation Loc)
198         : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
199           ConstructLoc(Loc) {}
200     SharingMapTy() = default;
201   };
202 
203   using StackTy = SmallVector<SharingMapTy, 4>;
204 
205   /// Stack of used declaration and their data-sharing attributes.
206   DeclSAMapTy Threadprivates;
207   const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
208   SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
209   /// true, if check for DSA must be from parent directive, false, if
210   /// from current directive.
211   OpenMPClauseKind ClauseKindMode = OMPC_unknown;
212   Sema &SemaRef;
213   bool ForceCapturing = false;
214   /// true if all the variables in the target executable directives must be
215   /// captured by reference.
216   bool ForceCaptureByReferenceInTargetExecutable = false;
217   CriticalsWithHintsTy Criticals;
218   unsigned IgnoredStackElements = 0;
219 
220   /// Iterators over the stack iterate in order from innermost to outermost
221   /// directive.
222   using const_iterator = StackTy::const_reverse_iterator;
223   const_iterator begin() const {
224     return Stack.empty() ? const_iterator()
225                          : Stack.back().first.rbegin() + IgnoredStackElements;
226   }
227   const_iterator end() const {
228     return Stack.empty() ? const_iterator() : Stack.back().first.rend();
229   }
230   using iterator = StackTy::reverse_iterator;
231   iterator begin() {
232     return Stack.empty() ? iterator()
233                          : Stack.back().first.rbegin() + IgnoredStackElements;
234   }
235   iterator end() {
236     return Stack.empty() ? iterator() : Stack.back().first.rend();
237   }
238 
239   // Convenience operations to get at the elements of the stack.
240 
241   bool isStackEmpty() const {
242     return Stack.empty() ||
243            Stack.back().second != CurrentNonCapturingFunctionScope ||
244            Stack.back().first.size() <= IgnoredStackElements;
245   }
246   size_t getStackSize() const {
247     return isStackEmpty() ? 0
248                           : Stack.back().first.size() - IgnoredStackElements;
249   }
250 
251   SharingMapTy *getTopOfStackOrNull() {
252     size_t Size = getStackSize();
253     if (Size == 0)
254       return nullptr;
255     return &Stack.back().first[Size - 1];
256   }
257   const SharingMapTy *getTopOfStackOrNull() const {
258     return const_cast<DSAStackTy &>(*this).getTopOfStackOrNull();
259   }
260   SharingMapTy &getTopOfStack() {
261     assert(!isStackEmpty() && "no current directive");
262     return *getTopOfStackOrNull();
263   }
264   const SharingMapTy &getTopOfStack() const {
265     return const_cast<DSAStackTy &>(*this).getTopOfStack();
266   }
267 
268   SharingMapTy *getSecondOnStackOrNull() {
269     size_t Size = getStackSize();
270     if (Size <= 1)
271       return nullptr;
272     return &Stack.back().first[Size - 2];
273   }
274   const SharingMapTy *getSecondOnStackOrNull() const {
275     return const_cast<DSAStackTy &>(*this).getSecondOnStackOrNull();
276   }
277 
278   /// Get the stack element at a certain level (previously returned by
279   /// \c getNestingLevel).
280   ///
281   /// Note that nesting levels count from outermost to innermost, and this is
282   /// the reverse of our iteration order where new inner levels are pushed at
283   /// the front of the stack.
284   SharingMapTy &getStackElemAtLevel(unsigned Level) {
285     assert(Level < getStackSize() && "no such stack element");
286     return Stack.back().first[Level];
287   }
288   const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
289     return const_cast<DSAStackTy &>(*this).getStackElemAtLevel(Level);
290   }
291 
292   DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
293 
294   /// Checks if the variable is a local for OpenMP region.
295   bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
296 
297   /// Vector of previously declared requires directives
298   SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
299   /// omp_allocator_handle_t type.
300   QualType OMPAllocatorHandleT;
301   /// omp_depend_t type.
302   QualType OMPDependT;
303   /// omp_event_handle_t type.
304   QualType OMPEventHandleT;
305   /// omp_alloctrait_t type.
306   QualType OMPAlloctraitT;
307   /// Expression for the predefined allocators.
308   Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
309       nullptr};
310   /// Vector of previously encountered target directives
311   SmallVector<SourceLocation, 2> TargetLocations;
312   SourceLocation AtomicLocation;
313   /// Vector of declare variant construct traits.
314   SmallVector<llvm::omp::TraitProperty, 8> ConstructTraits;
315 
316 public:
317   explicit DSAStackTy(Sema &S) : SemaRef(S) {}
318 
319   /// Sets omp_allocator_handle_t type.
320   void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
321   /// Gets omp_allocator_handle_t type.
322   QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
323   /// Sets omp_alloctrait_t type.
324   void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; }
325   /// Gets omp_alloctrait_t type.
326   QualType getOMPAlloctraitT() const { return OMPAlloctraitT; }
327   /// Sets the given default allocator.
328   void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
329                     Expr *Allocator) {
330     OMPPredefinedAllocators[AllocatorKind] = Allocator;
331   }
332   /// Returns the specified default allocator.
333   Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
334     return OMPPredefinedAllocators[AllocatorKind];
335   }
336   /// Sets omp_depend_t type.
337   void setOMPDependT(QualType Ty) { OMPDependT = Ty; }
338   /// Gets omp_depend_t type.
339   QualType getOMPDependT() const { return OMPDependT; }
340 
341   /// Sets omp_event_handle_t type.
342   void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; }
343   /// Gets omp_event_handle_t type.
344   QualType getOMPEventHandleT() const { return OMPEventHandleT; }
345 
346   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
347   OpenMPClauseKind getClauseParsingMode() const {
348     assert(isClauseParsingMode() && "Must be in clause parsing mode.");
349     return ClauseKindMode;
350   }
351   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
352 
353   bool isBodyComplete() const {
354     const SharingMapTy *Top = getTopOfStackOrNull();
355     return Top && Top->BodyComplete;
356   }
357   void setBodyComplete() { getTopOfStack().BodyComplete = true; }
358 
359   bool isForceVarCapturing() const { return ForceCapturing; }
360   void setForceVarCapturing(bool V) { ForceCapturing = V; }
361 
362   void setForceCaptureByReferenceInTargetExecutable(bool V) {
363     ForceCaptureByReferenceInTargetExecutable = V;
364   }
365   bool isForceCaptureByReferenceInTargetExecutable() const {
366     return ForceCaptureByReferenceInTargetExecutable;
367   }
368 
369   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
370             Scope *CurScope, SourceLocation Loc) {
371     assert(!IgnoredStackElements &&
372            "cannot change stack while ignoring elements");
373     if (Stack.empty() ||
374         Stack.back().second != CurrentNonCapturingFunctionScope)
375       Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
376     Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
377     Stack.back().first.back().DefaultAttrLoc = Loc;
378   }
379 
380   void pop() {
381     assert(!IgnoredStackElements &&
382            "cannot change stack while ignoring elements");
383     assert(!Stack.back().first.empty() &&
384            "Data-sharing attributes stack is empty!");
385     Stack.back().first.pop_back();
386   }
387 
388   /// RAII object to temporarily leave the scope of a directive when we want to
389   /// logically operate in its parent.
390   class ParentDirectiveScope {
391     DSAStackTy &Self;
392     bool Active;
393 
394   public:
395     ParentDirectiveScope(DSAStackTy &Self, bool Activate)
396         : Self(Self), Active(false) {
397       if (Activate)
398         enable();
399     }
400     ~ParentDirectiveScope() { disable(); }
401     void disable() {
402       if (Active) {
403         --Self.IgnoredStackElements;
404         Active = false;
405       }
406     }
407     void enable() {
408       if (!Active) {
409         ++Self.IgnoredStackElements;
410         Active = true;
411       }
412     }
413   };
414 
415   /// Marks that we're started loop parsing.
416   void loopInit() {
417     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
418            "Expected loop-based directive.");
419     getTopOfStack().LoopStart = true;
420   }
421   /// Start capturing of the variables in the loop context.
422   void loopStart() {
423     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
424            "Expected loop-based directive.");
425     getTopOfStack().LoopStart = false;
426   }
427   /// true, if variables are captured, false otherwise.
428   bool isLoopStarted() const {
429     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
430            "Expected loop-based directive.");
431     return !getTopOfStack().LoopStart;
432   }
433   /// Marks (or clears) declaration as possibly loop counter.
434   void resetPossibleLoopCounter(const Decl *D = nullptr) {
435     getTopOfStack().PossiblyLoopCounter = D ? D->getCanonicalDecl() : D;
436   }
437   /// Gets the possible loop counter decl.
438   const Decl *getPossiblyLoopCunter() const {
439     return getTopOfStack().PossiblyLoopCounter;
440   }
441   /// Start new OpenMP region stack in new non-capturing function.
442   void pushFunction() {
443     assert(!IgnoredStackElements &&
444            "cannot change stack while ignoring elements");
445     const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
446     assert(!isa<CapturingScopeInfo>(CurFnScope));
447     CurrentNonCapturingFunctionScope = CurFnScope;
448   }
449   /// Pop region stack for non-capturing function.
450   void popFunction(const FunctionScopeInfo *OldFSI) {
451     assert(!IgnoredStackElements &&
452            "cannot change stack while ignoring elements");
453     if (!Stack.empty() && Stack.back().second == OldFSI) {
454       assert(Stack.back().first.empty());
455       Stack.pop_back();
456     }
457     CurrentNonCapturingFunctionScope = nullptr;
458     for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
459       if (!isa<CapturingScopeInfo>(FSI)) {
460         CurrentNonCapturingFunctionScope = FSI;
461         break;
462       }
463     }
464   }
465 
466   void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
467     Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
468   }
469   const std::pair<const OMPCriticalDirective *, llvm::APSInt>
470   getCriticalWithHint(const DeclarationNameInfo &Name) const {
471     auto I = Criticals.find(Name.getAsString());
472     if (I != Criticals.end())
473       return I->second;
474     return std::make_pair(nullptr, llvm::APSInt());
475   }
476   /// If 'aligned' declaration for given variable \a D was not seen yet,
477   /// add it and return NULL; otherwise return previous occurrence's expression
478   /// for diagnostics.
479   const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
480   /// If 'nontemporal' declaration for given variable \a D was not seen yet,
481   /// add it and return NULL; otherwise return previous occurrence's expression
482   /// for diagnostics.
483   const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE);
484 
485   /// Register specified variable as loop control variable.
486   void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
487   /// Check if the specified variable is a loop control variable for
488   /// current region.
489   /// \return The index of the loop control variable in the list of associated
490   /// for-loops (from outer to inner).
491   const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
492   /// Check if the specified variable is a loop control variable for
493   /// parent region.
494   /// \return The index of the loop control variable in the list of associated
495   /// for-loops (from outer to inner).
496   const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
497   /// Check if the specified variable is a loop control variable for
498   /// current region.
499   /// \return The index of the loop control variable in the list of associated
500   /// for-loops (from outer to inner).
501   const LCDeclInfo isLoopControlVariable(const ValueDecl *D,
502                                          unsigned Level) const;
503   /// Get the loop control variable for the I-th loop (or nullptr) in
504   /// parent directive.
505   const ValueDecl *getParentLoopControlVariable(unsigned I) const;
506 
507   /// Marks the specified decl \p D as used in scan directive.
508   void markDeclAsUsedInScanDirective(ValueDecl *D) {
509     if (SharingMapTy *Stack = getSecondOnStackOrNull())
510       Stack->UsedInScanDirective.insert(D);
511   }
512 
513   /// Checks if the specified declaration was used in the inner scan directive.
514   bool isUsedInScanDirective(ValueDecl *D) const {
515     if (const SharingMapTy *Stack = getTopOfStackOrNull())
516       return Stack->UsedInScanDirective.contains(D);
517     return false;
518   }
519 
520   /// Adds explicit data sharing attribute to the specified declaration.
521   void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
522               DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0,
523               bool AppliedToPointee = false);
524 
525   /// Adds additional information for the reduction items with the reduction id
526   /// represented as an operator.
527   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
528                                  BinaryOperatorKind BOK);
529   /// Adds additional information for the reduction items with the reduction id
530   /// represented as reduction identifier.
531   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
532                                  const Expr *ReductionRef);
533   /// Returns the location and reduction operation from the innermost parent
534   /// region for the given \p D.
535   const DSAVarData
536   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
537                                    BinaryOperatorKind &BOK,
538                                    Expr *&TaskgroupDescriptor) const;
539   /// Returns the location and reduction operation from the innermost parent
540   /// region for the given \p D.
541   const DSAVarData
542   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
543                                    const Expr *&ReductionRef,
544                                    Expr *&TaskgroupDescriptor) const;
545   /// Return reduction reference expression for the current taskgroup or
546   /// parallel/worksharing directives with task reductions.
547   Expr *getTaskgroupReductionRef() const {
548     assert((getTopOfStack().Directive == OMPD_taskgroup ||
549             ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
550               isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
551              !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
552            "taskgroup reference expression requested for non taskgroup or "
553            "parallel/worksharing directive.");
554     return getTopOfStack().TaskgroupReductionRef;
555   }
556   /// Checks if the given \p VD declaration is actually a taskgroup reduction
557   /// descriptor variable at the \p Level of OpenMP regions.
558   bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
559     return getStackElemAtLevel(Level).TaskgroupReductionRef &&
560            cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef)
561                    ->getDecl() == VD;
562   }
563 
564   /// Returns data sharing attributes from top of the stack for the
565   /// specified declaration.
566   const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
567   /// Returns data-sharing attributes for the specified declaration.
568   const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
569   /// Returns data-sharing attributes for the specified declaration.
570   const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const;
571   /// Checks if the specified variables has data-sharing attributes which
572   /// match specified \a CPred predicate in any directive which matches \a DPred
573   /// predicate.
574   const DSAVarData
575   hasDSA(ValueDecl *D,
576          const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
577          const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
578          bool FromParent) const;
579   /// Checks if the specified variables has data-sharing attributes which
580   /// match specified \a CPred predicate in any innermost directive which
581   /// matches \a DPred predicate.
582   const DSAVarData
583   hasInnermostDSA(ValueDecl *D,
584                   const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
585                   const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
586                   bool FromParent) const;
587   /// Checks if the specified variables has explicit data-sharing
588   /// attributes which match specified \a CPred predicate at the specified
589   /// OpenMP region.
590   bool
591   hasExplicitDSA(const ValueDecl *D,
592                  const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
593                  unsigned Level, bool NotLastprivate = false) const;
594 
595   /// Returns true if the directive at level \Level matches in the
596   /// specified \a DPred predicate.
597   bool hasExplicitDirective(
598       const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
599       unsigned Level) const;
600 
601   /// Finds a directive which matches specified \a DPred predicate.
602   bool hasDirective(
603       const llvm::function_ref<bool(
604           OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
605           DPred,
606       bool FromParent) const;
607 
608   /// Returns currently analyzed directive.
609   OpenMPDirectiveKind getCurrentDirective() const {
610     const SharingMapTy *Top = getTopOfStackOrNull();
611     return Top ? Top->Directive : OMPD_unknown;
612   }
613   /// Returns directive kind at specified level.
614   OpenMPDirectiveKind getDirective(unsigned Level) const {
615     assert(!isStackEmpty() && "No directive at specified level.");
616     return getStackElemAtLevel(Level).Directive;
617   }
618   /// Returns the capture region at the specified level.
619   OpenMPDirectiveKind getCaptureRegion(unsigned Level,
620                                        unsigned OpenMPCaptureLevel) const {
621     SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
622     getOpenMPCaptureRegions(CaptureRegions, getDirective(Level));
623     return CaptureRegions[OpenMPCaptureLevel];
624   }
625   /// Returns parent directive.
626   OpenMPDirectiveKind getParentDirective() const {
627     const SharingMapTy *Parent = getSecondOnStackOrNull();
628     return Parent ? Parent->Directive : OMPD_unknown;
629   }
630 
631   /// Add requires decl to internal vector
632   void addRequiresDecl(OMPRequiresDecl *RD) { RequiresDecls.push_back(RD); }
633 
634   /// Checks if the defined 'requires' directive has specified type of clause.
635   template <typename ClauseType> bool hasRequiresDeclWithClause() const {
636     return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
637       return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
638         return isa<ClauseType>(C);
639       });
640     });
641   }
642 
643   /// Checks for a duplicate clause amongst previously declared requires
644   /// directives
645   bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
646     bool IsDuplicate = false;
647     for (OMPClause *CNew : ClauseList) {
648       for (const OMPRequiresDecl *D : RequiresDecls) {
649         for (const OMPClause *CPrev : D->clauselists()) {
650           if (CNew->getClauseKind() == CPrev->getClauseKind()) {
651             SemaRef.Diag(CNew->getBeginLoc(),
652                          diag::err_omp_requires_clause_redeclaration)
653                 << getOpenMPClauseName(CNew->getClauseKind());
654             SemaRef.Diag(CPrev->getBeginLoc(),
655                          diag::note_omp_requires_previous_clause)
656                 << getOpenMPClauseName(CPrev->getClauseKind());
657             IsDuplicate = true;
658           }
659         }
660       }
661     }
662     return IsDuplicate;
663   }
664 
665   /// Add location of previously encountered target to internal vector
666   void addTargetDirLocation(SourceLocation LocStart) {
667     TargetLocations.push_back(LocStart);
668   }
669 
670   /// Add location for the first encountered atomicc directive.
671   void addAtomicDirectiveLoc(SourceLocation Loc) {
672     if (AtomicLocation.isInvalid())
673       AtomicLocation = Loc;
674   }
675 
676   /// Returns the location of the first encountered atomic directive in the
677   /// module.
678   SourceLocation getAtomicDirectiveLoc() const { return AtomicLocation; }
679 
680   // Return previously encountered target region locations.
681   ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
682     return TargetLocations;
683   }
684 
685   /// Set default data sharing attribute to none.
686   void setDefaultDSANone(SourceLocation Loc) {
687     getTopOfStack().DefaultAttr = DSA_none;
688     getTopOfStack().DefaultAttrLoc = Loc;
689   }
690   /// Set default data sharing attribute to shared.
691   void setDefaultDSAShared(SourceLocation Loc) {
692     getTopOfStack().DefaultAttr = DSA_shared;
693     getTopOfStack().DefaultAttrLoc = Loc;
694   }
695   /// Set default data sharing attribute to firstprivate.
696   void setDefaultDSAFirstPrivate(SourceLocation Loc) {
697     getTopOfStack().DefaultAttr = DSA_firstprivate;
698     getTopOfStack().DefaultAttrLoc = Loc;
699   }
700   /// Set default data mapping attribute to Modifier:Kind
701   void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M,
702                          OpenMPDefaultmapClauseKind Kind, SourceLocation Loc) {
703     DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind];
704     DMI.ImplicitBehavior = M;
705     DMI.SLoc = Loc;
706   }
707   /// Check whether the implicit-behavior has been set in defaultmap
708   bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) {
709     if (VariableCategory == OMPC_DEFAULTMAP_unknown)
710       return getTopOfStack()
711                      .DefaultmapMap[OMPC_DEFAULTMAP_aggregate]
712                      .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
713              getTopOfStack()
714                      .DefaultmapMap[OMPC_DEFAULTMAP_scalar]
715                      .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
716              getTopOfStack()
717                      .DefaultmapMap[OMPC_DEFAULTMAP_pointer]
718                      .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown;
719     return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior !=
720            OMPC_DEFAULTMAP_MODIFIER_unknown;
721   }
722 
723   ArrayRef<llvm::omp::TraitProperty> getConstructTraits() {
724     return ConstructTraits;
725   }
726   void handleConstructTrait(ArrayRef<llvm::omp::TraitProperty> Traits,
727                             bool ScopeEntry) {
728     if (ScopeEntry)
729       ConstructTraits.append(Traits.begin(), Traits.end());
730     else
731       for (llvm::omp::TraitProperty Trait : llvm::reverse(Traits)) {
732         llvm::omp::TraitProperty Top = ConstructTraits.pop_back_val();
733         assert(Top == Trait && "Something left a trait on the stack!");
734         (void)Trait;
735         (void)Top;
736       }
737   }
738 
739   DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const {
740     return getStackSize() <= Level ? DSA_unspecified
741                                    : getStackElemAtLevel(Level).DefaultAttr;
742   }
743   DefaultDataSharingAttributes getDefaultDSA() const {
744     return isStackEmpty() ? DSA_unspecified : getTopOfStack().DefaultAttr;
745   }
746   SourceLocation getDefaultDSALocation() const {
747     return isStackEmpty() ? SourceLocation() : getTopOfStack().DefaultAttrLoc;
748   }
749   OpenMPDefaultmapClauseModifier
750   getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const {
751     return isStackEmpty()
752                ? OMPC_DEFAULTMAP_MODIFIER_unknown
753                : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior;
754   }
755   OpenMPDefaultmapClauseModifier
756   getDefaultmapModifierAtLevel(unsigned Level,
757                                OpenMPDefaultmapClauseKind Kind) const {
758     return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior;
759   }
760   bool isDefaultmapCapturedByRef(unsigned Level,
761                                  OpenMPDefaultmapClauseKind Kind) const {
762     OpenMPDefaultmapClauseModifier M =
763         getDefaultmapModifierAtLevel(Level, Kind);
764     if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) {
765       return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) ||
766              (M == OMPC_DEFAULTMAP_MODIFIER_to) ||
767              (M == OMPC_DEFAULTMAP_MODIFIER_from) ||
768              (M == OMPC_DEFAULTMAP_MODIFIER_tofrom);
769     }
770     return true;
771   }
772   static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M,
773                                      OpenMPDefaultmapClauseKind Kind) {
774     switch (Kind) {
775     case OMPC_DEFAULTMAP_scalar:
776     case OMPC_DEFAULTMAP_pointer:
777       return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) ||
778              (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) ||
779              (M == OMPC_DEFAULTMAP_MODIFIER_default);
780     case OMPC_DEFAULTMAP_aggregate:
781       return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate;
782     default:
783       break;
784     }
785     llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum");
786   }
787   bool mustBeFirstprivateAtLevel(unsigned Level,
788                                  OpenMPDefaultmapClauseKind Kind) const {
789     OpenMPDefaultmapClauseModifier M =
790         getDefaultmapModifierAtLevel(Level, Kind);
791     return mustBeFirstprivateBase(M, Kind);
792   }
793   bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const {
794     OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind);
795     return mustBeFirstprivateBase(M, Kind);
796   }
797 
798   /// Checks if the specified variable is a threadprivate.
799   bool isThreadPrivate(VarDecl *D) {
800     const DSAVarData DVar = getTopDSA(D, false);
801     return isOpenMPThreadPrivate(DVar.CKind);
802   }
803 
804   /// Marks current region as ordered (it has an 'ordered' clause).
805   void setOrderedRegion(bool IsOrdered, const Expr *Param,
806                         OMPOrderedClause *Clause) {
807     if (IsOrdered)
808       getTopOfStack().OrderedRegion.emplace(Param, Clause);
809     else
810       getTopOfStack().OrderedRegion.reset();
811   }
812   /// Returns true, if region is ordered (has associated 'ordered' clause),
813   /// false - otherwise.
814   bool isOrderedRegion() const {
815     if (const SharingMapTy *Top = getTopOfStackOrNull())
816       return Top->OrderedRegion.hasValue();
817     return false;
818   }
819   /// Returns optional parameter for the ordered region.
820   std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
821     if (const SharingMapTy *Top = getTopOfStackOrNull())
822       if (Top->OrderedRegion.hasValue())
823         return Top->OrderedRegion.getValue();
824     return std::make_pair(nullptr, nullptr);
825   }
826   /// Returns true, if parent region is ordered (has associated
827   /// 'ordered' clause), false - otherwise.
828   bool isParentOrderedRegion() const {
829     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
830       return Parent->OrderedRegion.hasValue();
831     return false;
832   }
833   /// Returns optional parameter for the ordered region.
834   std::pair<const Expr *, OMPOrderedClause *>
835   getParentOrderedRegionParam() const {
836     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
837       if (Parent->OrderedRegion.hasValue())
838         return Parent->OrderedRegion.getValue();
839     return std::make_pair(nullptr, nullptr);
840   }
841   /// Marks current region as nowait (it has a 'nowait' clause).
842   void setNowaitRegion(bool IsNowait = true) {
843     getTopOfStack().NowaitRegion = IsNowait;
844   }
845   /// Returns true, if parent region is nowait (has associated
846   /// 'nowait' clause), false - otherwise.
847   bool isParentNowaitRegion() const {
848     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
849       return Parent->NowaitRegion;
850     return false;
851   }
852   /// Marks parent region as cancel region.
853   void setParentCancelRegion(bool Cancel = true) {
854     if (SharingMapTy *Parent = getSecondOnStackOrNull())
855       Parent->CancelRegion |= Cancel;
856   }
857   /// Return true if current region has inner cancel construct.
858   bool isCancelRegion() const {
859     const SharingMapTy *Top = getTopOfStackOrNull();
860     return Top ? Top->CancelRegion : false;
861   }
862 
863   /// Mark that parent region already has scan directive.
864   void setParentHasScanDirective(SourceLocation Loc) {
865     if (SharingMapTy *Parent = getSecondOnStackOrNull())
866       Parent->PrevScanLocation = Loc;
867   }
868   /// Return true if current region has inner cancel construct.
869   bool doesParentHasScanDirective() const {
870     const SharingMapTy *Top = getSecondOnStackOrNull();
871     return Top ? Top->PrevScanLocation.isValid() : false;
872   }
873   /// Return true if current region has inner cancel construct.
874   SourceLocation getParentScanDirectiveLoc() const {
875     const SharingMapTy *Top = getSecondOnStackOrNull();
876     return Top ? Top->PrevScanLocation : SourceLocation();
877   }
878   /// Mark that parent region already has ordered directive.
879   void setParentHasOrderedDirective(SourceLocation Loc) {
880     if (SharingMapTy *Parent = getSecondOnStackOrNull())
881       Parent->PrevOrderedLocation = Loc;
882   }
883   /// Return true if current region has inner ordered construct.
884   bool doesParentHasOrderedDirective() const {
885     const SharingMapTy *Top = getSecondOnStackOrNull();
886     return Top ? Top->PrevOrderedLocation.isValid() : false;
887   }
888   /// Returns the location of the previously specified ordered directive.
889   SourceLocation getParentOrderedDirectiveLoc() const {
890     const SharingMapTy *Top = getSecondOnStackOrNull();
891     return Top ? Top->PrevOrderedLocation : SourceLocation();
892   }
893 
894   /// Set collapse value for the region.
895   void setAssociatedLoops(unsigned Val) {
896     getTopOfStack().AssociatedLoops = Val;
897     if (Val > 1)
898       getTopOfStack().HasMutipleLoops = true;
899   }
900   /// Return collapse value for region.
901   unsigned getAssociatedLoops() const {
902     const SharingMapTy *Top = getTopOfStackOrNull();
903     return Top ? Top->AssociatedLoops : 0;
904   }
905   /// Returns true if the construct is associated with multiple loops.
906   bool hasMutipleLoops() const {
907     const SharingMapTy *Top = getTopOfStackOrNull();
908     return Top ? Top->HasMutipleLoops : false;
909   }
910 
911   /// Marks current target region as one with closely nested teams
912   /// region.
913   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
914     if (SharingMapTy *Parent = getSecondOnStackOrNull())
915       Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
916   }
917   /// Returns true, if current region has closely nested teams region.
918   bool hasInnerTeamsRegion() const {
919     return getInnerTeamsRegionLoc().isValid();
920   }
921   /// Returns location of the nested teams region (if any).
922   SourceLocation getInnerTeamsRegionLoc() const {
923     const SharingMapTy *Top = getTopOfStackOrNull();
924     return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
925   }
926 
927   Scope *getCurScope() const {
928     const SharingMapTy *Top = getTopOfStackOrNull();
929     return Top ? Top->CurScope : nullptr;
930   }
931   void setContext(DeclContext *DC) { getTopOfStack().Context = DC; }
932   SourceLocation getConstructLoc() const {
933     const SharingMapTy *Top = getTopOfStackOrNull();
934     return Top ? Top->ConstructLoc : SourceLocation();
935   }
936 
937   /// Do the check specified in \a Check to all component lists and return true
938   /// if any issue is found.
939   bool checkMappableExprComponentListsForDecl(
940       const ValueDecl *VD, bool CurrentRegionOnly,
941       const llvm::function_ref<
942           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
943                OpenMPClauseKind)>
944           Check) const {
945     if (isStackEmpty())
946       return false;
947     auto SI = begin();
948     auto SE = end();
949 
950     if (SI == SE)
951       return false;
952 
953     if (CurrentRegionOnly)
954       SE = std::next(SI);
955     else
956       std::advance(SI, 1);
957 
958     for (; SI != SE; ++SI) {
959       auto MI = SI->MappedExprComponents.find(VD);
960       if (MI != SI->MappedExprComponents.end())
961         for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
962              MI->second.Components)
963           if (Check(L, MI->second.Kind))
964             return true;
965     }
966     return false;
967   }
968 
969   /// Do the check specified in \a Check to all component lists at a given level
970   /// and return true if any issue is found.
971   bool checkMappableExprComponentListsForDeclAtLevel(
972       const ValueDecl *VD, unsigned Level,
973       const llvm::function_ref<
974           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
975                OpenMPClauseKind)>
976           Check) const {
977     if (getStackSize() <= Level)
978       return false;
979 
980     const SharingMapTy &StackElem = getStackElemAtLevel(Level);
981     auto MI = StackElem.MappedExprComponents.find(VD);
982     if (MI != StackElem.MappedExprComponents.end())
983       for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
984            MI->second.Components)
985         if (Check(L, MI->second.Kind))
986           return true;
987     return false;
988   }
989 
990   /// Create a new mappable expression component list associated with a given
991   /// declaration and initialize it with the provided list of components.
992   void addMappableExpressionComponents(
993       const ValueDecl *VD,
994       OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
995       OpenMPClauseKind WhereFoundClauseKind) {
996     MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
997     // Create new entry and append the new components there.
998     MEC.Components.resize(MEC.Components.size() + 1);
999     MEC.Components.back().append(Components.begin(), Components.end());
1000     MEC.Kind = WhereFoundClauseKind;
1001   }
1002 
1003   unsigned getNestingLevel() const {
1004     assert(!isStackEmpty());
1005     return getStackSize() - 1;
1006   }
1007   void addDoacrossDependClause(OMPDependClause *C,
1008                                const OperatorOffsetTy &OpsOffs) {
1009     SharingMapTy *Parent = getSecondOnStackOrNull();
1010     assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
1011     Parent->DoacrossDepends.try_emplace(C, OpsOffs);
1012   }
1013   llvm::iterator_range<DoacrossDependMapTy::const_iterator>
1014   getDoacrossDependClauses() const {
1015     const SharingMapTy &StackElem = getTopOfStack();
1016     if (isOpenMPWorksharingDirective(StackElem.Directive)) {
1017       const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
1018       return llvm::make_range(Ref.begin(), Ref.end());
1019     }
1020     return llvm::make_range(StackElem.DoacrossDepends.end(),
1021                             StackElem.DoacrossDepends.end());
1022   }
1023 
1024   // Store types of classes which have been explicitly mapped
1025   void addMappedClassesQualTypes(QualType QT) {
1026     SharingMapTy &StackElem = getTopOfStack();
1027     StackElem.MappedClassesQualTypes.insert(QT);
1028   }
1029 
1030   // Return set of mapped classes types
1031   bool isClassPreviouslyMapped(QualType QT) const {
1032     const SharingMapTy &StackElem = getTopOfStack();
1033     return StackElem.MappedClassesQualTypes.contains(QT);
1034   }
1035 
1036   /// Adds global declare target to the parent target region.
1037   void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
1038     assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1039                E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
1040            "Expected declare target link global.");
1041     for (auto &Elem : *this) {
1042       if (isOpenMPTargetExecutionDirective(Elem.Directive)) {
1043         Elem.DeclareTargetLinkVarDecls.push_back(E);
1044         return;
1045       }
1046     }
1047   }
1048 
1049   /// Returns the list of globals with declare target link if current directive
1050   /// is target.
1051   ArrayRef<DeclRefExpr *> getLinkGlobals() const {
1052     assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
1053            "Expected target executable directive.");
1054     return getTopOfStack().DeclareTargetLinkVarDecls;
1055   }
1056 
1057   /// Adds list of allocators expressions.
1058   void addInnerAllocatorExpr(Expr *E) {
1059     getTopOfStack().InnerUsedAllocators.push_back(E);
1060   }
1061   /// Return list of used allocators.
1062   ArrayRef<Expr *> getInnerAllocators() const {
1063     return getTopOfStack().InnerUsedAllocators;
1064   }
1065   /// Marks the declaration as implicitly firstprivate nin the task-based
1066   /// regions.
1067   void addImplicitTaskFirstprivate(unsigned Level, Decl *D) {
1068     getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D);
1069   }
1070   /// Checks if the decl is implicitly firstprivate in the task-based region.
1071   bool isImplicitTaskFirstprivate(Decl *D) const {
1072     return getTopOfStack().ImplicitTaskFirstprivates.contains(D);
1073   }
1074 
1075   /// Marks decl as used in uses_allocators clause as the allocator.
1076   void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) {
1077     getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind);
1078   }
1079   /// Checks if specified decl is used in uses allocator clause as the
1080   /// allocator.
1081   Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level,
1082                                                         const Decl *D) const {
1083     const SharingMapTy &StackElem = getTopOfStack();
1084     auto I = StackElem.UsesAllocatorsDecls.find(D);
1085     if (I == StackElem.UsesAllocatorsDecls.end())
1086       return None;
1087     return I->getSecond();
1088   }
1089   Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const {
1090     const SharingMapTy &StackElem = getTopOfStack();
1091     auto I = StackElem.UsesAllocatorsDecls.find(D);
1092     if (I == StackElem.UsesAllocatorsDecls.end())
1093       return None;
1094     return I->getSecond();
1095   }
1096 
1097   void addDeclareMapperVarRef(Expr *Ref) {
1098     SharingMapTy &StackElem = getTopOfStack();
1099     StackElem.DeclareMapperVar = Ref;
1100   }
1101   const Expr *getDeclareMapperVarRef() const {
1102     const SharingMapTy *Top = getTopOfStackOrNull();
1103     return Top ? Top->DeclareMapperVar : nullptr;
1104   }
1105 };
1106 
1107 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1108   return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
1109 }
1110 
1111 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1112   return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) ||
1113          DKind == OMPD_unknown;
1114 }
1115 
1116 } // namespace
1117 
1118 static const Expr *getExprAsWritten(const Expr *E) {
1119   if (const auto *FE = dyn_cast<FullExpr>(E))
1120     E = FE->getSubExpr();
1121 
1122   if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
1123     E = MTE->getSubExpr();
1124 
1125   while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
1126     E = Binder->getSubExpr();
1127 
1128   if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
1129     E = ICE->getSubExprAsWritten();
1130   return E->IgnoreParens();
1131 }
1132 
1133 static Expr *getExprAsWritten(Expr *E) {
1134   return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
1135 }
1136 
1137 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
1138   if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
1139     if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
1140       D = ME->getMemberDecl();
1141   const auto *VD = dyn_cast<VarDecl>(D);
1142   const auto *FD = dyn_cast<FieldDecl>(D);
1143   if (VD != nullptr) {
1144     VD = VD->getCanonicalDecl();
1145     D = VD;
1146   } else {
1147     assert(FD);
1148     FD = FD->getCanonicalDecl();
1149     D = FD;
1150   }
1151   return D;
1152 }
1153 
1154 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
1155   return const_cast<ValueDecl *>(
1156       getCanonicalDecl(const_cast<const ValueDecl *>(D)));
1157 }
1158 
1159 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
1160                                           ValueDecl *D) const {
1161   D = getCanonicalDecl(D);
1162   auto *VD = dyn_cast<VarDecl>(D);
1163   const auto *FD = dyn_cast<FieldDecl>(D);
1164   DSAVarData DVar;
1165   if (Iter == end()) {
1166     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1167     // in a region but not in construct]
1168     //  File-scope or namespace-scope variables referenced in called routines
1169     //  in the region are shared unless they appear in a threadprivate
1170     //  directive.
1171     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
1172       DVar.CKind = OMPC_shared;
1173 
1174     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
1175     // in a region but not in construct]
1176     //  Variables with static storage duration that are declared in called
1177     //  routines in the region are shared.
1178     if (VD && VD->hasGlobalStorage())
1179       DVar.CKind = OMPC_shared;
1180 
1181     // Non-static data members are shared by default.
1182     if (FD)
1183       DVar.CKind = OMPC_shared;
1184 
1185     return DVar;
1186   }
1187 
1188   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1189   // in a Construct, C/C++, predetermined, p.1]
1190   // Variables with automatic storage duration that are declared in a scope
1191   // inside the construct are private.
1192   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
1193       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
1194     DVar.CKind = OMPC_private;
1195     return DVar;
1196   }
1197 
1198   DVar.DKind = Iter->Directive;
1199   // Explicitly specified attributes and local variables with predetermined
1200   // attributes.
1201   if (Iter->SharingMap.count(D)) {
1202     const DSAInfo &Data = Iter->SharingMap.lookup(D);
1203     DVar.RefExpr = Data.RefExpr.getPointer();
1204     DVar.PrivateCopy = Data.PrivateCopy;
1205     DVar.CKind = Data.Attributes;
1206     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1207     DVar.Modifier = Data.Modifier;
1208     DVar.AppliedToPointee = Data.AppliedToPointee;
1209     return DVar;
1210   }
1211 
1212   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1213   // in a Construct, C/C++, implicitly determined, p.1]
1214   //  In a parallel or task construct, the data-sharing attributes of these
1215   //  variables are determined by the default clause, if present.
1216   switch (Iter->DefaultAttr) {
1217   case DSA_shared:
1218     DVar.CKind = OMPC_shared;
1219     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1220     return DVar;
1221   case DSA_none:
1222     return DVar;
1223   case DSA_firstprivate:
1224     if (VD->getStorageDuration() == SD_Static &&
1225         VD->getDeclContext()->isFileContext()) {
1226       DVar.CKind = OMPC_unknown;
1227     } else {
1228       DVar.CKind = OMPC_firstprivate;
1229     }
1230     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1231     return DVar;
1232   case DSA_unspecified:
1233     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1234     // in a Construct, implicitly determined, p.2]
1235     //  In a parallel construct, if no default clause is present, these
1236     //  variables are shared.
1237     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1238     if ((isOpenMPParallelDirective(DVar.DKind) &&
1239          !isOpenMPTaskLoopDirective(DVar.DKind)) ||
1240         isOpenMPTeamsDirective(DVar.DKind)) {
1241       DVar.CKind = OMPC_shared;
1242       return DVar;
1243     }
1244 
1245     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1246     // in a Construct, implicitly determined, p.4]
1247     //  In a task construct, if no default clause is present, a variable that in
1248     //  the enclosing context is determined to be shared by all implicit tasks
1249     //  bound to the current team is shared.
1250     if (isOpenMPTaskingDirective(DVar.DKind)) {
1251       DSAVarData DVarTemp;
1252       const_iterator I = Iter, E = end();
1253       do {
1254         ++I;
1255         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
1256         // Referenced in a Construct, implicitly determined, p.6]
1257         //  In a task construct, if no default clause is present, a variable
1258         //  whose data-sharing attribute is not determined by the rules above is
1259         //  firstprivate.
1260         DVarTemp = getDSA(I, D);
1261         if (DVarTemp.CKind != OMPC_shared) {
1262           DVar.RefExpr = nullptr;
1263           DVar.CKind = OMPC_firstprivate;
1264           return DVar;
1265         }
1266       } while (I != E && !isImplicitTaskingRegion(I->Directive));
1267       DVar.CKind =
1268           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
1269       return DVar;
1270     }
1271   }
1272   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1273   // in a Construct, implicitly determined, p.3]
1274   //  For constructs other than task, if no default clause is present, these
1275   //  variables inherit their data-sharing attributes from the enclosing
1276   //  context.
1277   return getDSA(++Iter, D);
1278 }
1279 
1280 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1281                                          const Expr *NewDE) {
1282   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1283   D = getCanonicalDecl(D);
1284   SharingMapTy &StackElem = getTopOfStack();
1285   auto It = StackElem.AlignedMap.find(D);
1286   if (It == StackElem.AlignedMap.end()) {
1287     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1288     StackElem.AlignedMap[D] = NewDE;
1289     return nullptr;
1290   }
1291   assert(It->second && "Unexpected nullptr expr in the aligned map");
1292   return It->second;
1293 }
1294 
1295 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D,
1296                                              const Expr *NewDE) {
1297   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1298   D = getCanonicalDecl(D);
1299   SharingMapTy &StackElem = getTopOfStack();
1300   auto It = StackElem.NontemporalMap.find(D);
1301   if (It == StackElem.NontemporalMap.end()) {
1302     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1303     StackElem.NontemporalMap[D] = NewDE;
1304     return nullptr;
1305   }
1306   assert(It->second && "Unexpected nullptr expr in the aligned map");
1307   return It->second;
1308 }
1309 
1310 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1311   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1312   D = getCanonicalDecl(D);
1313   SharingMapTy &StackElem = getTopOfStack();
1314   StackElem.LCVMap.try_emplace(
1315       D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1316 }
1317 
1318 const DSAStackTy::LCDeclInfo
1319 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1320   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1321   D = getCanonicalDecl(D);
1322   const SharingMapTy &StackElem = getTopOfStack();
1323   auto It = StackElem.LCVMap.find(D);
1324   if (It != StackElem.LCVMap.end())
1325     return It->second;
1326   return {0, nullptr};
1327 }
1328 
1329 const DSAStackTy::LCDeclInfo
1330 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const {
1331   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1332   D = getCanonicalDecl(D);
1333   for (unsigned I = Level + 1; I > 0; --I) {
1334     const SharingMapTy &StackElem = getStackElemAtLevel(I - 1);
1335     auto It = StackElem.LCVMap.find(D);
1336     if (It != StackElem.LCVMap.end())
1337       return It->second;
1338   }
1339   return {0, nullptr};
1340 }
1341 
1342 const DSAStackTy::LCDeclInfo
1343 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1344   const SharingMapTy *Parent = getSecondOnStackOrNull();
1345   assert(Parent && "Data-sharing attributes stack is empty");
1346   D = getCanonicalDecl(D);
1347   auto It = Parent->LCVMap.find(D);
1348   if (It != Parent->LCVMap.end())
1349     return It->second;
1350   return {0, nullptr};
1351 }
1352 
1353 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1354   const SharingMapTy *Parent = getSecondOnStackOrNull();
1355   assert(Parent && "Data-sharing attributes stack is empty");
1356   if (Parent->LCVMap.size() < I)
1357     return nullptr;
1358   for (const auto &Pair : Parent->LCVMap)
1359     if (Pair.second.first == I)
1360       return Pair.first;
1361   return nullptr;
1362 }
1363 
1364 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1365                         DeclRefExpr *PrivateCopy, unsigned Modifier,
1366                         bool AppliedToPointee) {
1367   D = getCanonicalDecl(D);
1368   if (A == OMPC_threadprivate) {
1369     DSAInfo &Data = Threadprivates[D];
1370     Data.Attributes = A;
1371     Data.RefExpr.setPointer(E);
1372     Data.PrivateCopy = nullptr;
1373     Data.Modifier = Modifier;
1374   } else {
1375     DSAInfo &Data = getTopOfStack().SharingMap[D];
1376     assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1377            (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1378            (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1379            (isLoopControlVariable(D).first && A == OMPC_private));
1380     Data.Modifier = Modifier;
1381     if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1382       Data.RefExpr.setInt(/*IntVal=*/true);
1383       return;
1384     }
1385     const bool IsLastprivate =
1386         A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1387     Data.Attributes = A;
1388     Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1389     Data.PrivateCopy = PrivateCopy;
1390     Data.AppliedToPointee = AppliedToPointee;
1391     if (PrivateCopy) {
1392       DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1393       Data.Modifier = Modifier;
1394       Data.Attributes = A;
1395       Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1396       Data.PrivateCopy = nullptr;
1397       Data.AppliedToPointee = AppliedToPointee;
1398     }
1399   }
1400 }
1401 
1402 /// Build a variable declaration for OpenMP loop iteration variable.
1403 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1404                              StringRef Name, const AttrVec *Attrs = nullptr,
1405                              DeclRefExpr *OrigRef = nullptr) {
1406   DeclContext *DC = SemaRef.CurContext;
1407   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1408   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
1409   auto *Decl =
1410       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1411   if (Attrs) {
1412     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1413          I != E; ++I)
1414       Decl->addAttr(*I);
1415   }
1416   Decl->setImplicit();
1417   if (OrigRef) {
1418     Decl->addAttr(
1419         OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1420   }
1421   return Decl;
1422 }
1423 
1424 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1425                                      SourceLocation Loc,
1426                                      bool RefersToCapture = false) {
1427   D->setReferenced();
1428   D->markUsed(S.Context);
1429   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1430                              SourceLocation(), D, RefersToCapture, Loc, Ty,
1431                              VK_LValue);
1432 }
1433 
1434 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1435                                            BinaryOperatorKind BOK) {
1436   D = getCanonicalDecl(D);
1437   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1438   assert(
1439       getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1440       "Additional reduction info may be specified only for reduction items.");
1441   ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1442   assert(ReductionData.ReductionRange.isInvalid() &&
1443          (getTopOfStack().Directive == OMPD_taskgroup ||
1444           ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1445             isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1446            !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1447          "Additional reduction info may be specified only once for reduction "
1448          "items.");
1449   ReductionData.set(BOK, SR);
1450   Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1451   if (!TaskgroupReductionRef) {
1452     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1453                                SemaRef.Context.VoidPtrTy, ".task_red.");
1454     TaskgroupReductionRef =
1455         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1456   }
1457 }
1458 
1459 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1460                                            const Expr *ReductionRef) {
1461   D = getCanonicalDecl(D);
1462   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1463   assert(
1464       getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1465       "Additional reduction info may be specified only for reduction items.");
1466   ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1467   assert(ReductionData.ReductionRange.isInvalid() &&
1468          (getTopOfStack().Directive == OMPD_taskgroup ||
1469           ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1470             isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1471            !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1472          "Additional reduction info may be specified only once for reduction "
1473          "items.");
1474   ReductionData.set(ReductionRef, SR);
1475   Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1476   if (!TaskgroupReductionRef) {
1477     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1478                                SemaRef.Context.VoidPtrTy, ".task_red.");
1479     TaskgroupReductionRef =
1480         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1481   }
1482 }
1483 
1484 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1485     const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1486     Expr *&TaskgroupDescriptor) const {
1487   D = getCanonicalDecl(D);
1488   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1489   for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1490     const DSAInfo &Data = I->SharingMap.lookup(D);
1491     if (Data.Attributes != OMPC_reduction ||
1492         Data.Modifier != OMPC_REDUCTION_task)
1493       continue;
1494     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1495     if (!ReductionData.ReductionOp ||
1496         ReductionData.ReductionOp.is<const Expr *>())
1497       return DSAVarData();
1498     SR = ReductionData.ReductionRange;
1499     BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1500     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1501                                        "expression for the descriptor is not "
1502                                        "set.");
1503     TaskgroupDescriptor = I->TaskgroupReductionRef;
1504     return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1505                       Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1506                       /*AppliedToPointee=*/false);
1507   }
1508   return DSAVarData();
1509 }
1510 
1511 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1512     const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1513     Expr *&TaskgroupDescriptor) const {
1514   D = getCanonicalDecl(D);
1515   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1516   for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1517     const DSAInfo &Data = I->SharingMap.lookup(D);
1518     if (Data.Attributes != OMPC_reduction ||
1519         Data.Modifier != OMPC_REDUCTION_task)
1520       continue;
1521     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1522     if (!ReductionData.ReductionOp ||
1523         !ReductionData.ReductionOp.is<const Expr *>())
1524       return DSAVarData();
1525     SR = ReductionData.ReductionRange;
1526     ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1527     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1528                                        "expression for the descriptor is not "
1529                                        "set.");
1530     TaskgroupDescriptor = I->TaskgroupReductionRef;
1531     return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1532                       Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1533                       /*AppliedToPointee=*/false);
1534   }
1535   return DSAVarData();
1536 }
1537 
1538 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1539   D = D->getCanonicalDecl();
1540   for (const_iterator E = end(); I != E; ++I) {
1541     if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1542         isOpenMPTargetExecutionDirective(I->Directive)) {
1543       if (I->CurScope) {
1544         Scope *TopScope = I->CurScope->getParent();
1545         Scope *CurScope = getCurScope();
1546         while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1547           CurScope = CurScope->getParent();
1548         return CurScope != TopScope;
1549       }
1550       for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent())
1551         if (I->Context == DC)
1552           return true;
1553       return false;
1554     }
1555   }
1556   return false;
1557 }
1558 
1559 static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1560                                   bool AcceptIfMutable = true,
1561                                   bool *IsClassType = nullptr) {
1562   ASTContext &Context = SemaRef.getASTContext();
1563   Type = Type.getNonReferenceType().getCanonicalType();
1564   bool IsConstant = Type.isConstant(Context);
1565   Type = Context.getBaseElementType(Type);
1566   const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1567                                 ? Type->getAsCXXRecordDecl()
1568                                 : nullptr;
1569   if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1570     if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1571       RD = CTD->getTemplatedDecl();
1572   if (IsClassType)
1573     *IsClassType = RD;
1574   return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1575                          RD->hasDefinition() && RD->hasMutableFields());
1576 }
1577 
1578 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1579                                       QualType Type, OpenMPClauseKind CKind,
1580                                       SourceLocation ELoc,
1581                                       bool AcceptIfMutable = true,
1582                                       bool ListItemNotVar = false) {
1583   ASTContext &Context = SemaRef.getASTContext();
1584   bool IsClassType;
1585   if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1586     unsigned Diag = ListItemNotVar ? diag::err_omp_const_list_item
1587                     : IsClassType  ? diag::err_omp_const_not_mutable_variable
1588                                    : diag::err_omp_const_variable;
1589     SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1590     if (!ListItemNotVar && D) {
1591       const VarDecl *VD = dyn_cast<VarDecl>(D);
1592       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1593                                VarDecl::DeclarationOnly;
1594       SemaRef.Diag(D->getLocation(),
1595                    IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1596           << D;
1597     }
1598     return true;
1599   }
1600   return false;
1601 }
1602 
1603 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1604                                                    bool FromParent) {
1605   D = getCanonicalDecl(D);
1606   DSAVarData DVar;
1607 
1608   auto *VD = dyn_cast<VarDecl>(D);
1609   auto TI = Threadprivates.find(D);
1610   if (TI != Threadprivates.end()) {
1611     DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1612     DVar.CKind = OMPC_threadprivate;
1613     DVar.Modifier = TI->getSecond().Modifier;
1614     return DVar;
1615   }
1616   if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1617     DVar.RefExpr = buildDeclRefExpr(
1618         SemaRef, VD, D->getType().getNonReferenceType(),
1619         VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1620     DVar.CKind = OMPC_threadprivate;
1621     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1622     return DVar;
1623   }
1624   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1625   // in a Construct, C/C++, predetermined, p.1]
1626   //  Variables appearing in threadprivate directives are threadprivate.
1627   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1628        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1629          SemaRef.getLangOpts().OpenMPUseTLS &&
1630          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1631       (VD && VD->getStorageClass() == SC_Register &&
1632        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1633     DVar.RefExpr = buildDeclRefExpr(
1634         SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1635     DVar.CKind = OMPC_threadprivate;
1636     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1637     return DVar;
1638   }
1639   if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1640       VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1641       !isLoopControlVariable(D).first) {
1642     const_iterator IterTarget =
1643         std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1644           return isOpenMPTargetExecutionDirective(Data.Directive);
1645         });
1646     if (IterTarget != end()) {
1647       const_iterator ParentIterTarget = IterTarget + 1;
1648       for (const_iterator Iter = begin(); Iter != ParentIterTarget; ++Iter) {
1649         if (isOpenMPLocal(VD, Iter)) {
1650           DVar.RefExpr =
1651               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1652                                D->getLocation());
1653           DVar.CKind = OMPC_threadprivate;
1654           return DVar;
1655         }
1656       }
1657       if (!isClauseParsingMode() || IterTarget != begin()) {
1658         auto DSAIter = IterTarget->SharingMap.find(D);
1659         if (DSAIter != IterTarget->SharingMap.end() &&
1660             isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1661           DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1662           DVar.CKind = OMPC_threadprivate;
1663           return DVar;
1664         }
1665         const_iterator End = end();
1666         if (!SemaRef.isOpenMPCapturedByRef(D,
1667                                            std::distance(ParentIterTarget, End),
1668                                            /*OpenMPCaptureLevel=*/0)) {
1669           DVar.RefExpr =
1670               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1671                                IterTarget->ConstructLoc);
1672           DVar.CKind = OMPC_threadprivate;
1673           return DVar;
1674         }
1675       }
1676     }
1677   }
1678 
1679   if (isStackEmpty())
1680     // Not in OpenMP execution region and top scope was already checked.
1681     return DVar;
1682 
1683   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1684   // in a Construct, C/C++, predetermined, p.4]
1685   //  Static data members are shared.
1686   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1687   // in a Construct, C/C++, predetermined, p.7]
1688   //  Variables with static storage duration that are declared in a scope
1689   //  inside the construct are shared.
1690   if (VD && VD->isStaticDataMember()) {
1691     // Check for explicitly specified attributes.
1692     const_iterator I = begin();
1693     const_iterator EndI = end();
1694     if (FromParent && I != EndI)
1695       ++I;
1696     if (I != EndI) {
1697       auto It = I->SharingMap.find(D);
1698       if (It != I->SharingMap.end()) {
1699         const DSAInfo &Data = It->getSecond();
1700         DVar.RefExpr = Data.RefExpr.getPointer();
1701         DVar.PrivateCopy = Data.PrivateCopy;
1702         DVar.CKind = Data.Attributes;
1703         DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1704         DVar.DKind = I->Directive;
1705         DVar.Modifier = Data.Modifier;
1706         DVar.AppliedToPointee = Data.AppliedToPointee;
1707         return DVar;
1708       }
1709     }
1710 
1711     DVar.CKind = OMPC_shared;
1712     return DVar;
1713   }
1714 
1715   auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1716   // The predetermined shared attribute for const-qualified types having no
1717   // mutable members was removed after OpenMP 3.1.
1718   if (SemaRef.LangOpts.OpenMP <= 31) {
1719     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1720     // in a Construct, C/C++, predetermined, p.6]
1721     //  Variables with const qualified type having no mutable member are
1722     //  shared.
1723     if (isConstNotMutableType(SemaRef, D->getType())) {
1724       // Variables with const-qualified type having no mutable member may be
1725       // listed in a firstprivate clause, even if they are static data members.
1726       DSAVarData DVarTemp = hasInnermostDSA(
1727           D,
1728           [](OpenMPClauseKind C, bool) {
1729             return C == OMPC_firstprivate || C == OMPC_shared;
1730           },
1731           MatchesAlways, FromParent);
1732       if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1733         return DVarTemp;
1734 
1735       DVar.CKind = OMPC_shared;
1736       return DVar;
1737     }
1738   }
1739 
1740   // Explicitly specified attributes and local variables with predetermined
1741   // attributes.
1742   const_iterator I = begin();
1743   const_iterator EndI = end();
1744   if (FromParent && I != EndI)
1745     ++I;
1746   if (I == EndI)
1747     return DVar;
1748   auto It = I->SharingMap.find(D);
1749   if (It != I->SharingMap.end()) {
1750     const DSAInfo &Data = It->getSecond();
1751     DVar.RefExpr = Data.RefExpr.getPointer();
1752     DVar.PrivateCopy = Data.PrivateCopy;
1753     DVar.CKind = Data.Attributes;
1754     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1755     DVar.DKind = I->Directive;
1756     DVar.Modifier = Data.Modifier;
1757     DVar.AppliedToPointee = Data.AppliedToPointee;
1758   }
1759 
1760   return DVar;
1761 }
1762 
1763 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1764                                                         bool FromParent) const {
1765   if (isStackEmpty()) {
1766     const_iterator I;
1767     return getDSA(I, D);
1768   }
1769   D = getCanonicalDecl(D);
1770   const_iterator StartI = begin();
1771   const_iterator EndI = end();
1772   if (FromParent && StartI != EndI)
1773     ++StartI;
1774   return getDSA(StartI, D);
1775 }
1776 
1777 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1778                                                         unsigned Level) const {
1779   if (getStackSize() <= Level)
1780     return DSAVarData();
1781   D = getCanonicalDecl(D);
1782   const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level);
1783   return getDSA(StartI, D);
1784 }
1785 
1786 const DSAStackTy::DSAVarData
1787 DSAStackTy::hasDSA(ValueDecl *D,
1788                    const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1789                    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1790                    bool FromParent) const {
1791   if (isStackEmpty())
1792     return {};
1793   D = getCanonicalDecl(D);
1794   const_iterator I = begin();
1795   const_iterator EndI = end();
1796   if (FromParent && I != EndI)
1797     ++I;
1798   for (; I != EndI; ++I) {
1799     if (!DPred(I->Directive) &&
1800         !isImplicitOrExplicitTaskingRegion(I->Directive))
1801       continue;
1802     const_iterator NewI = I;
1803     DSAVarData DVar = getDSA(NewI, D);
1804     if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee))
1805       return DVar;
1806   }
1807   return {};
1808 }
1809 
1810 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1811     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1812     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1813     bool FromParent) const {
1814   if (isStackEmpty())
1815     return {};
1816   D = getCanonicalDecl(D);
1817   const_iterator StartI = begin();
1818   const_iterator EndI = end();
1819   if (FromParent && StartI != EndI)
1820     ++StartI;
1821   if (StartI == EndI || !DPred(StartI->Directive))
1822     return {};
1823   const_iterator NewI = StartI;
1824   DSAVarData DVar = getDSA(NewI, D);
1825   return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee))
1826              ? DVar
1827              : DSAVarData();
1828 }
1829 
1830 bool DSAStackTy::hasExplicitDSA(
1831     const ValueDecl *D,
1832     const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1833     unsigned Level, bool NotLastprivate) const {
1834   if (getStackSize() <= Level)
1835     return false;
1836   D = getCanonicalDecl(D);
1837   const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1838   auto I = StackElem.SharingMap.find(D);
1839   if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() &&
1840       CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) &&
1841       (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1842     return true;
1843   // Check predetermined rules for the loop control variables.
1844   auto LI = StackElem.LCVMap.find(D);
1845   if (LI != StackElem.LCVMap.end())
1846     return CPred(OMPC_private, /*AppliedToPointee=*/false);
1847   return false;
1848 }
1849 
1850 bool DSAStackTy::hasExplicitDirective(
1851     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1852     unsigned Level) const {
1853   if (getStackSize() <= Level)
1854     return false;
1855   const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1856   return DPred(StackElem.Directive);
1857 }
1858 
1859 bool DSAStackTy::hasDirective(
1860     const llvm::function_ref<bool(OpenMPDirectiveKind,
1861                                   const DeclarationNameInfo &, SourceLocation)>
1862         DPred,
1863     bool FromParent) const {
1864   // We look only in the enclosing region.
1865   size_t Skip = FromParent ? 2 : 1;
1866   for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1867        I != E; ++I) {
1868     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1869       return true;
1870   }
1871   return false;
1872 }
1873 
1874 void Sema::InitDataSharingAttributesStack() {
1875   VarDataSharingAttributesStack = new DSAStackTy(*this);
1876 }
1877 
1878 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1879 
1880 void Sema::pushOpenMPFunctionRegion() { DSAStack->pushFunction(); }
1881 
1882 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1883   DSAStack->popFunction(OldFSI);
1884 }
1885 
1886 static bool isOpenMPDeviceDelayedContext(Sema &S) {
1887   assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1888          "Expected OpenMP device compilation.");
1889   return !S.isInOpenMPTargetExecutionDirective();
1890 }
1891 
1892 namespace {
1893 /// Status of the function emission on the host/device.
1894 enum class FunctionEmissionStatus {
1895   Emitted,
1896   Discarded,
1897   Unknown,
1898 };
1899 } // anonymous namespace
1900 
1901 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1902                                                          unsigned DiagID,
1903                                                          FunctionDecl *FD) {
1904   assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1905          "Expected OpenMP device compilation.");
1906 
1907   SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
1908   if (FD) {
1909     FunctionEmissionStatus FES = getEmissionStatus(FD);
1910     switch (FES) {
1911     case FunctionEmissionStatus::Emitted:
1912       Kind = SemaDiagnosticBuilder::K_Immediate;
1913       break;
1914     case FunctionEmissionStatus::Unknown:
1915       // TODO: We should always delay diagnostics here in case a target
1916       //       region is in a function we do not emit. However, as the
1917       //       current diagnostics are associated with the function containing
1918       //       the target region and we do not emit that one, we would miss out
1919       //       on diagnostics for the target region itself. We need to anchor
1920       //       the diagnostics with the new generated function *or* ensure we
1921       //       emit diagnostics associated with the surrounding function.
1922       Kind = isOpenMPDeviceDelayedContext(*this)
1923                  ? SemaDiagnosticBuilder::K_Deferred
1924                  : SemaDiagnosticBuilder::K_Immediate;
1925       break;
1926     case FunctionEmissionStatus::TemplateDiscarded:
1927     case FunctionEmissionStatus::OMPDiscarded:
1928       Kind = SemaDiagnosticBuilder::K_Nop;
1929       break;
1930     case FunctionEmissionStatus::CUDADiscarded:
1931       llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
1932       break;
1933     }
1934   }
1935 
1936   return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this);
1937 }
1938 
1939 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc,
1940                                                        unsigned DiagID,
1941                                                        FunctionDecl *FD) {
1942   assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1943          "Expected OpenMP host compilation.");
1944 
1945   SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
1946   if (FD) {
1947     FunctionEmissionStatus FES = getEmissionStatus(FD);
1948     switch (FES) {
1949     case FunctionEmissionStatus::Emitted:
1950       Kind = SemaDiagnosticBuilder::K_Immediate;
1951       break;
1952     case FunctionEmissionStatus::Unknown:
1953       Kind = SemaDiagnosticBuilder::K_Deferred;
1954       break;
1955     case FunctionEmissionStatus::TemplateDiscarded:
1956     case FunctionEmissionStatus::OMPDiscarded:
1957     case FunctionEmissionStatus::CUDADiscarded:
1958       Kind = SemaDiagnosticBuilder::K_Nop;
1959       break;
1960     }
1961   }
1962 
1963   return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this);
1964 }
1965 
1966 static OpenMPDefaultmapClauseKind
1967 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) {
1968   if (LO.OpenMP <= 45) {
1969     if (VD->getType().getNonReferenceType()->isScalarType())
1970       return OMPC_DEFAULTMAP_scalar;
1971     return OMPC_DEFAULTMAP_aggregate;
1972   }
1973   if (VD->getType().getNonReferenceType()->isAnyPointerType())
1974     return OMPC_DEFAULTMAP_pointer;
1975   if (VD->getType().getNonReferenceType()->isScalarType())
1976     return OMPC_DEFAULTMAP_scalar;
1977   return OMPC_DEFAULTMAP_aggregate;
1978 }
1979 
1980 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
1981                                  unsigned OpenMPCaptureLevel) const {
1982   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1983 
1984   ASTContext &Ctx = getASTContext();
1985   bool IsByRef = true;
1986 
1987   // Find the directive that is associated with the provided scope.
1988   D = cast<ValueDecl>(D->getCanonicalDecl());
1989   QualType Ty = D->getType();
1990 
1991   bool IsVariableUsedInMapClause = false;
1992   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1993     // This table summarizes how a given variable should be passed to the device
1994     // given its type and the clauses where it appears. This table is based on
1995     // the description in OpenMP 4.5 [2.10.4, target Construct] and
1996     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1997     //
1998     // =========================================================================
1999     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
2000     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
2001     // =========================================================================
2002     // | scl  |               |     |       |       -       |          | bycopy|
2003     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
2004     // | scl  |               |  x  |   -   |       -       |     -    | null  |
2005     // | scl  |       x       |     |       |       -       |          | byref |
2006     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
2007     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
2008     // | scl  |               |  -  |   -   |       -       |     x    | byref |
2009     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
2010     //
2011     // | agg  |      n.a.     |     |       |       -       |          | byref |
2012     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
2013     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
2014     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
2015     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
2016     //
2017     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
2018     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
2019     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
2020     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
2021     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
2022     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
2023     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
2024     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
2025     // =========================================================================
2026     // Legend:
2027     //  scl - scalar
2028     //  ptr - pointer
2029     //  agg - aggregate
2030     //  x - applies
2031     //  - - invalid in this combination
2032     //  [] - mapped with an array section
2033     //  byref - should be mapped by reference
2034     //  byval - should be mapped by value
2035     //  null - initialize a local variable to null on the device
2036     //
2037     // Observations:
2038     //  - All scalar declarations that show up in a map clause have to be passed
2039     //    by reference, because they may have been mapped in the enclosing data
2040     //    environment.
2041     //  - If the scalar value does not fit the size of uintptr, it has to be
2042     //    passed by reference, regardless the result in the table above.
2043     //  - For pointers mapped by value that have either an implicit map or an
2044     //    array section, the runtime library may pass the NULL value to the
2045     //    device instead of the value passed to it by the compiler.
2046 
2047     if (Ty->isReferenceType())
2048       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
2049 
2050     // Locate map clauses and see if the variable being captured is referred to
2051     // in any of those clauses. Here we only care about variables, not fields,
2052     // because fields are part of aggregates.
2053     bool IsVariableAssociatedWithSection = false;
2054 
2055     DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2056         D, Level,
2057         [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection,
2058          D](OMPClauseMappableExprCommon::MappableExprComponentListRef
2059                 MapExprComponents,
2060             OpenMPClauseKind WhereFoundClauseKind) {
2061           // Only the map clause information influences how a variable is
2062           // captured. E.g. is_device_ptr does not require changing the default
2063           // behavior.
2064           if (WhereFoundClauseKind != OMPC_map)
2065             return false;
2066 
2067           auto EI = MapExprComponents.rbegin();
2068           auto EE = MapExprComponents.rend();
2069 
2070           assert(EI != EE && "Invalid map expression!");
2071 
2072           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
2073             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
2074 
2075           ++EI;
2076           if (EI == EE)
2077             return false;
2078 
2079           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
2080               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
2081               isa<MemberExpr>(EI->getAssociatedExpression()) ||
2082               isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) {
2083             IsVariableAssociatedWithSection = true;
2084             // There is nothing more we need to know about this variable.
2085             return true;
2086           }
2087 
2088           // Keep looking for more map info.
2089           return false;
2090         });
2091 
2092     if (IsVariableUsedInMapClause) {
2093       // If variable is identified in a map clause it is always captured by
2094       // reference except if it is a pointer that is dereferenced somehow.
2095       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
2096     } else {
2097       // By default, all the data that has a scalar type is mapped by copy
2098       // (except for reduction variables).
2099       // Defaultmap scalar is mutual exclusive to defaultmap pointer
2100       IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
2101                  !Ty->isAnyPointerType()) ||
2102                 !Ty->isScalarType() ||
2103                 DSAStack->isDefaultmapCapturedByRef(
2104                     Level, getVariableCategoryFromDecl(LangOpts, D)) ||
2105                 DSAStack->hasExplicitDSA(
2106                     D,
2107                     [](OpenMPClauseKind K, bool AppliedToPointee) {
2108                       return K == OMPC_reduction && !AppliedToPointee;
2109                     },
2110                     Level);
2111     }
2112   }
2113 
2114   if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
2115     IsByRef =
2116         ((IsVariableUsedInMapClause &&
2117           DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
2118               OMPD_target) ||
2119          !(DSAStack->hasExplicitDSA(
2120                D,
2121                [](OpenMPClauseKind K, bool AppliedToPointee) -> bool {
2122                  return K == OMPC_firstprivate ||
2123                         (K == OMPC_reduction && AppliedToPointee);
2124                },
2125                Level, /*NotLastprivate=*/true) ||
2126            DSAStack->isUsesAllocatorsDecl(Level, D))) &&
2127         // If the variable is artificial and must be captured by value - try to
2128         // capture by value.
2129         !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
2130           !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) &&
2131         // If the variable is implicitly firstprivate and scalar - capture by
2132         // copy
2133         !(DSAStack->getDefaultDSA() == DSA_firstprivate &&
2134           !DSAStack->hasExplicitDSA(
2135               D, [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; },
2136               Level) &&
2137           !DSAStack->isLoopControlVariable(D, Level).first);
2138   }
2139 
2140   // When passing data by copy, we need to make sure it fits the uintptr size
2141   // and alignment, because the runtime library only deals with uintptr types.
2142   // If it does not fit the uintptr size, we need to pass the data by reference
2143   // instead.
2144   if (!IsByRef &&
2145       (Ctx.getTypeSizeInChars(Ty) >
2146            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
2147        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
2148     IsByRef = true;
2149   }
2150 
2151   return IsByRef;
2152 }
2153 
2154 unsigned Sema::getOpenMPNestingLevel() const {
2155   assert(getLangOpts().OpenMP);
2156   return DSAStack->getNestingLevel();
2157 }
2158 
2159 bool Sema::isInOpenMPTargetExecutionDirective() const {
2160   return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
2161           !DSAStack->isClauseParsingMode()) ||
2162          DSAStack->hasDirective(
2163              [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2164                 SourceLocation) -> bool {
2165                return isOpenMPTargetExecutionDirective(K);
2166              },
2167              false);
2168 }
2169 
2170 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
2171                                     unsigned StopAt) {
2172   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2173   D = getCanonicalDecl(D);
2174 
2175   auto *VD = dyn_cast<VarDecl>(D);
2176   // Do not capture constexpr variables.
2177   if (VD && VD->isConstexpr())
2178     return nullptr;
2179 
2180   // If we want to determine whether the variable should be captured from the
2181   // perspective of the current capturing scope, and we've already left all the
2182   // capturing scopes of the top directive on the stack, check from the
2183   // perspective of its parent directive (if any) instead.
2184   DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
2185       *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
2186 
2187   // If we are attempting to capture a global variable in a directive with
2188   // 'target' we return true so that this global is also mapped to the device.
2189   //
2190   if (VD && !VD->hasLocalStorage() &&
2191       (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
2192     if (isInOpenMPTargetExecutionDirective()) {
2193       DSAStackTy::DSAVarData DVarTop =
2194           DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2195       if (DVarTop.CKind != OMPC_unknown && DVarTop.RefExpr)
2196         return VD;
2197       // If the declaration is enclosed in a 'declare target' directive,
2198       // then it should not be captured.
2199       //
2200       if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2201         return nullptr;
2202       CapturedRegionScopeInfo *CSI = nullptr;
2203       for (FunctionScopeInfo *FSI : llvm::drop_begin(
2204                llvm::reverse(FunctionScopes),
2205                CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) {
2206         if (!isa<CapturingScopeInfo>(FSI))
2207           return nullptr;
2208         if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2209           if (RSI->CapRegionKind == CR_OpenMP) {
2210             CSI = RSI;
2211             break;
2212           }
2213       }
2214       assert(CSI && "Failed to find CapturedRegionScopeInfo");
2215       SmallVector<OpenMPDirectiveKind, 4> Regions;
2216       getOpenMPCaptureRegions(Regions,
2217                               DSAStack->getDirective(CSI->OpenMPLevel));
2218       if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task)
2219         return VD;
2220     }
2221     if (isInOpenMPDeclareTargetContext()) {
2222       // Try to mark variable as declare target if it is used in capturing
2223       // regions.
2224       if (LangOpts.OpenMP <= 45 &&
2225           !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2226         checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
2227       return nullptr;
2228     }
2229   }
2230 
2231   if (CheckScopeInfo) {
2232     bool OpenMPFound = false;
2233     for (unsigned I = StopAt + 1; I > 0; --I) {
2234       FunctionScopeInfo *FSI = FunctionScopes[I - 1];
2235       if (!isa<CapturingScopeInfo>(FSI))
2236         return nullptr;
2237       if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2238         if (RSI->CapRegionKind == CR_OpenMP) {
2239           OpenMPFound = true;
2240           break;
2241         }
2242     }
2243     if (!OpenMPFound)
2244       return nullptr;
2245   }
2246 
2247   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2248       (!DSAStack->isClauseParsingMode() ||
2249        DSAStack->getParentDirective() != OMPD_unknown)) {
2250     auto &&Info = DSAStack->isLoopControlVariable(D);
2251     if (Info.first ||
2252         (VD && VD->hasLocalStorage() &&
2253          isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
2254         (VD && DSAStack->isForceVarCapturing()))
2255       return VD ? VD : Info.second;
2256     DSAStackTy::DSAVarData DVarTop =
2257         DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2258     if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind) &&
2259         (!VD || VD->hasLocalStorage() || !DVarTop.AppliedToPointee))
2260       return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl());
2261     // Threadprivate variables must not be captured.
2262     if (isOpenMPThreadPrivate(DVarTop.CKind))
2263       return nullptr;
2264     // The variable is not private or it is the variable in the directive with
2265     // default(none) clause and not used in any clause.
2266     DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2267         D,
2268         [](OpenMPClauseKind C, bool AppliedToPointee) {
2269           return isOpenMPPrivate(C) && !AppliedToPointee;
2270         },
2271         [](OpenMPDirectiveKind) { return true; },
2272         DSAStack->isClauseParsingMode());
2273     // Global shared must not be captured.
2274     if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown &&
2275         ((DSAStack->getDefaultDSA() != DSA_none &&
2276           DSAStack->getDefaultDSA() != DSA_firstprivate) ||
2277          DVarTop.CKind == OMPC_shared))
2278       return nullptr;
2279     if (DVarPrivate.CKind != OMPC_unknown ||
2280         (VD && (DSAStack->getDefaultDSA() == DSA_none ||
2281                 DSAStack->getDefaultDSA() == DSA_firstprivate)))
2282       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
2283   }
2284   return nullptr;
2285 }
2286 
2287 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
2288                                         unsigned Level) const {
2289   FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2290 }
2291 
2292 void Sema::startOpenMPLoop() {
2293   assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2294   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2295     DSAStack->loopInit();
2296 }
2297 
2298 void Sema::startOpenMPCXXRangeFor() {
2299   assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2300   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2301     DSAStack->resetPossibleLoopCounter();
2302     DSAStack->loopStart();
2303   }
2304 }
2305 
2306 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level,
2307                                            unsigned CapLevel) const {
2308   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2309   if (DSAStack->hasExplicitDirective(isOpenMPTaskingDirective, Level)) {
2310     bool IsTriviallyCopyable =
2311         D->getType().getNonReferenceType().isTriviallyCopyableType(Context) &&
2312         !D->getType()
2313              .getNonReferenceType()
2314              .getCanonicalType()
2315              ->getAsCXXRecordDecl();
2316     OpenMPDirectiveKind DKind = DSAStack->getDirective(Level);
2317     SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2318     getOpenMPCaptureRegions(CaptureRegions, DKind);
2319     if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) &&
2320         (IsTriviallyCopyable ||
2321          !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) {
2322       if (DSAStack->hasExplicitDSA(
2323               D,
2324               [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; },
2325               Level, /*NotLastprivate=*/true))
2326         return OMPC_firstprivate;
2327       DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2328       if (DVar.CKind != OMPC_shared &&
2329           !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) {
2330         DSAStack->addImplicitTaskFirstprivate(Level, D);
2331         return OMPC_firstprivate;
2332       }
2333     }
2334   }
2335   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2336     if (DSAStack->getAssociatedLoops() > 0 && !DSAStack->isLoopStarted()) {
2337       DSAStack->resetPossibleLoopCounter(D);
2338       DSAStack->loopStart();
2339       return OMPC_private;
2340     }
2341     if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
2342          DSAStack->isLoopControlVariable(D).first) &&
2343         !DSAStack->hasExplicitDSA(
2344             D, [](OpenMPClauseKind K, bool) { return K != OMPC_private; },
2345             Level) &&
2346         !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2347       return OMPC_private;
2348   }
2349   if (const auto *VD = dyn_cast<VarDecl>(D)) {
2350     if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
2351         DSAStack->isForceVarCapturing() &&
2352         !DSAStack->hasExplicitDSA(
2353             D, [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; },
2354             Level))
2355       return OMPC_private;
2356   }
2357   // User-defined allocators are private since they must be defined in the
2358   // context of target region.
2359   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) &&
2360       DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr(
2361           DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
2362           DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator)
2363     return OMPC_private;
2364   return (DSAStack->hasExplicitDSA(
2365               D, [](OpenMPClauseKind K, bool) { return K == OMPC_private; },
2366               Level) ||
2367           (DSAStack->isClauseParsingMode() &&
2368            DSAStack->getClauseParsingMode() == OMPC_private) ||
2369           // Consider taskgroup reduction descriptor variable a private
2370           // to avoid possible capture in the region.
2371           (DSAStack->hasExplicitDirective(
2372                [](OpenMPDirectiveKind K) {
2373                  return K == OMPD_taskgroup ||
2374                         ((isOpenMPParallelDirective(K) ||
2375                           isOpenMPWorksharingDirective(K)) &&
2376                          !isOpenMPSimdDirective(K));
2377                },
2378                Level) &&
2379            DSAStack->isTaskgroupReductionRef(D, Level)))
2380              ? OMPC_private
2381              : OMPC_unknown;
2382 }
2383 
2384 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2385                                 unsigned Level) {
2386   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2387   D = getCanonicalDecl(D);
2388   OpenMPClauseKind OMPC = OMPC_unknown;
2389   for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2390     const unsigned NewLevel = I - 1;
2391     if (DSAStack->hasExplicitDSA(
2392             D,
2393             [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) {
2394               if (isOpenMPPrivate(K) && !AppliedToPointee) {
2395                 OMPC = K;
2396                 return true;
2397               }
2398               return false;
2399             },
2400             NewLevel))
2401       break;
2402     if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2403             D, NewLevel,
2404             [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2405                OpenMPClauseKind) { return true; })) {
2406       OMPC = OMPC_map;
2407       break;
2408     }
2409     if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2410                                        NewLevel)) {
2411       OMPC = OMPC_map;
2412       if (DSAStack->mustBeFirstprivateAtLevel(
2413               NewLevel, getVariableCategoryFromDecl(LangOpts, D)))
2414         OMPC = OMPC_firstprivate;
2415       break;
2416     }
2417   }
2418   if (OMPC != OMPC_unknown)
2419     FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC)));
2420 }
2421 
2422 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level,
2423                                       unsigned CaptureLevel) const {
2424   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2425   // Return true if the current level is no longer enclosed in a target region.
2426 
2427   SmallVector<OpenMPDirectiveKind, 4> Regions;
2428   getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
2429   const auto *VD = dyn_cast<VarDecl>(D);
2430   return VD && !VD->hasLocalStorage() &&
2431          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2432                                         Level) &&
2433          Regions[CaptureLevel] != OMPD_task;
2434 }
2435 
2436 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level,
2437                                       unsigned CaptureLevel) const {
2438   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2439   // Return true if the current level is no longer enclosed in a target region.
2440 
2441   if (const auto *VD = dyn_cast<VarDecl>(D)) {
2442     if (!VD->hasLocalStorage()) {
2443       if (isInOpenMPTargetExecutionDirective())
2444         return true;
2445       DSAStackTy::DSAVarData TopDVar =
2446           DSAStack->getTopDSA(D, /*FromParent=*/false);
2447       unsigned NumLevels =
2448           getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2449       if (Level == 0)
2450         return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared;
2451       do {
2452         --Level;
2453         DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2454         if (DVar.CKind != OMPC_shared)
2455           return true;
2456       } while (Level > 0);
2457     }
2458   }
2459   return true;
2460 }
2461 
2462 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
2463 
2464 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc,
2465                                           OMPTraitInfo &TI) {
2466   OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI));
2467 }
2468 
2469 void Sema::ActOnOpenMPEndDeclareVariant() {
2470   assert(isInOpenMPDeclareVariantScope() &&
2471          "Not in OpenMP declare variant scope!");
2472 
2473   OMPDeclareVariantScopes.pop_back();
2474 }
2475 
2476 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller,
2477                                          const FunctionDecl *Callee,
2478                                          SourceLocation Loc) {
2479   assert(LangOpts.OpenMP && "Expected OpenMP compilation mode.");
2480   Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2481       OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl());
2482   // Ignore host functions during device analyzis.
2483   if (LangOpts.OpenMPIsDevice &&
2484       (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host))
2485     return;
2486   // Ignore nohost functions during host analyzis.
2487   if (!LangOpts.OpenMPIsDevice && DevTy &&
2488       *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2489     return;
2490   const FunctionDecl *FD = Callee->getMostRecentDecl();
2491   DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD);
2492   if (LangOpts.OpenMPIsDevice && DevTy &&
2493       *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2494     // Diagnose host function called during device codegen.
2495     StringRef HostDevTy =
2496         getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host);
2497     Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
2498     Diag(*OMPDeclareTargetDeclAttr::getLocation(FD),
2499          diag::note_omp_marked_device_type_here)
2500         << HostDevTy;
2501     return;
2502   }
2503   if (!LangOpts.OpenMPIsDevice && DevTy &&
2504       *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2505     // Diagnose nohost function called during host codegen.
2506     StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2507         OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
2508     Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
2509     Diag(*OMPDeclareTargetDeclAttr::getLocation(FD),
2510          diag::note_omp_marked_device_type_here)
2511         << NoHostDevTy;
2512   }
2513 }
2514 
2515 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2516                                const DeclarationNameInfo &DirName,
2517                                Scope *CurScope, SourceLocation Loc) {
2518   DSAStack->push(DKind, DirName, CurScope, Loc);
2519   PushExpressionEvaluationContext(
2520       ExpressionEvaluationContext::PotentiallyEvaluated);
2521 }
2522 
2523 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
2524   DSAStack->setClauseParsingMode(K);
2525 }
2526 
2527 void Sema::EndOpenMPClause() {
2528   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2529   CleanupVarDeclMarking();
2530 }
2531 
2532 static std::pair<ValueDecl *, bool>
2533 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
2534                SourceRange &ERange, bool AllowArraySection = false);
2535 
2536 /// Check consistency of the reduction clauses.
2537 static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2538                                   ArrayRef<OMPClause *> Clauses) {
2539   bool InscanFound = false;
2540   SourceLocation InscanLoc;
2541   // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2542   // A reduction clause without the inscan reduction-modifier may not appear on
2543   // a construct on which a reduction clause with the inscan reduction-modifier
2544   // appears.
2545   for (OMPClause *C : Clauses) {
2546     if (C->getClauseKind() != OMPC_reduction)
2547       continue;
2548     auto *RC = cast<OMPReductionClause>(C);
2549     if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2550       InscanFound = true;
2551       InscanLoc = RC->getModifierLoc();
2552       continue;
2553     }
2554     if (RC->getModifier() == OMPC_REDUCTION_task) {
2555       // OpenMP 5.0, 2.19.5.4 reduction Clause.
2556       // A reduction clause with the task reduction-modifier may only appear on
2557       // a parallel construct, a worksharing construct or a combined or
2558       // composite construct for which any of the aforementioned constructs is a
2559       // constituent construct and simd or loop are not constituent constructs.
2560       OpenMPDirectiveKind CurDir = Stack->getCurrentDirective();
2561       if (!(isOpenMPParallelDirective(CurDir) ||
2562             isOpenMPWorksharingDirective(CurDir)) ||
2563           isOpenMPSimdDirective(CurDir))
2564         S.Diag(RC->getModifierLoc(),
2565                diag::err_omp_reduction_task_not_parallel_or_worksharing);
2566       continue;
2567     }
2568   }
2569   if (InscanFound) {
2570     for (OMPClause *C : Clauses) {
2571       if (C->getClauseKind() != OMPC_reduction)
2572         continue;
2573       auto *RC = cast<OMPReductionClause>(C);
2574       if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2575         S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown
2576                    ? RC->getBeginLoc()
2577                    : RC->getModifierLoc(),
2578                diag::err_omp_inscan_reduction_expected);
2579         S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction);
2580         continue;
2581       }
2582       for (Expr *Ref : RC->varlists()) {
2583         assert(Ref && "NULL expr in OpenMP nontemporal clause.");
2584         SourceLocation ELoc;
2585         SourceRange ERange;
2586         Expr *SimpleRefExpr = Ref;
2587         auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
2588                                   /*AllowArraySection=*/true);
2589         ValueDecl *D = Res.first;
2590         if (!D)
2591           continue;
2592         if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) {
2593           S.Diag(Ref->getExprLoc(),
2594                  diag::err_omp_reduction_not_inclusive_exclusive)
2595               << Ref->getSourceRange();
2596         }
2597       }
2598     }
2599   }
2600 }
2601 
2602 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2603                                  ArrayRef<OMPClause *> Clauses);
2604 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2605                                  bool WithInit);
2606 
2607 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2608                               const ValueDecl *D,
2609                               const DSAStackTy::DSAVarData &DVar,
2610                               bool IsLoopIterVar = false);
2611 
2612 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
2613   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2614   //  A variable of class type (or array thereof) that appears in a lastprivate
2615   //  clause requires an accessible, unambiguous default constructor for the
2616   //  class type, unless the list item is also specified in a firstprivate
2617   //  clause.
2618   if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
2619     for (OMPClause *C : D->clauses()) {
2620       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
2621         SmallVector<Expr *, 8> PrivateCopies;
2622         for (Expr *DE : Clause->varlists()) {
2623           if (DE->isValueDependent() || DE->isTypeDependent()) {
2624             PrivateCopies.push_back(nullptr);
2625             continue;
2626           }
2627           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
2628           auto *VD = cast<VarDecl>(DRE->getDecl());
2629           QualType Type = VD->getType().getNonReferenceType();
2630           const DSAStackTy::DSAVarData DVar =
2631               DSAStack->getTopDSA(VD, /*FromParent=*/false);
2632           if (DVar.CKind == OMPC_lastprivate) {
2633             // Generate helper private variable and initialize it with the
2634             // default value. The address of the original variable is replaced
2635             // by the address of the new private variable in CodeGen. This new
2636             // variable is not added to IdResolver, so the code in the OpenMP
2637             // region uses original variable for proper diagnostics.
2638             VarDecl *VDPrivate = buildVarDecl(
2639                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
2640                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
2641             ActOnUninitializedDecl(VDPrivate);
2642             if (VDPrivate->isInvalidDecl()) {
2643               PrivateCopies.push_back(nullptr);
2644               continue;
2645             }
2646             PrivateCopies.push_back(buildDeclRefExpr(
2647                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
2648           } else {
2649             // The variable is also a firstprivate, so initialization sequence
2650             // for private copy is generated already.
2651             PrivateCopies.push_back(nullptr);
2652           }
2653         }
2654         Clause->setPrivateCopies(PrivateCopies);
2655         continue;
2656       }
2657       // Finalize nontemporal clause by handling private copies, if any.
2658       if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) {
2659         SmallVector<Expr *, 8> PrivateRefs;
2660         for (Expr *RefExpr : Clause->varlists()) {
2661           assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
2662           SourceLocation ELoc;
2663           SourceRange ERange;
2664           Expr *SimpleRefExpr = RefExpr;
2665           auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
2666           if (Res.second)
2667             // It will be analyzed later.
2668             PrivateRefs.push_back(RefExpr);
2669           ValueDecl *D = Res.first;
2670           if (!D)
2671             continue;
2672 
2673           const DSAStackTy::DSAVarData DVar =
2674               DSAStack->getTopDSA(D, /*FromParent=*/false);
2675           PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy
2676                                                  : SimpleRefExpr);
2677         }
2678         Clause->setPrivateRefs(PrivateRefs);
2679         continue;
2680       }
2681       if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) {
2682         for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) {
2683           OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I);
2684           auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts());
2685           if (!DRE)
2686             continue;
2687           ValueDecl *VD = DRE->getDecl();
2688           if (!VD || !isa<VarDecl>(VD))
2689             continue;
2690           DSAStackTy::DSAVarData DVar =
2691               DSAStack->getTopDSA(VD, /*FromParent=*/false);
2692           // OpenMP [2.12.5, target Construct]
2693           // Memory allocators that appear in a uses_allocators clause cannot
2694           // appear in other data-sharing attribute clauses or data-mapping
2695           // attribute clauses in the same construct.
2696           Expr *MapExpr = nullptr;
2697           if (DVar.RefExpr ||
2698               DSAStack->checkMappableExprComponentListsForDecl(
2699                   VD, /*CurrentRegionOnly=*/true,
2700                   [VD, &MapExpr](
2701                       OMPClauseMappableExprCommon::MappableExprComponentListRef
2702                           MapExprComponents,
2703                       OpenMPClauseKind C) {
2704                     auto MI = MapExprComponents.rbegin();
2705                     auto ME = MapExprComponents.rend();
2706                     if (MI != ME &&
2707                         MI->getAssociatedDeclaration()->getCanonicalDecl() ==
2708                             VD->getCanonicalDecl()) {
2709                       MapExpr = MI->getAssociatedExpression();
2710                       return true;
2711                     }
2712                     return false;
2713                   })) {
2714             Diag(D.Allocator->getExprLoc(),
2715                  diag::err_omp_allocator_used_in_clauses)
2716                 << D.Allocator->getSourceRange();
2717             if (DVar.RefExpr)
2718               reportOriginalDsa(*this, DSAStack, VD, DVar);
2719             else
2720               Diag(MapExpr->getExprLoc(), diag::note_used_here)
2721                   << MapExpr->getSourceRange();
2722           }
2723         }
2724         continue;
2725       }
2726     }
2727     // Check allocate clauses.
2728     if (!CurContext->isDependentContext())
2729       checkAllocateClauses(*this, DSAStack, D->clauses());
2730     checkReductionClauses(*this, DSAStack, D->clauses());
2731   }
2732 
2733   DSAStack->pop();
2734   DiscardCleanupsInEvaluationContext();
2735   PopExpressionEvaluationContext();
2736 }
2737 
2738 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2739                                      Expr *NumIterations, Sema &SemaRef,
2740                                      Scope *S, DSAStackTy *Stack);
2741 
2742 namespace {
2743 
2744 class VarDeclFilterCCC final : public CorrectionCandidateCallback {
2745 private:
2746   Sema &SemaRef;
2747 
2748 public:
2749   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
2750   bool ValidateCandidate(const TypoCorrection &Candidate) override {
2751     NamedDecl *ND = Candidate.getCorrectionDecl();
2752     if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
2753       return VD->hasGlobalStorage() &&
2754              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2755                                    SemaRef.getCurScope());
2756     }
2757     return false;
2758   }
2759 
2760   std::unique_ptr<CorrectionCandidateCallback> clone() override {
2761     return std::make_unique<VarDeclFilterCCC>(*this);
2762   }
2763 };
2764 
2765 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
2766 private:
2767   Sema &SemaRef;
2768 
2769 public:
2770   explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2771   bool ValidateCandidate(const TypoCorrection &Candidate) override {
2772     NamedDecl *ND = Candidate.getCorrectionDecl();
2773     if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
2774                isa<FunctionDecl>(ND))) {
2775       return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2776                                    SemaRef.getCurScope());
2777     }
2778     return false;
2779   }
2780 
2781   std::unique_ptr<CorrectionCandidateCallback> clone() override {
2782     return std::make_unique<VarOrFuncDeclFilterCCC>(*this);
2783   }
2784 };
2785 
2786 } // namespace
2787 
2788 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2789                                          CXXScopeSpec &ScopeSpec,
2790                                          const DeclarationNameInfo &Id,
2791                                          OpenMPDirectiveKind Kind) {
2792   LookupResult Lookup(*this, Id, LookupOrdinaryName);
2793   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2794 
2795   if (Lookup.isAmbiguous())
2796     return ExprError();
2797 
2798   VarDecl *VD;
2799   if (!Lookup.isSingleResult()) {
2800     VarDeclFilterCCC CCC(*this);
2801     if (TypoCorrection Corrected =
2802             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2803                         CTK_ErrorRecovery)) {
2804       diagnoseTypo(Corrected,
2805                    PDiag(Lookup.empty()
2806                              ? diag::err_undeclared_var_use_suggest
2807                              : diag::err_omp_expected_var_arg_suggest)
2808                        << Id.getName());
2809       VD = Corrected.getCorrectionDeclAs<VarDecl>();
2810     } else {
2811       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2812                                        : diag::err_omp_expected_var_arg)
2813           << Id.getName();
2814       return ExprError();
2815     }
2816   } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2817     Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2818     Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2819     return ExprError();
2820   }
2821   Lookup.suppressDiagnostics();
2822 
2823   // OpenMP [2.9.2, Syntax, C/C++]
2824   //   Variables must be file-scope, namespace-scope, or static block-scope.
2825   if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
2826     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
2827         << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
2828     bool IsDecl =
2829         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2830     Diag(VD->getLocation(),
2831          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2832         << VD;
2833     return ExprError();
2834   }
2835 
2836   VarDecl *CanonicalVD = VD->getCanonicalDecl();
2837   NamedDecl *ND = CanonicalVD;
2838   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2839   //   A threadprivate directive for file-scope variables must appear outside
2840   //   any definition or declaration.
2841   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2842       !getCurLexicalContext()->isTranslationUnit()) {
2843     Diag(Id.getLoc(), diag::err_omp_var_scope)
2844         << getOpenMPDirectiveName(Kind) << VD;
2845     bool IsDecl =
2846         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2847     Diag(VD->getLocation(),
2848          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2849         << VD;
2850     return ExprError();
2851   }
2852   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2853   //   A threadprivate directive for static class member variables must appear
2854   //   in the class definition, in the same scope in which the member
2855   //   variables are declared.
2856   if (CanonicalVD->isStaticDataMember() &&
2857       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2858     Diag(Id.getLoc(), diag::err_omp_var_scope)
2859         << getOpenMPDirectiveName(Kind) << VD;
2860     bool IsDecl =
2861         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2862     Diag(VD->getLocation(),
2863          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2864         << VD;
2865     return ExprError();
2866   }
2867   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2868   //   A threadprivate directive for namespace-scope variables must appear
2869   //   outside any definition or declaration other than the namespace
2870   //   definition itself.
2871   if (CanonicalVD->getDeclContext()->isNamespace() &&
2872       (!getCurLexicalContext()->isFileContext() ||
2873        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2874     Diag(Id.getLoc(), diag::err_omp_var_scope)
2875         << getOpenMPDirectiveName(Kind) << VD;
2876     bool IsDecl =
2877         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2878     Diag(VD->getLocation(),
2879          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2880         << VD;
2881     return ExprError();
2882   }
2883   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2884   //   A threadprivate directive for static block-scope variables must appear
2885   //   in the scope of the variable and not in a nested scope.
2886   if (CanonicalVD->isLocalVarDecl() && CurScope &&
2887       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
2888     Diag(Id.getLoc(), diag::err_omp_var_scope)
2889         << getOpenMPDirectiveName(Kind) << VD;
2890     bool IsDecl =
2891         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2892     Diag(VD->getLocation(),
2893          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2894         << VD;
2895     return ExprError();
2896   }
2897 
2898   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2899   //   A threadprivate directive must lexically precede all references to any
2900   //   of the variables in its list.
2901   if (Kind == OMPD_threadprivate && VD->isUsed() &&
2902       !DSAStack->isThreadPrivate(VD)) {
2903     Diag(Id.getLoc(), diag::err_omp_var_used)
2904         << getOpenMPDirectiveName(Kind) << VD;
2905     return ExprError();
2906   }
2907 
2908   QualType ExprType = VD->getType().getNonReferenceType();
2909   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2910                              SourceLocation(), VD,
2911                              /*RefersToEnclosingVariableOrCapture=*/false,
2912                              Id.getLoc(), ExprType, VK_LValue);
2913 }
2914 
2915 Sema::DeclGroupPtrTy
2916 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2917                                         ArrayRef<Expr *> VarList) {
2918   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
2919     CurContext->addDecl(D);
2920     return DeclGroupPtrTy::make(DeclGroupRef(D));
2921   }
2922   return nullptr;
2923 }
2924 
2925 namespace {
2926 class LocalVarRefChecker final
2927     : public ConstStmtVisitor<LocalVarRefChecker, bool> {
2928   Sema &SemaRef;
2929 
2930 public:
2931   bool VisitDeclRefExpr(const DeclRefExpr *E) {
2932     if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2933       if (VD->hasLocalStorage()) {
2934         SemaRef.Diag(E->getBeginLoc(),
2935                      diag::err_omp_local_var_in_threadprivate_init)
2936             << E->getSourceRange();
2937         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2938             << VD << VD->getSourceRange();
2939         return true;
2940       }
2941     }
2942     return false;
2943   }
2944   bool VisitStmt(const Stmt *S) {
2945     for (const Stmt *Child : S->children()) {
2946       if (Child && Visit(Child))
2947         return true;
2948     }
2949     return false;
2950   }
2951   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
2952 };
2953 } // namespace
2954 
2955 OMPThreadPrivateDecl *
2956 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
2957   SmallVector<Expr *, 8> Vars;
2958   for (Expr *RefExpr : VarList) {
2959     auto *DE = cast<DeclRefExpr>(RefExpr);
2960     auto *VD = cast<VarDecl>(DE->getDecl());
2961     SourceLocation ILoc = DE->getExprLoc();
2962 
2963     // Mark variable as used.
2964     VD->setReferenced();
2965     VD->markUsed(Context);
2966 
2967     QualType QType = VD->getType();
2968     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2969       // It will be analyzed later.
2970       Vars.push_back(DE);
2971       continue;
2972     }
2973 
2974     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2975     //   A threadprivate variable must not have an incomplete type.
2976     if (RequireCompleteType(ILoc, VD->getType(),
2977                             diag::err_omp_threadprivate_incomplete_type)) {
2978       continue;
2979     }
2980 
2981     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2982     //   A threadprivate variable must not have a reference type.
2983     if (VD->getType()->isReferenceType()) {
2984       Diag(ILoc, diag::err_omp_ref_type_arg)
2985           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2986       bool IsDecl =
2987           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2988       Diag(VD->getLocation(),
2989            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2990           << VD;
2991       continue;
2992     }
2993 
2994     // Check if this is a TLS variable. If TLS is not being supported, produce
2995     // the corresponding diagnostic.
2996     if ((VD->getTLSKind() != VarDecl::TLS_None &&
2997          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2998            getLangOpts().OpenMPUseTLS &&
2999            getASTContext().getTargetInfo().isTLSSupported())) ||
3000         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3001          !VD->isLocalVarDecl())) {
3002       Diag(ILoc, diag::err_omp_var_thread_local)
3003           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
3004       bool IsDecl =
3005           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3006       Diag(VD->getLocation(),
3007            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3008           << VD;
3009       continue;
3010     }
3011 
3012     // Check if initial value of threadprivate variable reference variable with
3013     // local storage (it is not supported by runtime).
3014     if (const Expr *Init = VD->getAnyInitializer()) {
3015       LocalVarRefChecker Checker(*this);
3016       if (Checker.Visit(Init))
3017         continue;
3018     }
3019 
3020     Vars.push_back(RefExpr);
3021     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
3022     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
3023         Context, SourceRange(Loc, Loc)));
3024     if (ASTMutationListener *ML = Context.getASTMutationListener())
3025       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
3026   }
3027   OMPThreadPrivateDecl *D = nullptr;
3028   if (!Vars.empty()) {
3029     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
3030                                      Vars);
3031     D->setAccess(AS_public);
3032   }
3033   return D;
3034 }
3035 
3036 static OMPAllocateDeclAttr::AllocatorTypeTy
3037 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
3038   if (!Allocator)
3039     return OMPAllocateDeclAttr::OMPNullMemAlloc;
3040   if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3041       Allocator->isInstantiationDependent() ||
3042       Allocator->containsUnexpandedParameterPack())
3043     return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3044   auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3045   const Expr *AE = Allocator->IgnoreParenImpCasts();
3046   for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
3047     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
3048     const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
3049     llvm::FoldingSetNodeID AEId, DAEId;
3050     AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
3051     DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
3052     if (AEId == DAEId) {
3053       AllocatorKindRes = AllocatorKind;
3054       break;
3055     }
3056   }
3057   return AllocatorKindRes;
3058 }
3059 
3060 static bool checkPreviousOMPAllocateAttribute(
3061     Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
3062     OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
3063   if (!VD->hasAttr<OMPAllocateDeclAttr>())
3064     return false;
3065   const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
3066   Expr *PrevAllocator = A->getAllocator();
3067   OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
3068       getAllocatorKind(S, Stack, PrevAllocator);
3069   bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
3070   if (AllocatorsMatch &&
3071       AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
3072       Allocator && PrevAllocator) {
3073     const Expr *AE = Allocator->IgnoreParenImpCasts();
3074     const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
3075     llvm::FoldingSetNodeID AEId, PAEId;
3076     AE->Profile(AEId, S.Context, /*Canonical=*/true);
3077     PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
3078     AllocatorsMatch = AEId == PAEId;
3079   }
3080   if (!AllocatorsMatch) {
3081     SmallString<256> AllocatorBuffer;
3082     llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
3083     if (Allocator)
3084       Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
3085     SmallString<256> PrevAllocatorBuffer;
3086     llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
3087     if (PrevAllocator)
3088       PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
3089                                  S.getPrintingPolicy());
3090 
3091     SourceLocation AllocatorLoc =
3092         Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
3093     SourceRange AllocatorRange =
3094         Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
3095     SourceLocation PrevAllocatorLoc =
3096         PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
3097     SourceRange PrevAllocatorRange =
3098         PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
3099     S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
3100         << (Allocator ? 1 : 0) << AllocatorStream.str()
3101         << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
3102         << AllocatorRange;
3103     S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
3104         << PrevAllocatorRange;
3105     return true;
3106   }
3107   return false;
3108 }
3109 
3110 static void
3111 applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
3112                           OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
3113                           Expr *Allocator, Expr *Alignment, SourceRange SR) {
3114   if (VD->hasAttr<OMPAllocateDeclAttr>())
3115     return;
3116   if (Alignment &&
3117       (Alignment->isTypeDependent() || Alignment->isValueDependent() ||
3118        Alignment->isInstantiationDependent() ||
3119        Alignment->containsUnexpandedParameterPack()))
3120     // Apply later when we have a usable value.
3121     return;
3122   if (Allocator &&
3123       (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3124        Allocator->isInstantiationDependent() ||
3125        Allocator->containsUnexpandedParameterPack()))
3126     return;
3127   auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
3128                                                 Allocator, Alignment, SR);
3129   VD->addAttr(A);
3130   if (ASTMutationListener *ML = S.Context.getASTMutationListener())
3131     ML->DeclarationMarkedOpenMPAllocate(VD, A);
3132 }
3133 
3134 Sema::DeclGroupPtrTy
3135 Sema::ActOnOpenMPAllocateDirective(SourceLocation Loc, ArrayRef<Expr *> VarList,
3136                                    ArrayRef<OMPClause *> Clauses,
3137                                    DeclContext *Owner) {
3138   assert(Clauses.size() <= 2 && "Expected at most two clauses.");
3139   Expr *Alignment = nullptr;
3140   Expr *Allocator = nullptr;
3141   if (Clauses.empty()) {
3142     // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
3143     // allocate directives that appear in a target region must specify an
3144     // allocator clause unless a requires directive with the dynamic_allocators
3145     // clause is present in the same compilation unit.
3146     if (LangOpts.OpenMPIsDevice &&
3147         !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
3148       targetDiag(Loc, diag::err_expected_allocator_clause);
3149   } else {
3150     for (const OMPClause *C : Clauses)
3151       if (const auto *AC = dyn_cast<OMPAllocatorClause>(C))
3152         Allocator = AC->getAllocator();
3153       else if (const auto *AC = dyn_cast<OMPAlignClause>(C))
3154         Alignment = AC->getAlignment();
3155       else
3156         llvm_unreachable("Unexpected clause on allocate directive");
3157   }
3158   OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3159       getAllocatorKind(*this, DSAStack, Allocator);
3160   SmallVector<Expr *, 8> Vars;
3161   for (Expr *RefExpr : VarList) {
3162     auto *DE = cast<DeclRefExpr>(RefExpr);
3163     auto *VD = cast<VarDecl>(DE->getDecl());
3164 
3165     // Check if this is a TLS variable or global register.
3166     if (VD->getTLSKind() != VarDecl::TLS_None ||
3167         VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
3168         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3169          !VD->isLocalVarDecl()))
3170       continue;
3171 
3172     // If the used several times in the allocate directive, the same allocator
3173     // must be used.
3174     if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
3175                                           AllocatorKind, Allocator))
3176       continue;
3177 
3178     // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
3179     // If a list item has a static storage type, the allocator expression in the
3180     // allocator clause must be a constant expression that evaluates to one of
3181     // the predefined memory allocator values.
3182     if (Allocator && VD->hasGlobalStorage()) {
3183       if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
3184         Diag(Allocator->getExprLoc(),
3185              diag::err_omp_expected_predefined_allocator)
3186             << Allocator->getSourceRange();
3187         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3188                       VarDecl::DeclarationOnly;
3189         Diag(VD->getLocation(),
3190              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3191             << VD;
3192         continue;
3193       }
3194     }
3195 
3196     Vars.push_back(RefExpr);
3197     applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, Alignment,
3198                               DE->getSourceRange());
3199   }
3200   if (Vars.empty())
3201     return nullptr;
3202   if (!Owner)
3203     Owner = getCurLexicalContext();
3204   auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
3205   D->setAccess(AS_public);
3206   Owner->addDecl(D);
3207   return DeclGroupPtrTy::make(DeclGroupRef(D));
3208 }
3209 
3210 Sema::DeclGroupPtrTy
3211 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
3212                                    ArrayRef<OMPClause *> ClauseList) {
3213   OMPRequiresDecl *D = nullptr;
3214   if (!CurContext->isFileContext()) {
3215     Diag(Loc, diag::err_omp_invalid_scope) << "requires";
3216   } else {
3217     D = CheckOMPRequiresDecl(Loc, ClauseList);
3218     if (D) {
3219       CurContext->addDecl(D);
3220       DSAStack->addRequiresDecl(D);
3221     }
3222   }
3223   return DeclGroupPtrTy::make(DeclGroupRef(D));
3224 }
3225 
3226 void Sema::ActOnOpenMPAssumesDirective(SourceLocation Loc,
3227                                        OpenMPDirectiveKind DKind,
3228                                        ArrayRef<std::string> Assumptions,
3229                                        bool SkippedClauses) {
3230   if (!SkippedClauses && Assumptions.empty())
3231     Diag(Loc, diag::err_omp_no_clause_for_directive)
3232         << llvm::omp::getAllAssumeClauseOptions()
3233         << llvm::omp::getOpenMPDirectiveName(DKind);
3234 
3235   auto *AA = AssumptionAttr::Create(Context, llvm::join(Assumptions, ","), Loc);
3236   if (DKind == llvm::omp::Directive::OMPD_begin_assumes) {
3237     OMPAssumeScoped.push_back(AA);
3238     return;
3239   }
3240 
3241   // Global assumes without assumption clauses are ignored.
3242   if (Assumptions.empty())
3243     return;
3244 
3245   assert(DKind == llvm::omp::Directive::OMPD_assumes &&
3246          "Unexpected omp assumption directive!");
3247   OMPAssumeGlobal.push_back(AA);
3248 
3249   // The OMPAssumeGlobal scope above will take care of new declarations but
3250   // we also want to apply the assumption to existing ones, e.g., to
3251   // declarations in included headers. To this end, we traverse all existing
3252   // declaration contexts and annotate function declarations here.
3253   SmallVector<DeclContext *, 8> DeclContexts;
3254   auto *Ctx = CurContext;
3255   while (Ctx->getLexicalParent())
3256     Ctx = Ctx->getLexicalParent();
3257   DeclContexts.push_back(Ctx);
3258   while (!DeclContexts.empty()) {
3259     DeclContext *DC = DeclContexts.pop_back_val();
3260     for (auto *SubDC : DC->decls()) {
3261       if (SubDC->isInvalidDecl())
3262         continue;
3263       if (auto *CTD = dyn_cast<ClassTemplateDecl>(SubDC)) {
3264         DeclContexts.push_back(CTD->getTemplatedDecl());
3265         for (auto *S : CTD->specializations())
3266           DeclContexts.push_back(S);
3267         continue;
3268       }
3269       if (auto *DC = dyn_cast<DeclContext>(SubDC))
3270         DeclContexts.push_back(DC);
3271       if (auto *F = dyn_cast<FunctionDecl>(SubDC)) {
3272         F->addAttr(AA);
3273         continue;
3274       }
3275     }
3276   }
3277 }
3278 
3279 void Sema::ActOnOpenMPEndAssumesDirective() {
3280   assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!");
3281   OMPAssumeScoped.pop_back();
3282 }
3283 
3284 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
3285                                             ArrayRef<OMPClause *> ClauseList) {
3286   /// For target specific clauses, the requires directive cannot be
3287   /// specified after the handling of any of the target regions in the
3288   /// current compilation unit.
3289   ArrayRef<SourceLocation> TargetLocations =
3290       DSAStack->getEncounteredTargetLocs();
3291   SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc();
3292   if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) {
3293     for (const OMPClause *CNew : ClauseList) {
3294       // Check if any of the requires clauses affect target regions.
3295       if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
3296           isa<OMPUnifiedAddressClause>(CNew) ||
3297           isa<OMPReverseOffloadClause>(CNew) ||
3298           isa<OMPDynamicAllocatorsClause>(CNew)) {
3299         Diag(Loc, diag::err_omp_directive_before_requires)
3300             << "target" << getOpenMPClauseName(CNew->getClauseKind());
3301         for (SourceLocation TargetLoc : TargetLocations) {
3302           Diag(TargetLoc, diag::note_omp_requires_encountered_directive)
3303               << "target";
3304         }
3305       } else if (!AtomicLoc.isInvalid() &&
3306                  isa<OMPAtomicDefaultMemOrderClause>(CNew)) {
3307         Diag(Loc, diag::err_omp_directive_before_requires)
3308             << "atomic" << getOpenMPClauseName(CNew->getClauseKind());
3309         Diag(AtomicLoc, diag::note_omp_requires_encountered_directive)
3310             << "atomic";
3311       }
3312     }
3313   }
3314 
3315   if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
3316     return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
3317                                    ClauseList);
3318   return nullptr;
3319 }
3320 
3321 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
3322                               const ValueDecl *D,
3323                               const DSAStackTy::DSAVarData &DVar,
3324                               bool IsLoopIterVar) {
3325   if (DVar.RefExpr) {
3326     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
3327         << getOpenMPClauseName(DVar.CKind);
3328     return;
3329   }
3330   enum {
3331     PDSA_StaticMemberShared,
3332     PDSA_StaticLocalVarShared,
3333     PDSA_LoopIterVarPrivate,
3334     PDSA_LoopIterVarLinear,
3335     PDSA_LoopIterVarLastprivate,
3336     PDSA_ConstVarShared,
3337     PDSA_GlobalVarShared,
3338     PDSA_TaskVarFirstprivate,
3339     PDSA_LocalVarPrivate,
3340     PDSA_Implicit
3341   } Reason = PDSA_Implicit;
3342   bool ReportHint = false;
3343   auto ReportLoc = D->getLocation();
3344   auto *VD = dyn_cast<VarDecl>(D);
3345   if (IsLoopIterVar) {
3346     if (DVar.CKind == OMPC_private)
3347       Reason = PDSA_LoopIterVarPrivate;
3348     else if (DVar.CKind == OMPC_lastprivate)
3349       Reason = PDSA_LoopIterVarLastprivate;
3350     else
3351       Reason = PDSA_LoopIterVarLinear;
3352   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
3353              DVar.CKind == OMPC_firstprivate) {
3354     Reason = PDSA_TaskVarFirstprivate;
3355     ReportLoc = DVar.ImplicitDSALoc;
3356   } else if (VD && VD->isStaticLocal())
3357     Reason = PDSA_StaticLocalVarShared;
3358   else if (VD && VD->isStaticDataMember())
3359     Reason = PDSA_StaticMemberShared;
3360   else if (VD && VD->isFileVarDecl())
3361     Reason = PDSA_GlobalVarShared;
3362   else if (D->getType().isConstant(SemaRef.getASTContext()))
3363     Reason = PDSA_ConstVarShared;
3364   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
3365     ReportHint = true;
3366     Reason = PDSA_LocalVarPrivate;
3367   }
3368   if (Reason != PDSA_Implicit) {
3369     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
3370         << Reason << ReportHint
3371         << getOpenMPDirectiveName(Stack->getCurrentDirective());
3372   } else if (DVar.ImplicitDSALoc.isValid()) {
3373     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
3374         << getOpenMPClauseName(DVar.CKind);
3375   }
3376 }
3377 
3378 static OpenMPMapClauseKind
3379 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M,
3380                              bool IsAggregateOrDeclareTarget) {
3381   OpenMPMapClauseKind Kind = OMPC_MAP_unknown;
3382   switch (M) {
3383   case OMPC_DEFAULTMAP_MODIFIER_alloc:
3384     Kind = OMPC_MAP_alloc;
3385     break;
3386   case OMPC_DEFAULTMAP_MODIFIER_to:
3387     Kind = OMPC_MAP_to;
3388     break;
3389   case OMPC_DEFAULTMAP_MODIFIER_from:
3390     Kind = OMPC_MAP_from;
3391     break;
3392   case OMPC_DEFAULTMAP_MODIFIER_tofrom:
3393     Kind = OMPC_MAP_tofrom;
3394     break;
3395   case OMPC_DEFAULTMAP_MODIFIER_present:
3396     // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description]
3397     // If implicit-behavior is present, each variable referenced in the
3398     // construct in the category specified by variable-category is treated as if
3399     // it had been listed in a map clause with the map-type of alloc and
3400     // map-type-modifier of present.
3401     Kind = OMPC_MAP_alloc;
3402     break;
3403   case OMPC_DEFAULTMAP_MODIFIER_firstprivate:
3404   case OMPC_DEFAULTMAP_MODIFIER_last:
3405     llvm_unreachable("Unexpected defaultmap implicit behavior");
3406   case OMPC_DEFAULTMAP_MODIFIER_none:
3407   case OMPC_DEFAULTMAP_MODIFIER_default:
3408   case OMPC_DEFAULTMAP_MODIFIER_unknown:
3409     // IsAggregateOrDeclareTarget could be true if:
3410     // 1. the implicit behavior for aggregate is tofrom
3411     // 2. it's a declare target link
3412     if (IsAggregateOrDeclareTarget) {
3413       Kind = OMPC_MAP_tofrom;
3414       break;
3415     }
3416     llvm_unreachable("Unexpected defaultmap implicit behavior");
3417   }
3418   assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known");
3419   return Kind;
3420 }
3421 
3422 namespace {
3423 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
3424   DSAStackTy *Stack;
3425   Sema &SemaRef;
3426   bool ErrorFound = false;
3427   bool TryCaptureCXXThisMembers = false;
3428   CapturedStmt *CS = nullptr;
3429   const static unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1;
3430   llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
3431   llvm::SmallVector<Expr *, 4> ImplicitMap[DefaultmapKindNum][OMPC_MAP_delete];
3432   llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
3433       ImplicitMapModifier[DefaultmapKindNum];
3434   Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
3435   llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
3436 
3437   void VisitSubCaptures(OMPExecutableDirective *S) {
3438     // Check implicitly captured variables.
3439     if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
3440       return;
3441     if (S->getDirectiveKind() == OMPD_atomic ||
3442         S->getDirectiveKind() == OMPD_critical ||
3443         S->getDirectiveKind() == OMPD_section ||
3444         S->getDirectiveKind() == OMPD_master ||
3445         S->getDirectiveKind() == OMPD_masked ||
3446         isOpenMPLoopTransformationDirective(S->getDirectiveKind())) {
3447       Visit(S->getAssociatedStmt());
3448       return;
3449     }
3450     visitSubCaptures(S->getInnermostCapturedStmt());
3451     // Try to capture inner this->member references to generate correct mappings
3452     // and diagnostics.
3453     if (TryCaptureCXXThisMembers ||
3454         (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3455          llvm::any_of(S->getInnermostCapturedStmt()->captures(),
3456                       [](const CapturedStmt::Capture &C) {
3457                         return C.capturesThis();
3458                       }))) {
3459       bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers;
3460       TryCaptureCXXThisMembers = true;
3461       Visit(S->getInnermostCapturedStmt()->getCapturedStmt());
3462       TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers;
3463     }
3464     // In tasks firstprivates are not captured anymore, need to analyze them
3465     // explicitly.
3466     if (isOpenMPTaskingDirective(S->getDirectiveKind()) &&
3467         !isOpenMPTaskLoopDirective(S->getDirectiveKind())) {
3468       for (OMPClause *C : S->clauses())
3469         if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) {
3470           for (Expr *Ref : FC->varlists())
3471             Visit(Ref);
3472         }
3473     }
3474   }
3475 
3476 public:
3477   void VisitDeclRefExpr(DeclRefExpr *E) {
3478     if (TryCaptureCXXThisMembers || E->isTypeDependent() ||
3479         E->isValueDependent() || E->containsUnexpandedParameterPack() ||
3480         E->isInstantiationDependent())
3481       return;
3482     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
3483       // Check the datasharing rules for the expressions in the clauses.
3484       if (!CS || (isa<OMPCapturedExprDecl>(VD) && !CS->capturesVariable(VD) &&
3485                   !Stack->getTopDSA(VD, /*FromParent=*/false).RefExpr)) {
3486         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3487           if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
3488             Visit(CED->getInit());
3489             return;
3490           }
3491       } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD))
3492         // Do not analyze internal variables and do not enclose them into
3493         // implicit clauses.
3494         return;
3495       VD = VD->getCanonicalDecl();
3496       // Skip internally declared variables.
3497       if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) &&
3498           !Stack->isImplicitTaskFirstprivate(VD))
3499         return;
3500       // Skip allocators in uses_allocators clauses.
3501       if (Stack->isUsesAllocatorsDecl(VD).hasValue())
3502         return;
3503 
3504       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
3505       // Check if the variable has explicit DSA set and stop analysis if it so.
3506       if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
3507         return;
3508 
3509       // Skip internally declared static variables.
3510       llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
3511           OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
3512       if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) &&
3513           (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
3514            !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) &&
3515           !Stack->isImplicitTaskFirstprivate(VD))
3516         return;
3517 
3518       SourceLocation ELoc = E->getExprLoc();
3519       OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3520       // The default(none) clause requires that each variable that is referenced
3521       // in the construct, and does not have a predetermined data-sharing
3522       // attribute, must have its data-sharing attribute explicitly determined
3523       // by being listed in a data-sharing attribute clause.
3524       if (DVar.CKind == OMPC_unknown &&
3525           (Stack->getDefaultDSA() == DSA_none ||
3526            Stack->getDefaultDSA() == DSA_firstprivate) &&
3527           isImplicitOrExplicitTaskingRegion(DKind) &&
3528           VarsWithInheritedDSA.count(VD) == 0) {
3529         bool InheritedDSA = Stack->getDefaultDSA() == DSA_none;
3530         if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) {
3531           DSAStackTy::DSAVarData DVar =
3532               Stack->getImplicitDSA(VD, /*FromParent=*/false);
3533           InheritedDSA = DVar.CKind == OMPC_unknown;
3534         }
3535         if (InheritedDSA)
3536           VarsWithInheritedDSA[VD] = E;
3537         return;
3538       }
3539 
3540       // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description]
3541       // If implicit-behavior is none, each variable referenced in the
3542       // construct that does not have a predetermined data-sharing attribute
3543       // and does not appear in a to or link clause on a declare target
3544       // directive must be listed in a data-mapping attribute clause, a
3545       // data-haring attribute clause (including a data-sharing attribute
3546       // clause on a combined construct where target. is one of the
3547       // constituent constructs), or an is_device_ptr clause.
3548       OpenMPDefaultmapClauseKind ClauseKind =
3549           getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD);
3550       if (SemaRef.getLangOpts().OpenMP >= 50) {
3551         bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) ==
3552                               OMPC_DEFAULTMAP_MODIFIER_none;
3553         if (DVar.CKind == OMPC_unknown && IsModifierNone &&
3554             VarsWithInheritedDSA.count(VD) == 0 && !Res) {
3555           // Only check for data-mapping attribute and is_device_ptr here
3556           // since we have already make sure that the declaration does not
3557           // have a data-sharing attribute above
3558           if (!Stack->checkMappableExprComponentListsForDecl(
3559                   VD, /*CurrentRegionOnly=*/true,
3560                   [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef
3561                            MapExprComponents,
3562                        OpenMPClauseKind) {
3563                     auto MI = MapExprComponents.rbegin();
3564                     auto ME = MapExprComponents.rend();
3565                     return MI != ME && MI->getAssociatedDeclaration() == VD;
3566                   })) {
3567             VarsWithInheritedDSA[VD] = E;
3568             return;
3569           }
3570         }
3571       }
3572       if (SemaRef.getLangOpts().OpenMP > 50) {
3573         bool IsModifierPresent = Stack->getDefaultmapModifier(ClauseKind) ==
3574                                  OMPC_DEFAULTMAP_MODIFIER_present;
3575         if (IsModifierPresent) {
3576           if (llvm::find(ImplicitMapModifier[ClauseKind],
3577                          OMPC_MAP_MODIFIER_present) ==
3578               std::end(ImplicitMapModifier[ClauseKind])) {
3579             ImplicitMapModifier[ClauseKind].push_back(
3580                 OMPC_MAP_MODIFIER_present);
3581           }
3582         }
3583       }
3584 
3585       if (isOpenMPTargetExecutionDirective(DKind) &&
3586           !Stack->isLoopControlVariable(VD).first) {
3587         if (!Stack->checkMappableExprComponentListsForDecl(
3588                 VD, /*CurrentRegionOnly=*/true,
3589                 [this](OMPClauseMappableExprCommon::MappableExprComponentListRef
3590                            StackComponents,
3591                        OpenMPClauseKind) {
3592                   if (SemaRef.LangOpts.OpenMP >= 50)
3593                     return !StackComponents.empty();
3594                   // Variable is used if it has been marked as an array, array
3595                   // section, array shaping or the variable iself.
3596                   return StackComponents.size() == 1 ||
3597                          std::all_of(
3598                              std::next(StackComponents.rbegin()),
3599                              StackComponents.rend(),
3600                              [](const OMPClauseMappableExprCommon::
3601                                     MappableComponent &MC) {
3602                                return MC.getAssociatedDeclaration() ==
3603                                           nullptr &&
3604                                       (isa<OMPArraySectionExpr>(
3605                                            MC.getAssociatedExpression()) ||
3606                                        isa<OMPArrayShapingExpr>(
3607                                            MC.getAssociatedExpression()) ||
3608                                        isa<ArraySubscriptExpr>(
3609                                            MC.getAssociatedExpression()));
3610                              });
3611                 })) {
3612           bool IsFirstprivate = false;
3613           // By default lambdas are captured as firstprivates.
3614           if (const auto *RD =
3615                   VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
3616             IsFirstprivate = RD->isLambda();
3617           IsFirstprivate =
3618               IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res);
3619           if (IsFirstprivate) {
3620             ImplicitFirstprivate.emplace_back(E);
3621           } else {
3622             OpenMPDefaultmapClauseModifier M =
3623                 Stack->getDefaultmapModifier(ClauseKind);
3624             OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3625                 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res);
3626             ImplicitMap[ClauseKind][Kind].emplace_back(E);
3627           }
3628           return;
3629         }
3630       }
3631 
3632       // OpenMP [2.9.3.6, Restrictions, p.2]
3633       //  A list item that appears in a reduction clause of the innermost
3634       //  enclosing worksharing or parallel construct may not be accessed in an
3635       //  explicit task.
3636       DVar = Stack->hasInnermostDSA(
3637           VD,
3638           [](OpenMPClauseKind C, bool AppliedToPointee) {
3639             return C == OMPC_reduction && !AppliedToPointee;
3640           },
3641           [](OpenMPDirectiveKind K) {
3642             return isOpenMPParallelDirective(K) ||
3643                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
3644           },
3645           /*FromParent=*/true);
3646       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
3647         ErrorFound = true;
3648         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3649         reportOriginalDsa(SemaRef, Stack, VD, DVar);
3650         return;
3651       }
3652 
3653       // Define implicit data-sharing attributes for task.
3654       DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
3655       if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) ||
3656            (Stack->getDefaultDSA() == DSA_firstprivate &&
3657             DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) &&
3658           !Stack->isLoopControlVariable(VD).first) {
3659         ImplicitFirstprivate.push_back(E);
3660         return;
3661       }
3662 
3663       // Store implicitly used globals with declare target link for parent
3664       // target.
3665       if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
3666           *Res == OMPDeclareTargetDeclAttr::MT_Link) {
3667         Stack->addToParentTargetRegionLinkGlobals(E);
3668         return;
3669       }
3670     }
3671   }
3672   void VisitMemberExpr(MemberExpr *E) {
3673     if (E->isTypeDependent() || E->isValueDependent() ||
3674         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
3675       return;
3676     auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
3677     OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3678     if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) {
3679       if (!FD)
3680         return;
3681       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
3682       // Check if the variable has explicit DSA set and stop analysis if it
3683       // so.
3684       if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
3685         return;
3686 
3687       if (isOpenMPTargetExecutionDirective(DKind) &&
3688           !Stack->isLoopControlVariable(FD).first &&
3689           !Stack->checkMappableExprComponentListsForDecl(
3690               FD, /*CurrentRegionOnly=*/true,
3691               [](OMPClauseMappableExprCommon::MappableExprComponentListRef
3692                      StackComponents,
3693                  OpenMPClauseKind) {
3694                 return isa<CXXThisExpr>(
3695                     cast<MemberExpr>(
3696                         StackComponents.back().getAssociatedExpression())
3697                         ->getBase()
3698                         ->IgnoreParens());
3699               })) {
3700         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
3701         //  A bit-field cannot appear in a map clause.
3702         //
3703         if (FD->isBitField())
3704           return;
3705 
3706         // Check to see if the member expression is referencing a class that
3707         // has already been explicitly mapped
3708         if (Stack->isClassPreviouslyMapped(TE->getType()))
3709           return;
3710 
3711         OpenMPDefaultmapClauseModifier Modifier =
3712             Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate);
3713         OpenMPDefaultmapClauseKind ClauseKind =
3714             getVariableCategoryFromDecl(SemaRef.getLangOpts(), FD);
3715         OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3716             Modifier, /*IsAggregateOrDeclareTarget*/ true);
3717         ImplicitMap[ClauseKind][Kind].emplace_back(E);
3718         return;
3719       }
3720 
3721       SourceLocation ELoc = E->getExprLoc();
3722       // OpenMP [2.9.3.6, Restrictions, p.2]
3723       //  A list item that appears in a reduction clause of the innermost
3724       //  enclosing worksharing or parallel construct may not be accessed in
3725       //  an  explicit task.
3726       DVar = Stack->hasInnermostDSA(
3727           FD,
3728           [](OpenMPClauseKind C, bool AppliedToPointee) {
3729             return C == OMPC_reduction && !AppliedToPointee;
3730           },
3731           [](OpenMPDirectiveKind K) {
3732             return isOpenMPParallelDirective(K) ||
3733                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
3734           },
3735           /*FromParent=*/true);
3736       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
3737         ErrorFound = true;
3738         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3739         reportOriginalDsa(SemaRef, Stack, FD, DVar);
3740         return;
3741       }
3742 
3743       // Define implicit data-sharing attributes for task.
3744       DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
3745       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
3746           !Stack->isLoopControlVariable(FD).first) {
3747         // Check if there is a captured expression for the current field in the
3748         // region. Do not mark it as firstprivate unless there is no captured
3749         // expression.
3750         // TODO: try to make it firstprivate.
3751         if (DVar.CKind != OMPC_unknown)
3752           ImplicitFirstprivate.push_back(E);
3753       }
3754       return;
3755     }
3756     if (isOpenMPTargetExecutionDirective(DKind)) {
3757       OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
3758       if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
3759                                         Stack->getCurrentDirective(),
3760                                         /*NoDiagnose=*/true))
3761         return;
3762       const auto *VD = cast<ValueDecl>(
3763           CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
3764       if (!Stack->checkMappableExprComponentListsForDecl(
3765               VD, /*CurrentRegionOnly=*/true,
3766               [&CurComponents](
3767                   OMPClauseMappableExprCommon::MappableExprComponentListRef
3768                       StackComponents,
3769                   OpenMPClauseKind) {
3770                 auto CCI = CurComponents.rbegin();
3771                 auto CCE = CurComponents.rend();
3772                 for (const auto &SC : llvm::reverse(StackComponents)) {
3773                   // Do both expressions have the same kind?
3774                   if (CCI->getAssociatedExpression()->getStmtClass() !=
3775                       SC.getAssociatedExpression()->getStmtClass())
3776                     if (!((isa<OMPArraySectionExpr>(
3777                                SC.getAssociatedExpression()) ||
3778                            isa<OMPArrayShapingExpr>(
3779                                SC.getAssociatedExpression())) &&
3780                           isa<ArraySubscriptExpr>(
3781                               CCI->getAssociatedExpression())))
3782                       return false;
3783 
3784                   const Decl *CCD = CCI->getAssociatedDeclaration();
3785                   const Decl *SCD = SC.getAssociatedDeclaration();
3786                   CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
3787                   SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
3788                   if (SCD != CCD)
3789                     return false;
3790                   std::advance(CCI, 1);
3791                   if (CCI == CCE)
3792                     break;
3793                 }
3794                 return true;
3795               })) {
3796         Visit(E->getBase());
3797       }
3798     } else if (!TryCaptureCXXThisMembers) {
3799       Visit(E->getBase());
3800     }
3801   }
3802   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
3803     for (OMPClause *C : S->clauses()) {
3804       // Skip analysis of arguments of private clauses for task|target
3805       // directives.
3806       if (isa_and_nonnull<OMPPrivateClause>(C))
3807         continue;
3808       // Skip analysis of arguments of implicitly defined firstprivate clause
3809       // for task|target directives.
3810       // Skip analysis of arguments of implicitly defined map clause for target
3811       // directives.
3812       if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
3813                  C->isImplicit() &&
3814                  !isOpenMPTaskingDirective(Stack->getCurrentDirective()))) {
3815         for (Stmt *CC : C->children()) {
3816           if (CC)
3817             Visit(CC);
3818         }
3819       }
3820     }
3821     // Check implicitly captured variables.
3822     VisitSubCaptures(S);
3823   }
3824 
3825   void VisitOMPLoopTransformationDirective(OMPLoopTransformationDirective *S) {
3826     // Loop transformation directives do not introduce data sharing
3827     VisitStmt(S);
3828   }
3829 
3830   void VisitCallExpr(CallExpr *S) {
3831     for (Stmt *C : S->arguments()) {
3832       if (C) {
3833         // Check implicitly captured variables in the task-based directives to
3834         // check if they must be firstprivatized.
3835         Visit(C);
3836       }
3837     }
3838     if (Expr *Callee = S->getCallee())
3839       if (auto *CE = dyn_cast<MemberExpr>(Callee->IgnoreParenImpCasts()))
3840         Visit(CE->getBase());
3841   }
3842   void VisitStmt(Stmt *S) {
3843     for (Stmt *C : S->children()) {
3844       if (C) {
3845         // Check implicitly captured variables in the task-based directives to
3846         // check if they must be firstprivatized.
3847         Visit(C);
3848       }
3849     }
3850   }
3851 
3852   void visitSubCaptures(CapturedStmt *S) {
3853     for (const CapturedStmt::Capture &Cap : S->captures()) {
3854       if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
3855         continue;
3856       VarDecl *VD = Cap.getCapturedVar();
3857       // Do not try to map the variable if it or its sub-component was mapped
3858       // already.
3859       if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3860           Stack->checkMappableExprComponentListsForDecl(
3861               VD, /*CurrentRegionOnly=*/true,
3862               [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
3863                  OpenMPClauseKind) { return true; }))
3864         continue;
3865       DeclRefExpr *DRE = buildDeclRefExpr(
3866           SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
3867           Cap.getLocation(), /*RefersToCapture=*/true);
3868       Visit(DRE);
3869     }
3870   }
3871   bool isErrorFound() const { return ErrorFound; }
3872   ArrayRef<Expr *> getImplicitFirstprivate() const {
3873     return ImplicitFirstprivate;
3874   }
3875   ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind DK,
3876                                   OpenMPMapClauseKind MK) const {
3877     return ImplicitMap[DK][MK];
3878   }
3879   ArrayRef<OpenMPMapModifierKind>
3880   getImplicitMapModifier(OpenMPDefaultmapClauseKind Kind) const {
3881     return ImplicitMapModifier[Kind];
3882   }
3883   const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
3884     return VarsWithInheritedDSA;
3885   }
3886 
3887   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
3888       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
3889     // Process declare target link variables for the target directives.
3890     if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
3891       for (DeclRefExpr *E : Stack->getLinkGlobals())
3892         Visit(E);
3893     }
3894   }
3895 };
3896 } // namespace
3897 
3898 static void handleDeclareVariantConstructTrait(DSAStackTy *Stack,
3899                                                OpenMPDirectiveKind DKind,
3900                                                bool ScopeEntry) {
3901   SmallVector<llvm::omp::TraitProperty, 8> Traits;
3902   if (isOpenMPTargetExecutionDirective(DKind))
3903     Traits.emplace_back(llvm::omp::TraitProperty::construct_target_target);
3904   if (isOpenMPTeamsDirective(DKind))
3905     Traits.emplace_back(llvm::omp::TraitProperty::construct_teams_teams);
3906   if (isOpenMPParallelDirective(DKind))
3907     Traits.emplace_back(llvm::omp::TraitProperty::construct_parallel_parallel);
3908   if (isOpenMPWorksharingDirective(DKind))
3909     Traits.emplace_back(llvm::omp::TraitProperty::construct_for_for);
3910   if (isOpenMPSimdDirective(DKind))
3911     Traits.emplace_back(llvm::omp::TraitProperty::construct_simd_simd);
3912   Stack->handleConstructTrait(Traits, ScopeEntry);
3913 }
3914 
3915 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
3916   switch (DKind) {
3917   case OMPD_parallel:
3918   case OMPD_parallel_for:
3919   case OMPD_parallel_for_simd:
3920   case OMPD_parallel_sections:
3921   case OMPD_parallel_master:
3922   case OMPD_teams:
3923   case OMPD_teams_distribute:
3924   case OMPD_teams_distribute_simd: {
3925     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3926     QualType KmpInt32PtrTy =
3927         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3928     Sema::CapturedParamNameType Params[] = {
3929         std::make_pair(".global_tid.", KmpInt32PtrTy),
3930         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3931         std::make_pair(StringRef(), QualType()) // __context with shared vars
3932     };
3933     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3934                              Params);
3935     break;
3936   }
3937   case OMPD_target_teams:
3938   case OMPD_target_parallel:
3939   case OMPD_target_parallel_for:
3940   case OMPD_target_parallel_for_simd:
3941   case OMPD_target_teams_distribute:
3942   case OMPD_target_teams_distribute_simd: {
3943     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3944     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3945     QualType KmpInt32PtrTy =
3946         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3947     QualType Args[] = {VoidPtrTy};
3948     FunctionProtoType::ExtProtoInfo EPI;
3949     EPI.Variadic = true;
3950     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3951     Sema::CapturedParamNameType Params[] = {
3952         std::make_pair(".global_tid.", KmpInt32Ty),
3953         std::make_pair(".part_id.", KmpInt32PtrTy),
3954         std::make_pair(".privates.", VoidPtrTy),
3955         std::make_pair(
3956             ".copy_fn.",
3957             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3958         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3959         std::make_pair(StringRef(), QualType()) // __context with shared vars
3960     };
3961     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3962                              Params, /*OpenMPCaptureLevel=*/0);
3963     // Mark this captured region as inlined, because we don't use outlined
3964     // function directly.
3965     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3966         AlwaysInlineAttr::CreateImplicit(
3967             Context, {}, AttributeCommonInfo::AS_Keyword,
3968             AlwaysInlineAttr::Keyword_forceinline));
3969     Sema::CapturedParamNameType ParamsTarget[] = {
3970         std::make_pair(StringRef(), QualType()) // __context with shared vars
3971     };
3972     // Start a captured region for 'target' with no implicit parameters.
3973     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3974                              ParamsTarget, /*OpenMPCaptureLevel=*/1);
3975     Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
3976         std::make_pair(".global_tid.", KmpInt32PtrTy),
3977         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3978         std::make_pair(StringRef(), QualType()) // __context with shared vars
3979     };
3980     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3981     // the same implicit parameters.
3982     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3983                              ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2);
3984     break;
3985   }
3986   case OMPD_target:
3987   case OMPD_target_simd: {
3988     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3989     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3990     QualType KmpInt32PtrTy =
3991         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3992     QualType Args[] = {VoidPtrTy};
3993     FunctionProtoType::ExtProtoInfo EPI;
3994     EPI.Variadic = true;
3995     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3996     Sema::CapturedParamNameType Params[] = {
3997         std::make_pair(".global_tid.", KmpInt32Ty),
3998         std::make_pair(".part_id.", KmpInt32PtrTy),
3999         std::make_pair(".privates.", VoidPtrTy),
4000         std::make_pair(
4001             ".copy_fn.",
4002             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4003         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4004         std::make_pair(StringRef(), QualType()) // __context with shared vars
4005     };
4006     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4007                              Params, /*OpenMPCaptureLevel=*/0);
4008     // Mark this captured region as inlined, because we don't use outlined
4009     // function directly.
4010     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4011         AlwaysInlineAttr::CreateImplicit(
4012             Context, {}, AttributeCommonInfo::AS_Keyword,
4013             AlwaysInlineAttr::Keyword_forceinline));
4014     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4015                              std::make_pair(StringRef(), QualType()),
4016                              /*OpenMPCaptureLevel=*/1);
4017     break;
4018   }
4019   case OMPD_atomic:
4020   case OMPD_critical:
4021   case OMPD_section:
4022   case OMPD_master:
4023   case OMPD_masked:
4024   case OMPD_tile:
4025   case OMPD_unroll:
4026     break;
4027   case OMPD_loop:
4028     // TODO: 'loop' may require additional parameters depending on the binding.
4029     // Treat similar to OMPD_simd/OMPD_for for now.
4030   case OMPD_simd:
4031   case OMPD_for:
4032   case OMPD_for_simd:
4033   case OMPD_sections:
4034   case OMPD_single:
4035   case OMPD_taskgroup:
4036   case OMPD_distribute:
4037   case OMPD_distribute_simd:
4038   case OMPD_ordered:
4039   case OMPD_target_data:
4040   case OMPD_dispatch: {
4041     Sema::CapturedParamNameType Params[] = {
4042         std::make_pair(StringRef(), QualType()) // __context with shared vars
4043     };
4044     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4045                              Params);
4046     break;
4047   }
4048   case OMPD_task: {
4049     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4050     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4051     QualType KmpInt32PtrTy =
4052         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4053     QualType Args[] = {VoidPtrTy};
4054     FunctionProtoType::ExtProtoInfo EPI;
4055     EPI.Variadic = true;
4056     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4057     Sema::CapturedParamNameType Params[] = {
4058         std::make_pair(".global_tid.", KmpInt32Ty),
4059         std::make_pair(".part_id.", KmpInt32PtrTy),
4060         std::make_pair(".privates.", VoidPtrTy),
4061         std::make_pair(
4062             ".copy_fn.",
4063             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4064         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4065         std::make_pair(StringRef(), QualType()) // __context with shared vars
4066     };
4067     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4068                              Params);
4069     // Mark this captured region as inlined, because we don't use outlined
4070     // function directly.
4071     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4072         AlwaysInlineAttr::CreateImplicit(
4073             Context, {}, AttributeCommonInfo::AS_Keyword,
4074             AlwaysInlineAttr::Keyword_forceinline));
4075     break;
4076   }
4077   case OMPD_taskloop:
4078   case OMPD_taskloop_simd:
4079   case OMPD_master_taskloop:
4080   case OMPD_master_taskloop_simd: {
4081     QualType KmpInt32Ty =
4082         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
4083             .withConst();
4084     QualType KmpUInt64Ty =
4085         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
4086             .withConst();
4087     QualType KmpInt64Ty =
4088         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
4089             .withConst();
4090     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4091     QualType KmpInt32PtrTy =
4092         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4093     QualType Args[] = {VoidPtrTy};
4094     FunctionProtoType::ExtProtoInfo EPI;
4095     EPI.Variadic = true;
4096     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4097     Sema::CapturedParamNameType Params[] = {
4098         std::make_pair(".global_tid.", KmpInt32Ty),
4099         std::make_pair(".part_id.", KmpInt32PtrTy),
4100         std::make_pair(".privates.", VoidPtrTy),
4101         std::make_pair(
4102             ".copy_fn.",
4103             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4104         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4105         std::make_pair(".lb.", KmpUInt64Ty),
4106         std::make_pair(".ub.", KmpUInt64Ty),
4107         std::make_pair(".st.", KmpInt64Ty),
4108         std::make_pair(".liter.", KmpInt32Ty),
4109         std::make_pair(".reductions.", VoidPtrTy),
4110         std::make_pair(StringRef(), QualType()) // __context with shared vars
4111     };
4112     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4113                              Params);
4114     // Mark this captured region as inlined, because we don't use outlined
4115     // function directly.
4116     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4117         AlwaysInlineAttr::CreateImplicit(
4118             Context, {}, AttributeCommonInfo::AS_Keyword,
4119             AlwaysInlineAttr::Keyword_forceinline));
4120     break;
4121   }
4122   case OMPD_parallel_master_taskloop:
4123   case OMPD_parallel_master_taskloop_simd: {
4124     QualType KmpInt32Ty =
4125         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
4126             .withConst();
4127     QualType KmpUInt64Ty =
4128         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
4129             .withConst();
4130     QualType KmpInt64Ty =
4131         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
4132             .withConst();
4133     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4134     QualType KmpInt32PtrTy =
4135         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4136     Sema::CapturedParamNameType ParamsParallel[] = {
4137         std::make_pair(".global_tid.", KmpInt32PtrTy),
4138         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4139         std::make_pair(StringRef(), QualType()) // __context with shared vars
4140     };
4141     // Start a captured region for 'parallel'.
4142     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4143                              ParamsParallel, /*OpenMPCaptureLevel=*/0);
4144     QualType Args[] = {VoidPtrTy};
4145     FunctionProtoType::ExtProtoInfo EPI;
4146     EPI.Variadic = true;
4147     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4148     Sema::CapturedParamNameType Params[] = {
4149         std::make_pair(".global_tid.", KmpInt32Ty),
4150         std::make_pair(".part_id.", KmpInt32PtrTy),
4151         std::make_pair(".privates.", VoidPtrTy),
4152         std::make_pair(
4153             ".copy_fn.",
4154             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4155         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4156         std::make_pair(".lb.", KmpUInt64Ty),
4157         std::make_pair(".ub.", KmpUInt64Ty),
4158         std::make_pair(".st.", KmpInt64Ty),
4159         std::make_pair(".liter.", KmpInt32Ty),
4160         std::make_pair(".reductions.", VoidPtrTy),
4161         std::make_pair(StringRef(), QualType()) // __context with shared vars
4162     };
4163     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4164                              Params, /*OpenMPCaptureLevel=*/1);
4165     // Mark this captured region as inlined, because we don't use outlined
4166     // function directly.
4167     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4168         AlwaysInlineAttr::CreateImplicit(
4169             Context, {}, AttributeCommonInfo::AS_Keyword,
4170             AlwaysInlineAttr::Keyword_forceinline));
4171     break;
4172   }
4173   case OMPD_distribute_parallel_for_simd:
4174   case OMPD_distribute_parallel_for: {
4175     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4176     QualType KmpInt32PtrTy =
4177         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4178     Sema::CapturedParamNameType Params[] = {
4179         std::make_pair(".global_tid.", KmpInt32PtrTy),
4180         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4181         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4182         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4183         std::make_pair(StringRef(), QualType()) // __context with shared vars
4184     };
4185     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4186                              Params);
4187     break;
4188   }
4189   case OMPD_target_teams_distribute_parallel_for:
4190   case OMPD_target_teams_distribute_parallel_for_simd: {
4191     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4192     QualType KmpInt32PtrTy =
4193         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4194     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4195 
4196     QualType Args[] = {VoidPtrTy};
4197     FunctionProtoType::ExtProtoInfo EPI;
4198     EPI.Variadic = true;
4199     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4200     Sema::CapturedParamNameType Params[] = {
4201         std::make_pair(".global_tid.", KmpInt32Ty),
4202         std::make_pair(".part_id.", KmpInt32PtrTy),
4203         std::make_pair(".privates.", VoidPtrTy),
4204         std::make_pair(
4205             ".copy_fn.",
4206             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4207         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4208         std::make_pair(StringRef(), QualType()) // __context with shared vars
4209     };
4210     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4211                              Params, /*OpenMPCaptureLevel=*/0);
4212     // Mark this captured region as inlined, because we don't use outlined
4213     // function directly.
4214     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4215         AlwaysInlineAttr::CreateImplicit(
4216             Context, {}, AttributeCommonInfo::AS_Keyword,
4217             AlwaysInlineAttr::Keyword_forceinline));
4218     Sema::CapturedParamNameType ParamsTarget[] = {
4219         std::make_pair(StringRef(), QualType()) // __context with shared vars
4220     };
4221     // Start a captured region for 'target' with no implicit parameters.
4222     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4223                              ParamsTarget, /*OpenMPCaptureLevel=*/1);
4224 
4225     Sema::CapturedParamNameType ParamsTeams[] = {
4226         std::make_pair(".global_tid.", KmpInt32PtrTy),
4227         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4228         std::make_pair(StringRef(), QualType()) // __context with shared vars
4229     };
4230     // Start a captured region for 'target' with no implicit parameters.
4231     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4232                              ParamsTeams, /*OpenMPCaptureLevel=*/2);
4233 
4234     Sema::CapturedParamNameType ParamsParallel[] = {
4235         std::make_pair(".global_tid.", KmpInt32PtrTy),
4236         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4237         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4238         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4239         std::make_pair(StringRef(), QualType()) // __context with shared vars
4240     };
4241     // Start a captured region for 'teams' or 'parallel'.  Both regions have
4242     // the same implicit parameters.
4243     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4244                              ParamsParallel, /*OpenMPCaptureLevel=*/3);
4245     break;
4246   }
4247 
4248   case OMPD_teams_distribute_parallel_for:
4249   case OMPD_teams_distribute_parallel_for_simd: {
4250     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4251     QualType KmpInt32PtrTy =
4252         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4253 
4254     Sema::CapturedParamNameType ParamsTeams[] = {
4255         std::make_pair(".global_tid.", KmpInt32PtrTy),
4256         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4257         std::make_pair(StringRef(), QualType()) // __context with shared vars
4258     };
4259     // Start a captured region for 'target' with no implicit parameters.
4260     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4261                              ParamsTeams, /*OpenMPCaptureLevel=*/0);
4262 
4263     Sema::CapturedParamNameType ParamsParallel[] = {
4264         std::make_pair(".global_tid.", KmpInt32PtrTy),
4265         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4266         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4267         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4268         std::make_pair(StringRef(), QualType()) // __context with shared vars
4269     };
4270     // Start a captured region for 'teams' or 'parallel'.  Both regions have
4271     // the same implicit parameters.
4272     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4273                              ParamsParallel, /*OpenMPCaptureLevel=*/1);
4274     break;
4275   }
4276   case OMPD_target_update:
4277   case OMPD_target_enter_data:
4278   case OMPD_target_exit_data: {
4279     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4280     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4281     QualType KmpInt32PtrTy =
4282         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4283     QualType Args[] = {VoidPtrTy};
4284     FunctionProtoType::ExtProtoInfo EPI;
4285     EPI.Variadic = true;
4286     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4287     Sema::CapturedParamNameType Params[] = {
4288         std::make_pair(".global_tid.", KmpInt32Ty),
4289         std::make_pair(".part_id.", KmpInt32PtrTy),
4290         std::make_pair(".privates.", VoidPtrTy),
4291         std::make_pair(
4292             ".copy_fn.",
4293             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4294         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4295         std::make_pair(StringRef(), QualType()) // __context with shared vars
4296     };
4297     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4298                              Params);
4299     // Mark this captured region as inlined, because we don't use outlined
4300     // function directly.
4301     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4302         AlwaysInlineAttr::CreateImplicit(
4303             Context, {}, AttributeCommonInfo::AS_Keyword,
4304             AlwaysInlineAttr::Keyword_forceinline));
4305     break;
4306   }
4307   case OMPD_threadprivate:
4308   case OMPD_allocate:
4309   case OMPD_taskyield:
4310   case OMPD_barrier:
4311   case OMPD_taskwait:
4312   case OMPD_cancellation_point:
4313   case OMPD_cancel:
4314   case OMPD_flush:
4315   case OMPD_depobj:
4316   case OMPD_scan:
4317   case OMPD_declare_reduction:
4318   case OMPD_declare_mapper:
4319   case OMPD_declare_simd:
4320   case OMPD_declare_target:
4321   case OMPD_end_declare_target:
4322   case OMPD_requires:
4323   case OMPD_declare_variant:
4324   case OMPD_begin_declare_variant:
4325   case OMPD_end_declare_variant:
4326   case OMPD_metadirective:
4327     llvm_unreachable("OpenMP Directive is not allowed");
4328   case OMPD_unknown:
4329   default:
4330     llvm_unreachable("Unknown OpenMP directive");
4331   }
4332   DSAStack->setContext(CurContext);
4333   handleDeclareVariantConstructTrait(DSAStack, DKind, /* ScopeEntry */ true);
4334 }
4335 
4336 int Sema::getNumberOfConstructScopes(unsigned Level) const {
4337   return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
4338 }
4339 
4340 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
4341   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4342   getOpenMPCaptureRegions(CaptureRegions, DKind);
4343   return CaptureRegions.size();
4344 }
4345 
4346 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
4347                                              Expr *CaptureExpr, bool WithInit,
4348                                              bool AsExpression) {
4349   assert(CaptureExpr);
4350   ASTContext &C = S.getASTContext();
4351   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
4352   QualType Ty = Init->getType();
4353   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
4354     if (S.getLangOpts().CPlusPlus) {
4355       Ty = C.getLValueReferenceType(Ty);
4356     } else {
4357       Ty = C.getPointerType(Ty);
4358       ExprResult Res =
4359           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
4360       if (!Res.isUsable())
4361         return nullptr;
4362       Init = Res.get();
4363     }
4364     WithInit = true;
4365   }
4366   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
4367                                           CaptureExpr->getBeginLoc());
4368   if (!WithInit)
4369     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
4370   S.CurContext->addHiddenDecl(CED);
4371   Sema::TentativeAnalysisScope Trap(S);
4372   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
4373   return CED;
4374 }
4375 
4376 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
4377                                  bool WithInit) {
4378   OMPCapturedExprDecl *CD;
4379   if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
4380     CD = cast<OMPCapturedExprDecl>(VD);
4381   else
4382     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
4383                           /*AsExpression=*/false);
4384   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
4385                           CaptureExpr->getExprLoc());
4386 }
4387 
4388 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
4389   CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
4390   if (!Ref) {
4391     OMPCapturedExprDecl *CD = buildCaptureDecl(
4392         S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
4393         /*WithInit=*/true, /*AsExpression=*/true);
4394     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
4395                            CaptureExpr->getExprLoc());
4396   }
4397   ExprResult Res = Ref;
4398   if (!S.getLangOpts().CPlusPlus &&
4399       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
4400       Ref->getType()->isPointerType()) {
4401     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
4402     if (!Res.isUsable())
4403       return ExprError();
4404   }
4405   return S.DefaultLvalueConversion(Res.get());
4406 }
4407 
4408 namespace {
4409 // OpenMP directives parsed in this section are represented as a
4410 // CapturedStatement with an associated statement.  If a syntax error
4411 // is detected during the parsing of the associated statement, the
4412 // compiler must abort processing and close the CapturedStatement.
4413 //
4414 // Combined directives such as 'target parallel' have more than one
4415 // nested CapturedStatements.  This RAII ensures that we unwind out
4416 // of all the nested CapturedStatements when an error is found.
4417 class CaptureRegionUnwinderRAII {
4418 private:
4419   Sema &S;
4420   bool &ErrorFound;
4421   OpenMPDirectiveKind DKind = OMPD_unknown;
4422 
4423 public:
4424   CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
4425                             OpenMPDirectiveKind DKind)
4426       : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
4427   ~CaptureRegionUnwinderRAII() {
4428     if (ErrorFound) {
4429       int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
4430       while (--ThisCaptureLevel >= 0)
4431         S.ActOnCapturedRegionError();
4432     }
4433   }
4434 };
4435 } // namespace
4436 
4437 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
4438   // Capture variables captured by reference in lambdas for target-based
4439   // directives.
4440   if (!CurContext->isDependentContext() &&
4441       (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
4442        isOpenMPTargetDataManagementDirective(
4443            DSAStack->getCurrentDirective()))) {
4444     QualType Type = V->getType();
4445     if (const auto *RD = Type.getCanonicalType()
4446                              .getNonReferenceType()
4447                              ->getAsCXXRecordDecl()) {
4448       bool SavedForceCaptureByReferenceInTargetExecutable =
4449           DSAStack->isForceCaptureByReferenceInTargetExecutable();
4450       DSAStack->setForceCaptureByReferenceInTargetExecutable(
4451           /*V=*/true);
4452       if (RD->isLambda()) {
4453         llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
4454         FieldDecl *ThisCapture;
4455         RD->getCaptureFields(Captures, ThisCapture);
4456         for (const LambdaCapture &LC : RD->captures()) {
4457           if (LC.getCaptureKind() == LCK_ByRef) {
4458             VarDecl *VD = LC.getCapturedVar();
4459             DeclContext *VDC = VD->getDeclContext();
4460             if (!VDC->Encloses(CurContext))
4461               continue;
4462             MarkVariableReferenced(LC.getLocation(), VD);
4463           } else if (LC.getCaptureKind() == LCK_This) {
4464             QualType ThisTy = getCurrentThisType();
4465             if (!ThisTy.isNull() &&
4466                 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
4467               CheckCXXThisCapture(LC.getLocation());
4468           }
4469         }
4470       }
4471       DSAStack->setForceCaptureByReferenceInTargetExecutable(
4472           SavedForceCaptureByReferenceInTargetExecutable);
4473     }
4474   }
4475 }
4476 
4477 static bool checkOrderedOrderSpecified(Sema &S,
4478                                        const ArrayRef<OMPClause *> Clauses) {
4479   const OMPOrderedClause *Ordered = nullptr;
4480   const OMPOrderClause *Order = nullptr;
4481 
4482   for (const OMPClause *Clause : Clauses) {
4483     if (Clause->getClauseKind() == OMPC_ordered)
4484       Ordered = cast<OMPOrderedClause>(Clause);
4485     else if (Clause->getClauseKind() == OMPC_order) {
4486       Order = cast<OMPOrderClause>(Clause);
4487       if (Order->getKind() != OMPC_ORDER_concurrent)
4488         Order = nullptr;
4489     }
4490     if (Ordered && Order)
4491       break;
4492   }
4493 
4494   if (Ordered && Order) {
4495     S.Diag(Order->getKindKwLoc(),
4496            diag::err_omp_simple_clause_incompatible_with_ordered)
4497         << getOpenMPClauseName(OMPC_order)
4498         << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent)
4499         << SourceRange(Order->getBeginLoc(), Order->getEndLoc());
4500     S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param)
4501         << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc());
4502     return true;
4503   }
4504   return false;
4505 }
4506 
4507 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
4508                                       ArrayRef<OMPClause *> Clauses) {
4509   handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(),
4510                                      /* ScopeEntry */ false);
4511   if (DSAStack->getCurrentDirective() == OMPD_atomic ||
4512       DSAStack->getCurrentDirective() == OMPD_critical ||
4513       DSAStack->getCurrentDirective() == OMPD_section ||
4514       DSAStack->getCurrentDirective() == OMPD_master ||
4515       DSAStack->getCurrentDirective() == OMPD_masked)
4516     return S;
4517 
4518   bool ErrorFound = false;
4519   CaptureRegionUnwinderRAII CaptureRegionUnwinder(
4520       *this, ErrorFound, DSAStack->getCurrentDirective());
4521   if (!S.isUsable()) {
4522     ErrorFound = true;
4523     return StmtError();
4524   }
4525 
4526   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4527   getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
4528   OMPOrderedClause *OC = nullptr;
4529   OMPScheduleClause *SC = nullptr;
4530   SmallVector<const OMPLinearClause *, 4> LCs;
4531   SmallVector<const OMPClauseWithPreInit *, 4> PICs;
4532   // This is required for proper codegen.
4533   for (OMPClause *Clause : Clauses) {
4534     if (!LangOpts.OpenMPSimd &&
4535         isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
4536         Clause->getClauseKind() == OMPC_in_reduction) {
4537       // Capture taskgroup task_reduction descriptors inside the tasking regions
4538       // with the corresponding in_reduction items.
4539       auto *IRC = cast<OMPInReductionClause>(Clause);
4540       for (Expr *E : IRC->taskgroup_descriptors())
4541         if (E)
4542           MarkDeclarationsReferencedInExpr(E);
4543     }
4544     if (isOpenMPPrivate(Clause->getClauseKind()) ||
4545         Clause->getClauseKind() == OMPC_copyprivate ||
4546         (getLangOpts().OpenMPUseTLS &&
4547          getASTContext().getTargetInfo().isTLSSupported() &&
4548          Clause->getClauseKind() == OMPC_copyin)) {
4549       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
4550       // Mark all variables in private list clauses as used in inner region.
4551       for (Stmt *VarRef : Clause->children()) {
4552         if (auto *E = cast_or_null<Expr>(VarRef)) {
4553           MarkDeclarationsReferencedInExpr(E);
4554         }
4555       }
4556       DSAStack->setForceVarCapturing(/*V=*/false);
4557     } else if (isOpenMPLoopTransformationDirective(
4558                    DSAStack->getCurrentDirective())) {
4559       assert(CaptureRegions.empty() &&
4560              "No captured regions in loop transformation directives.");
4561     } else if (CaptureRegions.size() > 1 ||
4562                CaptureRegions.back() != OMPD_unknown) {
4563       if (auto *C = OMPClauseWithPreInit::get(Clause))
4564         PICs.push_back(C);
4565       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
4566         if (Expr *E = C->getPostUpdateExpr())
4567           MarkDeclarationsReferencedInExpr(E);
4568       }
4569     }
4570     if (Clause->getClauseKind() == OMPC_schedule)
4571       SC = cast<OMPScheduleClause>(Clause);
4572     else if (Clause->getClauseKind() == OMPC_ordered)
4573       OC = cast<OMPOrderedClause>(Clause);
4574     else if (Clause->getClauseKind() == OMPC_linear)
4575       LCs.push_back(cast<OMPLinearClause>(Clause));
4576   }
4577   // Capture allocator expressions if used.
4578   for (Expr *E : DSAStack->getInnerAllocators())
4579     MarkDeclarationsReferencedInExpr(E);
4580   // OpenMP, 2.7.1 Loop Construct, Restrictions
4581   // The nonmonotonic modifier cannot be specified if an ordered clause is
4582   // specified.
4583   if (SC &&
4584       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
4585        SC->getSecondScheduleModifier() ==
4586            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
4587       OC) {
4588     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
4589              ? SC->getFirstScheduleModifierLoc()
4590              : SC->getSecondScheduleModifierLoc(),
4591          diag::err_omp_simple_clause_incompatible_with_ordered)
4592         << getOpenMPClauseName(OMPC_schedule)
4593         << getOpenMPSimpleClauseTypeName(OMPC_schedule,
4594                                          OMPC_SCHEDULE_MODIFIER_nonmonotonic)
4595         << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4596     ErrorFound = true;
4597   }
4598   // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions.
4599   // If an order(concurrent) clause is present, an ordered clause may not appear
4600   // on the same directive.
4601   if (checkOrderedOrderSpecified(*this, Clauses))
4602     ErrorFound = true;
4603   if (!LCs.empty() && OC && OC->getNumForLoops()) {
4604     for (const OMPLinearClause *C : LCs) {
4605       Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
4606           << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4607     }
4608     ErrorFound = true;
4609   }
4610   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
4611       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
4612       OC->getNumForLoops()) {
4613     Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
4614         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
4615     ErrorFound = true;
4616   }
4617   if (ErrorFound) {
4618     return StmtError();
4619   }
4620   StmtResult SR = S;
4621   unsigned CompletedRegions = 0;
4622   for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
4623     // Mark all variables in private list clauses as used in inner region.
4624     // Required for proper codegen of combined directives.
4625     // TODO: add processing for other clauses.
4626     if (ThisCaptureRegion != OMPD_unknown) {
4627       for (const clang::OMPClauseWithPreInit *C : PICs) {
4628         OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
4629         // Find the particular capture region for the clause if the
4630         // directive is a combined one with multiple capture regions.
4631         // If the directive is not a combined one, the capture region
4632         // associated with the clause is OMPD_unknown and is generated
4633         // only once.
4634         if (CaptureRegion == ThisCaptureRegion ||
4635             CaptureRegion == OMPD_unknown) {
4636           if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
4637             for (Decl *D : DS->decls())
4638               MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
4639           }
4640         }
4641       }
4642     }
4643     if (ThisCaptureRegion == OMPD_target) {
4644       // Capture allocator traits in the target region. They are used implicitly
4645       // and, thus, are not captured by default.
4646       for (OMPClause *C : Clauses) {
4647         if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) {
4648           for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End;
4649                ++I) {
4650             OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I);
4651             if (Expr *E = D.AllocatorTraits)
4652               MarkDeclarationsReferencedInExpr(E);
4653           }
4654           continue;
4655         }
4656       }
4657     }
4658     if (ThisCaptureRegion == OMPD_parallel) {
4659       // Capture temp arrays for inscan reductions and locals in aligned
4660       // clauses.
4661       for (OMPClause *C : Clauses) {
4662         if (auto *RC = dyn_cast<OMPReductionClause>(C)) {
4663           if (RC->getModifier() != OMPC_REDUCTION_inscan)
4664             continue;
4665           for (Expr *E : RC->copy_array_temps())
4666             MarkDeclarationsReferencedInExpr(E);
4667         }
4668         if (auto *AC = dyn_cast<OMPAlignedClause>(C)) {
4669           for (Expr *E : AC->varlists())
4670             MarkDeclarationsReferencedInExpr(E);
4671         }
4672       }
4673     }
4674     if (++CompletedRegions == CaptureRegions.size())
4675       DSAStack->setBodyComplete();
4676     SR = ActOnCapturedRegionEnd(SR.get());
4677   }
4678   return SR;
4679 }
4680 
4681 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
4682                               OpenMPDirectiveKind CancelRegion,
4683                               SourceLocation StartLoc) {
4684   // CancelRegion is only needed for cancel and cancellation_point.
4685   if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
4686     return false;
4687 
4688   if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
4689       CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
4690     return false;
4691 
4692   SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4693       << getOpenMPDirectiveName(CancelRegion);
4694   return true;
4695 }
4696 
4697 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
4698                                   OpenMPDirectiveKind CurrentRegion,
4699                                   const DeclarationNameInfo &CurrentName,
4700                                   OpenMPDirectiveKind CancelRegion,
4701                                   OpenMPBindClauseKind BindKind,
4702                                   SourceLocation StartLoc) {
4703   if (Stack->getCurScope()) {
4704     OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
4705     OpenMPDirectiveKind OffendingRegion = ParentRegion;
4706     bool NestingProhibited = false;
4707     bool CloseNesting = true;
4708     bool OrphanSeen = false;
4709     enum {
4710       NoRecommend,
4711       ShouldBeInParallelRegion,
4712       ShouldBeInOrderedRegion,
4713       ShouldBeInTargetRegion,
4714       ShouldBeInTeamsRegion,
4715       ShouldBeInLoopSimdRegion,
4716     } Recommend = NoRecommend;
4717     if (isOpenMPSimdDirective(ParentRegion) &&
4718         ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) ||
4719          (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered &&
4720           CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic &&
4721           CurrentRegion != OMPD_scan))) {
4722       // OpenMP [2.16, Nesting of Regions]
4723       // OpenMP constructs may not be nested inside a simd region.
4724       // OpenMP [2.8.1,simd Construct, Restrictions]
4725       // An ordered construct with the simd clause is the only OpenMP
4726       // construct that can appear in the simd region.
4727       // Allowing a SIMD construct nested in another SIMD construct is an
4728       // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
4729       // message.
4730       // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions]
4731       // The only OpenMP constructs that can be encountered during execution of
4732       // a simd region are the atomic construct, the loop construct, the simd
4733       // construct and the ordered construct with the simd clause.
4734       SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
4735                                  ? diag::err_omp_prohibited_region_simd
4736                                  : diag::warn_omp_nesting_simd)
4737           << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0);
4738       return CurrentRegion != OMPD_simd;
4739     }
4740     if (ParentRegion == OMPD_atomic) {
4741       // OpenMP [2.16, Nesting of Regions]
4742       // OpenMP constructs may not be nested inside an atomic region.
4743       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
4744       return true;
4745     }
4746     if (CurrentRegion == OMPD_section) {
4747       // OpenMP [2.7.2, sections Construct, Restrictions]
4748       // Orphaned section directives are prohibited. That is, the section
4749       // directives must appear within the sections construct and must not be
4750       // encountered elsewhere in the sections region.
4751       if (ParentRegion != OMPD_sections &&
4752           ParentRegion != OMPD_parallel_sections) {
4753         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
4754             << (ParentRegion != OMPD_unknown)
4755             << getOpenMPDirectiveName(ParentRegion);
4756         return true;
4757       }
4758       return false;
4759     }
4760     // Allow some constructs (except teams and cancellation constructs) to be
4761     // orphaned (they could be used in functions, called from OpenMP regions
4762     // with the required preconditions).
4763     if (ParentRegion == OMPD_unknown &&
4764         !isOpenMPNestingTeamsDirective(CurrentRegion) &&
4765         CurrentRegion != OMPD_cancellation_point &&
4766         CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan)
4767       return false;
4768     if (CurrentRegion == OMPD_cancellation_point ||
4769         CurrentRegion == OMPD_cancel) {
4770       // OpenMP [2.16, Nesting of Regions]
4771       // A cancellation point construct for which construct-type-clause is
4772       // taskgroup must be nested inside a task construct. A cancellation
4773       // point construct for which construct-type-clause is not taskgroup must
4774       // be closely nested inside an OpenMP construct that matches the type
4775       // specified in construct-type-clause.
4776       // A cancel construct for which construct-type-clause is taskgroup must be
4777       // nested inside a task construct. A cancel construct for which
4778       // construct-type-clause is not taskgroup must be closely nested inside an
4779       // OpenMP construct that matches the type specified in
4780       // construct-type-clause.
4781       NestingProhibited =
4782           !((CancelRegion == OMPD_parallel &&
4783              (ParentRegion == OMPD_parallel ||
4784               ParentRegion == OMPD_target_parallel)) ||
4785             (CancelRegion == OMPD_for &&
4786              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
4787               ParentRegion == OMPD_target_parallel_for ||
4788               ParentRegion == OMPD_distribute_parallel_for ||
4789               ParentRegion == OMPD_teams_distribute_parallel_for ||
4790               ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
4791             (CancelRegion == OMPD_taskgroup &&
4792              (ParentRegion == OMPD_task ||
4793               (SemaRef.getLangOpts().OpenMP >= 50 &&
4794                (ParentRegion == OMPD_taskloop ||
4795                 ParentRegion == OMPD_master_taskloop ||
4796                 ParentRegion == OMPD_parallel_master_taskloop)))) ||
4797             (CancelRegion == OMPD_sections &&
4798              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
4799               ParentRegion == OMPD_parallel_sections)));
4800       OrphanSeen = ParentRegion == OMPD_unknown;
4801     } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) {
4802       // OpenMP 5.1 [2.22, Nesting of Regions]
4803       // A masked region may not be closely nested inside a worksharing, loop,
4804       // atomic, task, or taskloop region.
4805       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4806                           isOpenMPGenericLoopDirective(ParentRegion) ||
4807                           isOpenMPTaskingDirective(ParentRegion);
4808     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
4809       // OpenMP [2.16, Nesting of Regions]
4810       // A critical region may not be nested (closely or otherwise) inside a
4811       // critical region with the same name. Note that this restriction is not
4812       // sufficient to prevent deadlock.
4813       SourceLocation PreviousCriticalLoc;
4814       bool DeadLock = Stack->hasDirective(
4815           [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
4816                                               const DeclarationNameInfo &DNI,
4817                                               SourceLocation Loc) {
4818             if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
4819               PreviousCriticalLoc = Loc;
4820               return true;
4821             }
4822             return false;
4823           },
4824           false /* skip top directive */);
4825       if (DeadLock) {
4826         SemaRef.Diag(StartLoc,
4827                      diag::err_omp_prohibited_region_critical_same_name)
4828             << CurrentName.getName();
4829         if (PreviousCriticalLoc.isValid())
4830           SemaRef.Diag(PreviousCriticalLoc,
4831                        diag::note_omp_previous_critical_region);
4832         return true;
4833       }
4834     } else if (CurrentRegion == OMPD_barrier) {
4835       // OpenMP 5.1 [2.22, Nesting of Regions]
4836       // A barrier region may not be closely nested inside a worksharing, loop,
4837       // task, taskloop, critical, ordered, atomic, or masked region.
4838       NestingProhibited =
4839           isOpenMPWorksharingDirective(ParentRegion) ||
4840           isOpenMPGenericLoopDirective(ParentRegion) ||
4841           isOpenMPTaskingDirective(ParentRegion) ||
4842           ParentRegion == OMPD_master || ParentRegion == OMPD_masked ||
4843           ParentRegion == OMPD_parallel_master ||
4844           ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
4845     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
4846                !isOpenMPParallelDirective(CurrentRegion) &&
4847                !isOpenMPTeamsDirective(CurrentRegion)) {
4848       // OpenMP 5.1 [2.22, Nesting of Regions]
4849       // A loop region that binds to a parallel region or a worksharing region
4850       // may not be closely nested inside a worksharing, loop, task, taskloop,
4851       // critical, ordered, atomic, or masked region.
4852       NestingProhibited =
4853           isOpenMPWorksharingDirective(ParentRegion) ||
4854           isOpenMPGenericLoopDirective(ParentRegion) ||
4855           isOpenMPTaskingDirective(ParentRegion) ||
4856           ParentRegion == OMPD_master || ParentRegion == OMPD_masked ||
4857           ParentRegion == OMPD_parallel_master ||
4858           ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
4859       Recommend = ShouldBeInParallelRegion;
4860     } else if (CurrentRegion == OMPD_ordered) {
4861       // OpenMP [2.16, Nesting of Regions]
4862       // An ordered region may not be closely nested inside a critical,
4863       // atomic, or explicit task region.
4864       // An ordered region must be closely nested inside a loop region (or
4865       // parallel loop region) with an ordered clause.
4866       // OpenMP [2.8.1,simd Construct, Restrictions]
4867       // An ordered construct with the simd clause is the only OpenMP construct
4868       // that can appear in the simd region.
4869       NestingProhibited = ParentRegion == OMPD_critical ||
4870                           isOpenMPTaskingDirective(ParentRegion) ||
4871                           !(isOpenMPSimdDirective(ParentRegion) ||
4872                             Stack->isParentOrderedRegion());
4873       Recommend = ShouldBeInOrderedRegion;
4874     } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
4875       // OpenMP [2.16, Nesting of Regions]
4876       // If specified, a teams construct must be contained within a target
4877       // construct.
4878       NestingProhibited =
4879           (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) ||
4880           (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown &&
4881            ParentRegion != OMPD_target);
4882       OrphanSeen = ParentRegion == OMPD_unknown;
4883       Recommend = ShouldBeInTargetRegion;
4884     } else if (CurrentRegion == OMPD_scan) {
4885       // OpenMP [2.16, Nesting of Regions]
4886       // If specified, a teams construct must be contained within a target
4887       // construct.
4888       NestingProhibited =
4889           SemaRef.LangOpts.OpenMP < 50 ||
4890           (ParentRegion != OMPD_simd && ParentRegion != OMPD_for &&
4891            ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for &&
4892            ParentRegion != OMPD_parallel_for_simd);
4893       OrphanSeen = ParentRegion == OMPD_unknown;
4894       Recommend = ShouldBeInLoopSimdRegion;
4895     }
4896     if (!NestingProhibited &&
4897         !isOpenMPTargetExecutionDirective(CurrentRegion) &&
4898         !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
4899         (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
4900       // OpenMP [5.1, 2.22, Nesting of Regions]
4901       // distribute, distribute simd, distribute parallel worksharing-loop,
4902       // distribute parallel worksharing-loop SIMD, loop, parallel regions,
4903       // including any parallel regions arising from combined constructs,
4904       // omp_get_num_teams() regions, and omp_get_team_num() regions are the
4905       // only OpenMP regions that may be strictly nested inside the teams
4906       // region.
4907       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
4908                           !isOpenMPDistributeDirective(CurrentRegion) &&
4909                           CurrentRegion != OMPD_loop;
4910       Recommend = ShouldBeInParallelRegion;
4911     }
4912     if (!NestingProhibited && CurrentRegion == OMPD_loop) {
4913       // OpenMP [5.1, 2.11.7, loop Construct, Restrictions]
4914       // If the bind clause is present on the loop construct and binding is
4915       // teams then the corresponding loop region must be strictly nested inside
4916       // a teams region.
4917       NestingProhibited = BindKind == OMPC_BIND_teams &&
4918                           ParentRegion != OMPD_teams &&
4919                           ParentRegion != OMPD_target_teams;
4920       Recommend = ShouldBeInTeamsRegion;
4921     }
4922     if (!NestingProhibited &&
4923         isOpenMPNestingDistributeDirective(CurrentRegion)) {
4924       // OpenMP 4.5 [2.17 Nesting of Regions]
4925       // The region associated with the distribute construct must be strictly
4926       // nested inside a teams region
4927       NestingProhibited =
4928           (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
4929       Recommend = ShouldBeInTeamsRegion;
4930     }
4931     if (!NestingProhibited &&
4932         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
4933          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
4934       // OpenMP 4.5 [2.17 Nesting of Regions]
4935       // If a target, target update, target data, target enter data, or
4936       // target exit data construct is encountered during execution of a
4937       // target region, the behavior is unspecified.
4938       NestingProhibited = Stack->hasDirective(
4939           [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
4940                              SourceLocation) {
4941             if (isOpenMPTargetExecutionDirective(K)) {
4942               OffendingRegion = K;
4943               return true;
4944             }
4945             return false;
4946           },
4947           false /* don't skip top directive */);
4948       CloseNesting = false;
4949     }
4950     if (NestingProhibited) {
4951       if (OrphanSeen) {
4952         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
4953             << getOpenMPDirectiveName(CurrentRegion) << Recommend;
4954       } else {
4955         SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
4956             << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
4957             << Recommend << getOpenMPDirectiveName(CurrentRegion);
4958       }
4959       return true;
4960     }
4961   }
4962   return false;
4963 }
4964 
4965 struct Kind2Unsigned {
4966   using argument_type = OpenMPDirectiveKind;
4967   unsigned operator()(argument_type DK) { return unsigned(DK); }
4968 };
4969 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
4970                            ArrayRef<OMPClause *> Clauses,
4971                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
4972   bool ErrorFound = false;
4973   unsigned NamedModifiersNumber = 0;
4974   llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers;
4975   FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1);
4976   SmallVector<SourceLocation, 4> NameModifierLoc;
4977   for (const OMPClause *C : Clauses) {
4978     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
4979       // At most one if clause without a directive-name-modifier can appear on
4980       // the directive.
4981       OpenMPDirectiveKind CurNM = IC->getNameModifier();
4982       if (FoundNameModifiers[CurNM]) {
4983         S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
4984             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
4985             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
4986         ErrorFound = true;
4987       } else if (CurNM != OMPD_unknown) {
4988         NameModifierLoc.push_back(IC->getNameModifierLoc());
4989         ++NamedModifiersNumber;
4990       }
4991       FoundNameModifiers[CurNM] = IC;
4992       if (CurNM == OMPD_unknown)
4993         continue;
4994       // Check if the specified name modifier is allowed for the current
4995       // directive.
4996       // At most one if clause with the particular directive-name-modifier can
4997       // appear on the directive.
4998       if (!llvm::is_contained(AllowedNameModifiers, CurNM)) {
4999         S.Diag(IC->getNameModifierLoc(),
5000                diag::err_omp_wrong_if_directive_name_modifier)
5001             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
5002         ErrorFound = true;
5003       }
5004     }
5005   }
5006   // If any if clause on the directive includes a directive-name-modifier then
5007   // all if clauses on the directive must include a directive-name-modifier.
5008   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
5009     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
5010       S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
5011              diag::err_omp_no_more_if_clause);
5012     } else {
5013       std::string Values;
5014       std::string Sep(", ");
5015       unsigned AllowedCnt = 0;
5016       unsigned TotalAllowedNum =
5017           AllowedNameModifiers.size() - NamedModifiersNumber;
5018       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
5019            ++Cnt) {
5020         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
5021         if (!FoundNameModifiers[NM]) {
5022           Values += "'";
5023           Values += getOpenMPDirectiveName(NM);
5024           Values += "'";
5025           if (AllowedCnt + 2 == TotalAllowedNum)
5026             Values += " or ";
5027           else if (AllowedCnt + 1 != TotalAllowedNum)
5028             Values += Sep;
5029           ++AllowedCnt;
5030         }
5031       }
5032       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
5033              diag::err_omp_unnamed_if_clause)
5034           << (TotalAllowedNum > 1) << Values;
5035     }
5036     for (SourceLocation Loc : NameModifierLoc) {
5037       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
5038     }
5039     ErrorFound = true;
5040   }
5041   return ErrorFound;
5042 }
5043 
5044 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr,
5045                                                    SourceLocation &ELoc,
5046                                                    SourceRange &ERange,
5047                                                    bool AllowArraySection) {
5048   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5049       RefExpr->containsUnexpandedParameterPack())
5050     return std::make_pair(nullptr, true);
5051 
5052   // OpenMP [3.1, C/C++]
5053   //  A list item is a variable name.
5054   // OpenMP  [2.9.3.3, Restrictions, p.1]
5055   //  A variable that is part of another variable (as an array or
5056   //  structure element) cannot appear in a private clause.
5057   RefExpr = RefExpr->IgnoreParens();
5058   enum {
5059     NoArrayExpr = -1,
5060     ArraySubscript = 0,
5061     OMPArraySection = 1
5062   } IsArrayExpr = NoArrayExpr;
5063   if (AllowArraySection) {
5064     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
5065       Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
5066       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
5067         Base = TempASE->getBase()->IgnoreParenImpCasts();
5068       RefExpr = Base;
5069       IsArrayExpr = ArraySubscript;
5070     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
5071       Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
5072       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
5073         Base = TempOASE->getBase()->IgnoreParenImpCasts();
5074       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
5075         Base = TempASE->getBase()->IgnoreParenImpCasts();
5076       RefExpr = Base;
5077       IsArrayExpr = OMPArraySection;
5078     }
5079   }
5080   ELoc = RefExpr->getExprLoc();
5081   ERange = RefExpr->getSourceRange();
5082   RefExpr = RefExpr->IgnoreParenImpCasts();
5083   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5084   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
5085   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
5086       (S.getCurrentThisType().isNull() || !ME ||
5087        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
5088        !isa<FieldDecl>(ME->getMemberDecl()))) {
5089     if (IsArrayExpr != NoArrayExpr) {
5090       S.Diag(ELoc, diag::err_omp_expected_base_var_name)
5091           << IsArrayExpr << ERange;
5092     } else {
5093       S.Diag(ELoc,
5094              AllowArraySection
5095                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
5096                  : diag::err_omp_expected_var_name_member_expr)
5097           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
5098     }
5099     return std::make_pair(nullptr, false);
5100   }
5101   return std::make_pair(
5102       getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
5103 }
5104 
5105 namespace {
5106 /// Checks if the allocator is used in uses_allocators clause to be allowed in
5107 /// target regions.
5108 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> {
5109   DSAStackTy *S = nullptr;
5110 
5111 public:
5112   bool VisitDeclRefExpr(const DeclRefExpr *E) {
5113     return S->isUsesAllocatorsDecl(E->getDecl())
5114                .getValueOr(
5115                    DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
5116            DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait;
5117   }
5118   bool VisitStmt(const Stmt *S) {
5119     for (const Stmt *Child : S->children()) {
5120       if (Child && Visit(Child))
5121         return true;
5122     }
5123     return false;
5124   }
5125   explicit AllocatorChecker(DSAStackTy *S) : S(S) {}
5126 };
5127 } // namespace
5128 
5129 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
5130                                  ArrayRef<OMPClause *> Clauses) {
5131   assert(!S.CurContext->isDependentContext() &&
5132          "Expected non-dependent context.");
5133   auto AllocateRange =
5134       llvm::make_filter_range(Clauses, OMPAllocateClause::classof);
5135   llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> DeclToCopy;
5136   auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) {
5137     return isOpenMPPrivate(C->getClauseKind());
5138   });
5139   for (OMPClause *Cl : PrivateRange) {
5140     MutableArrayRef<Expr *>::iterator I, It, Et;
5141     if (Cl->getClauseKind() == OMPC_private) {
5142       auto *PC = cast<OMPPrivateClause>(Cl);
5143       I = PC->private_copies().begin();
5144       It = PC->varlist_begin();
5145       Et = PC->varlist_end();
5146     } else if (Cl->getClauseKind() == OMPC_firstprivate) {
5147       auto *PC = cast<OMPFirstprivateClause>(Cl);
5148       I = PC->private_copies().begin();
5149       It = PC->varlist_begin();
5150       Et = PC->varlist_end();
5151     } else if (Cl->getClauseKind() == OMPC_lastprivate) {
5152       auto *PC = cast<OMPLastprivateClause>(Cl);
5153       I = PC->private_copies().begin();
5154       It = PC->varlist_begin();
5155       Et = PC->varlist_end();
5156     } else if (Cl->getClauseKind() == OMPC_linear) {
5157       auto *PC = cast<OMPLinearClause>(Cl);
5158       I = PC->privates().begin();
5159       It = PC->varlist_begin();
5160       Et = PC->varlist_end();
5161     } else if (Cl->getClauseKind() == OMPC_reduction) {
5162       auto *PC = cast<OMPReductionClause>(Cl);
5163       I = PC->privates().begin();
5164       It = PC->varlist_begin();
5165       Et = PC->varlist_end();
5166     } else if (Cl->getClauseKind() == OMPC_task_reduction) {
5167       auto *PC = cast<OMPTaskReductionClause>(Cl);
5168       I = PC->privates().begin();
5169       It = PC->varlist_begin();
5170       Et = PC->varlist_end();
5171     } else if (Cl->getClauseKind() == OMPC_in_reduction) {
5172       auto *PC = cast<OMPInReductionClause>(Cl);
5173       I = PC->privates().begin();
5174       It = PC->varlist_begin();
5175       Et = PC->varlist_end();
5176     } else {
5177       llvm_unreachable("Expected private clause.");
5178     }
5179     for (Expr *E : llvm::make_range(It, Et)) {
5180       if (!*I) {
5181         ++I;
5182         continue;
5183       }
5184       SourceLocation ELoc;
5185       SourceRange ERange;
5186       Expr *SimpleRefExpr = E;
5187       auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
5188                                 /*AllowArraySection=*/true);
5189       DeclToCopy.try_emplace(Res.first,
5190                              cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()));
5191       ++I;
5192     }
5193   }
5194   for (OMPClause *C : AllocateRange) {
5195     auto *AC = cast<OMPAllocateClause>(C);
5196     if (S.getLangOpts().OpenMP >= 50 &&
5197         !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() &&
5198         isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
5199         AC->getAllocator()) {
5200       Expr *Allocator = AC->getAllocator();
5201       // OpenMP, 2.12.5 target Construct
5202       // Memory allocators that do not appear in a uses_allocators clause cannot
5203       // appear as an allocator in an allocate clause or be used in the target
5204       // region unless a requires directive with the dynamic_allocators clause
5205       // is present in the same compilation unit.
5206       AllocatorChecker Checker(Stack);
5207       if (Checker.Visit(Allocator))
5208         S.Diag(Allocator->getExprLoc(),
5209                diag::err_omp_allocator_not_in_uses_allocators)
5210             << Allocator->getSourceRange();
5211     }
5212     OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
5213         getAllocatorKind(S, Stack, AC->getAllocator());
5214     // OpenMP, 2.11.4 allocate Clause, Restrictions.
5215     // For task, taskloop or target directives, allocation requests to memory
5216     // allocators with the trait access set to thread result in unspecified
5217     // behavior.
5218     if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
5219         (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
5220          isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) {
5221       S.Diag(AC->getAllocator()->getExprLoc(),
5222              diag::warn_omp_allocate_thread_on_task_target_directive)
5223           << getOpenMPDirectiveName(Stack->getCurrentDirective());
5224     }
5225     for (Expr *E : AC->varlists()) {
5226       SourceLocation ELoc;
5227       SourceRange ERange;
5228       Expr *SimpleRefExpr = E;
5229       auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange);
5230       ValueDecl *VD = Res.first;
5231       DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false);
5232       if (!isOpenMPPrivate(Data.CKind)) {
5233         S.Diag(E->getExprLoc(),
5234                diag::err_omp_expected_private_copy_for_allocate);
5235         continue;
5236       }
5237       VarDecl *PrivateVD = DeclToCopy[VD];
5238       if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD,
5239                                             AllocatorKind, AC->getAllocator()))
5240         continue;
5241       // Placeholder until allocate clause supports align modifier.
5242       Expr *Alignment = nullptr;
5243       applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(),
5244                                 Alignment, E->getSourceRange());
5245     }
5246   }
5247 }
5248 
5249 namespace {
5250 /// Rewrite statements and expressions for Sema \p Actions CurContext.
5251 ///
5252 /// Used to wrap already parsed statements/expressions into a new CapturedStmt
5253 /// context. DeclRefExpr used inside the new context are changed to refer to the
5254 /// captured variable instead.
5255 class CaptureVars : public TreeTransform<CaptureVars> {
5256   using BaseTransform = TreeTransform<CaptureVars>;
5257 
5258 public:
5259   CaptureVars(Sema &Actions) : BaseTransform(Actions) {}
5260 
5261   bool AlwaysRebuild() { return true; }
5262 };
5263 } // namespace
5264 
5265 static VarDecl *precomputeExpr(Sema &Actions,
5266                                SmallVectorImpl<Stmt *> &BodyStmts, Expr *E,
5267                                StringRef Name) {
5268   Expr *NewE = AssertSuccess(CaptureVars(Actions).TransformExpr(E));
5269   VarDecl *NewVar = buildVarDecl(Actions, {}, NewE->getType(), Name, nullptr,
5270                                  dyn_cast<DeclRefExpr>(E->IgnoreImplicit()));
5271   auto *NewDeclStmt = cast<DeclStmt>(AssertSuccess(
5272       Actions.ActOnDeclStmt(Actions.ConvertDeclToDeclGroup(NewVar), {}, {})));
5273   Actions.AddInitializerToDecl(NewDeclStmt->getSingleDecl(), NewE, false);
5274   BodyStmts.push_back(NewDeclStmt);
5275   return NewVar;
5276 }
5277 
5278 /// Create a closure that computes the number of iterations of a loop.
5279 ///
5280 /// \param Actions   The Sema object.
5281 /// \param LogicalTy Type for the logical iteration number.
5282 /// \param Rel       Comparison operator of the loop condition.
5283 /// \param StartExpr Value of the loop counter at the first iteration.
5284 /// \param StopExpr  Expression the loop counter is compared against in the loop
5285 /// condition. \param StepExpr      Amount of increment after each iteration.
5286 ///
5287 /// \return Closure (CapturedStmt) of the distance calculation.
5288 static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy,
5289                                        BinaryOperator::Opcode Rel,
5290                                        Expr *StartExpr, Expr *StopExpr,
5291                                        Expr *StepExpr) {
5292   ASTContext &Ctx = Actions.getASTContext();
5293   TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(LogicalTy);
5294 
5295   // Captured regions currently don't support return values, we use an
5296   // out-parameter instead. All inputs are implicit captures.
5297   // TODO: Instead of capturing each DeclRefExpr occurring in
5298   // StartExpr/StopExpr/Step, these could also be passed as a value capture.
5299   QualType ResultTy = Ctx.getLValueReferenceType(LogicalTy);
5300   Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy},
5301                                           {StringRef(), QualType()}};
5302   Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params);
5303 
5304   Stmt *Body;
5305   {
5306     Sema::CompoundScopeRAII CompoundScope(Actions);
5307     CapturedDecl *CS = cast<CapturedDecl>(Actions.CurContext);
5308 
5309     // Get the LValue expression for the result.
5310     ImplicitParamDecl *DistParam = CS->getParam(0);
5311     DeclRefExpr *DistRef = Actions.BuildDeclRefExpr(
5312         DistParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr);
5313 
5314     SmallVector<Stmt *, 4> BodyStmts;
5315 
5316     // Capture all referenced variable references.
5317     // TODO: Instead of computing NewStart/NewStop/NewStep inside the
5318     // CapturedStmt, we could compute them before and capture the result, to be
5319     // used jointly with the LoopVar function.
5320     VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, StartExpr, ".start");
5321     VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, StopExpr, ".stop");
5322     VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, StepExpr, ".step");
5323     auto BuildVarRef = [&](VarDecl *VD) {
5324       return buildDeclRefExpr(Actions, VD, VD->getType(), {});
5325     };
5326 
5327     IntegerLiteral *Zero = IntegerLiteral::Create(
5328         Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 0), LogicalTy, {});
5329     Expr *Dist;
5330     if (Rel == BO_NE) {
5331       // When using a != comparison, the increment can be +1 or -1. This can be
5332       // dynamic at runtime, so we need to check for the direction.
5333       Expr *IsNegStep = AssertSuccess(
5334           Actions.BuildBinOp(nullptr, {}, BO_LT, BuildVarRef(NewStep), Zero));
5335 
5336       // Positive increment.
5337       Expr *ForwardRange = AssertSuccess(Actions.BuildBinOp(
5338           nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart)));
5339       ForwardRange = AssertSuccess(
5340           Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, ForwardRange));
5341       Expr *ForwardDist = AssertSuccess(Actions.BuildBinOp(
5342           nullptr, {}, BO_Div, ForwardRange, BuildVarRef(NewStep)));
5343 
5344       // Negative increment.
5345       Expr *BackwardRange = AssertSuccess(Actions.BuildBinOp(
5346           nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop)));
5347       BackwardRange = AssertSuccess(
5348           Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, BackwardRange));
5349       Expr *NegIncAmount = AssertSuccess(
5350           Actions.BuildUnaryOp(nullptr, {}, UO_Minus, BuildVarRef(NewStep)));
5351       Expr *BackwardDist = AssertSuccess(
5352           Actions.BuildBinOp(nullptr, {}, BO_Div, BackwardRange, NegIncAmount));
5353 
5354       // Use the appropriate case.
5355       Dist = AssertSuccess(Actions.ActOnConditionalOp(
5356           {}, {}, IsNegStep, BackwardDist, ForwardDist));
5357     } else {
5358       assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) &&
5359              "Expected one of these relational operators");
5360 
5361       // We can derive the direction from any other comparison operator. It is
5362       // non well-formed OpenMP if Step increments/decrements in the other
5363       // directions. Whether at least the first iteration passes the loop
5364       // condition.
5365       Expr *HasAnyIteration = AssertSuccess(Actions.BuildBinOp(
5366           nullptr, {}, Rel, BuildVarRef(NewStart), BuildVarRef(NewStop)));
5367 
5368       // Compute the range between first and last counter value.
5369       Expr *Range;
5370       if (Rel == BO_GE || Rel == BO_GT)
5371         Range = AssertSuccess(Actions.BuildBinOp(
5372             nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop)));
5373       else
5374         Range = AssertSuccess(Actions.BuildBinOp(
5375             nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart)));
5376 
5377       // Ensure unsigned range space.
5378       Range =
5379           AssertSuccess(Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, Range));
5380 
5381       if (Rel == BO_LE || Rel == BO_GE) {
5382         // Add one to the range if the relational operator is inclusive.
5383         Range = AssertSuccess(Actions.BuildBinOp(
5384             nullptr, {}, BO_Add, Range,
5385             Actions.ActOnIntegerConstant(SourceLocation(), 1).get()));
5386       }
5387 
5388       // Divide by the absolute step amount.
5389       Expr *Divisor = BuildVarRef(NewStep);
5390       if (Rel == BO_GE || Rel == BO_GT)
5391         Divisor =
5392             AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Minus, Divisor));
5393       Dist = AssertSuccess(
5394           Actions.BuildBinOp(nullptr, {}, BO_Div, Range, Divisor));
5395 
5396       // If there is not at least one iteration, the range contains garbage. Fix
5397       // to zero in this case.
5398       Dist = AssertSuccess(
5399           Actions.ActOnConditionalOp({}, {}, HasAnyIteration, Dist, Zero));
5400     }
5401 
5402     // Assign the result to the out-parameter.
5403     Stmt *ResultAssign = AssertSuccess(Actions.BuildBinOp(
5404         Actions.getCurScope(), {}, BO_Assign, DistRef, Dist));
5405     BodyStmts.push_back(ResultAssign);
5406 
5407     Body = AssertSuccess(Actions.ActOnCompoundStmt({}, {}, BodyStmts, false));
5408   }
5409 
5410   return cast<CapturedStmt>(
5411       AssertSuccess(Actions.ActOnCapturedRegionEnd(Body)));
5412 }
5413 
5414 /// Create a closure that computes the loop variable from the logical iteration
5415 /// number.
5416 ///
5417 /// \param Actions   The Sema object.
5418 /// \param LoopVarTy Type for the loop variable used for result value.
5419 /// \param LogicalTy Type for the logical iteration number.
5420 /// \param StartExpr Value of the loop counter at the first iteration.
5421 /// \param Step      Amount of increment after each iteration.
5422 /// \param Deref     Whether the loop variable is a dereference of the loop
5423 /// counter variable.
5424 ///
5425 /// \return Closure (CapturedStmt) of the loop value calculation.
5426 static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy,
5427                                       QualType LogicalTy,
5428                                       DeclRefExpr *StartExpr, Expr *Step,
5429                                       bool Deref) {
5430   ASTContext &Ctx = Actions.getASTContext();
5431 
5432   // Pass the result as an out-parameter. Passing as return value would require
5433   // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to
5434   // invoke a copy constructor.
5435   QualType TargetParamTy = Ctx.getLValueReferenceType(LoopVarTy);
5436   Sema::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy},
5437                                           {"Logical", LogicalTy},
5438                                           {StringRef(), QualType()}};
5439   Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params);
5440 
5441   // Capture the initial iterator which represents the LoopVar value at the
5442   // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update
5443   // it in every iteration, capture it by value before it is modified.
5444   VarDecl *StartVar = cast<VarDecl>(StartExpr->getDecl());
5445   bool Invalid = Actions.tryCaptureVariable(StartVar, {},
5446                                             Sema::TryCapture_ExplicitByVal, {});
5447   (void)Invalid;
5448   assert(!Invalid && "Expecting capture-by-value to work.");
5449 
5450   Expr *Body;
5451   {
5452     Sema::CompoundScopeRAII CompoundScope(Actions);
5453     auto *CS = cast<CapturedDecl>(Actions.CurContext);
5454 
5455     ImplicitParamDecl *TargetParam = CS->getParam(0);
5456     DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr(
5457         TargetParam, LoopVarTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr);
5458     ImplicitParamDecl *IndvarParam = CS->getParam(1);
5459     DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr(
5460         IndvarParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr);
5461 
5462     // Capture the Start expression.
5463     CaptureVars Recap(Actions);
5464     Expr *NewStart = AssertSuccess(Recap.TransformExpr(StartExpr));
5465     Expr *NewStep = AssertSuccess(Recap.TransformExpr(Step));
5466 
5467     Expr *Skip = AssertSuccess(
5468         Actions.BuildBinOp(nullptr, {}, BO_Mul, NewStep, LogicalRef));
5469     // TODO: Explicitly cast to the iterator's difference_type instead of
5470     // relying on implicit conversion.
5471     Expr *Advanced =
5472         AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, NewStart, Skip));
5473 
5474     if (Deref) {
5475       // For range-based for-loops convert the loop counter value to a concrete
5476       // loop variable value by dereferencing the iterator.
5477       Advanced =
5478           AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Deref, Advanced));
5479     }
5480 
5481     // Assign the result to the output parameter.
5482     Body = AssertSuccess(Actions.BuildBinOp(Actions.getCurScope(), {},
5483                                             BO_Assign, TargetRef, Advanced));
5484   }
5485   return cast<CapturedStmt>(
5486       AssertSuccess(Actions.ActOnCapturedRegionEnd(Body)));
5487 }
5488 
5489 StmtResult Sema::ActOnOpenMPCanonicalLoop(Stmt *AStmt) {
5490   ASTContext &Ctx = getASTContext();
5491 
5492   // Extract the common elements of ForStmt and CXXForRangeStmt:
5493   // Loop variable, repeat condition, increment
5494   Expr *Cond, *Inc;
5495   VarDecl *LIVDecl, *LUVDecl;
5496   if (auto *For = dyn_cast<ForStmt>(AStmt)) {
5497     Stmt *Init = For->getInit();
5498     if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Init)) {
5499       // For statement declares loop variable.
5500       LIVDecl = cast<VarDecl>(LCVarDeclStmt->getSingleDecl());
5501     } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Init)) {
5502       // For statement reuses variable.
5503       assert(LCAssign->getOpcode() == BO_Assign &&
5504              "init part must be a loop variable assignment");
5505       auto *CounterRef = cast<DeclRefExpr>(LCAssign->getLHS());
5506       LIVDecl = cast<VarDecl>(CounterRef->getDecl());
5507     } else
5508       llvm_unreachable("Cannot determine loop variable");
5509     LUVDecl = LIVDecl;
5510 
5511     Cond = For->getCond();
5512     Inc = For->getInc();
5513   } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(AStmt)) {
5514     DeclStmt *BeginStmt = RangeFor->getBeginStmt();
5515     LIVDecl = cast<VarDecl>(BeginStmt->getSingleDecl());
5516     LUVDecl = RangeFor->getLoopVariable();
5517 
5518     Cond = RangeFor->getCond();
5519     Inc = RangeFor->getInc();
5520   } else
5521     llvm_unreachable("unhandled kind of loop");
5522 
5523   QualType CounterTy = LIVDecl->getType();
5524   QualType LVTy = LUVDecl->getType();
5525 
5526   // Analyze the loop condition.
5527   Expr *LHS, *RHS;
5528   BinaryOperator::Opcode CondRel;
5529   Cond = Cond->IgnoreImplicit();
5530   if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Cond)) {
5531     LHS = CondBinExpr->getLHS();
5532     RHS = CondBinExpr->getRHS();
5533     CondRel = CondBinExpr->getOpcode();
5534   } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Cond)) {
5535     assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands");
5536     LHS = CondCXXOp->getArg(0);
5537     RHS = CondCXXOp->getArg(1);
5538     switch (CondCXXOp->getOperator()) {
5539     case OO_ExclaimEqual:
5540       CondRel = BO_NE;
5541       break;
5542     case OO_Less:
5543       CondRel = BO_LT;
5544       break;
5545     case OO_LessEqual:
5546       CondRel = BO_LE;
5547       break;
5548     case OO_Greater:
5549       CondRel = BO_GT;
5550       break;
5551     case OO_GreaterEqual:
5552       CondRel = BO_GE;
5553       break;
5554     default:
5555       llvm_unreachable("unexpected iterator operator");
5556     }
5557   } else
5558     llvm_unreachable("unexpected loop condition");
5559 
5560   // Normalize such that the loop counter is on the LHS.
5561   if (!isa<DeclRefExpr>(LHS->IgnoreImplicit()) ||
5562       cast<DeclRefExpr>(LHS->IgnoreImplicit())->getDecl() != LIVDecl) {
5563     std::swap(LHS, RHS);
5564     CondRel = BinaryOperator::reverseComparisonOp(CondRel);
5565   }
5566   auto *CounterRef = cast<DeclRefExpr>(LHS->IgnoreImplicit());
5567 
5568   // Decide the bit width for the logical iteration counter. By default use the
5569   // unsigned ptrdiff_t integer size (for iterators and pointers).
5570   // TODO: For iterators, use iterator::difference_type,
5571   // std::iterator_traits<>::difference_type or decltype(it - end).
5572   QualType LogicalTy = Ctx.getUnsignedPointerDiffType();
5573   if (CounterTy->isIntegerType()) {
5574     unsigned BitWidth = Ctx.getIntWidth(CounterTy);
5575     LogicalTy = Ctx.getIntTypeForBitwidth(BitWidth, false);
5576   }
5577 
5578   // Analyze the loop increment.
5579   Expr *Step;
5580   if (auto *IncUn = dyn_cast<UnaryOperator>(Inc)) {
5581     int Direction;
5582     switch (IncUn->getOpcode()) {
5583     case UO_PreInc:
5584     case UO_PostInc:
5585       Direction = 1;
5586       break;
5587     case UO_PreDec:
5588     case UO_PostDec:
5589       Direction = -1;
5590       break;
5591     default:
5592       llvm_unreachable("unhandled unary increment operator");
5593     }
5594     Step = IntegerLiteral::Create(
5595         Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction), LogicalTy, {});
5596   } else if (auto *IncBin = dyn_cast<BinaryOperator>(Inc)) {
5597     if (IncBin->getOpcode() == BO_AddAssign) {
5598       Step = IncBin->getRHS();
5599     } else if (IncBin->getOpcode() == BO_SubAssign) {
5600       Step =
5601           AssertSuccess(BuildUnaryOp(nullptr, {}, UO_Minus, IncBin->getRHS()));
5602     } else
5603       llvm_unreachable("unhandled binary increment operator");
5604   } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Inc)) {
5605     switch (CondCXXOp->getOperator()) {
5606     case OO_PlusPlus:
5607       Step = IntegerLiteral::Create(
5608           Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {});
5609       break;
5610     case OO_MinusMinus:
5611       Step = IntegerLiteral::Create(
5612           Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), -1), LogicalTy, {});
5613       break;
5614     case OO_PlusEqual:
5615       Step = CondCXXOp->getArg(1);
5616       break;
5617     case OO_MinusEqual:
5618       Step = AssertSuccess(
5619           BuildUnaryOp(nullptr, {}, UO_Minus, CondCXXOp->getArg(1)));
5620       break;
5621     default:
5622       llvm_unreachable("unhandled overloaded increment operator");
5623     }
5624   } else
5625     llvm_unreachable("unknown increment expression");
5626 
5627   CapturedStmt *DistanceFunc =
5628       buildDistanceFunc(*this, LogicalTy, CondRel, LHS, RHS, Step);
5629   CapturedStmt *LoopVarFunc = buildLoopVarFunc(
5630       *this, LVTy, LogicalTy, CounterRef, Step, isa<CXXForRangeStmt>(AStmt));
5631   DeclRefExpr *LVRef = BuildDeclRefExpr(LUVDecl, LUVDecl->getType(), VK_LValue,
5632                                         {}, nullptr, nullptr, {}, nullptr);
5633   return OMPCanonicalLoop::create(getASTContext(), AStmt, DistanceFunc,
5634                                   LoopVarFunc, LVRef);
5635 }
5636 
5637 StmtResult Sema::ActOnOpenMPLoopnest(Stmt *AStmt) {
5638   // Handle a literal loop.
5639   if (isa<ForStmt>(AStmt) || isa<CXXForRangeStmt>(AStmt))
5640     return ActOnOpenMPCanonicalLoop(AStmt);
5641 
5642   // If not a literal loop, it must be the result of a loop transformation.
5643   OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(AStmt);
5644   assert(
5645       isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) &&
5646       "Loop transformation directive expected");
5647   return LoopTransform;
5648 }
5649 
5650 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
5651                                             CXXScopeSpec &MapperIdScopeSpec,
5652                                             const DeclarationNameInfo &MapperId,
5653                                             QualType Type,
5654                                             Expr *UnresolvedMapper);
5655 
5656 /// Perform DFS through the structure/class data members trying to find
5657 /// member(s) with user-defined 'default' mapper and generate implicit map
5658 /// clauses for such members with the found 'default' mapper.
5659 static void
5660 processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack,
5661                                       SmallVectorImpl<OMPClause *> &Clauses) {
5662   // Check for the deault mapper for data members.
5663   if (S.getLangOpts().OpenMP < 50)
5664     return;
5665   SmallVector<OMPClause *, 4> ImplicitMaps;
5666   for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) {
5667     auto *C = dyn_cast<OMPMapClause>(Clauses[Cnt]);
5668     if (!C)
5669       continue;
5670     SmallVector<Expr *, 4> SubExprs;
5671     auto *MI = C->mapperlist_begin();
5672     for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End;
5673          ++I, ++MI) {
5674       // Expression is mapped using mapper - skip it.
5675       if (*MI)
5676         continue;
5677       Expr *E = *I;
5678       // Expression is dependent - skip it, build the mapper when it gets
5679       // instantiated.
5680       if (E->isTypeDependent() || E->isValueDependent() ||
5681           E->containsUnexpandedParameterPack())
5682         continue;
5683       // Array section - need to check for the mapping of the array section
5684       // element.
5685       QualType CanonType = E->getType().getCanonicalType();
5686       if (CanonType->isSpecificBuiltinType(BuiltinType::OMPArraySection)) {
5687         const auto *OASE = cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts());
5688         QualType BaseType =
5689             OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
5690         QualType ElemType;
5691         if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
5692           ElemType = ATy->getElementType();
5693         else
5694           ElemType = BaseType->getPointeeType();
5695         CanonType = ElemType;
5696       }
5697 
5698       // DFS over data members in structures/classes.
5699       SmallVector<std::pair<QualType, FieldDecl *>, 4> Types(
5700           1, {CanonType, nullptr});
5701       llvm::DenseMap<const Type *, Expr *> Visited;
5702       SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain(
5703           1, {nullptr, 1});
5704       while (!Types.empty()) {
5705         QualType BaseType;
5706         FieldDecl *CurFD;
5707         std::tie(BaseType, CurFD) = Types.pop_back_val();
5708         while (ParentChain.back().second == 0)
5709           ParentChain.pop_back();
5710         --ParentChain.back().second;
5711         if (BaseType.isNull())
5712           continue;
5713         // Only structs/classes are allowed to have mappers.
5714         const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl();
5715         if (!RD)
5716           continue;
5717         auto It = Visited.find(BaseType.getTypePtr());
5718         if (It == Visited.end()) {
5719           // Try to find the associated user-defined mapper.
5720           CXXScopeSpec MapperIdScopeSpec;
5721           DeclarationNameInfo DefaultMapperId;
5722           DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier(
5723               &S.Context.Idents.get("default")));
5724           DefaultMapperId.setLoc(E->getExprLoc());
5725           ExprResult ER = buildUserDefinedMapperRef(
5726               S, Stack->getCurScope(), MapperIdScopeSpec, DefaultMapperId,
5727               BaseType, /*UnresolvedMapper=*/nullptr);
5728           if (ER.isInvalid())
5729             continue;
5730           It = Visited.try_emplace(BaseType.getTypePtr(), ER.get()).first;
5731         }
5732         // Found default mapper.
5733         if (It->second) {
5734           auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType,
5735                                                      VK_LValue, OK_Ordinary, E);
5736           OE->setIsUnique(/*V=*/true);
5737           Expr *BaseExpr = OE;
5738           for (const auto &P : ParentChain) {
5739             if (P.first) {
5740               BaseExpr = S.BuildMemberExpr(
5741                   BaseExpr, /*IsArrow=*/false, E->getExprLoc(),
5742                   NestedNameSpecifierLoc(), SourceLocation(), P.first,
5743                   DeclAccessPair::make(P.first, P.first->getAccess()),
5744                   /*HadMultipleCandidates=*/false, DeclarationNameInfo(),
5745                   P.first->getType(), VK_LValue, OK_Ordinary);
5746               BaseExpr = S.DefaultLvalueConversion(BaseExpr).get();
5747             }
5748           }
5749           if (CurFD)
5750             BaseExpr = S.BuildMemberExpr(
5751                 BaseExpr, /*IsArrow=*/false, E->getExprLoc(),
5752                 NestedNameSpecifierLoc(), SourceLocation(), CurFD,
5753                 DeclAccessPair::make(CurFD, CurFD->getAccess()),
5754                 /*HadMultipleCandidates=*/false, DeclarationNameInfo(),
5755                 CurFD->getType(), VK_LValue, OK_Ordinary);
5756           SubExprs.push_back(BaseExpr);
5757           continue;
5758         }
5759         // Check for the "default" mapper for data members.
5760         bool FirstIter = true;
5761         for (FieldDecl *FD : RD->fields()) {
5762           if (!FD)
5763             continue;
5764           QualType FieldTy = FD->getType();
5765           if (FieldTy.isNull() ||
5766               !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType()))
5767             continue;
5768           if (FirstIter) {
5769             FirstIter = false;
5770             ParentChain.emplace_back(CurFD, 1);
5771           } else {
5772             ++ParentChain.back().second;
5773           }
5774           Types.emplace_back(FieldTy, FD);
5775         }
5776       }
5777     }
5778     if (SubExprs.empty())
5779       continue;
5780     CXXScopeSpec MapperIdScopeSpec;
5781     DeclarationNameInfo MapperId;
5782     if (OMPClause *NewClause = S.ActOnOpenMPMapClause(
5783             C->getMapTypeModifiers(), C->getMapTypeModifiersLoc(),
5784             MapperIdScopeSpec, MapperId, C->getMapType(),
5785             /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(),
5786             SubExprs, OMPVarListLocTy()))
5787       Clauses.push_back(NewClause);
5788   }
5789 }
5790 
5791 StmtResult Sema::ActOnOpenMPExecutableDirective(
5792     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
5793     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
5794     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
5795   StmtResult Res = StmtError();
5796   OpenMPBindClauseKind BindKind = OMPC_BIND_unknown;
5797   if (const OMPBindClause *BC =
5798           OMPExecutableDirective::getSingleClause<OMPBindClause>(Clauses))
5799     BindKind = BC->getBindKind();
5800   // First check CancelRegion which is then used in checkNestingOfRegions.
5801   if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
5802       checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
5803                             BindKind, StartLoc))
5804     return StmtError();
5805 
5806   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
5807   VarsWithInheritedDSAType VarsWithInheritedDSA;
5808   bool ErrorFound = false;
5809   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
5810   if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic &&
5811       Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master &&
5812       Kind != OMPD_masked && !isOpenMPLoopTransformationDirective(Kind)) {
5813     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5814 
5815     // Check default data sharing attributes for referenced variables.
5816     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
5817     int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
5818     Stmt *S = AStmt;
5819     while (--ThisCaptureLevel >= 0)
5820       S = cast<CapturedStmt>(S)->getCapturedStmt();
5821     DSAChecker.Visit(S);
5822     if (!isOpenMPTargetDataManagementDirective(Kind) &&
5823         !isOpenMPTaskingDirective(Kind)) {
5824       // Visit subcaptures to generate implicit clauses for captured vars.
5825       auto *CS = cast<CapturedStmt>(AStmt);
5826       SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
5827       getOpenMPCaptureRegions(CaptureRegions, Kind);
5828       // Ignore outer tasking regions for target directives.
5829       if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
5830         CS = cast<CapturedStmt>(CS->getCapturedStmt());
5831       DSAChecker.visitSubCaptures(CS);
5832     }
5833     if (DSAChecker.isErrorFound())
5834       return StmtError();
5835     // Generate list of implicitly defined firstprivate variables.
5836     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
5837 
5838     SmallVector<Expr *, 4> ImplicitFirstprivates(
5839         DSAChecker.getImplicitFirstprivate().begin(),
5840         DSAChecker.getImplicitFirstprivate().end());
5841     const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1;
5842     SmallVector<Expr *, 4> ImplicitMaps[DefaultmapKindNum][OMPC_MAP_delete];
5843     SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
5844         ImplicitMapModifiers[DefaultmapKindNum];
5845     SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers>
5846         ImplicitMapModifiersLoc[DefaultmapKindNum];
5847     // Get the original location of present modifier from Defaultmap clause.
5848     SourceLocation PresentModifierLocs[DefaultmapKindNum];
5849     for (OMPClause *C : Clauses) {
5850       if (auto *DMC = dyn_cast<OMPDefaultmapClause>(C))
5851         if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present)
5852           PresentModifierLocs[DMC->getDefaultmapKind()] =
5853               DMC->getDefaultmapModifierLoc();
5854     }
5855     for (unsigned VC = 0; VC < DefaultmapKindNum; ++VC) {
5856       auto Kind = static_cast<OpenMPDefaultmapClauseKind>(VC);
5857       for (unsigned I = 0; I < OMPC_MAP_delete; ++I) {
5858         ArrayRef<Expr *> ImplicitMap = DSAChecker.getImplicitMap(
5859             Kind, static_cast<OpenMPMapClauseKind>(I));
5860         ImplicitMaps[VC][I].append(ImplicitMap.begin(), ImplicitMap.end());
5861       }
5862       ArrayRef<OpenMPMapModifierKind> ImplicitModifier =
5863           DSAChecker.getImplicitMapModifier(Kind);
5864       ImplicitMapModifiers[VC].append(ImplicitModifier.begin(),
5865                                       ImplicitModifier.end());
5866       std::fill_n(std::back_inserter(ImplicitMapModifiersLoc[VC]),
5867                   ImplicitModifier.size(), PresentModifierLocs[VC]);
5868     }
5869     // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
5870     for (OMPClause *C : Clauses) {
5871       if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
5872         for (Expr *E : IRC->taskgroup_descriptors())
5873           if (E)
5874             ImplicitFirstprivates.emplace_back(E);
5875       }
5876       // OpenMP 5.0, 2.10.1 task Construct
5877       // [detach clause]... The event-handle will be considered as if it was
5878       // specified on a firstprivate clause.
5879       if (auto *DC = dyn_cast<OMPDetachClause>(C))
5880         ImplicitFirstprivates.push_back(DC->getEventHandler());
5881     }
5882     if (!ImplicitFirstprivates.empty()) {
5883       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
5884               ImplicitFirstprivates, SourceLocation(), SourceLocation(),
5885               SourceLocation())) {
5886         ClausesWithImplicit.push_back(Implicit);
5887         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
5888                      ImplicitFirstprivates.size();
5889       } else {
5890         ErrorFound = true;
5891       }
5892     }
5893     // OpenMP 5.0 [2.19.7]
5894     // If a list item appears in a reduction, lastprivate or linear
5895     // clause on a combined target construct then it is treated as
5896     // if it also appears in a map clause with a map-type of tofrom
5897     if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target &&
5898         isOpenMPTargetExecutionDirective(Kind)) {
5899       SmallVector<Expr *, 4> ImplicitExprs;
5900       for (OMPClause *C : Clauses) {
5901         if (auto *RC = dyn_cast<OMPReductionClause>(C))
5902           for (Expr *E : RC->varlists())
5903             if (!isa<DeclRefExpr>(E->IgnoreParenImpCasts()))
5904               ImplicitExprs.emplace_back(E);
5905       }
5906       if (!ImplicitExprs.empty()) {
5907         ArrayRef<Expr *> Exprs = ImplicitExprs;
5908         CXXScopeSpec MapperIdScopeSpec;
5909         DeclarationNameInfo MapperId;
5910         if (OMPClause *Implicit = ActOnOpenMPMapClause(
5911                 OMPC_MAP_MODIFIER_unknown, SourceLocation(), MapperIdScopeSpec,
5912                 MapperId, OMPC_MAP_tofrom,
5913                 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(),
5914                 Exprs, OMPVarListLocTy(), /*NoDiagnose=*/true))
5915           ClausesWithImplicit.emplace_back(Implicit);
5916       }
5917     }
5918     for (unsigned I = 0, E = DefaultmapKindNum; I < E; ++I) {
5919       int ClauseKindCnt = -1;
5920       for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps[I]) {
5921         ++ClauseKindCnt;
5922         if (ImplicitMap.empty())
5923           continue;
5924         CXXScopeSpec MapperIdScopeSpec;
5925         DeclarationNameInfo MapperId;
5926         auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt);
5927         if (OMPClause *Implicit = ActOnOpenMPMapClause(
5928                 ImplicitMapModifiers[I], ImplicitMapModifiersLoc[I],
5929                 MapperIdScopeSpec, MapperId, Kind, /*IsMapTypeImplicit=*/true,
5930                 SourceLocation(), SourceLocation(), ImplicitMap,
5931                 OMPVarListLocTy())) {
5932           ClausesWithImplicit.emplace_back(Implicit);
5933           ErrorFound |= cast<OMPMapClause>(Implicit)->varlist_size() !=
5934                         ImplicitMap.size();
5935         } else {
5936           ErrorFound = true;
5937         }
5938       }
5939     }
5940     // Build expressions for implicit maps of data members with 'default'
5941     // mappers.
5942     if (LangOpts.OpenMP >= 50)
5943       processImplicitMapsWithDefaultMappers(*this, DSAStack,
5944                                             ClausesWithImplicit);
5945   }
5946 
5947   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
5948   switch (Kind) {
5949   case OMPD_parallel:
5950     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
5951                                        EndLoc);
5952     AllowedNameModifiers.push_back(OMPD_parallel);
5953     break;
5954   case OMPD_simd:
5955     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
5956                                    VarsWithInheritedDSA);
5957     if (LangOpts.OpenMP >= 50)
5958       AllowedNameModifiers.push_back(OMPD_simd);
5959     break;
5960   case OMPD_tile:
5961     Res =
5962         ActOnOpenMPTileDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
5963     break;
5964   case OMPD_unroll:
5965     Res = ActOnOpenMPUnrollDirective(ClausesWithImplicit, AStmt, StartLoc,
5966                                      EndLoc);
5967     break;
5968   case OMPD_for:
5969     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
5970                                   VarsWithInheritedDSA);
5971     break;
5972   case OMPD_for_simd:
5973     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
5974                                       EndLoc, VarsWithInheritedDSA);
5975     if (LangOpts.OpenMP >= 50)
5976       AllowedNameModifiers.push_back(OMPD_simd);
5977     break;
5978   case OMPD_sections:
5979     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
5980                                        EndLoc);
5981     break;
5982   case OMPD_section:
5983     assert(ClausesWithImplicit.empty() &&
5984            "No clauses are allowed for 'omp section' directive");
5985     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
5986     break;
5987   case OMPD_single:
5988     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
5989                                      EndLoc);
5990     break;
5991   case OMPD_master:
5992     assert(ClausesWithImplicit.empty() &&
5993            "No clauses are allowed for 'omp master' directive");
5994     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
5995     break;
5996   case OMPD_masked:
5997     Res = ActOnOpenMPMaskedDirective(ClausesWithImplicit, AStmt, StartLoc,
5998                                      EndLoc);
5999     break;
6000   case OMPD_critical:
6001     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
6002                                        StartLoc, EndLoc);
6003     break;
6004   case OMPD_parallel_for:
6005     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
6006                                           EndLoc, VarsWithInheritedDSA);
6007     AllowedNameModifiers.push_back(OMPD_parallel);
6008     break;
6009   case OMPD_parallel_for_simd:
6010     Res = ActOnOpenMPParallelForSimdDirective(
6011         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6012     AllowedNameModifiers.push_back(OMPD_parallel);
6013     if (LangOpts.OpenMP >= 50)
6014       AllowedNameModifiers.push_back(OMPD_simd);
6015     break;
6016   case OMPD_parallel_master:
6017     Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt,
6018                                              StartLoc, EndLoc);
6019     AllowedNameModifiers.push_back(OMPD_parallel);
6020     break;
6021   case OMPD_parallel_sections:
6022     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
6023                                                StartLoc, EndLoc);
6024     AllowedNameModifiers.push_back(OMPD_parallel);
6025     break;
6026   case OMPD_task:
6027     Res =
6028         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6029     AllowedNameModifiers.push_back(OMPD_task);
6030     break;
6031   case OMPD_taskyield:
6032     assert(ClausesWithImplicit.empty() &&
6033            "No clauses are allowed for 'omp taskyield' directive");
6034     assert(AStmt == nullptr &&
6035            "No associated statement allowed for 'omp taskyield' directive");
6036     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
6037     break;
6038   case OMPD_barrier:
6039     assert(ClausesWithImplicit.empty() &&
6040            "No clauses are allowed for 'omp barrier' directive");
6041     assert(AStmt == nullptr &&
6042            "No associated statement allowed for 'omp barrier' directive");
6043     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
6044     break;
6045   case OMPD_taskwait:
6046     assert(AStmt == nullptr &&
6047            "No associated statement allowed for 'omp taskwait' directive");
6048     Res = ActOnOpenMPTaskwaitDirective(ClausesWithImplicit, StartLoc, EndLoc);
6049     break;
6050   case OMPD_taskgroup:
6051     Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
6052                                         EndLoc);
6053     break;
6054   case OMPD_flush:
6055     assert(AStmt == nullptr &&
6056            "No associated statement allowed for 'omp flush' directive");
6057     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
6058     break;
6059   case OMPD_depobj:
6060     assert(AStmt == nullptr &&
6061            "No associated statement allowed for 'omp depobj' directive");
6062     Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc);
6063     break;
6064   case OMPD_scan:
6065     assert(AStmt == nullptr &&
6066            "No associated statement allowed for 'omp scan' directive");
6067     Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc);
6068     break;
6069   case OMPD_ordered:
6070     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
6071                                       EndLoc);
6072     break;
6073   case OMPD_atomic:
6074     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
6075                                      EndLoc);
6076     break;
6077   case OMPD_teams:
6078     Res =
6079         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6080     break;
6081   case OMPD_target:
6082     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
6083                                      EndLoc);
6084     AllowedNameModifiers.push_back(OMPD_target);
6085     break;
6086   case OMPD_target_parallel:
6087     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
6088                                              StartLoc, EndLoc);
6089     AllowedNameModifiers.push_back(OMPD_target);
6090     AllowedNameModifiers.push_back(OMPD_parallel);
6091     break;
6092   case OMPD_target_parallel_for:
6093     Res = ActOnOpenMPTargetParallelForDirective(
6094         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6095     AllowedNameModifiers.push_back(OMPD_target);
6096     AllowedNameModifiers.push_back(OMPD_parallel);
6097     break;
6098   case OMPD_cancellation_point:
6099     assert(ClausesWithImplicit.empty() &&
6100            "No clauses are allowed for 'omp cancellation point' directive");
6101     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
6102                                "cancellation point' directive");
6103     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
6104     break;
6105   case OMPD_cancel:
6106     assert(AStmt == nullptr &&
6107            "No associated statement allowed for 'omp cancel' directive");
6108     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
6109                                      CancelRegion);
6110     AllowedNameModifiers.push_back(OMPD_cancel);
6111     break;
6112   case OMPD_target_data:
6113     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
6114                                          EndLoc);
6115     AllowedNameModifiers.push_back(OMPD_target_data);
6116     break;
6117   case OMPD_target_enter_data:
6118     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
6119                                               EndLoc, AStmt);
6120     AllowedNameModifiers.push_back(OMPD_target_enter_data);
6121     break;
6122   case OMPD_target_exit_data:
6123     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
6124                                              EndLoc, AStmt);
6125     AllowedNameModifiers.push_back(OMPD_target_exit_data);
6126     break;
6127   case OMPD_taskloop:
6128     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
6129                                        EndLoc, VarsWithInheritedDSA);
6130     AllowedNameModifiers.push_back(OMPD_taskloop);
6131     break;
6132   case OMPD_taskloop_simd:
6133     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
6134                                            EndLoc, VarsWithInheritedDSA);
6135     AllowedNameModifiers.push_back(OMPD_taskloop);
6136     if (LangOpts.OpenMP >= 50)
6137       AllowedNameModifiers.push_back(OMPD_simd);
6138     break;
6139   case OMPD_master_taskloop:
6140     Res = ActOnOpenMPMasterTaskLoopDirective(
6141         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6142     AllowedNameModifiers.push_back(OMPD_taskloop);
6143     break;
6144   case OMPD_master_taskloop_simd:
6145     Res = ActOnOpenMPMasterTaskLoopSimdDirective(
6146         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6147     AllowedNameModifiers.push_back(OMPD_taskloop);
6148     if (LangOpts.OpenMP >= 50)
6149       AllowedNameModifiers.push_back(OMPD_simd);
6150     break;
6151   case OMPD_parallel_master_taskloop:
6152     Res = ActOnOpenMPParallelMasterTaskLoopDirective(
6153         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6154     AllowedNameModifiers.push_back(OMPD_taskloop);
6155     AllowedNameModifiers.push_back(OMPD_parallel);
6156     break;
6157   case OMPD_parallel_master_taskloop_simd:
6158     Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective(
6159         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6160     AllowedNameModifiers.push_back(OMPD_taskloop);
6161     AllowedNameModifiers.push_back(OMPD_parallel);
6162     if (LangOpts.OpenMP >= 50)
6163       AllowedNameModifiers.push_back(OMPD_simd);
6164     break;
6165   case OMPD_distribute:
6166     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
6167                                          EndLoc, VarsWithInheritedDSA);
6168     break;
6169   case OMPD_target_update:
6170     Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
6171                                            EndLoc, AStmt);
6172     AllowedNameModifiers.push_back(OMPD_target_update);
6173     break;
6174   case OMPD_distribute_parallel_for:
6175     Res = ActOnOpenMPDistributeParallelForDirective(
6176         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6177     AllowedNameModifiers.push_back(OMPD_parallel);
6178     break;
6179   case OMPD_distribute_parallel_for_simd:
6180     Res = ActOnOpenMPDistributeParallelForSimdDirective(
6181         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6182     AllowedNameModifiers.push_back(OMPD_parallel);
6183     if (LangOpts.OpenMP >= 50)
6184       AllowedNameModifiers.push_back(OMPD_simd);
6185     break;
6186   case OMPD_distribute_simd:
6187     Res = ActOnOpenMPDistributeSimdDirective(
6188         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6189     if (LangOpts.OpenMP >= 50)
6190       AllowedNameModifiers.push_back(OMPD_simd);
6191     break;
6192   case OMPD_target_parallel_for_simd:
6193     Res = ActOnOpenMPTargetParallelForSimdDirective(
6194         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6195     AllowedNameModifiers.push_back(OMPD_target);
6196     AllowedNameModifiers.push_back(OMPD_parallel);
6197     if (LangOpts.OpenMP >= 50)
6198       AllowedNameModifiers.push_back(OMPD_simd);
6199     break;
6200   case OMPD_target_simd:
6201     Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
6202                                          EndLoc, VarsWithInheritedDSA);
6203     AllowedNameModifiers.push_back(OMPD_target);
6204     if (LangOpts.OpenMP >= 50)
6205       AllowedNameModifiers.push_back(OMPD_simd);
6206     break;
6207   case OMPD_teams_distribute:
6208     Res = ActOnOpenMPTeamsDistributeDirective(
6209         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6210     break;
6211   case OMPD_teams_distribute_simd:
6212     Res = ActOnOpenMPTeamsDistributeSimdDirective(
6213         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6214     if (LangOpts.OpenMP >= 50)
6215       AllowedNameModifiers.push_back(OMPD_simd);
6216     break;
6217   case OMPD_teams_distribute_parallel_for_simd:
6218     Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
6219         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6220     AllowedNameModifiers.push_back(OMPD_parallel);
6221     if (LangOpts.OpenMP >= 50)
6222       AllowedNameModifiers.push_back(OMPD_simd);
6223     break;
6224   case OMPD_teams_distribute_parallel_for:
6225     Res = ActOnOpenMPTeamsDistributeParallelForDirective(
6226         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6227     AllowedNameModifiers.push_back(OMPD_parallel);
6228     break;
6229   case OMPD_target_teams:
6230     Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
6231                                           EndLoc);
6232     AllowedNameModifiers.push_back(OMPD_target);
6233     break;
6234   case OMPD_target_teams_distribute:
6235     Res = ActOnOpenMPTargetTeamsDistributeDirective(
6236         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6237     AllowedNameModifiers.push_back(OMPD_target);
6238     break;
6239   case OMPD_target_teams_distribute_parallel_for:
6240     Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
6241         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6242     AllowedNameModifiers.push_back(OMPD_target);
6243     AllowedNameModifiers.push_back(OMPD_parallel);
6244     break;
6245   case OMPD_target_teams_distribute_parallel_for_simd:
6246     Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
6247         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6248     AllowedNameModifiers.push_back(OMPD_target);
6249     AllowedNameModifiers.push_back(OMPD_parallel);
6250     if (LangOpts.OpenMP >= 50)
6251       AllowedNameModifiers.push_back(OMPD_simd);
6252     break;
6253   case OMPD_target_teams_distribute_simd:
6254     Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
6255         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6256     AllowedNameModifiers.push_back(OMPD_target);
6257     if (LangOpts.OpenMP >= 50)
6258       AllowedNameModifiers.push_back(OMPD_simd);
6259     break;
6260   case OMPD_interop:
6261     assert(AStmt == nullptr &&
6262            "No associated statement allowed for 'omp interop' directive");
6263     Res = ActOnOpenMPInteropDirective(ClausesWithImplicit, StartLoc, EndLoc);
6264     break;
6265   case OMPD_dispatch:
6266     Res = ActOnOpenMPDispatchDirective(ClausesWithImplicit, AStmt, StartLoc,
6267                                        EndLoc);
6268     break;
6269   case OMPD_loop:
6270     Res = ActOnOpenMPGenericLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
6271                                           EndLoc, VarsWithInheritedDSA);
6272     break;
6273   case OMPD_declare_target:
6274   case OMPD_end_declare_target:
6275   case OMPD_threadprivate:
6276   case OMPD_allocate:
6277   case OMPD_declare_reduction:
6278   case OMPD_declare_mapper:
6279   case OMPD_declare_simd:
6280   case OMPD_requires:
6281   case OMPD_declare_variant:
6282   case OMPD_begin_declare_variant:
6283   case OMPD_end_declare_variant:
6284     llvm_unreachable("OpenMP Directive is not allowed");
6285   case OMPD_unknown:
6286   default:
6287     llvm_unreachable("Unknown OpenMP directive");
6288   }
6289 
6290   ErrorFound = Res.isInvalid() || ErrorFound;
6291 
6292   // Check variables in the clauses if default(none) or
6293   // default(firstprivate) was specified.
6294   if (DSAStack->getDefaultDSA() == DSA_none ||
6295       DSAStack->getDefaultDSA() == DSA_firstprivate) {
6296     DSAAttrChecker DSAChecker(DSAStack, *this, nullptr);
6297     for (OMPClause *C : Clauses) {
6298       switch (C->getClauseKind()) {
6299       case OMPC_num_threads:
6300       case OMPC_dist_schedule:
6301         // Do not analyse if no parent teams directive.
6302         if (isOpenMPTeamsDirective(Kind))
6303           break;
6304         continue;
6305       case OMPC_if:
6306         if (isOpenMPTeamsDirective(Kind) &&
6307             cast<OMPIfClause>(C)->getNameModifier() != OMPD_target)
6308           break;
6309         if (isOpenMPParallelDirective(Kind) &&
6310             isOpenMPTaskLoopDirective(Kind) &&
6311             cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel)
6312           break;
6313         continue;
6314       case OMPC_schedule:
6315       case OMPC_detach:
6316         break;
6317       case OMPC_grainsize:
6318       case OMPC_num_tasks:
6319       case OMPC_final:
6320       case OMPC_priority:
6321       case OMPC_novariants:
6322       case OMPC_nocontext:
6323         // Do not analyze if no parent parallel directive.
6324         if (isOpenMPParallelDirective(Kind))
6325           break;
6326         continue;
6327       case OMPC_ordered:
6328       case OMPC_device:
6329       case OMPC_num_teams:
6330       case OMPC_thread_limit:
6331       case OMPC_hint:
6332       case OMPC_collapse:
6333       case OMPC_safelen:
6334       case OMPC_simdlen:
6335       case OMPC_sizes:
6336       case OMPC_default:
6337       case OMPC_proc_bind:
6338       case OMPC_private:
6339       case OMPC_firstprivate:
6340       case OMPC_lastprivate:
6341       case OMPC_shared:
6342       case OMPC_reduction:
6343       case OMPC_task_reduction:
6344       case OMPC_in_reduction:
6345       case OMPC_linear:
6346       case OMPC_aligned:
6347       case OMPC_copyin:
6348       case OMPC_copyprivate:
6349       case OMPC_nowait:
6350       case OMPC_untied:
6351       case OMPC_mergeable:
6352       case OMPC_allocate:
6353       case OMPC_read:
6354       case OMPC_write:
6355       case OMPC_update:
6356       case OMPC_capture:
6357       case OMPC_compare:
6358       case OMPC_seq_cst:
6359       case OMPC_acq_rel:
6360       case OMPC_acquire:
6361       case OMPC_release:
6362       case OMPC_relaxed:
6363       case OMPC_depend:
6364       case OMPC_threads:
6365       case OMPC_simd:
6366       case OMPC_map:
6367       case OMPC_nogroup:
6368       case OMPC_defaultmap:
6369       case OMPC_to:
6370       case OMPC_from:
6371       case OMPC_use_device_ptr:
6372       case OMPC_use_device_addr:
6373       case OMPC_is_device_ptr:
6374       case OMPC_nontemporal:
6375       case OMPC_order:
6376       case OMPC_destroy:
6377       case OMPC_inclusive:
6378       case OMPC_exclusive:
6379       case OMPC_uses_allocators:
6380       case OMPC_affinity:
6381       case OMPC_bind:
6382         continue;
6383       case OMPC_allocator:
6384       case OMPC_flush:
6385       case OMPC_depobj:
6386       case OMPC_threadprivate:
6387       case OMPC_uniform:
6388       case OMPC_unknown:
6389       case OMPC_unified_address:
6390       case OMPC_unified_shared_memory:
6391       case OMPC_reverse_offload:
6392       case OMPC_dynamic_allocators:
6393       case OMPC_atomic_default_mem_order:
6394       case OMPC_device_type:
6395       case OMPC_match:
6396       case OMPC_when:
6397       default:
6398         llvm_unreachable("Unexpected clause");
6399       }
6400       for (Stmt *CC : C->children()) {
6401         if (CC)
6402           DSAChecker.Visit(CC);
6403       }
6404     }
6405     for (const auto &P : DSAChecker.getVarsWithInheritedDSA())
6406       VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
6407   }
6408   for (const auto &P : VarsWithInheritedDSA) {
6409     if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst()))
6410       continue;
6411     ErrorFound = true;
6412     if (DSAStack->getDefaultDSA() == DSA_none ||
6413         DSAStack->getDefaultDSA() == DSA_firstprivate) {
6414       Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
6415           << P.first << P.second->getSourceRange();
6416       Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none);
6417     } else if (getLangOpts().OpenMP >= 50) {
6418       Diag(P.second->getExprLoc(),
6419            diag::err_omp_defaultmap_no_attr_for_variable)
6420           << P.first << P.second->getSourceRange();
6421       Diag(DSAStack->getDefaultDSALocation(),
6422            diag::note_omp_defaultmap_attr_none);
6423     }
6424   }
6425 
6426   if (!AllowedNameModifiers.empty())
6427     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
6428                  ErrorFound;
6429 
6430   if (ErrorFound)
6431     return StmtError();
6432 
6433   if (!CurContext->isDependentContext() &&
6434       isOpenMPTargetExecutionDirective(Kind) &&
6435       !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
6436         DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
6437         DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
6438         DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
6439     // Register target to DSA Stack.
6440     DSAStack->addTargetDirLocation(StartLoc);
6441   }
6442 
6443   return Res;
6444 }
6445 
6446 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
6447     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
6448     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
6449     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
6450     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
6451   assert(Aligneds.size() == Alignments.size());
6452   assert(Linears.size() == LinModifiers.size());
6453   assert(Linears.size() == Steps.size());
6454   if (!DG || DG.get().isNull())
6455     return DeclGroupPtrTy();
6456 
6457   const int SimdId = 0;
6458   if (!DG.get().isSingleDecl()) {
6459     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
6460         << SimdId;
6461     return DG;
6462   }
6463   Decl *ADecl = DG.get().getSingleDecl();
6464   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
6465     ADecl = FTD->getTemplatedDecl();
6466 
6467   auto *FD = dyn_cast<FunctionDecl>(ADecl);
6468   if (!FD) {
6469     Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId;
6470     return DeclGroupPtrTy();
6471   }
6472 
6473   // OpenMP [2.8.2, declare simd construct, Description]
6474   // The parameter of the simdlen clause must be a constant positive integer
6475   // expression.
6476   ExprResult SL;
6477   if (Simdlen)
6478     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
6479   // OpenMP [2.8.2, declare simd construct, Description]
6480   // The special this pointer can be used as if was one of the arguments to the
6481   // function in any of the linear, aligned, or uniform clauses.
6482   // The uniform clause declares one or more arguments to have an invariant
6483   // value for all concurrent invocations of the function in the execution of a
6484   // single SIMD loop.
6485   llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
6486   const Expr *UniformedLinearThis = nullptr;
6487   for (const Expr *E : Uniforms) {
6488     E = E->IgnoreParenImpCasts();
6489     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
6490       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
6491         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6492             FD->getParamDecl(PVD->getFunctionScopeIndex())
6493                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
6494           UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
6495           continue;
6496         }
6497     if (isa<CXXThisExpr>(E)) {
6498       UniformedLinearThis = E;
6499       continue;
6500     }
6501     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
6502         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
6503   }
6504   // OpenMP [2.8.2, declare simd construct, Description]
6505   // The aligned clause declares that the object to which each list item points
6506   // is aligned to the number of bytes expressed in the optional parameter of
6507   // the aligned clause.
6508   // The special this pointer can be used as if was one of the arguments to the
6509   // function in any of the linear, aligned, or uniform clauses.
6510   // The type of list items appearing in the aligned clause must be array,
6511   // pointer, reference to array, or reference to pointer.
6512   llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
6513   const Expr *AlignedThis = nullptr;
6514   for (const Expr *E : Aligneds) {
6515     E = E->IgnoreParenImpCasts();
6516     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
6517       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
6518         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
6519         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6520             FD->getParamDecl(PVD->getFunctionScopeIndex())
6521                     ->getCanonicalDecl() == CanonPVD) {
6522           // OpenMP  [2.8.1, simd construct, Restrictions]
6523           // A list-item cannot appear in more than one aligned clause.
6524           if (AlignedArgs.count(CanonPVD) > 0) {
6525             Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice)
6526                 << 1 << getOpenMPClauseName(OMPC_aligned)
6527                 << E->getSourceRange();
6528             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
6529                  diag::note_omp_explicit_dsa)
6530                 << getOpenMPClauseName(OMPC_aligned);
6531             continue;
6532           }
6533           AlignedArgs[CanonPVD] = E;
6534           QualType QTy = PVD->getType()
6535                              .getNonReferenceType()
6536                              .getUnqualifiedType()
6537                              .getCanonicalType();
6538           const Type *Ty = QTy.getTypePtrOrNull();
6539           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
6540             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
6541                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
6542             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
6543           }
6544           continue;
6545         }
6546       }
6547     if (isa<CXXThisExpr>(E)) {
6548       if (AlignedThis) {
6549         Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice)
6550             << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange();
6551         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
6552             << getOpenMPClauseName(OMPC_aligned);
6553       }
6554       AlignedThis = E;
6555       continue;
6556     }
6557     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
6558         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
6559   }
6560   // The optional parameter of the aligned clause, alignment, must be a constant
6561   // positive integer expression. If no optional parameter is specified,
6562   // implementation-defined default alignments for SIMD instructions on the
6563   // target platforms are assumed.
6564   SmallVector<const Expr *, 4> NewAligns;
6565   for (Expr *E : Alignments) {
6566     ExprResult Align;
6567     if (E)
6568       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
6569     NewAligns.push_back(Align.get());
6570   }
6571   // OpenMP [2.8.2, declare simd construct, Description]
6572   // The linear clause declares one or more list items to be private to a SIMD
6573   // lane and to have a linear relationship with respect to the iteration space
6574   // of a loop.
6575   // The special this pointer can be used as if was one of the arguments to the
6576   // function in any of the linear, aligned, or uniform clauses.
6577   // When a linear-step expression is specified in a linear clause it must be
6578   // either a constant integer expression or an integer-typed parameter that is
6579   // specified in a uniform clause on the directive.
6580   llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
6581   const bool IsUniformedThis = UniformedLinearThis != nullptr;
6582   auto MI = LinModifiers.begin();
6583   for (const Expr *E : Linears) {
6584     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
6585     ++MI;
6586     E = E->IgnoreParenImpCasts();
6587     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
6588       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
6589         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
6590         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6591             FD->getParamDecl(PVD->getFunctionScopeIndex())
6592                     ->getCanonicalDecl() == CanonPVD) {
6593           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
6594           // A list-item cannot appear in more than one linear clause.
6595           if (LinearArgs.count(CanonPVD) > 0) {
6596             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
6597                 << getOpenMPClauseName(OMPC_linear)
6598                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
6599             Diag(LinearArgs[CanonPVD]->getExprLoc(),
6600                  diag::note_omp_explicit_dsa)
6601                 << getOpenMPClauseName(OMPC_linear);
6602             continue;
6603           }
6604           // Each argument can appear in at most one uniform or linear clause.
6605           if (UniformedArgs.count(CanonPVD) > 0) {
6606             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
6607                 << getOpenMPClauseName(OMPC_linear)
6608                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
6609             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
6610                  diag::note_omp_explicit_dsa)
6611                 << getOpenMPClauseName(OMPC_uniform);
6612             continue;
6613           }
6614           LinearArgs[CanonPVD] = E;
6615           if (E->isValueDependent() || E->isTypeDependent() ||
6616               E->isInstantiationDependent() ||
6617               E->containsUnexpandedParameterPack())
6618             continue;
6619           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
6620                                       PVD->getOriginalType(),
6621                                       /*IsDeclareSimd=*/true);
6622           continue;
6623         }
6624       }
6625     if (isa<CXXThisExpr>(E)) {
6626       if (UniformedLinearThis) {
6627         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
6628             << getOpenMPClauseName(OMPC_linear)
6629             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
6630             << E->getSourceRange();
6631         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
6632             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
6633                                                    : OMPC_linear);
6634         continue;
6635       }
6636       UniformedLinearThis = E;
6637       if (E->isValueDependent() || E->isTypeDependent() ||
6638           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
6639         continue;
6640       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
6641                                   E->getType(), /*IsDeclareSimd=*/true);
6642       continue;
6643     }
6644     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
6645         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
6646   }
6647   Expr *Step = nullptr;
6648   Expr *NewStep = nullptr;
6649   SmallVector<Expr *, 4> NewSteps;
6650   for (Expr *E : Steps) {
6651     // Skip the same step expression, it was checked already.
6652     if (Step == E || !E) {
6653       NewSteps.push_back(E ? NewStep : nullptr);
6654       continue;
6655     }
6656     Step = E;
6657     if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
6658       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
6659         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
6660         if (UniformedArgs.count(CanonPVD) == 0) {
6661           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
6662               << Step->getSourceRange();
6663         } else if (E->isValueDependent() || E->isTypeDependent() ||
6664                    E->isInstantiationDependent() ||
6665                    E->containsUnexpandedParameterPack() ||
6666                    CanonPVD->getType()->hasIntegerRepresentation()) {
6667           NewSteps.push_back(Step);
6668         } else {
6669           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
6670               << Step->getSourceRange();
6671         }
6672         continue;
6673       }
6674     NewStep = Step;
6675     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6676         !Step->isInstantiationDependent() &&
6677         !Step->containsUnexpandedParameterPack()) {
6678       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
6679                     .get();
6680       if (NewStep)
6681         NewStep =
6682             VerifyIntegerConstantExpression(NewStep, /*FIXME*/ AllowFold).get();
6683     }
6684     NewSteps.push_back(NewStep);
6685   }
6686   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
6687       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
6688       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
6689       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
6690       const_cast<Expr **>(Linears.data()), Linears.size(),
6691       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
6692       NewSteps.data(), NewSteps.size(), SR);
6693   ADecl->addAttr(NewAttr);
6694   return DG;
6695 }
6696 
6697 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto,
6698                          QualType NewType) {
6699   assert(NewType->isFunctionProtoType() &&
6700          "Expected function type with prototype.");
6701   assert(FD->getType()->isFunctionNoProtoType() &&
6702          "Expected function with type with no prototype.");
6703   assert(FDWithProto->getType()->isFunctionProtoType() &&
6704          "Expected function with prototype.");
6705   // Synthesize parameters with the same types.
6706   FD->setType(NewType);
6707   SmallVector<ParmVarDecl *, 16> Params;
6708   for (const ParmVarDecl *P : FDWithProto->parameters()) {
6709     auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(),
6710                                       SourceLocation(), nullptr, P->getType(),
6711                                       /*TInfo=*/nullptr, SC_None, nullptr);
6712     Param->setScopeInfo(0, Params.size());
6713     Param->setImplicit();
6714     Params.push_back(Param);
6715   }
6716 
6717   FD->setParams(Params);
6718 }
6719 
6720 void Sema::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) {
6721   if (D->isInvalidDecl())
6722     return;
6723   FunctionDecl *FD = nullptr;
6724   if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D))
6725     FD = UTemplDecl->getTemplatedDecl();
6726   else
6727     FD = cast<FunctionDecl>(D);
6728   assert(FD && "Expected a function declaration!");
6729 
6730   // If we are instantiating templates we do *not* apply scoped assumptions but
6731   // only global ones. We apply scoped assumption to the template definition
6732   // though.
6733   if (!inTemplateInstantiation()) {
6734     for (AssumptionAttr *AA : OMPAssumeScoped)
6735       FD->addAttr(AA);
6736   }
6737   for (AssumptionAttr *AA : OMPAssumeGlobal)
6738     FD->addAttr(AA);
6739 }
6740 
6741 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI)
6742     : TI(&TI), NameSuffix(TI.getMangledName()) {}
6743 
6744 void Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(
6745     Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists,
6746     SmallVectorImpl<FunctionDecl *> &Bases) {
6747   if (!D.getIdentifier())
6748     return;
6749 
6750   OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
6751 
6752   // Template specialization is an extension, check if we do it.
6753   bool IsTemplated = !TemplateParamLists.empty();
6754   if (IsTemplated &
6755       !DVScope.TI->isExtensionActive(
6756           llvm::omp::TraitProperty::implementation_extension_allow_templates))
6757     return;
6758 
6759   IdentifierInfo *BaseII = D.getIdentifier();
6760   LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(),
6761                       LookupOrdinaryName);
6762   LookupParsedName(Lookup, S, &D.getCXXScopeSpec());
6763 
6764   TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
6765   QualType FType = TInfo->getType();
6766 
6767   bool IsConstexpr =
6768       D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr;
6769   bool IsConsteval =
6770       D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval;
6771 
6772   for (auto *Candidate : Lookup) {
6773     auto *CandidateDecl = Candidate->getUnderlyingDecl();
6774     FunctionDecl *UDecl = nullptr;
6775     if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) {
6776       auto *FTD = cast<FunctionTemplateDecl>(CandidateDecl);
6777       if (FTD->getTemplateParameters()->size() == TemplateParamLists.size())
6778         UDecl = FTD->getTemplatedDecl();
6779     } else if (!IsTemplated)
6780       UDecl = dyn_cast<FunctionDecl>(CandidateDecl);
6781     if (!UDecl)
6782       continue;
6783 
6784     // Don't specialize constexpr/consteval functions with
6785     // non-constexpr/consteval functions.
6786     if (UDecl->isConstexpr() && !IsConstexpr)
6787       continue;
6788     if (UDecl->isConsteval() && !IsConsteval)
6789       continue;
6790 
6791     QualType UDeclTy = UDecl->getType();
6792     if (!UDeclTy->isDependentType()) {
6793       QualType NewType = Context.mergeFunctionTypes(
6794           FType, UDeclTy, /* OfBlockPointer */ false,
6795           /* Unqualified */ false, /* AllowCXX */ true);
6796       if (NewType.isNull())
6797         continue;
6798     }
6799 
6800     // Found a base!
6801     Bases.push_back(UDecl);
6802   }
6803 
6804   bool UseImplicitBase = !DVScope.TI->isExtensionActive(
6805       llvm::omp::TraitProperty::implementation_extension_disable_implicit_base);
6806   // If no base was found we create a declaration that we use as base.
6807   if (Bases.empty() && UseImplicitBase) {
6808     D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration);
6809     Decl *BaseD = HandleDeclarator(S, D, TemplateParamLists);
6810     BaseD->setImplicit(true);
6811     if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(BaseD))
6812       Bases.push_back(BaseTemplD->getTemplatedDecl());
6813     else
6814       Bases.push_back(cast<FunctionDecl>(BaseD));
6815   }
6816 
6817   std::string MangledName;
6818   MangledName += D.getIdentifier()->getName();
6819   MangledName += getOpenMPVariantManglingSeparatorStr();
6820   MangledName += DVScope.NameSuffix;
6821   IdentifierInfo &VariantII = Context.Idents.get(MangledName);
6822 
6823   VariantII.setMangledOpenMPVariantName(true);
6824   D.SetIdentifier(&VariantII, D.getBeginLoc());
6825 }
6826 
6827 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope(
6828     Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) {
6829   // Do not mark function as is used to prevent its emission if this is the
6830   // only place where it is used.
6831   EnterExpressionEvaluationContext Unevaluated(
6832       *this, Sema::ExpressionEvaluationContext::Unevaluated);
6833 
6834   FunctionDecl *FD = nullptr;
6835   if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D))
6836     FD = UTemplDecl->getTemplatedDecl();
6837   else
6838     FD = cast<FunctionDecl>(D);
6839   auto *VariantFuncRef = DeclRefExpr::Create(
6840       Context, NestedNameSpecifierLoc(), SourceLocation(), FD,
6841       /* RefersToEnclosingVariableOrCapture */ false,
6842       /* NameLoc */ FD->getLocation(), FD->getType(),
6843       ExprValueKind::VK_PRValue);
6844 
6845   OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
6846   auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit(
6847       Context, VariantFuncRef, DVScope.TI,
6848       /*NothingArgs=*/nullptr, /*NothingArgsSize=*/0,
6849       /*NeedDevicePtrArgs=*/nullptr, /*NeedDevicePtrArgsSize=*/0,
6850       /*AppendArgs=*/nullptr, /*AppendArgsSize=*/0);
6851   for (FunctionDecl *BaseFD : Bases)
6852     BaseFD->addAttr(OMPDeclareVariantA);
6853 }
6854 
6855 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope,
6856                                  SourceLocation LParenLoc,
6857                                  MultiExprArg ArgExprs,
6858                                  SourceLocation RParenLoc, Expr *ExecConfig) {
6859   // The common case is a regular call we do not want to specialize at all. Try
6860   // to make that case fast by bailing early.
6861   CallExpr *CE = dyn_cast<CallExpr>(Call.get());
6862   if (!CE)
6863     return Call;
6864 
6865   FunctionDecl *CalleeFnDecl = CE->getDirectCallee();
6866   if (!CalleeFnDecl)
6867     return Call;
6868 
6869   if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>())
6870     return Call;
6871 
6872   ASTContext &Context = getASTContext();
6873   std::function<void(StringRef)> DiagUnknownTrait = [this,
6874                                                      CE](StringRef ISATrait) {
6875     // TODO Track the selector locations in a way that is accessible here to
6876     // improve the diagnostic location.
6877     Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait)
6878         << ISATrait;
6879   };
6880   TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait),
6881                           getCurFunctionDecl(), DSAStack->getConstructTraits());
6882 
6883   QualType CalleeFnType = CalleeFnDecl->getType();
6884 
6885   SmallVector<Expr *, 4> Exprs;
6886   SmallVector<VariantMatchInfo, 4> VMIs;
6887   while (CalleeFnDecl) {
6888     for (OMPDeclareVariantAttr *A :
6889          CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) {
6890       Expr *VariantRef = A->getVariantFuncRef();
6891 
6892       VariantMatchInfo VMI;
6893       OMPTraitInfo &TI = A->getTraitInfo();
6894       TI.getAsVariantMatchInfo(Context, VMI);
6895       if (!isVariantApplicableInContext(VMI, OMPCtx,
6896                                         /* DeviceSetOnly */ false))
6897         continue;
6898 
6899       VMIs.push_back(VMI);
6900       Exprs.push_back(VariantRef);
6901     }
6902 
6903     CalleeFnDecl = CalleeFnDecl->getPreviousDecl();
6904   }
6905 
6906   ExprResult NewCall;
6907   do {
6908     int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx);
6909     if (BestIdx < 0)
6910       return Call;
6911     Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]);
6912     Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl();
6913 
6914     {
6915       // Try to build a (member) call expression for the current best applicable
6916       // variant expression. We allow this to fail in which case we continue
6917       // with the next best variant expression. The fail case is part of the
6918       // implementation defined behavior in the OpenMP standard when it talks
6919       // about what differences in the function prototypes: "Any differences
6920       // that the specific OpenMP context requires in the prototype of the
6921       // variant from the base function prototype are implementation defined."
6922       // This wording is there to allow the specialized variant to have a
6923       // different type than the base function. This is intended and OK but if
6924       // we cannot create a call the difference is not in the "implementation
6925       // defined range" we allow.
6926       Sema::TentativeAnalysisScope Trap(*this);
6927 
6928       if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) {
6929         auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE);
6930         BestExpr = MemberExpr::CreateImplicit(
6931             Context, MemberCall->getImplicitObjectArgument(),
6932             /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy,
6933             MemberCall->getValueKind(), MemberCall->getObjectKind());
6934       }
6935       NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc,
6936                               ExecConfig);
6937       if (NewCall.isUsable()) {
6938         if (CallExpr *NCE = dyn_cast<CallExpr>(NewCall.get())) {
6939           FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee();
6940           QualType NewType = Context.mergeFunctionTypes(
6941               CalleeFnType, NewCalleeFnDecl->getType(),
6942               /* OfBlockPointer */ false,
6943               /* Unqualified */ false, /* AllowCXX */ true);
6944           if (!NewType.isNull())
6945             break;
6946           // Don't use the call if the function type was not compatible.
6947           NewCall = nullptr;
6948         }
6949       }
6950     }
6951 
6952     VMIs.erase(VMIs.begin() + BestIdx);
6953     Exprs.erase(Exprs.begin() + BestIdx);
6954   } while (!VMIs.empty());
6955 
6956   if (!NewCall.isUsable())
6957     return Call;
6958   return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0);
6959 }
6960 
6961 Optional<std::pair<FunctionDecl *, Expr *>>
6962 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG,
6963                                         Expr *VariantRef, OMPTraitInfo &TI,
6964                                         unsigned NumAppendArgs,
6965                                         SourceRange SR) {
6966   if (!DG || DG.get().isNull())
6967     return None;
6968 
6969   const int VariantId = 1;
6970   // Must be applied only to single decl.
6971   if (!DG.get().isSingleDecl()) {
6972     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
6973         << VariantId << SR;
6974     return None;
6975   }
6976   Decl *ADecl = DG.get().getSingleDecl();
6977   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
6978     ADecl = FTD->getTemplatedDecl();
6979 
6980   // Decl must be a function.
6981   auto *FD = dyn_cast<FunctionDecl>(ADecl);
6982   if (!FD) {
6983     Diag(ADecl->getLocation(), diag::err_omp_function_expected)
6984         << VariantId << SR;
6985     return None;
6986   }
6987 
6988   auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) {
6989     return FD->hasAttrs() &&
6990            (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() ||
6991             FD->hasAttr<TargetAttr>());
6992   };
6993   // OpenMP is not compatible with CPU-specific attributes.
6994   if (HasMultiVersionAttributes(FD)) {
6995     Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes)
6996         << SR;
6997     return None;
6998   }
6999 
7000   // Allow #pragma omp declare variant only if the function is not used.
7001   if (FD->isUsed(false))
7002     Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used)
7003         << FD->getLocation();
7004 
7005   // Check if the function was emitted already.
7006   const FunctionDecl *Definition;
7007   if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) &&
7008       (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition)))
7009     Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted)
7010         << FD->getLocation();
7011 
7012   // The VariantRef must point to function.
7013   if (!VariantRef) {
7014     Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId;
7015     return None;
7016   }
7017 
7018   auto ShouldDelayChecks = [](Expr *&E, bool) {
7019     return E && (E->isTypeDependent() || E->isValueDependent() ||
7020                  E->containsUnexpandedParameterPack() ||
7021                  E->isInstantiationDependent());
7022   };
7023   // Do not check templates, wait until instantiation.
7024   if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) ||
7025       TI.anyScoreOrCondition(ShouldDelayChecks))
7026     return std::make_pair(FD, VariantRef);
7027 
7028   // Deal with non-constant score and user condition expressions.
7029   auto HandleNonConstantScoresAndConditions = [this](Expr *&E,
7030                                                      bool IsScore) -> bool {
7031     if (!E || E->isIntegerConstantExpr(Context))
7032       return false;
7033 
7034     if (IsScore) {
7035       // We warn on non-constant scores and pretend they were not present.
7036       Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant)
7037           << E;
7038       E = nullptr;
7039     } else {
7040       // We could replace a non-constant user condition with "false" but we
7041       // will soon need to handle these anyway for the dynamic version of
7042       // OpenMP context selectors.
7043       Diag(E->getExprLoc(),
7044            diag::err_omp_declare_variant_user_condition_not_constant)
7045           << E;
7046     }
7047     return true;
7048   };
7049   if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions))
7050     return None;
7051 
7052   QualType AdjustedFnType = FD->getType();
7053   if (NumAppendArgs) {
7054     if (isa<FunctionNoProtoType>(FD->getType())) {
7055       Diag(FD->getLocation(), diag::err_omp_declare_variant_prototype_required)
7056           << SR;
7057       return None;
7058     }
7059     // Adjust the function type to account for an extra omp_interop_t for each
7060     // specified in the append_args clause.
7061     const TypeDecl *TD = nullptr;
7062     LookupResult Result(*this, &Context.Idents.get("omp_interop_t"),
7063                         SR.getBegin(), Sema::LookupOrdinaryName);
7064     if (LookupName(Result, getCurScope())) {
7065       NamedDecl *ND = Result.getFoundDecl();
7066       TD = dyn_cast_or_null<TypeDecl>(ND);
7067     }
7068     if (!TD) {
7069       Diag(SR.getBegin(), diag::err_omp_interop_type_not_found) << SR;
7070       return None;
7071     }
7072     QualType InteropType = QualType(TD->getTypeForDecl(), 0);
7073     auto *PTy = cast<FunctionProtoType>(FD->getType());
7074     if (PTy->isVariadic()) {
7075       Diag(FD->getLocation(), diag::err_omp_append_args_with_varargs) << SR;
7076       return None;
7077     }
7078     llvm::SmallVector<QualType, 8> Params;
7079     Params.append(PTy->param_type_begin(), PTy->param_type_end());
7080     Params.insert(Params.end(), NumAppendArgs, InteropType);
7081     AdjustedFnType = Context.getFunctionType(PTy->getReturnType(), Params,
7082                                              PTy->getExtProtoInfo());
7083   }
7084 
7085   // Convert VariantRef expression to the type of the original function to
7086   // resolve possible conflicts.
7087   ExprResult VariantRefCast = VariantRef;
7088   if (LangOpts.CPlusPlus) {
7089     QualType FnPtrType;
7090     auto *Method = dyn_cast<CXXMethodDecl>(FD);
7091     if (Method && !Method->isStatic()) {
7092       const Type *ClassType =
7093           Context.getTypeDeclType(Method->getParent()).getTypePtr();
7094       FnPtrType = Context.getMemberPointerType(AdjustedFnType, ClassType);
7095       ExprResult ER;
7096       {
7097         // Build adrr_of unary op to correctly handle type checks for member
7098         // functions.
7099         Sema::TentativeAnalysisScope Trap(*this);
7100         ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf,
7101                                   VariantRef);
7102       }
7103       if (!ER.isUsable()) {
7104         Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
7105             << VariantId << VariantRef->getSourceRange();
7106         return None;
7107       }
7108       VariantRef = ER.get();
7109     } else {
7110       FnPtrType = Context.getPointerType(AdjustedFnType);
7111     }
7112     QualType VarianPtrType = Context.getPointerType(VariantRef->getType());
7113     if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) {
7114       ImplicitConversionSequence ICS = TryImplicitConversion(
7115           VariantRef, FnPtrType.getUnqualifiedType(),
7116           /*SuppressUserConversions=*/false, AllowedExplicit::None,
7117           /*InOverloadResolution=*/false,
7118           /*CStyle=*/false,
7119           /*AllowObjCWritebackConversion=*/false);
7120       if (ICS.isFailure()) {
7121         Diag(VariantRef->getExprLoc(),
7122              diag::err_omp_declare_variant_incompat_types)
7123             << VariantRef->getType()
7124             << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType())
7125             << (NumAppendArgs ? 1 : 0) << VariantRef->getSourceRange();
7126         return None;
7127       }
7128       VariantRefCast = PerformImplicitConversion(
7129           VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting);
7130       if (!VariantRefCast.isUsable())
7131         return None;
7132     }
7133     // Drop previously built artificial addr_of unary op for member functions.
7134     if (Method && !Method->isStatic()) {
7135       Expr *PossibleAddrOfVariantRef = VariantRefCast.get();
7136       if (auto *UO = dyn_cast<UnaryOperator>(
7137               PossibleAddrOfVariantRef->IgnoreImplicit()))
7138         VariantRefCast = UO->getSubExpr();
7139     }
7140   }
7141 
7142   ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get());
7143   if (!ER.isUsable() ||
7144       !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) {
7145     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
7146         << VariantId << VariantRef->getSourceRange();
7147     return None;
7148   }
7149 
7150   // The VariantRef must point to function.
7151   auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts());
7152   if (!DRE) {
7153     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
7154         << VariantId << VariantRef->getSourceRange();
7155     return None;
7156   }
7157   auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl());
7158   if (!NewFD) {
7159     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
7160         << VariantId << VariantRef->getSourceRange();
7161     return None;
7162   }
7163 
7164   // Check if function types are compatible in C.
7165   if (!LangOpts.CPlusPlus) {
7166     QualType NewType =
7167         Context.mergeFunctionTypes(AdjustedFnType, NewFD->getType());
7168     if (NewType.isNull()) {
7169       Diag(VariantRef->getExprLoc(),
7170            diag::err_omp_declare_variant_incompat_types)
7171           << NewFD->getType() << FD->getType() << (NumAppendArgs ? 1 : 0)
7172           << VariantRef->getSourceRange();
7173       return None;
7174     }
7175     if (NewType->isFunctionProtoType()) {
7176       if (FD->getType()->isFunctionNoProtoType())
7177         setPrototype(*this, FD, NewFD, NewType);
7178       else if (NewFD->getType()->isFunctionNoProtoType())
7179         setPrototype(*this, NewFD, FD, NewType);
7180     }
7181   }
7182 
7183   // Check if variant function is not marked with declare variant directive.
7184   if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) {
7185     Diag(VariantRef->getExprLoc(),
7186          diag::warn_omp_declare_variant_marked_as_declare_variant)
7187         << VariantRef->getSourceRange();
7188     SourceRange SR =
7189         NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange();
7190     Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR;
7191     return None;
7192   }
7193 
7194   enum DoesntSupport {
7195     VirtFuncs = 1,
7196     Constructors = 3,
7197     Destructors = 4,
7198     DeletedFuncs = 5,
7199     DefaultedFuncs = 6,
7200     ConstexprFuncs = 7,
7201     ConstevalFuncs = 8,
7202   };
7203   if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) {
7204     if (CXXFD->isVirtual()) {
7205       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7206           << VirtFuncs;
7207       return None;
7208     }
7209 
7210     if (isa<CXXConstructorDecl>(FD)) {
7211       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7212           << Constructors;
7213       return None;
7214     }
7215 
7216     if (isa<CXXDestructorDecl>(FD)) {
7217       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7218           << Destructors;
7219       return None;
7220     }
7221   }
7222 
7223   if (FD->isDeleted()) {
7224     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7225         << DeletedFuncs;
7226     return None;
7227   }
7228 
7229   if (FD->isDefaulted()) {
7230     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7231         << DefaultedFuncs;
7232     return None;
7233   }
7234 
7235   if (FD->isConstexpr()) {
7236     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7237         << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs);
7238     return None;
7239   }
7240 
7241   // Check general compatibility.
7242   if (areMultiversionVariantFunctionsCompatible(
7243           FD, NewFD, PartialDiagnostic::NullDiagnostic(),
7244           PartialDiagnosticAt(SourceLocation(),
7245                               PartialDiagnostic::NullDiagnostic()),
7246           PartialDiagnosticAt(
7247               VariantRef->getExprLoc(),
7248               PDiag(diag::err_omp_declare_variant_doesnt_support)),
7249           PartialDiagnosticAt(VariantRef->getExprLoc(),
7250                               PDiag(diag::err_omp_declare_variant_diff)
7251                                   << FD->getLocation()),
7252           /*TemplatesSupported=*/true, /*ConstexprSupported=*/false,
7253           /*CLinkageMayDiffer=*/true))
7254     return None;
7255   return std::make_pair(FD, cast<Expr>(DRE));
7256 }
7257 
7258 void Sema::ActOnOpenMPDeclareVariantDirective(
7259     FunctionDecl *FD, Expr *VariantRef, OMPTraitInfo &TI,
7260     ArrayRef<Expr *> AdjustArgsNothing,
7261     ArrayRef<Expr *> AdjustArgsNeedDevicePtr,
7262     ArrayRef<OMPDeclareVariantAttr::InteropType> AppendArgs,
7263     SourceLocation AdjustArgsLoc, SourceLocation AppendArgsLoc,
7264     SourceRange SR) {
7265 
7266   // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
7267   // An adjust_args clause or append_args clause can only be specified if the
7268   // dispatch selector of the construct selector set appears in the match
7269   // clause.
7270 
7271   SmallVector<Expr *, 8> AllAdjustArgs;
7272   llvm::append_range(AllAdjustArgs, AdjustArgsNothing);
7273   llvm::append_range(AllAdjustArgs, AdjustArgsNeedDevicePtr);
7274 
7275   if (!AllAdjustArgs.empty() || !AppendArgs.empty()) {
7276     VariantMatchInfo VMI;
7277     TI.getAsVariantMatchInfo(Context, VMI);
7278     if (!llvm::is_contained(
7279             VMI.ConstructTraits,
7280             llvm::omp::TraitProperty::construct_dispatch_dispatch)) {
7281       if (!AllAdjustArgs.empty())
7282         Diag(AdjustArgsLoc, diag::err_omp_clause_requires_dispatch_construct)
7283             << getOpenMPClauseName(OMPC_adjust_args);
7284       if (!AppendArgs.empty())
7285         Diag(AppendArgsLoc, diag::err_omp_clause_requires_dispatch_construct)
7286             << getOpenMPClauseName(OMPC_append_args);
7287       return;
7288     }
7289   }
7290 
7291   // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
7292   // Each argument can only appear in a single adjust_args clause for each
7293   // declare variant directive.
7294   llvm::SmallPtrSet<const VarDecl *, 4> AdjustVars;
7295 
7296   for (Expr *E : AllAdjustArgs) {
7297     E = E->IgnoreParenImpCasts();
7298     if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
7299       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
7300         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7301         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7302             FD->getParamDecl(PVD->getFunctionScopeIndex())
7303                     ->getCanonicalDecl() == CanonPVD) {
7304           // It's a parameter of the function, check duplicates.
7305           if (!AdjustVars.insert(CanonPVD).second) {
7306             Diag(DRE->getLocation(), diag::err_omp_adjust_arg_multiple_clauses)
7307                 << PVD;
7308             return;
7309           }
7310           continue;
7311         }
7312       }
7313     }
7314     // Anything that is not a function parameter is an error.
7315     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) << FD << 0;
7316     return;
7317   }
7318 
7319   auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit(
7320       Context, VariantRef, &TI, const_cast<Expr **>(AdjustArgsNothing.data()),
7321       AdjustArgsNothing.size(),
7322       const_cast<Expr **>(AdjustArgsNeedDevicePtr.data()),
7323       AdjustArgsNeedDevicePtr.size(),
7324       const_cast<OMPDeclareVariantAttr::InteropType *>(AppendArgs.data()),
7325       AppendArgs.size(), SR);
7326   FD->addAttr(NewAttr);
7327 }
7328 
7329 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
7330                                               Stmt *AStmt,
7331                                               SourceLocation StartLoc,
7332                                               SourceLocation EndLoc) {
7333   if (!AStmt)
7334     return StmtError();
7335 
7336   auto *CS = cast<CapturedStmt>(AStmt);
7337   // 1.2.2 OpenMP Language Terminology
7338   // Structured block - An executable statement with a single entry at the
7339   // top and a single exit at the bottom.
7340   // The point of exit cannot be a branch out of the structured block.
7341   // longjmp() and throw() must not violate the entry/exit criteria.
7342   CS->getCapturedDecl()->setNothrow();
7343 
7344   setFunctionHasBranchProtectedScope();
7345 
7346   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
7347                                       DSAStack->getTaskgroupReductionRef(),
7348                                       DSAStack->isCancelRegion());
7349 }
7350 
7351 namespace {
7352 /// Iteration space of a single for loop.
7353 struct LoopIterationSpace final {
7354   /// True if the condition operator is the strict compare operator (<, > or
7355   /// !=).
7356   bool IsStrictCompare = false;
7357   /// Condition of the loop.
7358   Expr *PreCond = nullptr;
7359   /// This expression calculates the number of iterations in the loop.
7360   /// It is always possible to calculate it before starting the loop.
7361   Expr *NumIterations = nullptr;
7362   /// The loop counter variable.
7363   Expr *CounterVar = nullptr;
7364   /// Private loop counter variable.
7365   Expr *PrivateCounterVar = nullptr;
7366   /// This is initializer for the initial value of #CounterVar.
7367   Expr *CounterInit = nullptr;
7368   /// This is step for the #CounterVar used to generate its update:
7369   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
7370   Expr *CounterStep = nullptr;
7371   /// Should step be subtracted?
7372   bool Subtract = false;
7373   /// Source range of the loop init.
7374   SourceRange InitSrcRange;
7375   /// Source range of the loop condition.
7376   SourceRange CondSrcRange;
7377   /// Source range of the loop increment.
7378   SourceRange IncSrcRange;
7379   /// Minimum value that can have the loop control variable. Used to support
7380   /// non-rectangular loops. Applied only for LCV with the non-iterator types,
7381   /// since only such variables can be used in non-loop invariant expressions.
7382   Expr *MinValue = nullptr;
7383   /// Maximum value that can have the loop control variable. Used to support
7384   /// non-rectangular loops. Applied only for LCV with the non-iterator type,
7385   /// since only such variables can be used in non-loop invariant expressions.
7386   Expr *MaxValue = nullptr;
7387   /// true, if the lower bound depends on the outer loop control var.
7388   bool IsNonRectangularLB = false;
7389   /// true, if the upper bound depends on the outer loop control var.
7390   bool IsNonRectangularUB = false;
7391   /// Index of the loop this loop depends on and forms non-rectangular loop
7392   /// nest.
7393   unsigned LoopDependentIdx = 0;
7394   /// Final condition for the non-rectangular loop nest support. It is used to
7395   /// check that the number of iterations for this particular counter must be
7396   /// finished.
7397   Expr *FinalCondition = nullptr;
7398 };
7399 
7400 /// Helper class for checking canonical form of the OpenMP loops and
7401 /// extracting iteration space of each loop in the loop nest, that will be used
7402 /// for IR generation.
7403 class OpenMPIterationSpaceChecker {
7404   /// Reference to Sema.
7405   Sema &SemaRef;
7406   /// Does the loop associated directive support non-rectangular loops?
7407   bool SupportsNonRectangular;
7408   /// Data-sharing stack.
7409   DSAStackTy &Stack;
7410   /// A location for diagnostics (when there is no some better location).
7411   SourceLocation DefaultLoc;
7412   /// A location for diagnostics (when increment is not compatible).
7413   SourceLocation ConditionLoc;
7414   /// A source location for referring to loop init later.
7415   SourceRange InitSrcRange;
7416   /// A source location for referring to condition later.
7417   SourceRange ConditionSrcRange;
7418   /// A source location for referring to increment later.
7419   SourceRange IncrementSrcRange;
7420   /// Loop variable.
7421   ValueDecl *LCDecl = nullptr;
7422   /// Reference to loop variable.
7423   Expr *LCRef = nullptr;
7424   /// Lower bound (initializer for the var).
7425   Expr *LB = nullptr;
7426   /// Upper bound.
7427   Expr *UB = nullptr;
7428   /// Loop step (increment).
7429   Expr *Step = nullptr;
7430   /// This flag is true when condition is one of:
7431   ///   Var <  UB
7432   ///   Var <= UB
7433   ///   UB  >  Var
7434   ///   UB  >= Var
7435   /// This will have no value when the condition is !=
7436   llvm::Optional<bool> TestIsLessOp;
7437   /// This flag is true when condition is strict ( < or > ).
7438   bool TestIsStrictOp = false;
7439   /// This flag is true when step is subtracted on each iteration.
7440   bool SubtractStep = false;
7441   /// The outer loop counter this loop depends on (if any).
7442   const ValueDecl *DepDecl = nullptr;
7443   /// Contains number of loop (starts from 1) on which loop counter init
7444   /// expression of this loop depends on.
7445   Optional<unsigned> InitDependOnLC;
7446   /// Contains number of loop (starts from 1) on which loop counter condition
7447   /// expression of this loop depends on.
7448   Optional<unsigned> CondDependOnLC;
7449   /// Checks if the provide statement depends on the loop counter.
7450   Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer);
7451   /// Original condition required for checking of the exit condition for
7452   /// non-rectangular loop.
7453   Expr *Condition = nullptr;
7454 
7455 public:
7456   OpenMPIterationSpaceChecker(Sema &SemaRef, bool SupportsNonRectangular,
7457                               DSAStackTy &Stack, SourceLocation DefaultLoc)
7458       : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular),
7459         Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
7460   /// Check init-expr for canonical loop form and save loop counter
7461   /// variable - #Var and its initialization value - #LB.
7462   bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
7463   /// Check test-expr for canonical form, save upper-bound (#UB), flags
7464   /// for less/greater and for strict/non-strict comparison.
7465   bool checkAndSetCond(Expr *S);
7466   /// Check incr-expr for canonical loop form and return true if it
7467   /// does not conform, otherwise save loop step (#Step).
7468   bool checkAndSetInc(Expr *S);
7469   /// Return the loop counter variable.
7470   ValueDecl *getLoopDecl() const { return LCDecl; }
7471   /// Return the reference expression to loop counter variable.
7472   Expr *getLoopDeclRefExpr() const { return LCRef; }
7473   /// Source range of the loop init.
7474   SourceRange getInitSrcRange() const { return InitSrcRange; }
7475   /// Source range of the loop condition.
7476   SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
7477   /// Source range of the loop increment.
7478   SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
7479   /// True if the step should be subtracted.
7480   bool shouldSubtractStep() const { return SubtractStep; }
7481   /// True, if the compare operator is strict (<, > or !=).
7482   bool isStrictTestOp() const { return TestIsStrictOp; }
7483   /// Build the expression to calculate the number of iterations.
7484   Expr *buildNumIterations(
7485       Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
7486       llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
7487   /// Build the precondition expression for the loops.
7488   Expr *
7489   buildPreCond(Scope *S, Expr *Cond,
7490                llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
7491   /// Build reference expression to the counter be used for codegen.
7492   DeclRefExpr *
7493   buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
7494                   DSAStackTy &DSA) const;
7495   /// Build reference expression to the private counter be used for
7496   /// codegen.
7497   Expr *buildPrivateCounterVar() const;
7498   /// Build initialization of the counter be used for codegen.
7499   Expr *buildCounterInit() const;
7500   /// Build step of the counter be used for codegen.
7501   Expr *buildCounterStep() const;
7502   /// Build loop data with counter value for depend clauses in ordered
7503   /// directives.
7504   Expr *
7505   buildOrderedLoopData(Scope *S, Expr *Counter,
7506                        llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
7507                        SourceLocation Loc, Expr *Inc = nullptr,
7508                        OverloadedOperatorKind OOK = OO_Amp);
7509   /// Builds the minimum value for the loop counter.
7510   std::pair<Expr *, Expr *> buildMinMaxValues(
7511       Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
7512   /// Builds final condition for the non-rectangular loops.
7513   Expr *buildFinalCondition(Scope *S) const;
7514   /// Return true if any expression is dependent.
7515   bool dependent() const;
7516   /// Returns true if the initializer forms non-rectangular loop.
7517   bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); }
7518   /// Returns true if the condition forms non-rectangular loop.
7519   bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); }
7520   /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
7521   unsigned getLoopDependentIdx() const {
7522     return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0));
7523   }
7524 
7525 private:
7526   /// Check the right-hand side of an assignment in the increment
7527   /// expression.
7528   bool checkAndSetIncRHS(Expr *RHS);
7529   /// Helper to set loop counter variable and its initializer.
7530   bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
7531                       bool EmitDiags);
7532   /// Helper to set upper bound.
7533   bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
7534              SourceRange SR, SourceLocation SL);
7535   /// Helper to set loop increment.
7536   bool setStep(Expr *NewStep, bool Subtract);
7537 };
7538 
7539 bool OpenMPIterationSpaceChecker::dependent() const {
7540   if (!LCDecl) {
7541     assert(!LB && !UB && !Step);
7542     return false;
7543   }
7544   return LCDecl->getType()->isDependentType() ||
7545          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
7546          (Step && Step->isValueDependent());
7547 }
7548 
7549 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
7550                                                  Expr *NewLCRefExpr,
7551                                                  Expr *NewLB, bool EmitDiags) {
7552   // State consistency checking to ensure correct usage.
7553   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
7554          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
7555   if (!NewLCDecl || !NewLB || NewLB->containsErrors())
7556     return true;
7557   LCDecl = getCanonicalDecl(NewLCDecl);
7558   LCRef = NewLCRefExpr;
7559   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
7560     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
7561       if ((Ctor->isCopyOrMoveConstructor() ||
7562            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
7563           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
7564         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
7565   LB = NewLB;
7566   if (EmitDiags)
7567     InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true);
7568   return false;
7569 }
7570 
7571 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
7572                                         llvm::Optional<bool> LessOp,
7573                                         bool StrictOp, SourceRange SR,
7574                                         SourceLocation SL) {
7575   // State consistency checking to ensure correct usage.
7576   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
7577          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
7578   if (!NewUB || NewUB->containsErrors())
7579     return true;
7580   UB = NewUB;
7581   if (LessOp)
7582     TestIsLessOp = LessOp;
7583   TestIsStrictOp = StrictOp;
7584   ConditionSrcRange = SR;
7585   ConditionLoc = SL;
7586   CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false);
7587   return false;
7588 }
7589 
7590 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
7591   // State consistency checking to ensure correct usage.
7592   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
7593   if (!NewStep || NewStep->containsErrors())
7594     return true;
7595   if (!NewStep->isValueDependent()) {
7596     // Check that the step is integer expression.
7597     SourceLocation StepLoc = NewStep->getBeginLoc();
7598     ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
7599         StepLoc, getExprAsWritten(NewStep));
7600     if (Val.isInvalid())
7601       return true;
7602     NewStep = Val.get();
7603 
7604     // OpenMP [2.6, Canonical Loop Form, Restrictions]
7605     //  If test-expr is of form var relational-op b and relational-op is < or
7606     //  <= then incr-expr must cause var to increase on each iteration of the
7607     //  loop. If test-expr is of form var relational-op b and relational-op is
7608     //  > or >= then incr-expr must cause var to decrease on each iteration of
7609     //  the loop.
7610     //  If test-expr is of form b relational-op var and relational-op is < or
7611     //  <= then incr-expr must cause var to decrease on each iteration of the
7612     //  loop. If test-expr is of form b relational-op var and relational-op is
7613     //  > or >= then incr-expr must cause var to increase on each iteration of
7614     //  the loop.
7615     Optional<llvm::APSInt> Result =
7616         NewStep->getIntegerConstantExpr(SemaRef.Context);
7617     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
7618     bool IsConstNeg =
7619         Result && Result->isSigned() && (Subtract != Result->isNegative());
7620     bool IsConstPos =
7621         Result && Result->isSigned() && (Subtract == Result->isNegative());
7622     bool IsConstZero = Result && !Result->getBoolValue();
7623 
7624     // != with increment is treated as <; != with decrement is treated as >
7625     if (!TestIsLessOp.hasValue())
7626       TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
7627     if (UB &&
7628         (IsConstZero || (TestIsLessOp.getValue()
7629                              ? (IsConstNeg || (IsUnsigned && Subtract))
7630                              : (IsConstPos || (IsUnsigned && !Subtract))))) {
7631       SemaRef.Diag(NewStep->getExprLoc(),
7632                    diag::err_omp_loop_incr_not_compatible)
7633           << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
7634       SemaRef.Diag(ConditionLoc,
7635                    diag::note_omp_loop_cond_requres_compatible_incr)
7636           << TestIsLessOp.getValue() << ConditionSrcRange;
7637       return true;
7638     }
7639     if (TestIsLessOp.getValue() == Subtract) {
7640       NewStep =
7641           SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
7642               .get();
7643       Subtract = !Subtract;
7644     }
7645   }
7646 
7647   Step = NewStep;
7648   SubtractStep = Subtract;
7649   return false;
7650 }
7651 
7652 namespace {
7653 /// Checker for the non-rectangular loops. Checks if the initializer or
7654 /// condition expression references loop counter variable.
7655 class LoopCounterRefChecker final
7656     : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
7657   Sema &SemaRef;
7658   DSAStackTy &Stack;
7659   const ValueDecl *CurLCDecl = nullptr;
7660   const ValueDecl *DepDecl = nullptr;
7661   const ValueDecl *PrevDepDecl = nullptr;
7662   bool IsInitializer = true;
7663   bool SupportsNonRectangular;
7664   unsigned BaseLoopId = 0;
7665   bool checkDecl(const Expr *E, const ValueDecl *VD) {
7666     if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) {
7667       SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter)
7668           << (IsInitializer ? 0 : 1);
7669       return false;
7670     }
7671     const auto &&Data = Stack.isLoopControlVariable(VD);
7672     // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
7673     // The type of the loop iterator on which we depend may not have a random
7674     // access iterator type.
7675     if (Data.first && VD->getType()->isRecordType()) {
7676       SmallString<128> Name;
7677       llvm::raw_svector_ostream OS(Name);
7678       VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
7679                                /*Qualified=*/true);
7680       SemaRef.Diag(E->getExprLoc(),
7681                    diag::err_omp_wrong_dependency_iterator_type)
7682           << OS.str();
7683       SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD;
7684       return false;
7685     }
7686     if (Data.first && !SupportsNonRectangular) {
7687       SemaRef.Diag(E->getExprLoc(), diag::err_omp_invariant_dependency);
7688       return false;
7689     }
7690     if (Data.first &&
7691         (DepDecl || (PrevDepDecl &&
7692                      getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) {
7693       if (!DepDecl && PrevDepDecl)
7694         DepDecl = PrevDepDecl;
7695       SmallString<128> Name;
7696       llvm::raw_svector_ostream OS(Name);
7697       DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
7698                                     /*Qualified=*/true);
7699       SemaRef.Diag(E->getExprLoc(),
7700                    diag::err_omp_invariant_or_linear_dependency)
7701           << OS.str();
7702       return false;
7703     }
7704     if (Data.first) {
7705       DepDecl = VD;
7706       BaseLoopId = Data.first;
7707     }
7708     return Data.first;
7709   }
7710 
7711 public:
7712   bool VisitDeclRefExpr(const DeclRefExpr *E) {
7713     const ValueDecl *VD = E->getDecl();
7714     if (isa<VarDecl>(VD))
7715       return checkDecl(E, VD);
7716     return false;
7717   }
7718   bool VisitMemberExpr(const MemberExpr *E) {
7719     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
7720       const ValueDecl *VD = E->getMemberDecl();
7721       if (isa<VarDecl>(VD) || isa<FieldDecl>(VD))
7722         return checkDecl(E, VD);
7723     }
7724     return false;
7725   }
7726   bool VisitStmt(const Stmt *S) {
7727     bool Res = false;
7728     for (const Stmt *Child : S->children())
7729       Res = (Child && Visit(Child)) || Res;
7730     return Res;
7731   }
7732   explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
7733                                  const ValueDecl *CurLCDecl, bool IsInitializer,
7734                                  const ValueDecl *PrevDepDecl = nullptr,
7735                                  bool SupportsNonRectangular = true)
7736       : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
7737         PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer),
7738         SupportsNonRectangular(SupportsNonRectangular) {}
7739   unsigned getBaseLoopId() const {
7740     assert(CurLCDecl && "Expected loop dependency.");
7741     return BaseLoopId;
7742   }
7743   const ValueDecl *getDepDecl() const {
7744     assert(CurLCDecl && "Expected loop dependency.");
7745     return DepDecl;
7746   }
7747 };
7748 } // namespace
7749 
7750 Optional<unsigned>
7751 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
7752                                                      bool IsInitializer) {
7753   // Check for the non-rectangular loops.
7754   LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
7755                                         DepDecl, SupportsNonRectangular);
7756   if (LoopStmtChecker.Visit(S)) {
7757     DepDecl = LoopStmtChecker.getDepDecl();
7758     return LoopStmtChecker.getBaseLoopId();
7759   }
7760   return llvm::None;
7761 }
7762 
7763 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
7764   // Check init-expr for canonical loop form and save loop counter
7765   // variable - #Var and its initialization value - #LB.
7766   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
7767   //   var = lb
7768   //   integer-type var = lb
7769   //   random-access-iterator-type var = lb
7770   //   pointer-type var = lb
7771   //
7772   if (!S) {
7773     if (EmitDiags) {
7774       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
7775     }
7776     return true;
7777   }
7778   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
7779     if (!ExprTemp->cleanupsHaveSideEffects())
7780       S = ExprTemp->getSubExpr();
7781 
7782   InitSrcRange = S->getSourceRange();
7783   if (Expr *E = dyn_cast<Expr>(S))
7784     S = E->IgnoreParens();
7785   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
7786     if (BO->getOpcode() == BO_Assign) {
7787       Expr *LHS = BO->getLHS()->IgnoreParens();
7788       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
7789         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
7790           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
7791             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
7792                                   EmitDiags);
7793         return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags);
7794       }
7795       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
7796         if (ME->isArrow() &&
7797             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
7798           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
7799                                 EmitDiags);
7800       }
7801     }
7802   } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
7803     if (DS->isSingleDecl()) {
7804       if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
7805         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
7806           // Accept non-canonical init form here but emit ext. warning.
7807           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
7808             SemaRef.Diag(S->getBeginLoc(),
7809                          diag::ext_omp_loop_not_canonical_init)
7810                 << S->getSourceRange();
7811           return setLCDeclAndLB(
7812               Var,
7813               buildDeclRefExpr(SemaRef, Var,
7814                                Var->getType().getNonReferenceType(),
7815                                DS->getBeginLoc()),
7816               Var->getInit(), EmitDiags);
7817         }
7818       }
7819     }
7820   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
7821     if (CE->getOperator() == OO_Equal) {
7822       Expr *LHS = CE->getArg(0);
7823       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
7824         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
7825           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
7826             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
7827                                   EmitDiags);
7828         return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags);
7829       }
7830       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
7831         if (ME->isArrow() &&
7832             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
7833           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
7834                                 EmitDiags);
7835       }
7836     }
7837   }
7838 
7839   if (dependent() || SemaRef.CurContext->isDependentContext())
7840     return false;
7841   if (EmitDiags) {
7842     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
7843         << S->getSourceRange();
7844   }
7845   return true;
7846 }
7847 
7848 /// Ignore parenthesizes, implicit casts, copy constructor and return the
7849 /// variable (which may be the loop variable) if possible.
7850 static const ValueDecl *getInitLCDecl(const Expr *E) {
7851   if (!E)
7852     return nullptr;
7853   E = getExprAsWritten(E);
7854   if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
7855     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
7856       if ((Ctor->isCopyOrMoveConstructor() ||
7857            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
7858           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
7859         E = CE->getArg(0)->IgnoreParenImpCasts();
7860   if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
7861     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
7862       return getCanonicalDecl(VD);
7863   }
7864   if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
7865     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
7866       return getCanonicalDecl(ME->getMemberDecl());
7867   return nullptr;
7868 }
7869 
7870 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
7871   // Check test-expr for canonical form, save upper-bound UB, flags for
7872   // less/greater and for strict/non-strict comparison.
7873   // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
7874   //   var relational-op b
7875   //   b relational-op var
7876   //
7877   bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
7878   if (!S) {
7879     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond)
7880         << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
7881     return true;
7882   }
7883   Condition = S;
7884   S = getExprAsWritten(S);
7885   SourceLocation CondLoc = S->getBeginLoc();
7886   auto &&CheckAndSetCond = [this, IneqCondIsCanonical](
7887                                BinaryOperatorKind Opcode, const Expr *LHS,
7888                                const Expr *RHS, SourceRange SR,
7889                                SourceLocation OpLoc) -> llvm::Optional<bool> {
7890     if (BinaryOperator::isRelationalOp(Opcode)) {
7891       if (getInitLCDecl(LHS) == LCDecl)
7892         return setUB(const_cast<Expr *>(RHS),
7893                      (Opcode == BO_LT || Opcode == BO_LE),
7894                      (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc);
7895       if (getInitLCDecl(RHS) == LCDecl)
7896         return setUB(const_cast<Expr *>(LHS),
7897                      (Opcode == BO_GT || Opcode == BO_GE),
7898                      (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc);
7899     } else if (IneqCondIsCanonical && Opcode == BO_NE) {
7900       return setUB(const_cast<Expr *>(getInitLCDecl(LHS) == LCDecl ? RHS : LHS),
7901                    /*LessOp=*/llvm::None,
7902                    /*StrictOp=*/true, SR, OpLoc);
7903     }
7904     return llvm::None;
7905   };
7906   llvm::Optional<bool> Res;
7907   if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(S)) {
7908     CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm();
7909     Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(),
7910                           RBO->getOperatorLoc());
7911   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
7912     Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(),
7913                           BO->getSourceRange(), BO->getOperatorLoc());
7914   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
7915     if (CE->getNumArgs() == 2) {
7916       Res = CheckAndSetCond(
7917           BinaryOperator::getOverloadedOpcode(CE->getOperator()), CE->getArg(0),
7918           CE->getArg(1), CE->getSourceRange(), CE->getOperatorLoc());
7919     }
7920   }
7921   if (Res.hasValue())
7922     return *Res;
7923   if (dependent() || SemaRef.CurContext->isDependentContext())
7924     return false;
7925   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
7926       << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
7927   return true;
7928 }
7929 
7930 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
7931   // RHS of canonical loop form increment can be:
7932   //   var + incr
7933   //   incr + var
7934   //   var - incr
7935   //
7936   RHS = RHS->IgnoreParenImpCasts();
7937   if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
7938     if (BO->isAdditiveOp()) {
7939       bool IsAdd = BO->getOpcode() == BO_Add;
7940       if (getInitLCDecl(BO->getLHS()) == LCDecl)
7941         return setStep(BO->getRHS(), !IsAdd);
7942       if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
7943         return setStep(BO->getLHS(), /*Subtract=*/false);
7944     }
7945   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
7946     bool IsAdd = CE->getOperator() == OO_Plus;
7947     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
7948       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
7949         return setStep(CE->getArg(1), !IsAdd);
7950       if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
7951         return setStep(CE->getArg(0), /*Subtract=*/false);
7952     }
7953   }
7954   if (dependent() || SemaRef.CurContext->isDependentContext())
7955     return false;
7956   SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
7957       << RHS->getSourceRange() << LCDecl;
7958   return true;
7959 }
7960 
7961 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
7962   // Check incr-expr for canonical loop form and return true if it
7963   // does not conform.
7964   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
7965   //   ++var
7966   //   var++
7967   //   --var
7968   //   var--
7969   //   var += incr
7970   //   var -= incr
7971   //   var = var + incr
7972   //   var = incr + var
7973   //   var = var - incr
7974   //
7975   if (!S) {
7976     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
7977     return true;
7978   }
7979   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
7980     if (!ExprTemp->cleanupsHaveSideEffects())
7981       S = ExprTemp->getSubExpr();
7982 
7983   IncrementSrcRange = S->getSourceRange();
7984   S = S->IgnoreParens();
7985   if (auto *UO = dyn_cast<UnaryOperator>(S)) {
7986     if (UO->isIncrementDecrementOp() &&
7987         getInitLCDecl(UO->getSubExpr()) == LCDecl)
7988       return setStep(SemaRef
7989                          .ActOnIntegerConstant(UO->getBeginLoc(),
7990                                                (UO->isDecrementOp() ? -1 : 1))
7991                          .get(),
7992                      /*Subtract=*/false);
7993   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
7994     switch (BO->getOpcode()) {
7995     case BO_AddAssign:
7996     case BO_SubAssign:
7997       if (getInitLCDecl(BO->getLHS()) == LCDecl)
7998         return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
7999       break;
8000     case BO_Assign:
8001       if (getInitLCDecl(BO->getLHS()) == LCDecl)
8002         return checkAndSetIncRHS(BO->getRHS());
8003       break;
8004     default:
8005       break;
8006     }
8007   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
8008     switch (CE->getOperator()) {
8009     case OO_PlusPlus:
8010     case OO_MinusMinus:
8011       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
8012         return setStep(SemaRef
8013                            .ActOnIntegerConstant(
8014                                CE->getBeginLoc(),
8015                                ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
8016                            .get(),
8017                        /*Subtract=*/false);
8018       break;
8019     case OO_PlusEqual:
8020     case OO_MinusEqual:
8021       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
8022         return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
8023       break;
8024     case OO_Equal:
8025       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
8026         return checkAndSetIncRHS(CE->getArg(1));
8027       break;
8028     default:
8029       break;
8030     }
8031   }
8032   if (dependent() || SemaRef.CurContext->isDependentContext())
8033     return false;
8034   SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
8035       << S->getSourceRange() << LCDecl;
8036   return true;
8037 }
8038 
8039 static ExprResult
8040 tryBuildCapture(Sema &SemaRef, Expr *Capture,
8041                 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
8042   if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors())
8043     return Capture;
8044   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
8045     return SemaRef.PerformImplicitConversion(
8046         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
8047         /*AllowExplicit=*/true);
8048   auto I = Captures.find(Capture);
8049   if (I != Captures.end())
8050     return buildCapture(SemaRef, Capture, I->second);
8051   DeclRefExpr *Ref = nullptr;
8052   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
8053   Captures[Capture] = Ref;
8054   return Res;
8055 }
8056 
8057 /// Calculate number of iterations, transforming to unsigned, if number of
8058 /// iterations may be larger than the original type.
8059 static Expr *
8060 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc,
8061                   Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy,
8062                   bool TestIsStrictOp, bool RoundToStep,
8063                   llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
8064   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
8065   if (!NewStep.isUsable())
8066     return nullptr;
8067   llvm::APSInt LRes, SRes;
8068   bool IsLowerConst = false, IsStepConst = false;
8069   if (Optional<llvm::APSInt> Res =
8070           Lower->getIntegerConstantExpr(SemaRef.Context)) {
8071     LRes = *Res;
8072     IsLowerConst = true;
8073   }
8074   if (Optional<llvm::APSInt> Res =
8075           Step->getIntegerConstantExpr(SemaRef.Context)) {
8076     SRes = *Res;
8077     IsStepConst = true;
8078   }
8079   bool NoNeedToConvert = IsLowerConst && !RoundToStep &&
8080                          ((!TestIsStrictOp && LRes.isNonNegative()) ||
8081                           (TestIsStrictOp && LRes.isStrictlyPositive()));
8082   bool NeedToReorganize = false;
8083   // Check if any subexpressions in Lower -Step [+ 1] lead to overflow.
8084   if (!NoNeedToConvert && IsLowerConst &&
8085       (TestIsStrictOp || (RoundToStep && IsStepConst))) {
8086     NoNeedToConvert = true;
8087     if (RoundToStep) {
8088       unsigned BW = LRes.getBitWidth() > SRes.getBitWidth()
8089                         ? LRes.getBitWidth()
8090                         : SRes.getBitWidth();
8091       LRes = LRes.extend(BW + 1);
8092       LRes.setIsSigned(true);
8093       SRes = SRes.extend(BW + 1);
8094       SRes.setIsSigned(true);
8095       LRes -= SRes;
8096       NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes;
8097       LRes = LRes.trunc(BW);
8098     }
8099     if (TestIsStrictOp) {
8100       unsigned BW = LRes.getBitWidth();
8101       LRes = LRes.extend(BW + 1);
8102       LRes.setIsSigned(true);
8103       ++LRes;
8104       NoNeedToConvert =
8105           NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes;
8106       // truncate to the original bitwidth.
8107       LRes = LRes.trunc(BW);
8108     }
8109     NeedToReorganize = NoNeedToConvert;
8110   }
8111   llvm::APSInt URes;
8112   bool IsUpperConst = false;
8113   if (Optional<llvm::APSInt> Res =
8114           Upper->getIntegerConstantExpr(SemaRef.Context)) {
8115     URes = *Res;
8116     IsUpperConst = true;
8117   }
8118   if (NoNeedToConvert && IsLowerConst && IsUpperConst &&
8119       (!RoundToStep || IsStepConst)) {
8120     unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth()
8121                                                           : URes.getBitWidth();
8122     LRes = LRes.extend(BW + 1);
8123     LRes.setIsSigned(true);
8124     URes = URes.extend(BW + 1);
8125     URes.setIsSigned(true);
8126     URes -= LRes;
8127     NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes;
8128     NeedToReorganize = NoNeedToConvert;
8129   }
8130   // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant
8131   // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to
8132   // unsigned.
8133   if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) &&
8134       !LCTy->isDependentType() && LCTy->isIntegerType()) {
8135     QualType LowerTy = Lower->getType();
8136     QualType UpperTy = Upper->getType();
8137     uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy);
8138     uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy);
8139     if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) ||
8140         (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) {
8141       QualType CastType = SemaRef.Context.getIntTypeForBitwidth(
8142           LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0);
8143       Upper =
8144           SemaRef
8145               .PerformImplicitConversion(
8146                   SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(),
8147                   CastType, Sema::AA_Converting)
8148               .get();
8149       Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get();
8150       NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get());
8151     }
8152   }
8153   if (!Lower || !Upper || NewStep.isInvalid())
8154     return nullptr;
8155 
8156   ExprResult Diff;
8157   // If need to reorganize, then calculate the form as Upper - (Lower - Step [+
8158   // 1]).
8159   if (NeedToReorganize) {
8160     Diff = Lower;
8161 
8162     if (RoundToStep) {
8163       // Lower - Step
8164       Diff =
8165           SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get());
8166       if (!Diff.isUsable())
8167         return nullptr;
8168     }
8169 
8170     // Lower - Step [+ 1]
8171     if (TestIsStrictOp)
8172       Diff = SemaRef.BuildBinOp(
8173           S, DefaultLoc, BO_Add, Diff.get(),
8174           SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
8175     if (!Diff.isUsable())
8176       return nullptr;
8177 
8178     Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
8179     if (!Diff.isUsable())
8180       return nullptr;
8181 
8182     // Upper - (Lower - Step [+ 1]).
8183     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get());
8184     if (!Diff.isUsable())
8185       return nullptr;
8186   } else {
8187     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
8188 
8189     if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) {
8190       // BuildBinOp already emitted error, this one is to point user to upper
8191       // and lower bound, and to tell what is passed to 'operator-'.
8192       SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
8193           << Upper->getSourceRange() << Lower->getSourceRange();
8194       return nullptr;
8195     }
8196 
8197     if (!Diff.isUsable())
8198       return nullptr;
8199 
8200     // Upper - Lower [- 1]
8201     if (TestIsStrictOp)
8202       Diff = SemaRef.BuildBinOp(
8203           S, DefaultLoc, BO_Sub, Diff.get(),
8204           SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
8205     if (!Diff.isUsable())
8206       return nullptr;
8207 
8208     if (RoundToStep) {
8209       // Upper - Lower [- 1] + Step
8210       Diff =
8211           SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
8212       if (!Diff.isUsable())
8213         return nullptr;
8214     }
8215   }
8216 
8217   // Parentheses (for dumping/debugging purposes only).
8218   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
8219   if (!Diff.isUsable())
8220     return nullptr;
8221 
8222   // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step
8223   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
8224   if (!Diff.isUsable())
8225     return nullptr;
8226 
8227   return Diff.get();
8228 }
8229 
8230 /// Build the expression to calculate the number of iterations.
8231 Expr *OpenMPIterationSpaceChecker::buildNumIterations(
8232     Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
8233     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
8234   QualType VarType = LCDecl->getType().getNonReferenceType();
8235   if (!VarType->isIntegerType() && !VarType->isPointerType() &&
8236       !SemaRef.getLangOpts().CPlusPlus)
8237     return nullptr;
8238   Expr *LBVal = LB;
8239   Expr *UBVal = UB;
8240   // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
8241   // max(LB(MinVal), LB(MaxVal))
8242   if (InitDependOnLC) {
8243     const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1];
8244     if (!IS.MinValue || !IS.MaxValue)
8245       return nullptr;
8246     // OuterVar = Min
8247     ExprResult MinValue =
8248         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
8249     if (!MinValue.isUsable())
8250       return nullptr;
8251 
8252     ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
8253                                              IS.CounterVar, MinValue.get());
8254     if (!LBMinVal.isUsable())
8255       return nullptr;
8256     // OuterVar = Min, LBVal
8257     LBMinVal =
8258         SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal);
8259     if (!LBMinVal.isUsable())
8260       return nullptr;
8261     // (OuterVar = Min, LBVal)
8262     LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get());
8263     if (!LBMinVal.isUsable())
8264       return nullptr;
8265 
8266     // OuterVar = Max
8267     ExprResult MaxValue =
8268         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
8269     if (!MaxValue.isUsable())
8270       return nullptr;
8271 
8272     ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
8273                                              IS.CounterVar, MaxValue.get());
8274     if (!LBMaxVal.isUsable())
8275       return nullptr;
8276     // OuterVar = Max, LBVal
8277     LBMaxVal =
8278         SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal);
8279     if (!LBMaxVal.isUsable())
8280       return nullptr;
8281     // (OuterVar = Max, LBVal)
8282     LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get());
8283     if (!LBMaxVal.isUsable())
8284       return nullptr;
8285 
8286     Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get();
8287     Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get();
8288     if (!LBMin || !LBMax)
8289       return nullptr;
8290     // LB(MinVal) < LB(MaxVal)
8291     ExprResult MinLessMaxRes =
8292         SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax);
8293     if (!MinLessMaxRes.isUsable())
8294       return nullptr;
8295     Expr *MinLessMax =
8296         tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get();
8297     if (!MinLessMax)
8298       return nullptr;
8299     if (TestIsLessOp.getValue()) {
8300       // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
8301       // LB(MaxVal))
8302       ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
8303                                                     MinLessMax, LBMin, LBMax);
8304       if (!MinLB.isUsable())
8305         return nullptr;
8306       LBVal = MinLB.get();
8307     } else {
8308       // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
8309       // LB(MaxVal))
8310       ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
8311                                                     MinLessMax, LBMax, LBMin);
8312       if (!MaxLB.isUsable())
8313         return nullptr;
8314       LBVal = MaxLB.get();
8315     }
8316   }
8317   // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
8318   // min(UB(MinVal), UB(MaxVal))
8319   if (CondDependOnLC) {
8320     const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1];
8321     if (!IS.MinValue || !IS.MaxValue)
8322       return nullptr;
8323     // OuterVar = Min
8324     ExprResult MinValue =
8325         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
8326     if (!MinValue.isUsable())
8327       return nullptr;
8328 
8329     ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
8330                                              IS.CounterVar, MinValue.get());
8331     if (!UBMinVal.isUsable())
8332       return nullptr;
8333     // OuterVar = Min, UBVal
8334     UBMinVal =
8335         SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal);
8336     if (!UBMinVal.isUsable())
8337       return nullptr;
8338     // (OuterVar = Min, UBVal)
8339     UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get());
8340     if (!UBMinVal.isUsable())
8341       return nullptr;
8342 
8343     // OuterVar = Max
8344     ExprResult MaxValue =
8345         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
8346     if (!MaxValue.isUsable())
8347       return nullptr;
8348 
8349     ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
8350                                              IS.CounterVar, MaxValue.get());
8351     if (!UBMaxVal.isUsable())
8352       return nullptr;
8353     // OuterVar = Max, UBVal
8354     UBMaxVal =
8355         SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal);
8356     if (!UBMaxVal.isUsable())
8357       return nullptr;
8358     // (OuterVar = Max, UBVal)
8359     UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get());
8360     if (!UBMaxVal.isUsable())
8361       return nullptr;
8362 
8363     Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get();
8364     Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get();
8365     if (!UBMin || !UBMax)
8366       return nullptr;
8367     // UB(MinVal) > UB(MaxVal)
8368     ExprResult MinGreaterMaxRes =
8369         SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax);
8370     if (!MinGreaterMaxRes.isUsable())
8371       return nullptr;
8372     Expr *MinGreaterMax =
8373         tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get();
8374     if (!MinGreaterMax)
8375       return nullptr;
8376     if (TestIsLessOp.getValue()) {
8377       // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
8378       // UB(MaxVal))
8379       ExprResult MaxUB = SemaRef.ActOnConditionalOp(
8380           DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax);
8381       if (!MaxUB.isUsable())
8382         return nullptr;
8383       UBVal = MaxUB.get();
8384     } else {
8385       // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
8386       // UB(MaxVal))
8387       ExprResult MinUB = SemaRef.ActOnConditionalOp(
8388           DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin);
8389       if (!MinUB.isUsable())
8390         return nullptr;
8391       UBVal = MinUB.get();
8392     }
8393   }
8394   Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal;
8395   Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal;
8396   Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
8397   Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
8398   if (!Upper || !Lower)
8399     return nullptr;
8400 
8401   ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper,
8402                                       Step, VarType, TestIsStrictOp,
8403                                       /*RoundToStep=*/true, Captures);
8404   if (!Diff.isUsable())
8405     return nullptr;
8406 
8407   // OpenMP runtime requires 32-bit or 64-bit loop variables.
8408   QualType Type = Diff.get()->getType();
8409   ASTContext &C = SemaRef.Context;
8410   bool UseVarType = VarType->hasIntegerRepresentation() &&
8411                     C.getTypeSize(Type) > C.getTypeSize(VarType);
8412   if (!Type->isIntegerType() || UseVarType) {
8413     unsigned NewSize =
8414         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
8415     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
8416                                : Type->hasSignedIntegerRepresentation();
8417     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
8418     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
8419       Diff = SemaRef.PerformImplicitConversion(
8420           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
8421       if (!Diff.isUsable())
8422         return nullptr;
8423     }
8424   }
8425   if (LimitedType) {
8426     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
8427     if (NewSize != C.getTypeSize(Type)) {
8428       if (NewSize < C.getTypeSize(Type)) {
8429         assert(NewSize == 64 && "incorrect loop var size");
8430         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
8431             << InitSrcRange << ConditionSrcRange;
8432       }
8433       QualType NewType = C.getIntTypeForBitwidth(
8434           NewSize, Type->hasSignedIntegerRepresentation() ||
8435                        C.getTypeSize(Type) < NewSize);
8436       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
8437         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
8438                                                  Sema::AA_Converting, true);
8439         if (!Diff.isUsable())
8440           return nullptr;
8441       }
8442     }
8443   }
8444 
8445   return Diff.get();
8446 }
8447 
8448 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
8449     Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
8450   // Do not build for iterators, they cannot be used in non-rectangular loop
8451   // nests.
8452   if (LCDecl->getType()->isRecordType())
8453     return std::make_pair(nullptr, nullptr);
8454   // If we subtract, the min is in the condition, otherwise the min is in the
8455   // init value.
8456   Expr *MinExpr = nullptr;
8457   Expr *MaxExpr = nullptr;
8458   Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
8459   Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
8460   bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue()
8461                                            : CondDependOnLC.hasValue();
8462   bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue()
8463                                            : InitDependOnLC.hasValue();
8464   Expr *Lower =
8465       LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get();
8466   Expr *Upper =
8467       UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get();
8468   if (!Upper || !Lower)
8469     return std::make_pair(nullptr, nullptr);
8470 
8471   if (TestIsLessOp.getValue())
8472     MinExpr = Lower;
8473   else
8474     MaxExpr = Upper;
8475 
8476   // Build minimum/maximum value based on number of iterations.
8477   QualType VarType = LCDecl->getType().getNonReferenceType();
8478 
8479   ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper,
8480                                       Step, VarType, TestIsStrictOp,
8481                                       /*RoundToStep=*/false, Captures);
8482   if (!Diff.isUsable())
8483     return std::make_pair(nullptr, nullptr);
8484 
8485   // ((Upper - Lower [- 1]) / Step) * Step
8486   // Parentheses (for dumping/debugging purposes only).
8487   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
8488   if (!Diff.isUsable())
8489     return std::make_pair(nullptr, nullptr);
8490 
8491   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
8492   if (!NewStep.isUsable())
8493     return std::make_pair(nullptr, nullptr);
8494   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get());
8495   if (!Diff.isUsable())
8496     return std::make_pair(nullptr, nullptr);
8497 
8498   // Parentheses (for dumping/debugging purposes only).
8499   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
8500   if (!Diff.isUsable())
8501     return std::make_pair(nullptr, nullptr);
8502 
8503   // Convert to the ptrdiff_t, if original type is pointer.
8504   if (VarType->isAnyPointerType() &&
8505       !SemaRef.Context.hasSameType(
8506           Diff.get()->getType(),
8507           SemaRef.Context.getUnsignedPointerDiffType())) {
8508     Diff = SemaRef.PerformImplicitConversion(
8509         Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(),
8510         Sema::AA_Converting, /*AllowExplicit=*/true);
8511   }
8512   if (!Diff.isUsable())
8513     return std::make_pair(nullptr, nullptr);
8514 
8515   if (TestIsLessOp.getValue()) {
8516     // MinExpr = Lower;
8517     // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
8518     Diff = SemaRef.BuildBinOp(
8519         S, DefaultLoc, BO_Add,
8520         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(),
8521         Diff.get());
8522     if (!Diff.isUsable())
8523       return std::make_pair(nullptr, nullptr);
8524   } else {
8525     // MaxExpr = Upper;
8526     // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
8527     Diff = SemaRef.BuildBinOp(
8528         S, DefaultLoc, BO_Sub,
8529         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(),
8530         Diff.get());
8531     if (!Diff.isUsable())
8532       return std::make_pair(nullptr, nullptr);
8533   }
8534 
8535   // Convert to the original type.
8536   if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType))
8537     Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType,
8538                                              Sema::AA_Converting,
8539                                              /*AllowExplicit=*/true);
8540   if (!Diff.isUsable())
8541     return std::make_pair(nullptr, nullptr);
8542 
8543   Sema::TentativeAnalysisScope Trap(SemaRef);
8544   Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false);
8545   if (!Diff.isUsable())
8546     return std::make_pair(nullptr, nullptr);
8547 
8548   if (TestIsLessOp.getValue())
8549     MaxExpr = Diff.get();
8550   else
8551     MinExpr = Diff.get();
8552 
8553   return std::make_pair(MinExpr, MaxExpr);
8554 }
8555 
8556 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
8557   if (InitDependOnLC || CondDependOnLC)
8558     return Condition;
8559   return nullptr;
8560 }
8561 
8562 Expr *OpenMPIterationSpaceChecker::buildPreCond(
8563     Scope *S, Expr *Cond,
8564     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
8565   // Do not build a precondition when the condition/initialization is dependent
8566   // to prevent pessimistic early loop exit.
8567   // TODO: this can be improved by calculating min/max values but not sure that
8568   // it will be very effective.
8569   if (CondDependOnLC || InitDependOnLC)
8570     return SemaRef
8571         .PerformImplicitConversion(
8572             SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(),
8573             SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
8574             /*AllowExplicit=*/true)
8575         .get();
8576 
8577   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
8578   Sema::TentativeAnalysisScope Trap(SemaRef);
8579 
8580   ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
8581   ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
8582   if (!NewLB.isUsable() || !NewUB.isUsable())
8583     return nullptr;
8584 
8585   ExprResult CondExpr = SemaRef.BuildBinOp(
8586       S, DefaultLoc,
8587       TestIsLessOp.getValue() ? (TestIsStrictOp ? BO_LT : BO_LE)
8588                               : (TestIsStrictOp ? BO_GT : BO_GE),
8589       NewLB.get(), NewUB.get());
8590   if (CondExpr.isUsable()) {
8591     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
8592                                                 SemaRef.Context.BoolTy))
8593       CondExpr = SemaRef.PerformImplicitConversion(
8594           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
8595           /*AllowExplicit=*/true);
8596   }
8597 
8598   // Otherwise use original loop condition and evaluate it in runtime.
8599   return CondExpr.isUsable() ? CondExpr.get() : Cond;
8600 }
8601 
8602 /// Build reference expression to the counter be used for codegen.
8603 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
8604     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8605     DSAStackTy &DSA) const {
8606   auto *VD = dyn_cast<VarDecl>(LCDecl);
8607   if (!VD) {
8608     VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
8609     DeclRefExpr *Ref = buildDeclRefExpr(
8610         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
8611     const DSAStackTy::DSAVarData Data =
8612         DSA.getTopDSA(LCDecl, /*FromParent=*/false);
8613     // If the loop control decl is explicitly marked as private, do not mark it
8614     // as captured again.
8615     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
8616       Captures.insert(std::make_pair(LCRef, Ref));
8617     return Ref;
8618   }
8619   return cast<DeclRefExpr>(LCRef);
8620 }
8621 
8622 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
8623   if (LCDecl && !LCDecl->isInvalidDecl()) {
8624     QualType Type = LCDecl->getType().getNonReferenceType();
8625     VarDecl *PrivateVar = buildVarDecl(
8626         SemaRef, DefaultLoc, Type, LCDecl->getName(),
8627         LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
8628         isa<VarDecl>(LCDecl)
8629             ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
8630             : nullptr);
8631     if (PrivateVar->isInvalidDecl())
8632       return nullptr;
8633     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
8634   }
8635   return nullptr;
8636 }
8637 
8638 /// Build initialization of the counter to be used for codegen.
8639 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
8640 
8641 /// Build step of the counter be used for codegen.
8642 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
8643 
8644 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
8645     Scope *S, Expr *Counter,
8646     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
8647     Expr *Inc, OverloadedOperatorKind OOK) {
8648   Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
8649   if (!Cnt)
8650     return nullptr;
8651   if (Inc) {
8652     assert((OOK == OO_Plus || OOK == OO_Minus) &&
8653            "Expected only + or - operations for depend clauses.");
8654     BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
8655     Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
8656     if (!Cnt)
8657       return nullptr;
8658   }
8659   QualType VarType = LCDecl->getType().getNonReferenceType();
8660   if (!VarType->isIntegerType() && !VarType->isPointerType() &&
8661       !SemaRef.getLangOpts().CPlusPlus)
8662     return nullptr;
8663   // Upper - Lower
8664   Expr *Upper = TestIsLessOp.getValue()
8665                     ? Cnt
8666                     : tryBuildCapture(SemaRef, LB, Captures).get();
8667   Expr *Lower = TestIsLessOp.getValue()
8668                     ? tryBuildCapture(SemaRef, LB, Captures).get()
8669                     : Cnt;
8670   if (!Upper || !Lower)
8671     return nullptr;
8672 
8673   ExprResult Diff = calculateNumIters(
8674       SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType,
8675       /*TestIsStrictOp=*/false, /*RoundToStep=*/false, Captures);
8676   if (!Diff.isUsable())
8677     return nullptr;
8678 
8679   return Diff.get();
8680 }
8681 } // namespace
8682 
8683 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
8684   assert(getLangOpts().OpenMP && "OpenMP is not active.");
8685   assert(Init && "Expected loop in canonical form.");
8686   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
8687   if (AssociatedLoops > 0 &&
8688       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
8689     DSAStack->loopStart();
8690     OpenMPIterationSpaceChecker ISC(*this, /*SupportsNonRectangular=*/true,
8691                                     *DSAStack, ForLoc);
8692     if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
8693       if (ValueDecl *D = ISC.getLoopDecl()) {
8694         auto *VD = dyn_cast<VarDecl>(D);
8695         DeclRefExpr *PrivateRef = nullptr;
8696         if (!VD) {
8697           if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
8698             VD = Private;
8699           } else {
8700             PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
8701                                       /*WithInit=*/false);
8702             VD = cast<VarDecl>(PrivateRef->getDecl());
8703           }
8704         }
8705         DSAStack->addLoopControlVariable(D, VD);
8706         const Decl *LD = DSAStack->getPossiblyLoopCunter();
8707         if (LD != D->getCanonicalDecl()) {
8708           DSAStack->resetPossibleLoopCounter();
8709           if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
8710             MarkDeclarationsReferencedInExpr(
8711                 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
8712                                  Var->getType().getNonLValueExprType(Context),
8713                                  ForLoc, /*RefersToCapture=*/true));
8714         }
8715         OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8716         // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
8717         // Referenced in a Construct, C/C++]. The loop iteration variable in the
8718         // associated for-loop of a simd construct with just one associated
8719         // for-loop may be listed in a linear clause with a constant-linear-step
8720         // that is the increment of the associated for-loop. The loop iteration
8721         // variable(s) in the associated for-loop(s) of a for or parallel for
8722         // construct may be listed in a private or lastprivate clause.
8723         DSAStackTy::DSAVarData DVar =
8724             DSAStack->getTopDSA(D, /*FromParent=*/false);
8725         // If LoopVarRefExpr is nullptr it means the corresponding loop variable
8726         // is declared in the loop and it is predetermined as a private.
8727         Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
8728         OpenMPClauseKind PredeterminedCKind =
8729             isOpenMPSimdDirective(DKind)
8730                 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
8731                 : OMPC_private;
8732         if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
8733               DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
8734               (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate &&
8735                                          DVar.CKind != OMPC_private))) ||
8736              ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
8737                DKind == OMPD_master_taskloop ||
8738                DKind == OMPD_parallel_master_taskloop ||
8739                isOpenMPDistributeDirective(DKind)) &&
8740               !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
8741               DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
8742             (DVar.CKind != OMPC_private || DVar.RefExpr)) {
8743           Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
8744               << getOpenMPClauseName(DVar.CKind)
8745               << getOpenMPDirectiveName(DKind)
8746               << getOpenMPClauseName(PredeterminedCKind);
8747           if (DVar.RefExpr == nullptr)
8748             DVar.CKind = PredeterminedCKind;
8749           reportOriginalDsa(*this, DSAStack, D, DVar,
8750                             /*IsLoopIterVar=*/true);
8751         } else if (LoopDeclRefExpr) {
8752           // Make the loop iteration variable private (for worksharing
8753           // constructs), linear (for simd directives with the only one
8754           // associated loop) or lastprivate (for simd directives with several
8755           // collapsed or ordered loops).
8756           if (DVar.CKind == OMPC_unknown)
8757             DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind,
8758                              PrivateRef);
8759         }
8760       }
8761     }
8762     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
8763   }
8764 }
8765 
8766 /// Called on a for stmt to check and extract its iteration space
8767 /// for further processing (such as collapsing).
8768 static bool checkOpenMPIterationSpace(
8769     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
8770     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
8771     unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
8772     Expr *OrderedLoopCountExpr,
8773     Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
8774     llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
8775     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
8776   bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind);
8777   // OpenMP [2.9.1, Canonical Loop Form]
8778   //   for (init-expr; test-expr; incr-expr) structured-block
8779   //   for (range-decl: range-expr) structured-block
8780   if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(S))
8781     S = CanonLoop->getLoopStmt();
8782   auto *For = dyn_cast_or_null<ForStmt>(S);
8783   auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S);
8784   // Ranged for is supported only in OpenMP 5.0.
8785   if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) {
8786     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
8787         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
8788         << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
8789         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
8790     if (TotalNestedLoopCount > 1) {
8791       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
8792         SemaRef.Diag(DSA.getConstructLoc(),
8793                      diag::note_omp_collapse_ordered_expr)
8794             << 2 << CollapseLoopCountExpr->getSourceRange()
8795             << OrderedLoopCountExpr->getSourceRange();
8796       else if (CollapseLoopCountExpr)
8797         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
8798                      diag::note_omp_collapse_ordered_expr)
8799             << 0 << CollapseLoopCountExpr->getSourceRange();
8800       else
8801         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
8802                      diag::note_omp_collapse_ordered_expr)
8803             << 1 << OrderedLoopCountExpr->getSourceRange();
8804     }
8805     return true;
8806   }
8807   assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) &&
8808          "No loop body.");
8809   // Postpone analysis in dependent contexts for ranged for loops.
8810   if (CXXFor && SemaRef.CurContext->isDependentContext())
8811     return false;
8812 
8813   OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA,
8814                                   For ? For->getForLoc() : CXXFor->getForLoc());
8815 
8816   // Check init.
8817   Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt();
8818   if (ISC.checkAndSetInit(Init))
8819     return true;
8820 
8821   bool HasErrors = false;
8822 
8823   // Check loop variable's type.
8824   if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
8825     // OpenMP [2.6, Canonical Loop Form]
8826     // Var is one of the following:
8827     //   A variable of signed or unsigned integer type.
8828     //   For C++, a variable of a random access iterator type.
8829     //   For C, a variable of a pointer type.
8830     QualType VarType = LCDecl->getType().getNonReferenceType();
8831     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
8832         !VarType->isPointerType() &&
8833         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
8834       SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
8835           << SemaRef.getLangOpts().CPlusPlus;
8836       HasErrors = true;
8837     }
8838 
8839     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
8840     // a Construct
8841     // The loop iteration variable(s) in the associated for-loop(s) of a for or
8842     // parallel for construct is (are) private.
8843     // The loop iteration variable in the associated for-loop of a simd
8844     // construct with just one associated for-loop is linear with a
8845     // constant-linear-step that is the increment of the associated for-loop.
8846     // Exclude loop var from the list of variables with implicitly defined data
8847     // sharing attributes.
8848     VarsWithImplicitDSA.erase(LCDecl);
8849 
8850     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
8851 
8852     // Check test-expr.
8853     HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond());
8854 
8855     // Check incr-expr.
8856     HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc());
8857   }
8858 
8859   if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
8860     return HasErrors;
8861 
8862   // Build the loop's iteration space representation.
8863   ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond(
8864       DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures);
8865   ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
8866       ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces,
8867                              (isOpenMPWorksharingDirective(DKind) ||
8868                               isOpenMPGenericLoopDirective(DKind) ||
8869                               isOpenMPTaskLoopDirective(DKind) ||
8870                               isOpenMPDistributeDirective(DKind) ||
8871                               isOpenMPLoopTransformationDirective(DKind)),
8872                              Captures);
8873   ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
8874       ISC.buildCounterVar(Captures, DSA);
8875   ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
8876       ISC.buildPrivateCounterVar();
8877   ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
8878   ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
8879   ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
8880   ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
8881       ISC.getConditionSrcRange();
8882   ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
8883       ISC.getIncrementSrcRange();
8884   ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
8885   ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
8886       ISC.isStrictTestOp();
8887   std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue,
8888            ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
8889       ISC.buildMinMaxValues(DSA.getCurScope(), Captures);
8890   ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
8891       ISC.buildFinalCondition(DSA.getCurScope());
8892   ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
8893       ISC.doesInitDependOnLC();
8894   ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
8895       ISC.doesCondDependOnLC();
8896   ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
8897       ISC.getLoopDependentIdx();
8898 
8899   HasErrors |=
8900       (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
8901        ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
8902        ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
8903        ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
8904        ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
8905        ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
8906   if (!HasErrors && DSA.isOrderedRegion()) {
8907     if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
8908       if (CurrentNestedLoopCount <
8909           DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
8910         DSA.getOrderedRegionParam().second->setLoopNumIterations(
8911             CurrentNestedLoopCount,
8912             ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
8913         DSA.getOrderedRegionParam().second->setLoopCounter(
8914             CurrentNestedLoopCount,
8915             ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
8916       }
8917     }
8918     for (auto &Pair : DSA.getDoacrossDependClauses()) {
8919       if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
8920         // Erroneous case - clause has some problems.
8921         continue;
8922       }
8923       if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
8924           Pair.second.size() <= CurrentNestedLoopCount) {
8925         // Erroneous case - clause has some problems.
8926         Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
8927         continue;
8928       }
8929       Expr *CntValue;
8930       if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
8931         CntValue = ISC.buildOrderedLoopData(
8932             DSA.getCurScope(),
8933             ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
8934             Pair.first->getDependencyLoc());
8935       else
8936         CntValue = ISC.buildOrderedLoopData(
8937             DSA.getCurScope(),
8938             ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
8939             Pair.first->getDependencyLoc(),
8940             Pair.second[CurrentNestedLoopCount].first,
8941             Pair.second[CurrentNestedLoopCount].second);
8942       Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
8943     }
8944   }
8945 
8946   return HasErrors;
8947 }
8948 
8949 /// Build 'VarRef = Start.
8950 static ExprResult
8951 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
8952                  ExprResult Start, bool IsNonRectangularLB,
8953                  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
8954   // Build 'VarRef = Start.
8955   ExprResult NewStart = IsNonRectangularLB
8956                             ? Start.get()
8957                             : tryBuildCapture(SemaRef, Start.get(), Captures);
8958   if (!NewStart.isUsable())
8959     return ExprError();
8960   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
8961                                    VarRef.get()->getType())) {
8962     NewStart = SemaRef.PerformImplicitConversion(
8963         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
8964         /*AllowExplicit=*/true);
8965     if (!NewStart.isUsable())
8966       return ExprError();
8967   }
8968 
8969   ExprResult Init =
8970       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
8971   return Init;
8972 }
8973 
8974 /// Build 'VarRef = Start + Iter * Step'.
8975 static ExprResult buildCounterUpdate(
8976     Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
8977     ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
8978     bool IsNonRectangularLB,
8979     llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
8980   // Add parentheses (for debugging purposes only).
8981   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
8982   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
8983       !Step.isUsable())
8984     return ExprError();
8985 
8986   ExprResult NewStep = Step;
8987   if (Captures)
8988     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
8989   if (NewStep.isInvalid())
8990     return ExprError();
8991   ExprResult Update =
8992       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
8993   if (!Update.isUsable())
8994     return ExprError();
8995 
8996   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
8997   // 'VarRef = Start (+|-) Iter * Step'.
8998   if (!Start.isUsable())
8999     return ExprError();
9000   ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get());
9001   if (!NewStart.isUsable())
9002     return ExprError();
9003   if (Captures && !IsNonRectangularLB)
9004     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
9005   if (NewStart.isInvalid())
9006     return ExprError();
9007 
9008   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
9009   ExprResult SavedUpdate = Update;
9010   ExprResult UpdateVal;
9011   if (VarRef.get()->getType()->isOverloadableType() ||
9012       NewStart.get()->getType()->isOverloadableType() ||
9013       Update.get()->getType()->isOverloadableType()) {
9014     Sema::TentativeAnalysisScope Trap(SemaRef);
9015 
9016     Update =
9017         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
9018     if (Update.isUsable()) {
9019       UpdateVal =
9020           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
9021                              VarRef.get(), SavedUpdate.get());
9022       if (UpdateVal.isUsable()) {
9023         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
9024                                             UpdateVal.get());
9025       }
9026     }
9027   }
9028 
9029   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
9030   if (!Update.isUsable() || !UpdateVal.isUsable()) {
9031     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
9032                                 NewStart.get(), SavedUpdate.get());
9033     if (!Update.isUsable())
9034       return ExprError();
9035 
9036     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
9037                                      VarRef.get()->getType())) {
9038       Update = SemaRef.PerformImplicitConversion(
9039           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
9040       if (!Update.isUsable())
9041         return ExprError();
9042     }
9043 
9044     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
9045   }
9046   return Update;
9047 }
9048 
9049 /// Convert integer expression \a E to make it have at least \a Bits
9050 /// bits.
9051 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
9052   if (E == nullptr)
9053     return ExprError();
9054   ASTContext &C = SemaRef.Context;
9055   QualType OldType = E->getType();
9056   unsigned HasBits = C.getTypeSize(OldType);
9057   if (HasBits >= Bits)
9058     return ExprResult(E);
9059   // OK to convert to signed, because new type has more bits than old.
9060   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
9061   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
9062                                            true);
9063 }
9064 
9065 /// Check if the given expression \a E is a constant integer that fits
9066 /// into \a Bits bits.
9067 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
9068   if (E == nullptr)
9069     return false;
9070   if (Optional<llvm::APSInt> Result =
9071           E->getIntegerConstantExpr(SemaRef.Context))
9072     return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits);
9073   return false;
9074 }
9075 
9076 /// Build preinits statement for the given declarations.
9077 static Stmt *buildPreInits(ASTContext &Context,
9078                            MutableArrayRef<Decl *> PreInits) {
9079   if (!PreInits.empty()) {
9080     return new (Context) DeclStmt(
9081         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
9082         SourceLocation(), SourceLocation());
9083   }
9084   return nullptr;
9085 }
9086 
9087 /// Build preinits statement for the given declarations.
9088 static Stmt *
9089 buildPreInits(ASTContext &Context,
9090               const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
9091   if (!Captures.empty()) {
9092     SmallVector<Decl *, 16> PreInits;
9093     for (const auto &Pair : Captures)
9094       PreInits.push_back(Pair.second->getDecl());
9095     return buildPreInits(Context, PreInits);
9096   }
9097   return nullptr;
9098 }
9099 
9100 /// Build postupdate expression for the given list of postupdates expressions.
9101 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
9102   Expr *PostUpdate = nullptr;
9103   if (!PostUpdates.empty()) {
9104     for (Expr *E : PostUpdates) {
9105       Expr *ConvE = S.BuildCStyleCastExpr(
9106                          E->getExprLoc(),
9107                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
9108                          E->getExprLoc(), E)
9109                         .get();
9110       PostUpdate = PostUpdate
9111                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
9112                                               PostUpdate, ConvE)
9113                              .get()
9114                        : ConvE;
9115     }
9116   }
9117   return PostUpdate;
9118 }
9119 
9120 /// Called on a for stmt to check itself and nested loops (if any).
9121 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
9122 /// number of collapsed loops otherwise.
9123 static unsigned
9124 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
9125                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
9126                 DSAStackTy &DSA,
9127                 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
9128                 OMPLoopBasedDirective::HelperExprs &Built) {
9129   unsigned NestedLoopCount = 1;
9130   bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) &&
9131                                     !isOpenMPLoopTransformationDirective(DKind);
9132 
9133   if (CollapseLoopCountExpr) {
9134     // Found 'collapse' clause - calculate collapse number.
9135     Expr::EvalResult Result;
9136     if (!CollapseLoopCountExpr->isValueDependent() &&
9137         CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
9138       NestedLoopCount = Result.Val.getInt().getLimitedValue();
9139     } else {
9140       Built.clear(/*Size=*/1);
9141       return 1;
9142     }
9143   }
9144   unsigned OrderedLoopCount = 1;
9145   if (OrderedLoopCountExpr) {
9146     // Found 'ordered' clause - calculate collapse number.
9147     Expr::EvalResult EVResult;
9148     if (!OrderedLoopCountExpr->isValueDependent() &&
9149         OrderedLoopCountExpr->EvaluateAsInt(EVResult,
9150                                             SemaRef.getASTContext())) {
9151       llvm::APSInt Result = EVResult.Val.getInt();
9152       if (Result.getLimitedValue() < NestedLoopCount) {
9153         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
9154                      diag::err_omp_wrong_ordered_loop_count)
9155             << OrderedLoopCountExpr->getSourceRange();
9156         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
9157                      diag::note_collapse_loop_count)
9158             << CollapseLoopCountExpr->getSourceRange();
9159       }
9160       OrderedLoopCount = Result.getLimitedValue();
9161     } else {
9162       Built.clear(/*Size=*/1);
9163       return 1;
9164     }
9165   }
9166   // This is helper routine for loop directives (e.g., 'for', 'simd',
9167   // 'for simd', etc.).
9168   llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
9169   unsigned NumLoops = std::max(OrderedLoopCount, NestedLoopCount);
9170   SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops);
9171   if (!OMPLoopBasedDirective::doForAllLoops(
9172           AStmt->IgnoreContainers(!isOpenMPLoopTransformationDirective(DKind)),
9173           SupportsNonPerfectlyNested, NumLoops,
9174           [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount,
9175            CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA,
9176            &IterSpaces, &Captures](unsigned Cnt, Stmt *CurStmt) {
9177             if (checkOpenMPIterationSpace(
9178                     DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
9179                     NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr,
9180                     VarsWithImplicitDSA, IterSpaces, Captures))
9181               return true;
9182             if (Cnt > 0 && Cnt >= NestedLoopCount &&
9183                 IterSpaces[Cnt].CounterVar) {
9184               // Handle initialization of captured loop iterator variables.
9185               auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
9186               if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
9187                 Captures[DRE] = DRE;
9188               }
9189             }
9190             return false;
9191           },
9192           [&SemaRef, &Captures](OMPLoopTransformationDirective *Transform) {
9193             Stmt *DependentPreInits = Transform->getPreInits();
9194             if (!DependentPreInits)
9195               return;
9196             for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) {
9197               auto *D = cast<VarDecl>(C);
9198               DeclRefExpr *Ref = buildDeclRefExpr(SemaRef, D, D->getType(),
9199                                                   Transform->getBeginLoc());
9200               Captures[Ref] = Ref;
9201             }
9202           }))
9203     return 0;
9204 
9205   Built.clear(/* size */ NestedLoopCount);
9206 
9207   if (SemaRef.CurContext->isDependentContext())
9208     return NestedLoopCount;
9209 
9210   // An example of what is generated for the following code:
9211   //
9212   //   #pragma omp simd collapse(2) ordered(2)
9213   //   for (i = 0; i < NI; ++i)
9214   //     for (k = 0; k < NK; ++k)
9215   //       for (j = J0; j < NJ; j+=2) {
9216   //         <loop body>
9217   //       }
9218   //
9219   // We generate the code below.
9220   // Note: the loop body may be outlined in CodeGen.
9221   // Note: some counters may be C++ classes, operator- is used to find number of
9222   // iterations and operator+= to calculate counter value.
9223   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
9224   // or i64 is currently supported).
9225   //
9226   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
9227   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
9228   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
9229   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
9230   //     // similar updates for vars in clauses (e.g. 'linear')
9231   //     <loop body (using local i and j)>
9232   //   }
9233   //   i = NI; // assign final values of counters
9234   //   j = NJ;
9235   //
9236 
9237   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
9238   // the iteration counts of the collapsed for loops.
9239   // Precondition tests if there is at least one iteration (all conditions are
9240   // true).
9241   auto PreCond = ExprResult(IterSpaces[0].PreCond);
9242   Expr *N0 = IterSpaces[0].NumIterations;
9243   ExprResult LastIteration32 =
9244       widenIterationCount(/*Bits=*/32,
9245                           SemaRef
9246                               .PerformImplicitConversion(
9247                                   N0->IgnoreImpCasts(), N0->getType(),
9248                                   Sema::AA_Converting, /*AllowExplicit=*/true)
9249                               .get(),
9250                           SemaRef);
9251   ExprResult LastIteration64 = widenIterationCount(
9252       /*Bits=*/64,
9253       SemaRef
9254           .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
9255                                      Sema::AA_Converting,
9256                                      /*AllowExplicit=*/true)
9257           .get(),
9258       SemaRef);
9259 
9260   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
9261     return NestedLoopCount;
9262 
9263   ASTContext &C = SemaRef.Context;
9264   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
9265 
9266   Scope *CurScope = DSA.getCurScope();
9267   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
9268     if (PreCond.isUsable()) {
9269       PreCond =
9270           SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
9271                              PreCond.get(), IterSpaces[Cnt].PreCond);
9272     }
9273     Expr *N = IterSpaces[Cnt].NumIterations;
9274     SourceLocation Loc = N->getExprLoc();
9275     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
9276     if (LastIteration32.isUsable())
9277       LastIteration32 = SemaRef.BuildBinOp(
9278           CurScope, Loc, BO_Mul, LastIteration32.get(),
9279           SemaRef
9280               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
9281                                          Sema::AA_Converting,
9282                                          /*AllowExplicit=*/true)
9283               .get());
9284     if (LastIteration64.isUsable())
9285       LastIteration64 = SemaRef.BuildBinOp(
9286           CurScope, Loc, BO_Mul, LastIteration64.get(),
9287           SemaRef
9288               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
9289                                          Sema::AA_Converting,
9290                                          /*AllowExplicit=*/true)
9291               .get());
9292   }
9293 
9294   // Choose either the 32-bit or 64-bit version.
9295   ExprResult LastIteration = LastIteration64;
9296   if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
9297       (LastIteration32.isUsable() &&
9298        C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
9299        (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
9300         fitsInto(
9301             /*Bits=*/32,
9302             LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
9303             LastIteration64.get(), SemaRef))))
9304     LastIteration = LastIteration32;
9305   QualType VType = LastIteration.get()->getType();
9306   QualType RealVType = VType;
9307   QualType StrideVType = VType;
9308   if (isOpenMPTaskLoopDirective(DKind)) {
9309     VType =
9310         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
9311     StrideVType =
9312         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
9313   }
9314 
9315   if (!LastIteration.isUsable())
9316     return 0;
9317 
9318   // Save the number of iterations.
9319   ExprResult NumIterations = LastIteration;
9320   {
9321     LastIteration = SemaRef.BuildBinOp(
9322         CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
9323         LastIteration.get(),
9324         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
9325     if (!LastIteration.isUsable())
9326       return 0;
9327   }
9328 
9329   // Calculate the last iteration number beforehand instead of doing this on
9330   // each iteration. Do not do this if the number of iterations may be kfold-ed.
9331   bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context);
9332   ExprResult CalcLastIteration;
9333   if (!IsConstant) {
9334     ExprResult SaveRef =
9335         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
9336     LastIteration = SaveRef;
9337 
9338     // Prepare SaveRef + 1.
9339     NumIterations = SemaRef.BuildBinOp(
9340         CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
9341         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
9342     if (!NumIterations.isUsable())
9343       return 0;
9344   }
9345 
9346   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
9347 
9348   // Build variables passed into runtime, necessary for worksharing directives.
9349   ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
9350   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
9351       isOpenMPDistributeDirective(DKind) ||
9352       isOpenMPGenericLoopDirective(DKind) ||
9353       isOpenMPLoopTransformationDirective(DKind)) {
9354     // Lower bound variable, initialized with zero.
9355     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
9356     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
9357     SemaRef.AddInitializerToDecl(LBDecl,
9358                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
9359                                  /*DirectInit*/ false);
9360 
9361     // Upper bound variable, initialized with last iteration number.
9362     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
9363     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
9364     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
9365                                  /*DirectInit*/ false);
9366 
9367     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
9368     // This will be used to implement clause 'lastprivate'.
9369     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
9370     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
9371     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
9372     SemaRef.AddInitializerToDecl(ILDecl,
9373                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
9374                                  /*DirectInit*/ false);
9375 
9376     // Stride variable returned by runtime (we initialize it to 1 by default).
9377     VarDecl *STDecl =
9378         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
9379     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
9380     SemaRef.AddInitializerToDecl(STDecl,
9381                                  SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
9382                                  /*DirectInit*/ false);
9383 
9384     // Build expression: UB = min(UB, LastIteration)
9385     // It is necessary for CodeGen of directives with static scheduling.
9386     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
9387                                                 UB.get(), LastIteration.get());
9388     ExprResult CondOp = SemaRef.ActOnConditionalOp(
9389         LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
9390         LastIteration.get(), UB.get());
9391     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
9392                              CondOp.get());
9393     EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
9394 
9395     // If we have a combined directive that combines 'distribute', 'for' or
9396     // 'simd' we need to be able to access the bounds of the schedule of the
9397     // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
9398     // by scheduling 'distribute' have to be passed to the schedule of 'for'.
9399     if (isOpenMPLoopBoundSharingDirective(DKind)) {
9400       // Lower bound variable, initialized with zero.
9401       VarDecl *CombLBDecl =
9402           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
9403       CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
9404       SemaRef.AddInitializerToDecl(
9405           CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
9406           /*DirectInit*/ false);
9407 
9408       // Upper bound variable, initialized with last iteration number.
9409       VarDecl *CombUBDecl =
9410           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
9411       CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
9412       SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
9413                                    /*DirectInit*/ false);
9414 
9415       ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
9416           CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
9417       ExprResult CombCondOp =
9418           SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
9419                                      LastIteration.get(), CombUB.get());
9420       CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
9421                                    CombCondOp.get());
9422       CombEUB =
9423           SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
9424 
9425       const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
9426       // We expect to have at least 2 more parameters than the 'parallel'
9427       // directive does - the lower and upper bounds of the previous schedule.
9428       assert(CD->getNumParams() >= 4 &&
9429              "Unexpected number of parameters in loop combined directive");
9430 
9431       // Set the proper type for the bounds given what we learned from the
9432       // enclosed loops.
9433       ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
9434       ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
9435 
9436       // Previous lower and upper bounds are obtained from the region
9437       // parameters.
9438       PrevLB =
9439           buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
9440       PrevUB =
9441           buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
9442     }
9443   }
9444 
9445   // Build the iteration variable and its initialization before loop.
9446   ExprResult IV;
9447   ExprResult Init, CombInit;
9448   {
9449     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
9450     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
9451     Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
9452                  isOpenMPGenericLoopDirective(DKind) ||
9453                  isOpenMPTaskLoopDirective(DKind) ||
9454                  isOpenMPDistributeDirective(DKind) ||
9455                  isOpenMPLoopTransformationDirective(DKind))
9456                     ? LB.get()
9457                     : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
9458     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
9459     Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
9460 
9461     if (isOpenMPLoopBoundSharingDirective(DKind)) {
9462       Expr *CombRHS =
9463           (isOpenMPWorksharingDirective(DKind) ||
9464            isOpenMPGenericLoopDirective(DKind) ||
9465            isOpenMPTaskLoopDirective(DKind) ||
9466            isOpenMPDistributeDirective(DKind))
9467               ? CombLB.get()
9468               : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
9469       CombInit =
9470           SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
9471       CombInit =
9472           SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
9473     }
9474   }
9475 
9476   bool UseStrictCompare =
9477       RealVType->hasUnsignedIntegerRepresentation() &&
9478       llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
9479         return LIS.IsStrictCompare;
9480       });
9481   // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
9482   // unsigned IV)) for worksharing loops.
9483   SourceLocation CondLoc = AStmt->getBeginLoc();
9484   Expr *BoundUB = UB.get();
9485   if (UseStrictCompare) {
9486     BoundUB =
9487         SemaRef
9488             .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
9489                         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
9490             .get();
9491     BoundUB =
9492         SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
9493   }
9494   ExprResult Cond =
9495       (isOpenMPWorksharingDirective(DKind) ||
9496        isOpenMPGenericLoopDirective(DKind) ||
9497        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) ||
9498        isOpenMPLoopTransformationDirective(DKind))
9499           ? SemaRef.BuildBinOp(CurScope, CondLoc,
9500                                UseStrictCompare ? BO_LT : BO_LE, IV.get(),
9501                                BoundUB)
9502           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
9503                                NumIterations.get());
9504   ExprResult CombDistCond;
9505   if (isOpenMPLoopBoundSharingDirective(DKind)) {
9506     CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
9507                                       NumIterations.get());
9508   }
9509 
9510   ExprResult CombCond;
9511   if (isOpenMPLoopBoundSharingDirective(DKind)) {
9512     Expr *BoundCombUB = CombUB.get();
9513     if (UseStrictCompare) {
9514       BoundCombUB =
9515           SemaRef
9516               .BuildBinOp(
9517                   CurScope, CondLoc, BO_Add, BoundCombUB,
9518                   SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
9519               .get();
9520       BoundCombUB =
9521           SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
9522               .get();
9523     }
9524     CombCond =
9525         SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
9526                            IV.get(), BoundCombUB);
9527   }
9528   // Loop increment (IV = IV + 1)
9529   SourceLocation IncLoc = AStmt->getBeginLoc();
9530   ExprResult Inc =
9531       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
9532                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
9533   if (!Inc.isUsable())
9534     return 0;
9535   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
9536   Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
9537   if (!Inc.isUsable())
9538     return 0;
9539 
9540   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
9541   // Used for directives with static scheduling.
9542   // In combined construct, add combined version that use CombLB and CombUB
9543   // base variables for the update
9544   ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
9545   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
9546       isOpenMPGenericLoopDirective(DKind) ||
9547       isOpenMPDistributeDirective(DKind) ||
9548       isOpenMPLoopTransformationDirective(DKind)) {
9549     // LB + ST
9550     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
9551     if (!NextLB.isUsable())
9552       return 0;
9553     // LB = LB + ST
9554     NextLB =
9555         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
9556     NextLB =
9557         SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
9558     if (!NextLB.isUsable())
9559       return 0;
9560     // UB + ST
9561     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
9562     if (!NextUB.isUsable())
9563       return 0;
9564     // UB = UB + ST
9565     NextUB =
9566         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
9567     NextUB =
9568         SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
9569     if (!NextUB.isUsable())
9570       return 0;
9571     if (isOpenMPLoopBoundSharingDirective(DKind)) {
9572       CombNextLB =
9573           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
9574       if (!NextLB.isUsable())
9575         return 0;
9576       // LB = LB + ST
9577       CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
9578                                       CombNextLB.get());
9579       CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
9580                                                /*DiscardedValue*/ false);
9581       if (!CombNextLB.isUsable())
9582         return 0;
9583       // UB + ST
9584       CombNextUB =
9585           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
9586       if (!CombNextUB.isUsable())
9587         return 0;
9588       // UB = UB + ST
9589       CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
9590                                       CombNextUB.get());
9591       CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
9592                                                /*DiscardedValue*/ false);
9593       if (!CombNextUB.isUsable())
9594         return 0;
9595     }
9596   }
9597 
9598   // Create increment expression for distribute loop when combined in a same
9599   // directive with for as IV = IV + ST; ensure upper bound expression based
9600   // on PrevUB instead of NumIterations - used to implement 'for' when found
9601   // in combination with 'distribute', like in 'distribute parallel for'
9602   SourceLocation DistIncLoc = AStmt->getBeginLoc();
9603   ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
9604   if (isOpenMPLoopBoundSharingDirective(DKind)) {
9605     DistCond = SemaRef.BuildBinOp(
9606         CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
9607     assert(DistCond.isUsable() && "distribute cond expr was not built");
9608 
9609     DistInc =
9610         SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
9611     assert(DistInc.isUsable() && "distribute inc expr was not built");
9612     DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
9613                                  DistInc.get());
9614     DistInc =
9615         SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
9616     assert(DistInc.isUsable() && "distribute inc expr was not built");
9617 
9618     // Build expression: UB = min(UB, prevUB) for #for in composite or combined
9619     // construct
9620     ExprResult NewPrevUB = PrevUB;
9621     SourceLocation DistEUBLoc = AStmt->getBeginLoc();
9622     if (!SemaRef.Context.hasSameType(UB.get()->getType(),
9623                                      PrevUB.get()->getType())) {
9624       NewPrevUB = SemaRef.BuildCStyleCastExpr(
9625           DistEUBLoc,
9626           SemaRef.Context.getTrivialTypeSourceInfo(UB.get()->getType()),
9627           DistEUBLoc, NewPrevUB.get());
9628       if (!NewPrevUB.isUsable())
9629         return 0;
9630     }
9631     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT,
9632                                                 UB.get(), NewPrevUB.get());
9633     ExprResult CondOp = SemaRef.ActOnConditionalOp(
9634         DistEUBLoc, DistEUBLoc, IsUBGreater.get(), NewPrevUB.get(), UB.get());
9635     PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
9636                                  CondOp.get());
9637     PrevEUB =
9638         SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
9639 
9640     // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
9641     // parallel for is in combination with a distribute directive with
9642     // schedule(static, 1)
9643     Expr *BoundPrevUB = PrevUB.get();
9644     if (UseStrictCompare) {
9645       BoundPrevUB =
9646           SemaRef
9647               .BuildBinOp(
9648                   CurScope, CondLoc, BO_Add, BoundPrevUB,
9649                   SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
9650               .get();
9651       BoundPrevUB =
9652           SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
9653               .get();
9654     }
9655     ParForInDistCond =
9656         SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
9657                            IV.get(), BoundPrevUB);
9658   }
9659 
9660   // Build updates and final values of the loop counters.
9661   bool HasErrors = false;
9662   Built.Counters.resize(NestedLoopCount);
9663   Built.Inits.resize(NestedLoopCount);
9664   Built.Updates.resize(NestedLoopCount);
9665   Built.Finals.resize(NestedLoopCount);
9666   Built.DependentCounters.resize(NestedLoopCount);
9667   Built.DependentInits.resize(NestedLoopCount);
9668   Built.FinalsConditions.resize(NestedLoopCount);
9669   {
9670     // We implement the following algorithm for obtaining the
9671     // original loop iteration variable values based on the
9672     // value of the collapsed loop iteration variable IV.
9673     //
9674     // Let n+1 be the number of collapsed loops in the nest.
9675     // Iteration variables (I0, I1, .... In)
9676     // Iteration counts (N0, N1, ... Nn)
9677     //
9678     // Acc = IV;
9679     //
9680     // To compute Ik for loop k, 0 <= k <= n, generate:
9681     //    Prod = N(k+1) * N(k+2) * ... * Nn;
9682     //    Ik = Acc / Prod;
9683     //    Acc -= Ik * Prod;
9684     //
9685     ExprResult Acc = IV;
9686     for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
9687       LoopIterationSpace &IS = IterSpaces[Cnt];
9688       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
9689       ExprResult Iter;
9690 
9691       // Compute prod
9692       ExprResult Prod = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
9693       for (unsigned int K = Cnt + 1; K < NestedLoopCount; ++K)
9694         Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
9695                                   IterSpaces[K].NumIterations);
9696 
9697       // Iter = Acc / Prod
9698       // If there is at least one more inner loop to avoid
9699       // multiplication by 1.
9700       if (Cnt + 1 < NestedLoopCount)
9701         Iter =
9702             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, Acc.get(), Prod.get());
9703       else
9704         Iter = Acc;
9705       if (!Iter.isUsable()) {
9706         HasErrors = true;
9707         break;
9708       }
9709 
9710       // Update Acc:
9711       // Acc -= Iter * Prod
9712       // Check if there is at least one more inner loop to avoid
9713       // multiplication by 1.
9714       if (Cnt + 1 < NestedLoopCount)
9715         Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Iter.get(),
9716                                   Prod.get());
9717       else
9718         Prod = Iter;
9719       Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, Acc.get(), Prod.get());
9720 
9721       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
9722       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
9723       DeclRefExpr *CounterVar = buildDeclRefExpr(
9724           SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
9725           /*RefersToCapture=*/true);
9726       ExprResult Init =
9727           buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
9728                            IS.CounterInit, IS.IsNonRectangularLB, Captures);
9729       if (!Init.isUsable()) {
9730         HasErrors = true;
9731         break;
9732       }
9733       ExprResult Update = buildCounterUpdate(
9734           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
9735           IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures);
9736       if (!Update.isUsable()) {
9737         HasErrors = true;
9738         break;
9739       }
9740 
9741       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
9742       ExprResult Final =
9743           buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
9744                              IS.CounterInit, IS.NumIterations, IS.CounterStep,
9745                              IS.Subtract, IS.IsNonRectangularLB, &Captures);
9746       if (!Final.isUsable()) {
9747         HasErrors = true;
9748         break;
9749       }
9750 
9751       if (!Update.isUsable() || !Final.isUsable()) {
9752         HasErrors = true;
9753         break;
9754       }
9755       // Save results
9756       Built.Counters[Cnt] = IS.CounterVar;
9757       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
9758       Built.Inits[Cnt] = Init.get();
9759       Built.Updates[Cnt] = Update.get();
9760       Built.Finals[Cnt] = Final.get();
9761       Built.DependentCounters[Cnt] = nullptr;
9762       Built.DependentInits[Cnt] = nullptr;
9763       Built.FinalsConditions[Cnt] = nullptr;
9764       if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) {
9765         Built.DependentCounters[Cnt] =
9766             Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx];
9767         Built.DependentInits[Cnt] =
9768             Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx];
9769         Built.FinalsConditions[Cnt] = IS.FinalCondition;
9770       }
9771     }
9772   }
9773 
9774   if (HasErrors)
9775     return 0;
9776 
9777   // Save results
9778   Built.IterationVarRef = IV.get();
9779   Built.LastIteration = LastIteration.get();
9780   Built.NumIterations = NumIterations.get();
9781   Built.CalcLastIteration = SemaRef
9782                                 .ActOnFinishFullExpr(CalcLastIteration.get(),
9783                                                      /*DiscardedValue=*/false)
9784                                 .get();
9785   Built.PreCond = PreCond.get();
9786   Built.PreInits = buildPreInits(C, Captures);
9787   Built.Cond = Cond.get();
9788   Built.Init = Init.get();
9789   Built.Inc = Inc.get();
9790   Built.LB = LB.get();
9791   Built.UB = UB.get();
9792   Built.IL = IL.get();
9793   Built.ST = ST.get();
9794   Built.EUB = EUB.get();
9795   Built.NLB = NextLB.get();
9796   Built.NUB = NextUB.get();
9797   Built.PrevLB = PrevLB.get();
9798   Built.PrevUB = PrevUB.get();
9799   Built.DistInc = DistInc.get();
9800   Built.PrevEUB = PrevEUB.get();
9801   Built.DistCombinedFields.LB = CombLB.get();
9802   Built.DistCombinedFields.UB = CombUB.get();
9803   Built.DistCombinedFields.EUB = CombEUB.get();
9804   Built.DistCombinedFields.Init = CombInit.get();
9805   Built.DistCombinedFields.Cond = CombCond.get();
9806   Built.DistCombinedFields.NLB = CombNextLB.get();
9807   Built.DistCombinedFields.NUB = CombNextUB.get();
9808   Built.DistCombinedFields.DistCond = CombDistCond.get();
9809   Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
9810 
9811   return NestedLoopCount;
9812 }
9813 
9814 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
9815   auto CollapseClauses =
9816       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
9817   if (CollapseClauses.begin() != CollapseClauses.end())
9818     return (*CollapseClauses.begin())->getNumForLoops();
9819   return nullptr;
9820 }
9821 
9822 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
9823   auto OrderedClauses =
9824       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
9825   if (OrderedClauses.begin() != OrderedClauses.end())
9826     return (*OrderedClauses.begin())->getNumForLoops();
9827   return nullptr;
9828 }
9829 
9830 static bool checkSimdlenSafelenSpecified(Sema &S,
9831                                          const ArrayRef<OMPClause *> Clauses) {
9832   const OMPSafelenClause *Safelen = nullptr;
9833   const OMPSimdlenClause *Simdlen = nullptr;
9834 
9835   for (const OMPClause *Clause : Clauses) {
9836     if (Clause->getClauseKind() == OMPC_safelen)
9837       Safelen = cast<OMPSafelenClause>(Clause);
9838     else if (Clause->getClauseKind() == OMPC_simdlen)
9839       Simdlen = cast<OMPSimdlenClause>(Clause);
9840     if (Safelen && Simdlen)
9841       break;
9842   }
9843 
9844   if (Simdlen && Safelen) {
9845     const Expr *SimdlenLength = Simdlen->getSimdlen();
9846     const Expr *SafelenLength = Safelen->getSafelen();
9847     if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
9848         SimdlenLength->isInstantiationDependent() ||
9849         SimdlenLength->containsUnexpandedParameterPack())
9850       return false;
9851     if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
9852         SafelenLength->isInstantiationDependent() ||
9853         SafelenLength->containsUnexpandedParameterPack())
9854       return false;
9855     Expr::EvalResult SimdlenResult, SafelenResult;
9856     SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
9857     SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
9858     llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
9859     llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
9860     // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
9861     // If both simdlen and safelen clauses are specified, the value of the
9862     // simdlen parameter must be less than or equal to the value of the safelen
9863     // parameter.
9864     if (SimdlenRes > SafelenRes) {
9865       S.Diag(SimdlenLength->getExprLoc(),
9866              diag::err_omp_wrong_simdlen_safelen_values)
9867           << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
9868       return true;
9869     }
9870   }
9871   return false;
9872 }
9873 
9874 StmtResult
9875 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
9876                                SourceLocation StartLoc, SourceLocation EndLoc,
9877                                VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9878   if (!AStmt)
9879     return StmtError();
9880 
9881   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9882   OMPLoopBasedDirective::HelperExprs B;
9883   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9884   // define the nested loops number.
9885   unsigned NestedLoopCount = checkOpenMPLoop(
9886       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
9887       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
9888   if (NestedLoopCount == 0)
9889     return StmtError();
9890 
9891   assert((CurContext->isDependentContext() || B.builtAll()) &&
9892          "omp simd loop exprs were not built");
9893 
9894   if (!CurContext->isDependentContext()) {
9895     // Finalize the clauses that need pre-built expressions for CodeGen.
9896     for (OMPClause *C : Clauses) {
9897       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9898         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9899                                      B.NumIterations, *this, CurScope,
9900                                      DSAStack))
9901           return StmtError();
9902     }
9903   }
9904 
9905   if (checkSimdlenSafelenSpecified(*this, Clauses))
9906     return StmtError();
9907 
9908   setFunctionHasBranchProtectedScope();
9909   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
9910                                   Clauses, AStmt, B);
9911 }
9912 
9913 StmtResult
9914 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
9915                               SourceLocation StartLoc, SourceLocation EndLoc,
9916                               VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9917   if (!AStmt)
9918     return StmtError();
9919 
9920   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9921   OMPLoopBasedDirective::HelperExprs B;
9922   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9923   // define the nested loops number.
9924   unsigned NestedLoopCount = checkOpenMPLoop(
9925       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
9926       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
9927   if (NestedLoopCount == 0)
9928     return StmtError();
9929 
9930   assert((CurContext->isDependentContext() || B.builtAll()) &&
9931          "omp for loop exprs were not built");
9932 
9933   if (!CurContext->isDependentContext()) {
9934     // Finalize the clauses that need pre-built expressions for CodeGen.
9935     for (OMPClause *C : Clauses) {
9936       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9937         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9938                                      B.NumIterations, *this, CurScope,
9939                                      DSAStack))
9940           return StmtError();
9941     }
9942   }
9943 
9944   setFunctionHasBranchProtectedScope();
9945   return OMPForDirective::Create(
9946       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9947       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
9948 }
9949 
9950 StmtResult Sema::ActOnOpenMPForSimdDirective(
9951     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9952     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9953   if (!AStmt)
9954     return StmtError();
9955 
9956   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9957   OMPLoopBasedDirective::HelperExprs B;
9958   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9959   // define the nested loops number.
9960   unsigned NestedLoopCount =
9961       checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
9962                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
9963                       VarsWithImplicitDSA, B);
9964   if (NestedLoopCount == 0)
9965     return StmtError();
9966 
9967   assert((CurContext->isDependentContext() || B.builtAll()) &&
9968          "omp for simd loop exprs were not built");
9969 
9970   if (!CurContext->isDependentContext()) {
9971     // Finalize the clauses that need pre-built expressions for CodeGen.
9972     for (OMPClause *C : Clauses) {
9973       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9974         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9975                                      B.NumIterations, *this, CurScope,
9976                                      DSAStack))
9977           return StmtError();
9978     }
9979   }
9980 
9981   if (checkSimdlenSafelenSpecified(*this, Clauses))
9982     return StmtError();
9983 
9984   setFunctionHasBranchProtectedScope();
9985   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
9986                                      Clauses, AStmt, B);
9987 }
9988 
9989 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
9990                                               Stmt *AStmt,
9991                                               SourceLocation StartLoc,
9992                                               SourceLocation EndLoc) {
9993   if (!AStmt)
9994     return StmtError();
9995 
9996   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9997   auto BaseStmt = AStmt;
9998   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
9999     BaseStmt = CS->getCapturedStmt();
10000   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
10001     auto S = C->children();
10002     if (S.begin() == S.end())
10003       return StmtError();
10004     // All associated statements must be '#pragma omp section' except for
10005     // the first one.
10006     for (Stmt *SectionStmt : llvm::drop_begin(S)) {
10007       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
10008         if (SectionStmt)
10009           Diag(SectionStmt->getBeginLoc(),
10010                diag::err_omp_sections_substmt_not_section);
10011         return StmtError();
10012       }
10013       cast<OMPSectionDirective>(SectionStmt)
10014           ->setHasCancel(DSAStack->isCancelRegion());
10015     }
10016   } else {
10017     Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
10018     return StmtError();
10019   }
10020 
10021   setFunctionHasBranchProtectedScope();
10022 
10023   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
10024                                       DSAStack->getTaskgroupReductionRef(),
10025                                       DSAStack->isCancelRegion());
10026 }
10027 
10028 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
10029                                              SourceLocation StartLoc,
10030                                              SourceLocation EndLoc) {
10031   if (!AStmt)
10032     return StmtError();
10033 
10034   setFunctionHasBranchProtectedScope();
10035   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
10036 
10037   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
10038                                      DSAStack->isCancelRegion());
10039 }
10040 
10041 static Expr *getDirectCallExpr(Expr *E) {
10042   E = E->IgnoreParenCasts()->IgnoreImplicit();
10043   if (auto *CE = dyn_cast<CallExpr>(E))
10044     if (CE->getDirectCallee())
10045       return E;
10046   return nullptr;
10047 }
10048 
10049 StmtResult Sema::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses,
10050                                               Stmt *AStmt,
10051                                               SourceLocation StartLoc,
10052                                               SourceLocation EndLoc) {
10053   if (!AStmt)
10054     return StmtError();
10055 
10056   Stmt *S = cast<CapturedStmt>(AStmt)->getCapturedStmt();
10057 
10058   // 5.1 OpenMP
10059   // expression-stmt : an expression statement with one of the following forms:
10060   //   expression = target-call ( [expression-list] );
10061   //   target-call ( [expression-list] );
10062 
10063   SourceLocation TargetCallLoc;
10064 
10065   if (!CurContext->isDependentContext()) {
10066     Expr *TargetCall = nullptr;
10067 
10068     auto *E = dyn_cast<Expr>(S);
10069     if (!E) {
10070       Diag(S->getBeginLoc(), diag::err_omp_dispatch_statement_call);
10071       return StmtError();
10072     }
10073 
10074     E = E->IgnoreParenCasts()->IgnoreImplicit();
10075 
10076     if (auto *BO = dyn_cast<BinaryOperator>(E)) {
10077       if (BO->getOpcode() == BO_Assign)
10078         TargetCall = getDirectCallExpr(BO->getRHS());
10079     } else {
10080       if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(E))
10081         if (COCE->getOperator() == OO_Equal)
10082           TargetCall = getDirectCallExpr(COCE->getArg(1));
10083       if (!TargetCall)
10084         TargetCall = getDirectCallExpr(E);
10085     }
10086     if (!TargetCall) {
10087       Diag(E->getBeginLoc(), diag::err_omp_dispatch_statement_call);
10088       return StmtError();
10089     }
10090     TargetCallLoc = TargetCall->getExprLoc();
10091   }
10092 
10093   setFunctionHasBranchProtectedScope();
10094 
10095   return OMPDispatchDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
10096                                       TargetCallLoc);
10097 }
10098 
10099 StmtResult Sema::ActOnOpenMPGenericLoopDirective(
10100     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10101     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10102   if (!AStmt)
10103     return StmtError();
10104 
10105   // OpenMP 5.1 [2.11.7, loop construct]
10106   // A list item may not appear in a lastprivate clause unless it is the
10107   // loop iteration variable of a loop that is associated with the construct.
10108   for (OMPClause *C : Clauses) {
10109     if (auto *LPC = dyn_cast<OMPLastprivateClause>(C)) {
10110       for (Expr *RefExpr : LPC->varlists()) {
10111         SourceLocation ELoc;
10112         SourceRange ERange;
10113         Expr *SimpleRefExpr = RefExpr;
10114         auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
10115         if (ValueDecl *D = Res.first) {
10116           auto &&Info = DSAStack->isLoopControlVariable(D);
10117           if (!Info.first) {
10118             Diag(ELoc, diag::err_omp_lastprivate_loop_var_non_loop_iteration);
10119             return StmtError();
10120           }
10121         }
10122       }
10123     }
10124   }
10125 
10126   auto *CS = cast<CapturedStmt>(AStmt);
10127   // 1.2.2 OpenMP Language Terminology
10128   // Structured block - An executable statement with a single entry at the
10129   // top and a single exit at the bottom.
10130   // The point of exit cannot be a branch out of the structured block.
10131   // longjmp() and throw() must not violate the entry/exit criteria.
10132   CS->getCapturedDecl()->setNothrow();
10133 
10134   OMPLoopDirective::HelperExprs B;
10135   // In presence of clause 'collapse', it will define the nested loops number.
10136   unsigned NestedLoopCount = checkOpenMPLoop(
10137       OMPD_loop, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
10138       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
10139   if (NestedLoopCount == 0)
10140     return StmtError();
10141 
10142   assert((CurContext->isDependentContext() || B.builtAll()) &&
10143          "omp loop exprs were not built");
10144 
10145   setFunctionHasBranchProtectedScope();
10146   return OMPGenericLoopDirective::Create(Context, StartLoc, EndLoc,
10147                                          NestedLoopCount, Clauses, AStmt, B);
10148 }
10149 
10150 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
10151                                             Stmt *AStmt,
10152                                             SourceLocation StartLoc,
10153                                             SourceLocation EndLoc) {
10154   if (!AStmt)
10155     return StmtError();
10156 
10157   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10158 
10159   setFunctionHasBranchProtectedScope();
10160 
10161   // OpenMP [2.7.3, single Construct, Restrictions]
10162   // The copyprivate clause must not be used with the nowait clause.
10163   const OMPClause *Nowait = nullptr;
10164   const OMPClause *Copyprivate = nullptr;
10165   for (const OMPClause *Clause : Clauses) {
10166     if (Clause->getClauseKind() == OMPC_nowait)
10167       Nowait = Clause;
10168     else if (Clause->getClauseKind() == OMPC_copyprivate)
10169       Copyprivate = Clause;
10170     if (Copyprivate && Nowait) {
10171       Diag(Copyprivate->getBeginLoc(),
10172            diag::err_omp_single_copyprivate_with_nowait);
10173       Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
10174       return StmtError();
10175     }
10176   }
10177 
10178   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
10179 }
10180 
10181 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
10182                                             SourceLocation StartLoc,
10183                                             SourceLocation EndLoc) {
10184   if (!AStmt)
10185     return StmtError();
10186 
10187   setFunctionHasBranchProtectedScope();
10188 
10189   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
10190 }
10191 
10192 StmtResult Sema::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses,
10193                                             Stmt *AStmt,
10194                                             SourceLocation StartLoc,
10195                                             SourceLocation EndLoc) {
10196   if (!AStmt)
10197     return StmtError();
10198 
10199   setFunctionHasBranchProtectedScope();
10200 
10201   return OMPMaskedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
10202 }
10203 
10204 StmtResult Sema::ActOnOpenMPCriticalDirective(
10205     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
10206     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
10207   if (!AStmt)
10208     return StmtError();
10209 
10210   bool ErrorFound = false;
10211   llvm::APSInt Hint;
10212   SourceLocation HintLoc;
10213   bool DependentHint = false;
10214   for (const OMPClause *C : Clauses) {
10215     if (C->getClauseKind() == OMPC_hint) {
10216       if (!DirName.getName()) {
10217         Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
10218         ErrorFound = true;
10219       }
10220       Expr *E = cast<OMPHintClause>(C)->getHint();
10221       if (E->isTypeDependent() || E->isValueDependent() ||
10222           E->isInstantiationDependent()) {
10223         DependentHint = true;
10224       } else {
10225         Hint = E->EvaluateKnownConstInt(Context);
10226         HintLoc = C->getBeginLoc();
10227       }
10228     }
10229   }
10230   if (ErrorFound)
10231     return StmtError();
10232   const auto Pair = DSAStack->getCriticalWithHint(DirName);
10233   if (Pair.first && DirName.getName() && !DependentHint) {
10234     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
10235       Diag(StartLoc, diag::err_omp_critical_with_hint);
10236       if (HintLoc.isValid())
10237         Diag(HintLoc, diag::note_omp_critical_hint_here)
10238             << 0 << toString(Hint, /*Radix=*/10, /*Signed=*/false);
10239       else
10240         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
10241       if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
10242         Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
10243             << 1
10244             << toString(C->getHint()->EvaluateKnownConstInt(Context),
10245                         /*Radix=*/10, /*Signed=*/false);
10246       } else {
10247         Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
10248       }
10249     }
10250   }
10251 
10252   setFunctionHasBranchProtectedScope();
10253 
10254   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
10255                                            Clauses, AStmt);
10256   if (!Pair.first && DirName.getName() && !DependentHint)
10257     DSAStack->addCriticalWithHint(Dir, Hint);
10258   return Dir;
10259 }
10260 
10261 StmtResult Sema::ActOnOpenMPParallelForDirective(
10262     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10263     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10264   if (!AStmt)
10265     return StmtError();
10266 
10267   auto *CS = cast<CapturedStmt>(AStmt);
10268   // 1.2.2 OpenMP Language Terminology
10269   // Structured block - An executable statement with a single entry at the
10270   // top and a single exit at the bottom.
10271   // The point of exit cannot be a branch out of the structured block.
10272   // longjmp() and throw() must not violate the entry/exit criteria.
10273   CS->getCapturedDecl()->setNothrow();
10274 
10275   OMPLoopBasedDirective::HelperExprs B;
10276   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10277   // define the nested loops number.
10278   unsigned NestedLoopCount =
10279       checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
10280                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
10281                       VarsWithImplicitDSA, B);
10282   if (NestedLoopCount == 0)
10283     return StmtError();
10284 
10285   assert((CurContext->isDependentContext() || B.builtAll()) &&
10286          "omp parallel for loop exprs were not built");
10287 
10288   if (!CurContext->isDependentContext()) {
10289     // Finalize the clauses that need pre-built expressions for CodeGen.
10290     for (OMPClause *C : Clauses) {
10291       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10292         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10293                                      B.NumIterations, *this, CurScope,
10294                                      DSAStack))
10295           return StmtError();
10296     }
10297   }
10298 
10299   setFunctionHasBranchProtectedScope();
10300   return OMPParallelForDirective::Create(
10301       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
10302       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10303 }
10304 
10305 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
10306     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10307     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10308   if (!AStmt)
10309     return StmtError();
10310 
10311   auto *CS = cast<CapturedStmt>(AStmt);
10312   // 1.2.2 OpenMP Language Terminology
10313   // Structured block - An executable statement with a single entry at the
10314   // top and a single exit at the bottom.
10315   // The point of exit cannot be a branch out of the structured block.
10316   // longjmp() and throw() must not violate the entry/exit criteria.
10317   CS->getCapturedDecl()->setNothrow();
10318 
10319   OMPLoopBasedDirective::HelperExprs B;
10320   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10321   // define the nested loops number.
10322   unsigned NestedLoopCount =
10323       checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
10324                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
10325                       VarsWithImplicitDSA, B);
10326   if (NestedLoopCount == 0)
10327     return StmtError();
10328 
10329   if (!CurContext->isDependentContext()) {
10330     // Finalize the clauses that need pre-built expressions for CodeGen.
10331     for (OMPClause *C : Clauses) {
10332       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10333         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10334                                      B.NumIterations, *this, CurScope,
10335                                      DSAStack))
10336           return StmtError();
10337     }
10338   }
10339 
10340   if (checkSimdlenSafelenSpecified(*this, Clauses))
10341     return StmtError();
10342 
10343   setFunctionHasBranchProtectedScope();
10344   return OMPParallelForSimdDirective::Create(
10345       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10346 }
10347 
10348 StmtResult
10349 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses,
10350                                          Stmt *AStmt, SourceLocation StartLoc,
10351                                          SourceLocation EndLoc) {
10352   if (!AStmt)
10353     return StmtError();
10354 
10355   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10356   auto *CS = cast<CapturedStmt>(AStmt);
10357   // 1.2.2 OpenMP Language Terminology
10358   // Structured block - An executable statement with a single entry at the
10359   // top and a single exit at the bottom.
10360   // The point of exit cannot be a branch out of the structured block.
10361   // longjmp() and throw() must not violate the entry/exit criteria.
10362   CS->getCapturedDecl()->setNothrow();
10363 
10364   setFunctionHasBranchProtectedScope();
10365 
10366   return OMPParallelMasterDirective::Create(
10367       Context, StartLoc, EndLoc, Clauses, AStmt,
10368       DSAStack->getTaskgroupReductionRef());
10369 }
10370 
10371 StmtResult
10372 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
10373                                            Stmt *AStmt, SourceLocation StartLoc,
10374                                            SourceLocation EndLoc) {
10375   if (!AStmt)
10376     return StmtError();
10377 
10378   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10379   auto BaseStmt = AStmt;
10380   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
10381     BaseStmt = CS->getCapturedStmt();
10382   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
10383     auto S = C->children();
10384     if (S.begin() == S.end())
10385       return StmtError();
10386     // All associated statements must be '#pragma omp section' except for
10387     // the first one.
10388     for (Stmt *SectionStmt : llvm::drop_begin(S)) {
10389       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
10390         if (SectionStmt)
10391           Diag(SectionStmt->getBeginLoc(),
10392                diag::err_omp_parallel_sections_substmt_not_section);
10393         return StmtError();
10394       }
10395       cast<OMPSectionDirective>(SectionStmt)
10396           ->setHasCancel(DSAStack->isCancelRegion());
10397     }
10398   } else {
10399     Diag(AStmt->getBeginLoc(),
10400          diag::err_omp_parallel_sections_not_compound_stmt);
10401     return StmtError();
10402   }
10403 
10404   setFunctionHasBranchProtectedScope();
10405 
10406   return OMPParallelSectionsDirective::Create(
10407       Context, StartLoc, EndLoc, Clauses, AStmt,
10408       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10409 }
10410 
10411 /// Find and diagnose mutually exclusive clause kinds.
10412 static bool checkMutuallyExclusiveClauses(
10413     Sema &S, ArrayRef<OMPClause *> Clauses,
10414     ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) {
10415   const OMPClause *PrevClause = nullptr;
10416   bool ErrorFound = false;
10417   for (const OMPClause *C : Clauses) {
10418     if (llvm::is_contained(MutuallyExclusiveClauses, C->getClauseKind())) {
10419       if (!PrevClause) {
10420         PrevClause = C;
10421       } else if (PrevClause->getClauseKind() != C->getClauseKind()) {
10422         S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive)
10423             << getOpenMPClauseName(C->getClauseKind())
10424             << getOpenMPClauseName(PrevClause->getClauseKind());
10425         S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause)
10426             << getOpenMPClauseName(PrevClause->getClauseKind());
10427         ErrorFound = true;
10428       }
10429     }
10430   }
10431   return ErrorFound;
10432 }
10433 
10434 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
10435                                           Stmt *AStmt, SourceLocation StartLoc,
10436                                           SourceLocation EndLoc) {
10437   if (!AStmt)
10438     return StmtError();
10439 
10440   // OpenMP 5.0, 2.10.1 task Construct
10441   // If a detach clause appears on the directive, then a mergeable clause cannot
10442   // appear on the same directive.
10443   if (checkMutuallyExclusiveClauses(*this, Clauses,
10444                                     {OMPC_detach, OMPC_mergeable}))
10445     return StmtError();
10446 
10447   auto *CS = cast<CapturedStmt>(AStmt);
10448   // 1.2.2 OpenMP Language Terminology
10449   // Structured block - An executable statement with a single entry at the
10450   // top and a single exit at the bottom.
10451   // The point of exit cannot be a branch out of the structured block.
10452   // longjmp() and throw() must not violate the entry/exit criteria.
10453   CS->getCapturedDecl()->setNothrow();
10454 
10455   setFunctionHasBranchProtectedScope();
10456 
10457   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
10458                                   DSAStack->isCancelRegion());
10459 }
10460 
10461 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
10462                                                SourceLocation EndLoc) {
10463   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
10464 }
10465 
10466 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
10467                                              SourceLocation EndLoc) {
10468   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
10469 }
10470 
10471 StmtResult Sema::ActOnOpenMPTaskwaitDirective(ArrayRef<OMPClause *> Clauses,
10472                                               SourceLocation StartLoc,
10473                                               SourceLocation EndLoc) {
10474   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc, Clauses);
10475 }
10476 
10477 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
10478                                                Stmt *AStmt,
10479                                                SourceLocation StartLoc,
10480                                                SourceLocation EndLoc) {
10481   if (!AStmt)
10482     return StmtError();
10483 
10484   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10485 
10486   setFunctionHasBranchProtectedScope();
10487 
10488   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
10489                                        AStmt,
10490                                        DSAStack->getTaskgroupReductionRef());
10491 }
10492 
10493 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
10494                                            SourceLocation StartLoc,
10495                                            SourceLocation EndLoc) {
10496   OMPFlushClause *FC = nullptr;
10497   OMPClause *OrderClause = nullptr;
10498   for (OMPClause *C : Clauses) {
10499     if (C->getClauseKind() == OMPC_flush)
10500       FC = cast<OMPFlushClause>(C);
10501     else
10502       OrderClause = C;
10503   }
10504   OpenMPClauseKind MemOrderKind = OMPC_unknown;
10505   SourceLocation MemOrderLoc;
10506   for (const OMPClause *C : Clauses) {
10507     if (C->getClauseKind() == OMPC_acq_rel ||
10508         C->getClauseKind() == OMPC_acquire ||
10509         C->getClauseKind() == OMPC_release) {
10510       if (MemOrderKind != OMPC_unknown) {
10511         Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses)
10512             << getOpenMPDirectiveName(OMPD_flush) << 1
10513             << SourceRange(C->getBeginLoc(), C->getEndLoc());
10514         Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause)
10515             << getOpenMPClauseName(MemOrderKind);
10516       } else {
10517         MemOrderKind = C->getClauseKind();
10518         MemOrderLoc = C->getBeginLoc();
10519       }
10520     }
10521   }
10522   if (FC && OrderClause) {
10523     Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list)
10524         << getOpenMPClauseName(OrderClause->getClauseKind());
10525     Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here)
10526         << getOpenMPClauseName(OrderClause->getClauseKind());
10527     return StmtError();
10528   }
10529   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
10530 }
10531 
10532 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses,
10533                                             SourceLocation StartLoc,
10534                                             SourceLocation EndLoc) {
10535   if (Clauses.empty()) {
10536     Diag(StartLoc, diag::err_omp_depobj_expected);
10537     return StmtError();
10538   } else if (Clauses[0]->getClauseKind() != OMPC_depobj) {
10539     Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected);
10540     return StmtError();
10541   }
10542   // Only depobj expression and another single clause is allowed.
10543   if (Clauses.size() > 2) {
10544     Diag(Clauses[2]->getBeginLoc(),
10545          diag::err_omp_depobj_single_clause_expected);
10546     return StmtError();
10547   } else if (Clauses.size() < 1) {
10548     Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected);
10549     return StmtError();
10550   }
10551   return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses);
10552 }
10553 
10554 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses,
10555                                           SourceLocation StartLoc,
10556                                           SourceLocation EndLoc) {
10557   // Check that exactly one clause is specified.
10558   if (Clauses.size() != 1) {
10559     Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(),
10560          diag::err_omp_scan_single_clause_expected);
10561     return StmtError();
10562   }
10563   // Check that scan directive is used in the scopeof the OpenMP loop body.
10564   if (Scope *S = DSAStack->getCurScope()) {
10565     Scope *ParentS = S->getParent();
10566     if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() ||
10567         !ParentS->getBreakParent()->isOpenMPLoopScope())
10568       return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive)
10569                        << getOpenMPDirectiveName(OMPD_scan) << 5);
10570   }
10571   // Check that only one instance of scan directives is used in the same outer
10572   // region.
10573   if (DSAStack->doesParentHasScanDirective()) {
10574     Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan";
10575     Diag(DSAStack->getParentScanDirectiveLoc(),
10576          diag::note_omp_previous_directive)
10577         << "scan";
10578     return StmtError();
10579   }
10580   DSAStack->setParentHasScanDirective(StartLoc);
10581   return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses);
10582 }
10583 
10584 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
10585                                              Stmt *AStmt,
10586                                              SourceLocation StartLoc,
10587                                              SourceLocation EndLoc) {
10588   const OMPClause *DependFound = nullptr;
10589   const OMPClause *DependSourceClause = nullptr;
10590   const OMPClause *DependSinkClause = nullptr;
10591   bool ErrorFound = false;
10592   const OMPThreadsClause *TC = nullptr;
10593   const OMPSIMDClause *SC = nullptr;
10594   for (const OMPClause *C : Clauses) {
10595     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
10596       DependFound = C;
10597       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
10598         if (DependSourceClause) {
10599           Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
10600               << getOpenMPDirectiveName(OMPD_ordered)
10601               << getOpenMPClauseName(OMPC_depend) << 2;
10602           ErrorFound = true;
10603         } else {
10604           DependSourceClause = C;
10605         }
10606         if (DependSinkClause) {
10607           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
10608               << 0;
10609           ErrorFound = true;
10610         }
10611       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
10612         if (DependSourceClause) {
10613           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
10614               << 1;
10615           ErrorFound = true;
10616         }
10617         DependSinkClause = C;
10618       }
10619     } else if (C->getClauseKind() == OMPC_threads) {
10620       TC = cast<OMPThreadsClause>(C);
10621     } else if (C->getClauseKind() == OMPC_simd) {
10622       SC = cast<OMPSIMDClause>(C);
10623     }
10624   }
10625   if (!ErrorFound && !SC &&
10626       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
10627     // OpenMP [2.8.1,simd Construct, Restrictions]
10628     // An ordered construct with the simd clause is the only OpenMP construct
10629     // that can appear in the simd region.
10630     Diag(StartLoc, diag::err_omp_prohibited_region_simd)
10631         << (LangOpts.OpenMP >= 50 ? 1 : 0);
10632     ErrorFound = true;
10633   } else if (DependFound && (TC || SC)) {
10634     Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
10635         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
10636     ErrorFound = true;
10637   } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
10638     Diag(DependFound->getBeginLoc(),
10639          diag::err_omp_ordered_directive_without_param);
10640     ErrorFound = true;
10641   } else if (TC || Clauses.empty()) {
10642     if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
10643       SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
10644       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
10645           << (TC != nullptr);
10646       Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1;
10647       ErrorFound = true;
10648     }
10649   }
10650   if ((!AStmt && !DependFound) || ErrorFound)
10651     return StmtError();
10652 
10653   // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions.
10654   // During execution of an iteration of a worksharing-loop or a loop nest
10655   // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread
10656   // must not execute more than one ordered region corresponding to an ordered
10657   // construct without a depend clause.
10658   if (!DependFound) {
10659     if (DSAStack->doesParentHasOrderedDirective()) {
10660       Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered";
10661       Diag(DSAStack->getParentOrderedDirectiveLoc(),
10662            diag::note_omp_previous_directive)
10663           << "ordered";
10664       return StmtError();
10665     }
10666     DSAStack->setParentHasOrderedDirective(StartLoc);
10667   }
10668 
10669   if (AStmt) {
10670     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10671 
10672     setFunctionHasBranchProtectedScope();
10673   }
10674 
10675   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
10676 }
10677 
10678 namespace {
10679 /// Helper class for checking expression in 'omp atomic [update]'
10680 /// construct.
10681 class OpenMPAtomicUpdateChecker {
10682   /// Error results for atomic update expressions.
10683   enum ExprAnalysisErrorCode {
10684     /// A statement is not an expression statement.
10685     NotAnExpression,
10686     /// Expression is not builtin binary or unary operation.
10687     NotABinaryOrUnaryExpression,
10688     /// Unary operation is not post-/pre- increment/decrement operation.
10689     NotAnUnaryIncDecExpression,
10690     /// An expression is not of scalar type.
10691     NotAScalarType,
10692     /// A binary operation is not an assignment operation.
10693     NotAnAssignmentOp,
10694     /// RHS part of the binary operation is not a binary expression.
10695     NotABinaryExpression,
10696     /// RHS part is not additive/multiplicative/shift/biwise binary
10697     /// expression.
10698     NotABinaryOperator,
10699     /// RHS binary operation does not have reference to the updated LHS
10700     /// part.
10701     NotAnUpdateExpression,
10702     /// No errors is found.
10703     NoError
10704   };
10705   /// Reference to Sema.
10706   Sema &SemaRef;
10707   /// A location for note diagnostics (when error is found).
10708   SourceLocation NoteLoc;
10709   /// 'x' lvalue part of the source atomic expression.
10710   Expr *X;
10711   /// 'expr' rvalue part of the source atomic expression.
10712   Expr *E;
10713   /// Helper expression of the form
10714   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
10715   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
10716   Expr *UpdateExpr;
10717   /// Is 'x' a LHS in a RHS part of full update expression. It is
10718   /// important for non-associative operations.
10719   bool IsXLHSInRHSPart;
10720   BinaryOperatorKind Op;
10721   SourceLocation OpLoc;
10722   /// true if the source expression is a postfix unary operation, false
10723   /// if it is a prefix unary operation.
10724   bool IsPostfixUpdate;
10725 
10726 public:
10727   OpenMPAtomicUpdateChecker(Sema &SemaRef)
10728       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
10729         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
10730   /// Check specified statement that it is suitable for 'atomic update'
10731   /// constructs and extract 'x', 'expr' and Operation from the original
10732   /// expression. If DiagId and NoteId == 0, then only check is performed
10733   /// without error notification.
10734   /// \param DiagId Diagnostic which should be emitted if error is found.
10735   /// \param NoteId Diagnostic note for the main error message.
10736   /// \return true if statement is not an update expression, false otherwise.
10737   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
10738   /// Return the 'x' lvalue part of the source atomic expression.
10739   Expr *getX() const { return X; }
10740   /// Return the 'expr' rvalue part of the source atomic expression.
10741   Expr *getExpr() const { return E; }
10742   /// Return the update expression used in calculation of the updated
10743   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
10744   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
10745   Expr *getUpdateExpr() const { return UpdateExpr; }
10746   /// Return true if 'x' is LHS in RHS part of full update expression,
10747   /// false otherwise.
10748   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
10749 
10750   /// true if the source expression is a postfix unary operation, false
10751   /// if it is a prefix unary operation.
10752   bool isPostfixUpdate() const { return IsPostfixUpdate; }
10753 
10754 private:
10755   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
10756                             unsigned NoteId = 0);
10757 };
10758 
10759 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
10760     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
10761   ExprAnalysisErrorCode ErrorFound = NoError;
10762   SourceLocation ErrorLoc, NoteLoc;
10763   SourceRange ErrorRange, NoteRange;
10764   // Allowed constructs are:
10765   //  x = x binop expr;
10766   //  x = expr binop x;
10767   if (AtomicBinOp->getOpcode() == BO_Assign) {
10768     X = AtomicBinOp->getLHS();
10769     if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
10770             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
10771       if (AtomicInnerBinOp->isMultiplicativeOp() ||
10772           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
10773           AtomicInnerBinOp->isBitwiseOp()) {
10774         Op = AtomicInnerBinOp->getOpcode();
10775         OpLoc = AtomicInnerBinOp->getOperatorLoc();
10776         Expr *LHS = AtomicInnerBinOp->getLHS();
10777         Expr *RHS = AtomicInnerBinOp->getRHS();
10778         llvm::FoldingSetNodeID XId, LHSId, RHSId;
10779         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
10780                                           /*Canonical=*/true);
10781         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
10782                                             /*Canonical=*/true);
10783         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
10784                                             /*Canonical=*/true);
10785         if (XId == LHSId) {
10786           E = RHS;
10787           IsXLHSInRHSPart = true;
10788         } else if (XId == RHSId) {
10789           E = LHS;
10790           IsXLHSInRHSPart = false;
10791         } else {
10792           ErrorLoc = AtomicInnerBinOp->getExprLoc();
10793           ErrorRange = AtomicInnerBinOp->getSourceRange();
10794           NoteLoc = X->getExprLoc();
10795           NoteRange = X->getSourceRange();
10796           ErrorFound = NotAnUpdateExpression;
10797         }
10798       } else {
10799         ErrorLoc = AtomicInnerBinOp->getExprLoc();
10800         ErrorRange = AtomicInnerBinOp->getSourceRange();
10801         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
10802         NoteRange = SourceRange(NoteLoc, NoteLoc);
10803         ErrorFound = NotABinaryOperator;
10804       }
10805     } else {
10806       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
10807       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
10808       ErrorFound = NotABinaryExpression;
10809     }
10810   } else {
10811     ErrorLoc = AtomicBinOp->getExprLoc();
10812     ErrorRange = AtomicBinOp->getSourceRange();
10813     NoteLoc = AtomicBinOp->getOperatorLoc();
10814     NoteRange = SourceRange(NoteLoc, NoteLoc);
10815     ErrorFound = NotAnAssignmentOp;
10816   }
10817   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
10818     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
10819     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
10820     return true;
10821   }
10822   if (SemaRef.CurContext->isDependentContext())
10823     E = X = UpdateExpr = nullptr;
10824   return ErrorFound != NoError;
10825 }
10826 
10827 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
10828                                                unsigned NoteId) {
10829   ExprAnalysisErrorCode ErrorFound = NoError;
10830   SourceLocation ErrorLoc, NoteLoc;
10831   SourceRange ErrorRange, NoteRange;
10832   // Allowed constructs are:
10833   //  x++;
10834   //  x--;
10835   //  ++x;
10836   //  --x;
10837   //  x binop= expr;
10838   //  x = x binop expr;
10839   //  x = expr binop x;
10840   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
10841     AtomicBody = AtomicBody->IgnoreParenImpCasts();
10842     if (AtomicBody->getType()->isScalarType() ||
10843         AtomicBody->isInstantiationDependent()) {
10844       if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
10845               AtomicBody->IgnoreParenImpCasts())) {
10846         // Check for Compound Assignment Operation
10847         Op = BinaryOperator::getOpForCompoundAssignment(
10848             AtomicCompAssignOp->getOpcode());
10849         OpLoc = AtomicCompAssignOp->getOperatorLoc();
10850         E = AtomicCompAssignOp->getRHS();
10851         X = AtomicCompAssignOp->getLHS()->IgnoreParens();
10852         IsXLHSInRHSPart = true;
10853       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
10854                      AtomicBody->IgnoreParenImpCasts())) {
10855         // Check for Binary Operation
10856         if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
10857           return true;
10858       } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
10859                      AtomicBody->IgnoreParenImpCasts())) {
10860         // Check for Unary Operation
10861         if (AtomicUnaryOp->isIncrementDecrementOp()) {
10862           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
10863           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
10864           OpLoc = AtomicUnaryOp->getOperatorLoc();
10865           X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
10866           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
10867           IsXLHSInRHSPart = true;
10868         } else {
10869           ErrorFound = NotAnUnaryIncDecExpression;
10870           ErrorLoc = AtomicUnaryOp->getExprLoc();
10871           ErrorRange = AtomicUnaryOp->getSourceRange();
10872           NoteLoc = AtomicUnaryOp->getOperatorLoc();
10873           NoteRange = SourceRange(NoteLoc, NoteLoc);
10874         }
10875       } else if (!AtomicBody->isInstantiationDependent()) {
10876         ErrorFound = NotABinaryOrUnaryExpression;
10877         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
10878         NoteRange = ErrorRange = AtomicBody->getSourceRange();
10879       }
10880     } else {
10881       ErrorFound = NotAScalarType;
10882       NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
10883       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
10884     }
10885   } else {
10886     ErrorFound = NotAnExpression;
10887     NoteLoc = ErrorLoc = S->getBeginLoc();
10888     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
10889   }
10890   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
10891     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
10892     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
10893     return true;
10894   }
10895   if (SemaRef.CurContext->isDependentContext())
10896     E = X = UpdateExpr = nullptr;
10897   if (ErrorFound == NoError && E && X) {
10898     // Build an update expression of form 'OpaqueValueExpr(x) binop
10899     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
10900     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
10901     auto *OVEX = new (SemaRef.getASTContext())
10902         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue);
10903     auto *OVEExpr = new (SemaRef.getASTContext())
10904         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue);
10905     ExprResult Update =
10906         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
10907                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
10908     if (Update.isInvalid())
10909       return true;
10910     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
10911                                                Sema::AA_Casting);
10912     if (Update.isInvalid())
10913       return true;
10914     UpdateExpr = Update.get();
10915   }
10916   return ErrorFound != NoError;
10917 }
10918 } // namespace
10919 
10920 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
10921                                             Stmt *AStmt,
10922                                             SourceLocation StartLoc,
10923                                             SourceLocation EndLoc) {
10924   // Register location of the first atomic directive.
10925   DSAStack->addAtomicDirectiveLoc(StartLoc);
10926   if (!AStmt)
10927     return StmtError();
10928 
10929   // 1.2.2 OpenMP Language Terminology
10930   // Structured block - An executable statement with a single entry at the
10931   // top and a single exit at the bottom.
10932   // The point of exit cannot be a branch out of the structured block.
10933   // longjmp() and throw() must not violate the entry/exit criteria.
10934   OpenMPClauseKind AtomicKind = OMPC_unknown;
10935   SourceLocation AtomicKindLoc;
10936   OpenMPClauseKind MemOrderKind = OMPC_unknown;
10937   SourceLocation MemOrderLoc;
10938   for (const OMPClause *C : Clauses) {
10939     switch (C->getClauseKind()) {
10940     case OMPC_read:
10941     case OMPC_write:
10942     case OMPC_update:
10943     case OMPC_capture:
10944     case OMPC_compare: {
10945       if (AtomicKind != OMPC_unknown) {
10946         Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
10947             << SourceRange(C->getBeginLoc(), C->getEndLoc());
10948         Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause)
10949             << getOpenMPClauseName(AtomicKind);
10950       } else {
10951         AtomicKind = C->getClauseKind();
10952         AtomicKindLoc = C->getBeginLoc();
10953       }
10954       break;
10955     }
10956     case OMPC_seq_cst:
10957     case OMPC_acq_rel:
10958     case OMPC_acquire:
10959     case OMPC_release:
10960     case OMPC_relaxed: {
10961       if (MemOrderKind != OMPC_unknown) {
10962         Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses)
10963             << getOpenMPDirectiveName(OMPD_atomic) << 0
10964             << SourceRange(C->getBeginLoc(), C->getEndLoc());
10965         Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause)
10966             << getOpenMPClauseName(MemOrderKind);
10967       } else {
10968         MemOrderKind = C->getClauseKind();
10969         MemOrderLoc = C->getBeginLoc();
10970       }
10971       break;
10972     }
10973     // The following clauses are allowed, but we don't need to do anything here.
10974     case OMPC_hint:
10975       break;
10976     default:
10977       llvm_unreachable("unknown clause is encountered");
10978     }
10979   }
10980   // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions
10981   // If atomic-clause is read then memory-order-clause must not be acq_rel or
10982   // release.
10983   // If atomic-clause is write then memory-order-clause must not be acq_rel or
10984   // acquire.
10985   // If atomic-clause is update or not present then memory-order-clause must not
10986   // be acq_rel or acquire.
10987   if ((AtomicKind == OMPC_read &&
10988        (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) ||
10989       ((AtomicKind == OMPC_write || AtomicKind == OMPC_update ||
10990         AtomicKind == OMPC_unknown) &&
10991        (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) {
10992     SourceLocation Loc = AtomicKindLoc;
10993     if (AtomicKind == OMPC_unknown)
10994       Loc = StartLoc;
10995     Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause)
10996         << getOpenMPClauseName(AtomicKind)
10997         << (AtomicKind == OMPC_unknown ? 1 : 0)
10998         << getOpenMPClauseName(MemOrderKind);
10999     Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause)
11000         << getOpenMPClauseName(MemOrderKind);
11001   }
11002 
11003   Stmt *Body = AStmt;
11004   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
11005     Body = EWC->getSubExpr();
11006 
11007   Expr *X = nullptr;
11008   Expr *V = nullptr;
11009   Expr *E = nullptr;
11010   Expr *UE = nullptr;
11011   bool IsXLHSInRHSPart = false;
11012   bool IsPostfixUpdate = false;
11013   // OpenMP [2.12.6, atomic Construct]
11014   // In the next expressions:
11015   // * x and v (as applicable) are both l-value expressions with scalar type.
11016   // * During the execution of an atomic region, multiple syntactic
11017   // occurrences of x must designate the same storage location.
11018   // * Neither of v and expr (as applicable) may access the storage location
11019   // designated by x.
11020   // * Neither of x and expr (as applicable) may access the storage location
11021   // designated by v.
11022   // * expr is an expression with scalar type.
11023   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
11024   // * binop, binop=, ++, and -- are not overloaded operators.
11025   // * The expression x binop expr must be numerically equivalent to x binop
11026   // (expr). This requirement is satisfied if the operators in expr have
11027   // precedence greater than binop, or by using parentheses around expr or
11028   // subexpressions of expr.
11029   // * The expression expr binop x must be numerically equivalent to (expr)
11030   // binop x. This requirement is satisfied if the operators in expr have
11031   // precedence equal to or greater than binop, or by using parentheses around
11032   // expr or subexpressions of expr.
11033   // * For forms that allow multiple occurrences of x, the number of times
11034   // that x is evaluated is unspecified.
11035   if (AtomicKind == OMPC_read) {
11036     enum {
11037       NotAnExpression,
11038       NotAnAssignmentOp,
11039       NotAScalarType,
11040       NotAnLValue,
11041       NoError
11042     } ErrorFound = NoError;
11043     SourceLocation ErrorLoc, NoteLoc;
11044     SourceRange ErrorRange, NoteRange;
11045     // If clause is read:
11046     //  v = x;
11047     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
11048       const auto *AtomicBinOp =
11049           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
11050       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
11051         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
11052         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
11053         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
11054             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
11055           if (!X->isLValue() || !V->isLValue()) {
11056             const Expr *NotLValueExpr = X->isLValue() ? V : X;
11057             ErrorFound = NotAnLValue;
11058             ErrorLoc = AtomicBinOp->getExprLoc();
11059             ErrorRange = AtomicBinOp->getSourceRange();
11060             NoteLoc = NotLValueExpr->getExprLoc();
11061             NoteRange = NotLValueExpr->getSourceRange();
11062           }
11063         } else if (!X->isInstantiationDependent() ||
11064                    !V->isInstantiationDependent()) {
11065           const Expr *NotScalarExpr =
11066               (X->isInstantiationDependent() || X->getType()->isScalarType())
11067                   ? V
11068                   : X;
11069           ErrorFound = NotAScalarType;
11070           ErrorLoc = AtomicBinOp->getExprLoc();
11071           ErrorRange = AtomicBinOp->getSourceRange();
11072           NoteLoc = NotScalarExpr->getExprLoc();
11073           NoteRange = NotScalarExpr->getSourceRange();
11074         }
11075       } else if (!AtomicBody->isInstantiationDependent()) {
11076         ErrorFound = NotAnAssignmentOp;
11077         ErrorLoc = AtomicBody->getExprLoc();
11078         ErrorRange = AtomicBody->getSourceRange();
11079         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
11080                               : AtomicBody->getExprLoc();
11081         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
11082                                 : AtomicBody->getSourceRange();
11083       }
11084     } else {
11085       ErrorFound = NotAnExpression;
11086       NoteLoc = ErrorLoc = Body->getBeginLoc();
11087       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
11088     }
11089     if (ErrorFound != NoError) {
11090       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
11091           << ErrorRange;
11092       Diag(NoteLoc, diag::note_omp_atomic_read_write)
11093           << ErrorFound << NoteRange;
11094       return StmtError();
11095     }
11096     if (CurContext->isDependentContext())
11097       V = X = nullptr;
11098   } else if (AtomicKind == OMPC_write) {
11099     enum {
11100       NotAnExpression,
11101       NotAnAssignmentOp,
11102       NotAScalarType,
11103       NotAnLValue,
11104       NoError
11105     } ErrorFound = NoError;
11106     SourceLocation ErrorLoc, NoteLoc;
11107     SourceRange ErrorRange, NoteRange;
11108     // If clause is write:
11109     //  x = expr;
11110     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
11111       const auto *AtomicBinOp =
11112           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
11113       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
11114         X = AtomicBinOp->getLHS();
11115         E = AtomicBinOp->getRHS();
11116         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
11117             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
11118           if (!X->isLValue()) {
11119             ErrorFound = NotAnLValue;
11120             ErrorLoc = AtomicBinOp->getExprLoc();
11121             ErrorRange = AtomicBinOp->getSourceRange();
11122             NoteLoc = X->getExprLoc();
11123             NoteRange = X->getSourceRange();
11124           }
11125         } else if (!X->isInstantiationDependent() ||
11126                    !E->isInstantiationDependent()) {
11127           const Expr *NotScalarExpr =
11128               (X->isInstantiationDependent() || X->getType()->isScalarType())
11129                   ? E
11130                   : X;
11131           ErrorFound = NotAScalarType;
11132           ErrorLoc = AtomicBinOp->getExprLoc();
11133           ErrorRange = AtomicBinOp->getSourceRange();
11134           NoteLoc = NotScalarExpr->getExprLoc();
11135           NoteRange = NotScalarExpr->getSourceRange();
11136         }
11137       } else if (!AtomicBody->isInstantiationDependent()) {
11138         ErrorFound = NotAnAssignmentOp;
11139         ErrorLoc = AtomicBody->getExprLoc();
11140         ErrorRange = AtomicBody->getSourceRange();
11141         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
11142                               : AtomicBody->getExprLoc();
11143         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
11144                                 : AtomicBody->getSourceRange();
11145       }
11146     } else {
11147       ErrorFound = NotAnExpression;
11148       NoteLoc = ErrorLoc = Body->getBeginLoc();
11149       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
11150     }
11151     if (ErrorFound != NoError) {
11152       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
11153           << ErrorRange;
11154       Diag(NoteLoc, diag::note_omp_atomic_read_write)
11155           << ErrorFound << NoteRange;
11156       return StmtError();
11157     }
11158     if (CurContext->isDependentContext())
11159       E = X = nullptr;
11160   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
11161     // If clause is update:
11162     //  x++;
11163     //  x--;
11164     //  ++x;
11165     //  --x;
11166     //  x binop= expr;
11167     //  x = x binop expr;
11168     //  x = expr binop x;
11169     OpenMPAtomicUpdateChecker Checker(*this);
11170     if (Checker.checkStatement(
11171             Body,
11172             (AtomicKind == OMPC_update)
11173                 ? diag::err_omp_atomic_update_not_expression_statement
11174                 : diag::err_omp_atomic_not_expression_statement,
11175             diag::note_omp_atomic_update))
11176       return StmtError();
11177     if (!CurContext->isDependentContext()) {
11178       E = Checker.getExpr();
11179       X = Checker.getX();
11180       UE = Checker.getUpdateExpr();
11181       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
11182     }
11183   } else if (AtomicKind == OMPC_capture) {
11184     enum {
11185       NotAnAssignmentOp,
11186       NotACompoundStatement,
11187       NotTwoSubstatements,
11188       NotASpecificExpression,
11189       NoError
11190     } ErrorFound = NoError;
11191     SourceLocation ErrorLoc, NoteLoc;
11192     SourceRange ErrorRange, NoteRange;
11193     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
11194       // If clause is a capture:
11195       //  v = x++;
11196       //  v = x--;
11197       //  v = ++x;
11198       //  v = --x;
11199       //  v = x binop= expr;
11200       //  v = x = x binop expr;
11201       //  v = x = expr binop x;
11202       const auto *AtomicBinOp =
11203           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
11204       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
11205         V = AtomicBinOp->getLHS();
11206         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
11207         OpenMPAtomicUpdateChecker Checker(*this);
11208         if (Checker.checkStatement(
11209                 Body, diag::err_omp_atomic_capture_not_expression_statement,
11210                 diag::note_omp_atomic_update))
11211           return StmtError();
11212         E = Checker.getExpr();
11213         X = Checker.getX();
11214         UE = Checker.getUpdateExpr();
11215         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
11216         IsPostfixUpdate = Checker.isPostfixUpdate();
11217       } else if (!AtomicBody->isInstantiationDependent()) {
11218         ErrorLoc = AtomicBody->getExprLoc();
11219         ErrorRange = AtomicBody->getSourceRange();
11220         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
11221                               : AtomicBody->getExprLoc();
11222         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
11223                                 : AtomicBody->getSourceRange();
11224         ErrorFound = NotAnAssignmentOp;
11225       }
11226       if (ErrorFound != NoError) {
11227         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
11228             << ErrorRange;
11229         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
11230         return StmtError();
11231       }
11232       if (CurContext->isDependentContext())
11233         UE = V = E = X = nullptr;
11234     } else {
11235       // If clause is a capture:
11236       //  { v = x; x = expr; }
11237       //  { v = x; x++; }
11238       //  { v = x; x--; }
11239       //  { v = x; ++x; }
11240       //  { v = x; --x; }
11241       //  { v = x; x binop= expr; }
11242       //  { v = x; x = x binop expr; }
11243       //  { v = x; x = expr binop x; }
11244       //  { x++; v = x; }
11245       //  { x--; v = x; }
11246       //  { ++x; v = x; }
11247       //  { --x; v = x; }
11248       //  { x binop= expr; v = x; }
11249       //  { x = x binop expr; v = x; }
11250       //  { x = expr binop x; v = x; }
11251       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
11252         // Check that this is { expr1; expr2; }
11253         if (CS->size() == 2) {
11254           Stmt *First = CS->body_front();
11255           Stmt *Second = CS->body_back();
11256           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
11257             First = EWC->getSubExpr()->IgnoreParenImpCasts();
11258           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
11259             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
11260           // Need to find what subexpression is 'v' and what is 'x'.
11261           OpenMPAtomicUpdateChecker Checker(*this);
11262           bool IsUpdateExprFound = !Checker.checkStatement(Second);
11263           BinaryOperator *BinOp = nullptr;
11264           if (IsUpdateExprFound) {
11265             BinOp = dyn_cast<BinaryOperator>(First);
11266             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
11267           }
11268           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
11269             //  { v = x; x++; }
11270             //  { v = x; x--; }
11271             //  { v = x; ++x; }
11272             //  { v = x; --x; }
11273             //  { v = x; x binop= expr; }
11274             //  { v = x; x = x binop expr; }
11275             //  { v = x; x = expr binop x; }
11276             // Check that the first expression has form v = x.
11277             Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
11278             llvm::FoldingSetNodeID XId, PossibleXId;
11279             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
11280             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
11281             IsUpdateExprFound = XId == PossibleXId;
11282             if (IsUpdateExprFound) {
11283               V = BinOp->getLHS();
11284               X = Checker.getX();
11285               E = Checker.getExpr();
11286               UE = Checker.getUpdateExpr();
11287               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
11288               IsPostfixUpdate = true;
11289             }
11290           }
11291           if (!IsUpdateExprFound) {
11292             IsUpdateExprFound = !Checker.checkStatement(First);
11293             BinOp = nullptr;
11294             if (IsUpdateExprFound) {
11295               BinOp = dyn_cast<BinaryOperator>(Second);
11296               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
11297             }
11298             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
11299               //  { x++; v = x; }
11300               //  { x--; v = x; }
11301               //  { ++x; v = x; }
11302               //  { --x; v = x; }
11303               //  { x binop= expr; v = x; }
11304               //  { x = x binop expr; v = x; }
11305               //  { x = expr binop x; v = x; }
11306               // Check that the second expression has form v = x.
11307               Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
11308               llvm::FoldingSetNodeID XId, PossibleXId;
11309               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
11310               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
11311               IsUpdateExprFound = XId == PossibleXId;
11312               if (IsUpdateExprFound) {
11313                 V = BinOp->getLHS();
11314                 X = Checker.getX();
11315                 E = Checker.getExpr();
11316                 UE = Checker.getUpdateExpr();
11317                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
11318                 IsPostfixUpdate = false;
11319               }
11320             }
11321           }
11322           if (!IsUpdateExprFound) {
11323             //  { v = x; x = expr; }
11324             auto *FirstExpr = dyn_cast<Expr>(First);
11325             auto *SecondExpr = dyn_cast<Expr>(Second);
11326             if (!FirstExpr || !SecondExpr ||
11327                 !(FirstExpr->isInstantiationDependent() ||
11328                   SecondExpr->isInstantiationDependent())) {
11329               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
11330               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
11331                 ErrorFound = NotAnAssignmentOp;
11332                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
11333                                                 : First->getBeginLoc();
11334                 NoteRange = ErrorRange = FirstBinOp
11335                                              ? FirstBinOp->getSourceRange()
11336                                              : SourceRange(ErrorLoc, ErrorLoc);
11337               } else {
11338                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
11339                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
11340                   ErrorFound = NotAnAssignmentOp;
11341                   NoteLoc = ErrorLoc = SecondBinOp
11342                                            ? SecondBinOp->getOperatorLoc()
11343                                            : Second->getBeginLoc();
11344                   NoteRange = ErrorRange =
11345                       SecondBinOp ? SecondBinOp->getSourceRange()
11346                                   : SourceRange(ErrorLoc, ErrorLoc);
11347                 } else {
11348                   Expr *PossibleXRHSInFirst =
11349                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
11350                   Expr *PossibleXLHSInSecond =
11351                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
11352                   llvm::FoldingSetNodeID X1Id, X2Id;
11353                   PossibleXRHSInFirst->Profile(X1Id, Context,
11354                                                /*Canonical=*/true);
11355                   PossibleXLHSInSecond->Profile(X2Id, Context,
11356                                                 /*Canonical=*/true);
11357                   IsUpdateExprFound = X1Id == X2Id;
11358                   if (IsUpdateExprFound) {
11359                     V = FirstBinOp->getLHS();
11360                     X = SecondBinOp->getLHS();
11361                     E = SecondBinOp->getRHS();
11362                     UE = nullptr;
11363                     IsXLHSInRHSPart = false;
11364                     IsPostfixUpdate = true;
11365                   } else {
11366                     ErrorFound = NotASpecificExpression;
11367                     ErrorLoc = FirstBinOp->getExprLoc();
11368                     ErrorRange = FirstBinOp->getSourceRange();
11369                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
11370                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
11371                   }
11372                 }
11373               }
11374             }
11375           }
11376         } else {
11377           NoteLoc = ErrorLoc = Body->getBeginLoc();
11378           NoteRange = ErrorRange =
11379               SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
11380           ErrorFound = NotTwoSubstatements;
11381         }
11382       } else {
11383         NoteLoc = ErrorLoc = Body->getBeginLoc();
11384         NoteRange = ErrorRange =
11385             SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
11386         ErrorFound = NotACompoundStatement;
11387       }
11388     }
11389     if (ErrorFound != NoError) {
11390       Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
11391           << ErrorRange;
11392       Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
11393       return StmtError();
11394     }
11395     if (CurContext->isDependentContext())
11396       UE = V = E = X = nullptr;
11397   } else if (AtomicKind == OMPC_compare) {
11398     // TODO: For now we emit an error here and in emitOMPAtomicExpr we ignore
11399     // code gen.
11400     unsigned DiagID = Diags.getCustomDiagID(
11401         DiagnosticsEngine::Error, "atomic compare is not supported for now");
11402     Diag(AtomicKindLoc, DiagID);
11403   }
11404 
11405   setFunctionHasBranchProtectedScope();
11406 
11407   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
11408                                     X, V, E, UE, IsXLHSInRHSPart,
11409                                     IsPostfixUpdate);
11410 }
11411 
11412 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
11413                                             Stmt *AStmt,
11414                                             SourceLocation StartLoc,
11415                                             SourceLocation EndLoc) {
11416   if (!AStmt)
11417     return StmtError();
11418 
11419   auto *CS = cast<CapturedStmt>(AStmt);
11420   // 1.2.2 OpenMP Language Terminology
11421   // Structured block - An executable statement with a single entry at the
11422   // top and a single exit at the bottom.
11423   // The point of exit cannot be a branch out of the structured block.
11424   // longjmp() and throw() must not violate the entry/exit criteria.
11425   CS->getCapturedDecl()->setNothrow();
11426   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
11427        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11428     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11429     // 1.2.2 OpenMP Language Terminology
11430     // Structured block - An executable statement with a single entry at the
11431     // top and a single exit at the bottom.
11432     // The point of exit cannot be a branch out of the structured block.
11433     // longjmp() and throw() must not violate the entry/exit criteria.
11434     CS->getCapturedDecl()->setNothrow();
11435   }
11436 
11437   // OpenMP [2.16, Nesting of Regions]
11438   // If specified, a teams construct must be contained within a target
11439   // construct. That target construct must contain no statements or directives
11440   // outside of the teams construct.
11441   if (DSAStack->hasInnerTeamsRegion()) {
11442     const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
11443     bool OMPTeamsFound = true;
11444     if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
11445       auto I = CS->body_begin();
11446       while (I != CS->body_end()) {
11447         const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
11448         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
11449             OMPTeamsFound) {
11450 
11451           OMPTeamsFound = false;
11452           break;
11453         }
11454         ++I;
11455       }
11456       assert(I != CS->body_end() && "Not found statement");
11457       S = *I;
11458     } else {
11459       const auto *OED = dyn_cast<OMPExecutableDirective>(S);
11460       OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
11461     }
11462     if (!OMPTeamsFound) {
11463       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
11464       Diag(DSAStack->getInnerTeamsRegionLoc(),
11465            diag::note_omp_nested_teams_construct_here);
11466       Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
11467           << isa<OMPExecutableDirective>(S);
11468       return StmtError();
11469     }
11470   }
11471 
11472   setFunctionHasBranchProtectedScope();
11473 
11474   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
11475 }
11476 
11477 StmtResult
11478 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
11479                                          Stmt *AStmt, SourceLocation StartLoc,
11480                                          SourceLocation EndLoc) {
11481   if (!AStmt)
11482     return StmtError();
11483 
11484   auto *CS = cast<CapturedStmt>(AStmt);
11485   // 1.2.2 OpenMP Language Terminology
11486   // Structured block - An executable statement with a single entry at the
11487   // top and a single exit at the bottom.
11488   // The point of exit cannot be a branch out of the structured block.
11489   // longjmp() and throw() must not violate the entry/exit criteria.
11490   CS->getCapturedDecl()->setNothrow();
11491   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
11492        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11493     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11494     // 1.2.2 OpenMP Language Terminology
11495     // Structured block - An executable statement with a single entry at the
11496     // top and a single exit at the bottom.
11497     // The point of exit cannot be a branch out of the structured block.
11498     // longjmp() and throw() must not violate the entry/exit criteria.
11499     CS->getCapturedDecl()->setNothrow();
11500   }
11501 
11502   setFunctionHasBranchProtectedScope();
11503 
11504   return OMPTargetParallelDirective::Create(
11505       Context, StartLoc, EndLoc, Clauses, AStmt,
11506       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11507 }
11508 
11509 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
11510     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11511     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11512   if (!AStmt)
11513     return StmtError();
11514 
11515   auto *CS = cast<CapturedStmt>(AStmt);
11516   // 1.2.2 OpenMP Language Terminology
11517   // Structured block - An executable statement with a single entry at the
11518   // top and a single exit at the bottom.
11519   // The point of exit cannot be a branch out of the structured block.
11520   // longjmp() and throw() must not violate the entry/exit criteria.
11521   CS->getCapturedDecl()->setNothrow();
11522   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
11523        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11524     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11525     // 1.2.2 OpenMP Language Terminology
11526     // Structured block - An executable statement with a single entry at the
11527     // top and a single exit at the bottom.
11528     // The point of exit cannot be a branch out of the structured block.
11529     // longjmp() and throw() must not violate the entry/exit criteria.
11530     CS->getCapturedDecl()->setNothrow();
11531   }
11532 
11533   OMPLoopBasedDirective::HelperExprs B;
11534   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11535   // define the nested loops number.
11536   unsigned NestedLoopCount =
11537       checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
11538                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
11539                       VarsWithImplicitDSA, B);
11540   if (NestedLoopCount == 0)
11541     return StmtError();
11542 
11543   assert((CurContext->isDependentContext() || B.builtAll()) &&
11544          "omp target parallel for loop exprs were not built");
11545 
11546   if (!CurContext->isDependentContext()) {
11547     // Finalize the clauses that need pre-built expressions for CodeGen.
11548     for (OMPClause *C : Clauses) {
11549       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11550         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11551                                      B.NumIterations, *this, CurScope,
11552                                      DSAStack))
11553           return StmtError();
11554     }
11555   }
11556 
11557   setFunctionHasBranchProtectedScope();
11558   return OMPTargetParallelForDirective::Create(
11559       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
11560       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11561 }
11562 
11563 /// Check for existence of a map clause in the list of clauses.
11564 static bool hasClauses(ArrayRef<OMPClause *> Clauses,
11565                        const OpenMPClauseKind K) {
11566   return llvm::any_of(
11567       Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
11568 }
11569 
11570 template <typename... Params>
11571 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
11572                        const Params... ClauseTypes) {
11573   return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
11574 }
11575 
11576 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
11577                                                 Stmt *AStmt,
11578                                                 SourceLocation StartLoc,
11579                                                 SourceLocation EndLoc) {
11580   if (!AStmt)
11581     return StmtError();
11582 
11583   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11584 
11585   // OpenMP [2.12.2, target data Construct, Restrictions]
11586   // At least one map, use_device_addr or use_device_ptr clause must appear on
11587   // the directive.
11588   if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) &&
11589       (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) {
11590     StringRef Expected;
11591     if (LangOpts.OpenMP < 50)
11592       Expected = "'map' or 'use_device_ptr'";
11593     else
11594       Expected = "'map', 'use_device_ptr', or 'use_device_addr'";
11595     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
11596         << Expected << getOpenMPDirectiveName(OMPD_target_data);
11597     return StmtError();
11598   }
11599 
11600   setFunctionHasBranchProtectedScope();
11601 
11602   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
11603                                         AStmt);
11604 }
11605 
11606 StmtResult
11607 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
11608                                           SourceLocation StartLoc,
11609                                           SourceLocation EndLoc, Stmt *AStmt) {
11610   if (!AStmt)
11611     return StmtError();
11612 
11613   auto *CS = cast<CapturedStmt>(AStmt);
11614   // 1.2.2 OpenMP Language Terminology
11615   // Structured block - An executable statement with a single entry at the
11616   // top and a single exit at the bottom.
11617   // The point of exit cannot be a branch out of the structured block.
11618   // longjmp() and throw() must not violate the entry/exit criteria.
11619   CS->getCapturedDecl()->setNothrow();
11620   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
11621        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11622     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11623     // 1.2.2 OpenMP Language Terminology
11624     // Structured block - An executable statement with a single entry at the
11625     // top and a single exit at the bottom.
11626     // The point of exit cannot be a branch out of the structured block.
11627     // longjmp() and throw() must not violate the entry/exit criteria.
11628     CS->getCapturedDecl()->setNothrow();
11629   }
11630 
11631   // OpenMP [2.10.2, Restrictions, p. 99]
11632   // At least one map clause must appear on the directive.
11633   if (!hasClauses(Clauses, OMPC_map)) {
11634     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
11635         << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
11636     return StmtError();
11637   }
11638 
11639   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
11640                                              AStmt);
11641 }
11642 
11643 StmtResult
11644 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
11645                                          SourceLocation StartLoc,
11646                                          SourceLocation EndLoc, Stmt *AStmt) {
11647   if (!AStmt)
11648     return StmtError();
11649 
11650   auto *CS = cast<CapturedStmt>(AStmt);
11651   // 1.2.2 OpenMP Language Terminology
11652   // Structured block - An executable statement with a single entry at the
11653   // top and a single exit at the bottom.
11654   // The point of exit cannot be a branch out of the structured block.
11655   // longjmp() and throw() must not violate the entry/exit criteria.
11656   CS->getCapturedDecl()->setNothrow();
11657   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
11658        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11659     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11660     // 1.2.2 OpenMP Language Terminology
11661     // Structured block - An executable statement with a single entry at the
11662     // top and a single exit at the bottom.
11663     // The point of exit cannot be a branch out of the structured block.
11664     // longjmp() and throw() must not violate the entry/exit criteria.
11665     CS->getCapturedDecl()->setNothrow();
11666   }
11667 
11668   // OpenMP [2.10.3, Restrictions, p. 102]
11669   // At least one map clause must appear on the directive.
11670   if (!hasClauses(Clauses, OMPC_map)) {
11671     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
11672         << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
11673     return StmtError();
11674   }
11675 
11676   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
11677                                             AStmt);
11678 }
11679 
11680 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
11681                                                   SourceLocation StartLoc,
11682                                                   SourceLocation EndLoc,
11683                                                   Stmt *AStmt) {
11684   if (!AStmt)
11685     return StmtError();
11686 
11687   auto *CS = cast<CapturedStmt>(AStmt);
11688   // 1.2.2 OpenMP Language Terminology
11689   // Structured block - An executable statement with a single entry at the
11690   // top and a single exit at the bottom.
11691   // The point of exit cannot be a branch out of the structured block.
11692   // longjmp() and throw() must not violate the entry/exit criteria.
11693   CS->getCapturedDecl()->setNothrow();
11694   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
11695        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11696     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11697     // 1.2.2 OpenMP Language Terminology
11698     // Structured block - An executable statement with a single entry at the
11699     // top and a single exit at the bottom.
11700     // The point of exit cannot be a branch out of the structured block.
11701     // longjmp() and throw() must not violate the entry/exit criteria.
11702     CS->getCapturedDecl()->setNothrow();
11703   }
11704 
11705   if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
11706     Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
11707     return StmtError();
11708   }
11709   return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
11710                                           AStmt);
11711 }
11712 
11713 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
11714                                            Stmt *AStmt, SourceLocation StartLoc,
11715                                            SourceLocation EndLoc) {
11716   if (!AStmt)
11717     return StmtError();
11718 
11719   auto *CS = cast<CapturedStmt>(AStmt);
11720   // 1.2.2 OpenMP Language Terminology
11721   // Structured block - An executable statement with a single entry at the
11722   // top and a single exit at the bottom.
11723   // The point of exit cannot be a branch out of the structured block.
11724   // longjmp() and throw() must not violate the entry/exit criteria.
11725   CS->getCapturedDecl()->setNothrow();
11726 
11727   setFunctionHasBranchProtectedScope();
11728 
11729   DSAStack->setParentTeamsRegionLoc(StartLoc);
11730 
11731   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
11732 }
11733 
11734 StmtResult
11735 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
11736                                             SourceLocation EndLoc,
11737                                             OpenMPDirectiveKind CancelRegion) {
11738   if (DSAStack->isParentNowaitRegion()) {
11739     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
11740     return StmtError();
11741   }
11742   if (DSAStack->isParentOrderedRegion()) {
11743     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
11744     return StmtError();
11745   }
11746   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
11747                                                CancelRegion);
11748 }
11749 
11750 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
11751                                             SourceLocation StartLoc,
11752                                             SourceLocation EndLoc,
11753                                             OpenMPDirectiveKind CancelRegion) {
11754   if (DSAStack->isParentNowaitRegion()) {
11755     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
11756     return StmtError();
11757   }
11758   if (DSAStack->isParentOrderedRegion()) {
11759     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
11760     return StmtError();
11761   }
11762   DSAStack->setParentCancelRegion(/*Cancel=*/true);
11763   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
11764                                     CancelRegion);
11765 }
11766 
11767 static bool checkReductionClauseWithNogroup(Sema &S,
11768                                             ArrayRef<OMPClause *> Clauses) {
11769   const OMPClause *ReductionClause = nullptr;
11770   const OMPClause *NogroupClause = nullptr;
11771   for (const OMPClause *C : Clauses) {
11772     if (C->getClauseKind() == OMPC_reduction) {
11773       ReductionClause = C;
11774       if (NogroupClause)
11775         break;
11776       continue;
11777     }
11778     if (C->getClauseKind() == OMPC_nogroup) {
11779       NogroupClause = C;
11780       if (ReductionClause)
11781         break;
11782       continue;
11783     }
11784   }
11785   if (ReductionClause && NogroupClause) {
11786     S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
11787         << SourceRange(NogroupClause->getBeginLoc(),
11788                        NogroupClause->getEndLoc());
11789     return true;
11790   }
11791   return false;
11792 }
11793 
11794 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
11795     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11796     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11797   if (!AStmt)
11798     return StmtError();
11799 
11800   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11801   OMPLoopBasedDirective::HelperExprs B;
11802   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11803   // define the nested loops number.
11804   unsigned NestedLoopCount =
11805       checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
11806                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
11807                       VarsWithImplicitDSA, B);
11808   if (NestedLoopCount == 0)
11809     return StmtError();
11810 
11811   assert((CurContext->isDependentContext() || B.builtAll()) &&
11812          "omp for loop exprs were not built");
11813 
11814   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11815   // The grainsize clause and num_tasks clause are mutually exclusive and may
11816   // not appear on the same taskloop directive.
11817   if (checkMutuallyExclusiveClauses(*this, Clauses,
11818                                     {OMPC_grainsize, OMPC_num_tasks}))
11819     return StmtError();
11820   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11821   // If a reduction clause is present on the taskloop directive, the nogroup
11822   // clause must not be specified.
11823   if (checkReductionClauseWithNogroup(*this, Clauses))
11824     return StmtError();
11825 
11826   setFunctionHasBranchProtectedScope();
11827   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
11828                                       NestedLoopCount, Clauses, AStmt, B,
11829                                       DSAStack->isCancelRegion());
11830 }
11831 
11832 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
11833     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11834     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11835   if (!AStmt)
11836     return StmtError();
11837 
11838   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11839   OMPLoopBasedDirective::HelperExprs B;
11840   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11841   // define the nested loops number.
11842   unsigned NestedLoopCount =
11843       checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
11844                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
11845                       VarsWithImplicitDSA, B);
11846   if (NestedLoopCount == 0)
11847     return StmtError();
11848 
11849   assert((CurContext->isDependentContext() || B.builtAll()) &&
11850          "omp for loop exprs were not built");
11851 
11852   if (!CurContext->isDependentContext()) {
11853     // Finalize the clauses that need pre-built expressions for CodeGen.
11854     for (OMPClause *C : Clauses) {
11855       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11856         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11857                                      B.NumIterations, *this, CurScope,
11858                                      DSAStack))
11859           return StmtError();
11860     }
11861   }
11862 
11863   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11864   // The grainsize clause and num_tasks clause are mutually exclusive and may
11865   // not appear on the same taskloop directive.
11866   if (checkMutuallyExclusiveClauses(*this, Clauses,
11867                                     {OMPC_grainsize, OMPC_num_tasks}))
11868     return StmtError();
11869   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11870   // If a reduction clause is present on the taskloop directive, the nogroup
11871   // clause must not be specified.
11872   if (checkReductionClauseWithNogroup(*this, Clauses))
11873     return StmtError();
11874   if (checkSimdlenSafelenSpecified(*this, Clauses))
11875     return StmtError();
11876 
11877   setFunctionHasBranchProtectedScope();
11878   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
11879                                           NestedLoopCount, Clauses, AStmt, B);
11880 }
11881 
11882 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective(
11883     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11884     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11885   if (!AStmt)
11886     return StmtError();
11887 
11888   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11889   OMPLoopBasedDirective::HelperExprs B;
11890   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11891   // define the nested loops number.
11892   unsigned NestedLoopCount =
11893       checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses),
11894                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
11895                       VarsWithImplicitDSA, B);
11896   if (NestedLoopCount == 0)
11897     return StmtError();
11898 
11899   assert((CurContext->isDependentContext() || B.builtAll()) &&
11900          "omp for loop exprs were not built");
11901 
11902   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11903   // The grainsize clause and num_tasks clause are mutually exclusive and may
11904   // not appear on the same taskloop directive.
11905   if (checkMutuallyExclusiveClauses(*this, Clauses,
11906                                     {OMPC_grainsize, OMPC_num_tasks}))
11907     return StmtError();
11908   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11909   // If a reduction clause is present on the taskloop directive, the nogroup
11910   // clause must not be specified.
11911   if (checkReductionClauseWithNogroup(*this, Clauses))
11912     return StmtError();
11913 
11914   setFunctionHasBranchProtectedScope();
11915   return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc,
11916                                             NestedLoopCount, Clauses, AStmt, B,
11917                                             DSAStack->isCancelRegion());
11918 }
11919 
11920 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective(
11921     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11922     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11923   if (!AStmt)
11924     return StmtError();
11925 
11926   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11927   OMPLoopBasedDirective::HelperExprs B;
11928   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11929   // define the nested loops number.
11930   unsigned NestedLoopCount =
11931       checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses),
11932                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
11933                       VarsWithImplicitDSA, B);
11934   if (NestedLoopCount == 0)
11935     return StmtError();
11936 
11937   assert((CurContext->isDependentContext() || B.builtAll()) &&
11938          "omp for loop exprs were not built");
11939 
11940   if (!CurContext->isDependentContext()) {
11941     // Finalize the clauses that need pre-built expressions for CodeGen.
11942     for (OMPClause *C : Clauses) {
11943       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11944         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11945                                      B.NumIterations, *this, CurScope,
11946                                      DSAStack))
11947           return StmtError();
11948     }
11949   }
11950 
11951   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11952   // The grainsize clause and num_tasks clause are mutually exclusive and may
11953   // not appear on the same taskloop directive.
11954   if (checkMutuallyExclusiveClauses(*this, Clauses,
11955                                     {OMPC_grainsize, OMPC_num_tasks}))
11956     return StmtError();
11957   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11958   // If a reduction clause is present on the taskloop directive, the nogroup
11959   // clause must not be specified.
11960   if (checkReductionClauseWithNogroup(*this, Clauses))
11961     return StmtError();
11962   if (checkSimdlenSafelenSpecified(*this, Clauses))
11963     return StmtError();
11964 
11965   setFunctionHasBranchProtectedScope();
11966   return OMPMasterTaskLoopSimdDirective::Create(
11967       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
11968 }
11969 
11970 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective(
11971     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11972     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11973   if (!AStmt)
11974     return StmtError();
11975 
11976   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11977   auto *CS = cast<CapturedStmt>(AStmt);
11978   // 1.2.2 OpenMP Language Terminology
11979   // Structured block - An executable statement with a single entry at the
11980   // top and a single exit at the bottom.
11981   // The point of exit cannot be a branch out of the structured block.
11982   // longjmp() and throw() must not violate the entry/exit criteria.
11983   CS->getCapturedDecl()->setNothrow();
11984   for (int ThisCaptureLevel =
11985            getOpenMPCaptureLevels(OMPD_parallel_master_taskloop);
11986        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11987     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11988     // 1.2.2 OpenMP Language Terminology
11989     // Structured block - An executable statement with a single entry at the
11990     // top and a single exit at the bottom.
11991     // The point of exit cannot be a branch out of the structured block.
11992     // longjmp() and throw() must not violate the entry/exit criteria.
11993     CS->getCapturedDecl()->setNothrow();
11994   }
11995 
11996   OMPLoopBasedDirective::HelperExprs B;
11997   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11998   // define the nested loops number.
11999   unsigned NestedLoopCount = checkOpenMPLoop(
12000       OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses),
12001       /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack,
12002       VarsWithImplicitDSA, B);
12003   if (NestedLoopCount == 0)
12004     return StmtError();
12005 
12006   assert((CurContext->isDependentContext() || B.builtAll()) &&
12007          "omp for loop exprs were not built");
12008 
12009   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
12010   // The grainsize clause and num_tasks clause are mutually exclusive and may
12011   // not appear on the same taskloop directive.
12012   if (checkMutuallyExclusiveClauses(*this, Clauses,
12013                                     {OMPC_grainsize, OMPC_num_tasks}))
12014     return StmtError();
12015   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
12016   // If a reduction clause is present on the taskloop directive, the nogroup
12017   // clause must not be specified.
12018   if (checkReductionClauseWithNogroup(*this, Clauses))
12019     return StmtError();
12020 
12021   setFunctionHasBranchProtectedScope();
12022   return OMPParallelMasterTaskLoopDirective::Create(
12023       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
12024       DSAStack->isCancelRegion());
12025 }
12026 
12027 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective(
12028     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12029     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12030   if (!AStmt)
12031     return StmtError();
12032 
12033   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
12034   auto *CS = cast<CapturedStmt>(AStmt);
12035   // 1.2.2 OpenMP Language Terminology
12036   // Structured block - An executable statement with a single entry at the
12037   // top and a single exit at the bottom.
12038   // The point of exit cannot be a branch out of the structured block.
12039   // longjmp() and throw() must not violate the entry/exit criteria.
12040   CS->getCapturedDecl()->setNothrow();
12041   for (int ThisCaptureLevel =
12042            getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd);
12043        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12044     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12045     // 1.2.2 OpenMP Language Terminology
12046     // Structured block - An executable statement with a single entry at the
12047     // top and a single exit at the bottom.
12048     // The point of exit cannot be a branch out of the structured block.
12049     // longjmp() and throw() must not violate the entry/exit criteria.
12050     CS->getCapturedDecl()->setNothrow();
12051   }
12052 
12053   OMPLoopBasedDirective::HelperExprs B;
12054   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
12055   // define the nested loops number.
12056   unsigned NestedLoopCount = checkOpenMPLoop(
12057       OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses),
12058       /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack,
12059       VarsWithImplicitDSA, B);
12060   if (NestedLoopCount == 0)
12061     return StmtError();
12062 
12063   assert((CurContext->isDependentContext() || B.builtAll()) &&
12064          "omp for loop exprs were not built");
12065 
12066   if (!CurContext->isDependentContext()) {
12067     // Finalize the clauses that need pre-built expressions for CodeGen.
12068     for (OMPClause *C : Clauses) {
12069       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12070         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12071                                      B.NumIterations, *this, CurScope,
12072                                      DSAStack))
12073           return StmtError();
12074     }
12075   }
12076 
12077   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
12078   // The grainsize clause and num_tasks clause are mutually exclusive and may
12079   // not appear on the same taskloop directive.
12080   if (checkMutuallyExclusiveClauses(*this, Clauses,
12081                                     {OMPC_grainsize, OMPC_num_tasks}))
12082     return StmtError();
12083   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
12084   // If a reduction clause is present on the taskloop directive, the nogroup
12085   // clause must not be specified.
12086   if (checkReductionClauseWithNogroup(*this, Clauses))
12087     return StmtError();
12088   if (checkSimdlenSafelenSpecified(*this, Clauses))
12089     return StmtError();
12090 
12091   setFunctionHasBranchProtectedScope();
12092   return OMPParallelMasterTaskLoopSimdDirective::Create(
12093       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12094 }
12095 
12096 StmtResult Sema::ActOnOpenMPDistributeDirective(
12097     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12098     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12099   if (!AStmt)
12100     return StmtError();
12101 
12102   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
12103   OMPLoopBasedDirective::HelperExprs B;
12104   // In presence of clause 'collapse' with number of loops, it will
12105   // define the nested loops number.
12106   unsigned NestedLoopCount =
12107       checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
12108                       nullptr /*ordered not a clause on distribute*/, AStmt,
12109                       *this, *DSAStack, VarsWithImplicitDSA, B);
12110   if (NestedLoopCount == 0)
12111     return StmtError();
12112 
12113   assert((CurContext->isDependentContext() || B.builtAll()) &&
12114          "omp for loop exprs were not built");
12115 
12116   setFunctionHasBranchProtectedScope();
12117   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
12118                                         NestedLoopCount, Clauses, AStmt, B);
12119 }
12120 
12121 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
12122     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12123     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12124   if (!AStmt)
12125     return StmtError();
12126 
12127   auto *CS = cast<CapturedStmt>(AStmt);
12128   // 1.2.2 OpenMP Language Terminology
12129   // Structured block - An executable statement with a single entry at the
12130   // top and a single exit at the bottom.
12131   // The point of exit cannot be a branch out of the structured block.
12132   // longjmp() and throw() must not violate the entry/exit criteria.
12133   CS->getCapturedDecl()->setNothrow();
12134   for (int ThisCaptureLevel =
12135            getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
12136        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12137     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12138     // 1.2.2 OpenMP Language Terminology
12139     // Structured block - An executable statement with a single entry at the
12140     // top and a single exit at the bottom.
12141     // The point of exit cannot be a branch out of the structured block.
12142     // longjmp() and throw() must not violate the entry/exit criteria.
12143     CS->getCapturedDecl()->setNothrow();
12144   }
12145 
12146   OMPLoopBasedDirective::HelperExprs B;
12147   // In presence of clause 'collapse' with number of loops, it will
12148   // define the nested loops number.
12149   unsigned NestedLoopCount = checkOpenMPLoop(
12150       OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
12151       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12152       VarsWithImplicitDSA, B);
12153   if (NestedLoopCount == 0)
12154     return StmtError();
12155 
12156   assert((CurContext->isDependentContext() || B.builtAll()) &&
12157          "omp for loop exprs were not built");
12158 
12159   setFunctionHasBranchProtectedScope();
12160   return OMPDistributeParallelForDirective::Create(
12161       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
12162       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
12163 }
12164 
12165 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
12166     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12167     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12168   if (!AStmt)
12169     return StmtError();
12170 
12171   auto *CS = cast<CapturedStmt>(AStmt);
12172   // 1.2.2 OpenMP Language Terminology
12173   // Structured block - An executable statement with a single entry at the
12174   // top and a single exit at the bottom.
12175   // The point of exit cannot be a branch out of the structured block.
12176   // longjmp() and throw() must not violate the entry/exit criteria.
12177   CS->getCapturedDecl()->setNothrow();
12178   for (int ThisCaptureLevel =
12179            getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
12180        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12181     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12182     // 1.2.2 OpenMP Language Terminology
12183     // Structured block - An executable statement with a single entry at the
12184     // top and a single exit at the bottom.
12185     // The point of exit cannot be a branch out of the structured block.
12186     // longjmp() and throw() must not violate the entry/exit criteria.
12187     CS->getCapturedDecl()->setNothrow();
12188   }
12189 
12190   OMPLoopBasedDirective::HelperExprs B;
12191   // In presence of clause 'collapse' with number of loops, it will
12192   // define the nested loops number.
12193   unsigned NestedLoopCount = checkOpenMPLoop(
12194       OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
12195       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12196       VarsWithImplicitDSA, B);
12197   if (NestedLoopCount == 0)
12198     return StmtError();
12199 
12200   assert((CurContext->isDependentContext() || B.builtAll()) &&
12201          "omp for loop exprs were not built");
12202 
12203   if (!CurContext->isDependentContext()) {
12204     // Finalize the clauses that need pre-built expressions for CodeGen.
12205     for (OMPClause *C : Clauses) {
12206       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12207         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12208                                      B.NumIterations, *this, CurScope,
12209                                      DSAStack))
12210           return StmtError();
12211     }
12212   }
12213 
12214   if (checkSimdlenSafelenSpecified(*this, Clauses))
12215     return StmtError();
12216 
12217   setFunctionHasBranchProtectedScope();
12218   return OMPDistributeParallelForSimdDirective::Create(
12219       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12220 }
12221 
12222 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
12223     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12224     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12225   if (!AStmt)
12226     return StmtError();
12227 
12228   auto *CS = cast<CapturedStmt>(AStmt);
12229   // 1.2.2 OpenMP Language Terminology
12230   // Structured block - An executable statement with a single entry at the
12231   // top and a single exit at the bottom.
12232   // The point of exit cannot be a branch out of the structured block.
12233   // longjmp() and throw() must not violate the entry/exit criteria.
12234   CS->getCapturedDecl()->setNothrow();
12235   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
12236        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12237     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12238     // 1.2.2 OpenMP Language Terminology
12239     // Structured block - An executable statement with a single entry at the
12240     // top and a single exit at the bottom.
12241     // The point of exit cannot be a branch out of the structured block.
12242     // longjmp() and throw() must not violate the entry/exit criteria.
12243     CS->getCapturedDecl()->setNothrow();
12244   }
12245 
12246   OMPLoopBasedDirective::HelperExprs B;
12247   // In presence of clause 'collapse' with number of loops, it will
12248   // define the nested loops number.
12249   unsigned NestedLoopCount =
12250       checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
12251                       nullptr /*ordered not a clause on distribute*/, CS, *this,
12252                       *DSAStack, VarsWithImplicitDSA, B);
12253   if (NestedLoopCount == 0)
12254     return StmtError();
12255 
12256   assert((CurContext->isDependentContext() || B.builtAll()) &&
12257          "omp for loop exprs were not built");
12258 
12259   if (!CurContext->isDependentContext()) {
12260     // Finalize the clauses that need pre-built expressions for CodeGen.
12261     for (OMPClause *C : Clauses) {
12262       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12263         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12264                                      B.NumIterations, *this, CurScope,
12265                                      DSAStack))
12266           return StmtError();
12267     }
12268   }
12269 
12270   if (checkSimdlenSafelenSpecified(*this, Clauses))
12271     return StmtError();
12272 
12273   setFunctionHasBranchProtectedScope();
12274   return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
12275                                             NestedLoopCount, Clauses, AStmt, B);
12276 }
12277 
12278 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
12279     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12280     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12281   if (!AStmt)
12282     return StmtError();
12283 
12284   auto *CS = cast<CapturedStmt>(AStmt);
12285   // 1.2.2 OpenMP Language Terminology
12286   // Structured block - An executable statement with a single entry at the
12287   // top and a single exit at the bottom.
12288   // The point of exit cannot be a branch out of the structured block.
12289   // longjmp() and throw() must not violate the entry/exit criteria.
12290   CS->getCapturedDecl()->setNothrow();
12291   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
12292        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12293     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12294     // 1.2.2 OpenMP Language Terminology
12295     // Structured block - An executable statement with a single entry at the
12296     // top and a single exit at the bottom.
12297     // The point of exit cannot be a branch out of the structured block.
12298     // longjmp() and throw() must not violate the entry/exit criteria.
12299     CS->getCapturedDecl()->setNothrow();
12300   }
12301 
12302   OMPLoopBasedDirective::HelperExprs B;
12303   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
12304   // define the nested loops number.
12305   unsigned NestedLoopCount = checkOpenMPLoop(
12306       OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
12307       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, VarsWithImplicitDSA,
12308       B);
12309   if (NestedLoopCount == 0)
12310     return StmtError();
12311 
12312   assert((CurContext->isDependentContext() || B.builtAll()) &&
12313          "omp target parallel for simd loop exprs were not built");
12314 
12315   if (!CurContext->isDependentContext()) {
12316     // Finalize the clauses that need pre-built expressions for CodeGen.
12317     for (OMPClause *C : Clauses) {
12318       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12319         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12320                                      B.NumIterations, *this, CurScope,
12321                                      DSAStack))
12322           return StmtError();
12323     }
12324   }
12325   if (checkSimdlenSafelenSpecified(*this, Clauses))
12326     return StmtError();
12327 
12328   setFunctionHasBranchProtectedScope();
12329   return OMPTargetParallelForSimdDirective::Create(
12330       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12331 }
12332 
12333 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
12334     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12335     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12336   if (!AStmt)
12337     return StmtError();
12338 
12339   auto *CS = cast<CapturedStmt>(AStmt);
12340   // 1.2.2 OpenMP Language Terminology
12341   // Structured block - An executable statement with a single entry at the
12342   // top and a single exit at the bottom.
12343   // The point of exit cannot be a branch out of the structured block.
12344   // longjmp() and throw() must not violate the entry/exit criteria.
12345   CS->getCapturedDecl()->setNothrow();
12346   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
12347        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12348     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12349     // 1.2.2 OpenMP Language Terminology
12350     // Structured block - An executable statement with a single entry at the
12351     // top and a single exit at the bottom.
12352     // The point of exit cannot be a branch out of the structured block.
12353     // longjmp() and throw() must not violate the entry/exit criteria.
12354     CS->getCapturedDecl()->setNothrow();
12355   }
12356 
12357   OMPLoopBasedDirective::HelperExprs B;
12358   // In presence of clause 'collapse' with number of loops, it will define the
12359   // nested loops number.
12360   unsigned NestedLoopCount =
12361       checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
12362                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
12363                       VarsWithImplicitDSA, B);
12364   if (NestedLoopCount == 0)
12365     return StmtError();
12366 
12367   assert((CurContext->isDependentContext() || B.builtAll()) &&
12368          "omp target simd loop exprs were not built");
12369 
12370   if (!CurContext->isDependentContext()) {
12371     // Finalize the clauses that need pre-built expressions for CodeGen.
12372     for (OMPClause *C : Clauses) {
12373       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12374         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12375                                      B.NumIterations, *this, CurScope,
12376                                      DSAStack))
12377           return StmtError();
12378     }
12379   }
12380 
12381   if (checkSimdlenSafelenSpecified(*this, Clauses))
12382     return StmtError();
12383 
12384   setFunctionHasBranchProtectedScope();
12385   return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
12386                                         NestedLoopCount, Clauses, AStmt, B);
12387 }
12388 
12389 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
12390     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12391     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12392   if (!AStmt)
12393     return StmtError();
12394 
12395   auto *CS = cast<CapturedStmt>(AStmt);
12396   // 1.2.2 OpenMP Language Terminology
12397   // Structured block - An executable statement with a single entry at the
12398   // top and a single exit at the bottom.
12399   // The point of exit cannot be a branch out of the structured block.
12400   // longjmp() and throw() must not violate the entry/exit criteria.
12401   CS->getCapturedDecl()->setNothrow();
12402   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
12403        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12404     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12405     // 1.2.2 OpenMP Language Terminology
12406     // Structured block - An executable statement with a single entry at the
12407     // top and a single exit at the bottom.
12408     // The point of exit cannot be a branch out of the structured block.
12409     // longjmp() and throw() must not violate the entry/exit criteria.
12410     CS->getCapturedDecl()->setNothrow();
12411   }
12412 
12413   OMPLoopBasedDirective::HelperExprs B;
12414   // In presence of clause 'collapse' with number of loops, it will
12415   // define the nested loops number.
12416   unsigned NestedLoopCount =
12417       checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
12418                       nullptr /*ordered not a clause on distribute*/, CS, *this,
12419                       *DSAStack, VarsWithImplicitDSA, B);
12420   if (NestedLoopCount == 0)
12421     return StmtError();
12422 
12423   assert((CurContext->isDependentContext() || B.builtAll()) &&
12424          "omp teams distribute loop exprs were not built");
12425 
12426   setFunctionHasBranchProtectedScope();
12427 
12428   DSAStack->setParentTeamsRegionLoc(StartLoc);
12429 
12430   return OMPTeamsDistributeDirective::Create(
12431       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12432 }
12433 
12434 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
12435     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12436     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12437   if (!AStmt)
12438     return StmtError();
12439 
12440   auto *CS = cast<CapturedStmt>(AStmt);
12441   // 1.2.2 OpenMP Language Terminology
12442   // Structured block - An executable statement with a single entry at the
12443   // top and a single exit at the bottom.
12444   // The point of exit cannot be a branch out of the structured block.
12445   // longjmp() and throw() must not violate the entry/exit criteria.
12446   CS->getCapturedDecl()->setNothrow();
12447   for (int ThisCaptureLevel =
12448            getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
12449        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12450     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12451     // 1.2.2 OpenMP Language Terminology
12452     // Structured block - An executable statement with a single entry at the
12453     // top and a single exit at the bottom.
12454     // The point of exit cannot be a branch out of the structured block.
12455     // longjmp() and throw() must not violate the entry/exit criteria.
12456     CS->getCapturedDecl()->setNothrow();
12457   }
12458 
12459   OMPLoopBasedDirective::HelperExprs B;
12460   // In presence of clause 'collapse' with number of loops, it will
12461   // define the nested loops number.
12462   unsigned NestedLoopCount = checkOpenMPLoop(
12463       OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
12464       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12465       VarsWithImplicitDSA, B);
12466 
12467   if (NestedLoopCount == 0)
12468     return StmtError();
12469 
12470   assert((CurContext->isDependentContext() || B.builtAll()) &&
12471          "omp teams distribute simd loop exprs were not built");
12472 
12473   if (!CurContext->isDependentContext()) {
12474     // Finalize the clauses that need pre-built expressions for CodeGen.
12475     for (OMPClause *C : Clauses) {
12476       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12477         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12478                                      B.NumIterations, *this, CurScope,
12479                                      DSAStack))
12480           return StmtError();
12481     }
12482   }
12483 
12484   if (checkSimdlenSafelenSpecified(*this, Clauses))
12485     return StmtError();
12486 
12487   setFunctionHasBranchProtectedScope();
12488 
12489   DSAStack->setParentTeamsRegionLoc(StartLoc);
12490 
12491   return OMPTeamsDistributeSimdDirective::Create(
12492       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12493 }
12494 
12495 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
12496     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12497     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12498   if (!AStmt)
12499     return StmtError();
12500 
12501   auto *CS = cast<CapturedStmt>(AStmt);
12502   // 1.2.2 OpenMP Language Terminology
12503   // Structured block - An executable statement with a single entry at the
12504   // top and a single exit at the bottom.
12505   // The point of exit cannot be a branch out of the structured block.
12506   // longjmp() and throw() must not violate the entry/exit criteria.
12507   CS->getCapturedDecl()->setNothrow();
12508 
12509   for (int ThisCaptureLevel =
12510            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
12511        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12512     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12513     // 1.2.2 OpenMP Language Terminology
12514     // Structured block - An executable statement with a single entry at the
12515     // top and a single exit at the bottom.
12516     // The point of exit cannot be a branch out of the structured block.
12517     // longjmp() and throw() must not violate the entry/exit criteria.
12518     CS->getCapturedDecl()->setNothrow();
12519   }
12520 
12521   OMPLoopBasedDirective::HelperExprs B;
12522   // In presence of clause 'collapse' with number of loops, it will
12523   // define the nested loops number.
12524   unsigned NestedLoopCount = checkOpenMPLoop(
12525       OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
12526       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12527       VarsWithImplicitDSA, B);
12528 
12529   if (NestedLoopCount == 0)
12530     return StmtError();
12531 
12532   assert((CurContext->isDependentContext() || B.builtAll()) &&
12533          "omp for loop exprs were not built");
12534 
12535   if (!CurContext->isDependentContext()) {
12536     // Finalize the clauses that need pre-built expressions for CodeGen.
12537     for (OMPClause *C : Clauses) {
12538       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12539         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12540                                      B.NumIterations, *this, CurScope,
12541                                      DSAStack))
12542           return StmtError();
12543     }
12544   }
12545 
12546   if (checkSimdlenSafelenSpecified(*this, Clauses))
12547     return StmtError();
12548 
12549   setFunctionHasBranchProtectedScope();
12550 
12551   DSAStack->setParentTeamsRegionLoc(StartLoc);
12552 
12553   return OMPTeamsDistributeParallelForSimdDirective::Create(
12554       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12555 }
12556 
12557 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
12558     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12559     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12560   if (!AStmt)
12561     return StmtError();
12562 
12563   auto *CS = cast<CapturedStmt>(AStmt);
12564   // 1.2.2 OpenMP Language Terminology
12565   // Structured block - An executable statement with a single entry at the
12566   // top and a single exit at the bottom.
12567   // The point of exit cannot be a branch out of the structured block.
12568   // longjmp() and throw() must not violate the entry/exit criteria.
12569   CS->getCapturedDecl()->setNothrow();
12570 
12571   for (int ThisCaptureLevel =
12572            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
12573        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12574     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12575     // 1.2.2 OpenMP Language Terminology
12576     // Structured block - An executable statement with a single entry at the
12577     // top and a single exit at the bottom.
12578     // The point of exit cannot be a branch out of the structured block.
12579     // longjmp() and throw() must not violate the entry/exit criteria.
12580     CS->getCapturedDecl()->setNothrow();
12581   }
12582 
12583   OMPLoopBasedDirective::HelperExprs B;
12584   // In presence of clause 'collapse' with number of loops, it will
12585   // define the nested loops number.
12586   unsigned NestedLoopCount = checkOpenMPLoop(
12587       OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
12588       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12589       VarsWithImplicitDSA, B);
12590 
12591   if (NestedLoopCount == 0)
12592     return StmtError();
12593 
12594   assert((CurContext->isDependentContext() || B.builtAll()) &&
12595          "omp for loop exprs were not built");
12596 
12597   setFunctionHasBranchProtectedScope();
12598 
12599   DSAStack->setParentTeamsRegionLoc(StartLoc);
12600 
12601   return OMPTeamsDistributeParallelForDirective::Create(
12602       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
12603       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
12604 }
12605 
12606 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
12607                                                  Stmt *AStmt,
12608                                                  SourceLocation StartLoc,
12609                                                  SourceLocation EndLoc) {
12610   if (!AStmt)
12611     return StmtError();
12612 
12613   auto *CS = cast<CapturedStmt>(AStmt);
12614   // 1.2.2 OpenMP Language Terminology
12615   // Structured block - An executable statement with a single entry at the
12616   // top and a single exit at the bottom.
12617   // The point of exit cannot be a branch out of the structured block.
12618   // longjmp() and throw() must not violate the entry/exit criteria.
12619   CS->getCapturedDecl()->setNothrow();
12620 
12621   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
12622        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12623     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12624     // 1.2.2 OpenMP Language Terminology
12625     // Structured block - An executable statement with a single entry at the
12626     // top and a single exit at the bottom.
12627     // The point of exit cannot be a branch out of the structured block.
12628     // longjmp() and throw() must not violate the entry/exit criteria.
12629     CS->getCapturedDecl()->setNothrow();
12630   }
12631   setFunctionHasBranchProtectedScope();
12632 
12633   return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
12634                                          AStmt);
12635 }
12636 
12637 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
12638     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12639     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12640   if (!AStmt)
12641     return StmtError();
12642 
12643   auto *CS = cast<CapturedStmt>(AStmt);
12644   // 1.2.2 OpenMP Language Terminology
12645   // Structured block - An executable statement with a single entry at the
12646   // top and a single exit at the bottom.
12647   // The point of exit cannot be a branch out of the structured block.
12648   // longjmp() and throw() must not violate the entry/exit criteria.
12649   CS->getCapturedDecl()->setNothrow();
12650   for (int ThisCaptureLevel =
12651            getOpenMPCaptureLevels(OMPD_target_teams_distribute);
12652        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12653     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12654     // 1.2.2 OpenMP Language Terminology
12655     // Structured block - An executable statement with a single entry at the
12656     // top and a single exit at the bottom.
12657     // The point of exit cannot be a branch out of the structured block.
12658     // longjmp() and throw() must not violate the entry/exit criteria.
12659     CS->getCapturedDecl()->setNothrow();
12660   }
12661 
12662   OMPLoopBasedDirective::HelperExprs B;
12663   // In presence of clause 'collapse' with number of loops, it will
12664   // define the nested loops number.
12665   unsigned NestedLoopCount = checkOpenMPLoop(
12666       OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
12667       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12668       VarsWithImplicitDSA, B);
12669   if (NestedLoopCount == 0)
12670     return StmtError();
12671 
12672   assert((CurContext->isDependentContext() || B.builtAll()) &&
12673          "omp target teams distribute loop exprs were not built");
12674 
12675   setFunctionHasBranchProtectedScope();
12676   return OMPTargetTeamsDistributeDirective::Create(
12677       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12678 }
12679 
12680 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
12681     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12682     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12683   if (!AStmt)
12684     return StmtError();
12685 
12686   auto *CS = cast<CapturedStmt>(AStmt);
12687   // 1.2.2 OpenMP Language Terminology
12688   // Structured block - An executable statement with a single entry at the
12689   // top and a single exit at the bottom.
12690   // The point of exit cannot be a branch out of the structured block.
12691   // longjmp() and throw() must not violate the entry/exit criteria.
12692   CS->getCapturedDecl()->setNothrow();
12693   for (int ThisCaptureLevel =
12694            getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
12695        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12696     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12697     // 1.2.2 OpenMP Language Terminology
12698     // Structured block - An executable statement with a single entry at the
12699     // top and a single exit at the bottom.
12700     // The point of exit cannot be a branch out of the structured block.
12701     // longjmp() and throw() must not violate the entry/exit criteria.
12702     CS->getCapturedDecl()->setNothrow();
12703   }
12704 
12705   OMPLoopBasedDirective::HelperExprs B;
12706   // In presence of clause 'collapse' with number of loops, it will
12707   // define the nested loops number.
12708   unsigned NestedLoopCount = checkOpenMPLoop(
12709       OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
12710       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12711       VarsWithImplicitDSA, B);
12712   if (NestedLoopCount == 0)
12713     return StmtError();
12714 
12715   assert((CurContext->isDependentContext() || B.builtAll()) &&
12716          "omp target teams distribute parallel for loop exprs were not built");
12717 
12718   if (!CurContext->isDependentContext()) {
12719     // Finalize the clauses that need pre-built expressions for CodeGen.
12720     for (OMPClause *C : Clauses) {
12721       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12722         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12723                                      B.NumIterations, *this, CurScope,
12724                                      DSAStack))
12725           return StmtError();
12726     }
12727   }
12728 
12729   setFunctionHasBranchProtectedScope();
12730   return OMPTargetTeamsDistributeParallelForDirective::Create(
12731       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
12732       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
12733 }
12734 
12735 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
12736     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12737     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12738   if (!AStmt)
12739     return StmtError();
12740 
12741   auto *CS = cast<CapturedStmt>(AStmt);
12742   // 1.2.2 OpenMP Language Terminology
12743   // Structured block - An executable statement with a single entry at the
12744   // top and a single exit at the bottom.
12745   // The point of exit cannot be a branch out of the structured block.
12746   // longjmp() and throw() must not violate the entry/exit criteria.
12747   CS->getCapturedDecl()->setNothrow();
12748   for (int ThisCaptureLevel = getOpenMPCaptureLevels(
12749            OMPD_target_teams_distribute_parallel_for_simd);
12750        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12751     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12752     // 1.2.2 OpenMP Language Terminology
12753     // Structured block - An executable statement with a single entry at the
12754     // top and a single exit at the bottom.
12755     // The point of exit cannot be a branch out of the structured block.
12756     // longjmp() and throw() must not violate the entry/exit criteria.
12757     CS->getCapturedDecl()->setNothrow();
12758   }
12759 
12760   OMPLoopBasedDirective::HelperExprs B;
12761   // In presence of clause 'collapse' with number of loops, it will
12762   // define the nested loops number.
12763   unsigned NestedLoopCount =
12764       checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
12765                       getCollapseNumberExpr(Clauses),
12766                       nullptr /*ordered not a clause on distribute*/, CS, *this,
12767                       *DSAStack, VarsWithImplicitDSA, B);
12768   if (NestedLoopCount == 0)
12769     return StmtError();
12770 
12771   assert((CurContext->isDependentContext() || B.builtAll()) &&
12772          "omp target teams distribute parallel for simd loop exprs were not "
12773          "built");
12774 
12775   if (!CurContext->isDependentContext()) {
12776     // Finalize the clauses that need pre-built expressions for CodeGen.
12777     for (OMPClause *C : Clauses) {
12778       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12779         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12780                                      B.NumIterations, *this, CurScope,
12781                                      DSAStack))
12782           return StmtError();
12783     }
12784   }
12785 
12786   if (checkSimdlenSafelenSpecified(*this, Clauses))
12787     return StmtError();
12788 
12789   setFunctionHasBranchProtectedScope();
12790   return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
12791       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12792 }
12793 
12794 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
12795     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12796     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12797   if (!AStmt)
12798     return StmtError();
12799 
12800   auto *CS = cast<CapturedStmt>(AStmt);
12801   // 1.2.2 OpenMP Language Terminology
12802   // Structured block - An executable statement with a single entry at the
12803   // top and a single exit at the bottom.
12804   // The point of exit cannot be a branch out of the structured block.
12805   // longjmp() and throw() must not violate the entry/exit criteria.
12806   CS->getCapturedDecl()->setNothrow();
12807   for (int ThisCaptureLevel =
12808            getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
12809        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12810     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12811     // 1.2.2 OpenMP Language Terminology
12812     // Structured block - An executable statement with a single entry at the
12813     // top and a single exit at the bottom.
12814     // The point of exit cannot be a branch out of the structured block.
12815     // longjmp() and throw() must not violate the entry/exit criteria.
12816     CS->getCapturedDecl()->setNothrow();
12817   }
12818 
12819   OMPLoopBasedDirective::HelperExprs B;
12820   // In presence of clause 'collapse' with number of loops, it will
12821   // define the nested loops number.
12822   unsigned NestedLoopCount = checkOpenMPLoop(
12823       OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
12824       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12825       VarsWithImplicitDSA, B);
12826   if (NestedLoopCount == 0)
12827     return StmtError();
12828 
12829   assert((CurContext->isDependentContext() || B.builtAll()) &&
12830          "omp target teams distribute simd loop exprs were not built");
12831 
12832   if (!CurContext->isDependentContext()) {
12833     // Finalize the clauses that need pre-built expressions for CodeGen.
12834     for (OMPClause *C : Clauses) {
12835       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12836         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12837                                      B.NumIterations, *this, CurScope,
12838                                      DSAStack))
12839           return StmtError();
12840     }
12841   }
12842 
12843   if (checkSimdlenSafelenSpecified(*this, Clauses))
12844     return StmtError();
12845 
12846   setFunctionHasBranchProtectedScope();
12847   return OMPTargetTeamsDistributeSimdDirective::Create(
12848       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12849 }
12850 
12851 bool Sema::checkTransformableLoopNest(
12852     OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops,
12853     SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers,
12854     Stmt *&Body,
12855     SmallVectorImpl<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>>
12856         &OriginalInits) {
12857   OriginalInits.emplace_back();
12858   bool Result = OMPLoopBasedDirective::doForAllLoops(
12859       AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops,
12860       [this, &LoopHelpers, &Body, &OriginalInits, Kind](unsigned Cnt,
12861                                                         Stmt *CurStmt) {
12862         VarsWithInheritedDSAType TmpDSA;
12863         unsigned SingleNumLoops =
12864             checkOpenMPLoop(Kind, nullptr, nullptr, CurStmt, *this, *DSAStack,
12865                             TmpDSA, LoopHelpers[Cnt]);
12866         if (SingleNumLoops == 0)
12867           return true;
12868         assert(SingleNumLoops == 1 && "Expect single loop iteration space");
12869         if (auto *For = dyn_cast<ForStmt>(CurStmt)) {
12870           OriginalInits.back().push_back(For->getInit());
12871           Body = For->getBody();
12872         } else {
12873           assert(isa<CXXForRangeStmt>(CurStmt) &&
12874                  "Expected canonical for or range-based for loops.");
12875           auto *CXXFor = cast<CXXForRangeStmt>(CurStmt);
12876           OriginalInits.back().push_back(CXXFor->getBeginStmt());
12877           Body = CXXFor->getBody();
12878         }
12879         OriginalInits.emplace_back();
12880         return false;
12881       },
12882       [&OriginalInits](OMPLoopBasedDirective *Transform) {
12883         Stmt *DependentPreInits;
12884         if (auto *Dir = dyn_cast<OMPTileDirective>(Transform))
12885           DependentPreInits = Dir->getPreInits();
12886         else if (auto *Dir = dyn_cast<OMPUnrollDirective>(Transform))
12887           DependentPreInits = Dir->getPreInits();
12888         else
12889           llvm_unreachable("Unhandled loop transformation");
12890         if (!DependentPreInits)
12891           return;
12892         for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup())
12893           OriginalInits.back().push_back(C);
12894       });
12895   assert(OriginalInits.back().empty() && "No preinit after innermost loop");
12896   OriginalInits.pop_back();
12897   return Result;
12898 }
12899 
12900 StmtResult Sema::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
12901                                           Stmt *AStmt, SourceLocation StartLoc,
12902                                           SourceLocation EndLoc) {
12903   auto SizesClauses =
12904       OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses);
12905   if (SizesClauses.empty()) {
12906     // A missing 'sizes' clause is already reported by the parser.
12907     return StmtError();
12908   }
12909   const OMPSizesClause *SizesClause = *SizesClauses.begin();
12910   unsigned NumLoops = SizesClause->getNumSizes();
12911 
12912   // Empty statement should only be possible if there already was an error.
12913   if (!AStmt)
12914     return StmtError();
12915 
12916   // Verify and diagnose loop nest.
12917   SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
12918   Stmt *Body = nullptr;
12919   SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, 4>
12920       OriginalInits;
12921   if (!checkTransformableLoopNest(OMPD_tile, AStmt, NumLoops, LoopHelpers, Body,
12922                                   OriginalInits))
12923     return StmtError();
12924 
12925   // Delay tiling to when template is completely instantiated.
12926   if (CurContext->isDependentContext())
12927     return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses,
12928                                     NumLoops, AStmt, nullptr, nullptr);
12929 
12930   SmallVector<Decl *, 4> PreInits;
12931 
12932   // Create iteration variables for the generated loops.
12933   SmallVector<VarDecl *, 4> FloorIndVars;
12934   SmallVector<VarDecl *, 4> TileIndVars;
12935   FloorIndVars.resize(NumLoops);
12936   TileIndVars.resize(NumLoops);
12937   for (unsigned I = 0; I < NumLoops; ++I) {
12938     OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
12939 
12940     assert(LoopHelper.Counters.size() == 1 &&
12941            "Expect single-dimensional loop iteration space");
12942     auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters.front());
12943     std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
12944     DeclRefExpr *IterVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef);
12945     QualType CntTy = IterVarRef->getType();
12946 
12947     // Iteration variable for the floor (i.e. outer) loop.
12948     {
12949       std::string FloorCntName =
12950           (Twine(".floor_") + llvm::utostr(I) + ".iv." + OrigVarName).str();
12951       VarDecl *FloorCntDecl =
12952           buildVarDecl(*this, {}, CntTy, FloorCntName, nullptr, OrigCntVar);
12953       FloorIndVars[I] = FloorCntDecl;
12954     }
12955 
12956     // Iteration variable for the tile (i.e. inner) loop.
12957     {
12958       std::string TileCntName =
12959           (Twine(".tile_") + llvm::utostr(I) + ".iv." + OrigVarName).str();
12960 
12961       // Reuse the iteration variable created by checkOpenMPLoop. It is also
12962       // used by the expressions to derive the original iteration variable's
12963       // value from the logical iteration number.
12964       auto *TileCntDecl = cast<VarDecl>(IterVarRef->getDecl());
12965       TileCntDecl->setDeclName(&PP.getIdentifierTable().get(TileCntName));
12966       TileIndVars[I] = TileCntDecl;
12967     }
12968     for (auto &P : OriginalInits[I]) {
12969       if (auto *D = P.dyn_cast<Decl *>())
12970         PreInits.push_back(D);
12971       else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>()))
12972         PreInits.append(PI->decl_begin(), PI->decl_end());
12973     }
12974     if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits))
12975       PreInits.append(PI->decl_begin(), PI->decl_end());
12976     // Gather declarations for the data members used as counters.
12977     for (Expr *CounterRef : LoopHelper.Counters) {
12978       auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl();
12979       if (isa<OMPCapturedExprDecl>(CounterDecl))
12980         PreInits.push_back(CounterDecl);
12981     }
12982   }
12983 
12984   // Once the original iteration values are set, append the innermost body.
12985   Stmt *Inner = Body;
12986 
12987   // Create tile loops from the inside to the outside.
12988   for (int I = NumLoops - 1; I >= 0; --I) {
12989     OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
12990     Expr *NumIterations = LoopHelper.NumIterations;
12991     auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]);
12992     QualType CntTy = OrigCntVar->getType();
12993     Expr *DimTileSize = SizesClause->getSizesRefs()[I];
12994     Scope *CurScope = getCurScope();
12995 
12996     // Commonly used variables.
12997     DeclRefExpr *TileIV = buildDeclRefExpr(*this, TileIndVars[I], CntTy,
12998                                            OrigCntVar->getExprLoc());
12999     DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy,
13000                                             OrigCntVar->getExprLoc());
13001 
13002     // For init-statement: auto .tile.iv = .floor.iv
13003     AddInitializerToDecl(TileIndVars[I], DefaultLvalueConversion(FloorIV).get(),
13004                          /*DirectInit=*/false);
13005     Decl *CounterDecl = TileIndVars[I];
13006     StmtResult InitStmt = new (Context)
13007         DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1),
13008                  OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
13009     if (!InitStmt.isUsable())
13010       return StmtError();
13011 
13012     // For cond-expression: .tile.iv < min(.floor.iv + DimTileSize,
13013     // NumIterations)
13014     ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(),
13015                                       BO_Add, FloorIV, DimTileSize);
13016     if (!EndOfTile.isUsable())
13017       return StmtError();
13018     ExprResult IsPartialTile =
13019         BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT,
13020                    NumIterations, EndOfTile.get());
13021     if (!IsPartialTile.isUsable())
13022       return StmtError();
13023     ExprResult MinTileAndIterSpace = ActOnConditionalOp(
13024         LoopHelper.Cond->getBeginLoc(), LoopHelper.Cond->getEndLoc(),
13025         IsPartialTile.get(), NumIterations, EndOfTile.get());
13026     if (!MinTileAndIterSpace.isUsable())
13027       return StmtError();
13028     ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(),
13029                                      BO_LT, TileIV, MinTileAndIterSpace.get());
13030     if (!CondExpr.isUsable())
13031       return StmtError();
13032 
13033     // For incr-statement: ++.tile.iv
13034     ExprResult IncrStmt =
13035         BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV);
13036     if (!IncrStmt.isUsable())
13037       return StmtError();
13038 
13039     // Statements to set the original iteration variable's value from the
13040     // logical iteration number.
13041     // Generated for loop is:
13042     // Original_for_init;
13043     // for (auto .tile.iv = .floor.iv; .tile.iv < min(.floor.iv + DimTileSize,
13044     // NumIterations); ++.tile.iv) {
13045     //   Original_Body;
13046     //   Original_counter_update;
13047     // }
13048     // FIXME: If the innermost body is an loop itself, inserting these
13049     // statements stops it being recognized  as a perfectly nested loop (e.g.
13050     // for applying tiling again). If this is the case, sink the expressions
13051     // further into the inner loop.
13052     SmallVector<Stmt *, 4> BodyParts;
13053     BodyParts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end());
13054     BodyParts.push_back(Inner);
13055     Inner = CompoundStmt::Create(Context, BodyParts, Inner->getBeginLoc(),
13056                                  Inner->getEndLoc());
13057     Inner = new (Context)
13058         ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
13059                 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
13060                 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
13061   }
13062 
13063   // Create floor loops from the inside to the outside.
13064   for (int I = NumLoops - 1; I >= 0; --I) {
13065     auto &LoopHelper = LoopHelpers[I];
13066     Expr *NumIterations = LoopHelper.NumIterations;
13067     DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]);
13068     QualType CntTy = OrigCntVar->getType();
13069     Expr *DimTileSize = SizesClause->getSizesRefs()[I];
13070     Scope *CurScope = getCurScope();
13071 
13072     // Commonly used variables.
13073     DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy,
13074                                             OrigCntVar->getExprLoc());
13075 
13076     // For init-statement: auto .floor.iv = 0
13077     AddInitializerToDecl(
13078         FloorIndVars[I],
13079         ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(),
13080         /*DirectInit=*/false);
13081     Decl *CounterDecl = FloorIndVars[I];
13082     StmtResult InitStmt = new (Context)
13083         DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1),
13084                  OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
13085     if (!InitStmt.isUsable())
13086       return StmtError();
13087 
13088     // For cond-expression: .floor.iv < NumIterations
13089     ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(),
13090                                      BO_LT, FloorIV, NumIterations);
13091     if (!CondExpr.isUsable())
13092       return StmtError();
13093 
13094     // For incr-statement: .floor.iv += DimTileSize
13095     ExprResult IncrStmt = BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(),
13096                                      BO_AddAssign, FloorIV, DimTileSize);
13097     if (!IncrStmt.isUsable())
13098       return StmtError();
13099 
13100     Inner = new (Context)
13101         ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
13102                 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
13103                 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
13104   }
13105 
13106   return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, NumLoops,
13107                                   AStmt, Inner,
13108                                   buildPreInits(Context, PreInits));
13109 }
13110 
13111 StmtResult Sema::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses,
13112                                             Stmt *AStmt,
13113                                             SourceLocation StartLoc,
13114                                             SourceLocation EndLoc) {
13115   // Empty statement should only be possible if there already was an error.
13116   if (!AStmt)
13117     return StmtError();
13118 
13119   if (checkMutuallyExclusiveClauses(*this, Clauses, {OMPC_partial, OMPC_full}))
13120     return StmtError();
13121 
13122   const OMPFullClause *FullClause =
13123       OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses);
13124   const OMPPartialClause *PartialClause =
13125       OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses);
13126   assert(!(FullClause && PartialClause) &&
13127          "mutual exclusivity must have been checked before");
13128 
13129   constexpr unsigned NumLoops = 1;
13130   Stmt *Body = nullptr;
13131   SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
13132       NumLoops);
13133   SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, NumLoops + 1>
13134       OriginalInits;
13135   if (!checkTransformableLoopNest(OMPD_unroll, AStmt, NumLoops, LoopHelpers,
13136                                   Body, OriginalInits))
13137     return StmtError();
13138 
13139   unsigned NumGeneratedLoops = PartialClause ? 1 : 0;
13140 
13141   // Delay unrolling to when template is completely instantiated.
13142   if (CurContext->isDependentContext())
13143     return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
13144                                       NumGeneratedLoops, nullptr, nullptr);
13145 
13146   OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
13147 
13148   if (FullClause) {
13149     if (!VerifyPositiveIntegerConstantInClause(
13150              LoopHelper.NumIterations, OMPC_full, /*StrictlyPositive=*/false,
13151              /*SuppressExprDigs=*/true)
13152              .isUsable()) {
13153       Diag(AStmt->getBeginLoc(), diag::err_omp_unroll_full_variable_trip_count);
13154       Diag(FullClause->getBeginLoc(), diag::note_omp_directive_here)
13155           << "#pragma omp unroll full";
13156       return StmtError();
13157     }
13158   }
13159 
13160   // The generated loop may only be passed to other loop-associated directive
13161   // when a partial clause is specified. Without the requirement it is
13162   // sufficient to generate loop unroll metadata at code-generation.
13163   if (NumGeneratedLoops == 0)
13164     return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
13165                                       NumGeneratedLoops, nullptr, nullptr);
13166 
13167   // Otherwise, we need to provide a de-sugared/transformed AST that can be
13168   // associated with another loop directive.
13169   //
13170   // The canonical loop analysis return by checkTransformableLoopNest assumes
13171   // the following structure to be the same loop without transformations or
13172   // directives applied: \code OriginalInits; LoopHelper.PreInits;
13173   // LoopHelper.Counters;
13174   // for (; IV < LoopHelper.NumIterations; ++IV) {
13175   //   LoopHelper.Updates;
13176   //   Body;
13177   // }
13178   // \endcode
13179   // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits
13180   // and referenced by LoopHelper.IterationVarRef.
13181   //
13182   // The unrolling directive transforms this into the following loop:
13183   // \code
13184   // OriginalInits;         \
13185   // LoopHelper.PreInits;    > NewPreInits
13186   // LoopHelper.Counters;   /
13187   // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) {
13188   //   #pragma clang loop unroll_count(Factor)
13189   //   for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV)
13190   //   {
13191   //     LoopHelper.Updates;
13192   //     Body;
13193   //   }
13194   // }
13195   // \endcode
13196   // where UIV is a new logical iteration counter. IV must be the same VarDecl
13197   // as the original LoopHelper.IterationVarRef because LoopHelper.Updates
13198   // references it. If the partially unrolled loop is associated with another
13199   // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to
13200   // analyze this loop, i.e. the outer loop must fulfill the constraints of an
13201   // OpenMP canonical loop. The inner loop is not an associable canonical loop
13202   // and only exists to defer its unrolling to LLVM's LoopUnroll instead of
13203   // doing it in the frontend (by adding loop metadata). NewPreInits becomes a
13204   // property of the OMPLoopBasedDirective instead of statements in
13205   // CompoundStatement. This is to allow the loop to become a non-outermost loop
13206   // of a canonical loop nest where these PreInits are emitted before the
13207   // outermost directive.
13208 
13209   // Determine the PreInit declarations.
13210   SmallVector<Decl *, 4> PreInits;
13211   assert(OriginalInits.size() == 1 &&
13212          "Expecting a single-dimensional loop iteration space");
13213   for (auto &P : OriginalInits[0]) {
13214     if (auto *D = P.dyn_cast<Decl *>())
13215       PreInits.push_back(D);
13216     else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>()))
13217       PreInits.append(PI->decl_begin(), PI->decl_end());
13218   }
13219   if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits))
13220     PreInits.append(PI->decl_begin(), PI->decl_end());
13221   // Gather declarations for the data members used as counters.
13222   for (Expr *CounterRef : LoopHelper.Counters) {
13223     auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl();
13224     if (isa<OMPCapturedExprDecl>(CounterDecl))
13225       PreInits.push_back(CounterDecl);
13226   }
13227 
13228   auto *IterationVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef);
13229   QualType IVTy = IterationVarRef->getType();
13230   assert(LoopHelper.Counters.size() == 1 &&
13231          "Expecting a single-dimensional loop iteration space");
13232   auto *OrigVar = cast<DeclRefExpr>(LoopHelper.Counters.front());
13233 
13234   // Determine the unroll factor.
13235   uint64_t Factor;
13236   SourceLocation FactorLoc;
13237   if (Expr *FactorVal = PartialClause->getFactor()) {
13238     Factor =
13239         FactorVal->getIntegerConstantExpr(Context).getValue().getZExtValue();
13240     FactorLoc = FactorVal->getExprLoc();
13241   } else {
13242     // TODO: Use a better profitability model.
13243     Factor = 2;
13244   }
13245   assert(Factor > 0 && "Expected positive unroll factor");
13246   auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() {
13247     return IntegerLiteral::Create(
13248         Context, llvm::APInt(Context.getIntWidth(IVTy), Factor), IVTy,
13249         FactorLoc);
13250   };
13251 
13252   // Iteration variable SourceLocations.
13253   SourceLocation OrigVarLoc = OrigVar->getExprLoc();
13254   SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
13255   SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
13256 
13257   // Internal variable names.
13258   std::string OrigVarName = OrigVar->getNameInfo().getAsString();
13259   std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str();
13260   std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str();
13261   std::string InnerTripCountName =
13262       (Twine(".unroll_inner.tripcount.") + OrigVarName).str();
13263 
13264   // Create the iteration variable for the unrolled loop.
13265   VarDecl *OuterIVDecl =
13266       buildVarDecl(*this, {}, IVTy, OuterIVName, nullptr, OrigVar);
13267   auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() {
13268     return buildDeclRefExpr(*this, OuterIVDecl, IVTy, OrigVarLoc);
13269   };
13270 
13271   // Iteration variable for the inner loop: Reuse the iteration variable created
13272   // by checkOpenMPLoop.
13273   auto *InnerIVDecl = cast<VarDecl>(IterationVarRef->getDecl());
13274   InnerIVDecl->setDeclName(&PP.getIdentifierTable().get(InnerIVName));
13275   auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() {
13276     return buildDeclRefExpr(*this, InnerIVDecl, IVTy, OrigVarLoc);
13277   };
13278 
13279   // Make a copy of the NumIterations expression for each use: By the AST
13280   // constraints, every expression object in a DeclContext must be unique.
13281   CaptureVars CopyTransformer(*this);
13282   auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * {
13283     return AssertSuccess(
13284         CopyTransformer.TransformExpr(LoopHelper.NumIterations));
13285   };
13286 
13287   // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv
13288   ExprResult LValueConv = DefaultLvalueConversion(MakeOuterRef());
13289   AddInitializerToDecl(InnerIVDecl, LValueConv.get(), /*DirectInit=*/false);
13290   StmtResult InnerInit = new (Context)
13291       DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd);
13292   if (!InnerInit.isUsable())
13293     return StmtError();
13294 
13295   // Inner For cond-expression:
13296   // \code
13297   //   .unroll_inner.iv < .unrolled.iv + Factor &&
13298   //   .unroll_inner.iv < NumIterations
13299   // \endcode
13300   // This conjunction of two conditions allows ScalarEvolution to derive the
13301   // maximum trip count of the inner loop.
13302   ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(),
13303                                     BO_Add, MakeOuterRef(), MakeFactorExpr());
13304   if (!EndOfTile.isUsable())
13305     return StmtError();
13306   ExprResult InnerCond1 = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(),
13307                                      BO_LE, MakeInnerRef(), EndOfTile.get());
13308   if (!InnerCond1.isUsable())
13309     return StmtError();
13310   ExprResult InnerCond2 =
13311       BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LE, MakeInnerRef(),
13312                  MakeNumIterations());
13313   if (!InnerCond2.isUsable())
13314     return StmtError();
13315   ExprResult InnerCond =
13316       BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LAnd,
13317                  InnerCond1.get(), InnerCond2.get());
13318   if (!InnerCond.isUsable())
13319     return StmtError();
13320 
13321   // Inner For incr-statement: ++.unroll_inner.iv
13322   ExprResult InnerIncr = BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(),
13323                                       UO_PreInc, MakeInnerRef());
13324   if (!InnerIncr.isUsable())
13325     return StmtError();
13326 
13327   // Inner For statement.
13328   SmallVector<Stmt *> InnerBodyStmts;
13329   InnerBodyStmts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end());
13330   InnerBodyStmts.push_back(Body);
13331   CompoundStmt *InnerBody = CompoundStmt::Create(
13332       Context, InnerBodyStmts, Body->getBeginLoc(), Body->getEndLoc());
13333   ForStmt *InnerFor = new (Context)
13334       ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr,
13335               InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(),
13336               LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
13337 
13338   // Unroll metadata for the inner loop.
13339   // This needs to take into account the remainder portion of the unrolled loop,
13340   // hence `unroll(full)` does not apply here, even though the LoopUnroll pass
13341   // supports multiple loop exits. Instead, unroll using a factor equivalent to
13342   // the maximum trip count, which will also generate a remainder loop. Just
13343   // `unroll(enable)` (which could have been useful if the user has not
13344   // specified a concrete factor; even though the outer loop cannot be
13345   // influenced anymore, would avoid more code bloat than necessary) will refuse
13346   // the loop because "Won't unroll; remainder loop could not be generated when
13347   // assuming runtime trip count". Even if it did work, it must not choose a
13348   // larger unroll factor than the maximum loop length, or it would always just
13349   // execute the remainder loop.
13350   LoopHintAttr *UnrollHintAttr =
13351       LoopHintAttr::CreateImplicit(Context, LoopHintAttr::UnrollCount,
13352                                    LoopHintAttr::Numeric, MakeFactorExpr());
13353   AttributedStmt *InnerUnrolled =
13354       AttributedStmt::Create(Context, StartLoc, {UnrollHintAttr}, InnerFor);
13355 
13356   // Outer For init-statement: auto .unrolled.iv = 0
13357   AddInitializerToDecl(
13358       OuterIVDecl, ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(),
13359       /*DirectInit=*/false);
13360   StmtResult OuterInit = new (Context)
13361       DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd);
13362   if (!OuterInit.isUsable())
13363     return StmtError();
13364 
13365   // Outer For cond-expression: .unrolled.iv < NumIterations
13366   ExprResult OuterConde =
13367       BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, MakeOuterRef(),
13368                  MakeNumIterations());
13369   if (!OuterConde.isUsable())
13370     return StmtError();
13371 
13372   // Outer For incr-statement: .unrolled.iv += Factor
13373   ExprResult OuterIncr =
13374       BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), BO_AddAssign,
13375                  MakeOuterRef(), MakeFactorExpr());
13376   if (!OuterIncr.isUsable())
13377     return StmtError();
13378 
13379   // Outer For statement.
13380   ForStmt *OuterFor = new (Context)
13381       ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr,
13382               OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(),
13383               LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
13384 
13385   return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
13386                                     NumGeneratedLoops, OuterFor,
13387                                     buildPreInits(Context, PreInits));
13388 }
13389 
13390 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
13391                                              SourceLocation StartLoc,
13392                                              SourceLocation LParenLoc,
13393                                              SourceLocation EndLoc) {
13394   OMPClause *Res = nullptr;
13395   switch (Kind) {
13396   case OMPC_final:
13397     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
13398     break;
13399   case OMPC_num_threads:
13400     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
13401     break;
13402   case OMPC_safelen:
13403     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
13404     break;
13405   case OMPC_simdlen:
13406     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
13407     break;
13408   case OMPC_allocator:
13409     Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc);
13410     break;
13411   case OMPC_collapse:
13412     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
13413     break;
13414   case OMPC_ordered:
13415     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
13416     break;
13417   case OMPC_num_teams:
13418     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
13419     break;
13420   case OMPC_thread_limit:
13421     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
13422     break;
13423   case OMPC_priority:
13424     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
13425     break;
13426   case OMPC_grainsize:
13427     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
13428     break;
13429   case OMPC_num_tasks:
13430     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
13431     break;
13432   case OMPC_hint:
13433     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
13434     break;
13435   case OMPC_depobj:
13436     Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc);
13437     break;
13438   case OMPC_detach:
13439     Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc);
13440     break;
13441   case OMPC_novariants:
13442     Res = ActOnOpenMPNovariantsClause(Expr, StartLoc, LParenLoc, EndLoc);
13443     break;
13444   case OMPC_nocontext:
13445     Res = ActOnOpenMPNocontextClause(Expr, StartLoc, LParenLoc, EndLoc);
13446     break;
13447   case OMPC_filter:
13448     Res = ActOnOpenMPFilterClause(Expr, StartLoc, LParenLoc, EndLoc);
13449     break;
13450   case OMPC_partial:
13451     Res = ActOnOpenMPPartialClause(Expr, StartLoc, LParenLoc, EndLoc);
13452     break;
13453   case OMPC_align:
13454     Res = ActOnOpenMPAlignClause(Expr, StartLoc, LParenLoc, EndLoc);
13455     break;
13456   case OMPC_device:
13457   case OMPC_if:
13458   case OMPC_default:
13459   case OMPC_proc_bind:
13460   case OMPC_schedule:
13461   case OMPC_private:
13462   case OMPC_firstprivate:
13463   case OMPC_lastprivate:
13464   case OMPC_shared:
13465   case OMPC_reduction:
13466   case OMPC_task_reduction:
13467   case OMPC_in_reduction:
13468   case OMPC_linear:
13469   case OMPC_aligned:
13470   case OMPC_copyin:
13471   case OMPC_copyprivate:
13472   case OMPC_nowait:
13473   case OMPC_untied:
13474   case OMPC_mergeable:
13475   case OMPC_threadprivate:
13476   case OMPC_sizes:
13477   case OMPC_allocate:
13478   case OMPC_flush:
13479   case OMPC_read:
13480   case OMPC_write:
13481   case OMPC_update:
13482   case OMPC_capture:
13483   case OMPC_compare:
13484   case OMPC_seq_cst:
13485   case OMPC_acq_rel:
13486   case OMPC_acquire:
13487   case OMPC_release:
13488   case OMPC_relaxed:
13489   case OMPC_depend:
13490   case OMPC_threads:
13491   case OMPC_simd:
13492   case OMPC_map:
13493   case OMPC_nogroup:
13494   case OMPC_dist_schedule:
13495   case OMPC_defaultmap:
13496   case OMPC_unknown:
13497   case OMPC_uniform:
13498   case OMPC_to:
13499   case OMPC_from:
13500   case OMPC_use_device_ptr:
13501   case OMPC_use_device_addr:
13502   case OMPC_is_device_ptr:
13503   case OMPC_unified_address:
13504   case OMPC_unified_shared_memory:
13505   case OMPC_reverse_offload:
13506   case OMPC_dynamic_allocators:
13507   case OMPC_atomic_default_mem_order:
13508   case OMPC_device_type:
13509   case OMPC_match:
13510   case OMPC_nontemporal:
13511   case OMPC_order:
13512   case OMPC_destroy:
13513   case OMPC_inclusive:
13514   case OMPC_exclusive:
13515   case OMPC_uses_allocators:
13516   case OMPC_affinity:
13517   case OMPC_when:
13518   case OMPC_bind:
13519   default:
13520     llvm_unreachable("Clause is not allowed.");
13521   }
13522   return Res;
13523 }
13524 
13525 // An OpenMP directive such as 'target parallel' has two captured regions:
13526 // for the 'target' and 'parallel' respectively.  This function returns
13527 // the region in which to capture expressions associated with a clause.
13528 // A return value of OMPD_unknown signifies that the expression should not
13529 // be captured.
13530 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
13531     OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion,
13532     OpenMPDirectiveKind NameModifier = OMPD_unknown) {
13533   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
13534   switch (CKind) {
13535   case OMPC_if:
13536     switch (DKind) {
13537     case OMPD_target_parallel_for_simd:
13538       if (OpenMPVersion >= 50 &&
13539           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) {
13540         CaptureRegion = OMPD_parallel;
13541         break;
13542       }
13543       LLVM_FALLTHROUGH;
13544     case OMPD_target_parallel:
13545     case OMPD_target_parallel_for:
13546       // If this clause applies to the nested 'parallel' region, capture within
13547       // the 'target' region, otherwise do not capture.
13548       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
13549         CaptureRegion = OMPD_target;
13550       break;
13551     case OMPD_target_teams_distribute_parallel_for_simd:
13552       if (OpenMPVersion >= 50 &&
13553           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) {
13554         CaptureRegion = OMPD_parallel;
13555         break;
13556       }
13557       LLVM_FALLTHROUGH;
13558     case OMPD_target_teams_distribute_parallel_for:
13559       // If this clause applies to the nested 'parallel' region, capture within
13560       // the 'teams' region, otherwise do not capture.
13561       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
13562         CaptureRegion = OMPD_teams;
13563       break;
13564     case OMPD_teams_distribute_parallel_for_simd:
13565       if (OpenMPVersion >= 50 &&
13566           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) {
13567         CaptureRegion = OMPD_parallel;
13568         break;
13569       }
13570       LLVM_FALLTHROUGH;
13571     case OMPD_teams_distribute_parallel_for:
13572       CaptureRegion = OMPD_teams;
13573       break;
13574     case OMPD_target_update:
13575     case OMPD_target_enter_data:
13576     case OMPD_target_exit_data:
13577       CaptureRegion = OMPD_task;
13578       break;
13579     case OMPD_parallel_master_taskloop:
13580       if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop)
13581         CaptureRegion = OMPD_parallel;
13582       break;
13583     case OMPD_parallel_master_taskloop_simd:
13584       if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) ||
13585           NameModifier == OMPD_taskloop) {
13586         CaptureRegion = OMPD_parallel;
13587         break;
13588       }
13589       if (OpenMPVersion <= 45)
13590         break;
13591       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
13592         CaptureRegion = OMPD_taskloop;
13593       break;
13594     case OMPD_parallel_for_simd:
13595       if (OpenMPVersion <= 45)
13596         break;
13597       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
13598         CaptureRegion = OMPD_parallel;
13599       break;
13600     case OMPD_taskloop_simd:
13601     case OMPD_master_taskloop_simd:
13602       if (OpenMPVersion <= 45)
13603         break;
13604       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
13605         CaptureRegion = OMPD_taskloop;
13606       break;
13607     case OMPD_distribute_parallel_for_simd:
13608       if (OpenMPVersion <= 45)
13609         break;
13610       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
13611         CaptureRegion = OMPD_parallel;
13612       break;
13613     case OMPD_target_simd:
13614       if (OpenMPVersion >= 50 &&
13615           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd))
13616         CaptureRegion = OMPD_target;
13617       break;
13618     case OMPD_teams_distribute_simd:
13619     case OMPD_target_teams_distribute_simd:
13620       if (OpenMPVersion >= 50 &&
13621           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd))
13622         CaptureRegion = OMPD_teams;
13623       break;
13624     case OMPD_cancel:
13625     case OMPD_parallel:
13626     case OMPD_parallel_master:
13627     case OMPD_parallel_sections:
13628     case OMPD_parallel_for:
13629     case OMPD_target:
13630     case OMPD_target_teams:
13631     case OMPD_target_teams_distribute:
13632     case OMPD_distribute_parallel_for:
13633     case OMPD_task:
13634     case OMPD_taskloop:
13635     case OMPD_master_taskloop:
13636     case OMPD_target_data:
13637     case OMPD_simd:
13638     case OMPD_for_simd:
13639     case OMPD_distribute_simd:
13640       // Do not capture if-clause expressions.
13641       break;
13642     case OMPD_threadprivate:
13643     case OMPD_allocate:
13644     case OMPD_taskyield:
13645     case OMPD_barrier:
13646     case OMPD_taskwait:
13647     case OMPD_cancellation_point:
13648     case OMPD_flush:
13649     case OMPD_depobj:
13650     case OMPD_scan:
13651     case OMPD_declare_reduction:
13652     case OMPD_declare_mapper:
13653     case OMPD_declare_simd:
13654     case OMPD_declare_variant:
13655     case OMPD_begin_declare_variant:
13656     case OMPD_end_declare_variant:
13657     case OMPD_declare_target:
13658     case OMPD_end_declare_target:
13659     case OMPD_loop:
13660     case OMPD_teams:
13661     case OMPD_tile:
13662     case OMPD_unroll:
13663     case OMPD_for:
13664     case OMPD_sections:
13665     case OMPD_section:
13666     case OMPD_single:
13667     case OMPD_master:
13668     case OMPD_masked:
13669     case OMPD_critical:
13670     case OMPD_taskgroup:
13671     case OMPD_distribute:
13672     case OMPD_ordered:
13673     case OMPD_atomic:
13674     case OMPD_teams_distribute:
13675     case OMPD_requires:
13676     case OMPD_metadirective:
13677       llvm_unreachable("Unexpected OpenMP directive with if-clause");
13678     case OMPD_unknown:
13679     default:
13680       llvm_unreachable("Unknown OpenMP directive");
13681     }
13682     break;
13683   case OMPC_num_threads:
13684     switch (DKind) {
13685     case OMPD_target_parallel:
13686     case OMPD_target_parallel_for:
13687     case OMPD_target_parallel_for_simd:
13688       CaptureRegion = OMPD_target;
13689       break;
13690     case OMPD_teams_distribute_parallel_for:
13691     case OMPD_teams_distribute_parallel_for_simd:
13692     case OMPD_target_teams_distribute_parallel_for:
13693     case OMPD_target_teams_distribute_parallel_for_simd:
13694       CaptureRegion = OMPD_teams;
13695       break;
13696     case OMPD_parallel:
13697     case OMPD_parallel_master:
13698     case OMPD_parallel_sections:
13699     case OMPD_parallel_for:
13700     case OMPD_parallel_for_simd:
13701     case OMPD_distribute_parallel_for:
13702     case OMPD_distribute_parallel_for_simd:
13703     case OMPD_parallel_master_taskloop:
13704     case OMPD_parallel_master_taskloop_simd:
13705       // Do not capture num_threads-clause expressions.
13706       break;
13707     case OMPD_target_data:
13708     case OMPD_target_enter_data:
13709     case OMPD_target_exit_data:
13710     case OMPD_target_update:
13711     case OMPD_target:
13712     case OMPD_target_simd:
13713     case OMPD_target_teams:
13714     case OMPD_target_teams_distribute:
13715     case OMPD_target_teams_distribute_simd:
13716     case OMPD_cancel:
13717     case OMPD_task:
13718     case OMPD_taskloop:
13719     case OMPD_taskloop_simd:
13720     case OMPD_master_taskloop:
13721     case OMPD_master_taskloop_simd:
13722     case OMPD_threadprivate:
13723     case OMPD_allocate:
13724     case OMPD_taskyield:
13725     case OMPD_barrier:
13726     case OMPD_taskwait:
13727     case OMPD_cancellation_point:
13728     case OMPD_flush:
13729     case OMPD_depobj:
13730     case OMPD_scan:
13731     case OMPD_declare_reduction:
13732     case OMPD_declare_mapper:
13733     case OMPD_declare_simd:
13734     case OMPD_declare_variant:
13735     case OMPD_begin_declare_variant:
13736     case OMPD_end_declare_variant:
13737     case OMPD_declare_target:
13738     case OMPD_end_declare_target:
13739     case OMPD_loop:
13740     case OMPD_teams:
13741     case OMPD_simd:
13742     case OMPD_tile:
13743     case OMPD_unroll:
13744     case OMPD_for:
13745     case OMPD_for_simd:
13746     case OMPD_sections:
13747     case OMPD_section:
13748     case OMPD_single:
13749     case OMPD_master:
13750     case OMPD_masked:
13751     case OMPD_critical:
13752     case OMPD_taskgroup:
13753     case OMPD_distribute:
13754     case OMPD_ordered:
13755     case OMPD_atomic:
13756     case OMPD_distribute_simd:
13757     case OMPD_teams_distribute:
13758     case OMPD_teams_distribute_simd:
13759     case OMPD_requires:
13760     case OMPD_metadirective:
13761       llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
13762     case OMPD_unknown:
13763     default:
13764       llvm_unreachable("Unknown OpenMP directive");
13765     }
13766     break;
13767   case OMPC_num_teams:
13768     switch (DKind) {
13769     case OMPD_target_teams:
13770     case OMPD_target_teams_distribute:
13771     case OMPD_target_teams_distribute_simd:
13772     case OMPD_target_teams_distribute_parallel_for:
13773     case OMPD_target_teams_distribute_parallel_for_simd:
13774       CaptureRegion = OMPD_target;
13775       break;
13776     case OMPD_teams_distribute_parallel_for:
13777     case OMPD_teams_distribute_parallel_for_simd:
13778     case OMPD_teams:
13779     case OMPD_teams_distribute:
13780     case OMPD_teams_distribute_simd:
13781       // Do not capture num_teams-clause expressions.
13782       break;
13783     case OMPD_distribute_parallel_for:
13784     case OMPD_distribute_parallel_for_simd:
13785     case OMPD_task:
13786     case OMPD_taskloop:
13787     case OMPD_taskloop_simd:
13788     case OMPD_master_taskloop:
13789     case OMPD_master_taskloop_simd:
13790     case OMPD_parallel_master_taskloop:
13791     case OMPD_parallel_master_taskloop_simd:
13792     case OMPD_target_data:
13793     case OMPD_target_enter_data:
13794     case OMPD_target_exit_data:
13795     case OMPD_target_update:
13796     case OMPD_cancel:
13797     case OMPD_parallel:
13798     case OMPD_parallel_master:
13799     case OMPD_parallel_sections:
13800     case OMPD_parallel_for:
13801     case OMPD_parallel_for_simd:
13802     case OMPD_target:
13803     case OMPD_target_simd:
13804     case OMPD_target_parallel:
13805     case OMPD_target_parallel_for:
13806     case OMPD_target_parallel_for_simd:
13807     case OMPD_threadprivate:
13808     case OMPD_allocate:
13809     case OMPD_taskyield:
13810     case OMPD_barrier:
13811     case OMPD_taskwait:
13812     case OMPD_cancellation_point:
13813     case OMPD_flush:
13814     case OMPD_depobj:
13815     case OMPD_scan:
13816     case OMPD_declare_reduction:
13817     case OMPD_declare_mapper:
13818     case OMPD_declare_simd:
13819     case OMPD_declare_variant:
13820     case OMPD_begin_declare_variant:
13821     case OMPD_end_declare_variant:
13822     case OMPD_declare_target:
13823     case OMPD_end_declare_target:
13824     case OMPD_loop:
13825     case OMPD_simd:
13826     case OMPD_tile:
13827     case OMPD_unroll:
13828     case OMPD_for:
13829     case OMPD_for_simd:
13830     case OMPD_sections:
13831     case OMPD_section:
13832     case OMPD_single:
13833     case OMPD_master:
13834     case OMPD_masked:
13835     case OMPD_critical:
13836     case OMPD_taskgroup:
13837     case OMPD_distribute:
13838     case OMPD_ordered:
13839     case OMPD_atomic:
13840     case OMPD_distribute_simd:
13841     case OMPD_requires:
13842     case OMPD_metadirective:
13843       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
13844     case OMPD_unknown:
13845     default:
13846       llvm_unreachable("Unknown OpenMP directive");
13847     }
13848     break;
13849   case OMPC_thread_limit:
13850     switch (DKind) {
13851     case OMPD_target_teams:
13852     case OMPD_target_teams_distribute:
13853     case OMPD_target_teams_distribute_simd:
13854     case OMPD_target_teams_distribute_parallel_for:
13855     case OMPD_target_teams_distribute_parallel_for_simd:
13856       CaptureRegion = OMPD_target;
13857       break;
13858     case OMPD_teams_distribute_parallel_for:
13859     case OMPD_teams_distribute_parallel_for_simd:
13860     case OMPD_teams:
13861     case OMPD_teams_distribute:
13862     case OMPD_teams_distribute_simd:
13863       // Do not capture thread_limit-clause expressions.
13864       break;
13865     case OMPD_distribute_parallel_for:
13866     case OMPD_distribute_parallel_for_simd:
13867     case OMPD_task:
13868     case OMPD_taskloop:
13869     case OMPD_taskloop_simd:
13870     case OMPD_master_taskloop:
13871     case OMPD_master_taskloop_simd:
13872     case OMPD_parallel_master_taskloop:
13873     case OMPD_parallel_master_taskloop_simd:
13874     case OMPD_target_data:
13875     case OMPD_target_enter_data:
13876     case OMPD_target_exit_data:
13877     case OMPD_target_update:
13878     case OMPD_cancel:
13879     case OMPD_parallel:
13880     case OMPD_parallel_master:
13881     case OMPD_parallel_sections:
13882     case OMPD_parallel_for:
13883     case OMPD_parallel_for_simd:
13884     case OMPD_target:
13885     case OMPD_target_simd:
13886     case OMPD_target_parallel:
13887     case OMPD_target_parallel_for:
13888     case OMPD_target_parallel_for_simd:
13889     case OMPD_threadprivate:
13890     case OMPD_allocate:
13891     case OMPD_taskyield:
13892     case OMPD_barrier:
13893     case OMPD_taskwait:
13894     case OMPD_cancellation_point:
13895     case OMPD_flush:
13896     case OMPD_depobj:
13897     case OMPD_scan:
13898     case OMPD_declare_reduction:
13899     case OMPD_declare_mapper:
13900     case OMPD_declare_simd:
13901     case OMPD_declare_variant:
13902     case OMPD_begin_declare_variant:
13903     case OMPD_end_declare_variant:
13904     case OMPD_declare_target:
13905     case OMPD_end_declare_target:
13906     case OMPD_loop:
13907     case OMPD_simd:
13908     case OMPD_tile:
13909     case OMPD_unroll:
13910     case OMPD_for:
13911     case OMPD_for_simd:
13912     case OMPD_sections:
13913     case OMPD_section:
13914     case OMPD_single:
13915     case OMPD_master:
13916     case OMPD_masked:
13917     case OMPD_critical:
13918     case OMPD_taskgroup:
13919     case OMPD_distribute:
13920     case OMPD_ordered:
13921     case OMPD_atomic:
13922     case OMPD_distribute_simd:
13923     case OMPD_requires:
13924     case OMPD_metadirective:
13925       llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
13926     case OMPD_unknown:
13927     default:
13928       llvm_unreachable("Unknown OpenMP directive");
13929     }
13930     break;
13931   case OMPC_schedule:
13932     switch (DKind) {
13933     case OMPD_parallel_for:
13934     case OMPD_parallel_for_simd:
13935     case OMPD_distribute_parallel_for:
13936     case OMPD_distribute_parallel_for_simd:
13937     case OMPD_teams_distribute_parallel_for:
13938     case OMPD_teams_distribute_parallel_for_simd:
13939     case OMPD_target_parallel_for:
13940     case OMPD_target_parallel_for_simd:
13941     case OMPD_target_teams_distribute_parallel_for:
13942     case OMPD_target_teams_distribute_parallel_for_simd:
13943       CaptureRegion = OMPD_parallel;
13944       break;
13945     case OMPD_for:
13946     case OMPD_for_simd:
13947       // Do not capture schedule-clause expressions.
13948       break;
13949     case OMPD_task:
13950     case OMPD_taskloop:
13951     case OMPD_taskloop_simd:
13952     case OMPD_master_taskloop:
13953     case OMPD_master_taskloop_simd:
13954     case OMPD_parallel_master_taskloop:
13955     case OMPD_parallel_master_taskloop_simd:
13956     case OMPD_target_data:
13957     case OMPD_target_enter_data:
13958     case OMPD_target_exit_data:
13959     case OMPD_target_update:
13960     case OMPD_teams:
13961     case OMPD_teams_distribute:
13962     case OMPD_teams_distribute_simd:
13963     case OMPD_target_teams_distribute:
13964     case OMPD_target_teams_distribute_simd:
13965     case OMPD_target:
13966     case OMPD_target_simd:
13967     case OMPD_target_parallel:
13968     case OMPD_cancel:
13969     case OMPD_parallel:
13970     case OMPD_parallel_master:
13971     case OMPD_parallel_sections:
13972     case OMPD_threadprivate:
13973     case OMPD_allocate:
13974     case OMPD_taskyield:
13975     case OMPD_barrier:
13976     case OMPD_taskwait:
13977     case OMPD_cancellation_point:
13978     case OMPD_flush:
13979     case OMPD_depobj:
13980     case OMPD_scan:
13981     case OMPD_declare_reduction:
13982     case OMPD_declare_mapper:
13983     case OMPD_declare_simd:
13984     case OMPD_declare_variant:
13985     case OMPD_begin_declare_variant:
13986     case OMPD_end_declare_variant:
13987     case OMPD_declare_target:
13988     case OMPD_end_declare_target:
13989     case OMPD_loop:
13990     case OMPD_simd:
13991     case OMPD_tile:
13992     case OMPD_unroll:
13993     case OMPD_sections:
13994     case OMPD_section:
13995     case OMPD_single:
13996     case OMPD_master:
13997     case OMPD_masked:
13998     case OMPD_critical:
13999     case OMPD_taskgroup:
14000     case OMPD_distribute:
14001     case OMPD_ordered:
14002     case OMPD_atomic:
14003     case OMPD_distribute_simd:
14004     case OMPD_target_teams:
14005     case OMPD_requires:
14006     case OMPD_metadirective:
14007       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
14008     case OMPD_unknown:
14009     default:
14010       llvm_unreachable("Unknown OpenMP directive");
14011     }
14012     break;
14013   case OMPC_dist_schedule:
14014     switch (DKind) {
14015     case OMPD_teams_distribute_parallel_for:
14016     case OMPD_teams_distribute_parallel_for_simd:
14017     case OMPD_teams_distribute:
14018     case OMPD_teams_distribute_simd:
14019     case OMPD_target_teams_distribute_parallel_for:
14020     case OMPD_target_teams_distribute_parallel_for_simd:
14021     case OMPD_target_teams_distribute:
14022     case OMPD_target_teams_distribute_simd:
14023       CaptureRegion = OMPD_teams;
14024       break;
14025     case OMPD_distribute_parallel_for:
14026     case OMPD_distribute_parallel_for_simd:
14027     case OMPD_distribute:
14028     case OMPD_distribute_simd:
14029       // Do not capture dist_schedule-clause expressions.
14030       break;
14031     case OMPD_parallel_for:
14032     case OMPD_parallel_for_simd:
14033     case OMPD_target_parallel_for_simd:
14034     case OMPD_target_parallel_for:
14035     case OMPD_task:
14036     case OMPD_taskloop:
14037     case OMPD_taskloop_simd:
14038     case OMPD_master_taskloop:
14039     case OMPD_master_taskloop_simd:
14040     case OMPD_parallel_master_taskloop:
14041     case OMPD_parallel_master_taskloop_simd:
14042     case OMPD_target_data:
14043     case OMPD_target_enter_data:
14044     case OMPD_target_exit_data:
14045     case OMPD_target_update:
14046     case OMPD_teams:
14047     case OMPD_target:
14048     case OMPD_target_simd:
14049     case OMPD_target_parallel:
14050     case OMPD_cancel:
14051     case OMPD_parallel:
14052     case OMPD_parallel_master:
14053     case OMPD_parallel_sections:
14054     case OMPD_threadprivate:
14055     case OMPD_allocate:
14056     case OMPD_taskyield:
14057     case OMPD_barrier:
14058     case OMPD_taskwait:
14059     case OMPD_cancellation_point:
14060     case OMPD_flush:
14061     case OMPD_depobj:
14062     case OMPD_scan:
14063     case OMPD_declare_reduction:
14064     case OMPD_declare_mapper:
14065     case OMPD_declare_simd:
14066     case OMPD_declare_variant:
14067     case OMPD_begin_declare_variant:
14068     case OMPD_end_declare_variant:
14069     case OMPD_declare_target:
14070     case OMPD_end_declare_target:
14071     case OMPD_loop:
14072     case OMPD_simd:
14073     case OMPD_tile:
14074     case OMPD_unroll:
14075     case OMPD_for:
14076     case OMPD_for_simd:
14077     case OMPD_sections:
14078     case OMPD_section:
14079     case OMPD_single:
14080     case OMPD_master:
14081     case OMPD_masked:
14082     case OMPD_critical:
14083     case OMPD_taskgroup:
14084     case OMPD_ordered:
14085     case OMPD_atomic:
14086     case OMPD_target_teams:
14087     case OMPD_requires:
14088     case OMPD_metadirective:
14089       llvm_unreachable("Unexpected OpenMP directive with dist_schedule clause");
14090     case OMPD_unknown:
14091     default:
14092       llvm_unreachable("Unknown OpenMP directive");
14093     }
14094     break;
14095   case OMPC_device:
14096     switch (DKind) {
14097     case OMPD_target_update:
14098     case OMPD_target_enter_data:
14099     case OMPD_target_exit_data:
14100     case OMPD_target:
14101     case OMPD_target_simd:
14102     case OMPD_target_teams:
14103     case OMPD_target_parallel:
14104     case OMPD_target_teams_distribute:
14105     case OMPD_target_teams_distribute_simd:
14106     case OMPD_target_parallel_for:
14107     case OMPD_target_parallel_for_simd:
14108     case OMPD_target_teams_distribute_parallel_for:
14109     case OMPD_target_teams_distribute_parallel_for_simd:
14110     case OMPD_dispatch:
14111       CaptureRegion = OMPD_task;
14112       break;
14113     case OMPD_target_data:
14114     case OMPD_interop:
14115       // Do not capture device-clause expressions.
14116       break;
14117     case OMPD_teams_distribute_parallel_for:
14118     case OMPD_teams_distribute_parallel_for_simd:
14119     case OMPD_teams:
14120     case OMPD_teams_distribute:
14121     case OMPD_teams_distribute_simd:
14122     case OMPD_distribute_parallel_for:
14123     case OMPD_distribute_parallel_for_simd:
14124     case OMPD_task:
14125     case OMPD_taskloop:
14126     case OMPD_taskloop_simd:
14127     case OMPD_master_taskloop:
14128     case OMPD_master_taskloop_simd:
14129     case OMPD_parallel_master_taskloop:
14130     case OMPD_parallel_master_taskloop_simd:
14131     case OMPD_cancel:
14132     case OMPD_parallel:
14133     case OMPD_parallel_master:
14134     case OMPD_parallel_sections:
14135     case OMPD_parallel_for:
14136     case OMPD_parallel_for_simd:
14137     case OMPD_threadprivate:
14138     case OMPD_allocate:
14139     case OMPD_taskyield:
14140     case OMPD_barrier:
14141     case OMPD_taskwait:
14142     case OMPD_cancellation_point:
14143     case OMPD_flush:
14144     case OMPD_depobj:
14145     case OMPD_scan:
14146     case OMPD_declare_reduction:
14147     case OMPD_declare_mapper:
14148     case OMPD_declare_simd:
14149     case OMPD_declare_variant:
14150     case OMPD_begin_declare_variant:
14151     case OMPD_end_declare_variant:
14152     case OMPD_declare_target:
14153     case OMPD_end_declare_target:
14154     case OMPD_loop:
14155     case OMPD_simd:
14156     case OMPD_tile:
14157     case OMPD_unroll:
14158     case OMPD_for:
14159     case OMPD_for_simd:
14160     case OMPD_sections:
14161     case OMPD_section:
14162     case OMPD_single:
14163     case OMPD_master:
14164     case OMPD_masked:
14165     case OMPD_critical:
14166     case OMPD_taskgroup:
14167     case OMPD_distribute:
14168     case OMPD_ordered:
14169     case OMPD_atomic:
14170     case OMPD_distribute_simd:
14171     case OMPD_requires:
14172     case OMPD_metadirective:
14173       llvm_unreachable("Unexpected OpenMP directive with device-clause");
14174     case OMPD_unknown:
14175     default:
14176       llvm_unreachable("Unknown OpenMP directive");
14177     }
14178     break;
14179   case OMPC_grainsize:
14180   case OMPC_num_tasks:
14181   case OMPC_final:
14182   case OMPC_priority:
14183     switch (DKind) {
14184     case OMPD_task:
14185     case OMPD_taskloop:
14186     case OMPD_taskloop_simd:
14187     case OMPD_master_taskloop:
14188     case OMPD_master_taskloop_simd:
14189       break;
14190     case OMPD_parallel_master_taskloop:
14191     case OMPD_parallel_master_taskloop_simd:
14192       CaptureRegion = OMPD_parallel;
14193       break;
14194     case OMPD_target_update:
14195     case OMPD_target_enter_data:
14196     case OMPD_target_exit_data:
14197     case OMPD_target:
14198     case OMPD_target_simd:
14199     case OMPD_target_teams:
14200     case OMPD_target_parallel:
14201     case OMPD_target_teams_distribute:
14202     case OMPD_target_teams_distribute_simd:
14203     case OMPD_target_parallel_for:
14204     case OMPD_target_parallel_for_simd:
14205     case OMPD_target_teams_distribute_parallel_for:
14206     case OMPD_target_teams_distribute_parallel_for_simd:
14207     case OMPD_target_data:
14208     case OMPD_teams_distribute_parallel_for:
14209     case OMPD_teams_distribute_parallel_for_simd:
14210     case OMPD_teams:
14211     case OMPD_teams_distribute:
14212     case OMPD_teams_distribute_simd:
14213     case OMPD_distribute_parallel_for:
14214     case OMPD_distribute_parallel_for_simd:
14215     case OMPD_cancel:
14216     case OMPD_parallel:
14217     case OMPD_parallel_master:
14218     case OMPD_parallel_sections:
14219     case OMPD_parallel_for:
14220     case OMPD_parallel_for_simd:
14221     case OMPD_threadprivate:
14222     case OMPD_allocate:
14223     case OMPD_taskyield:
14224     case OMPD_barrier:
14225     case OMPD_taskwait:
14226     case OMPD_cancellation_point:
14227     case OMPD_flush:
14228     case OMPD_depobj:
14229     case OMPD_scan:
14230     case OMPD_declare_reduction:
14231     case OMPD_declare_mapper:
14232     case OMPD_declare_simd:
14233     case OMPD_declare_variant:
14234     case OMPD_begin_declare_variant:
14235     case OMPD_end_declare_variant:
14236     case OMPD_declare_target:
14237     case OMPD_end_declare_target:
14238     case OMPD_loop:
14239     case OMPD_simd:
14240     case OMPD_tile:
14241     case OMPD_unroll:
14242     case OMPD_for:
14243     case OMPD_for_simd:
14244     case OMPD_sections:
14245     case OMPD_section:
14246     case OMPD_single:
14247     case OMPD_master:
14248     case OMPD_masked:
14249     case OMPD_critical:
14250     case OMPD_taskgroup:
14251     case OMPD_distribute:
14252     case OMPD_ordered:
14253     case OMPD_atomic:
14254     case OMPD_distribute_simd:
14255     case OMPD_requires:
14256     case OMPD_metadirective:
14257       llvm_unreachable("Unexpected OpenMP directive with grainsize-clause");
14258     case OMPD_unknown:
14259     default:
14260       llvm_unreachable("Unknown OpenMP directive");
14261     }
14262     break;
14263   case OMPC_novariants:
14264   case OMPC_nocontext:
14265     switch (DKind) {
14266     case OMPD_dispatch:
14267       CaptureRegion = OMPD_task;
14268       break;
14269     default:
14270       llvm_unreachable("Unexpected OpenMP directive");
14271     }
14272     break;
14273   case OMPC_filter:
14274     // Do not capture filter-clause expressions.
14275     break;
14276   case OMPC_when:
14277     if (DKind == OMPD_metadirective) {
14278       CaptureRegion = OMPD_metadirective;
14279     } else if (DKind == OMPD_unknown) {
14280       llvm_unreachable("Unknown OpenMP directive");
14281     } else {
14282       llvm_unreachable("Unexpected OpenMP directive with when clause");
14283     }
14284     break;
14285   case OMPC_firstprivate:
14286   case OMPC_lastprivate:
14287   case OMPC_reduction:
14288   case OMPC_task_reduction:
14289   case OMPC_in_reduction:
14290   case OMPC_linear:
14291   case OMPC_default:
14292   case OMPC_proc_bind:
14293   case OMPC_safelen:
14294   case OMPC_simdlen:
14295   case OMPC_sizes:
14296   case OMPC_allocator:
14297   case OMPC_collapse:
14298   case OMPC_private:
14299   case OMPC_shared:
14300   case OMPC_aligned:
14301   case OMPC_copyin:
14302   case OMPC_copyprivate:
14303   case OMPC_ordered:
14304   case OMPC_nowait:
14305   case OMPC_untied:
14306   case OMPC_mergeable:
14307   case OMPC_threadprivate:
14308   case OMPC_allocate:
14309   case OMPC_flush:
14310   case OMPC_depobj:
14311   case OMPC_read:
14312   case OMPC_write:
14313   case OMPC_update:
14314   case OMPC_capture:
14315   case OMPC_compare:
14316   case OMPC_seq_cst:
14317   case OMPC_acq_rel:
14318   case OMPC_acquire:
14319   case OMPC_release:
14320   case OMPC_relaxed:
14321   case OMPC_depend:
14322   case OMPC_threads:
14323   case OMPC_simd:
14324   case OMPC_map:
14325   case OMPC_nogroup:
14326   case OMPC_hint:
14327   case OMPC_defaultmap:
14328   case OMPC_unknown:
14329   case OMPC_uniform:
14330   case OMPC_to:
14331   case OMPC_from:
14332   case OMPC_use_device_ptr:
14333   case OMPC_use_device_addr:
14334   case OMPC_is_device_ptr:
14335   case OMPC_unified_address:
14336   case OMPC_unified_shared_memory:
14337   case OMPC_reverse_offload:
14338   case OMPC_dynamic_allocators:
14339   case OMPC_atomic_default_mem_order:
14340   case OMPC_device_type:
14341   case OMPC_match:
14342   case OMPC_nontemporal:
14343   case OMPC_order:
14344   case OMPC_destroy:
14345   case OMPC_detach:
14346   case OMPC_inclusive:
14347   case OMPC_exclusive:
14348   case OMPC_uses_allocators:
14349   case OMPC_affinity:
14350   case OMPC_bind:
14351   default:
14352     llvm_unreachable("Unexpected OpenMP clause.");
14353   }
14354   return CaptureRegion;
14355 }
14356 
14357 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
14358                                      Expr *Condition, SourceLocation StartLoc,
14359                                      SourceLocation LParenLoc,
14360                                      SourceLocation NameModifierLoc,
14361                                      SourceLocation ColonLoc,
14362                                      SourceLocation EndLoc) {
14363   Expr *ValExpr = Condition;
14364   Stmt *HelperValStmt = nullptr;
14365   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
14366   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
14367       !Condition->isInstantiationDependent() &&
14368       !Condition->containsUnexpandedParameterPack()) {
14369     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
14370     if (Val.isInvalid())
14371       return nullptr;
14372 
14373     ValExpr = Val.get();
14374 
14375     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14376     CaptureRegion = getOpenMPCaptureRegionForClause(
14377         DKind, OMPC_if, LangOpts.OpenMP, NameModifier);
14378     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14379       ValExpr = MakeFullExpr(ValExpr).get();
14380       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14381       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14382       HelperValStmt = buildPreInits(Context, Captures);
14383     }
14384   }
14385 
14386   return new (Context)
14387       OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
14388                   LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
14389 }
14390 
14391 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
14392                                         SourceLocation StartLoc,
14393                                         SourceLocation LParenLoc,
14394                                         SourceLocation EndLoc) {
14395   Expr *ValExpr = Condition;
14396   Stmt *HelperValStmt = nullptr;
14397   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
14398   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
14399       !Condition->isInstantiationDependent() &&
14400       !Condition->containsUnexpandedParameterPack()) {
14401     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
14402     if (Val.isInvalid())
14403       return nullptr;
14404 
14405     ValExpr = MakeFullExpr(Val.get()).get();
14406 
14407     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14408     CaptureRegion =
14409         getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP);
14410     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14411       ValExpr = MakeFullExpr(ValExpr).get();
14412       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14413       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14414       HelperValStmt = buildPreInits(Context, Captures);
14415     }
14416   }
14417 
14418   return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion,
14419                                       StartLoc, LParenLoc, EndLoc);
14420 }
14421 
14422 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
14423                                                         Expr *Op) {
14424   if (!Op)
14425     return ExprError();
14426 
14427   class IntConvertDiagnoser : public ICEConvertDiagnoser {
14428   public:
14429     IntConvertDiagnoser()
14430         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
14431     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
14432                                          QualType T) override {
14433       return S.Diag(Loc, diag::err_omp_not_integral) << T;
14434     }
14435     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
14436                                              QualType T) override {
14437       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
14438     }
14439     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
14440                                                QualType T,
14441                                                QualType ConvTy) override {
14442       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
14443     }
14444     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
14445                                            QualType ConvTy) override {
14446       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
14447              << ConvTy->isEnumeralType() << ConvTy;
14448     }
14449     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
14450                                             QualType T) override {
14451       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
14452     }
14453     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
14454                                         QualType ConvTy) override {
14455       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
14456              << ConvTy->isEnumeralType() << ConvTy;
14457     }
14458     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
14459                                              QualType) override {
14460       llvm_unreachable("conversion functions are permitted");
14461     }
14462   } ConvertDiagnoser;
14463   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
14464 }
14465 
14466 static bool
14467 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind,
14468                           bool StrictlyPositive, bool BuildCapture = false,
14469                           OpenMPDirectiveKind DKind = OMPD_unknown,
14470                           OpenMPDirectiveKind *CaptureRegion = nullptr,
14471                           Stmt **HelperValStmt = nullptr) {
14472   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
14473       !ValExpr->isInstantiationDependent()) {
14474     SourceLocation Loc = ValExpr->getExprLoc();
14475     ExprResult Value =
14476         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
14477     if (Value.isInvalid())
14478       return false;
14479 
14480     ValExpr = Value.get();
14481     // The expression must evaluate to a non-negative integer value.
14482     if (Optional<llvm::APSInt> Result =
14483             ValExpr->getIntegerConstantExpr(SemaRef.Context)) {
14484       if (Result->isSigned() &&
14485           !((!StrictlyPositive && Result->isNonNegative()) ||
14486             (StrictlyPositive && Result->isStrictlyPositive()))) {
14487         SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
14488             << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
14489             << ValExpr->getSourceRange();
14490         return false;
14491       }
14492     }
14493     if (!BuildCapture)
14494       return true;
14495     *CaptureRegion =
14496         getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP);
14497     if (*CaptureRegion != OMPD_unknown &&
14498         !SemaRef.CurContext->isDependentContext()) {
14499       ValExpr = SemaRef.MakeFullExpr(ValExpr).get();
14500       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14501       ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get();
14502       *HelperValStmt = buildPreInits(SemaRef.Context, Captures);
14503     }
14504   }
14505   return true;
14506 }
14507 
14508 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
14509                                              SourceLocation StartLoc,
14510                                              SourceLocation LParenLoc,
14511                                              SourceLocation EndLoc) {
14512   Expr *ValExpr = NumThreads;
14513   Stmt *HelperValStmt = nullptr;
14514 
14515   // OpenMP [2.5, Restrictions]
14516   //  The num_threads expression must evaluate to a positive integer value.
14517   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
14518                                  /*StrictlyPositive=*/true))
14519     return nullptr;
14520 
14521   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14522   OpenMPDirectiveKind CaptureRegion =
14523       getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP);
14524   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14525     ValExpr = MakeFullExpr(ValExpr).get();
14526     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14527     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14528     HelperValStmt = buildPreInits(Context, Captures);
14529   }
14530 
14531   return new (Context) OMPNumThreadsClause(
14532       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
14533 }
14534 
14535 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
14536                                                        OpenMPClauseKind CKind,
14537                                                        bool StrictlyPositive,
14538                                                        bool SuppressExprDiags) {
14539   if (!E)
14540     return ExprError();
14541   if (E->isValueDependent() || E->isTypeDependent() ||
14542       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
14543     return E;
14544 
14545   llvm::APSInt Result;
14546   ExprResult ICE;
14547   if (SuppressExprDiags) {
14548     // Use a custom diagnoser that suppresses 'note' diagnostics about the
14549     // expression.
14550     struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser {
14551       SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {}
14552       Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S,
14553                                                  SourceLocation Loc) override {
14554         llvm_unreachable("Diagnostic suppressed");
14555       }
14556     } Diagnoser;
14557     ICE = VerifyIntegerConstantExpression(E, &Result, Diagnoser, AllowFold);
14558   } else {
14559     ICE = VerifyIntegerConstantExpression(E, &Result, /*FIXME*/ AllowFold);
14560   }
14561   if (ICE.isInvalid())
14562     return ExprError();
14563 
14564   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
14565       (!StrictlyPositive && !Result.isNonNegative())) {
14566     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
14567         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
14568         << E->getSourceRange();
14569     return ExprError();
14570   }
14571   if ((CKind == OMPC_aligned || CKind == OMPC_align) && !Result.isPowerOf2()) {
14572     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
14573         << E->getSourceRange();
14574     return ExprError();
14575   }
14576   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
14577     DSAStack->setAssociatedLoops(Result.getExtValue());
14578   else if (CKind == OMPC_ordered)
14579     DSAStack->setAssociatedLoops(Result.getExtValue());
14580   return ICE;
14581 }
14582 
14583 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
14584                                           SourceLocation LParenLoc,
14585                                           SourceLocation EndLoc) {
14586   // OpenMP [2.8.1, simd construct, Description]
14587   // The parameter of the safelen clause must be a constant
14588   // positive integer expression.
14589   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
14590   if (Safelen.isInvalid())
14591     return nullptr;
14592   return new (Context)
14593       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
14594 }
14595 
14596 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
14597                                           SourceLocation LParenLoc,
14598                                           SourceLocation EndLoc) {
14599   // OpenMP [2.8.1, simd construct, Description]
14600   // The parameter of the simdlen clause must be a constant
14601   // positive integer expression.
14602   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
14603   if (Simdlen.isInvalid())
14604     return nullptr;
14605   return new (Context)
14606       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
14607 }
14608 
14609 /// Tries to find omp_allocator_handle_t type.
14610 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
14611                                     DSAStackTy *Stack) {
14612   QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT();
14613   if (!OMPAllocatorHandleT.isNull())
14614     return true;
14615   // Build the predefined allocator expressions.
14616   bool ErrorFound = false;
14617   for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
14618     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
14619     StringRef Allocator =
14620         OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
14621     DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator);
14622     auto *VD = dyn_cast_or_null<ValueDecl>(
14623         S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName));
14624     if (!VD) {
14625       ErrorFound = true;
14626       break;
14627     }
14628     QualType AllocatorType =
14629         VD->getType().getNonLValueExprType(S.getASTContext());
14630     ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc);
14631     if (!Res.isUsable()) {
14632       ErrorFound = true;
14633       break;
14634     }
14635     if (OMPAllocatorHandleT.isNull())
14636       OMPAllocatorHandleT = AllocatorType;
14637     if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) {
14638       ErrorFound = true;
14639       break;
14640     }
14641     Stack->setAllocator(AllocatorKind, Res.get());
14642   }
14643   if (ErrorFound) {
14644     S.Diag(Loc, diag::err_omp_implied_type_not_found)
14645         << "omp_allocator_handle_t";
14646     return false;
14647   }
14648   OMPAllocatorHandleT.addConst();
14649   Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT);
14650   return true;
14651 }
14652 
14653 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc,
14654                                             SourceLocation LParenLoc,
14655                                             SourceLocation EndLoc) {
14656   // OpenMP [2.11.3, allocate Directive, Description]
14657   // allocator is an expression of omp_allocator_handle_t type.
14658   if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack))
14659     return nullptr;
14660 
14661   ExprResult Allocator = DefaultLvalueConversion(A);
14662   if (Allocator.isInvalid())
14663     return nullptr;
14664   Allocator = PerformImplicitConversion(Allocator.get(),
14665                                         DSAStack->getOMPAllocatorHandleT(),
14666                                         Sema::AA_Initializing,
14667                                         /*AllowExplicit=*/true);
14668   if (Allocator.isInvalid())
14669     return nullptr;
14670   return new (Context)
14671       OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
14672 }
14673 
14674 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
14675                                            SourceLocation StartLoc,
14676                                            SourceLocation LParenLoc,
14677                                            SourceLocation EndLoc) {
14678   // OpenMP [2.7.1, loop construct, Description]
14679   // OpenMP [2.8.1, simd construct, Description]
14680   // OpenMP [2.9.6, distribute construct, Description]
14681   // The parameter of the collapse clause must be a constant
14682   // positive integer expression.
14683   ExprResult NumForLoopsResult =
14684       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
14685   if (NumForLoopsResult.isInvalid())
14686     return nullptr;
14687   return new (Context)
14688       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
14689 }
14690 
14691 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
14692                                           SourceLocation EndLoc,
14693                                           SourceLocation LParenLoc,
14694                                           Expr *NumForLoops) {
14695   // OpenMP [2.7.1, loop construct, Description]
14696   // OpenMP [2.8.1, simd construct, Description]
14697   // OpenMP [2.9.6, distribute construct, Description]
14698   // The parameter of the ordered clause must be a constant
14699   // positive integer expression if any.
14700   if (NumForLoops && LParenLoc.isValid()) {
14701     ExprResult NumForLoopsResult =
14702         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
14703     if (NumForLoopsResult.isInvalid())
14704       return nullptr;
14705     NumForLoops = NumForLoopsResult.get();
14706   } else {
14707     NumForLoops = nullptr;
14708   }
14709   auto *Clause = OMPOrderedClause::Create(
14710       Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
14711       StartLoc, LParenLoc, EndLoc);
14712   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
14713   return Clause;
14714 }
14715 
14716 OMPClause *Sema::ActOnOpenMPSimpleClause(
14717     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
14718     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
14719   OMPClause *Res = nullptr;
14720   switch (Kind) {
14721   case OMPC_default:
14722     Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument),
14723                                    ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14724     break;
14725   case OMPC_proc_bind:
14726     Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument),
14727                                     ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14728     break;
14729   case OMPC_atomic_default_mem_order:
14730     Res = ActOnOpenMPAtomicDefaultMemOrderClause(
14731         static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
14732         ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14733     break;
14734   case OMPC_order:
14735     Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument),
14736                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14737     break;
14738   case OMPC_update:
14739     Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument),
14740                                   ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14741     break;
14742   case OMPC_bind:
14743     Res = ActOnOpenMPBindClause(static_cast<OpenMPBindClauseKind>(Argument),
14744                                 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14745     break;
14746   case OMPC_if:
14747   case OMPC_final:
14748   case OMPC_num_threads:
14749   case OMPC_safelen:
14750   case OMPC_simdlen:
14751   case OMPC_sizes:
14752   case OMPC_allocator:
14753   case OMPC_collapse:
14754   case OMPC_schedule:
14755   case OMPC_private:
14756   case OMPC_firstprivate:
14757   case OMPC_lastprivate:
14758   case OMPC_shared:
14759   case OMPC_reduction:
14760   case OMPC_task_reduction:
14761   case OMPC_in_reduction:
14762   case OMPC_linear:
14763   case OMPC_aligned:
14764   case OMPC_copyin:
14765   case OMPC_copyprivate:
14766   case OMPC_ordered:
14767   case OMPC_nowait:
14768   case OMPC_untied:
14769   case OMPC_mergeable:
14770   case OMPC_threadprivate:
14771   case OMPC_allocate:
14772   case OMPC_flush:
14773   case OMPC_depobj:
14774   case OMPC_read:
14775   case OMPC_write:
14776   case OMPC_capture:
14777   case OMPC_compare:
14778   case OMPC_seq_cst:
14779   case OMPC_acq_rel:
14780   case OMPC_acquire:
14781   case OMPC_release:
14782   case OMPC_relaxed:
14783   case OMPC_depend:
14784   case OMPC_device:
14785   case OMPC_threads:
14786   case OMPC_simd:
14787   case OMPC_map:
14788   case OMPC_num_teams:
14789   case OMPC_thread_limit:
14790   case OMPC_priority:
14791   case OMPC_grainsize:
14792   case OMPC_nogroup:
14793   case OMPC_num_tasks:
14794   case OMPC_hint:
14795   case OMPC_dist_schedule:
14796   case OMPC_defaultmap:
14797   case OMPC_unknown:
14798   case OMPC_uniform:
14799   case OMPC_to:
14800   case OMPC_from:
14801   case OMPC_use_device_ptr:
14802   case OMPC_use_device_addr:
14803   case OMPC_is_device_ptr:
14804   case OMPC_unified_address:
14805   case OMPC_unified_shared_memory:
14806   case OMPC_reverse_offload:
14807   case OMPC_dynamic_allocators:
14808   case OMPC_device_type:
14809   case OMPC_match:
14810   case OMPC_nontemporal:
14811   case OMPC_destroy:
14812   case OMPC_novariants:
14813   case OMPC_nocontext:
14814   case OMPC_detach:
14815   case OMPC_inclusive:
14816   case OMPC_exclusive:
14817   case OMPC_uses_allocators:
14818   case OMPC_affinity:
14819   case OMPC_when:
14820   default:
14821     llvm_unreachable("Clause is not allowed.");
14822   }
14823   return Res;
14824 }
14825 
14826 static std::string
14827 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
14828                         ArrayRef<unsigned> Exclude = llvm::None) {
14829   SmallString<256> Buffer;
14830   llvm::raw_svector_ostream Out(Buffer);
14831   unsigned Skipped = Exclude.size();
14832   auto S = Exclude.begin(), E = Exclude.end();
14833   for (unsigned I = First; I < Last; ++I) {
14834     if (std::find(S, E, I) != E) {
14835       --Skipped;
14836       continue;
14837     }
14838     Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
14839     if (I + Skipped + 2 == Last)
14840       Out << " or ";
14841     else if (I + Skipped + 1 != Last)
14842       Out << ", ";
14843   }
14844   return std::string(Out.str());
14845 }
14846 
14847 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind,
14848                                           SourceLocation KindKwLoc,
14849                                           SourceLocation StartLoc,
14850                                           SourceLocation LParenLoc,
14851                                           SourceLocation EndLoc) {
14852   if (Kind == OMP_DEFAULT_unknown) {
14853     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14854         << getListOfPossibleValues(OMPC_default, /*First=*/0,
14855                                    /*Last=*/unsigned(OMP_DEFAULT_unknown))
14856         << getOpenMPClauseName(OMPC_default);
14857     return nullptr;
14858   }
14859 
14860   switch (Kind) {
14861   case OMP_DEFAULT_none:
14862     DSAStack->setDefaultDSANone(KindKwLoc);
14863     break;
14864   case OMP_DEFAULT_shared:
14865     DSAStack->setDefaultDSAShared(KindKwLoc);
14866     break;
14867   case OMP_DEFAULT_firstprivate:
14868     DSAStack->setDefaultDSAFirstPrivate(KindKwLoc);
14869     break;
14870   default:
14871     llvm_unreachable("DSA unexpected in OpenMP default clause");
14872   }
14873 
14874   return new (Context)
14875       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
14876 }
14877 
14878 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind,
14879                                            SourceLocation KindKwLoc,
14880                                            SourceLocation StartLoc,
14881                                            SourceLocation LParenLoc,
14882                                            SourceLocation EndLoc) {
14883   if (Kind == OMP_PROC_BIND_unknown) {
14884     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14885         << getListOfPossibleValues(OMPC_proc_bind,
14886                                    /*First=*/unsigned(OMP_PROC_BIND_master),
14887                                    /*Last=*/
14888                                    unsigned(LangOpts.OpenMP > 50
14889                                                 ? OMP_PROC_BIND_primary
14890                                                 : OMP_PROC_BIND_spread) +
14891                                        1)
14892         << getOpenMPClauseName(OMPC_proc_bind);
14893     return nullptr;
14894   }
14895   if (Kind == OMP_PROC_BIND_primary && LangOpts.OpenMP < 51)
14896     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14897         << getListOfPossibleValues(OMPC_proc_bind,
14898                                    /*First=*/unsigned(OMP_PROC_BIND_master),
14899                                    /*Last=*/
14900                                    unsigned(OMP_PROC_BIND_spread) + 1)
14901         << getOpenMPClauseName(OMPC_proc_bind);
14902   return new (Context)
14903       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
14904 }
14905 
14906 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
14907     OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
14908     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
14909   if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
14910     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14911         << getListOfPossibleValues(
14912                OMPC_atomic_default_mem_order, /*First=*/0,
14913                /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
14914         << getOpenMPClauseName(OMPC_atomic_default_mem_order);
14915     return nullptr;
14916   }
14917   return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
14918                                                       LParenLoc, EndLoc);
14919 }
14920 
14921 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind,
14922                                         SourceLocation KindKwLoc,
14923                                         SourceLocation StartLoc,
14924                                         SourceLocation LParenLoc,
14925                                         SourceLocation EndLoc) {
14926   if (Kind == OMPC_ORDER_unknown) {
14927     static_assert(OMPC_ORDER_unknown > 0,
14928                   "OMPC_ORDER_unknown not greater than 0");
14929     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14930         << getListOfPossibleValues(OMPC_order, /*First=*/0,
14931                                    /*Last=*/OMPC_ORDER_unknown)
14932         << getOpenMPClauseName(OMPC_order);
14933     return nullptr;
14934   }
14935   return new (Context)
14936       OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
14937 }
14938 
14939 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind,
14940                                          SourceLocation KindKwLoc,
14941                                          SourceLocation StartLoc,
14942                                          SourceLocation LParenLoc,
14943                                          SourceLocation EndLoc) {
14944   if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source ||
14945       Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) {
14946     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink,
14947                          OMPC_DEPEND_depobj};
14948     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14949         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
14950                                    /*Last=*/OMPC_DEPEND_unknown, Except)
14951         << getOpenMPClauseName(OMPC_update);
14952     return nullptr;
14953   }
14954   return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind,
14955                                  EndLoc);
14956 }
14957 
14958 OMPClause *Sema::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs,
14959                                         SourceLocation StartLoc,
14960                                         SourceLocation LParenLoc,
14961                                         SourceLocation EndLoc) {
14962   for (Expr *SizeExpr : SizeExprs) {
14963     ExprResult NumForLoopsResult = VerifyPositiveIntegerConstantInClause(
14964         SizeExpr, OMPC_sizes, /*StrictlyPositive=*/true);
14965     if (!NumForLoopsResult.isUsable())
14966       return nullptr;
14967   }
14968 
14969   DSAStack->setAssociatedLoops(SizeExprs.size());
14970   return OMPSizesClause::Create(Context, StartLoc, LParenLoc, EndLoc,
14971                                 SizeExprs);
14972 }
14973 
14974 OMPClause *Sema::ActOnOpenMPFullClause(SourceLocation StartLoc,
14975                                        SourceLocation EndLoc) {
14976   return OMPFullClause::Create(Context, StartLoc, EndLoc);
14977 }
14978 
14979 OMPClause *Sema::ActOnOpenMPPartialClause(Expr *FactorExpr,
14980                                           SourceLocation StartLoc,
14981                                           SourceLocation LParenLoc,
14982                                           SourceLocation EndLoc) {
14983   if (FactorExpr) {
14984     // If an argument is specified, it must be a constant (or an unevaluated
14985     // template expression).
14986     ExprResult FactorResult = VerifyPositiveIntegerConstantInClause(
14987         FactorExpr, OMPC_partial, /*StrictlyPositive=*/true);
14988     if (FactorResult.isInvalid())
14989       return nullptr;
14990     FactorExpr = FactorResult.get();
14991   }
14992 
14993   return OMPPartialClause::Create(Context, StartLoc, LParenLoc, EndLoc,
14994                                   FactorExpr);
14995 }
14996 
14997 OMPClause *Sema::ActOnOpenMPAlignClause(Expr *A, SourceLocation StartLoc,
14998                                         SourceLocation LParenLoc,
14999                                         SourceLocation EndLoc) {
15000   ExprResult AlignVal;
15001   AlignVal = VerifyPositiveIntegerConstantInClause(A, OMPC_align);
15002   if (AlignVal.isInvalid())
15003     return nullptr;
15004   return OMPAlignClause::Create(Context, AlignVal.get(), StartLoc, LParenLoc,
15005                                 EndLoc);
15006 }
15007 
15008 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
15009     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
15010     SourceLocation StartLoc, SourceLocation LParenLoc,
15011     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
15012     SourceLocation EndLoc) {
15013   OMPClause *Res = nullptr;
15014   switch (Kind) {
15015   case OMPC_schedule:
15016     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
15017     assert(Argument.size() == NumberOfElements &&
15018            ArgumentLoc.size() == NumberOfElements);
15019     Res = ActOnOpenMPScheduleClause(
15020         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
15021         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
15022         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
15023         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
15024         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
15025     break;
15026   case OMPC_if:
15027     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
15028     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
15029                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
15030                               DelimLoc, EndLoc);
15031     break;
15032   case OMPC_dist_schedule:
15033     Res = ActOnOpenMPDistScheduleClause(
15034         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
15035         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
15036     break;
15037   case OMPC_defaultmap:
15038     enum { Modifier, DefaultmapKind };
15039     Res = ActOnOpenMPDefaultmapClause(
15040         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
15041         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
15042         StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
15043         EndLoc);
15044     break;
15045   case OMPC_device:
15046     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
15047     Res = ActOnOpenMPDeviceClause(
15048         static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr,
15049         StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc);
15050     break;
15051   case OMPC_final:
15052   case OMPC_num_threads:
15053   case OMPC_safelen:
15054   case OMPC_simdlen:
15055   case OMPC_sizes:
15056   case OMPC_allocator:
15057   case OMPC_collapse:
15058   case OMPC_default:
15059   case OMPC_proc_bind:
15060   case OMPC_private:
15061   case OMPC_firstprivate:
15062   case OMPC_lastprivate:
15063   case OMPC_shared:
15064   case OMPC_reduction:
15065   case OMPC_task_reduction:
15066   case OMPC_in_reduction:
15067   case OMPC_linear:
15068   case OMPC_aligned:
15069   case OMPC_copyin:
15070   case OMPC_copyprivate:
15071   case OMPC_ordered:
15072   case OMPC_nowait:
15073   case OMPC_untied:
15074   case OMPC_mergeable:
15075   case OMPC_threadprivate:
15076   case OMPC_allocate:
15077   case OMPC_flush:
15078   case OMPC_depobj:
15079   case OMPC_read:
15080   case OMPC_write:
15081   case OMPC_update:
15082   case OMPC_capture:
15083   case OMPC_compare:
15084   case OMPC_seq_cst:
15085   case OMPC_acq_rel:
15086   case OMPC_acquire:
15087   case OMPC_release:
15088   case OMPC_relaxed:
15089   case OMPC_depend:
15090   case OMPC_threads:
15091   case OMPC_simd:
15092   case OMPC_map:
15093   case OMPC_num_teams:
15094   case OMPC_thread_limit:
15095   case OMPC_priority:
15096   case OMPC_grainsize:
15097   case OMPC_nogroup:
15098   case OMPC_num_tasks:
15099   case OMPC_hint:
15100   case OMPC_unknown:
15101   case OMPC_uniform:
15102   case OMPC_to:
15103   case OMPC_from:
15104   case OMPC_use_device_ptr:
15105   case OMPC_use_device_addr:
15106   case OMPC_is_device_ptr:
15107   case OMPC_unified_address:
15108   case OMPC_unified_shared_memory:
15109   case OMPC_reverse_offload:
15110   case OMPC_dynamic_allocators:
15111   case OMPC_atomic_default_mem_order:
15112   case OMPC_device_type:
15113   case OMPC_match:
15114   case OMPC_nontemporal:
15115   case OMPC_order:
15116   case OMPC_destroy:
15117   case OMPC_novariants:
15118   case OMPC_nocontext:
15119   case OMPC_detach:
15120   case OMPC_inclusive:
15121   case OMPC_exclusive:
15122   case OMPC_uses_allocators:
15123   case OMPC_affinity:
15124   case OMPC_when:
15125   case OMPC_bind:
15126   default:
15127     llvm_unreachable("Clause is not allowed.");
15128   }
15129   return Res;
15130 }
15131 
15132 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
15133                                    OpenMPScheduleClauseModifier M2,
15134                                    SourceLocation M1Loc, SourceLocation M2Loc) {
15135   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
15136     SmallVector<unsigned, 2> Excluded;
15137     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
15138       Excluded.push_back(M2);
15139     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
15140       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
15141     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
15142       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
15143     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
15144         << getListOfPossibleValues(OMPC_schedule,
15145                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
15146                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
15147                                    Excluded)
15148         << getOpenMPClauseName(OMPC_schedule);
15149     return true;
15150   }
15151   return false;
15152 }
15153 
15154 OMPClause *Sema::ActOnOpenMPScheduleClause(
15155     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
15156     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
15157     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
15158     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
15159   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
15160       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
15161     return nullptr;
15162   // OpenMP, 2.7.1, Loop Construct, Restrictions
15163   // Either the monotonic modifier or the nonmonotonic modifier can be specified
15164   // but not both.
15165   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
15166       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
15167        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
15168       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
15169        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
15170     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
15171         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
15172         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
15173     return nullptr;
15174   }
15175   if (Kind == OMPC_SCHEDULE_unknown) {
15176     std::string Values;
15177     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
15178       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
15179       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
15180                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
15181                                        Exclude);
15182     } else {
15183       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
15184                                        /*Last=*/OMPC_SCHEDULE_unknown);
15185     }
15186     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
15187         << Values << getOpenMPClauseName(OMPC_schedule);
15188     return nullptr;
15189   }
15190   // OpenMP, 2.7.1, Loop Construct, Restrictions
15191   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
15192   // schedule(guided).
15193   // OpenMP 5.0 does not have this restriction.
15194   if (LangOpts.OpenMP < 50 &&
15195       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
15196        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
15197       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
15198     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
15199          diag::err_omp_schedule_nonmonotonic_static);
15200     return nullptr;
15201   }
15202   Expr *ValExpr = ChunkSize;
15203   Stmt *HelperValStmt = nullptr;
15204   if (ChunkSize) {
15205     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
15206         !ChunkSize->isInstantiationDependent() &&
15207         !ChunkSize->containsUnexpandedParameterPack()) {
15208       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
15209       ExprResult Val =
15210           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
15211       if (Val.isInvalid())
15212         return nullptr;
15213 
15214       ValExpr = Val.get();
15215 
15216       // OpenMP [2.7.1, Restrictions]
15217       //  chunk_size must be a loop invariant integer expression with a positive
15218       //  value.
15219       if (Optional<llvm::APSInt> Result =
15220               ValExpr->getIntegerConstantExpr(Context)) {
15221         if (Result->isSigned() && !Result->isStrictlyPositive()) {
15222           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
15223               << "schedule" << 1 << ChunkSize->getSourceRange();
15224           return nullptr;
15225         }
15226       } else if (getOpenMPCaptureRegionForClause(
15227                      DSAStack->getCurrentDirective(), OMPC_schedule,
15228                      LangOpts.OpenMP) != OMPD_unknown &&
15229                  !CurContext->isDependentContext()) {
15230         ValExpr = MakeFullExpr(ValExpr).get();
15231         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15232         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15233         HelperValStmt = buildPreInits(Context, Captures);
15234       }
15235     }
15236   }
15237 
15238   return new (Context)
15239       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
15240                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
15241 }
15242 
15243 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
15244                                    SourceLocation StartLoc,
15245                                    SourceLocation EndLoc) {
15246   OMPClause *Res = nullptr;
15247   switch (Kind) {
15248   case OMPC_ordered:
15249     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
15250     break;
15251   case OMPC_nowait:
15252     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
15253     break;
15254   case OMPC_untied:
15255     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
15256     break;
15257   case OMPC_mergeable:
15258     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
15259     break;
15260   case OMPC_read:
15261     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
15262     break;
15263   case OMPC_write:
15264     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
15265     break;
15266   case OMPC_update:
15267     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
15268     break;
15269   case OMPC_capture:
15270     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
15271     break;
15272   case OMPC_compare:
15273     Res = ActOnOpenMPCompareClause(StartLoc, EndLoc);
15274     break;
15275   case OMPC_seq_cst:
15276     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
15277     break;
15278   case OMPC_acq_rel:
15279     Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc);
15280     break;
15281   case OMPC_acquire:
15282     Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc);
15283     break;
15284   case OMPC_release:
15285     Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc);
15286     break;
15287   case OMPC_relaxed:
15288     Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc);
15289     break;
15290   case OMPC_threads:
15291     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
15292     break;
15293   case OMPC_simd:
15294     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
15295     break;
15296   case OMPC_nogroup:
15297     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
15298     break;
15299   case OMPC_unified_address:
15300     Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
15301     break;
15302   case OMPC_unified_shared_memory:
15303     Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
15304     break;
15305   case OMPC_reverse_offload:
15306     Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
15307     break;
15308   case OMPC_dynamic_allocators:
15309     Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
15310     break;
15311   case OMPC_destroy:
15312     Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc,
15313                                    /*LParenLoc=*/SourceLocation(),
15314                                    /*VarLoc=*/SourceLocation(), EndLoc);
15315     break;
15316   case OMPC_full:
15317     Res = ActOnOpenMPFullClause(StartLoc, EndLoc);
15318     break;
15319   case OMPC_partial:
15320     Res = ActOnOpenMPPartialClause(nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc);
15321     break;
15322   case OMPC_if:
15323   case OMPC_final:
15324   case OMPC_num_threads:
15325   case OMPC_safelen:
15326   case OMPC_simdlen:
15327   case OMPC_sizes:
15328   case OMPC_allocator:
15329   case OMPC_collapse:
15330   case OMPC_schedule:
15331   case OMPC_private:
15332   case OMPC_firstprivate:
15333   case OMPC_lastprivate:
15334   case OMPC_shared:
15335   case OMPC_reduction:
15336   case OMPC_task_reduction:
15337   case OMPC_in_reduction:
15338   case OMPC_linear:
15339   case OMPC_aligned:
15340   case OMPC_copyin:
15341   case OMPC_copyprivate:
15342   case OMPC_default:
15343   case OMPC_proc_bind:
15344   case OMPC_threadprivate:
15345   case OMPC_allocate:
15346   case OMPC_flush:
15347   case OMPC_depobj:
15348   case OMPC_depend:
15349   case OMPC_device:
15350   case OMPC_map:
15351   case OMPC_num_teams:
15352   case OMPC_thread_limit:
15353   case OMPC_priority:
15354   case OMPC_grainsize:
15355   case OMPC_num_tasks:
15356   case OMPC_hint:
15357   case OMPC_dist_schedule:
15358   case OMPC_defaultmap:
15359   case OMPC_unknown:
15360   case OMPC_uniform:
15361   case OMPC_to:
15362   case OMPC_from:
15363   case OMPC_use_device_ptr:
15364   case OMPC_use_device_addr:
15365   case OMPC_is_device_ptr:
15366   case OMPC_atomic_default_mem_order:
15367   case OMPC_device_type:
15368   case OMPC_match:
15369   case OMPC_nontemporal:
15370   case OMPC_order:
15371   case OMPC_novariants:
15372   case OMPC_nocontext:
15373   case OMPC_detach:
15374   case OMPC_inclusive:
15375   case OMPC_exclusive:
15376   case OMPC_uses_allocators:
15377   case OMPC_affinity:
15378   case OMPC_when:
15379   default:
15380     llvm_unreachable("Clause is not allowed.");
15381   }
15382   return Res;
15383 }
15384 
15385 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
15386                                          SourceLocation EndLoc) {
15387   DSAStack->setNowaitRegion();
15388   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
15389 }
15390 
15391 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
15392                                          SourceLocation EndLoc) {
15393   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
15394 }
15395 
15396 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
15397                                             SourceLocation EndLoc) {
15398   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
15399 }
15400 
15401 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
15402                                        SourceLocation EndLoc) {
15403   return new (Context) OMPReadClause(StartLoc, EndLoc);
15404 }
15405 
15406 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
15407                                         SourceLocation EndLoc) {
15408   return new (Context) OMPWriteClause(StartLoc, EndLoc);
15409 }
15410 
15411 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
15412                                          SourceLocation EndLoc) {
15413   return OMPUpdateClause::Create(Context, StartLoc, EndLoc);
15414 }
15415 
15416 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
15417                                           SourceLocation EndLoc) {
15418   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
15419 }
15420 
15421 OMPClause *Sema::ActOnOpenMPCompareClause(SourceLocation StartLoc,
15422                                           SourceLocation EndLoc) {
15423   return new (Context) OMPCompareClause(StartLoc, EndLoc);
15424 }
15425 
15426 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
15427                                          SourceLocation EndLoc) {
15428   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
15429 }
15430 
15431 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc,
15432                                          SourceLocation EndLoc) {
15433   return new (Context) OMPAcqRelClause(StartLoc, EndLoc);
15434 }
15435 
15436 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc,
15437                                           SourceLocation EndLoc) {
15438   return new (Context) OMPAcquireClause(StartLoc, EndLoc);
15439 }
15440 
15441 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc,
15442                                           SourceLocation EndLoc) {
15443   return new (Context) OMPReleaseClause(StartLoc, EndLoc);
15444 }
15445 
15446 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc,
15447                                           SourceLocation EndLoc) {
15448   return new (Context) OMPRelaxedClause(StartLoc, EndLoc);
15449 }
15450 
15451 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
15452                                           SourceLocation EndLoc) {
15453   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
15454 }
15455 
15456 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
15457                                        SourceLocation EndLoc) {
15458   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
15459 }
15460 
15461 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
15462                                           SourceLocation EndLoc) {
15463   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
15464 }
15465 
15466 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
15467                                                  SourceLocation EndLoc) {
15468   return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
15469 }
15470 
15471 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
15472                                                       SourceLocation EndLoc) {
15473   return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
15474 }
15475 
15476 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
15477                                                  SourceLocation EndLoc) {
15478   return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
15479 }
15480 
15481 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
15482                                                     SourceLocation EndLoc) {
15483   return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
15484 }
15485 
15486 StmtResult Sema::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses,
15487                                              SourceLocation StartLoc,
15488                                              SourceLocation EndLoc) {
15489 
15490   // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
15491   // At least one action-clause must appear on a directive.
15492   if (!hasClauses(Clauses, OMPC_init, OMPC_use, OMPC_destroy, OMPC_nowait)) {
15493     StringRef Expected = "'init', 'use', 'destroy', or 'nowait'";
15494     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
15495         << Expected << getOpenMPDirectiveName(OMPD_interop);
15496     return StmtError();
15497   }
15498 
15499   // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
15500   // A depend clause can only appear on the directive if a targetsync
15501   // interop-type is present or the interop-var was initialized with
15502   // the targetsync interop-type.
15503 
15504   // If there is any 'init' clause diagnose if there is no 'init' clause with
15505   // interop-type of 'targetsync'. Cases involving other directives cannot be
15506   // diagnosed.
15507   const OMPDependClause *DependClause = nullptr;
15508   bool HasInitClause = false;
15509   bool IsTargetSync = false;
15510   for (const OMPClause *C : Clauses) {
15511     if (IsTargetSync)
15512       break;
15513     if (const auto *InitClause = dyn_cast<OMPInitClause>(C)) {
15514       HasInitClause = true;
15515       if (InitClause->getIsTargetSync())
15516         IsTargetSync = true;
15517     } else if (const auto *DC = dyn_cast<OMPDependClause>(C)) {
15518       DependClause = DC;
15519     }
15520   }
15521   if (DependClause && HasInitClause && !IsTargetSync) {
15522     Diag(DependClause->getBeginLoc(), diag::err_omp_interop_bad_depend_clause);
15523     return StmtError();
15524   }
15525 
15526   // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
15527   // Each interop-var may be specified for at most one action-clause of each
15528   // interop construct.
15529   llvm::SmallPtrSet<const VarDecl *, 4> InteropVars;
15530   for (const OMPClause *C : Clauses) {
15531     OpenMPClauseKind ClauseKind = C->getClauseKind();
15532     const DeclRefExpr *DRE = nullptr;
15533     SourceLocation VarLoc;
15534 
15535     if (ClauseKind == OMPC_init) {
15536       const auto *IC = cast<OMPInitClause>(C);
15537       VarLoc = IC->getVarLoc();
15538       DRE = dyn_cast_or_null<DeclRefExpr>(IC->getInteropVar());
15539     } else if (ClauseKind == OMPC_use) {
15540       const auto *UC = cast<OMPUseClause>(C);
15541       VarLoc = UC->getVarLoc();
15542       DRE = dyn_cast_or_null<DeclRefExpr>(UC->getInteropVar());
15543     } else if (ClauseKind == OMPC_destroy) {
15544       const auto *DC = cast<OMPDestroyClause>(C);
15545       VarLoc = DC->getVarLoc();
15546       DRE = dyn_cast_or_null<DeclRefExpr>(DC->getInteropVar());
15547     }
15548 
15549     if (!DRE)
15550       continue;
15551 
15552     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
15553       if (!InteropVars.insert(VD->getCanonicalDecl()).second) {
15554         Diag(VarLoc, diag::err_omp_interop_var_multiple_actions) << VD;
15555         return StmtError();
15556       }
15557     }
15558   }
15559 
15560   return OMPInteropDirective::Create(Context, StartLoc, EndLoc, Clauses);
15561 }
15562 
15563 static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr,
15564                                    SourceLocation VarLoc,
15565                                    OpenMPClauseKind Kind) {
15566   if (InteropVarExpr->isValueDependent() || InteropVarExpr->isTypeDependent() ||
15567       InteropVarExpr->isInstantiationDependent() ||
15568       InteropVarExpr->containsUnexpandedParameterPack())
15569     return true;
15570 
15571   const auto *DRE = dyn_cast<DeclRefExpr>(InteropVarExpr);
15572   if (!DRE || !isa<VarDecl>(DRE->getDecl())) {
15573     SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) << 0;
15574     return false;
15575   }
15576 
15577   // Interop variable should be of type omp_interop_t.
15578   bool HasError = false;
15579   QualType InteropType;
15580   LookupResult Result(SemaRef, &SemaRef.Context.Idents.get("omp_interop_t"),
15581                       VarLoc, Sema::LookupOrdinaryName);
15582   if (SemaRef.LookupName(Result, SemaRef.getCurScope())) {
15583     NamedDecl *ND = Result.getFoundDecl();
15584     if (const auto *TD = dyn_cast<TypeDecl>(ND)) {
15585       InteropType = QualType(TD->getTypeForDecl(), 0);
15586     } else {
15587       HasError = true;
15588     }
15589   } else {
15590     HasError = true;
15591   }
15592 
15593   if (HasError) {
15594     SemaRef.Diag(VarLoc, diag::err_omp_implied_type_not_found)
15595         << "omp_interop_t";
15596     return false;
15597   }
15598 
15599   QualType VarType = InteropVarExpr->getType().getUnqualifiedType();
15600   if (!SemaRef.Context.hasSameType(InteropType, VarType)) {
15601     SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_wrong_type);
15602     return false;
15603   }
15604 
15605   // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
15606   // The interop-var passed to init or destroy must be non-const.
15607   if ((Kind == OMPC_init || Kind == OMPC_destroy) &&
15608       isConstNotMutableType(SemaRef, InteropVarExpr->getType())) {
15609     SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected)
15610         << /*non-const*/ 1;
15611     return false;
15612   }
15613   return true;
15614 }
15615 
15616 OMPClause *
15617 Sema::ActOnOpenMPInitClause(Expr *InteropVar, ArrayRef<Expr *> PrefExprs,
15618                             bool IsTarget, bool IsTargetSync,
15619                             SourceLocation StartLoc, SourceLocation LParenLoc,
15620                             SourceLocation VarLoc, SourceLocation EndLoc) {
15621 
15622   if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_init))
15623     return nullptr;
15624 
15625   // Check prefer_type values.  These foreign-runtime-id values are either
15626   // string literals or constant integral expressions.
15627   for (const Expr *E : PrefExprs) {
15628     if (E->isValueDependent() || E->isTypeDependent() ||
15629         E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
15630       continue;
15631     if (E->isIntegerConstantExpr(Context))
15632       continue;
15633     if (isa<StringLiteral>(E))
15634       continue;
15635     Diag(E->getExprLoc(), diag::err_omp_interop_prefer_type);
15636     return nullptr;
15637   }
15638 
15639   return OMPInitClause::Create(Context, InteropVar, PrefExprs, IsTarget,
15640                                IsTargetSync, StartLoc, LParenLoc, VarLoc,
15641                                EndLoc);
15642 }
15643 
15644 OMPClause *Sema::ActOnOpenMPUseClause(Expr *InteropVar, SourceLocation StartLoc,
15645                                       SourceLocation LParenLoc,
15646                                       SourceLocation VarLoc,
15647                                       SourceLocation EndLoc) {
15648 
15649   if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_use))
15650     return nullptr;
15651 
15652   return new (Context)
15653       OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
15654 }
15655 
15656 OMPClause *Sema::ActOnOpenMPDestroyClause(Expr *InteropVar,
15657                                           SourceLocation StartLoc,
15658                                           SourceLocation LParenLoc,
15659                                           SourceLocation VarLoc,
15660                                           SourceLocation EndLoc) {
15661   if (InteropVar &&
15662       !isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_destroy))
15663     return nullptr;
15664 
15665   return new (Context)
15666       OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
15667 }
15668 
15669 OMPClause *Sema::ActOnOpenMPNovariantsClause(Expr *Condition,
15670                                              SourceLocation StartLoc,
15671                                              SourceLocation LParenLoc,
15672                                              SourceLocation EndLoc) {
15673   Expr *ValExpr = Condition;
15674   Stmt *HelperValStmt = nullptr;
15675   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
15676   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
15677       !Condition->isInstantiationDependent() &&
15678       !Condition->containsUnexpandedParameterPack()) {
15679     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
15680     if (Val.isInvalid())
15681       return nullptr;
15682 
15683     ValExpr = MakeFullExpr(Val.get()).get();
15684 
15685     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
15686     CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_novariants,
15687                                                     LangOpts.OpenMP);
15688     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
15689       ValExpr = MakeFullExpr(ValExpr).get();
15690       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15691       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15692       HelperValStmt = buildPreInits(Context, Captures);
15693     }
15694   }
15695 
15696   return new (Context) OMPNovariantsClause(
15697       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
15698 }
15699 
15700 OMPClause *Sema::ActOnOpenMPNocontextClause(Expr *Condition,
15701                                             SourceLocation StartLoc,
15702                                             SourceLocation LParenLoc,
15703                                             SourceLocation EndLoc) {
15704   Expr *ValExpr = Condition;
15705   Stmt *HelperValStmt = nullptr;
15706   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
15707   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
15708       !Condition->isInstantiationDependent() &&
15709       !Condition->containsUnexpandedParameterPack()) {
15710     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
15711     if (Val.isInvalid())
15712       return nullptr;
15713 
15714     ValExpr = MakeFullExpr(Val.get()).get();
15715 
15716     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
15717     CaptureRegion =
15718         getOpenMPCaptureRegionForClause(DKind, OMPC_nocontext, LangOpts.OpenMP);
15719     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
15720       ValExpr = MakeFullExpr(ValExpr).get();
15721       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15722       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15723       HelperValStmt = buildPreInits(Context, Captures);
15724     }
15725   }
15726 
15727   return new (Context) OMPNocontextClause(ValExpr, HelperValStmt, CaptureRegion,
15728                                           StartLoc, LParenLoc, EndLoc);
15729 }
15730 
15731 OMPClause *Sema::ActOnOpenMPFilterClause(Expr *ThreadID,
15732                                          SourceLocation StartLoc,
15733                                          SourceLocation LParenLoc,
15734                                          SourceLocation EndLoc) {
15735   Expr *ValExpr = ThreadID;
15736   Stmt *HelperValStmt = nullptr;
15737 
15738   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
15739   OpenMPDirectiveKind CaptureRegion =
15740       getOpenMPCaptureRegionForClause(DKind, OMPC_filter, LangOpts.OpenMP);
15741   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
15742     ValExpr = MakeFullExpr(ValExpr).get();
15743     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15744     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15745     HelperValStmt = buildPreInits(Context, Captures);
15746   }
15747 
15748   return new (Context) OMPFilterClause(ValExpr, HelperValStmt, CaptureRegion,
15749                                        StartLoc, LParenLoc, EndLoc);
15750 }
15751 
15752 OMPClause *Sema::ActOnOpenMPVarListClause(
15753     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr,
15754     const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
15755     CXXScopeSpec &ReductionOrMapperIdScopeSpec,
15756     DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier,
15757     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
15758     ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit,
15759     SourceLocation ExtraModifierLoc,
15760     ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
15761     ArrayRef<SourceLocation> MotionModifiersLoc) {
15762   SourceLocation StartLoc = Locs.StartLoc;
15763   SourceLocation LParenLoc = Locs.LParenLoc;
15764   SourceLocation EndLoc = Locs.EndLoc;
15765   OMPClause *Res = nullptr;
15766   switch (Kind) {
15767   case OMPC_private:
15768     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
15769     break;
15770   case OMPC_firstprivate:
15771     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
15772     break;
15773   case OMPC_lastprivate:
15774     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown &&
15775            "Unexpected lastprivate modifier.");
15776     Res = ActOnOpenMPLastprivateClause(
15777         VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier),
15778         ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc);
15779     break;
15780   case OMPC_shared:
15781     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
15782     break;
15783   case OMPC_reduction:
15784     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown &&
15785            "Unexpected lastprivate modifier.");
15786     Res = ActOnOpenMPReductionClause(
15787         VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier),
15788         StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc,
15789         ReductionOrMapperIdScopeSpec, ReductionOrMapperId);
15790     break;
15791   case OMPC_task_reduction:
15792     Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
15793                                          EndLoc, ReductionOrMapperIdScopeSpec,
15794                                          ReductionOrMapperId);
15795     break;
15796   case OMPC_in_reduction:
15797     Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
15798                                        EndLoc, ReductionOrMapperIdScopeSpec,
15799                                        ReductionOrMapperId);
15800     break;
15801   case OMPC_linear:
15802     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown &&
15803            "Unexpected linear modifier.");
15804     Res = ActOnOpenMPLinearClause(
15805         VarList, DepModOrTailExpr, StartLoc, LParenLoc,
15806         static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc,
15807         ColonLoc, EndLoc);
15808     break;
15809   case OMPC_aligned:
15810     Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc,
15811                                    LParenLoc, ColonLoc, EndLoc);
15812     break;
15813   case OMPC_copyin:
15814     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
15815     break;
15816   case OMPC_copyprivate:
15817     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
15818     break;
15819   case OMPC_flush:
15820     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
15821     break;
15822   case OMPC_depend:
15823     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown &&
15824            "Unexpected depend modifier.");
15825     Res = ActOnOpenMPDependClause(
15826         DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier),
15827         ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc);
15828     break;
15829   case OMPC_map:
15830     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown &&
15831            "Unexpected map modifier.");
15832     Res = ActOnOpenMPMapClause(
15833         MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec,
15834         ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier),
15835         IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs);
15836     break;
15837   case OMPC_to:
15838     Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc,
15839                               ReductionOrMapperIdScopeSpec, ReductionOrMapperId,
15840                               ColonLoc, VarList, Locs);
15841     break;
15842   case OMPC_from:
15843     Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc,
15844                                 ReductionOrMapperIdScopeSpec,
15845                                 ReductionOrMapperId, ColonLoc, VarList, Locs);
15846     break;
15847   case OMPC_use_device_ptr:
15848     Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
15849     break;
15850   case OMPC_use_device_addr:
15851     Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs);
15852     break;
15853   case OMPC_is_device_ptr:
15854     Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
15855     break;
15856   case OMPC_allocate:
15857     Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc,
15858                                     LParenLoc, ColonLoc, EndLoc);
15859     break;
15860   case OMPC_nontemporal:
15861     Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc);
15862     break;
15863   case OMPC_inclusive:
15864     Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
15865     break;
15866   case OMPC_exclusive:
15867     Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
15868     break;
15869   case OMPC_affinity:
15870     Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc,
15871                                     DepModOrTailExpr, VarList);
15872     break;
15873   case OMPC_if:
15874   case OMPC_depobj:
15875   case OMPC_final:
15876   case OMPC_num_threads:
15877   case OMPC_safelen:
15878   case OMPC_simdlen:
15879   case OMPC_sizes:
15880   case OMPC_allocator:
15881   case OMPC_collapse:
15882   case OMPC_default:
15883   case OMPC_proc_bind:
15884   case OMPC_schedule:
15885   case OMPC_ordered:
15886   case OMPC_nowait:
15887   case OMPC_untied:
15888   case OMPC_mergeable:
15889   case OMPC_threadprivate:
15890   case OMPC_read:
15891   case OMPC_write:
15892   case OMPC_update:
15893   case OMPC_capture:
15894   case OMPC_compare:
15895   case OMPC_seq_cst:
15896   case OMPC_acq_rel:
15897   case OMPC_acquire:
15898   case OMPC_release:
15899   case OMPC_relaxed:
15900   case OMPC_device:
15901   case OMPC_threads:
15902   case OMPC_simd:
15903   case OMPC_num_teams:
15904   case OMPC_thread_limit:
15905   case OMPC_priority:
15906   case OMPC_grainsize:
15907   case OMPC_nogroup:
15908   case OMPC_num_tasks:
15909   case OMPC_hint:
15910   case OMPC_dist_schedule:
15911   case OMPC_defaultmap:
15912   case OMPC_unknown:
15913   case OMPC_uniform:
15914   case OMPC_unified_address:
15915   case OMPC_unified_shared_memory:
15916   case OMPC_reverse_offload:
15917   case OMPC_dynamic_allocators:
15918   case OMPC_atomic_default_mem_order:
15919   case OMPC_device_type:
15920   case OMPC_match:
15921   case OMPC_order:
15922   case OMPC_destroy:
15923   case OMPC_novariants:
15924   case OMPC_nocontext:
15925   case OMPC_detach:
15926   case OMPC_uses_allocators:
15927   case OMPC_when:
15928   case OMPC_bind:
15929   default:
15930     llvm_unreachable("Clause is not allowed.");
15931   }
15932   return Res;
15933 }
15934 
15935 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
15936                                        ExprObjectKind OK, SourceLocation Loc) {
15937   ExprResult Res = BuildDeclRefExpr(
15938       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
15939   if (!Res.isUsable())
15940     return ExprError();
15941   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
15942     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
15943     if (!Res.isUsable())
15944       return ExprError();
15945   }
15946   if (VK != VK_LValue && Res.get()->isGLValue()) {
15947     Res = DefaultLvalueConversion(Res.get());
15948     if (!Res.isUsable())
15949       return ExprError();
15950   }
15951   return Res;
15952 }
15953 
15954 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
15955                                           SourceLocation StartLoc,
15956                                           SourceLocation LParenLoc,
15957                                           SourceLocation EndLoc) {
15958   SmallVector<Expr *, 8> Vars;
15959   SmallVector<Expr *, 8> PrivateCopies;
15960   for (Expr *RefExpr : VarList) {
15961     assert(RefExpr && "NULL expr in OpenMP private clause.");
15962     SourceLocation ELoc;
15963     SourceRange ERange;
15964     Expr *SimpleRefExpr = RefExpr;
15965     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15966     if (Res.second) {
15967       // It will be analyzed later.
15968       Vars.push_back(RefExpr);
15969       PrivateCopies.push_back(nullptr);
15970     }
15971     ValueDecl *D = Res.first;
15972     if (!D)
15973       continue;
15974 
15975     QualType Type = D->getType();
15976     auto *VD = dyn_cast<VarDecl>(D);
15977 
15978     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
15979     //  A variable that appears in a private clause must not have an incomplete
15980     //  type or a reference type.
15981     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
15982       continue;
15983     Type = Type.getNonReferenceType();
15984 
15985     // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
15986     // A variable that is privatized must not have a const-qualified type
15987     // unless it is of class type with a mutable member. This restriction does
15988     // not apply to the firstprivate clause.
15989     //
15990     // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
15991     // A variable that appears in a private clause must not have a
15992     // const-qualified type unless it is of class type with a mutable member.
15993     if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
15994       continue;
15995 
15996     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
15997     // in a Construct]
15998     //  Variables with the predetermined data-sharing attributes may not be
15999     //  listed in data-sharing attributes clauses, except for the cases
16000     //  listed below. For these exceptions only, listing a predetermined
16001     //  variable in a data-sharing attribute clause is allowed and overrides
16002     //  the variable's predetermined data-sharing attributes.
16003     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
16004     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
16005       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
16006                                           << getOpenMPClauseName(OMPC_private);
16007       reportOriginalDsa(*this, DSAStack, D, DVar);
16008       continue;
16009     }
16010 
16011     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
16012     // Variably modified types are not supported for tasks.
16013     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
16014         isOpenMPTaskingDirective(CurrDir)) {
16015       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
16016           << getOpenMPClauseName(OMPC_private) << Type
16017           << getOpenMPDirectiveName(CurrDir);
16018       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
16019                                VarDecl::DeclarationOnly;
16020       Diag(D->getLocation(),
16021            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
16022           << D;
16023       continue;
16024     }
16025 
16026     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
16027     // A list item cannot appear in both a map clause and a data-sharing
16028     // attribute clause on the same construct
16029     //
16030     // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
16031     // A list item cannot appear in both a map clause and a data-sharing
16032     // attribute clause on the same construct unless the construct is a
16033     // combined construct.
16034     if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) ||
16035         CurrDir == OMPD_target) {
16036       OpenMPClauseKind ConflictKind;
16037       if (DSAStack->checkMappableExprComponentListsForDecl(
16038               VD, /*CurrentRegionOnly=*/true,
16039               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
16040                   OpenMPClauseKind WhereFoundClauseKind) -> bool {
16041                 ConflictKind = WhereFoundClauseKind;
16042                 return true;
16043               })) {
16044         Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
16045             << getOpenMPClauseName(OMPC_private)
16046             << getOpenMPClauseName(ConflictKind)
16047             << getOpenMPDirectiveName(CurrDir);
16048         reportOriginalDsa(*this, DSAStack, D, DVar);
16049         continue;
16050       }
16051     }
16052 
16053     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
16054     //  A variable of class type (or array thereof) that appears in a private
16055     //  clause requires an accessible, unambiguous default constructor for the
16056     //  class type.
16057     // Generate helper private variable and initialize it with the default
16058     // value. The address of the original variable is replaced by the address of
16059     // the new private variable in CodeGen. This new variable is not added to
16060     // IdResolver, so the code in the OpenMP region uses original variable for
16061     // proper diagnostics.
16062     Type = Type.getUnqualifiedType();
16063     VarDecl *VDPrivate =
16064         buildVarDecl(*this, ELoc, Type, D->getName(),
16065                      D->hasAttrs() ? &D->getAttrs() : nullptr,
16066                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
16067     ActOnUninitializedDecl(VDPrivate);
16068     if (VDPrivate->isInvalidDecl())
16069       continue;
16070     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
16071         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
16072 
16073     DeclRefExpr *Ref = nullptr;
16074     if (!VD && !CurContext->isDependentContext())
16075       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
16076     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
16077     Vars.push_back((VD || CurContext->isDependentContext())
16078                        ? RefExpr->IgnoreParens()
16079                        : Ref);
16080     PrivateCopies.push_back(VDPrivateRefExpr);
16081   }
16082 
16083   if (Vars.empty())
16084     return nullptr;
16085 
16086   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
16087                                   PrivateCopies);
16088 }
16089 
16090 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
16091                                                SourceLocation StartLoc,
16092                                                SourceLocation LParenLoc,
16093                                                SourceLocation EndLoc) {
16094   SmallVector<Expr *, 8> Vars;
16095   SmallVector<Expr *, 8> PrivateCopies;
16096   SmallVector<Expr *, 8> Inits;
16097   SmallVector<Decl *, 4> ExprCaptures;
16098   bool IsImplicitClause =
16099       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
16100   SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
16101 
16102   for (Expr *RefExpr : VarList) {
16103     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
16104     SourceLocation ELoc;
16105     SourceRange ERange;
16106     Expr *SimpleRefExpr = RefExpr;
16107     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16108     if (Res.second) {
16109       // It will be analyzed later.
16110       Vars.push_back(RefExpr);
16111       PrivateCopies.push_back(nullptr);
16112       Inits.push_back(nullptr);
16113     }
16114     ValueDecl *D = Res.first;
16115     if (!D)
16116       continue;
16117 
16118     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
16119     QualType Type = D->getType();
16120     auto *VD = dyn_cast<VarDecl>(D);
16121 
16122     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
16123     //  A variable that appears in a private clause must not have an incomplete
16124     //  type or a reference type.
16125     if (RequireCompleteType(ELoc, Type,
16126                             diag::err_omp_firstprivate_incomplete_type))
16127       continue;
16128     Type = Type.getNonReferenceType();
16129 
16130     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
16131     //  A variable of class type (or array thereof) that appears in a private
16132     //  clause requires an accessible, unambiguous copy constructor for the
16133     //  class type.
16134     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
16135 
16136     // If an implicit firstprivate variable found it was checked already.
16137     DSAStackTy::DSAVarData TopDVar;
16138     if (!IsImplicitClause) {
16139       DSAStackTy::DSAVarData DVar =
16140           DSAStack->getTopDSA(D, /*FromParent=*/false);
16141       TopDVar = DVar;
16142       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
16143       bool IsConstant = ElemType.isConstant(Context);
16144       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
16145       //  A list item that specifies a given variable may not appear in more
16146       // than one clause on the same directive, except that a variable may be
16147       //  specified in both firstprivate and lastprivate clauses.
16148       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
16149       // A list item may appear in a firstprivate or lastprivate clause but not
16150       // both.
16151       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
16152           (isOpenMPDistributeDirective(CurrDir) ||
16153            DVar.CKind != OMPC_lastprivate) &&
16154           DVar.RefExpr) {
16155         Diag(ELoc, diag::err_omp_wrong_dsa)
16156             << getOpenMPClauseName(DVar.CKind)
16157             << getOpenMPClauseName(OMPC_firstprivate);
16158         reportOriginalDsa(*this, DSAStack, D, DVar);
16159         continue;
16160       }
16161 
16162       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
16163       // in a Construct]
16164       //  Variables with the predetermined data-sharing attributes may not be
16165       //  listed in data-sharing attributes clauses, except for the cases
16166       //  listed below. For these exceptions only, listing a predetermined
16167       //  variable in a data-sharing attribute clause is allowed and overrides
16168       //  the variable's predetermined data-sharing attributes.
16169       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
16170       // in a Construct, C/C++, p.2]
16171       //  Variables with const-qualified type having no mutable member may be
16172       //  listed in a firstprivate clause, even if they are static data members.
16173       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
16174           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
16175         Diag(ELoc, diag::err_omp_wrong_dsa)
16176             << getOpenMPClauseName(DVar.CKind)
16177             << getOpenMPClauseName(OMPC_firstprivate);
16178         reportOriginalDsa(*this, DSAStack, D, DVar);
16179         continue;
16180       }
16181 
16182       // OpenMP [2.9.3.4, Restrictions, p.2]
16183       //  A list item that is private within a parallel region must not appear
16184       //  in a firstprivate clause on a worksharing construct if any of the
16185       //  worksharing regions arising from the worksharing construct ever bind
16186       //  to any of the parallel regions arising from the parallel construct.
16187       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
16188       // A list item that is private within a teams region must not appear in a
16189       // firstprivate clause on a distribute construct if any of the distribute
16190       // regions arising from the distribute construct ever bind to any of the
16191       // teams regions arising from the teams construct.
16192       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
16193       // A list item that appears in a reduction clause of a teams construct
16194       // must not appear in a firstprivate clause on a distribute construct if
16195       // any of the distribute regions arising from the distribute construct
16196       // ever bind to any of the teams regions arising from the teams construct.
16197       if ((isOpenMPWorksharingDirective(CurrDir) ||
16198            isOpenMPDistributeDirective(CurrDir)) &&
16199           !isOpenMPParallelDirective(CurrDir) &&
16200           !isOpenMPTeamsDirective(CurrDir)) {
16201         DVar = DSAStack->getImplicitDSA(D, true);
16202         if (DVar.CKind != OMPC_shared &&
16203             (isOpenMPParallelDirective(DVar.DKind) ||
16204              isOpenMPTeamsDirective(DVar.DKind) ||
16205              DVar.DKind == OMPD_unknown)) {
16206           Diag(ELoc, diag::err_omp_required_access)
16207               << getOpenMPClauseName(OMPC_firstprivate)
16208               << getOpenMPClauseName(OMPC_shared);
16209           reportOriginalDsa(*this, DSAStack, D, DVar);
16210           continue;
16211         }
16212       }
16213       // OpenMP [2.9.3.4, Restrictions, p.3]
16214       //  A list item that appears in a reduction clause of a parallel construct
16215       //  must not appear in a firstprivate clause on a worksharing or task
16216       //  construct if any of the worksharing or task regions arising from the
16217       //  worksharing or task construct ever bind to any of the parallel regions
16218       //  arising from the parallel construct.
16219       // OpenMP [2.9.3.4, Restrictions, p.4]
16220       //  A list item that appears in a reduction clause in worksharing
16221       //  construct must not appear in a firstprivate clause in a task construct
16222       //  encountered during execution of any of the worksharing regions arising
16223       //  from the worksharing construct.
16224       if (isOpenMPTaskingDirective(CurrDir)) {
16225         DVar = DSAStack->hasInnermostDSA(
16226             D,
16227             [](OpenMPClauseKind C, bool AppliedToPointee) {
16228               return C == OMPC_reduction && !AppliedToPointee;
16229             },
16230             [](OpenMPDirectiveKind K) {
16231               return isOpenMPParallelDirective(K) ||
16232                      isOpenMPWorksharingDirective(K) ||
16233                      isOpenMPTeamsDirective(K);
16234             },
16235             /*FromParent=*/true);
16236         if (DVar.CKind == OMPC_reduction &&
16237             (isOpenMPParallelDirective(DVar.DKind) ||
16238              isOpenMPWorksharingDirective(DVar.DKind) ||
16239              isOpenMPTeamsDirective(DVar.DKind))) {
16240           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
16241               << getOpenMPDirectiveName(DVar.DKind);
16242           reportOriginalDsa(*this, DSAStack, D, DVar);
16243           continue;
16244         }
16245       }
16246 
16247       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
16248       // A list item cannot appear in both a map clause and a data-sharing
16249       // attribute clause on the same construct
16250       //
16251       // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
16252       // A list item cannot appear in both a map clause and a data-sharing
16253       // attribute clause on the same construct unless the construct is a
16254       // combined construct.
16255       if ((LangOpts.OpenMP <= 45 &&
16256            isOpenMPTargetExecutionDirective(CurrDir)) ||
16257           CurrDir == OMPD_target) {
16258         OpenMPClauseKind ConflictKind;
16259         if (DSAStack->checkMappableExprComponentListsForDecl(
16260                 VD, /*CurrentRegionOnly=*/true,
16261                 [&ConflictKind](
16262                     OMPClauseMappableExprCommon::MappableExprComponentListRef,
16263                     OpenMPClauseKind WhereFoundClauseKind) {
16264                   ConflictKind = WhereFoundClauseKind;
16265                   return true;
16266                 })) {
16267           Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
16268               << getOpenMPClauseName(OMPC_firstprivate)
16269               << getOpenMPClauseName(ConflictKind)
16270               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
16271           reportOriginalDsa(*this, DSAStack, D, DVar);
16272           continue;
16273         }
16274       }
16275     }
16276 
16277     // Variably modified types are not supported for tasks.
16278     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
16279         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
16280       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
16281           << getOpenMPClauseName(OMPC_firstprivate) << Type
16282           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
16283       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
16284                                VarDecl::DeclarationOnly;
16285       Diag(D->getLocation(),
16286            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
16287           << D;
16288       continue;
16289     }
16290 
16291     Type = Type.getUnqualifiedType();
16292     VarDecl *VDPrivate =
16293         buildVarDecl(*this, ELoc, Type, D->getName(),
16294                      D->hasAttrs() ? &D->getAttrs() : nullptr,
16295                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
16296     // Generate helper private variable and initialize it with the value of the
16297     // original variable. The address of the original variable is replaced by
16298     // the address of the new private variable in the CodeGen. This new variable
16299     // is not added to IdResolver, so the code in the OpenMP region uses
16300     // original variable for proper diagnostics and variable capturing.
16301     Expr *VDInitRefExpr = nullptr;
16302     // For arrays generate initializer for single element and replace it by the
16303     // original array element in CodeGen.
16304     if (Type->isArrayType()) {
16305       VarDecl *VDInit =
16306           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
16307       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
16308       Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
16309       ElemType = ElemType.getUnqualifiedType();
16310       VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
16311                                          ".firstprivate.temp");
16312       InitializedEntity Entity =
16313           InitializedEntity::InitializeVariable(VDInitTemp);
16314       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
16315 
16316       InitializationSequence InitSeq(*this, Entity, Kind, Init);
16317       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
16318       if (Result.isInvalid())
16319         VDPrivate->setInvalidDecl();
16320       else
16321         VDPrivate->setInit(Result.getAs<Expr>());
16322       // Remove temp variable declaration.
16323       Context.Deallocate(VDInitTemp);
16324     } else {
16325       VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
16326                                      ".firstprivate.temp");
16327       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
16328                                        RefExpr->getExprLoc());
16329       AddInitializerToDecl(VDPrivate,
16330                            DefaultLvalueConversion(VDInitRefExpr).get(),
16331                            /*DirectInit=*/false);
16332     }
16333     if (VDPrivate->isInvalidDecl()) {
16334       if (IsImplicitClause) {
16335         Diag(RefExpr->getExprLoc(),
16336              diag::note_omp_task_predetermined_firstprivate_here);
16337       }
16338       continue;
16339     }
16340     CurContext->addDecl(VDPrivate);
16341     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
16342         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
16343         RefExpr->getExprLoc());
16344     DeclRefExpr *Ref = nullptr;
16345     if (!VD && !CurContext->isDependentContext()) {
16346       if (TopDVar.CKind == OMPC_lastprivate) {
16347         Ref = TopDVar.PrivateCopy;
16348       } else {
16349         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
16350         if (!isOpenMPCapturedDecl(D))
16351           ExprCaptures.push_back(Ref->getDecl());
16352       }
16353     }
16354     if (!IsImplicitClause)
16355       DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
16356     Vars.push_back((VD || CurContext->isDependentContext())
16357                        ? RefExpr->IgnoreParens()
16358                        : Ref);
16359     PrivateCopies.push_back(VDPrivateRefExpr);
16360     Inits.push_back(VDInitRefExpr);
16361   }
16362 
16363   if (Vars.empty())
16364     return nullptr;
16365 
16366   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
16367                                        Vars, PrivateCopies, Inits,
16368                                        buildPreInits(Context, ExprCaptures));
16369 }
16370 
16371 OMPClause *Sema::ActOnOpenMPLastprivateClause(
16372     ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind,
16373     SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc,
16374     SourceLocation LParenLoc, SourceLocation EndLoc) {
16375   if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) {
16376     assert(ColonLoc.isValid() && "Colon location must be valid.");
16377     Diag(LPKindLoc, diag::err_omp_unexpected_clause_value)
16378         << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0,
16379                                    /*Last=*/OMPC_LASTPRIVATE_unknown)
16380         << getOpenMPClauseName(OMPC_lastprivate);
16381     return nullptr;
16382   }
16383 
16384   SmallVector<Expr *, 8> Vars;
16385   SmallVector<Expr *, 8> SrcExprs;
16386   SmallVector<Expr *, 8> DstExprs;
16387   SmallVector<Expr *, 8> AssignmentOps;
16388   SmallVector<Decl *, 4> ExprCaptures;
16389   SmallVector<Expr *, 4> ExprPostUpdates;
16390   for (Expr *RefExpr : VarList) {
16391     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
16392     SourceLocation ELoc;
16393     SourceRange ERange;
16394     Expr *SimpleRefExpr = RefExpr;
16395     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16396     if (Res.second) {
16397       // It will be analyzed later.
16398       Vars.push_back(RefExpr);
16399       SrcExprs.push_back(nullptr);
16400       DstExprs.push_back(nullptr);
16401       AssignmentOps.push_back(nullptr);
16402     }
16403     ValueDecl *D = Res.first;
16404     if (!D)
16405       continue;
16406 
16407     QualType Type = D->getType();
16408     auto *VD = dyn_cast<VarDecl>(D);
16409 
16410     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
16411     //  A variable that appears in a lastprivate clause must not have an
16412     //  incomplete type or a reference type.
16413     if (RequireCompleteType(ELoc, Type,
16414                             diag::err_omp_lastprivate_incomplete_type))
16415       continue;
16416     Type = Type.getNonReferenceType();
16417 
16418     // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
16419     // A variable that is privatized must not have a const-qualified type
16420     // unless it is of class type with a mutable member. This restriction does
16421     // not apply to the firstprivate clause.
16422     //
16423     // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
16424     // A variable that appears in a lastprivate clause must not have a
16425     // const-qualified type unless it is of class type with a mutable member.
16426     if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
16427       continue;
16428 
16429     // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions]
16430     // A list item that appears in a lastprivate clause with the conditional
16431     // modifier must be a scalar variable.
16432     if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) {
16433       Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar);
16434       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
16435                                VarDecl::DeclarationOnly;
16436       Diag(D->getLocation(),
16437            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
16438           << D;
16439       continue;
16440     }
16441 
16442     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
16443     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
16444     // in a Construct]
16445     //  Variables with the predetermined data-sharing attributes may not be
16446     //  listed in data-sharing attributes clauses, except for the cases
16447     //  listed below.
16448     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
16449     // A list item may appear in a firstprivate or lastprivate clause but not
16450     // both.
16451     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
16452     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
16453         (isOpenMPDistributeDirective(CurrDir) ||
16454          DVar.CKind != OMPC_firstprivate) &&
16455         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
16456       Diag(ELoc, diag::err_omp_wrong_dsa)
16457           << getOpenMPClauseName(DVar.CKind)
16458           << getOpenMPClauseName(OMPC_lastprivate);
16459       reportOriginalDsa(*this, DSAStack, D, DVar);
16460       continue;
16461     }
16462 
16463     // OpenMP [2.14.3.5, Restrictions, p.2]
16464     // A list item that is private within a parallel region, or that appears in
16465     // the reduction clause of a parallel construct, must not appear in a
16466     // lastprivate clause on a worksharing construct if any of the corresponding
16467     // worksharing regions ever binds to any of the corresponding parallel
16468     // regions.
16469     DSAStackTy::DSAVarData TopDVar = DVar;
16470     if (isOpenMPWorksharingDirective(CurrDir) &&
16471         !isOpenMPParallelDirective(CurrDir) &&
16472         !isOpenMPTeamsDirective(CurrDir)) {
16473       DVar = DSAStack->getImplicitDSA(D, true);
16474       if (DVar.CKind != OMPC_shared) {
16475         Diag(ELoc, diag::err_omp_required_access)
16476             << getOpenMPClauseName(OMPC_lastprivate)
16477             << getOpenMPClauseName(OMPC_shared);
16478         reportOriginalDsa(*this, DSAStack, D, DVar);
16479         continue;
16480       }
16481     }
16482 
16483     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
16484     //  A variable of class type (or array thereof) that appears in a
16485     //  lastprivate clause requires an accessible, unambiguous default
16486     //  constructor for the class type, unless the list item is also specified
16487     //  in a firstprivate clause.
16488     //  A variable of class type (or array thereof) that appears in a
16489     //  lastprivate clause requires an accessible, unambiguous copy assignment
16490     //  operator for the class type.
16491     Type = Context.getBaseElementType(Type).getNonReferenceType();
16492     VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
16493                                   Type.getUnqualifiedType(), ".lastprivate.src",
16494                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
16495     DeclRefExpr *PseudoSrcExpr =
16496         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
16497     VarDecl *DstVD =
16498         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
16499                      D->hasAttrs() ? &D->getAttrs() : nullptr);
16500     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
16501     // For arrays generate assignment operation for single element and replace
16502     // it by the original array element in CodeGen.
16503     ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
16504                                          PseudoDstExpr, PseudoSrcExpr);
16505     if (AssignmentOp.isInvalid())
16506       continue;
16507     AssignmentOp =
16508         ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
16509     if (AssignmentOp.isInvalid())
16510       continue;
16511 
16512     DeclRefExpr *Ref = nullptr;
16513     if (!VD && !CurContext->isDependentContext()) {
16514       if (TopDVar.CKind == OMPC_firstprivate) {
16515         Ref = TopDVar.PrivateCopy;
16516       } else {
16517         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
16518         if (!isOpenMPCapturedDecl(D))
16519           ExprCaptures.push_back(Ref->getDecl());
16520       }
16521       if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) ||
16522           (!isOpenMPCapturedDecl(D) &&
16523            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
16524         ExprResult RefRes = DefaultLvalueConversion(Ref);
16525         if (!RefRes.isUsable())
16526           continue;
16527         ExprResult PostUpdateRes =
16528             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
16529                        RefRes.get());
16530         if (!PostUpdateRes.isUsable())
16531           continue;
16532         ExprPostUpdates.push_back(
16533             IgnoredValueConversions(PostUpdateRes.get()).get());
16534       }
16535     }
16536     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
16537     Vars.push_back((VD || CurContext->isDependentContext())
16538                        ? RefExpr->IgnoreParens()
16539                        : Ref);
16540     SrcExprs.push_back(PseudoSrcExpr);
16541     DstExprs.push_back(PseudoDstExpr);
16542     AssignmentOps.push_back(AssignmentOp.get());
16543   }
16544 
16545   if (Vars.empty())
16546     return nullptr;
16547 
16548   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
16549                                       Vars, SrcExprs, DstExprs, AssignmentOps,
16550                                       LPKind, LPKindLoc, ColonLoc,
16551                                       buildPreInits(Context, ExprCaptures),
16552                                       buildPostUpdate(*this, ExprPostUpdates));
16553 }
16554 
16555 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
16556                                          SourceLocation StartLoc,
16557                                          SourceLocation LParenLoc,
16558                                          SourceLocation EndLoc) {
16559   SmallVector<Expr *, 8> Vars;
16560   for (Expr *RefExpr : VarList) {
16561     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
16562     SourceLocation ELoc;
16563     SourceRange ERange;
16564     Expr *SimpleRefExpr = RefExpr;
16565     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16566     if (Res.second) {
16567       // It will be analyzed later.
16568       Vars.push_back(RefExpr);
16569     }
16570     ValueDecl *D = Res.first;
16571     if (!D)
16572       continue;
16573 
16574     auto *VD = dyn_cast<VarDecl>(D);
16575     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
16576     // in a Construct]
16577     //  Variables with the predetermined data-sharing attributes may not be
16578     //  listed in data-sharing attributes clauses, except for the cases
16579     //  listed below. For these exceptions only, listing a predetermined
16580     //  variable in a data-sharing attribute clause is allowed and overrides
16581     //  the variable's predetermined data-sharing attributes.
16582     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
16583     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
16584         DVar.RefExpr) {
16585       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
16586                                           << getOpenMPClauseName(OMPC_shared);
16587       reportOriginalDsa(*this, DSAStack, D, DVar);
16588       continue;
16589     }
16590 
16591     DeclRefExpr *Ref = nullptr;
16592     if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
16593       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
16594     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
16595     Vars.push_back((VD || !Ref || CurContext->isDependentContext())
16596                        ? RefExpr->IgnoreParens()
16597                        : Ref);
16598   }
16599 
16600   if (Vars.empty())
16601     return nullptr;
16602 
16603   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
16604 }
16605 
16606 namespace {
16607 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
16608   DSAStackTy *Stack;
16609 
16610 public:
16611   bool VisitDeclRefExpr(DeclRefExpr *E) {
16612     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
16613       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
16614       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
16615         return false;
16616       if (DVar.CKind != OMPC_unknown)
16617         return true;
16618       DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
16619           VD,
16620           [](OpenMPClauseKind C, bool AppliedToPointee) {
16621             return isOpenMPPrivate(C) && !AppliedToPointee;
16622           },
16623           [](OpenMPDirectiveKind) { return true; },
16624           /*FromParent=*/true);
16625       return DVarPrivate.CKind != OMPC_unknown;
16626     }
16627     return false;
16628   }
16629   bool VisitStmt(Stmt *S) {
16630     for (Stmt *Child : S->children()) {
16631       if (Child && Visit(Child))
16632         return true;
16633     }
16634     return false;
16635   }
16636   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
16637 };
16638 } // namespace
16639 
16640 namespace {
16641 // Transform MemberExpression for specified FieldDecl of current class to
16642 // DeclRefExpr to specified OMPCapturedExprDecl.
16643 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
16644   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
16645   ValueDecl *Field = nullptr;
16646   DeclRefExpr *CapturedExpr = nullptr;
16647 
16648 public:
16649   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
16650       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
16651 
16652   ExprResult TransformMemberExpr(MemberExpr *E) {
16653     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
16654         E->getMemberDecl() == Field) {
16655       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
16656       return CapturedExpr;
16657     }
16658     return BaseTransform::TransformMemberExpr(E);
16659   }
16660   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
16661 };
16662 } // namespace
16663 
16664 template <typename T, typename U>
16665 static T filterLookupForUDReductionAndMapper(
16666     SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
16667   for (U &Set : Lookups) {
16668     for (auto *D : Set) {
16669       if (T Res = Gen(cast<ValueDecl>(D)))
16670         return Res;
16671     }
16672   }
16673   return T();
16674 }
16675 
16676 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
16677   assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
16678 
16679   for (auto RD : D->redecls()) {
16680     // Don't bother with extra checks if we already know this one isn't visible.
16681     if (RD == D)
16682       continue;
16683 
16684     auto ND = cast<NamedDecl>(RD);
16685     if (LookupResult::isVisible(SemaRef, ND))
16686       return ND;
16687   }
16688 
16689   return nullptr;
16690 }
16691 
16692 static void
16693 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
16694                         SourceLocation Loc, QualType Ty,
16695                         SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
16696   // Find all of the associated namespaces and classes based on the
16697   // arguments we have.
16698   Sema::AssociatedNamespaceSet AssociatedNamespaces;
16699   Sema::AssociatedClassSet AssociatedClasses;
16700   OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
16701   SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
16702                                              AssociatedClasses);
16703 
16704   // C++ [basic.lookup.argdep]p3:
16705   //   Let X be the lookup set produced by unqualified lookup (3.4.1)
16706   //   and let Y be the lookup set produced by argument dependent
16707   //   lookup (defined as follows). If X contains [...] then Y is
16708   //   empty. Otherwise Y is the set of declarations found in the
16709   //   namespaces associated with the argument types as described
16710   //   below. The set of declarations found by the lookup of the name
16711   //   is the union of X and Y.
16712   //
16713   // Here, we compute Y and add its members to the overloaded
16714   // candidate set.
16715   for (auto *NS : AssociatedNamespaces) {
16716     //   When considering an associated namespace, the lookup is the
16717     //   same as the lookup performed when the associated namespace is
16718     //   used as a qualifier (3.4.3.2) except that:
16719     //
16720     //     -- Any using-directives in the associated namespace are
16721     //        ignored.
16722     //
16723     //     -- Any namespace-scope friend functions declared in
16724     //        associated classes are visible within their respective
16725     //        namespaces even if they are not visible during an ordinary
16726     //        lookup (11.4).
16727     DeclContext::lookup_result R = NS->lookup(Id.getName());
16728     for (auto *D : R) {
16729       auto *Underlying = D;
16730       if (auto *USD = dyn_cast<UsingShadowDecl>(D))
16731         Underlying = USD->getTargetDecl();
16732 
16733       if (!isa<OMPDeclareReductionDecl>(Underlying) &&
16734           !isa<OMPDeclareMapperDecl>(Underlying))
16735         continue;
16736 
16737       if (!SemaRef.isVisible(D)) {
16738         D = findAcceptableDecl(SemaRef, D);
16739         if (!D)
16740           continue;
16741         if (auto *USD = dyn_cast<UsingShadowDecl>(D))
16742           Underlying = USD->getTargetDecl();
16743       }
16744       Lookups.emplace_back();
16745       Lookups.back().addDecl(Underlying);
16746     }
16747   }
16748 }
16749 
16750 static ExprResult
16751 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
16752                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
16753                          const DeclarationNameInfo &ReductionId, QualType Ty,
16754                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
16755   if (ReductionIdScopeSpec.isInvalid())
16756     return ExprError();
16757   SmallVector<UnresolvedSet<8>, 4> Lookups;
16758   if (S) {
16759     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
16760     Lookup.suppressDiagnostics();
16761     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
16762       NamedDecl *D = Lookup.getRepresentativeDecl();
16763       do {
16764         S = S->getParent();
16765       } while (S && !S->isDeclScope(D));
16766       if (S)
16767         S = S->getParent();
16768       Lookups.emplace_back();
16769       Lookups.back().append(Lookup.begin(), Lookup.end());
16770       Lookup.clear();
16771     }
16772   } else if (auto *ULE =
16773                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
16774     Lookups.push_back(UnresolvedSet<8>());
16775     Decl *PrevD = nullptr;
16776     for (NamedDecl *D : ULE->decls()) {
16777       if (D == PrevD)
16778         Lookups.push_back(UnresolvedSet<8>());
16779       else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D))
16780         Lookups.back().addDecl(DRD);
16781       PrevD = D;
16782     }
16783   }
16784   if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
16785       Ty->isInstantiationDependentType() ||
16786       Ty->containsUnexpandedParameterPack() ||
16787       filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
16788         return !D->isInvalidDecl() &&
16789                (D->getType()->isDependentType() ||
16790                 D->getType()->isInstantiationDependentType() ||
16791                 D->getType()->containsUnexpandedParameterPack());
16792       })) {
16793     UnresolvedSet<8> ResSet;
16794     for (const UnresolvedSet<8> &Set : Lookups) {
16795       if (Set.empty())
16796         continue;
16797       ResSet.append(Set.begin(), Set.end());
16798       // The last item marks the end of all declarations at the specified scope.
16799       ResSet.addDecl(Set[Set.size() - 1]);
16800     }
16801     return UnresolvedLookupExpr::Create(
16802         SemaRef.Context, /*NamingClass=*/nullptr,
16803         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
16804         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
16805   }
16806   // Lookup inside the classes.
16807   // C++ [over.match.oper]p3:
16808   //   For a unary operator @ with an operand of a type whose
16809   //   cv-unqualified version is T1, and for a binary operator @ with
16810   //   a left operand of a type whose cv-unqualified version is T1 and
16811   //   a right operand of a type whose cv-unqualified version is T2,
16812   //   three sets of candidate functions, designated member
16813   //   candidates, non-member candidates and built-in candidates, are
16814   //   constructed as follows:
16815   //     -- If T1 is a complete class type or a class currently being
16816   //        defined, the set of member candidates is the result of the
16817   //        qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
16818   //        the set of member candidates is empty.
16819   LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
16820   Lookup.suppressDiagnostics();
16821   if (const auto *TyRec = Ty->getAs<RecordType>()) {
16822     // Complete the type if it can be completed.
16823     // If the type is neither complete nor being defined, bail out now.
16824     if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
16825         TyRec->getDecl()->getDefinition()) {
16826       Lookup.clear();
16827       SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
16828       if (Lookup.empty()) {
16829         Lookups.emplace_back();
16830         Lookups.back().append(Lookup.begin(), Lookup.end());
16831       }
16832     }
16833   }
16834   // Perform ADL.
16835   if (SemaRef.getLangOpts().CPlusPlus)
16836     argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
16837   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
16838           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
16839             if (!D->isInvalidDecl() &&
16840                 SemaRef.Context.hasSameType(D->getType(), Ty))
16841               return D;
16842             return nullptr;
16843           }))
16844     return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
16845                                     VK_LValue, Loc);
16846   if (SemaRef.getLangOpts().CPlusPlus) {
16847     if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
16848             Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
16849               if (!D->isInvalidDecl() &&
16850                   SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
16851                   !Ty.isMoreQualifiedThan(D->getType()))
16852                 return D;
16853               return nullptr;
16854             })) {
16855       CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
16856                          /*DetectVirtual=*/false);
16857       if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
16858         if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
16859                 VD->getType().getUnqualifiedType()))) {
16860           if (SemaRef.CheckBaseClassAccess(
16861                   Loc, VD->getType(), Ty, Paths.front(),
16862                   /*DiagID=*/0) != Sema::AR_inaccessible) {
16863             SemaRef.BuildBasePathArray(Paths, BasePath);
16864             return SemaRef.BuildDeclRefExpr(
16865                 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
16866           }
16867         }
16868       }
16869     }
16870   }
16871   if (ReductionIdScopeSpec.isSet()) {
16872     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier)
16873         << Ty << Range;
16874     return ExprError();
16875   }
16876   return ExprEmpty();
16877 }
16878 
16879 namespace {
16880 /// Data for the reduction-based clauses.
16881 struct ReductionData {
16882   /// List of original reduction items.
16883   SmallVector<Expr *, 8> Vars;
16884   /// List of private copies of the reduction items.
16885   SmallVector<Expr *, 8> Privates;
16886   /// LHS expressions for the reduction_op expressions.
16887   SmallVector<Expr *, 8> LHSs;
16888   /// RHS expressions for the reduction_op expressions.
16889   SmallVector<Expr *, 8> RHSs;
16890   /// Reduction operation expression.
16891   SmallVector<Expr *, 8> ReductionOps;
16892   /// inscan copy operation expressions.
16893   SmallVector<Expr *, 8> InscanCopyOps;
16894   /// inscan copy temp array expressions for prefix sums.
16895   SmallVector<Expr *, 8> InscanCopyArrayTemps;
16896   /// inscan copy temp array element expressions for prefix sums.
16897   SmallVector<Expr *, 8> InscanCopyArrayElems;
16898   /// Taskgroup descriptors for the corresponding reduction items in
16899   /// in_reduction clauses.
16900   SmallVector<Expr *, 8> TaskgroupDescriptors;
16901   /// List of captures for clause.
16902   SmallVector<Decl *, 4> ExprCaptures;
16903   /// List of postupdate expressions.
16904   SmallVector<Expr *, 4> ExprPostUpdates;
16905   /// Reduction modifier.
16906   unsigned RedModifier = 0;
16907   ReductionData() = delete;
16908   /// Reserves required memory for the reduction data.
16909   ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) {
16910     Vars.reserve(Size);
16911     Privates.reserve(Size);
16912     LHSs.reserve(Size);
16913     RHSs.reserve(Size);
16914     ReductionOps.reserve(Size);
16915     if (RedModifier == OMPC_REDUCTION_inscan) {
16916       InscanCopyOps.reserve(Size);
16917       InscanCopyArrayTemps.reserve(Size);
16918       InscanCopyArrayElems.reserve(Size);
16919     }
16920     TaskgroupDescriptors.reserve(Size);
16921     ExprCaptures.reserve(Size);
16922     ExprPostUpdates.reserve(Size);
16923   }
16924   /// Stores reduction item and reduction operation only (required for dependent
16925   /// reduction item).
16926   void push(Expr *Item, Expr *ReductionOp) {
16927     Vars.emplace_back(Item);
16928     Privates.emplace_back(nullptr);
16929     LHSs.emplace_back(nullptr);
16930     RHSs.emplace_back(nullptr);
16931     ReductionOps.emplace_back(ReductionOp);
16932     TaskgroupDescriptors.emplace_back(nullptr);
16933     if (RedModifier == OMPC_REDUCTION_inscan) {
16934       InscanCopyOps.push_back(nullptr);
16935       InscanCopyArrayTemps.push_back(nullptr);
16936       InscanCopyArrayElems.push_back(nullptr);
16937     }
16938   }
16939   /// Stores reduction data.
16940   void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
16941             Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp,
16942             Expr *CopyArrayElem) {
16943     Vars.emplace_back(Item);
16944     Privates.emplace_back(Private);
16945     LHSs.emplace_back(LHS);
16946     RHSs.emplace_back(RHS);
16947     ReductionOps.emplace_back(ReductionOp);
16948     TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
16949     if (RedModifier == OMPC_REDUCTION_inscan) {
16950       InscanCopyOps.push_back(CopyOp);
16951       InscanCopyArrayTemps.push_back(CopyArrayTemp);
16952       InscanCopyArrayElems.push_back(CopyArrayElem);
16953     } else {
16954       assert(CopyOp == nullptr && CopyArrayTemp == nullptr &&
16955              CopyArrayElem == nullptr &&
16956              "Copy operation must be used for inscan reductions only.");
16957     }
16958   }
16959 };
16960 } // namespace
16961 
16962 static bool checkOMPArraySectionConstantForReduction(
16963     ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
16964     SmallVectorImpl<llvm::APSInt> &ArraySizes) {
16965   const Expr *Length = OASE->getLength();
16966   if (Length == nullptr) {
16967     // For array sections of the form [1:] or [:], we would need to analyze
16968     // the lower bound...
16969     if (OASE->getColonLocFirst().isValid())
16970       return false;
16971 
16972     // This is an array subscript which has implicit length 1!
16973     SingleElement = true;
16974     ArraySizes.push_back(llvm::APSInt::get(1));
16975   } else {
16976     Expr::EvalResult Result;
16977     if (!Length->EvaluateAsInt(Result, Context))
16978       return false;
16979 
16980     llvm::APSInt ConstantLengthValue = Result.Val.getInt();
16981     SingleElement = (ConstantLengthValue.getSExtValue() == 1);
16982     ArraySizes.push_back(ConstantLengthValue);
16983   }
16984 
16985   // Get the base of this array section and walk up from there.
16986   const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
16987 
16988   // We require length = 1 for all array sections except the right-most to
16989   // guarantee that the memory region is contiguous and has no holes in it.
16990   while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
16991     Length = TempOASE->getLength();
16992     if (Length == nullptr) {
16993       // For array sections of the form [1:] or [:], we would need to analyze
16994       // the lower bound...
16995       if (OASE->getColonLocFirst().isValid())
16996         return false;
16997 
16998       // This is an array subscript which has implicit length 1!
16999       ArraySizes.push_back(llvm::APSInt::get(1));
17000     } else {
17001       Expr::EvalResult Result;
17002       if (!Length->EvaluateAsInt(Result, Context))
17003         return false;
17004 
17005       llvm::APSInt ConstantLengthValue = Result.Val.getInt();
17006       if (ConstantLengthValue.getSExtValue() != 1)
17007         return false;
17008 
17009       ArraySizes.push_back(ConstantLengthValue);
17010     }
17011     Base = TempOASE->getBase()->IgnoreParenImpCasts();
17012   }
17013 
17014   // If we have a single element, we don't need to add the implicit lengths.
17015   if (!SingleElement) {
17016     while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
17017       // Has implicit length 1!
17018       ArraySizes.push_back(llvm::APSInt::get(1));
17019       Base = TempASE->getBase()->IgnoreParenImpCasts();
17020     }
17021   }
17022 
17023   // This array section can be privatized as a single value or as a constant
17024   // sized array.
17025   return true;
17026 }
17027 
17028 static BinaryOperatorKind
17029 getRelatedCompoundReductionOp(BinaryOperatorKind BOK) {
17030   if (BOK == BO_Add)
17031     return BO_AddAssign;
17032   if (BOK == BO_Mul)
17033     return BO_MulAssign;
17034   if (BOK == BO_And)
17035     return BO_AndAssign;
17036   if (BOK == BO_Or)
17037     return BO_OrAssign;
17038   if (BOK == BO_Xor)
17039     return BO_XorAssign;
17040   return BOK;
17041 }
17042 
17043 static bool actOnOMPReductionKindClause(
17044     Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
17045     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
17046     SourceLocation ColonLoc, SourceLocation EndLoc,
17047     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
17048     ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
17049   DeclarationName DN = ReductionId.getName();
17050   OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
17051   BinaryOperatorKind BOK = BO_Comma;
17052 
17053   ASTContext &Context = S.Context;
17054   // OpenMP [2.14.3.6, reduction clause]
17055   // C
17056   // reduction-identifier is either an identifier or one of the following
17057   // operators: +, -, *,  &, |, ^, && and ||
17058   // C++
17059   // reduction-identifier is either an id-expression or one of the following
17060   // operators: +, -, *, &, |, ^, && and ||
17061   switch (OOK) {
17062   case OO_Plus:
17063   case OO_Minus:
17064     BOK = BO_Add;
17065     break;
17066   case OO_Star:
17067     BOK = BO_Mul;
17068     break;
17069   case OO_Amp:
17070     BOK = BO_And;
17071     break;
17072   case OO_Pipe:
17073     BOK = BO_Or;
17074     break;
17075   case OO_Caret:
17076     BOK = BO_Xor;
17077     break;
17078   case OO_AmpAmp:
17079     BOK = BO_LAnd;
17080     break;
17081   case OO_PipePipe:
17082     BOK = BO_LOr;
17083     break;
17084   case OO_New:
17085   case OO_Delete:
17086   case OO_Array_New:
17087   case OO_Array_Delete:
17088   case OO_Slash:
17089   case OO_Percent:
17090   case OO_Tilde:
17091   case OO_Exclaim:
17092   case OO_Equal:
17093   case OO_Less:
17094   case OO_Greater:
17095   case OO_LessEqual:
17096   case OO_GreaterEqual:
17097   case OO_PlusEqual:
17098   case OO_MinusEqual:
17099   case OO_StarEqual:
17100   case OO_SlashEqual:
17101   case OO_PercentEqual:
17102   case OO_CaretEqual:
17103   case OO_AmpEqual:
17104   case OO_PipeEqual:
17105   case OO_LessLess:
17106   case OO_GreaterGreater:
17107   case OO_LessLessEqual:
17108   case OO_GreaterGreaterEqual:
17109   case OO_EqualEqual:
17110   case OO_ExclaimEqual:
17111   case OO_Spaceship:
17112   case OO_PlusPlus:
17113   case OO_MinusMinus:
17114   case OO_Comma:
17115   case OO_ArrowStar:
17116   case OO_Arrow:
17117   case OO_Call:
17118   case OO_Subscript:
17119   case OO_Conditional:
17120   case OO_Coawait:
17121   case NUM_OVERLOADED_OPERATORS:
17122     llvm_unreachable("Unexpected reduction identifier");
17123   case OO_None:
17124     if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
17125       if (II->isStr("max"))
17126         BOK = BO_GT;
17127       else if (II->isStr("min"))
17128         BOK = BO_LT;
17129     }
17130     break;
17131   }
17132   SourceRange ReductionIdRange;
17133   if (ReductionIdScopeSpec.isValid())
17134     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
17135   else
17136     ReductionIdRange.setBegin(ReductionId.getBeginLoc());
17137   ReductionIdRange.setEnd(ReductionId.getEndLoc());
17138 
17139   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
17140   bool FirstIter = true;
17141   for (Expr *RefExpr : VarList) {
17142     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
17143     // OpenMP [2.1, C/C++]
17144     //  A list item is a variable or array section, subject to the restrictions
17145     //  specified in Section 2.4 on page 42 and in each of the sections
17146     // describing clauses and directives for which a list appears.
17147     // OpenMP  [2.14.3.3, Restrictions, p.1]
17148     //  A variable that is part of another variable (as an array or
17149     //  structure element) cannot appear in a private clause.
17150     if (!FirstIter && IR != ER)
17151       ++IR;
17152     FirstIter = false;
17153     SourceLocation ELoc;
17154     SourceRange ERange;
17155     Expr *SimpleRefExpr = RefExpr;
17156     auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
17157                               /*AllowArraySection=*/true);
17158     if (Res.second) {
17159       // Try to find 'declare reduction' corresponding construct before using
17160       // builtin/overloaded operators.
17161       QualType Type = Context.DependentTy;
17162       CXXCastPath BasePath;
17163       ExprResult DeclareReductionRef = buildDeclareReductionRef(
17164           S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
17165           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
17166       Expr *ReductionOp = nullptr;
17167       if (S.CurContext->isDependentContext() &&
17168           (DeclareReductionRef.isUnset() ||
17169            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
17170         ReductionOp = DeclareReductionRef.get();
17171       // It will be analyzed later.
17172       RD.push(RefExpr, ReductionOp);
17173     }
17174     ValueDecl *D = Res.first;
17175     if (!D)
17176       continue;
17177 
17178     Expr *TaskgroupDescriptor = nullptr;
17179     QualType Type;
17180     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
17181     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
17182     if (ASE) {
17183       Type = ASE->getType().getNonReferenceType();
17184     } else if (OASE) {
17185       QualType BaseType =
17186           OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
17187       if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
17188         Type = ATy->getElementType();
17189       else
17190         Type = BaseType->getPointeeType();
17191       Type = Type.getNonReferenceType();
17192     } else {
17193       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
17194     }
17195     auto *VD = dyn_cast<VarDecl>(D);
17196 
17197     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
17198     //  A variable that appears in a private clause must not have an incomplete
17199     //  type or a reference type.
17200     if (S.RequireCompleteType(ELoc, D->getType(),
17201                               diag::err_omp_reduction_incomplete_type))
17202       continue;
17203     // OpenMP [2.14.3.6, reduction clause, Restrictions]
17204     // A list item that appears in a reduction clause must not be
17205     // const-qualified.
17206     if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
17207                                   /*AcceptIfMutable*/ false, ASE || OASE))
17208       continue;
17209 
17210     OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
17211     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
17212     //  If a list-item is a reference type then it must bind to the same object
17213     //  for all threads of the team.
17214     if (!ASE && !OASE) {
17215       if (VD) {
17216         VarDecl *VDDef = VD->getDefinition();
17217         if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
17218           DSARefChecker Check(Stack);
17219           if (Check.Visit(VDDef->getInit())) {
17220             S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
17221                 << getOpenMPClauseName(ClauseKind) << ERange;
17222             S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
17223             continue;
17224           }
17225         }
17226       }
17227 
17228       // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
17229       // in a Construct]
17230       //  Variables with the predetermined data-sharing attributes may not be
17231       //  listed in data-sharing attributes clauses, except for the cases
17232       //  listed below. For these exceptions only, listing a predetermined
17233       //  variable in a data-sharing attribute clause is allowed and overrides
17234       //  the variable's predetermined data-sharing attributes.
17235       // OpenMP [2.14.3.6, Restrictions, p.3]
17236       //  Any number of reduction clauses can be specified on the directive,
17237       //  but a list item can appear only once in the reduction clauses for that
17238       //  directive.
17239       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
17240       if (DVar.CKind == OMPC_reduction) {
17241         S.Diag(ELoc, diag::err_omp_once_referenced)
17242             << getOpenMPClauseName(ClauseKind);
17243         if (DVar.RefExpr)
17244           S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
17245         continue;
17246       }
17247       if (DVar.CKind != OMPC_unknown) {
17248         S.Diag(ELoc, diag::err_omp_wrong_dsa)
17249             << getOpenMPClauseName(DVar.CKind)
17250             << getOpenMPClauseName(OMPC_reduction);
17251         reportOriginalDsa(S, Stack, D, DVar);
17252         continue;
17253       }
17254 
17255       // OpenMP [2.14.3.6, Restrictions, p.1]
17256       //  A list item that appears in a reduction clause of a worksharing
17257       //  construct must be shared in the parallel regions to which any of the
17258       //  worksharing regions arising from the worksharing construct bind.
17259       if (isOpenMPWorksharingDirective(CurrDir) &&
17260           !isOpenMPParallelDirective(CurrDir) &&
17261           !isOpenMPTeamsDirective(CurrDir)) {
17262         DVar = Stack->getImplicitDSA(D, true);
17263         if (DVar.CKind != OMPC_shared) {
17264           S.Diag(ELoc, diag::err_omp_required_access)
17265               << getOpenMPClauseName(OMPC_reduction)
17266               << getOpenMPClauseName(OMPC_shared);
17267           reportOriginalDsa(S, Stack, D, DVar);
17268           continue;
17269         }
17270       }
17271     } else {
17272       // Threadprivates cannot be shared between threads, so dignose if the base
17273       // is a threadprivate variable.
17274       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
17275       if (DVar.CKind == OMPC_threadprivate) {
17276         S.Diag(ELoc, diag::err_omp_wrong_dsa)
17277             << getOpenMPClauseName(DVar.CKind)
17278             << getOpenMPClauseName(OMPC_reduction);
17279         reportOriginalDsa(S, Stack, D, DVar);
17280         continue;
17281       }
17282     }
17283 
17284     // Try to find 'declare reduction' corresponding construct before using
17285     // builtin/overloaded operators.
17286     CXXCastPath BasePath;
17287     ExprResult DeclareReductionRef = buildDeclareReductionRef(
17288         S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
17289         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
17290     if (DeclareReductionRef.isInvalid())
17291       continue;
17292     if (S.CurContext->isDependentContext() &&
17293         (DeclareReductionRef.isUnset() ||
17294          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
17295       RD.push(RefExpr, DeclareReductionRef.get());
17296       continue;
17297     }
17298     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
17299       // Not allowed reduction identifier is found.
17300       S.Diag(ReductionId.getBeginLoc(),
17301              diag::err_omp_unknown_reduction_identifier)
17302           << Type << ReductionIdRange;
17303       continue;
17304     }
17305 
17306     // OpenMP [2.14.3.6, reduction clause, Restrictions]
17307     // The type of a list item that appears in a reduction clause must be valid
17308     // for the reduction-identifier. For a max or min reduction in C, the type
17309     // of the list item must be an allowed arithmetic data type: char, int,
17310     // float, double, or _Bool, possibly modified with long, short, signed, or
17311     // unsigned. For a max or min reduction in C++, the type of the list item
17312     // must be an allowed arithmetic data type: char, wchar_t, int, float,
17313     // double, or bool, possibly modified with long, short, signed, or unsigned.
17314     if (DeclareReductionRef.isUnset()) {
17315       if ((BOK == BO_GT || BOK == BO_LT) &&
17316           !(Type->isScalarType() ||
17317             (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
17318         S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
17319             << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
17320         if (!ASE && !OASE) {
17321           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
17322                                    VarDecl::DeclarationOnly;
17323           S.Diag(D->getLocation(),
17324                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
17325               << D;
17326         }
17327         continue;
17328       }
17329       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
17330           !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
17331         S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
17332             << getOpenMPClauseName(ClauseKind);
17333         if (!ASE && !OASE) {
17334           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
17335                                    VarDecl::DeclarationOnly;
17336           S.Diag(D->getLocation(),
17337                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
17338               << D;
17339         }
17340         continue;
17341       }
17342     }
17343 
17344     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
17345     VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
17346                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
17347     VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
17348                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
17349     QualType PrivateTy = Type;
17350 
17351     // Try if we can determine constant lengths for all array sections and avoid
17352     // the VLA.
17353     bool ConstantLengthOASE = false;
17354     if (OASE) {
17355       bool SingleElement;
17356       llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
17357       ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
17358           Context, OASE, SingleElement, ArraySizes);
17359 
17360       // If we don't have a single element, we must emit a constant array type.
17361       if (ConstantLengthOASE && !SingleElement) {
17362         for (llvm::APSInt &Size : ArraySizes)
17363           PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr,
17364                                                    ArrayType::Normal,
17365                                                    /*IndexTypeQuals=*/0);
17366       }
17367     }
17368 
17369     if ((OASE && !ConstantLengthOASE) ||
17370         (!OASE && !ASE &&
17371          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
17372       if (!Context.getTargetInfo().isVLASupported()) {
17373         if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) {
17374           S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
17375           S.Diag(ELoc, diag::note_vla_unsupported);
17376           continue;
17377         } else {
17378           S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
17379           S.targetDiag(ELoc, diag::note_vla_unsupported);
17380         }
17381       }
17382       // For arrays/array sections only:
17383       // Create pseudo array type for private copy. The size for this array will
17384       // be generated during codegen.
17385       // For array subscripts or single variables Private Ty is the same as Type
17386       // (type of the variable or single array element).
17387       PrivateTy = Context.getVariableArrayType(
17388           Type,
17389           new (Context)
17390               OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue),
17391           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
17392     } else if (!ASE && !OASE &&
17393                Context.getAsArrayType(D->getType().getNonReferenceType())) {
17394       PrivateTy = D->getType().getNonReferenceType();
17395     }
17396     // Private copy.
17397     VarDecl *PrivateVD =
17398         buildVarDecl(S, ELoc, PrivateTy, D->getName(),
17399                      D->hasAttrs() ? &D->getAttrs() : nullptr,
17400                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
17401     // Add initializer for private variable.
17402     Expr *Init = nullptr;
17403     DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
17404     DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
17405     if (DeclareReductionRef.isUsable()) {
17406       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
17407       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
17408       if (DRD->getInitializer()) {
17409         Init = DRDRef;
17410         RHSVD->setInit(DRDRef);
17411         RHSVD->setInitStyle(VarDecl::CallInit);
17412       }
17413     } else {
17414       switch (BOK) {
17415       case BO_Add:
17416       case BO_Xor:
17417       case BO_Or:
17418       case BO_LOr:
17419         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
17420         if (Type->isScalarType() || Type->isAnyComplexType())
17421           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
17422         break;
17423       case BO_Mul:
17424       case BO_LAnd:
17425         if (Type->isScalarType() || Type->isAnyComplexType()) {
17426           // '*' and '&&' reduction ops - initializer is '1'.
17427           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
17428         }
17429         break;
17430       case BO_And: {
17431         // '&' reduction op - initializer is '~0'.
17432         QualType OrigType = Type;
17433         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
17434           Type = ComplexTy->getElementType();
17435         if (Type->isRealFloatingType()) {
17436           llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue(
17437               Context.getFloatTypeSemantics(Type));
17438           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
17439                                          Type, ELoc);
17440         } else if (Type->isScalarType()) {
17441           uint64_t Size = Context.getTypeSize(Type);
17442           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
17443           llvm::APInt InitValue = llvm::APInt::getAllOnes(Size);
17444           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
17445         }
17446         if (Init && OrigType->isAnyComplexType()) {
17447           // Init = 0xFFFF + 0xFFFFi;
17448           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
17449           Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
17450         }
17451         Type = OrigType;
17452         break;
17453       }
17454       case BO_LT:
17455       case BO_GT: {
17456         // 'min' reduction op - initializer is 'Largest representable number in
17457         // the reduction list item type'.
17458         // 'max' reduction op - initializer is 'Least representable number in
17459         // the reduction list item type'.
17460         if (Type->isIntegerType() || Type->isPointerType()) {
17461           bool IsSigned = Type->hasSignedIntegerRepresentation();
17462           uint64_t Size = Context.getTypeSize(Type);
17463           QualType IntTy =
17464               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
17465           llvm::APInt InitValue =
17466               (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
17467                                         : llvm::APInt::getMinValue(Size)
17468               : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
17469                              : llvm::APInt::getMaxValue(Size);
17470           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
17471           if (Type->isPointerType()) {
17472             // Cast to pointer type.
17473             ExprResult CastExpr = S.BuildCStyleCastExpr(
17474                 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
17475             if (CastExpr.isInvalid())
17476               continue;
17477             Init = CastExpr.get();
17478           }
17479         } else if (Type->isRealFloatingType()) {
17480           llvm::APFloat InitValue = llvm::APFloat::getLargest(
17481               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
17482           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
17483                                          Type, ELoc);
17484         }
17485         break;
17486       }
17487       case BO_PtrMemD:
17488       case BO_PtrMemI:
17489       case BO_MulAssign:
17490       case BO_Div:
17491       case BO_Rem:
17492       case BO_Sub:
17493       case BO_Shl:
17494       case BO_Shr:
17495       case BO_LE:
17496       case BO_GE:
17497       case BO_EQ:
17498       case BO_NE:
17499       case BO_Cmp:
17500       case BO_AndAssign:
17501       case BO_XorAssign:
17502       case BO_OrAssign:
17503       case BO_Assign:
17504       case BO_AddAssign:
17505       case BO_SubAssign:
17506       case BO_DivAssign:
17507       case BO_RemAssign:
17508       case BO_ShlAssign:
17509       case BO_ShrAssign:
17510       case BO_Comma:
17511         llvm_unreachable("Unexpected reduction operation");
17512       }
17513     }
17514     if (Init && DeclareReductionRef.isUnset()) {
17515       S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
17516       // Store initializer for single element in private copy. Will be used
17517       // during codegen.
17518       PrivateVD->setInit(RHSVD->getInit());
17519       PrivateVD->setInitStyle(RHSVD->getInitStyle());
17520     } else if (!Init) {
17521       S.ActOnUninitializedDecl(RHSVD);
17522       // Store initializer for single element in private copy. Will be used
17523       // during codegen.
17524       PrivateVD->setInit(RHSVD->getInit());
17525       PrivateVD->setInitStyle(RHSVD->getInitStyle());
17526     }
17527     if (RHSVD->isInvalidDecl())
17528       continue;
17529     if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
17530       S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
17531           << Type << ReductionIdRange;
17532       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
17533                                VarDecl::DeclarationOnly;
17534       S.Diag(D->getLocation(),
17535              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
17536           << D;
17537       continue;
17538     }
17539     DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
17540     ExprResult ReductionOp;
17541     if (DeclareReductionRef.isUsable()) {
17542       QualType RedTy = DeclareReductionRef.get()->getType();
17543       QualType PtrRedTy = Context.getPointerType(RedTy);
17544       ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
17545       ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
17546       if (!BasePath.empty()) {
17547         LHS = S.DefaultLvalueConversion(LHS.get());
17548         RHS = S.DefaultLvalueConversion(RHS.get());
17549         LHS = ImplicitCastExpr::Create(
17550             Context, PtrRedTy, CK_UncheckedDerivedToBase, LHS.get(), &BasePath,
17551             LHS.get()->getValueKind(), FPOptionsOverride());
17552         RHS = ImplicitCastExpr::Create(
17553             Context, PtrRedTy, CK_UncheckedDerivedToBase, RHS.get(), &BasePath,
17554             RHS.get()->getValueKind(), FPOptionsOverride());
17555       }
17556       FunctionProtoType::ExtProtoInfo EPI;
17557       QualType Params[] = {PtrRedTy, PtrRedTy};
17558       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
17559       auto *OVE = new (Context) OpaqueValueExpr(
17560           ELoc, Context.getPointerType(FnTy), VK_PRValue, OK_Ordinary,
17561           S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
17562       Expr *Args[] = {LHS.get(), RHS.get()};
17563       ReductionOp =
17564           CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_PRValue, ELoc,
17565                            S.CurFPFeatureOverrides());
17566     } else {
17567       BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK);
17568       if (Type->isRecordType() && CombBOK != BOK) {
17569         Sema::TentativeAnalysisScope Trap(S);
17570         ReductionOp =
17571             S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
17572                          CombBOK, LHSDRE, RHSDRE);
17573       }
17574       if (!ReductionOp.isUsable()) {
17575         ReductionOp =
17576             S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), BOK,
17577                          LHSDRE, RHSDRE);
17578         if (ReductionOp.isUsable()) {
17579           if (BOK != BO_LT && BOK != BO_GT) {
17580             ReductionOp =
17581                 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
17582                              BO_Assign, LHSDRE, ReductionOp.get());
17583           } else {
17584             auto *ConditionalOp = new (Context)
17585                 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc,
17586                                     RHSDRE, Type, VK_LValue, OK_Ordinary);
17587             ReductionOp =
17588                 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
17589                              BO_Assign, LHSDRE, ConditionalOp);
17590           }
17591         }
17592       }
17593       if (ReductionOp.isUsable())
17594         ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
17595                                             /*DiscardedValue*/ false);
17596       if (!ReductionOp.isUsable())
17597         continue;
17598     }
17599 
17600     // Add copy operations for inscan reductions.
17601     // LHS = RHS;
17602     ExprResult CopyOpRes, TempArrayRes, TempArrayElem;
17603     if (ClauseKind == OMPC_reduction &&
17604         RD.RedModifier == OMPC_REDUCTION_inscan) {
17605       ExprResult RHS = S.DefaultLvalueConversion(RHSDRE);
17606       CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE,
17607                                RHS.get());
17608       if (!CopyOpRes.isUsable())
17609         continue;
17610       CopyOpRes =
17611           S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true);
17612       if (!CopyOpRes.isUsable())
17613         continue;
17614       // For simd directive and simd-based directives in simd mode no need to
17615       // construct temp array, need just a single temp element.
17616       if (Stack->getCurrentDirective() == OMPD_simd ||
17617           (S.getLangOpts().OpenMPSimd &&
17618            isOpenMPSimdDirective(Stack->getCurrentDirective()))) {
17619         VarDecl *TempArrayVD =
17620             buildVarDecl(S, ELoc, PrivateTy, D->getName(),
17621                          D->hasAttrs() ? &D->getAttrs() : nullptr);
17622         // Add a constructor to the temp decl.
17623         S.ActOnUninitializedDecl(TempArrayVD);
17624         TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc);
17625       } else {
17626         // Build temp array for prefix sum.
17627         auto *Dim = new (S.Context)
17628             OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
17629         QualType ArrayTy =
17630             S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal,
17631                                            /*IndexTypeQuals=*/0, {ELoc, ELoc});
17632         VarDecl *TempArrayVD =
17633             buildVarDecl(S, ELoc, ArrayTy, D->getName(),
17634                          D->hasAttrs() ? &D->getAttrs() : nullptr);
17635         // Add a constructor to the temp decl.
17636         S.ActOnUninitializedDecl(TempArrayVD);
17637         TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc);
17638         TempArrayElem =
17639             S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get());
17640         auto *Idx = new (S.Context)
17641             OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
17642         TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(),
17643                                                           ELoc, Idx, ELoc);
17644       }
17645     }
17646 
17647     // OpenMP [2.15.4.6, Restrictions, p.2]
17648     // A list item that appears in an in_reduction clause of a task construct
17649     // must appear in a task_reduction clause of a construct associated with a
17650     // taskgroup region that includes the participating task in its taskgroup
17651     // set. The construct associated with the innermost region that meets this
17652     // condition must specify the same reduction-identifier as the in_reduction
17653     // clause.
17654     if (ClauseKind == OMPC_in_reduction) {
17655       SourceRange ParentSR;
17656       BinaryOperatorKind ParentBOK;
17657       const Expr *ParentReductionOp = nullptr;
17658       Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr;
17659       DSAStackTy::DSAVarData ParentBOKDSA =
17660           Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
17661                                                   ParentBOKTD);
17662       DSAStackTy::DSAVarData ParentReductionOpDSA =
17663           Stack->getTopMostTaskgroupReductionData(
17664               D, ParentSR, ParentReductionOp, ParentReductionOpTD);
17665       bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
17666       bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
17667       if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
17668           (DeclareReductionRef.isUsable() && IsParentBOK) ||
17669           (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) {
17670         bool EmitError = true;
17671         if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
17672           llvm::FoldingSetNodeID RedId, ParentRedId;
17673           ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
17674           DeclareReductionRef.get()->Profile(RedId, Context,
17675                                              /*Canonical=*/true);
17676           EmitError = RedId != ParentRedId;
17677         }
17678         if (EmitError) {
17679           S.Diag(ReductionId.getBeginLoc(),
17680                  diag::err_omp_reduction_identifier_mismatch)
17681               << ReductionIdRange << RefExpr->getSourceRange();
17682           S.Diag(ParentSR.getBegin(),
17683                  diag::note_omp_previous_reduction_identifier)
17684               << ParentSR
17685               << (IsParentBOK ? ParentBOKDSA.RefExpr
17686                               : ParentReductionOpDSA.RefExpr)
17687                      ->getSourceRange();
17688           continue;
17689         }
17690       }
17691       TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
17692     }
17693 
17694     DeclRefExpr *Ref = nullptr;
17695     Expr *VarsExpr = RefExpr->IgnoreParens();
17696     if (!VD && !S.CurContext->isDependentContext()) {
17697       if (ASE || OASE) {
17698         TransformExprToCaptures RebuildToCapture(S, D);
17699         VarsExpr =
17700             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
17701         Ref = RebuildToCapture.getCapturedExpr();
17702       } else {
17703         VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
17704       }
17705       if (!S.isOpenMPCapturedDecl(D)) {
17706         RD.ExprCaptures.emplace_back(Ref->getDecl());
17707         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
17708           ExprResult RefRes = S.DefaultLvalueConversion(Ref);
17709           if (!RefRes.isUsable())
17710             continue;
17711           ExprResult PostUpdateRes =
17712               S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
17713                            RefRes.get());
17714           if (!PostUpdateRes.isUsable())
17715             continue;
17716           if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
17717               Stack->getCurrentDirective() == OMPD_taskgroup) {
17718             S.Diag(RefExpr->getExprLoc(),
17719                    diag::err_omp_reduction_non_addressable_expression)
17720                 << RefExpr->getSourceRange();
17721             continue;
17722           }
17723           RD.ExprPostUpdates.emplace_back(
17724               S.IgnoredValueConversions(PostUpdateRes.get()).get());
17725         }
17726       }
17727     }
17728     // All reduction items are still marked as reduction (to do not increase
17729     // code base size).
17730     unsigned Modifier = RD.RedModifier;
17731     // Consider task_reductions as reductions with task modifier. Required for
17732     // correct analysis of in_reduction clauses.
17733     if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction)
17734       Modifier = OMPC_REDUCTION_task;
17735     Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier,
17736                   ASE || OASE);
17737     if (Modifier == OMPC_REDUCTION_task &&
17738         (CurrDir == OMPD_taskgroup ||
17739          ((isOpenMPParallelDirective(CurrDir) ||
17740            isOpenMPWorksharingDirective(CurrDir)) &&
17741           !isOpenMPSimdDirective(CurrDir)))) {
17742       if (DeclareReductionRef.isUsable())
17743         Stack->addTaskgroupReductionData(D, ReductionIdRange,
17744                                          DeclareReductionRef.get());
17745       else
17746         Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
17747     }
17748     RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
17749             TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(),
17750             TempArrayElem.get());
17751   }
17752   return RD.Vars.empty();
17753 }
17754 
17755 OMPClause *Sema::ActOnOpenMPReductionClause(
17756     ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier,
17757     SourceLocation StartLoc, SourceLocation LParenLoc,
17758     SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc,
17759     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
17760     ArrayRef<Expr *> UnresolvedReductions) {
17761   if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) {
17762     Diag(LParenLoc, diag::err_omp_unexpected_clause_value)
17763         << getListOfPossibleValues(OMPC_reduction, /*First=*/0,
17764                                    /*Last=*/OMPC_REDUCTION_unknown)
17765         << getOpenMPClauseName(OMPC_reduction);
17766     return nullptr;
17767   }
17768   // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions
17769   // A reduction clause with the inscan reduction-modifier may only appear on a
17770   // worksharing-loop construct, a worksharing-loop SIMD construct, a simd
17771   // construct, a parallel worksharing-loop construct or a parallel
17772   // worksharing-loop SIMD construct.
17773   if (Modifier == OMPC_REDUCTION_inscan &&
17774       (DSAStack->getCurrentDirective() != OMPD_for &&
17775        DSAStack->getCurrentDirective() != OMPD_for_simd &&
17776        DSAStack->getCurrentDirective() != OMPD_simd &&
17777        DSAStack->getCurrentDirective() != OMPD_parallel_for &&
17778        DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) {
17779     Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction);
17780     return nullptr;
17781   }
17782 
17783   ReductionData RD(VarList.size(), Modifier);
17784   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
17785                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
17786                                   ReductionIdScopeSpec, ReductionId,
17787                                   UnresolvedReductions, RD))
17788     return nullptr;
17789 
17790   return OMPReductionClause::Create(
17791       Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier,
17792       RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
17793       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps,
17794       RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems,
17795       buildPreInits(Context, RD.ExprCaptures),
17796       buildPostUpdate(*this, RD.ExprPostUpdates));
17797 }
17798 
17799 OMPClause *Sema::ActOnOpenMPTaskReductionClause(
17800     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
17801     SourceLocation ColonLoc, SourceLocation EndLoc,
17802     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
17803     ArrayRef<Expr *> UnresolvedReductions) {
17804   ReductionData RD(VarList.size());
17805   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
17806                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
17807                                   ReductionIdScopeSpec, ReductionId,
17808                                   UnresolvedReductions, RD))
17809     return nullptr;
17810 
17811   return OMPTaskReductionClause::Create(
17812       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
17813       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
17814       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
17815       buildPreInits(Context, RD.ExprCaptures),
17816       buildPostUpdate(*this, RD.ExprPostUpdates));
17817 }
17818 
17819 OMPClause *Sema::ActOnOpenMPInReductionClause(
17820     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
17821     SourceLocation ColonLoc, SourceLocation EndLoc,
17822     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
17823     ArrayRef<Expr *> UnresolvedReductions) {
17824   ReductionData RD(VarList.size());
17825   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
17826                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
17827                                   ReductionIdScopeSpec, ReductionId,
17828                                   UnresolvedReductions, RD))
17829     return nullptr;
17830 
17831   return OMPInReductionClause::Create(
17832       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
17833       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
17834       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
17835       buildPreInits(Context, RD.ExprCaptures),
17836       buildPostUpdate(*this, RD.ExprPostUpdates));
17837 }
17838 
17839 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
17840                                      SourceLocation LinLoc) {
17841   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
17842       LinKind == OMPC_LINEAR_unknown) {
17843     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
17844     return true;
17845   }
17846   return false;
17847 }
17848 
17849 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
17850                                  OpenMPLinearClauseKind LinKind, QualType Type,
17851                                  bool IsDeclareSimd) {
17852   const auto *VD = dyn_cast_or_null<VarDecl>(D);
17853   // A variable must not have an incomplete type or a reference type.
17854   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
17855     return true;
17856   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
17857       !Type->isReferenceType()) {
17858     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
17859         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
17860     return true;
17861   }
17862   Type = Type.getNonReferenceType();
17863 
17864   // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
17865   // A variable that is privatized must not have a const-qualified type
17866   // unless it is of class type with a mutable member. This restriction does
17867   // not apply to the firstprivate clause, nor to the linear clause on
17868   // declarative directives (like declare simd).
17869   if (!IsDeclareSimd &&
17870       rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
17871     return true;
17872 
17873   // A list item must be of integral or pointer type.
17874   Type = Type.getUnqualifiedType().getCanonicalType();
17875   const auto *Ty = Type.getTypePtrOrNull();
17876   if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() &&
17877               !Ty->isIntegralType(Context) && !Ty->isPointerType())) {
17878     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
17879     if (D) {
17880       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
17881                                VarDecl::DeclarationOnly;
17882       Diag(D->getLocation(),
17883            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
17884           << D;
17885     }
17886     return true;
17887   }
17888   return false;
17889 }
17890 
17891 OMPClause *Sema::ActOnOpenMPLinearClause(
17892     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
17893     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
17894     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
17895   SmallVector<Expr *, 8> Vars;
17896   SmallVector<Expr *, 8> Privates;
17897   SmallVector<Expr *, 8> Inits;
17898   SmallVector<Decl *, 4> ExprCaptures;
17899   SmallVector<Expr *, 4> ExprPostUpdates;
17900   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
17901     LinKind = OMPC_LINEAR_val;
17902   for (Expr *RefExpr : VarList) {
17903     assert(RefExpr && "NULL expr in OpenMP linear clause.");
17904     SourceLocation ELoc;
17905     SourceRange ERange;
17906     Expr *SimpleRefExpr = RefExpr;
17907     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
17908     if (Res.second) {
17909       // It will be analyzed later.
17910       Vars.push_back(RefExpr);
17911       Privates.push_back(nullptr);
17912       Inits.push_back(nullptr);
17913     }
17914     ValueDecl *D = Res.first;
17915     if (!D)
17916       continue;
17917 
17918     QualType Type = D->getType();
17919     auto *VD = dyn_cast<VarDecl>(D);
17920 
17921     // OpenMP [2.14.3.7, linear clause]
17922     //  A list-item cannot appear in more than one linear clause.
17923     //  A list-item that appears in a linear clause cannot appear in any
17924     //  other data-sharing attribute clause.
17925     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
17926     if (DVar.RefExpr) {
17927       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
17928                                           << getOpenMPClauseName(OMPC_linear);
17929       reportOriginalDsa(*this, DSAStack, D, DVar);
17930       continue;
17931     }
17932 
17933     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
17934       continue;
17935     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
17936 
17937     // Build private copy of original var.
17938     VarDecl *Private =
17939         buildVarDecl(*this, ELoc, Type, D->getName(),
17940                      D->hasAttrs() ? &D->getAttrs() : nullptr,
17941                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
17942     DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
17943     // Build var to save initial value.
17944     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
17945     Expr *InitExpr;
17946     DeclRefExpr *Ref = nullptr;
17947     if (!VD && !CurContext->isDependentContext()) {
17948       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
17949       if (!isOpenMPCapturedDecl(D)) {
17950         ExprCaptures.push_back(Ref->getDecl());
17951         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
17952           ExprResult RefRes = DefaultLvalueConversion(Ref);
17953           if (!RefRes.isUsable())
17954             continue;
17955           ExprResult PostUpdateRes =
17956               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
17957                          SimpleRefExpr, RefRes.get());
17958           if (!PostUpdateRes.isUsable())
17959             continue;
17960           ExprPostUpdates.push_back(
17961               IgnoredValueConversions(PostUpdateRes.get()).get());
17962         }
17963       }
17964     }
17965     if (LinKind == OMPC_LINEAR_uval)
17966       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
17967     else
17968       InitExpr = VD ? SimpleRefExpr : Ref;
17969     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
17970                          /*DirectInit=*/false);
17971     DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
17972 
17973     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
17974     Vars.push_back((VD || CurContext->isDependentContext())
17975                        ? RefExpr->IgnoreParens()
17976                        : Ref);
17977     Privates.push_back(PrivateRef);
17978     Inits.push_back(InitRef);
17979   }
17980 
17981   if (Vars.empty())
17982     return nullptr;
17983 
17984   Expr *StepExpr = Step;
17985   Expr *CalcStepExpr = nullptr;
17986   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
17987       !Step->isInstantiationDependent() &&
17988       !Step->containsUnexpandedParameterPack()) {
17989     SourceLocation StepLoc = Step->getBeginLoc();
17990     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
17991     if (Val.isInvalid())
17992       return nullptr;
17993     StepExpr = Val.get();
17994 
17995     // Build var to save the step value.
17996     VarDecl *SaveVar =
17997         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
17998     ExprResult SaveRef =
17999         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
18000     ExprResult CalcStep =
18001         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
18002     CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
18003 
18004     // Warn about zero linear step (it would be probably better specified as
18005     // making corresponding variables 'const').
18006     if (Optional<llvm::APSInt> Result =
18007             StepExpr->getIntegerConstantExpr(Context)) {
18008       if (!Result->isNegative() && !Result->isStrictlyPositive())
18009         Diag(StepLoc, diag::warn_omp_linear_step_zero)
18010             << Vars[0] << (Vars.size() > 1);
18011     } else if (CalcStep.isUsable()) {
18012       // Calculate the step beforehand instead of doing this on each iteration.
18013       // (This is not used if the number of iterations may be kfold-ed).
18014       CalcStepExpr = CalcStep.get();
18015     }
18016   }
18017 
18018   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
18019                                  ColonLoc, EndLoc, Vars, Privates, Inits,
18020                                  StepExpr, CalcStepExpr,
18021                                  buildPreInits(Context, ExprCaptures),
18022                                  buildPostUpdate(*this, ExprPostUpdates));
18023 }
18024 
18025 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
18026                                      Expr *NumIterations, Sema &SemaRef,
18027                                      Scope *S, DSAStackTy *Stack) {
18028   // Walk the vars and build update/final expressions for the CodeGen.
18029   SmallVector<Expr *, 8> Updates;
18030   SmallVector<Expr *, 8> Finals;
18031   SmallVector<Expr *, 8> UsedExprs;
18032   Expr *Step = Clause.getStep();
18033   Expr *CalcStep = Clause.getCalcStep();
18034   // OpenMP [2.14.3.7, linear clause]
18035   // If linear-step is not specified it is assumed to be 1.
18036   if (!Step)
18037     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
18038   else if (CalcStep)
18039     Step = cast<BinaryOperator>(CalcStep)->getLHS();
18040   bool HasErrors = false;
18041   auto CurInit = Clause.inits().begin();
18042   auto CurPrivate = Clause.privates().begin();
18043   OpenMPLinearClauseKind LinKind = Clause.getModifier();
18044   for (Expr *RefExpr : Clause.varlists()) {
18045     SourceLocation ELoc;
18046     SourceRange ERange;
18047     Expr *SimpleRefExpr = RefExpr;
18048     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
18049     ValueDecl *D = Res.first;
18050     if (Res.second || !D) {
18051       Updates.push_back(nullptr);
18052       Finals.push_back(nullptr);
18053       HasErrors = true;
18054       continue;
18055     }
18056     auto &&Info = Stack->isLoopControlVariable(D);
18057     // OpenMP [2.15.11, distribute simd Construct]
18058     // A list item may not appear in a linear clause, unless it is the loop
18059     // iteration variable.
18060     if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
18061         isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
18062       SemaRef.Diag(ELoc,
18063                    diag::err_omp_linear_distribute_var_non_loop_iteration);
18064       Updates.push_back(nullptr);
18065       Finals.push_back(nullptr);
18066       HasErrors = true;
18067       continue;
18068     }
18069     Expr *InitExpr = *CurInit;
18070 
18071     // Build privatized reference to the current linear var.
18072     auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
18073     Expr *CapturedRef;
18074     if (LinKind == OMPC_LINEAR_uval)
18075       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
18076     else
18077       CapturedRef =
18078           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
18079                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
18080                            /*RefersToCapture=*/true);
18081 
18082     // Build update: Var = InitExpr + IV * Step
18083     ExprResult Update;
18084     if (!Info.first)
18085       Update = buildCounterUpdate(
18086           SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step,
18087           /*Subtract=*/false, /*IsNonRectangularLB=*/false);
18088     else
18089       Update = *CurPrivate;
18090     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
18091                                          /*DiscardedValue*/ false);
18092 
18093     // Build final: Var = PrivCopy;
18094     ExprResult Final;
18095     if (!Info.first)
18096       Final = SemaRef.BuildBinOp(
18097           S, RefExpr->getExprLoc(), BO_Assign, CapturedRef,
18098           SemaRef.DefaultLvalueConversion(*CurPrivate).get());
18099     else
18100       Final = *CurPrivate;
18101     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
18102                                         /*DiscardedValue*/ false);
18103 
18104     if (!Update.isUsable() || !Final.isUsable()) {
18105       Updates.push_back(nullptr);
18106       Finals.push_back(nullptr);
18107       UsedExprs.push_back(nullptr);
18108       HasErrors = true;
18109     } else {
18110       Updates.push_back(Update.get());
18111       Finals.push_back(Final.get());
18112       if (!Info.first)
18113         UsedExprs.push_back(SimpleRefExpr);
18114     }
18115     ++CurInit;
18116     ++CurPrivate;
18117   }
18118   if (Expr *S = Clause.getStep())
18119     UsedExprs.push_back(S);
18120   // Fill the remaining part with the nullptr.
18121   UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr);
18122   Clause.setUpdates(Updates);
18123   Clause.setFinals(Finals);
18124   Clause.setUsedExprs(UsedExprs);
18125   return HasErrors;
18126 }
18127 
18128 OMPClause *Sema::ActOnOpenMPAlignedClause(
18129     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
18130     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
18131   SmallVector<Expr *, 8> Vars;
18132   for (Expr *RefExpr : VarList) {
18133     assert(RefExpr && "NULL expr in OpenMP linear clause.");
18134     SourceLocation ELoc;
18135     SourceRange ERange;
18136     Expr *SimpleRefExpr = RefExpr;
18137     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
18138     if (Res.second) {
18139       // It will be analyzed later.
18140       Vars.push_back(RefExpr);
18141     }
18142     ValueDecl *D = Res.first;
18143     if (!D)
18144       continue;
18145 
18146     QualType QType = D->getType();
18147     auto *VD = dyn_cast<VarDecl>(D);
18148 
18149     // OpenMP  [2.8.1, simd construct, Restrictions]
18150     // The type of list items appearing in the aligned clause must be
18151     // array, pointer, reference to array, or reference to pointer.
18152     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
18153     const Type *Ty = QType.getTypePtrOrNull();
18154     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
18155       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
18156           << QType << getLangOpts().CPlusPlus << ERange;
18157       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
18158                                VarDecl::DeclarationOnly;
18159       Diag(D->getLocation(),
18160            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
18161           << D;
18162       continue;
18163     }
18164 
18165     // OpenMP  [2.8.1, simd construct, Restrictions]
18166     // A list-item cannot appear in more than one aligned clause.
18167     if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
18168       Diag(ELoc, diag::err_omp_used_in_clause_twice)
18169           << 0 << getOpenMPClauseName(OMPC_aligned) << ERange;
18170       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
18171           << getOpenMPClauseName(OMPC_aligned);
18172       continue;
18173     }
18174 
18175     DeclRefExpr *Ref = nullptr;
18176     if (!VD && isOpenMPCapturedDecl(D))
18177       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
18178     Vars.push_back(DefaultFunctionArrayConversion(
18179                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
18180                        .get());
18181   }
18182 
18183   // OpenMP [2.8.1, simd construct, Description]
18184   // The parameter of the aligned clause, alignment, must be a constant
18185   // positive integer expression.
18186   // If no optional parameter is specified, implementation-defined default
18187   // alignments for SIMD instructions on the target platforms are assumed.
18188   if (Alignment != nullptr) {
18189     ExprResult AlignResult =
18190         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
18191     if (AlignResult.isInvalid())
18192       return nullptr;
18193     Alignment = AlignResult.get();
18194   }
18195   if (Vars.empty())
18196     return nullptr;
18197 
18198   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
18199                                   EndLoc, Vars, Alignment);
18200 }
18201 
18202 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
18203                                          SourceLocation StartLoc,
18204                                          SourceLocation LParenLoc,
18205                                          SourceLocation EndLoc) {
18206   SmallVector<Expr *, 8> Vars;
18207   SmallVector<Expr *, 8> SrcExprs;
18208   SmallVector<Expr *, 8> DstExprs;
18209   SmallVector<Expr *, 8> AssignmentOps;
18210   for (Expr *RefExpr : VarList) {
18211     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
18212     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
18213       // It will be analyzed later.
18214       Vars.push_back(RefExpr);
18215       SrcExprs.push_back(nullptr);
18216       DstExprs.push_back(nullptr);
18217       AssignmentOps.push_back(nullptr);
18218       continue;
18219     }
18220 
18221     SourceLocation ELoc = RefExpr->getExprLoc();
18222     // OpenMP [2.1, C/C++]
18223     //  A list item is a variable name.
18224     // OpenMP  [2.14.4.1, Restrictions, p.1]
18225     //  A list item that appears in a copyin clause must be threadprivate.
18226     auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
18227     if (!DE || !isa<VarDecl>(DE->getDecl())) {
18228       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
18229           << 0 << RefExpr->getSourceRange();
18230       continue;
18231     }
18232 
18233     Decl *D = DE->getDecl();
18234     auto *VD = cast<VarDecl>(D);
18235 
18236     QualType Type = VD->getType();
18237     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
18238       // It will be analyzed later.
18239       Vars.push_back(DE);
18240       SrcExprs.push_back(nullptr);
18241       DstExprs.push_back(nullptr);
18242       AssignmentOps.push_back(nullptr);
18243       continue;
18244     }
18245 
18246     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
18247     //  A list item that appears in a copyin clause must be threadprivate.
18248     if (!DSAStack->isThreadPrivate(VD)) {
18249       Diag(ELoc, diag::err_omp_required_access)
18250           << getOpenMPClauseName(OMPC_copyin)
18251           << getOpenMPDirectiveName(OMPD_threadprivate);
18252       continue;
18253     }
18254 
18255     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
18256     //  A variable of class type (or array thereof) that appears in a
18257     //  copyin clause requires an accessible, unambiguous copy assignment
18258     //  operator for the class type.
18259     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
18260     VarDecl *SrcVD =
18261         buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
18262                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
18263     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
18264         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
18265     VarDecl *DstVD =
18266         buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
18267                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
18268     DeclRefExpr *PseudoDstExpr =
18269         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
18270     // For arrays generate assignment operation for single element and replace
18271     // it by the original array element in CodeGen.
18272     ExprResult AssignmentOp =
18273         BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
18274                    PseudoSrcExpr);
18275     if (AssignmentOp.isInvalid())
18276       continue;
18277     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
18278                                        /*DiscardedValue*/ false);
18279     if (AssignmentOp.isInvalid())
18280       continue;
18281 
18282     DSAStack->addDSA(VD, DE, OMPC_copyin);
18283     Vars.push_back(DE);
18284     SrcExprs.push_back(PseudoSrcExpr);
18285     DstExprs.push_back(PseudoDstExpr);
18286     AssignmentOps.push_back(AssignmentOp.get());
18287   }
18288 
18289   if (Vars.empty())
18290     return nullptr;
18291 
18292   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
18293                                  SrcExprs, DstExprs, AssignmentOps);
18294 }
18295 
18296 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
18297                                               SourceLocation StartLoc,
18298                                               SourceLocation LParenLoc,
18299                                               SourceLocation EndLoc) {
18300   SmallVector<Expr *, 8> Vars;
18301   SmallVector<Expr *, 8> SrcExprs;
18302   SmallVector<Expr *, 8> DstExprs;
18303   SmallVector<Expr *, 8> AssignmentOps;
18304   for (Expr *RefExpr : VarList) {
18305     assert(RefExpr && "NULL expr in OpenMP linear clause.");
18306     SourceLocation ELoc;
18307     SourceRange ERange;
18308     Expr *SimpleRefExpr = RefExpr;
18309     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
18310     if (Res.second) {
18311       // It will be analyzed later.
18312       Vars.push_back(RefExpr);
18313       SrcExprs.push_back(nullptr);
18314       DstExprs.push_back(nullptr);
18315       AssignmentOps.push_back(nullptr);
18316     }
18317     ValueDecl *D = Res.first;
18318     if (!D)
18319       continue;
18320 
18321     QualType Type = D->getType();
18322     auto *VD = dyn_cast<VarDecl>(D);
18323 
18324     // OpenMP [2.14.4.2, Restrictions, p.2]
18325     //  A list item that appears in a copyprivate clause may not appear in a
18326     //  private or firstprivate clause on the single construct.
18327     if (!VD || !DSAStack->isThreadPrivate(VD)) {
18328       DSAStackTy::DSAVarData DVar =
18329           DSAStack->getTopDSA(D, /*FromParent=*/false);
18330       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
18331           DVar.RefExpr) {
18332         Diag(ELoc, diag::err_omp_wrong_dsa)
18333             << getOpenMPClauseName(DVar.CKind)
18334             << getOpenMPClauseName(OMPC_copyprivate);
18335         reportOriginalDsa(*this, DSAStack, D, DVar);
18336         continue;
18337       }
18338 
18339       // OpenMP [2.11.4.2, Restrictions, p.1]
18340       //  All list items that appear in a copyprivate clause must be either
18341       //  threadprivate or private in the enclosing context.
18342       if (DVar.CKind == OMPC_unknown) {
18343         DVar = DSAStack->getImplicitDSA(D, false);
18344         if (DVar.CKind == OMPC_shared) {
18345           Diag(ELoc, diag::err_omp_required_access)
18346               << getOpenMPClauseName(OMPC_copyprivate)
18347               << "threadprivate or private in the enclosing context";
18348           reportOriginalDsa(*this, DSAStack, D, DVar);
18349           continue;
18350         }
18351       }
18352     }
18353 
18354     // Variably modified types are not supported.
18355     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
18356       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
18357           << getOpenMPClauseName(OMPC_copyprivate) << Type
18358           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
18359       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
18360                                VarDecl::DeclarationOnly;
18361       Diag(D->getLocation(),
18362            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
18363           << D;
18364       continue;
18365     }
18366 
18367     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
18368     //  A variable of class type (or array thereof) that appears in a
18369     //  copyin clause requires an accessible, unambiguous copy assignment
18370     //  operator for the class type.
18371     Type = Context.getBaseElementType(Type.getNonReferenceType())
18372                .getUnqualifiedType();
18373     VarDecl *SrcVD =
18374         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
18375                      D->hasAttrs() ? &D->getAttrs() : nullptr);
18376     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
18377     VarDecl *DstVD =
18378         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
18379                      D->hasAttrs() ? &D->getAttrs() : nullptr);
18380     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
18381     ExprResult AssignmentOp = BuildBinOp(
18382         DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
18383     if (AssignmentOp.isInvalid())
18384       continue;
18385     AssignmentOp =
18386         ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
18387     if (AssignmentOp.isInvalid())
18388       continue;
18389 
18390     // No need to mark vars as copyprivate, they are already threadprivate or
18391     // implicitly private.
18392     assert(VD || isOpenMPCapturedDecl(D));
18393     Vars.push_back(
18394         VD ? RefExpr->IgnoreParens()
18395            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
18396     SrcExprs.push_back(PseudoSrcExpr);
18397     DstExprs.push_back(PseudoDstExpr);
18398     AssignmentOps.push_back(AssignmentOp.get());
18399   }
18400 
18401   if (Vars.empty())
18402     return nullptr;
18403 
18404   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
18405                                       Vars, SrcExprs, DstExprs, AssignmentOps);
18406 }
18407 
18408 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
18409                                         SourceLocation StartLoc,
18410                                         SourceLocation LParenLoc,
18411                                         SourceLocation EndLoc) {
18412   if (VarList.empty())
18413     return nullptr;
18414 
18415   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
18416 }
18417 
18418 /// Tries to find omp_depend_t. type.
18419 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack,
18420                            bool Diagnose = true) {
18421   QualType OMPDependT = Stack->getOMPDependT();
18422   if (!OMPDependT.isNull())
18423     return true;
18424   IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t");
18425   ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope());
18426   if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
18427     if (Diagnose)
18428       S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t";
18429     return false;
18430   }
18431   Stack->setOMPDependT(PT.get());
18432   return true;
18433 }
18434 
18435 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc,
18436                                          SourceLocation LParenLoc,
18437                                          SourceLocation EndLoc) {
18438   if (!Depobj)
18439     return nullptr;
18440 
18441   bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack);
18442 
18443   // OpenMP 5.0, 2.17.10.1 depobj Construct
18444   // depobj is an lvalue expression of type omp_depend_t.
18445   if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() &&
18446       !Depobj->isInstantiationDependent() &&
18447       !Depobj->containsUnexpandedParameterPack() &&
18448       (OMPDependTFound &&
18449        !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(),
18450                                    /*CompareUnqualified=*/true))) {
18451     Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue)
18452         << 0 << Depobj->getType() << Depobj->getSourceRange();
18453   }
18454 
18455   if (!Depobj->isLValue()) {
18456     Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue)
18457         << 1 << Depobj->getSourceRange();
18458   }
18459 
18460   return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj);
18461 }
18462 
18463 OMPClause *
18464 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind,
18465                               SourceLocation DepLoc, SourceLocation ColonLoc,
18466                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
18467                               SourceLocation LParenLoc, SourceLocation EndLoc) {
18468   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
18469       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
18470     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
18471         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
18472     return nullptr;
18473   }
18474   if (DSAStack->getCurrentDirective() == OMPD_taskwait &&
18475       DepKind == OMPC_DEPEND_mutexinoutset) {
18476     Diag(DepLoc, diag::err_omp_taskwait_depend_mutexinoutset_not_allowed);
18477     return nullptr;
18478   }
18479   if ((DSAStack->getCurrentDirective() != OMPD_ordered ||
18480        DSAStack->getCurrentDirective() == OMPD_depobj) &&
18481       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
18482        DepKind == OMPC_DEPEND_sink ||
18483        ((LangOpts.OpenMP < 50 ||
18484          DSAStack->getCurrentDirective() == OMPD_depobj) &&
18485         DepKind == OMPC_DEPEND_depobj))) {
18486     SmallVector<unsigned, 3> Except;
18487     Except.push_back(OMPC_DEPEND_source);
18488     Except.push_back(OMPC_DEPEND_sink);
18489     if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj)
18490       Except.push_back(OMPC_DEPEND_depobj);
18491     std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier)
18492                                ? "depend modifier(iterator) or "
18493                                : "";
18494     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
18495         << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0,
18496                                               /*Last=*/OMPC_DEPEND_unknown,
18497                                               Except)
18498         << getOpenMPClauseName(OMPC_depend);
18499     return nullptr;
18500   }
18501   if (DepModifier &&
18502       (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) {
18503     Diag(DepModifier->getExprLoc(),
18504          diag::err_omp_depend_sink_source_with_modifier);
18505     return nullptr;
18506   }
18507   if (DepModifier &&
18508       !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator))
18509     Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator);
18510 
18511   SmallVector<Expr *, 8> Vars;
18512   DSAStackTy::OperatorOffsetTy OpsOffs;
18513   llvm::APSInt DepCounter(/*BitWidth=*/32);
18514   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
18515   if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
18516     if (const Expr *OrderedCountExpr =
18517             DSAStack->getParentOrderedRegionParam().first) {
18518       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
18519       TotalDepCount.setIsUnsigned(/*Val=*/true);
18520     }
18521   }
18522   for (Expr *RefExpr : VarList) {
18523     assert(RefExpr && "NULL expr in OpenMP shared clause.");
18524     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
18525       // It will be analyzed later.
18526       Vars.push_back(RefExpr);
18527       continue;
18528     }
18529 
18530     SourceLocation ELoc = RefExpr->getExprLoc();
18531     Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
18532     if (DepKind == OMPC_DEPEND_sink) {
18533       if (DSAStack->getParentOrderedRegionParam().first &&
18534           DepCounter >= TotalDepCount) {
18535         Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
18536         continue;
18537       }
18538       ++DepCounter;
18539       // OpenMP  [2.13.9, Summary]
18540       // depend(dependence-type : vec), where dependence-type is:
18541       // 'sink' and where vec is the iteration vector, which has the form:
18542       //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
18543       // where n is the value specified by the ordered clause in the loop
18544       // directive, xi denotes the loop iteration variable of the i-th nested
18545       // loop associated with the loop directive, and di is a constant
18546       // non-negative integer.
18547       if (CurContext->isDependentContext()) {
18548         // It will be analyzed later.
18549         Vars.push_back(RefExpr);
18550         continue;
18551       }
18552       SimpleExpr = SimpleExpr->IgnoreImplicit();
18553       OverloadedOperatorKind OOK = OO_None;
18554       SourceLocation OOLoc;
18555       Expr *LHS = SimpleExpr;
18556       Expr *RHS = nullptr;
18557       if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
18558         OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
18559         OOLoc = BO->getOperatorLoc();
18560         LHS = BO->getLHS()->IgnoreParenImpCasts();
18561         RHS = BO->getRHS()->IgnoreParenImpCasts();
18562       } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
18563         OOK = OCE->getOperator();
18564         OOLoc = OCE->getOperatorLoc();
18565         LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
18566         RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
18567       } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
18568         OOK = MCE->getMethodDecl()
18569                   ->getNameInfo()
18570                   .getName()
18571                   .getCXXOverloadedOperator();
18572         OOLoc = MCE->getCallee()->getExprLoc();
18573         LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
18574         RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
18575       }
18576       SourceLocation ELoc;
18577       SourceRange ERange;
18578       auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
18579       if (Res.second) {
18580         // It will be analyzed later.
18581         Vars.push_back(RefExpr);
18582       }
18583       ValueDecl *D = Res.first;
18584       if (!D)
18585         continue;
18586 
18587       if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
18588         Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
18589         continue;
18590       }
18591       if (RHS) {
18592         ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
18593             RHS, OMPC_depend, /*StrictlyPositive=*/false);
18594         if (RHSRes.isInvalid())
18595           continue;
18596       }
18597       if (!CurContext->isDependentContext() &&
18598           DSAStack->getParentOrderedRegionParam().first &&
18599           DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
18600         const ValueDecl *VD =
18601             DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
18602         if (VD)
18603           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
18604               << 1 << VD;
18605         else
18606           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
18607         continue;
18608       }
18609       OpsOffs.emplace_back(RHS, OOK);
18610     } else {
18611       bool OMPDependTFound = LangOpts.OpenMP >= 50;
18612       if (OMPDependTFound)
18613         OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack,
18614                                          DepKind == OMPC_DEPEND_depobj);
18615       if (DepKind == OMPC_DEPEND_depobj) {
18616         // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
18617         // List items used in depend clauses with the depobj dependence type
18618         // must be expressions of the omp_depend_t type.
18619         if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
18620             !RefExpr->isInstantiationDependent() &&
18621             !RefExpr->containsUnexpandedParameterPack() &&
18622             (OMPDependTFound &&
18623              !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(),
18624                                              RefExpr->getType()))) {
18625           Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue)
18626               << 0 << RefExpr->getType() << RefExpr->getSourceRange();
18627           continue;
18628         }
18629         if (!RefExpr->isLValue()) {
18630           Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue)
18631               << 1 << RefExpr->getType() << RefExpr->getSourceRange();
18632           continue;
18633         }
18634       } else {
18635         // OpenMP 5.0 [2.17.11, Restrictions]
18636         // List items used in depend clauses cannot be zero-length array
18637         // sections.
18638         QualType ExprTy = RefExpr->getType().getNonReferenceType();
18639         const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
18640         if (OASE) {
18641           QualType BaseType =
18642               OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
18643           if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
18644             ExprTy = ATy->getElementType();
18645           else
18646             ExprTy = BaseType->getPointeeType();
18647           ExprTy = ExprTy.getNonReferenceType();
18648           const Expr *Length = OASE->getLength();
18649           Expr::EvalResult Result;
18650           if (Length && !Length->isValueDependent() &&
18651               Length->EvaluateAsInt(Result, Context) &&
18652               Result.Val.getInt().isZero()) {
18653             Diag(ELoc,
18654                  diag::err_omp_depend_zero_length_array_section_not_allowed)
18655                 << SimpleExpr->getSourceRange();
18656             continue;
18657           }
18658         }
18659 
18660         // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
18661         // List items used in depend clauses with the in, out, inout or
18662         // mutexinoutset dependence types cannot be expressions of the
18663         // omp_depend_t type.
18664         if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
18665             !RefExpr->isInstantiationDependent() &&
18666             !RefExpr->containsUnexpandedParameterPack() &&
18667             (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
18668              (OMPDependTFound &&
18669               DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr()))) {
18670           Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
18671               << (LangOpts.OpenMP >= 50 ? 1 : 0)
18672               << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange();
18673           continue;
18674         }
18675 
18676         auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
18677         if (ASE && !ASE->getBase()->isTypeDependent() &&
18678             !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
18679             !ASE->getBase()->getType().getNonReferenceType()->isArrayType()) {
18680           Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
18681               << (LangOpts.OpenMP >= 50 ? 1 : 0)
18682               << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange();
18683           continue;
18684         }
18685 
18686         ExprResult Res;
18687         {
18688           Sema::TentativeAnalysisScope Trap(*this);
18689           Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf,
18690                                      RefExpr->IgnoreParenImpCasts());
18691         }
18692         if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) &&
18693             !isa<OMPArrayShapingExpr>(SimpleExpr)) {
18694           Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
18695               << (LangOpts.OpenMP >= 50 ? 1 : 0)
18696               << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange();
18697           continue;
18698         }
18699       }
18700     }
18701     Vars.push_back(RefExpr->IgnoreParenImpCasts());
18702   }
18703 
18704   if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
18705       TotalDepCount > VarList.size() &&
18706       DSAStack->getParentOrderedRegionParam().first &&
18707       DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
18708     Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
18709         << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
18710   }
18711   if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
18712       Vars.empty())
18713     return nullptr;
18714 
18715   auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
18716                                     DepModifier, DepKind, DepLoc, ColonLoc,
18717                                     Vars, TotalDepCount.getZExtValue());
18718   if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
18719       DSAStack->isParentOrderedRegion())
18720     DSAStack->addDoacrossDependClause(C, OpsOffs);
18721   return C;
18722 }
18723 
18724 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier,
18725                                          Expr *Device, SourceLocation StartLoc,
18726                                          SourceLocation LParenLoc,
18727                                          SourceLocation ModifierLoc,
18728                                          SourceLocation EndLoc) {
18729   assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) &&
18730          "Unexpected device modifier in OpenMP < 50.");
18731 
18732   bool ErrorFound = false;
18733   if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) {
18734     std::string Values =
18735         getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown);
18736     Diag(ModifierLoc, diag::err_omp_unexpected_clause_value)
18737         << Values << getOpenMPClauseName(OMPC_device);
18738     ErrorFound = true;
18739   }
18740 
18741   Expr *ValExpr = Device;
18742   Stmt *HelperValStmt = nullptr;
18743 
18744   // OpenMP [2.9.1, Restrictions]
18745   // The device expression must evaluate to a non-negative integer value.
18746   ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
18747                                           /*StrictlyPositive=*/false) ||
18748                ErrorFound;
18749   if (ErrorFound)
18750     return nullptr;
18751 
18752   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18753   OpenMPDirectiveKind CaptureRegion =
18754       getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP);
18755   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
18756     ValExpr = MakeFullExpr(ValExpr).get();
18757     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18758     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
18759     HelperValStmt = buildPreInits(Context, Captures);
18760   }
18761 
18762   return new (Context)
18763       OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
18764                       LParenLoc, ModifierLoc, EndLoc);
18765 }
18766 
18767 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
18768                               DSAStackTy *Stack, QualType QTy,
18769                               bool FullCheck = true) {
18770   if (SemaRef.RequireCompleteType(SL, QTy, diag::err_incomplete_type))
18771     return false;
18772   if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
18773       !QTy.isTriviallyCopyableType(SemaRef.Context))
18774     SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
18775   return true;
18776 }
18777 
18778 /// Return true if it can be proven that the provided array expression
18779 /// (array section or array subscript) does NOT specify the whole size of the
18780 /// array whose base type is \a BaseQTy.
18781 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
18782                                                         const Expr *E,
18783                                                         QualType BaseQTy) {
18784   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
18785 
18786   // If this is an array subscript, it refers to the whole size if the size of
18787   // the dimension is constant and equals 1. Also, an array section assumes the
18788   // format of an array subscript if no colon is used.
18789   if (isa<ArraySubscriptExpr>(E) ||
18790       (OASE && OASE->getColonLocFirst().isInvalid())) {
18791     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
18792       return ATy->getSize().getSExtValue() != 1;
18793     // Size can't be evaluated statically.
18794     return false;
18795   }
18796 
18797   assert(OASE && "Expecting array section if not an array subscript.");
18798   const Expr *LowerBound = OASE->getLowerBound();
18799   const Expr *Length = OASE->getLength();
18800 
18801   // If there is a lower bound that does not evaluates to zero, we are not
18802   // covering the whole dimension.
18803   if (LowerBound) {
18804     Expr::EvalResult Result;
18805     if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
18806       return false; // Can't get the integer value as a constant.
18807 
18808     llvm::APSInt ConstLowerBound = Result.Val.getInt();
18809     if (ConstLowerBound.getSExtValue())
18810       return true;
18811   }
18812 
18813   // If we don't have a length we covering the whole dimension.
18814   if (!Length)
18815     return false;
18816 
18817   // If the base is a pointer, we don't have a way to get the size of the
18818   // pointee.
18819   if (BaseQTy->isPointerType())
18820     return false;
18821 
18822   // We can only check if the length is the same as the size of the dimension
18823   // if we have a constant array.
18824   const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
18825   if (!CATy)
18826     return false;
18827 
18828   Expr::EvalResult Result;
18829   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
18830     return false; // Can't get the integer value as a constant.
18831 
18832   llvm::APSInt ConstLength = Result.Val.getInt();
18833   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
18834 }
18835 
18836 // Return true if it can be proven that the provided array expression (array
18837 // section or array subscript) does NOT specify a single element of the array
18838 // whose base type is \a BaseQTy.
18839 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
18840                                                         const Expr *E,
18841                                                         QualType BaseQTy) {
18842   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
18843 
18844   // An array subscript always refer to a single element. Also, an array section
18845   // assumes the format of an array subscript if no colon is used.
18846   if (isa<ArraySubscriptExpr>(E) ||
18847       (OASE && OASE->getColonLocFirst().isInvalid()))
18848     return false;
18849 
18850   assert(OASE && "Expecting array section if not an array subscript.");
18851   const Expr *Length = OASE->getLength();
18852 
18853   // If we don't have a length we have to check if the array has unitary size
18854   // for this dimension. Also, we should always expect a length if the base type
18855   // is pointer.
18856   if (!Length) {
18857     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
18858       return ATy->getSize().getSExtValue() != 1;
18859     // We cannot assume anything.
18860     return false;
18861   }
18862 
18863   // Check if the length evaluates to 1.
18864   Expr::EvalResult Result;
18865   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
18866     return false; // Can't get the integer value as a constant.
18867 
18868   llvm::APSInt ConstLength = Result.Val.getInt();
18869   return ConstLength.getSExtValue() != 1;
18870 }
18871 
18872 // The base of elements of list in a map clause have to be either:
18873 //  - a reference to variable or field.
18874 //  - a member expression.
18875 //  - an array expression.
18876 //
18877 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
18878 // reference to 'r'.
18879 //
18880 // If we have:
18881 //
18882 // struct SS {
18883 //   Bla S;
18884 //   foo() {
18885 //     #pragma omp target map (S.Arr[:12]);
18886 //   }
18887 // }
18888 //
18889 // We want to retrieve the member expression 'this->S';
18890 
18891 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2]
18892 //  If a list item is an array section, it must specify contiguous storage.
18893 //
18894 // For this restriction it is sufficient that we make sure only references
18895 // to variables or fields and array expressions, and that no array sections
18896 // exist except in the rightmost expression (unless they cover the whole
18897 // dimension of the array). E.g. these would be invalid:
18898 //
18899 //   r.ArrS[3:5].Arr[6:7]
18900 //
18901 //   r.ArrS[3:5].x
18902 //
18903 // but these would be valid:
18904 //   r.ArrS[3].Arr[6:7]
18905 //
18906 //   r.ArrS[3].x
18907 namespace {
18908 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> {
18909   Sema &SemaRef;
18910   OpenMPClauseKind CKind = OMPC_unknown;
18911   OpenMPDirectiveKind DKind = OMPD_unknown;
18912   OMPClauseMappableExprCommon::MappableExprComponentList &Components;
18913   bool IsNonContiguous = false;
18914   bool NoDiagnose = false;
18915   const Expr *RelevantExpr = nullptr;
18916   bool AllowUnitySizeArraySection = true;
18917   bool AllowWholeSizeArraySection = true;
18918   bool AllowAnotherPtr = true;
18919   SourceLocation ELoc;
18920   SourceRange ERange;
18921 
18922   void emitErrorMsg() {
18923     // If nothing else worked, this is not a valid map clause expression.
18924     if (SemaRef.getLangOpts().OpenMP < 50) {
18925       SemaRef.Diag(ELoc,
18926                    diag::err_omp_expected_named_var_member_or_array_expression)
18927           << ERange;
18928     } else {
18929       SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses)
18930           << getOpenMPClauseName(CKind) << ERange;
18931     }
18932   }
18933 
18934 public:
18935   bool VisitDeclRefExpr(DeclRefExpr *DRE) {
18936     if (!isa<VarDecl>(DRE->getDecl())) {
18937       emitErrorMsg();
18938       return false;
18939     }
18940     assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
18941     RelevantExpr = DRE;
18942     // Record the component.
18943     Components.emplace_back(DRE, DRE->getDecl(), IsNonContiguous);
18944     return true;
18945   }
18946 
18947   bool VisitMemberExpr(MemberExpr *ME) {
18948     Expr *E = ME;
18949     Expr *BaseE = ME->getBase()->IgnoreParenCasts();
18950 
18951     if (isa<CXXThisExpr>(BaseE)) {
18952       assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
18953       // We found a base expression: this->Val.
18954       RelevantExpr = ME;
18955     } else {
18956       E = BaseE;
18957     }
18958 
18959     if (!isa<FieldDecl>(ME->getMemberDecl())) {
18960       if (!NoDiagnose) {
18961         SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
18962             << ME->getSourceRange();
18963         return false;
18964       }
18965       if (RelevantExpr)
18966         return false;
18967       return Visit(E);
18968     }
18969 
18970     auto *FD = cast<FieldDecl>(ME->getMemberDecl());
18971 
18972     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
18973     //  A bit-field cannot appear in a map clause.
18974     //
18975     if (FD->isBitField()) {
18976       if (!NoDiagnose) {
18977         SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
18978             << ME->getSourceRange() << getOpenMPClauseName(CKind);
18979         return false;
18980       }
18981       if (RelevantExpr)
18982         return false;
18983       return Visit(E);
18984     }
18985 
18986     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
18987     //  If the type of a list item is a reference to a type T then the type
18988     //  will be considered to be T for all purposes of this clause.
18989     QualType CurType = BaseE->getType().getNonReferenceType();
18990 
18991     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
18992     //  A list item cannot be a variable that is a member of a structure with
18993     //  a union type.
18994     //
18995     if (CurType->isUnionType()) {
18996       if (!NoDiagnose) {
18997         SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
18998             << ME->getSourceRange();
18999         return false;
19000       }
19001       return RelevantExpr || Visit(E);
19002     }
19003 
19004     // If we got a member expression, we should not expect any array section
19005     // before that:
19006     //
19007     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
19008     //  If a list item is an element of a structure, only the rightmost symbol
19009     //  of the variable reference can be an array section.
19010     //
19011     AllowUnitySizeArraySection = false;
19012     AllowWholeSizeArraySection = false;
19013 
19014     // Record the component.
19015     Components.emplace_back(ME, FD, IsNonContiguous);
19016     return RelevantExpr || Visit(E);
19017   }
19018 
19019   bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) {
19020     Expr *E = AE->getBase()->IgnoreParenImpCasts();
19021 
19022     if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
19023       if (!NoDiagnose) {
19024         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
19025             << 0 << AE->getSourceRange();
19026         return false;
19027       }
19028       return RelevantExpr || Visit(E);
19029     }
19030 
19031     // If we got an array subscript that express the whole dimension we
19032     // can have any array expressions before. If it only expressing part of
19033     // the dimension, we can only have unitary-size array expressions.
19034     if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, E->getType()))
19035       AllowWholeSizeArraySection = false;
19036 
19037     if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) {
19038       Expr::EvalResult Result;
19039       if (!AE->getIdx()->isValueDependent() &&
19040           AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) &&
19041           !Result.Val.getInt().isZero()) {
19042         SemaRef.Diag(AE->getIdx()->getExprLoc(),
19043                      diag::err_omp_invalid_map_this_expr);
19044         SemaRef.Diag(AE->getIdx()->getExprLoc(),
19045                      diag::note_omp_invalid_subscript_on_this_ptr_map);
19046       }
19047       assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
19048       RelevantExpr = TE;
19049     }
19050 
19051     // Record the component - we don't have any declaration associated.
19052     Components.emplace_back(AE, nullptr, IsNonContiguous);
19053 
19054     return RelevantExpr || Visit(E);
19055   }
19056 
19057   bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) {
19058     // After OMP 5.0  Array section in reduction clause will be implicitly
19059     // mapped
19060     assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) &&
19061            "Array sections cannot be implicitly mapped.");
19062     Expr *E = OASE->getBase()->IgnoreParenImpCasts();
19063     QualType CurType =
19064         OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
19065 
19066     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
19067     //  If the type of a list item is a reference to a type T then the type
19068     //  will be considered to be T for all purposes of this clause.
19069     if (CurType->isReferenceType())
19070       CurType = CurType->getPointeeType();
19071 
19072     bool IsPointer = CurType->isAnyPointerType();
19073 
19074     if (!IsPointer && !CurType->isArrayType()) {
19075       SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
19076           << 0 << OASE->getSourceRange();
19077       return false;
19078     }
19079 
19080     bool NotWhole =
19081         checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType);
19082     bool NotUnity =
19083         checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType);
19084 
19085     if (AllowWholeSizeArraySection) {
19086       // Any array section is currently allowed. Allowing a whole size array
19087       // section implies allowing a unity array section as well.
19088       //
19089       // If this array section refers to the whole dimension we can still
19090       // accept other array sections before this one, except if the base is a
19091       // pointer. Otherwise, only unitary sections are accepted.
19092       if (NotWhole || IsPointer)
19093         AllowWholeSizeArraySection = false;
19094     } else if (DKind == OMPD_target_update &&
19095                SemaRef.getLangOpts().OpenMP >= 50) {
19096       if (IsPointer && !AllowAnotherPtr)
19097         SemaRef.Diag(ELoc, diag::err_omp_section_length_undefined)
19098             << /*array of unknown bound */ 1;
19099       else
19100         IsNonContiguous = true;
19101     } else if (AllowUnitySizeArraySection && NotUnity) {
19102       // A unity or whole array section is not allowed and that is not
19103       // compatible with the properties of the current array section.
19104       if (NoDiagnose)
19105         return false;
19106       SemaRef.Diag(ELoc,
19107                    diag::err_array_section_does_not_specify_contiguous_storage)
19108           << OASE->getSourceRange();
19109       return false;
19110     }
19111 
19112     if (IsPointer)
19113       AllowAnotherPtr = false;
19114 
19115     if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
19116       Expr::EvalResult ResultR;
19117       Expr::EvalResult ResultL;
19118       if (!OASE->getLength()->isValueDependent() &&
19119           OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) &&
19120           !ResultR.Val.getInt().isOne()) {
19121         SemaRef.Diag(OASE->getLength()->getExprLoc(),
19122                      diag::err_omp_invalid_map_this_expr);
19123         SemaRef.Diag(OASE->getLength()->getExprLoc(),
19124                      diag::note_omp_invalid_length_on_this_ptr_mapping);
19125       }
19126       if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() &&
19127           OASE->getLowerBound()->EvaluateAsInt(ResultL,
19128                                                SemaRef.getASTContext()) &&
19129           !ResultL.Val.getInt().isZero()) {
19130         SemaRef.Diag(OASE->getLowerBound()->getExprLoc(),
19131                      diag::err_omp_invalid_map_this_expr);
19132         SemaRef.Diag(OASE->getLowerBound()->getExprLoc(),
19133                      diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
19134       }
19135       assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
19136       RelevantExpr = TE;
19137     }
19138 
19139     // Record the component - we don't have any declaration associated.
19140     Components.emplace_back(OASE, nullptr, /*IsNonContiguous=*/false);
19141     return RelevantExpr || Visit(E);
19142   }
19143   bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) {
19144     Expr *Base = E->getBase();
19145 
19146     // Record the component - we don't have any declaration associated.
19147     Components.emplace_back(E, nullptr, IsNonContiguous);
19148 
19149     return Visit(Base->IgnoreParenImpCasts());
19150   }
19151 
19152   bool VisitUnaryOperator(UnaryOperator *UO) {
19153     if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() ||
19154         UO->getOpcode() != UO_Deref) {
19155       emitErrorMsg();
19156       return false;
19157     }
19158     if (!RelevantExpr) {
19159       // Record the component if haven't found base decl.
19160       Components.emplace_back(UO, nullptr, /*IsNonContiguous=*/false);
19161     }
19162     return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts());
19163   }
19164   bool VisitBinaryOperator(BinaryOperator *BO) {
19165     if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) {
19166       emitErrorMsg();
19167       return false;
19168     }
19169 
19170     // Pointer arithmetic is the only thing we expect to happen here so after we
19171     // make sure the binary operator is a pointer type, the we only thing need
19172     // to to is to visit the subtree that has the same type as root (so that we
19173     // know the other subtree is just an offset)
19174     Expr *LE = BO->getLHS()->IgnoreParenImpCasts();
19175     Expr *RE = BO->getRHS()->IgnoreParenImpCasts();
19176     Components.emplace_back(BO, nullptr, false);
19177     assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() ||
19178             RE->getType().getTypePtr() == BO->getType().getTypePtr()) &&
19179            "Either LHS or RHS have base decl inside");
19180     if (BO->getType().getTypePtr() == LE->getType().getTypePtr())
19181       return RelevantExpr || Visit(LE);
19182     return RelevantExpr || Visit(RE);
19183   }
19184   bool VisitCXXThisExpr(CXXThisExpr *CTE) {
19185     assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
19186     RelevantExpr = CTE;
19187     Components.emplace_back(CTE, nullptr, IsNonContiguous);
19188     return true;
19189   }
19190   bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) {
19191     assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
19192     Components.emplace_back(COCE, nullptr, IsNonContiguous);
19193     return true;
19194   }
19195   bool VisitOpaqueValueExpr(OpaqueValueExpr *E) {
19196     Expr *Source = E->getSourceExpr();
19197     if (!Source) {
19198       emitErrorMsg();
19199       return false;
19200     }
19201     return Visit(Source);
19202   }
19203   bool VisitStmt(Stmt *) {
19204     emitErrorMsg();
19205     return false;
19206   }
19207   const Expr *getFoundBase() const { return RelevantExpr; }
19208   explicit MapBaseChecker(
19209       Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind,
19210       OMPClauseMappableExprCommon::MappableExprComponentList &Components,
19211       bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange)
19212       : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components),
19213         NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {}
19214 };
19215 } // namespace
19216 
19217 /// Return the expression of the base of the mappable expression or null if it
19218 /// cannot be determined and do all the necessary checks to see if the
19219 /// expression is valid as a standalone mappable expression. In the process,
19220 /// record all the components of the expression.
19221 static const Expr *checkMapClauseExpressionBase(
19222     Sema &SemaRef, Expr *E,
19223     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
19224     OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) {
19225   SourceLocation ELoc = E->getExprLoc();
19226   SourceRange ERange = E->getSourceRange();
19227   MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc,
19228                          ERange);
19229   if (Checker.Visit(E->IgnoreParens())) {
19230     // Check if the highest dimension array section has length specified
19231     if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() &&
19232         (CKind == OMPC_to || CKind == OMPC_from)) {
19233       auto CI = CurComponents.rbegin();
19234       auto CE = CurComponents.rend();
19235       for (; CI != CE; ++CI) {
19236         const auto *OASE =
19237             dyn_cast<OMPArraySectionExpr>(CI->getAssociatedExpression());
19238         if (!OASE)
19239           continue;
19240         if (OASE && OASE->getLength())
19241           break;
19242         SemaRef.Diag(ELoc, diag::err_array_section_does_not_specify_length)
19243             << ERange;
19244       }
19245     }
19246     return Checker.getFoundBase();
19247   }
19248   return nullptr;
19249 }
19250 
19251 // Return true if expression E associated with value VD has conflicts with other
19252 // map information.
19253 static bool checkMapConflicts(
19254     Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
19255     bool CurrentRegionOnly,
19256     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
19257     OpenMPClauseKind CKind) {
19258   assert(VD && E);
19259   SourceLocation ELoc = E->getExprLoc();
19260   SourceRange ERange = E->getSourceRange();
19261 
19262   // In order to easily check the conflicts we need to match each component of
19263   // the expression under test with the components of the expressions that are
19264   // already in the stack.
19265 
19266   assert(!CurComponents.empty() && "Map clause expression with no components!");
19267   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
19268          "Map clause expression with unexpected base!");
19269 
19270   // Variables to help detecting enclosing problems in data environment nests.
19271   bool IsEnclosedByDataEnvironmentExpr = false;
19272   const Expr *EnclosingExpr = nullptr;
19273 
19274   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
19275       VD, CurrentRegionOnly,
19276       [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
19277        ERange, CKind, &EnclosingExpr,
19278        CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
19279                           StackComponents,
19280                       OpenMPClauseKind Kind) {
19281         if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50)
19282           return false;
19283         assert(!StackComponents.empty() &&
19284                "Map clause expression with no components!");
19285         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
19286                "Map clause expression with unexpected base!");
19287         (void)VD;
19288 
19289         // The whole expression in the stack.
19290         const Expr *RE = StackComponents.front().getAssociatedExpression();
19291 
19292         // Expressions must start from the same base. Here we detect at which
19293         // point both expressions diverge from each other and see if we can
19294         // detect if the memory referred to both expressions is contiguous and
19295         // do not overlap.
19296         auto CI = CurComponents.rbegin();
19297         auto CE = CurComponents.rend();
19298         auto SI = StackComponents.rbegin();
19299         auto SE = StackComponents.rend();
19300         for (; CI != CE && SI != SE; ++CI, ++SI) {
19301 
19302           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
19303           //  At most one list item can be an array item derived from a given
19304           //  variable in map clauses of the same construct.
19305           if (CurrentRegionOnly &&
19306               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
19307                isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) ||
19308                isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) &&
19309               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
19310                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) ||
19311                isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) {
19312             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
19313                          diag::err_omp_multiple_array_items_in_map_clause)
19314                 << CI->getAssociatedExpression()->getSourceRange();
19315             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
19316                          diag::note_used_here)
19317                 << SI->getAssociatedExpression()->getSourceRange();
19318             return true;
19319           }
19320 
19321           // Do both expressions have the same kind?
19322           if (CI->getAssociatedExpression()->getStmtClass() !=
19323               SI->getAssociatedExpression()->getStmtClass())
19324             break;
19325 
19326           // Are we dealing with different variables/fields?
19327           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
19328             break;
19329         }
19330         // Check if the extra components of the expressions in the enclosing
19331         // data environment are redundant for the current base declaration.
19332         // If they are, the maps completely overlap, which is legal.
19333         for (; SI != SE; ++SI) {
19334           QualType Type;
19335           if (const auto *ASE =
19336                   dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
19337             Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
19338           } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
19339                          SI->getAssociatedExpression())) {
19340             const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
19341             Type =
19342                 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
19343           } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>(
19344                          SI->getAssociatedExpression())) {
19345             Type = OASE->getBase()->getType()->getPointeeType();
19346           }
19347           if (Type.isNull() || Type->isAnyPointerType() ||
19348               checkArrayExpressionDoesNotReferToWholeSize(
19349                   SemaRef, SI->getAssociatedExpression(), Type))
19350             break;
19351         }
19352 
19353         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
19354         //  List items of map clauses in the same construct must not share
19355         //  original storage.
19356         //
19357         // If the expressions are exactly the same or one is a subset of the
19358         // other, it means they are sharing storage.
19359         if (CI == CE && SI == SE) {
19360           if (CurrentRegionOnly) {
19361             if (CKind == OMPC_map) {
19362               SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
19363             } else {
19364               assert(CKind == OMPC_to || CKind == OMPC_from);
19365               SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
19366                   << ERange;
19367             }
19368             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
19369                 << RE->getSourceRange();
19370             return true;
19371           }
19372           // If we find the same expression in the enclosing data environment,
19373           // that is legal.
19374           IsEnclosedByDataEnvironmentExpr = true;
19375           return false;
19376         }
19377 
19378         QualType DerivedType =
19379             std::prev(CI)->getAssociatedDeclaration()->getType();
19380         SourceLocation DerivedLoc =
19381             std::prev(CI)->getAssociatedExpression()->getExprLoc();
19382 
19383         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
19384         //  If the type of a list item is a reference to a type T then the type
19385         //  will be considered to be T for all purposes of this clause.
19386         DerivedType = DerivedType.getNonReferenceType();
19387 
19388         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
19389         //  A variable for which the type is pointer and an array section
19390         //  derived from that variable must not appear as list items of map
19391         //  clauses of the same construct.
19392         //
19393         // Also, cover one of the cases in:
19394         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
19395         //  If any part of the original storage of a list item has corresponding
19396         //  storage in the device data environment, all of the original storage
19397         //  must have corresponding storage in the device data environment.
19398         //
19399         if (DerivedType->isAnyPointerType()) {
19400           if (CI == CE || SI == SE) {
19401             SemaRef.Diag(
19402                 DerivedLoc,
19403                 diag::err_omp_pointer_mapped_along_with_derived_section)
19404                 << DerivedLoc;
19405             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
19406                 << RE->getSourceRange();
19407             return true;
19408           }
19409           if (CI->getAssociatedExpression()->getStmtClass() !=
19410                   SI->getAssociatedExpression()->getStmtClass() ||
19411               CI->getAssociatedDeclaration()->getCanonicalDecl() ==
19412                   SI->getAssociatedDeclaration()->getCanonicalDecl()) {
19413             assert(CI != CE && SI != SE);
19414             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
19415                 << DerivedLoc;
19416             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
19417                 << RE->getSourceRange();
19418             return true;
19419           }
19420         }
19421 
19422         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
19423         //  List items of map clauses in the same construct must not share
19424         //  original storage.
19425         //
19426         // An expression is a subset of the other.
19427         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
19428           if (CKind == OMPC_map) {
19429             if (CI != CE || SI != SE) {
19430               // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
19431               // a pointer.
19432               auto Begin =
19433                   CI != CE ? CurComponents.begin() : StackComponents.begin();
19434               auto End = CI != CE ? CurComponents.end() : StackComponents.end();
19435               auto It = Begin;
19436               while (It != End && !It->getAssociatedDeclaration())
19437                 std::advance(It, 1);
19438               assert(It != End &&
19439                      "Expected at least one component with the declaration.");
19440               if (It != Begin && It->getAssociatedDeclaration()
19441                                      ->getType()
19442                                      .getCanonicalType()
19443                                      ->isAnyPointerType()) {
19444                 IsEnclosedByDataEnvironmentExpr = false;
19445                 EnclosingExpr = nullptr;
19446                 return false;
19447               }
19448             }
19449             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
19450           } else {
19451             assert(CKind == OMPC_to || CKind == OMPC_from);
19452             SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
19453                 << ERange;
19454           }
19455           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
19456               << RE->getSourceRange();
19457           return true;
19458         }
19459 
19460         // The current expression uses the same base as other expression in the
19461         // data environment but does not contain it completely.
19462         if (!CurrentRegionOnly && SI != SE)
19463           EnclosingExpr = RE;
19464 
19465         // The current expression is a subset of the expression in the data
19466         // environment.
19467         IsEnclosedByDataEnvironmentExpr |=
19468             (!CurrentRegionOnly && CI != CE && SI == SE);
19469 
19470         return false;
19471       });
19472 
19473   if (CurrentRegionOnly)
19474     return FoundError;
19475 
19476   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
19477   //  If any part of the original storage of a list item has corresponding
19478   //  storage in the device data environment, all of the original storage must
19479   //  have corresponding storage in the device data environment.
19480   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
19481   //  If a list item is an element of a structure, and a different element of
19482   //  the structure has a corresponding list item in the device data environment
19483   //  prior to a task encountering the construct associated with the map clause,
19484   //  then the list item must also have a corresponding list item in the device
19485   //  data environment prior to the task encountering the construct.
19486   //
19487   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
19488     SemaRef.Diag(ELoc,
19489                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
19490         << ERange;
19491     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
19492         << EnclosingExpr->getSourceRange();
19493     return true;
19494   }
19495 
19496   return FoundError;
19497 }
19498 
19499 // Look up the user-defined mapper given the mapper name and mapped type, and
19500 // build a reference to it.
19501 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
19502                                             CXXScopeSpec &MapperIdScopeSpec,
19503                                             const DeclarationNameInfo &MapperId,
19504                                             QualType Type,
19505                                             Expr *UnresolvedMapper) {
19506   if (MapperIdScopeSpec.isInvalid())
19507     return ExprError();
19508   // Get the actual type for the array type.
19509   if (Type->isArrayType()) {
19510     assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type");
19511     Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType();
19512   }
19513   // Find all user-defined mappers with the given MapperId.
19514   SmallVector<UnresolvedSet<8>, 4> Lookups;
19515   LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
19516   Lookup.suppressDiagnostics();
19517   if (S) {
19518     while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
19519       NamedDecl *D = Lookup.getRepresentativeDecl();
19520       while (S && !S->isDeclScope(D))
19521         S = S->getParent();
19522       if (S)
19523         S = S->getParent();
19524       Lookups.emplace_back();
19525       Lookups.back().append(Lookup.begin(), Lookup.end());
19526       Lookup.clear();
19527     }
19528   } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
19529     // Extract the user-defined mappers with the given MapperId.
19530     Lookups.push_back(UnresolvedSet<8>());
19531     for (NamedDecl *D : ULE->decls()) {
19532       auto *DMD = cast<OMPDeclareMapperDecl>(D);
19533       assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
19534       Lookups.back().addDecl(DMD);
19535     }
19536   }
19537   // Defer the lookup for dependent types. The results will be passed through
19538   // UnresolvedMapper on instantiation.
19539   if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
19540       Type->isInstantiationDependentType() ||
19541       Type->containsUnexpandedParameterPack() ||
19542       filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
19543         return !D->isInvalidDecl() &&
19544                (D->getType()->isDependentType() ||
19545                 D->getType()->isInstantiationDependentType() ||
19546                 D->getType()->containsUnexpandedParameterPack());
19547       })) {
19548     UnresolvedSet<8> URS;
19549     for (const UnresolvedSet<8> &Set : Lookups) {
19550       if (Set.empty())
19551         continue;
19552       URS.append(Set.begin(), Set.end());
19553     }
19554     return UnresolvedLookupExpr::Create(
19555         SemaRef.Context, /*NamingClass=*/nullptr,
19556         MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
19557         /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
19558   }
19559   SourceLocation Loc = MapperId.getLoc();
19560   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
19561   //  The type must be of struct, union or class type in C and C++
19562   if (!Type->isStructureOrClassType() && !Type->isUnionType() &&
19563       (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) {
19564     SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type);
19565     return ExprError();
19566   }
19567   // Perform argument dependent lookup.
19568   if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
19569     argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
19570   // Return the first user-defined mapper with the desired type.
19571   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
19572           Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
19573             if (!D->isInvalidDecl() &&
19574                 SemaRef.Context.hasSameType(D->getType(), Type))
19575               return D;
19576             return nullptr;
19577           }))
19578     return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
19579   // Find the first user-defined mapper with a type derived from the desired
19580   // type.
19581   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
19582           Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
19583             if (!D->isInvalidDecl() &&
19584                 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
19585                 !Type.isMoreQualifiedThan(D->getType()))
19586               return D;
19587             return nullptr;
19588           })) {
19589     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
19590                        /*DetectVirtual=*/false);
19591     if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
19592       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
19593               VD->getType().getUnqualifiedType()))) {
19594         if (SemaRef.CheckBaseClassAccess(
19595                 Loc, VD->getType(), Type, Paths.front(),
19596                 /*DiagID=*/0) != Sema::AR_inaccessible) {
19597           return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
19598         }
19599       }
19600     }
19601   }
19602   // Report error if a mapper is specified, but cannot be found.
19603   if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
19604     SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
19605         << Type << MapperId.getName();
19606     return ExprError();
19607   }
19608   return ExprEmpty();
19609 }
19610 
19611 namespace {
19612 // Utility struct that gathers all the related lists associated with a mappable
19613 // expression.
19614 struct MappableVarListInfo {
19615   // The list of expressions.
19616   ArrayRef<Expr *> VarList;
19617   // The list of processed expressions.
19618   SmallVector<Expr *, 16> ProcessedVarList;
19619   // The mappble components for each expression.
19620   OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
19621   // The base declaration of the variable.
19622   SmallVector<ValueDecl *, 16> VarBaseDeclarations;
19623   // The reference to the user-defined mapper associated with every expression.
19624   SmallVector<Expr *, 16> UDMapperList;
19625 
19626   MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
19627     // We have a list of components and base declarations for each entry in the
19628     // variable list.
19629     VarComponents.reserve(VarList.size());
19630     VarBaseDeclarations.reserve(VarList.size());
19631   }
19632 };
19633 } // namespace
19634 
19635 // Check the validity of the provided variable list for the provided clause kind
19636 // \a CKind. In the check process the valid expressions, mappable expression
19637 // components, variables, and user-defined mappers are extracted and used to
19638 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
19639 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
19640 // and \a MapperId are expected to be valid if the clause kind is 'map'.
19641 static void checkMappableExpressionList(
19642     Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
19643     MappableVarListInfo &MVLI, SourceLocation StartLoc,
19644     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
19645     ArrayRef<Expr *> UnresolvedMappers,
19646     OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
19647     ArrayRef<OpenMPMapModifierKind> Modifiers = None,
19648     bool IsMapTypeImplicit = false, bool NoDiagnose = false) {
19649   // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
19650   assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
19651          "Unexpected clause kind with mappable expressions!");
19652 
19653   // If the identifier of user-defined mapper is not specified, it is "default".
19654   // We do not change the actual name in this clause to distinguish whether a
19655   // mapper is specified explicitly, i.e., it is not explicitly specified when
19656   // MapperId.getName() is empty.
19657   if (!MapperId.getName() || MapperId.getName().isEmpty()) {
19658     auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
19659     MapperId.setName(DeclNames.getIdentifier(
19660         &SemaRef.getASTContext().Idents.get("default")));
19661     MapperId.setLoc(StartLoc);
19662   }
19663 
19664   // Iterators to find the current unresolved mapper expression.
19665   auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
19666   bool UpdateUMIt = false;
19667   Expr *UnresolvedMapper = nullptr;
19668 
19669   bool HasHoldModifier =
19670       llvm::is_contained(Modifiers, OMPC_MAP_MODIFIER_ompx_hold);
19671 
19672   // Keep track of the mappable components and base declarations in this clause.
19673   // Each entry in the list is going to have a list of components associated. We
19674   // record each set of the components so that we can build the clause later on.
19675   // In the end we should have the same amount of declarations and component
19676   // lists.
19677 
19678   for (Expr *RE : MVLI.VarList) {
19679     assert(RE && "Null expr in omp to/from/map clause");
19680     SourceLocation ELoc = RE->getExprLoc();
19681 
19682     // Find the current unresolved mapper expression.
19683     if (UpdateUMIt && UMIt != UMEnd) {
19684       UMIt++;
19685       assert(
19686           UMIt != UMEnd &&
19687           "Expect the size of UnresolvedMappers to match with that of VarList");
19688     }
19689     UpdateUMIt = true;
19690     if (UMIt != UMEnd)
19691       UnresolvedMapper = *UMIt;
19692 
19693     const Expr *VE = RE->IgnoreParenLValueCasts();
19694 
19695     if (VE->isValueDependent() || VE->isTypeDependent() ||
19696         VE->isInstantiationDependent() ||
19697         VE->containsUnexpandedParameterPack()) {
19698       // Try to find the associated user-defined mapper.
19699       ExprResult ER = buildUserDefinedMapperRef(
19700           SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
19701           VE->getType().getCanonicalType(), UnresolvedMapper);
19702       if (ER.isInvalid())
19703         continue;
19704       MVLI.UDMapperList.push_back(ER.get());
19705       // We can only analyze this information once the missing information is
19706       // resolved.
19707       MVLI.ProcessedVarList.push_back(RE);
19708       continue;
19709     }
19710 
19711     Expr *SimpleExpr = RE->IgnoreParenCasts();
19712 
19713     if (!RE->isLValue()) {
19714       if (SemaRef.getLangOpts().OpenMP < 50) {
19715         SemaRef.Diag(
19716             ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
19717             << RE->getSourceRange();
19718       } else {
19719         SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses)
19720             << getOpenMPClauseName(CKind) << RE->getSourceRange();
19721       }
19722       continue;
19723     }
19724 
19725     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
19726     ValueDecl *CurDeclaration = nullptr;
19727 
19728     // Obtain the array or member expression bases if required. Also, fill the
19729     // components array with all the components identified in the process.
19730     const Expr *BE =
19731         checkMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind,
19732                                      DSAS->getCurrentDirective(), NoDiagnose);
19733     if (!BE)
19734       continue;
19735 
19736     assert(!CurComponents.empty() &&
19737            "Invalid mappable expression information.");
19738 
19739     if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
19740       // Add store "this" pointer to class in DSAStackTy for future checking
19741       DSAS->addMappedClassesQualTypes(TE->getType());
19742       // Try to find the associated user-defined mapper.
19743       ExprResult ER = buildUserDefinedMapperRef(
19744           SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
19745           VE->getType().getCanonicalType(), UnresolvedMapper);
19746       if (ER.isInvalid())
19747         continue;
19748       MVLI.UDMapperList.push_back(ER.get());
19749       // Skip restriction checking for variable or field declarations
19750       MVLI.ProcessedVarList.push_back(RE);
19751       MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
19752       MVLI.VarComponents.back().append(CurComponents.begin(),
19753                                        CurComponents.end());
19754       MVLI.VarBaseDeclarations.push_back(nullptr);
19755       continue;
19756     }
19757 
19758     // For the following checks, we rely on the base declaration which is
19759     // expected to be associated with the last component. The declaration is
19760     // expected to be a variable or a field (if 'this' is being mapped).
19761     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
19762     assert(CurDeclaration && "Null decl on map clause.");
19763     assert(
19764         CurDeclaration->isCanonicalDecl() &&
19765         "Expecting components to have associated only canonical declarations.");
19766 
19767     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
19768     const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
19769 
19770     assert((VD || FD) && "Only variables or fields are expected here!");
19771     (void)FD;
19772 
19773     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
19774     // threadprivate variables cannot appear in a map clause.
19775     // OpenMP 4.5 [2.10.5, target update Construct]
19776     // threadprivate variables cannot appear in a from clause.
19777     if (VD && DSAS->isThreadPrivate(VD)) {
19778       if (NoDiagnose)
19779         continue;
19780       DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
19781       SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
19782           << getOpenMPClauseName(CKind);
19783       reportOriginalDsa(SemaRef, DSAS, VD, DVar);
19784       continue;
19785     }
19786 
19787     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
19788     //  A list item cannot appear in both a map clause and a data-sharing
19789     //  attribute clause on the same construct.
19790 
19791     // Check conflicts with other map clause expressions. We check the conflicts
19792     // with the current construct separately from the enclosing data
19793     // environment, because the restrictions are different. We only have to
19794     // check conflicts across regions for the map clauses.
19795     if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
19796                           /*CurrentRegionOnly=*/true, CurComponents, CKind))
19797       break;
19798     if (CKind == OMPC_map &&
19799         (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) &&
19800         checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
19801                           /*CurrentRegionOnly=*/false, CurComponents, CKind))
19802       break;
19803 
19804     // OpenMP 4.5 [2.10.5, target update Construct]
19805     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
19806     //  If the type of a list item is a reference to a type T then the type will
19807     //  be considered to be T for all purposes of this clause.
19808     auto I = llvm::find_if(
19809         CurComponents,
19810         [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
19811           return MC.getAssociatedDeclaration();
19812         });
19813     assert(I != CurComponents.end() && "Null decl on map clause.");
19814     (void)I;
19815     QualType Type;
19816     auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens());
19817     auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens());
19818     auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens());
19819     if (ASE) {
19820       Type = ASE->getType().getNonReferenceType();
19821     } else if (OASE) {
19822       QualType BaseType =
19823           OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
19824       if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
19825         Type = ATy->getElementType();
19826       else
19827         Type = BaseType->getPointeeType();
19828       Type = Type.getNonReferenceType();
19829     } else if (OAShE) {
19830       Type = OAShE->getBase()->getType()->getPointeeType();
19831     } else {
19832       Type = VE->getType();
19833     }
19834 
19835     // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
19836     // A list item in a to or from clause must have a mappable type.
19837     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
19838     //  A list item must have a mappable type.
19839     if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
19840                            DSAS, Type, /*FullCheck=*/true))
19841       continue;
19842 
19843     if (CKind == OMPC_map) {
19844       // target enter data
19845       // OpenMP [2.10.2, Restrictions, p. 99]
19846       // A map-type must be specified in all map clauses and must be either
19847       // to or alloc.
19848       OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
19849       if (DKind == OMPD_target_enter_data &&
19850           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
19851         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
19852             << (IsMapTypeImplicit ? 1 : 0)
19853             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
19854             << getOpenMPDirectiveName(DKind);
19855         continue;
19856       }
19857 
19858       // target exit_data
19859       // OpenMP [2.10.3, Restrictions, p. 102]
19860       // A map-type must be specified in all map clauses and must be either
19861       // from, release, or delete.
19862       if (DKind == OMPD_target_exit_data &&
19863           !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
19864             MapType == OMPC_MAP_delete)) {
19865         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
19866             << (IsMapTypeImplicit ? 1 : 0)
19867             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
19868             << getOpenMPDirectiveName(DKind);
19869         continue;
19870       }
19871 
19872       // The 'ompx_hold' modifier is specifically intended to be used on a
19873       // 'target' or 'target data' directive to prevent data from being unmapped
19874       // during the associated statement.  It is not permitted on a 'target
19875       // enter data' or 'target exit data' directive, which have no associated
19876       // statement.
19877       if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) &&
19878           HasHoldModifier) {
19879         SemaRef.Diag(StartLoc,
19880                      diag::err_omp_invalid_map_type_modifier_for_directive)
19881             << getOpenMPSimpleClauseTypeName(OMPC_map,
19882                                              OMPC_MAP_MODIFIER_ompx_hold)
19883             << getOpenMPDirectiveName(DKind);
19884         continue;
19885       }
19886 
19887       // target, target data
19888       // OpenMP 5.0 [2.12.2, Restrictions, p. 163]
19889       // OpenMP 5.0 [2.12.5, Restrictions, p. 174]
19890       // A map-type in a map clause must be to, from, tofrom or alloc
19891       if ((DKind == OMPD_target_data ||
19892            isOpenMPTargetExecutionDirective(DKind)) &&
19893           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from ||
19894             MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) {
19895         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
19896             << (IsMapTypeImplicit ? 1 : 0)
19897             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
19898             << getOpenMPDirectiveName(DKind);
19899         continue;
19900       }
19901 
19902       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
19903       // A list item cannot appear in both a map clause and a data-sharing
19904       // attribute clause on the same construct
19905       //
19906       // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
19907       // A list item cannot appear in both a map clause and a data-sharing
19908       // attribute clause on the same construct unless the construct is a
19909       // combined construct.
19910       if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
19911                   isOpenMPTargetExecutionDirective(DKind)) ||
19912                  DKind == OMPD_target)) {
19913         DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
19914         if (isOpenMPPrivate(DVar.CKind)) {
19915           SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
19916               << getOpenMPClauseName(DVar.CKind)
19917               << getOpenMPClauseName(OMPC_map)
19918               << getOpenMPDirectiveName(DSAS->getCurrentDirective());
19919           reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
19920           continue;
19921         }
19922       }
19923     }
19924 
19925     // Try to find the associated user-defined mapper.
19926     ExprResult ER = buildUserDefinedMapperRef(
19927         SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
19928         Type.getCanonicalType(), UnresolvedMapper);
19929     if (ER.isInvalid())
19930       continue;
19931     MVLI.UDMapperList.push_back(ER.get());
19932 
19933     // Save the current expression.
19934     MVLI.ProcessedVarList.push_back(RE);
19935 
19936     // Store the components in the stack so that they can be used to check
19937     // against other clauses later on.
19938     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
19939                                           /*WhereFoundClauseKind=*/OMPC_map);
19940 
19941     // Save the components and declaration to create the clause. For purposes of
19942     // the clause creation, any component list that has has base 'this' uses
19943     // null as base declaration.
19944     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
19945     MVLI.VarComponents.back().append(CurComponents.begin(),
19946                                      CurComponents.end());
19947     MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
19948                                                            : CurDeclaration);
19949   }
19950 }
19951 
19952 OMPClause *Sema::ActOnOpenMPMapClause(
19953     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
19954     ArrayRef<SourceLocation> MapTypeModifiersLoc,
19955     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
19956     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
19957     SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
19958     const OMPVarListLocTy &Locs, bool NoDiagnose,
19959     ArrayRef<Expr *> UnresolvedMappers) {
19960   OpenMPMapModifierKind Modifiers[] = {
19961       OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
19962       OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
19963       OMPC_MAP_MODIFIER_unknown};
19964   SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];
19965 
19966   // Process map-type-modifiers, flag errors for duplicate modifiers.
19967   unsigned Count = 0;
19968   for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
19969     if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
19970         llvm::is_contained(Modifiers, MapTypeModifiers[I])) {
19971       Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
19972       continue;
19973     }
19974     assert(Count < NumberOfOMPMapClauseModifiers &&
19975            "Modifiers exceed the allowed number of map type modifiers");
19976     Modifiers[Count] = MapTypeModifiers[I];
19977     ModifiersLoc[Count] = MapTypeModifiersLoc[I];
19978     ++Count;
19979   }
19980 
19981   MappableVarListInfo MVLI(VarList);
19982   checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
19983                               MapperIdScopeSpec, MapperId, UnresolvedMappers,
19984                               MapType, Modifiers, IsMapTypeImplicit,
19985                               NoDiagnose);
19986 
19987   // We need to produce a map clause even if we don't have variables so that
19988   // other diagnostics related with non-existing map clauses are accurate.
19989   return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
19990                               MVLI.VarBaseDeclarations, MVLI.VarComponents,
19991                               MVLI.UDMapperList, Modifiers, ModifiersLoc,
19992                               MapperIdScopeSpec.getWithLocInContext(Context),
19993                               MapperId, MapType, IsMapTypeImplicit, MapLoc);
19994 }
19995 
19996 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
19997                                                TypeResult ParsedType) {
19998   assert(ParsedType.isUsable());
19999 
20000   QualType ReductionType = GetTypeFromParser(ParsedType.get());
20001   if (ReductionType.isNull())
20002     return QualType();
20003 
20004   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
20005   // A type name in a declare reduction directive cannot be a function type, an
20006   // array type, a reference type, or a type qualified with const, volatile or
20007   // restrict.
20008   if (ReductionType.hasQualifiers()) {
20009     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
20010     return QualType();
20011   }
20012 
20013   if (ReductionType->isFunctionType()) {
20014     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
20015     return QualType();
20016   }
20017   if (ReductionType->isReferenceType()) {
20018     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
20019     return QualType();
20020   }
20021   if (ReductionType->isArrayType()) {
20022     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
20023     return QualType();
20024   }
20025   return ReductionType;
20026 }
20027 
20028 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
20029     Scope *S, DeclContext *DC, DeclarationName Name,
20030     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
20031     AccessSpecifier AS, Decl *PrevDeclInScope) {
20032   SmallVector<Decl *, 8> Decls;
20033   Decls.reserve(ReductionTypes.size());
20034 
20035   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
20036                       forRedeclarationInCurContext());
20037   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
20038   // A reduction-identifier may not be re-declared in the current scope for the
20039   // same type or for a type that is compatible according to the base language
20040   // rules.
20041   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
20042   OMPDeclareReductionDecl *PrevDRD = nullptr;
20043   bool InCompoundScope = true;
20044   if (S != nullptr) {
20045     // Find previous declaration with the same name not referenced in other
20046     // declarations.
20047     FunctionScopeInfo *ParentFn = getEnclosingFunction();
20048     InCompoundScope =
20049         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
20050     LookupName(Lookup, S);
20051     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
20052                          /*AllowInlineNamespace=*/false);
20053     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
20054     LookupResult::Filter Filter = Lookup.makeFilter();
20055     while (Filter.hasNext()) {
20056       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
20057       if (InCompoundScope) {
20058         auto I = UsedAsPrevious.find(PrevDecl);
20059         if (I == UsedAsPrevious.end())
20060           UsedAsPrevious[PrevDecl] = false;
20061         if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
20062           UsedAsPrevious[D] = true;
20063       }
20064       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
20065           PrevDecl->getLocation();
20066     }
20067     Filter.done();
20068     if (InCompoundScope) {
20069       for (const auto &PrevData : UsedAsPrevious) {
20070         if (!PrevData.second) {
20071           PrevDRD = PrevData.first;
20072           break;
20073         }
20074       }
20075     }
20076   } else if (PrevDeclInScope != nullptr) {
20077     auto *PrevDRDInScope = PrevDRD =
20078         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
20079     do {
20080       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
20081           PrevDRDInScope->getLocation();
20082       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
20083     } while (PrevDRDInScope != nullptr);
20084   }
20085   for (const auto &TyData : ReductionTypes) {
20086     const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
20087     bool Invalid = false;
20088     if (I != PreviousRedeclTypes.end()) {
20089       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
20090           << TyData.first;
20091       Diag(I->second, diag::note_previous_definition);
20092       Invalid = true;
20093     }
20094     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
20095     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
20096                                                 Name, TyData.first, PrevDRD);
20097     DC->addDecl(DRD);
20098     DRD->setAccess(AS);
20099     Decls.push_back(DRD);
20100     if (Invalid)
20101       DRD->setInvalidDecl();
20102     else
20103       PrevDRD = DRD;
20104   }
20105 
20106   return DeclGroupPtrTy::make(
20107       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
20108 }
20109 
20110 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
20111   auto *DRD = cast<OMPDeclareReductionDecl>(D);
20112 
20113   // Enter new function scope.
20114   PushFunctionScope();
20115   setFunctionHasBranchProtectedScope();
20116   getCurFunction()->setHasOMPDeclareReductionCombiner();
20117 
20118   if (S != nullptr)
20119     PushDeclContext(S, DRD);
20120   else
20121     CurContext = DRD;
20122 
20123   PushExpressionEvaluationContext(
20124       ExpressionEvaluationContext::PotentiallyEvaluated);
20125 
20126   QualType ReductionType = DRD->getType();
20127   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
20128   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
20129   // uses semantics of argument handles by value, but it should be passed by
20130   // reference. C lang does not support references, so pass all parameters as
20131   // pointers.
20132   // Create 'T omp_in;' variable.
20133   VarDecl *OmpInParm =
20134       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
20135   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
20136   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
20137   // uses semantics of argument handles by value, but it should be passed by
20138   // reference. C lang does not support references, so pass all parameters as
20139   // pointers.
20140   // Create 'T omp_out;' variable.
20141   VarDecl *OmpOutParm =
20142       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
20143   if (S != nullptr) {
20144     PushOnScopeChains(OmpInParm, S);
20145     PushOnScopeChains(OmpOutParm, S);
20146   } else {
20147     DRD->addDecl(OmpInParm);
20148     DRD->addDecl(OmpOutParm);
20149   }
20150   Expr *InE =
20151       ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
20152   Expr *OutE =
20153       ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
20154   DRD->setCombinerData(InE, OutE);
20155 }
20156 
20157 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
20158   auto *DRD = cast<OMPDeclareReductionDecl>(D);
20159   DiscardCleanupsInEvaluationContext();
20160   PopExpressionEvaluationContext();
20161 
20162   PopDeclContext();
20163   PopFunctionScopeInfo();
20164 
20165   if (Combiner != nullptr)
20166     DRD->setCombiner(Combiner);
20167   else
20168     DRD->setInvalidDecl();
20169 }
20170 
20171 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
20172   auto *DRD = cast<OMPDeclareReductionDecl>(D);
20173 
20174   // Enter new function scope.
20175   PushFunctionScope();
20176   setFunctionHasBranchProtectedScope();
20177 
20178   if (S != nullptr)
20179     PushDeclContext(S, DRD);
20180   else
20181     CurContext = DRD;
20182 
20183   PushExpressionEvaluationContext(
20184       ExpressionEvaluationContext::PotentiallyEvaluated);
20185 
20186   QualType ReductionType = DRD->getType();
20187   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
20188   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
20189   // uses semantics of argument handles by value, but it should be passed by
20190   // reference. C lang does not support references, so pass all parameters as
20191   // pointers.
20192   // Create 'T omp_priv;' variable.
20193   VarDecl *OmpPrivParm =
20194       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
20195   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
20196   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
20197   // uses semantics of argument handles by value, but it should be passed by
20198   // reference. C lang does not support references, so pass all parameters as
20199   // pointers.
20200   // Create 'T omp_orig;' variable.
20201   VarDecl *OmpOrigParm =
20202       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
20203   if (S != nullptr) {
20204     PushOnScopeChains(OmpPrivParm, S);
20205     PushOnScopeChains(OmpOrigParm, S);
20206   } else {
20207     DRD->addDecl(OmpPrivParm);
20208     DRD->addDecl(OmpOrigParm);
20209   }
20210   Expr *OrigE =
20211       ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
20212   Expr *PrivE =
20213       ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
20214   DRD->setInitializerData(OrigE, PrivE);
20215   return OmpPrivParm;
20216 }
20217 
20218 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
20219                                                      VarDecl *OmpPrivParm) {
20220   auto *DRD = cast<OMPDeclareReductionDecl>(D);
20221   DiscardCleanupsInEvaluationContext();
20222   PopExpressionEvaluationContext();
20223 
20224   PopDeclContext();
20225   PopFunctionScopeInfo();
20226 
20227   if (Initializer != nullptr) {
20228     DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
20229   } else if (OmpPrivParm->hasInit()) {
20230     DRD->setInitializer(OmpPrivParm->getInit(),
20231                         OmpPrivParm->isDirectInit()
20232                             ? OMPDeclareReductionDecl::DirectInit
20233                             : OMPDeclareReductionDecl::CopyInit);
20234   } else {
20235     DRD->setInvalidDecl();
20236   }
20237 }
20238 
20239 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
20240     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
20241   for (Decl *D : DeclReductions.get()) {
20242     if (IsValid) {
20243       if (S)
20244         PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
20245                           /*AddToContext=*/false);
20246     } else {
20247       D->setInvalidDecl();
20248     }
20249   }
20250   return DeclReductions;
20251 }
20252 
20253 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
20254   TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
20255   QualType T = TInfo->getType();
20256   if (D.isInvalidType())
20257     return true;
20258 
20259   if (getLangOpts().CPlusPlus) {
20260     // Check that there are no default arguments (C++ only).
20261     CheckExtraCXXDefaultArguments(D);
20262   }
20263 
20264   return CreateParsedType(T, TInfo);
20265 }
20266 
20267 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
20268                                             TypeResult ParsedType) {
20269   assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
20270 
20271   QualType MapperType = GetTypeFromParser(ParsedType.get());
20272   assert(!MapperType.isNull() && "Expect valid mapper type");
20273 
20274   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
20275   //  The type must be of struct, union or class type in C and C++
20276   if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
20277     Diag(TyLoc, diag::err_omp_mapper_wrong_type);
20278     return QualType();
20279   }
20280   return MapperType;
20281 }
20282 
20283 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective(
20284     Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
20285     SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
20286     Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) {
20287   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
20288                       forRedeclarationInCurContext());
20289   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
20290   //  A mapper-identifier may not be redeclared in the current scope for the
20291   //  same type or for a type that is compatible according to the base language
20292   //  rules.
20293   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
20294   OMPDeclareMapperDecl *PrevDMD = nullptr;
20295   bool InCompoundScope = true;
20296   if (S != nullptr) {
20297     // Find previous declaration with the same name not referenced in other
20298     // declarations.
20299     FunctionScopeInfo *ParentFn = getEnclosingFunction();
20300     InCompoundScope =
20301         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
20302     LookupName(Lookup, S);
20303     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
20304                          /*AllowInlineNamespace=*/false);
20305     llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
20306     LookupResult::Filter Filter = Lookup.makeFilter();
20307     while (Filter.hasNext()) {
20308       auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
20309       if (InCompoundScope) {
20310         auto I = UsedAsPrevious.find(PrevDecl);
20311         if (I == UsedAsPrevious.end())
20312           UsedAsPrevious[PrevDecl] = false;
20313         if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
20314           UsedAsPrevious[D] = true;
20315       }
20316       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
20317           PrevDecl->getLocation();
20318     }
20319     Filter.done();
20320     if (InCompoundScope) {
20321       for (const auto &PrevData : UsedAsPrevious) {
20322         if (!PrevData.second) {
20323           PrevDMD = PrevData.first;
20324           break;
20325         }
20326       }
20327     }
20328   } else if (PrevDeclInScope) {
20329     auto *PrevDMDInScope = PrevDMD =
20330         cast<OMPDeclareMapperDecl>(PrevDeclInScope);
20331     do {
20332       PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
20333           PrevDMDInScope->getLocation();
20334       PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
20335     } while (PrevDMDInScope != nullptr);
20336   }
20337   const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
20338   bool Invalid = false;
20339   if (I != PreviousRedeclTypes.end()) {
20340     Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
20341         << MapperType << Name;
20342     Diag(I->second, diag::note_previous_definition);
20343     Invalid = true;
20344   }
20345   // Build expressions for implicit maps of data members with 'default'
20346   // mappers.
20347   SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses.begin(),
20348                                                   Clauses.end());
20349   if (LangOpts.OpenMP >= 50)
20350     processImplicitMapsWithDefaultMappers(*this, DSAStack, ClausesWithImplicit);
20351   auto *DMD =
20352       OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, MapperType, VN,
20353                                    ClausesWithImplicit, PrevDMD);
20354   if (S)
20355     PushOnScopeChains(DMD, S);
20356   else
20357     DC->addDecl(DMD);
20358   DMD->setAccess(AS);
20359   if (Invalid)
20360     DMD->setInvalidDecl();
20361 
20362   auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl();
20363   VD->setDeclContext(DMD);
20364   VD->setLexicalDeclContext(DMD);
20365   DMD->addDecl(VD);
20366   DMD->setMapperVarRef(MapperVarRef);
20367 
20368   return DeclGroupPtrTy::make(DeclGroupRef(DMD));
20369 }
20370 
20371 ExprResult
20372 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType,
20373                                                SourceLocation StartLoc,
20374                                                DeclarationName VN) {
20375   TypeSourceInfo *TInfo =
20376       Context.getTrivialTypeSourceInfo(MapperType, StartLoc);
20377   auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(),
20378                              StartLoc, StartLoc, VN.getAsIdentifierInfo(),
20379                              MapperType, TInfo, SC_None);
20380   if (S)
20381     PushOnScopeChains(VD, S, /*AddToContext=*/false);
20382   Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
20383   DSAStack->addDeclareMapperVarRef(E);
20384   return E;
20385 }
20386 
20387 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const {
20388   assert(LangOpts.OpenMP && "Expected OpenMP mode.");
20389   const Expr *Ref = DSAStack->getDeclareMapperVarRef();
20390   if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) {
20391     if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl())
20392       return true;
20393     if (VD->isUsableInConstantExpressions(Context))
20394       return true;
20395     return false;
20396   }
20397   return true;
20398 }
20399 
20400 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const {
20401   assert(LangOpts.OpenMP && "Expected OpenMP mode.");
20402   return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl();
20403 }
20404 
20405 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
20406                                            SourceLocation StartLoc,
20407                                            SourceLocation LParenLoc,
20408                                            SourceLocation EndLoc) {
20409   Expr *ValExpr = NumTeams;
20410   Stmt *HelperValStmt = nullptr;
20411 
20412   // OpenMP [teams Constrcut, Restrictions]
20413   // The num_teams expression must evaluate to a positive integer value.
20414   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
20415                                  /*StrictlyPositive=*/true))
20416     return nullptr;
20417 
20418   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
20419   OpenMPDirectiveKind CaptureRegion =
20420       getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP);
20421   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
20422     ValExpr = MakeFullExpr(ValExpr).get();
20423     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
20424     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
20425     HelperValStmt = buildPreInits(Context, Captures);
20426   }
20427 
20428   return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
20429                                          StartLoc, LParenLoc, EndLoc);
20430 }
20431 
20432 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
20433                                               SourceLocation StartLoc,
20434                                               SourceLocation LParenLoc,
20435                                               SourceLocation EndLoc) {
20436   Expr *ValExpr = ThreadLimit;
20437   Stmt *HelperValStmt = nullptr;
20438 
20439   // OpenMP [teams Constrcut, Restrictions]
20440   // The thread_limit expression must evaluate to a positive integer value.
20441   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
20442                                  /*StrictlyPositive=*/true))
20443     return nullptr;
20444 
20445   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
20446   OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
20447       DKind, OMPC_thread_limit, LangOpts.OpenMP);
20448   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
20449     ValExpr = MakeFullExpr(ValExpr).get();
20450     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
20451     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
20452     HelperValStmt = buildPreInits(Context, Captures);
20453   }
20454 
20455   return new (Context) OMPThreadLimitClause(
20456       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
20457 }
20458 
20459 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
20460                                            SourceLocation StartLoc,
20461                                            SourceLocation LParenLoc,
20462                                            SourceLocation EndLoc) {
20463   Expr *ValExpr = Priority;
20464   Stmt *HelperValStmt = nullptr;
20465   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
20466 
20467   // OpenMP [2.9.1, task Constrcut]
20468   // The priority-value is a non-negative numerical scalar expression.
20469   if (!isNonNegativeIntegerValue(
20470           ValExpr, *this, OMPC_priority,
20471           /*StrictlyPositive=*/false, /*BuildCapture=*/true,
20472           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
20473     return nullptr;
20474 
20475   return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion,
20476                                          StartLoc, LParenLoc, EndLoc);
20477 }
20478 
20479 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
20480                                             SourceLocation StartLoc,
20481                                             SourceLocation LParenLoc,
20482                                             SourceLocation EndLoc) {
20483   Expr *ValExpr = Grainsize;
20484   Stmt *HelperValStmt = nullptr;
20485   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
20486 
20487   // OpenMP [2.9.2, taskloop Constrcut]
20488   // The parameter of the grainsize clause must be a positive integer
20489   // expression.
20490   if (!isNonNegativeIntegerValue(
20491           ValExpr, *this, OMPC_grainsize,
20492           /*StrictlyPositive=*/true, /*BuildCapture=*/true,
20493           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
20494     return nullptr;
20495 
20496   return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion,
20497                                           StartLoc, LParenLoc, EndLoc);
20498 }
20499 
20500 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
20501                                            SourceLocation StartLoc,
20502                                            SourceLocation LParenLoc,
20503                                            SourceLocation EndLoc) {
20504   Expr *ValExpr = NumTasks;
20505   Stmt *HelperValStmt = nullptr;
20506   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
20507 
20508   // OpenMP [2.9.2, taskloop Constrcut]
20509   // The parameter of the num_tasks clause must be a positive integer
20510   // expression.
20511   if (!isNonNegativeIntegerValue(
20512           ValExpr, *this, OMPC_num_tasks,
20513           /*StrictlyPositive=*/true, /*BuildCapture=*/true,
20514           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
20515     return nullptr;
20516 
20517   return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion,
20518                                          StartLoc, LParenLoc, EndLoc);
20519 }
20520 
20521 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
20522                                        SourceLocation LParenLoc,
20523                                        SourceLocation EndLoc) {
20524   // OpenMP [2.13.2, critical construct, Description]
20525   // ... where hint-expression is an integer constant expression that evaluates
20526   // to a valid lock hint.
20527   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
20528   if (HintExpr.isInvalid())
20529     return nullptr;
20530   return new (Context)
20531       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
20532 }
20533 
20534 /// Tries to find omp_event_handle_t type.
20535 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc,
20536                                 DSAStackTy *Stack) {
20537   QualType OMPEventHandleT = Stack->getOMPEventHandleT();
20538   if (!OMPEventHandleT.isNull())
20539     return true;
20540   IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t");
20541   ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope());
20542   if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
20543     S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t";
20544     return false;
20545   }
20546   Stack->setOMPEventHandleT(PT.get());
20547   return true;
20548 }
20549 
20550 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc,
20551                                          SourceLocation LParenLoc,
20552                                          SourceLocation EndLoc) {
20553   if (!Evt->isValueDependent() && !Evt->isTypeDependent() &&
20554       !Evt->isInstantiationDependent() &&
20555       !Evt->containsUnexpandedParameterPack()) {
20556     if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack))
20557       return nullptr;
20558     // OpenMP 5.0, 2.10.1 task Construct.
20559     // event-handle is a variable of the omp_event_handle_t type.
20560     auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts());
20561     if (!Ref) {
20562       Diag(Evt->getExprLoc(), diag::err_omp_var_expected)
20563           << "omp_event_handle_t" << 0 << Evt->getSourceRange();
20564       return nullptr;
20565     }
20566     auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl());
20567     if (!VD) {
20568       Diag(Evt->getExprLoc(), diag::err_omp_var_expected)
20569           << "omp_event_handle_t" << 0 << Evt->getSourceRange();
20570       return nullptr;
20571     }
20572     if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(),
20573                                         VD->getType()) ||
20574         VD->getType().isConstant(Context)) {
20575       Diag(Evt->getExprLoc(), diag::err_omp_var_expected)
20576           << "omp_event_handle_t" << 1 << VD->getType()
20577           << Evt->getSourceRange();
20578       return nullptr;
20579     }
20580     // OpenMP 5.0, 2.10.1 task Construct
20581     // [detach clause]... The event-handle will be considered as if it was
20582     // specified on a firstprivate clause.
20583     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false);
20584     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
20585         DVar.RefExpr) {
20586       Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa)
20587           << getOpenMPClauseName(DVar.CKind)
20588           << getOpenMPClauseName(OMPC_firstprivate);
20589       reportOriginalDsa(*this, DSAStack, VD, DVar);
20590       return nullptr;
20591     }
20592   }
20593 
20594   return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc);
20595 }
20596 
20597 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
20598     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
20599     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
20600     SourceLocation EndLoc) {
20601   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
20602     std::string Values;
20603     Values += "'";
20604     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
20605     Values += "'";
20606     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
20607         << Values << getOpenMPClauseName(OMPC_dist_schedule);
20608     return nullptr;
20609   }
20610   Expr *ValExpr = ChunkSize;
20611   Stmt *HelperValStmt = nullptr;
20612   if (ChunkSize) {
20613     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
20614         !ChunkSize->isInstantiationDependent() &&
20615         !ChunkSize->containsUnexpandedParameterPack()) {
20616       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
20617       ExprResult Val =
20618           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
20619       if (Val.isInvalid())
20620         return nullptr;
20621 
20622       ValExpr = Val.get();
20623 
20624       // OpenMP [2.7.1, Restrictions]
20625       //  chunk_size must be a loop invariant integer expression with a positive
20626       //  value.
20627       if (Optional<llvm::APSInt> Result =
20628               ValExpr->getIntegerConstantExpr(Context)) {
20629         if (Result->isSigned() && !Result->isStrictlyPositive()) {
20630           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
20631               << "dist_schedule" << ChunkSize->getSourceRange();
20632           return nullptr;
20633         }
20634       } else if (getOpenMPCaptureRegionForClause(
20635                      DSAStack->getCurrentDirective(), OMPC_dist_schedule,
20636                      LangOpts.OpenMP) != OMPD_unknown &&
20637                  !CurContext->isDependentContext()) {
20638         ValExpr = MakeFullExpr(ValExpr).get();
20639         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
20640         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
20641         HelperValStmt = buildPreInits(Context, Captures);
20642       }
20643     }
20644   }
20645 
20646   return new (Context)
20647       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
20648                             Kind, ValExpr, HelperValStmt);
20649 }
20650 
20651 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
20652     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
20653     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
20654     SourceLocation KindLoc, SourceLocation EndLoc) {
20655   if (getLangOpts().OpenMP < 50) {
20656     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
20657         Kind != OMPC_DEFAULTMAP_scalar) {
20658       std::string Value;
20659       SourceLocation Loc;
20660       Value += "'";
20661       if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
20662         Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
20663                                                OMPC_DEFAULTMAP_MODIFIER_tofrom);
20664         Loc = MLoc;
20665       } else {
20666         Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
20667                                                OMPC_DEFAULTMAP_scalar);
20668         Loc = KindLoc;
20669       }
20670       Value += "'";
20671       Diag(Loc, diag::err_omp_unexpected_clause_value)
20672           << Value << getOpenMPClauseName(OMPC_defaultmap);
20673       return nullptr;
20674     }
20675   } else {
20676     bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown);
20677     bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) ||
20678                             (LangOpts.OpenMP >= 50 && KindLoc.isInvalid());
20679     if (!isDefaultmapKind || !isDefaultmapModifier) {
20680       StringRef KindValue = "'scalar', 'aggregate', 'pointer'";
20681       if (LangOpts.OpenMP == 50) {
20682         StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', "
20683                                   "'firstprivate', 'none', 'default'";
20684         if (!isDefaultmapKind && isDefaultmapModifier) {
20685           Diag(KindLoc, diag::err_omp_unexpected_clause_value)
20686               << KindValue << getOpenMPClauseName(OMPC_defaultmap);
20687         } else if (isDefaultmapKind && !isDefaultmapModifier) {
20688           Diag(MLoc, diag::err_omp_unexpected_clause_value)
20689               << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
20690         } else {
20691           Diag(MLoc, diag::err_omp_unexpected_clause_value)
20692               << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
20693           Diag(KindLoc, diag::err_omp_unexpected_clause_value)
20694               << KindValue << getOpenMPClauseName(OMPC_defaultmap);
20695         }
20696       } else {
20697         StringRef ModifierValue =
20698             "'alloc', 'from', 'to', 'tofrom', "
20699             "'firstprivate', 'none', 'default', 'present'";
20700         if (!isDefaultmapKind && isDefaultmapModifier) {
20701           Diag(KindLoc, diag::err_omp_unexpected_clause_value)
20702               << KindValue << getOpenMPClauseName(OMPC_defaultmap);
20703         } else if (isDefaultmapKind && !isDefaultmapModifier) {
20704           Diag(MLoc, diag::err_omp_unexpected_clause_value)
20705               << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
20706         } else {
20707           Diag(MLoc, diag::err_omp_unexpected_clause_value)
20708               << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
20709           Diag(KindLoc, diag::err_omp_unexpected_clause_value)
20710               << KindValue << getOpenMPClauseName(OMPC_defaultmap);
20711         }
20712       }
20713       return nullptr;
20714     }
20715 
20716     // OpenMP [5.0, 2.12.5, Restrictions, p. 174]
20717     //  At most one defaultmap clause for each category can appear on the
20718     //  directive.
20719     if (DSAStack->checkDefaultmapCategory(Kind)) {
20720       Diag(StartLoc, diag::err_omp_one_defaultmap_each_category);
20721       return nullptr;
20722     }
20723   }
20724   if (Kind == OMPC_DEFAULTMAP_unknown) {
20725     // Variable category is not specified - mark all categories.
20726     DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc);
20727     DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc);
20728     DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc);
20729   } else {
20730     DSAStack->setDefaultDMAAttr(M, Kind, StartLoc);
20731   }
20732 
20733   return new (Context)
20734       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
20735 }
20736 
20737 bool Sema::ActOnStartOpenMPDeclareTargetContext(
20738     DeclareTargetContextInfo &DTCI) {
20739   DeclContext *CurLexicalContext = getCurLexicalContext();
20740   if (!CurLexicalContext->isFileContext() &&
20741       !CurLexicalContext->isExternCContext() &&
20742       !CurLexicalContext->isExternCXXContext() &&
20743       !isa<CXXRecordDecl>(CurLexicalContext) &&
20744       !isa<ClassTemplateDecl>(CurLexicalContext) &&
20745       !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
20746       !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
20747     Diag(DTCI.Loc, diag::err_omp_region_not_file_context);
20748     return false;
20749   }
20750   DeclareTargetNesting.push_back(DTCI);
20751   return true;
20752 }
20753 
20754 const Sema::DeclareTargetContextInfo
20755 Sema::ActOnOpenMPEndDeclareTargetDirective() {
20756   assert(!DeclareTargetNesting.empty() &&
20757          "check isInOpenMPDeclareTargetContext() first!");
20758   return DeclareTargetNesting.pop_back_val();
20759 }
20760 
20761 void Sema::ActOnFinishedOpenMPDeclareTargetContext(
20762     DeclareTargetContextInfo &DTCI) {
20763   for (auto &It : DTCI.ExplicitlyMapped)
20764     ActOnOpenMPDeclareTargetName(It.first, It.second.Loc, It.second.MT,
20765                                  DTCI.DT);
20766 }
20767 
20768 NamedDecl *Sema::lookupOpenMPDeclareTargetName(Scope *CurScope,
20769                                                CXXScopeSpec &ScopeSpec,
20770                                                const DeclarationNameInfo &Id) {
20771   LookupResult Lookup(*this, Id, LookupOrdinaryName);
20772   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
20773 
20774   if (Lookup.isAmbiguous())
20775     return nullptr;
20776   Lookup.suppressDiagnostics();
20777 
20778   if (!Lookup.isSingleResult()) {
20779     VarOrFuncDeclFilterCCC CCC(*this);
20780     if (TypoCorrection Corrected =
20781             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
20782                         CTK_ErrorRecovery)) {
20783       diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
20784                                   << Id.getName());
20785       checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
20786       return nullptr;
20787     }
20788 
20789     Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
20790     return nullptr;
20791   }
20792 
20793   NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
20794   if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) &&
20795       !isa<FunctionTemplateDecl>(ND)) {
20796     Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
20797     return nullptr;
20798   }
20799   return ND;
20800 }
20801 
20802 void Sema::ActOnOpenMPDeclareTargetName(
20803     NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
20804     OMPDeclareTargetDeclAttr::DevTypeTy DT) {
20805   assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
20806           isa<FunctionTemplateDecl>(ND)) &&
20807          "Expected variable, function or function template.");
20808 
20809   // Diagnose marking after use as it may lead to incorrect diagnosis and
20810   // codegen.
20811   if (LangOpts.OpenMP >= 50 &&
20812       (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
20813     Diag(Loc, diag::warn_omp_declare_target_after_first_use);
20814 
20815   // Explicit declare target lists have precedence.
20816   const unsigned Level = -1;
20817 
20818   auto *VD = cast<ValueDecl>(ND);
20819   llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
20820       OMPDeclareTargetDeclAttr::getActiveAttr(VD);
20821   if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getDevType() != DT &&
20822       ActiveAttr.getValue()->getLevel() == Level) {
20823     Diag(Loc, diag::err_omp_device_type_mismatch)
20824         << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT)
20825         << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(
20826                ActiveAttr.getValue()->getDevType());
20827     return;
20828   }
20829   if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getMapType() != MT &&
20830       ActiveAttr.getValue()->getLevel() == Level) {
20831     Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND;
20832     return;
20833   }
20834 
20835   if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() == Level)
20836     return;
20837 
20838   auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT, Level,
20839                                                      SourceRange(Loc, Loc));
20840   ND->addAttr(A);
20841   if (ASTMutationListener *ML = Context.getASTMutationListener())
20842     ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
20843   checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc);
20844 }
20845 
20846 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
20847                                      Sema &SemaRef, Decl *D) {
20848   if (!D || !isa<VarDecl>(D))
20849     return;
20850   auto *VD = cast<VarDecl>(D);
20851   Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
20852       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
20853   if (SemaRef.LangOpts.OpenMP >= 50 &&
20854       (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
20855        SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
20856       VD->hasGlobalStorage()) {
20857     if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) {
20858       // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
20859       // If a lambda declaration and definition appears between a
20860       // declare target directive and the matching end declare target
20861       // directive, all variables that are captured by the lambda
20862       // expression must also appear in a to clause.
20863       SemaRef.Diag(VD->getLocation(),
20864                    diag::err_omp_lambda_capture_in_declare_target_not_to);
20865       SemaRef.Diag(SL, diag::note_var_explicitly_captured_here)
20866           << VD << 0 << SR;
20867       return;
20868     }
20869   }
20870   if (MapTy.hasValue())
20871     return;
20872   SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
20873   SemaRef.Diag(SL, diag::note_used_here) << SR;
20874 }
20875 
20876 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
20877                                    Sema &SemaRef, DSAStackTy *Stack,
20878                                    ValueDecl *VD) {
20879   return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
20880          checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
20881                            /*FullCheck=*/false);
20882 }
20883 
20884 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
20885                                             SourceLocation IdLoc) {
20886   if (!D || D->isInvalidDecl())
20887     return;
20888   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
20889   SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
20890   if (auto *VD = dyn_cast<VarDecl>(D)) {
20891     // Only global variables can be marked as declare target.
20892     if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
20893         !VD->isStaticDataMember())
20894       return;
20895     // 2.10.6: threadprivate variable cannot appear in a declare target
20896     // directive.
20897     if (DSAStack->isThreadPrivate(VD)) {
20898       Diag(SL, diag::err_omp_threadprivate_in_target);
20899       reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
20900       return;
20901     }
20902   }
20903   if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
20904     D = FTD->getTemplatedDecl();
20905   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
20906     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
20907         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
20908     if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
20909       Diag(IdLoc, diag::err_omp_function_in_link_clause);
20910       Diag(FD->getLocation(), diag::note_defined_here) << FD;
20911       return;
20912     }
20913   }
20914   if (auto *VD = dyn_cast<ValueDecl>(D)) {
20915     // Problem if any with var declared with incomplete type will be reported
20916     // as normal, so no need to check it here.
20917     if ((E || !VD->getType()->isIncompleteType()) &&
20918         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
20919       return;
20920     if (!E && isInOpenMPDeclareTargetContext()) {
20921       // Checking declaration inside declare target region.
20922       if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
20923           isa<FunctionTemplateDecl>(D)) {
20924         llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
20925             OMPDeclareTargetDeclAttr::getActiveAttr(VD);
20926         unsigned Level = DeclareTargetNesting.size();
20927         if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() >= Level)
20928           return;
20929         DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back();
20930         auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
20931             Context, OMPDeclareTargetDeclAttr::MT_To, DTCI.DT, Level,
20932             SourceRange(DTCI.Loc, DTCI.Loc));
20933         D->addAttr(A);
20934         if (ASTMutationListener *ML = Context.getASTMutationListener())
20935           ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
20936       }
20937       return;
20938     }
20939   }
20940   if (!E)
20941     return;
20942   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
20943 }
20944 
20945 OMPClause *Sema::ActOnOpenMPToClause(
20946     ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
20947     ArrayRef<SourceLocation> MotionModifiersLoc,
20948     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
20949     SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
20950     const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
20951   OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
20952                                           OMPC_MOTION_MODIFIER_unknown};
20953   SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
20954 
20955   // Process motion-modifiers, flag errors for duplicate modifiers.
20956   unsigned Count = 0;
20957   for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
20958     if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
20959         llvm::is_contained(Modifiers, MotionModifiers[I])) {
20960       Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier);
20961       continue;
20962     }
20963     assert(Count < NumberOfOMPMotionModifiers &&
20964            "Modifiers exceed the allowed number of motion modifiers");
20965     Modifiers[Count] = MotionModifiers[I];
20966     ModifiersLoc[Count] = MotionModifiersLoc[I];
20967     ++Count;
20968   }
20969 
20970   MappableVarListInfo MVLI(VarList);
20971   checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
20972                               MapperIdScopeSpec, MapperId, UnresolvedMappers);
20973   if (MVLI.ProcessedVarList.empty())
20974     return nullptr;
20975 
20976   return OMPToClause::Create(
20977       Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
20978       MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc,
20979       MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
20980 }
20981 
20982 OMPClause *Sema::ActOnOpenMPFromClause(
20983     ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
20984     ArrayRef<SourceLocation> MotionModifiersLoc,
20985     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
20986     SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
20987     const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
20988   OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
20989                                           OMPC_MOTION_MODIFIER_unknown};
20990   SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
20991 
20992   // Process motion-modifiers, flag errors for duplicate modifiers.
20993   unsigned Count = 0;
20994   for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
20995     if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
20996         llvm::is_contained(Modifiers, MotionModifiers[I])) {
20997       Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier);
20998       continue;
20999     }
21000     assert(Count < NumberOfOMPMotionModifiers &&
21001            "Modifiers exceed the allowed number of motion modifiers");
21002     Modifiers[Count] = MotionModifiers[I];
21003     ModifiersLoc[Count] = MotionModifiersLoc[I];
21004     ++Count;
21005   }
21006 
21007   MappableVarListInfo MVLI(VarList);
21008   checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
21009                               MapperIdScopeSpec, MapperId, UnresolvedMappers);
21010   if (MVLI.ProcessedVarList.empty())
21011     return nullptr;
21012 
21013   return OMPFromClause::Create(
21014       Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
21015       MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc,
21016       MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
21017 }
21018 
21019 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
21020                                                const OMPVarListLocTy &Locs) {
21021   MappableVarListInfo MVLI(VarList);
21022   SmallVector<Expr *, 8> PrivateCopies;
21023   SmallVector<Expr *, 8> Inits;
21024 
21025   for (Expr *RefExpr : VarList) {
21026     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
21027     SourceLocation ELoc;
21028     SourceRange ERange;
21029     Expr *SimpleRefExpr = RefExpr;
21030     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
21031     if (Res.second) {
21032       // It will be analyzed later.
21033       MVLI.ProcessedVarList.push_back(RefExpr);
21034       PrivateCopies.push_back(nullptr);
21035       Inits.push_back(nullptr);
21036     }
21037     ValueDecl *D = Res.first;
21038     if (!D)
21039       continue;
21040 
21041     QualType Type = D->getType();
21042     Type = Type.getNonReferenceType().getUnqualifiedType();
21043 
21044     auto *VD = dyn_cast<VarDecl>(D);
21045 
21046     // Item should be a pointer or reference to pointer.
21047     if (!Type->isPointerType()) {
21048       Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
21049           << 0 << RefExpr->getSourceRange();
21050       continue;
21051     }
21052 
21053     // Build the private variable and the expression that refers to it.
21054     auto VDPrivate =
21055         buildVarDecl(*this, ELoc, Type, D->getName(),
21056                      D->hasAttrs() ? &D->getAttrs() : nullptr,
21057                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
21058     if (VDPrivate->isInvalidDecl())
21059       continue;
21060 
21061     CurContext->addDecl(VDPrivate);
21062     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
21063         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
21064 
21065     // Add temporary variable to initialize the private copy of the pointer.
21066     VarDecl *VDInit =
21067         buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
21068     DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
21069         *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
21070     AddInitializerToDecl(VDPrivate,
21071                          DefaultLvalueConversion(VDInitRefExpr).get(),
21072                          /*DirectInit=*/false);
21073 
21074     // If required, build a capture to implement the privatization initialized
21075     // with the current list item value.
21076     DeclRefExpr *Ref = nullptr;
21077     if (!VD)
21078       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
21079     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
21080     PrivateCopies.push_back(VDPrivateRefExpr);
21081     Inits.push_back(VDInitRefExpr);
21082 
21083     // We need to add a data sharing attribute for this variable to make sure it
21084     // is correctly captured. A variable that shows up in a use_device_ptr has
21085     // similar properties of a first private variable.
21086     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
21087 
21088     // Create a mappable component for the list item. List items in this clause
21089     // only need a component.
21090     MVLI.VarBaseDeclarations.push_back(D);
21091     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
21092     MVLI.VarComponents.back().emplace_back(SimpleRefExpr, D,
21093                                            /*IsNonContiguous=*/false);
21094   }
21095 
21096   if (MVLI.ProcessedVarList.empty())
21097     return nullptr;
21098 
21099   return OMPUseDevicePtrClause::Create(
21100       Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
21101       MVLI.VarBaseDeclarations, MVLI.VarComponents);
21102 }
21103 
21104 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList,
21105                                                 const OMPVarListLocTy &Locs) {
21106   MappableVarListInfo MVLI(VarList);
21107 
21108   for (Expr *RefExpr : VarList) {
21109     assert(RefExpr && "NULL expr in OpenMP use_device_addr clause.");
21110     SourceLocation ELoc;
21111     SourceRange ERange;
21112     Expr *SimpleRefExpr = RefExpr;
21113     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
21114                               /*AllowArraySection=*/true);
21115     if (Res.second) {
21116       // It will be analyzed later.
21117       MVLI.ProcessedVarList.push_back(RefExpr);
21118     }
21119     ValueDecl *D = Res.first;
21120     if (!D)
21121       continue;
21122     auto *VD = dyn_cast<VarDecl>(D);
21123 
21124     // If required, build a capture to implement the privatization initialized
21125     // with the current list item value.
21126     DeclRefExpr *Ref = nullptr;
21127     if (!VD)
21128       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
21129     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
21130 
21131     // We need to add a data sharing attribute for this variable to make sure it
21132     // is correctly captured. A variable that shows up in a use_device_addr has
21133     // similar properties of a first private variable.
21134     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
21135 
21136     // Create a mappable component for the list item. List items in this clause
21137     // only need a component.
21138     MVLI.VarBaseDeclarations.push_back(D);
21139     MVLI.VarComponents.emplace_back();
21140     Expr *Component = SimpleRefExpr;
21141     if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) ||
21142                isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts())))
21143       Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get();
21144     MVLI.VarComponents.back().emplace_back(Component, D,
21145                                            /*IsNonContiguous=*/false);
21146   }
21147 
21148   if (MVLI.ProcessedVarList.empty())
21149     return nullptr;
21150 
21151   return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList,
21152                                         MVLI.VarBaseDeclarations,
21153                                         MVLI.VarComponents);
21154 }
21155 
21156 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
21157                                               const OMPVarListLocTy &Locs) {
21158   MappableVarListInfo MVLI(VarList);
21159   for (Expr *RefExpr : VarList) {
21160     assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
21161     SourceLocation ELoc;
21162     SourceRange ERange;
21163     Expr *SimpleRefExpr = RefExpr;
21164     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
21165     if (Res.second) {
21166       // It will be analyzed later.
21167       MVLI.ProcessedVarList.push_back(RefExpr);
21168     }
21169     ValueDecl *D = Res.first;
21170     if (!D)
21171       continue;
21172 
21173     QualType Type = D->getType();
21174     // item should be a pointer or array or reference to pointer or array
21175     if (!Type.getNonReferenceType()->isPointerType() &&
21176         !Type.getNonReferenceType()->isArrayType()) {
21177       Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
21178           << 0 << RefExpr->getSourceRange();
21179       continue;
21180     }
21181 
21182     // Check if the declaration in the clause does not show up in any data
21183     // sharing attribute.
21184     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
21185     if (isOpenMPPrivate(DVar.CKind)) {
21186       Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
21187           << getOpenMPClauseName(DVar.CKind)
21188           << getOpenMPClauseName(OMPC_is_device_ptr)
21189           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
21190       reportOriginalDsa(*this, DSAStack, D, DVar);
21191       continue;
21192     }
21193 
21194     const Expr *ConflictExpr;
21195     if (DSAStack->checkMappableExprComponentListsForDecl(
21196             D, /*CurrentRegionOnly=*/true,
21197             [&ConflictExpr](
21198                 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
21199                 OpenMPClauseKind) -> bool {
21200               ConflictExpr = R.front().getAssociatedExpression();
21201               return true;
21202             })) {
21203       Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
21204       Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
21205           << ConflictExpr->getSourceRange();
21206       continue;
21207     }
21208 
21209     // Store the components in the stack so that they can be used to check
21210     // against other clauses later on.
21211     OMPClauseMappableExprCommon::MappableComponent MC(
21212         SimpleRefExpr, D, /*IsNonContiguous=*/false);
21213     DSAStack->addMappableExpressionComponents(
21214         D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
21215 
21216     // Record the expression we've just processed.
21217     MVLI.ProcessedVarList.push_back(SimpleRefExpr);
21218 
21219     // Create a mappable component for the list item. List items in this clause
21220     // only need a component. We use a null declaration to signal fields in
21221     // 'this'.
21222     assert((isa<DeclRefExpr>(SimpleRefExpr) ||
21223             isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
21224            "Unexpected device pointer expression!");
21225     MVLI.VarBaseDeclarations.push_back(
21226         isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
21227     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
21228     MVLI.VarComponents.back().push_back(MC);
21229   }
21230 
21231   if (MVLI.ProcessedVarList.empty())
21232     return nullptr;
21233 
21234   return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
21235                                       MVLI.VarBaseDeclarations,
21236                                       MVLI.VarComponents);
21237 }
21238 
21239 OMPClause *Sema::ActOnOpenMPAllocateClause(
21240     Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
21241     SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
21242   if (Allocator) {
21243     // OpenMP [2.11.4 allocate Clause, Description]
21244     // allocator is an expression of omp_allocator_handle_t type.
21245     if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack))
21246       return nullptr;
21247 
21248     ExprResult AllocatorRes = DefaultLvalueConversion(Allocator);
21249     if (AllocatorRes.isInvalid())
21250       return nullptr;
21251     AllocatorRes = PerformImplicitConversion(AllocatorRes.get(),
21252                                              DSAStack->getOMPAllocatorHandleT(),
21253                                              Sema::AA_Initializing,
21254                                              /*AllowExplicit=*/true);
21255     if (AllocatorRes.isInvalid())
21256       return nullptr;
21257     Allocator = AllocatorRes.get();
21258   } else {
21259     // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
21260     // allocate clauses that appear on a target construct or on constructs in a
21261     // target region must specify an allocator expression unless a requires
21262     // directive with the dynamic_allocators clause is present in the same
21263     // compilation unit.
21264     if (LangOpts.OpenMPIsDevice &&
21265         !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
21266       targetDiag(StartLoc, diag::err_expected_allocator_expression);
21267   }
21268   // Analyze and build list of variables.
21269   SmallVector<Expr *, 8> Vars;
21270   for (Expr *RefExpr : VarList) {
21271     assert(RefExpr && "NULL expr in OpenMP private clause.");
21272     SourceLocation ELoc;
21273     SourceRange ERange;
21274     Expr *SimpleRefExpr = RefExpr;
21275     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
21276     if (Res.second) {
21277       // It will be analyzed later.
21278       Vars.push_back(RefExpr);
21279     }
21280     ValueDecl *D = Res.first;
21281     if (!D)
21282       continue;
21283 
21284     auto *VD = dyn_cast<VarDecl>(D);
21285     DeclRefExpr *Ref = nullptr;
21286     if (!VD && !CurContext->isDependentContext())
21287       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
21288     Vars.push_back((VD || CurContext->isDependentContext())
21289                        ? RefExpr->IgnoreParens()
21290                        : Ref);
21291   }
21292 
21293   if (Vars.empty())
21294     return nullptr;
21295 
21296   if (Allocator)
21297     DSAStack->addInnerAllocatorExpr(Allocator);
21298   return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator,
21299                                    ColonLoc, EndLoc, Vars);
21300 }
21301 
21302 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList,
21303                                               SourceLocation StartLoc,
21304                                               SourceLocation LParenLoc,
21305                                               SourceLocation EndLoc) {
21306   SmallVector<Expr *, 8> Vars;
21307   for (Expr *RefExpr : VarList) {
21308     assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
21309     SourceLocation ELoc;
21310     SourceRange ERange;
21311     Expr *SimpleRefExpr = RefExpr;
21312     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
21313     if (Res.second)
21314       // It will be analyzed later.
21315       Vars.push_back(RefExpr);
21316     ValueDecl *D = Res.first;
21317     if (!D)
21318       continue;
21319 
21320     // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions.
21321     // A list-item cannot appear in more than one nontemporal clause.
21322     if (const Expr *PrevRef =
21323             DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) {
21324       Diag(ELoc, diag::err_omp_used_in_clause_twice)
21325           << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange;
21326       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
21327           << getOpenMPClauseName(OMPC_nontemporal);
21328       continue;
21329     }
21330 
21331     Vars.push_back(RefExpr);
21332   }
21333 
21334   if (Vars.empty())
21335     return nullptr;
21336 
21337   return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc,
21338                                       Vars);
21339 }
21340 
21341 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList,
21342                                             SourceLocation StartLoc,
21343                                             SourceLocation LParenLoc,
21344                                             SourceLocation EndLoc) {
21345   SmallVector<Expr *, 8> Vars;
21346   for (Expr *RefExpr : VarList) {
21347     assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
21348     SourceLocation ELoc;
21349     SourceRange ERange;
21350     Expr *SimpleRefExpr = RefExpr;
21351     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
21352                               /*AllowArraySection=*/true);
21353     if (Res.second)
21354       // It will be analyzed later.
21355       Vars.push_back(RefExpr);
21356     ValueDecl *D = Res.first;
21357     if (!D)
21358       continue;
21359 
21360     const DSAStackTy::DSAVarData DVar =
21361         DSAStack->getTopDSA(D, /*FromParent=*/true);
21362     // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
21363     // A list item that appears in the inclusive or exclusive clause must appear
21364     // in a reduction clause with the inscan modifier on the enclosing
21365     // worksharing-loop, worksharing-loop SIMD, or simd construct.
21366     if (DVar.CKind != OMPC_reduction || DVar.Modifier != OMPC_REDUCTION_inscan)
21367       Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
21368           << RefExpr->getSourceRange();
21369 
21370     if (DSAStack->getParentDirective() != OMPD_unknown)
21371       DSAStack->markDeclAsUsedInScanDirective(D);
21372     Vars.push_back(RefExpr);
21373   }
21374 
21375   if (Vars.empty())
21376     return nullptr;
21377 
21378   return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
21379 }
21380 
21381 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList,
21382                                             SourceLocation StartLoc,
21383                                             SourceLocation LParenLoc,
21384                                             SourceLocation EndLoc) {
21385   SmallVector<Expr *, 8> Vars;
21386   for (Expr *RefExpr : VarList) {
21387     assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
21388     SourceLocation ELoc;
21389     SourceRange ERange;
21390     Expr *SimpleRefExpr = RefExpr;
21391     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
21392                               /*AllowArraySection=*/true);
21393     if (Res.second)
21394       // It will be analyzed later.
21395       Vars.push_back(RefExpr);
21396     ValueDecl *D = Res.first;
21397     if (!D)
21398       continue;
21399 
21400     OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
21401     DSAStackTy::DSAVarData DVar;
21402     if (ParentDirective != OMPD_unknown)
21403       DVar = DSAStack->getTopDSA(D, /*FromParent=*/true);
21404     // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
21405     // A list item that appears in the inclusive or exclusive clause must appear
21406     // in a reduction clause with the inscan modifier on the enclosing
21407     // worksharing-loop, worksharing-loop SIMD, or simd construct.
21408     if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction ||
21409         DVar.Modifier != OMPC_REDUCTION_inscan) {
21410       Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
21411           << RefExpr->getSourceRange();
21412     } else {
21413       DSAStack->markDeclAsUsedInScanDirective(D);
21414     }
21415     Vars.push_back(RefExpr);
21416   }
21417 
21418   if (Vars.empty())
21419     return nullptr;
21420 
21421   return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
21422 }
21423 
21424 /// Tries to find omp_alloctrait_t type.
21425 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) {
21426   QualType OMPAlloctraitT = Stack->getOMPAlloctraitT();
21427   if (!OMPAlloctraitT.isNull())
21428     return true;
21429   IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t");
21430   ParsedType PT = S.getTypeName(II, Loc, S.getCurScope());
21431   if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
21432     S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t";
21433     return false;
21434   }
21435   Stack->setOMPAlloctraitT(PT.get());
21436   return true;
21437 }
21438 
21439 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause(
21440     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc,
21441     ArrayRef<UsesAllocatorsData> Data) {
21442   // OpenMP [2.12.5, target Construct]
21443   // allocator is an identifier of omp_allocator_handle_t type.
21444   if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack))
21445     return nullptr;
21446   // OpenMP [2.12.5, target Construct]
21447   // allocator-traits-array is an identifier of const omp_alloctrait_t * type.
21448   if (llvm::any_of(
21449           Data,
21450           [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) &&
21451       !findOMPAlloctraitT(*this, StartLoc, DSAStack))
21452     return nullptr;
21453   llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators;
21454   for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
21455     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
21456     StringRef Allocator =
21457         OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
21458     DeclarationName AllocatorName = &Context.Idents.get(Allocator);
21459     PredefinedAllocators.insert(LookupSingleName(
21460         TUScope, AllocatorName, StartLoc, Sema::LookupAnyName));
21461   }
21462 
21463   SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData;
21464   for (const UsesAllocatorsData &D : Data) {
21465     Expr *AllocatorExpr = nullptr;
21466     // Check allocator expression.
21467     if (D.Allocator->isTypeDependent()) {
21468       AllocatorExpr = D.Allocator;
21469     } else {
21470       // Traits were specified - need to assign new allocator to the specified
21471       // allocator, so it must be an lvalue.
21472       AllocatorExpr = D.Allocator->IgnoreParenImpCasts();
21473       auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr);
21474       bool IsPredefinedAllocator = false;
21475       if (DRE)
21476         IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl());
21477       if (!DRE ||
21478           !(Context.hasSameUnqualifiedType(
21479                 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) ||
21480             Context.typesAreCompatible(AllocatorExpr->getType(),
21481                                        DSAStack->getOMPAllocatorHandleT(),
21482                                        /*CompareUnqualified=*/true)) ||
21483           (!IsPredefinedAllocator &&
21484            (AllocatorExpr->getType().isConstant(Context) ||
21485             !AllocatorExpr->isLValue()))) {
21486         Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected)
21487             << "omp_allocator_handle_t" << (DRE ? 1 : 0)
21488             << AllocatorExpr->getType() << D.Allocator->getSourceRange();
21489         continue;
21490       }
21491       // OpenMP [2.12.5, target Construct]
21492       // Predefined allocators appearing in a uses_allocators clause cannot have
21493       // traits specified.
21494       if (IsPredefinedAllocator && D.AllocatorTraits) {
21495         Diag(D.AllocatorTraits->getExprLoc(),
21496              diag::err_omp_predefined_allocator_with_traits)
21497             << D.AllocatorTraits->getSourceRange();
21498         Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator)
21499             << cast<NamedDecl>(DRE->getDecl())->getName()
21500             << D.Allocator->getSourceRange();
21501         continue;
21502       }
21503       // OpenMP [2.12.5, target Construct]
21504       // Non-predefined allocators appearing in a uses_allocators clause must
21505       // have traits specified.
21506       if (!IsPredefinedAllocator && !D.AllocatorTraits) {
21507         Diag(D.Allocator->getExprLoc(),
21508              diag::err_omp_nonpredefined_allocator_without_traits);
21509         continue;
21510       }
21511       // No allocator traits - just convert it to rvalue.
21512       if (!D.AllocatorTraits)
21513         AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get();
21514       DSAStack->addUsesAllocatorsDecl(
21515           DRE->getDecl(),
21516           IsPredefinedAllocator
21517               ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator
21518               : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator);
21519     }
21520     Expr *AllocatorTraitsExpr = nullptr;
21521     if (D.AllocatorTraits) {
21522       if (D.AllocatorTraits->isTypeDependent()) {
21523         AllocatorTraitsExpr = D.AllocatorTraits;
21524       } else {
21525         // OpenMP [2.12.5, target Construct]
21526         // Arrays that contain allocator traits that appear in a uses_allocators
21527         // clause must be constant arrays, have constant values and be defined
21528         // in the same scope as the construct in which the clause appears.
21529         AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts();
21530         // Check that traits expr is a constant array.
21531         QualType TraitTy;
21532         if (const ArrayType *Ty =
21533                 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe())
21534           if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty))
21535             TraitTy = ConstArrayTy->getElementType();
21536         if (TraitTy.isNull() ||
21537             !(Context.hasSameUnqualifiedType(TraitTy,
21538                                              DSAStack->getOMPAlloctraitT()) ||
21539               Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(),
21540                                          /*CompareUnqualified=*/true))) {
21541           Diag(D.AllocatorTraits->getExprLoc(),
21542                diag::err_omp_expected_array_alloctraits)
21543               << AllocatorTraitsExpr->getType();
21544           continue;
21545         }
21546         // Do not map by default allocator traits if it is a standalone
21547         // variable.
21548         if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr))
21549           DSAStack->addUsesAllocatorsDecl(
21550               DRE->getDecl(),
21551               DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait);
21552       }
21553     }
21554     OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back();
21555     NewD.Allocator = AllocatorExpr;
21556     NewD.AllocatorTraits = AllocatorTraitsExpr;
21557     NewD.LParenLoc = D.LParenLoc;
21558     NewD.RParenLoc = D.RParenLoc;
21559   }
21560   return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc,
21561                                          NewData);
21562 }
21563 
21564 OMPClause *Sema::ActOnOpenMPAffinityClause(
21565     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
21566     SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) {
21567   SmallVector<Expr *, 8> Vars;
21568   for (Expr *RefExpr : Locators) {
21569     assert(RefExpr && "NULL expr in OpenMP shared clause.");
21570     if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) {
21571       // It will be analyzed later.
21572       Vars.push_back(RefExpr);
21573       continue;
21574     }
21575 
21576     SourceLocation ELoc = RefExpr->getExprLoc();
21577     Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts();
21578 
21579     if (!SimpleExpr->isLValue()) {
21580       Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
21581           << 1 << 0 << RefExpr->getSourceRange();
21582       continue;
21583     }
21584 
21585     ExprResult Res;
21586     {
21587       Sema::TentativeAnalysisScope Trap(*this);
21588       Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr);
21589     }
21590     if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) &&
21591         !isa<OMPArrayShapingExpr>(SimpleExpr)) {
21592       Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
21593           << 1 << 0 << RefExpr->getSourceRange();
21594       continue;
21595     }
21596     Vars.push_back(SimpleExpr);
21597   }
21598 
21599   return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
21600                                    EndLoc, Modifier, Vars);
21601 }
21602 
21603 OMPClause *Sema::ActOnOpenMPBindClause(OpenMPBindClauseKind Kind,
21604                                        SourceLocation KindLoc,
21605                                        SourceLocation StartLoc,
21606                                        SourceLocation LParenLoc,
21607                                        SourceLocation EndLoc) {
21608   if (Kind == OMPC_BIND_unknown) {
21609     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
21610         << getListOfPossibleValues(OMPC_bind, /*First=*/0,
21611                                    /*Last=*/unsigned(OMPC_BIND_unknown))
21612         << getOpenMPClauseName(OMPC_bind);
21613     return nullptr;
21614   }
21615 
21616   return OMPBindClause::Create(Context, Kind, KindLoc, StartLoc, LParenLoc,
21617                                EndLoc);
21618 }
21619