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() {
358     getTopOfStack().BodyComplete = true;
359   }
360 
361   bool isForceVarCapturing() const { return ForceCapturing; }
362   void setForceVarCapturing(bool V) { ForceCapturing = V; }
363 
364   void setForceCaptureByReferenceInTargetExecutable(bool V) {
365     ForceCaptureByReferenceInTargetExecutable = V;
366   }
367   bool isForceCaptureByReferenceInTargetExecutable() const {
368     return ForceCaptureByReferenceInTargetExecutable;
369   }
370 
371   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
372             Scope *CurScope, SourceLocation Loc) {
373     assert(!IgnoredStackElements &&
374            "cannot change stack while ignoring elements");
375     if (Stack.empty() ||
376         Stack.back().second != CurrentNonCapturingFunctionScope)
377       Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
378     Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
379     Stack.back().first.back().DefaultAttrLoc = Loc;
380   }
381 
382   void pop() {
383     assert(!IgnoredStackElements &&
384            "cannot change stack while ignoring elements");
385     assert(!Stack.back().first.empty() &&
386            "Data-sharing attributes stack is empty!");
387     Stack.back().first.pop_back();
388   }
389 
390   /// RAII object to temporarily leave the scope of a directive when we want to
391   /// logically operate in its parent.
392   class ParentDirectiveScope {
393     DSAStackTy &Self;
394     bool Active;
395   public:
396     ParentDirectiveScope(DSAStackTy &Self, bool Activate)
397         : Self(Self), Active(false) {
398       if (Activate)
399         enable();
400     }
401     ~ParentDirectiveScope() { disable(); }
402     void disable() {
403       if (Active) {
404         --Self.IgnoredStackElements;
405         Active = false;
406       }
407     }
408     void enable() {
409       if (!Active) {
410         ++Self.IgnoredStackElements;
411         Active = true;
412       }
413     }
414   };
415 
416   /// Marks that we're started loop parsing.
417   void loopInit() {
418     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
419            "Expected loop-based directive.");
420     getTopOfStack().LoopStart = true;
421   }
422   /// Start capturing of the variables in the loop context.
423   void loopStart() {
424     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
425            "Expected loop-based directive.");
426     getTopOfStack().LoopStart = false;
427   }
428   /// true, if variables are captured, false otherwise.
429   bool isLoopStarted() const {
430     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
431            "Expected loop-based directive.");
432     return !getTopOfStack().LoopStart;
433   }
434   /// Marks (or clears) declaration as possibly loop counter.
435   void resetPossibleLoopCounter(const Decl *D = nullptr) {
436     getTopOfStack().PossiblyLoopCounter =
437         D ? D->getCanonicalDecl() : D;
438   }
439   /// Gets the possible loop counter decl.
440   const Decl *getPossiblyLoopCunter() const {
441     return getTopOfStack().PossiblyLoopCounter;
442   }
443   /// Start new OpenMP region stack in new non-capturing function.
444   void pushFunction() {
445     assert(!IgnoredStackElements &&
446            "cannot change stack while ignoring elements");
447     const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
448     assert(!isa<CapturingScopeInfo>(CurFnScope));
449     CurrentNonCapturingFunctionScope = CurFnScope;
450   }
451   /// Pop region stack for non-capturing function.
452   void popFunction(const FunctionScopeInfo *OldFSI) {
453     assert(!IgnoredStackElements &&
454            "cannot change stack while ignoring elements");
455     if (!Stack.empty() && Stack.back().second == OldFSI) {
456       assert(Stack.back().first.empty());
457       Stack.pop_back();
458     }
459     CurrentNonCapturingFunctionScope = nullptr;
460     for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
461       if (!isa<CapturingScopeInfo>(FSI)) {
462         CurrentNonCapturingFunctionScope = FSI;
463         break;
464       }
465     }
466   }
467 
468   void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
469     Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
470   }
471   const std::pair<const OMPCriticalDirective *, llvm::APSInt>
472   getCriticalWithHint(const DeclarationNameInfo &Name) const {
473     auto I = Criticals.find(Name.getAsString());
474     if (I != Criticals.end())
475       return I->second;
476     return std::make_pair(nullptr, llvm::APSInt());
477   }
478   /// If 'aligned' declaration for given variable \a D was not seen yet,
479   /// add it and return NULL; otherwise return previous occurrence's expression
480   /// for diagnostics.
481   const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
482   /// If 'nontemporal' declaration for given variable \a D was not seen yet,
483   /// add it and return NULL; otherwise return previous occurrence's expression
484   /// for diagnostics.
485   const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE);
486 
487   /// Register specified variable as loop control variable.
488   void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
489   /// Check if the specified variable is a loop control variable for
490   /// current region.
491   /// \return The index of the loop control variable in the list of associated
492   /// for-loops (from outer to inner).
493   const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
494   /// Check if the specified variable is a loop control variable for
495   /// parent region.
496   /// \return The index of the loop control variable in the list of associated
497   /// for-loops (from outer to inner).
498   const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
499   /// Check if the specified variable is a loop control variable for
500   /// current region.
501   /// \return The index of the loop control variable in the list of associated
502   /// for-loops (from outer to inner).
503   const LCDeclInfo isLoopControlVariable(const ValueDecl *D,
504                                          unsigned Level) const;
505   /// Get the loop control variable for the I-th loop (or nullptr) in
506   /// parent directive.
507   const ValueDecl *getParentLoopControlVariable(unsigned I) const;
508 
509   /// Marks the specified decl \p D as used in scan directive.
510   void markDeclAsUsedInScanDirective(ValueDecl *D) {
511     if (SharingMapTy *Stack = getSecondOnStackOrNull())
512       Stack->UsedInScanDirective.insert(D);
513   }
514 
515   /// Checks if the specified declaration was used in the inner scan directive.
516   bool isUsedInScanDirective(ValueDecl *D) const {
517     if (const SharingMapTy *Stack = getTopOfStackOrNull())
518       return Stack->UsedInScanDirective.contains(D);
519     return false;
520   }
521 
522   /// Adds explicit data sharing attribute to the specified declaration.
523   void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
524               DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0,
525               bool AppliedToPointee = false);
526 
527   /// Adds additional information for the reduction items with the reduction id
528   /// represented as an operator.
529   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
530                                  BinaryOperatorKind BOK);
531   /// Adds additional information for the reduction items with the reduction id
532   /// represented as reduction identifier.
533   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
534                                  const Expr *ReductionRef);
535   /// Returns the location and reduction operation from the innermost parent
536   /// region for the given \p D.
537   const DSAVarData
538   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
539                                    BinaryOperatorKind &BOK,
540                                    Expr *&TaskgroupDescriptor) const;
541   /// Returns the location and reduction operation from the innermost parent
542   /// region for the given \p D.
543   const DSAVarData
544   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
545                                    const Expr *&ReductionRef,
546                                    Expr *&TaskgroupDescriptor) const;
547   /// Return reduction reference expression for the current taskgroup or
548   /// parallel/worksharing directives with task reductions.
549   Expr *getTaskgroupReductionRef() const {
550     assert((getTopOfStack().Directive == OMPD_taskgroup ||
551             ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
552               isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
553              !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
554            "taskgroup reference expression requested for non taskgroup or "
555            "parallel/worksharing directive.");
556     return getTopOfStack().TaskgroupReductionRef;
557   }
558   /// Checks if the given \p VD declaration is actually a taskgroup reduction
559   /// descriptor variable at the \p Level of OpenMP regions.
560   bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
561     return getStackElemAtLevel(Level).TaskgroupReductionRef &&
562            cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef)
563                    ->getDecl() == VD;
564   }
565 
566   /// Returns data sharing attributes from top of the stack for the
567   /// specified declaration.
568   const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
569   /// Returns data-sharing attributes for the specified declaration.
570   const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
571   /// Returns data-sharing attributes for the specified declaration.
572   const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const;
573   /// Checks if the specified variables has data-sharing attributes which
574   /// match specified \a CPred predicate in any directive which matches \a DPred
575   /// predicate.
576   const DSAVarData
577   hasDSA(ValueDecl *D,
578          const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
579          const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
580          bool FromParent) const;
581   /// Checks if the specified variables has data-sharing attributes which
582   /// match specified \a CPred predicate in any innermost directive which
583   /// matches \a DPred predicate.
584   const DSAVarData
585   hasInnermostDSA(ValueDecl *D,
586                   const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
587                   const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
588                   bool FromParent) const;
589   /// Checks if the specified variables has explicit data-sharing
590   /// attributes which match specified \a CPred predicate at the specified
591   /// OpenMP region.
592   bool
593   hasExplicitDSA(const ValueDecl *D,
594                  const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
595                  unsigned Level, bool NotLastprivate = false) const;
596 
597   /// Returns true if the directive at level \Level matches in the
598   /// specified \a DPred predicate.
599   bool hasExplicitDirective(
600       const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
601       unsigned Level) const;
602 
603   /// Finds a directive which matches specified \a DPred predicate.
604   bool hasDirective(
605       const llvm::function_ref<bool(
606           OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
607           DPred,
608       bool FromParent) const;
609 
610   /// Returns currently analyzed directive.
611   OpenMPDirectiveKind getCurrentDirective() const {
612     const SharingMapTy *Top = getTopOfStackOrNull();
613     return Top ? Top->Directive : OMPD_unknown;
614   }
615   /// Returns directive kind at specified level.
616   OpenMPDirectiveKind getDirective(unsigned Level) const {
617     assert(!isStackEmpty() && "No directive at specified level.");
618     return getStackElemAtLevel(Level).Directive;
619   }
620   /// Returns the capture region at the specified level.
621   OpenMPDirectiveKind getCaptureRegion(unsigned Level,
622                                        unsigned OpenMPCaptureLevel) const {
623     SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
624     getOpenMPCaptureRegions(CaptureRegions, getDirective(Level));
625     return CaptureRegions[OpenMPCaptureLevel];
626   }
627   /// Returns parent directive.
628   OpenMPDirectiveKind getParentDirective() const {
629     const SharingMapTy *Parent = getSecondOnStackOrNull();
630     return Parent ? Parent->Directive : OMPD_unknown;
631   }
632 
633   /// Add requires decl to internal vector
634   void addRequiresDecl(OMPRequiresDecl *RD) {
635     RequiresDecls.push_back(RD);
636   }
637 
638   /// Checks if the defined 'requires' directive has specified type of clause.
639   template <typename ClauseType>
640   bool hasRequiresDeclWithClause() const {
641     return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
642       return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
643         return isa<ClauseType>(C);
644       });
645     });
646   }
647 
648   /// Checks for a duplicate clause amongst previously declared requires
649   /// directives
650   bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
651     bool IsDuplicate = false;
652     for (OMPClause *CNew : ClauseList) {
653       for (const OMPRequiresDecl *D : RequiresDecls) {
654         for (const OMPClause *CPrev : D->clauselists()) {
655           if (CNew->getClauseKind() == CPrev->getClauseKind()) {
656             SemaRef.Diag(CNew->getBeginLoc(),
657                          diag::err_omp_requires_clause_redeclaration)
658                 << getOpenMPClauseName(CNew->getClauseKind());
659             SemaRef.Diag(CPrev->getBeginLoc(),
660                          diag::note_omp_requires_previous_clause)
661                 << getOpenMPClauseName(CPrev->getClauseKind());
662             IsDuplicate = true;
663           }
664         }
665       }
666     }
667     return IsDuplicate;
668   }
669 
670   /// Add location of previously encountered target to internal vector
671   void addTargetDirLocation(SourceLocation LocStart) {
672     TargetLocations.push_back(LocStart);
673   }
674 
675   /// Add location for the first encountered atomicc directive.
676   void addAtomicDirectiveLoc(SourceLocation Loc) {
677     if (AtomicLocation.isInvalid())
678       AtomicLocation = Loc;
679   }
680 
681   /// Returns the location of the first encountered atomic directive in the
682   /// module.
683   SourceLocation getAtomicDirectiveLoc() const {
684     return AtomicLocation;
685   }
686 
687   // Return previously encountered target region locations.
688   ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
689     return TargetLocations;
690   }
691 
692   /// Set default data sharing attribute to none.
693   void setDefaultDSANone(SourceLocation Loc) {
694     getTopOfStack().DefaultAttr = DSA_none;
695     getTopOfStack().DefaultAttrLoc = Loc;
696   }
697   /// Set default data sharing attribute to shared.
698   void setDefaultDSAShared(SourceLocation Loc) {
699     getTopOfStack().DefaultAttr = DSA_shared;
700     getTopOfStack().DefaultAttrLoc = Loc;
701   }
702   /// Set default data sharing attribute to firstprivate.
703   void setDefaultDSAFirstPrivate(SourceLocation Loc) {
704     getTopOfStack().DefaultAttr = DSA_firstprivate;
705     getTopOfStack().DefaultAttrLoc = Loc;
706   }
707   /// Set default data mapping attribute to Modifier:Kind
708   void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M,
709                          OpenMPDefaultmapClauseKind Kind,
710                          SourceLocation Loc) {
711     DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind];
712     DMI.ImplicitBehavior = M;
713     DMI.SLoc = Loc;
714   }
715   /// Check whether the implicit-behavior has been set in defaultmap
716   bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) {
717     if (VariableCategory == OMPC_DEFAULTMAP_unknown)
718       return getTopOfStack()
719                      .DefaultmapMap[OMPC_DEFAULTMAP_aggregate]
720                      .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
721              getTopOfStack()
722                      .DefaultmapMap[OMPC_DEFAULTMAP_scalar]
723                      .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
724              getTopOfStack()
725                      .DefaultmapMap[OMPC_DEFAULTMAP_pointer]
726                      .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown;
727     return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior !=
728            OMPC_DEFAULTMAP_MODIFIER_unknown;
729   }
730 
731   ArrayRef<llvm::omp::TraitProperty> getConstructTraits() {
732     return ConstructTraits;
733   }
734   void handleConstructTrait(ArrayRef<llvm::omp::TraitProperty> Traits,
735                             bool ScopeEntry) {
736     if (ScopeEntry)
737       ConstructTraits.append(Traits.begin(), Traits.end());
738     else
739       for (llvm::omp::TraitProperty Trait : llvm::reverse(Traits)) {
740         llvm::omp::TraitProperty Top = ConstructTraits.pop_back_val();
741         assert(Top == Trait && "Something left a trait on the stack!");
742         (void)Trait;
743         (void)Top;
744       }
745   }
746 
747   DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const {
748     return getStackSize() <= Level ? DSA_unspecified
749                                    : getStackElemAtLevel(Level).DefaultAttr;
750   }
751   DefaultDataSharingAttributes getDefaultDSA() const {
752     return isStackEmpty() ? DSA_unspecified
753                           : getTopOfStack().DefaultAttr;
754   }
755   SourceLocation getDefaultDSALocation() const {
756     return isStackEmpty() ? SourceLocation()
757                           : getTopOfStack().DefaultAttrLoc;
758   }
759   OpenMPDefaultmapClauseModifier
760   getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const {
761     return isStackEmpty()
762                ? OMPC_DEFAULTMAP_MODIFIER_unknown
763                : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior;
764   }
765   OpenMPDefaultmapClauseModifier
766   getDefaultmapModifierAtLevel(unsigned Level,
767                                OpenMPDefaultmapClauseKind Kind) const {
768     return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior;
769   }
770   bool isDefaultmapCapturedByRef(unsigned Level,
771                                  OpenMPDefaultmapClauseKind Kind) const {
772     OpenMPDefaultmapClauseModifier M =
773         getDefaultmapModifierAtLevel(Level, Kind);
774     if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) {
775       return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) ||
776              (M == OMPC_DEFAULTMAP_MODIFIER_to) ||
777              (M == OMPC_DEFAULTMAP_MODIFIER_from) ||
778              (M == OMPC_DEFAULTMAP_MODIFIER_tofrom);
779     }
780     return true;
781   }
782   static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M,
783                                      OpenMPDefaultmapClauseKind Kind) {
784     switch (Kind) {
785     case OMPC_DEFAULTMAP_scalar:
786     case OMPC_DEFAULTMAP_pointer:
787       return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) ||
788              (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) ||
789              (M == OMPC_DEFAULTMAP_MODIFIER_default);
790     case OMPC_DEFAULTMAP_aggregate:
791       return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate;
792     default:
793       break;
794     }
795     llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum");
796   }
797   bool mustBeFirstprivateAtLevel(unsigned Level,
798                                  OpenMPDefaultmapClauseKind Kind) const {
799     OpenMPDefaultmapClauseModifier M =
800         getDefaultmapModifierAtLevel(Level, Kind);
801     return mustBeFirstprivateBase(M, Kind);
802   }
803   bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const {
804     OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind);
805     return mustBeFirstprivateBase(M, Kind);
806   }
807 
808   /// Checks if the specified variable is a threadprivate.
809   bool isThreadPrivate(VarDecl *D) {
810     const DSAVarData DVar = getTopDSA(D, false);
811     return isOpenMPThreadPrivate(DVar.CKind);
812   }
813 
814   /// Marks current region as ordered (it has an 'ordered' clause).
815   void setOrderedRegion(bool IsOrdered, const Expr *Param,
816                         OMPOrderedClause *Clause) {
817     if (IsOrdered)
818       getTopOfStack().OrderedRegion.emplace(Param, Clause);
819     else
820       getTopOfStack().OrderedRegion.reset();
821   }
822   /// Returns true, if region is ordered (has associated 'ordered' clause),
823   /// false - otherwise.
824   bool isOrderedRegion() const {
825     if (const SharingMapTy *Top = getTopOfStackOrNull())
826       return Top->OrderedRegion.hasValue();
827     return false;
828   }
829   /// Returns optional parameter for the ordered region.
830   std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
831     if (const SharingMapTy *Top = getTopOfStackOrNull())
832       if (Top->OrderedRegion.hasValue())
833         return Top->OrderedRegion.getValue();
834     return std::make_pair(nullptr, nullptr);
835   }
836   /// Returns true, if parent region is ordered (has associated
837   /// 'ordered' clause), false - otherwise.
838   bool isParentOrderedRegion() const {
839     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
840       return Parent->OrderedRegion.hasValue();
841     return false;
842   }
843   /// Returns optional parameter for the ordered region.
844   std::pair<const Expr *, OMPOrderedClause *>
845   getParentOrderedRegionParam() const {
846     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
847       if (Parent->OrderedRegion.hasValue())
848         return Parent->OrderedRegion.getValue();
849     return std::make_pair(nullptr, nullptr);
850   }
851   /// Marks current region as nowait (it has a 'nowait' clause).
852   void setNowaitRegion(bool IsNowait = true) {
853     getTopOfStack().NowaitRegion = IsNowait;
854   }
855   /// Returns true, if parent region is nowait (has associated
856   /// 'nowait' clause), false - otherwise.
857   bool isParentNowaitRegion() const {
858     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
859       return Parent->NowaitRegion;
860     return false;
861   }
862   /// Marks parent region as cancel region.
863   void setParentCancelRegion(bool Cancel = true) {
864     if (SharingMapTy *Parent = getSecondOnStackOrNull())
865       Parent->CancelRegion |= Cancel;
866   }
867   /// Return true if current region has inner cancel construct.
868   bool isCancelRegion() const {
869     const SharingMapTy *Top = getTopOfStackOrNull();
870     return Top ? Top->CancelRegion : false;
871   }
872 
873   /// Mark that parent region already has scan directive.
874   void setParentHasScanDirective(SourceLocation Loc) {
875     if (SharingMapTy *Parent = getSecondOnStackOrNull())
876       Parent->PrevScanLocation = Loc;
877   }
878   /// Return true if current region has inner cancel construct.
879   bool doesParentHasScanDirective() const {
880     const SharingMapTy *Top = getSecondOnStackOrNull();
881     return Top ? Top->PrevScanLocation.isValid() : false;
882   }
883   /// Return true if current region has inner cancel construct.
884   SourceLocation getParentScanDirectiveLoc() const {
885     const SharingMapTy *Top = getSecondOnStackOrNull();
886     return Top ? Top->PrevScanLocation : SourceLocation();
887   }
888   /// Mark that parent region already has ordered directive.
889   void setParentHasOrderedDirective(SourceLocation Loc) {
890     if (SharingMapTy *Parent = getSecondOnStackOrNull())
891       Parent->PrevOrderedLocation = Loc;
892   }
893   /// Return true if current region has inner ordered construct.
894   bool doesParentHasOrderedDirective() const {
895     const SharingMapTy *Top = getSecondOnStackOrNull();
896     return Top ? Top->PrevOrderedLocation.isValid() : false;
897   }
898   /// Returns the location of the previously specified ordered directive.
899   SourceLocation getParentOrderedDirectiveLoc() const {
900     const SharingMapTy *Top = getSecondOnStackOrNull();
901     return Top ? Top->PrevOrderedLocation : SourceLocation();
902   }
903 
904   /// Set collapse value for the region.
905   void setAssociatedLoops(unsigned Val) {
906     getTopOfStack().AssociatedLoops = Val;
907     if (Val > 1)
908       getTopOfStack().HasMutipleLoops = true;
909   }
910   /// Return collapse value for region.
911   unsigned getAssociatedLoops() const {
912     const SharingMapTy *Top = getTopOfStackOrNull();
913     return Top ? Top->AssociatedLoops : 0;
914   }
915   /// Returns true if the construct is associated with multiple loops.
916   bool hasMutipleLoops() const {
917     const SharingMapTy *Top = getTopOfStackOrNull();
918     return Top ? Top->HasMutipleLoops : false;
919   }
920 
921   /// Marks current target region as one with closely nested teams
922   /// region.
923   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
924     if (SharingMapTy *Parent = getSecondOnStackOrNull())
925       Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
926   }
927   /// Returns true, if current region has closely nested teams region.
928   bool hasInnerTeamsRegion() const {
929     return getInnerTeamsRegionLoc().isValid();
930   }
931   /// Returns location of the nested teams region (if any).
932   SourceLocation getInnerTeamsRegionLoc() const {
933     const SharingMapTy *Top = getTopOfStackOrNull();
934     return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
935   }
936 
937   Scope *getCurScope() const {
938     const SharingMapTy *Top = getTopOfStackOrNull();
939     return Top ? Top->CurScope : nullptr;
940   }
941   void setContext(DeclContext *DC) { getTopOfStack().Context = DC; }
942   SourceLocation getConstructLoc() const {
943     const SharingMapTy *Top = getTopOfStackOrNull();
944     return Top ? Top->ConstructLoc : SourceLocation();
945   }
946 
947   /// Do the check specified in \a Check to all component lists and return true
948   /// if any issue is found.
949   bool checkMappableExprComponentListsForDecl(
950       const ValueDecl *VD, bool CurrentRegionOnly,
951       const llvm::function_ref<
952           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
953                OpenMPClauseKind)>
954           Check) const {
955     if (isStackEmpty())
956       return false;
957     auto SI = begin();
958     auto SE = end();
959 
960     if (SI == SE)
961       return false;
962 
963     if (CurrentRegionOnly)
964       SE = std::next(SI);
965     else
966       std::advance(SI, 1);
967 
968     for (; SI != SE; ++SI) {
969       auto MI = SI->MappedExprComponents.find(VD);
970       if (MI != SI->MappedExprComponents.end())
971         for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
972              MI->second.Components)
973           if (Check(L, MI->second.Kind))
974             return true;
975     }
976     return false;
977   }
978 
979   /// Do the check specified in \a Check to all component lists at a given level
980   /// and return true if any issue is found.
981   bool checkMappableExprComponentListsForDeclAtLevel(
982       const ValueDecl *VD, unsigned Level,
983       const llvm::function_ref<
984           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
985                OpenMPClauseKind)>
986           Check) const {
987     if (getStackSize() <= Level)
988       return false;
989 
990     const SharingMapTy &StackElem = getStackElemAtLevel(Level);
991     auto MI = StackElem.MappedExprComponents.find(VD);
992     if (MI != StackElem.MappedExprComponents.end())
993       for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
994            MI->second.Components)
995         if (Check(L, MI->second.Kind))
996           return true;
997     return false;
998   }
999 
1000   /// Create a new mappable expression component list associated with a given
1001   /// declaration and initialize it with the provided list of components.
1002   void addMappableExpressionComponents(
1003       const ValueDecl *VD,
1004       OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
1005       OpenMPClauseKind WhereFoundClauseKind) {
1006     MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
1007     // Create new entry and append the new components there.
1008     MEC.Components.resize(MEC.Components.size() + 1);
1009     MEC.Components.back().append(Components.begin(), Components.end());
1010     MEC.Kind = WhereFoundClauseKind;
1011   }
1012 
1013   unsigned getNestingLevel() const {
1014     assert(!isStackEmpty());
1015     return getStackSize() - 1;
1016   }
1017   void addDoacrossDependClause(OMPDependClause *C,
1018                                const OperatorOffsetTy &OpsOffs) {
1019     SharingMapTy *Parent = getSecondOnStackOrNull();
1020     assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
1021     Parent->DoacrossDepends.try_emplace(C, OpsOffs);
1022   }
1023   llvm::iterator_range<DoacrossDependMapTy::const_iterator>
1024   getDoacrossDependClauses() const {
1025     const SharingMapTy &StackElem = getTopOfStack();
1026     if (isOpenMPWorksharingDirective(StackElem.Directive)) {
1027       const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
1028       return llvm::make_range(Ref.begin(), Ref.end());
1029     }
1030     return llvm::make_range(StackElem.DoacrossDepends.end(),
1031                             StackElem.DoacrossDepends.end());
1032   }
1033 
1034   // Store types of classes which have been explicitly mapped
1035   void addMappedClassesQualTypes(QualType QT) {
1036     SharingMapTy &StackElem = getTopOfStack();
1037     StackElem.MappedClassesQualTypes.insert(QT);
1038   }
1039 
1040   // Return set of mapped classes types
1041   bool isClassPreviouslyMapped(QualType QT) const {
1042     const SharingMapTy &StackElem = getTopOfStack();
1043     return StackElem.MappedClassesQualTypes.contains(QT);
1044   }
1045 
1046   /// Adds global declare target to the parent target region.
1047   void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
1048     assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1049                E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
1050            "Expected declare target link global.");
1051     for (auto &Elem : *this) {
1052       if (isOpenMPTargetExecutionDirective(Elem.Directive)) {
1053         Elem.DeclareTargetLinkVarDecls.push_back(E);
1054         return;
1055       }
1056     }
1057   }
1058 
1059   /// Returns the list of globals with declare target link if current directive
1060   /// is target.
1061   ArrayRef<DeclRefExpr *> getLinkGlobals() const {
1062     assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
1063            "Expected target executable directive.");
1064     return getTopOfStack().DeclareTargetLinkVarDecls;
1065   }
1066 
1067   /// Adds list of allocators expressions.
1068   void addInnerAllocatorExpr(Expr *E) {
1069     getTopOfStack().InnerUsedAllocators.push_back(E);
1070   }
1071   /// Return list of used allocators.
1072   ArrayRef<Expr *> getInnerAllocators() const {
1073     return getTopOfStack().InnerUsedAllocators;
1074   }
1075   /// Marks the declaration as implicitly firstprivate nin the task-based
1076   /// regions.
1077   void addImplicitTaskFirstprivate(unsigned Level, Decl *D) {
1078     getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D);
1079   }
1080   /// Checks if the decl is implicitly firstprivate in the task-based region.
1081   bool isImplicitTaskFirstprivate(Decl *D) const {
1082     return getTopOfStack().ImplicitTaskFirstprivates.contains(D);
1083   }
1084 
1085   /// Marks decl as used in uses_allocators clause as the allocator.
1086   void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) {
1087     getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind);
1088   }
1089   /// Checks if specified decl is used in uses allocator clause as the
1090   /// allocator.
1091   Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level,
1092                                                         const Decl *D) const {
1093     const SharingMapTy &StackElem = getTopOfStack();
1094     auto I = StackElem.UsesAllocatorsDecls.find(D);
1095     if (I == StackElem.UsesAllocatorsDecls.end())
1096       return None;
1097     return I->getSecond();
1098   }
1099   Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const {
1100     const SharingMapTy &StackElem = getTopOfStack();
1101     auto I = StackElem.UsesAllocatorsDecls.find(D);
1102     if (I == StackElem.UsesAllocatorsDecls.end())
1103       return None;
1104     return I->getSecond();
1105   }
1106 
1107   void addDeclareMapperVarRef(Expr *Ref) {
1108     SharingMapTy &StackElem = getTopOfStack();
1109     StackElem.DeclareMapperVar = Ref;
1110   }
1111   const Expr *getDeclareMapperVarRef() const {
1112     const SharingMapTy *Top = getTopOfStackOrNull();
1113     return Top ? Top->DeclareMapperVar : nullptr;
1114   }
1115 };
1116 
1117 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1118   return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
1119 }
1120 
1121 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1122   return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) ||
1123          DKind == OMPD_unknown;
1124 }
1125 
1126 } // namespace
1127 
1128 static const Expr *getExprAsWritten(const Expr *E) {
1129   if (const auto *FE = dyn_cast<FullExpr>(E))
1130     E = FE->getSubExpr();
1131 
1132   if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
1133     E = MTE->getSubExpr();
1134 
1135   while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
1136     E = Binder->getSubExpr();
1137 
1138   if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
1139     E = ICE->getSubExprAsWritten();
1140   return E->IgnoreParens();
1141 }
1142 
1143 static Expr *getExprAsWritten(Expr *E) {
1144   return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
1145 }
1146 
1147 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
1148   if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
1149     if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
1150       D = ME->getMemberDecl();
1151   const auto *VD = dyn_cast<VarDecl>(D);
1152   const auto *FD = dyn_cast<FieldDecl>(D);
1153   if (VD != nullptr) {
1154     VD = VD->getCanonicalDecl();
1155     D = VD;
1156   } else {
1157     assert(FD);
1158     FD = FD->getCanonicalDecl();
1159     D = FD;
1160   }
1161   return D;
1162 }
1163 
1164 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
1165   return const_cast<ValueDecl *>(
1166       getCanonicalDecl(const_cast<const ValueDecl *>(D)));
1167 }
1168 
1169 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
1170                                           ValueDecl *D) const {
1171   D = getCanonicalDecl(D);
1172   auto *VD = dyn_cast<VarDecl>(D);
1173   const auto *FD = dyn_cast<FieldDecl>(D);
1174   DSAVarData DVar;
1175   if (Iter == end()) {
1176     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1177     // in a region but not in construct]
1178     //  File-scope or namespace-scope variables referenced in called routines
1179     //  in the region are shared unless they appear in a threadprivate
1180     //  directive.
1181     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
1182       DVar.CKind = OMPC_shared;
1183 
1184     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
1185     // in a region but not in construct]
1186     //  Variables with static storage duration that are declared in called
1187     //  routines in the region are shared.
1188     if (VD && VD->hasGlobalStorage())
1189       DVar.CKind = OMPC_shared;
1190 
1191     // Non-static data members are shared by default.
1192     if (FD)
1193       DVar.CKind = OMPC_shared;
1194 
1195     return DVar;
1196   }
1197 
1198   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1199   // in a Construct, C/C++, predetermined, p.1]
1200   // Variables with automatic storage duration that are declared in a scope
1201   // inside the construct are private.
1202   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
1203       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
1204     DVar.CKind = OMPC_private;
1205     return DVar;
1206   }
1207 
1208   DVar.DKind = Iter->Directive;
1209   // Explicitly specified attributes and local variables with predetermined
1210   // attributes.
1211   if (Iter->SharingMap.count(D)) {
1212     const DSAInfo &Data = Iter->SharingMap.lookup(D);
1213     DVar.RefExpr = Data.RefExpr.getPointer();
1214     DVar.PrivateCopy = Data.PrivateCopy;
1215     DVar.CKind = Data.Attributes;
1216     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1217     DVar.Modifier = Data.Modifier;
1218     DVar.AppliedToPointee = Data.AppliedToPointee;
1219     return DVar;
1220   }
1221 
1222   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1223   // in a Construct, C/C++, implicitly determined, p.1]
1224   //  In a parallel or task construct, the data-sharing attributes of these
1225   //  variables are determined by the default clause, if present.
1226   switch (Iter->DefaultAttr) {
1227   case DSA_shared:
1228     DVar.CKind = OMPC_shared;
1229     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1230     return DVar;
1231   case DSA_none:
1232     return DVar;
1233   case DSA_firstprivate:
1234     if (VD->getStorageDuration() == SD_Static &&
1235         VD->getDeclContext()->isFileContext()) {
1236       DVar.CKind = OMPC_unknown;
1237     } else {
1238       DVar.CKind = OMPC_firstprivate;
1239     }
1240     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1241     return DVar;
1242   case DSA_unspecified:
1243     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1244     // in a Construct, implicitly determined, p.2]
1245     //  In a parallel construct, if no default clause is present, these
1246     //  variables are shared.
1247     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1248     if ((isOpenMPParallelDirective(DVar.DKind) &&
1249          !isOpenMPTaskLoopDirective(DVar.DKind)) ||
1250         isOpenMPTeamsDirective(DVar.DKind)) {
1251       DVar.CKind = OMPC_shared;
1252       return DVar;
1253     }
1254 
1255     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1256     // in a Construct, implicitly determined, p.4]
1257     //  In a task construct, if no default clause is present, a variable that in
1258     //  the enclosing context is determined to be shared by all implicit tasks
1259     //  bound to the current team is shared.
1260     if (isOpenMPTaskingDirective(DVar.DKind)) {
1261       DSAVarData DVarTemp;
1262       const_iterator I = Iter, E = end();
1263       do {
1264         ++I;
1265         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
1266         // Referenced in a Construct, implicitly determined, p.6]
1267         //  In a task construct, if no default clause is present, a variable
1268         //  whose data-sharing attribute is not determined by the rules above is
1269         //  firstprivate.
1270         DVarTemp = getDSA(I, D);
1271         if (DVarTemp.CKind != OMPC_shared) {
1272           DVar.RefExpr = nullptr;
1273           DVar.CKind = OMPC_firstprivate;
1274           return DVar;
1275         }
1276       } while (I != E && !isImplicitTaskingRegion(I->Directive));
1277       DVar.CKind =
1278           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
1279       return DVar;
1280     }
1281   }
1282   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1283   // in a Construct, implicitly determined, p.3]
1284   //  For constructs other than task, if no default clause is present, these
1285   //  variables inherit their data-sharing attributes from the enclosing
1286   //  context.
1287   return getDSA(++Iter, D);
1288 }
1289 
1290 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1291                                          const Expr *NewDE) {
1292   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1293   D = getCanonicalDecl(D);
1294   SharingMapTy &StackElem = getTopOfStack();
1295   auto It = StackElem.AlignedMap.find(D);
1296   if (It == StackElem.AlignedMap.end()) {
1297     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1298     StackElem.AlignedMap[D] = NewDE;
1299     return nullptr;
1300   }
1301   assert(It->second && "Unexpected nullptr expr in the aligned map");
1302   return It->second;
1303 }
1304 
1305 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D,
1306                                              const Expr *NewDE) {
1307   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1308   D = getCanonicalDecl(D);
1309   SharingMapTy &StackElem = getTopOfStack();
1310   auto It = StackElem.NontemporalMap.find(D);
1311   if (It == StackElem.NontemporalMap.end()) {
1312     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1313     StackElem.NontemporalMap[D] = NewDE;
1314     return nullptr;
1315   }
1316   assert(It->second && "Unexpected nullptr expr in the aligned map");
1317   return It->second;
1318 }
1319 
1320 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1321   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1322   D = getCanonicalDecl(D);
1323   SharingMapTy &StackElem = getTopOfStack();
1324   StackElem.LCVMap.try_emplace(
1325       D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1326 }
1327 
1328 const DSAStackTy::LCDeclInfo
1329 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1330   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1331   D = getCanonicalDecl(D);
1332   const SharingMapTy &StackElem = getTopOfStack();
1333   auto It = StackElem.LCVMap.find(D);
1334   if (It != StackElem.LCVMap.end())
1335     return It->second;
1336   return {0, nullptr};
1337 }
1338 
1339 const DSAStackTy::LCDeclInfo
1340 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const {
1341   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1342   D = getCanonicalDecl(D);
1343   for (unsigned I = Level + 1; I > 0; --I) {
1344     const SharingMapTy &StackElem = getStackElemAtLevel(I - 1);
1345     auto It = StackElem.LCVMap.find(D);
1346     if (It != StackElem.LCVMap.end())
1347       return It->second;
1348   }
1349   return {0, nullptr};
1350 }
1351 
1352 const DSAStackTy::LCDeclInfo
1353 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1354   const SharingMapTy *Parent = getSecondOnStackOrNull();
1355   assert(Parent && "Data-sharing attributes stack is empty");
1356   D = getCanonicalDecl(D);
1357   auto It = Parent->LCVMap.find(D);
1358   if (It != Parent->LCVMap.end())
1359     return It->second;
1360   return {0, nullptr};
1361 }
1362 
1363 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1364   const SharingMapTy *Parent = getSecondOnStackOrNull();
1365   assert(Parent && "Data-sharing attributes stack is empty");
1366   if (Parent->LCVMap.size() < I)
1367     return nullptr;
1368   for (const auto &Pair : Parent->LCVMap)
1369     if (Pair.second.first == I)
1370       return Pair.first;
1371   return nullptr;
1372 }
1373 
1374 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1375                         DeclRefExpr *PrivateCopy, unsigned Modifier,
1376                         bool AppliedToPointee) {
1377   D = getCanonicalDecl(D);
1378   if (A == OMPC_threadprivate) {
1379     DSAInfo &Data = Threadprivates[D];
1380     Data.Attributes = A;
1381     Data.RefExpr.setPointer(E);
1382     Data.PrivateCopy = nullptr;
1383     Data.Modifier = Modifier;
1384   } else {
1385     DSAInfo &Data = getTopOfStack().SharingMap[D];
1386     assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1387            (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1388            (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1389            (isLoopControlVariable(D).first && A == OMPC_private));
1390     Data.Modifier = Modifier;
1391     if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1392       Data.RefExpr.setInt(/*IntVal=*/true);
1393       return;
1394     }
1395     const bool IsLastprivate =
1396         A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1397     Data.Attributes = A;
1398     Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1399     Data.PrivateCopy = PrivateCopy;
1400     Data.AppliedToPointee = AppliedToPointee;
1401     if (PrivateCopy) {
1402       DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1403       Data.Modifier = Modifier;
1404       Data.Attributes = A;
1405       Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1406       Data.PrivateCopy = nullptr;
1407       Data.AppliedToPointee = AppliedToPointee;
1408     }
1409   }
1410 }
1411 
1412 /// Build a variable declaration for OpenMP loop iteration variable.
1413 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1414                              StringRef Name, const AttrVec *Attrs = nullptr,
1415                              DeclRefExpr *OrigRef = nullptr) {
1416   DeclContext *DC = SemaRef.CurContext;
1417   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1418   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
1419   auto *Decl =
1420       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1421   if (Attrs) {
1422     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1423          I != E; ++I)
1424       Decl->addAttr(*I);
1425   }
1426   Decl->setImplicit();
1427   if (OrigRef) {
1428     Decl->addAttr(
1429         OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1430   }
1431   return Decl;
1432 }
1433 
1434 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1435                                      SourceLocation Loc,
1436                                      bool RefersToCapture = false) {
1437   D->setReferenced();
1438   D->markUsed(S.Context);
1439   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1440                              SourceLocation(), D, RefersToCapture, Loc, Ty,
1441                              VK_LValue);
1442 }
1443 
1444 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1445                                            BinaryOperatorKind BOK) {
1446   D = getCanonicalDecl(D);
1447   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1448   assert(
1449       getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1450       "Additional reduction info may be specified only for reduction items.");
1451   ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1452   assert(ReductionData.ReductionRange.isInvalid() &&
1453          (getTopOfStack().Directive == OMPD_taskgroup ||
1454           ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1455             isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1456            !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1457          "Additional reduction info may be specified only once for reduction "
1458          "items.");
1459   ReductionData.set(BOK, SR);
1460   Expr *&TaskgroupReductionRef =
1461       getTopOfStack().TaskgroupReductionRef;
1462   if (!TaskgroupReductionRef) {
1463     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1464                                SemaRef.Context.VoidPtrTy, ".task_red.");
1465     TaskgroupReductionRef =
1466         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1467   }
1468 }
1469 
1470 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1471                                            const Expr *ReductionRef) {
1472   D = getCanonicalDecl(D);
1473   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1474   assert(
1475       getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1476       "Additional reduction info may be specified only for reduction items.");
1477   ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1478   assert(ReductionData.ReductionRange.isInvalid() &&
1479          (getTopOfStack().Directive == OMPD_taskgroup ||
1480           ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1481             isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1482            !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1483          "Additional reduction info may be specified only once for reduction "
1484          "items.");
1485   ReductionData.set(ReductionRef, SR);
1486   Expr *&TaskgroupReductionRef =
1487       getTopOfStack().TaskgroupReductionRef;
1488   if (!TaskgroupReductionRef) {
1489     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1490                                SemaRef.Context.VoidPtrTy, ".task_red.");
1491     TaskgroupReductionRef =
1492         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1493   }
1494 }
1495 
1496 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1497     const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1498     Expr *&TaskgroupDescriptor) const {
1499   D = getCanonicalDecl(D);
1500   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1501   for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1502     const DSAInfo &Data = I->SharingMap.lookup(D);
1503     if (Data.Attributes != OMPC_reduction ||
1504         Data.Modifier != OMPC_REDUCTION_task)
1505       continue;
1506     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1507     if (!ReductionData.ReductionOp ||
1508         ReductionData.ReductionOp.is<const Expr *>())
1509       return DSAVarData();
1510     SR = ReductionData.ReductionRange;
1511     BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1512     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1513                                        "expression for the descriptor is not "
1514                                        "set.");
1515     TaskgroupDescriptor = I->TaskgroupReductionRef;
1516     return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1517                       Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1518                       /*AppliedToPointee=*/false);
1519   }
1520   return DSAVarData();
1521 }
1522 
1523 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1524     const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1525     Expr *&TaskgroupDescriptor) const {
1526   D = getCanonicalDecl(D);
1527   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1528   for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1529     const DSAInfo &Data = I->SharingMap.lookup(D);
1530     if (Data.Attributes != OMPC_reduction ||
1531         Data.Modifier != OMPC_REDUCTION_task)
1532       continue;
1533     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1534     if (!ReductionData.ReductionOp ||
1535         !ReductionData.ReductionOp.is<const Expr *>())
1536       return DSAVarData();
1537     SR = ReductionData.ReductionRange;
1538     ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1539     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1540                                        "expression for the descriptor is not "
1541                                        "set.");
1542     TaskgroupDescriptor = I->TaskgroupReductionRef;
1543     return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1544                       Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1545                       /*AppliedToPointee=*/false);
1546   }
1547   return DSAVarData();
1548 }
1549 
1550 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1551   D = D->getCanonicalDecl();
1552   for (const_iterator E = end(); I != E; ++I) {
1553     if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1554         isOpenMPTargetExecutionDirective(I->Directive)) {
1555       if (I->CurScope) {
1556         Scope *TopScope = I->CurScope->getParent();
1557         Scope *CurScope = getCurScope();
1558         while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1559           CurScope = CurScope->getParent();
1560         return CurScope != TopScope;
1561       }
1562       for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent())
1563         if (I->Context == DC)
1564           return true;
1565       return false;
1566     }
1567   }
1568   return false;
1569 }
1570 
1571 static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1572                                   bool AcceptIfMutable = true,
1573                                   bool *IsClassType = nullptr) {
1574   ASTContext &Context = SemaRef.getASTContext();
1575   Type = Type.getNonReferenceType().getCanonicalType();
1576   bool IsConstant = Type.isConstant(Context);
1577   Type = Context.getBaseElementType(Type);
1578   const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1579                                 ? Type->getAsCXXRecordDecl()
1580                                 : nullptr;
1581   if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1582     if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1583       RD = CTD->getTemplatedDecl();
1584   if (IsClassType)
1585     *IsClassType = RD;
1586   return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1587                          RD->hasDefinition() && RD->hasMutableFields());
1588 }
1589 
1590 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1591                                       QualType Type, OpenMPClauseKind CKind,
1592                                       SourceLocation ELoc,
1593                                       bool AcceptIfMutable = true,
1594                                       bool ListItemNotVar = false) {
1595   ASTContext &Context = SemaRef.getASTContext();
1596   bool IsClassType;
1597   if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1598     unsigned Diag = ListItemNotVar
1599                         ? diag::err_omp_const_list_item
1600                         : IsClassType ? diag::err_omp_const_not_mutable_variable
1601                                       : diag::err_omp_const_variable;
1602     SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1603     if (!ListItemNotVar && D) {
1604       const VarDecl *VD = dyn_cast<VarDecl>(D);
1605       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1606                                VarDecl::DeclarationOnly;
1607       SemaRef.Diag(D->getLocation(),
1608                    IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1609           << D;
1610     }
1611     return true;
1612   }
1613   return false;
1614 }
1615 
1616 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1617                                                    bool FromParent) {
1618   D = getCanonicalDecl(D);
1619   DSAVarData DVar;
1620 
1621   auto *VD = dyn_cast<VarDecl>(D);
1622   auto TI = Threadprivates.find(D);
1623   if (TI != Threadprivates.end()) {
1624     DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1625     DVar.CKind = OMPC_threadprivate;
1626     DVar.Modifier = TI->getSecond().Modifier;
1627     return DVar;
1628   }
1629   if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1630     DVar.RefExpr = buildDeclRefExpr(
1631         SemaRef, VD, D->getType().getNonReferenceType(),
1632         VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1633     DVar.CKind = OMPC_threadprivate;
1634     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1635     return DVar;
1636   }
1637   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1638   // in a Construct, C/C++, predetermined, p.1]
1639   //  Variables appearing in threadprivate directives are threadprivate.
1640   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1641        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1642          SemaRef.getLangOpts().OpenMPUseTLS &&
1643          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1644       (VD && VD->getStorageClass() == SC_Register &&
1645        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1646     DVar.RefExpr = buildDeclRefExpr(
1647         SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1648     DVar.CKind = OMPC_threadprivate;
1649     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1650     return DVar;
1651   }
1652   if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1653       VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1654       !isLoopControlVariable(D).first) {
1655     const_iterator IterTarget =
1656         std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1657           return isOpenMPTargetExecutionDirective(Data.Directive);
1658         });
1659     if (IterTarget != end()) {
1660       const_iterator ParentIterTarget = IterTarget + 1;
1661       for (const_iterator Iter = begin();
1662            Iter != ParentIterTarget; ++Iter) {
1663         if (isOpenMPLocal(VD, Iter)) {
1664           DVar.RefExpr =
1665               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1666                                D->getLocation());
1667           DVar.CKind = OMPC_threadprivate;
1668           return DVar;
1669         }
1670       }
1671       if (!isClauseParsingMode() || IterTarget != begin()) {
1672         auto DSAIter = IterTarget->SharingMap.find(D);
1673         if (DSAIter != IterTarget->SharingMap.end() &&
1674             isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1675           DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1676           DVar.CKind = OMPC_threadprivate;
1677           return DVar;
1678         }
1679         const_iterator End = end();
1680         if (!SemaRef.isOpenMPCapturedByRef(
1681                 D, std::distance(ParentIterTarget, End),
1682                 /*OpenMPCaptureLevel=*/0)) {
1683           DVar.RefExpr =
1684               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1685                                IterTarget->ConstructLoc);
1686           DVar.CKind = OMPC_threadprivate;
1687           return DVar;
1688         }
1689       }
1690     }
1691   }
1692 
1693   if (isStackEmpty())
1694     // Not in OpenMP execution region and top scope was already checked.
1695     return DVar;
1696 
1697   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1698   // in a Construct, C/C++, predetermined, p.4]
1699   //  Static data members are shared.
1700   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1701   // in a Construct, C/C++, predetermined, p.7]
1702   //  Variables with static storage duration that are declared in a scope
1703   //  inside the construct are shared.
1704   if (VD && VD->isStaticDataMember()) {
1705     // Check for explicitly specified attributes.
1706     const_iterator I = begin();
1707     const_iterator EndI = end();
1708     if (FromParent && I != EndI)
1709       ++I;
1710     if (I != EndI) {
1711       auto It = I->SharingMap.find(D);
1712       if (It != I->SharingMap.end()) {
1713         const DSAInfo &Data = It->getSecond();
1714         DVar.RefExpr = Data.RefExpr.getPointer();
1715         DVar.PrivateCopy = Data.PrivateCopy;
1716         DVar.CKind = Data.Attributes;
1717         DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1718         DVar.DKind = I->Directive;
1719         DVar.Modifier = Data.Modifier;
1720         DVar.AppliedToPointee = Data.AppliedToPointee;
1721         return DVar;
1722       }
1723     }
1724 
1725     DVar.CKind = OMPC_shared;
1726     return DVar;
1727   }
1728 
1729   auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1730   // The predetermined shared attribute for const-qualified types having no
1731   // mutable members was removed after OpenMP 3.1.
1732   if (SemaRef.LangOpts.OpenMP <= 31) {
1733     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1734     // in a Construct, C/C++, predetermined, p.6]
1735     //  Variables with const qualified type having no mutable member are
1736     //  shared.
1737     if (isConstNotMutableType(SemaRef, D->getType())) {
1738       // Variables with const-qualified type having no mutable member may be
1739       // listed in a firstprivate clause, even if they are static data members.
1740       DSAVarData DVarTemp = hasInnermostDSA(
1741           D,
1742           [](OpenMPClauseKind C, bool) {
1743             return C == OMPC_firstprivate || C == OMPC_shared;
1744           },
1745           MatchesAlways, FromParent);
1746       if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1747         return DVarTemp;
1748 
1749       DVar.CKind = OMPC_shared;
1750       return DVar;
1751     }
1752   }
1753 
1754   // Explicitly specified attributes and local variables with predetermined
1755   // attributes.
1756   const_iterator I = begin();
1757   const_iterator EndI = end();
1758   if (FromParent && I != EndI)
1759     ++I;
1760   if (I == EndI)
1761     return DVar;
1762   auto It = I->SharingMap.find(D);
1763   if (It != I->SharingMap.end()) {
1764     const DSAInfo &Data = It->getSecond();
1765     DVar.RefExpr = Data.RefExpr.getPointer();
1766     DVar.PrivateCopy = Data.PrivateCopy;
1767     DVar.CKind = Data.Attributes;
1768     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1769     DVar.DKind = I->Directive;
1770     DVar.Modifier = Data.Modifier;
1771     DVar.AppliedToPointee = Data.AppliedToPointee;
1772   }
1773 
1774   return DVar;
1775 }
1776 
1777 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1778                                                         bool FromParent) const {
1779   if (isStackEmpty()) {
1780     const_iterator I;
1781     return getDSA(I, D);
1782   }
1783   D = getCanonicalDecl(D);
1784   const_iterator StartI = begin();
1785   const_iterator EndI = end();
1786   if (FromParent && StartI != EndI)
1787     ++StartI;
1788   return getDSA(StartI, D);
1789 }
1790 
1791 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1792                                                         unsigned Level) const {
1793   if (getStackSize() <= Level)
1794     return DSAVarData();
1795   D = getCanonicalDecl(D);
1796   const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level);
1797   return getDSA(StartI, D);
1798 }
1799 
1800 const DSAStackTy::DSAVarData
1801 DSAStackTy::hasDSA(ValueDecl *D,
1802                    const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1803                    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1804                    bool FromParent) const {
1805   if (isStackEmpty())
1806     return {};
1807   D = getCanonicalDecl(D);
1808   const_iterator I = begin();
1809   const_iterator EndI = end();
1810   if (FromParent && I != EndI)
1811     ++I;
1812   for (; I != EndI; ++I) {
1813     if (!DPred(I->Directive) &&
1814         !isImplicitOrExplicitTaskingRegion(I->Directive))
1815       continue;
1816     const_iterator NewI = I;
1817     DSAVarData DVar = getDSA(NewI, D);
1818     if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee))
1819       return DVar;
1820   }
1821   return {};
1822 }
1823 
1824 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1825     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1826     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1827     bool FromParent) const {
1828   if (isStackEmpty())
1829     return {};
1830   D = getCanonicalDecl(D);
1831   const_iterator StartI = begin();
1832   const_iterator EndI = end();
1833   if (FromParent && StartI != EndI)
1834     ++StartI;
1835   if (StartI == EndI || !DPred(StartI->Directive))
1836     return {};
1837   const_iterator NewI = StartI;
1838   DSAVarData DVar = getDSA(NewI, D);
1839   return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee))
1840              ? DVar
1841              : DSAVarData();
1842 }
1843 
1844 bool DSAStackTy::hasExplicitDSA(
1845     const ValueDecl *D,
1846     const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1847     unsigned Level, bool NotLastprivate) const {
1848   if (getStackSize() <= Level)
1849     return false;
1850   D = getCanonicalDecl(D);
1851   const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1852   auto I = StackElem.SharingMap.find(D);
1853   if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() &&
1854       CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) &&
1855       (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1856     return true;
1857   // Check predetermined rules for the loop control variables.
1858   auto LI = StackElem.LCVMap.find(D);
1859   if (LI != StackElem.LCVMap.end())
1860     return CPred(OMPC_private, /*AppliedToPointee=*/false);
1861   return false;
1862 }
1863 
1864 bool DSAStackTy::hasExplicitDirective(
1865     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1866     unsigned Level) const {
1867   if (getStackSize() <= Level)
1868     return false;
1869   const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1870   return DPred(StackElem.Directive);
1871 }
1872 
1873 bool DSAStackTy::hasDirective(
1874     const llvm::function_ref<bool(OpenMPDirectiveKind,
1875                                   const DeclarationNameInfo &, SourceLocation)>
1876         DPred,
1877     bool FromParent) const {
1878   // We look only in the enclosing region.
1879   size_t Skip = FromParent ? 2 : 1;
1880   for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1881        I != E; ++I) {
1882     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1883       return true;
1884   }
1885   return false;
1886 }
1887 
1888 void Sema::InitDataSharingAttributesStack() {
1889   VarDataSharingAttributesStack = new DSAStackTy(*this);
1890 }
1891 
1892 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1893 
1894 void Sema::pushOpenMPFunctionRegion() {
1895   DSAStack->pushFunction();
1896 }
1897 
1898 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1899   DSAStack->popFunction(OldFSI);
1900 }
1901 
1902 static bool isOpenMPDeviceDelayedContext(Sema &S) {
1903   assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1904          "Expected OpenMP device compilation.");
1905   return !S.isInOpenMPTargetExecutionDirective();
1906 }
1907 
1908 namespace {
1909 /// Status of the function emission on the host/device.
1910 enum class FunctionEmissionStatus {
1911   Emitted,
1912   Discarded,
1913   Unknown,
1914 };
1915 } // anonymous namespace
1916 
1917 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1918                                                          unsigned DiagID,
1919                                                          FunctionDecl *FD) {
1920   assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1921          "Expected OpenMP device compilation.");
1922 
1923   SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
1924   if (FD) {
1925     FunctionEmissionStatus FES = getEmissionStatus(FD);
1926     switch (FES) {
1927     case FunctionEmissionStatus::Emitted:
1928       Kind = SemaDiagnosticBuilder::K_Immediate;
1929       break;
1930     case FunctionEmissionStatus::Unknown:
1931       // TODO: We should always delay diagnostics here in case a target
1932       //       region is in a function we do not emit. However, as the
1933       //       current diagnostics are associated with the function containing
1934       //       the target region and we do not emit that one, we would miss out
1935       //       on diagnostics for the target region itself. We need to anchor
1936       //       the diagnostics with the new generated function *or* ensure we
1937       //       emit diagnostics associated with the surrounding function.
1938       Kind = isOpenMPDeviceDelayedContext(*this)
1939                  ? SemaDiagnosticBuilder::K_Deferred
1940                  : SemaDiagnosticBuilder::K_Immediate;
1941       break;
1942     case FunctionEmissionStatus::TemplateDiscarded:
1943     case FunctionEmissionStatus::OMPDiscarded:
1944       Kind = SemaDiagnosticBuilder::K_Nop;
1945       break;
1946     case FunctionEmissionStatus::CUDADiscarded:
1947       llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
1948       break;
1949     }
1950   }
1951 
1952   return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this);
1953 }
1954 
1955 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc,
1956                                                        unsigned DiagID,
1957                                                        FunctionDecl *FD) {
1958   assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1959          "Expected OpenMP host compilation.");
1960 
1961   SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
1962   if (FD) {
1963     FunctionEmissionStatus FES = getEmissionStatus(FD);
1964     switch (FES) {
1965     case FunctionEmissionStatus::Emitted:
1966       Kind = SemaDiagnosticBuilder::K_Immediate;
1967       break;
1968     case FunctionEmissionStatus::Unknown:
1969       Kind = SemaDiagnosticBuilder::K_Deferred;
1970       break;
1971     case FunctionEmissionStatus::TemplateDiscarded:
1972     case FunctionEmissionStatus::OMPDiscarded:
1973     case FunctionEmissionStatus::CUDADiscarded:
1974       Kind = SemaDiagnosticBuilder::K_Nop;
1975       break;
1976     }
1977   }
1978 
1979   return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this);
1980 }
1981 
1982 static OpenMPDefaultmapClauseKind
1983 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) {
1984   if (LO.OpenMP <= 45) {
1985     if (VD->getType().getNonReferenceType()->isScalarType())
1986       return OMPC_DEFAULTMAP_scalar;
1987     return OMPC_DEFAULTMAP_aggregate;
1988   }
1989   if (VD->getType().getNonReferenceType()->isAnyPointerType())
1990     return OMPC_DEFAULTMAP_pointer;
1991   if (VD->getType().getNonReferenceType()->isScalarType())
1992     return OMPC_DEFAULTMAP_scalar;
1993   return OMPC_DEFAULTMAP_aggregate;
1994 }
1995 
1996 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
1997                                  unsigned OpenMPCaptureLevel) const {
1998   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1999 
2000   ASTContext &Ctx = getASTContext();
2001   bool IsByRef = true;
2002 
2003   // Find the directive that is associated with the provided scope.
2004   D = cast<ValueDecl>(D->getCanonicalDecl());
2005   QualType Ty = D->getType();
2006 
2007   bool IsVariableUsedInMapClause = false;
2008   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
2009     // This table summarizes how a given variable should be passed to the device
2010     // given its type and the clauses where it appears. This table is based on
2011     // the description in OpenMP 4.5 [2.10.4, target Construct] and
2012     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
2013     //
2014     // =========================================================================
2015     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
2016     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
2017     // =========================================================================
2018     // | scl  |               |     |       |       -       |          | bycopy|
2019     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
2020     // | scl  |               |  x  |   -   |       -       |     -    | null  |
2021     // | scl  |       x       |     |       |       -       |          | byref |
2022     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
2023     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
2024     // | scl  |               |  -  |   -   |       -       |     x    | byref |
2025     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
2026     //
2027     // | agg  |      n.a.     |     |       |       -       |          | byref |
2028     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
2029     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
2030     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
2031     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
2032     //
2033     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
2034     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
2035     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
2036     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
2037     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
2038     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
2039     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
2040     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
2041     // =========================================================================
2042     // Legend:
2043     //  scl - scalar
2044     //  ptr - pointer
2045     //  agg - aggregate
2046     //  x - applies
2047     //  - - invalid in this combination
2048     //  [] - mapped with an array section
2049     //  byref - should be mapped by reference
2050     //  byval - should be mapped by value
2051     //  null - initialize a local variable to null on the device
2052     //
2053     // Observations:
2054     //  - All scalar declarations that show up in a map clause have to be passed
2055     //    by reference, because they may have been mapped in the enclosing data
2056     //    environment.
2057     //  - If the scalar value does not fit the size of uintptr, it has to be
2058     //    passed by reference, regardless the result in the table above.
2059     //  - For pointers mapped by value that have either an implicit map or an
2060     //    array section, the runtime library may pass the NULL value to the
2061     //    device instead of the value passed to it by the compiler.
2062 
2063     if (Ty->isReferenceType())
2064       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
2065 
2066     // Locate map clauses and see if the variable being captured is referred to
2067     // in any of those clauses. Here we only care about variables, not fields,
2068     // because fields are part of aggregates.
2069     bool IsVariableAssociatedWithSection = false;
2070 
2071     DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2072         D, Level,
2073         [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
2074             OMPClauseMappableExprCommon::MappableExprComponentListRef
2075                 MapExprComponents,
2076             OpenMPClauseKind WhereFoundClauseKind) {
2077           // Only the map clause information influences how a variable is
2078           // captured. E.g. is_device_ptr does not require changing the default
2079           // behavior.
2080           if (WhereFoundClauseKind != OMPC_map)
2081             return false;
2082 
2083           auto EI = MapExprComponents.rbegin();
2084           auto EE = MapExprComponents.rend();
2085 
2086           assert(EI != EE && "Invalid map expression!");
2087 
2088           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
2089             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
2090 
2091           ++EI;
2092           if (EI == EE)
2093             return false;
2094 
2095           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
2096               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
2097               isa<MemberExpr>(EI->getAssociatedExpression()) ||
2098               isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) {
2099             IsVariableAssociatedWithSection = true;
2100             // There is nothing more we need to know about this variable.
2101             return true;
2102           }
2103 
2104           // Keep looking for more map info.
2105           return false;
2106         });
2107 
2108     if (IsVariableUsedInMapClause) {
2109       // If variable is identified in a map clause it is always captured by
2110       // reference except if it is a pointer that is dereferenced somehow.
2111       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
2112     } else {
2113       // By default, all the data that has a scalar type is mapped by copy
2114       // (except for reduction variables).
2115       // Defaultmap scalar is mutual exclusive to defaultmap pointer
2116       IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
2117                  !Ty->isAnyPointerType()) ||
2118                 !Ty->isScalarType() ||
2119                 DSAStack->isDefaultmapCapturedByRef(
2120                     Level, getVariableCategoryFromDecl(LangOpts, D)) ||
2121                 DSAStack->hasExplicitDSA(
2122                     D,
2123                     [](OpenMPClauseKind K, bool AppliedToPointee) {
2124                       return K == OMPC_reduction && !AppliedToPointee;
2125                     },
2126                     Level);
2127     }
2128   }
2129 
2130   if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
2131     IsByRef =
2132         ((IsVariableUsedInMapClause &&
2133           DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
2134               OMPD_target) ||
2135          !(DSAStack->hasExplicitDSA(
2136                D,
2137                [](OpenMPClauseKind K, bool AppliedToPointee) -> bool {
2138                  return K == OMPC_firstprivate ||
2139                         (K == OMPC_reduction && AppliedToPointee);
2140                },
2141                Level, /*NotLastprivate=*/true) ||
2142            DSAStack->isUsesAllocatorsDecl(Level, D))) &&
2143         // If the variable is artificial and must be captured by value - try to
2144         // capture by value.
2145         !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
2146           !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) &&
2147         // If the variable is implicitly firstprivate and scalar - capture by
2148         // copy
2149         !(DSAStack->getDefaultDSA() == DSA_firstprivate &&
2150           !DSAStack->hasExplicitDSA(
2151               D, [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; },
2152               Level) &&
2153           !DSAStack->isLoopControlVariable(D, Level).first);
2154   }
2155 
2156   // When passing data by copy, we need to make sure it fits the uintptr size
2157   // and alignment, because the runtime library only deals with uintptr types.
2158   // If it does not fit the uintptr size, we need to pass the data by reference
2159   // instead.
2160   if (!IsByRef &&
2161       (Ctx.getTypeSizeInChars(Ty) >
2162            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
2163        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
2164     IsByRef = true;
2165   }
2166 
2167   return IsByRef;
2168 }
2169 
2170 unsigned Sema::getOpenMPNestingLevel() const {
2171   assert(getLangOpts().OpenMP);
2172   return DSAStack->getNestingLevel();
2173 }
2174 
2175 bool Sema::isInOpenMPTargetExecutionDirective() const {
2176   return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
2177           !DSAStack->isClauseParsingMode()) ||
2178          DSAStack->hasDirective(
2179              [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2180                 SourceLocation) -> bool {
2181                return isOpenMPTargetExecutionDirective(K);
2182              },
2183              false);
2184 }
2185 
2186 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
2187                                     unsigned StopAt) {
2188   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2189   D = getCanonicalDecl(D);
2190 
2191   auto *VD = dyn_cast<VarDecl>(D);
2192   // Do not capture constexpr variables.
2193   if (VD && VD->isConstexpr())
2194     return nullptr;
2195 
2196   // If we want to determine whether the variable should be captured from the
2197   // perspective of the current capturing scope, and we've already left all the
2198   // capturing scopes of the top directive on the stack, check from the
2199   // perspective of its parent directive (if any) instead.
2200   DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
2201       *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
2202 
2203   // If we are attempting to capture a global variable in a directive with
2204   // 'target' we return true so that this global is also mapped to the device.
2205   //
2206   if (VD && !VD->hasLocalStorage() &&
2207       (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
2208     if (isInOpenMPTargetExecutionDirective()) {
2209       DSAStackTy::DSAVarData DVarTop =
2210           DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2211       if (DVarTop.CKind != OMPC_unknown && DVarTop.RefExpr)
2212         return VD;
2213       // If the declaration is enclosed in a 'declare target' directive,
2214       // then it should not be captured.
2215       //
2216       if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2217         return nullptr;
2218       CapturedRegionScopeInfo *CSI = nullptr;
2219       for (FunctionScopeInfo *FSI : llvm::drop_begin(
2220                llvm::reverse(FunctionScopes),
2221                CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) {
2222         if (!isa<CapturingScopeInfo>(FSI))
2223           return nullptr;
2224         if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2225           if (RSI->CapRegionKind == CR_OpenMP) {
2226             CSI = RSI;
2227             break;
2228           }
2229       }
2230       assert(CSI && "Failed to find CapturedRegionScopeInfo");
2231       SmallVector<OpenMPDirectiveKind, 4> Regions;
2232       getOpenMPCaptureRegions(Regions,
2233                               DSAStack->getDirective(CSI->OpenMPLevel));
2234       if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task)
2235         return VD;
2236     }
2237     if (isInOpenMPDeclareTargetContext()) {
2238       // Try to mark variable as declare target if it is used in capturing
2239       // regions.
2240       if (LangOpts.OpenMP <= 45 &&
2241           !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2242         checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
2243       return nullptr;
2244     }
2245   }
2246 
2247   if (CheckScopeInfo) {
2248     bool OpenMPFound = false;
2249     for (unsigned I = StopAt + 1; I > 0; --I) {
2250       FunctionScopeInfo *FSI = FunctionScopes[I - 1];
2251       if(!isa<CapturingScopeInfo>(FSI))
2252         return nullptr;
2253       if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2254         if (RSI->CapRegionKind == CR_OpenMP) {
2255           OpenMPFound = true;
2256           break;
2257         }
2258     }
2259     if (!OpenMPFound)
2260       return nullptr;
2261   }
2262 
2263   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2264       (!DSAStack->isClauseParsingMode() ||
2265        DSAStack->getParentDirective() != OMPD_unknown)) {
2266     auto &&Info = DSAStack->isLoopControlVariable(D);
2267     if (Info.first ||
2268         (VD && VD->hasLocalStorage() &&
2269          isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
2270         (VD && DSAStack->isForceVarCapturing()))
2271       return VD ? VD : Info.second;
2272     DSAStackTy::DSAVarData DVarTop =
2273         DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2274     if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind) &&
2275         (!VD || VD->hasLocalStorage() || !DVarTop.AppliedToPointee))
2276       return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl());
2277     // Threadprivate variables must not be captured.
2278     if (isOpenMPThreadPrivate(DVarTop.CKind))
2279       return nullptr;
2280     // The variable is not private or it is the variable in the directive with
2281     // default(none) clause and not used in any clause.
2282     DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2283         D,
2284         [](OpenMPClauseKind C, bool AppliedToPointee) {
2285           return isOpenMPPrivate(C) && !AppliedToPointee;
2286         },
2287         [](OpenMPDirectiveKind) { return true; },
2288         DSAStack->isClauseParsingMode());
2289     // Global shared must not be captured.
2290     if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown &&
2291         ((DSAStack->getDefaultDSA() != DSA_none &&
2292           DSAStack->getDefaultDSA() != DSA_firstprivate) ||
2293          DVarTop.CKind == OMPC_shared))
2294       return nullptr;
2295     if (DVarPrivate.CKind != OMPC_unknown ||
2296         (VD && (DSAStack->getDefaultDSA() == DSA_none ||
2297                 DSAStack->getDefaultDSA() == DSA_firstprivate)))
2298       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
2299   }
2300   return nullptr;
2301 }
2302 
2303 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
2304                                         unsigned Level) const {
2305   FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2306 }
2307 
2308 void Sema::startOpenMPLoop() {
2309   assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2310   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2311     DSAStack->loopInit();
2312 }
2313 
2314 void Sema::startOpenMPCXXRangeFor() {
2315   assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2316   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2317     DSAStack->resetPossibleLoopCounter();
2318     DSAStack->loopStart();
2319   }
2320 }
2321 
2322 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level,
2323                                            unsigned CapLevel) const {
2324   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2325   if (DSAStack->hasExplicitDirective(
2326           [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); },
2327           Level)) {
2328     bool IsTriviallyCopyable =
2329         D->getType().getNonReferenceType().isTriviallyCopyableType(Context) &&
2330         !D->getType()
2331              .getNonReferenceType()
2332              .getCanonicalType()
2333              ->getAsCXXRecordDecl();
2334     OpenMPDirectiveKind DKind = DSAStack->getDirective(Level);
2335     SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2336     getOpenMPCaptureRegions(CaptureRegions, DKind);
2337     if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) &&
2338         (IsTriviallyCopyable ||
2339          !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) {
2340       if (DSAStack->hasExplicitDSA(
2341               D,
2342               [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; },
2343               Level, /*NotLastprivate=*/true))
2344         return OMPC_firstprivate;
2345       DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2346       if (DVar.CKind != OMPC_shared &&
2347           !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) {
2348         DSAStack->addImplicitTaskFirstprivate(Level, D);
2349         return OMPC_firstprivate;
2350       }
2351     }
2352   }
2353   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2354     if (DSAStack->getAssociatedLoops() > 0 &&
2355         !DSAStack->isLoopStarted()) {
2356       DSAStack->resetPossibleLoopCounter(D);
2357       DSAStack->loopStart();
2358       return OMPC_private;
2359     }
2360     if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
2361          DSAStack->isLoopControlVariable(D).first) &&
2362         !DSAStack->hasExplicitDSA(
2363             D, [](OpenMPClauseKind K, bool) { return K != OMPC_private; },
2364             Level) &&
2365         !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2366       return OMPC_private;
2367   }
2368   if (const auto *VD = dyn_cast<VarDecl>(D)) {
2369     if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
2370         DSAStack->isForceVarCapturing() &&
2371         !DSAStack->hasExplicitDSA(
2372             D, [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; },
2373             Level))
2374       return OMPC_private;
2375   }
2376   // User-defined allocators are private since they must be defined in the
2377   // context of target region.
2378   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) &&
2379       DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr(
2380           DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
2381           DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator)
2382     return OMPC_private;
2383   return (DSAStack->hasExplicitDSA(
2384               D, [](OpenMPClauseKind K, bool) { return K == OMPC_private; },
2385               Level) ||
2386           (DSAStack->isClauseParsingMode() &&
2387            DSAStack->getClauseParsingMode() == OMPC_private) ||
2388           // Consider taskgroup reduction descriptor variable a private
2389           // to avoid possible capture in the region.
2390           (DSAStack->hasExplicitDirective(
2391                [](OpenMPDirectiveKind K) {
2392                  return K == OMPD_taskgroup ||
2393                         ((isOpenMPParallelDirective(K) ||
2394                           isOpenMPWorksharingDirective(K)) &&
2395                          !isOpenMPSimdDirective(K));
2396                },
2397                Level) &&
2398            DSAStack->isTaskgroupReductionRef(D, Level)))
2399              ? OMPC_private
2400              : OMPC_unknown;
2401 }
2402 
2403 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2404                                 unsigned Level) {
2405   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2406   D = getCanonicalDecl(D);
2407   OpenMPClauseKind OMPC = OMPC_unknown;
2408   for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2409     const unsigned NewLevel = I - 1;
2410     if (DSAStack->hasExplicitDSA(
2411             D,
2412             [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) {
2413               if (isOpenMPPrivate(K) && !AppliedToPointee) {
2414                 OMPC = K;
2415                 return true;
2416               }
2417               return false;
2418             },
2419             NewLevel))
2420       break;
2421     if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2422             D, NewLevel,
2423             [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2424                OpenMPClauseKind) { return true; })) {
2425       OMPC = OMPC_map;
2426       break;
2427     }
2428     if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2429                                        NewLevel)) {
2430       OMPC = OMPC_map;
2431       if (DSAStack->mustBeFirstprivateAtLevel(
2432               NewLevel, getVariableCategoryFromDecl(LangOpts, D)))
2433         OMPC = OMPC_firstprivate;
2434       break;
2435     }
2436   }
2437   if (OMPC != OMPC_unknown)
2438     FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC)));
2439 }
2440 
2441 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level,
2442                                       unsigned CaptureLevel) const {
2443   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2444   // Return true if the current level is no longer enclosed in a target region.
2445 
2446   SmallVector<OpenMPDirectiveKind, 4> Regions;
2447   getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
2448   const auto *VD = dyn_cast<VarDecl>(D);
2449   return VD && !VD->hasLocalStorage() &&
2450          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2451                                         Level) &&
2452          Regions[CaptureLevel] != OMPD_task;
2453 }
2454 
2455 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level,
2456                                       unsigned CaptureLevel) const {
2457   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2458   // Return true if the current level is no longer enclosed in a target region.
2459 
2460   if (const auto *VD = dyn_cast<VarDecl>(D)) {
2461     if (!VD->hasLocalStorage()) {
2462       if (isInOpenMPTargetExecutionDirective())
2463         return true;
2464       DSAStackTy::DSAVarData TopDVar =
2465           DSAStack->getTopDSA(D, /*FromParent=*/false);
2466       unsigned NumLevels =
2467           getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2468       if (Level == 0)
2469         return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared;
2470       do {
2471         --Level;
2472         DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2473         if (DVar.CKind != OMPC_shared)
2474           return true;
2475       } while (Level > 0);
2476     }
2477   }
2478   return true;
2479 }
2480 
2481 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
2482 
2483 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc,
2484                                           OMPTraitInfo &TI) {
2485   OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI));
2486 }
2487 
2488 void Sema::ActOnOpenMPEndDeclareVariant() {
2489   assert(isInOpenMPDeclareVariantScope() &&
2490          "Not in OpenMP declare variant scope!");
2491 
2492   OMPDeclareVariantScopes.pop_back();
2493 }
2494 
2495 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller,
2496                                          const FunctionDecl *Callee,
2497                                          SourceLocation Loc) {
2498   assert(LangOpts.OpenMP && "Expected OpenMP compilation mode.");
2499   Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2500       OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl());
2501   // Ignore host functions during device analyzis.
2502   if (LangOpts.OpenMPIsDevice &&
2503       (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host))
2504     return;
2505   // Ignore nohost functions during host analyzis.
2506   if (!LangOpts.OpenMPIsDevice && DevTy &&
2507       *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2508     return;
2509   const FunctionDecl *FD = Callee->getMostRecentDecl();
2510   DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD);
2511   if (LangOpts.OpenMPIsDevice && DevTy &&
2512       *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2513     // Diagnose host function called during device codegen.
2514     StringRef HostDevTy =
2515         getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host);
2516     Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
2517     Diag(*OMPDeclareTargetDeclAttr::getLocation(FD),
2518          diag::note_omp_marked_device_type_here)
2519         << HostDevTy;
2520     return;
2521   }
2522       if (!LangOpts.OpenMPIsDevice && DevTy &&
2523           *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2524         // Diagnose nohost function called during host codegen.
2525         StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2526             OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
2527         Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
2528         Diag(*OMPDeclareTargetDeclAttr::getLocation(FD),
2529              diag::note_omp_marked_device_type_here)
2530             << NoHostDevTy;
2531       }
2532 }
2533 
2534 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2535                                const DeclarationNameInfo &DirName,
2536                                Scope *CurScope, SourceLocation Loc) {
2537   DSAStack->push(DKind, DirName, CurScope, Loc);
2538   PushExpressionEvaluationContext(
2539       ExpressionEvaluationContext::PotentiallyEvaluated);
2540 }
2541 
2542 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
2543   DSAStack->setClauseParsingMode(K);
2544 }
2545 
2546 void Sema::EndOpenMPClause() {
2547   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2548   CleanupVarDeclMarking();
2549 }
2550 
2551 static std::pair<ValueDecl *, bool>
2552 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
2553                SourceRange &ERange, bool AllowArraySection = false);
2554 
2555 /// Check consistency of the reduction clauses.
2556 static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2557                                   ArrayRef<OMPClause *> Clauses) {
2558   bool InscanFound = false;
2559   SourceLocation InscanLoc;
2560   // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2561   // A reduction clause without the inscan reduction-modifier may not appear on
2562   // a construct on which a reduction clause with the inscan reduction-modifier
2563   // appears.
2564   for (OMPClause *C : Clauses) {
2565     if (C->getClauseKind() != OMPC_reduction)
2566       continue;
2567     auto *RC = cast<OMPReductionClause>(C);
2568     if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2569       InscanFound = true;
2570       InscanLoc = RC->getModifierLoc();
2571       continue;
2572     }
2573     if (RC->getModifier() == OMPC_REDUCTION_task) {
2574       // OpenMP 5.0, 2.19.5.4 reduction Clause.
2575       // A reduction clause with the task reduction-modifier may only appear on
2576       // a parallel construct, a worksharing construct or a combined or
2577       // composite construct for which any of the aforementioned constructs is a
2578       // constituent construct and simd or loop are not constituent constructs.
2579       OpenMPDirectiveKind CurDir = Stack->getCurrentDirective();
2580       if (!(isOpenMPParallelDirective(CurDir) ||
2581             isOpenMPWorksharingDirective(CurDir)) ||
2582           isOpenMPSimdDirective(CurDir))
2583         S.Diag(RC->getModifierLoc(),
2584                diag::err_omp_reduction_task_not_parallel_or_worksharing);
2585       continue;
2586     }
2587   }
2588   if (InscanFound) {
2589     for (OMPClause *C : Clauses) {
2590       if (C->getClauseKind() != OMPC_reduction)
2591         continue;
2592       auto *RC = cast<OMPReductionClause>(C);
2593       if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2594         S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown
2595                    ? RC->getBeginLoc()
2596                    : RC->getModifierLoc(),
2597                diag::err_omp_inscan_reduction_expected);
2598         S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction);
2599         continue;
2600       }
2601       for (Expr *Ref : RC->varlists()) {
2602         assert(Ref && "NULL expr in OpenMP nontemporal clause.");
2603         SourceLocation ELoc;
2604         SourceRange ERange;
2605         Expr *SimpleRefExpr = Ref;
2606         auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
2607                                   /*AllowArraySection=*/true);
2608         ValueDecl *D = Res.first;
2609         if (!D)
2610           continue;
2611         if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) {
2612           S.Diag(Ref->getExprLoc(),
2613                  diag::err_omp_reduction_not_inclusive_exclusive)
2614               << Ref->getSourceRange();
2615         }
2616       }
2617     }
2618   }
2619 }
2620 
2621 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2622                                  ArrayRef<OMPClause *> Clauses);
2623 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2624                                  bool WithInit);
2625 
2626 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2627                               const ValueDecl *D,
2628                               const DSAStackTy::DSAVarData &DVar,
2629                               bool IsLoopIterVar = false);
2630 
2631 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
2632   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2633   //  A variable of class type (or array thereof) that appears in a lastprivate
2634   //  clause requires an accessible, unambiguous default constructor for the
2635   //  class type, unless the list item is also specified in a firstprivate
2636   //  clause.
2637   if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
2638     for (OMPClause *C : D->clauses()) {
2639       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
2640         SmallVector<Expr *, 8> PrivateCopies;
2641         for (Expr *DE : Clause->varlists()) {
2642           if (DE->isValueDependent() || DE->isTypeDependent()) {
2643             PrivateCopies.push_back(nullptr);
2644             continue;
2645           }
2646           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
2647           auto *VD = cast<VarDecl>(DRE->getDecl());
2648           QualType Type = VD->getType().getNonReferenceType();
2649           const DSAStackTy::DSAVarData DVar =
2650               DSAStack->getTopDSA(VD, /*FromParent=*/false);
2651           if (DVar.CKind == OMPC_lastprivate) {
2652             // Generate helper private variable and initialize it with the
2653             // default value. The address of the original variable is replaced
2654             // by the address of the new private variable in CodeGen. This new
2655             // variable is not added to IdResolver, so the code in the OpenMP
2656             // region uses original variable for proper diagnostics.
2657             VarDecl *VDPrivate = buildVarDecl(
2658                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
2659                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
2660             ActOnUninitializedDecl(VDPrivate);
2661             if (VDPrivate->isInvalidDecl()) {
2662               PrivateCopies.push_back(nullptr);
2663               continue;
2664             }
2665             PrivateCopies.push_back(buildDeclRefExpr(
2666                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
2667           } else {
2668             // The variable is also a firstprivate, so initialization sequence
2669             // for private copy is generated already.
2670             PrivateCopies.push_back(nullptr);
2671           }
2672         }
2673         Clause->setPrivateCopies(PrivateCopies);
2674         continue;
2675       }
2676       // Finalize nontemporal clause by handling private copies, if any.
2677       if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) {
2678         SmallVector<Expr *, 8> PrivateRefs;
2679         for (Expr *RefExpr : Clause->varlists()) {
2680           assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
2681           SourceLocation ELoc;
2682           SourceRange ERange;
2683           Expr *SimpleRefExpr = RefExpr;
2684           auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
2685           if (Res.second)
2686             // It will be analyzed later.
2687             PrivateRefs.push_back(RefExpr);
2688           ValueDecl *D = Res.first;
2689           if (!D)
2690             continue;
2691 
2692           const DSAStackTy::DSAVarData DVar =
2693               DSAStack->getTopDSA(D, /*FromParent=*/false);
2694           PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy
2695                                                  : SimpleRefExpr);
2696         }
2697         Clause->setPrivateRefs(PrivateRefs);
2698         continue;
2699       }
2700       if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) {
2701         for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) {
2702           OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I);
2703           auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts());
2704           if (!DRE)
2705             continue;
2706           ValueDecl *VD = DRE->getDecl();
2707           if (!VD || !isa<VarDecl>(VD))
2708             continue;
2709           DSAStackTy::DSAVarData DVar =
2710               DSAStack->getTopDSA(VD, /*FromParent=*/false);
2711           // OpenMP [2.12.5, target Construct]
2712           // Memory allocators that appear in a uses_allocators clause cannot
2713           // appear in other data-sharing attribute clauses or data-mapping
2714           // attribute clauses in the same construct.
2715           Expr *MapExpr = nullptr;
2716           if (DVar.RefExpr ||
2717               DSAStack->checkMappableExprComponentListsForDecl(
2718                   VD, /*CurrentRegionOnly=*/true,
2719                   [VD, &MapExpr](
2720                       OMPClauseMappableExprCommon::MappableExprComponentListRef
2721                           MapExprComponents,
2722                       OpenMPClauseKind C) {
2723                     auto MI = MapExprComponents.rbegin();
2724                     auto ME = MapExprComponents.rend();
2725                     if (MI != ME &&
2726                         MI->getAssociatedDeclaration()->getCanonicalDecl() ==
2727                             VD->getCanonicalDecl()) {
2728                       MapExpr = MI->getAssociatedExpression();
2729                       return true;
2730                     }
2731                     return false;
2732                   })) {
2733             Diag(D.Allocator->getExprLoc(),
2734                  diag::err_omp_allocator_used_in_clauses)
2735                 << D.Allocator->getSourceRange();
2736             if (DVar.RefExpr)
2737               reportOriginalDsa(*this, DSAStack, VD, DVar);
2738             else
2739               Diag(MapExpr->getExprLoc(), diag::note_used_here)
2740                   << MapExpr->getSourceRange();
2741           }
2742         }
2743         continue;
2744       }
2745     }
2746     // Check allocate clauses.
2747     if (!CurContext->isDependentContext())
2748       checkAllocateClauses(*this, DSAStack, D->clauses());
2749     checkReductionClauses(*this, DSAStack, D->clauses());
2750   }
2751 
2752   DSAStack->pop();
2753   DiscardCleanupsInEvaluationContext();
2754   PopExpressionEvaluationContext();
2755 }
2756 
2757 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2758                                      Expr *NumIterations, Sema &SemaRef,
2759                                      Scope *S, DSAStackTy *Stack);
2760 
2761 namespace {
2762 
2763 class VarDeclFilterCCC final : public CorrectionCandidateCallback {
2764 private:
2765   Sema &SemaRef;
2766 
2767 public:
2768   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
2769   bool ValidateCandidate(const TypoCorrection &Candidate) override {
2770     NamedDecl *ND = Candidate.getCorrectionDecl();
2771     if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
2772       return VD->hasGlobalStorage() &&
2773              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2774                                    SemaRef.getCurScope());
2775     }
2776     return false;
2777   }
2778 
2779   std::unique_ptr<CorrectionCandidateCallback> clone() override {
2780     return std::make_unique<VarDeclFilterCCC>(*this);
2781   }
2782 
2783 };
2784 
2785 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
2786 private:
2787   Sema &SemaRef;
2788 
2789 public:
2790   explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2791   bool ValidateCandidate(const TypoCorrection &Candidate) override {
2792     NamedDecl *ND = Candidate.getCorrectionDecl();
2793     if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
2794                isa<FunctionDecl>(ND))) {
2795       return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2796                                    SemaRef.getCurScope());
2797     }
2798     return false;
2799   }
2800 
2801   std::unique_ptr<CorrectionCandidateCallback> clone() override {
2802     return std::make_unique<VarOrFuncDeclFilterCCC>(*this);
2803   }
2804 };
2805 
2806 } // namespace
2807 
2808 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2809                                          CXXScopeSpec &ScopeSpec,
2810                                          const DeclarationNameInfo &Id,
2811                                          OpenMPDirectiveKind Kind) {
2812   LookupResult Lookup(*this, Id, LookupOrdinaryName);
2813   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2814 
2815   if (Lookup.isAmbiguous())
2816     return ExprError();
2817 
2818   VarDecl *VD;
2819   if (!Lookup.isSingleResult()) {
2820     VarDeclFilterCCC CCC(*this);
2821     if (TypoCorrection Corrected =
2822             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2823                         CTK_ErrorRecovery)) {
2824       diagnoseTypo(Corrected,
2825                    PDiag(Lookup.empty()
2826                              ? diag::err_undeclared_var_use_suggest
2827                              : diag::err_omp_expected_var_arg_suggest)
2828                        << Id.getName());
2829       VD = Corrected.getCorrectionDeclAs<VarDecl>();
2830     } else {
2831       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2832                                        : diag::err_omp_expected_var_arg)
2833           << Id.getName();
2834       return ExprError();
2835     }
2836   } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2837     Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2838     Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2839     return ExprError();
2840   }
2841   Lookup.suppressDiagnostics();
2842 
2843   // OpenMP [2.9.2, Syntax, C/C++]
2844   //   Variables must be file-scope, namespace-scope, or static block-scope.
2845   if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
2846     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
2847         << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
2848     bool IsDecl =
2849         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2850     Diag(VD->getLocation(),
2851          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2852         << VD;
2853     return ExprError();
2854   }
2855 
2856   VarDecl *CanonicalVD = VD->getCanonicalDecl();
2857   NamedDecl *ND = CanonicalVD;
2858   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2859   //   A threadprivate directive for file-scope variables must appear outside
2860   //   any definition or declaration.
2861   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2862       !getCurLexicalContext()->isTranslationUnit()) {
2863     Diag(Id.getLoc(), diag::err_omp_var_scope)
2864         << getOpenMPDirectiveName(Kind) << VD;
2865     bool IsDecl =
2866         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2867     Diag(VD->getLocation(),
2868          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2869         << VD;
2870     return ExprError();
2871   }
2872   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2873   //   A threadprivate directive for static class member variables must appear
2874   //   in the class definition, in the same scope in which the member
2875   //   variables are declared.
2876   if (CanonicalVD->isStaticDataMember() &&
2877       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2878     Diag(Id.getLoc(), diag::err_omp_var_scope)
2879         << getOpenMPDirectiveName(Kind) << VD;
2880     bool IsDecl =
2881         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2882     Diag(VD->getLocation(),
2883          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2884         << VD;
2885     return ExprError();
2886   }
2887   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2888   //   A threadprivate directive for namespace-scope variables must appear
2889   //   outside any definition or declaration other than the namespace
2890   //   definition itself.
2891   if (CanonicalVD->getDeclContext()->isNamespace() &&
2892       (!getCurLexicalContext()->isFileContext() ||
2893        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2894     Diag(Id.getLoc(), diag::err_omp_var_scope)
2895         << getOpenMPDirectiveName(Kind) << VD;
2896     bool IsDecl =
2897         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2898     Diag(VD->getLocation(),
2899          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2900         << VD;
2901     return ExprError();
2902   }
2903   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2904   //   A threadprivate directive for static block-scope variables must appear
2905   //   in the scope of the variable and not in a nested scope.
2906   if (CanonicalVD->isLocalVarDecl() && CurScope &&
2907       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
2908     Diag(Id.getLoc(), diag::err_omp_var_scope)
2909         << getOpenMPDirectiveName(Kind) << VD;
2910     bool IsDecl =
2911         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2912     Diag(VD->getLocation(),
2913          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2914         << VD;
2915     return ExprError();
2916   }
2917 
2918   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2919   //   A threadprivate directive must lexically precede all references to any
2920   //   of the variables in its list.
2921   if (Kind == OMPD_threadprivate && VD->isUsed() &&
2922       !DSAStack->isThreadPrivate(VD)) {
2923     Diag(Id.getLoc(), diag::err_omp_var_used)
2924         << getOpenMPDirectiveName(Kind) << VD;
2925     return ExprError();
2926   }
2927 
2928   QualType ExprType = VD->getType().getNonReferenceType();
2929   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2930                              SourceLocation(), VD,
2931                              /*RefersToEnclosingVariableOrCapture=*/false,
2932                              Id.getLoc(), ExprType, VK_LValue);
2933 }
2934 
2935 Sema::DeclGroupPtrTy
2936 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2937                                         ArrayRef<Expr *> VarList) {
2938   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
2939     CurContext->addDecl(D);
2940     return DeclGroupPtrTy::make(DeclGroupRef(D));
2941   }
2942   return nullptr;
2943 }
2944 
2945 namespace {
2946 class LocalVarRefChecker final
2947     : public ConstStmtVisitor<LocalVarRefChecker, bool> {
2948   Sema &SemaRef;
2949 
2950 public:
2951   bool VisitDeclRefExpr(const DeclRefExpr *E) {
2952     if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2953       if (VD->hasLocalStorage()) {
2954         SemaRef.Diag(E->getBeginLoc(),
2955                      diag::err_omp_local_var_in_threadprivate_init)
2956             << E->getSourceRange();
2957         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2958             << VD << VD->getSourceRange();
2959         return true;
2960       }
2961     }
2962     return false;
2963   }
2964   bool VisitStmt(const Stmt *S) {
2965     for (const Stmt *Child : S->children()) {
2966       if (Child && Visit(Child))
2967         return true;
2968     }
2969     return false;
2970   }
2971   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
2972 };
2973 } // namespace
2974 
2975 OMPThreadPrivateDecl *
2976 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
2977   SmallVector<Expr *, 8> Vars;
2978   for (Expr *RefExpr : VarList) {
2979     auto *DE = cast<DeclRefExpr>(RefExpr);
2980     auto *VD = cast<VarDecl>(DE->getDecl());
2981     SourceLocation ILoc = DE->getExprLoc();
2982 
2983     // Mark variable as used.
2984     VD->setReferenced();
2985     VD->markUsed(Context);
2986 
2987     QualType QType = VD->getType();
2988     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2989       // It will be analyzed later.
2990       Vars.push_back(DE);
2991       continue;
2992     }
2993 
2994     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2995     //   A threadprivate variable must not have an incomplete type.
2996     if (RequireCompleteType(ILoc, VD->getType(),
2997                             diag::err_omp_threadprivate_incomplete_type)) {
2998       continue;
2999     }
3000 
3001     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
3002     //   A threadprivate variable must not have a reference type.
3003     if (VD->getType()->isReferenceType()) {
3004       Diag(ILoc, diag::err_omp_ref_type_arg)
3005           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
3006       bool IsDecl =
3007           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3008       Diag(VD->getLocation(),
3009            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3010           << VD;
3011       continue;
3012     }
3013 
3014     // Check if this is a TLS variable. If TLS is not being supported, produce
3015     // the corresponding diagnostic.
3016     if ((VD->getTLSKind() != VarDecl::TLS_None &&
3017          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
3018            getLangOpts().OpenMPUseTLS &&
3019            getASTContext().getTargetInfo().isTLSSupported())) ||
3020         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3021          !VD->isLocalVarDecl())) {
3022       Diag(ILoc, diag::err_omp_var_thread_local)
3023           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
3024       bool IsDecl =
3025           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3026       Diag(VD->getLocation(),
3027            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3028           << VD;
3029       continue;
3030     }
3031 
3032     // Check if initial value of threadprivate variable reference variable with
3033     // local storage (it is not supported by runtime).
3034     if (const Expr *Init = VD->getAnyInitializer()) {
3035       LocalVarRefChecker Checker(*this);
3036       if (Checker.Visit(Init))
3037         continue;
3038     }
3039 
3040     Vars.push_back(RefExpr);
3041     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
3042     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
3043         Context, SourceRange(Loc, Loc)));
3044     if (ASTMutationListener *ML = Context.getASTMutationListener())
3045       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
3046   }
3047   OMPThreadPrivateDecl *D = nullptr;
3048   if (!Vars.empty()) {
3049     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
3050                                      Vars);
3051     D->setAccess(AS_public);
3052   }
3053   return D;
3054 }
3055 
3056 static OMPAllocateDeclAttr::AllocatorTypeTy
3057 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
3058   if (!Allocator)
3059     return OMPAllocateDeclAttr::OMPNullMemAlloc;
3060   if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3061       Allocator->isInstantiationDependent() ||
3062       Allocator->containsUnexpandedParameterPack())
3063     return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3064   auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3065   const Expr *AE = Allocator->IgnoreParenImpCasts();
3066   for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
3067     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
3068     const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
3069     llvm::FoldingSetNodeID AEId, DAEId;
3070     AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
3071     DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
3072     if (AEId == DAEId) {
3073       AllocatorKindRes = AllocatorKind;
3074       break;
3075     }
3076   }
3077   return AllocatorKindRes;
3078 }
3079 
3080 static bool checkPreviousOMPAllocateAttribute(
3081     Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
3082     OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
3083   if (!VD->hasAttr<OMPAllocateDeclAttr>())
3084     return false;
3085   const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
3086   Expr *PrevAllocator = A->getAllocator();
3087   OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
3088       getAllocatorKind(S, Stack, PrevAllocator);
3089   bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
3090   if (AllocatorsMatch &&
3091       AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
3092       Allocator && PrevAllocator) {
3093     const Expr *AE = Allocator->IgnoreParenImpCasts();
3094     const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
3095     llvm::FoldingSetNodeID AEId, PAEId;
3096     AE->Profile(AEId, S.Context, /*Canonical=*/true);
3097     PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
3098     AllocatorsMatch = AEId == PAEId;
3099   }
3100   if (!AllocatorsMatch) {
3101     SmallString<256> AllocatorBuffer;
3102     llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
3103     if (Allocator)
3104       Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
3105     SmallString<256> PrevAllocatorBuffer;
3106     llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
3107     if (PrevAllocator)
3108       PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
3109                                  S.getPrintingPolicy());
3110 
3111     SourceLocation AllocatorLoc =
3112         Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
3113     SourceRange AllocatorRange =
3114         Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
3115     SourceLocation PrevAllocatorLoc =
3116         PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
3117     SourceRange PrevAllocatorRange =
3118         PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
3119     S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
3120         << (Allocator ? 1 : 0) << AllocatorStream.str()
3121         << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
3122         << AllocatorRange;
3123     S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
3124         << PrevAllocatorRange;
3125     return true;
3126   }
3127   return false;
3128 }
3129 
3130 static void
3131 applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
3132                           OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
3133                           Expr *Allocator, SourceRange SR) {
3134   if (VD->hasAttr<OMPAllocateDeclAttr>())
3135     return;
3136   if (Allocator &&
3137       (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3138        Allocator->isInstantiationDependent() ||
3139        Allocator->containsUnexpandedParameterPack()))
3140     return;
3141   auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
3142                                                 Allocator, SR);
3143   VD->addAttr(A);
3144   if (ASTMutationListener *ML = S.Context.getASTMutationListener())
3145     ML->DeclarationMarkedOpenMPAllocate(VD, A);
3146 }
3147 
3148 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective(
3149     SourceLocation Loc, ArrayRef<Expr *> VarList,
3150     ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
3151   assert(Clauses.size() <= 1 && "Expected at most one clause.");
3152   Expr *Allocator = nullptr;
3153   if (Clauses.empty()) {
3154     // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
3155     // allocate directives that appear in a target region must specify an
3156     // allocator clause unless a requires directive with the dynamic_allocators
3157     // clause is present in the same compilation unit.
3158     if (LangOpts.OpenMPIsDevice &&
3159         !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
3160       targetDiag(Loc, diag::err_expected_allocator_clause);
3161   } else {
3162     Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
3163   }
3164   OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3165       getAllocatorKind(*this, DSAStack, Allocator);
3166   SmallVector<Expr *, 8> Vars;
3167   for (Expr *RefExpr : VarList) {
3168     auto *DE = cast<DeclRefExpr>(RefExpr);
3169     auto *VD = cast<VarDecl>(DE->getDecl());
3170 
3171     // Check if this is a TLS variable or global register.
3172     if (VD->getTLSKind() != VarDecl::TLS_None ||
3173         VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
3174         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3175          !VD->isLocalVarDecl()))
3176       continue;
3177 
3178     // If the used several times in the allocate directive, the same allocator
3179     // must be used.
3180     if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
3181                                           AllocatorKind, Allocator))
3182       continue;
3183 
3184     // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
3185     // If a list item has a static storage type, the allocator expression in the
3186     // allocator clause must be a constant expression that evaluates to one of
3187     // the predefined memory allocator values.
3188     if (Allocator && VD->hasGlobalStorage()) {
3189       if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
3190         Diag(Allocator->getExprLoc(),
3191              diag::err_omp_expected_predefined_allocator)
3192             << Allocator->getSourceRange();
3193         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3194                       VarDecl::DeclarationOnly;
3195         Diag(VD->getLocation(),
3196              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3197             << VD;
3198         continue;
3199       }
3200     }
3201 
3202     Vars.push_back(RefExpr);
3203     applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator,
3204                               DE->getSourceRange());
3205   }
3206   if (Vars.empty())
3207     return nullptr;
3208   if (!Owner)
3209     Owner = getCurLexicalContext();
3210   auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
3211   D->setAccess(AS_public);
3212   Owner->addDecl(D);
3213   return DeclGroupPtrTy::make(DeclGroupRef(D));
3214 }
3215 
3216 Sema::DeclGroupPtrTy
3217 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
3218                                    ArrayRef<OMPClause *> ClauseList) {
3219   OMPRequiresDecl *D = nullptr;
3220   if (!CurContext->isFileContext()) {
3221     Diag(Loc, diag::err_omp_invalid_scope) << "requires";
3222   } else {
3223     D = CheckOMPRequiresDecl(Loc, ClauseList);
3224     if (D) {
3225       CurContext->addDecl(D);
3226       DSAStack->addRequiresDecl(D);
3227     }
3228   }
3229   return DeclGroupPtrTy::make(DeclGroupRef(D));
3230 }
3231 
3232 void Sema::ActOnOpenMPAssumesDirective(SourceLocation Loc,
3233                                        OpenMPDirectiveKind DKind,
3234                                        ArrayRef<std::string> Assumptions,
3235                                        bool SkippedClauses) {
3236   if (!SkippedClauses && Assumptions.empty())
3237     Diag(Loc, diag::err_omp_no_clause_for_directive)
3238         << llvm::omp::getAllAssumeClauseOptions()
3239         << llvm::omp::getOpenMPDirectiveName(DKind);
3240 
3241   auto *AA = AssumptionAttr::Create(Context, llvm::join(Assumptions, ","), Loc);
3242   if (DKind == llvm::omp::Directive::OMPD_begin_assumes) {
3243     OMPAssumeScoped.push_back(AA);
3244     return;
3245   }
3246 
3247   // Global assumes without assumption clauses are ignored.
3248   if (Assumptions.empty())
3249     return;
3250 
3251   assert(DKind == llvm::omp::Directive::OMPD_assumes &&
3252          "Unexpected omp assumption directive!");
3253   OMPAssumeGlobal.push_back(AA);
3254 
3255   // The OMPAssumeGlobal scope above will take care of new declarations but
3256   // we also want to apply the assumption to existing ones, e.g., to
3257   // declarations in included headers. To this end, we traverse all existing
3258   // declaration contexts and annotate function declarations here.
3259   SmallVector<DeclContext *, 8> DeclContexts;
3260   auto *Ctx = CurContext;
3261   while (Ctx->getLexicalParent())
3262     Ctx = Ctx->getLexicalParent();
3263   DeclContexts.push_back(Ctx);
3264   while (!DeclContexts.empty()) {
3265     DeclContext *DC = DeclContexts.pop_back_val();
3266     for (auto *SubDC : DC->decls()) {
3267       if (SubDC->isInvalidDecl())
3268         continue;
3269       if (auto *CTD = dyn_cast<ClassTemplateDecl>(SubDC)) {
3270         DeclContexts.push_back(CTD->getTemplatedDecl());
3271         for (auto *S : CTD->specializations())
3272           DeclContexts.push_back(S);
3273         continue;
3274       }
3275       if (auto *DC = dyn_cast<DeclContext>(SubDC))
3276         DeclContexts.push_back(DC);
3277       if (auto *F = dyn_cast<FunctionDecl>(SubDC)) {
3278         F->addAttr(AA);
3279         continue;
3280       }
3281     }
3282   }
3283 }
3284 
3285 void Sema::ActOnOpenMPEndAssumesDirective() {
3286   assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!");
3287   OMPAssumeScoped.pop_back();
3288 }
3289 
3290 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
3291                                             ArrayRef<OMPClause *> ClauseList) {
3292   /// For target specific clauses, the requires directive cannot be
3293   /// specified after the handling of any of the target regions in the
3294   /// current compilation unit.
3295   ArrayRef<SourceLocation> TargetLocations =
3296       DSAStack->getEncounteredTargetLocs();
3297   SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc();
3298   if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) {
3299     for (const OMPClause *CNew : ClauseList) {
3300       // Check if any of the requires clauses affect target regions.
3301       if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
3302           isa<OMPUnifiedAddressClause>(CNew) ||
3303           isa<OMPReverseOffloadClause>(CNew) ||
3304           isa<OMPDynamicAllocatorsClause>(CNew)) {
3305         Diag(Loc, diag::err_omp_directive_before_requires)
3306             << "target" << getOpenMPClauseName(CNew->getClauseKind());
3307         for (SourceLocation TargetLoc : TargetLocations) {
3308           Diag(TargetLoc, diag::note_omp_requires_encountered_directive)
3309               << "target";
3310         }
3311       } else if (!AtomicLoc.isInvalid() &&
3312                  isa<OMPAtomicDefaultMemOrderClause>(CNew)) {
3313         Diag(Loc, diag::err_omp_directive_before_requires)
3314             << "atomic" << getOpenMPClauseName(CNew->getClauseKind());
3315         Diag(AtomicLoc, diag::note_omp_requires_encountered_directive)
3316             << "atomic";
3317       }
3318     }
3319   }
3320 
3321   if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
3322     return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
3323                                    ClauseList);
3324   return nullptr;
3325 }
3326 
3327 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
3328                               const ValueDecl *D,
3329                               const DSAStackTy::DSAVarData &DVar,
3330                               bool IsLoopIterVar) {
3331   if (DVar.RefExpr) {
3332     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
3333         << getOpenMPClauseName(DVar.CKind);
3334     return;
3335   }
3336   enum {
3337     PDSA_StaticMemberShared,
3338     PDSA_StaticLocalVarShared,
3339     PDSA_LoopIterVarPrivate,
3340     PDSA_LoopIterVarLinear,
3341     PDSA_LoopIterVarLastprivate,
3342     PDSA_ConstVarShared,
3343     PDSA_GlobalVarShared,
3344     PDSA_TaskVarFirstprivate,
3345     PDSA_LocalVarPrivate,
3346     PDSA_Implicit
3347   } Reason = PDSA_Implicit;
3348   bool ReportHint = false;
3349   auto ReportLoc = D->getLocation();
3350   auto *VD = dyn_cast<VarDecl>(D);
3351   if (IsLoopIterVar) {
3352     if (DVar.CKind == OMPC_private)
3353       Reason = PDSA_LoopIterVarPrivate;
3354     else if (DVar.CKind == OMPC_lastprivate)
3355       Reason = PDSA_LoopIterVarLastprivate;
3356     else
3357       Reason = PDSA_LoopIterVarLinear;
3358   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
3359              DVar.CKind == OMPC_firstprivate) {
3360     Reason = PDSA_TaskVarFirstprivate;
3361     ReportLoc = DVar.ImplicitDSALoc;
3362   } else if (VD && VD->isStaticLocal())
3363     Reason = PDSA_StaticLocalVarShared;
3364   else if (VD && VD->isStaticDataMember())
3365     Reason = PDSA_StaticMemberShared;
3366   else if (VD && VD->isFileVarDecl())
3367     Reason = PDSA_GlobalVarShared;
3368   else if (D->getType().isConstant(SemaRef.getASTContext()))
3369     Reason = PDSA_ConstVarShared;
3370   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
3371     ReportHint = true;
3372     Reason = PDSA_LocalVarPrivate;
3373   }
3374   if (Reason != PDSA_Implicit) {
3375     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
3376         << Reason << ReportHint
3377         << getOpenMPDirectiveName(Stack->getCurrentDirective());
3378   } else if (DVar.ImplicitDSALoc.isValid()) {
3379     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
3380         << getOpenMPClauseName(DVar.CKind);
3381   }
3382 }
3383 
3384 static OpenMPMapClauseKind
3385 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M,
3386                              bool IsAggregateOrDeclareTarget) {
3387   OpenMPMapClauseKind Kind = OMPC_MAP_unknown;
3388   switch (M) {
3389   case OMPC_DEFAULTMAP_MODIFIER_alloc:
3390     Kind = OMPC_MAP_alloc;
3391     break;
3392   case OMPC_DEFAULTMAP_MODIFIER_to:
3393     Kind = OMPC_MAP_to;
3394     break;
3395   case OMPC_DEFAULTMAP_MODIFIER_from:
3396     Kind = OMPC_MAP_from;
3397     break;
3398   case OMPC_DEFAULTMAP_MODIFIER_tofrom:
3399     Kind = OMPC_MAP_tofrom;
3400     break;
3401   case OMPC_DEFAULTMAP_MODIFIER_present:
3402     // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description]
3403     // If implicit-behavior is present, each variable referenced in the
3404     // construct in the category specified by variable-category is treated as if
3405     // it had been listed in a map clause with the map-type of alloc and
3406     // map-type-modifier of present.
3407     Kind = OMPC_MAP_alloc;
3408     break;
3409   case OMPC_DEFAULTMAP_MODIFIER_firstprivate:
3410   case OMPC_DEFAULTMAP_MODIFIER_last:
3411     llvm_unreachable("Unexpected defaultmap implicit behavior");
3412   case OMPC_DEFAULTMAP_MODIFIER_none:
3413   case OMPC_DEFAULTMAP_MODIFIER_default:
3414   case OMPC_DEFAULTMAP_MODIFIER_unknown:
3415     // IsAggregateOrDeclareTarget could be true if:
3416     // 1. the implicit behavior for aggregate is tofrom
3417     // 2. it's a declare target link
3418     if (IsAggregateOrDeclareTarget) {
3419       Kind = OMPC_MAP_tofrom;
3420       break;
3421     }
3422     llvm_unreachable("Unexpected defaultmap implicit behavior");
3423   }
3424   assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known");
3425   return Kind;
3426 }
3427 
3428 namespace {
3429 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
3430   DSAStackTy *Stack;
3431   Sema &SemaRef;
3432   bool ErrorFound = false;
3433   bool TryCaptureCXXThisMembers = false;
3434   CapturedStmt *CS = nullptr;
3435   const static unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1;
3436   llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
3437   llvm::SmallVector<Expr *, 4> ImplicitMap[DefaultmapKindNum][OMPC_MAP_delete];
3438   llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
3439       ImplicitMapModifier[DefaultmapKindNum];
3440   Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
3441   llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
3442 
3443   void VisitSubCaptures(OMPExecutableDirective *S) {
3444     // Check implicitly captured variables.
3445     if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
3446       return;
3447     if (S->getDirectiveKind() == OMPD_atomic ||
3448         S->getDirectiveKind() == OMPD_critical ||
3449         S->getDirectiveKind() == OMPD_section ||
3450         S->getDirectiveKind() == OMPD_master ||
3451         S->getDirectiveKind() == OMPD_masked ||
3452         isOpenMPLoopTransformationDirective(S->getDirectiveKind())) {
3453       Visit(S->getAssociatedStmt());
3454       return;
3455     }
3456     visitSubCaptures(S->getInnermostCapturedStmt());
3457     // Try to capture inner this->member references to generate correct mappings
3458     // and diagnostics.
3459     if (TryCaptureCXXThisMembers ||
3460         (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3461          llvm::any_of(S->getInnermostCapturedStmt()->captures(),
3462                       [](const CapturedStmt::Capture &C) {
3463                         return C.capturesThis();
3464                       }))) {
3465       bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers;
3466       TryCaptureCXXThisMembers = true;
3467       Visit(S->getInnermostCapturedStmt()->getCapturedStmt());
3468       TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers;
3469     }
3470     // In tasks firstprivates are not captured anymore, need to analyze them
3471     // explicitly.
3472     if (isOpenMPTaskingDirective(S->getDirectiveKind()) &&
3473         !isOpenMPTaskLoopDirective(S->getDirectiveKind())) {
3474       for (OMPClause *C : S->clauses())
3475         if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) {
3476           for (Expr *Ref : FC->varlists())
3477             Visit(Ref);
3478         }
3479     }
3480   }
3481 
3482 public:
3483   void VisitDeclRefExpr(DeclRefExpr *E) {
3484     if (TryCaptureCXXThisMembers || E->isTypeDependent() ||
3485         E->isValueDependent() || E->containsUnexpandedParameterPack() ||
3486         E->isInstantiationDependent())
3487       return;
3488     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
3489       // Check the datasharing rules for the expressions in the clauses.
3490       if (!CS) {
3491         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3492           if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
3493             Visit(CED->getInit());
3494             return;
3495           }
3496       } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD))
3497         // Do not analyze internal variables and do not enclose them into
3498         // implicit clauses.
3499         return;
3500       VD = VD->getCanonicalDecl();
3501       // Skip internally declared variables.
3502       if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) &&
3503           !Stack->isImplicitTaskFirstprivate(VD))
3504         return;
3505       // Skip allocators in uses_allocators clauses.
3506       if (Stack->isUsesAllocatorsDecl(VD).hasValue())
3507         return;
3508 
3509       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
3510       // Check if the variable has explicit DSA set and stop analysis if it so.
3511       if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
3512         return;
3513 
3514       // Skip internally declared static variables.
3515       llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
3516           OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
3517       if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) &&
3518           (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
3519            !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) &&
3520           !Stack->isImplicitTaskFirstprivate(VD))
3521         return;
3522 
3523       SourceLocation ELoc = E->getExprLoc();
3524       OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3525       // The default(none) clause requires that each variable that is referenced
3526       // in the construct, and does not have a predetermined data-sharing
3527       // attribute, must have its data-sharing attribute explicitly determined
3528       // by being listed in a data-sharing attribute clause.
3529       if (DVar.CKind == OMPC_unknown &&
3530           (Stack->getDefaultDSA() == DSA_none ||
3531            Stack->getDefaultDSA() == DSA_firstprivate) &&
3532           isImplicitOrExplicitTaskingRegion(DKind) &&
3533           VarsWithInheritedDSA.count(VD) == 0) {
3534         bool InheritedDSA = Stack->getDefaultDSA() == DSA_none;
3535         if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) {
3536           DSAStackTy::DSAVarData DVar =
3537               Stack->getImplicitDSA(VD, /*FromParent=*/false);
3538           InheritedDSA = DVar.CKind == OMPC_unknown;
3539         }
3540         if (InheritedDSA)
3541           VarsWithInheritedDSA[VD] = E;
3542         return;
3543       }
3544 
3545       // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description]
3546       // If implicit-behavior is none, each variable referenced in the
3547       // construct that does not have a predetermined data-sharing attribute
3548       // and does not appear in a to or link clause on a declare target
3549       // directive must be listed in a data-mapping attribute clause, a
3550       // data-haring attribute clause (including a data-sharing attribute
3551       // clause on a combined construct where target. is one of the
3552       // constituent constructs), or an is_device_ptr clause.
3553       OpenMPDefaultmapClauseKind ClauseKind =
3554           getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD);
3555       if (SemaRef.getLangOpts().OpenMP >= 50) {
3556         bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) ==
3557                               OMPC_DEFAULTMAP_MODIFIER_none;
3558         if (DVar.CKind == OMPC_unknown && IsModifierNone &&
3559             VarsWithInheritedDSA.count(VD) == 0 && !Res) {
3560           // Only check for data-mapping attribute and is_device_ptr here
3561           // since we have already make sure that the declaration does not
3562           // have a data-sharing attribute above
3563           if (!Stack->checkMappableExprComponentListsForDecl(
3564                   VD, /*CurrentRegionOnly=*/true,
3565                   [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef
3566                            MapExprComponents,
3567                        OpenMPClauseKind) {
3568                     auto MI = MapExprComponents.rbegin();
3569                     auto ME = MapExprComponents.rend();
3570                     return MI != ME && MI->getAssociatedDeclaration() == VD;
3571                   })) {
3572             VarsWithInheritedDSA[VD] = E;
3573             return;
3574           }
3575         }
3576       }
3577       if (SemaRef.getLangOpts().OpenMP > 50) {
3578         bool IsModifierPresent = Stack->getDefaultmapModifier(ClauseKind) ==
3579                                  OMPC_DEFAULTMAP_MODIFIER_present;
3580         if (IsModifierPresent) {
3581           if (llvm::find(ImplicitMapModifier[ClauseKind],
3582                          OMPC_MAP_MODIFIER_present) ==
3583               std::end(ImplicitMapModifier[ClauseKind])) {
3584             ImplicitMapModifier[ClauseKind].push_back(
3585                 OMPC_MAP_MODIFIER_present);
3586           }
3587         }
3588       }
3589 
3590       if (isOpenMPTargetExecutionDirective(DKind) &&
3591           !Stack->isLoopControlVariable(VD).first) {
3592         if (!Stack->checkMappableExprComponentListsForDecl(
3593                 VD, /*CurrentRegionOnly=*/true,
3594                 [this](OMPClauseMappableExprCommon::MappableExprComponentListRef
3595                            StackComponents,
3596                        OpenMPClauseKind) {
3597                   if (SemaRef.LangOpts.OpenMP >= 50)
3598                     return !StackComponents.empty();
3599                   // Variable is used if it has been marked as an array, array
3600                   // section, array shaping or the variable iself.
3601                   return StackComponents.size() == 1 ||
3602                          std::all_of(
3603                              std::next(StackComponents.rbegin()),
3604                              StackComponents.rend(),
3605                              [](const OMPClauseMappableExprCommon::
3606                                     MappableComponent &MC) {
3607                                return MC.getAssociatedDeclaration() ==
3608                                           nullptr &&
3609                                       (isa<OMPArraySectionExpr>(
3610                                            MC.getAssociatedExpression()) ||
3611                                        isa<OMPArrayShapingExpr>(
3612                                            MC.getAssociatedExpression()) ||
3613                                        isa<ArraySubscriptExpr>(
3614                                            MC.getAssociatedExpression()));
3615                              });
3616                 })) {
3617           bool IsFirstprivate = false;
3618           // By default lambdas are captured as firstprivates.
3619           if (const auto *RD =
3620                   VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
3621             IsFirstprivate = RD->isLambda();
3622           IsFirstprivate =
3623               IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res);
3624           if (IsFirstprivate) {
3625             ImplicitFirstprivate.emplace_back(E);
3626           } else {
3627             OpenMPDefaultmapClauseModifier M =
3628                 Stack->getDefaultmapModifier(ClauseKind);
3629             OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3630                 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res);
3631             ImplicitMap[ClauseKind][Kind].emplace_back(E);
3632           }
3633           return;
3634         }
3635       }
3636 
3637       // OpenMP [2.9.3.6, Restrictions, p.2]
3638       //  A list item that appears in a reduction clause of the innermost
3639       //  enclosing worksharing or parallel construct may not be accessed in an
3640       //  explicit task.
3641       DVar = Stack->hasInnermostDSA(
3642           VD,
3643           [](OpenMPClauseKind C, bool AppliedToPointee) {
3644             return C == OMPC_reduction && !AppliedToPointee;
3645           },
3646           [](OpenMPDirectiveKind K) {
3647             return isOpenMPParallelDirective(K) ||
3648                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
3649           },
3650           /*FromParent=*/true);
3651       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
3652         ErrorFound = true;
3653         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3654         reportOriginalDsa(SemaRef, Stack, VD, DVar);
3655         return;
3656       }
3657 
3658       // Define implicit data-sharing attributes for task.
3659       DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
3660       if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) ||
3661            (Stack->getDefaultDSA() == DSA_firstprivate &&
3662             DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) &&
3663           !Stack->isLoopControlVariable(VD).first) {
3664         ImplicitFirstprivate.push_back(E);
3665         return;
3666       }
3667 
3668       // Store implicitly used globals with declare target link for parent
3669       // target.
3670       if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
3671           *Res == OMPDeclareTargetDeclAttr::MT_Link) {
3672         Stack->addToParentTargetRegionLinkGlobals(E);
3673         return;
3674       }
3675     }
3676   }
3677   void VisitMemberExpr(MemberExpr *E) {
3678     if (E->isTypeDependent() || E->isValueDependent() ||
3679         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
3680       return;
3681     auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
3682     OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3683     if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) {
3684       if (!FD)
3685         return;
3686       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
3687       // Check if the variable has explicit DSA set and stop analysis if it
3688       // so.
3689       if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
3690         return;
3691 
3692       if (isOpenMPTargetExecutionDirective(DKind) &&
3693           !Stack->isLoopControlVariable(FD).first &&
3694           !Stack->checkMappableExprComponentListsForDecl(
3695               FD, /*CurrentRegionOnly=*/true,
3696               [](OMPClauseMappableExprCommon::MappableExprComponentListRef
3697                      StackComponents,
3698                  OpenMPClauseKind) {
3699                 return isa<CXXThisExpr>(
3700                     cast<MemberExpr>(
3701                         StackComponents.back().getAssociatedExpression())
3702                         ->getBase()
3703                         ->IgnoreParens());
3704               })) {
3705         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
3706         //  A bit-field cannot appear in a map clause.
3707         //
3708         if (FD->isBitField())
3709           return;
3710 
3711         // Check to see if the member expression is referencing a class that
3712         // has already been explicitly mapped
3713         if (Stack->isClassPreviouslyMapped(TE->getType()))
3714           return;
3715 
3716         OpenMPDefaultmapClauseModifier Modifier =
3717             Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate);
3718         OpenMPDefaultmapClauseKind ClauseKind =
3719             getVariableCategoryFromDecl(SemaRef.getLangOpts(), FD);
3720         OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3721             Modifier, /*IsAggregateOrDeclareTarget*/ true);
3722         ImplicitMap[ClauseKind][Kind].emplace_back(E);
3723         return;
3724       }
3725 
3726       SourceLocation ELoc = E->getExprLoc();
3727       // OpenMP [2.9.3.6, Restrictions, p.2]
3728       //  A list item that appears in a reduction clause of the innermost
3729       //  enclosing worksharing or parallel construct may not be accessed in
3730       //  an  explicit task.
3731       DVar = Stack->hasInnermostDSA(
3732           FD,
3733           [](OpenMPClauseKind C, bool AppliedToPointee) {
3734             return C == OMPC_reduction && !AppliedToPointee;
3735           },
3736           [](OpenMPDirectiveKind K) {
3737             return isOpenMPParallelDirective(K) ||
3738                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
3739           },
3740           /*FromParent=*/true);
3741       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
3742         ErrorFound = true;
3743         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3744         reportOriginalDsa(SemaRef, Stack, FD, DVar);
3745         return;
3746       }
3747 
3748       // Define implicit data-sharing attributes for task.
3749       DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
3750       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
3751           !Stack->isLoopControlVariable(FD).first) {
3752         // Check if there is a captured expression for the current field in the
3753         // region. Do not mark it as firstprivate unless there is no captured
3754         // expression.
3755         // TODO: try to make it firstprivate.
3756         if (DVar.CKind != OMPC_unknown)
3757           ImplicitFirstprivate.push_back(E);
3758       }
3759       return;
3760     }
3761     if (isOpenMPTargetExecutionDirective(DKind)) {
3762       OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
3763       if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
3764                                         Stack->getCurrentDirective(),
3765                                         /*NoDiagnose=*/true))
3766         return;
3767       const auto *VD = cast<ValueDecl>(
3768           CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
3769       if (!Stack->checkMappableExprComponentListsForDecl(
3770               VD, /*CurrentRegionOnly=*/true,
3771               [&CurComponents](
3772                   OMPClauseMappableExprCommon::MappableExprComponentListRef
3773                       StackComponents,
3774                   OpenMPClauseKind) {
3775                 auto CCI = CurComponents.rbegin();
3776                 auto CCE = CurComponents.rend();
3777                 for (const auto &SC : llvm::reverse(StackComponents)) {
3778                   // Do both expressions have the same kind?
3779                   if (CCI->getAssociatedExpression()->getStmtClass() !=
3780                       SC.getAssociatedExpression()->getStmtClass())
3781                     if (!((isa<OMPArraySectionExpr>(
3782                                SC.getAssociatedExpression()) ||
3783                            isa<OMPArrayShapingExpr>(
3784                                SC.getAssociatedExpression())) &&
3785                           isa<ArraySubscriptExpr>(
3786                               CCI->getAssociatedExpression())))
3787                       return false;
3788 
3789                   const Decl *CCD = CCI->getAssociatedDeclaration();
3790                   const Decl *SCD = SC.getAssociatedDeclaration();
3791                   CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
3792                   SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
3793                   if (SCD != CCD)
3794                     return false;
3795                   std::advance(CCI, 1);
3796                   if (CCI == CCE)
3797                     break;
3798                 }
3799                 return true;
3800               })) {
3801         Visit(E->getBase());
3802       }
3803     } else if (!TryCaptureCXXThisMembers) {
3804       Visit(E->getBase());
3805     }
3806   }
3807   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
3808     for (OMPClause *C : S->clauses()) {
3809       // Skip analysis of arguments of implicitly defined firstprivate clause
3810       // for task|target directives.
3811       // Skip analysis of arguments of implicitly defined map clause for target
3812       // directives.
3813       if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
3814                  C->isImplicit() &&
3815                  !isOpenMPTaskingDirective(Stack->getCurrentDirective()))) {
3816         for (Stmt *CC : C->children()) {
3817           if (CC)
3818             Visit(CC);
3819         }
3820       }
3821     }
3822     // Check implicitly captured variables.
3823     VisitSubCaptures(S);
3824   }
3825 
3826   void VisitOMPLoopTransformationDirective(OMPLoopTransformationDirective *S) {
3827     // Loop transformation directives do not introduce data sharing
3828     VisitStmt(S);
3829   }
3830 
3831   void VisitStmt(Stmt *S) {
3832     for (Stmt *C : S->children()) {
3833       if (C) {
3834         // Check implicitly captured variables in the task-based directives to
3835         // check if they must be firstprivatized.
3836         Visit(C);
3837       }
3838     }
3839   }
3840 
3841   void visitSubCaptures(CapturedStmt *S) {
3842     for (const CapturedStmt::Capture &Cap : S->captures()) {
3843       if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
3844         continue;
3845       VarDecl *VD = Cap.getCapturedVar();
3846       // Do not try to map the variable if it or its sub-component was mapped
3847       // already.
3848       if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3849           Stack->checkMappableExprComponentListsForDecl(
3850               VD, /*CurrentRegionOnly=*/true,
3851               [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
3852                  OpenMPClauseKind) { return true; }))
3853         continue;
3854       DeclRefExpr *DRE = buildDeclRefExpr(
3855           SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
3856           Cap.getLocation(), /*RefersToCapture=*/true);
3857       Visit(DRE);
3858     }
3859   }
3860   bool isErrorFound() const { return ErrorFound; }
3861   ArrayRef<Expr *> getImplicitFirstprivate() const {
3862     return ImplicitFirstprivate;
3863   }
3864   ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind DK,
3865                                   OpenMPMapClauseKind MK) const {
3866     return ImplicitMap[DK][MK];
3867   }
3868   ArrayRef<OpenMPMapModifierKind>
3869   getImplicitMapModifier(OpenMPDefaultmapClauseKind Kind) const {
3870     return ImplicitMapModifier[Kind];
3871   }
3872   const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
3873     return VarsWithInheritedDSA;
3874   }
3875 
3876   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
3877       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
3878     // Process declare target link variables for the target directives.
3879     if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
3880       for (DeclRefExpr *E : Stack->getLinkGlobals())
3881         Visit(E);
3882     }
3883   }
3884 };
3885 } // namespace
3886 
3887 static void handleDeclareVariantConstructTrait(DSAStackTy *Stack,
3888                                                OpenMPDirectiveKind DKind,
3889                                                bool ScopeEntry) {
3890   SmallVector<llvm::omp::TraitProperty, 8> Traits;
3891   if (isOpenMPTargetExecutionDirective(DKind))
3892     Traits.emplace_back(llvm::omp::TraitProperty::construct_target_target);
3893   if (isOpenMPTeamsDirective(DKind))
3894     Traits.emplace_back(llvm::omp::TraitProperty::construct_teams_teams);
3895   if (isOpenMPParallelDirective(DKind))
3896     Traits.emplace_back(llvm::omp::TraitProperty::construct_parallel_parallel);
3897   if (isOpenMPWorksharingDirective(DKind))
3898     Traits.emplace_back(llvm::omp::TraitProperty::construct_for_for);
3899   if (isOpenMPSimdDirective(DKind))
3900     Traits.emplace_back(llvm::omp::TraitProperty::construct_simd_simd);
3901   Stack->handleConstructTrait(Traits, ScopeEntry);
3902 }
3903 
3904 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
3905   switch (DKind) {
3906   case OMPD_parallel:
3907   case OMPD_parallel_for:
3908   case OMPD_parallel_for_simd:
3909   case OMPD_parallel_sections:
3910   case OMPD_parallel_master:
3911   case OMPD_teams:
3912   case OMPD_teams_distribute:
3913   case OMPD_teams_distribute_simd: {
3914     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3915     QualType KmpInt32PtrTy =
3916         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3917     Sema::CapturedParamNameType Params[] = {
3918         std::make_pair(".global_tid.", KmpInt32PtrTy),
3919         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3920         std::make_pair(StringRef(), QualType()) // __context with shared vars
3921     };
3922     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3923                              Params);
3924     break;
3925   }
3926   case OMPD_target_teams:
3927   case OMPD_target_parallel:
3928   case OMPD_target_parallel_for:
3929   case OMPD_target_parallel_for_simd:
3930   case OMPD_target_teams_distribute:
3931   case OMPD_target_teams_distribute_simd: {
3932     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3933     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3934     QualType KmpInt32PtrTy =
3935         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3936     QualType Args[] = {VoidPtrTy};
3937     FunctionProtoType::ExtProtoInfo EPI;
3938     EPI.Variadic = true;
3939     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3940     Sema::CapturedParamNameType Params[] = {
3941         std::make_pair(".global_tid.", KmpInt32Ty),
3942         std::make_pair(".part_id.", KmpInt32PtrTy),
3943         std::make_pair(".privates.", VoidPtrTy),
3944         std::make_pair(
3945             ".copy_fn.",
3946             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3947         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3948         std::make_pair(StringRef(), QualType()) // __context with shared vars
3949     };
3950     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3951                              Params, /*OpenMPCaptureLevel=*/0);
3952     // Mark this captured region as inlined, because we don't use outlined
3953     // function directly.
3954     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3955         AlwaysInlineAttr::CreateImplicit(
3956             Context, {}, AttributeCommonInfo::AS_Keyword,
3957             AlwaysInlineAttr::Keyword_forceinline));
3958     Sema::CapturedParamNameType ParamsTarget[] = {
3959         std::make_pair(StringRef(), QualType()) // __context with shared vars
3960     };
3961     // Start a captured region for 'target' with no implicit parameters.
3962     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3963                              ParamsTarget, /*OpenMPCaptureLevel=*/1);
3964     Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
3965         std::make_pair(".global_tid.", KmpInt32PtrTy),
3966         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3967         std::make_pair(StringRef(), QualType()) // __context with shared vars
3968     };
3969     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3970     // the same implicit parameters.
3971     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3972                              ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2);
3973     break;
3974   }
3975   case OMPD_target:
3976   case OMPD_target_simd: {
3977     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3978     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3979     QualType KmpInt32PtrTy =
3980         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3981     QualType Args[] = {VoidPtrTy};
3982     FunctionProtoType::ExtProtoInfo EPI;
3983     EPI.Variadic = true;
3984     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3985     Sema::CapturedParamNameType Params[] = {
3986         std::make_pair(".global_tid.", KmpInt32Ty),
3987         std::make_pair(".part_id.", KmpInt32PtrTy),
3988         std::make_pair(".privates.", VoidPtrTy),
3989         std::make_pair(
3990             ".copy_fn.",
3991             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3992         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3993         std::make_pair(StringRef(), QualType()) // __context with shared vars
3994     };
3995     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3996                              Params, /*OpenMPCaptureLevel=*/0);
3997     // Mark this captured region as inlined, because we don't use outlined
3998     // function directly.
3999     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4000         AlwaysInlineAttr::CreateImplicit(
4001             Context, {}, AttributeCommonInfo::AS_Keyword,
4002             AlwaysInlineAttr::Keyword_forceinline));
4003     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4004                              std::make_pair(StringRef(), QualType()),
4005                              /*OpenMPCaptureLevel=*/1);
4006     break;
4007   }
4008   case OMPD_atomic:
4009   case OMPD_critical:
4010   case OMPD_section:
4011   case OMPD_master:
4012   case OMPD_masked:
4013   case OMPD_tile:
4014   case OMPD_unroll:
4015     break;
4016   case OMPD_loop:
4017     // TODO: 'loop' may require additional parameters depending on the binding.
4018     // Treat similar to OMPD_simd/OMPD_for for now.
4019   case OMPD_simd:
4020   case OMPD_for:
4021   case OMPD_for_simd:
4022   case OMPD_sections:
4023   case OMPD_single:
4024   case OMPD_taskgroup:
4025   case OMPD_distribute:
4026   case OMPD_distribute_simd:
4027   case OMPD_ordered:
4028   case OMPD_target_data:
4029   case OMPD_dispatch: {
4030     Sema::CapturedParamNameType Params[] = {
4031         std::make_pair(StringRef(), QualType()) // __context with shared vars
4032     };
4033     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4034                              Params);
4035     break;
4036   }
4037   case OMPD_task: {
4038     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4039     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4040     QualType KmpInt32PtrTy =
4041         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4042     QualType Args[] = {VoidPtrTy};
4043     FunctionProtoType::ExtProtoInfo EPI;
4044     EPI.Variadic = true;
4045     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4046     Sema::CapturedParamNameType Params[] = {
4047         std::make_pair(".global_tid.", KmpInt32Ty),
4048         std::make_pair(".part_id.", KmpInt32PtrTy),
4049         std::make_pair(".privates.", VoidPtrTy),
4050         std::make_pair(
4051             ".copy_fn.",
4052             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4053         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4054         std::make_pair(StringRef(), QualType()) // __context with shared vars
4055     };
4056     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4057                              Params);
4058     // Mark this captured region as inlined, because we don't use outlined
4059     // function directly.
4060     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4061         AlwaysInlineAttr::CreateImplicit(
4062             Context, {}, AttributeCommonInfo::AS_Keyword,
4063             AlwaysInlineAttr::Keyword_forceinline));
4064     break;
4065   }
4066   case OMPD_taskloop:
4067   case OMPD_taskloop_simd:
4068   case OMPD_master_taskloop:
4069   case OMPD_master_taskloop_simd: {
4070     QualType KmpInt32Ty =
4071         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
4072             .withConst();
4073     QualType KmpUInt64Ty =
4074         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
4075             .withConst();
4076     QualType KmpInt64Ty =
4077         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
4078             .withConst();
4079     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4080     QualType KmpInt32PtrTy =
4081         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4082     QualType Args[] = {VoidPtrTy};
4083     FunctionProtoType::ExtProtoInfo EPI;
4084     EPI.Variadic = true;
4085     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4086     Sema::CapturedParamNameType Params[] = {
4087         std::make_pair(".global_tid.", KmpInt32Ty),
4088         std::make_pair(".part_id.", KmpInt32PtrTy),
4089         std::make_pair(".privates.", VoidPtrTy),
4090         std::make_pair(
4091             ".copy_fn.",
4092             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4093         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4094         std::make_pair(".lb.", KmpUInt64Ty),
4095         std::make_pair(".ub.", KmpUInt64Ty),
4096         std::make_pair(".st.", KmpInt64Ty),
4097         std::make_pair(".liter.", KmpInt32Ty),
4098         std::make_pair(".reductions.", VoidPtrTy),
4099         std::make_pair(StringRef(), QualType()) // __context with shared vars
4100     };
4101     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4102                              Params);
4103     // Mark this captured region as inlined, because we don't use outlined
4104     // function directly.
4105     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4106         AlwaysInlineAttr::CreateImplicit(
4107             Context, {}, AttributeCommonInfo::AS_Keyword,
4108             AlwaysInlineAttr::Keyword_forceinline));
4109     break;
4110   }
4111   case OMPD_parallel_master_taskloop:
4112   case OMPD_parallel_master_taskloop_simd: {
4113     QualType KmpInt32Ty =
4114         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
4115             .withConst();
4116     QualType KmpUInt64Ty =
4117         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
4118             .withConst();
4119     QualType KmpInt64Ty =
4120         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
4121             .withConst();
4122     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4123     QualType KmpInt32PtrTy =
4124         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4125     Sema::CapturedParamNameType ParamsParallel[] = {
4126         std::make_pair(".global_tid.", KmpInt32PtrTy),
4127         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4128         std::make_pair(StringRef(), QualType()) // __context with shared vars
4129     };
4130     // Start a captured region for 'parallel'.
4131     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4132                              ParamsParallel, /*OpenMPCaptureLevel=*/0);
4133     QualType Args[] = {VoidPtrTy};
4134     FunctionProtoType::ExtProtoInfo EPI;
4135     EPI.Variadic = true;
4136     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4137     Sema::CapturedParamNameType Params[] = {
4138         std::make_pair(".global_tid.", KmpInt32Ty),
4139         std::make_pair(".part_id.", KmpInt32PtrTy),
4140         std::make_pair(".privates.", VoidPtrTy),
4141         std::make_pair(
4142             ".copy_fn.",
4143             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4144         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4145         std::make_pair(".lb.", KmpUInt64Ty),
4146         std::make_pair(".ub.", KmpUInt64Ty),
4147         std::make_pair(".st.", KmpInt64Ty),
4148         std::make_pair(".liter.", KmpInt32Ty),
4149         std::make_pair(".reductions.", VoidPtrTy),
4150         std::make_pair(StringRef(), QualType()) // __context with shared vars
4151     };
4152     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4153                              Params, /*OpenMPCaptureLevel=*/1);
4154     // Mark this captured region as inlined, because we don't use outlined
4155     // function directly.
4156     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4157         AlwaysInlineAttr::CreateImplicit(
4158             Context, {}, AttributeCommonInfo::AS_Keyword,
4159             AlwaysInlineAttr::Keyword_forceinline));
4160     break;
4161   }
4162   case OMPD_distribute_parallel_for_simd:
4163   case OMPD_distribute_parallel_for: {
4164     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4165     QualType KmpInt32PtrTy =
4166         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4167     Sema::CapturedParamNameType Params[] = {
4168         std::make_pair(".global_tid.", KmpInt32PtrTy),
4169         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4170         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4171         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4172         std::make_pair(StringRef(), QualType()) // __context with shared vars
4173     };
4174     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4175                              Params);
4176     break;
4177   }
4178   case OMPD_target_teams_distribute_parallel_for:
4179   case OMPD_target_teams_distribute_parallel_for_simd: {
4180     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4181     QualType KmpInt32PtrTy =
4182         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4183     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4184 
4185     QualType Args[] = {VoidPtrTy};
4186     FunctionProtoType::ExtProtoInfo EPI;
4187     EPI.Variadic = true;
4188     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4189     Sema::CapturedParamNameType Params[] = {
4190         std::make_pair(".global_tid.", KmpInt32Ty),
4191         std::make_pair(".part_id.", KmpInt32PtrTy),
4192         std::make_pair(".privates.", VoidPtrTy),
4193         std::make_pair(
4194             ".copy_fn.",
4195             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4196         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4197         std::make_pair(StringRef(), QualType()) // __context with shared vars
4198     };
4199     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4200                              Params, /*OpenMPCaptureLevel=*/0);
4201     // Mark this captured region as inlined, because we don't use outlined
4202     // function directly.
4203     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4204         AlwaysInlineAttr::CreateImplicit(
4205             Context, {}, AttributeCommonInfo::AS_Keyword,
4206             AlwaysInlineAttr::Keyword_forceinline));
4207     Sema::CapturedParamNameType ParamsTarget[] = {
4208         std::make_pair(StringRef(), QualType()) // __context with shared vars
4209     };
4210     // Start a captured region for 'target' with no implicit parameters.
4211     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4212                              ParamsTarget, /*OpenMPCaptureLevel=*/1);
4213 
4214     Sema::CapturedParamNameType ParamsTeams[] = {
4215         std::make_pair(".global_tid.", KmpInt32PtrTy),
4216         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4217         std::make_pair(StringRef(), QualType()) // __context with shared vars
4218     };
4219     // Start a captured region for 'target' with no implicit parameters.
4220     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4221                              ParamsTeams, /*OpenMPCaptureLevel=*/2);
4222 
4223     Sema::CapturedParamNameType ParamsParallel[] = {
4224         std::make_pair(".global_tid.", KmpInt32PtrTy),
4225         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4226         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4227         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4228         std::make_pair(StringRef(), QualType()) // __context with shared vars
4229     };
4230     // Start a captured region for 'teams' or 'parallel'.  Both regions have
4231     // the same implicit parameters.
4232     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4233                              ParamsParallel, /*OpenMPCaptureLevel=*/3);
4234     break;
4235   }
4236 
4237   case OMPD_teams_distribute_parallel_for:
4238   case OMPD_teams_distribute_parallel_for_simd: {
4239     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4240     QualType KmpInt32PtrTy =
4241         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4242 
4243     Sema::CapturedParamNameType ParamsTeams[] = {
4244         std::make_pair(".global_tid.", KmpInt32PtrTy),
4245         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4246         std::make_pair(StringRef(), QualType()) // __context with shared vars
4247     };
4248     // Start a captured region for 'target' with no implicit parameters.
4249     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4250                              ParamsTeams, /*OpenMPCaptureLevel=*/0);
4251 
4252     Sema::CapturedParamNameType ParamsParallel[] = {
4253         std::make_pair(".global_tid.", KmpInt32PtrTy),
4254         std::make_pair(".bound_tid.", KmpInt32PtrTy),
4255         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4256         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4257         std::make_pair(StringRef(), QualType()) // __context with shared vars
4258     };
4259     // Start a captured region for 'teams' or 'parallel'.  Both regions have
4260     // the same implicit parameters.
4261     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4262                              ParamsParallel, /*OpenMPCaptureLevel=*/1);
4263     break;
4264   }
4265   case OMPD_target_update:
4266   case OMPD_target_enter_data:
4267   case OMPD_target_exit_data: {
4268     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4269     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4270     QualType KmpInt32PtrTy =
4271         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4272     QualType Args[] = {VoidPtrTy};
4273     FunctionProtoType::ExtProtoInfo EPI;
4274     EPI.Variadic = true;
4275     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4276     Sema::CapturedParamNameType Params[] = {
4277         std::make_pair(".global_tid.", KmpInt32Ty),
4278         std::make_pair(".part_id.", KmpInt32PtrTy),
4279         std::make_pair(".privates.", VoidPtrTy),
4280         std::make_pair(
4281             ".copy_fn.",
4282             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4283         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4284         std::make_pair(StringRef(), QualType()) // __context with shared vars
4285     };
4286     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4287                              Params);
4288     // Mark this captured region as inlined, because we don't use outlined
4289     // function directly.
4290     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4291         AlwaysInlineAttr::CreateImplicit(
4292             Context, {}, AttributeCommonInfo::AS_Keyword,
4293             AlwaysInlineAttr::Keyword_forceinline));
4294     break;
4295   }
4296   case OMPD_threadprivate:
4297   case OMPD_allocate:
4298   case OMPD_taskyield:
4299   case OMPD_barrier:
4300   case OMPD_taskwait:
4301   case OMPD_cancellation_point:
4302   case OMPD_cancel:
4303   case OMPD_flush:
4304   case OMPD_depobj:
4305   case OMPD_scan:
4306   case OMPD_declare_reduction:
4307   case OMPD_declare_mapper:
4308   case OMPD_declare_simd:
4309   case OMPD_declare_target:
4310   case OMPD_end_declare_target:
4311   case OMPD_requires:
4312   case OMPD_declare_variant:
4313   case OMPD_begin_declare_variant:
4314   case OMPD_end_declare_variant:
4315   case OMPD_metadirective:
4316     llvm_unreachable("OpenMP Directive is not allowed");
4317   case OMPD_unknown:
4318   default:
4319     llvm_unreachable("Unknown OpenMP directive");
4320   }
4321   DSAStack->setContext(CurContext);
4322   handleDeclareVariantConstructTrait(DSAStack, DKind, /* ScopeEntry */ true);
4323 }
4324 
4325 int Sema::getNumberOfConstructScopes(unsigned Level) const {
4326   return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
4327 }
4328 
4329 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
4330   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4331   getOpenMPCaptureRegions(CaptureRegions, DKind);
4332   return CaptureRegions.size();
4333 }
4334 
4335 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
4336                                              Expr *CaptureExpr, bool WithInit,
4337                                              bool AsExpression) {
4338   assert(CaptureExpr);
4339   ASTContext &C = S.getASTContext();
4340   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
4341   QualType Ty = Init->getType();
4342   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
4343     if (S.getLangOpts().CPlusPlus) {
4344       Ty = C.getLValueReferenceType(Ty);
4345     } else {
4346       Ty = C.getPointerType(Ty);
4347       ExprResult Res =
4348           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
4349       if (!Res.isUsable())
4350         return nullptr;
4351       Init = Res.get();
4352     }
4353     WithInit = true;
4354   }
4355   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
4356                                           CaptureExpr->getBeginLoc());
4357   if (!WithInit)
4358     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
4359   S.CurContext->addHiddenDecl(CED);
4360   Sema::TentativeAnalysisScope Trap(S);
4361   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
4362   return CED;
4363 }
4364 
4365 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
4366                                  bool WithInit) {
4367   OMPCapturedExprDecl *CD;
4368   if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
4369     CD = cast<OMPCapturedExprDecl>(VD);
4370   else
4371     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
4372                           /*AsExpression=*/false);
4373   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
4374                           CaptureExpr->getExprLoc());
4375 }
4376 
4377 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
4378   CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
4379   if (!Ref) {
4380     OMPCapturedExprDecl *CD = buildCaptureDecl(
4381         S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
4382         /*WithInit=*/true, /*AsExpression=*/true);
4383     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
4384                            CaptureExpr->getExprLoc());
4385   }
4386   ExprResult Res = Ref;
4387   if (!S.getLangOpts().CPlusPlus &&
4388       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
4389       Ref->getType()->isPointerType()) {
4390     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
4391     if (!Res.isUsable())
4392       return ExprError();
4393   }
4394   return S.DefaultLvalueConversion(Res.get());
4395 }
4396 
4397 namespace {
4398 // OpenMP directives parsed in this section are represented as a
4399 // CapturedStatement with an associated statement.  If a syntax error
4400 // is detected during the parsing of the associated statement, the
4401 // compiler must abort processing and close the CapturedStatement.
4402 //
4403 // Combined directives such as 'target parallel' have more than one
4404 // nested CapturedStatements.  This RAII ensures that we unwind out
4405 // of all the nested CapturedStatements when an error is found.
4406 class CaptureRegionUnwinderRAII {
4407 private:
4408   Sema &S;
4409   bool &ErrorFound;
4410   OpenMPDirectiveKind DKind = OMPD_unknown;
4411 
4412 public:
4413   CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
4414                             OpenMPDirectiveKind DKind)
4415       : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
4416   ~CaptureRegionUnwinderRAII() {
4417     if (ErrorFound) {
4418       int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
4419       while (--ThisCaptureLevel >= 0)
4420         S.ActOnCapturedRegionError();
4421     }
4422   }
4423 };
4424 } // namespace
4425 
4426 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
4427   // Capture variables captured by reference in lambdas for target-based
4428   // directives.
4429   if (!CurContext->isDependentContext() &&
4430       (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
4431        isOpenMPTargetDataManagementDirective(
4432            DSAStack->getCurrentDirective()))) {
4433     QualType Type = V->getType();
4434     if (const auto *RD = Type.getCanonicalType()
4435                              .getNonReferenceType()
4436                              ->getAsCXXRecordDecl()) {
4437       bool SavedForceCaptureByReferenceInTargetExecutable =
4438           DSAStack->isForceCaptureByReferenceInTargetExecutable();
4439       DSAStack->setForceCaptureByReferenceInTargetExecutable(
4440           /*V=*/true);
4441       if (RD->isLambda()) {
4442         llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
4443         FieldDecl *ThisCapture;
4444         RD->getCaptureFields(Captures, ThisCapture);
4445         for (const LambdaCapture &LC : RD->captures()) {
4446           if (LC.getCaptureKind() == LCK_ByRef) {
4447             VarDecl *VD = LC.getCapturedVar();
4448             DeclContext *VDC = VD->getDeclContext();
4449             if (!VDC->Encloses(CurContext))
4450               continue;
4451             MarkVariableReferenced(LC.getLocation(), VD);
4452           } else if (LC.getCaptureKind() == LCK_This) {
4453             QualType ThisTy = getCurrentThisType();
4454             if (!ThisTy.isNull() &&
4455                 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
4456               CheckCXXThisCapture(LC.getLocation());
4457           }
4458         }
4459       }
4460       DSAStack->setForceCaptureByReferenceInTargetExecutable(
4461           SavedForceCaptureByReferenceInTargetExecutable);
4462     }
4463   }
4464 }
4465 
4466 static bool checkOrderedOrderSpecified(Sema &S,
4467                                        const ArrayRef<OMPClause *> Clauses) {
4468   const OMPOrderedClause *Ordered = nullptr;
4469   const OMPOrderClause *Order = nullptr;
4470 
4471   for (const OMPClause *Clause : Clauses) {
4472     if (Clause->getClauseKind() == OMPC_ordered)
4473       Ordered = cast<OMPOrderedClause>(Clause);
4474     else if (Clause->getClauseKind() == OMPC_order) {
4475       Order = cast<OMPOrderClause>(Clause);
4476       if (Order->getKind() != OMPC_ORDER_concurrent)
4477         Order = nullptr;
4478     }
4479     if (Ordered && Order)
4480       break;
4481   }
4482 
4483   if (Ordered && Order) {
4484     S.Diag(Order->getKindKwLoc(),
4485            diag::err_omp_simple_clause_incompatible_with_ordered)
4486         << getOpenMPClauseName(OMPC_order)
4487         << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent)
4488         << SourceRange(Order->getBeginLoc(), Order->getEndLoc());
4489     S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param)
4490         << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc());
4491     return true;
4492   }
4493   return false;
4494 }
4495 
4496 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
4497                                       ArrayRef<OMPClause *> Clauses) {
4498   handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(),
4499                                      /* ScopeEntry */ false);
4500   if (DSAStack->getCurrentDirective() == OMPD_atomic ||
4501       DSAStack->getCurrentDirective() == OMPD_critical ||
4502       DSAStack->getCurrentDirective() == OMPD_section ||
4503       DSAStack->getCurrentDirective() == OMPD_master ||
4504       DSAStack->getCurrentDirective() == OMPD_masked)
4505     return S;
4506 
4507   bool ErrorFound = false;
4508   CaptureRegionUnwinderRAII CaptureRegionUnwinder(
4509       *this, ErrorFound, DSAStack->getCurrentDirective());
4510   if (!S.isUsable()) {
4511     ErrorFound = true;
4512     return StmtError();
4513   }
4514 
4515   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4516   getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
4517   OMPOrderedClause *OC = nullptr;
4518   OMPScheduleClause *SC = nullptr;
4519   SmallVector<const OMPLinearClause *, 4> LCs;
4520   SmallVector<const OMPClauseWithPreInit *, 4> PICs;
4521   // This is required for proper codegen.
4522   for (OMPClause *Clause : Clauses) {
4523     if (!LangOpts.OpenMPSimd &&
4524         isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
4525         Clause->getClauseKind() == OMPC_in_reduction) {
4526       // Capture taskgroup task_reduction descriptors inside the tasking regions
4527       // with the corresponding in_reduction items.
4528       auto *IRC = cast<OMPInReductionClause>(Clause);
4529       for (Expr *E : IRC->taskgroup_descriptors())
4530         if (E)
4531           MarkDeclarationsReferencedInExpr(E);
4532     }
4533     if (isOpenMPPrivate(Clause->getClauseKind()) ||
4534         Clause->getClauseKind() == OMPC_copyprivate ||
4535         (getLangOpts().OpenMPUseTLS &&
4536          getASTContext().getTargetInfo().isTLSSupported() &&
4537          Clause->getClauseKind() == OMPC_copyin)) {
4538       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
4539       // Mark all variables in private list clauses as used in inner region.
4540       for (Stmt *VarRef : Clause->children()) {
4541         if (auto *E = cast_or_null<Expr>(VarRef)) {
4542           MarkDeclarationsReferencedInExpr(E);
4543         }
4544       }
4545       DSAStack->setForceVarCapturing(/*V=*/false);
4546     } else if (isOpenMPLoopTransformationDirective(
4547                    DSAStack->getCurrentDirective())) {
4548       assert(CaptureRegions.empty() &&
4549              "No captured regions in loop transformation directives.");
4550     } else if (CaptureRegions.size() > 1 ||
4551                CaptureRegions.back() != OMPD_unknown) {
4552       if (auto *C = OMPClauseWithPreInit::get(Clause))
4553         PICs.push_back(C);
4554       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
4555         if (Expr *E = C->getPostUpdateExpr())
4556           MarkDeclarationsReferencedInExpr(E);
4557       }
4558     }
4559     if (Clause->getClauseKind() == OMPC_schedule)
4560       SC = cast<OMPScheduleClause>(Clause);
4561     else if (Clause->getClauseKind() == OMPC_ordered)
4562       OC = cast<OMPOrderedClause>(Clause);
4563     else if (Clause->getClauseKind() == OMPC_linear)
4564       LCs.push_back(cast<OMPLinearClause>(Clause));
4565   }
4566   // Capture allocator expressions if used.
4567   for (Expr *E : DSAStack->getInnerAllocators())
4568     MarkDeclarationsReferencedInExpr(E);
4569   // OpenMP, 2.7.1 Loop Construct, Restrictions
4570   // The nonmonotonic modifier cannot be specified if an ordered clause is
4571   // specified.
4572   if (SC &&
4573       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
4574        SC->getSecondScheduleModifier() ==
4575            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
4576       OC) {
4577     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
4578              ? SC->getFirstScheduleModifierLoc()
4579              : SC->getSecondScheduleModifierLoc(),
4580          diag::err_omp_simple_clause_incompatible_with_ordered)
4581         << getOpenMPClauseName(OMPC_schedule)
4582         << getOpenMPSimpleClauseTypeName(OMPC_schedule,
4583                                          OMPC_SCHEDULE_MODIFIER_nonmonotonic)
4584         << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4585     ErrorFound = true;
4586   }
4587   // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions.
4588   // If an order(concurrent) clause is present, an ordered clause may not appear
4589   // on the same directive.
4590   if (checkOrderedOrderSpecified(*this, Clauses))
4591     ErrorFound = true;
4592   if (!LCs.empty() && OC && OC->getNumForLoops()) {
4593     for (const OMPLinearClause *C : LCs) {
4594       Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
4595           << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4596     }
4597     ErrorFound = true;
4598   }
4599   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
4600       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
4601       OC->getNumForLoops()) {
4602     Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
4603         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
4604     ErrorFound = true;
4605   }
4606   if (ErrorFound) {
4607     return StmtError();
4608   }
4609   StmtResult SR = S;
4610   unsigned CompletedRegions = 0;
4611   for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
4612     // Mark all variables in private list clauses as used in inner region.
4613     // Required for proper codegen of combined directives.
4614     // TODO: add processing for other clauses.
4615     if (ThisCaptureRegion != OMPD_unknown) {
4616       for (const clang::OMPClauseWithPreInit *C : PICs) {
4617         OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
4618         // Find the particular capture region for the clause if the
4619         // directive is a combined one with multiple capture regions.
4620         // If the directive is not a combined one, the capture region
4621         // associated with the clause is OMPD_unknown and is generated
4622         // only once.
4623         if (CaptureRegion == ThisCaptureRegion ||
4624             CaptureRegion == OMPD_unknown) {
4625           if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
4626             for (Decl *D : DS->decls())
4627               MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
4628           }
4629         }
4630       }
4631     }
4632     if (ThisCaptureRegion == OMPD_target) {
4633       // Capture allocator traits in the target region. They are used implicitly
4634       // and, thus, are not captured by default.
4635       for (OMPClause *C : Clauses) {
4636         if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) {
4637           for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End;
4638                ++I) {
4639             OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I);
4640             if (Expr *E = D.AllocatorTraits)
4641               MarkDeclarationsReferencedInExpr(E);
4642           }
4643           continue;
4644         }
4645       }
4646     }
4647     if (ThisCaptureRegion == OMPD_parallel) {
4648       // Capture temp arrays for inscan reductions and locals in aligned
4649       // clauses.
4650       for (OMPClause *C : Clauses) {
4651         if (auto *RC = dyn_cast<OMPReductionClause>(C)) {
4652           if (RC->getModifier() != OMPC_REDUCTION_inscan)
4653             continue;
4654           for (Expr *E : RC->copy_array_temps())
4655             MarkDeclarationsReferencedInExpr(E);
4656         }
4657         if (auto *AC = dyn_cast<OMPAlignedClause>(C)) {
4658           for (Expr *E : AC->varlists())
4659             MarkDeclarationsReferencedInExpr(E);
4660         }
4661       }
4662     }
4663     if (++CompletedRegions == CaptureRegions.size())
4664       DSAStack->setBodyComplete();
4665     SR = ActOnCapturedRegionEnd(SR.get());
4666   }
4667   return SR;
4668 }
4669 
4670 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
4671                               OpenMPDirectiveKind CancelRegion,
4672                               SourceLocation StartLoc) {
4673   // CancelRegion is only needed for cancel and cancellation_point.
4674   if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
4675     return false;
4676 
4677   if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
4678       CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
4679     return false;
4680 
4681   SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4682       << getOpenMPDirectiveName(CancelRegion);
4683   return true;
4684 }
4685 
4686 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
4687                                   OpenMPDirectiveKind CurrentRegion,
4688                                   const DeclarationNameInfo &CurrentName,
4689                                   OpenMPDirectiveKind CancelRegion,
4690                                   SourceLocation StartLoc) {
4691   if (Stack->getCurScope()) {
4692     OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
4693     OpenMPDirectiveKind OffendingRegion = ParentRegion;
4694     bool NestingProhibited = false;
4695     bool CloseNesting = true;
4696     bool OrphanSeen = false;
4697     enum {
4698       NoRecommend,
4699       ShouldBeInParallelRegion,
4700       ShouldBeInOrderedRegion,
4701       ShouldBeInTargetRegion,
4702       ShouldBeInTeamsRegion,
4703       ShouldBeInLoopSimdRegion,
4704     } Recommend = NoRecommend;
4705     if (isOpenMPSimdDirective(ParentRegion) &&
4706         ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) ||
4707          (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered &&
4708           CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic &&
4709           CurrentRegion != OMPD_scan))) {
4710       // OpenMP [2.16, Nesting of Regions]
4711       // OpenMP constructs may not be nested inside a simd region.
4712       // OpenMP [2.8.1,simd Construct, Restrictions]
4713       // An ordered construct with the simd clause is the only OpenMP
4714       // construct that can appear in the simd region.
4715       // Allowing a SIMD construct nested in another SIMD construct is an
4716       // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
4717       // message.
4718       // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions]
4719       // The only OpenMP constructs that can be encountered during execution of
4720       // a simd region are the atomic construct, the loop construct, the simd
4721       // construct and the ordered construct with the simd clause.
4722       SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
4723                                  ? diag::err_omp_prohibited_region_simd
4724                                  : diag::warn_omp_nesting_simd)
4725           << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0);
4726       return CurrentRegion != OMPD_simd;
4727     }
4728     if (ParentRegion == OMPD_atomic) {
4729       // OpenMP [2.16, Nesting of Regions]
4730       // OpenMP constructs may not be nested inside an atomic region.
4731       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
4732       return true;
4733     }
4734     if (CurrentRegion == OMPD_section) {
4735       // OpenMP [2.7.2, sections Construct, Restrictions]
4736       // Orphaned section directives are prohibited. That is, the section
4737       // directives must appear within the sections construct and must not be
4738       // encountered elsewhere in the sections region.
4739       if (ParentRegion != OMPD_sections &&
4740           ParentRegion != OMPD_parallel_sections) {
4741         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
4742             << (ParentRegion != OMPD_unknown)
4743             << getOpenMPDirectiveName(ParentRegion);
4744         return true;
4745       }
4746       return false;
4747     }
4748     // Allow some constructs (except teams and cancellation constructs) to be
4749     // orphaned (they could be used in functions, called from OpenMP regions
4750     // with the required preconditions).
4751     if (ParentRegion == OMPD_unknown &&
4752         !isOpenMPNestingTeamsDirective(CurrentRegion) &&
4753         CurrentRegion != OMPD_cancellation_point &&
4754         CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan)
4755       return false;
4756     if (CurrentRegion == OMPD_cancellation_point ||
4757         CurrentRegion == OMPD_cancel) {
4758       // OpenMP [2.16, Nesting of Regions]
4759       // A cancellation point construct for which construct-type-clause is
4760       // taskgroup must be nested inside a task construct. A cancellation
4761       // point construct for which construct-type-clause is not taskgroup must
4762       // be closely nested inside an OpenMP construct that matches the type
4763       // specified in construct-type-clause.
4764       // A cancel construct for which construct-type-clause is taskgroup must be
4765       // nested inside a task construct. A cancel construct for which
4766       // construct-type-clause is not taskgroup must be closely nested inside an
4767       // OpenMP construct that matches the type specified in
4768       // construct-type-clause.
4769       NestingProhibited =
4770           !((CancelRegion == OMPD_parallel &&
4771              (ParentRegion == OMPD_parallel ||
4772               ParentRegion == OMPD_target_parallel)) ||
4773             (CancelRegion == OMPD_for &&
4774              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
4775               ParentRegion == OMPD_target_parallel_for ||
4776               ParentRegion == OMPD_distribute_parallel_for ||
4777               ParentRegion == OMPD_teams_distribute_parallel_for ||
4778               ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
4779             (CancelRegion == OMPD_taskgroup &&
4780              (ParentRegion == OMPD_task ||
4781               (SemaRef.getLangOpts().OpenMP >= 50 &&
4782                (ParentRegion == OMPD_taskloop ||
4783                 ParentRegion == OMPD_master_taskloop ||
4784                 ParentRegion == OMPD_parallel_master_taskloop)))) ||
4785             (CancelRegion == OMPD_sections &&
4786              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
4787               ParentRegion == OMPD_parallel_sections)));
4788       OrphanSeen = ParentRegion == OMPD_unknown;
4789     } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) {
4790       // OpenMP 5.1 [2.22, Nesting of Regions]
4791       // A masked region may not be closely nested inside a worksharing, loop,
4792       // atomic, task, or taskloop region.
4793       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4794                           isOpenMPGenericLoopDirective(ParentRegion) ||
4795                           isOpenMPTaskingDirective(ParentRegion);
4796     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
4797       // OpenMP [2.16, Nesting of Regions]
4798       // A critical region may not be nested (closely or otherwise) inside a
4799       // critical region with the same name. Note that this restriction is not
4800       // sufficient to prevent deadlock.
4801       SourceLocation PreviousCriticalLoc;
4802       bool DeadLock = Stack->hasDirective(
4803           [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
4804                                               const DeclarationNameInfo &DNI,
4805                                               SourceLocation Loc) {
4806             if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
4807               PreviousCriticalLoc = Loc;
4808               return true;
4809             }
4810             return false;
4811           },
4812           false /* skip top directive */);
4813       if (DeadLock) {
4814         SemaRef.Diag(StartLoc,
4815                      diag::err_omp_prohibited_region_critical_same_name)
4816             << CurrentName.getName();
4817         if (PreviousCriticalLoc.isValid())
4818           SemaRef.Diag(PreviousCriticalLoc,
4819                        diag::note_omp_previous_critical_region);
4820         return true;
4821       }
4822     } else if (CurrentRegion == OMPD_barrier) {
4823       // OpenMP 5.1 [2.22, Nesting of Regions]
4824       // A barrier region may not be closely nested inside a worksharing, loop,
4825       // task, taskloop, critical, ordered, atomic, or masked region.
4826       NestingProhibited =
4827           isOpenMPWorksharingDirective(ParentRegion) ||
4828           isOpenMPGenericLoopDirective(ParentRegion) ||
4829           isOpenMPTaskingDirective(ParentRegion) ||
4830           ParentRegion == OMPD_master || ParentRegion == OMPD_masked ||
4831           ParentRegion == OMPD_parallel_master ||
4832           ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
4833     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
4834                !isOpenMPParallelDirective(CurrentRegion) &&
4835                !isOpenMPTeamsDirective(CurrentRegion)) {
4836       // OpenMP 5.1 [2.22, Nesting of Regions]
4837       // A loop region that binds to a parallel region or a worksharing region
4838       // may not be closely nested inside a worksharing, loop, task, taskloop,
4839       // critical, ordered, atomic, or masked region.
4840       NestingProhibited =
4841           isOpenMPWorksharingDirective(ParentRegion) ||
4842           isOpenMPGenericLoopDirective(ParentRegion) ||
4843           isOpenMPTaskingDirective(ParentRegion) ||
4844           ParentRegion == OMPD_master || ParentRegion == OMPD_masked ||
4845           ParentRegion == OMPD_parallel_master ||
4846           ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
4847       Recommend = ShouldBeInParallelRegion;
4848     } else if (CurrentRegion == OMPD_ordered) {
4849       // OpenMP [2.16, Nesting of Regions]
4850       // An ordered region may not be closely nested inside a critical,
4851       // atomic, or explicit task region.
4852       // An ordered region must be closely nested inside a loop region (or
4853       // parallel loop region) with an ordered clause.
4854       // OpenMP [2.8.1,simd Construct, Restrictions]
4855       // An ordered construct with the simd clause is the only OpenMP construct
4856       // that can appear in the simd region.
4857       NestingProhibited = ParentRegion == OMPD_critical ||
4858                           isOpenMPTaskingDirective(ParentRegion) ||
4859                           !(isOpenMPSimdDirective(ParentRegion) ||
4860                             Stack->isParentOrderedRegion());
4861       Recommend = ShouldBeInOrderedRegion;
4862     } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
4863       // OpenMP [2.16, Nesting of Regions]
4864       // If specified, a teams construct must be contained within a target
4865       // construct.
4866       NestingProhibited =
4867           (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) ||
4868           (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown &&
4869            ParentRegion != OMPD_target);
4870       OrphanSeen = ParentRegion == OMPD_unknown;
4871       Recommend = ShouldBeInTargetRegion;
4872     } else if (CurrentRegion == OMPD_scan) {
4873       // OpenMP [2.16, Nesting of Regions]
4874       // If specified, a teams construct must be contained within a target
4875       // construct.
4876       NestingProhibited =
4877           SemaRef.LangOpts.OpenMP < 50 ||
4878           (ParentRegion != OMPD_simd && ParentRegion != OMPD_for &&
4879            ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for &&
4880            ParentRegion != OMPD_parallel_for_simd);
4881       OrphanSeen = ParentRegion == OMPD_unknown;
4882       Recommend = ShouldBeInLoopSimdRegion;
4883     }
4884     if (!NestingProhibited &&
4885         !isOpenMPTargetExecutionDirective(CurrentRegion) &&
4886         !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
4887         (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
4888       // OpenMP [5.1, 2.22, Nesting of Regions]
4889       // distribute, distribute simd, distribute parallel worksharing-loop,
4890       // distribute parallel worksharing-loop SIMD, loop, parallel regions,
4891       // including any parallel regions arising from combined constructs,
4892       // omp_get_num_teams() regions, and omp_get_team_num() regions are the
4893       // only OpenMP regions that may be strictly nested inside the teams
4894       // region.
4895       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
4896                           !isOpenMPDistributeDirective(CurrentRegion) &&
4897                           CurrentRegion != OMPD_loop;
4898       Recommend = ShouldBeInParallelRegion;
4899     }
4900     if (!NestingProhibited &&
4901         isOpenMPNestingDistributeDirective(CurrentRegion)) {
4902       // OpenMP 4.5 [2.17 Nesting of Regions]
4903       // The region associated with the distribute construct must be strictly
4904       // nested inside a teams region
4905       NestingProhibited =
4906           (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
4907       Recommend = ShouldBeInTeamsRegion;
4908     }
4909     if (!NestingProhibited &&
4910         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
4911          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
4912       // OpenMP 4.5 [2.17 Nesting of Regions]
4913       // If a target, target update, target data, target enter data, or
4914       // target exit data construct is encountered during execution of a
4915       // target region, the behavior is unspecified.
4916       NestingProhibited = Stack->hasDirective(
4917           [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
4918                              SourceLocation) {
4919             if (isOpenMPTargetExecutionDirective(K)) {
4920               OffendingRegion = K;
4921               return true;
4922             }
4923             return false;
4924           },
4925           false /* don't skip top directive */);
4926       CloseNesting = false;
4927     }
4928     if (NestingProhibited) {
4929       if (OrphanSeen) {
4930         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
4931             << getOpenMPDirectiveName(CurrentRegion) << Recommend;
4932       } else {
4933         SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
4934             << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
4935             << Recommend << getOpenMPDirectiveName(CurrentRegion);
4936       }
4937       return true;
4938     }
4939   }
4940   return false;
4941 }
4942 
4943 struct Kind2Unsigned {
4944   using argument_type = OpenMPDirectiveKind;
4945   unsigned operator()(argument_type DK) { return unsigned(DK); }
4946 };
4947 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
4948                            ArrayRef<OMPClause *> Clauses,
4949                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
4950   bool ErrorFound = false;
4951   unsigned NamedModifiersNumber = 0;
4952   llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers;
4953   FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1);
4954   SmallVector<SourceLocation, 4> NameModifierLoc;
4955   for (const OMPClause *C : Clauses) {
4956     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
4957       // At most one if clause without a directive-name-modifier can appear on
4958       // the directive.
4959       OpenMPDirectiveKind CurNM = IC->getNameModifier();
4960       if (FoundNameModifiers[CurNM]) {
4961         S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
4962             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
4963             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
4964         ErrorFound = true;
4965       } else if (CurNM != OMPD_unknown) {
4966         NameModifierLoc.push_back(IC->getNameModifierLoc());
4967         ++NamedModifiersNumber;
4968       }
4969       FoundNameModifiers[CurNM] = IC;
4970       if (CurNM == OMPD_unknown)
4971         continue;
4972       // Check if the specified name modifier is allowed for the current
4973       // directive.
4974       // At most one if clause with the particular directive-name-modifier can
4975       // appear on the directive.
4976       if (!llvm::is_contained(AllowedNameModifiers, CurNM)) {
4977         S.Diag(IC->getNameModifierLoc(),
4978                diag::err_omp_wrong_if_directive_name_modifier)
4979             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
4980         ErrorFound = true;
4981       }
4982     }
4983   }
4984   // If any if clause on the directive includes a directive-name-modifier then
4985   // all if clauses on the directive must include a directive-name-modifier.
4986   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
4987     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
4988       S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
4989              diag::err_omp_no_more_if_clause);
4990     } else {
4991       std::string Values;
4992       std::string Sep(", ");
4993       unsigned AllowedCnt = 0;
4994       unsigned TotalAllowedNum =
4995           AllowedNameModifiers.size() - NamedModifiersNumber;
4996       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
4997            ++Cnt) {
4998         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
4999         if (!FoundNameModifiers[NM]) {
5000           Values += "'";
5001           Values += getOpenMPDirectiveName(NM);
5002           Values += "'";
5003           if (AllowedCnt + 2 == TotalAllowedNum)
5004             Values += " or ";
5005           else if (AllowedCnt + 1 != TotalAllowedNum)
5006             Values += Sep;
5007           ++AllowedCnt;
5008         }
5009       }
5010       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
5011              diag::err_omp_unnamed_if_clause)
5012           << (TotalAllowedNum > 1) << Values;
5013     }
5014     for (SourceLocation Loc : NameModifierLoc) {
5015       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
5016     }
5017     ErrorFound = true;
5018   }
5019   return ErrorFound;
5020 }
5021 
5022 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr,
5023                                                    SourceLocation &ELoc,
5024                                                    SourceRange &ERange,
5025                                                    bool AllowArraySection) {
5026   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5027       RefExpr->containsUnexpandedParameterPack())
5028     return std::make_pair(nullptr, true);
5029 
5030   // OpenMP [3.1, C/C++]
5031   //  A list item is a variable name.
5032   // OpenMP  [2.9.3.3, Restrictions, p.1]
5033   //  A variable that is part of another variable (as an array or
5034   //  structure element) cannot appear in a private clause.
5035   RefExpr = RefExpr->IgnoreParens();
5036   enum {
5037     NoArrayExpr = -1,
5038     ArraySubscript = 0,
5039     OMPArraySection = 1
5040   } IsArrayExpr = NoArrayExpr;
5041   if (AllowArraySection) {
5042     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
5043       Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
5044       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
5045         Base = TempASE->getBase()->IgnoreParenImpCasts();
5046       RefExpr = Base;
5047       IsArrayExpr = ArraySubscript;
5048     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
5049       Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
5050       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
5051         Base = TempOASE->getBase()->IgnoreParenImpCasts();
5052       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
5053         Base = TempASE->getBase()->IgnoreParenImpCasts();
5054       RefExpr = Base;
5055       IsArrayExpr = OMPArraySection;
5056     }
5057   }
5058   ELoc = RefExpr->getExprLoc();
5059   ERange = RefExpr->getSourceRange();
5060   RefExpr = RefExpr->IgnoreParenImpCasts();
5061   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5062   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
5063   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
5064       (S.getCurrentThisType().isNull() || !ME ||
5065        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
5066        !isa<FieldDecl>(ME->getMemberDecl()))) {
5067     if (IsArrayExpr != NoArrayExpr) {
5068       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
5069                                                          << ERange;
5070     } else {
5071       S.Diag(ELoc,
5072              AllowArraySection
5073                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
5074                  : diag::err_omp_expected_var_name_member_expr)
5075           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
5076     }
5077     return std::make_pair(nullptr, false);
5078   }
5079   return std::make_pair(
5080       getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
5081 }
5082 
5083 namespace {
5084 /// Checks if the allocator is used in uses_allocators clause to be allowed in
5085 /// target regions.
5086 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> {
5087   DSAStackTy *S = nullptr;
5088 
5089 public:
5090   bool VisitDeclRefExpr(const DeclRefExpr *E) {
5091     return S->isUsesAllocatorsDecl(E->getDecl())
5092                .getValueOr(
5093                    DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
5094            DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait;
5095   }
5096   bool VisitStmt(const Stmt *S) {
5097     for (const Stmt *Child : S->children()) {
5098       if (Child && Visit(Child))
5099         return true;
5100     }
5101     return false;
5102   }
5103   explicit AllocatorChecker(DSAStackTy *S) : S(S) {}
5104 };
5105 } // namespace
5106 
5107 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
5108                                  ArrayRef<OMPClause *> Clauses) {
5109   assert(!S.CurContext->isDependentContext() &&
5110          "Expected non-dependent context.");
5111   auto AllocateRange =
5112       llvm::make_filter_range(Clauses, OMPAllocateClause::classof);
5113   llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>>
5114       DeclToCopy;
5115   auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) {
5116     return isOpenMPPrivate(C->getClauseKind());
5117   });
5118   for (OMPClause *Cl : PrivateRange) {
5119     MutableArrayRef<Expr *>::iterator I, It, Et;
5120     if (Cl->getClauseKind() == OMPC_private) {
5121       auto *PC = cast<OMPPrivateClause>(Cl);
5122       I = PC->private_copies().begin();
5123       It = PC->varlist_begin();
5124       Et = PC->varlist_end();
5125     } else if (Cl->getClauseKind() == OMPC_firstprivate) {
5126       auto *PC = cast<OMPFirstprivateClause>(Cl);
5127       I = PC->private_copies().begin();
5128       It = PC->varlist_begin();
5129       Et = PC->varlist_end();
5130     } else if (Cl->getClauseKind() == OMPC_lastprivate) {
5131       auto *PC = cast<OMPLastprivateClause>(Cl);
5132       I = PC->private_copies().begin();
5133       It = PC->varlist_begin();
5134       Et = PC->varlist_end();
5135     } else if (Cl->getClauseKind() == OMPC_linear) {
5136       auto *PC = cast<OMPLinearClause>(Cl);
5137       I = PC->privates().begin();
5138       It = PC->varlist_begin();
5139       Et = PC->varlist_end();
5140     } else if (Cl->getClauseKind() == OMPC_reduction) {
5141       auto *PC = cast<OMPReductionClause>(Cl);
5142       I = PC->privates().begin();
5143       It = PC->varlist_begin();
5144       Et = PC->varlist_end();
5145     } else if (Cl->getClauseKind() == OMPC_task_reduction) {
5146       auto *PC = cast<OMPTaskReductionClause>(Cl);
5147       I = PC->privates().begin();
5148       It = PC->varlist_begin();
5149       Et = PC->varlist_end();
5150     } else if (Cl->getClauseKind() == OMPC_in_reduction) {
5151       auto *PC = cast<OMPInReductionClause>(Cl);
5152       I = PC->privates().begin();
5153       It = PC->varlist_begin();
5154       Et = PC->varlist_end();
5155     } else {
5156       llvm_unreachable("Expected private clause.");
5157     }
5158     for (Expr *E : llvm::make_range(It, Et)) {
5159       if (!*I) {
5160         ++I;
5161         continue;
5162       }
5163       SourceLocation ELoc;
5164       SourceRange ERange;
5165       Expr *SimpleRefExpr = E;
5166       auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
5167                                 /*AllowArraySection=*/true);
5168       DeclToCopy.try_emplace(Res.first,
5169                              cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()));
5170       ++I;
5171     }
5172   }
5173   for (OMPClause *C : AllocateRange) {
5174     auto *AC = cast<OMPAllocateClause>(C);
5175     if (S.getLangOpts().OpenMP >= 50 &&
5176         !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() &&
5177         isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
5178         AC->getAllocator()) {
5179       Expr *Allocator = AC->getAllocator();
5180       // OpenMP, 2.12.5 target Construct
5181       // Memory allocators that do not appear in a uses_allocators clause cannot
5182       // appear as an allocator in an allocate clause or be used in the target
5183       // region unless a requires directive with the dynamic_allocators clause
5184       // is present in the same compilation unit.
5185       AllocatorChecker Checker(Stack);
5186       if (Checker.Visit(Allocator))
5187         S.Diag(Allocator->getExprLoc(),
5188                diag::err_omp_allocator_not_in_uses_allocators)
5189             << Allocator->getSourceRange();
5190     }
5191     OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
5192         getAllocatorKind(S, Stack, AC->getAllocator());
5193     // OpenMP, 2.11.4 allocate Clause, Restrictions.
5194     // For task, taskloop or target directives, allocation requests to memory
5195     // allocators with the trait access set to thread result in unspecified
5196     // behavior.
5197     if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
5198         (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
5199          isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) {
5200       S.Diag(AC->getAllocator()->getExprLoc(),
5201              diag::warn_omp_allocate_thread_on_task_target_directive)
5202           << getOpenMPDirectiveName(Stack->getCurrentDirective());
5203     }
5204     for (Expr *E : AC->varlists()) {
5205       SourceLocation ELoc;
5206       SourceRange ERange;
5207       Expr *SimpleRefExpr = E;
5208       auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange);
5209       ValueDecl *VD = Res.first;
5210       DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false);
5211       if (!isOpenMPPrivate(Data.CKind)) {
5212         S.Diag(E->getExprLoc(),
5213                diag::err_omp_expected_private_copy_for_allocate);
5214         continue;
5215       }
5216       VarDecl *PrivateVD = DeclToCopy[VD];
5217       if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD,
5218                                             AllocatorKind, AC->getAllocator()))
5219         continue;
5220       applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(),
5221                                 E->getSourceRange());
5222     }
5223   }
5224 }
5225 
5226 namespace {
5227 /// Rewrite statements and expressions for Sema \p Actions CurContext.
5228 ///
5229 /// Used to wrap already parsed statements/expressions into a new CapturedStmt
5230 /// context. DeclRefExpr used inside the new context are changed to refer to the
5231 /// captured variable instead.
5232 class CaptureVars : public TreeTransform<CaptureVars> {
5233   using BaseTransform = TreeTransform<CaptureVars>;
5234 
5235 public:
5236   CaptureVars(Sema &Actions) : BaseTransform(Actions) {}
5237 
5238   bool AlwaysRebuild() { return true; }
5239 };
5240 } // namespace
5241 
5242 static VarDecl *precomputeExpr(Sema &Actions,
5243                                SmallVectorImpl<Stmt *> &BodyStmts, Expr *E,
5244                                StringRef Name) {
5245   Expr *NewE = AssertSuccess(CaptureVars(Actions).TransformExpr(E));
5246   VarDecl *NewVar = buildVarDecl(Actions, {}, NewE->getType(), Name, nullptr,
5247                                  dyn_cast<DeclRefExpr>(E->IgnoreImplicit()));
5248   auto *NewDeclStmt = cast<DeclStmt>(AssertSuccess(
5249       Actions.ActOnDeclStmt(Actions.ConvertDeclToDeclGroup(NewVar), {}, {})));
5250   Actions.AddInitializerToDecl(NewDeclStmt->getSingleDecl(), NewE, false);
5251   BodyStmts.push_back(NewDeclStmt);
5252   return NewVar;
5253 }
5254 
5255 /// Create a closure that computes the number of iterations of a loop.
5256 ///
5257 /// \param Actions   The Sema object.
5258 /// \param LogicalTy Type for the logical iteration number.
5259 /// \param Rel       Comparison operator of the loop condition.
5260 /// \param StartExpr Value of the loop counter at the first iteration.
5261 /// \param StopExpr  Expression the loop counter is compared against in the loop
5262 /// condition. \param StepExpr      Amount of increment after each iteration.
5263 ///
5264 /// \return Closure (CapturedStmt) of the distance calculation.
5265 static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy,
5266                                        BinaryOperator::Opcode Rel,
5267                                        Expr *StartExpr, Expr *StopExpr,
5268                                        Expr *StepExpr) {
5269   ASTContext &Ctx = Actions.getASTContext();
5270   TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(LogicalTy);
5271 
5272   // Captured regions currently don't support return values, we use an
5273   // out-parameter instead. All inputs are implicit captures.
5274   // TODO: Instead of capturing each DeclRefExpr occurring in
5275   // StartExpr/StopExpr/Step, these could also be passed as a value capture.
5276   QualType ResultTy = Ctx.getLValueReferenceType(LogicalTy);
5277   Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy},
5278                                           {StringRef(), QualType()}};
5279   Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params);
5280 
5281   Stmt *Body;
5282   {
5283     Sema::CompoundScopeRAII CompoundScope(Actions);
5284     CapturedDecl *CS = cast<CapturedDecl>(Actions.CurContext);
5285 
5286     // Get the LValue expression for the result.
5287     ImplicitParamDecl *DistParam = CS->getParam(0);
5288     DeclRefExpr *DistRef = Actions.BuildDeclRefExpr(
5289         DistParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr);
5290 
5291     SmallVector<Stmt *, 4> BodyStmts;
5292 
5293     // Capture all referenced variable references.
5294     // TODO: Instead of computing NewStart/NewStop/NewStep inside the
5295     // CapturedStmt, we could compute them before and capture the result, to be
5296     // used jointly with the LoopVar function.
5297     VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, StartExpr, ".start");
5298     VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, StopExpr, ".stop");
5299     VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, StepExpr, ".step");
5300     auto BuildVarRef = [&](VarDecl *VD) {
5301       return buildDeclRefExpr(Actions, VD, VD->getType(), {});
5302     };
5303 
5304     IntegerLiteral *Zero = IntegerLiteral::Create(
5305         Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 0), LogicalTy, {});
5306     Expr *Dist;
5307     if (Rel == BO_NE) {
5308       // When using a != comparison, the increment can be +1 or -1. This can be
5309       // dynamic at runtime, so we need to check for the direction.
5310       Expr *IsNegStep = AssertSuccess(
5311           Actions.BuildBinOp(nullptr, {}, BO_LT, BuildVarRef(NewStep), Zero));
5312 
5313       // Positive increment.
5314       Expr *ForwardRange = AssertSuccess(Actions.BuildBinOp(
5315           nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart)));
5316       ForwardRange = AssertSuccess(
5317           Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, ForwardRange));
5318       Expr *ForwardDist = AssertSuccess(Actions.BuildBinOp(
5319           nullptr, {}, BO_Div, ForwardRange, BuildVarRef(NewStep)));
5320 
5321       // Negative increment.
5322       Expr *BackwardRange = AssertSuccess(Actions.BuildBinOp(
5323           nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop)));
5324       BackwardRange = AssertSuccess(
5325           Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, BackwardRange));
5326       Expr *NegIncAmount = AssertSuccess(
5327           Actions.BuildUnaryOp(nullptr, {}, UO_Minus, BuildVarRef(NewStep)));
5328       Expr *BackwardDist = AssertSuccess(
5329           Actions.BuildBinOp(nullptr, {}, BO_Div, BackwardRange, NegIncAmount));
5330 
5331       // Use the appropriate case.
5332       Dist = AssertSuccess(Actions.ActOnConditionalOp(
5333           {}, {}, IsNegStep, BackwardDist, ForwardDist));
5334     } else {
5335       assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) &&
5336              "Expected one of these relational operators");
5337 
5338       // We can derive the direction from any other comparison operator. It is
5339       // non well-formed OpenMP if Step increments/decrements in the other
5340       // directions. Whether at least the first iteration passes the loop
5341       // condition.
5342       Expr *HasAnyIteration = AssertSuccess(Actions.BuildBinOp(
5343           nullptr, {}, Rel, BuildVarRef(NewStart), BuildVarRef(NewStop)));
5344 
5345       // Compute the range between first and last counter value.
5346       Expr *Range;
5347       if (Rel == BO_GE || Rel == BO_GT)
5348         Range = AssertSuccess(Actions.BuildBinOp(
5349             nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop)));
5350       else
5351         Range = AssertSuccess(Actions.BuildBinOp(
5352             nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart)));
5353 
5354       // Ensure unsigned range space.
5355       Range =
5356           AssertSuccess(Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, Range));
5357 
5358       if (Rel == BO_LE || Rel == BO_GE) {
5359         // Add one to the range if the relational operator is inclusive.
5360         Range = AssertSuccess(Actions.BuildBinOp(
5361             nullptr, {}, BO_Add, Range,
5362             Actions.ActOnIntegerConstant(SourceLocation(), 1).get()));
5363       }
5364 
5365       // Divide by the absolute step amount.
5366       Expr *Divisor = BuildVarRef(NewStep);
5367       if (Rel == BO_GE || Rel == BO_GT)
5368         Divisor =
5369             AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Minus, Divisor));
5370       Dist = AssertSuccess(
5371           Actions.BuildBinOp(nullptr, {}, BO_Div, Range, Divisor));
5372 
5373       // If there is not at least one iteration, the range contains garbage. Fix
5374       // to zero in this case.
5375       Dist = AssertSuccess(
5376           Actions.ActOnConditionalOp({}, {}, HasAnyIteration, Dist, Zero));
5377     }
5378 
5379     // Assign the result to the out-parameter.
5380     Stmt *ResultAssign = AssertSuccess(Actions.BuildBinOp(
5381         Actions.getCurScope(), {}, BO_Assign, DistRef, Dist));
5382     BodyStmts.push_back(ResultAssign);
5383 
5384     Body = AssertSuccess(Actions.ActOnCompoundStmt({}, {}, BodyStmts, false));
5385   }
5386 
5387   return cast<CapturedStmt>(
5388       AssertSuccess(Actions.ActOnCapturedRegionEnd(Body)));
5389 }
5390 
5391 /// Create a closure that computes the loop variable from the logical iteration
5392 /// number.
5393 ///
5394 /// \param Actions   The Sema object.
5395 /// \param LoopVarTy Type for the loop variable used for result value.
5396 /// \param LogicalTy Type for the logical iteration number.
5397 /// \param StartExpr Value of the loop counter at the first iteration.
5398 /// \param Step      Amount of increment after each iteration.
5399 /// \param Deref     Whether the loop variable is a dereference of the loop
5400 /// counter variable.
5401 ///
5402 /// \return Closure (CapturedStmt) of the loop value calculation.
5403 static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy,
5404                                       QualType LogicalTy,
5405                                       DeclRefExpr *StartExpr, Expr *Step,
5406                                       bool Deref) {
5407   ASTContext &Ctx = Actions.getASTContext();
5408 
5409   // Pass the result as an out-parameter. Passing as return value would require
5410   // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to
5411   // invoke a copy constructor.
5412   QualType TargetParamTy = Ctx.getLValueReferenceType(LoopVarTy);
5413   Sema::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy},
5414                                           {"Logical", LogicalTy},
5415                                           {StringRef(), QualType()}};
5416   Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params);
5417 
5418   // Capture the initial iterator which represents the LoopVar value at the
5419   // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update
5420   // it in every iteration, capture it by value before it is modified.
5421   VarDecl *StartVar = cast<VarDecl>(StartExpr->getDecl());
5422   bool Invalid = Actions.tryCaptureVariable(StartVar, {},
5423                                             Sema::TryCapture_ExplicitByVal, {});
5424   (void)Invalid;
5425   assert(!Invalid && "Expecting capture-by-value to work.");
5426 
5427   Expr *Body;
5428   {
5429     Sema::CompoundScopeRAII CompoundScope(Actions);
5430     auto *CS = cast<CapturedDecl>(Actions.CurContext);
5431 
5432     ImplicitParamDecl *TargetParam = CS->getParam(0);
5433     DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr(
5434         TargetParam, LoopVarTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr);
5435     ImplicitParamDecl *IndvarParam = CS->getParam(1);
5436     DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr(
5437         IndvarParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr);
5438 
5439     // Capture the Start expression.
5440     CaptureVars Recap(Actions);
5441     Expr *NewStart = AssertSuccess(Recap.TransformExpr(StartExpr));
5442     Expr *NewStep = AssertSuccess(Recap.TransformExpr(Step));
5443 
5444     Expr *Skip = AssertSuccess(
5445         Actions.BuildBinOp(nullptr, {}, BO_Mul, NewStep, LogicalRef));
5446     // TODO: Explicitly cast to the iterator's difference_type instead of
5447     // relying on implicit conversion.
5448     Expr *Advanced =
5449         AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, NewStart, Skip));
5450 
5451     if (Deref) {
5452       // For range-based for-loops convert the loop counter value to a concrete
5453       // loop variable value by dereferencing the iterator.
5454       Advanced =
5455           AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Deref, Advanced));
5456     }
5457 
5458     // Assign the result to the output parameter.
5459     Body = AssertSuccess(Actions.BuildBinOp(Actions.getCurScope(), {},
5460                                             BO_Assign, TargetRef, Advanced));
5461   }
5462   return cast<CapturedStmt>(
5463       AssertSuccess(Actions.ActOnCapturedRegionEnd(Body)));
5464 }
5465 
5466 StmtResult Sema::ActOnOpenMPCanonicalLoop(Stmt *AStmt) {
5467   ASTContext &Ctx = getASTContext();
5468 
5469   // Extract the common elements of ForStmt and CXXForRangeStmt:
5470   // Loop variable, repeat condition, increment
5471   Expr *Cond, *Inc;
5472   VarDecl *LIVDecl, *LUVDecl;
5473   if (auto *For = dyn_cast<ForStmt>(AStmt)) {
5474     Stmt *Init = For->getInit();
5475     if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Init)) {
5476       // For statement declares loop variable.
5477       LIVDecl = cast<VarDecl>(LCVarDeclStmt->getSingleDecl());
5478     } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Init)) {
5479       // For statement reuses variable.
5480       assert(LCAssign->getOpcode() == BO_Assign &&
5481              "init part must be a loop variable assignment");
5482       auto *CounterRef = cast<DeclRefExpr>(LCAssign->getLHS());
5483       LIVDecl = cast<VarDecl>(CounterRef->getDecl());
5484     } else
5485       llvm_unreachable("Cannot determine loop variable");
5486     LUVDecl = LIVDecl;
5487 
5488     Cond = For->getCond();
5489     Inc = For->getInc();
5490   } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(AStmt)) {
5491     DeclStmt *BeginStmt = RangeFor->getBeginStmt();
5492     LIVDecl = cast<VarDecl>(BeginStmt->getSingleDecl());
5493     LUVDecl = RangeFor->getLoopVariable();
5494 
5495     Cond = RangeFor->getCond();
5496     Inc = RangeFor->getInc();
5497   } else
5498     llvm_unreachable("unhandled kind of loop");
5499 
5500   QualType CounterTy = LIVDecl->getType();
5501   QualType LVTy = LUVDecl->getType();
5502 
5503   // Analyze the loop condition.
5504   Expr *LHS, *RHS;
5505   BinaryOperator::Opcode CondRel;
5506   Cond = Cond->IgnoreImplicit();
5507   if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Cond)) {
5508     LHS = CondBinExpr->getLHS();
5509     RHS = CondBinExpr->getRHS();
5510     CondRel = CondBinExpr->getOpcode();
5511   } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Cond)) {
5512     assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands");
5513     LHS = CondCXXOp->getArg(0);
5514     RHS = CondCXXOp->getArg(1);
5515     switch (CondCXXOp->getOperator()) {
5516     case OO_ExclaimEqual:
5517       CondRel = BO_NE;
5518       break;
5519     case OO_Less:
5520       CondRel = BO_LT;
5521       break;
5522     case OO_LessEqual:
5523       CondRel = BO_LE;
5524       break;
5525     case OO_Greater:
5526       CondRel = BO_GT;
5527       break;
5528     case OO_GreaterEqual:
5529       CondRel = BO_GE;
5530       break;
5531     default:
5532       llvm_unreachable("unexpected iterator operator");
5533     }
5534   } else
5535     llvm_unreachable("unexpected loop condition");
5536 
5537   // Normalize such that the loop counter is on the LHS.
5538   if (!isa<DeclRefExpr>(LHS->IgnoreImplicit()) ||
5539       cast<DeclRefExpr>(LHS->IgnoreImplicit())->getDecl() != LIVDecl) {
5540     std::swap(LHS, RHS);
5541     CondRel = BinaryOperator::reverseComparisonOp(CondRel);
5542   }
5543   auto *CounterRef = cast<DeclRefExpr>(LHS->IgnoreImplicit());
5544 
5545   // Decide the bit width for the logical iteration counter. By default use the
5546   // unsigned ptrdiff_t integer size (for iterators and pointers).
5547   // TODO: For iterators, use iterator::difference_type,
5548   // std::iterator_traits<>::difference_type or decltype(it - end).
5549   QualType LogicalTy = Ctx.getUnsignedPointerDiffType();
5550   if (CounterTy->isIntegerType()) {
5551     unsigned BitWidth = Ctx.getIntWidth(CounterTy);
5552     LogicalTy = Ctx.getIntTypeForBitwidth(BitWidth, false);
5553   }
5554 
5555   // Analyze the loop increment.
5556   Expr *Step;
5557   if (auto *IncUn = dyn_cast<UnaryOperator>(Inc)) {
5558     int Direction;
5559     switch (IncUn->getOpcode()) {
5560     case UO_PreInc:
5561     case UO_PostInc:
5562       Direction = 1;
5563       break;
5564     case UO_PreDec:
5565     case UO_PostDec:
5566       Direction = -1;
5567       break;
5568     default:
5569       llvm_unreachable("unhandled unary increment operator");
5570     }
5571     Step = IntegerLiteral::Create(
5572         Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction), LogicalTy, {});
5573   } else if (auto *IncBin = dyn_cast<BinaryOperator>(Inc)) {
5574     if (IncBin->getOpcode() == BO_AddAssign) {
5575       Step = IncBin->getRHS();
5576     } else if (IncBin->getOpcode() == BO_SubAssign) {
5577       Step =
5578           AssertSuccess(BuildUnaryOp(nullptr, {}, UO_Minus, IncBin->getRHS()));
5579     } else
5580       llvm_unreachable("unhandled binary increment operator");
5581   } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Inc)) {
5582     switch (CondCXXOp->getOperator()) {
5583     case OO_PlusPlus:
5584       Step = IntegerLiteral::Create(
5585           Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {});
5586       break;
5587     case OO_MinusMinus:
5588       Step = IntegerLiteral::Create(
5589           Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), -1), LogicalTy, {});
5590       break;
5591     case OO_PlusEqual:
5592       Step = CondCXXOp->getArg(1);
5593       break;
5594     case OO_MinusEqual:
5595       Step = AssertSuccess(
5596           BuildUnaryOp(nullptr, {}, UO_Minus, CondCXXOp->getArg(1)));
5597       break;
5598     default:
5599       llvm_unreachable("unhandled overloaded increment operator");
5600     }
5601   } else
5602     llvm_unreachable("unknown increment expression");
5603 
5604   CapturedStmt *DistanceFunc =
5605       buildDistanceFunc(*this, LogicalTy, CondRel, LHS, RHS, Step);
5606   CapturedStmt *LoopVarFunc = buildLoopVarFunc(
5607       *this, LVTy, LogicalTy, CounterRef, Step, isa<CXXForRangeStmt>(AStmt));
5608   DeclRefExpr *LVRef = BuildDeclRefExpr(LUVDecl, LUVDecl->getType(), VK_LValue,
5609                                         {}, nullptr, nullptr, {}, nullptr);
5610   return OMPCanonicalLoop::create(getASTContext(), AStmt, DistanceFunc,
5611                                   LoopVarFunc, LVRef);
5612 }
5613 
5614 StmtResult Sema::ActOnOpenMPLoopnest(Stmt *AStmt) {
5615   // Handle a literal loop.
5616   if (isa<ForStmt>(AStmt) || isa<CXXForRangeStmt>(AStmt))
5617     return ActOnOpenMPCanonicalLoop(AStmt);
5618 
5619   // If not a literal loop, it must be the result of a loop transformation.
5620   OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(AStmt);
5621   assert(
5622       isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) &&
5623       "Loop transformation directive expected");
5624   return LoopTransform;
5625 }
5626 
5627 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
5628                                             CXXScopeSpec &MapperIdScopeSpec,
5629                                             const DeclarationNameInfo &MapperId,
5630                                             QualType Type,
5631                                             Expr *UnresolvedMapper);
5632 
5633 /// Perform DFS through the structure/class data members trying to find
5634 /// member(s) with user-defined 'default' mapper and generate implicit map
5635 /// clauses for such members with the found 'default' mapper.
5636 static void
5637 processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack,
5638                                       SmallVectorImpl<OMPClause *> &Clauses) {
5639   // Check for the deault mapper for data members.
5640   if (S.getLangOpts().OpenMP < 50)
5641     return;
5642   SmallVector<OMPClause *, 4> ImplicitMaps;
5643   for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) {
5644     auto *C = dyn_cast<OMPMapClause>(Clauses[Cnt]);
5645     if (!C)
5646       continue;
5647     SmallVector<Expr *, 4> SubExprs;
5648     auto *MI = C->mapperlist_begin();
5649     for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End;
5650          ++I, ++MI) {
5651       // Expression is mapped using mapper - skip it.
5652       if (*MI)
5653         continue;
5654       Expr *E = *I;
5655       // Expression is dependent - skip it, build the mapper when it gets
5656       // instantiated.
5657       if (E->isTypeDependent() || E->isValueDependent() ||
5658           E->containsUnexpandedParameterPack())
5659         continue;
5660       // Array section - need to check for the mapping of the array section
5661       // element.
5662       QualType CanonType = E->getType().getCanonicalType();
5663       if (CanonType->isSpecificBuiltinType(BuiltinType::OMPArraySection)) {
5664         const auto *OASE = cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts());
5665         QualType BaseType =
5666             OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
5667         QualType ElemType;
5668         if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
5669           ElemType = ATy->getElementType();
5670         else
5671           ElemType = BaseType->getPointeeType();
5672         CanonType = ElemType;
5673       }
5674 
5675       // DFS over data members in structures/classes.
5676       SmallVector<std::pair<QualType, FieldDecl *>, 4> Types(
5677           1, {CanonType, nullptr});
5678       llvm::DenseMap<const Type *, Expr *> Visited;
5679       SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain(
5680           1, {nullptr, 1});
5681       while (!Types.empty()) {
5682         QualType BaseType;
5683         FieldDecl *CurFD;
5684         std::tie(BaseType, CurFD) = Types.pop_back_val();
5685         while (ParentChain.back().second == 0)
5686           ParentChain.pop_back();
5687         --ParentChain.back().second;
5688         if (BaseType.isNull())
5689           continue;
5690         // Only structs/classes are allowed to have mappers.
5691         const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl();
5692         if (!RD)
5693           continue;
5694         auto It = Visited.find(BaseType.getTypePtr());
5695         if (It == Visited.end()) {
5696           // Try to find the associated user-defined mapper.
5697           CXXScopeSpec MapperIdScopeSpec;
5698           DeclarationNameInfo DefaultMapperId;
5699           DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier(
5700               &S.Context.Idents.get("default")));
5701           DefaultMapperId.setLoc(E->getExprLoc());
5702           ExprResult ER = buildUserDefinedMapperRef(
5703               S, Stack->getCurScope(), MapperIdScopeSpec, DefaultMapperId,
5704               BaseType, /*UnresolvedMapper=*/nullptr);
5705           if (ER.isInvalid())
5706             continue;
5707           It = Visited.try_emplace(BaseType.getTypePtr(), ER.get()).first;
5708         }
5709         // Found default mapper.
5710         if (It->second) {
5711           auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType,
5712                                                      VK_LValue, OK_Ordinary, E);
5713           OE->setIsUnique(/*V=*/true);
5714           Expr *BaseExpr = OE;
5715           for (const auto &P : ParentChain) {
5716             if (P.first) {
5717               BaseExpr = S.BuildMemberExpr(
5718                   BaseExpr, /*IsArrow=*/false, E->getExprLoc(),
5719                   NestedNameSpecifierLoc(), SourceLocation(), P.first,
5720                   DeclAccessPair::make(P.first, P.first->getAccess()),
5721                   /*HadMultipleCandidates=*/false, DeclarationNameInfo(),
5722                   P.first->getType(), VK_LValue, OK_Ordinary);
5723               BaseExpr = S.DefaultLvalueConversion(BaseExpr).get();
5724             }
5725           }
5726           if (CurFD)
5727             BaseExpr = S.BuildMemberExpr(
5728                 BaseExpr, /*IsArrow=*/false, E->getExprLoc(),
5729                 NestedNameSpecifierLoc(), SourceLocation(), CurFD,
5730                 DeclAccessPair::make(CurFD, CurFD->getAccess()),
5731                 /*HadMultipleCandidates=*/false, DeclarationNameInfo(),
5732                 CurFD->getType(), VK_LValue, OK_Ordinary);
5733           SubExprs.push_back(BaseExpr);
5734           continue;
5735         }
5736         // Check for the "default" mapper for data members.
5737         bool FirstIter = true;
5738         for (FieldDecl *FD : RD->fields()) {
5739           if (!FD)
5740             continue;
5741           QualType FieldTy = FD->getType();
5742           if (FieldTy.isNull() ||
5743               !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType()))
5744             continue;
5745           if (FirstIter) {
5746             FirstIter = false;
5747             ParentChain.emplace_back(CurFD, 1);
5748           } else {
5749             ++ParentChain.back().second;
5750           }
5751           Types.emplace_back(FieldTy, FD);
5752         }
5753       }
5754     }
5755     if (SubExprs.empty())
5756       continue;
5757     CXXScopeSpec MapperIdScopeSpec;
5758     DeclarationNameInfo MapperId;
5759     if (OMPClause *NewClause = S.ActOnOpenMPMapClause(
5760             C->getMapTypeModifiers(), C->getMapTypeModifiersLoc(),
5761             MapperIdScopeSpec, MapperId, C->getMapType(),
5762             /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(),
5763             SubExprs, OMPVarListLocTy()))
5764       Clauses.push_back(NewClause);
5765   }
5766 }
5767 
5768 StmtResult Sema::ActOnOpenMPExecutableDirective(
5769     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
5770     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
5771     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
5772   StmtResult Res = StmtError();
5773   // First check CancelRegion which is then used in checkNestingOfRegions.
5774   if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
5775       checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
5776                             StartLoc))
5777     return StmtError();
5778 
5779   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
5780   VarsWithInheritedDSAType VarsWithInheritedDSA;
5781   bool ErrorFound = false;
5782   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
5783   if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic &&
5784       Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master &&
5785       Kind != OMPD_masked && !isOpenMPLoopTransformationDirective(Kind)) {
5786     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5787 
5788     // Check default data sharing attributes for referenced variables.
5789     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
5790     int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
5791     Stmt *S = AStmt;
5792     while (--ThisCaptureLevel >= 0)
5793       S = cast<CapturedStmt>(S)->getCapturedStmt();
5794     DSAChecker.Visit(S);
5795     if (!isOpenMPTargetDataManagementDirective(Kind) &&
5796         !isOpenMPTaskingDirective(Kind)) {
5797       // Visit subcaptures to generate implicit clauses for captured vars.
5798       auto *CS = cast<CapturedStmt>(AStmt);
5799       SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
5800       getOpenMPCaptureRegions(CaptureRegions, Kind);
5801       // Ignore outer tasking regions for target directives.
5802       if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
5803         CS = cast<CapturedStmt>(CS->getCapturedStmt());
5804       DSAChecker.visitSubCaptures(CS);
5805     }
5806     if (DSAChecker.isErrorFound())
5807       return StmtError();
5808     // Generate list of implicitly defined firstprivate variables.
5809     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
5810 
5811     SmallVector<Expr *, 4> ImplicitFirstprivates(
5812         DSAChecker.getImplicitFirstprivate().begin(),
5813         DSAChecker.getImplicitFirstprivate().end());
5814     const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1;
5815     SmallVector<Expr *, 4> ImplicitMaps[DefaultmapKindNum][OMPC_MAP_delete];
5816     SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
5817         ImplicitMapModifiers[DefaultmapKindNum];
5818     SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers>
5819         ImplicitMapModifiersLoc[DefaultmapKindNum];
5820     // Get the original location of present modifier from Defaultmap clause.
5821     SourceLocation PresentModifierLocs[DefaultmapKindNum];
5822     for (OMPClause *C : Clauses) {
5823       if (auto *DMC = dyn_cast<OMPDefaultmapClause>(C))
5824         if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present)
5825           PresentModifierLocs[DMC->getDefaultmapKind()] =
5826               DMC->getDefaultmapModifierLoc();
5827     }
5828     for (unsigned VC = 0; VC < DefaultmapKindNum; ++VC) {
5829       auto Kind = static_cast<OpenMPDefaultmapClauseKind>(VC);
5830       for (unsigned I = 0; I < OMPC_MAP_delete; ++I) {
5831         ArrayRef<Expr *> ImplicitMap = DSAChecker.getImplicitMap(
5832             Kind, static_cast<OpenMPMapClauseKind>(I));
5833         ImplicitMaps[VC][I].append(ImplicitMap.begin(), ImplicitMap.end());
5834       }
5835       ArrayRef<OpenMPMapModifierKind> ImplicitModifier =
5836           DSAChecker.getImplicitMapModifier(Kind);
5837       ImplicitMapModifiers[VC].append(ImplicitModifier.begin(),
5838                                       ImplicitModifier.end());
5839       std::fill_n(std::back_inserter(ImplicitMapModifiersLoc[VC]),
5840                   ImplicitModifier.size(), PresentModifierLocs[VC]);
5841     }
5842     // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
5843     for (OMPClause *C : Clauses) {
5844       if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
5845         for (Expr *E : IRC->taskgroup_descriptors())
5846           if (E)
5847             ImplicitFirstprivates.emplace_back(E);
5848       }
5849       // OpenMP 5.0, 2.10.1 task Construct
5850       // [detach clause]... The event-handle will be considered as if it was
5851       // specified on a firstprivate clause.
5852       if (auto *DC = dyn_cast<OMPDetachClause>(C))
5853         ImplicitFirstprivates.push_back(DC->getEventHandler());
5854     }
5855     if (!ImplicitFirstprivates.empty()) {
5856       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
5857               ImplicitFirstprivates, SourceLocation(), SourceLocation(),
5858               SourceLocation())) {
5859         ClausesWithImplicit.push_back(Implicit);
5860         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
5861                      ImplicitFirstprivates.size();
5862       } else {
5863         ErrorFound = true;
5864       }
5865     }
5866     // OpenMP 5.0 [2.19.7]
5867     // If a list item appears in a reduction, lastprivate or linear
5868     // clause on a combined target construct then it is treated as
5869     // if it also appears in a map clause with a map-type of tofrom
5870     if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target &&
5871         isOpenMPTargetExecutionDirective(Kind)) {
5872       SmallVector<Expr *, 4> ImplicitExprs;
5873       for (OMPClause *C : Clauses) {
5874         if (auto *RC = dyn_cast<OMPReductionClause>(C))
5875           for (Expr *E : RC->varlists())
5876             if (!isa<DeclRefExpr>(E->IgnoreParenImpCasts()))
5877               ImplicitExprs.emplace_back(E);
5878       }
5879       if (!ImplicitExprs.empty()) {
5880         ArrayRef<Expr *> Exprs = ImplicitExprs;
5881         CXXScopeSpec MapperIdScopeSpec;
5882         DeclarationNameInfo MapperId;
5883         if (OMPClause *Implicit = ActOnOpenMPMapClause(
5884                 OMPC_MAP_MODIFIER_unknown, SourceLocation(), MapperIdScopeSpec,
5885                 MapperId, OMPC_MAP_tofrom,
5886                 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(),
5887                 Exprs, OMPVarListLocTy(), /*NoDiagnose=*/true))
5888           ClausesWithImplicit.emplace_back(Implicit);
5889       }
5890     }
5891     for (unsigned I = 0, E = DefaultmapKindNum; I < E; ++I) {
5892       int ClauseKindCnt = -1;
5893       for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps[I]) {
5894         ++ClauseKindCnt;
5895         if (ImplicitMap.empty())
5896           continue;
5897         CXXScopeSpec MapperIdScopeSpec;
5898         DeclarationNameInfo MapperId;
5899         auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt);
5900         if (OMPClause *Implicit = ActOnOpenMPMapClause(
5901                 ImplicitMapModifiers[I], ImplicitMapModifiersLoc[I],
5902                 MapperIdScopeSpec, MapperId, Kind, /*IsMapTypeImplicit=*/true,
5903                 SourceLocation(), SourceLocation(), ImplicitMap,
5904                 OMPVarListLocTy())) {
5905           ClausesWithImplicit.emplace_back(Implicit);
5906           ErrorFound |= cast<OMPMapClause>(Implicit)->varlist_size() !=
5907                         ImplicitMap.size();
5908         } else {
5909           ErrorFound = true;
5910         }
5911       }
5912     }
5913     // Build expressions for implicit maps of data members with 'default'
5914     // mappers.
5915     if (LangOpts.OpenMP >= 50)
5916       processImplicitMapsWithDefaultMappers(*this, DSAStack,
5917                                             ClausesWithImplicit);
5918   }
5919 
5920   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
5921   switch (Kind) {
5922   case OMPD_parallel:
5923     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
5924                                        EndLoc);
5925     AllowedNameModifiers.push_back(OMPD_parallel);
5926     break;
5927   case OMPD_simd:
5928     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
5929                                    VarsWithInheritedDSA);
5930     if (LangOpts.OpenMP >= 50)
5931       AllowedNameModifiers.push_back(OMPD_simd);
5932     break;
5933   case OMPD_tile:
5934     Res =
5935         ActOnOpenMPTileDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
5936     break;
5937   case OMPD_unroll:
5938     Res = ActOnOpenMPUnrollDirective(ClausesWithImplicit, AStmt, StartLoc,
5939                                      EndLoc);
5940     break;
5941   case OMPD_for:
5942     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
5943                                   VarsWithInheritedDSA);
5944     break;
5945   case OMPD_for_simd:
5946     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
5947                                       EndLoc, VarsWithInheritedDSA);
5948     if (LangOpts.OpenMP >= 50)
5949       AllowedNameModifiers.push_back(OMPD_simd);
5950     break;
5951   case OMPD_sections:
5952     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
5953                                        EndLoc);
5954     break;
5955   case OMPD_section:
5956     assert(ClausesWithImplicit.empty() &&
5957            "No clauses are allowed for 'omp section' directive");
5958     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
5959     break;
5960   case OMPD_single:
5961     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
5962                                      EndLoc);
5963     break;
5964   case OMPD_master:
5965     assert(ClausesWithImplicit.empty() &&
5966            "No clauses are allowed for 'omp master' directive");
5967     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
5968     break;
5969   case OMPD_masked:
5970     Res = ActOnOpenMPMaskedDirective(ClausesWithImplicit, AStmt, StartLoc,
5971                                      EndLoc);
5972     break;
5973   case OMPD_critical:
5974     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
5975                                        StartLoc, EndLoc);
5976     break;
5977   case OMPD_parallel_for:
5978     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
5979                                           EndLoc, VarsWithInheritedDSA);
5980     AllowedNameModifiers.push_back(OMPD_parallel);
5981     break;
5982   case OMPD_parallel_for_simd:
5983     Res = ActOnOpenMPParallelForSimdDirective(
5984         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5985     AllowedNameModifiers.push_back(OMPD_parallel);
5986     if (LangOpts.OpenMP >= 50)
5987       AllowedNameModifiers.push_back(OMPD_simd);
5988     break;
5989   case OMPD_parallel_master:
5990     Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt,
5991                                                StartLoc, EndLoc);
5992     AllowedNameModifiers.push_back(OMPD_parallel);
5993     break;
5994   case OMPD_parallel_sections:
5995     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
5996                                                StartLoc, EndLoc);
5997     AllowedNameModifiers.push_back(OMPD_parallel);
5998     break;
5999   case OMPD_task:
6000     Res =
6001         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6002     AllowedNameModifiers.push_back(OMPD_task);
6003     break;
6004   case OMPD_taskyield:
6005     assert(ClausesWithImplicit.empty() &&
6006            "No clauses are allowed for 'omp taskyield' directive");
6007     assert(AStmt == nullptr &&
6008            "No associated statement allowed for 'omp taskyield' directive");
6009     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
6010     break;
6011   case OMPD_barrier:
6012     assert(ClausesWithImplicit.empty() &&
6013            "No clauses are allowed for 'omp barrier' directive");
6014     assert(AStmt == nullptr &&
6015            "No associated statement allowed for 'omp barrier' directive");
6016     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
6017     break;
6018   case OMPD_taskwait:
6019     assert(ClausesWithImplicit.empty() &&
6020            "No clauses are allowed for 'omp taskwait' directive");
6021     assert(AStmt == nullptr &&
6022            "No associated statement allowed for 'omp taskwait' directive");
6023     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
6024     break;
6025   case OMPD_taskgroup:
6026     Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
6027                                         EndLoc);
6028     break;
6029   case OMPD_flush:
6030     assert(AStmt == nullptr &&
6031            "No associated statement allowed for 'omp flush' directive");
6032     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
6033     break;
6034   case OMPD_depobj:
6035     assert(AStmt == nullptr &&
6036            "No associated statement allowed for 'omp depobj' directive");
6037     Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc);
6038     break;
6039   case OMPD_scan:
6040     assert(AStmt == nullptr &&
6041            "No associated statement allowed for 'omp scan' directive");
6042     Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc);
6043     break;
6044   case OMPD_ordered:
6045     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
6046                                       EndLoc);
6047     break;
6048   case OMPD_atomic:
6049     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
6050                                      EndLoc);
6051     break;
6052   case OMPD_teams:
6053     Res =
6054         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6055     break;
6056   case OMPD_target:
6057     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
6058                                      EndLoc);
6059     AllowedNameModifiers.push_back(OMPD_target);
6060     break;
6061   case OMPD_target_parallel:
6062     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
6063                                              StartLoc, EndLoc);
6064     AllowedNameModifiers.push_back(OMPD_target);
6065     AllowedNameModifiers.push_back(OMPD_parallel);
6066     break;
6067   case OMPD_target_parallel_for:
6068     Res = ActOnOpenMPTargetParallelForDirective(
6069         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6070     AllowedNameModifiers.push_back(OMPD_target);
6071     AllowedNameModifiers.push_back(OMPD_parallel);
6072     break;
6073   case OMPD_cancellation_point:
6074     assert(ClausesWithImplicit.empty() &&
6075            "No clauses are allowed for 'omp cancellation point' directive");
6076     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
6077                                "cancellation point' directive");
6078     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
6079     break;
6080   case OMPD_cancel:
6081     assert(AStmt == nullptr &&
6082            "No associated statement allowed for 'omp cancel' directive");
6083     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
6084                                      CancelRegion);
6085     AllowedNameModifiers.push_back(OMPD_cancel);
6086     break;
6087   case OMPD_target_data:
6088     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
6089                                          EndLoc);
6090     AllowedNameModifiers.push_back(OMPD_target_data);
6091     break;
6092   case OMPD_target_enter_data:
6093     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
6094                                               EndLoc, AStmt);
6095     AllowedNameModifiers.push_back(OMPD_target_enter_data);
6096     break;
6097   case OMPD_target_exit_data:
6098     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
6099                                              EndLoc, AStmt);
6100     AllowedNameModifiers.push_back(OMPD_target_exit_data);
6101     break;
6102   case OMPD_taskloop:
6103     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
6104                                        EndLoc, VarsWithInheritedDSA);
6105     AllowedNameModifiers.push_back(OMPD_taskloop);
6106     break;
6107   case OMPD_taskloop_simd:
6108     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
6109                                            EndLoc, VarsWithInheritedDSA);
6110     AllowedNameModifiers.push_back(OMPD_taskloop);
6111     if (LangOpts.OpenMP >= 50)
6112       AllowedNameModifiers.push_back(OMPD_simd);
6113     break;
6114   case OMPD_master_taskloop:
6115     Res = ActOnOpenMPMasterTaskLoopDirective(
6116         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6117     AllowedNameModifiers.push_back(OMPD_taskloop);
6118     break;
6119   case OMPD_master_taskloop_simd:
6120     Res = ActOnOpenMPMasterTaskLoopSimdDirective(
6121         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6122     AllowedNameModifiers.push_back(OMPD_taskloop);
6123     if (LangOpts.OpenMP >= 50)
6124       AllowedNameModifiers.push_back(OMPD_simd);
6125     break;
6126   case OMPD_parallel_master_taskloop:
6127     Res = ActOnOpenMPParallelMasterTaskLoopDirective(
6128         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6129     AllowedNameModifiers.push_back(OMPD_taskloop);
6130     AllowedNameModifiers.push_back(OMPD_parallel);
6131     break;
6132   case OMPD_parallel_master_taskloop_simd:
6133     Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective(
6134         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6135     AllowedNameModifiers.push_back(OMPD_taskloop);
6136     AllowedNameModifiers.push_back(OMPD_parallel);
6137     if (LangOpts.OpenMP >= 50)
6138       AllowedNameModifiers.push_back(OMPD_simd);
6139     break;
6140   case OMPD_distribute:
6141     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
6142                                          EndLoc, VarsWithInheritedDSA);
6143     break;
6144   case OMPD_target_update:
6145     Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
6146                                            EndLoc, AStmt);
6147     AllowedNameModifiers.push_back(OMPD_target_update);
6148     break;
6149   case OMPD_distribute_parallel_for:
6150     Res = ActOnOpenMPDistributeParallelForDirective(
6151         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6152     AllowedNameModifiers.push_back(OMPD_parallel);
6153     break;
6154   case OMPD_distribute_parallel_for_simd:
6155     Res = ActOnOpenMPDistributeParallelForSimdDirective(
6156         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6157     AllowedNameModifiers.push_back(OMPD_parallel);
6158     if (LangOpts.OpenMP >= 50)
6159       AllowedNameModifiers.push_back(OMPD_simd);
6160     break;
6161   case OMPD_distribute_simd:
6162     Res = ActOnOpenMPDistributeSimdDirective(
6163         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6164     if (LangOpts.OpenMP >= 50)
6165       AllowedNameModifiers.push_back(OMPD_simd);
6166     break;
6167   case OMPD_target_parallel_for_simd:
6168     Res = ActOnOpenMPTargetParallelForSimdDirective(
6169         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6170     AllowedNameModifiers.push_back(OMPD_target);
6171     AllowedNameModifiers.push_back(OMPD_parallel);
6172     if (LangOpts.OpenMP >= 50)
6173       AllowedNameModifiers.push_back(OMPD_simd);
6174     break;
6175   case OMPD_target_simd:
6176     Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
6177                                          EndLoc, VarsWithInheritedDSA);
6178     AllowedNameModifiers.push_back(OMPD_target);
6179     if (LangOpts.OpenMP >= 50)
6180       AllowedNameModifiers.push_back(OMPD_simd);
6181     break;
6182   case OMPD_teams_distribute:
6183     Res = ActOnOpenMPTeamsDistributeDirective(
6184         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6185     break;
6186   case OMPD_teams_distribute_simd:
6187     Res = ActOnOpenMPTeamsDistributeSimdDirective(
6188         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6189     if (LangOpts.OpenMP >= 50)
6190       AllowedNameModifiers.push_back(OMPD_simd);
6191     break;
6192   case OMPD_teams_distribute_parallel_for_simd:
6193     Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
6194         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6195     AllowedNameModifiers.push_back(OMPD_parallel);
6196     if (LangOpts.OpenMP >= 50)
6197       AllowedNameModifiers.push_back(OMPD_simd);
6198     break;
6199   case OMPD_teams_distribute_parallel_for:
6200     Res = ActOnOpenMPTeamsDistributeParallelForDirective(
6201         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6202     AllowedNameModifiers.push_back(OMPD_parallel);
6203     break;
6204   case OMPD_target_teams:
6205     Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
6206                                           EndLoc);
6207     AllowedNameModifiers.push_back(OMPD_target);
6208     break;
6209   case OMPD_target_teams_distribute:
6210     Res = ActOnOpenMPTargetTeamsDistributeDirective(
6211         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6212     AllowedNameModifiers.push_back(OMPD_target);
6213     break;
6214   case OMPD_target_teams_distribute_parallel_for:
6215     Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
6216         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6217     AllowedNameModifiers.push_back(OMPD_target);
6218     AllowedNameModifiers.push_back(OMPD_parallel);
6219     break;
6220   case OMPD_target_teams_distribute_parallel_for_simd:
6221     Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
6222         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6223     AllowedNameModifiers.push_back(OMPD_target);
6224     AllowedNameModifiers.push_back(OMPD_parallel);
6225     if (LangOpts.OpenMP >= 50)
6226       AllowedNameModifiers.push_back(OMPD_simd);
6227     break;
6228   case OMPD_target_teams_distribute_simd:
6229     Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
6230         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
6231     AllowedNameModifiers.push_back(OMPD_target);
6232     if (LangOpts.OpenMP >= 50)
6233       AllowedNameModifiers.push_back(OMPD_simd);
6234     break;
6235   case OMPD_interop:
6236     assert(AStmt == nullptr &&
6237            "No associated statement allowed for 'omp interop' directive");
6238     Res = ActOnOpenMPInteropDirective(ClausesWithImplicit, StartLoc, EndLoc);
6239     break;
6240   case OMPD_dispatch:
6241     Res = ActOnOpenMPDispatchDirective(ClausesWithImplicit, AStmt, StartLoc,
6242                                        EndLoc);
6243     break;
6244   case OMPD_loop:
6245     Res = ActOnOpenMPGenericLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
6246                                           EndLoc, VarsWithInheritedDSA);
6247     break;
6248   case OMPD_declare_target:
6249   case OMPD_end_declare_target:
6250   case OMPD_threadprivate:
6251   case OMPD_allocate:
6252   case OMPD_declare_reduction:
6253   case OMPD_declare_mapper:
6254   case OMPD_declare_simd:
6255   case OMPD_requires:
6256   case OMPD_declare_variant:
6257   case OMPD_begin_declare_variant:
6258   case OMPD_end_declare_variant:
6259     llvm_unreachable("OpenMP Directive is not allowed");
6260   case OMPD_unknown:
6261   default:
6262     llvm_unreachable("Unknown OpenMP directive");
6263   }
6264 
6265   ErrorFound = Res.isInvalid() || ErrorFound;
6266 
6267   // Check variables in the clauses if default(none) or
6268   // default(firstprivate) was specified.
6269   if (DSAStack->getDefaultDSA() == DSA_none ||
6270       DSAStack->getDefaultDSA() == DSA_firstprivate) {
6271     DSAAttrChecker DSAChecker(DSAStack, *this, nullptr);
6272     for (OMPClause *C : Clauses) {
6273       switch (C->getClauseKind()) {
6274       case OMPC_num_threads:
6275       case OMPC_dist_schedule:
6276         // Do not analyse if no parent teams directive.
6277         if (isOpenMPTeamsDirective(Kind))
6278           break;
6279         continue;
6280       case OMPC_if:
6281         if (isOpenMPTeamsDirective(Kind) &&
6282             cast<OMPIfClause>(C)->getNameModifier() != OMPD_target)
6283           break;
6284         if (isOpenMPParallelDirective(Kind) &&
6285             isOpenMPTaskLoopDirective(Kind) &&
6286             cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel)
6287           break;
6288         continue;
6289       case OMPC_schedule:
6290       case OMPC_detach:
6291         break;
6292       case OMPC_grainsize:
6293       case OMPC_num_tasks:
6294       case OMPC_final:
6295       case OMPC_priority:
6296       case OMPC_novariants:
6297       case OMPC_nocontext:
6298         // Do not analyze if no parent parallel directive.
6299         if (isOpenMPParallelDirective(Kind))
6300           break;
6301         continue;
6302       case OMPC_ordered:
6303       case OMPC_device:
6304       case OMPC_num_teams:
6305       case OMPC_thread_limit:
6306       case OMPC_hint:
6307       case OMPC_collapse:
6308       case OMPC_safelen:
6309       case OMPC_simdlen:
6310       case OMPC_sizes:
6311       case OMPC_default:
6312       case OMPC_proc_bind:
6313       case OMPC_private:
6314       case OMPC_firstprivate:
6315       case OMPC_lastprivate:
6316       case OMPC_shared:
6317       case OMPC_reduction:
6318       case OMPC_task_reduction:
6319       case OMPC_in_reduction:
6320       case OMPC_linear:
6321       case OMPC_aligned:
6322       case OMPC_copyin:
6323       case OMPC_copyprivate:
6324       case OMPC_nowait:
6325       case OMPC_untied:
6326       case OMPC_mergeable:
6327       case OMPC_allocate:
6328       case OMPC_read:
6329       case OMPC_write:
6330       case OMPC_update:
6331       case OMPC_capture:
6332       case OMPC_seq_cst:
6333       case OMPC_acq_rel:
6334       case OMPC_acquire:
6335       case OMPC_release:
6336       case OMPC_relaxed:
6337       case OMPC_depend:
6338       case OMPC_threads:
6339       case OMPC_simd:
6340       case OMPC_map:
6341       case OMPC_nogroup:
6342       case OMPC_defaultmap:
6343       case OMPC_to:
6344       case OMPC_from:
6345       case OMPC_use_device_ptr:
6346       case OMPC_use_device_addr:
6347       case OMPC_is_device_ptr:
6348       case OMPC_nontemporal:
6349       case OMPC_order:
6350       case OMPC_destroy:
6351       case OMPC_inclusive:
6352       case OMPC_exclusive:
6353       case OMPC_uses_allocators:
6354       case OMPC_affinity:
6355         continue;
6356       case OMPC_allocator:
6357       case OMPC_flush:
6358       case OMPC_depobj:
6359       case OMPC_threadprivate:
6360       case OMPC_uniform:
6361       case OMPC_unknown:
6362       case OMPC_unified_address:
6363       case OMPC_unified_shared_memory:
6364       case OMPC_reverse_offload:
6365       case OMPC_dynamic_allocators:
6366       case OMPC_atomic_default_mem_order:
6367       case OMPC_device_type:
6368       case OMPC_match:
6369       case OMPC_when:
6370       default:
6371         llvm_unreachable("Unexpected clause");
6372       }
6373       for (Stmt *CC : C->children()) {
6374         if (CC)
6375           DSAChecker.Visit(CC);
6376       }
6377     }
6378     for (const auto &P : DSAChecker.getVarsWithInheritedDSA())
6379       VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
6380   }
6381   for (const auto &P : VarsWithInheritedDSA) {
6382     if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst()))
6383       continue;
6384     ErrorFound = true;
6385     if (DSAStack->getDefaultDSA() == DSA_none ||
6386         DSAStack->getDefaultDSA() == DSA_firstprivate) {
6387       Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
6388           << P.first << P.second->getSourceRange();
6389       Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none);
6390     } else if (getLangOpts().OpenMP >= 50) {
6391       Diag(P.second->getExprLoc(),
6392            diag::err_omp_defaultmap_no_attr_for_variable)
6393           << P.first << P.second->getSourceRange();
6394       Diag(DSAStack->getDefaultDSALocation(),
6395            diag::note_omp_defaultmap_attr_none);
6396     }
6397   }
6398 
6399   if (!AllowedNameModifiers.empty())
6400     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
6401                  ErrorFound;
6402 
6403   if (ErrorFound)
6404     return StmtError();
6405 
6406   if (!CurContext->isDependentContext() &&
6407       isOpenMPTargetExecutionDirective(Kind) &&
6408       !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
6409         DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
6410         DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
6411         DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
6412     // Register target to DSA Stack.
6413     DSAStack->addTargetDirLocation(StartLoc);
6414   }
6415 
6416   return Res;
6417 }
6418 
6419 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
6420     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
6421     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
6422     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
6423     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
6424   assert(Aligneds.size() == Alignments.size());
6425   assert(Linears.size() == LinModifiers.size());
6426   assert(Linears.size() == Steps.size());
6427   if (!DG || DG.get().isNull())
6428     return DeclGroupPtrTy();
6429 
6430   const int SimdId = 0;
6431   if (!DG.get().isSingleDecl()) {
6432     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
6433         << SimdId;
6434     return DG;
6435   }
6436   Decl *ADecl = DG.get().getSingleDecl();
6437   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
6438     ADecl = FTD->getTemplatedDecl();
6439 
6440   auto *FD = dyn_cast<FunctionDecl>(ADecl);
6441   if (!FD) {
6442     Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId;
6443     return DeclGroupPtrTy();
6444   }
6445 
6446   // OpenMP [2.8.2, declare simd construct, Description]
6447   // The parameter of the simdlen clause must be a constant positive integer
6448   // expression.
6449   ExprResult SL;
6450   if (Simdlen)
6451     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
6452   // OpenMP [2.8.2, declare simd construct, Description]
6453   // The special this pointer can be used as if was one of the arguments to the
6454   // function in any of the linear, aligned, or uniform clauses.
6455   // The uniform clause declares one or more arguments to have an invariant
6456   // value for all concurrent invocations of the function in the execution of a
6457   // single SIMD loop.
6458   llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
6459   const Expr *UniformedLinearThis = nullptr;
6460   for (const Expr *E : Uniforms) {
6461     E = E->IgnoreParenImpCasts();
6462     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
6463       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
6464         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6465             FD->getParamDecl(PVD->getFunctionScopeIndex())
6466                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
6467           UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
6468           continue;
6469         }
6470     if (isa<CXXThisExpr>(E)) {
6471       UniformedLinearThis = E;
6472       continue;
6473     }
6474     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
6475         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
6476   }
6477   // OpenMP [2.8.2, declare simd construct, Description]
6478   // The aligned clause declares that the object to which each list item points
6479   // is aligned to the number of bytes expressed in the optional parameter of
6480   // the aligned clause.
6481   // The special this pointer can be used as if was one of the arguments to the
6482   // function in any of the linear, aligned, or uniform clauses.
6483   // The type of list items appearing in the aligned clause must be array,
6484   // pointer, reference to array, or reference to pointer.
6485   llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
6486   const Expr *AlignedThis = nullptr;
6487   for (const Expr *E : Aligneds) {
6488     E = E->IgnoreParenImpCasts();
6489     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
6490       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
6491         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
6492         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6493             FD->getParamDecl(PVD->getFunctionScopeIndex())
6494                     ->getCanonicalDecl() == CanonPVD) {
6495           // OpenMP  [2.8.1, simd construct, Restrictions]
6496           // A list-item cannot appear in more than one aligned clause.
6497           if (AlignedArgs.count(CanonPVD) > 0) {
6498             Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice)
6499                 << 1 << getOpenMPClauseName(OMPC_aligned)
6500                 << E->getSourceRange();
6501             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
6502                  diag::note_omp_explicit_dsa)
6503                 << getOpenMPClauseName(OMPC_aligned);
6504             continue;
6505           }
6506           AlignedArgs[CanonPVD] = E;
6507           QualType QTy = PVD->getType()
6508                              .getNonReferenceType()
6509                              .getUnqualifiedType()
6510                              .getCanonicalType();
6511           const Type *Ty = QTy.getTypePtrOrNull();
6512           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
6513             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
6514                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
6515             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
6516           }
6517           continue;
6518         }
6519       }
6520     if (isa<CXXThisExpr>(E)) {
6521       if (AlignedThis) {
6522         Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice)
6523             << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange();
6524         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
6525             << getOpenMPClauseName(OMPC_aligned);
6526       }
6527       AlignedThis = E;
6528       continue;
6529     }
6530     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
6531         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
6532   }
6533   // The optional parameter of the aligned clause, alignment, must be a constant
6534   // positive integer expression. If no optional parameter is specified,
6535   // implementation-defined default alignments for SIMD instructions on the
6536   // target platforms are assumed.
6537   SmallVector<const Expr *, 4> NewAligns;
6538   for (Expr *E : Alignments) {
6539     ExprResult Align;
6540     if (E)
6541       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
6542     NewAligns.push_back(Align.get());
6543   }
6544   // OpenMP [2.8.2, declare simd construct, Description]
6545   // The linear clause declares one or more list items to be private to a SIMD
6546   // lane and to have a linear relationship with respect to the iteration space
6547   // of a loop.
6548   // The special this pointer can be used as if was one of the arguments to the
6549   // function in any of the linear, aligned, or uniform clauses.
6550   // When a linear-step expression is specified in a linear clause it must be
6551   // either a constant integer expression or an integer-typed parameter that is
6552   // specified in a uniform clause on the directive.
6553   llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
6554   const bool IsUniformedThis = UniformedLinearThis != nullptr;
6555   auto MI = LinModifiers.begin();
6556   for (const Expr *E : Linears) {
6557     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
6558     ++MI;
6559     E = E->IgnoreParenImpCasts();
6560     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
6561       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
6562         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
6563         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6564             FD->getParamDecl(PVD->getFunctionScopeIndex())
6565                     ->getCanonicalDecl() == CanonPVD) {
6566           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
6567           // A list-item cannot appear in more than one linear clause.
6568           if (LinearArgs.count(CanonPVD) > 0) {
6569             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
6570                 << getOpenMPClauseName(OMPC_linear)
6571                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
6572             Diag(LinearArgs[CanonPVD]->getExprLoc(),
6573                  diag::note_omp_explicit_dsa)
6574                 << getOpenMPClauseName(OMPC_linear);
6575             continue;
6576           }
6577           // Each argument can appear in at most one uniform or linear clause.
6578           if (UniformedArgs.count(CanonPVD) > 0) {
6579             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
6580                 << getOpenMPClauseName(OMPC_linear)
6581                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
6582             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
6583                  diag::note_omp_explicit_dsa)
6584                 << getOpenMPClauseName(OMPC_uniform);
6585             continue;
6586           }
6587           LinearArgs[CanonPVD] = E;
6588           if (E->isValueDependent() || E->isTypeDependent() ||
6589               E->isInstantiationDependent() ||
6590               E->containsUnexpandedParameterPack())
6591             continue;
6592           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
6593                                       PVD->getOriginalType(),
6594                                       /*IsDeclareSimd=*/true);
6595           continue;
6596         }
6597       }
6598     if (isa<CXXThisExpr>(E)) {
6599       if (UniformedLinearThis) {
6600         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
6601             << getOpenMPClauseName(OMPC_linear)
6602             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
6603             << E->getSourceRange();
6604         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
6605             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
6606                                                    : OMPC_linear);
6607         continue;
6608       }
6609       UniformedLinearThis = E;
6610       if (E->isValueDependent() || E->isTypeDependent() ||
6611           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
6612         continue;
6613       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
6614                                   E->getType(), /*IsDeclareSimd=*/true);
6615       continue;
6616     }
6617     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
6618         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
6619   }
6620   Expr *Step = nullptr;
6621   Expr *NewStep = nullptr;
6622   SmallVector<Expr *, 4> NewSteps;
6623   for (Expr *E : Steps) {
6624     // Skip the same step expression, it was checked already.
6625     if (Step == E || !E) {
6626       NewSteps.push_back(E ? NewStep : nullptr);
6627       continue;
6628     }
6629     Step = E;
6630     if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
6631       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
6632         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
6633         if (UniformedArgs.count(CanonPVD) == 0) {
6634           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
6635               << Step->getSourceRange();
6636         } else if (E->isValueDependent() || E->isTypeDependent() ||
6637                    E->isInstantiationDependent() ||
6638                    E->containsUnexpandedParameterPack() ||
6639                    CanonPVD->getType()->hasIntegerRepresentation()) {
6640           NewSteps.push_back(Step);
6641         } else {
6642           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
6643               << Step->getSourceRange();
6644         }
6645         continue;
6646       }
6647     NewStep = Step;
6648     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6649         !Step->isInstantiationDependent() &&
6650         !Step->containsUnexpandedParameterPack()) {
6651       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
6652                     .get();
6653       if (NewStep)
6654         NewStep =
6655             VerifyIntegerConstantExpression(NewStep, /*FIXME*/ AllowFold).get();
6656     }
6657     NewSteps.push_back(NewStep);
6658   }
6659   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
6660       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
6661       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
6662       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
6663       const_cast<Expr **>(Linears.data()), Linears.size(),
6664       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
6665       NewSteps.data(), NewSteps.size(), SR);
6666   ADecl->addAttr(NewAttr);
6667   return DG;
6668 }
6669 
6670 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto,
6671                          QualType NewType) {
6672   assert(NewType->isFunctionProtoType() &&
6673          "Expected function type with prototype.");
6674   assert(FD->getType()->isFunctionNoProtoType() &&
6675          "Expected function with type with no prototype.");
6676   assert(FDWithProto->getType()->isFunctionProtoType() &&
6677          "Expected function with prototype.");
6678   // Synthesize parameters with the same types.
6679   FD->setType(NewType);
6680   SmallVector<ParmVarDecl *, 16> Params;
6681   for (const ParmVarDecl *P : FDWithProto->parameters()) {
6682     auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(),
6683                                       SourceLocation(), nullptr, P->getType(),
6684                                       /*TInfo=*/nullptr, SC_None, nullptr);
6685     Param->setScopeInfo(0, Params.size());
6686     Param->setImplicit();
6687     Params.push_back(Param);
6688   }
6689 
6690   FD->setParams(Params);
6691 }
6692 
6693 void Sema::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) {
6694   if (D->isInvalidDecl())
6695     return;
6696   FunctionDecl *FD = nullptr;
6697   if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D))
6698     FD = UTemplDecl->getTemplatedDecl();
6699   else
6700     FD = cast<FunctionDecl>(D);
6701   assert(FD && "Expected a function declaration!");
6702 
6703   // If we are instantiating templates we do *not* apply scoped assumptions but
6704   // only global ones. We apply scoped assumption to the template definition
6705   // though.
6706   if (!inTemplateInstantiation()) {
6707     for (AssumptionAttr *AA : OMPAssumeScoped)
6708       FD->addAttr(AA);
6709   }
6710   for (AssumptionAttr *AA : OMPAssumeGlobal)
6711     FD->addAttr(AA);
6712 }
6713 
6714 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI)
6715     : TI(&TI), NameSuffix(TI.getMangledName()) {}
6716 
6717 void Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(
6718     Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists,
6719     SmallVectorImpl<FunctionDecl *> &Bases) {
6720   if (!D.getIdentifier())
6721     return;
6722 
6723   OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
6724 
6725   // Template specialization is an extension, check if we do it.
6726   bool IsTemplated = !TemplateParamLists.empty();
6727   if (IsTemplated &
6728       !DVScope.TI->isExtensionActive(
6729           llvm::omp::TraitProperty::implementation_extension_allow_templates))
6730     return;
6731 
6732   IdentifierInfo *BaseII = D.getIdentifier();
6733   LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(),
6734                       LookupOrdinaryName);
6735   LookupParsedName(Lookup, S, &D.getCXXScopeSpec());
6736 
6737   TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
6738   QualType FType = TInfo->getType();
6739 
6740   bool IsConstexpr =
6741       D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr;
6742   bool IsConsteval =
6743       D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval;
6744 
6745   for (auto *Candidate : Lookup) {
6746     auto *CandidateDecl = Candidate->getUnderlyingDecl();
6747     FunctionDecl *UDecl = nullptr;
6748     if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) {
6749       auto *FTD = cast<FunctionTemplateDecl>(CandidateDecl);
6750       if (FTD->getTemplateParameters()->size() == TemplateParamLists.size())
6751         UDecl = FTD->getTemplatedDecl();
6752     } else if (!IsTemplated)
6753       UDecl = dyn_cast<FunctionDecl>(CandidateDecl);
6754     if (!UDecl)
6755       continue;
6756 
6757     // Don't specialize constexpr/consteval functions with
6758     // non-constexpr/consteval functions.
6759     if (UDecl->isConstexpr() && !IsConstexpr)
6760       continue;
6761     if (UDecl->isConsteval() && !IsConsteval)
6762       continue;
6763 
6764     QualType UDeclTy = UDecl->getType();
6765     if (!UDeclTy->isDependentType()) {
6766       QualType NewType = Context.mergeFunctionTypes(
6767           FType, UDeclTy, /* OfBlockPointer */ false,
6768           /* Unqualified */ false, /* AllowCXX */ true);
6769       if (NewType.isNull())
6770         continue;
6771     }
6772 
6773     // Found a base!
6774     Bases.push_back(UDecl);
6775   }
6776 
6777   bool UseImplicitBase = !DVScope.TI->isExtensionActive(
6778       llvm::omp::TraitProperty::implementation_extension_disable_implicit_base);
6779   // If no base was found we create a declaration that we use as base.
6780   if (Bases.empty() && UseImplicitBase) {
6781     D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration);
6782     Decl *BaseD = HandleDeclarator(S, D, TemplateParamLists);
6783     BaseD->setImplicit(true);
6784     if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(BaseD))
6785       Bases.push_back(BaseTemplD->getTemplatedDecl());
6786     else
6787       Bases.push_back(cast<FunctionDecl>(BaseD));
6788   }
6789 
6790   std::string MangledName;
6791   MangledName += D.getIdentifier()->getName();
6792   MangledName += getOpenMPVariantManglingSeparatorStr();
6793   MangledName += DVScope.NameSuffix;
6794   IdentifierInfo &VariantII = Context.Idents.get(MangledName);
6795 
6796   VariantII.setMangledOpenMPVariantName(true);
6797   D.SetIdentifier(&VariantII, D.getBeginLoc());
6798 }
6799 
6800 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope(
6801     Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) {
6802   // Do not mark function as is used to prevent its emission if this is the
6803   // only place where it is used.
6804   EnterExpressionEvaluationContext Unevaluated(
6805       *this, Sema::ExpressionEvaluationContext::Unevaluated);
6806 
6807   FunctionDecl *FD = nullptr;
6808   if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D))
6809     FD = UTemplDecl->getTemplatedDecl();
6810   else
6811     FD = cast<FunctionDecl>(D);
6812   auto *VariantFuncRef = DeclRefExpr::Create(
6813       Context, NestedNameSpecifierLoc(), SourceLocation(), FD,
6814       /* RefersToEnclosingVariableOrCapture */ false,
6815       /* NameLoc */ FD->getLocation(), FD->getType(),
6816       ExprValueKind::VK_PRValue);
6817 
6818   OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
6819   auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit(
6820       Context, VariantFuncRef, DVScope.TI,
6821       /*NothingArgs=*/nullptr, /*NothingArgsSize=*/0,
6822       /*NeedDevicePtrArgs=*/nullptr, /*NeedDevicePtrArgsSize=*/0,
6823       /*AppendArgs=*/nullptr, /*AppendArgsSize=*/0);
6824   for (FunctionDecl *BaseFD : Bases)
6825     BaseFD->addAttr(OMPDeclareVariantA);
6826 }
6827 
6828 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope,
6829                                  SourceLocation LParenLoc,
6830                                  MultiExprArg ArgExprs,
6831                                  SourceLocation RParenLoc, Expr *ExecConfig) {
6832   // The common case is a regular call we do not want to specialize at all. Try
6833   // to make that case fast by bailing early.
6834   CallExpr *CE = dyn_cast<CallExpr>(Call.get());
6835   if (!CE)
6836     return Call;
6837 
6838   FunctionDecl *CalleeFnDecl = CE->getDirectCallee();
6839   if (!CalleeFnDecl)
6840     return Call;
6841 
6842   if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>())
6843     return Call;
6844 
6845   ASTContext &Context = getASTContext();
6846   std::function<void(StringRef)> DiagUnknownTrait = [this,
6847                                                      CE](StringRef ISATrait) {
6848     // TODO Track the selector locations in a way that is accessible here to
6849     // improve the diagnostic location.
6850     Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait)
6851         << ISATrait;
6852   };
6853   TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait),
6854                           getCurFunctionDecl(), DSAStack->getConstructTraits());
6855 
6856   QualType CalleeFnType = CalleeFnDecl->getType();
6857 
6858   SmallVector<Expr *, 4> Exprs;
6859   SmallVector<VariantMatchInfo, 4> VMIs;
6860   while (CalleeFnDecl) {
6861     for (OMPDeclareVariantAttr *A :
6862          CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) {
6863       Expr *VariantRef = A->getVariantFuncRef();
6864 
6865       VariantMatchInfo VMI;
6866       OMPTraitInfo &TI = A->getTraitInfo();
6867       TI.getAsVariantMatchInfo(Context, VMI);
6868       if (!isVariantApplicableInContext(VMI, OMPCtx,
6869                                         /* DeviceSetOnly */ false))
6870         continue;
6871 
6872       VMIs.push_back(VMI);
6873       Exprs.push_back(VariantRef);
6874     }
6875 
6876     CalleeFnDecl = CalleeFnDecl->getPreviousDecl();
6877   }
6878 
6879   ExprResult NewCall;
6880   do {
6881     int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx);
6882     if (BestIdx < 0)
6883       return Call;
6884     Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]);
6885     Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl();
6886 
6887     {
6888       // Try to build a (member) call expression for the current best applicable
6889       // variant expression. We allow this to fail in which case we continue
6890       // with the next best variant expression. The fail case is part of the
6891       // implementation defined behavior in the OpenMP standard when it talks
6892       // about what differences in the function prototypes: "Any differences
6893       // that the specific OpenMP context requires in the prototype of the
6894       // variant from the base function prototype are implementation defined."
6895       // This wording is there to allow the specialized variant to have a
6896       // different type than the base function. This is intended and OK but if
6897       // we cannot create a call the difference is not in the "implementation
6898       // defined range" we allow.
6899       Sema::TentativeAnalysisScope Trap(*this);
6900 
6901       if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) {
6902         auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE);
6903         BestExpr = MemberExpr::CreateImplicit(
6904             Context, MemberCall->getImplicitObjectArgument(),
6905             /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy,
6906             MemberCall->getValueKind(), MemberCall->getObjectKind());
6907       }
6908       NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc,
6909                               ExecConfig);
6910       if (NewCall.isUsable()) {
6911         if (CallExpr *NCE = dyn_cast<CallExpr>(NewCall.get())) {
6912           FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee();
6913           QualType NewType = Context.mergeFunctionTypes(
6914               CalleeFnType, NewCalleeFnDecl->getType(),
6915               /* OfBlockPointer */ false,
6916               /* Unqualified */ false, /* AllowCXX */ true);
6917           if (!NewType.isNull())
6918             break;
6919           // Don't use the call if the function type was not compatible.
6920           NewCall = nullptr;
6921         }
6922       }
6923     }
6924 
6925     VMIs.erase(VMIs.begin() + BestIdx);
6926     Exprs.erase(Exprs.begin() + BestIdx);
6927   } while (!VMIs.empty());
6928 
6929   if (!NewCall.isUsable())
6930     return Call;
6931   return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0);
6932 }
6933 
6934 Optional<std::pair<FunctionDecl *, Expr *>>
6935 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG,
6936                                         Expr *VariantRef, OMPTraitInfo &TI,
6937                                         unsigned NumAppendArgs,
6938                                         SourceRange SR) {
6939   if (!DG || DG.get().isNull())
6940     return None;
6941 
6942   const int VariantId = 1;
6943   // Must be applied only to single decl.
6944   if (!DG.get().isSingleDecl()) {
6945     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
6946         << VariantId << SR;
6947     return None;
6948   }
6949   Decl *ADecl = DG.get().getSingleDecl();
6950   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
6951     ADecl = FTD->getTemplatedDecl();
6952 
6953   // Decl must be a function.
6954   auto *FD = dyn_cast<FunctionDecl>(ADecl);
6955   if (!FD) {
6956     Diag(ADecl->getLocation(), diag::err_omp_function_expected)
6957         << VariantId << SR;
6958     return None;
6959   }
6960 
6961   auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) {
6962     return FD->hasAttrs() &&
6963            (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() ||
6964             FD->hasAttr<TargetAttr>());
6965   };
6966   // OpenMP is not compatible with CPU-specific attributes.
6967   if (HasMultiVersionAttributes(FD)) {
6968     Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes)
6969         << SR;
6970     return None;
6971   }
6972 
6973   // Allow #pragma omp declare variant only if the function is not used.
6974   if (FD->isUsed(false))
6975     Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used)
6976         << FD->getLocation();
6977 
6978   // Check if the function was emitted already.
6979   const FunctionDecl *Definition;
6980   if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) &&
6981       (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition)))
6982     Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted)
6983         << FD->getLocation();
6984 
6985   // The VariantRef must point to function.
6986   if (!VariantRef) {
6987     Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId;
6988     return None;
6989   }
6990 
6991   auto ShouldDelayChecks = [](Expr *&E, bool) {
6992     return E && (E->isTypeDependent() || E->isValueDependent() ||
6993                  E->containsUnexpandedParameterPack() ||
6994                  E->isInstantiationDependent());
6995   };
6996   // Do not check templates, wait until instantiation.
6997   if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) ||
6998       TI.anyScoreOrCondition(ShouldDelayChecks))
6999     return std::make_pair(FD, VariantRef);
7000 
7001   // Deal with non-constant score and user condition expressions.
7002   auto HandleNonConstantScoresAndConditions = [this](Expr *&E,
7003                                                      bool IsScore) -> bool {
7004     if (!E || E->isIntegerConstantExpr(Context))
7005       return false;
7006 
7007     if (IsScore) {
7008       // We warn on non-constant scores and pretend they were not present.
7009       Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant)
7010           << E;
7011       E = nullptr;
7012     } else {
7013       // We could replace a non-constant user condition with "false" but we
7014       // will soon need to handle these anyway for the dynamic version of
7015       // OpenMP context selectors.
7016       Diag(E->getExprLoc(),
7017            diag::err_omp_declare_variant_user_condition_not_constant)
7018           << E;
7019     }
7020     return true;
7021   };
7022   if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions))
7023     return None;
7024 
7025   QualType AdjustedFnType = FD->getType();
7026   if (NumAppendArgs) {
7027     if (isa<FunctionNoProtoType>(FD->getType())) {
7028       Diag(FD->getLocation(), diag::err_omp_declare_variant_prototype_required)
7029           << SR;
7030       return None;
7031     }
7032     // Adjust the function type to account for an extra omp_interop_t for each
7033     // specified in the append_args clause.
7034     const TypeDecl *TD = nullptr;
7035     LookupResult Result(*this, &Context.Idents.get("omp_interop_t"),
7036                         SR.getBegin(), Sema::LookupOrdinaryName);
7037     if (LookupName(Result, getCurScope())) {
7038       NamedDecl *ND = Result.getFoundDecl();
7039       TD = dyn_cast_or_null<TypeDecl>(ND);
7040     }
7041     if (!TD) {
7042       Diag(SR.getBegin(), diag::err_omp_interop_type_not_found) << SR;
7043       return None;
7044     }
7045     QualType InteropType = QualType(TD->getTypeForDecl(), 0);
7046     auto *PTy = cast<FunctionProtoType>(FD->getType());
7047     if (PTy->isVariadic()) {
7048       Diag(FD->getLocation(), diag::err_omp_append_args_with_varargs) << SR;
7049       return None;
7050     }
7051     llvm::SmallVector<QualType, 8> Params;
7052     Params.append(PTy->param_type_begin(), PTy->param_type_end());
7053     Params.insert(Params.end(), NumAppendArgs, InteropType);
7054     AdjustedFnType = Context.getFunctionType(PTy->getReturnType(), Params,
7055                                              PTy->getExtProtoInfo());
7056   }
7057 
7058   // Convert VariantRef expression to the type of the original function to
7059   // resolve possible conflicts.
7060   ExprResult VariantRefCast = VariantRef;
7061   if (LangOpts.CPlusPlus) {
7062     QualType FnPtrType;
7063     auto *Method = dyn_cast<CXXMethodDecl>(FD);
7064     if (Method && !Method->isStatic()) {
7065       const Type *ClassType =
7066           Context.getTypeDeclType(Method->getParent()).getTypePtr();
7067       FnPtrType = Context.getMemberPointerType(AdjustedFnType, ClassType);
7068       ExprResult ER;
7069       {
7070         // Build adrr_of unary op to correctly handle type checks for member
7071         // functions.
7072         Sema::TentativeAnalysisScope Trap(*this);
7073         ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf,
7074                                   VariantRef);
7075       }
7076       if (!ER.isUsable()) {
7077         Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
7078             << VariantId << VariantRef->getSourceRange();
7079         return None;
7080       }
7081       VariantRef = ER.get();
7082     } else {
7083       FnPtrType = Context.getPointerType(AdjustedFnType);
7084     }
7085     QualType VarianPtrType = Context.getPointerType(VariantRef->getType());
7086     if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) {
7087       ImplicitConversionSequence ICS = TryImplicitConversion(
7088           VariantRef, FnPtrType.getUnqualifiedType(),
7089           /*SuppressUserConversions=*/false, AllowedExplicit::None,
7090           /*InOverloadResolution=*/false,
7091           /*CStyle=*/false,
7092           /*AllowObjCWritebackConversion=*/false);
7093       if (ICS.isFailure()) {
7094         Diag(VariantRef->getExprLoc(),
7095              diag::err_omp_declare_variant_incompat_types)
7096             << VariantRef->getType()
7097             << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType())
7098             << (NumAppendArgs ? 1 : 0) << VariantRef->getSourceRange();
7099         return None;
7100       }
7101       VariantRefCast = PerformImplicitConversion(
7102           VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting);
7103       if (!VariantRefCast.isUsable())
7104         return None;
7105     }
7106     // Drop previously built artificial addr_of unary op for member functions.
7107     if (Method && !Method->isStatic()) {
7108       Expr *PossibleAddrOfVariantRef = VariantRefCast.get();
7109       if (auto *UO = dyn_cast<UnaryOperator>(
7110               PossibleAddrOfVariantRef->IgnoreImplicit()))
7111         VariantRefCast = UO->getSubExpr();
7112     }
7113   }
7114 
7115   ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get());
7116   if (!ER.isUsable() ||
7117       !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) {
7118     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
7119         << VariantId << VariantRef->getSourceRange();
7120     return None;
7121   }
7122 
7123   // The VariantRef must point to function.
7124   auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts());
7125   if (!DRE) {
7126     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
7127         << VariantId << VariantRef->getSourceRange();
7128     return None;
7129   }
7130   auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl());
7131   if (!NewFD) {
7132     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
7133         << VariantId << VariantRef->getSourceRange();
7134     return None;
7135   }
7136 
7137   // Check if function types are compatible in C.
7138   if (!LangOpts.CPlusPlus) {
7139     QualType NewType =
7140         Context.mergeFunctionTypes(AdjustedFnType, NewFD->getType());
7141     if (NewType.isNull()) {
7142       Diag(VariantRef->getExprLoc(),
7143            diag::err_omp_declare_variant_incompat_types)
7144           << NewFD->getType() << FD->getType() << (NumAppendArgs ? 1 : 0)
7145           << VariantRef->getSourceRange();
7146       return None;
7147     }
7148     if (NewType->isFunctionProtoType()) {
7149       if (FD->getType()->isFunctionNoProtoType())
7150         setPrototype(*this, FD, NewFD, NewType);
7151       else if (NewFD->getType()->isFunctionNoProtoType())
7152         setPrototype(*this, NewFD, FD, NewType);
7153     }
7154   }
7155 
7156   // Check if variant function is not marked with declare variant directive.
7157   if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) {
7158     Diag(VariantRef->getExprLoc(),
7159          diag::warn_omp_declare_variant_marked_as_declare_variant)
7160         << VariantRef->getSourceRange();
7161     SourceRange SR =
7162         NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange();
7163     Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR;
7164     return None;
7165   }
7166 
7167   enum DoesntSupport {
7168     VirtFuncs = 1,
7169     Constructors = 3,
7170     Destructors = 4,
7171     DeletedFuncs = 5,
7172     DefaultedFuncs = 6,
7173     ConstexprFuncs = 7,
7174     ConstevalFuncs = 8,
7175   };
7176   if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) {
7177     if (CXXFD->isVirtual()) {
7178       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7179           << VirtFuncs;
7180       return None;
7181     }
7182 
7183     if (isa<CXXConstructorDecl>(FD)) {
7184       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7185           << Constructors;
7186       return None;
7187     }
7188 
7189     if (isa<CXXDestructorDecl>(FD)) {
7190       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7191           << Destructors;
7192       return None;
7193     }
7194   }
7195 
7196   if (FD->isDeleted()) {
7197     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7198         << DeletedFuncs;
7199     return None;
7200   }
7201 
7202   if (FD->isDefaulted()) {
7203     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7204         << DefaultedFuncs;
7205     return None;
7206   }
7207 
7208   if (FD->isConstexpr()) {
7209     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
7210         << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs);
7211     return None;
7212   }
7213 
7214   // Check general compatibility.
7215   if (areMultiversionVariantFunctionsCompatible(
7216           FD, NewFD, PartialDiagnostic::NullDiagnostic(),
7217           PartialDiagnosticAt(SourceLocation(),
7218                               PartialDiagnostic::NullDiagnostic()),
7219           PartialDiagnosticAt(
7220               VariantRef->getExprLoc(),
7221               PDiag(diag::err_omp_declare_variant_doesnt_support)),
7222           PartialDiagnosticAt(VariantRef->getExprLoc(),
7223                               PDiag(diag::err_omp_declare_variant_diff)
7224                                   << FD->getLocation()),
7225           /*TemplatesSupported=*/true, /*ConstexprSupported=*/false,
7226           /*CLinkageMayDiffer=*/true))
7227     return None;
7228   return std::make_pair(FD, cast<Expr>(DRE));
7229 }
7230 
7231 void Sema::ActOnOpenMPDeclareVariantDirective(
7232     FunctionDecl *FD, Expr *VariantRef, OMPTraitInfo &TI,
7233     ArrayRef<Expr *> AdjustArgsNothing,
7234     ArrayRef<Expr *> AdjustArgsNeedDevicePtr,
7235     ArrayRef<OMPDeclareVariantAttr::InteropType> AppendArgs,
7236     SourceLocation AdjustArgsLoc, SourceLocation AppendArgsLoc,
7237     SourceRange SR) {
7238 
7239   // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
7240   // An adjust_args clause or append_args clause can only be specified if the
7241   // dispatch selector of the construct selector set appears in the match
7242   // clause.
7243 
7244   SmallVector<Expr *, 8> AllAdjustArgs;
7245   llvm::append_range(AllAdjustArgs, AdjustArgsNothing);
7246   llvm::append_range(AllAdjustArgs, AdjustArgsNeedDevicePtr);
7247 
7248   if (!AllAdjustArgs.empty() || !AppendArgs.empty()) {
7249     VariantMatchInfo VMI;
7250     TI.getAsVariantMatchInfo(Context, VMI);
7251     if (!llvm::is_contained(
7252             VMI.ConstructTraits,
7253             llvm::omp::TraitProperty::construct_dispatch_dispatch)) {
7254       if (!AllAdjustArgs.empty())
7255         Diag(AdjustArgsLoc, diag::err_omp_clause_requires_dispatch_construct)
7256             << getOpenMPClauseName(OMPC_adjust_args);
7257       if (!AppendArgs.empty())
7258         Diag(AppendArgsLoc, diag::err_omp_clause_requires_dispatch_construct)
7259             << getOpenMPClauseName(OMPC_append_args);
7260       return;
7261     }
7262   }
7263 
7264   // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
7265   // Each argument can only appear in a single adjust_args clause for each
7266   // declare variant directive.
7267   llvm::SmallPtrSet<const VarDecl *, 4> AdjustVars;
7268 
7269   for (Expr *E : AllAdjustArgs) {
7270     E = E->IgnoreParenImpCasts();
7271     if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
7272       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
7273         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7274         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7275             FD->getParamDecl(PVD->getFunctionScopeIndex())
7276                     ->getCanonicalDecl() == CanonPVD) {
7277           // It's a parameter of the function, check duplicates.
7278           if (!AdjustVars.insert(CanonPVD).second) {
7279             Diag(DRE->getLocation(), diag::err_omp_adjust_arg_multiple_clauses)
7280                 << PVD;
7281             return;
7282           }
7283           continue;
7284         }
7285       }
7286     }
7287     // Anything that is not a function parameter is an error.
7288     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) << FD << 0;
7289     return;
7290   }
7291 
7292   auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit(
7293       Context, VariantRef, &TI, const_cast<Expr **>(AdjustArgsNothing.data()),
7294       AdjustArgsNothing.size(),
7295       const_cast<Expr **>(AdjustArgsNeedDevicePtr.data()),
7296       AdjustArgsNeedDevicePtr.size(),
7297       const_cast<OMPDeclareVariantAttr::InteropType *>(AppendArgs.data()),
7298       AppendArgs.size(), SR);
7299   FD->addAttr(NewAttr);
7300 }
7301 
7302 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
7303                                               Stmt *AStmt,
7304                                               SourceLocation StartLoc,
7305                                               SourceLocation EndLoc) {
7306   if (!AStmt)
7307     return StmtError();
7308 
7309   auto *CS = cast<CapturedStmt>(AStmt);
7310   // 1.2.2 OpenMP Language Terminology
7311   // Structured block - An executable statement with a single entry at the
7312   // top and a single exit at the bottom.
7313   // The point of exit cannot be a branch out of the structured block.
7314   // longjmp() and throw() must not violate the entry/exit criteria.
7315   CS->getCapturedDecl()->setNothrow();
7316 
7317   setFunctionHasBranchProtectedScope();
7318 
7319   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
7320                                       DSAStack->getTaskgroupReductionRef(),
7321                                       DSAStack->isCancelRegion());
7322 }
7323 
7324 namespace {
7325 /// Iteration space of a single for loop.
7326 struct LoopIterationSpace final {
7327   /// True if the condition operator is the strict compare operator (<, > or
7328   /// !=).
7329   bool IsStrictCompare = false;
7330   /// Condition of the loop.
7331   Expr *PreCond = nullptr;
7332   /// This expression calculates the number of iterations in the loop.
7333   /// It is always possible to calculate it before starting the loop.
7334   Expr *NumIterations = nullptr;
7335   /// The loop counter variable.
7336   Expr *CounterVar = nullptr;
7337   /// Private loop counter variable.
7338   Expr *PrivateCounterVar = nullptr;
7339   /// This is initializer for the initial value of #CounterVar.
7340   Expr *CounterInit = nullptr;
7341   /// This is step for the #CounterVar used to generate its update:
7342   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
7343   Expr *CounterStep = nullptr;
7344   /// Should step be subtracted?
7345   bool Subtract = false;
7346   /// Source range of the loop init.
7347   SourceRange InitSrcRange;
7348   /// Source range of the loop condition.
7349   SourceRange CondSrcRange;
7350   /// Source range of the loop increment.
7351   SourceRange IncSrcRange;
7352   /// Minimum value that can have the loop control variable. Used to support
7353   /// non-rectangular loops. Applied only for LCV with the non-iterator types,
7354   /// since only such variables can be used in non-loop invariant expressions.
7355   Expr *MinValue = nullptr;
7356   /// Maximum value that can have the loop control variable. Used to support
7357   /// non-rectangular loops. Applied only for LCV with the non-iterator type,
7358   /// since only such variables can be used in non-loop invariant expressions.
7359   Expr *MaxValue = nullptr;
7360   /// true, if the lower bound depends on the outer loop control var.
7361   bool IsNonRectangularLB = false;
7362   /// true, if the upper bound depends on the outer loop control var.
7363   bool IsNonRectangularUB = false;
7364   /// Index of the loop this loop depends on and forms non-rectangular loop
7365   /// nest.
7366   unsigned LoopDependentIdx = 0;
7367   /// Final condition for the non-rectangular loop nest support. It is used to
7368   /// check that the number of iterations for this particular counter must be
7369   /// finished.
7370   Expr *FinalCondition = nullptr;
7371 };
7372 
7373 /// Helper class for checking canonical form of the OpenMP loops and
7374 /// extracting iteration space of each loop in the loop nest, that will be used
7375 /// for IR generation.
7376 class OpenMPIterationSpaceChecker {
7377   /// Reference to Sema.
7378   Sema &SemaRef;
7379   /// Does the loop associated directive support non-rectangular loops?
7380   bool SupportsNonRectangular;
7381   /// Data-sharing stack.
7382   DSAStackTy &Stack;
7383   /// A location for diagnostics (when there is no some better location).
7384   SourceLocation DefaultLoc;
7385   /// A location for diagnostics (when increment is not compatible).
7386   SourceLocation ConditionLoc;
7387   /// A source location for referring to loop init later.
7388   SourceRange InitSrcRange;
7389   /// A source location for referring to condition later.
7390   SourceRange ConditionSrcRange;
7391   /// A source location for referring to increment later.
7392   SourceRange IncrementSrcRange;
7393   /// Loop variable.
7394   ValueDecl *LCDecl = nullptr;
7395   /// Reference to loop variable.
7396   Expr *LCRef = nullptr;
7397   /// Lower bound (initializer for the var).
7398   Expr *LB = nullptr;
7399   /// Upper bound.
7400   Expr *UB = nullptr;
7401   /// Loop step (increment).
7402   Expr *Step = nullptr;
7403   /// This flag is true when condition is one of:
7404   ///   Var <  UB
7405   ///   Var <= UB
7406   ///   UB  >  Var
7407   ///   UB  >= Var
7408   /// This will have no value when the condition is !=
7409   llvm::Optional<bool> TestIsLessOp;
7410   /// This flag is true when condition is strict ( < or > ).
7411   bool TestIsStrictOp = false;
7412   /// This flag is true when step is subtracted on each iteration.
7413   bool SubtractStep = false;
7414   /// The outer loop counter this loop depends on (if any).
7415   const ValueDecl *DepDecl = nullptr;
7416   /// Contains number of loop (starts from 1) on which loop counter init
7417   /// expression of this loop depends on.
7418   Optional<unsigned> InitDependOnLC;
7419   /// Contains number of loop (starts from 1) on which loop counter condition
7420   /// expression of this loop depends on.
7421   Optional<unsigned> CondDependOnLC;
7422   /// Checks if the provide statement depends on the loop counter.
7423   Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer);
7424   /// Original condition required for checking of the exit condition for
7425   /// non-rectangular loop.
7426   Expr *Condition = nullptr;
7427 
7428 public:
7429   OpenMPIterationSpaceChecker(Sema &SemaRef, bool SupportsNonRectangular,
7430                               DSAStackTy &Stack, SourceLocation DefaultLoc)
7431       : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular),
7432         Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
7433   /// Check init-expr for canonical loop form and save loop counter
7434   /// variable - #Var and its initialization value - #LB.
7435   bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
7436   /// Check test-expr for canonical form, save upper-bound (#UB), flags
7437   /// for less/greater and for strict/non-strict comparison.
7438   bool checkAndSetCond(Expr *S);
7439   /// Check incr-expr for canonical loop form and return true if it
7440   /// does not conform, otherwise save loop step (#Step).
7441   bool checkAndSetInc(Expr *S);
7442   /// Return the loop counter variable.
7443   ValueDecl *getLoopDecl() const { return LCDecl; }
7444   /// Return the reference expression to loop counter variable.
7445   Expr *getLoopDeclRefExpr() const { return LCRef; }
7446   /// Source range of the loop init.
7447   SourceRange getInitSrcRange() const { return InitSrcRange; }
7448   /// Source range of the loop condition.
7449   SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
7450   /// Source range of the loop increment.
7451   SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
7452   /// True if the step should be subtracted.
7453   bool shouldSubtractStep() const { return SubtractStep; }
7454   /// True, if the compare operator is strict (<, > or !=).
7455   bool isStrictTestOp() const { return TestIsStrictOp; }
7456   /// Build the expression to calculate the number of iterations.
7457   Expr *buildNumIterations(
7458       Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
7459       llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
7460   /// Build the precondition expression for the loops.
7461   Expr *
7462   buildPreCond(Scope *S, Expr *Cond,
7463                llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
7464   /// Build reference expression to the counter be used for codegen.
7465   DeclRefExpr *
7466   buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
7467                   DSAStackTy &DSA) const;
7468   /// Build reference expression to the private counter be used for
7469   /// codegen.
7470   Expr *buildPrivateCounterVar() const;
7471   /// Build initialization of the counter be used for codegen.
7472   Expr *buildCounterInit() const;
7473   /// Build step of the counter be used for codegen.
7474   Expr *buildCounterStep() const;
7475   /// Build loop data with counter value for depend clauses in ordered
7476   /// directives.
7477   Expr *
7478   buildOrderedLoopData(Scope *S, Expr *Counter,
7479                        llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
7480                        SourceLocation Loc, Expr *Inc = nullptr,
7481                        OverloadedOperatorKind OOK = OO_Amp);
7482   /// Builds the minimum value for the loop counter.
7483   std::pair<Expr *, Expr *> buildMinMaxValues(
7484       Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
7485   /// Builds final condition for the non-rectangular loops.
7486   Expr *buildFinalCondition(Scope *S) const;
7487   /// Return true if any expression is dependent.
7488   bool dependent() const;
7489   /// Returns true if the initializer forms non-rectangular loop.
7490   bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); }
7491   /// Returns true if the condition forms non-rectangular loop.
7492   bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); }
7493   /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
7494   unsigned getLoopDependentIdx() const {
7495     return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0));
7496   }
7497 
7498 private:
7499   /// Check the right-hand side of an assignment in the increment
7500   /// expression.
7501   bool checkAndSetIncRHS(Expr *RHS);
7502   /// Helper to set loop counter variable and its initializer.
7503   bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
7504                       bool EmitDiags);
7505   /// Helper to set upper bound.
7506   bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
7507              SourceRange SR, SourceLocation SL);
7508   /// Helper to set loop increment.
7509   bool setStep(Expr *NewStep, bool Subtract);
7510 };
7511 
7512 bool OpenMPIterationSpaceChecker::dependent() const {
7513   if (!LCDecl) {
7514     assert(!LB && !UB && !Step);
7515     return false;
7516   }
7517   return LCDecl->getType()->isDependentType() ||
7518          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
7519          (Step && Step->isValueDependent());
7520 }
7521 
7522 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
7523                                                  Expr *NewLCRefExpr,
7524                                                  Expr *NewLB, bool EmitDiags) {
7525   // State consistency checking to ensure correct usage.
7526   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
7527          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
7528   if (!NewLCDecl || !NewLB || NewLB->containsErrors())
7529     return true;
7530   LCDecl = getCanonicalDecl(NewLCDecl);
7531   LCRef = NewLCRefExpr;
7532   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
7533     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
7534       if ((Ctor->isCopyOrMoveConstructor() ||
7535            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
7536           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
7537         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
7538   LB = NewLB;
7539   if (EmitDiags)
7540     InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true);
7541   return false;
7542 }
7543 
7544 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
7545                                         llvm::Optional<bool> LessOp,
7546                                         bool StrictOp, SourceRange SR,
7547                                         SourceLocation SL) {
7548   // State consistency checking to ensure correct usage.
7549   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
7550          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
7551   if (!NewUB || NewUB->containsErrors())
7552     return true;
7553   UB = NewUB;
7554   if (LessOp)
7555     TestIsLessOp = LessOp;
7556   TestIsStrictOp = StrictOp;
7557   ConditionSrcRange = SR;
7558   ConditionLoc = SL;
7559   CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false);
7560   return false;
7561 }
7562 
7563 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
7564   // State consistency checking to ensure correct usage.
7565   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
7566   if (!NewStep || NewStep->containsErrors())
7567     return true;
7568   if (!NewStep->isValueDependent()) {
7569     // Check that the step is integer expression.
7570     SourceLocation StepLoc = NewStep->getBeginLoc();
7571     ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
7572         StepLoc, getExprAsWritten(NewStep));
7573     if (Val.isInvalid())
7574       return true;
7575     NewStep = Val.get();
7576 
7577     // OpenMP [2.6, Canonical Loop Form, Restrictions]
7578     //  If test-expr is of form var relational-op b and relational-op is < or
7579     //  <= then incr-expr must cause var to increase on each iteration of the
7580     //  loop. If test-expr is of form var relational-op b and relational-op is
7581     //  > or >= then incr-expr must cause var to decrease on each iteration of
7582     //  the loop.
7583     //  If test-expr is of form b relational-op var and relational-op is < or
7584     //  <= then incr-expr must cause var to decrease on each iteration of the
7585     //  loop. If test-expr is of form b relational-op var and relational-op is
7586     //  > or >= then incr-expr must cause var to increase on each iteration of
7587     //  the loop.
7588     Optional<llvm::APSInt> Result =
7589         NewStep->getIntegerConstantExpr(SemaRef.Context);
7590     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
7591     bool IsConstNeg =
7592         Result && Result->isSigned() && (Subtract != Result->isNegative());
7593     bool IsConstPos =
7594         Result && Result->isSigned() && (Subtract == Result->isNegative());
7595     bool IsConstZero = Result && !Result->getBoolValue();
7596 
7597     // != with increment is treated as <; != with decrement is treated as >
7598     if (!TestIsLessOp.hasValue())
7599       TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
7600     if (UB && (IsConstZero ||
7601                (TestIsLessOp.getValue() ?
7602                   (IsConstNeg || (IsUnsigned && Subtract)) :
7603                   (IsConstPos || (IsUnsigned && !Subtract))))) {
7604       SemaRef.Diag(NewStep->getExprLoc(),
7605                    diag::err_omp_loop_incr_not_compatible)
7606           << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
7607       SemaRef.Diag(ConditionLoc,
7608                    diag::note_omp_loop_cond_requres_compatible_incr)
7609           << TestIsLessOp.getValue() << ConditionSrcRange;
7610       return true;
7611     }
7612     if (TestIsLessOp.getValue() == Subtract) {
7613       NewStep =
7614           SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
7615               .get();
7616       Subtract = !Subtract;
7617     }
7618   }
7619 
7620   Step = NewStep;
7621   SubtractStep = Subtract;
7622   return false;
7623 }
7624 
7625 namespace {
7626 /// Checker for the non-rectangular loops. Checks if the initializer or
7627 /// condition expression references loop counter variable.
7628 class LoopCounterRefChecker final
7629     : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
7630   Sema &SemaRef;
7631   DSAStackTy &Stack;
7632   const ValueDecl *CurLCDecl = nullptr;
7633   const ValueDecl *DepDecl = nullptr;
7634   const ValueDecl *PrevDepDecl = nullptr;
7635   bool IsInitializer = true;
7636   bool SupportsNonRectangular;
7637   unsigned BaseLoopId = 0;
7638   bool checkDecl(const Expr *E, const ValueDecl *VD) {
7639     if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) {
7640       SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter)
7641           << (IsInitializer ? 0 : 1);
7642       return false;
7643     }
7644     const auto &&Data = Stack.isLoopControlVariable(VD);
7645     // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
7646     // The type of the loop iterator on which we depend may not have a random
7647     // access iterator type.
7648     if (Data.first && VD->getType()->isRecordType()) {
7649       SmallString<128> Name;
7650       llvm::raw_svector_ostream OS(Name);
7651       VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
7652                                /*Qualified=*/true);
7653       SemaRef.Diag(E->getExprLoc(),
7654                    diag::err_omp_wrong_dependency_iterator_type)
7655           << OS.str();
7656       SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD;
7657       return false;
7658     }
7659     if (Data.first && !SupportsNonRectangular) {
7660       SemaRef.Diag(E->getExprLoc(), diag::err_omp_invariant_dependency);
7661       return false;
7662     }
7663     if (Data.first &&
7664         (DepDecl || (PrevDepDecl &&
7665                      getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) {
7666       if (!DepDecl && PrevDepDecl)
7667         DepDecl = PrevDepDecl;
7668       SmallString<128> Name;
7669       llvm::raw_svector_ostream OS(Name);
7670       DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
7671                                     /*Qualified=*/true);
7672       SemaRef.Diag(E->getExprLoc(),
7673                    diag::err_omp_invariant_or_linear_dependency)
7674           << OS.str();
7675       return false;
7676     }
7677     if (Data.first) {
7678       DepDecl = VD;
7679       BaseLoopId = Data.first;
7680     }
7681     return Data.first;
7682   }
7683 
7684 public:
7685   bool VisitDeclRefExpr(const DeclRefExpr *E) {
7686     const ValueDecl *VD = E->getDecl();
7687     if (isa<VarDecl>(VD))
7688       return checkDecl(E, VD);
7689     return false;
7690   }
7691   bool VisitMemberExpr(const MemberExpr *E) {
7692     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
7693       const ValueDecl *VD = E->getMemberDecl();
7694       if (isa<VarDecl>(VD) || isa<FieldDecl>(VD))
7695         return checkDecl(E, VD);
7696     }
7697     return false;
7698   }
7699   bool VisitStmt(const Stmt *S) {
7700     bool Res = false;
7701     for (const Stmt *Child : S->children())
7702       Res = (Child && Visit(Child)) || Res;
7703     return Res;
7704   }
7705   explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
7706                                  const ValueDecl *CurLCDecl, bool IsInitializer,
7707                                  const ValueDecl *PrevDepDecl = nullptr,
7708                                  bool SupportsNonRectangular = true)
7709       : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
7710         PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer),
7711         SupportsNonRectangular(SupportsNonRectangular) {}
7712   unsigned getBaseLoopId() const {
7713     assert(CurLCDecl && "Expected loop dependency.");
7714     return BaseLoopId;
7715   }
7716   const ValueDecl *getDepDecl() const {
7717     assert(CurLCDecl && "Expected loop dependency.");
7718     return DepDecl;
7719   }
7720 };
7721 } // namespace
7722 
7723 Optional<unsigned>
7724 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
7725                                                      bool IsInitializer) {
7726   // Check for the non-rectangular loops.
7727   LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
7728                                         DepDecl, SupportsNonRectangular);
7729   if (LoopStmtChecker.Visit(S)) {
7730     DepDecl = LoopStmtChecker.getDepDecl();
7731     return LoopStmtChecker.getBaseLoopId();
7732   }
7733   return llvm::None;
7734 }
7735 
7736 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
7737   // Check init-expr for canonical loop form and save loop counter
7738   // variable - #Var and its initialization value - #LB.
7739   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
7740   //   var = lb
7741   //   integer-type var = lb
7742   //   random-access-iterator-type var = lb
7743   //   pointer-type var = lb
7744   //
7745   if (!S) {
7746     if (EmitDiags) {
7747       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
7748     }
7749     return true;
7750   }
7751   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
7752     if (!ExprTemp->cleanupsHaveSideEffects())
7753       S = ExprTemp->getSubExpr();
7754 
7755   InitSrcRange = S->getSourceRange();
7756   if (Expr *E = dyn_cast<Expr>(S))
7757     S = E->IgnoreParens();
7758   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
7759     if (BO->getOpcode() == BO_Assign) {
7760       Expr *LHS = BO->getLHS()->IgnoreParens();
7761       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
7762         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
7763           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
7764             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
7765                                   EmitDiags);
7766         return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags);
7767       }
7768       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
7769         if (ME->isArrow() &&
7770             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
7771           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
7772                                 EmitDiags);
7773       }
7774     }
7775   } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
7776     if (DS->isSingleDecl()) {
7777       if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
7778         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
7779           // Accept non-canonical init form here but emit ext. warning.
7780           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
7781             SemaRef.Diag(S->getBeginLoc(),
7782                          diag::ext_omp_loop_not_canonical_init)
7783                 << S->getSourceRange();
7784           return setLCDeclAndLB(
7785               Var,
7786               buildDeclRefExpr(SemaRef, Var,
7787                                Var->getType().getNonReferenceType(),
7788                                DS->getBeginLoc()),
7789               Var->getInit(), EmitDiags);
7790         }
7791       }
7792     }
7793   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
7794     if (CE->getOperator() == OO_Equal) {
7795       Expr *LHS = CE->getArg(0);
7796       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
7797         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
7798           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
7799             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
7800                                   EmitDiags);
7801         return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags);
7802       }
7803       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
7804         if (ME->isArrow() &&
7805             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
7806           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
7807                                 EmitDiags);
7808       }
7809     }
7810   }
7811 
7812   if (dependent() || SemaRef.CurContext->isDependentContext())
7813     return false;
7814   if (EmitDiags) {
7815     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
7816         << S->getSourceRange();
7817   }
7818   return true;
7819 }
7820 
7821 /// Ignore parenthesizes, implicit casts, copy constructor and return the
7822 /// variable (which may be the loop variable) if possible.
7823 static const ValueDecl *getInitLCDecl(const Expr *E) {
7824   if (!E)
7825     return nullptr;
7826   E = getExprAsWritten(E);
7827   if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
7828     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
7829       if ((Ctor->isCopyOrMoveConstructor() ||
7830            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
7831           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
7832         E = CE->getArg(0)->IgnoreParenImpCasts();
7833   if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
7834     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
7835       return getCanonicalDecl(VD);
7836   }
7837   if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
7838     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
7839       return getCanonicalDecl(ME->getMemberDecl());
7840   return nullptr;
7841 }
7842 
7843 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
7844   // Check test-expr for canonical form, save upper-bound UB, flags for
7845   // less/greater and for strict/non-strict comparison.
7846   // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
7847   //   var relational-op b
7848   //   b relational-op var
7849   //
7850   bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
7851   if (!S) {
7852     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond)
7853         << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
7854     return true;
7855   }
7856   Condition = S;
7857   S = getExprAsWritten(S);
7858   SourceLocation CondLoc = S->getBeginLoc();
7859   auto &&CheckAndSetCond = [this, IneqCondIsCanonical](
7860                                BinaryOperatorKind Opcode, const Expr *LHS,
7861                                const Expr *RHS, SourceRange SR,
7862                                SourceLocation OpLoc) -> llvm::Optional<bool> {
7863     if (BinaryOperator::isRelationalOp(Opcode)) {
7864       if (getInitLCDecl(LHS) == LCDecl)
7865         return setUB(const_cast<Expr *>(RHS),
7866                      (Opcode == BO_LT || Opcode == BO_LE),
7867                      (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc);
7868       if (getInitLCDecl(RHS) == LCDecl)
7869         return setUB(const_cast<Expr *>(LHS),
7870                      (Opcode == BO_GT || Opcode == BO_GE),
7871                      (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc);
7872     } else if (IneqCondIsCanonical && Opcode == BO_NE) {
7873       return setUB(const_cast<Expr *>(getInitLCDecl(LHS) == LCDecl ? RHS : LHS),
7874                    /*LessOp=*/llvm::None,
7875                    /*StrictOp=*/true, SR, OpLoc);
7876     }
7877     return llvm::None;
7878   };
7879   llvm::Optional<bool> Res;
7880   if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(S)) {
7881     CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm();
7882     Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(),
7883                           RBO->getOperatorLoc());
7884   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
7885     Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(),
7886                           BO->getSourceRange(), BO->getOperatorLoc());
7887   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
7888     if (CE->getNumArgs() == 2) {
7889       Res = CheckAndSetCond(
7890           BinaryOperator::getOverloadedOpcode(CE->getOperator()), CE->getArg(0),
7891           CE->getArg(1), CE->getSourceRange(), CE->getOperatorLoc());
7892     }
7893   }
7894   if (Res.hasValue())
7895     return *Res;
7896   if (dependent() || SemaRef.CurContext->isDependentContext())
7897     return false;
7898   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
7899       << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
7900   return true;
7901 }
7902 
7903 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
7904   // RHS of canonical loop form increment can be:
7905   //   var + incr
7906   //   incr + var
7907   //   var - incr
7908   //
7909   RHS = RHS->IgnoreParenImpCasts();
7910   if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
7911     if (BO->isAdditiveOp()) {
7912       bool IsAdd = BO->getOpcode() == BO_Add;
7913       if (getInitLCDecl(BO->getLHS()) == LCDecl)
7914         return setStep(BO->getRHS(), !IsAdd);
7915       if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
7916         return setStep(BO->getLHS(), /*Subtract=*/false);
7917     }
7918   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
7919     bool IsAdd = CE->getOperator() == OO_Plus;
7920     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
7921       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
7922         return setStep(CE->getArg(1), !IsAdd);
7923       if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
7924         return setStep(CE->getArg(0), /*Subtract=*/false);
7925     }
7926   }
7927   if (dependent() || SemaRef.CurContext->isDependentContext())
7928     return false;
7929   SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
7930       << RHS->getSourceRange() << LCDecl;
7931   return true;
7932 }
7933 
7934 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
7935   // Check incr-expr for canonical loop form and return true if it
7936   // does not conform.
7937   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
7938   //   ++var
7939   //   var++
7940   //   --var
7941   //   var--
7942   //   var += incr
7943   //   var -= incr
7944   //   var = var + incr
7945   //   var = incr + var
7946   //   var = var - incr
7947   //
7948   if (!S) {
7949     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
7950     return true;
7951   }
7952   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
7953     if (!ExprTemp->cleanupsHaveSideEffects())
7954       S = ExprTemp->getSubExpr();
7955 
7956   IncrementSrcRange = S->getSourceRange();
7957   S = S->IgnoreParens();
7958   if (auto *UO = dyn_cast<UnaryOperator>(S)) {
7959     if (UO->isIncrementDecrementOp() &&
7960         getInitLCDecl(UO->getSubExpr()) == LCDecl)
7961       return setStep(SemaRef
7962                          .ActOnIntegerConstant(UO->getBeginLoc(),
7963                                                (UO->isDecrementOp() ? -1 : 1))
7964                          .get(),
7965                      /*Subtract=*/false);
7966   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
7967     switch (BO->getOpcode()) {
7968     case BO_AddAssign:
7969     case BO_SubAssign:
7970       if (getInitLCDecl(BO->getLHS()) == LCDecl)
7971         return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
7972       break;
7973     case BO_Assign:
7974       if (getInitLCDecl(BO->getLHS()) == LCDecl)
7975         return checkAndSetIncRHS(BO->getRHS());
7976       break;
7977     default:
7978       break;
7979     }
7980   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
7981     switch (CE->getOperator()) {
7982     case OO_PlusPlus:
7983     case OO_MinusMinus:
7984       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
7985         return setStep(SemaRef
7986                            .ActOnIntegerConstant(
7987                                CE->getBeginLoc(),
7988                                ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
7989                            .get(),
7990                        /*Subtract=*/false);
7991       break;
7992     case OO_PlusEqual:
7993     case OO_MinusEqual:
7994       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
7995         return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
7996       break;
7997     case OO_Equal:
7998       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
7999         return checkAndSetIncRHS(CE->getArg(1));
8000       break;
8001     default:
8002       break;
8003     }
8004   }
8005   if (dependent() || SemaRef.CurContext->isDependentContext())
8006     return false;
8007   SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
8008       << S->getSourceRange() << LCDecl;
8009   return true;
8010 }
8011 
8012 static ExprResult
8013 tryBuildCapture(Sema &SemaRef, Expr *Capture,
8014                 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
8015   if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors())
8016     return Capture;
8017   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
8018     return SemaRef.PerformImplicitConversion(
8019         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
8020         /*AllowExplicit=*/true);
8021   auto I = Captures.find(Capture);
8022   if (I != Captures.end())
8023     return buildCapture(SemaRef, Capture, I->second);
8024   DeclRefExpr *Ref = nullptr;
8025   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
8026   Captures[Capture] = Ref;
8027   return Res;
8028 }
8029 
8030 /// Calculate number of iterations, transforming to unsigned, if number of
8031 /// iterations may be larger than the original type.
8032 static Expr *
8033 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc,
8034                   Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy,
8035                   bool TestIsStrictOp, bool RoundToStep,
8036                   llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
8037   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
8038   if (!NewStep.isUsable())
8039     return nullptr;
8040   llvm::APSInt LRes, SRes;
8041   bool IsLowerConst = false, IsStepConst = false;
8042   if (Optional<llvm::APSInt> Res = Lower->getIntegerConstantExpr(SemaRef.Context)) {
8043     LRes = *Res;
8044     IsLowerConst = true;
8045   }
8046   if (Optional<llvm::APSInt> Res = Step->getIntegerConstantExpr(SemaRef.Context)) {
8047     SRes = *Res;
8048     IsStepConst = true;
8049   }
8050   bool NoNeedToConvert = IsLowerConst && !RoundToStep &&
8051                          ((!TestIsStrictOp && LRes.isNonNegative()) ||
8052                           (TestIsStrictOp && LRes.isStrictlyPositive()));
8053   bool NeedToReorganize = false;
8054   // Check if any subexpressions in Lower -Step [+ 1] lead to overflow.
8055   if (!NoNeedToConvert && IsLowerConst &&
8056       (TestIsStrictOp || (RoundToStep && IsStepConst))) {
8057     NoNeedToConvert = true;
8058     if (RoundToStep) {
8059       unsigned BW = LRes.getBitWidth() > SRes.getBitWidth()
8060                         ? LRes.getBitWidth()
8061                         : SRes.getBitWidth();
8062       LRes = LRes.extend(BW + 1);
8063       LRes.setIsSigned(true);
8064       SRes = SRes.extend(BW + 1);
8065       SRes.setIsSigned(true);
8066       LRes -= SRes;
8067       NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes;
8068       LRes = LRes.trunc(BW);
8069     }
8070     if (TestIsStrictOp) {
8071       unsigned BW = LRes.getBitWidth();
8072       LRes = LRes.extend(BW + 1);
8073       LRes.setIsSigned(true);
8074       ++LRes;
8075       NoNeedToConvert =
8076           NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes;
8077       // truncate to the original bitwidth.
8078       LRes = LRes.trunc(BW);
8079     }
8080     NeedToReorganize = NoNeedToConvert;
8081   }
8082   llvm::APSInt URes;
8083   bool IsUpperConst = false;
8084   if (Optional<llvm::APSInt> Res = Upper->getIntegerConstantExpr(SemaRef.Context)) {
8085     URes = *Res;
8086     IsUpperConst = true;
8087   }
8088   if (NoNeedToConvert && IsLowerConst && IsUpperConst &&
8089       (!RoundToStep || IsStepConst)) {
8090     unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth()
8091                                                           : URes.getBitWidth();
8092     LRes = LRes.extend(BW + 1);
8093     LRes.setIsSigned(true);
8094     URes = URes.extend(BW + 1);
8095     URes.setIsSigned(true);
8096     URes -= LRes;
8097     NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes;
8098     NeedToReorganize = NoNeedToConvert;
8099   }
8100   // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant
8101   // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to
8102   // unsigned.
8103   if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) &&
8104       !LCTy->isDependentType() && LCTy->isIntegerType()) {
8105     QualType LowerTy = Lower->getType();
8106     QualType UpperTy = Upper->getType();
8107     uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy);
8108     uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy);
8109     if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) ||
8110         (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) {
8111       QualType CastType = SemaRef.Context.getIntTypeForBitwidth(
8112           LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0);
8113       Upper =
8114           SemaRef
8115               .PerformImplicitConversion(
8116                   SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(),
8117                   CastType, Sema::AA_Converting)
8118               .get();
8119       Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get();
8120       NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get());
8121     }
8122   }
8123   if (!Lower || !Upper || NewStep.isInvalid())
8124     return nullptr;
8125 
8126   ExprResult Diff;
8127   // If need to reorganize, then calculate the form as Upper - (Lower - Step [+
8128   // 1]).
8129   if (NeedToReorganize) {
8130     Diff = Lower;
8131 
8132     if (RoundToStep) {
8133       // Lower - Step
8134       Diff =
8135           SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get());
8136       if (!Diff.isUsable())
8137         return nullptr;
8138     }
8139 
8140     // Lower - Step [+ 1]
8141     if (TestIsStrictOp)
8142       Diff = SemaRef.BuildBinOp(
8143           S, DefaultLoc, BO_Add, Diff.get(),
8144           SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
8145     if (!Diff.isUsable())
8146       return nullptr;
8147 
8148     Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
8149     if (!Diff.isUsable())
8150       return nullptr;
8151 
8152     // Upper - (Lower - Step [+ 1]).
8153     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get());
8154     if (!Diff.isUsable())
8155       return nullptr;
8156   } else {
8157     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
8158 
8159     if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) {
8160       // BuildBinOp already emitted error, this one is to point user to upper
8161       // and lower bound, and to tell what is passed to 'operator-'.
8162       SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
8163           << Upper->getSourceRange() << Lower->getSourceRange();
8164       return nullptr;
8165     }
8166 
8167     if (!Diff.isUsable())
8168       return nullptr;
8169 
8170     // Upper - Lower [- 1]
8171     if (TestIsStrictOp)
8172       Diff = SemaRef.BuildBinOp(
8173           S, DefaultLoc, BO_Sub, Diff.get(),
8174           SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
8175     if (!Diff.isUsable())
8176       return nullptr;
8177 
8178     if (RoundToStep) {
8179       // Upper - Lower [- 1] + Step
8180       Diff =
8181           SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
8182       if (!Diff.isUsable())
8183         return nullptr;
8184     }
8185   }
8186 
8187   // Parentheses (for dumping/debugging purposes only).
8188   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
8189   if (!Diff.isUsable())
8190     return nullptr;
8191 
8192   // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step
8193   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
8194   if (!Diff.isUsable())
8195     return nullptr;
8196 
8197   return Diff.get();
8198 }
8199 
8200 /// Build the expression to calculate the number of iterations.
8201 Expr *OpenMPIterationSpaceChecker::buildNumIterations(
8202     Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
8203     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
8204   QualType VarType = LCDecl->getType().getNonReferenceType();
8205   if (!VarType->isIntegerType() && !VarType->isPointerType() &&
8206       !SemaRef.getLangOpts().CPlusPlus)
8207     return nullptr;
8208   Expr *LBVal = LB;
8209   Expr *UBVal = UB;
8210   // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
8211   // max(LB(MinVal), LB(MaxVal))
8212   if (InitDependOnLC) {
8213     const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1];
8214     if (!IS.MinValue || !IS.MaxValue)
8215       return nullptr;
8216     // OuterVar = Min
8217     ExprResult MinValue =
8218         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
8219     if (!MinValue.isUsable())
8220       return nullptr;
8221 
8222     ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
8223                                              IS.CounterVar, MinValue.get());
8224     if (!LBMinVal.isUsable())
8225       return nullptr;
8226     // OuterVar = Min, LBVal
8227     LBMinVal =
8228         SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal);
8229     if (!LBMinVal.isUsable())
8230       return nullptr;
8231     // (OuterVar = Min, LBVal)
8232     LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get());
8233     if (!LBMinVal.isUsable())
8234       return nullptr;
8235 
8236     // OuterVar = Max
8237     ExprResult MaxValue =
8238         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
8239     if (!MaxValue.isUsable())
8240       return nullptr;
8241 
8242     ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
8243                                              IS.CounterVar, MaxValue.get());
8244     if (!LBMaxVal.isUsable())
8245       return nullptr;
8246     // OuterVar = Max, LBVal
8247     LBMaxVal =
8248         SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal);
8249     if (!LBMaxVal.isUsable())
8250       return nullptr;
8251     // (OuterVar = Max, LBVal)
8252     LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get());
8253     if (!LBMaxVal.isUsable())
8254       return nullptr;
8255 
8256     Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get();
8257     Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get();
8258     if (!LBMin || !LBMax)
8259       return nullptr;
8260     // LB(MinVal) < LB(MaxVal)
8261     ExprResult MinLessMaxRes =
8262         SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax);
8263     if (!MinLessMaxRes.isUsable())
8264       return nullptr;
8265     Expr *MinLessMax =
8266         tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get();
8267     if (!MinLessMax)
8268       return nullptr;
8269     if (TestIsLessOp.getValue()) {
8270       // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
8271       // LB(MaxVal))
8272       ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
8273                                                     MinLessMax, LBMin, LBMax);
8274       if (!MinLB.isUsable())
8275         return nullptr;
8276       LBVal = MinLB.get();
8277     } else {
8278       // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
8279       // LB(MaxVal))
8280       ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
8281                                                     MinLessMax, LBMax, LBMin);
8282       if (!MaxLB.isUsable())
8283         return nullptr;
8284       LBVal = MaxLB.get();
8285     }
8286   }
8287   // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
8288   // min(UB(MinVal), UB(MaxVal))
8289   if (CondDependOnLC) {
8290     const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1];
8291     if (!IS.MinValue || !IS.MaxValue)
8292       return nullptr;
8293     // OuterVar = Min
8294     ExprResult MinValue =
8295         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
8296     if (!MinValue.isUsable())
8297       return nullptr;
8298 
8299     ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
8300                                              IS.CounterVar, MinValue.get());
8301     if (!UBMinVal.isUsable())
8302       return nullptr;
8303     // OuterVar = Min, UBVal
8304     UBMinVal =
8305         SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal);
8306     if (!UBMinVal.isUsable())
8307       return nullptr;
8308     // (OuterVar = Min, UBVal)
8309     UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get());
8310     if (!UBMinVal.isUsable())
8311       return nullptr;
8312 
8313     // OuterVar = Max
8314     ExprResult MaxValue =
8315         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
8316     if (!MaxValue.isUsable())
8317       return nullptr;
8318 
8319     ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
8320                                              IS.CounterVar, MaxValue.get());
8321     if (!UBMaxVal.isUsable())
8322       return nullptr;
8323     // OuterVar = Max, UBVal
8324     UBMaxVal =
8325         SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal);
8326     if (!UBMaxVal.isUsable())
8327       return nullptr;
8328     // (OuterVar = Max, UBVal)
8329     UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get());
8330     if (!UBMaxVal.isUsable())
8331       return nullptr;
8332 
8333     Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get();
8334     Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get();
8335     if (!UBMin || !UBMax)
8336       return nullptr;
8337     // UB(MinVal) > UB(MaxVal)
8338     ExprResult MinGreaterMaxRes =
8339         SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax);
8340     if (!MinGreaterMaxRes.isUsable())
8341       return nullptr;
8342     Expr *MinGreaterMax =
8343         tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get();
8344     if (!MinGreaterMax)
8345       return nullptr;
8346     if (TestIsLessOp.getValue()) {
8347       // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
8348       // UB(MaxVal))
8349       ExprResult MaxUB = SemaRef.ActOnConditionalOp(
8350           DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax);
8351       if (!MaxUB.isUsable())
8352         return nullptr;
8353       UBVal = MaxUB.get();
8354     } else {
8355       // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
8356       // UB(MaxVal))
8357       ExprResult MinUB = SemaRef.ActOnConditionalOp(
8358           DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin);
8359       if (!MinUB.isUsable())
8360         return nullptr;
8361       UBVal = MinUB.get();
8362     }
8363   }
8364   Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal;
8365   Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal;
8366   Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
8367   Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
8368   if (!Upper || !Lower)
8369     return nullptr;
8370 
8371   ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper,
8372                                       Step, VarType, TestIsStrictOp,
8373                                       /*RoundToStep=*/true, Captures);
8374   if (!Diff.isUsable())
8375     return nullptr;
8376 
8377   // OpenMP runtime requires 32-bit or 64-bit loop variables.
8378   QualType Type = Diff.get()->getType();
8379   ASTContext &C = SemaRef.Context;
8380   bool UseVarType = VarType->hasIntegerRepresentation() &&
8381                     C.getTypeSize(Type) > C.getTypeSize(VarType);
8382   if (!Type->isIntegerType() || UseVarType) {
8383     unsigned NewSize =
8384         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
8385     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
8386                                : Type->hasSignedIntegerRepresentation();
8387     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
8388     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
8389       Diff = SemaRef.PerformImplicitConversion(
8390           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
8391       if (!Diff.isUsable())
8392         return nullptr;
8393     }
8394   }
8395   if (LimitedType) {
8396     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
8397     if (NewSize != C.getTypeSize(Type)) {
8398       if (NewSize < C.getTypeSize(Type)) {
8399         assert(NewSize == 64 && "incorrect loop var size");
8400         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
8401             << InitSrcRange << ConditionSrcRange;
8402       }
8403       QualType NewType = C.getIntTypeForBitwidth(
8404           NewSize, Type->hasSignedIntegerRepresentation() ||
8405                        C.getTypeSize(Type) < NewSize);
8406       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
8407         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
8408                                                  Sema::AA_Converting, true);
8409         if (!Diff.isUsable())
8410           return nullptr;
8411       }
8412     }
8413   }
8414 
8415   return Diff.get();
8416 }
8417 
8418 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
8419     Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
8420   // Do not build for iterators, they cannot be used in non-rectangular loop
8421   // nests.
8422   if (LCDecl->getType()->isRecordType())
8423     return std::make_pair(nullptr, nullptr);
8424   // If we subtract, the min is in the condition, otherwise the min is in the
8425   // init value.
8426   Expr *MinExpr = nullptr;
8427   Expr *MaxExpr = nullptr;
8428   Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
8429   Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
8430   bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue()
8431                                            : CondDependOnLC.hasValue();
8432   bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue()
8433                                            : InitDependOnLC.hasValue();
8434   Expr *Lower =
8435       LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get();
8436   Expr *Upper =
8437       UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get();
8438   if (!Upper || !Lower)
8439     return std::make_pair(nullptr, nullptr);
8440 
8441   if (TestIsLessOp.getValue())
8442     MinExpr = Lower;
8443   else
8444     MaxExpr = Upper;
8445 
8446   // Build minimum/maximum value based on number of iterations.
8447   QualType VarType = LCDecl->getType().getNonReferenceType();
8448 
8449   ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper,
8450                                       Step, VarType, TestIsStrictOp,
8451                                       /*RoundToStep=*/false, Captures);
8452   if (!Diff.isUsable())
8453     return std::make_pair(nullptr, nullptr);
8454 
8455   // ((Upper - Lower [- 1]) / Step) * Step
8456   // Parentheses (for dumping/debugging purposes only).
8457   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
8458   if (!Diff.isUsable())
8459     return std::make_pair(nullptr, nullptr);
8460 
8461   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
8462   if (!NewStep.isUsable())
8463     return std::make_pair(nullptr, nullptr);
8464   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get());
8465   if (!Diff.isUsable())
8466     return std::make_pair(nullptr, nullptr);
8467 
8468   // Parentheses (for dumping/debugging purposes only).
8469   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
8470   if (!Diff.isUsable())
8471     return std::make_pair(nullptr, nullptr);
8472 
8473   // Convert to the ptrdiff_t, if original type is pointer.
8474   if (VarType->isAnyPointerType() &&
8475       !SemaRef.Context.hasSameType(
8476           Diff.get()->getType(),
8477           SemaRef.Context.getUnsignedPointerDiffType())) {
8478     Diff = SemaRef.PerformImplicitConversion(
8479         Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(),
8480         Sema::AA_Converting, /*AllowExplicit=*/true);
8481   }
8482   if (!Diff.isUsable())
8483     return std::make_pair(nullptr, nullptr);
8484 
8485   if (TestIsLessOp.getValue()) {
8486     // MinExpr = Lower;
8487     // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
8488     Diff = SemaRef.BuildBinOp(
8489         S, DefaultLoc, BO_Add,
8490         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(),
8491         Diff.get());
8492     if (!Diff.isUsable())
8493       return std::make_pair(nullptr, nullptr);
8494   } else {
8495     // MaxExpr = Upper;
8496     // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
8497     Diff = SemaRef.BuildBinOp(
8498         S, DefaultLoc, BO_Sub,
8499         SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(),
8500         Diff.get());
8501     if (!Diff.isUsable())
8502       return std::make_pair(nullptr, nullptr);
8503   }
8504 
8505   // Convert to the original type.
8506   if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType))
8507     Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType,
8508                                              Sema::AA_Converting,
8509                                              /*AllowExplicit=*/true);
8510   if (!Diff.isUsable())
8511     return std::make_pair(nullptr, nullptr);
8512 
8513   Sema::TentativeAnalysisScope Trap(SemaRef);
8514   Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false);
8515   if (!Diff.isUsable())
8516     return std::make_pair(nullptr, nullptr);
8517 
8518   if (TestIsLessOp.getValue())
8519     MaxExpr = Diff.get();
8520   else
8521     MinExpr = Diff.get();
8522 
8523   return std::make_pair(MinExpr, MaxExpr);
8524 }
8525 
8526 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
8527   if (InitDependOnLC || CondDependOnLC)
8528     return Condition;
8529   return nullptr;
8530 }
8531 
8532 Expr *OpenMPIterationSpaceChecker::buildPreCond(
8533     Scope *S, Expr *Cond,
8534     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
8535   // Do not build a precondition when the condition/initialization is dependent
8536   // to prevent pessimistic early loop exit.
8537   // TODO: this can be improved by calculating min/max values but not sure that
8538   // it will be very effective.
8539   if (CondDependOnLC || InitDependOnLC)
8540     return SemaRef.PerformImplicitConversion(
8541         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(),
8542         SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
8543         /*AllowExplicit=*/true).get();
8544 
8545   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
8546   Sema::TentativeAnalysisScope Trap(SemaRef);
8547 
8548   ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
8549   ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
8550   if (!NewLB.isUsable() || !NewUB.isUsable())
8551     return nullptr;
8552 
8553   ExprResult CondExpr =
8554       SemaRef.BuildBinOp(S, DefaultLoc,
8555                          TestIsLessOp.getValue() ?
8556                            (TestIsStrictOp ? BO_LT : BO_LE) :
8557                            (TestIsStrictOp ? BO_GT : BO_GE),
8558                          NewLB.get(), NewUB.get());
8559   if (CondExpr.isUsable()) {
8560     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
8561                                                 SemaRef.Context.BoolTy))
8562       CondExpr = SemaRef.PerformImplicitConversion(
8563           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
8564           /*AllowExplicit=*/true);
8565   }
8566 
8567   // Otherwise use original loop condition and evaluate it in runtime.
8568   return CondExpr.isUsable() ? CondExpr.get() : Cond;
8569 }
8570 
8571 /// Build reference expression to the counter be used for codegen.
8572 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
8573     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8574     DSAStackTy &DSA) const {
8575   auto *VD = dyn_cast<VarDecl>(LCDecl);
8576   if (!VD) {
8577     VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
8578     DeclRefExpr *Ref = buildDeclRefExpr(
8579         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
8580     const DSAStackTy::DSAVarData Data =
8581         DSA.getTopDSA(LCDecl, /*FromParent=*/false);
8582     // If the loop control decl is explicitly marked as private, do not mark it
8583     // as captured again.
8584     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
8585       Captures.insert(std::make_pair(LCRef, Ref));
8586     return Ref;
8587   }
8588   return cast<DeclRefExpr>(LCRef);
8589 }
8590 
8591 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
8592   if (LCDecl && !LCDecl->isInvalidDecl()) {
8593     QualType Type = LCDecl->getType().getNonReferenceType();
8594     VarDecl *PrivateVar = buildVarDecl(
8595         SemaRef, DefaultLoc, Type, LCDecl->getName(),
8596         LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
8597         isa<VarDecl>(LCDecl)
8598             ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
8599             : nullptr);
8600     if (PrivateVar->isInvalidDecl())
8601       return nullptr;
8602     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
8603   }
8604   return nullptr;
8605 }
8606 
8607 /// Build initialization of the counter to be used for codegen.
8608 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
8609 
8610 /// Build step of the counter be used for codegen.
8611 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
8612 
8613 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
8614     Scope *S, Expr *Counter,
8615     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
8616     Expr *Inc, OverloadedOperatorKind OOK) {
8617   Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
8618   if (!Cnt)
8619     return nullptr;
8620   if (Inc) {
8621     assert((OOK == OO_Plus || OOK == OO_Minus) &&
8622            "Expected only + or - operations for depend clauses.");
8623     BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
8624     Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
8625     if (!Cnt)
8626       return nullptr;
8627   }
8628   QualType VarType = LCDecl->getType().getNonReferenceType();
8629   if (!VarType->isIntegerType() && !VarType->isPointerType() &&
8630       !SemaRef.getLangOpts().CPlusPlus)
8631     return nullptr;
8632   // Upper - Lower
8633   Expr *Upper = TestIsLessOp.getValue()
8634                     ? Cnt
8635                     : tryBuildCapture(SemaRef, LB, Captures).get();
8636   Expr *Lower = TestIsLessOp.getValue()
8637                     ? tryBuildCapture(SemaRef, LB, Captures).get()
8638                     : Cnt;
8639   if (!Upper || !Lower)
8640     return nullptr;
8641 
8642   ExprResult Diff = calculateNumIters(
8643       SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType,
8644       /*TestIsStrictOp=*/false, /*RoundToStep=*/false, Captures);
8645   if (!Diff.isUsable())
8646     return nullptr;
8647 
8648   return Diff.get();
8649 }
8650 } // namespace
8651 
8652 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
8653   assert(getLangOpts().OpenMP && "OpenMP is not active.");
8654   assert(Init && "Expected loop in canonical form.");
8655   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
8656   if (AssociatedLoops > 0 &&
8657       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
8658     DSAStack->loopStart();
8659     OpenMPIterationSpaceChecker ISC(*this, /*SupportsNonRectangular=*/true,
8660                                     *DSAStack, ForLoc);
8661     if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
8662       if (ValueDecl *D = ISC.getLoopDecl()) {
8663         auto *VD = dyn_cast<VarDecl>(D);
8664         DeclRefExpr *PrivateRef = nullptr;
8665         if (!VD) {
8666           if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
8667             VD = Private;
8668           } else {
8669             PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
8670                                       /*WithInit=*/false);
8671             VD = cast<VarDecl>(PrivateRef->getDecl());
8672           }
8673         }
8674         DSAStack->addLoopControlVariable(D, VD);
8675         const Decl *LD = DSAStack->getPossiblyLoopCunter();
8676         if (LD != D->getCanonicalDecl()) {
8677           DSAStack->resetPossibleLoopCounter();
8678           if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
8679             MarkDeclarationsReferencedInExpr(
8680                 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
8681                                  Var->getType().getNonLValueExprType(Context),
8682                                  ForLoc, /*RefersToCapture=*/true));
8683         }
8684         OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8685         // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
8686         // Referenced in a Construct, C/C++]. The loop iteration variable in the
8687         // associated for-loop of a simd construct with just one associated
8688         // for-loop may be listed in a linear clause with a constant-linear-step
8689         // that is the increment of the associated for-loop. The loop iteration
8690         // variable(s) in the associated for-loop(s) of a for or parallel for
8691         // construct may be listed in a private or lastprivate clause.
8692         DSAStackTy::DSAVarData DVar =
8693             DSAStack->getTopDSA(D, /*FromParent=*/false);
8694         // If LoopVarRefExpr is nullptr it means the corresponding loop variable
8695         // is declared in the loop and it is predetermined as a private.
8696         Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
8697         OpenMPClauseKind PredeterminedCKind =
8698             isOpenMPSimdDirective(DKind)
8699                 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
8700                 : OMPC_private;
8701         if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
8702               DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
8703               (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate &&
8704                                          DVar.CKind != OMPC_private))) ||
8705              ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
8706                DKind == OMPD_master_taskloop ||
8707                DKind == OMPD_parallel_master_taskloop ||
8708                isOpenMPDistributeDirective(DKind)) &&
8709               !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
8710               DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
8711             (DVar.CKind != OMPC_private || DVar.RefExpr)) {
8712           Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
8713               << getOpenMPClauseName(DVar.CKind)
8714               << getOpenMPDirectiveName(DKind)
8715               << getOpenMPClauseName(PredeterminedCKind);
8716           if (DVar.RefExpr == nullptr)
8717             DVar.CKind = PredeterminedCKind;
8718           reportOriginalDsa(*this, DSAStack, D, DVar,
8719                             /*IsLoopIterVar=*/true);
8720         } else if (LoopDeclRefExpr) {
8721           // Make the loop iteration variable private (for worksharing
8722           // constructs), linear (for simd directives with the only one
8723           // associated loop) or lastprivate (for simd directives with several
8724           // collapsed or ordered loops).
8725           if (DVar.CKind == OMPC_unknown)
8726             DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind,
8727                              PrivateRef);
8728         }
8729       }
8730     }
8731     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
8732   }
8733 }
8734 
8735 /// Called on a for stmt to check and extract its iteration space
8736 /// for further processing (such as collapsing).
8737 static bool checkOpenMPIterationSpace(
8738     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
8739     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
8740     unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
8741     Expr *OrderedLoopCountExpr,
8742     Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
8743     llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
8744     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
8745   bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind);
8746   // OpenMP [2.9.1, Canonical Loop Form]
8747   //   for (init-expr; test-expr; incr-expr) structured-block
8748   //   for (range-decl: range-expr) structured-block
8749   if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(S))
8750     S = CanonLoop->getLoopStmt();
8751   auto *For = dyn_cast_or_null<ForStmt>(S);
8752   auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S);
8753   // Ranged for is supported only in OpenMP 5.0.
8754   if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) {
8755     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
8756         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
8757         << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
8758         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
8759     if (TotalNestedLoopCount > 1) {
8760       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
8761         SemaRef.Diag(DSA.getConstructLoc(),
8762                      diag::note_omp_collapse_ordered_expr)
8763             << 2 << CollapseLoopCountExpr->getSourceRange()
8764             << OrderedLoopCountExpr->getSourceRange();
8765       else if (CollapseLoopCountExpr)
8766         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
8767                      diag::note_omp_collapse_ordered_expr)
8768             << 0 << CollapseLoopCountExpr->getSourceRange();
8769       else
8770         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
8771                      diag::note_omp_collapse_ordered_expr)
8772             << 1 << OrderedLoopCountExpr->getSourceRange();
8773     }
8774     return true;
8775   }
8776   assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) &&
8777          "No loop body.");
8778 
8779   OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA,
8780                                   For ? For->getForLoc() : CXXFor->getForLoc());
8781 
8782   // Check init.
8783   Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt();
8784   if (ISC.checkAndSetInit(Init))
8785     return true;
8786 
8787   bool HasErrors = false;
8788 
8789   // Check loop variable's type.
8790   if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
8791     // OpenMP [2.6, Canonical Loop Form]
8792     // Var is one of the following:
8793     //   A variable of signed or unsigned integer type.
8794     //   For C++, a variable of a random access iterator type.
8795     //   For C, a variable of a pointer type.
8796     QualType VarType = LCDecl->getType().getNonReferenceType();
8797     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
8798         !VarType->isPointerType() &&
8799         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
8800       SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
8801           << SemaRef.getLangOpts().CPlusPlus;
8802       HasErrors = true;
8803     }
8804 
8805     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
8806     // a Construct
8807     // The loop iteration variable(s) in the associated for-loop(s) of a for or
8808     // parallel for construct is (are) private.
8809     // The loop iteration variable in the associated for-loop of a simd
8810     // construct with just one associated for-loop is linear with a
8811     // constant-linear-step that is the increment of the associated for-loop.
8812     // Exclude loop var from the list of variables with implicitly defined data
8813     // sharing attributes.
8814     VarsWithImplicitDSA.erase(LCDecl);
8815 
8816     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
8817 
8818     // Check test-expr.
8819     HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond());
8820 
8821     // Check incr-expr.
8822     HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc());
8823   }
8824 
8825   if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
8826     return HasErrors;
8827 
8828   // Build the loop's iteration space representation.
8829   ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond(
8830       DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures);
8831   ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
8832       ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces,
8833                              (isOpenMPWorksharingDirective(DKind) ||
8834                               isOpenMPGenericLoopDirective(DKind) ||
8835                               isOpenMPTaskLoopDirective(DKind) ||
8836                               isOpenMPDistributeDirective(DKind) ||
8837                               isOpenMPLoopTransformationDirective(DKind)),
8838                              Captures);
8839   ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
8840       ISC.buildCounterVar(Captures, DSA);
8841   ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
8842       ISC.buildPrivateCounterVar();
8843   ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
8844   ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
8845   ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
8846   ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
8847       ISC.getConditionSrcRange();
8848   ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
8849       ISC.getIncrementSrcRange();
8850   ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
8851   ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
8852       ISC.isStrictTestOp();
8853   std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue,
8854            ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
8855       ISC.buildMinMaxValues(DSA.getCurScope(), Captures);
8856   ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
8857       ISC.buildFinalCondition(DSA.getCurScope());
8858   ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
8859       ISC.doesInitDependOnLC();
8860   ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
8861       ISC.doesCondDependOnLC();
8862   ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
8863       ISC.getLoopDependentIdx();
8864 
8865   HasErrors |=
8866       (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
8867        ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
8868        ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
8869        ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
8870        ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
8871        ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
8872   if (!HasErrors && DSA.isOrderedRegion()) {
8873     if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
8874       if (CurrentNestedLoopCount <
8875           DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
8876         DSA.getOrderedRegionParam().second->setLoopNumIterations(
8877             CurrentNestedLoopCount,
8878             ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
8879         DSA.getOrderedRegionParam().second->setLoopCounter(
8880             CurrentNestedLoopCount,
8881             ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
8882       }
8883     }
8884     for (auto &Pair : DSA.getDoacrossDependClauses()) {
8885       if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
8886         // Erroneous case - clause has some problems.
8887         continue;
8888       }
8889       if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
8890           Pair.second.size() <= CurrentNestedLoopCount) {
8891         // Erroneous case - clause has some problems.
8892         Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
8893         continue;
8894       }
8895       Expr *CntValue;
8896       if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
8897         CntValue = ISC.buildOrderedLoopData(
8898             DSA.getCurScope(),
8899             ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
8900             Pair.first->getDependencyLoc());
8901       else
8902         CntValue = ISC.buildOrderedLoopData(
8903             DSA.getCurScope(),
8904             ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
8905             Pair.first->getDependencyLoc(),
8906             Pair.second[CurrentNestedLoopCount].first,
8907             Pair.second[CurrentNestedLoopCount].second);
8908       Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
8909     }
8910   }
8911 
8912   return HasErrors;
8913 }
8914 
8915 /// Build 'VarRef = Start.
8916 static ExprResult
8917 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
8918                  ExprResult Start, bool IsNonRectangularLB,
8919                  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
8920   // Build 'VarRef = Start.
8921   ExprResult NewStart = IsNonRectangularLB
8922                             ? Start.get()
8923                             : tryBuildCapture(SemaRef, Start.get(), Captures);
8924   if (!NewStart.isUsable())
8925     return ExprError();
8926   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
8927                                    VarRef.get()->getType())) {
8928     NewStart = SemaRef.PerformImplicitConversion(
8929         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
8930         /*AllowExplicit=*/true);
8931     if (!NewStart.isUsable())
8932       return ExprError();
8933   }
8934 
8935   ExprResult Init =
8936       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
8937   return Init;
8938 }
8939 
8940 /// Build 'VarRef = Start + Iter * Step'.
8941 static ExprResult buildCounterUpdate(
8942     Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
8943     ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
8944     bool IsNonRectangularLB,
8945     llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
8946   // Add parentheses (for debugging purposes only).
8947   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
8948   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
8949       !Step.isUsable())
8950     return ExprError();
8951 
8952   ExprResult NewStep = Step;
8953   if (Captures)
8954     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
8955   if (NewStep.isInvalid())
8956     return ExprError();
8957   ExprResult Update =
8958       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
8959   if (!Update.isUsable())
8960     return ExprError();
8961 
8962   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
8963   // 'VarRef = Start (+|-) Iter * Step'.
8964   if (!Start.isUsable())
8965     return ExprError();
8966   ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get());
8967   if (!NewStart.isUsable())
8968     return ExprError();
8969   if (Captures && !IsNonRectangularLB)
8970     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
8971   if (NewStart.isInvalid())
8972     return ExprError();
8973 
8974   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
8975   ExprResult SavedUpdate = Update;
8976   ExprResult UpdateVal;
8977   if (VarRef.get()->getType()->isOverloadableType() ||
8978       NewStart.get()->getType()->isOverloadableType() ||
8979       Update.get()->getType()->isOverloadableType()) {
8980     Sema::TentativeAnalysisScope Trap(SemaRef);
8981 
8982     Update =
8983         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
8984     if (Update.isUsable()) {
8985       UpdateVal =
8986           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
8987                              VarRef.get(), SavedUpdate.get());
8988       if (UpdateVal.isUsable()) {
8989         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
8990                                             UpdateVal.get());
8991       }
8992     }
8993   }
8994 
8995   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
8996   if (!Update.isUsable() || !UpdateVal.isUsable()) {
8997     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
8998                                 NewStart.get(), SavedUpdate.get());
8999     if (!Update.isUsable())
9000       return ExprError();
9001 
9002     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
9003                                      VarRef.get()->getType())) {
9004       Update = SemaRef.PerformImplicitConversion(
9005           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
9006       if (!Update.isUsable())
9007         return ExprError();
9008     }
9009 
9010     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
9011   }
9012   return Update;
9013 }
9014 
9015 /// Convert integer expression \a E to make it have at least \a Bits
9016 /// bits.
9017 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
9018   if (E == nullptr)
9019     return ExprError();
9020   ASTContext &C = SemaRef.Context;
9021   QualType OldType = E->getType();
9022   unsigned HasBits = C.getTypeSize(OldType);
9023   if (HasBits >= Bits)
9024     return ExprResult(E);
9025   // OK to convert to signed, because new type has more bits than old.
9026   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
9027   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
9028                                            true);
9029 }
9030 
9031 /// Check if the given expression \a E is a constant integer that fits
9032 /// into \a Bits bits.
9033 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
9034   if (E == nullptr)
9035     return false;
9036   if (Optional<llvm::APSInt> Result =
9037           E->getIntegerConstantExpr(SemaRef.Context))
9038     return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits);
9039   return false;
9040 }
9041 
9042 /// Build preinits statement for the given declarations.
9043 static Stmt *buildPreInits(ASTContext &Context,
9044                            MutableArrayRef<Decl *> PreInits) {
9045   if (!PreInits.empty()) {
9046     return new (Context) DeclStmt(
9047         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
9048         SourceLocation(), SourceLocation());
9049   }
9050   return nullptr;
9051 }
9052 
9053 /// Build preinits statement for the given declarations.
9054 static Stmt *
9055 buildPreInits(ASTContext &Context,
9056               const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
9057   if (!Captures.empty()) {
9058     SmallVector<Decl *, 16> PreInits;
9059     for (const auto &Pair : Captures)
9060       PreInits.push_back(Pair.second->getDecl());
9061     return buildPreInits(Context, PreInits);
9062   }
9063   return nullptr;
9064 }
9065 
9066 /// Build postupdate expression for the given list of postupdates expressions.
9067 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
9068   Expr *PostUpdate = nullptr;
9069   if (!PostUpdates.empty()) {
9070     for (Expr *E : PostUpdates) {
9071       Expr *ConvE = S.BuildCStyleCastExpr(
9072                          E->getExprLoc(),
9073                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
9074                          E->getExprLoc(), E)
9075                         .get();
9076       PostUpdate = PostUpdate
9077                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
9078                                               PostUpdate, ConvE)
9079                              .get()
9080                        : ConvE;
9081     }
9082   }
9083   return PostUpdate;
9084 }
9085 
9086 /// Called on a for stmt to check itself and nested loops (if any).
9087 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
9088 /// number of collapsed loops otherwise.
9089 static unsigned
9090 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
9091                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
9092                 DSAStackTy &DSA,
9093                 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
9094                 OMPLoopBasedDirective::HelperExprs &Built) {
9095   unsigned NestedLoopCount = 1;
9096   bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) &&
9097                                     !isOpenMPLoopTransformationDirective(DKind);
9098 
9099   if (CollapseLoopCountExpr) {
9100     // Found 'collapse' clause - calculate collapse number.
9101     Expr::EvalResult Result;
9102     if (!CollapseLoopCountExpr->isValueDependent() &&
9103         CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
9104       NestedLoopCount = Result.Val.getInt().getLimitedValue();
9105     } else {
9106       Built.clear(/*Size=*/1);
9107       return 1;
9108     }
9109   }
9110   unsigned OrderedLoopCount = 1;
9111   if (OrderedLoopCountExpr) {
9112     // Found 'ordered' clause - calculate collapse number.
9113     Expr::EvalResult EVResult;
9114     if (!OrderedLoopCountExpr->isValueDependent() &&
9115         OrderedLoopCountExpr->EvaluateAsInt(EVResult,
9116                                             SemaRef.getASTContext())) {
9117       llvm::APSInt Result = EVResult.Val.getInt();
9118       if (Result.getLimitedValue() < NestedLoopCount) {
9119         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
9120                      diag::err_omp_wrong_ordered_loop_count)
9121             << OrderedLoopCountExpr->getSourceRange();
9122         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
9123                      diag::note_collapse_loop_count)
9124             << CollapseLoopCountExpr->getSourceRange();
9125       }
9126       OrderedLoopCount = Result.getLimitedValue();
9127     } else {
9128       Built.clear(/*Size=*/1);
9129       return 1;
9130     }
9131   }
9132   // This is helper routine for loop directives (e.g., 'for', 'simd',
9133   // 'for simd', etc.).
9134   llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
9135   unsigned NumLoops = std::max(OrderedLoopCount, NestedLoopCount);
9136   SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops);
9137   if (!OMPLoopBasedDirective::doForAllLoops(
9138           AStmt->IgnoreContainers(!isOpenMPLoopTransformationDirective(DKind)),
9139           SupportsNonPerfectlyNested, NumLoops,
9140           [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount,
9141            CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA,
9142            &IterSpaces, &Captures](unsigned Cnt, Stmt *CurStmt) {
9143             if (checkOpenMPIterationSpace(
9144                     DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
9145                     NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr,
9146                     VarsWithImplicitDSA, IterSpaces, Captures))
9147               return true;
9148             if (Cnt > 0 && Cnt >= NestedLoopCount &&
9149                 IterSpaces[Cnt].CounterVar) {
9150               // Handle initialization of captured loop iterator variables.
9151               auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
9152               if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
9153                 Captures[DRE] = DRE;
9154               }
9155             }
9156             return false;
9157           },
9158           [&SemaRef, &Captures](OMPLoopTransformationDirective *Transform) {
9159             Stmt *DependentPreInits = Transform->getPreInits();
9160             if (!DependentPreInits)
9161               return;
9162             for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) {
9163               auto *D = cast<VarDecl>(C);
9164               DeclRefExpr *Ref = buildDeclRefExpr(SemaRef, D, D->getType(),
9165                                                   Transform->getBeginLoc());
9166               Captures[Ref] = Ref;
9167             }
9168           }))
9169     return 0;
9170 
9171   Built.clear(/* size */ NestedLoopCount);
9172 
9173   if (SemaRef.CurContext->isDependentContext())
9174     return NestedLoopCount;
9175 
9176   // An example of what is generated for the following code:
9177   //
9178   //   #pragma omp simd collapse(2) ordered(2)
9179   //   for (i = 0; i < NI; ++i)
9180   //     for (k = 0; k < NK; ++k)
9181   //       for (j = J0; j < NJ; j+=2) {
9182   //         <loop body>
9183   //       }
9184   //
9185   // We generate the code below.
9186   // Note: the loop body may be outlined in CodeGen.
9187   // Note: some counters may be C++ classes, operator- is used to find number of
9188   // iterations and operator+= to calculate counter value.
9189   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
9190   // or i64 is currently supported).
9191   //
9192   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
9193   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
9194   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
9195   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
9196   //     // similar updates for vars in clauses (e.g. 'linear')
9197   //     <loop body (using local i and j)>
9198   //   }
9199   //   i = NI; // assign final values of counters
9200   //   j = NJ;
9201   //
9202 
9203   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
9204   // the iteration counts of the collapsed for loops.
9205   // Precondition tests if there is at least one iteration (all conditions are
9206   // true).
9207   auto PreCond = ExprResult(IterSpaces[0].PreCond);
9208   Expr *N0 = IterSpaces[0].NumIterations;
9209   ExprResult LastIteration32 =
9210       widenIterationCount(/*Bits=*/32,
9211                           SemaRef
9212                               .PerformImplicitConversion(
9213                                   N0->IgnoreImpCasts(), N0->getType(),
9214                                   Sema::AA_Converting, /*AllowExplicit=*/true)
9215                               .get(),
9216                           SemaRef);
9217   ExprResult LastIteration64 = widenIterationCount(
9218       /*Bits=*/64,
9219       SemaRef
9220           .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
9221                                      Sema::AA_Converting,
9222                                      /*AllowExplicit=*/true)
9223           .get(),
9224       SemaRef);
9225 
9226   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
9227     return NestedLoopCount;
9228 
9229   ASTContext &C = SemaRef.Context;
9230   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
9231 
9232   Scope *CurScope = DSA.getCurScope();
9233   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
9234     if (PreCond.isUsable()) {
9235       PreCond =
9236           SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
9237                              PreCond.get(), IterSpaces[Cnt].PreCond);
9238     }
9239     Expr *N = IterSpaces[Cnt].NumIterations;
9240     SourceLocation Loc = N->getExprLoc();
9241     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
9242     if (LastIteration32.isUsable())
9243       LastIteration32 = SemaRef.BuildBinOp(
9244           CurScope, Loc, BO_Mul, LastIteration32.get(),
9245           SemaRef
9246               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
9247                                          Sema::AA_Converting,
9248                                          /*AllowExplicit=*/true)
9249               .get());
9250     if (LastIteration64.isUsable())
9251       LastIteration64 = SemaRef.BuildBinOp(
9252           CurScope, Loc, BO_Mul, LastIteration64.get(),
9253           SemaRef
9254               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
9255                                          Sema::AA_Converting,
9256                                          /*AllowExplicit=*/true)
9257               .get());
9258   }
9259 
9260   // Choose either the 32-bit or 64-bit version.
9261   ExprResult LastIteration = LastIteration64;
9262   if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
9263       (LastIteration32.isUsable() &&
9264        C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
9265        (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
9266         fitsInto(
9267             /*Bits=*/32,
9268             LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
9269             LastIteration64.get(), SemaRef))))
9270     LastIteration = LastIteration32;
9271   QualType VType = LastIteration.get()->getType();
9272   QualType RealVType = VType;
9273   QualType StrideVType = VType;
9274   if (isOpenMPTaskLoopDirective(DKind)) {
9275     VType =
9276         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
9277     StrideVType =
9278         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
9279   }
9280 
9281   if (!LastIteration.isUsable())
9282     return 0;
9283 
9284   // Save the number of iterations.
9285   ExprResult NumIterations = LastIteration;
9286   {
9287     LastIteration = SemaRef.BuildBinOp(
9288         CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
9289         LastIteration.get(),
9290         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
9291     if (!LastIteration.isUsable())
9292       return 0;
9293   }
9294 
9295   // Calculate the last iteration number beforehand instead of doing this on
9296   // each iteration. Do not do this if the number of iterations may be kfold-ed.
9297   bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context);
9298   ExprResult CalcLastIteration;
9299   if (!IsConstant) {
9300     ExprResult SaveRef =
9301         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
9302     LastIteration = SaveRef;
9303 
9304     // Prepare SaveRef + 1.
9305     NumIterations = SemaRef.BuildBinOp(
9306         CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
9307         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
9308     if (!NumIterations.isUsable())
9309       return 0;
9310   }
9311 
9312   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
9313 
9314   // Build variables passed into runtime, necessary for worksharing directives.
9315   ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
9316   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
9317       isOpenMPDistributeDirective(DKind) ||
9318       isOpenMPGenericLoopDirective(DKind) ||
9319       isOpenMPLoopTransformationDirective(DKind)) {
9320     // Lower bound variable, initialized with zero.
9321     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
9322     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
9323     SemaRef.AddInitializerToDecl(LBDecl,
9324                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
9325                                  /*DirectInit*/ false);
9326 
9327     // Upper bound variable, initialized with last iteration number.
9328     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
9329     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
9330     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
9331                                  /*DirectInit*/ false);
9332 
9333     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
9334     // This will be used to implement clause 'lastprivate'.
9335     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
9336     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
9337     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
9338     SemaRef.AddInitializerToDecl(ILDecl,
9339                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
9340                                  /*DirectInit*/ false);
9341 
9342     // Stride variable returned by runtime (we initialize it to 1 by default).
9343     VarDecl *STDecl =
9344         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
9345     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
9346     SemaRef.AddInitializerToDecl(STDecl,
9347                                  SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
9348                                  /*DirectInit*/ false);
9349 
9350     // Build expression: UB = min(UB, LastIteration)
9351     // It is necessary for CodeGen of directives with static scheduling.
9352     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
9353                                                 UB.get(), LastIteration.get());
9354     ExprResult CondOp = SemaRef.ActOnConditionalOp(
9355         LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
9356         LastIteration.get(), UB.get());
9357     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
9358                              CondOp.get());
9359     EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
9360 
9361     // If we have a combined directive that combines 'distribute', 'for' or
9362     // 'simd' we need to be able to access the bounds of the schedule of the
9363     // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
9364     // by scheduling 'distribute' have to be passed to the schedule of 'for'.
9365     if (isOpenMPLoopBoundSharingDirective(DKind)) {
9366       // Lower bound variable, initialized with zero.
9367       VarDecl *CombLBDecl =
9368           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
9369       CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
9370       SemaRef.AddInitializerToDecl(
9371           CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
9372           /*DirectInit*/ false);
9373 
9374       // Upper bound variable, initialized with last iteration number.
9375       VarDecl *CombUBDecl =
9376           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
9377       CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
9378       SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
9379                                    /*DirectInit*/ false);
9380 
9381       ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
9382           CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
9383       ExprResult CombCondOp =
9384           SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
9385                                      LastIteration.get(), CombUB.get());
9386       CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
9387                                    CombCondOp.get());
9388       CombEUB =
9389           SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
9390 
9391       const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
9392       // We expect to have at least 2 more parameters than the 'parallel'
9393       // directive does - the lower and upper bounds of the previous schedule.
9394       assert(CD->getNumParams() >= 4 &&
9395              "Unexpected number of parameters in loop combined directive");
9396 
9397       // Set the proper type for the bounds given what we learned from the
9398       // enclosed loops.
9399       ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
9400       ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
9401 
9402       // Previous lower and upper bounds are obtained from the region
9403       // parameters.
9404       PrevLB =
9405           buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
9406       PrevUB =
9407           buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
9408     }
9409   }
9410 
9411   // Build the iteration variable and its initialization before loop.
9412   ExprResult IV;
9413   ExprResult Init, CombInit;
9414   {
9415     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
9416     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
9417     Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
9418                  isOpenMPGenericLoopDirective(DKind) ||
9419                  isOpenMPTaskLoopDirective(DKind) ||
9420                  isOpenMPDistributeDirective(DKind) ||
9421                  isOpenMPLoopTransformationDirective(DKind))
9422                     ? LB.get()
9423                     : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
9424     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
9425     Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
9426 
9427     if (isOpenMPLoopBoundSharingDirective(DKind)) {
9428       Expr *CombRHS =
9429           (isOpenMPWorksharingDirective(DKind) ||
9430            isOpenMPGenericLoopDirective(DKind) ||
9431            isOpenMPTaskLoopDirective(DKind) ||
9432            isOpenMPDistributeDirective(DKind))
9433               ? CombLB.get()
9434               : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
9435       CombInit =
9436           SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
9437       CombInit =
9438           SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
9439     }
9440   }
9441 
9442   bool UseStrictCompare =
9443       RealVType->hasUnsignedIntegerRepresentation() &&
9444       llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
9445         return LIS.IsStrictCompare;
9446       });
9447   // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
9448   // unsigned IV)) for worksharing loops.
9449   SourceLocation CondLoc = AStmt->getBeginLoc();
9450   Expr *BoundUB = UB.get();
9451   if (UseStrictCompare) {
9452     BoundUB =
9453         SemaRef
9454             .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
9455                         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
9456             .get();
9457     BoundUB =
9458         SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
9459   }
9460   ExprResult Cond =
9461       (isOpenMPWorksharingDirective(DKind) ||
9462        isOpenMPGenericLoopDirective(DKind) ||
9463        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) ||
9464        isOpenMPLoopTransformationDirective(DKind))
9465           ? SemaRef.BuildBinOp(CurScope, CondLoc,
9466                                UseStrictCompare ? BO_LT : BO_LE, IV.get(),
9467                                BoundUB)
9468           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
9469                                NumIterations.get());
9470   ExprResult CombDistCond;
9471   if (isOpenMPLoopBoundSharingDirective(DKind)) {
9472     CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
9473                                       NumIterations.get());
9474   }
9475 
9476   ExprResult CombCond;
9477   if (isOpenMPLoopBoundSharingDirective(DKind)) {
9478     Expr *BoundCombUB = CombUB.get();
9479     if (UseStrictCompare) {
9480       BoundCombUB =
9481           SemaRef
9482               .BuildBinOp(
9483                   CurScope, CondLoc, BO_Add, BoundCombUB,
9484                   SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
9485               .get();
9486       BoundCombUB =
9487           SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
9488               .get();
9489     }
9490     CombCond =
9491         SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
9492                            IV.get(), BoundCombUB);
9493   }
9494   // Loop increment (IV = IV + 1)
9495   SourceLocation IncLoc = AStmt->getBeginLoc();
9496   ExprResult Inc =
9497       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
9498                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
9499   if (!Inc.isUsable())
9500     return 0;
9501   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
9502   Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
9503   if (!Inc.isUsable())
9504     return 0;
9505 
9506   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
9507   // Used for directives with static scheduling.
9508   // In combined construct, add combined version that use CombLB and CombUB
9509   // base variables for the update
9510   ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
9511   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
9512       isOpenMPGenericLoopDirective(DKind) ||
9513       isOpenMPDistributeDirective(DKind) ||
9514       isOpenMPLoopTransformationDirective(DKind)) {
9515     // LB + ST
9516     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
9517     if (!NextLB.isUsable())
9518       return 0;
9519     // LB = LB + ST
9520     NextLB =
9521         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
9522     NextLB =
9523         SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
9524     if (!NextLB.isUsable())
9525       return 0;
9526     // UB + ST
9527     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
9528     if (!NextUB.isUsable())
9529       return 0;
9530     // UB = UB + ST
9531     NextUB =
9532         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
9533     NextUB =
9534         SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
9535     if (!NextUB.isUsable())
9536       return 0;
9537     if (isOpenMPLoopBoundSharingDirective(DKind)) {
9538       CombNextLB =
9539           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
9540       if (!NextLB.isUsable())
9541         return 0;
9542       // LB = LB + ST
9543       CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
9544                                       CombNextLB.get());
9545       CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
9546                                                /*DiscardedValue*/ false);
9547       if (!CombNextLB.isUsable())
9548         return 0;
9549       // UB + ST
9550       CombNextUB =
9551           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
9552       if (!CombNextUB.isUsable())
9553         return 0;
9554       // UB = UB + ST
9555       CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
9556                                       CombNextUB.get());
9557       CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
9558                                                /*DiscardedValue*/ false);
9559       if (!CombNextUB.isUsable())
9560         return 0;
9561     }
9562   }
9563 
9564   // Create increment expression for distribute loop when combined in a same
9565   // directive with for as IV = IV + ST; ensure upper bound expression based
9566   // on PrevUB instead of NumIterations - used to implement 'for' when found
9567   // in combination with 'distribute', like in 'distribute parallel for'
9568   SourceLocation DistIncLoc = AStmt->getBeginLoc();
9569   ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
9570   if (isOpenMPLoopBoundSharingDirective(DKind)) {
9571     DistCond = SemaRef.BuildBinOp(
9572         CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
9573     assert(DistCond.isUsable() && "distribute cond expr was not built");
9574 
9575     DistInc =
9576         SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
9577     assert(DistInc.isUsable() && "distribute inc expr was not built");
9578     DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
9579                                  DistInc.get());
9580     DistInc =
9581         SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
9582     assert(DistInc.isUsable() && "distribute inc expr was not built");
9583 
9584     // Build expression: UB = min(UB, prevUB) for #for in composite or combined
9585     // construct
9586     ExprResult NewPrevUB = PrevUB;
9587     SourceLocation DistEUBLoc = AStmt->getBeginLoc();
9588     if (!SemaRef.Context.hasSameType(UB.get()->getType(),
9589                                      PrevUB.get()->getType())) {
9590       NewPrevUB = SemaRef.BuildCStyleCastExpr(
9591           DistEUBLoc,
9592           SemaRef.Context.getTrivialTypeSourceInfo(UB.get()->getType()),
9593           DistEUBLoc, NewPrevUB.get());
9594       if (!NewPrevUB.isUsable())
9595         return 0;
9596     }
9597     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT,
9598                                                 UB.get(), NewPrevUB.get());
9599     ExprResult CondOp = SemaRef.ActOnConditionalOp(
9600         DistEUBLoc, DistEUBLoc, IsUBGreater.get(), NewPrevUB.get(), UB.get());
9601     PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
9602                                  CondOp.get());
9603     PrevEUB =
9604         SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
9605 
9606     // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
9607     // parallel for is in combination with a distribute directive with
9608     // schedule(static, 1)
9609     Expr *BoundPrevUB = PrevUB.get();
9610     if (UseStrictCompare) {
9611       BoundPrevUB =
9612           SemaRef
9613               .BuildBinOp(
9614                   CurScope, CondLoc, BO_Add, BoundPrevUB,
9615                   SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
9616               .get();
9617       BoundPrevUB =
9618           SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
9619               .get();
9620     }
9621     ParForInDistCond =
9622         SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
9623                            IV.get(), BoundPrevUB);
9624   }
9625 
9626   // Build updates and final values of the loop counters.
9627   bool HasErrors = false;
9628   Built.Counters.resize(NestedLoopCount);
9629   Built.Inits.resize(NestedLoopCount);
9630   Built.Updates.resize(NestedLoopCount);
9631   Built.Finals.resize(NestedLoopCount);
9632   Built.DependentCounters.resize(NestedLoopCount);
9633   Built.DependentInits.resize(NestedLoopCount);
9634   Built.FinalsConditions.resize(NestedLoopCount);
9635   {
9636     // We implement the following algorithm for obtaining the
9637     // original loop iteration variable values based on the
9638     // value of the collapsed loop iteration variable IV.
9639     //
9640     // Let n+1 be the number of collapsed loops in the nest.
9641     // Iteration variables (I0, I1, .... In)
9642     // Iteration counts (N0, N1, ... Nn)
9643     //
9644     // Acc = IV;
9645     //
9646     // To compute Ik for loop k, 0 <= k <= n, generate:
9647     //    Prod = N(k+1) * N(k+2) * ... * Nn;
9648     //    Ik = Acc / Prod;
9649     //    Acc -= Ik * Prod;
9650     //
9651     ExprResult Acc = IV;
9652     for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
9653       LoopIterationSpace &IS = IterSpaces[Cnt];
9654       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
9655       ExprResult Iter;
9656 
9657       // Compute prod
9658       ExprResult Prod =
9659           SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
9660       for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
9661         Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
9662                                   IterSpaces[K].NumIterations);
9663 
9664       // Iter = Acc / Prod
9665       // If there is at least one more inner loop to avoid
9666       // multiplication by 1.
9667       if (Cnt + 1 < NestedLoopCount)
9668         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
9669                                   Acc.get(), Prod.get());
9670       else
9671         Iter = Acc;
9672       if (!Iter.isUsable()) {
9673         HasErrors = true;
9674         break;
9675       }
9676 
9677       // Update Acc:
9678       // Acc -= Iter * Prod
9679       // Check if there is at least one more inner loop to avoid
9680       // multiplication by 1.
9681       if (Cnt + 1 < NestedLoopCount)
9682         Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
9683                                   Iter.get(), Prod.get());
9684       else
9685         Prod = Iter;
9686       Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
9687                                Acc.get(), Prod.get());
9688 
9689       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
9690       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
9691       DeclRefExpr *CounterVar = buildDeclRefExpr(
9692           SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
9693           /*RefersToCapture=*/true);
9694       ExprResult Init =
9695           buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
9696                            IS.CounterInit, IS.IsNonRectangularLB, Captures);
9697       if (!Init.isUsable()) {
9698         HasErrors = true;
9699         break;
9700       }
9701       ExprResult Update = buildCounterUpdate(
9702           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
9703           IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures);
9704       if (!Update.isUsable()) {
9705         HasErrors = true;
9706         break;
9707       }
9708 
9709       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
9710       ExprResult Final =
9711           buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
9712                              IS.CounterInit, IS.NumIterations, IS.CounterStep,
9713                              IS.Subtract, IS.IsNonRectangularLB, &Captures);
9714       if (!Final.isUsable()) {
9715         HasErrors = true;
9716         break;
9717       }
9718 
9719       if (!Update.isUsable() || !Final.isUsable()) {
9720         HasErrors = true;
9721         break;
9722       }
9723       // Save results
9724       Built.Counters[Cnt] = IS.CounterVar;
9725       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
9726       Built.Inits[Cnt] = Init.get();
9727       Built.Updates[Cnt] = Update.get();
9728       Built.Finals[Cnt] = Final.get();
9729       Built.DependentCounters[Cnt] = nullptr;
9730       Built.DependentInits[Cnt] = nullptr;
9731       Built.FinalsConditions[Cnt] = nullptr;
9732       if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) {
9733         Built.DependentCounters[Cnt] =
9734             Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx];
9735         Built.DependentInits[Cnt] =
9736             Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx];
9737         Built.FinalsConditions[Cnt] = IS.FinalCondition;
9738       }
9739     }
9740   }
9741 
9742   if (HasErrors)
9743     return 0;
9744 
9745   // Save results
9746   Built.IterationVarRef = IV.get();
9747   Built.LastIteration = LastIteration.get();
9748   Built.NumIterations = NumIterations.get();
9749   Built.CalcLastIteration = SemaRef
9750                                 .ActOnFinishFullExpr(CalcLastIteration.get(),
9751                                                      /*DiscardedValue=*/false)
9752                                 .get();
9753   Built.PreCond = PreCond.get();
9754   Built.PreInits = buildPreInits(C, Captures);
9755   Built.Cond = Cond.get();
9756   Built.Init = Init.get();
9757   Built.Inc = Inc.get();
9758   Built.LB = LB.get();
9759   Built.UB = UB.get();
9760   Built.IL = IL.get();
9761   Built.ST = ST.get();
9762   Built.EUB = EUB.get();
9763   Built.NLB = NextLB.get();
9764   Built.NUB = NextUB.get();
9765   Built.PrevLB = PrevLB.get();
9766   Built.PrevUB = PrevUB.get();
9767   Built.DistInc = DistInc.get();
9768   Built.PrevEUB = PrevEUB.get();
9769   Built.DistCombinedFields.LB = CombLB.get();
9770   Built.DistCombinedFields.UB = CombUB.get();
9771   Built.DistCombinedFields.EUB = CombEUB.get();
9772   Built.DistCombinedFields.Init = CombInit.get();
9773   Built.DistCombinedFields.Cond = CombCond.get();
9774   Built.DistCombinedFields.NLB = CombNextLB.get();
9775   Built.DistCombinedFields.NUB = CombNextUB.get();
9776   Built.DistCombinedFields.DistCond = CombDistCond.get();
9777   Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
9778 
9779   return NestedLoopCount;
9780 }
9781 
9782 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
9783   auto CollapseClauses =
9784       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
9785   if (CollapseClauses.begin() != CollapseClauses.end())
9786     return (*CollapseClauses.begin())->getNumForLoops();
9787   return nullptr;
9788 }
9789 
9790 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
9791   auto OrderedClauses =
9792       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
9793   if (OrderedClauses.begin() != OrderedClauses.end())
9794     return (*OrderedClauses.begin())->getNumForLoops();
9795   return nullptr;
9796 }
9797 
9798 static bool checkSimdlenSafelenSpecified(Sema &S,
9799                                          const ArrayRef<OMPClause *> Clauses) {
9800   const OMPSafelenClause *Safelen = nullptr;
9801   const OMPSimdlenClause *Simdlen = nullptr;
9802 
9803   for (const OMPClause *Clause : Clauses) {
9804     if (Clause->getClauseKind() == OMPC_safelen)
9805       Safelen = cast<OMPSafelenClause>(Clause);
9806     else if (Clause->getClauseKind() == OMPC_simdlen)
9807       Simdlen = cast<OMPSimdlenClause>(Clause);
9808     if (Safelen && Simdlen)
9809       break;
9810   }
9811 
9812   if (Simdlen && Safelen) {
9813     const Expr *SimdlenLength = Simdlen->getSimdlen();
9814     const Expr *SafelenLength = Safelen->getSafelen();
9815     if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
9816         SimdlenLength->isInstantiationDependent() ||
9817         SimdlenLength->containsUnexpandedParameterPack())
9818       return false;
9819     if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
9820         SafelenLength->isInstantiationDependent() ||
9821         SafelenLength->containsUnexpandedParameterPack())
9822       return false;
9823     Expr::EvalResult SimdlenResult, SafelenResult;
9824     SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
9825     SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
9826     llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
9827     llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
9828     // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
9829     // If both simdlen and safelen clauses are specified, the value of the
9830     // simdlen parameter must be less than or equal to the value of the safelen
9831     // parameter.
9832     if (SimdlenRes > SafelenRes) {
9833       S.Diag(SimdlenLength->getExprLoc(),
9834              diag::err_omp_wrong_simdlen_safelen_values)
9835           << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
9836       return true;
9837     }
9838   }
9839   return false;
9840 }
9841 
9842 StmtResult
9843 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
9844                                SourceLocation StartLoc, SourceLocation EndLoc,
9845                                VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9846   if (!AStmt)
9847     return StmtError();
9848 
9849   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9850   OMPLoopBasedDirective::HelperExprs B;
9851   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9852   // define the nested loops number.
9853   unsigned NestedLoopCount = checkOpenMPLoop(
9854       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
9855       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
9856   if (NestedLoopCount == 0)
9857     return StmtError();
9858 
9859   assert((CurContext->isDependentContext() || B.builtAll()) &&
9860          "omp simd loop exprs were not built");
9861 
9862   if (!CurContext->isDependentContext()) {
9863     // Finalize the clauses that need pre-built expressions for CodeGen.
9864     for (OMPClause *C : Clauses) {
9865       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9866         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9867                                      B.NumIterations, *this, CurScope,
9868                                      DSAStack))
9869           return StmtError();
9870     }
9871   }
9872 
9873   if (checkSimdlenSafelenSpecified(*this, Clauses))
9874     return StmtError();
9875 
9876   setFunctionHasBranchProtectedScope();
9877   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
9878                                   Clauses, AStmt, B);
9879 }
9880 
9881 StmtResult
9882 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
9883                               SourceLocation StartLoc, SourceLocation EndLoc,
9884                               VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9885   if (!AStmt)
9886     return StmtError();
9887 
9888   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9889   OMPLoopBasedDirective::HelperExprs B;
9890   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9891   // define the nested loops number.
9892   unsigned NestedLoopCount = checkOpenMPLoop(
9893       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
9894       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
9895   if (NestedLoopCount == 0)
9896     return StmtError();
9897 
9898   assert((CurContext->isDependentContext() || B.builtAll()) &&
9899          "omp for loop exprs were not built");
9900 
9901   if (!CurContext->isDependentContext()) {
9902     // Finalize the clauses that need pre-built expressions for CodeGen.
9903     for (OMPClause *C : Clauses) {
9904       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9905         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9906                                      B.NumIterations, *this, CurScope,
9907                                      DSAStack))
9908           return StmtError();
9909     }
9910   }
9911 
9912   setFunctionHasBranchProtectedScope();
9913   return OMPForDirective::Create(
9914       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9915       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
9916 }
9917 
9918 StmtResult Sema::ActOnOpenMPForSimdDirective(
9919     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9920     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9921   if (!AStmt)
9922     return StmtError();
9923 
9924   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9925   OMPLoopBasedDirective::HelperExprs B;
9926   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9927   // define the nested loops number.
9928   unsigned NestedLoopCount =
9929       checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
9930                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
9931                       VarsWithImplicitDSA, B);
9932   if (NestedLoopCount == 0)
9933     return StmtError();
9934 
9935   assert((CurContext->isDependentContext() || B.builtAll()) &&
9936          "omp for simd loop exprs were not built");
9937 
9938   if (!CurContext->isDependentContext()) {
9939     // Finalize the clauses that need pre-built expressions for CodeGen.
9940     for (OMPClause *C : Clauses) {
9941       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9942         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9943                                      B.NumIterations, *this, CurScope,
9944                                      DSAStack))
9945           return StmtError();
9946     }
9947   }
9948 
9949   if (checkSimdlenSafelenSpecified(*this, Clauses))
9950     return StmtError();
9951 
9952   setFunctionHasBranchProtectedScope();
9953   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
9954                                      Clauses, AStmt, B);
9955 }
9956 
9957 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
9958                                               Stmt *AStmt,
9959                                               SourceLocation StartLoc,
9960                                               SourceLocation EndLoc) {
9961   if (!AStmt)
9962     return StmtError();
9963 
9964   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9965   auto BaseStmt = AStmt;
9966   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
9967     BaseStmt = CS->getCapturedStmt();
9968   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
9969     auto S = C->children();
9970     if (S.begin() == S.end())
9971       return StmtError();
9972     // All associated statements must be '#pragma omp section' except for
9973     // the first one.
9974     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
9975       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
9976         if (SectionStmt)
9977           Diag(SectionStmt->getBeginLoc(),
9978                diag::err_omp_sections_substmt_not_section);
9979         return StmtError();
9980       }
9981       cast<OMPSectionDirective>(SectionStmt)
9982           ->setHasCancel(DSAStack->isCancelRegion());
9983     }
9984   } else {
9985     Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
9986     return StmtError();
9987   }
9988 
9989   setFunctionHasBranchProtectedScope();
9990 
9991   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
9992                                       DSAStack->getTaskgroupReductionRef(),
9993                                       DSAStack->isCancelRegion());
9994 }
9995 
9996 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
9997                                              SourceLocation StartLoc,
9998                                              SourceLocation EndLoc) {
9999   if (!AStmt)
10000     return StmtError();
10001 
10002   setFunctionHasBranchProtectedScope();
10003   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
10004 
10005   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
10006                                      DSAStack->isCancelRegion());
10007 }
10008 
10009 static Expr *getDirectCallExpr(Expr *E) {
10010   E = E->IgnoreParenCasts()->IgnoreImplicit();
10011   if (auto *CE = dyn_cast<CallExpr>(E))
10012     if (CE->getDirectCallee())
10013       return E;
10014   return nullptr;
10015 }
10016 
10017 StmtResult Sema::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses,
10018                                               Stmt *AStmt,
10019                                               SourceLocation StartLoc,
10020                                               SourceLocation EndLoc) {
10021   if (!AStmt)
10022     return StmtError();
10023 
10024   Stmt *S = cast<CapturedStmt>(AStmt)->getCapturedStmt();
10025 
10026   // 5.1 OpenMP
10027   // expression-stmt : an expression statement with one of the following forms:
10028   //   expression = target-call ( [expression-list] );
10029   //   target-call ( [expression-list] );
10030 
10031   SourceLocation TargetCallLoc;
10032 
10033   if (!CurContext->isDependentContext()) {
10034     Expr *TargetCall = nullptr;
10035 
10036     auto *E = dyn_cast<Expr>(S);
10037     if (!E) {
10038       Diag(S->getBeginLoc(), diag::err_omp_dispatch_statement_call);
10039       return StmtError();
10040     }
10041 
10042     E = E->IgnoreParenCasts()->IgnoreImplicit();
10043 
10044     if (auto *BO = dyn_cast<BinaryOperator>(E)) {
10045       if (BO->getOpcode() == BO_Assign)
10046         TargetCall = getDirectCallExpr(BO->getRHS());
10047     } else {
10048       if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(E))
10049         if (COCE->getOperator() == OO_Equal)
10050           TargetCall = getDirectCallExpr(COCE->getArg(1));
10051       if (!TargetCall)
10052         TargetCall = getDirectCallExpr(E);
10053     }
10054     if (!TargetCall) {
10055       Diag(E->getBeginLoc(), diag::err_omp_dispatch_statement_call);
10056       return StmtError();
10057     }
10058     TargetCallLoc = TargetCall->getExprLoc();
10059   }
10060 
10061   setFunctionHasBranchProtectedScope();
10062 
10063   return OMPDispatchDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
10064                                       TargetCallLoc);
10065 }
10066 
10067 StmtResult Sema::ActOnOpenMPGenericLoopDirective(
10068     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10069     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10070   if (!AStmt)
10071     return StmtError();
10072 
10073   // OpenMP 5.1 [2.11.7, loop construct]
10074   // A list item may not appear in a lastprivate clause unless it is the
10075   // loop iteration variable of a loop that is associated with the construct.
10076   for (OMPClause *C : Clauses) {
10077     if (auto *LPC = dyn_cast<OMPLastprivateClause>(C)) {
10078       for (Expr *RefExpr : LPC->varlists()) {
10079         SourceLocation ELoc;
10080         SourceRange ERange;
10081         Expr *SimpleRefExpr = RefExpr;
10082         auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
10083         if (ValueDecl *D = Res.first) {
10084           auto &&Info = DSAStack->isLoopControlVariable(D);
10085           if (!Info.first) {
10086             Diag(ELoc, diag::err_omp_lastprivate_loop_var_non_loop_iteration);
10087             return StmtError();
10088           }
10089         }
10090       }
10091     }
10092   }
10093 
10094   auto *CS = cast<CapturedStmt>(AStmt);
10095   // 1.2.2 OpenMP Language Terminology
10096   // Structured block - An executable statement with a single entry at the
10097   // top and a single exit at the bottom.
10098   // The point of exit cannot be a branch out of the structured block.
10099   // longjmp() and throw() must not violate the entry/exit criteria.
10100   CS->getCapturedDecl()->setNothrow();
10101 
10102   OMPLoopDirective::HelperExprs B;
10103   // In presence of clause 'collapse', it will define the nested loops number.
10104   unsigned NestedLoopCount = checkOpenMPLoop(
10105       OMPD_loop, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
10106       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
10107   if (NestedLoopCount == 0)
10108     return StmtError();
10109 
10110   assert((CurContext->isDependentContext() || B.builtAll()) &&
10111          "omp loop exprs were not built");
10112 
10113   setFunctionHasBranchProtectedScope();
10114   return OMPGenericLoopDirective::Create(Context, StartLoc, EndLoc,
10115                                          NestedLoopCount, Clauses, AStmt, B);
10116 }
10117 
10118 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
10119                                             Stmt *AStmt,
10120                                             SourceLocation StartLoc,
10121                                             SourceLocation EndLoc) {
10122   if (!AStmt)
10123     return StmtError();
10124 
10125   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10126 
10127   setFunctionHasBranchProtectedScope();
10128 
10129   // OpenMP [2.7.3, single Construct, Restrictions]
10130   // The copyprivate clause must not be used with the nowait clause.
10131   const OMPClause *Nowait = nullptr;
10132   const OMPClause *Copyprivate = nullptr;
10133   for (const OMPClause *Clause : Clauses) {
10134     if (Clause->getClauseKind() == OMPC_nowait)
10135       Nowait = Clause;
10136     else if (Clause->getClauseKind() == OMPC_copyprivate)
10137       Copyprivate = Clause;
10138     if (Copyprivate && Nowait) {
10139       Diag(Copyprivate->getBeginLoc(),
10140            diag::err_omp_single_copyprivate_with_nowait);
10141       Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
10142       return StmtError();
10143     }
10144   }
10145 
10146   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
10147 }
10148 
10149 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
10150                                             SourceLocation StartLoc,
10151                                             SourceLocation EndLoc) {
10152   if (!AStmt)
10153     return StmtError();
10154 
10155   setFunctionHasBranchProtectedScope();
10156 
10157   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
10158 }
10159 
10160 StmtResult Sema::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses,
10161                                             Stmt *AStmt,
10162                                             SourceLocation StartLoc,
10163                                             SourceLocation EndLoc) {
10164   if (!AStmt)
10165     return StmtError();
10166 
10167   setFunctionHasBranchProtectedScope();
10168 
10169   return OMPMaskedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
10170 }
10171 
10172 StmtResult Sema::ActOnOpenMPCriticalDirective(
10173     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
10174     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
10175   if (!AStmt)
10176     return StmtError();
10177 
10178   bool ErrorFound = false;
10179   llvm::APSInt Hint;
10180   SourceLocation HintLoc;
10181   bool DependentHint = false;
10182   for (const OMPClause *C : Clauses) {
10183     if (C->getClauseKind() == OMPC_hint) {
10184       if (!DirName.getName()) {
10185         Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
10186         ErrorFound = true;
10187       }
10188       Expr *E = cast<OMPHintClause>(C)->getHint();
10189       if (E->isTypeDependent() || E->isValueDependent() ||
10190           E->isInstantiationDependent()) {
10191         DependentHint = true;
10192       } else {
10193         Hint = E->EvaluateKnownConstInt(Context);
10194         HintLoc = C->getBeginLoc();
10195       }
10196     }
10197   }
10198   if (ErrorFound)
10199     return StmtError();
10200   const auto Pair = DSAStack->getCriticalWithHint(DirName);
10201   if (Pair.first && DirName.getName() && !DependentHint) {
10202     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
10203       Diag(StartLoc, diag::err_omp_critical_with_hint);
10204       if (HintLoc.isValid())
10205         Diag(HintLoc, diag::note_omp_critical_hint_here)
10206             << 0 << toString(Hint, /*Radix=*/10, /*Signed=*/false);
10207       else
10208         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
10209       if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
10210         Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
10211             << 1
10212             << toString(C->getHint()->EvaluateKnownConstInt(Context),
10213                         /*Radix=*/10, /*Signed=*/false);
10214       } else {
10215         Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
10216       }
10217     }
10218   }
10219 
10220   setFunctionHasBranchProtectedScope();
10221 
10222   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
10223                                            Clauses, AStmt);
10224   if (!Pair.first && DirName.getName() && !DependentHint)
10225     DSAStack->addCriticalWithHint(Dir, Hint);
10226   return Dir;
10227 }
10228 
10229 StmtResult Sema::ActOnOpenMPParallelForDirective(
10230     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10231     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10232   if (!AStmt)
10233     return StmtError();
10234 
10235   auto *CS = cast<CapturedStmt>(AStmt);
10236   // 1.2.2 OpenMP Language Terminology
10237   // Structured block - An executable statement with a single entry at the
10238   // top and a single exit at the bottom.
10239   // The point of exit cannot be a branch out of the structured block.
10240   // longjmp() and throw() must not violate the entry/exit criteria.
10241   CS->getCapturedDecl()->setNothrow();
10242 
10243   OMPLoopBasedDirective::HelperExprs B;
10244   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10245   // define the nested loops number.
10246   unsigned NestedLoopCount =
10247       checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
10248                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
10249                       VarsWithImplicitDSA, B);
10250   if (NestedLoopCount == 0)
10251     return StmtError();
10252 
10253   assert((CurContext->isDependentContext() || B.builtAll()) &&
10254          "omp parallel for loop exprs were not built");
10255 
10256   if (!CurContext->isDependentContext()) {
10257     // Finalize the clauses that need pre-built expressions for CodeGen.
10258     for (OMPClause *C : Clauses) {
10259       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10260         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10261                                      B.NumIterations, *this, CurScope,
10262                                      DSAStack))
10263           return StmtError();
10264     }
10265   }
10266 
10267   setFunctionHasBranchProtectedScope();
10268   return OMPParallelForDirective::Create(
10269       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
10270       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10271 }
10272 
10273 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
10274     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10275     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10276   if (!AStmt)
10277     return StmtError();
10278 
10279   auto *CS = cast<CapturedStmt>(AStmt);
10280   // 1.2.2 OpenMP Language Terminology
10281   // Structured block - An executable statement with a single entry at the
10282   // top and a single exit at the bottom.
10283   // The point of exit cannot be a branch out of the structured block.
10284   // longjmp() and throw() must not violate the entry/exit criteria.
10285   CS->getCapturedDecl()->setNothrow();
10286 
10287   OMPLoopBasedDirective::HelperExprs B;
10288   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10289   // define the nested loops number.
10290   unsigned NestedLoopCount =
10291       checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
10292                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
10293                       VarsWithImplicitDSA, B);
10294   if (NestedLoopCount == 0)
10295     return StmtError();
10296 
10297   if (!CurContext->isDependentContext()) {
10298     // Finalize the clauses that need pre-built expressions for CodeGen.
10299     for (OMPClause *C : Clauses) {
10300       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10301         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10302                                      B.NumIterations, *this, CurScope,
10303                                      DSAStack))
10304           return StmtError();
10305     }
10306   }
10307 
10308   if (checkSimdlenSafelenSpecified(*this, Clauses))
10309     return StmtError();
10310 
10311   setFunctionHasBranchProtectedScope();
10312   return OMPParallelForSimdDirective::Create(
10313       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10314 }
10315 
10316 StmtResult
10317 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses,
10318                                          Stmt *AStmt, SourceLocation StartLoc,
10319                                          SourceLocation EndLoc) {
10320   if (!AStmt)
10321     return StmtError();
10322 
10323   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10324   auto *CS = cast<CapturedStmt>(AStmt);
10325   // 1.2.2 OpenMP Language Terminology
10326   // Structured block - An executable statement with a single entry at the
10327   // top and a single exit at the bottom.
10328   // The point of exit cannot be a branch out of the structured block.
10329   // longjmp() and throw() must not violate the entry/exit criteria.
10330   CS->getCapturedDecl()->setNothrow();
10331 
10332   setFunctionHasBranchProtectedScope();
10333 
10334   return OMPParallelMasterDirective::Create(
10335       Context, StartLoc, EndLoc, Clauses, AStmt,
10336       DSAStack->getTaskgroupReductionRef());
10337 }
10338 
10339 StmtResult
10340 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
10341                                            Stmt *AStmt, SourceLocation StartLoc,
10342                                            SourceLocation EndLoc) {
10343   if (!AStmt)
10344     return StmtError();
10345 
10346   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10347   auto BaseStmt = AStmt;
10348   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
10349     BaseStmt = CS->getCapturedStmt();
10350   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
10351     auto S = C->children();
10352     if (S.begin() == S.end())
10353       return StmtError();
10354     // All associated statements must be '#pragma omp section' except for
10355     // the first one.
10356     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
10357       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
10358         if (SectionStmt)
10359           Diag(SectionStmt->getBeginLoc(),
10360                diag::err_omp_parallel_sections_substmt_not_section);
10361         return StmtError();
10362       }
10363       cast<OMPSectionDirective>(SectionStmt)
10364           ->setHasCancel(DSAStack->isCancelRegion());
10365     }
10366   } else {
10367     Diag(AStmt->getBeginLoc(),
10368          diag::err_omp_parallel_sections_not_compound_stmt);
10369     return StmtError();
10370   }
10371 
10372   setFunctionHasBranchProtectedScope();
10373 
10374   return OMPParallelSectionsDirective::Create(
10375       Context, StartLoc, EndLoc, Clauses, AStmt,
10376       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10377 }
10378 
10379 /// Find and diagnose mutually exclusive clause kinds.
10380 static bool checkMutuallyExclusiveClauses(
10381     Sema &S, ArrayRef<OMPClause *> Clauses,
10382     ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) {
10383   const OMPClause *PrevClause = nullptr;
10384   bool ErrorFound = false;
10385   for (const OMPClause *C : Clauses) {
10386     if (llvm::is_contained(MutuallyExclusiveClauses, C->getClauseKind())) {
10387       if (!PrevClause) {
10388         PrevClause = C;
10389       } else if (PrevClause->getClauseKind() != C->getClauseKind()) {
10390         S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive)
10391             << getOpenMPClauseName(C->getClauseKind())
10392             << getOpenMPClauseName(PrevClause->getClauseKind());
10393         S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause)
10394             << getOpenMPClauseName(PrevClause->getClauseKind());
10395         ErrorFound = true;
10396       }
10397     }
10398   }
10399   return ErrorFound;
10400 }
10401 
10402 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
10403                                           Stmt *AStmt, SourceLocation StartLoc,
10404                                           SourceLocation EndLoc) {
10405   if (!AStmt)
10406     return StmtError();
10407 
10408   // OpenMP 5.0, 2.10.1 task Construct
10409   // If a detach clause appears on the directive, then a mergeable clause cannot
10410   // appear on the same directive.
10411   if (checkMutuallyExclusiveClauses(*this, Clauses,
10412                                     {OMPC_detach, OMPC_mergeable}))
10413     return StmtError();
10414 
10415   auto *CS = cast<CapturedStmt>(AStmt);
10416   // 1.2.2 OpenMP Language Terminology
10417   // Structured block - An executable statement with a single entry at the
10418   // top and a single exit at the bottom.
10419   // The point of exit cannot be a branch out of the structured block.
10420   // longjmp() and throw() must not violate the entry/exit criteria.
10421   CS->getCapturedDecl()->setNothrow();
10422 
10423   setFunctionHasBranchProtectedScope();
10424 
10425   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
10426                                   DSAStack->isCancelRegion());
10427 }
10428 
10429 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
10430                                                SourceLocation EndLoc) {
10431   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
10432 }
10433 
10434 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
10435                                              SourceLocation EndLoc) {
10436   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
10437 }
10438 
10439 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
10440                                               SourceLocation EndLoc) {
10441   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
10442 }
10443 
10444 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
10445                                                Stmt *AStmt,
10446                                                SourceLocation StartLoc,
10447                                                SourceLocation EndLoc) {
10448   if (!AStmt)
10449     return StmtError();
10450 
10451   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10452 
10453   setFunctionHasBranchProtectedScope();
10454 
10455   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
10456                                        AStmt,
10457                                        DSAStack->getTaskgroupReductionRef());
10458 }
10459 
10460 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
10461                                            SourceLocation StartLoc,
10462                                            SourceLocation EndLoc) {
10463   OMPFlushClause *FC = nullptr;
10464   OMPClause *OrderClause = nullptr;
10465   for (OMPClause *C : Clauses) {
10466     if (C->getClauseKind() == OMPC_flush)
10467       FC = cast<OMPFlushClause>(C);
10468     else
10469       OrderClause = C;
10470   }
10471   OpenMPClauseKind MemOrderKind = OMPC_unknown;
10472   SourceLocation MemOrderLoc;
10473   for (const OMPClause *C : Clauses) {
10474     if (C->getClauseKind() == OMPC_acq_rel ||
10475         C->getClauseKind() == OMPC_acquire ||
10476         C->getClauseKind() == OMPC_release) {
10477       if (MemOrderKind != OMPC_unknown) {
10478         Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses)
10479             << getOpenMPDirectiveName(OMPD_flush) << 1
10480             << SourceRange(C->getBeginLoc(), C->getEndLoc());
10481         Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause)
10482             << getOpenMPClauseName(MemOrderKind);
10483       } else {
10484         MemOrderKind = C->getClauseKind();
10485         MemOrderLoc = C->getBeginLoc();
10486       }
10487     }
10488   }
10489   if (FC && OrderClause) {
10490     Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list)
10491         << getOpenMPClauseName(OrderClause->getClauseKind());
10492     Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here)
10493         << getOpenMPClauseName(OrderClause->getClauseKind());
10494     return StmtError();
10495   }
10496   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
10497 }
10498 
10499 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses,
10500                                             SourceLocation StartLoc,
10501                                             SourceLocation EndLoc) {
10502   if (Clauses.empty()) {
10503     Diag(StartLoc, diag::err_omp_depobj_expected);
10504     return StmtError();
10505   } else if (Clauses[0]->getClauseKind() != OMPC_depobj) {
10506     Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected);
10507     return StmtError();
10508   }
10509   // Only depobj expression and another single clause is allowed.
10510   if (Clauses.size() > 2) {
10511     Diag(Clauses[2]->getBeginLoc(),
10512          diag::err_omp_depobj_single_clause_expected);
10513     return StmtError();
10514   } else if (Clauses.size() < 1) {
10515     Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected);
10516     return StmtError();
10517   }
10518   return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses);
10519 }
10520 
10521 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses,
10522                                           SourceLocation StartLoc,
10523                                           SourceLocation EndLoc) {
10524   // Check that exactly one clause is specified.
10525   if (Clauses.size() != 1) {
10526     Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(),
10527          diag::err_omp_scan_single_clause_expected);
10528     return StmtError();
10529   }
10530   // Check that scan directive is used in the scopeof the OpenMP loop body.
10531   if (Scope *S = DSAStack->getCurScope()) {
10532     Scope *ParentS = S->getParent();
10533     if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() ||
10534         !ParentS->getBreakParent()->isOpenMPLoopScope())
10535       return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive)
10536                        << getOpenMPDirectiveName(OMPD_scan) << 5);
10537   }
10538   // Check that only one instance of scan directives is used in the same outer
10539   // region.
10540   if (DSAStack->doesParentHasScanDirective()) {
10541     Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan";
10542     Diag(DSAStack->getParentScanDirectiveLoc(),
10543          diag::note_omp_previous_directive)
10544         << "scan";
10545     return StmtError();
10546   }
10547   DSAStack->setParentHasScanDirective(StartLoc);
10548   return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses);
10549 }
10550 
10551 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
10552                                              Stmt *AStmt,
10553                                              SourceLocation StartLoc,
10554                                              SourceLocation EndLoc) {
10555   const OMPClause *DependFound = nullptr;
10556   const OMPClause *DependSourceClause = nullptr;
10557   const OMPClause *DependSinkClause = nullptr;
10558   bool ErrorFound = false;
10559   const OMPThreadsClause *TC = nullptr;
10560   const OMPSIMDClause *SC = nullptr;
10561   for (const OMPClause *C : Clauses) {
10562     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
10563       DependFound = C;
10564       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
10565         if (DependSourceClause) {
10566           Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
10567               << getOpenMPDirectiveName(OMPD_ordered)
10568               << getOpenMPClauseName(OMPC_depend) << 2;
10569           ErrorFound = true;
10570         } else {
10571           DependSourceClause = C;
10572         }
10573         if (DependSinkClause) {
10574           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
10575               << 0;
10576           ErrorFound = true;
10577         }
10578       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
10579         if (DependSourceClause) {
10580           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
10581               << 1;
10582           ErrorFound = true;
10583         }
10584         DependSinkClause = C;
10585       }
10586     } else if (C->getClauseKind() == OMPC_threads) {
10587       TC = cast<OMPThreadsClause>(C);
10588     } else if (C->getClauseKind() == OMPC_simd) {
10589       SC = cast<OMPSIMDClause>(C);
10590     }
10591   }
10592   if (!ErrorFound && !SC &&
10593       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
10594     // OpenMP [2.8.1,simd Construct, Restrictions]
10595     // An ordered construct with the simd clause is the only OpenMP construct
10596     // that can appear in the simd region.
10597     Diag(StartLoc, diag::err_omp_prohibited_region_simd)
10598         << (LangOpts.OpenMP >= 50 ? 1 : 0);
10599     ErrorFound = true;
10600   } else if (DependFound && (TC || SC)) {
10601     Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
10602         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
10603     ErrorFound = true;
10604   } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
10605     Diag(DependFound->getBeginLoc(),
10606          diag::err_omp_ordered_directive_without_param);
10607     ErrorFound = true;
10608   } else if (TC || Clauses.empty()) {
10609     if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
10610       SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
10611       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
10612           << (TC != nullptr);
10613       Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1;
10614       ErrorFound = true;
10615     }
10616   }
10617   if ((!AStmt && !DependFound) || ErrorFound)
10618     return StmtError();
10619 
10620   // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions.
10621   // During execution of an iteration of a worksharing-loop or a loop nest
10622   // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread
10623   // must not execute more than one ordered region corresponding to an ordered
10624   // construct without a depend clause.
10625   if (!DependFound) {
10626     if (DSAStack->doesParentHasOrderedDirective()) {
10627       Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered";
10628       Diag(DSAStack->getParentOrderedDirectiveLoc(),
10629            diag::note_omp_previous_directive)
10630           << "ordered";
10631       return StmtError();
10632     }
10633     DSAStack->setParentHasOrderedDirective(StartLoc);
10634   }
10635 
10636   if (AStmt) {
10637     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10638 
10639     setFunctionHasBranchProtectedScope();
10640   }
10641 
10642   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
10643 }
10644 
10645 namespace {
10646 /// Helper class for checking expression in 'omp atomic [update]'
10647 /// construct.
10648 class OpenMPAtomicUpdateChecker {
10649   /// Error results for atomic update expressions.
10650   enum ExprAnalysisErrorCode {
10651     /// A statement is not an expression statement.
10652     NotAnExpression,
10653     /// Expression is not builtin binary or unary operation.
10654     NotABinaryOrUnaryExpression,
10655     /// Unary operation is not post-/pre- increment/decrement operation.
10656     NotAnUnaryIncDecExpression,
10657     /// An expression is not of scalar type.
10658     NotAScalarType,
10659     /// A binary operation is not an assignment operation.
10660     NotAnAssignmentOp,
10661     /// RHS part of the binary operation is not a binary expression.
10662     NotABinaryExpression,
10663     /// RHS part is not additive/multiplicative/shift/biwise binary
10664     /// expression.
10665     NotABinaryOperator,
10666     /// RHS binary operation does not have reference to the updated LHS
10667     /// part.
10668     NotAnUpdateExpression,
10669     /// No errors is found.
10670     NoError
10671   };
10672   /// Reference to Sema.
10673   Sema &SemaRef;
10674   /// A location for note diagnostics (when error is found).
10675   SourceLocation NoteLoc;
10676   /// 'x' lvalue part of the source atomic expression.
10677   Expr *X;
10678   /// 'expr' rvalue part of the source atomic expression.
10679   Expr *E;
10680   /// Helper expression of the form
10681   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
10682   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
10683   Expr *UpdateExpr;
10684   /// Is 'x' a LHS in a RHS part of full update expression. It is
10685   /// important for non-associative operations.
10686   bool IsXLHSInRHSPart;
10687   BinaryOperatorKind Op;
10688   SourceLocation OpLoc;
10689   /// true if the source expression is a postfix unary operation, false
10690   /// if it is a prefix unary operation.
10691   bool IsPostfixUpdate;
10692 
10693 public:
10694   OpenMPAtomicUpdateChecker(Sema &SemaRef)
10695       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
10696         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
10697   /// Check specified statement that it is suitable for 'atomic update'
10698   /// constructs and extract 'x', 'expr' and Operation from the original
10699   /// expression. If DiagId and NoteId == 0, then only check is performed
10700   /// without error notification.
10701   /// \param DiagId Diagnostic which should be emitted if error is found.
10702   /// \param NoteId Diagnostic note for the main error message.
10703   /// \return true if statement is not an update expression, false otherwise.
10704   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
10705   /// Return the 'x' lvalue part of the source atomic expression.
10706   Expr *getX() const { return X; }
10707   /// Return the 'expr' rvalue part of the source atomic expression.
10708   Expr *getExpr() const { return E; }
10709   /// Return the update expression used in calculation of the updated
10710   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
10711   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
10712   Expr *getUpdateExpr() const { return UpdateExpr; }
10713   /// Return true if 'x' is LHS in RHS part of full update expression,
10714   /// false otherwise.
10715   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
10716 
10717   /// true if the source expression is a postfix unary operation, false
10718   /// if it is a prefix unary operation.
10719   bool isPostfixUpdate() const { return IsPostfixUpdate; }
10720 
10721 private:
10722   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
10723                             unsigned NoteId = 0);
10724 };
10725 } // namespace
10726 
10727 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
10728     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
10729   ExprAnalysisErrorCode ErrorFound = NoError;
10730   SourceLocation ErrorLoc, NoteLoc;
10731   SourceRange ErrorRange, NoteRange;
10732   // Allowed constructs are:
10733   //  x = x binop expr;
10734   //  x = expr binop x;
10735   if (AtomicBinOp->getOpcode() == BO_Assign) {
10736     X = AtomicBinOp->getLHS();
10737     if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
10738             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
10739       if (AtomicInnerBinOp->isMultiplicativeOp() ||
10740           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
10741           AtomicInnerBinOp->isBitwiseOp()) {
10742         Op = AtomicInnerBinOp->getOpcode();
10743         OpLoc = AtomicInnerBinOp->getOperatorLoc();
10744         Expr *LHS = AtomicInnerBinOp->getLHS();
10745         Expr *RHS = AtomicInnerBinOp->getRHS();
10746         llvm::FoldingSetNodeID XId, LHSId, RHSId;
10747         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
10748                                           /*Canonical=*/true);
10749         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
10750                                             /*Canonical=*/true);
10751         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
10752                                             /*Canonical=*/true);
10753         if (XId == LHSId) {
10754           E = RHS;
10755           IsXLHSInRHSPart = true;
10756         } else if (XId == RHSId) {
10757           E = LHS;
10758           IsXLHSInRHSPart = false;
10759         } else {
10760           ErrorLoc = AtomicInnerBinOp->getExprLoc();
10761           ErrorRange = AtomicInnerBinOp->getSourceRange();
10762           NoteLoc = X->getExprLoc();
10763           NoteRange = X->getSourceRange();
10764           ErrorFound = NotAnUpdateExpression;
10765         }
10766       } else {
10767         ErrorLoc = AtomicInnerBinOp->getExprLoc();
10768         ErrorRange = AtomicInnerBinOp->getSourceRange();
10769         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
10770         NoteRange = SourceRange(NoteLoc, NoteLoc);
10771         ErrorFound = NotABinaryOperator;
10772       }
10773     } else {
10774       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
10775       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
10776       ErrorFound = NotABinaryExpression;
10777     }
10778   } else {
10779     ErrorLoc = AtomicBinOp->getExprLoc();
10780     ErrorRange = AtomicBinOp->getSourceRange();
10781     NoteLoc = AtomicBinOp->getOperatorLoc();
10782     NoteRange = SourceRange(NoteLoc, NoteLoc);
10783     ErrorFound = NotAnAssignmentOp;
10784   }
10785   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
10786     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
10787     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
10788     return true;
10789   }
10790   if (SemaRef.CurContext->isDependentContext())
10791     E = X = UpdateExpr = nullptr;
10792   return ErrorFound != NoError;
10793 }
10794 
10795 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
10796                                                unsigned NoteId) {
10797   ExprAnalysisErrorCode ErrorFound = NoError;
10798   SourceLocation ErrorLoc, NoteLoc;
10799   SourceRange ErrorRange, NoteRange;
10800   // Allowed constructs are:
10801   //  x++;
10802   //  x--;
10803   //  ++x;
10804   //  --x;
10805   //  x binop= expr;
10806   //  x = x binop expr;
10807   //  x = expr binop x;
10808   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
10809     AtomicBody = AtomicBody->IgnoreParenImpCasts();
10810     if (AtomicBody->getType()->isScalarType() ||
10811         AtomicBody->isInstantiationDependent()) {
10812       if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
10813               AtomicBody->IgnoreParenImpCasts())) {
10814         // Check for Compound Assignment Operation
10815         Op = BinaryOperator::getOpForCompoundAssignment(
10816             AtomicCompAssignOp->getOpcode());
10817         OpLoc = AtomicCompAssignOp->getOperatorLoc();
10818         E = AtomicCompAssignOp->getRHS();
10819         X = AtomicCompAssignOp->getLHS()->IgnoreParens();
10820         IsXLHSInRHSPart = true;
10821       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
10822                      AtomicBody->IgnoreParenImpCasts())) {
10823         // Check for Binary Operation
10824         if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
10825           return true;
10826       } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
10827                      AtomicBody->IgnoreParenImpCasts())) {
10828         // Check for Unary Operation
10829         if (AtomicUnaryOp->isIncrementDecrementOp()) {
10830           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
10831           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
10832           OpLoc = AtomicUnaryOp->getOperatorLoc();
10833           X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
10834           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
10835           IsXLHSInRHSPart = true;
10836         } else {
10837           ErrorFound = NotAnUnaryIncDecExpression;
10838           ErrorLoc = AtomicUnaryOp->getExprLoc();
10839           ErrorRange = AtomicUnaryOp->getSourceRange();
10840           NoteLoc = AtomicUnaryOp->getOperatorLoc();
10841           NoteRange = SourceRange(NoteLoc, NoteLoc);
10842         }
10843       } else if (!AtomicBody->isInstantiationDependent()) {
10844         ErrorFound = NotABinaryOrUnaryExpression;
10845         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
10846         NoteRange = ErrorRange = AtomicBody->getSourceRange();
10847       }
10848     } else {
10849       ErrorFound = NotAScalarType;
10850       NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
10851       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
10852     }
10853   } else {
10854     ErrorFound = NotAnExpression;
10855     NoteLoc = ErrorLoc = S->getBeginLoc();
10856     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
10857   }
10858   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
10859     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
10860     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
10861     return true;
10862   }
10863   if (SemaRef.CurContext->isDependentContext())
10864     E = X = UpdateExpr = nullptr;
10865   if (ErrorFound == NoError && E && X) {
10866     // Build an update expression of form 'OpaqueValueExpr(x) binop
10867     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
10868     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
10869     auto *OVEX = new (SemaRef.getASTContext())
10870         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue);
10871     auto *OVEExpr = new (SemaRef.getASTContext())
10872         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue);
10873     ExprResult Update =
10874         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
10875                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
10876     if (Update.isInvalid())
10877       return true;
10878     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
10879                                                Sema::AA_Casting);
10880     if (Update.isInvalid())
10881       return true;
10882     UpdateExpr = Update.get();
10883   }
10884   return ErrorFound != NoError;
10885 }
10886 
10887 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
10888                                             Stmt *AStmt,
10889                                             SourceLocation StartLoc,
10890                                             SourceLocation EndLoc) {
10891   // Register location of the first atomic directive.
10892   DSAStack->addAtomicDirectiveLoc(StartLoc);
10893   if (!AStmt)
10894     return StmtError();
10895 
10896   // 1.2.2 OpenMP Language Terminology
10897   // Structured block - An executable statement with a single entry at the
10898   // top and a single exit at the bottom.
10899   // The point of exit cannot be a branch out of the structured block.
10900   // longjmp() and throw() must not violate the entry/exit criteria.
10901   OpenMPClauseKind AtomicKind = OMPC_unknown;
10902   SourceLocation AtomicKindLoc;
10903   OpenMPClauseKind MemOrderKind = OMPC_unknown;
10904   SourceLocation MemOrderLoc;
10905   for (const OMPClause *C : Clauses) {
10906     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
10907         C->getClauseKind() == OMPC_update ||
10908         C->getClauseKind() == OMPC_capture) {
10909       if (AtomicKind != OMPC_unknown) {
10910         Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
10911             << SourceRange(C->getBeginLoc(), C->getEndLoc());
10912         Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause)
10913             << getOpenMPClauseName(AtomicKind);
10914       } else {
10915         AtomicKind = C->getClauseKind();
10916         AtomicKindLoc = C->getBeginLoc();
10917       }
10918     }
10919     if (C->getClauseKind() == OMPC_seq_cst ||
10920         C->getClauseKind() == OMPC_acq_rel ||
10921         C->getClauseKind() == OMPC_acquire ||
10922         C->getClauseKind() == OMPC_release ||
10923         C->getClauseKind() == OMPC_relaxed) {
10924       if (MemOrderKind != OMPC_unknown) {
10925         Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses)
10926             << getOpenMPDirectiveName(OMPD_atomic) << 0
10927             << SourceRange(C->getBeginLoc(), C->getEndLoc());
10928         Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause)
10929             << getOpenMPClauseName(MemOrderKind);
10930       } else {
10931         MemOrderKind = C->getClauseKind();
10932         MemOrderLoc = C->getBeginLoc();
10933       }
10934     }
10935   }
10936   // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions
10937   // If atomic-clause is read then memory-order-clause must not be acq_rel or
10938   // release.
10939   // If atomic-clause is write then memory-order-clause must not be acq_rel or
10940   // acquire.
10941   // If atomic-clause is update or not present then memory-order-clause must not
10942   // be acq_rel or acquire.
10943   if ((AtomicKind == OMPC_read &&
10944        (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) ||
10945       ((AtomicKind == OMPC_write || AtomicKind == OMPC_update ||
10946         AtomicKind == OMPC_unknown) &&
10947        (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) {
10948     SourceLocation Loc = AtomicKindLoc;
10949     if (AtomicKind == OMPC_unknown)
10950       Loc = StartLoc;
10951     Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause)
10952         << getOpenMPClauseName(AtomicKind)
10953         << (AtomicKind == OMPC_unknown ? 1 : 0)
10954         << getOpenMPClauseName(MemOrderKind);
10955     Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause)
10956         << getOpenMPClauseName(MemOrderKind);
10957   }
10958 
10959   Stmt *Body = AStmt;
10960   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
10961     Body = EWC->getSubExpr();
10962 
10963   Expr *X = nullptr;
10964   Expr *V = nullptr;
10965   Expr *E = nullptr;
10966   Expr *UE = nullptr;
10967   bool IsXLHSInRHSPart = false;
10968   bool IsPostfixUpdate = false;
10969   // OpenMP [2.12.6, atomic Construct]
10970   // In the next expressions:
10971   // * x and v (as applicable) are both l-value expressions with scalar type.
10972   // * During the execution of an atomic region, multiple syntactic
10973   // occurrences of x must designate the same storage location.
10974   // * Neither of v and expr (as applicable) may access the storage location
10975   // designated by x.
10976   // * Neither of x and expr (as applicable) may access the storage location
10977   // designated by v.
10978   // * expr is an expression with scalar type.
10979   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
10980   // * binop, binop=, ++, and -- are not overloaded operators.
10981   // * The expression x binop expr must be numerically equivalent to x binop
10982   // (expr). This requirement is satisfied if the operators in expr have
10983   // precedence greater than binop, or by using parentheses around expr or
10984   // subexpressions of expr.
10985   // * The expression expr binop x must be numerically equivalent to (expr)
10986   // binop x. This requirement is satisfied if the operators in expr have
10987   // precedence equal to or greater than binop, or by using parentheses around
10988   // expr or subexpressions of expr.
10989   // * For forms that allow multiple occurrences of x, the number of times
10990   // that x is evaluated is unspecified.
10991   if (AtomicKind == OMPC_read) {
10992     enum {
10993       NotAnExpression,
10994       NotAnAssignmentOp,
10995       NotAScalarType,
10996       NotAnLValue,
10997       NoError
10998     } ErrorFound = NoError;
10999     SourceLocation ErrorLoc, NoteLoc;
11000     SourceRange ErrorRange, NoteRange;
11001     // If clause is read:
11002     //  v = x;
11003     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
11004       const auto *AtomicBinOp =
11005           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
11006       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
11007         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
11008         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
11009         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
11010             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
11011           if (!X->isLValue() || !V->isLValue()) {
11012             const Expr *NotLValueExpr = X->isLValue() ? V : X;
11013             ErrorFound = NotAnLValue;
11014             ErrorLoc = AtomicBinOp->getExprLoc();
11015             ErrorRange = AtomicBinOp->getSourceRange();
11016             NoteLoc = NotLValueExpr->getExprLoc();
11017             NoteRange = NotLValueExpr->getSourceRange();
11018           }
11019         } else if (!X->isInstantiationDependent() ||
11020                    !V->isInstantiationDependent()) {
11021           const Expr *NotScalarExpr =
11022               (X->isInstantiationDependent() || X->getType()->isScalarType())
11023                   ? V
11024                   : X;
11025           ErrorFound = NotAScalarType;
11026           ErrorLoc = AtomicBinOp->getExprLoc();
11027           ErrorRange = AtomicBinOp->getSourceRange();
11028           NoteLoc = NotScalarExpr->getExprLoc();
11029           NoteRange = NotScalarExpr->getSourceRange();
11030         }
11031       } else if (!AtomicBody->isInstantiationDependent()) {
11032         ErrorFound = NotAnAssignmentOp;
11033         ErrorLoc = AtomicBody->getExprLoc();
11034         ErrorRange = AtomicBody->getSourceRange();
11035         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
11036                               : AtomicBody->getExprLoc();
11037         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
11038                                 : AtomicBody->getSourceRange();
11039       }
11040     } else {
11041       ErrorFound = NotAnExpression;
11042       NoteLoc = ErrorLoc = Body->getBeginLoc();
11043       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
11044     }
11045     if (ErrorFound != NoError) {
11046       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
11047           << ErrorRange;
11048       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
11049                                                       << NoteRange;
11050       return StmtError();
11051     }
11052     if (CurContext->isDependentContext())
11053       V = X = nullptr;
11054   } else if (AtomicKind == OMPC_write) {
11055     enum {
11056       NotAnExpression,
11057       NotAnAssignmentOp,
11058       NotAScalarType,
11059       NotAnLValue,
11060       NoError
11061     } ErrorFound = NoError;
11062     SourceLocation ErrorLoc, NoteLoc;
11063     SourceRange ErrorRange, NoteRange;
11064     // If clause is write:
11065     //  x = expr;
11066     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
11067       const auto *AtomicBinOp =
11068           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
11069       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
11070         X = AtomicBinOp->getLHS();
11071         E = AtomicBinOp->getRHS();
11072         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
11073             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
11074           if (!X->isLValue()) {
11075             ErrorFound = NotAnLValue;
11076             ErrorLoc = AtomicBinOp->getExprLoc();
11077             ErrorRange = AtomicBinOp->getSourceRange();
11078             NoteLoc = X->getExprLoc();
11079             NoteRange = X->getSourceRange();
11080           }
11081         } else if (!X->isInstantiationDependent() ||
11082                    !E->isInstantiationDependent()) {
11083           const Expr *NotScalarExpr =
11084               (X->isInstantiationDependent() || X->getType()->isScalarType())
11085                   ? E
11086                   : X;
11087           ErrorFound = NotAScalarType;
11088           ErrorLoc = AtomicBinOp->getExprLoc();
11089           ErrorRange = AtomicBinOp->getSourceRange();
11090           NoteLoc = NotScalarExpr->getExprLoc();
11091           NoteRange = NotScalarExpr->getSourceRange();
11092         }
11093       } else if (!AtomicBody->isInstantiationDependent()) {
11094         ErrorFound = NotAnAssignmentOp;
11095         ErrorLoc = AtomicBody->getExprLoc();
11096         ErrorRange = AtomicBody->getSourceRange();
11097         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
11098                               : AtomicBody->getExprLoc();
11099         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
11100                                 : AtomicBody->getSourceRange();
11101       }
11102     } else {
11103       ErrorFound = NotAnExpression;
11104       NoteLoc = ErrorLoc = Body->getBeginLoc();
11105       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
11106     }
11107     if (ErrorFound != NoError) {
11108       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
11109           << ErrorRange;
11110       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
11111                                                       << NoteRange;
11112       return StmtError();
11113     }
11114     if (CurContext->isDependentContext())
11115       E = X = nullptr;
11116   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
11117     // If clause is update:
11118     //  x++;
11119     //  x--;
11120     //  ++x;
11121     //  --x;
11122     //  x binop= expr;
11123     //  x = x binop expr;
11124     //  x = expr binop x;
11125     OpenMPAtomicUpdateChecker Checker(*this);
11126     if (Checker.checkStatement(
11127             Body, (AtomicKind == OMPC_update)
11128                       ? diag::err_omp_atomic_update_not_expression_statement
11129                       : diag::err_omp_atomic_not_expression_statement,
11130             diag::note_omp_atomic_update))
11131       return StmtError();
11132     if (!CurContext->isDependentContext()) {
11133       E = Checker.getExpr();
11134       X = Checker.getX();
11135       UE = Checker.getUpdateExpr();
11136       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
11137     }
11138   } else if (AtomicKind == OMPC_capture) {
11139     enum {
11140       NotAnAssignmentOp,
11141       NotACompoundStatement,
11142       NotTwoSubstatements,
11143       NotASpecificExpression,
11144       NoError
11145     } ErrorFound = NoError;
11146     SourceLocation ErrorLoc, NoteLoc;
11147     SourceRange ErrorRange, NoteRange;
11148     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
11149       // If clause is a capture:
11150       //  v = x++;
11151       //  v = x--;
11152       //  v = ++x;
11153       //  v = --x;
11154       //  v = x binop= expr;
11155       //  v = x = x binop expr;
11156       //  v = x = expr binop x;
11157       const auto *AtomicBinOp =
11158           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
11159       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
11160         V = AtomicBinOp->getLHS();
11161         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
11162         OpenMPAtomicUpdateChecker Checker(*this);
11163         if (Checker.checkStatement(
11164                 Body, diag::err_omp_atomic_capture_not_expression_statement,
11165                 diag::note_omp_atomic_update))
11166           return StmtError();
11167         E = Checker.getExpr();
11168         X = Checker.getX();
11169         UE = Checker.getUpdateExpr();
11170         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
11171         IsPostfixUpdate = Checker.isPostfixUpdate();
11172       } else if (!AtomicBody->isInstantiationDependent()) {
11173         ErrorLoc = AtomicBody->getExprLoc();
11174         ErrorRange = AtomicBody->getSourceRange();
11175         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
11176                               : AtomicBody->getExprLoc();
11177         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
11178                                 : AtomicBody->getSourceRange();
11179         ErrorFound = NotAnAssignmentOp;
11180       }
11181       if (ErrorFound != NoError) {
11182         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
11183             << ErrorRange;
11184         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
11185         return StmtError();
11186       }
11187       if (CurContext->isDependentContext())
11188         UE = V = E = X = nullptr;
11189     } else {
11190       // If clause is a capture:
11191       //  { v = x; x = expr; }
11192       //  { v = x; x++; }
11193       //  { v = x; x--; }
11194       //  { v = x; ++x; }
11195       //  { v = x; --x; }
11196       //  { v = x; x binop= expr; }
11197       //  { v = x; x = x binop expr; }
11198       //  { v = x; x = expr binop x; }
11199       //  { x++; v = x; }
11200       //  { x--; v = x; }
11201       //  { ++x; v = x; }
11202       //  { --x; v = x; }
11203       //  { x binop= expr; v = x; }
11204       //  { x = x binop expr; v = x; }
11205       //  { x = expr binop x; v = x; }
11206       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
11207         // Check that this is { expr1; expr2; }
11208         if (CS->size() == 2) {
11209           Stmt *First = CS->body_front();
11210           Stmt *Second = CS->body_back();
11211           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
11212             First = EWC->getSubExpr()->IgnoreParenImpCasts();
11213           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
11214             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
11215           // Need to find what subexpression is 'v' and what is 'x'.
11216           OpenMPAtomicUpdateChecker Checker(*this);
11217           bool IsUpdateExprFound = !Checker.checkStatement(Second);
11218           BinaryOperator *BinOp = nullptr;
11219           if (IsUpdateExprFound) {
11220             BinOp = dyn_cast<BinaryOperator>(First);
11221             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
11222           }
11223           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
11224             //  { v = x; x++; }
11225             //  { v = x; x--; }
11226             //  { v = x; ++x; }
11227             //  { v = x; --x; }
11228             //  { v = x; x binop= expr; }
11229             //  { v = x; x = x binop expr; }
11230             //  { v = x; x = expr binop x; }
11231             // Check that the first expression has form v = x.
11232             Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
11233             llvm::FoldingSetNodeID XId, PossibleXId;
11234             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
11235             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
11236             IsUpdateExprFound = XId == PossibleXId;
11237             if (IsUpdateExprFound) {
11238               V = BinOp->getLHS();
11239               X = Checker.getX();
11240               E = Checker.getExpr();
11241               UE = Checker.getUpdateExpr();
11242               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
11243               IsPostfixUpdate = true;
11244             }
11245           }
11246           if (!IsUpdateExprFound) {
11247             IsUpdateExprFound = !Checker.checkStatement(First);
11248             BinOp = nullptr;
11249             if (IsUpdateExprFound) {
11250               BinOp = dyn_cast<BinaryOperator>(Second);
11251               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
11252             }
11253             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
11254               //  { x++; v = x; }
11255               //  { x--; v = x; }
11256               //  { ++x; v = x; }
11257               //  { --x; v = x; }
11258               //  { x binop= expr; v = x; }
11259               //  { x = x binop expr; v = x; }
11260               //  { x = expr binop x; v = x; }
11261               // Check that the second expression has form v = x.
11262               Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
11263               llvm::FoldingSetNodeID XId, PossibleXId;
11264               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
11265               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
11266               IsUpdateExprFound = XId == PossibleXId;
11267               if (IsUpdateExprFound) {
11268                 V = BinOp->getLHS();
11269                 X = Checker.getX();
11270                 E = Checker.getExpr();
11271                 UE = Checker.getUpdateExpr();
11272                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
11273                 IsPostfixUpdate = false;
11274               }
11275             }
11276           }
11277           if (!IsUpdateExprFound) {
11278             //  { v = x; x = expr; }
11279             auto *FirstExpr = dyn_cast<Expr>(First);
11280             auto *SecondExpr = dyn_cast<Expr>(Second);
11281             if (!FirstExpr || !SecondExpr ||
11282                 !(FirstExpr->isInstantiationDependent() ||
11283                   SecondExpr->isInstantiationDependent())) {
11284               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
11285               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
11286                 ErrorFound = NotAnAssignmentOp;
11287                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
11288                                                 : First->getBeginLoc();
11289                 NoteRange = ErrorRange = FirstBinOp
11290                                              ? FirstBinOp->getSourceRange()
11291                                              : SourceRange(ErrorLoc, ErrorLoc);
11292               } else {
11293                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
11294                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
11295                   ErrorFound = NotAnAssignmentOp;
11296                   NoteLoc = ErrorLoc = SecondBinOp
11297                                            ? SecondBinOp->getOperatorLoc()
11298                                            : Second->getBeginLoc();
11299                   NoteRange = ErrorRange =
11300                       SecondBinOp ? SecondBinOp->getSourceRange()
11301                                   : SourceRange(ErrorLoc, ErrorLoc);
11302                 } else {
11303                   Expr *PossibleXRHSInFirst =
11304                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
11305                   Expr *PossibleXLHSInSecond =
11306                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
11307                   llvm::FoldingSetNodeID X1Id, X2Id;
11308                   PossibleXRHSInFirst->Profile(X1Id, Context,
11309                                                /*Canonical=*/true);
11310                   PossibleXLHSInSecond->Profile(X2Id, Context,
11311                                                 /*Canonical=*/true);
11312                   IsUpdateExprFound = X1Id == X2Id;
11313                   if (IsUpdateExprFound) {
11314                     V = FirstBinOp->getLHS();
11315                     X = SecondBinOp->getLHS();
11316                     E = SecondBinOp->getRHS();
11317                     UE = nullptr;
11318                     IsXLHSInRHSPart = false;
11319                     IsPostfixUpdate = true;
11320                   } else {
11321                     ErrorFound = NotASpecificExpression;
11322                     ErrorLoc = FirstBinOp->getExprLoc();
11323                     ErrorRange = FirstBinOp->getSourceRange();
11324                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
11325                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
11326                   }
11327                 }
11328               }
11329             }
11330           }
11331         } else {
11332           NoteLoc = ErrorLoc = Body->getBeginLoc();
11333           NoteRange = ErrorRange =
11334               SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
11335           ErrorFound = NotTwoSubstatements;
11336         }
11337       } else {
11338         NoteLoc = ErrorLoc = Body->getBeginLoc();
11339         NoteRange = ErrorRange =
11340             SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
11341         ErrorFound = NotACompoundStatement;
11342       }
11343       if (ErrorFound != NoError) {
11344         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
11345             << ErrorRange;
11346         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
11347         return StmtError();
11348       }
11349       if (CurContext->isDependentContext())
11350         UE = V = E = X = nullptr;
11351     }
11352   }
11353 
11354   setFunctionHasBranchProtectedScope();
11355 
11356   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
11357                                     X, V, E, UE, IsXLHSInRHSPart,
11358                                     IsPostfixUpdate);
11359 }
11360 
11361 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
11362                                             Stmt *AStmt,
11363                                             SourceLocation StartLoc,
11364                                             SourceLocation EndLoc) {
11365   if (!AStmt)
11366     return StmtError();
11367 
11368   auto *CS = cast<CapturedStmt>(AStmt);
11369   // 1.2.2 OpenMP Language Terminology
11370   // Structured block - An executable statement with a single entry at the
11371   // top and a single exit at the bottom.
11372   // The point of exit cannot be a branch out of the structured block.
11373   // longjmp() and throw() must not violate the entry/exit criteria.
11374   CS->getCapturedDecl()->setNothrow();
11375   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
11376        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11377     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11378     // 1.2.2 OpenMP Language Terminology
11379     // Structured block - An executable statement with a single entry at the
11380     // top and a single exit at the bottom.
11381     // The point of exit cannot be a branch out of the structured block.
11382     // longjmp() and throw() must not violate the entry/exit criteria.
11383     CS->getCapturedDecl()->setNothrow();
11384   }
11385 
11386   // OpenMP [2.16, Nesting of Regions]
11387   // If specified, a teams construct must be contained within a target
11388   // construct. That target construct must contain no statements or directives
11389   // outside of the teams construct.
11390   if (DSAStack->hasInnerTeamsRegion()) {
11391     const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
11392     bool OMPTeamsFound = true;
11393     if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
11394       auto I = CS->body_begin();
11395       while (I != CS->body_end()) {
11396         const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
11397         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
11398             OMPTeamsFound) {
11399 
11400           OMPTeamsFound = false;
11401           break;
11402         }
11403         ++I;
11404       }
11405       assert(I != CS->body_end() && "Not found statement");
11406       S = *I;
11407     } else {
11408       const auto *OED = dyn_cast<OMPExecutableDirective>(S);
11409       OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
11410     }
11411     if (!OMPTeamsFound) {
11412       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
11413       Diag(DSAStack->getInnerTeamsRegionLoc(),
11414            diag::note_omp_nested_teams_construct_here);
11415       Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
11416           << isa<OMPExecutableDirective>(S);
11417       return StmtError();
11418     }
11419   }
11420 
11421   setFunctionHasBranchProtectedScope();
11422 
11423   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
11424 }
11425 
11426 StmtResult
11427 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
11428                                          Stmt *AStmt, SourceLocation StartLoc,
11429                                          SourceLocation EndLoc) {
11430   if (!AStmt)
11431     return StmtError();
11432 
11433   auto *CS = cast<CapturedStmt>(AStmt);
11434   // 1.2.2 OpenMP Language Terminology
11435   // Structured block - An executable statement with a single entry at the
11436   // top and a single exit at the bottom.
11437   // The point of exit cannot be a branch out of the structured block.
11438   // longjmp() and throw() must not violate the entry/exit criteria.
11439   CS->getCapturedDecl()->setNothrow();
11440   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
11441        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11442     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11443     // 1.2.2 OpenMP Language Terminology
11444     // Structured block - An executable statement with a single entry at the
11445     // top and a single exit at the bottom.
11446     // The point of exit cannot be a branch out of the structured block.
11447     // longjmp() and throw() must not violate the entry/exit criteria.
11448     CS->getCapturedDecl()->setNothrow();
11449   }
11450 
11451   setFunctionHasBranchProtectedScope();
11452 
11453   return OMPTargetParallelDirective::Create(
11454       Context, StartLoc, EndLoc, Clauses, AStmt,
11455       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11456 }
11457 
11458 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
11459     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11460     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11461   if (!AStmt)
11462     return StmtError();
11463 
11464   auto *CS = cast<CapturedStmt>(AStmt);
11465   // 1.2.2 OpenMP Language Terminology
11466   // Structured block - An executable statement with a single entry at the
11467   // top and a single exit at the bottom.
11468   // The point of exit cannot be a branch out of the structured block.
11469   // longjmp() and throw() must not violate the entry/exit criteria.
11470   CS->getCapturedDecl()->setNothrow();
11471   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
11472        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11473     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11474     // 1.2.2 OpenMP Language Terminology
11475     // Structured block - An executable statement with a single entry at the
11476     // top and a single exit at the bottom.
11477     // The point of exit cannot be a branch out of the structured block.
11478     // longjmp() and throw() must not violate the entry/exit criteria.
11479     CS->getCapturedDecl()->setNothrow();
11480   }
11481 
11482   OMPLoopBasedDirective::HelperExprs B;
11483   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11484   // define the nested loops number.
11485   unsigned NestedLoopCount =
11486       checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
11487                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
11488                       VarsWithImplicitDSA, B);
11489   if (NestedLoopCount == 0)
11490     return StmtError();
11491 
11492   assert((CurContext->isDependentContext() || B.builtAll()) &&
11493          "omp target parallel for loop exprs were not built");
11494 
11495   if (!CurContext->isDependentContext()) {
11496     // Finalize the clauses that need pre-built expressions for CodeGen.
11497     for (OMPClause *C : Clauses) {
11498       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11499         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11500                                      B.NumIterations, *this, CurScope,
11501                                      DSAStack))
11502           return StmtError();
11503     }
11504   }
11505 
11506   setFunctionHasBranchProtectedScope();
11507   return OMPTargetParallelForDirective::Create(
11508       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
11509       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11510 }
11511 
11512 /// Check for existence of a map clause in the list of clauses.
11513 static bool hasClauses(ArrayRef<OMPClause *> Clauses,
11514                        const OpenMPClauseKind K) {
11515   return llvm::any_of(
11516       Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
11517 }
11518 
11519 template <typename... Params>
11520 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
11521                        const Params... ClauseTypes) {
11522   return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
11523 }
11524 
11525 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
11526                                                 Stmt *AStmt,
11527                                                 SourceLocation StartLoc,
11528                                                 SourceLocation EndLoc) {
11529   if (!AStmt)
11530     return StmtError();
11531 
11532   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11533 
11534   // OpenMP [2.12.2, target data Construct, Restrictions]
11535   // At least one map, use_device_addr or use_device_ptr clause must appear on
11536   // the directive.
11537   if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) &&
11538       (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) {
11539     StringRef Expected;
11540     if (LangOpts.OpenMP < 50)
11541       Expected = "'map' or 'use_device_ptr'";
11542     else
11543       Expected = "'map', 'use_device_ptr', or 'use_device_addr'";
11544     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
11545         << Expected << getOpenMPDirectiveName(OMPD_target_data);
11546     return StmtError();
11547   }
11548 
11549   setFunctionHasBranchProtectedScope();
11550 
11551   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
11552                                         AStmt);
11553 }
11554 
11555 StmtResult
11556 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
11557                                           SourceLocation StartLoc,
11558                                           SourceLocation EndLoc, Stmt *AStmt) {
11559   if (!AStmt)
11560     return StmtError();
11561 
11562   auto *CS = cast<CapturedStmt>(AStmt);
11563   // 1.2.2 OpenMP Language Terminology
11564   // Structured block - An executable statement with a single entry at the
11565   // top and a single exit at the bottom.
11566   // The point of exit cannot be a branch out of the structured block.
11567   // longjmp() and throw() must not violate the entry/exit criteria.
11568   CS->getCapturedDecl()->setNothrow();
11569   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
11570        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11571     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11572     // 1.2.2 OpenMP Language Terminology
11573     // Structured block - An executable statement with a single entry at the
11574     // top and a single exit at the bottom.
11575     // The point of exit cannot be a branch out of the structured block.
11576     // longjmp() and throw() must not violate the entry/exit criteria.
11577     CS->getCapturedDecl()->setNothrow();
11578   }
11579 
11580   // OpenMP [2.10.2, Restrictions, p. 99]
11581   // At least one map clause must appear on the directive.
11582   if (!hasClauses(Clauses, OMPC_map)) {
11583     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
11584         << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
11585     return StmtError();
11586   }
11587 
11588   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
11589                                              AStmt);
11590 }
11591 
11592 StmtResult
11593 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
11594                                          SourceLocation StartLoc,
11595                                          SourceLocation EndLoc, Stmt *AStmt) {
11596   if (!AStmt)
11597     return StmtError();
11598 
11599   auto *CS = cast<CapturedStmt>(AStmt);
11600   // 1.2.2 OpenMP Language Terminology
11601   // Structured block - An executable statement with a single entry at the
11602   // top and a single exit at the bottom.
11603   // The point of exit cannot be a branch out of the structured block.
11604   // longjmp() and throw() must not violate the entry/exit criteria.
11605   CS->getCapturedDecl()->setNothrow();
11606   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
11607        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11608     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11609     // 1.2.2 OpenMP Language Terminology
11610     // Structured block - An executable statement with a single entry at the
11611     // top and a single exit at the bottom.
11612     // The point of exit cannot be a branch out of the structured block.
11613     // longjmp() and throw() must not violate the entry/exit criteria.
11614     CS->getCapturedDecl()->setNothrow();
11615   }
11616 
11617   // OpenMP [2.10.3, Restrictions, p. 102]
11618   // At least one map clause must appear on the directive.
11619   if (!hasClauses(Clauses, OMPC_map)) {
11620     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
11621         << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
11622     return StmtError();
11623   }
11624 
11625   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
11626                                             AStmt);
11627 }
11628 
11629 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
11630                                                   SourceLocation StartLoc,
11631                                                   SourceLocation EndLoc,
11632                                                   Stmt *AStmt) {
11633   if (!AStmt)
11634     return StmtError();
11635 
11636   auto *CS = cast<CapturedStmt>(AStmt);
11637   // 1.2.2 OpenMP Language Terminology
11638   // Structured block - An executable statement with a single entry at the
11639   // top and a single exit at the bottom.
11640   // The point of exit cannot be a branch out of the structured block.
11641   // longjmp() and throw() must not violate the entry/exit criteria.
11642   CS->getCapturedDecl()->setNothrow();
11643   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
11644        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11645     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11646     // 1.2.2 OpenMP Language Terminology
11647     // Structured block - An executable statement with a single entry at the
11648     // top and a single exit at the bottom.
11649     // The point of exit cannot be a branch out of the structured block.
11650     // longjmp() and throw() must not violate the entry/exit criteria.
11651     CS->getCapturedDecl()->setNothrow();
11652   }
11653 
11654   if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
11655     Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
11656     return StmtError();
11657   }
11658   return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
11659                                           AStmt);
11660 }
11661 
11662 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
11663                                            Stmt *AStmt, SourceLocation StartLoc,
11664                                            SourceLocation EndLoc) {
11665   if (!AStmt)
11666     return StmtError();
11667 
11668   auto *CS = cast<CapturedStmt>(AStmt);
11669   // 1.2.2 OpenMP Language Terminology
11670   // Structured block - An executable statement with a single entry at the
11671   // top and a single exit at the bottom.
11672   // The point of exit cannot be a branch out of the structured block.
11673   // longjmp() and throw() must not violate the entry/exit criteria.
11674   CS->getCapturedDecl()->setNothrow();
11675 
11676   setFunctionHasBranchProtectedScope();
11677 
11678   DSAStack->setParentTeamsRegionLoc(StartLoc);
11679 
11680   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
11681 }
11682 
11683 StmtResult
11684 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
11685                                             SourceLocation EndLoc,
11686                                             OpenMPDirectiveKind CancelRegion) {
11687   if (DSAStack->isParentNowaitRegion()) {
11688     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
11689     return StmtError();
11690   }
11691   if (DSAStack->isParentOrderedRegion()) {
11692     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
11693     return StmtError();
11694   }
11695   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
11696                                                CancelRegion);
11697 }
11698 
11699 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
11700                                             SourceLocation StartLoc,
11701                                             SourceLocation EndLoc,
11702                                             OpenMPDirectiveKind CancelRegion) {
11703   if (DSAStack->isParentNowaitRegion()) {
11704     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
11705     return StmtError();
11706   }
11707   if (DSAStack->isParentOrderedRegion()) {
11708     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
11709     return StmtError();
11710   }
11711   DSAStack->setParentCancelRegion(/*Cancel=*/true);
11712   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
11713                                     CancelRegion);
11714 }
11715 
11716 static bool checkReductionClauseWithNogroup(Sema &S,
11717                                             ArrayRef<OMPClause *> Clauses) {
11718   const OMPClause *ReductionClause = nullptr;
11719   const OMPClause *NogroupClause = nullptr;
11720   for (const OMPClause *C : Clauses) {
11721     if (C->getClauseKind() == OMPC_reduction) {
11722       ReductionClause = C;
11723       if (NogroupClause)
11724         break;
11725       continue;
11726     }
11727     if (C->getClauseKind() == OMPC_nogroup) {
11728       NogroupClause = C;
11729       if (ReductionClause)
11730         break;
11731       continue;
11732     }
11733   }
11734   if (ReductionClause && NogroupClause) {
11735     S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
11736         << SourceRange(NogroupClause->getBeginLoc(),
11737                        NogroupClause->getEndLoc());
11738     return true;
11739   }
11740   return false;
11741 }
11742 
11743 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
11744     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11745     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11746   if (!AStmt)
11747     return StmtError();
11748 
11749   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11750   OMPLoopBasedDirective::HelperExprs B;
11751   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11752   // define the nested loops number.
11753   unsigned NestedLoopCount =
11754       checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
11755                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
11756                       VarsWithImplicitDSA, B);
11757   if (NestedLoopCount == 0)
11758     return StmtError();
11759 
11760   assert((CurContext->isDependentContext() || B.builtAll()) &&
11761          "omp for loop exprs were not built");
11762 
11763   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11764   // The grainsize clause and num_tasks clause are mutually exclusive and may
11765   // not appear on the same taskloop directive.
11766   if (checkMutuallyExclusiveClauses(*this, Clauses,
11767                                     {OMPC_grainsize, OMPC_num_tasks}))
11768     return StmtError();
11769   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11770   // If a reduction clause is present on the taskloop directive, the nogroup
11771   // clause must not be specified.
11772   if (checkReductionClauseWithNogroup(*this, Clauses))
11773     return StmtError();
11774 
11775   setFunctionHasBranchProtectedScope();
11776   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
11777                                       NestedLoopCount, Clauses, AStmt, B,
11778                                       DSAStack->isCancelRegion());
11779 }
11780 
11781 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
11782     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11783     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11784   if (!AStmt)
11785     return StmtError();
11786 
11787   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11788   OMPLoopBasedDirective::HelperExprs B;
11789   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11790   // define the nested loops number.
11791   unsigned NestedLoopCount =
11792       checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
11793                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
11794                       VarsWithImplicitDSA, B);
11795   if (NestedLoopCount == 0)
11796     return StmtError();
11797 
11798   assert((CurContext->isDependentContext() || B.builtAll()) &&
11799          "omp for loop exprs were not built");
11800 
11801   if (!CurContext->isDependentContext()) {
11802     // Finalize the clauses that need pre-built expressions for CodeGen.
11803     for (OMPClause *C : Clauses) {
11804       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11805         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11806                                      B.NumIterations, *this, CurScope,
11807                                      DSAStack))
11808           return StmtError();
11809     }
11810   }
11811 
11812   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11813   // The grainsize clause and num_tasks clause are mutually exclusive and may
11814   // not appear on the same taskloop directive.
11815   if (checkMutuallyExclusiveClauses(*this, Clauses,
11816                                     {OMPC_grainsize, OMPC_num_tasks}))
11817     return StmtError();
11818   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11819   // If a reduction clause is present on the taskloop directive, the nogroup
11820   // clause must not be specified.
11821   if (checkReductionClauseWithNogroup(*this, Clauses))
11822     return StmtError();
11823   if (checkSimdlenSafelenSpecified(*this, Clauses))
11824     return StmtError();
11825 
11826   setFunctionHasBranchProtectedScope();
11827   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
11828                                           NestedLoopCount, Clauses, AStmt, B);
11829 }
11830 
11831 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective(
11832     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11833     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11834   if (!AStmt)
11835     return StmtError();
11836 
11837   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11838   OMPLoopBasedDirective::HelperExprs B;
11839   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11840   // define the nested loops number.
11841   unsigned NestedLoopCount =
11842       checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses),
11843                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
11844                       VarsWithImplicitDSA, B);
11845   if (NestedLoopCount == 0)
11846     return StmtError();
11847 
11848   assert((CurContext->isDependentContext() || B.builtAll()) &&
11849          "omp for loop exprs were not built");
11850 
11851   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11852   // The grainsize clause and num_tasks clause are mutually exclusive and may
11853   // not appear on the same taskloop directive.
11854   if (checkMutuallyExclusiveClauses(*this, Clauses,
11855                                     {OMPC_grainsize, OMPC_num_tasks}))
11856     return StmtError();
11857   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11858   // If a reduction clause is present on the taskloop directive, the nogroup
11859   // clause must not be specified.
11860   if (checkReductionClauseWithNogroup(*this, Clauses))
11861     return StmtError();
11862 
11863   setFunctionHasBranchProtectedScope();
11864   return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc,
11865                                             NestedLoopCount, Clauses, AStmt, B,
11866                                             DSAStack->isCancelRegion());
11867 }
11868 
11869 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective(
11870     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11871     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11872   if (!AStmt)
11873     return StmtError();
11874 
11875   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11876   OMPLoopBasedDirective::HelperExprs B;
11877   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11878   // define the nested loops number.
11879   unsigned NestedLoopCount =
11880       checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses),
11881                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
11882                       VarsWithImplicitDSA, B);
11883   if (NestedLoopCount == 0)
11884     return StmtError();
11885 
11886   assert((CurContext->isDependentContext() || B.builtAll()) &&
11887          "omp for loop exprs were not built");
11888 
11889   if (!CurContext->isDependentContext()) {
11890     // Finalize the clauses that need pre-built expressions for CodeGen.
11891     for (OMPClause *C : Clauses) {
11892       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11893         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11894                                      B.NumIterations, *this, CurScope,
11895                                      DSAStack))
11896           return StmtError();
11897     }
11898   }
11899 
11900   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11901   // The grainsize clause and num_tasks clause are mutually exclusive and may
11902   // not appear on the same taskloop directive.
11903   if (checkMutuallyExclusiveClauses(*this, Clauses,
11904                                     {OMPC_grainsize, OMPC_num_tasks}))
11905     return StmtError();
11906   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11907   // If a reduction clause is present on the taskloop directive, the nogroup
11908   // clause must not be specified.
11909   if (checkReductionClauseWithNogroup(*this, Clauses))
11910     return StmtError();
11911   if (checkSimdlenSafelenSpecified(*this, Clauses))
11912     return StmtError();
11913 
11914   setFunctionHasBranchProtectedScope();
11915   return OMPMasterTaskLoopSimdDirective::Create(
11916       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
11917 }
11918 
11919 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective(
11920     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11921     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11922   if (!AStmt)
11923     return StmtError();
11924 
11925   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11926   auto *CS = cast<CapturedStmt>(AStmt);
11927   // 1.2.2 OpenMP Language Terminology
11928   // Structured block - An executable statement with a single entry at the
11929   // top and a single exit at the bottom.
11930   // The point of exit cannot be a branch out of the structured block.
11931   // longjmp() and throw() must not violate the entry/exit criteria.
11932   CS->getCapturedDecl()->setNothrow();
11933   for (int ThisCaptureLevel =
11934            getOpenMPCaptureLevels(OMPD_parallel_master_taskloop);
11935        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11936     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11937     // 1.2.2 OpenMP Language Terminology
11938     // Structured block - An executable statement with a single entry at the
11939     // top and a single exit at the bottom.
11940     // The point of exit cannot be a branch out of the structured block.
11941     // longjmp() and throw() must not violate the entry/exit criteria.
11942     CS->getCapturedDecl()->setNothrow();
11943   }
11944 
11945   OMPLoopBasedDirective::HelperExprs B;
11946   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11947   // define the nested loops number.
11948   unsigned NestedLoopCount = checkOpenMPLoop(
11949       OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses),
11950       /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack,
11951       VarsWithImplicitDSA, B);
11952   if (NestedLoopCount == 0)
11953     return StmtError();
11954 
11955   assert((CurContext->isDependentContext() || B.builtAll()) &&
11956          "omp for loop exprs were not built");
11957 
11958   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11959   // The grainsize clause and num_tasks clause are mutually exclusive and may
11960   // not appear on the same taskloop directive.
11961   if (checkMutuallyExclusiveClauses(*this, Clauses,
11962                                     {OMPC_grainsize, OMPC_num_tasks}))
11963     return StmtError();
11964   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
11965   // If a reduction clause is present on the taskloop directive, the nogroup
11966   // clause must not be specified.
11967   if (checkReductionClauseWithNogroup(*this, Clauses))
11968     return StmtError();
11969 
11970   setFunctionHasBranchProtectedScope();
11971   return OMPParallelMasterTaskLoopDirective::Create(
11972       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
11973       DSAStack->isCancelRegion());
11974 }
11975 
11976 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective(
11977     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11978     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11979   if (!AStmt)
11980     return StmtError();
11981 
11982   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11983   auto *CS = cast<CapturedStmt>(AStmt);
11984   // 1.2.2 OpenMP Language Terminology
11985   // Structured block - An executable statement with a single entry at the
11986   // top and a single exit at the bottom.
11987   // The point of exit cannot be a branch out of the structured block.
11988   // longjmp() and throw() must not violate the entry/exit criteria.
11989   CS->getCapturedDecl()->setNothrow();
11990   for (int ThisCaptureLevel =
11991            getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd);
11992        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11993     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11994     // 1.2.2 OpenMP Language Terminology
11995     // Structured block - An executable statement with a single entry at the
11996     // top and a single exit at the bottom.
11997     // The point of exit cannot be a branch out of the structured block.
11998     // longjmp() and throw() must not violate the entry/exit criteria.
11999     CS->getCapturedDecl()->setNothrow();
12000   }
12001 
12002   OMPLoopBasedDirective::HelperExprs B;
12003   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
12004   // define the nested loops number.
12005   unsigned NestedLoopCount = checkOpenMPLoop(
12006       OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses),
12007       /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack,
12008       VarsWithImplicitDSA, B);
12009   if (NestedLoopCount == 0)
12010     return StmtError();
12011 
12012   assert((CurContext->isDependentContext() || B.builtAll()) &&
12013          "omp for loop exprs were not built");
12014 
12015   if (!CurContext->isDependentContext()) {
12016     // Finalize the clauses that need pre-built expressions for CodeGen.
12017     for (OMPClause *C : Clauses) {
12018       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12019         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12020                                      B.NumIterations, *this, CurScope,
12021                                      DSAStack))
12022           return StmtError();
12023     }
12024   }
12025 
12026   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
12027   // The grainsize clause and num_tasks clause are mutually exclusive and may
12028   // not appear on the same taskloop directive.
12029   if (checkMutuallyExclusiveClauses(*this, Clauses,
12030                                     {OMPC_grainsize, OMPC_num_tasks}))
12031     return StmtError();
12032   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
12033   // If a reduction clause is present on the taskloop directive, the nogroup
12034   // clause must not be specified.
12035   if (checkReductionClauseWithNogroup(*this, Clauses))
12036     return StmtError();
12037   if (checkSimdlenSafelenSpecified(*this, Clauses))
12038     return StmtError();
12039 
12040   setFunctionHasBranchProtectedScope();
12041   return OMPParallelMasterTaskLoopSimdDirective::Create(
12042       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12043 }
12044 
12045 StmtResult Sema::ActOnOpenMPDistributeDirective(
12046     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12047     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12048   if (!AStmt)
12049     return StmtError();
12050 
12051   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
12052   OMPLoopBasedDirective::HelperExprs B;
12053   // In presence of clause 'collapse' with number of loops, it will
12054   // define the nested loops number.
12055   unsigned NestedLoopCount =
12056       checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
12057                       nullptr /*ordered not a clause on distribute*/, AStmt,
12058                       *this, *DSAStack, VarsWithImplicitDSA, B);
12059   if (NestedLoopCount == 0)
12060     return StmtError();
12061 
12062   assert((CurContext->isDependentContext() || B.builtAll()) &&
12063          "omp for loop exprs were not built");
12064 
12065   setFunctionHasBranchProtectedScope();
12066   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
12067                                         NestedLoopCount, Clauses, AStmt, B);
12068 }
12069 
12070 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
12071     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12072     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12073   if (!AStmt)
12074     return StmtError();
12075 
12076   auto *CS = cast<CapturedStmt>(AStmt);
12077   // 1.2.2 OpenMP Language Terminology
12078   // Structured block - An executable statement with a single entry at the
12079   // top and a single exit at the bottom.
12080   // The point of exit cannot be a branch out of the structured block.
12081   // longjmp() and throw() must not violate the entry/exit criteria.
12082   CS->getCapturedDecl()->setNothrow();
12083   for (int ThisCaptureLevel =
12084            getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
12085        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12086     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12087     // 1.2.2 OpenMP Language Terminology
12088     // Structured block - An executable statement with a single entry at the
12089     // top and a single exit at the bottom.
12090     // The point of exit cannot be a branch out of the structured block.
12091     // longjmp() and throw() must not violate the entry/exit criteria.
12092     CS->getCapturedDecl()->setNothrow();
12093   }
12094 
12095   OMPLoopBasedDirective::HelperExprs B;
12096   // In presence of clause 'collapse' with number of loops, it will
12097   // define the nested loops number.
12098   unsigned NestedLoopCount = checkOpenMPLoop(
12099       OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
12100       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12101       VarsWithImplicitDSA, B);
12102   if (NestedLoopCount == 0)
12103     return StmtError();
12104 
12105   assert((CurContext->isDependentContext() || B.builtAll()) &&
12106          "omp for loop exprs were not built");
12107 
12108   setFunctionHasBranchProtectedScope();
12109   return OMPDistributeParallelForDirective::Create(
12110       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
12111       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
12112 }
12113 
12114 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
12115     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12116     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12117   if (!AStmt)
12118     return StmtError();
12119 
12120   auto *CS = cast<CapturedStmt>(AStmt);
12121   // 1.2.2 OpenMP Language Terminology
12122   // Structured block - An executable statement with a single entry at the
12123   // top and a single exit at the bottom.
12124   // The point of exit cannot be a branch out of the structured block.
12125   // longjmp() and throw() must not violate the entry/exit criteria.
12126   CS->getCapturedDecl()->setNothrow();
12127   for (int ThisCaptureLevel =
12128            getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
12129        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12130     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12131     // 1.2.2 OpenMP Language Terminology
12132     // Structured block - An executable statement with a single entry at the
12133     // top and a single exit at the bottom.
12134     // The point of exit cannot be a branch out of the structured block.
12135     // longjmp() and throw() must not violate the entry/exit criteria.
12136     CS->getCapturedDecl()->setNothrow();
12137   }
12138 
12139   OMPLoopBasedDirective::HelperExprs B;
12140   // In presence of clause 'collapse' with number of loops, it will
12141   // define the nested loops number.
12142   unsigned NestedLoopCount = checkOpenMPLoop(
12143       OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
12144       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12145       VarsWithImplicitDSA, B);
12146   if (NestedLoopCount == 0)
12147     return StmtError();
12148 
12149   assert((CurContext->isDependentContext() || B.builtAll()) &&
12150          "omp for loop exprs were not built");
12151 
12152   if (!CurContext->isDependentContext()) {
12153     // Finalize the clauses that need pre-built expressions for CodeGen.
12154     for (OMPClause *C : Clauses) {
12155       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12156         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12157                                      B.NumIterations, *this, CurScope,
12158                                      DSAStack))
12159           return StmtError();
12160     }
12161   }
12162 
12163   if (checkSimdlenSafelenSpecified(*this, Clauses))
12164     return StmtError();
12165 
12166   setFunctionHasBranchProtectedScope();
12167   return OMPDistributeParallelForSimdDirective::Create(
12168       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12169 }
12170 
12171 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
12172     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12173     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12174   if (!AStmt)
12175     return StmtError();
12176 
12177   auto *CS = cast<CapturedStmt>(AStmt);
12178   // 1.2.2 OpenMP Language Terminology
12179   // Structured block - An executable statement with a single entry at the
12180   // top and a single exit at the bottom.
12181   // The point of exit cannot be a branch out of the structured block.
12182   // longjmp() and throw() must not violate the entry/exit criteria.
12183   CS->getCapturedDecl()->setNothrow();
12184   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
12185        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12186     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12187     // 1.2.2 OpenMP Language Terminology
12188     // Structured block - An executable statement with a single entry at the
12189     // top and a single exit at the bottom.
12190     // The point of exit cannot be a branch out of the structured block.
12191     // longjmp() and throw() must not violate the entry/exit criteria.
12192     CS->getCapturedDecl()->setNothrow();
12193   }
12194 
12195   OMPLoopBasedDirective::HelperExprs B;
12196   // In presence of clause 'collapse' with number of loops, it will
12197   // define the nested loops number.
12198   unsigned NestedLoopCount =
12199       checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
12200                       nullptr /*ordered not a clause on distribute*/, CS, *this,
12201                       *DSAStack, VarsWithImplicitDSA, B);
12202   if (NestedLoopCount == 0)
12203     return StmtError();
12204 
12205   assert((CurContext->isDependentContext() || B.builtAll()) &&
12206          "omp for loop exprs were not built");
12207 
12208   if (!CurContext->isDependentContext()) {
12209     // Finalize the clauses that need pre-built expressions for CodeGen.
12210     for (OMPClause *C : Clauses) {
12211       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12212         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12213                                      B.NumIterations, *this, CurScope,
12214                                      DSAStack))
12215           return StmtError();
12216     }
12217   }
12218 
12219   if (checkSimdlenSafelenSpecified(*this, Clauses))
12220     return StmtError();
12221 
12222   setFunctionHasBranchProtectedScope();
12223   return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
12224                                             NestedLoopCount, Clauses, AStmt, B);
12225 }
12226 
12227 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
12228     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12229     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12230   if (!AStmt)
12231     return StmtError();
12232 
12233   auto *CS = cast<CapturedStmt>(AStmt);
12234   // 1.2.2 OpenMP Language Terminology
12235   // Structured block - An executable statement with a single entry at the
12236   // top and a single exit at the bottom.
12237   // The point of exit cannot be a branch out of the structured block.
12238   // longjmp() and throw() must not violate the entry/exit criteria.
12239   CS->getCapturedDecl()->setNothrow();
12240   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
12241        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12242     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12243     // 1.2.2 OpenMP Language Terminology
12244     // Structured block - An executable statement with a single entry at the
12245     // top and a single exit at the bottom.
12246     // The point of exit cannot be a branch out of the structured block.
12247     // longjmp() and throw() must not violate the entry/exit criteria.
12248     CS->getCapturedDecl()->setNothrow();
12249   }
12250 
12251   OMPLoopBasedDirective::HelperExprs B;
12252   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
12253   // define the nested loops number.
12254   unsigned NestedLoopCount = checkOpenMPLoop(
12255       OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
12256       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
12257       VarsWithImplicitDSA, B);
12258   if (NestedLoopCount == 0)
12259     return StmtError();
12260 
12261   assert((CurContext->isDependentContext() || B.builtAll()) &&
12262          "omp target parallel for simd loop exprs were not built");
12263 
12264   if (!CurContext->isDependentContext()) {
12265     // Finalize the clauses that need pre-built expressions for CodeGen.
12266     for (OMPClause *C : Clauses) {
12267       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12268         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12269                                      B.NumIterations, *this, CurScope,
12270                                      DSAStack))
12271           return StmtError();
12272     }
12273   }
12274   if (checkSimdlenSafelenSpecified(*this, Clauses))
12275     return StmtError();
12276 
12277   setFunctionHasBranchProtectedScope();
12278   return OMPTargetParallelForSimdDirective::Create(
12279       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12280 }
12281 
12282 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
12283     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12284     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12285   if (!AStmt)
12286     return StmtError();
12287 
12288   auto *CS = cast<CapturedStmt>(AStmt);
12289   // 1.2.2 OpenMP Language Terminology
12290   // Structured block - An executable statement with a single entry at the
12291   // top and a single exit at the bottom.
12292   // The point of exit cannot be a branch out of the structured block.
12293   // longjmp() and throw() must not violate the entry/exit criteria.
12294   CS->getCapturedDecl()->setNothrow();
12295   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
12296        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12297     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12298     // 1.2.2 OpenMP Language Terminology
12299     // Structured block - An executable statement with a single entry at the
12300     // top and a single exit at the bottom.
12301     // The point of exit cannot be a branch out of the structured block.
12302     // longjmp() and throw() must not violate the entry/exit criteria.
12303     CS->getCapturedDecl()->setNothrow();
12304   }
12305 
12306   OMPLoopBasedDirective::HelperExprs B;
12307   // In presence of clause 'collapse' with number of loops, it will define the
12308   // nested loops number.
12309   unsigned NestedLoopCount =
12310       checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
12311                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
12312                       VarsWithImplicitDSA, B);
12313   if (NestedLoopCount == 0)
12314     return StmtError();
12315 
12316   assert((CurContext->isDependentContext() || B.builtAll()) &&
12317          "omp target simd loop exprs were not built");
12318 
12319   if (!CurContext->isDependentContext()) {
12320     // Finalize the clauses that need pre-built expressions for CodeGen.
12321     for (OMPClause *C : Clauses) {
12322       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12323         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12324                                      B.NumIterations, *this, CurScope,
12325                                      DSAStack))
12326           return StmtError();
12327     }
12328   }
12329 
12330   if (checkSimdlenSafelenSpecified(*this, Clauses))
12331     return StmtError();
12332 
12333   setFunctionHasBranchProtectedScope();
12334   return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
12335                                         NestedLoopCount, Clauses, AStmt, B);
12336 }
12337 
12338 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
12339     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12340     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12341   if (!AStmt)
12342     return StmtError();
12343 
12344   auto *CS = cast<CapturedStmt>(AStmt);
12345   // 1.2.2 OpenMP Language Terminology
12346   // Structured block - An executable statement with a single entry at the
12347   // top and a single exit at the bottom.
12348   // The point of exit cannot be a branch out of the structured block.
12349   // longjmp() and throw() must not violate the entry/exit criteria.
12350   CS->getCapturedDecl()->setNothrow();
12351   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
12352        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12353     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12354     // 1.2.2 OpenMP Language Terminology
12355     // Structured block - An executable statement with a single entry at the
12356     // top and a single exit at the bottom.
12357     // The point of exit cannot be a branch out of the structured block.
12358     // longjmp() and throw() must not violate the entry/exit criteria.
12359     CS->getCapturedDecl()->setNothrow();
12360   }
12361 
12362   OMPLoopBasedDirective::HelperExprs B;
12363   // In presence of clause 'collapse' with number of loops, it will
12364   // define the nested loops number.
12365   unsigned NestedLoopCount =
12366       checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
12367                       nullptr /*ordered not a clause on distribute*/, CS, *this,
12368                       *DSAStack, VarsWithImplicitDSA, B);
12369   if (NestedLoopCount == 0)
12370     return StmtError();
12371 
12372   assert((CurContext->isDependentContext() || B.builtAll()) &&
12373          "omp teams distribute loop exprs were not built");
12374 
12375   setFunctionHasBranchProtectedScope();
12376 
12377   DSAStack->setParentTeamsRegionLoc(StartLoc);
12378 
12379   return OMPTeamsDistributeDirective::Create(
12380       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12381 }
12382 
12383 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
12384     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12385     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12386   if (!AStmt)
12387     return StmtError();
12388 
12389   auto *CS = cast<CapturedStmt>(AStmt);
12390   // 1.2.2 OpenMP Language Terminology
12391   // Structured block - An executable statement with a single entry at the
12392   // top and a single exit at the bottom.
12393   // The point of exit cannot be a branch out of the structured block.
12394   // longjmp() and throw() must not violate the entry/exit criteria.
12395   CS->getCapturedDecl()->setNothrow();
12396   for (int ThisCaptureLevel =
12397            getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
12398        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12399     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12400     // 1.2.2 OpenMP Language Terminology
12401     // Structured block - An executable statement with a single entry at the
12402     // top and a single exit at the bottom.
12403     // The point of exit cannot be a branch out of the structured block.
12404     // longjmp() and throw() must not violate the entry/exit criteria.
12405     CS->getCapturedDecl()->setNothrow();
12406   }
12407 
12408   OMPLoopBasedDirective::HelperExprs B;
12409   // In presence of clause 'collapse' with number of loops, it will
12410   // define the nested loops number.
12411   unsigned NestedLoopCount = checkOpenMPLoop(
12412       OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
12413       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12414       VarsWithImplicitDSA, B);
12415 
12416   if (NestedLoopCount == 0)
12417     return StmtError();
12418 
12419   assert((CurContext->isDependentContext() || B.builtAll()) &&
12420          "omp teams distribute simd loop exprs were not built");
12421 
12422   if (!CurContext->isDependentContext()) {
12423     // Finalize the clauses that need pre-built expressions for CodeGen.
12424     for (OMPClause *C : Clauses) {
12425       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12426         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12427                                      B.NumIterations, *this, CurScope,
12428                                      DSAStack))
12429           return StmtError();
12430     }
12431   }
12432 
12433   if (checkSimdlenSafelenSpecified(*this, Clauses))
12434     return StmtError();
12435 
12436   setFunctionHasBranchProtectedScope();
12437 
12438   DSAStack->setParentTeamsRegionLoc(StartLoc);
12439 
12440   return OMPTeamsDistributeSimdDirective::Create(
12441       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12442 }
12443 
12444 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
12445     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12446     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12447   if (!AStmt)
12448     return StmtError();
12449 
12450   auto *CS = cast<CapturedStmt>(AStmt);
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   for (int ThisCaptureLevel =
12459            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
12460        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12461     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12462     // 1.2.2 OpenMP Language Terminology
12463     // Structured block - An executable statement with a single entry at the
12464     // top and a single exit at the bottom.
12465     // The point of exit cannot be a branch out of the structured block.
12466     // longjmp() and throw() must not violate the entry/exit criteria.
12467     CS->getCapturedDecl()->setNothrow();
12468   }
12469 
12470   OMPLoopBasedDirective::HelperExprs B;
12471   // In presence of clause 'collapse' with number of loops, it will
12472   // define the nested loops number.
12473   unsigned NestedLoopCount = checkOpenMPLoop(
12474       OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
12475       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12476       VarsWithImplicitDSA, B);
12477 
12478   if (NestedLoopCount == 0)
12479     return StmtError();
12480 
12481   assert((CurContext->isDependentContext() || B.builtAll()) &&
12482          "omp for loop exprs were not built");
12483 
12484   if (!CurContext->isDependentContext()) {
12485     // Finalize the clauses that need pre-built expressions for CodeGen.
12486     for (OMPClause *C : Clauses) {
12487       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12488         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12489                                      B.NumIterations, *this, CurScope,
12490                                      DSAStack))
12491           return StmtError();
12492     }
12493   }
12494 
12495   if (checkSimdlenSafelenSpecified(*this, Clauses))
12496     return StmtError();
12497 
12498   setFunctionHasBranchProtectedScope();
12499 
12500   DSAStack->setParentTeamsRegionLoc(StartLoc);
12501 
12502   return OMPTeamsDistributeParallelForSimdDirective::Create(
12503       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12504 }
12505 
12506 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
12507     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12508     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12509   if (!AStmt)
12510     return StmtError();
12511 
12512   auto *CS = cast<CapturedStmt>(AStmt);
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   for (int ThisCaptureLevel =
12521            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
12522        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12523     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12524     // 1.2.2 OpenMP Language Terminology
12525     // Structured block - An executable statement with a single entry at the
12526     // top and a single exit at the bottom.
12527     // The point of exit cannot be a branch out of the structured block.
12528     // longjmp() and throw() must not violate the entry/exit criteria.
12529     CS->getCapturedDecl()->setNothrow();
12530   }
12531 
12532   OMPLoopBasedDirective::HelperExprs B;
12533   // In presence of clause 'collapse' with number of loops, it will
12534   // define the nested loops number.
12535   unsigned NestedLoopCount = checkOpenMPLoop(
12536       OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
12537       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12538       VarsWithImplicitDSA, B);
12539 
12540   if (NestedLoopCount == 0)
12541     return StmtError();
12542 
12543   assert((CurContext->isDependentContext() || B.builtAll()) &&
12544          "omp for loop exprs were not built");
12545 
12546   setFunctionHasBranchProtectedScope();
12547 
12548   DSAStack->setParentTeamsRegionLoc(StartLoc);
12549 
12550   return OMPTeamsDistributeParallelForDirective::Create(
12551       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
12552       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
12553 }
12554 
12555 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
12556                                                  Stmt *AStmt,
12557                                                  SourceLocation StartLoc,
12558                                                  SourceLocation EndLoc) {
12559   if (!AStmt)
12560     return StmtError();
12561 
12562   auto *CS = cast<CapturedStmt>(AStmt);
12563   // 1.2.2 OpenMP Language Terminology
12564   // Structured block - An executable statement with a single entry at the
12565   // top and a single exit at the bottom.
12566   // The point of exit cannot be a branch out of the structured block.
12567   // longjmp() and throw() must not violate the entry/exit criteria.
12568   CS->getCapturedDecl()->setNothrow();
12569 
12570   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
12571        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12572     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12573     // 1.2.2 OpenMP Language Terminology
12574     // Structured block - An executable statement with a single entry at the
12575     // top and a single exit at the bottom.
12576     // The point of exit cannot be a branch out of the structured block.
12577     // longjmp() and throw() must not violate the entry/exit criteria.
12578     CS->getCapturedDecl()->setNothrow();
12579   }
12580   setFunctionHasBranchProtectedScope();
12581 
12582   return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
12583                                          AStmt);
12584 }
12585 
12586 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
12587     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12588     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12589   if (!AStmt)
12590     return StmtError();
12591 
12592   auto *CS = cast<CapturedStmt>(AStmt);
12593   // 1.2.2 OpenMP Language Terminology
12594   // Structured block - An executable statement with a single entry at the
12595   // top and a single exit at the bottom.
12596   // The point of exit cannot be a branch out of the structured block.
12597   // longjmp() and throw() must not violate the entry/exit criteria.
12598   CS->getCapturedDecl()->setNothrow();
12599   for (int ThisCaptureLevel =
12600            getOpenMPCaptureLevels(OMPD_target_teams_distribute);
12601        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12602     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12603     // 1.2.2 OpenMP Language Terminology
12604     // Structured block - An executable statement with a single entry at the
12605     // top and a single exit at the bottom.
12606     // The point of exit cannot be a branch out of the structured block.
12607     // longjmp() and throw() must not violate the entry/exit criteria.
12608     CS->getCapturedDecl()->setNothrow();
12609   }
12610 
12611   OMPLoopBasedDirective::HelperExprs B;
12612   // In presence of clause 'collapse' with number of loops, it will
12613   // define the nested loops number.
12614   unsigned NestedLoopCount = checkOpenMPLoop(
12615       OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
12616       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12617       VarsWithImplicitDSA, B);
12618   if (NestedLoopCount == 0)
12619     return StmtError();
12620 
12621   assert((CurContext->isDependentContext() || B.builtAll()) &&
12622          "omp target teams distribute loop exprs were not built");
12623 
12624   setFunctionHasBranchProtectedScope();
12625   return OMPTargetTeamsDistributeDirective::Create(
12626       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12627 }
12628 
12629 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
12630     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12631     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12632   if (!AStmt)
12633     return StmtError();
12634 
12635   auto *CS = cast<CapturedStmt>(AStmt);
12636   // 1.2.2 OpenMP Language Terminology
12637   // Structured block - An executable statement with a single entry at the
12638   // top and a single exit at the bottom.
12639   // The point of exit cannot be a branch out of the structured block.
12640   // longjmp() and throw() must not violate the entry/exit criteria.
12641   CS->getCapturedDecl()->setNothrow();
12642   for (int ThisCaptureLevel =
12643            getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
12644        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12645     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12646     // 1.2.2 OpenMP Language Terminology
12647     // Structured block - An executable statement with a single entry at the
12648     // top and a single exit at the bottom.
12649     // The point of exit cannot be a branch out of the structured block.
12650     // longjmp() and throw() must not violate the entry/exit criteria.
12651     CS->getCapturedDecl()->setNothrow();
12652   }
12653 
12654   OMPLoopBasedDirective::HelperExprs B;
12655   // In presence of clause 'collapse' with number of loops, it will
12656   // define the nested loops number.
12657   unsigned NestedLoopCount = checkOpenMPLoop(
12658       OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
12659       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12660       VarsWithImplicitDSA, B);
12661   if (NestedLoopCount == 0)
12662     return StmtError();
12663 
12664   assert((CurContext->isDependentContext() || B.builtAll()) &&
12665          "omp target teams distribute parallel for loop exprs were not built");
12666 
12667   if (!CurContext->isDependentContext()) {
12668     // Finalize the clauses that need pre-built expressions for CodeGen.
12669     for (OMPClause *C : Clauses) {
12670       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12671         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12672                                      B.NumIterations, *this, CurScope,
12673                                      DSAStack))
12674           return StmtError();
12675     }
12676   }
12677 
12678   setFunctionHasBranchProtectedScope();
12679   return OMPTargetTeamsDistributeParallelForDirective::Create(
12680       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
12681       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
12682 }
12683 
12684 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
12685     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12686     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12687   if (!AStmt)
12688     return StmtError();
12689 
12690   auto *CS = cast<CapturedStmt>(AStmt);
12691   // 1.2.2 OpenMP Language Terminology
12692   // Structured block - An executable statement with a single entry at the
12693   // top and a single exit at the bottom.
12694   // The point of exit cannot be a branch out of the structured block.
12695   // longjmp() and throw() must not violate the entry/exit criteria.
12696   CS->getCapturedDecl()->setNothrow();
12697   for (int ThisCaptureLevel = getOpenMPCaptureLevels(
12698            OMPD_target_teams_distribute_parallel_for_simd);
12699        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12700     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12701     // 1.2.2 OpenMP Language Terminology
12702     // Structured block - An executable statement with a single entry at the
12703     // top and a single exit at the bottom.
12704     // The point of exit cannot be a branch out of the structured block.
12705     // longjmp() and throw() must not violate the entry/exit criteria.
12706     CS->getCapturedDecl()->setNothrow();
12707   }
12708 
12709   OMPLoopBasedDirective::HelperExprs B;
12710   // In presence of clause 'collapse' with number of loops, it will
12711   // define the nested loops number.
12712   unsigned NestedLoopCount =
12713       checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
12714                       getCollapseNumberExpr(Clauses),
12715                       nullptr /*ordered not a clause on distribute*/, CS, *this,
12716                       *DSAStack, VarsWithImplicitDSA, B);
12717   if (NestedLoopCount == 0)
12718     return StmtError();
12719 
12720   assert((CurContext->isDependentContext() || B.builtAll()) &&
12721          "omp target teams distribute parallel for simd loop exprs were not "
12722          "built");
12723 
12724   if (!CurContext->isDependentContext()) {
12725     // Finalize the clauses that need pre-built expressions for CodeGen.
12726     for (OMPClause *C : Clauses) {
12727       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12728         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12729                                      B.NumIterations, *this, CurScope,
12730                                      DSAStack))
12731           return StmtError();
12732     }
12733   }
12734 
12735   if (checkSimdlenSafelenSpecified(*this, Clauses))
12736     return StmtError();
12737 
12738   setFunctionHasBranchProtectedScope();
12739   return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
12740       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12741 }
12742 
12743 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
12744     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12745     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12746   if (!AStmt)
12747     return StmtError();
12748 
12749   auto *CS = cast<CapturedStmt>(AStmt);
12750   // 1.2.2 OpenMP Language Terminology
12751   // Structured block - An executable statement with a single entry at the
12752   // top and a single exit at the bottom.
12753   // The point of exit cannot be a branch out of the structured block.
12754   // longjmp() and throw() must not violate the entry/exit criteria.
12755   CS->getCapturedDecl()->setNothrow();
12756   for (int ThisCaptureLevel =
12757            getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
12758        ThisCaptureLevel > 1; --ThisCaptureLevel) {
12759     CS = cast<CapturedStmt>(CS->getCapturedStmt());
12760     // 1.2.2 OpenMP Language Terminology
12761     // Structured block - An executable statement with a single entry at the
12762     // top and a single exit at the bottom.
12763     // The point of exit cannot be a branch out of the structured block.
12764     // longjmp() and throw() must not violate the entry/exit criteria.
12765     CS->getCapturedDecl()->setNothrow();
12766   }
12767 
12768   OMPLoopBasedDirective::HelperExprs B;
12769   // In presence of clause 'collapse' with number of loops, it will
12770   // define the nested loops number.
12771   unsigned NestedLoopCount = checkOpenMPLoop(
12772       OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
12773       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
12774       VarsWithImplicitDSA, B);
12775   if (NestedLoopCount == 0)
12776     return StmtError();
12777 
12778   assert((CurContext->isDependentContext() || B.builtAll()) &&
12779          "omp target teams distribute simd loop exprs were not built");
12780 
12781   if (!CurContext->isDependentContext()) {
12782     // Finalize the clauses that need pre-built expressions for CodeGen.
12783     for (OMPClause *C : Clauses) {
12784       if (auto *LC = dyn_cast<OMPLinearClause>(C))
12785         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
12786                                      B.NumIterations, *this, CurScope,
12787                                      DSAStack))
12788           return StmtError();
12789     }
12790   }
12791 
12792   if (checkSimdlenSafelenSpecified(*this, Clauses))
12793     return StmtError();
12794 
12795   setFunctionHasBranchProtectedScope();
12796   return OMPTargetTeamsDistributeSimdDirective::Create(
12797       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
12798 }
12799 
12800 bool Sema::checkTransformableLoopNest(
12801     OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops,
12802     SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers,
12803     Stmt *&Body,
12804     SmallVectorImpl<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>>
12805         &OriginalInits) {
12806   OriginalInits.emplace_back();
12807   bool Result = OMPLoopBasedDirective::doForAllLoops(
12808       AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops,
12809       [this, &LoopHelpers, &Body, &OriginalInits, Kind](unsigned Cnt,
12810                                                         Stmt *CurStmt) {
12811         VarsWithInheritedDSAType TmpDSA;
12812         unsigned SingleNumLoops =
12813             checkOpenMPLoop(Kind, nullptr, nullptr, CurStmt, *this, *DSAStack,
12814                             TmpDSA, LoopHelpers[Cnt]);
12815         if (SingleNumLoops == 0)
12816           return true;
12817         assert(SingleNumLoops == 1 && "Expect single loop iteration space");
12818         if (auto *For = dyn_cast<ForStmt>(CurStmt)) {
12819           OriginalInits.back().push_back(For->getInit());
12820           Body = For->getBody();
12821         } else {
12822           assert(isa<CXXForRangeStmt>(CurStmt) &&
12823                  "Expected canonical for or range-based for loops.");
12824           auto *CXXFor = cast<CXXForRangeStmt>(CurStmt);
12825           OriginalInits.back().push_back(CXXFor->getBeginStmt());
12826           Body = CXXFor->getBody();
12827         }
12828         OriginalInits.emplace_back();
12829         return false;
12830       },
12831       [&OriginalInits](OMPLoopBasedDirective *Transform) {
12832         Stmt *DependentPreInits;
12833         if (auto *Dir = dyn_cast<OMPTileDirective>(Transform))
12834           DependentPreInits = Dir->getPreInits();
12835         else if (auto *Dir = dyn_cast<OMPUnrollDirective>(Transform))
12836           DependentPreInits = Dir->getPreInits();
12837         else
12838           llvm_unreachable("Unhandled loop transformation");
12839         if (!DependentPreInits)
12840           return;
12841         for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup())
12842           OriginalInits.back().push_back(C);
12843       });
12844   assert(OriginalInits.back().empty() && "No preinit after innermost loop");
12845   OriginalInits.pop_back();
12846   return Result;
12847 }
12848 
12849 StmtResult Sema::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
12850                                           Stmt *AStmt, SourceLocation StartLoc,
12851                                           SourceLocation EndLoc) {
12852   auto SizesClauses =
12853       OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses);
12854   if (SizesClauses.empty()) {
12855     // A missing 'sizes' clause is already reported by the parser.
12856     return StmtError();
12857   }
12858   const OMPSizesClause *SizesClause = *SizesClauses.begin();
12859   unsigned NumLoops = SizesClause->getNumSizes();
12860 
12861   // Empty statement should only be possible if there already was an error.
12862   if (!AStmt)
12863     return StmtError();
12864 
12865   // Verify and diagnose loop nest.
12866   SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
12867   Stmt *Body = nullptr;
12868   SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, 4>
12869       OriginalInits;
12870   if (!checkTransformableLoopNest(OMPD_tile, AStmt, NumLoops, LoopHelpers, Body,
12871                                   OriginalInits))
12872     return StmtError();
12873 
12874   // Delay tiling to when template is completely instantiated.
12875   if (CurContext->isDependentContext())
12876     return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses,
12877                                     NumLoops, AStmt, nullptr, nullptr);
12878 
12879   SmallVector<Decl *, 4> PreInits;
12880 
12881   // Create iteration variables for the generated loops.
12882   SmallVector<VarDecl *, 4> FloorIndVars;
12883   SmallVector<VarDecl *, 4> TileIndVars;
12884   FloorIndVars.resize(NumLoops);
12885   TileIndVars.resize(NumLoops);
12886   for (unsigned I = 0; I < NumLoops; ++I) {
12887     OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
12888 
12889     assert(LoopHelper.Counters.size() == 1 &&
12890            "Expect single-dimensional loop iteration space");
12891     auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters.front());
12892     std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
12893     DeclRefExpr *IterVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef);
12894     QualType CntTy = IterVarRef->getType();
12895 
12896     // Iteration variable for the floor (i.e. outer) loop.
12897     {
12898       std::string FloorCntName =
12899           (Twine(".floor_") + llvm::utostr(I) + ".iv." + OrigVarName).str();
12900       VarDecl *FloorCntDecl =
12901           buildVarDecl(*this, {}, CntTy, FloorCntName, nullptr, OrigCntVar);
12902       FloorIndVars[I] = FloorCntDecl;
12903     }
12904 
12905     // Iteration variable for the tile (i.e. inner) loop.
12906     {
12907       std::string TileCntName =
12908           (Twine(".tile_") + llvm::utostr(I) + ".iv." + OrigVarName).str();
12909 
12910       // Reuse the iteration variable created by checkOpenMPLoop. It is also
12911       // used by the expressions to derive the original iteration variable's
12912       // value from the logical iteration number.
12913       auto *TileCntDecl = cast<VarDecl>(IterVarRef->getDecl());
12914       TileCntDecl->setDeclName(&PP.getIdentifierTable().get(TileCntName));
12915       TileIndVars[I] = TileCntDecl;
12916     }
12917     for (auto &P : OriginalInits[I]) {
12918       if (auto *D = P.dyn_cast<Decl *>())
12919         PreInits.push_back(D);
12920       else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>()))
12921         PreInits.append(PI->decl_begin(), PI->decl_end());
12922     }
12923     if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits))
12924       PreInits.append(PI->decl_begin(), PI->decl_end());
12925     // Gather declarations for the data members used as counters.
12926     for (Expr *CounterRef : LoopHelper.Counters) {
12927       auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl();
12928       if (isa<OMPCapturedExprDecl>(CounterDecl))
12929         PreInits.push_back(CounterDecl);
12930     }
12931   }
12932 
12933   // Once the original iteration values are set, append the innermost body.
12934   Stmt *Inner = Body;
12935 
12936   // Create tile loops from the inside to the outside.
12937   for (int I = NumLoops - 1; I >= 0; --I) {
12938     OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
12939     Expr *NumIterations = LoopHelper.NumIterations;
12940     auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]);
12941     QualType CntTy = OrigCntVar->getType();
12942     Expr *DimTileSize = SizesClause->getSizesRefs()[I];
12943     Scope *CurScope = getCurScope();
12944 
12945     // Commonly used variables.
12946     DeclRefExpr *TileIV = buildDeclRefExpr(*this, TileIndVars[I], CntTy,
12947                                            OrigCntVar->getExprLoc());
12948     DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy,
12949                                             OrigCntVar->getExprLoc());
12950 
12951     // For init-statement: auto .tile.iv = .floor.iv
12952     AddInitializerToDecl(TileIndVars[I], DefaultLvalueConversion(FloorIV).get(),
12953                          /*DirectInit=*/false);
12954     Decl *CounterDecl = TileIndVars[I];
12955     StmtResult InitStmt = new (Context)
12956         DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1),
12957                  OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
12958     if (!InitStmt.isUsable())
12959       return StmtError();
12960 
12961     // For cond-expression: .tile.iv < min(.floor.iv + DimTileSize,
12962     // NumIterations)
12963     ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(),
12964                                       BO_Add, FloorIV, DimTileSize);
12965     if (!EndOfTile.isUsable())
12966       return StmtError();
12967     ExprResult IsPartialTile =
12968         BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT,
12969                    NumIterations, EndOfTile.get());
12970     if (!IsPartialTile.isUsable())
12971       return StmtError();
12972     ExprResult MinTileAndIterSpace = ActOnConditionalOp(
12973         LoopHelper.Cond->getBeginLoc(), LoopHelper.Cond->getEndLoc(),
12974         IsPartialTile.get(), NumIterations, EndOfTile.get());
12975     if (!MinTileAndIterSpace.isUsable())
12976       return StmtError();
12977     ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(),
12978                                      BO_LT, TileIV, MinTileAndIterSpace.get());
12979     if (!CondExpr.isUsable())
12980       return StmtError();
12981 
12982     // For incr-statement: ++.tile.iv
12983     ExprResult IncrStmt =
12984         BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV);
12985     if (!IncrStmt.isUsable())
12986       return StmtError();
12987 
12988     // Statements to set the original iteration variable's value from the
12989     // logical iteration number.
12990     // Generated for loop is:
12991     // Original_for_init;
12992     // for (auto .tile.iv = .floor.iv; .tile.iv < min(.floor.iv + DimTileSize,
12993     // NumIterations); ++.tile.iv) {
12994     //   Original_Body;
12995     //   Original_counter_update;
12996     // }
12997     // FIXME: If the innermost body is an loop itself, inserting these
12998     // statements stops it being recognized  as a perfectly nested loop (e.g.
12999     // for applying tiling again). If this is the case, sink the expressions
13000     // further into the inner loop.
13001     SmallVector<Stmt *, 4> BodyParts;
13002     BodyParts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end());
13003     BodyParts.push_back(Inner);
13004     Inner = CompoundStmt::Create(Context, BodyParts, Inner->getBeginLoc(),
13005                                  Inner->getEndLoc());
13006     Inner = new (Context)
13007         ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
13008                 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
13009                 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
13010   }
13011 
13012   // Create floor loops from the inside to the outside.
13013   for (int I = NumLoops - 1; I >= 0; --I) {
13014     auto &LoopHelper = LoopHelpers[I];
13015     Expr *NumIterations = LoopHelper.NumIterations;
13016     DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]);
13017     QualType CntTy = OrigCntVar->getType();
13018     Expr *DimTileSize = SizesClause->getSizesRefs()[I];
13019     Scope *CurScope = getCurScope();
13020 
13021     // Commonly used variables.
13022     DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy,
13023                                             OrigCntVar->getExprLoc());
13024 
13025     // For init-statement: auto .floor.iv = 0
13026     AddInitializerToDecl(
13027         FloorIndVars[I],
13028         ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(),
13029         /*DirectInit=*/false);
13030     Decl *CounterDecl = FloorIndVars[I];
13031     StmtResult InitStmt = new (Context)
13032         DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1),
13033                  OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
13034     if (!InitStmt.isUsable())
13035       return StmtError();
13036 
13037     // For cond-expression: .floor.iv < NumIterations
13038     ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(),
13039                                      BO_LT, FloorIV, NumIterations);
13040     if (!CondExpr.isUsable())
13041       return StmtError();
13042 
13043     // For incr-statement: .floor.iv += DimTileSize
13044     ExprResult IncrStmt = BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(),
13045                                      BO_AddAssign, FloorIV, DimTileSize);
13046     if (!IncrStmt.isUsable())
13047       return StmtError();
13048 
13049     Inner = new (Context)
13050         ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
13051                 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
13052                 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
13053   }
13054 
13055   return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, NumLoops,
13056                                   AStmt, Inner,
13057                                   buildPreInits(Context, PreInits));
13058 }
13059 
13060 StmtResult Sema::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses,
13061                                             Stmt *AStmt,
13062                                             SourceLocation StartLoc,
13063                                             SourceLocation EndLoc) {
13064   // Empty statement should only be possible if there already was an error.
13065   if (!AStmt)
13066     return StmtError();
13067 
13068   if (checkMutuallyExclusiveClauses(*this, Clauses, {OMPC_partial, OMPC_full}))
13069     return StmtError();
13070 
13071   const OMPFullClause *FullClause =
13072       OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses);
13073   const OMPPartialClause *PartialClause =
13074       OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses);
13075   assert(!(FullClause && PartialClause) &&
13076          "mutual exclusivity must have been checked before");
13077 
13078   constexpr unsigned NumLoops = 1;
13079   Stmt *Body = nullptr;
13080   SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
13081       NumLoops);
13082   SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, NumLoops + 1>
13083       OriginalInits;
13084   if (!checkTransformableLoopNest(OMPD_unroll, AStmt, NumLoops, LoopHelpers,
13085                                   Body, OriginalInits))
13086     return StmtError();
13087 
13088   unsigned NumGeneratedLoops = PartialClause ? 1 : 0;
13089 
13090   // Delay unrolling to when template is completely instantiated.
13091   if (CurContext->isDependentContext())
13092     return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
13093                                       NumGeneratedLoops, nullptr, nullptr);
13094 
13095   OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
13096 
13097   if (FullClause) {
13098     if (!VerifyPositiveIntegerConstantInClause(
13099              LoopHelper.NumIterations, OMPC_full, /*StrictlyPositive=*/false,
13100              /*SuppressExprDigs=*/true)
13101              .isUsable()) {
13102       Diag(AStmt->getBeginLoc(), diag::err_omp_unroll_full_variable_trip_count);
13103       Diag(FullClause->getBeginLoc(), diag::note_omp_directive_here)
13104           << "#pragma omp unroll full";
13105       return StmtError();
13106     }
13107   }
13108 
13109   // The generated loop may only be passed to other loop-associated directive
13110   // when a partial clause is specified. Without the requirement it is
13111   // sufficient to generate loop unroll metadata at code-generation.
13112   if (NumGeneratedLoops == 0)
13113     return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
13114                                       NumGeneratedLoops, nullptr, nullptr);
13115 
13116   // Otherwise, we need to provide a de-sugared/transformed AST that can be
13117   // associated with another loop directive.
13118   //
13119   // The canonical loop analysis return by checkTransformableLoopNest assumes
13120   // the following structure to be the same loop without transformations or
13121   // directives applied: \code OriginalInits; LoopHelper.PreInits;
13122   // LoopHelper.Counters;
13123   // for (; IV < LoopHelper.NumIterations; ++IV) {
13124   //   LoopHelper.Updates;
13125   //   Body;
13126   // }
13127   // \endcode
13128   // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits
13129   // and referenced by LoopHelper.IterationVarRef.
13130   //
13131   // The unrolling directive transforms this into the following loop:
13132   // \code
13133   // OriginalInits;         \
13134   // LoopHelper.PreInits;    > NewPreInits
13135   // LoopHelper.Counters;   /
13136   // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) {
13137   //   #pragma clang loop unroll_count(Factor)
13138   //   for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV)
13139   //   {
13140   //     LoopHelper.Updates;
13141   //     Body;
13142   //   }
13143   // }
13144   // \endcode
13145   // where UIV is a new logical iteration counter. IV must be the same VarDecl
13146   // as the original LoopHelper.IterationVarRef because LoopHelper.Updates
13147   // references it. If the partially unrolled loop is associated with another
13148   // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to
13149   // analyze this loop, i.e. the outer loop must fulfill the constraints of an
13150   // OpenMP canonical loop. The inner loop is not an associable canonical loop
13151   // and only exists to defer its unrolling to LLVM's LoopUnroll instead of
13152   // doing it in the frontend (by adding loop metadata). NewPreInits becomes a
13153   // property of the OMPLoopBasedDirective instead of statements in
13154   // CompoundStatement. This is to allow the loop to become a non-outermost loop
13155   // of a canonical loop nest where these PreInits are emitted before the
13156   // outermost directive.
13157 
13158   // Determine the PreInit declarations.
13159   SmallVector<Decl *, 4> PreInits;
13160   assert(OriginalInits.size() == 1 &&
13161          "Expecting a single-dimensional loop iteration space");
13162   for (auto &P : OriginalInits[0]) {
13163     if (auto *D = P.dyn_cast<Decl *>())
13164       PreInits.push_back(D);
13165     else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>()))
13166       PreInits.append(PI->decl_begin(), PI->decl_end());
13167   }
13168   if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits))
13169     PreInits.append(PI->decl_begin(), PI->decl_end());
13170   // Gather declarations for the data members used as counters.
13171   for (Expr *CounterRef : LoopHelper.Counters) {
13172     auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl();
13173     if (isa<OMPCapturedExprDecl>(CounterDecl))
13174       PreInits.push_back(CounterDecl);
13175   }
13176 
13177   auto *IterationVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef);
13178   QualType IVTy = IterationVarRef->getType();
13179   assert(LoopHelper.Counters.size() == 1 &&
13180          "Expecting a single-dimensional loop iteration space");
13181   auto *OrigVar = cast<DeclRefExpr>(LoopHelper.Counters.front());
13182 
13183   // Determine the unroll factor.
13184   uint64_t Factor;
13185   SourceLocation FactorLoc;
13186   if (Expr *FactorVal = PartialClause->getFactor()) {
13187     Factor =
13188         FactorVal->getIntegerConstantExpr(Context).getValue().getZExtValue();
13189     FactorLoc = FactorVal->getExprLoc();
13190   } else {
13191     // TODO: Use a better profitability model.
13192     Factor = 2;
13193   }
13194   assert(Factor > 0 && "Expected positive unroll factor");
13195   auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() {
13196     return IntegerLiteral::Create(
13197         Context, llvm::APInt(Context.getIntWidth(IVTy), Factor), IVTy,
13198         FactorLoc);
13199   };
13200 
13201   // Iteration variable SourceLocations.
13202   SourceLocation OrigVarLoc = OrigVar->getExprLoc();
13203   SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
13204   SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
13205 
13206   // Internal variable names.
13207   std::string OrigVarName = OrigVar->getNameInfo().getAsString();
13208   std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str();
13209   std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str();
13210   std::string InnerTripCountName =
13211       (Twine(".unroll_inner.tripcount.") + OrigVarName).str();
13212 
13213   // Create the iteration variable for the unrolled loop.
13214   VarDecl *OuterIVDecl =
13215       buildVarDecl(*this, {}, IVTy, OuterIVName, nullptr, OrigVar);
13216   auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() {
13217     return buildDeclRefExpr(*this, OuterIVDecl, IVTy, OrigVarLoc);
13218   };
13219 
13220   // Iteration variable for the inner loop: Reuse the iteration variable created
13221   // by checkOpenMPLoop.
13222   auto *InnerIVDecl = cast<VarDecl>(IterationVarRef->getDecl());
13223   InnerIVDecl->setDeclName(&PP.getIdentifierTable().get(InnerIVName));
13224   auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() {
13225     return buildDeclRefExpr(*this, InnerIVDecl, IVTy, OrigVarLoc);
13226   };
13227 
13228   // Make a copy of the NumIterations expression for each use: By the AST
13229   // constraints, every expression object in a DeclContext must be unique.
13230   CaptureVars CopyTransformer(*this);
13231   auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * {
13232     return AssertSuccess(
13233         CopyTransformer.TransformExpr(LoopHelper.NumIterations));
13234   };
13235 
13236   // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv
13237   ExprResult LValueConv = DefaultLvalueConversion(MakeOuterRef());
13238   AddInitializerToDecl(InnerIVDecl, LValueConv.get(), /*DirectInit=*/false);
13239   StmtResult InnerInit = new (Context)
13240       DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd);
13241   if (!InnerInit.isUsable())
13242     return StmtError();
13243 
13244   // Inner For cond-expression:
13245   // \code
13246   //   .unroll_inner.iv < .unrolled.iv + Factor &&
13247   //   .unroll_inner.iv < NumIterations
13248   // \endcode
13249   // This conjunction of two conditions allows ScalarEvolution to derive the
13250   // maximum trip count of the inner loop.
13251   ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(),
13252                                     BO_Add, MakeOuterRef(), MakeFactorExpr());
13253   if (!EndOfTile.isUsable())
13254     return StmtError();
13255   ExprResult InnerCond1 = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(),
13256                                      BO_LE, MakeInnerRef(), EndOfTile.get());
13257   if (!InnerCond1.isUsable())
13258     return StmtError();
13259   ExprResult InnerCond2 =
13260       BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LE, MakeInnerRef(),
13261                  MakeNumIterations());
13262   if (!InnerCond2.isUsable())
13263     return StmtError();
13264   ExprResult InnerCond =
13265       BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LAnd,
13266                  InnerCond1.get(), InnerCond2.get());
13267   if (!InnerCond.isUsable())
13268     return StmtError();
13269 
13270   // Inner For incr-statement: ++.unroll_inner.iv
13271   ExprResult InnerIncr = BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(),
13272                                       UO_PreInc, MakeInnerRef());
13273   if (!InnerIncr.isUsable())
13274     return StmtError();
13275 
13276   // Inner For statement.
13277   SmallVector<Stmt *> InnerBodyStmts;
13278   InnerBodyStmts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end());
13279   InnerBodyStmts.push_back(Body);
13280   CompoundStmt *InnerBody = CompoundStmt::Create(
13281       Context, InnerBodyStmts, Body->getBeginLoc(), Body->getEndLoc());
13282   ForStmt *InnerFor = new (Context)
13283       ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr,
13284               InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(),
13285               LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
13286 
13287   // Unroll metadata for the inner loop.
13288   // This needs to take into account the remainder portion of the unrolled loop,
13289   // hence `unroll(full)` does not apply here, even though the LoopUnroll pass
13290   // supports multiple loop exits. Instead, unroll using a factor equivalent to
13291   // the maximum trip count, which will also generate a remainder loop. Just
13292   // `unroll(enable)` (which could have been useful if the user has not
13293   // specified a concrete factor; even though the outer loop cannot be
13294   // influenced anymore, would avoid more code bloat than necessary) will refuse
13295   // the loop because "Won't unroll; remainder loop could not be generated when
13296   // assuming runtime trip count". Even if it did work, it must not choose a
13297   // larger unroll factor than the maximum loop length, or it would always just
13298   // execute the remainder loop.
13299   LoopHintAttr *UnrollHintAttr =
13300       LoopHintAttr::CreateImplicit(Context, LoopHintAttr::UnrollCount,
13301                                    LoopHintAttr::Numeric, MakeFactorExpr());
13302   AttributedStmt *InnerUnrolled =
13303       AttributedStmt::Create(Context, StartLoc, {UnrollHintAttr}, InnerFor);
13304 
13305   // Outer For init-statement: auto .unrolled.iv = 0
13306   AddInitializerToDecl(
13307       OuterIVDecl, ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(),
13308       /*DirectInit=*/false);
13309   StmtResult OuterInit = new (Context)
13310       DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd);
13311   if (!OuterInit.isUsable())
13312     return StmtError();
13313 
13314   // Outer For cond-expression: .unrolled.iv < NumIterations
13315   ExprResult OuterConde =
13316       BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, MakeOuterRef(),
13317                  MakeNumIterations());
13318   if (!OuterConde.isUsable())
13319     return StmtError();
13320 
13321   // Outer For incr-statement: .unrolled.iv += Factor
13322   ExprResult OuterIncr =
13323       BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), BO_AddAssign,
13324                  MakeOuterRef(), MakeFactorExpr());
13325   if (!OuterIncr.isUsable())
13326     return StmtError();
13327 
13328   // Outer For statement.
13329   ForStmt *OuterFor = new (Context)
13330       ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr,
13331               OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(),
13332               LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
13333 
13334   return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
13335                                     NumGeneratedLoops, OuterFor,
13336                                     buildPreInits(Context, PreInits));
13337 }
13338 
13339 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
13340                                              SourceLocation StartLoc,
13341                                              SourceLocation LParenLoc,
13342                                              SourceLocation EndLoc) {
13343   OMPClause *Res = nullptr;
13344   switch (Kind) {
13345   case OMPC_final:
13346     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
13347     break;
13348   case OMPC_num_threads:
13349     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
13350     break;
13351   case OMPC_safelen:
13352     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
13353     break;
13354   case OMPC_simdlen:
13355     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
13356     break;
13357   case OMPC_allocator:
13358     Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc);
13359     break;
13360   case OMPC_collapse:
13361     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
13362     break;
13363   case OMPC_ordered:
13364     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
13365     break;
13366   case OMPC_num_teams:
13367     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
13368     break;
13369   case OMPC_thread_limit:
13370     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
13371     break;
13372   case OMPC_priority:
13373     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
13374     break;
13375   case OMPC_grainsize:
13376     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
13377     break;
13378   case OMPC_num_tasks:
13379     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
13380     break;
13381   case OMPC_hint:
13382     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
13383     break;
13384   case OMPC_depobj:
13385     Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc);
13386     break;
13387   case OMPC_detach:
13388     Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc);
13389     break;
13390   case OMPC_novariants:
13391     Res = ActOnOpenMPNovariantsClause(Expr, StartLoc, LParenLoc, EndLoc);
13392     break;
13393   case OMPC_nocontext:
13394     Res = ActOnOpenMPNocontextClause(Expr, StartLoc, LParenLoc, EndLoc);
13395     break;
13396   case OMPC_filter:
13397     Res = ActOnOpenMPFilterClause(Expr, StartLoc, LParenLoc, EndLoc);
13398     break;
13399   case OMPC_partial:
13400     Res = ActOnOpenMPPartialClause(Expr, StartLoc, LParenLoc, EndLoc);
13401     break;
13402   case OMPC_device:
13403   case OMPC_if:
13404   case OMPC_default:
13405   case OMPC_proc_bind:
13406   case OMPC_schedule:
13407   case OMPC_private:
13408   case OMPC_firstprivate:
13409   case OMPC_lastprivate:
13410   case OMPC_shared:
13411   case OMPC_reduction:
13412   case OMPC_task_reduction:
13413   case OMPC_in_reduction:
13414   case OMPC_linear:
13415   case OMPC_aligned:
13416   case OMPC_copyin:
13417   case OMPC_copyprivate:
13418   case OMPC_nowait:
13419   case OMPC_untied:
13420   case OMPC_mergeable:
13421   case OMPC_threadprivate:
13422   case OMPC_sizes:
13423   case OMPC_allocate:
13424   case OMPC_flush:
13425   case OMPC_read:
13426   case OMPC_write:
13427   case OMPC_update:
13428   case OMPC_capture:
13429   case OMPC_seq_cst:
13430   case OMPC_acq_rel:
13431   case OMPC_acquire:
13432   case OMPC_release:
13433   case OMPC_relaxed:
13434   case OMPC_depend:
13435   case OMPC_threads:
13436   case OMPC_simd:
13437   case OMPC_map:
13438   case OMPC_nogroup:
13439   case OMPC_dist_schedule:
13440   case OMPC_defaultmap:
13441   case OMPC_unknown:
13442   case OMPC_uniform:
13443   case OMPC_to:
13444   case OMPC_from:
13445   case OMPC_use_device_ptr:
13446   case OMPC_use_device_addr:
13447   case OMPC_is_device_ptr:
13448   case OMPC_unified_address:
13449   case OMPC_unified_shared_memory:
13450   case OMPC_reverse_offload:
13451   case OMPC_dynamic_allocators:
13452   case OMPC_atomic_default_mem_order:
13453   case OMPC_device_type:
13454   case OMPC_match:
13455   case OMPC_nontemporal:
13456   case OMPC_order:
13457   case OMPC_destroy:
13458   case OMPC_inclusive:
13459   case OMPC_exclusive:
13460   case OMPC_uses_allocators:
13461   case OMPC_affinity:
13462   case OMPC_when:
13463   default:
13464     llvm_unreachable("Clause is not allowed.");
13465   }
13466   return Res;
13467 }
13468 
13469 // An OpenMP directive such as 'target parallel' has two captured regions:
13470 // for the 'target' and 'parallel' respectively.  This function returns
13471 // the region in which to capture expressions associated with a clause.
13472 // A return value of OMPD_unknown signifies that the expression should not
13473 // be captured.
13474 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
13475     OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion,
13476     OpenMPDirectiveKind NameModifier = OMPD_unknown) {
13477   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
13478   switch (CKind) {
13479   case OMPC_if:
13480     switch (DKind) {
13481     case OMPD_target_parallel_for_simd:
13482       if (OpenMPVersion >= 50 &&
13483           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) {
13484         CaptureRegion = OMPD_parallel;
13485         break;
13486       }
13487       LLVM_FALLTHROUGH;
13488     case OMPD_target_parallel:
13489     case OMPD_target_parallel_for:
13490       // If this clause applies to the nested 'parallel' region, capture within
13491       // the 'target' region, otherwise do not capture.
13492       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
13493         CaptureRegion = OMPD_target;
13494       break;
13495     case OMPD_target_teams_distribute_parallel_for_simd:
13496       if (OpenMPVersion >= 50 &&
13497           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) {
13498         CaptureRegion = OMPD_parallel;
13499         break;
13500       }
13501       LLVM_FALLTHROUGH;
13502     case OMPD_target_teams_distribute_parallel_for:
13503       // If this clause applies to the nested 'parallel' region, capture within
13504       // the 'teams' region, otherwise do not capture.
13505       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
13506         CaptureRegion = OMPD_teams;
13507       break;
13508     case OMPD_teams_distribute_parallel_for_simd:
13509       if (OpenMPVersion >= 50 &&
13510           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) {
13511         CaptureRegion = OMPD_parallel;
13512         break;
13513       }
13514       LLVM_FALLTHROUGH;
13515     case OMPD_teams_distribute_parallel_for:
13516       CaptureRegion = OMPD_teams;
13517       break;
13518     case OMPD_target_update:
13519     case OMPD_target_enter_data:
13520     case OMPD_target_exit_data:
13521       CaptureRegion = OMPD_task;
13522       break;
13523     case OMPD_parallel_master_taskloop:
13524       if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop)
13525         CaptureRegion = OMPD_parallel;
13526       break;
13527     case OMPD_parallel_master_taskloop_simd:
13528       if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) ||
13529           NameModifier == OMPD_taskloop) {
13530         CaptureRegion = OMPD_parallel;
13531         break;
13532       }
13533       if (OpenMPVersion <= 45)
13534         break;
13535       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
13536         CaptureRegion = OMPD_taskloop;
13537       break;
13538     case OMPD_parallel_for_simd:
13539       if (OpenMPVersion <= 45)
13540         break;
13541       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
13542         CaptureRegion = OMPD_parallel;
13543       break;
13544     case OMPD_taskloop_simd:
13545     case OMPD_master_taskloop_simd:
13546       if (OpenMPVersion <= 45)
13547         break;
13548       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
13549         CaptureRegion = OMPD_taskloop;
13550       break;
13551     case OMPD_distribute_parallel_for_simd:
13552       if (OpenMPVersion <= 45)
13553         break;
13554       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
13555         CaptureRegion = OMPD_parallel;
13556       break;
13557     case OMPD_target_simd:
13558       if (OpenMPVersion >= 50 &&
13559           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd))
13560         CaptureRegion = OMPD_target;
13561       break;
13562     case OMPD_teams_distribute_simd:
13563     case OMPD_target_teams_distribute_simd:
13564       if (OpenMPVersion >= 50 &&
13565           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd))
13566         CaptureRegion = OMPD_teams;
13567       break;
13568     case OMPD_cancel:
13569     case OMPD_parallel:
13570     case OMPD_parallel_master:
13571     case OMPD_parallel_sections:
13572     case OMPD_parallel_for:
13573     case OMPD_target:
13574     case OMPD_target_teams:
13575     case OMPD_target_teams_distribute:
13576     case OMPD_distribute_parallel_for:
13577     case OMPD_task:
13578     case OMPD_taskloop:
13579     case OMPD_master_taskloop:
13580     case OMPD_target_data:
13581     case OMPD_simd:
13582     case OMPD_for_simd:
13583     case OMPD_distribute_simd:
13584       // Do not capture if-clause expressions.
13585       break;
13586     case OMPD_threadprivate:
13587     case OMPD_allocate:
13588     case OMPD_taskyield:
13589     case OMPD_barrier:
13590     case OMPD_taskwait:
13591     case OMPD_cancellation_point:
13592     case OMPD_flush:
13593     case OMPD_depobj:
13594     case OMPD_scan:
13595     case OMPD_declare_reduction:
13596     case OMPD_declare_mapper:
13597     case OMPD_declare_simd:
13598     case OMPD_declare_variant:
13599     case OMPD_begin_declare_variant:
13600     case OMPD_end_declare_variant:
13601     case OMPD_declare_target:
13602     case OMPD_end_declare_target:
13603     case OMPD_loop:
13604     case OMPD_teams:
13605     case OMPD_tile:
13606     case OMPD_unroll:
13607     case OMPD_for:
13608     case OMPD_sections:
13609     case OMPD_section:
13610     case OMPD_single:
13611     case OMPD_master:
13612     case OMPD_masked:
13613     case OMPD_critical:
13614     case OMPD_taskgroup:
13615     case OMPD_distribute:
13616     case OMPD_ordered:
13617     case OMPD_atomic:
13618     case OMPD_teams_distribute:
13619     case OMPD_requires:
13620     case OMPD_metadirective:
13621       llvm_unreachable("Unexpected OpenMP directive with if-clause");
13622     case OMPD_unknown:
13623     default:
13624       llvm_unreachable("Unknown OpenMP directive");
13625     }
13626     break;
13627   case OMPC_num_threads:
13628     switch (DKind) {
13629     case OMPD_target_parallel:
13630     case OMPD_target_parallel_for:
13631     case OMPD_target_parallel_for_simd:
13632       CaptureRegion = OMPD_target;
13633       break;
13634     case OMPD_teams_distribute_parallel_for:
13635     case OMPD_teams_distribute_parallel_for_simd:
13636     case OMPD_target_teams_distribute_parallel_for:
13637     case OMPD_target_teams_distribute_parallel_for_simd:
13638       CaptureRegion = OMPD_teams;
13639       break;
13640     case OMPD_parallel:
13641     case OMPD_parallel_master:
13642     case OMPD_parallel_sections:
13643     case OMPD_parallel_for:
13644     case OMPD_parallel_for_simd:
13645     case OMPD_distribute_parallel_for:
13646     case OMPD_distribute_parallel_for_simd:
13647     case OMPD_parallel_master_taskloop:
13648     case OMPD_parallel_master_taskloop_simd:
13649       // Do not capture num_threads-clause expressions.
13650       break;
13651     case OMPD_target_data:
13652     case OMPD_target_enter_data:
13653     case OMPD_target_exit_data:
13654     case OMPD_target_update:
13655     case OMPD_target:
13656     case OMPD_target_simd:
13657     case OMPD_target_teams:
13658     case OMPD_target_teams_distribute:
13659     case OMPD_target_teams_distribute_simd:
13660     case OMPD_cancel:
13661     case OMPD_task:
13662     case OMPD_taskloop:
13663     case OMPD_taskloop_simd:
13664     case OMPD_master_taskloop:
13665     case OMPD_master_taskloop_simd:
13666     case OMPD_threadprivate:
13667     case OMPD_allocate:
13668     case OMPD_taskyield:
13669     case OMPD_barrier:
13670     case OMPD_taskwait:
13671     case OMPD_cancellation_point:
13672     case OMPD_flush:
13673     case OMPD_depobj:
13674     case OMPD_scan:
13675     case OMPD_declare_reduction:
13676     case OMPD_declare_mapper:
13677     case OMPD_declare_simd:
13678     case OMPD_declare_variant:
13679     case OMPD_begin_declare_variant:
13680     case OMPD_end_declare_variant:
13681     case OMPD_declare_target:
13682     case OMPD_end_declare_target:
13683     case OMPD_loop:
13684     case OMPD_teams:
13685     case OMPD_simd:
13686     case OMPD_tile:
13687     case OMPD_unroll:
13688     case OMPD_for:
13689     case OMPD_for_simd:
13690     case OMPD_sections:
13691     case OMPD_section:
13692     case OMPD_single:
13693     case OMPD_master:
13694     case OMPD_masked:
13695     case OMPD_critical:
13696     case OMPD_taskgroup:
13697     case OMPD_distribute:
13698     case OMPD_ordered:
13699     case OMPD_atomic:
13700     case OMPD_distribute_simd:
13701     case OMPD_teams_distribute:
13702     case OMPD_teams_distribute_simd:
13703     case OMPD_requires:
13704     case OMPD_metadirective:
13705       llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
13706     case OMPD_unknown:
13707     default:
13708       llvm_unreachable("Unknown OpenMP directive");
13709     }
13710     break;
13711   case OMPC_num_teams:
13712     switch (DKind) {
13713     case OMPD_target_teams:
13714     case OMPD_target_teams_distribute:
13715     case OMPD_target_teams_distribute_simd:
13716     case OMPD_target_teams_distribute_parallel_for:
13717     case OMPD_target_teams_distribute_parallel_for_simd:
13718       CaptureRegion = OMPD_target;
13719       break;
13720     case OMPD_teams_distribute_parallel_for:
13721     case OMPD_teams_distribute_parallel_for_simd:
13722     case OMPD_teams:
13723     case OMPD_teams_distribute:
13724     case OMPD_teams_distribute_simd:
13725       // Do not capture num_teams-clause expressions.
13726       break;
13727     case OMPD_distribute_parallel_for:
13728     case OMPD_distribute_parallel_for_simd:
13729     case OMPD_task:
13730     case OMPD_taskloop:
13731     case OMPD_taskloop_simd:
13732     case OMPD_master_taskloop:
13733     case OMPD_master_taskloop_simd:
13734     case OMPD_parallel_master_taskloop:
13735     case OMPD_parallel_master_taskloop_simd:
13736     case OMPD_target_data:
13737     case OMPD_target_enter_data:
13738     case OMPD_target_exit_data:
13739     case OMPD_target_update:
13740     case OMPD_cancel:
13741     case OMPD_parallel:
13742     case OMPD_parallel_master:
13743     case OMPD_parallel_sections:
13744     case OMPD_parallel_for:
13745     case OMPD_parallel_for_simd:
13746     case OMPD_target:
13747     case OMPD_target_simd:
13748     case OMPD_target_parallel:
13749     case OMPD_target_parallel_for:
13750     case OMPD_target_parallel_for_simd:
13751     case OMPD_threadprivate:
13752     case OMPD_allocate:
13753     case OMPD_taskyield:
13754     case OMPD_barrier:
13755     case OMPD_taskwait:
13756     case OMPD_cancellation_point:
13757     case OMPD_flush:
13758     case OMPD_depobj:
13759     case OMPD_scan:
13760     case OMPD_declare_reduction:
13761     case OMPD_declare_mapper:
13762     case OMPD_declare_simd:
13763     case OMPD_declare_variant:
13764     case OMPD_begin_declare_variant:
13765     case OMPD_end_declare_variant:
13766     case OMPD_declare_target:
13767     case OMPD_end_declare_target:
13768     case OMPD_loop:
13769     case OMPD_simd:
13770     case OMPD_tile:
13771     case OMPD_unroll:
13772     case OMPD_for:
13773     case OMPD_for_simd:
13774     case OMPD_sections:
13775     case OMPD_section:
13776     case OMPD_single:
13777     case OMPD_master:
13778     case OMPD_masked:
13779     case OMPD_critical:
13780     case OMPD_taskgroup:
13781     case OMPD_distribute:
13782     case OMPD_ordered:
13783     case OMPD_atomic:
13784     case OMPD_distribute_simd:
13785     case OMPD_requires:
13786     case OMPD_metadirective:
13787       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
13788     case OMPD_unknown:
13789     default:
13790       llvm_unreachable("Unknown OpenMP directive");
13791     }
13792     break;
13793   case OMPC_thread_limit:
13794     switch (DKind) {
13795     case OMPD_target_teams:
13796     case OMPD_target_teams_distribute:
13797     case OMPD_target_teams_distribute_simd:
13798     case OMPD_target_teams_distribute_parallel_for:
13799     case OMPD_target_teams_distribute_parallel_for_simd:
13800       CaptureRegion = OMPD_target;
13801       break;
13802     case OMPD_teams_distribute_parallel_for:
13803     case OMPD_teams_distribute_parallel_for_simd:
13804     case OMPD_teams:
13805     case OMPD_teams_distribute:
13806     case OMPD_teams_distribute_simd:
13807       // Do not capture thread_limit-clause expressions.
13808       break;
13809     case OMPD_distribute_parallel_for:
13810     case OMPD_distribute_parallel_for_simd:
13811     case OMPD_task:
13812     case OMPD_taskloop:
13813     case OMPD_taskloop_simd:
13814     case OMPD_master_taskloop:
13815     case OMPD_master_taskloop_simd:
13816     case OMPD_parallel_master_taskloop:
13817     case OMPD_parallel_master_taskloop_simd:
13818     case OMPD_target_data:
13819     case OMPD_target_enter_data:
13820     case OMPD_target_exit_data:
13821     case OMPD_target_update:
13822     case OMPD_cancel:
13823     case OMPD_parallel:
13824     case OMPD_parallel_master:
13825     case OMPD_parallel_sections:
13826     case OMPD_parallel_for:
13827     case OMPD_parallel_for_simd:
13828     case OMPD_target:
13829     case OMPD_target_simd:
13830     case OMPD_target_parallel:
13831     case OMPD_target_parallel_for:
13832     case OMPD_target_parallel_for_simd:
13833     case OMPD_threadprivate:
13834     case OMPD_allocate:
13835     case OMPD_taskyield:
13836     case OMPD_barrier:
13837     case OMPD_taskwait:
13838     case OMPD_cancellation_point:
13839     case OMPD_flush:
13840     case OMPD_depobj:
13841     case OMPD_scan:
13842     case OMPD_declare_reduction:
13843     case OMPD_declare_mapper:
13844     case OMPD_declare_simd:
13845     case OMPD_declare_variant:
13846     case OMPD_begin_declare_variant:
13847     case OMPD_end_declare_variant:
13848     case OMPD_declare_target:
13849     case OMPD_end_declare_target:
13850     case OMPD_loop:
13851     case OMPD_simd:
13852     case OMPD_tile:
13853     case OMPD_unroll:
13854     case OMPD_for:
13855     case OMPD_for_simd:
13856     case OMPD_sections:
13857     case OMPD_section:
13858     case OMPD_single:
13859     case OMPD_master:
13860     case OMPD_masked:
13861     case OMPD_critical:
13862     case OMPD_taskgroup:
13863     case OMPD_distribute:
13864     case OMPD_ordered:
13865     case OMPD_atomic:
13866     case OMPD_distribute_simd:
13867     case OMPD_requires:
13868     case OMPD_metadirective:
13869       llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
13870     case OMPD_unknown:
13871     default:
13872       llvm_unreachable("Unknown OpenMP directive");
13873     }
13874     break;
13875   case OMPC_schedule:
13876     switch (DKind) {
13877     case OMPD_parallel_for:
13878     case OMPD_parallel_for_simd:
13879     case OMPD_distribute_parallel_for:
13880     case OMPD_distribute_parallel_for_simd:
13881     case OMPD_teams_distribute_parallel_for:
13882     case OMPD_teams_distribute_parallel_for_simd:
13883     case OMPD_target_parallel_for:
13884     case OMPD_target_parallel_for_simd:
13885     case OMPD_target_teams_distribute_parallel_for:
13886     case OMPD_target_teams_distribute_parallel_for_simd:
13887       CaptureRegion = OMPD_parallel;
13888       break;
13889     case OMPD_for:
13890     case OMPD_for_simd:
13891       // Do not capture schedule-clause expressions.
13892       break;
13893     case OMPD_task:
13894     case OMPD_taskloop:
13895     case OMPD_taskloop_simd:
13896     case OMPD_master_taskloop:
13897     case OMPD_master_taskloop_simd:
13898     case OMPD_parallel_master_taskloop:
13899     case OMPD_parallel_master_taskloop_simd:
13900     case OMPD_target_data:
13901     case OMPD_target_enter_data:
13902     case OMPD_target_exit_data:
13903     case OMPD_target_update:
13904     case OMPD_teams:
13905     case OMPD_teams_distribute:
13906     case OMPD_teams_distribute_simd:
13907     case OMPD_target_teams_distribute:
13908     case OMPD_target_teams_distribute_simd:
13909     case OMPD_target:
13910     case OMPD_target_simd:
13911     case OMPD_target_parallel:
13912     case OMPD_cancel:
13913     case OMPD_parallel:
13914     case OMPD_parallel_master:
13915     case OMPD_parallel_sections:
13916     case OMPD_threadprivate:
13917     case OMPD_allocate:
13918     case OMPD_taskyield:
13919     case OMPD_barrier:
13920     case OMPD_taskwait:
13921     case OMPD_cancellation_point:
13922     case OMPD_flush:
13923     case OMPD_depobj:
13924     case OMPD_scan:
13925     case OMPD_declare_reduction:
13926     case OMPD_declare_mapper:
13927     case OMPD_declare_simd:
13928     case OMPD_declare_variant:
13929     case OMPD_begin_declare_variant:
13930     case OMPD_end_declare_variant:
13931     case OMPD_declare_target:
13932     case OMPD_end_declare_target:
13933     case OMPD_loop:
13934     case OMPD_simd:
13935     case OMPD_tile:
13936     case OMPD_unroll:
13937     case OMPD_sections:
13938     case OMPD_section:
13939     case OMPD_single:
13940     case OMPD_master:
13941     case OMPD_masked:
13942     case OMPD_critical:
13943     case OMPD_taskgroup:
13944     case OMPD_distribute:
13945     case OMPD_ordered:
13946     case OMPD_atomic:
13947     case OMPD_distribute_simd:
13948     case OMPD_target_teams:
13949     case OMPD_requires:
13950     case OMPD_metadirective:
13951       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
13952     case OMPD_unknown:
13953     default:
13954       llvm_unreachable("Unknown OpenMP directive");
13955     }
13956     break;
13957   case OMPC_dist_schedule:
13958     switch (DKind) {
13959     case OMPD_teams_distribute_parallel_for:
13960     case OMPD_teams_distribute_parallel_for_simd:
13961     case OMPD_teams_distribute:
13962     case OMPD_teams_distribute_simd:
13963     case OMPD_target_teams_distribute_parallel_for:
13964     case OMPD_target_teams_distribute_parallel_for_simd:
13965     case OMPD_target_teams_distribute:
13966     case OMPD_target_teams_distribute_simd:
13967       CaptureRegion = OMPD_teams;
13968       break;
13969     case OMPD_distribute_parallel_for:
13970     case OMPD_distribute_parallel_for_simd:
13971     case OMPD_distribute:
13972     case OMPD_distribute_simd:
13973       // Do not capture dist_schedule-clause expressions.
13974       break;
13975     case OMPD_parallel_for:
13976     case OMPD_parallel_for_simd:
13977     case OMPD_target_parallel_for_simd:
13978     case OMPD_target_parallel_for:
13979     case OMPD_task:
13980     case OMPD_taskloop:
13981     case OMPD_taskloop_simd:
13982     case OMPD_master_taskloop:
13983     case OMPD_master_taskloop_simd:
13984     case OMPD_parallel_master_taskloop:
13985     case OMPD_parallel_master_taskloop_simd:
13986     case OMPD_target_data:
13987     case OMPD_target_enter_data:
13988     case OMPD_target_exit_data:
13989     case OMPD_target_update:
13990     case OMPD_teams:
13991     case OMPD_target:
13992     case OMPD_target_simd:
13993     case OMPD_target_parallel:
13994     case OMPD_cancel:
13995     case OMPD_parallel:
13996     case OMPD_parallel_master:
13997     case OMPD_parallel_sections:
13998     case OMPD_threadprivate:
13999     case OMPD_allocate:
14000     case OMPD_taskyield:
14001     case OMPD_barrier:
14002     case OMPD_taskwait:
14003     case OMPD_cancellation_point:
14004     case OMPD_flush:
14005     case OMPD_depobj:
14006     case OMPD_scan:
14007     case OMPD_declare_reduction:
14008     case OMPD_declare_mapper:
14009     case OMPD_declare_simd:
14010     case OMPD_declare_variant:
14011     case OMPD_begin_declare_variant:
14012     case OMPD_end_declare_variant:
14013     case OMPD_declare_target:
14014     case OMPD_end_declare_target:
14015     case OMPD_loop:
14016     case OMPD_simd:
14017     case OMPD_tile:
14018     case OMPD_unroll:
14019     case OMPD_for:
14020     case OMPD_for_simd:
14021     case OMPD_sections:
14022     case OMPD_section:
14023     case OMPD_single:
14024     case OMPD_master:
14025     case OMPD_masked:
14026     case OMPD_critical:
14027     case OMPD_taskgroup:
14028     case OMPD_ordered:
14029     case OMPD_atomic:
14030     case OMPD_target_teams:
14031     case OMPD_requires:
14032     case OMPD_metadirective:
14033       llvm_unreachable("Unexpected OpenMP directive with dist_schedule clause");
14034     case OMPD_unknown:
14035     default:
14036       llvm_unreachable("Unknown OpenMP directive");
14037     }
14038     break;
14039   case OMPC_device:
14040     switch (DKind) {
14041     case OMPD_target_update:
14042     case OMPD_target_enter_data:
14043     case OMPD_target_exit_data:
14044     case OMPD_target:
14045     case OMPD_target_simd:
14046     case OMPD_target_teams:
14047     case OMPD_target_parallel:
14048     case OMPD_target_teams_distribute:
14049     case OMPD_target_teams_distribute_simd:
14050     case OMPD_target_parallel_for:
14051     case OMPD_target_parallel_for_simd:
14052     case OMPD_target_teams_distribute_parallel_for:
14053     case OMPD_target_teams_distribute_parallel_for_simd:
14054     case OMPD_dispatch:
14055       CaptureRegion = OMPD_task;
14056       break;
14057     case OMPD_target_data:
14058     case OMPD_interop:
14059       // Do not capture device-clause expressions.
14060       break;
14061     case OMPD_teams_distribute_parallel_for:
14062     case OMPD_teams_distribute_parallel_for_simd:
14063     case OMPD_teams:
14064     case OMPD_teams_distribute:
14065     case OMPD_teams_distribute_simd:
14066     case OMPD_distribute_parallel_for:
14067     case OMPD_distribute_parallel_for_simd:
14068     case OMPD_task:
14069     case OMPD_taskloop:
14070     case OMPD_taskloop_simd:
14071     case OMPD_master_taskloop:
14072     case OMPD_master_taskloop_simd:
14073     case OMPD_parallel_master_taskloop:
14074     case OMPD_parallel_master_taskloop_simd:
14075     case OMPD_cancel:
14076     case OMPD_parallel:
14077     case OMPD_parallel_master:
14078     case OMPD_parallel_sections:
14079     case OMPD_parallel_for:
14080     case OMPD_parallel_for_simd:
14081     case OMPD_threadprivate:
14082     case OMPD_allocate:
14083     case OMPD_taskyield:
14084     case OMPD_barrier:
14085     case OMPD_taskwait:
14086     case OMPD_cancellation_point:
14087     case OMPD_flush:
14088     case OMPD_depobj:
14089     case OMPD_scan:
14090     case OMPD_declare_reduction:
14091     case OMPD_declare_mapper:
14092     case OMPD_declare_simd:
14093     case OMPD_declare_variant:
14094     case OMPD_begin_declare_variant:
14095     case OMPD_end_declare_variant:
14096     case OMPD_declare_target:
14097     case OMPD_end_declare_target:
14098     case OMPD_loop:
14099     case OMPD_simd:
14100     case OMPD_tile:
14101     case OMPD_unroll:
14102     case OMPD_for:
14103     case OMPD_for_simd:
14104     case OMPD_sections:
14105     case OMPD_section:
14106     case OMPD_single:
14107     case OMPD_master:
14108     case OMPD_masked:
14109     case OMPD_critical:
14110     case OMPD_taskgroup:
14111     case OMPD_distribute:
14112     case OMPD_ordered:
14113     case OMPD_atomic:
14114     case OMPD_distribute_simd:
14115     case OMPD_requires:
14116     case OMPD_metadirective:
14117       llvm_unreachable("Unexpected OpenMP directive with device-clause");
14118     case OMPD_unknown:
14119     default:
14120       llvm_unreachable("Unknown OpenMP directive");
14121     }
14122     break;
14123   case OMPC_grainsize:
14124   case OMPC_num_tasks:
14125   case OMPC_final:
14126   case OMPC_priority:
14127     switch (DKind) {
14128     case OMPD_task:
14129     case OMPD_taskloop:
14130     case OMPD_taskloop_simd:
14131     case OMPD_master_taskloop:
14132     case OMPD_master_taskloop_simd:
14133       break;
14134     case OMPD_parallel_master_taskloop:
14135     case OMPD_parallel_master_taskloop_simd:
14136       CaptureRegion = OMPD_parallel;
14137       break;
14138     case OMPD_target_update:
14139     case OMPD_target_enter_data:
14140     case OMPD_target_exit_data:
14141     case OMPD_target:
14142     case OMPD_target_simd:
14143     case OMPD_target_teams:
14144     case OMPD_target_parallel:
14145     case OMPD_target_teams_distribute:
14146     case OMPD_target_teams_distribute_simd:
14147     case OMPD_target_parallel_for:
14148     case OMPD_target_parallel_for_simd:
14149     case OMPD_target_teams_distribute_parallel_for:
14150     case OMPD_target_teams_distribute_parallel_for_simd:
14151     case OMPD_target_data:
14152     case OMPD_teams_distribute_parallel_for:
14153     case OMPD_teams_distribute_parallel_for_simd:
14154     case OMPD_teams:
14155     case OMPD_teams_distribute:
14156     case OMPD_teams_distribute_simd:
14157     case OMPD_distribute_parallel_for:
14158     case OMPD_distribute_parallel_for_simd:
14159     case OMPD_cancel:
14160     case OMPD_parallel:
14161     case OMPD_parallel_master:
14162     case OMPD_parallel_sections:
14163     case OMPD_parallel_for:
14164     case OMPD_parallel_for_simd:
14165     case OMPD_threadprivate:
14166     case OMPD_allocate:
14167     case OMPD_taskyield:
14168     case OMPD_barrier:
14169     case OMPD_taskwait:
14170     case OMPD_cancellation_point:
14171     case OMPD_flush:
14172     case OMPD_depobj:
14173     case OMPD_scan:
14174     case OMPD_declare_reduction:
14175     case OMPD_declare_mapper:
14176     case OMPD_declare_simd:
14177     case OMPD_declare_variant:
14178     case OMPD_begin_declare_variant:
14179     case OMPD_end_declare_variant:
14180     case OMPD_declare_target:
14181     case OMPD_end_declare_target:
14182     case OMPD_loop:
14183     case OMPD_simd:
14184     case OMPD_tile:
14185     case OMPD_unroll:
14186     case OMPD_for:
14187     case OMPD_for_simd:
14188     case OMPD_sections:
14189     case OMPD_section:
14190     case OMPD_single:
14191     case OMPD_master:
14192     case OMPD_masked:
14193     case OMPD_critical:
14194     case OMPD_taskgroup:
14195     case OMPD_distribute:
14196     case OMPD_ordered:
14197     case OMPD_atomic:
14198     case OMPD_distribute_simd:
14199     case OMPD_requires:
14200     case OMPD_metadirective:
14201       llvm_unreachable("Unexpected OpenMP directive with grainsize-clause");
14202     case OMPD_unknown:
14203     default:
14204       llvm_unreachable("Unknown OpenMP directive");
14205     }
14206     break;
14207   case OMPC_novariants:
14208   case OMPC_nocontext:
14209     switch (DKind) {
14210     case OMPD_dispatch:
14211       CaptureRegion = OMPD_task;
14212       break;
14213     default:
14214       llvm_unreachable("Unexpected OpenMP directive");
14215     }
14216     break;
14217   case OMPC_filter:
14218     // Do not capture filter-clause expressions.
14219     break;
14220   case OMPC_when:
14221     if (DKind == OMPD_metadirective) {
14222       CaptureRegion = OMPD_metadirective;
14223     } else if (DKind == OMPD_unknown) {
14224       llvm_unreachable("Unknown OpenMP directive");
14225     } else {
14226       llvm_unreachable("Unexpected OpenMP directive with when clause");
14227     }
14228     break;
14229   case OMPC_firstprivate:
14230   case OMPC_lastprivate:
14231   case OMPC_reduction:
14232   case OMPC_task_reduction:
14233   case OMPC_in_reduction:
14234   case OMPC_linear:
14235   case OMPC_default:
14236   case OMPC_proc_bind:
14237   case OMPC_safelen:
14238   case OMPC_simdlen:
14239   case OMPC_sizes:
14240   case OMPC_allocator:
14241   case OMPC_collapse:
14242   case OMPC_private:
14243   case OMPC_shared:
14244   case OMPC_aligned:
14245   case OMPC_copyin:
14246   case OMPC_copyprivate:
14247   case OMPC_ordered:
14248   case OMPC_nowait:
14249   case OMPC_untied:
14250   case OMPC_mergeable:
14251   case OMPC_threadprivate:
14252   case OMPC_allocate:
14253   case OMPC_flush:
14254   case OMPC_depobj:
14255   case OMPC_read:
14256   case OMPC_write:
14257   case OMPC_update:
14258   case OMPC_capture:
14259   case OMPC_seq_cst:
14260   case OMPC_acq_rel:
14261   case OMPC_acquire:
14262   case OMPC_release:
14263   case OMPC_relaxed:
14264   case OMPC_depend:
14265   case OMPC_threads:
14266   case OMPC_simd:
14267   case OMPC_map:
14268   case OMPC_nogroup:
14269   case OMPC_hint:
14270   case OMPC_defaultmap:
14271   case OMPC_unknown:
14272   case OMPC_uniform:
14273   case OMPC_to:
14274   case OMPC_from:
14275   case OMPC_use_device_ptr:
14276   case OMPC_use_device_addr:
14277   case OMPC_is_device_ptr:
14278   case OMPC_unified_address:
14279   case OMPC_unified_shared_memory:
14280   case OMPC_reverse_offload:
14281   case OMPC_dynamic_allocators:
14282   case OMPC_atomic_default_mem_order:
14283   case OMPC_device_type:
14284   case OMPC_match:
14285   case OMPC_nontemporal:
14286   case OMPC_order:
14287   case OMPC_destroy:
14288   case OMPC_detach:
14289   case OMPC_inclusive:
14290   case OMPC_exclusive:
14291   case OMPC_uses_allocators:
14292   case OMPC_affinity:
14293   default:
14294     llvm_unreachable("Unexpected OpenMP clause.");
14295   }
14296   return CaptureRegion;
14297 }
14298 
14299 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
14300                                      Expr *Condition, SourceLocation StartLoc,
14301                                      SourceLocation LParenLoc,
14302                                      SourceLocation NameModifierLoc,
14303                                      SourceLocation ColonLoc,
14304                                      SourceLocation EndLoc) {
14305   Expr *ValExpr = Condition;
14306   Stmt *HelperValStmt = nullptr;
14307   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
14308   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
14309       !Condition->isInstantiationDependent() &&
14310       !Condition->containsUnexpandedParameterPack()) {
14311     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
14312     if (Val.isInvalid())
14313       return nullptr;
14314 
14315     ValExpr = Val.get();
14316 
14317     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14318     CaptureRegion = getOpenMPCaptureRegionForClause(
14319         DKind, OMPC_if, LangOpts.OpenMP, NameModifier);
14320     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14321       ValExpr = MakeFullExpr(ValExpr).get();
14322       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14323       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14324       HelperValStmt = buildPreInits(Context, Captures);
14325     }
14326   }
14327 
14328   return new (Context)
14329       OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
14330                   LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
14331 }
14332 
14333 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
14334                                         SourceLocation StartLoc,
14335                                         SourceLocation LParenLoc,
14336                                         SourceLocation EndLoc) {
14337   Expr *ValExpr = Condition;
14338   Stmt *HelperValStmt = nullptr;
14339   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
14340   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
14341       !Condition->isInstantiationDependent() &&
14342       !Condition->containsUnexpandedParameterPack()) {
14343     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
14344     if (Val.isInvalid())
14345       return nullptr;
14346 
14347     ValExpr = MakeFullExpr(Val.get()).get();
14348 
14349     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14350     CaptureRegion =
14351         getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP);
14352     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14353       ValExpr = MakeFullExpr(ValExpr).get();
14354       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14355       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14356       HelperValStmt = buildPreInits(Context, Captures);
14357     }
14358   }
14359 
14360   return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion,
14361                                       StartLoc, LParenLoc, EndLoc);
14362 }
14363 
14364 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
14365                                                         Expr *Op) {
14366   if (!Op)
14367     return ExprError();
14368 
14369   class IntConvertDiagnoser : public ICEConvertDiagnoser {
14370   public:
14371     IntConvertDiagnoser()
14372         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
14373     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
14374                                          QualType T) override {
14375       return S.Diag(Loc, diag::err_omp_not_integral) << T;
14376     }
14377     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
14378                                              QualType T) override {
14379       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
14380     }
14381     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
14382                                                QualType T,
14383                                                QualType ConvTy) override {
14384       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
14385     }
14386     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
14387                                            QualType ConvTy) override {
14388       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
14389              << ConvTy->isEnumeralType() << ConvTy;
14390     }
14391     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
14392                                             QualType T) override {
14393       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
14394     }
14395     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
14396                                         QualType ConvTy) override {
14397       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
14398              << ConvTy->isEnumeralType() << ConvTy;
14399     }
14400     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
14401                                              QualType) override {
14402       llvm_unreachable("conversion functions are permitted");
14403     }
14404   } ConvertDiagnoser;
14405   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
14406 }
14407 
14408 static bool
14409 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind,
14410                           bool StrictlyPositive, bool BuildCapture = false,
14411                           OpenMPDirectiveKind DKind = OMPD_unknown,
14412                           OpenMPDirectiveKind *CaptureRegion = nullptr,
14413                           Stmt **HelperValStmt = nullptr) {
14414   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
14415       !ValExpr->isInstantiationDependent()) {
14416     SourceLocation Loc = ValExpr->getExprLoc();
14417     ExprResult Value =
14418         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
14419     if (Value.isInvalid())
14420       return false;
14421 
14422     ValExpr = Value.get();
14423     // The expression must evaluate to a non-negative integer value.
14424     if (Optional<llvm::APSInt> Result =
14425             ValExpr->getIntegerConstantExpr(SemaRef.Context)) {
14426       if (Result->isSigned() &&
14427           !((!StrictlyPositive && Result->isNonNegative()) ||
14428             (StrictlyPositive && Result->isStrictlyPositive()))) {
14429         SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
14430             << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
14431             << ValExpr->getSourceRange();
14432         return false;
14433       }
14434     }
14435     if (!BuildCapture)
14436       return true;
14437     *CaptureRegion =
14438         getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP);
14439     if (*CaptureRegion != OMPD_unknown &&
14440         !SemaRef.CurContext->isDependentContext()) {
14441       ValExpr = SemaRef.MakeFullExpr(ValExpr).get();
14442       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14443       ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get();
14444       *HelperValStmt = buildPreInits(SemaRef.Context, Captures);
14445     }
14446   }
14447   return true;
14448 }
14449 
14450 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
14451                                              SourceLocation StartLoc,
14452                                              SourceLocation LParenLoc,
14453                                              SourceLocation EndLoc) {
14454   Expr *ValExpr = NumThreads;
14455   Stmt *HelperValStmt = nullptr;
14456 
14457   // OpenMP [2.5, Restrictions]
14458   //  The num_threads expression must evaluate to a positive integer value.
14459   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
14460                                  /*StrictlyPositive=*/true))
14461     return nullptr;
14462 
14463   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14464   OpenMPDirectiveKind CaptureRegion =
14465       getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP);
14466   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14467     ValExpr = MakeFullExpr(ValExpr).get();
14468     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14469     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14470     HelperValStmt = buildPreInits(Context, Captures);
14471   }
14472 
14473   return new (Context) OMPNumThreadsClause(
14474       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
14475 }
14476 
14477 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
14478                                                        OpenMPClauseKind CKind,
14479                                                        bool StrictlyPositive,
14480                                                        bool SuppressExprDiags) {
14481   if (!E)
14482     return ExprError();
14483   if (E->isValueDependent() || E->isTypeDependent() ||
14484       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
14485     return E;
14486 
14487   llvm::APSInt Result;
14488   ExprResult ICE;
14489   if (SuppressExprDiags) {
14490     // Use a custom diagnoser that suppresses 'note' diagnostics about the
14491     // expression.
14492     struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser {
14493       SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {}
14494       Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S,
14495                                                  SourceLocation Loc) override {
14496         llvm_unreachable("Diagnostic suppressed");
14497       }
14498     } Diagnoser;
14499     ICE = VerifyIntegerConstantExpression(E, &Result, Diagnoser, AllowFold);
14500   } else {
14501     ICE = VerifyIntegerConstantExpression(E, &Result, /*FIXME*/ AllowFold);
14502   }
14503   if (ICE.isInvalid())
14504     return ExprError();
14505 
14506   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
14507       (!StrictlyPositive && !Result.isNonNegative())) {
14508     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
14509         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
14510         << E->getSourceRange();
14511     return ExprError();
14512   }
14513   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
14514     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
14515         << E->getSourceRange();
14516     return ExprError();
14517   }
14518   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
14519     DSAStack->setAssociatedLoops(Result.getExtValue());
14520   else if (CKind == OMPC_ordered)
14521     DSAStack->setAssociatedLoops(Result.getExtValue());
14522   return ICE;
14523 }
14524 
14525 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
14526                                           SourceLocation LParenLoc,
14527                                           SourceLocation EndLoc) {
14528   // OpenMP [2.8.1, simd construct, Description]
14529   // The parameter of the safelen clause must be a constant
14530   // positive integer expression.
14531   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
14532   if (Safelen.isInvalid())
14533     return nullptr;
14534   return new (Context)
14535       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
14536 }
14537 
14538 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
14539                                           SourceLocation LParenLoc,
14540                                           SourceLocation EndLoc) {
14541   // OpenMP [2.8.1, simd construct, Description]
14542   // The parameter of the simdlen clause must be a constant
14543   // positive integer expression.
14544   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
14545   if (Simdlen.isInvalid())
14546     return nullptr;
14547   return new (Context)
14548       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
14549 }
14550 
14551 /// Tries to find omp_allocator_handle_t type.
14552 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
14553                                     DSAStackTy *Stack) {
14554   QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT();
14555   if (!OMPAllocatorHandleT.isNull())
14556     return true;
14557   // Build the predefined allocator expressions.
14558   bool ErrorFound = false;
14559   for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
14560     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
14561     StringRef Allocator =
14562         OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
14563     DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator);
14564     auto *VD = dyn_cast_or_null<ValueDecl>(
14565         S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName));
14566     if (!VD) {
14567       ErrorFound = true;
14568       break;
14569     }
14570     QualType AllocatorType =
14571         VD->getType().getNonLValueExprType(S.getASTContext());
14572     ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc);
14573     if (!Res.isUsable()) {
14574       ErrorFound = true;
14575       break;
14576     }
14577     if (OMPAllocatorHandleT.isNull())
14578       OMPAllocatorHandleT = AllocatorType;
14579     if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) {
14580       ErrorFound = true;
14581       break;
14582     }
14583     Stack->setAllocator(AllocatorKind, Res.get());
14584   }
14585   if (ErrorFound) {
14586     S.Diag(Loc, diag::err_omp_implied_type_not_found)
14587         << "omp_allocator_handle_t";
14588     return false;
14589   }
14590   OMPAllocatorHandleT.addConst();
14591   Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT);
14592   return true;
14593 }
14594 
14595 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc,
14596                                             SourceLocation LParenLoc,
14597                                             SourceLocation EndLoc) {
14598   // OpenMP [2.11.3, allocate Directive, Description]
14599   // allocator is an expression of omp_allocator_handle_t type.
14600   if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack))
14601     return nullptr;
14602 
14603   ExprResult Allocator = DefaultLvalueConversion(A);
14604   if (Allocator.isInvalid())
14605     return nullptr;
14606   Allocator = PerformImplicitConversion(Allocator.get(),
14607                                         DSAStack->getOMPAllocatorHandleT(),
14608                                         Sema::AA_Initializing,
14609                                         /*AllowExplicit=*/true);
14610   if (Allocator.isInvalid())
14611     return nullptr;
14612   return new (Context)
14613       OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
14614 }
14615 
14616 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
14617                                            SourceLocation StartLoc,
14618                                            SourceLocation LParenLoc,
14619                                            SourceLocation EndLoc) {
14620   // OpenMP [2.7.1, loop construct, Description]
14621   // OpenMP [2.8.1, simd construct, Description]
14622   // OpenMP [2.9.6, distribute construct, Description]
14623   // The parameter of the collapse clause must be a constant
14624   // positive integer expression.
14625   ExprResult NumForLoopsResult =
14626       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
14627   if (NumForLoopsResult.isInvalid())
14628     return nullptr;
14629   return new (Context)
14630       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
14631 }
14632 
14633 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
14634                                           SourceLocation EndLoc,
14635                                           SourceLocation LParenLoc,
14636                                           Expr *NumForLoops) {
14637   // OpenMP [2.7.1, loop construct, Description]
14638   // OpenMP [2.8.1, simd construct, Description]
14639   // OpenMP [2.9.6, distribute construct, Description]
14640   // The parameter of the ordered clause must be a constant
14641   // positive integer expression if any.
14642   if (NumForLoops && LParenLoc.isValid()) {
14643     ExprResult NumForLoopsResult =
14644         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
14645     if (NumForLoopsResult.isInvalid())
14646       return nullptr;
14647     NumForLoops = NumForLoopsResult.get();
14648   } else {
14649     NumForLoops = nullptr;
14650   }
14651   auto *Clause = OMPOrderedClause::Create(
14652       Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
14653       StartLoc, LParenLoc, EndLoc);
14654   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
14655   return Clause;
14656 }
14657 
14658 OMPClause *Sema::ActOnOpenMPSimpleClause(
14659     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
14660     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
14661   OMPClause *Res = nullptr;
14662   switch (Kind) {
14663   case OMPC_default:
14664     Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument),
14665                                    ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14666     break;
14667   case OMPC_proc_bind:
14668     Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument),
14669                                     ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14670     break;
14671   case OMPC_atomic_default_mem_order:
14672     Res = ActOnOpenMPAtomicDefaultMemOrderClause(
14673         static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
14674         ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14675     break;
14676   case OMPC_order:
14677     Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument),
14678                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14679     break;
14680   case OMPC_update:
14681     Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument),
14682                                   ArgumentLoc, StartLoc, LParenLoc, EndLoc);
14683     break;
14684   case OMPC_if:
14685   case OMPC_final:
14686   case OMPC_num_threads:
14687   case OMPC_safelen:
14688   case OMPC_simdlen:
14689   case OMPC_sizes:
14690   case OMPC_allocator:
14691   case OMPC_collapse:
14692   case OMPC_schedule:
14693   case OMPC_private:
14694   case OMPC_firstprivate:
14695   case OMPC_lastprivate:
14696   case OMPC_shared:
14697   case OMPC_reduction:
14698   case OMPC_task_reduction:
14699   case OMPC_in_reduction:
14700   case OMPC_linear:
14701   case OMPC_aligned:
14702   case OMPC_copyin:
14703   case OMPC_copyprivate:
14704   case OMPC_ordered:
14705   case OMPC_nowait:
14706   case OMPC_untied:
14707   case OMPC_mergeable:
14708   case OMPC_threadprivate:
14709   case OMPC_allocate:
14710   case OMPC_flush:
14711   case OMPC_depobj:
14712   case OMPC_read:
14713   case OMPC_write:
14714   case OMPC_capture:
14715   case OMPC_seq_cst:
14716   case OMPC_acq_rel:
14717   case OMPC_acquire:
14718   case OMPC_release:
14719   case OMPC_relaxed:
14720   case OMPC_depend:
14721   case OMPC_device:
14722   case OMPC_threads:
14723   case OMPC_simd:
14724   case OMPC_map:
14725   case OMPC_num_teams:
14726   case OMPC_thread_limit:
14727   case OMPC_priority:
14728   case OMPC_grainsize:
14729   case OMPC_nogroup:
14730   case OMPC_num_tasks:
14731   case OMPC_hint:
14732   case OMPC_dist_schedule:
14733   case OMPC_defaultmap:
14734   case OMPC_unknown:
14735   case OMPC_uniform:
14736   case OMPC_to:
14737   case OMPC_from:
14738   case OMPC_use_device_ptr:
14739   case OMPC_use_device_addr:
14740   case OMPC_is_device_ptr:
14741   case OMPC_unified_address:
14742   case OMPC_unified_shared_memory:
14743   case OMPC_reverse_offload:
14744   case OMPC_dynamic_allocators:
14745   case OMPC_device_type:
14746   case OMPC_match:
14747   case OMPC_nontemporal:
14748   case OMPC_destroy:
14749   case OMPC_novariants:
14750   case OMPC_nocontext:
14751   case OMPC_detach:
14752   case OMPC_inclusive:
14753   case OMPC_exclusive:
14754   case OMPC_uses_allocators:
14755   case OMPC_affinity:
14756   case OMPC_when:
14757   default:
14758     llvm_unreachable("Clause is not allowed.");
14759   }
14760   return Res;
14761 }
14762 
14763 static std::string
14764 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
14765                         ArrayRef<unsigned> Exclude = llvm::None) {
14766   SmallString<256> Buffer;
14767   llvm::raw_svector_ostream Out(Buffer);
14768   unsigned Skipped = Exclude.size();
14769   auto S = Exclude.begin(), E = Exclude.end();
14770   for (unsigned I = First; I < Last; ++I) {
14771     if (std::find(S, E, I) != E) {
14772       --Skipped;
14773       continue;
14774     }
14775     Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
14776     if (I + Skipped + 2 == Last)
14777       Out << " or ";
14778     else if (I + Skipped + 1 != Last)
14779       Out << ", ";
14780   }
14781   return std::string(Out.str());
14782 }
14783 
14784 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind,
14785                                           SourceLocation KindKwLoc,
14786                                           SourceLocation StartLoc,
14787                                           SourceLocation LParenLoc,
14788                                           SourceLocation EndLoc) {
14789   if (Kind == OMP_DEFAULT_unknown) {
14790     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14791         << getListOfPossibleValues(OMPC_default, /*First=*/0,
14792                                    /*Last=*/unsigned(OMP_DEFAULT_unknown))
14793         << getOpenMPClauseName(OMPC_default);
14794     return nullptr;
14795   }
14796 
14797   switch (Kind) {
14798   case OMP_DEFAULT_none:
14799     DSAStack->setDefaultDSANone(KindKwLoc);
14800     break;
14801   case OMP_DEFAULT_shared:
14802     DSAStack->setDefaultDSAShared(KindKwLoc);
14803     break;
14804   case OMP_DEFAULT_firstprivate:
14805     DSAStack->setDefaultDSAFirstPrivate(KindKwLoc);
14806     break;
14807   default:
14808     llvm_unreachable("DSA unexpected in OpenMP default clause");
14809   }
14810 
14811   return new (Context)
14812       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
14813 }
14814 
14815 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind,
14816                                            SourceLocation KindKwLoc,
14817                                            SourceLocation StartLoc,
14818                                            SourceLocation LParenLoc,
14819                                            SourceLocation EndLoc) {
14820   if (Kind == OMP_PROC_BIND_unknown) {
14821     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14822         << getListOfPossibleValues(OMPC_proc_bind,
14823                                    /*First=*/unsigned(OMP_PROC_BIND_master),
14824                                    /*Last=*/
14825                                    unsigned(LangOpts.OpenMP > 50
14826                                                 ? OMP_PROC_BIND_primary
14827                                                 : OMP_PROC_BIND_spread) +
14828                                        1)
14829         << getOpenMPClauseName(OMPC_proc_bind);
14830     return nullptr;
14831   }
14832   if (Kind == OMP_PROC_BIND_primary && LangOpts.OpenMP < 51)
14833     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14834         << getListOfPossibleValues(OMPC_proc_bind,
14835                                    /*First=*/unsigned(OMP_PROC_BIND_master),
14836                                    /*Last=*/
14837                                    unsigned(OMP_PROC_BIND_spread) + 1)
14838         << getOpenMPClauseName(OMPC_proc_bind);
14839   return new (Context)
14840       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
14841 }
14842 
14843 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
14844     OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
14845     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
14846   if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
14847     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14848         << getListOfPossibleValues(
14849                OMPC_atomic_default_mem_order, /*First=*/0,
14850                /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
14851         << getOpenMPClauseName(OMPC_atomic_default_mem_order);
14852     return nullptr;
14853   }
14854   return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
14855                                                       LParenLoc, EndLoc);
14856 }
14857 
14858 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind,
14859                                         SourceLocation KindKwLoc,
14860                                         SourceLocation StartLoc,
14861                                         SourceLocation LParenLoc,
14862                                         SourceLocation EndLoc) {
14863   if (Kind == OMPC_ORDER_unknown) {
14864     static_assert(OMPC_ORDER_unknown > 0,
14865                   "OMPC_ORDER_unknown not greater than 0");
14866     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14867         << getListOfPossibleValues(OMPC_order, /*First=*/0,
14868                                    /*Last=*/OMPC_ORDER_unknown)
14869         << getOpenMPClauseName(OMPC_order);
14870     return nullptr;
14871   }
14872   return new (Context)
14873       OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
14874 }
14875 
14876 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind,
14877                                          SourceLocation KindKwLoc,
14878                                          SourceLocation StartLoc,
14879                                          SourceLocation LParenLoc,
14880                                          SourceLocation EndLoc) {
14881   if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source ||
14882       Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) {
14883     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink,
14884                          OMPC_DEPEND_depobj};
14885     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
14886         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
14887                                    /*Last=*/OMPC_DEPEND_unknown, Except)
14888         << getOpenMPClauseName(OMPC_update);
14889     return nullptr;
14890   }
14891   return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind,
14892                                  EndLoc);
14893 }
14894 
14895 OMPClause *Sema::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs,
14896                                         SourceLocation StartLoc,
14897                                         SourceLocation LParenLoc,
14898                                         SourceLocation EndLoc) {
14899   for (Expr *SizeExpr : SizeExprs) {
14900     ExprResult NumForLoopsResult = VerifyPositiveIntegerConstantInClause(
14901         SizeExpr, OMPC_sizes, /*StrictlyPositive=*/true);
14902     if (!NumForLoopsResult.isUsable())
14903       return nullptr;
14904   }
14905 
14906   DSAStack->setAssociatedLoops(SizeExprs.size());
14907   return OMPSizesClause::Create(Context, StartLoc, LParenLoc, EndLoc,
14908                                 SizeExprs);
14909 }
14910 
14911 OMPClause *Sema::ActOnOpenMPFullClause(SourceLocation StartLoc,
14912                                        SourceLocation EndLoc) {
14913   return OMPFullClause::Create(Context, StartLoc, EndLoc);
14914 }
14915 
14916 OMPClause *Sema::ActOnOpenMPPartialClause(Expr *FactorExpr,
14917                                           SourceLocation StartLoc,
14918                                           SourceLocation LParenLoc,
14919                                           SourceLocation EndLoc) {
14920   if (FactorExpr) {
14921     // If an argument is specified, it must be a constant (or an unevaluated
14922     // template expression).
14923     ExprResult FactorResult = VerifyPositiveIntegerConstantInClause(
14924         FactorExpr, OMPC_partial, /*StrictlyPositive=*/true);
14925     if (FactorResult.isInvalid())
14926       return nullptr;
14927     FactorExpr = FactorResult.get();
14928   }
14929 
14930   return OMPPartialClause::Create(Context, StartLoc, LParenLoc, EndLoc,
14931                                   FactorExpr);
14932 }
14933 
14934 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
14935     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
14936     SourceLocation StartLoc, SourceLocation LParenLoc,
14937     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
14938     SourceLocation EndLoc) {
14939   OMPClause *Res = nullptr;
14940   switch (Kind) {
14941   case OMPC_schedule:
14942     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
14943     assert(Argument.size() == NumberOfElements &&
14944            ArgumentLoc.size() == NumberOfElements);
14945     Res = ActOnOpenMPScheduleClause(
14946         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
14947         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
14948         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
14949         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
14950         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
14951     break;
14952   case OMPC_if:
14953     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
14954     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
14955                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
14956                               DelimLoc, EndLoc);
14957     break;
14958   case OMPC_dist_schedule:
14959     Res = ActOnOpenMPDistScheduleClause(
14960         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
14961         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
14962     break;
14963   case OMPC_defaultmap:
14964     enum { Modifier, DefaultmapKind };
14965     Res = ActOnOpenMPDefaultmapClause(
14966         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
14967         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
14968         StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
14969         EndLoc);
14970     break;
14971   case OMPC_device:
14972     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
14973     Res = ActOnOpenMPDeviceClause(
14974         static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr,
14975         StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc);
14976     break;
14977   case OMPC_final:
14978   case OMPC_num_threads:
14979   case OMPC_safelen:
14980   case OMPC_simdlen:
14981   case OMPC_sizes:
14982   case OMPC_allocator:
14983   case OMPC_collapse:
14984   case OMPC_default:
14985   case OMPC_proc_bind:
14986   case OMPC_private:
14987   case OMPC_firstprivate:
14988   case OMPC_lastprivate:
14989   case OMPC_shared:
14990   case OMPC_reduction:
14991   case OMPC_task_reduction:
14992   case OMPC_in_reduction:
14993   case OMPC_linear:
14994   case OMPC_aligned:
14995   case OMPC_copyin:
14996   case OMPC_copyprivate:
14997   case OMPC_ordered:
14998   case OMPC_nowait:
14999   case OMPC_untied:
15000   case OMPC_mergeable:
15001   case OMPC_threadprivate:
15002   case OMPC_allocate:
15003   case OMPC_flush:
15004   case OMPC_depobj:
15005   case OMPC_read:
15006   case OMPC_write:
15007   case OMPC_update:
15008   case OMPC_capture:
15009   case OMPC_seq_cst:
15010   case OMPC_acq_rel:
15011   case OMPC_acquire:
15012   case OMPC_release:
15013   case OMPC_relaxed:
15014   case OMPC_depend:
15015   case OMPC_threads:
15016   case OMPC_simd:
15017   case OMPC_map:
15018   case OMPC_num_teams:
15019   case OMPC_thread_limit:
15020   case OMPC_priority:
15021   case OMPC_grainsize:
15022   case OMPC_nogroup:
15023   case OMPC_num_tasks:
15024   case OMPC_hint:
15025   case OMPC_unknown:
15026   case OMPC_uniform:
15027   case OMPC_to:
15028   case OMPC_from:
15029   case OMPC_use_device_ptr:
15030   case OMPC_use_device_addr:
15031   case OMPC_is_device_ptr:
15032   case OMPC_unified_address:
15033   case OMPC_unified_shared_memory:
15034   case OMPC_reverse_offload:
15035   case OMPC_dynamic_allocators:
15036   case OMPC_atomic_default_mem_order:
15037   case OMPC_device_type:
15038   case OMPC_match:
15039   case OMPC_nontemporal:
15040   case OMPC_order:
15041   case OMPC_destroy:
15042   case OMPC_novariants:
15043   case OMPC_nocontext:
15044   case OMPC_detach:
15045   case OMPC_inclusive:
15046   case OMPC_exclusive:
15047   case OMPC_uses_allocators:
15048   case OMPC_affinity:
15049   case OMPC_when:
15050   default:
15051     llvm_unreachable("Clause is not allowed.");
15052   }
15053   return Res;
15054 }
15055 
15056 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
15057                                    OpenMPScheduleClauseModifier M2,
15058                                    SourceLocation M1Loc, SourceLocation M2Loc) {
15059   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
15060     SmallVector<unsigned, 2> Excluded;
15061     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
15062       Excluded.push_back(M2);
15063     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
15064       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
15065     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
15066       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
15067     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
15068         << getListOfPossibleValues(OMPC_schedule,
15069                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
15070                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
15071                                    Excluded)
15072         << getOpenMPClauseName(OMPC_schedule);
15073     return true;
15074   }
15075   return false;
15076 }
15077 
15078 OMPClause *Sema::ActOnOpenMPScheduleClause(
15079     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
15080     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
15081     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
15082     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
15083   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
15084       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
15085     return nullptr;
15086   // OpenMP, 2.7.1, Loop Construct, Restrictions
15087   // Either the monotonic modifier or the nonmonotonic modifier can be specified
15088   // but not both.
15089   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
15090       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
15091        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
15092       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
15093        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
15094     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
15095         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
15096         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
15097     return nullptr;
15098   }
15099   if (Kind == OMPC_SCHEDULE_unknown) {
15100     std::string Values;
15101     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
15102       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
15103       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
15104                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
15105                                        Exclude);
15106     } else {
15107       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
15108                                        /*Last=*/OMPC_SCHEDULE_unknown);
15109     }
15110     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
15111         << Values << getOpenMPClauseName(OMPC_schedule);
15112     return nullptr;
15113   }
15114   // OpenMP, 2.7.1, Loop Construct, Restrictions
15115   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
15116   // schedule(guided).
15117   // OpenMP 5.0 does not have this restriction.
15118   if (LangOpts.OpenMP < 50 &&
15119       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
15120        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
15121       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
15122     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
15123          diag::err_omp_schedule_nonmonotonic_static);
15124     return nullptr;
15125   }
15126   Expr *ValExpr = ChunkSize;
15127   Stmt *HelperValStmt = nullptr;
15128   if (ChunkSize) {
15129     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
15130         !ChunkSize->isInstantiationDependent() &&
15131         !ChunkSize->containsUnexpandedParameterPack()) {
15132       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
15133       ExprResult Val =
15134           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
15135       if (Val.isInvalid())
15136         return nullptr;
15137 
15138       ValExpr = Val.get();
15139 
15140       // OpenMP [2.7.1, Restrictions]
15141       //  chunk_size must be a loop invariant integer expression with a positive
15142       //  value.
15143       if (Optional<llvm::APSInt> Result =
15144               ValExpr->getIntegerConstantExpr(Context)) {
15145         if (Result->isSigned() && !Result->isStrictlyPositive()) {
15146           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
15147               << "schedule" << 1 << ChunkSize->getSourceRange();
15148           return nullptr;
15149         }
15150       } else if (getOpenMPCaptureRegionForClause(
15151                      DSAStack->getCurrentDirective(), OMPC_schedule,
15152                      LangOpts.OpenMP) != OMPD_unknown &&
15153                  !CurContext->isDependentContext()) {
15154         ValExpr = MakeFullExpr(ValExpr).get();
15155         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15156         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15157         HelperValStmt = buildPreInits(Context, Captures);
15158       }
15159     }
15160   }
15161 
15162   return new (Context)
15163       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
15164                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
15165 }
15166 
15167 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
15168                                    SourceLocation StartLoc,
15169                                    SourceLocation EndLoc) {
15170   OMPClause *Res = nullptr;
15171   switch (Kind) {
15172   case OMPC_ordered:
15173     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
15174     break;
15175   case OMPC_nowait:
15176     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
15177     break;
15178   case OMPC_untied:
15179     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
15180     break;
15181   case OMPC_mergeable:
15182     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
15183     break;
15184   case OMPC_read:
15185     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
15186     break;
15187   case OMPC_write:
15188     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
15189     break;
15190   case OMPC_update:
15191     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
15192     break;
15193   case OMPC_capture:
15194     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
15195     break;
15196   case OMPC_seq_cst:
15197     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
15198     break;
15199   case OMPC_acq_rel:
15200     Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc);
15201     break;
15202   case OMPC_acquire:
15203     Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc);
15204     break;
15205   case OMPC_release:
15206     Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc);
15207     break;
15208   case OMPC_relaxed:
15209     Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc);
15210     break;
15211   case OMPC_threads:
15212     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
15213     break;
15214   case OMPC_simd:
15215     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
15216     break;
15217   case OMPC_nogroup:
15218     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
15219     break;
15220   case OMPC_unified_address:
15221     Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
15222     break;
15223   case OMPC_unified_shared_memory:
15224     Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
15225     break;
15226   case OMPC_reverse_offload:
15227     Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
15228     break;
15229   case OMPC_dynamic_allocators:
15230     Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
15231     break;
15232   case OMPC_destroy:
15233     Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc,
15234                                    /*LParenLoc=*/SourceLocation(),
15235                                    /*VarLoc=*/SourceLocation(), EndLoc);
15236     break;
15237   case OMPC_full:
15238     Res = ActOnOpenMPFullClause(StartLoc, EndLoc);
15239     break;
15240   case OMPC_partial:
15241     Res = ActOnOpenMPPartialClause(nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc);
15242     break;
15243   case OMPC_if:
15244   case OMPC_final:
15245   case OMPC_num_threads:
15246   case OMPC_safelen:
15247   case OMPC_simdlen:
15248   case OMPC_sizes:
15249   case OMPC_allocator:
15250   case OMPC_collapse:
15251   case OMPC_schedule:
15252   case OMPC_private:
15253   case OMPC_firstprivate:
15254   case OMPC_lastprivate:
15255   case OMPC_shared:
15256   case OMPC_reduction:
15257   case OMPC_task_reduction:
15258   case OMPC_in_reduction:
15259   case OMPC_linear:
15260   case OMPC_aligned:
15261   case OMPC_copyin:
15262   case OMPC_copyprivate:
15263   case OMPC_default:
15264   case OMPC_proc_bind:
15265   case OMPC_threadprivate:
15266   case OMPC_allocate:
15267   case OMPC_flush:
15268   case OMPC_depobj:
15269   case OMPC_depend:
15270   case OMPC_device:
15271   case OMPC_map:
15272   case OMPC_num_teams:
15273   case OMPC_thread_limit:
15274   case OMPC_priority:
15275   case OMPC_grainsize:
15276   case OMPC_num_tasks:
15277   case OMPC_hint:
15278   case OMPC_dist_schedule:
15279   case OMPC_defaultmap:
15280   case OMPC_unknown:
15281   case OMPC_uniform:
15282   case OMPC_to:
15283   case OMPC_from:
15284   case OMPC_use_device_ptr:
15285   case OMPC_use_device_addr:
15286   case OMPC_is_device_ptr:
15287   case OMPC_atomic_default_mem_order:
15288   case OMPC_device_type:
15289   case OMPC_match:
15290   case OMPC_nontemporal:
15291   case OMPC_order:
15292   case OMPC_novariants:
15293   case OMPC_nocontext:
15294   case OMPC_detach:
15295   case OMPC_inclusive:
15296   case OMPC_exclusive:
15297   case OMPC_uses_allocators:
15298   case OMPC_affinity:
15299   case OMPC_when:
15300   default:
15301     llvm_unreachable("Clause is not allowed.");
15302   }
15303   return Res;
15304 }
15305 
15306 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
15307                                          SourceLocation EndLoc) {
15308   DSAStack->setNowaitRegion();
15309   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
15310 }
15311 
15312 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
15313                                          SourceLocation EndLoc) {
15314   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
15315 }
15316 
15317 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
15318                                             SourceLocation EndLoc) {
15319   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
15320 }
15321 
15322 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
15323                                        SourceLocation EndLoc) {
15324   return new (Context) OMPReadClause(StartLoc, EndLoc);
15325 }
15326 
15327 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
15328                                         SourceLocation EndLoc) {
15329   return new (Context) OMPWriteClause(StartLoc, EndLoc);
15330 }
15331 
15332 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
15333                                          SourceLocation EndLoc) {
15334   return OMPUpdateClause::Create(Context, StartLoc, EndLoc);
15335 }
15336 
15337 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
15338                                           SourceLocation EndLoc) {
15339   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
15340 }
15341 
15342 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
15343                                          SourceLocation EndLoc) {
15344   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
15345 }
15346 
15347 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc,
15348                                          SourceLocation EndLoc) {
15349   return new (Context) OMPAcqRelClause(StartLoc, EndLoc);
15350 }
15351 
15352 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc,
15353                                           SourceLocation EndLoc) {
15354   return new (Context) OMPAcquireClause(StartLoc, EndLoc);
15355 }
15356 
15357 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc,
15358                                           SourceLocation EndLoc) {
15359   return new (Context) OMPReleaseClause(StartLoc, EndLoc);
15360 }
15361 
15362 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc,
15363                                           SourceLocation EndLoc) {
15364   return new (Context) OMPRelaxedClause(StartLoc, EndLoc);
15365 }
15366 
15367 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
15368                                           SourceLocation EndLoc) {
15369   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
15370 }
15371 
15372 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
15373                                        SourceLocation EndLoc) {
15374   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
15375 }
15376 
15377 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
15378                                           SourceLocation EndLoc) {
15379   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
15380 }
15381 
15382 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
15383                                                  SourceLocation EndLoc) {
15384   return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
15385 }
15386 
15387 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
15388                                                       SourceLocation EndLoc) {
15389   return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
15390 }
15391 
15392 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
15393                                                  SourceLocation EndLoc) {
15394   return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
15395 }
15396 
15397 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
15398                                                     SourceLocation EndLoc) {
15399   return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
15400 }
15401 
15402 StmtResult Sema::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses,
15403                                              SourceLocation StartLoc,
15404                                              SourceLocation EndLoc) {
15405 
15406   // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
15407   // At least one action-clause must appear on a directive.
15408   if (!hasClauses(Clauses, OMPC_init, OMPC_use, OMPC_destroy, OMPC_nowait)) {
15409     StringRef Expected = "'init', 'use', 'destroy', or 'nowait'";
15410     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
15411         << Expected << getOpenMPDirectiveName(OMPD_interop);
15412     return StmtError();
15413   }
15414 
15415   // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
15416   // A depend clause can only appear on the directive if a targetsync
15417   // interop-type is present or the interop-var was initialized with
15418   // the targetsync interop-type.
15419 
15420   // If there is any 'init' clause diagnose if there is no 'init' clause with
15421   // interop-type of 'targetsync'. Cases involving other directives cannot be
15422   // diagnosed.
15423   const OMPDependClause *DependClause = nullptr;
15424   bool HasInitClause = false;
15425   bool IsTargetSync = false;
15426   for (const OMPClause *C : Clauses) {
15427     if (IsTargetSync)
15428       break;
15429     if (const auto *InitClause = dyn_cast<OMPInitClause>(C)) {
15430       HasInitClause = true;
15431       if (InitClause->getIsTargetSync())
15432         IsTargetSync = true;
15433     } else if (const auto *DC = dyn_cast<OMPDependClause>(C)) {
15434       DependClause = DC;
15435     }
15436   }
15437   if (DependClause && HasInitClause && !IsTargetSync) {
15438     Diag(DependClause->getBeginLoc(), diag::err_omp_interop_bad_depend_clause);
15439     return StmtError();
15440   }
15441 
15442   // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
15443   // Each interop-var may be specified for at most one action-clause of each
15444   // interop construct.
15445   llvm::SmallPtrSet<const VarDecl *, 4> InteropVars;
15446   for (const OMPClause *C : Clauses) {
15447     OpenMPClauseKind ClauseKind = C->getClauseKind();
15448     const DeclRefExpr *DRE = nullptr;
15449     SourceLocation VarLoc;
15450 
15451     if (ClauseKind == OMPC_init) {
15452       const auto *IC = cast<OMPInitClause>(C);
15453       VarLoc = IC->getVarLoc();
15454       DRE = dyn_cast_or_null<DeclRefExpr>(IC->getInteropVar());
15455     } else if (ClauseKind == OMPC_use) {
15456       const auto *UC = cast<OMPUseClause>(C);
15457       VarLoc = UC->getVarLoc();
15458       DRE = dyn_cast_or_null<DeclRefExpr>(UC->getInteropVar());
15459     } else if (ClauseKind == OMPC_destroy) {
15460       const auto *DC = cast<OMPDestroyClause>(C);
15461       VarLoc = DC->getVarLoc();
15462       DRE = dyn_cast_or_null<DeclRefExpr>(DC->getInteropVar());
15463     }
15464 
15465     if (!DRE)
15466       continue;
15467 
15468     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
15469       if (!InteropVars.insert(VD->getCanonicalDecl()).second) {
15470         Diag(VarLoc, diag::err_omp_interop_var_multiple_actions) << VD;
15471         return StmtError();
15472       }
15473     }
15474   }
15475 
15476   return OMPInteropDirective::Create(Context, StartLoc, EndLoc, Clauses);
15477 }
15478 
15479 static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr,
15480                                    SourceLocation VarLoc,
15481                                    OpenMPClauseKind Kind) {
15482   if (InteropVarExpr->isValueDependent() || InteropVarExpr->isTypeDependent() ||
15483       InteropVarExpr->isInstantiationDependent() ||
15484       InteropVarExpr->containsUnexpandedParameterPack())
15485     return true;
15486 
15487   const auto *DRE = dyn_cast<DeclRefExpr>(InteropVarExpr);
15488   if (!DRE || !isa<VarDecl>(DRE->getDecl())) {
15489     SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) << 0;
15490     return false;
15491   }
15492 
15493   // Interop variable should be of type omp_interop_t.
15494   bool HasError = false;
15495   QualType InteropType;
15496   LookupResult Result(SemaRef, &SemaRef.Context.Idents.get("omp_interop_t"),
15497                       VarLoc, Sema::LookupOrdinaryName);
15498   if (SemaRef.LookupName(Result, SemaRef.getCurScope())) {
15499     NamedDecl *ND = Result.getFoundDecl();
15500     if (const auto *TD = dyn_cast<TypeDecl>(ND)) {
15501       InteropType = QualType(TD->getTypeForDecl(), 0);
15502     } else {
15503       HasError = true;
15504     }
15505   } else {
15506     HasError = true;
15507   }
15508 
15509   if (HasError) {
15510     SemaRef.Diag(VarLoc, diag::err_omp_implied_type_not_found)
15511         << "omp_interop_t";
15512     return false;
15513   }
15514 
15515   QualType VarType = InteropVarExpr->getType().getUnqualifiedType();
15516   if (!SemaRef.Context.hasSameType(InteropType, VarType)) {
15517     SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_wrong_type);
15518     return false;
15519   }
15520 
15521   // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
15522   // The interop-var passed to init or destroy must be non-const.
15523   if ((Kind == OMPC_init || Kind == OMPC_destroy) &&
15524       isConstNotMutableType(SemaRef, InteropVarExpr->getType())) {
15525     SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected)
15526         << /*non-const*/ 1;
15527     return false;
15528   }
15529   return true;
15530 }
15531 
15532 OMPClause *
15533 Sema::ActOnOpenMPInitClause(Expr *InteropVar, ArrayRef<Expr *> PrefExprs,
15534                             bool IsTarget, bool IsTargetSync,
15535                             SourceLocation StartLoc, SourceLocation LParenLoc,
15536                             SourceLocation VarLoc, SourceLocation EndLoc) {
15537 
15538   if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_init))
15539     return nullptr;
15540 
15541   // Check prefer_type values.  These foreign-runtime-id values are either
15542   // string literals or constant integral expressions.
15543   for (const Expr *E : PrefExprs) {
15544     if (E->isValueDependent() || E->isTypeDependent() ||
15545         E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
15546       continue;
15547     if (E->isIntegerConstantExpr(Context))
15548       continue;
15549     if (isa<StringLiteral>(E))
15550       continue;
15551     Diag(E->getExprLoc(), diag::err_omp_interop_prefer_type);
15552     return nullptr;
15553   }
15554 
15555   return OMPInitClause::Create(Context, InteropVar, PrefExprs, IsTarget,
15556                                IsTargetSync, StartLoc, LParenLoc, VarLoc,
15557                                EndLoc);
15558 }
15559 
15560 OMPClause *Sema::ActOnOpenMPUseClause(Expr *InteropVar, SourceLocation StartLoc,
15561                                       SourceLocation LParenLoc,
15562                                       SourceLocation VarLoc,
15563                                       SourceLocation EndLoc) {
15564 
15565   if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_use))
15566     return nullptr;
15567 
15568   return new (Context)
15569       OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
15570 }
15571 
15572 OMPClause *Sema::ActOnOpenMPDestroyClause(Expr *InteropVar,
15573                                           SourceLocation StartLoc,
15574                                           SourceLocation LParenLoc,
15575                                           SourceLocation VarLoc,
15576                                           SourceLocation EndLoc) {
15577   if (InteropVar &&
15578       !isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_destroy))
15579     return nullptr;
15580 
15581   return new (Context)
15582       OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
15583 }
15584 
15585 OMPClause *Sema::ActOnOpenMPNovariantsClause(Expr *Condition,
15586                                              SourceLocation StartLoc,
15587                                              SourceLocation LParenLoc,
15588                                              SourceLocation EndLoc) {
15589   Expr *ValExpr = Condition;
15590   Stmt *HelperValStmt = nullptr;
15591   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
15592   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
15593       !Condition->isInstantiationDependent() &&
15594       !Condition->containsUnexpandedParameterPack()) {
15595     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
15596     if (Val.isInvalid())
15597       return nullptr;
15598 
15599     ValExpr = MakeFullExpr(Val.get()).get();
15600 
15601     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
15602     CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_novariants,
15603                                                     LangOpts.OpenMP);
15604     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
15605       ValExpr = MakeFullExpr(ValExpr).get();
15606       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15607       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15608       HelperValStmt = buildPreInits(Context, Captures);
15609     }
15610   }
15611 
15612   return new (Context) OMPNovariantsClause(
15613       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
15614 }
15615 
15616 OMPClause *Sema::ActOnOpenMPNocontextClause(Expr *Condition,
15617                                             SourceLocation StartLoc,
15618                                             SourceLocation LParenLoc,
15619                                             SourceLocation EndLoc) {
15620   Expr *ValExpr = Condition;
15621   Stmt *HelperValStmt = nullptr;
15622   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
15623   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
15624       !Condition->isInstantiationDependent() &&
15625       !Condition->containsUnexpandedParameterPack()) {
15626     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
15627     if (Val.isInvalid())
15628       return nullptr;
15629 
15630     ValExpr = MakeFullExpr(Val.get()).get();
15631 
15632     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
15633     CaptureRegion =
15634         getOpenMPCaptureRegionForClause(DKind, OMPC_nocontext, LangOpts.OpenMP);
15635     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
15636       ValExpr = MakeFullExpr(ValExpr).get();
15637       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15638       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15639       HelperValStmt = buildPreInits(Context, Captures);
15640     }
15641   }
15642 
15643   return new (Context) OMPNocontextClause(ValExpr, HelperValStmt, CaptureRegion,
15644                                           StartLoc, LParenLoc, EndLoc);
15645 }
15646 
15647 OMPClause *Sema::ActOnOpenMPFilterClause(Expr *ThreadID,
15648                                          SourceLocation StartLoc,
15649                                          SourceLocation LParenLoc,
15650                                          SourceLocation EndLoc) {
15651   Expr *ValExpr = ThreadID;
15652   Stmt *HelperValStmt = nullptr;
15653 
15654   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
15655   OpenMPDirectiveKind CaptureRegion =
15656       getOpenMPCaptureRegionForClause(DKind, OMPC_filter, LangOpts.OpenMP);
15657   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
15658     ValExpr = MakeFullExpr(ValExpr).get();
15659     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15660     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15661     HelperValStmt = buildPreInits(Context, Captures);
15662   }
15663 
15664   return new (Context) OMPFilterClause(ValExpr, HelperValStmt, CaptureRegion,
15665                                        StartLoc, LParenLoc, EndLoc);
15666 }
15667 
15668 OMPClause *Sema::ActOnOpenMPVarListClause(
15669     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr,
15670     const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
15671     CXXScopeSpec &ReductionOrMapperIdScopeSpec,
15672     DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier,
15673     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
15674     ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit,
15675     SourceLocation ExtraModifierLoc,
15676     ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
15677     ArrayRef<SourceLocation> MotionModifiersLoc) {
15678   SourceLocation StartLoc = Locs.StartLoc;
15679   SourceLocation LParenLoc = Locs.LParenLoc;
15680   SourceLocation EndLoc = Locs.EndLoc;
15681   OMPClause *Res = nullptr;
15682   switch (Kind) {
15683   case OMPC_private:
15684     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
15685     break;
15686   case OMPC_firstprivate:
15687     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
15688     break;
15689   case OMPC_lastprivate:
15690     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown &&
15691            "Unexpected lastprivate modifier.");
15692     Res = ActOnOpenMPLastprivateClause(
15693         VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier),
15694         ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc);
15695     break;
15696   case OMPC_shared:
15697     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
15698     break;
15699   case OMPC_reduction:
15700     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown &&
15701            "Unexpected lastprivate modifier.");
15702     Res = ActOnOpenMPReductionClause(
15703         VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier),
15704         StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc,
15705         ReductionOrMapperIdScopeSpec, ReductionOrMapperId);
15706     break;
15707   case OMPC_task_reduction:
15708     Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
15709                                          EndLoc, ReductionOrMapperIdScopeSpec,
15710                                          ReductionOrMapperId);
15711     break;
15712   case OMPC_in_reduction:
15713     Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
15714                                        EndLoc, ReductionOrMapperIdScopeSpec,
15715                                        ReductionOrMapperId);
15716     break;
15717   case OMPC_linear:
15718     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown &&
15719            "Unexpected linear modifier.");
15720     Res = ActOnOpenMPLinearClause(
15721         VarList, DepModOrTailExpr, StartLoc, LParenLoc,
15722         static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc,
15723         ColonLoc, EndLoc);
15724     break;
15725   case OMPC_aligned:
15726     Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc,
15727                                    LParenLoc, ColonLoc, EndLoc);
15728     break;
15729   case OMPC_copyin:
15730     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
15731     break;
15732   case OMPC_copyprivate:
15733     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
15734     break;
15735   case OMPC_flush:
15736     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
15737     break;
15738   case OMPC_depend:
15739     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown &&
15740            "Unexpected depend modifier.");
15741     Res = ActOnOpenMPDependClause(
15742         DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier),
15743         ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc);
15744     break;
15745   case OMPC_map:
15746     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown &&
15747            "Unexpected map modifier.");
15748     Res = ActOnOpenMPMapClause(
15749         MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec,
15750         ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier),
15751         IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs);
15752     break;
15753   case OMPC_to:
15754     Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc,
15755                               ReductionOrMapperIdScopeSpec, ReductionOrMapperId,
15756                               ColonLoc, VarList, Locs);
15757     break;
15758   case OMPC_from:
15759     Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc,
15760                                 ReductionOrMapperIdScopeSpec,
15761                                 ReductionOrMapperId, ColonLoc, VarList, Locs);
15762     break;
15763   case OMPC_use_device_ptr:
15764     Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
15765     break;
15766   case OMPC_use_device_addr:
15767     Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs);
15768     break;
15769   case OMPC_is_device_ptr:
15770     Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
15771     break;
15772   case OMPC_allocate:
15773     Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc,
15774                                     LParenLoc, ColonLoc, EndLoc);
15775     break;
15776   case OMPC_nontemporal:
15777     Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc);
15778     break;
15779   case OMPC_inclusive:
15780     Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
15781     break;
15782   case OMPC_exclusive:
15783     Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
15784     break;
15785   case OMPC_affinity:
15786     Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc,
15787                                     DepModOrTailExpr, VarList);
15788     break;
15789   case OMPC_if:
15790   case OMPC_depobj:
15791   case OMPC_final:
15792   case OMPC_num_threads:
15793   case OMPC_safelen:
15794   case OMPC_simdlen:
15795   case OMPC_sizes:
15796   case OMPC_allocator:
15797   case OMPC_collapse:
15798   case OMPC_default:
15799   case OMPC_proc_bind:
15800   case OMPC_schedule:
15801   case OMPC_ordered:
15802   case OMPC_nowait:
15803   case OMPC_untied:
15804   case OMPC_mergeable:
15805   case OMPC_threadprivate:
15806   case OMPC_read:
15807   case OMPC_write:
15808   case OMPC_update:
15809   case OMPC_capture:
15810   case OMPC_seq_cst:
15811   case OMPC_acq_rel:
15812   case OMPC_acquire:
15813   case OMPC_release:
15814   case OMPC_relaxed:
15815   case OMPC_device:
15816   case OMPC_threads:
15817   case OMPC_simd:
15818   case OMPC_num_teams:
15819   case OMPC_thread_limit:
15820   case OMPC_priority:
15821   case OMPC_grainsize:
15822   case OMPC_nogroup:
15823   case OMPC_num_tasks:
15824   case OMPC_hint:
15825   case OMPC_dist_schedule:
15826   case OMPC_defaultmap:
15827   case OMPC_unknown:
15828   case OMPC_uniform:
15829   case OMPC_unified_address:
15830   case OMPC_unified_shared_memory:
15831   case OMPC_reverse_offload:
15832   case OMPC_dynamic_allocators:
15833   case OMPC_atomic_default_mem_order:
15834   case OMPC_device_type:
15835   case OMPC_match:
15836   case OMPC_order:
15837   case OMPC_destroy:
15838   case OMPC_novariants:
15839   case OMPC_nocontext:
15840   case OMPC_detach:
15841   case OMPC_uses_allocators:
15842   case OMPC_when:
15843   default:
15844     llvm_unreachable("Clause is not allowed.");
15845   }
15846   return Res;
15847 }
15848 
15849 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
15850                                        ExprObjectKind OK, SourceLocation Loc) {
15851   ExprResult Res = BuildDeclRefExpr(
15852       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
15853   if (!Res.isUsable())
15854     return ExprError();
15855   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
15856     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
15857     if (!Res.isUsable())
15858       return ExprError();
15859   }
15860   if (VK != VK_LValue && Res.get()->isGLValue()) {
15861     Res = DefaultLvalueConversion(Res.get());
15862     if (!Res.isUsable())
15863       return ExprError();
15864   }
15865   return Res;
15866 }
15867 
15868 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
15869                                           SourceLocation StartLoc,
15870                                           SourceLocation LParenLoc,
15871                                           SourceLocation EndLoc) {
15872   SmallVector<Expr *, 8> Vars;
15873   SmallVector<Expr *, 8> PrivateCopies;
15874   for (Expr *RefExpr : VarList) {
15875     assert(RefExpr && "NULL expr in OpenMP private clause.");
15876     SourceLocation ELoc;
15877     SourceRange ERange;
15878     Expr *SimpleRefExpr = RefExpr;
15879     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15880     if (Res.second) {
15881       // It will be analyzed later.
15882       Vars.push_back(RefExpr);
15883       PrivateCopies.push_back(nullptr);
15884     }
15885     ValueDecl *D = Res.first;
15886     if (!D)
15887       continue;
15888 
15889     QualType Type = D->getType();
15890     auto *VD = dyn_cast<VarDecl>(D);
15891 
15892     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
15893     //  A variable that appears in a private clause must not have an incomplete
15894     //  type or a reference type.
15895     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
15896       continue;
15897     Type = Type.getNonReferenceType();
15898 
15899     // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
15900     // A variable that is privatized must not have a const-qualified type
15901     // unless it is of class type with a mutable member. This restriction does
15902     // not apply to the firstprivate clause.
15903     //
15904     // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
15905     // A variable that appears in a private clause must not have a
15906     // const-qualified type unless it is of class type with a mutable member.
15907     if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
15908       continue;
15909 
15910     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
15911     // in a Construct]
15912     //  Variables with the predetermined data-sharing attributes may not be
15913     //  listed in data-sharing attributes clauses, except for the cases
15914     //  listed below. For these exceptions only, listing a predetermined
15915     //  variable in a data-sharing attribute clause is allowed and overrides
15916     //  the variable's predetermined data-sharing attributes.
15917     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
15918     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
15919       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
15920                                           << getOpenMPClauseName(OMPC_private);
15921       reportOriginalDsa(*this, DSAStack, D, DVar);
15922       continue;
15923     }
15924 
15925     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
15926     // Variably modified types are not supported for tasks.
15927     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
15928         isOpenMPTaskingDirective(CurrDir)) {
15929       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
15930           << getOpenMPClauseName(OMPC_private) << Type
15931           << getOpenMPDirectiveName(CurrDir);
15932       bool IsDecl =
15933           !VD ||
15934           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
15935       Diag(D->getLocation(),
15936            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
15937           << D;
15938       continue;
15939     }
15940 
15941     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
15942     // A list item cannot appear in both a map clause and a data-sharing
15943     // attribute clause on the same construct
15944     //
15945     // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
15946     // A list item cannot appear in both a map clause and a data-sharing
15947     // attribute clause on the same construct unless the construct is a
15948     // combined construct.
15949     if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) ||
15950         CurrDir == OMPD_target) {
15951       OpenMPClauseKind ConflictKind;
15952       if (DSAStack->checkMappableExprComponentListsForDecl(
15953               VD, /*CurrentRegionOnly=*/true,
15954               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
15955                   OpenMPClauseKind WhereFoundClauseKind) -> bool {
15956                 ConflictKind = WhereFoundClauseKind;
15957                 return true;
15958               })) {
15959         Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
15960             << getOpenMPClauseName(OMPC_private)
15961             << getOpenMPClauseName(ConflictKind)
15962             << getOpenMPDirectiveName(CurrDir);
15963         reportOriginalDsa(*this, DSAStack, D, DVar);
15964         continue;
15965       }
15966     }
15967 
15968     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
15969     //  A variable of class type (or array thereof) that appears in a private
15970     //  clause requires an accessible, unambiguous default constructor for the
15971     //  class type.
15972     // Generate helper private variable and initialize it with the default
15973     // value. The address of the original variable is replaced by the address of
15974     // the new private variable in CodeGen. This new variable is not added to
15975     // IdResolver, so the code in the OpenMP region uses original variable for
15976     // proper diagnostics.
15977     Type = Type.getUnqualifiedType();
15978     VarDecl *VDPrivate =
15979         buildVarDecl(*this, ELoc, Type, D->getName(),
15980                      D->hasAttrs() ? &D->getAttrs() : nullptr,
15981                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
15982     ActOnUninitializedDecl(VDPrivate);
15983     if (VDPrivate->isInvalidDecl())
15984       continue;
15985     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
15986         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
15987 
15988     DeclRefExpr *Ref = nullptr;
15989     if (!VD && !CurContext->isDependentContext())
15990       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
15991     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
15992     Vars.push_back((VD || CurContext->isDependentContext())
15993                        ? RefExpr->IgnoreParens()
15994                        : Ref);
15995     PrivateCopies.push_back(VDPrivateRefExpr);
15996   }
15997 
15998   if (Vars.empty())
15999     return nullptr;
16000 
16001   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
16002                                   PrivateCopies);
16003 }
16004 
16005 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
16006                                                SourceLocation StartLoc,
16007                                                SourceLocation LParenLoc,
16008                                                SourceLocation EndLoc) {
16009   SmallVector<Expr *, 8> Vars;
16010   SmallVector<Expr *, 8> PrivateCopies;
16011   SmallVector<Expr *, 8> Inits;
16012   SmallVector<Decl *, 4> ExprCaptures;
16013   bool IsImplicitClause =
16014       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
16015   SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
16016 
16017   for (Expr *RefExpr : VarList) {
16018     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
16019     SourceLocation ELoc;
16020     SourceRange ERange;
16021     Expr *SimpleRefExpr = RefExpr;
16022     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16023     if (Res.second) {
16024       // It will be analyzed later.
16025       Vars.push_back(RefExpr);
16026       PrivateCopies.push_back(nullptr);
16027       Inits.push_back(nullptr);
16028     }
16029     ValueDecl *D = Res.first;
16030     if (!D)
16031       continue;
16032 
16033     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
16034     QualType Type = D->getType();
16035     auto *VD = dyn_cast<VarDecl>(D);
16036 
16037     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
16038     //  A variable that appears in a private clause must not have an incomplete
16039     //  type or a reference type.
16040     if (RequireCompleteType(ELoc, Type,
16041                             diag::err_omp_firstprivate_incomplete_type))
16042       continue;
16043     Type = Type.getNonReferenceType();
16044 
16045     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
16046     //  A variable of class type (or array thereof) that appears in a private
16047     //  clause requires an accessible, unambiguous copy constructor for the
16048     //  class type.
16049     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
16050 
16051     // If an implicit firstprivate variable found it was checked already.
16052     DSAStackTy::DSAVarData TopDVar;
16053     if (!IsImplicitClause) {
16054       DSAStackTy::DSAVarData DVar =
16055           DSAStack->getTopDSA(D, /*FromParent=*/false);
16056       TopDVar = DVar;
16057       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
16058       bool IsConstant = ElemType.isConstant(Context);
16059       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
16060       //  A list item that specifies a given variable may not appear in more
16061       // than one clause on the same directive, except that a variable may be
16062       //  specified in both firstprivate and lastprivate clauses.
16063       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
16064       // A list item may appear in a firstprivate or lastprivate clause but not
16065       // both.
16066       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
16067           (isOpenMPDistributeDirective(CurrDir) ||
16068            DVar.CKind != OMPC_lastprivate) &&
16069           DVar.RefExpr) {
16070         Diag(ELoc, diag::err_omp_wrong_dsa)
16071             << getOpenMPClauseName(DVar.CKind)
16072             << getOpenMPClauseName(OMPC_firstprivate);
16073         reportOriginalDsa(*this, DSAStack, D, DVar);
16074         continue;
16075       }
16076 
16077       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
16078       // in a Construct]
16079       //  Variables with the predetermined data-sharing attributes may not be
16080       //  listed in data-sharing attributes clauses, except for the cases
16081       //  listed below. For these exceptions only, listing a predetermined
16082       //  variable in a data-sharing attribute clause is allowed and overrides
16083       //  the variable's predetermined data-sharing attributes.
16084       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
16085       // in a Construct, C/C++, p.2]
16086       //  Variables with const-qualified type having no mutable member may be
16087       //  listed in a firstprivate clause, even if they are static data members.
16088       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
16089           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
16090         Diag(ELoc, diag::err_omp_wrong_dsa)
16091             << getOpenMPClauseName(DVar.CKind)
16092             << getOpenMPClauseName(OMPC_firstprivate);
16093         reportOriginalDsa(*this, DSAStack, D, DVar);
16094         continue;
16095       }
16096 
16097       // OpenMP [2.9.3.4, Restrictions, p.2]
16098       //  A list item that is private within a parallel region must not appear
16099       //  in a firstprivate clause on a worksharing construct if any of the
16100       //  worksharing regions arising from the worksharing construct ever bind
16101       //  to any of the parallel regions arising from the parallel construct.
16102       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
16103       // A list item that is private within a teams region must not appear in a
16104       // firstprivate clause on a distribute construct if any of the distribute
16105       // regions arising from the distribute construct ever bind to any of the
16106       // teams regions arising from the teams construct.
16107       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
16108       // A list item that appears in a reduction clause of a teams construct
16109       // must not appear in a firstprivate clause on a distribute construct if
16110       // any of the distribute regions arising from the distribute construct
16111       // ever bind to any of the teams regions arising from the teams construct.
16112       if ((isOpenMPWorksharingDirective(CurrDir) ||
16113            isOpenMPDistributeDirective(CurrDir)) &&
16114           !isOpenMPParallelDirective(CurrDir) &&
16115           !isOpenMPTeamsDirective(CurrDir)) {
16116         DVar = DSAStack->getImplicitDSA(D, true);
16117         if (DVar.CKind != OMPC_shared &&
16118             (isOpenMPParallelDirective(DVar.DKind) ||
16119              isOpenMPTeamsDirective(DVar.DKind) ||
16120              DVar.DKind == OMPD_unknown)) {
16121           Diag(ELoc, diag::err_omp_required_access)
16122               << getOpenMPClauseName(OMPC_firstprivate)
16123               << getOpenMPClauseName(OMPC_shared);
16124           reportOriginalDsa(*this, DSAStack, D, DVar);
16125           continue;
16126         }
16127       }
16128       // OpenMP [2.9.3.4, Restrictions, p.3]
16129       //  A list item that appears in a reduction clause of a parallel construct
16130       //  must not appear in a firstprivate clause on a worksharing or task
16131       //  construct if any of the worksharing or task regions arising from the
16132       //  worksharing or task construct ever bind to any of the parallel regions
16133       //  arising from the parallel construct.
16134       // OpenMP [2.9.3.4, Restrictions, p.4]
16135       //  A list item that appears in a reduction clause in worksharing
16136       //  construct must not appear in a firstprivate clause in a task construct
16137       //  encountered during execution of any of the worksharing regions arising
16138       //  from the worksharing construct.
16139       if (isOpenMPTaskingDirective(CurrDir)) {
16140         DVar = DSAStack->hasInnermostDSA(
16141             D,
16142             [](OpenMPClauseKind C, bool AppliedToPointee) {
16143               return C == OMPC_reduction && !AppliedToPointee;
16144             },
16145             [](OpenMPDirectiveKind K) {
16146               return isOpenMPParallelDirective(K) ||
16147                      isOpenMPWorksharingDirective(K) ||
16148                      isOpenMPTeamsDirective(K);
16149             },
16150             /*FromParent=*/true);
16151         if (DVar.CKind == OMPC_reduction &&
16152             (isOpenMPParallelDirective(DVar.DKind) ||
16153              isOpenMPWorksharingDirective(DVar.DKind) ||
16154              isOpenMPTeamsDirective(DVar.DKind))) {
16155           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
16156               << getOpenMPDirectiveName(DVar.DKind);
16157           reportOriginalDsa(*this, DSAStack, D, DVar);
16158           continue;
16159         }
16160       }
16161 
16162       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
16163       // A list item cannot appear in both a map clause and a data-sharing
16164       // attribute clause on the same construct
16165       //
16166       // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
16167       // A list item cannot appear in both a map clause and a data-sharing
16168       // attribute clause on the same construct unless the construct is a
16169       // combined construct.
16170       if ((LangOpts.OpenMP <= 45 &&
16171            isOpenMPTargetExecutionDirective(CurrDir)) ||
16172           CurrDir == OMPD_target) {
16173         OpenMPClauseKind ConflictKind;
16174         if (DSAStack->checkMappableExprComponentListsForDecl(
16175                 VD, /*CurrentRegionOnly=*/true,
16176                 [&ConflictKind](
16177                     OMPClauseMappableExprCommon::MappableExprComponentListRef,
16178                     OpenMPClauseKind WhereFoundClauseKind) {
16179                   ConflictKind = WhereFoundClauseKind;
16180                   return true;
16181                 })) {
16182           Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
16183               << getOpenMPClauseName(OMPC_firstprivate)
16184               << getOpenMPClauseName(ConflictKind)
16185               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
16186           reportOriginalDsa(*this, DSAStack, D, DVar);
16187           continue;
16188         }
16189       }
16190     }
16191 
16192     // Variably modified types are not supported for tasks.
16193     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
16194         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
16195       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
16196           << getOpenMPClauseName(OMPC_firstprivate) << Type
16197           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
16198       bool IsDecl =
16199           !VD ||
16200           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
16201       Diag(D->getLocation(),
16202            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
16203           << D;
16204       continue;
16205     }
16206 
16207     Type = Type.getUnqualifiedType();
16208     VarDecl *VDPrivate =
16209         buildVarDecl(*this, ELoc, Type, D->getName(),
16210                      D->hasAttrs() ? &D->getAttrs() : nullptr,
16211                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
16212     // Generate helper private variable and initialize it with the value of the
16213     // original variable. The address of the original variable is replaced by
16214     // the address of the new private variable in the CodeGen. This new variable
16215     // is not added to IdResolver, so the code in the OpenMP region uses
16216     // original variable for proper diagnostics and variable capturing.
16217     Expr *VDInitRefExpr = nullptr;
16218     // For arrays generate initializer for single element and replace it by the
16219     // original array element in CodeGen.
16220     if (Type->isArrayType()) {
16221       VarDecl *VDInit =
16222           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
16223       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
16224       Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
16225       ElemType = ElemType.getUnqualifiedType();
16226       VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
16227                                          ".firstprivate.temp");
16228       InitializedEntity Entity =
16229           InitializedEntity::InitializeVariable(VDInitTemp);
16230       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
16231 
16232       InitializationSequence InitSeq(*this, Entity, Kind, Init);
16233       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
16234       if (Result.isInvalid())
16235         VDPrivate->setInvalidDecl();
16236       else
16237         VDPrivate->setInit(Result.getAs<Expr>());
16238       // Remove temp variable declaration.
16239       Context.Deallocate(VDInitTemp);
16240     } else {
16241       VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
16242                                      ".firstprivate.temp");
16243       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
16244                                        RefExpr->getExprLoc());
16245       AddInitializerToDecl(VDPrivate,
16246                            DefaultLvalueConversion(VDInitRefExpr).get(),
16247                            /*DirectInit=*/false);
16248     }
16249     if (VDPrivate->isInvalidDecl()) {
16250       if (IsImplicitClause) {
16251         Diag(RefExpr->getExprLoc(),
16252              diag::note_omp_task_predetermined_firstprivate_here);
16253       }
16254       continue;
16255     }
16256     CurContext->addDecl(VDPrivate);
16257     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
16258         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
16259         RefExpr->getExprLoc());
16260     DeclRefExpr *Ref = nullptr;
16261     if (!VD && !CurContext->isDependentContext()) {
16262       if (TopDVar.CKind == OMPC_lastprivate) {
16263         Ref = TopDVar.PrivateCopy;
16264       } else {
16265         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
16266         if (!isOpenMPCapturedDecl(D))
16267           ExprCaptures.push_back(Ref->getDecl());
16268       }
16269     }
16270     if (!IsImplicitClause)
16271       DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
16272     Vars.push_back((VD || CurContext->isDependentContext())
16273                        ? RefExpr->IgnoreParens()
16274                        : Ref);
16275     PrivateCopies.push_back(VDPrivateRefExpr);
16276     Inits.push_back(VDInitRefExpr);
16277   }
16278 
16279   if (Vars.empty())
16280     return nullptr;
16281 
16282   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
16283                                        Vars, PrivateCopies, Inits,
16284                                        buildPreInits(Context, ExprCaptures));
16285 }
16286 
16287 OMPClause *Sema::ActOnOpenMPLastprivateClause(
16288     ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind,
16289     SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc,
16290     SourceLocation LParenLoc, SourceLocation EndLoc) {
16291   if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) {
16292     assert(ColonLoc.isValid() && "Colon location must be valid.");
16293     Diag(LPKindLoc, diag::err_omp_unexpected_clause_value)
16294         << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0,
16295                                    /*Last=*/OMPC_LASTPRIVATE_unknown)
16296         << getOpenMPClauseName(OMPC_lastprivate);
16297     return nullptr;
16298   }
16299 
16300   SmallVector<Expr *, 8> Vars;
16301   SmallVector<Expr *, 8> SrcExprs;
16302   SmallVector<Expr *, 8> DstExprs;
16303   SmallVector<Expr *, 8> AssignmentOps;
16304   SmallVector<Decl *, 4> ExprCaptures;
16305   SmallVector<Expr *, 4> ExprPostUpdates;
16306   for (Expr *RefExpr : VarList) {
16307     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
16308     SourceLocation ELoc;
16309     SourceRange ERange;
16310     Expr *SimpleRefExpr = RefExpr;
16311     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16312     if (Res.second) {
16313       // It will be analyzed later.
16314       Vars.push_back(RefExpr);
16315       SrcExprs.push_back(nullptr);
16316       DstExprs.push_back(nullptr);
16317       AssignmentOps.push_back(nullptr);
16318     }
16319     ValueDecl *D = Res.first;
16320     if (!D)
16321       continue;
16322 
16323     QualType Type = D->getType();
16324     auto *VD = dyn_cast<VarDecl>(D);
16325 
16326     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
16327     //  A variable that appears in a lastprivate clause must not have an
16328     //  incomplete type or a reference type.
16329     if (RequireCompleteType(ELoc, Type,
16330                             diag::err_omp_lastprivate_incomplete_type))
16331       continue;
16332     Type = Type.getNonReferenceType();
16333 
16334     // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
16335     // A variable that is privatized must not have a const-qualified type
16336     // unless it is of class type with a mutable member. This restriction does
16337     // not apply to the firstprivate clause.
16338     //
16339     // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
16340     // A variable that appears in a lastprivate clause must not have a
16341     // const-qualified type unless it is of class type with a mutable member.
16342     if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
16343       continue;
16344 
16345     // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions]
16346     // A list item that appears in a lastprivate clause with the conditional
16347     // modifier must be a scalar variable.
16348     if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) {
16349       Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar);
16350       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
16351                                VarDecl::DeclarationOnly;
16352       Diag(D->getLocation(),
16353            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
16354           << D;
16355       continue;
16356     }
16357 
16358     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
16359     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
16360     // in a Construct]
16361     //  Variables with the predetermined data-sharing attributes may not be
16362     //  listed in data-sharing attributes clauses, except for the cases
16363     //  listed below.
16364     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
16365     // A list item may appear in a firstprivate or lastprivate clause but not
16366     // both.
16367     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
16368     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
16369         (isOpenMPDistributeDirective(CurrDir) ||
16370          DVar.CKind != OMPC_firstprivate) &&
16371         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
16372       Diag(ELoc, diag::err_omp_wrong_dsa)
16373           << getOpenMPClauseName(DVar.CKind)
16374           << getOpenMPClauseName(OMPC_lastprivate);
16375       reportOriginalDsa(*this, DSAStack, D, DVar);
16376       continue;
16377     }
16378 
16379     // OpenMP [2.14.3.5, Restrictions, p.2]
16380     // A list item that is private within a parallel region, or that appears in
16381     // the reduction clause of a parallel construct, must not appear in a
16382     // lastprivate clause on a worksharing construct if any of the corresponding
16383     // worksharing regions ever binds to any of the corresponding parallel
16384     // regions.
16385     DSAStackTy::DSAVarData TopDVar = DVar;
16386     if (isOpenMPWorksharingDirective(CurrDir) &&
16387         !isOpenMPParallelDirective(CurrDir) &&
16388         !isOpenMPTeamsDirective(CurrDir)) {
16389       DVar = DSAStack->getImplicitDSA(D, true);
16390       if (DVar.CKind != OMPC_shared) {
16391         Diag(ELoc, diag::err_omp_required_access)
16392             << getOpenMPClauseName(OMPC_lastprivate)
16393             << getOpenMPClauseName(OMPC_shared);
16394         reportOriginalDsa(*this, DSAStack, D, DVar);
16395         continue;
16396       }
16397     }
16398 
16399     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
16400     //  A variable of class type (or array thereof) that appears in a
16401     //  lastprivate clause requires an accessible, unambiguous default
16402     //  constructor for the class type, unless the list item is also specified
16403     //  in a firstprivate clause.
16404     //  A variable of class type (or array thereof) that appears in a
16405     //  lastprivate clause requires an accessible, unambiguous copy assignment
16406     //  operator for the class type.
16407     Type = Context.getBaseElementType(Type).getNonReferenceType();
16408     VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
16409                                   Type.getUnqualifiedType(), ".lastprivate.src",
16410                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
16411     DeclRefExpr *PseudoSrcExpr =
16412         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
16413     VarDecl *DstVD =
16414         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
16415                      D->hasAttrs() ? &D->getAttrs() : nullptr);
16416     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
16417     // For arrays generate assignment operation for single element and replace
16418     // it by the original array element in CodeGen.
16419     ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
16420                                          PseudoDstExpr, PseudoSrcExpr);
16421     if (AssignmentOp.isInvalid())
16422       continue;
16423     AssignmentOp =
16424         ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
16425     if (AssignmentOp.isInvalid())
16426       continue;
16427 
16428     DeclRefExpr *Ref = nullptr;
16429     if (!VD && !CurContext->isDependentContext()) {
16430       if (TopDVar.CKind == OMPC_firstprivate) {
16431         Ref = TopDVar.PrivateCopy;
16432       } else {
16433         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
16434         if (!isOpenMPCapturedDecl(D))
16435           ExprCaptures.push_back(Ref->getDecl());
16436       }
16437       if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) ||
16438           (!isOpenMPCapturedDecl(D) &&
16439            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
16440         ExprResult RefRes = DefaultLvalueConversion(Ref);
16441         if (!RefRes.isUsable())
16442           continue;
16443         ExprResult PostUpdateRes =
16444             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
16445                        RefRes.get());
16446         if (!PostUpdateRes.isUsable())
16447           continue;
16448         ExprPostUpdates.push_back(
16449             IgnoredValueConversions(PostUpdateRes.get()).get());
16450       }
16451     }
16452     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
16453     Vars.push_back((VD || CurContext->isDependentContext())
16454                        ? RefExpr->IgnoreParens()
16455                        : Ref);
16456     SrcExprs.push_back(PseudoSrcExpr);
16457     DstExprs.push_back(PseudoDstExpr);
16458     AssignmentOps.push_back(AssignmentOp.get());
16459   }
16460 
16461   if (Vars.empty())
16462     return nullptr;
16463 
16464   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
16465                                       Vars, SrcExprs, DstExprs, AssignmentOps,
16466                                       LPKind, LPKindLoc, ColonLoc,
16467                                       buildPreInits(Context, ExprCaptures),
16468                                       buildPostUpdate(*this, ExprPostUpdates));
16469 }
16470 
16471 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
16472                                          SourceLocation StartLoc,
16473                                          SourceLocation LParenLoc,
16474                                          SourceLocation EndLoc) {
16475   SmallVector<Expr *, 8> Vars;
16476   for (Expr *RefExpr : VarList) {
16477     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
16478     SourceLocation ELoc;
16479     SourceRange ERange;
16480     Expr *SimpleRefExpr = RefExpr;
16481     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16482     if (Res.second) {
16483       // It will be analyzed later.
16484       Vars.push_back(RefExpr);
16485     }
16486     ValueDecl *D = Res.first;
16487     if (!D)
16488       continue;
16489 
16490     auto *VD = dyn_cast<VarDecl>(D);
16491     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
16492     // in a Construct]
16493     //  Variables with the predetermined data-sharing attributes may not be
16494     //  listed in data-sharing attributes clauses, except for the cases
16495     //  listed below. For these exceptions only, listing a predetermined
16496     //  variable in a data-sharing attribute clause is allowed and overrides
16497     //  the variable's predetermined data-sharing attributes.
16498     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
16499     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
16500         DVar.RefExpr) {
16501       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
16502                                           << getOpenMPClauseName(OMPC_shared);
16503       reportOriginalDsa(*this, DSAStack, D, DVar);
16504       continue;
16505     }
16506 
16507     DeclRefExpr *Ref = nullptr;
16508     if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
16509       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
16510     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
16511     Vars.push_back((VD || !Ref || CurContext->isDependentContext())
16512                        ? RefExpr->IgnoreParens()
16513                        : Ref);
16514   }
16515 
16516   if (Vars.empty())
16517     return nullptr;
16518 
16519   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
16520 }
16521 
16522 namespace {
16523 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
16524   DSAStackTy *Stack;
16525 
16526 public:
16527   bool VisitDeclRefExpr(DeclRefExpr *E) {
16528     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
16529       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
16530       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
16531         return false;
16532       if (DVar.CKind != OMPC_unknown)
16533         return true;
16534       DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
16535           VD,
16536           [](OpenMPClauseKind C, bool AppliedToPointee) {
16537             return isOpenMPPrivate(C) && !AppliedToPointee;
16538           },
16539           [](OpenMPDirectiveKind) { return true; },
16540           /*FromParent=*/true);
16541       return DVarPrivate.CKind != OMPC_unknown;
16542     }
16543     return false;
16544   }
16545   bool VisitStmt(Stmt *S) {
16546     for (Stmt *Child : S->children()) {
16547       if (Child && Visit(Child))
16548         return true;
16549     }
16550     return false;
16551   }
16552   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
16553 };
16554 } // namespace
16555 
16556 namespace {
16557 // Transform MemberExpression for specified FieldDecl of current class to
16558 // DeclRefExpr to specified OMPCapturedExprDecl.
16559 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
16560   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
16561   ValueDecl *Field = nullptr;
16562   DeclRefExpr *CapturedExpr = nullptr;
16563 
16564 public:
16565   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
16566       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
16567 
16568   ExprResult TransformMemberExpr(MemberExpr *E) {
16569     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
16570         E->getMemberDecl() == Field) {
16571       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
16572       return CapturedExpr;
16573     }
16574     return BaseTransform::TransformMemberExpr(E);
16575   }
16576   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
16577 };
16578 } // namespace
16579 
16580 template <typename T, typename U>
16581 static T filterLookupForUDReductionAndMapper(
16582     SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
16583   for (U &Set : Lookups) {
16584     for (auto *D : Set) {
16585       if (T Res = Gen(cast<ValueDecl>(D)))
16586         return Res;
16587     }
16588   }
16589   return T();
16590 }
16591 
16592 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
16593   assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
16594 
16595   for (auto RD : D->redecls()) {
16596     // Don't bother with extra checks if we already know this one isn't visible.
16597     if (RD == D)
16598       continue;
16599 
16600     auto ND = cast<NamedDecl>(RD);
16601     if (LookupResult::isVisible(SemaRef, ND))
16602       return ND;
16603   }
16604 
16605   return nullptr;
16606 }
16607 
16608 static void
16609 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
16610                         SourceLocation Loc, QualType Ty,
16611                         SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
16612   // Find all of the associated namespaces and classes based on the
16613   // arguments we have.
16614   Sema::AssociatedNamespaceSet AssociatedNamespaces;
16615   Sema::AssociatedClassSet AssociatedClasses;
16616   OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
16617   SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
16618                                              AssociatedClasses);
16619 
16620   // C++ [basic.lookup.argdep]p3:
16621   //   Let X be the lookup set produced by unqualified lookup (3.4.1)
16622   //   and let Y be the lookup set produced by argument dependent
16623   //   lookup (defined as follows). If X contains [...] then Y is
16624   //   empty. Otherwise Y is the set of declarations found in the
16625   //   namespaces associated with the argument types as described
16626   //   below. The set of declarations found by the lookup of the name
16627   //   is the union of X and Y.
16628   //
16629   // Here, we compute Y and add its members to the overloaded
16630   // candidate set.
16631   for (auto *NS : AssociatedNamespaces) {
16632     //   When considering an associated namespace, the lookup is the
16633     //   same as the lookup performed when the associated namespace is
16634     //   used as a qualifier (3.4.3.2) except that:
16635     //
16636     //     -- Any using-directives in the associated namespace are
16637     //        ignored.
16638     //
16639     //     -- Any namespace-scope friend functions declared in
16640     //        associated classes are visible within their respective
16641     //        namespaces even if they are not visible during an ordinary
16642     //        lookup (11.4).
16643     DeclContext::lookup_result R = NS->lookup(Id.getName());
16644     for (auto *D : R) {
16645       auto *Underlying = D;
16646       if (auto *USD = dyn_cast<UsingShadowDecl>(D))
16647         Underlying = USD->getTargetDecl();
16648 
16649       if (!isa<OMPDeclareReductionDecl>(Underlying) &&
16650           !isa<OMPDeclareMapperDecl>(Underlying))
16651         continue;
16652 
16653       if (!SemaRef.isVisible(D)) {
16654         D = findAcceptableDecl(SemaRef, D);
16655         if (!D)
16656           continue;
16657         if (auto *USD = dyn_cast<UsingShadowDecl>(D))
16658           Underlying = USD->getTargetDecl();
16659       }
16660       Lookups.emplace_back();
16661       Lookups.back().addDecl(Underlying);
16662     }
16663   }
16664 }
16665 
16666 static ExprResult
16667 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
16668                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
16669                          const DeclarationNameInfo &ReductionId, QualType Ty,
16670                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
16671   if (ReductionIdScopeSpec.isInvalid())
16672     return ExprError();
16673   SmallVector<UnresolvedSet<8>, 4> Lookups;
16674   if (S) {
16675     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
16676     Lookup.suppressDiagnostics();
16677     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
16678       NamedDecl *D = Lookup.getRepresentativeDecl();
16679       do {
16680         S = S->getParent();
16681       } while (S && !S->isDeclScope(D));
16682       if (S)
16683         S = S->getParent();
16684       Lookups.emplace_back();
16685       Lookups.back().append(Lookup.begin(), Lookup.end());
16686       Lookup.clear();
16687     }
16688   } else if (auto *ULE =
16689                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
16690     Lookups.push_back(UnresolvedSet<8>());
16691     Decl *PrevD = nullptr;
16692     for (NamedDecl *D : ULE->decls()) {
16693       if (D == PrevD)
16694         Lookups.push_back(UnresolvedSet<8>());
16695       else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D))
16696         Lookups.back().addDecl(DRD);
16697       PrevD = D;
16698     }
16699   }
16700   if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
16701       Ty->isInstantiationDependentType() ||
16702       Ty->containsUnexpandedParameterPack() ||
16703       filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
16704         return !D->isInvalidDecl() &&
16705                (D->getType()->isDependentType() ||
16706                 D->getType()->isInstantiationDependentType() ||
16707                 D->getType()->containsUnexpandedParameterPack());
16708       })) {
16709     UnresolvedSet<8> ResSet;
16710     for (const UnresolvedSet<8> &Set : Lookups) {
16711       if (Set.empty())
16712         continue;
16713       ResSet.append(Set.begin(), Set.end());
16714       // The last item marks the end of all declarations at the specified scope.
16715       ResSet.addDecl(Set[Set.size() - 1]);
16716     }
16717     return UnresolvedLookupExpr::Create(
16718         SemaRef.Context, /*NamingClass=*/nullptr,
16719         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
16720         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
16721   }
16722   // Lookup inside the classes.
16723   // C++ [over.match.oper]p3:
16724   //   For a unary operator @ with an operand of a type whose
16725   //   cv-unqualified version is T1, and for a binary operator @ with
16726   //   a left operand of a type whose cv-unqualified version is T1 and
16727   //   a right operand of a type whose cv-unqualified version is T2,
16728   //   three sets of candidate functions, designated member
16729   //   candidates, non-member candidates and built-in candidates, are
16730   //   constructed as follows:
16731   //     -- If T1 is a complete class type or a class currently being
16732   //        defined, the set of member candidates is the result of the
16733   //        qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
16734   //        the set of member candidates is empty.
16735   LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
16736   Lookup.suppressDiagnostics();
16737   if (const auto *TyRec = Ty->getAs<RecordType>()) {
16738     // Complete the type if it can be completed.
16739     // If the type is neither complete nor being defined, bail out now.
16740     if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
16741         TyRec->getDecl()->getDefinition()) {
16742       Lookup.clear();
16743       SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
16744       if (Lookup.empty()) {
16745         Lookups.emplace_back();
16746         Lookups.back().append(Lookup.begin(), Lookup.end());
16747       }
16748     }
16749   }
16750   // Perform ADL.
16751   if (SemaRef.getLangOpts().CPlusPlus)
16752     argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
16753   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
16754           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
16755             if (!D->isInvalidDecl() &&
16756                 SemaRef.Context.hasSameType(D->getType(), Ty))
16757               return D;
16758             return nullptr;
16759           }))
16760     return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
16761                                     VK_LValue, Loc);
16762   if (SemaRef.getLangOpts().CPlusPlus) {
16763     if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
16764             Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
16765               if (!D->isInvalidDecl() &&
16766                   SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
16767                   !Ty.isMoreQualifiedThan(D->getType()))
16768                 return D;
16769               return nullptr;
16770             })) {
16771       CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
16772                          /*DetectVirtual=*/false);
16773       if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
16774         if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
16775                 VD->getType().getUnqualifiedType()))) {
16776           if (SemaRef.CheckBaseClassAccess(
16777                   Loc, VD->getType(), Ty, Paths.front(),
16778                   /*DiagID=*/0) != Sema::AR_inaccessible) {
16779             SemaRef.BuildBasePathArray(Paths, BasePath);
16780             return SemaRef.BuildDeclRefExpr(
16781                 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
16782           }
16783         }
16784       }
16785     }
16786   }
16787   if (ReductionIdScopeSpec.isSet()) {
16788     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier)
16789         << Ty << Range;
16790     return ExprError();
16791   }
16792   return ExprEmpty();
16793 }
16794 
16795 namespace {
16796 /// Data for the reduction-based clauses.
16797 struct ReductionData {
16798   /// List of original reduction items.
16799   SmallVector<Expr *, 8> Vars;
16800   /// List of private copies of the reduction items.
16801   SmallVector<Expr *, 8> Privates;
16802   /// LHS expressions for the reduction_op expressions.
16803   SmallVector<Expr *, 8> LHSs;
16804   /// RHS expressions for the reduction_op expressions.
16805   SmallVector<Expr *, 8> RHSs;
16806   /// Reduction operation expression.
16807   SmallVector<Expr *, 8> ReductionOps;
16808   /// inscan copy operation expressions.
16809   SmallVector<Expr *, 8> InscanCopyOps;
16810   /// inscan copy temp array expressions for prefix sums.
16811   SmallVector<Expr *, 8> InscanCopyArrayTemps;
16812   /// inscan copy temp array element expressions for prefix sums.
16813   SmallVector<Expr *, 8> InscanCopyArrayElems;
16814   /// Taskgroup descriptors for the corresponding reduction items in
16815   /// in_reduction clauses.
16816   SmallVector<Expr *, 8> TaskgroupDescriptors;
16817   /// List of captures for clause.
16818   SmallVector<Decl *, 4> ExprCaptures;
16819   /// List of postupdate expressions.
16820   SmallVector<Expr *, 4> ExprPostUpdates;
16821   /// Reduction modifier.
16822   unsigned RedModifier = 0;
16823   ReductionData() = delete;
16824   /// Reserves required memory for the reduction data.
16825   ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) {
16826     Vars.reserve(Size);
16827     Privates.reserve(Size);
16828     LHSs.reserve(Size);
16829     RHSs.reserve(Size);
16830     ReductionOps.reserve(Size);
16831     if (RedModifier == OMPC_REDUCTION_inscan) {
16832       InscanCopyOps.reserve(Size);
16833       InscanCopyArrayTemps.reserve(Size);
16834       InscanCopyArrayElems.reserve(Size);
16835     }
16836     TaskgroupDescriptors.reserve(Size);
16837     ExprCaptures.reserve(Size);
16838     ExprPostUpdates.reserve(Size);
16839   }
16840   /// Stores reduction item and reduction operation only (required for dependent
16841   /// reduction item).
16842   void push(Expr *Item, Expr *ReductionOp) {
16843     Vars.emplace_back(Item);
16844     Privates.emplace_back(nullptr);
16845     LHSs.emplace_back(nullptr);
16846     RHSs.emplace_back(nullptr);
16847     ReductionOps.emplace_back(ReductionOp);
16848     TaskgroupDescriptors.emplace_back(nullptr);
16849     if (RedModifier == OMPC_REDUCTION_inscan) {
16850       InscanCopyOps.push_back(nullptr);
16851       InscanCopyArrayTemps.push_back(nullptr);
16852       InscanCopyArrayElems.push_back(nullptr);
16853     }
16854   }
16855   /// Stores reduction data.
16856   void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
16857             Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp,
16858             Expr *CopyArrayElem) {
16859     Vars.emplace_back(Item);
16860     Privates.emplace_back(Private);
16861     LHSs.emplace_back(LHS);
16862     RHSs.emplace_back(RHS);
16863     ReductionOps.emplace_back(ReductionOp);
16864     TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
16865     if (RedModifier == OMPC_REDUCTION_inscan) {
16866       InscanCopyOps.push_back(CopyOp);
16867       InscanCopyArrayTemps.push_back(CopyArrayTemp);
16868       InscanCopyArrayElems.push_back(CopyArrayElem);
16869     } else {
16870       assert(CopyOp == nullptr && CopyArrayTemp == nullptr &&
16871              CopyArrayElem == nullptr &&
16872              "Copy operation must be used for inscan reductions only.");
16873     }
16874   }
16875 };
16876 } // namespace
16877 
16878 static bool checkOMPArraySectionConstantForReduction(
16879     ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
16880     SmallVectorImpl<llvm::APSInt> &ArraySizes) {
16881   const Expr *Length = OASE->getLength();
16882   if (Length == nullptr) {
16883     // For array sections of the form [1:] or [:], we would need to analyze
16884     // the lower bound...
16885     if (OASE->getColonLocFirst().isValid())
16886       return false;
16887 
16888     // This is an array subscript which has implicit length 1!
16889     SingleElement = true;
16890     ArraySizes.push_back(llvm::APSInt::get(1));
16891   } else {
16892     Expr::EvalResult Result;
16893     if (!Length->EvaluateAsInt(Result, Context))
16894       return false;
16895 
16896     llvm::APSInt ConstantLengthValue = Result.Val.getInt();
16897     SingleElement = (ConstantLengthValue.getSExtValue() == 1);
16898     ArraySizes.push_back(ConstantLengthValue);
16899   }
16900 
16901   // Get the base of this array section and walk up from there.
16902   const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
16903 
16904   // We require length = 1 for all array sections except the right-most to
16905   // guarantee that the memory region is contiguous and has no holes in it.
16906   while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
16907     Length = TempOASE->getLength();
16908     if (Length == nullptr) {
16909       // For array sections of the form [1:] or [:], we would need to analyze
16910       // the lower bound...
16911       if (OASE->getColonLocFirst().isValid())
16912         return false;
16913 
16914       // This is an array subscript which has implicit length 1!
16915       ArraySizes.push_back(llvm::APSInt::get(1));
16916     } else {
16917       Expr::EvalResult Result;
16918       if (!Length->EvaluateAsInt(Result, Context))
16919         return false;
16920 
16921       llvm::APSInt ConstantLengthValue = Result.Val.getInt();
16922       if (ConstantLengthValue.getSExtValue() != 1)
16923         return false;
16924 
16925       ArraySizes.push_back(ConstantLengthValue);
16926     }
16927     Base = TempOASE->getBase()->IgnoreParenImpCasts();
16928   }
16929 
16930   // If we have a single element, we don't need to add the implicit lengths.
16931   if (!SingleElement) {
16932     while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
16933       // Has implicit length 1!
16934       ArraySizes.push_back(llvm::APSInt::get(1));
16935       Base = TempASE->getBase()->IgnoreParenImpCasts();
16936     }
16937   }
16938 
16939   // This array section can be privatized as a single value or as a constant
16940   // sized array.
16941   return true;
16942 }
16943 
16944 static BinaryOperatorKind
16945 getRelatedCompoundReductionOp(BinaryOperatorKind BOK) {
16946   if (BOK == BO_Add)
16947     return BO_AddAssign;
16948   if (BOK == BO_Mul)
16949     return BO_MulAssign;
16950   if (BOK == BO_And)
16951     return BO_AndAssign;
16952   if (BOK == BO_Or)
16953     return BO_OrAssign;
16954   if (BOK == BO_Xor)
16955     return BO_XorAssign;
16956   return BOK;
16957 }
16958 
16959 static bool actOnOMPReductionKindClause(
16960     Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
16961     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
16962     SourceLocation ColonLoc, SourceLocation EndLoc,
16963     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
16964     ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
16965   DeclarationName DN = ReductionId.getName();
16966   OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
16967   BinaryOperatorKind BOK = BO_Comma;
16968 
16969   ASTContext &Context = S.Context;
16970   // OpenMP [2.14.3.6, reduction clause]
16971   // C
16972   // reduction-identifier is either an identifier or one of the following
16973   // operators: +, -, *,  &, |, ^, && and ||
16974   // C++
16975   // reduction-identifier is either an id-expression or one of the following
16976   // operators: +, -, *, &, |, ^, && and ||
16977   switch (OOK) {
16978   case OO_Plus:
16979   case OO_Minus:
16980     BOK = BO_Add;
16981     break;
16982   case OO_Star:
16983     BOK = BO_Mul;
16984     break;
16985   case OO_Amp:
16986     BOK = BO_And;
16987     break;
16988   case OO_Pipe:
16989     BOK = BO_Or;
16990     break;
16991   case OO_Caret:
16992     BOK = BO_Xor;
16993     break;
16994   case OO_AmpAmp:
16995     BOK = BO_LAnd;
16996     break;
16997   case OO_PipePipe:
16998     BOK = BO_LOr;
16999     break;
17000   case OO_New:
17001   case OO_Delete:
17002   case OO_Array_New:
17003   case OO_Array_Delete:
17004   case OO_Slash:
17005   case OO_Percent:
17006   case OO_Tilde:
17007   case OO_Exclaim:
17008   case OO_Equal:
17009   case OO_Less:
17010   case OO_Greater:
17011   case OO_LessEqual:
17012   case OO_GreaterEqual:
17013   case OO_PlusEqual:
17014   case OO_MinusEqual:
17015   case OO_StarEqual:
17016   case OO_SlashEqual:
17017   case OO_PercentEqual:
17018   case OO_CaretEqual:
17019   case OO_AmpEqual:
17020   case OO_PipeEqual:
17021   case OO_LessLess:
17022   case OO_GreaterGreater:
17023   case OO_LessLessEqual:
17024   case OO_GreaterGreaterEqual:
17025   case OO_EqualEqual:
17026   case OO_ExclaimEqual:
17027   case OO_Spaceship:
17028   case OO_PlusPlus:
17029   case OO_MinusMinus:
17030   case OO_Comma:
17031   case OO_ArrowStar:
17032   case OO_Arrow:
17033   case OO_Call:
17034   case OO_Subscript:
17035   case OO_Conditional:
17036   case OO_Coawait:
17037   case NUM_OVERLOADED_OPERATORS:
17038     llvm_unreachable("Unexpected reduction identifier");
17039   case OO_None:
17040     if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
17041       if (II->isStr("max"))
17042         BOK = BO_GT;
17043       else if (II->isStr("min"))
17044         BOK = BO_LT;
17045     }
17046     break;
17047   }
17048   SourceRange ReductionIdRange;
17049   if (ReductionIdScopeSpec.isValid())
17050     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
17051   else
17052     ReductionIdRange.setBegin(ReductionId.getBeginLoc());
17053   ReductionIdRange.setEnd(ReductionId.getEndLoc());
17054 
17055   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
17056   bool FirstIter = true;
17057   for (Expr *RefExpr : VarList) {
17058     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
17059     // OpenMP [2.1, C/C++]
17060     //  A list item is a variable or array section, subject to the restrictions
17061     //  specified in Section 2.4 on page 42 and in each of the sections
17062     // describing clauses and directives for which a list appears.
17063     // OpenMP  [2.14.3.3, Restrictions, p.1]
17064     //  A variable that is part of another variable (as an array or
17065     //  structure element) cannot appear in a private clause.
17066     if (!FirstIter && IR != ER)
17067       ++IR;
17068     FirstIter = false;
17069     SourceLocation ELoc;
17070     SourceRange ERange;
17071     Expr *SimpleRefExpr = RefExpr;
17072     auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
17073                               /*AllowArraySection=*/true);
17074     if (Res.second) {
17075       // Try to find 'declare reduction' corresponding construct before using
17076       // builtin/overloaded operators.
17077       QualType Type = Context.DependentTy;
17078       CXXCastPath BasePath;
17079       ExprResult DeclareReductionRef = buildDeclareReductionRef(
17080           S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
17081           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
17082       Expr *ReductionOp = nullptr;
17083       if (S.CurContext->isDependentContext() &&
17084           (DeclareReductionRef.isUnset() ||
17085            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
17086         ReductionOp = DeclareReductionRef.get();
17087       // It will be analyzed later.
17088       RD.push(RefExpr, ReductionOp);
17089     }
17090     ValueDecl *D = Res.first;
17091     if (!D)
17092       continue;
17093 
17094     Expr *TaskgroupDescriptor = nullptr;
17095     QualType Type;
17096     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
17097     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
17098     if (ASE) {
17099       Type = ASE->getType().getNonReferenceType();
17100     } else if (OASE) {
17101       QualType BaseType =
17102           OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
17103       if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
17104         Type = ATy->getElementType();
17105       else
17106         Type = BaseType->getPointeeType();
17107       Type = Type.getNonReferenceType();
17108     } else {
17109       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
17110     }
17111     auto *VD = dyn_cast<VarDecl>(D);
17112 
17113     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
17114     //  A variable that appears in a private clause must not have an incomplete
17115     //  type or a reference type.
17116     if (S.RequireCompleteType(ELoc, D->getType(),
17117                               diag::err_omp_reduction_incomplete_type))
17118       continue;
17119     // OpenMP [2.14.3.6, reduction clause, Restrictions]
17120     // A list item that appears in a reduction clause must not be
17121     // const-qualified.
17122     if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
17123                                   /*AcceptIfMutable*/ false, ASE || OASE))
17124       continue;
17125 
17126     OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
17127     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
17128     //  If a list-item is a reference type then it must bind to the same object
17129     //  for all threads of the team.
17130     if (!ASE && !OASE) {
17131       if (VD) {
17132         VarDecl *VDDef = VD->getDefinition();
17133         if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
17134           DSARefChecker Check(Stack);
17135           if (Check.Visit(VDDef->getInit())) {
17136             S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
17137                 << getOpenMPClauseName(ClauseKind) << ERange;
17138             S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
17139             continue;
17140           }
17141         }
17142       }
17143 
17144       // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
17145       // in a Construct]
17146       //  Variables with the predetermined data-sharing attributes may not be
17147       //  listed in data-sharing attributes clauses, except for the cases
17148       //  listed below. For these exceptions only, listing a predetermined
17149       //  variable in a data-sharing attribute clause is allowed and overrides
17150       //  the variable's predetermined data-sharing attributes.
17151       // OpenMP [2.14.3.6, Restrictions, p.3]
17152       //  Any number of reduction clauses can be specified on the directive,
17153       //  but a list item can appear only once in the reduction clauses for that
17154       //  directive.
17155       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
17156       if (DVar.CKind == OMPC_reduction) {
17157         S.Diag(ELoc, diag::err_omp_once_referenced)
17158             << getOpenMPClauseName(ClauseKind);
17159         if (DVar.RefExpr)
17160           S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
17161         continue;
17162       }
17163       if (DVar.CKind != OMPC_unknown) {
17164         S.Diag(ELoc, diag::err_omp_wrong_dsa)
17165             << getOpenMPClauseName(DVar.CKind)
17166             << getOpenMPClauseName(OMPC_reduction);
17167         reportOriginalDsa(S, Stack, D, DVar);
17168         continue;
17169       }
17170 
17171       // OpenMP [2.14.3.6, Restrictions, p.1]
17172       //  A list item that appears in a reduction clause of a worksharing
17173       //  construct must be shared in the parallel regions to which any of the
17174       //  worksharing regions arising from the worksharing construct bind.
17175       if (isOpenMPWorksharingDirective(CurrDir) &&
17176           !isOpenMPParallelDirective(CurrDir) &&
17177           !isOpenMPTeamsDirective(CurrDir)) {
17178         DVar = Stack->getImplicitDSA(D, true);
17179         if (DVar.CKind != OMPC_shared) {
17180           S.Diag(ELoc, diag::err_omp_required_access)
17181               << getOpenMPClauseName(OMPC_reduction)
17182               << getOpenMPClauseName(OMPC_shared);
17183           reportOriginalDsa(S, Stack, D, DVar);
17184           continue;
17185         }
17186       }
17187     } else {
17188       // Threadprivates cannot be shared between threads, so dignose if the base
17189       // is a threadprivate variable.
17190       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
17191       if (DVar.CKind == OMPC_threadprivate) {
17192         S.Diag(ELoc, diag::err_omp_wrong_dsa)
17193             << getOpenMPClauseName(DVar.CKind)
17194             << getOpenMPClauseName(OMPC_reduction);
17195         reportOriginalDsa(S, Stack, D, DVar);
17196         continue;
17197       }
17198     }
17199 
17200     // Try to find 'declare reduction' corresponding construct before using
17201     // builtin/overloaded operators.
17202     CXXCastPath BasePath;
17203     ExprResult DeclareReductionRef = buildDeclareReductionRef(
17204         S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
17205         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
17206     if (DeclareReductionRef.isInvalid())
17207       continue;
17208     if (S.CurContext->isDependentContext() &&
17209         (DeclareReductionRef.isUnset() ||
17210          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
17211       RD.push(RefExpr, DeclareReductionRef.get());
17212       continue;
17213     }
17214     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
17215       // Not allowed reduction identifier is found.
17216       S.Diag(ReductionId.getBeginLoc(),
17217              diag::err_omp_unknown_reduction_identifier)
17218           << Type << ReductionIdRange;
17219       continue;
17220     }
17221 
17222     // OpenMP [2.14.3.6, reduction clause, Restrictions]
17223     // The type of a list item that appears in a reduction clause must be valid
17224     // for the reduction-identifier. For a max or min reduction in C, the type
17225     // of the list item must be an allowed arithmetic data type: char, int,
17226     // float, double, or _Bool, possibly modified with long, short, signed, or
17227     // unsigned. For a max or min reduction in C++, the type of the list item
17228     // must be an allowed arithmetic data type: char, wchar_t, int, float,
17229     // double, or bool, possibly modified with long, short, signed, or unsigned.
17230     if (DeclareReductionRef.isUnset()) {
17231       if ((BOK == BO_GT || BOK == BO_LT) &&
17232           !(Type->isScalarType() ||
17233             (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
17234         S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
17235             << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
17236         if (!ASE && !OASE) {
17237           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
17238                                    VarDecl::DeclarationOnly;
17239           S.Diag(D->getLocation(),
17240                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
17241               << D;
17242         }
17243         continue;
17244       }
17245       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
17246           !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
17247         S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
17248             << getOpenMPClauseName(ClauseKind);
17249         if (!ASE && !OASE) {
17250           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
17251                                    VarDecl::DeclarationOnly;
17252           S.Diag(D->getLocation(),
17253                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
17254               << D;
17255         }
17256         continue;
17257       }
17258     }
17259 
17260     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
17261     VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
17262                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
17263     VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
17264                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
17265     QualType PrivateTy = Type;
17266 
17267     // Try if we can determine constant lengths for all array sections and avoid
17268     // the VLA.
17269     bool ConstantLengthOASE = false;
17270     if (OASE) {
17271       bool SingleElement;
17272       llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
17273       ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
17274           Context, OASE, SingleElement, ArraySizes);
17275 
17276       // If we don't have a single element, we must emit a constant array type.
17277       if (ConstantLengthOASE && !SingleElement) {
17278         for (llvm::APSInt &Size : ArraySizes)
17279           PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr,
17280                                                    ArrayType::Normal,
17281                                                    /*IndexTypeQuals=*/0);
17282       }
17283     }
17284 
17285     if ((OASE && !ConstantLengthOASE) ||
17286         (!OASE && !ASE &&
17287          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
17288       if (!Context.getTargetInfo().isVLASupported()) {
17289         if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) {
17290           S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
17291           S.Diag(ELoc, diag::note_vla_unsupported);
17292           continue;
17293         } else {
17294           S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
17295           S.targetDiag(ELoc, diag::note_vla_unsupported);
17296         }
17297       }
17298       // For arrays/array sections only:
17299       // Create pseudo array type for private copy. The size for this array will
17300       // be generated during codegen.
17301       // For array subscripts or single variables Private Ty is the same as Type
17302       // (type of the variable or single array element).
17303       PrivateTy = Context.getVariableArrayType(
17304           Type,
17305           new (Context)
17306               OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue),
17307           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
17308     } else if (!ASE && !OASE &&
17309                Context.getAsArrayType(D->getType().getNonReferenceType())) {
17310       PrivateTy = D->getType().getNonReferenceType();
17311     }
17312     // Private copy.
17313     VarDecl *PrivateVD =
17314         buildVarDecl(S, ELoc, PrivateTy, D->getName(),
17315                      D->hasAttrs() ? &D->getAttrs() : nullptr,
17316                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
17317     // Add initializer for private variable.
17318     Expr *Init = nullptr;
17319     DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
17320     DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
17321     if (DeclareReductionRef.isUsable()) {
17322       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
17323       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
17324       if (DRD->getInitializer()) {
17325         Init = DRDRef;
17326         RHSVD->setInit(DRDRef);
17327         RHSVD->setInitStyle(VarDecl::CallInit);
17328       }
17329     } else {
17330       switch (BOK) {
17331       case BO_Add:
17332       case BO_Xor:
17333       case BO_Or:
17334       case BO_LOr:
17335         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
17336         if (Type->isScalarType() || Type->isAnyComplexType())
17337           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
17338         break;
17339       case BO_Mul:
17340       case BO_LAnd:
17341         if (Type->isScalarType() || Type->isAnyComplexType()) {
17342           // '*' and '&&' reduction ops - initializer is '1'.
17343           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
17344         }
17345         break;
17346       case BO_And: {
17347         // '&' reduction op - initializer is '~0'.
17348         QualType OrigType = Type;
17349         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
17350           Type = ComplexTy->getElementType();
17351         if (Type->isRealFloatingType()) {
17352           llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue(
17353               Context.getFloatTypeSemantics(Type));
17354           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
17355                                          Type, ELoc);
17356         } else if (Type->isScalarType()) {
17357           uint64_t Size = Context.getTypeSize(Type);
17358           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
17359           llvm::APInt InitValue = llvm::APInt::getAllOnes(Size);
17360           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
17361         }
17362         if (Init && OrigType->isAnyComplexType()) {
17363           // Init = 0xFFFF + 0xFFFFi;
17364           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
17365           Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
17366         }
17367         Type = OrigType;
17368         break;
17369       }
17370       case BO_LT:
17371       case BO_GT: {
17372         // 'min' reduction op - initializer is 'Largest representable number in
17373         // the reduction list item type'.
17374         // 'max' reduction op - initializer is 'Least representable number in
17375         // the reduction list item type'.
17376         if (Type->isIntegerType() || Type->isPointerType()) {
17377           bool IsSigned = Type->hasSignedIntegerRepresentation();
17378           uint64_t Size = Context.getTypeSize(Type);
17379           QualType IntTy =
17380               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
17381           llvm::APInt InitValue =
17382               (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
17383                                         : llvm::APInt::getMinValue(Size)
17384                              : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
17385                                         : llvm::APInt::getMaxValue(Size);
17386           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
17387           if (Type->isPointerType()) {
17388             // Cast to pointer type.
17389             ExprResult CastExpr = S.BuildCStyleCastExpr(
17390                 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
17391             if (CastExpr.isInvalid())
17392               continue;
17393             Init = CastExpr.get();
17394           }
17395         } else if (Type->isRealFloatingType()) {
17396           llvm::APFloat InitValue = llvm::APFloat::getLargest(
17397               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
17398           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
17399                                          Type, ELoc);
17400         }
17401         break;
17402       }
17403       case BO_PtrMemD:
17404       case BO_PtrMemI:
17405       case BO_MulAssign:
17406       case BO_Div:
17407       case BO_Rem:
17408       case BO_Sub:
17409       case BO_Shl:
17410       case BO_Shr:
17411       case BO_LE:
17412       case BO_GE:
17413       case BO_EQ:
17414       case BO_NE:
17415       case BO_Cmp:
17416       case BO_AndAssign:
17417       case BO_XorAssign:
17418       case BO_OrAssign:
17419       case BO_Assign:
17420       case BO_AddAssign:
17421       case BO_SubAssign:
17422       case BO_DivAssign:
17423       case BO_RemAssign:
17424       case BO_ShlAssign:
17425       case BO_ShrAssign:
17426       case BO_Comma:
17427         llvm_unreachable("Unexpected reduction operation");
17428       }
17429     }
17430     if (Init && DeclareReductionRef.isUnset()) {
17431       S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
17432       // Store initializer for single element in private copy. Will be used
17433       // during codegen.
17434       PrivateVD->setInit(RHSVD->getInit());
17435       PrivateVD->setInitStyle(RHSVD->getInitStyle());
17436     } else if (!Init) {
17437       S.ActOnUninitializedDecl(RHSVD);
17438       // Store initializer for single element in private copy. Will be used
17439       // during codegen.
17440       PrivateVD->setInit(RHSVD->getInit());
17441       PrivateVD->setInitStyle(RHSVD->getInitStyle());
17442     }
17443     if (RHSVD->isInvalidDecl())
17444       continue;
17445     if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
17446       S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
17447           << Type << ReductionIdRange;
17448       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
17449                                VarDecl::DeclarationOnly;
17450       S.Diag(D->getLocation(),
17451              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
17452           << D;
17453       continue;
17454     }
17455     DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
17456     ExprResult ReductionOp;
17457     if (DeclareReductionRef.isUsable()) {
17458       QualType RedTy = DeclareReductionRef.get()->getType();
17459       QualType PtrRedTy = Context.getPointerType(RedTy);
17460       ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
17461       ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
17462       if (!BasePath.empty()) {
17463         LHS = S.DefaultLvalueConversion(LHS.get());
17464         RHS = S.DefaultLvalueConversion(RHS.get());
17465         LHS = ImplicitCastExpr::Create(
17466             Context, PtrRedTy, CK_UncheckedDerivedToBase, LHS.get(), &BasePath,
17467             LHS.get()->getValueKind(), FPOptionsOverride());
17468         RHS = ImplicitCastExpr::Create(
17469             Context, PtrRedTy, CK_UncheckedDerivedToBase, RHS.get(), &BasePath,
17470             RHS.get()->getValueKind(), FPOptionsOverride());
17471       }
17472       FunctionProtoType::ExtProtoInfo EPI;
17473       QualType Params[] = {PtrRedTy, PtrRedTy};
17474       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
17475       auto *OVE = new (Context) OpaqueValueExpr(
17476           ELoc, Context.getPointerType(FnTy), VK_PRValue, OK_Ordinary,
17477           S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
17478       Expr *Args[] = {LHS.get(), RHS.get()};
17479       ReductionOp =
17480           CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_PRValue, ELoc,
17481                            S.CurFPFeatureOverrides());
17482     } else {
17483       BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK);
17484       if (Type->isRecordType() && CombBOK != BOK) {
17485         Sema::TentativeAnalysisScope Trap(S);
17486         ReductionOp =
17487             S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
17488                          CombBOK, LHSDRE, RHSDRE);
17489       }
17490       if (!ReductionOp.isUsable()) {
17491         ReductionOp =
17492             S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), BOK,
17493                          LHSDRE, RHSDRE);
17494         if (ReductionOp.isUsable()) {
17495           if (BOK != BO_LT && BOK != BO_GT) {
17496             ReductionOp =
17497                 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
17498                              BO_Assign, LHSDRE, ReductionOp.get());
17499           } else {
17500             auto *ConditionalOp = new (Context)
17501                 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc,
17502                                     RHSDRE, Type, VK_LValue, OK_Ordinary);
17503             ReductionOp =
17504                 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
17505                              BO_Assign, LHSDRE, ConditionalOp);
17506           }
17507         }
17508       }
17509       if (ReductionOp.isUsable())
17510         ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
17511                                             /*DiscardedValue*/ false);
17512       if (!ReductionOp.isUsable())
17513         continue;
17514     }
17515 
17516     // Add copy operations for inscan reductions.
17517     // LHS = RHS;
17518     ExprResult CopyOpRes, TempArrayRes, TempArrayElem;
17519     if (ClauseKind == OMPC_reduction &&
17520         RD.RedModifier == OMPC_REDUCTION_inscan) {
17521       ExprResult RHS = S.DefaultLvalueConversion(RHSDRE);
17522       CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE,
17523                                RHS.get());
17524       if (!CopyOpRes.isUsable())
17525         continue;
17526       CopyOpRes =
17527           S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true);
17528       if (!CopyOpRes.isUsable())
17529         continue;
17530       // For simd directive and simd-based directives in simd mode no need to
17531       // construct temp array, need just a single temp element.
17532       if (Stack->getCurrentDirective() == OMPD_simd ||
17533           (S.getLangOpts().OpenMPSimd &&
17534            isOpenMPSimdDirective(Stack->getCurrentDirective()))) {
17535         VarDecl *TempArrayVD =
17536             buildVarDecl(S, ELoc, PrivateTy, D->getName(),
17537                          D->hasAttrs() ? &D->getAttrs() : nullptr);
17538         // Add a constructor to the temp decl.
17539         S.ActOnUninitializedDecl(TempArrayVD);
17540         TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc);
17541       } else {
17542         // Build temp array for prefix sum.
17543         auto *Dim = new (S.Context)
17544             OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
17545         QualType ArrayTy =
17546             S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal,
17547                                            /*IndexTypeQuals=*/0, {ELoc, ELoc});
17548         VarDecl *TempArrayVD =
17549             buildVarDecl(S, ELoc, ArrayTy, D->getName(),
17550                          D->hasAttrs() ? &D->getAttrs() : nullptr);
17551         // Add a constructor to the temp decl.
17552         S.ActOnUninitializedDecl(TempArrayVD);
17553         TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc);
17554         TempArrayElem =
17555             S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get());
17556         auto *Idx = new (S.Context)
17557             OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
17558         TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(),
17559                                                           ELoc, Idx, ELoc);
17560       }
17561     }
17562 
17563     // OpenMP [2.15.4.6, Restrictions, p.2]
17564     // A list item that appears in an in_reduction clause of a task construct
17565     // must appear in a task_reduction clause of a construct associated with a
17566     // taskgroup region that includes the participating task in its taskgroup
17567     // set. The construct associated with the innermost region that meets this
17568     // condition must specify the same reduction-identifier as the in_reduction
17569     // clause.
17570     if (ClauseKind == OMPC_in_reduction) {
17571       SourceRange ParentSR;
17572       BinaryOperatorKind ParentBOK;
17573       const Expr *ParentReductionOp = nullptr;
17574       Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr;
17575       DSAStackTy::DSAVarData ParentBOKDSA =
17576           Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
17577                                                   ParentBOKTD);
17578       DSAStackTy::DSAVarData ParentReductionOpDSA =
17579           Stack->getTopMostTaskgroupReductionData(
17580               D, ParentSR, ParentReductionOp, ParentReductionOpTD);
17581       bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
17582       bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
17583       if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
17584           (DeclareReductionRef.isUsable() && IsParentBOK) ||
17585           (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) {
17586         bool EmitError = true;
17587         if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
17588           llvm::FoldingSetNodeID RedId, ParentRedId;
17589           ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
17590           DeclareReductionRef.get()->Profile(RedId, Context,
17591                                              /*Canonical=*/true);
17592           EmitError = RedId != ParentRedId;
17593         }
17594         if (EmitError) {
17595           S.Diag(ReductionId.getBeginLoc(),
17596                  diag::err_omp_reduction_identifier_mismatch)
17597               << ReductionIdRange << RefExpr->getSourceRange();
17598           S.Diag(ParentSR.getBegin(),
17599                  diag::note_omp_previous_reduction_identifier)
17600               << ParentSR
17601               << (IsParentBOK ? ParentBOKDSA.RefExpr
17602                               : ParentReductionOpDSA.RefExpr)
17603                      ->getSourceRange();
17604           continue;
17605         }
17606       }
17607       TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
17608     }
17609 
17610     DeclRefExpr *Ref = nullptr;
17611     Expr *VarsExpr = RefExpr->IgnoreParens();
17612     if (!VD && !S.CurContext->isDependentContext()) {
17613       if (ASE || OASE) {
17614         TransformExprToCaptures RebuildToCapture(S, D);
17615         VarsExpr =
17616             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
17617         Ref = RebuildToCapture.getCapturedExpr();
17618       } else {
17619         VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
17620       }
17621       if (!S.isOpenMPCapturedDecl(D)) {
17622         RD.ExprCaptures.emplace_back(Ref->getDecl());
17623         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
17624           ExprResult RefRes = S.DefaultLvalueConversion(Ref);
17625           if (!RefRes.isUsable())
17626             continue;
17627           ExprResult PostUpdateRes =
17628               S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
17629                            RefRes.get());
17630           if (!PostUpdateRes.isUsable())
17631             continue;
17632           if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
17633               Stack->getCurrentDirective() == OMPD_taskgroup) {
17634             S.Diag(RefExpr->getExprLoc(),
17635                    diag::err_omp_reduction_non_addressable_expression)
17636                 << RefExpr->getSourceRange();
17637             continue;
17638           }
17639           RD.ExprPostUpdates.emplace_back(
17640               S.IgnoredValueConversions(PostUpdateRes.get()).get());
17641         }
17642       }
17643     }
17644     // All reduction items are still marked as reduction (to do not increase
17645     // code base size).
17646     unsigned Modifier = RD.RedModifier;
17647     // Consider task_reductions as reductions with task modifier. Required for
17648     // correct analysis of in_reduction clauses.
17649     if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction)
17650       Modifier = OMPC_REDUCTION_task;
17651     Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier,
17652                   ASE || OASE);
17653     if (Modifier == OMPC_REDUCTION_task &&
17654         (CurrDir == OMPD_taskgroup ||
17655          ((isOpenMPParallelDirective(CurrDir) ||
17656            isOpenMPWorksharingDirective(CurrDir)) &&
17657           !isOpenMPSimdDirective(CurrDir)))) {
17658       if (DeclareReductionRef.isUsable())
17659         Stack->addTaskgroupReductionData(D, ReductionIdRange,
17660                                          DeclareReductionRef.get());
17661       else
17662         Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
17663     }
17664     RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
17665             TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(),
17666             TempArrayElem.get());
17667   }
17668   return RD.Vars.empty();
17669 }
17670 
17671 OMPClause *Sema::ActOnOpenMPReductionClause(
17672     ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier,
17673     SourceLocation StartLoc, SourceLocation LParenLoc,
17674     SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc,
17675     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
17676     ArrayRef<Expr *> UnresolvedReductions) {
17677   if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) {
17678     Diag(LParenLoc, diag::err_omp_unexpected_clause_value)
17679         << getListOfPossibleValues(OMPC_reduction, /*First=*/0,
17680                                    /*Last=*/OMPC_REDUCTION_unknown)
17681         << getOpenMPClauseName(OMPC_reduction);
17682     return nullptr;
17683   }
17684   // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions
17685   // A reduction clause with the inscan reduction-modifier may only appear on a
17686   // worksharing-loop construct, a worksharing-loop SIMD construct, a simd
17687   // construct, a parallel worksharing-loop construct or a parallel
17688   // worksharing-loop SIMD construct.
17689   if (Modifier == OMPC_REDUCTION_inscan &&
17690       (DSAStack->getCurrentDirective() != OMPD_for &&
17691        DSAStack->getCurrentDirective() != OMPD_for_simd &&
17692        DSAStack->getCurrentDirective() != OMPD_simd &&
17693        DSAStack->getCurrentDirective() != OMPD_parallel_for &&
17694        DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) {
17695     Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction);
17696     return nullptr;
17697   }
17698 
17699   ReductionData RD(VarList.size(), Modifier);
17700   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
17701                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
17702                                   ReductionIdScopeSpec, ReductionId,
17703                                   UnresolvedReductions, RD))
17704     return nullptr;
17705 
17706   return OMPReductionClause::Create(
17707       Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier,
17708       RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
17709       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps,
17710       RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems,
17711       buildPreInits(Context, RD.ExprCaptures),
17712       buildPostUpdate(*this, RD.ExprPostUpdates));
17713 }
17714 
17715 OMPClause *Sema::ActOnOpenMPTaskReductionClause(
17716     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
17717     SourceLocation ColonLoc, SourceLocation EndLoc,
17718     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
17719     ArrayRef<Expr *> UnresolvedReductions) {
17720   ReductionData RD(VarList.size());
17721   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
17722                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
17723                                   ReductionIdScopeSpec, ReductionId,
17724                                   UnresolvedReductions, RD))
17725     return nullptr;
17726 
17727   return OMPTaskReductionClause::Create(
17728       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
17729       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
17730       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
17731       buildPreInits(Context, RD.ExprCaptures),
17732       buildPostUpdate(*this, RD.ExprPostUpdates));
17733 }
17734 
17735 OMPClause *Sema::ActOnOpenMPInReductionClause(
17736     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
17737     SourceLocation ColonLoc, SourceLocation EndLoc,
17738     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
17739     ArrayRef<Expr *> UnresolvedReductions) {
17740   ReductionData RD(VarList.size());
17741   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
17742                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
17743                                   ReductionIdScopeSpec, ReductionId,
17744                                   UnresolvedReductions, RD))
17745     return nullptr;
17746 
17747   return OMPInReductionClause::Create(
17748       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
17749       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
17750       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
17751       buildPreInits(Context, RD.ExprCaptures),
17752       buildPostUpdate(*this, RD.ExprPostUpdates));
17753 }
17754 
17755 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
17756                                      SourceLocation LinLoc) {
17757   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
17758       LinKind == OMPC_LINEAR_unknown) {
17759     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
17760     return true;
17761   }
17762   return false;
17763 }
17764 
17765 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
17766                                  OpenMPLinearClauseKind LinKind, QualType Type,
17767                                  bool IsDeclareSimd) {
17768   const auto *VD = dyn_cast_or_null<VarDecl>(D);
17769   // A variable must not have an incomplete type or a reference type.
17770   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
17771     return true;
17772   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
17773       !Type->isReferenceType()) {
17774     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
17775         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
17776     return true;
17777   }
17778   Type = Type.getNonReferenceType();
17779 
17780   // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
17781   // A variable that is privatized must not have a const-qualified type
17782   // unless it is of class type with a mutable member. This restriction does
17783   // not apply to the firstprivate clause, nor to the linear clause on
17784   // declarative directives (like declare simd).
17785   if (!IsDeclareSimd &&
17786       rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
17787     return true;
17788 
17789   // A list item must be of integral or pointer type.
17790   Type = Type.getUnqualifiedType().getCanonicalType();
17791   const auto *Ty = Type.getTypePtrOrNull();
17792   if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() &&
17793               !Ty->isIntegralType(Context) && !Ty->isPointerType())) {
17794     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
17795     if (D) {
17796       bool IsDecl =
17797           !VD ||
17798           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
17799       Diag(D->getLocation(),
17800            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
17801           << D;
17802     }
17803     return true;
17804   }
17805   return false;
17806 }
17807 
17808 OMPClause *Sema::ActOnOpenMPLinearClause(
17809     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
17810     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
17811     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
17812   SmallVector<Expr *, 8> Vars;
17813   SmallVector<Expr *, 8> Privates;
17814   SmallVector<Expr *, 8> Inits;
17815   SmallVector<Decl *, 4> ExprCaptures;
17816   SmallVector<Expr *, 4> ExprPostUpdates;
17817   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
17818     LinKind = OMPC_LINEAR_val;
17819   for (Expr *RefExpr : VarList) {
17820     assert(RefExpr && "NULL expr in OpenMP linear clause.");
17821     SourceLocation ELoc;
17822     SourceRange ERange;
17823     Expr *SimpleRefExpr = RefExpr;
17824     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
17825     if (Res.second) {
17826       // It will be analyzed later.
17827       Vars.push_back(RefExpr);
17828       Privates.push_back(nullptr);
17829       Inits.push_back(nullptr);
17830     }
17831     ValueDecl *D = Res.first;
17832     if (!D)
17833       continue;
17834 
17835     QualType Type = D->getType();
17836     auto *VD = dyn_cast<VarDecl>(D);
17837 
17838     // OpenMP [2.14.3.7, linear clause]
17839     //  A list-item cannot appear in more than one linear clause.
17840     //  A list-item that appears in a linear clause cannot appear in any
17841     //  other data-sharing attribute clause.
17842     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
17843     if (DVar.RefExpr) {
17844       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
17845                                           << getOpenMPClauseName(OMPC_linear);
17846       reportOriginalDsa(*this, DSAStack, D, DVar);
17847       continue;
17848     }
17849 
17850     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
17851       continue;
17852     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
17853 
17854     // Build private copy of original var.
17855     VarDecl *Private =
17856         buildVarDecl(*this, ELoc, Type, D->getName(),
17857                      D->hasAttrs() ? &D->getAttrs() : nullptr,
17858                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
17859     DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
17860     // Build var to save initial value.
17861     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
17862     Expr *InitExpr;
17863     DeclRefExpr *Ref = nullptr;
17864     if (!VD && !CurContext->isDependentContext()) {
17865       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
17866       if (!isOpenMPCapturedDecl(D)) {
17867         ExprCaptures.push_back(Ref->getDecl());
17868         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
17869           ExprResult RefRes = DefaultLvalueConversion(Ref);
17870           if (!RefRes.isUsable())
17871             continue;
17872           ExprResult PostUpdateRes =
17873               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
17874                          SimpleRefExpr, RefRes.get());
17875           if (!PostUpdateRes.isUsable())
17876             continue;
17877           ExprPostUpdates.push_back(
17878               IgnoredValueConversions(PostUpdateRes.get()).get());
17879         }
17880       }
17881     }
17882     if (LinKind == OMPC_LINEAR_uval)
17883       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
17884     else
17885       InitExpr = VD ? SimpleRefExpr : Ref;
17886     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
17887                          /*DirectInit=*/false);
17888     DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
17889 
17890     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
17891     Vars.push_back((VD || CurContext->isDependentContext())
17892                        ? RefExpr->IgnoreParens()
17893                        : Ref);
17894     Privates.push_back(PrivateRef);
17895     Inits.push_back(InitRef);
17896   }
17897 
17898   if (Vars.empty())
17899     return nullptr;
17900 
17901   Expr *StepExpr = Step;
17902   Expr *CalcStepExpr = nullptr;
17903   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
17904       !Step->isInstantiationDependent() &&
17905       !Step->containsUnexpandedParameterPack()) {
17906     SourceLocation StepLoc = Step->getBeginLoc();
17907     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
17908     if (Val.isInvalid())
17909       return nullptr;
17910     StepExpr = Val.get();
17911 
17912     // Build var to save the step value.
17913     VarDecl *SaveVar =
17914         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
17915     ExprResult SaveRef =
17916         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
17917     ExprResult CalcStep =
17918         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
17919     CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
17920 
17921     // Warn about zero linear step (it would be probably better specified as
17922     // making corresponding variables 'const').
17923     if (Optional<llvm::APSInt> Result =
17924             StepExpr->getIntegerConstantExpr(Context)) {
17925       if (!Result->isNegative() && !Result->isStrictlyPositive())
17926         Diag(StepLoc, diag::warn_omp_linear_step_zero)
17927             << Vars[0] << (Vars.size() > 1);
17928     } else if (CalcStep.isUsable()) {
17929       // Calculate the step beforehand instead of doing this on each iteration.
17930       // (This is not used if the number of iterations may be kfold-ed).
17931       CalcStepExpr = CalcStep.get();
17932     }
17933   }
17934 
17935   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
17936                                  ColonLoc, EndLoc, Vars, Privates, Inits,
17937                                  StepExpr, CalcStepExpr,
17938                                  buildPreInits(Context, ExprCaptures),
17939                                  buildPostUpdate(*this, ExprPostUpdates));
17940 }
17941 
17942 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
17943                                      Expr *NumIterations, Sema &SemaRef,
17944                                      Scope *S, DSAStackTy *Stack) {
17945   // Walk the vars and build update/final expressions for the CodeGen.
17946   SmallVector<Expr *, 8> Updates;
17947   SmallVector<Expr *, 8> Finals;
17948   SmallVector<Expr *, 8> UsedExprs;
17949   Expr *Step = Clause.getStep();
17950   Expr *CalcStep = Clause.getCalcStep();
17951   // OpenMP [2.14.3.7, linear clause]
17952   // If linear-step is not specified it is assumed to be 1.
17953   if (!Step)
17954     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
17955   else if (CalcStep)
17956     Step = cast<BinaryOperator>(CalcStep)->getLHS();
17957   bool HasErrors = false;
17958   auto CurInit = Clause.inits().begin();
17959   auto CurPrivate = Clause.privates().begin();
17960   OpenMPLinearClauseKind LinKind = Clause.getModifier();
17961   for (Expr *RefExpr : Clause.varlists()) {
17962     SourceLocation ELoc;
17963     SourceRange ERange;
17964     Expr *SimpleRefExpr = RefExpr;
17965     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
17966     ValueDecl *D = Res.first;
17967     if (Res.second || !D) {
17968       Updates.push_back(nullptr);
17969       Finals.push_back(nullptr);
17970       HasErrors = true;
17971       continue;
17972     }
17973     auto &&Info = Stack->isLoopControlVariable(D);
17974     // OpenMP [2.15.11, distribute simd Construct]
17975     // A list item may not appear in a linear clause, unless it is the loop
17976     // iteration variable.
17977     if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
17978         isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
17979       SemaRef.Diag(ELoc,
17980                    diag::err_omp_linear_distribute_var_non_loop_iteration);
17981       Updates.push_back(nullptr);
17982       Finals.push_back(nullptr);
17983       HasErrors = true;
17984       continue;
17985     }
17986     Expr *InitExpr = *CurInit;
17987 
17988     // Build privatized reference to the current linear var.
17989     auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
17990     Expr *CapturedRef;
17991     if (LinKind == OMPC_LINEAR_uval)
17992       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
17993     else
17994       CapturedRef =
17995           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
17996                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
17997                            /*RefersToCapture=*/true);
17998 
17999     // Build update: Var = InitExpr + IV * Step
18000     ExprResult Update;
18001     if (!Info.first)
18002       Update = buildCounterUpdate(
18003           SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step,
18004           /*Subtract=*/false, /*IsNonRectangularLB=*/false);
18005     else
18006       Update = *CurPrivate;
18007     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
18008                                          /*DiscardedValue*/ false);
18009 
18010     // Build final: Var = InitExpr + NumIterations * Step
18011     ExprResult Final;
18012     if (!Info.first)
18013       Final =
18014           buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
18015                              InitExpr, NumIterations, Step, /*Subtract=*/false,
18016                              /*IsNonRectangularLB=*/false);
18017     else
18018       Final = *CurPrivate;
18019     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
18020                                         /*DiscardedValue*/ false);
18021 
18022     if (!Update.isUsable() || !Final.isUsable()) {
18023       Updates.push_back(nullptr);
18024       Finals.push_back(nullptr);
18025       UsedExprs.push_back(nullptr);
18026       HasErrors = true;
18027     } else {
18028       Updates.push_back(Update.get());
18029       Finals.push_back(Final.get());
18030       if (!Info.first)
18031         UsedExprs.push_back(SimpleRefExpr);
18032     }
18033     ++CurInit;
18034     ++CurPrivate;
18035   }
18036   if (Expr *S = Clause.getStep())
18037     UsedExprs.push_back(S);
18038   // Fill the remaining part with the nullptr.
18039   UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr);
18040   Clause.setUpdates(Updates);
18041   Clause.setFinals(Finals);
18042   Clause.setUsedExprs(UsedExprs);
18043   return HasErrors;
18044 }
18045 
18046 OMPClause *Sema::ActOnOpenMPAlignedClause(
18047     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
18048     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
18049   SmallVector<Expr *, 8> Vars;
18050   for (Expr *RefExpr : VarList) {
18051     assert(RefExpr && "NULL expr in OpenMP linear clause.");
18052     SourceLocation ELoc;
18053     SourceRange ERange;
18054     Expr *SimpleRefExpr = RefExpr;
18055     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
18056     if (Res.second) {
18057       // It will be analyzed later.
18058       Vars.push_back(RefExpr);
18059     }
18060     ValueDecl *D = Res.first;
18061     if (!D)
18062       continue;
18063 
18064     QualType QType = D->getType();
18065     auto *VD = dyn_cast<VarDecl>(D);
18066 
18067     // OpenMP  [2.8.1, simd construct, Restrictions]
18068     // The type of list items appearing in the aligned clause must be
18069     // array, pointer, reference to array, or reference to pointer.
18070     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
18071     const Type *Ty = QType.getTypePtrOrNull();
18072     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
18073       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
18074           << QType << getLangOpts().CPlusPlus << ERange;
18075       bool IsDecl =
18076           !VD ||
18077           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
18078       Diag(D->getLocation(),
18079            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
18080           << D;
18081       continue;
18082     }
18083 
18084     // OpenMP  [2.8.1, simd construct, Restrictions]
18085     // A list-item cannot appear in more than one aligned clause.
18086     if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
18087       Diag(ELoc, diag::err_omp_used_in_clause_twice)
18088           << 0 << getOpenMPClauseName(OMPC_aligned) << ERange;
18089       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
18090           << getOpenMPClauseName(OMPC_aligned);
18091       continue;
18092     }
18093 
18094     DeclRefExpr *Ref = nullptr;
18095     if (!VD && isOpenMPCapturedDecl(D))
18096       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
18097     Vars.push_back(DefaultFunctionArrayConversion(
18098                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
18099                        .get());
18100   }
18101 
18102   // OpenMP [2.8.1, simd construct, Description]
18103   // The parameter of the aligned clause, alignment, must be a constant
18104   // positive integer expression.
18105   // If no optional parameter is specified, implementation-defined default
18106   // alignments for SIMD instructions on the target platforms are assumed.
18107   if (Alignment != nullptr) {
18108     ExprResult AlignResult =
18109         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
18110     if (AlignResult.isInvalid())
18111       return nullptr;
18112     Alignment = AlignResult.get();
18113   }
18114   if (Vars.empty())
18115     return nullptr;
18116 
18117   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
18118                                   EndLoc, Vars, Alignment);
18119 }
18120 
18121 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
18122                                          SourceLocation StartLoc,
18123                                          SourceLocation LParenLoc,
18124                                          SourceLocation EndLoc) {
18125   SmallVector<Expr *, 8> Vars;
18126   SmallVector<Expr *, 8> SrcExprs;
18127   SmallVector<Expr *, 8> DstExprs;
18128   SmallVector<Expr *, 8> AssignmentOps;
18129   for (Expr *RefExpr : VarList) {
18130     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
18131     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
18132       // It will be analyzed later.
18133       Vars.push_back(RefExpr);
18134       SrcExprs.push_back(nullptr);
18135       DstExprs.push_back(nullptr);
18136       AssignmentOps.push_back(nullptr);
18137       continue;
18138     }
18139 
18140     SourceLocation ELoc = RefExpr->getExprLoc();
18141     // OpenMP [2.1, C/C++]
18142     //  A list item is a variable name.
18143     // OpenMP  [2.14.4.1, Restrictions, p.1]
18144     //  A list item that appears in a copyin clause must be threadprivate.
18145     auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
18146     if (!DE || !isa<VarDecl>(DE->getDecl())) {
18147       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
18148           << 0 << RefExpr->getSourceRange();
18149       continue;
18150     }
18151 
18152     Decl *D = DE->getDecl();
18153     auto *VD = cast<VarDecl>(D);
18154 
18155     QualType Type = VD->getType();
18156     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
18157       // It will be analyzed later.
18158       Vars.push_back(DE);
18159       SrcExprs.push_back(nullptr);
18160       DstExprs.push_back(nullptr);
18161       AssignmentOps.push_back(nullptr);
18162       continue;
18163     }
18164 
18165     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
18166     //  A list item that appears in a copyin clause must be threadprivate.
18167     if (!DSAStack->isThreadPrivate(VD)) {
18168       Diag(ELoc, diag::err_omp_required_access)
18169           << getOpenMPClauseName(OMPC_copyin)
18170           << getOpenMPDirectiveName(OMPD_threadprivate);
18171       continue;
18172     }
18173 
18174     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
18175     //  A variable of class type (or array thereof) that appears in a
18176     //  copyin clause requires an accessible, unambiguous copy assignment
18177     //  operator for the class type.
18178     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
18179     VarDecl *SrcVD =
18180         buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
18181                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
18182     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
18183         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
18184     VarDecl *DstVD =
18185         buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
18186                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
18187     DeclRefExpr *PseudoDstExpr =
18188         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
18189     // For arrays generate assignment operation for single element and replace
18190     // it by the original array element in CodeGen.
18191     ExprResult AssignmentOp =
18192         BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
18193                    PseudoSrcExpr);
18194     if (AssignmentOp.isInvalid())
18195       continue;
18196     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
18197                                        /*DiscardedValue*/ false);
18198     if (AssignmentOp.isInvalid())
18199       continue;
18200 
18201     DSAStack->addDSA(VD, DE, OMPC_copyin);
18202     Vars.push_back(DE);
18203     SrcExprs.push_back(PseudoSrcExpr);
18204     DstExprs.push_back(PseudoDstExpr);
18205     AssignmentOps.push_back(AssignmentOp.get());
18206   }
18207 
18208   if (Vars.empty())
18209     return nullptr;
18210 
18211   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
18212                                  SrcExprs, DstExprs, AssignmentOps);
18213 }
18214 
18215 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
18216                                               SourceLocation StartLoc,
18217                                               SourceLocation LParenLoc,
18218                                               SourceLocation EndLoc) {
18219   SmallVector<Expr *, 8> Vars;
18220   SmallVector<Expr *, 8> SrcExprs;
18221   SmallVector<Expr *, 8> DstExprs;
18222   SmallVector<Expr *, 8> AssignmentOps;
18223   for (Expr *RefExpr : VarList) {
18224     assert(RefExpr && "NULL expr in OpenMP linear clause.");
18225     SourceLocation ELoc;
18226     SourceRange ERange;
18227     Expr *SimpleRefExpr = RefExpr;
18228     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
18229     if (Res.second) {
18230       // It will be analyzed later.
18231       Vars.push_back(RefExpr);
18232       SrcExprs.push_back(nullptr);
18233       DstExprs.push_back(nullptr);
18234       AssignmentOps.push_back(nullptr);
18235     }
18236     ValueDecl *D = Res.first;
18237     if (!D)
18238       continue;
18239 
18240     QualType Type = D->getType();
18241     auto *VD = dyn_cast<VarDecl>(D);
18242 
18243     // OpenMP [2.14.4.2, Restrictions, p.2]
18244     //  A list item that appears in a copyprivate clause may not appear in a
18245     //  private or firstprivate clause on the single construct.
18246     if (!VD || !DSAStack->isThreadPrivate(VD)) {
18247       DSAStackTy::DSAVarData DVar =
18248           DSAStack->getTopDSA(D, /*FromParent=*/false);
18249       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
18250           DVar.RefExpr) {
18251         Diag(ELoc, diag::err_omp_wrong_dsa)
18252             << getOpenMPClauseName(DVar.CKind)
18253             << getOpenMPClauseName(OMPC_copyprivate);
18254         reportOriginalDsa(*this, DSAStack, D, DVar);
18255         continue;
18256       }
18257 
18258       // OpenMP [2.11.4.2, Restrictions, p.1]
18259       //  All list items that appear in a copyprivate clause must be either
18260       //  threadprivate or private in the enclosing context.
18261       if (DVar.CKind == OMPC_unknown) {
18262         DVar = DSAStack->getImplicitDSA(D, false);
18263         if (DVar.CKind == OMPC_shared) {
18264           Diag(ELoc, diag::err_omp_required_access)
18265               << getOpenMPClauseName(OMPC_copyprivate)
18266               << "threadprivate or private in the enclosing context";
18267           reportOriginalDsa(*this, DSAStack, D, DVar);
18268           continue;
18269         }
18270       }
18271     }
18272 
18273     // Variably modified types are not supported.
18274     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
18275       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
18276           << getOpenMPClauseName(OMPC_copyprivate) << Type
18277           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
18278       bool IsDecl =
18279           !VD ||
18280           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
18281       Diag(D->getLocation(),
18282            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
18283           << D;
18284       continue;
18285     }
18286 
18287     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
18288     //  A variable of class type (or array thereof) that appears in a
18289     //  copyin clause requires an accessible, unambiguous copy assignment
18290     //  operator for the class type.
18291     Type = Context.getBaseElementType(Type.getNonReferenceType())
18292                .getUnqualifiedType();
18293     VarDecl *SrcVD =
18294         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
18295                      D->hasAttrs() ? &D->getAttrs() : nullptr);
18296     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
18297     VarDecl *DstVD =
18298         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
18299                      D->hasAttrs() ? &D->getAttrs() : nullptr);
18300     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
18301     ExprResult AssignmentOp = BuildBinOp(
18302         DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
18303     if (AssignmentOp.isInvalid())
18304       continue;
18305     AssignmentOp =
18306         ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
18307     if (AssignmentOp.isInvalid())
18308       continue;
18309 
18310     // No need to mark vars as copyprivate, they are already threadprivate or
18311     // implicitly private.
18312     assert(VD || isOpenMPCapturedDecl(D));
18313     Vars.push_back(
18314         VD ? RefExpr->IgnoreParens()
18315            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
18316     SrcExprs.push_back(PseudoSrcExpr);
18317     DstExprs.push_back(PseudoDstExpr);
18318     AssignmentOps.push_back(AssignmentOp.get());
18319   }
18320 
18321   if (Vars.empty())
18322     return nullptr;
18323 
18324   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
18325                                       Vars, SrcExprs, DstExprs, AssignmentOps);
18326 }
18327 
18328 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
18329                                         SourceLocation StartLoc,
18330                                         SourceLocation LParenLoc,
18331                                         SourceLocation EndLoc) {
18332   if (VarList.empty())
18333     return nullptr;
18334 
18335   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
18336 }
18337 
18338 /// Tries to find omp_depend_t. type.
18339 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack,
18340                            bool Diagnose = true) {
18341   QualType OMPDependT = Stack->getOMPDependT();
18342   if (!OMPDependT.isNull())
18343     return true;
18344   IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t");
18345   ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope());
18346   if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
18347     if (Diagnose)
18348       S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t";
18349     return false;
18350   }
18351   Stack->setOMPDependT(PT.get());
18352   return true;
18353 }
18354 
18355 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc,
18356                                          SourceLocation LParenLoc,
18357                                          SourceLocation EndLoc) {
18358   if (!Depobj)
18359     return nullptr;
18360 
18361   bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack);
18362 
18363   // OpenMP 5.0, 2.17.10.1 depobj Construct
18364   // depobj is an lvalue expression of type omp_depend_t.
18365   if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() &&
18366       !Depobj->isInstantiationDependent() &&
18367       !Depobj->containsUnexpandedParameterPack() &&
18368       (OMPDependTFound &&
18369        !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(),
18370                                    /*CompareUnqualified=*/true))) {
18371     Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue)
18372         << 0 << Depobj->getType() << Depobj->getSourceRange();
18373   }
18374 
18375   if (!Depobj->isLValue()) {
18376     Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue)
18377         << 1 << Depobj->getSourceRange();
18378   }
18379 
18380   return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj);
18381 }
18382 
18383 OMPClause *
18384 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind,
18385                               SourceLocation DepLoc, SourceLocation ColonLoc,
18386                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
18387                               SourceLocation LParenLoc, SourceLocation EndLoc) {
18388   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
18389       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
18390     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
18391         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
18392     return nullptr;
18393   }
18394   if ((DSAStack->getCurrentDirective() != OMPD_ordered ||
18395        DSAStack->getCurrentDirective() == OMPD_depobj) &&
18396       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
18397        DepKind == OMPC_DEPEND_sink ||
18398        ((LangOpts.OpenMP < 50 ||
18399          DSAStack->getCurrentDirective() == OMPD_depobj) &&
18400         DepKind == OMPC_DEPEND_depobj))) {
18401     SmallVector<unsigned, 3> Except;
18402     Except.push_back(OMPC_DEPEND_source);
18403     Except.push_back(OMPC_DEPEND_sink);
18404     if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj)
18405       Except.push_back(OMPC_DEPEND_depobj);
18406     std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier)
18407                                ? "depend modifier(iterator) or "
18408                                : "";
18409     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
18410         << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0,
18411                                               /*Last=*/OMPC_DEPEND_unknown,
18412                                               Except)
18413         << getOpenMPClauseName(OMPC_depend);
18414     return nullptr;
18415   }
18416   if (DepModifier &&
18417       (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) {
18418     Diag(DepModifier->getExprLoc(),
18419          diag::err_omp_depend_sink_source_with_modifier);
18420     return nullptr;
18421   }
18422   if (DepModifier &&
18423       !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator))
18424     Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator);
18425 
18426   SmallVector<Expr *, 8> Vars;
18427   DSAStackTy::OperatorOffsetTy OpsOffs;
18428   llvm::APSInt DepCounter(/*BitWidth=*/32);
18429   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
18430   if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
18431     if (const Expr *OrderedCountExpr =
18432             DSAStack->getParentOrderedRegionParam().first) {
18433       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
18434       TotalDepCount.setIsUnsigned(/*Val=*/true);
18435     }
18436   }
18437   for (Expr *RefExpr : VarList) {
18438     assert(RefExpr && "NULL expr in OpenMP shared clause.");
18439     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
18440       // It will be analyzed later.
18441       Vars.push_back(RefExpr);
18442       continue;
18443     }
18444 
18445     SourceLocation ELoc = RefExpr->getExprLoc();
18446     Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
18447     if (DepKind == OMPC_DEPEND_sink) {
18448       if (DSAStack->getParentOrderedRegionParam().first &&
18449           DepCounter >= TotalDepCount) {
18450         Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
18451         continue;
18452       }
18453       ++DepCounter;
18454       // OpenMP  [2.13.9, Summary]
18455       // depend(dependence-type : vec), where dependence-type is:
18456       // 'sink' and where vec is the iteration vector, which has the form:
18457       //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
18458       // where n is the value specified by the ordered clause in the loop
18459       // directive, xi denotes the loop iteration variable of the i-th nested
18460       // loop associated with the loop directive, and di is a constant
18461       // non-negative integer.
18462       if (CurContext->isDependentContext()) {
18463         // It will be analyzed later.
18464         Vars.push_back(RefExpr);
18465         continue;
18466       }
18467       SimpleExpr = SimpleExpr->IgnoreImplicit();
18468       OverloadedOperatorKind OOK = OO_None;
18469       SourceLocation OOLoc;
18470       Expr *LHS = SimpleExpr;
18471       Expr *RHS = nullptr;
18472       if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
18473         OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
18474         OOLoc = BO->getOperatorLoc();
18475         LHS = BO->getLHS()->IgnoreParenImpCasts();
18476         RHS = BO->getRHS()->IgnoreParenImpCasts();
18477       } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
18478         OOK = OCE->getOperator();
18479         OOLoc = OCE->getOperatorLoc();
18480         LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
18481         RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
18482       } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
18483         OOK = MCE->getMethodDecl()
18484                   ->getNameInfo()
18485                   .getName()
18486                   .getCXXOverloadedOperator();
18487         OOLoc = MCE->getCallee()->getExprLoc();
18488         LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
18489         RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
18490       }
18491       SourceLocation ELoc;
18492       SourceRange ERange;
18493       auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
18494       if (Res.second) {
18495         // It will be analyzed later.
18496         Vars.push_back(RefExpr);
18497       }
18498       ValueDecl *D = Res.first;
18499       if (!D)
18500         continue;
18501 
18502       if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
18503         Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
18504         continue;
18505       }
18506       if (RHS) {
18507         ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
18508             RHS, OMPC_depend, /*StrictlyPositive=*/false);
18509         if (RHSRes.isInvalid())
18510           continue;
18511       }
18512       if (!CurContext->isDependentContext() &&
18513           DSAStack->getParentOrderedRegionParam().first &&
18514           DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
18515         const ValueDecl *VD =
18516             DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
18517         if (VD)
18518           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
18519               << 1 << VD;
18520         else
18521           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
18522         continue;
18523       }
18524       OpsOffs.emplace_back(RHS, OOK);
18525     } else {
18526       bool OMPDependTFound = LangOpts.OpenMP >= 50;
18527       if (OMPDependTFound)
18528         OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack,
18529                                          DepKind == OMPC_DEPEND_depobj);
18530       if (DepKind == OMPC_DEPEND_depobj) {
18531         // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
18532         // List items used in depend clauses with the depobj dependence type
18533         // must be expressions of the omp_depend_t type.
18534         if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
18535             !RefExpr->isInstantiationDependent() &&
18536             !RefExpr->containsUnexpandedParameterPack() &&
18537             (OMPDependTFound &&
18538              !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(),
18539                                              RefExpr->getType()))) {
18540           Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue)
18541               << 0 << RefExpr->getType() << RefExpr->getSourceRange();
18542           continue;
18543         }
18544         if (!RefExpr->isLValue()) {
18545           Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue)
18546               << 1 << RefExpr->getType() << RefExpr->getSourceRange();
18547           continue;
18548         }
18549       } else {
18550         // OpenMP 5.0 [2.17.11, Restrictions]
18551         // List items used in depend clauses cannot be zero-length array
18552         // sections.
18553         QualType ExprTy = RefExpr->getType().getNonReferenceType();
18554         const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
18555         if (OASE) {
18556           QualType BaseType =
18557               OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
18558           if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
18559             ExprTy = ATy->getElementType();
18560           else
18561             ExprTy = BaseType->getPointeeType();
18562           ExprTy = ExprTy.getNonReferenceType();
18563           const Expr *Length = OASE->getLength();
18564           Expr::EvalResult Result;
18565           if (Length && !Length->isValueDependent() &&
18566               Length->EvaluateAsInt(Result, Context) &&
18567               Result.Val.getInt().isZero()) {
18568             Diag(ELoc,
18569                  diag::err_omp_depend_zero_length_array_section_not_allowed)
18570                 << SimpleExpr->getSourceRange();
18571             continue;
18572           }
18573         }
18574 
18575         // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
18576         // List items used in depend clauses with the in, out, inout or
18577         // mutexinoutset dependence types cannot be expressions of the
18578         // omp_depend_t type.
18579         if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
18580             !RefExpr->isInstantiationDependent() &&
18581             !RefExpr->containsUnexpandedParameterPack() &&
18582             (OMPDependTFound &&
18583              DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) {
18584           Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
18585               << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1
18586               << RefExpr->getSourceRange();
18587           continue;
18588         }
18589 
18590         auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
18591         if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
18592             (ASE && !ASE->getBase()->isTypeDependent() &&
18593              !ASE->getBase()
18594                   ->getType()
18595                   .getNonReferenceType()
18596                   ->isPointerType() &&
18597              !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
18598           Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
18599               << (LangOpts.OpenMP >= 50 ? 1 : 0)
18600               << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange();
18601           continue;
18602         }
18603 
18604         ExprResult Res;
18605         {
18606           Sema::TentativeAnalysisScope Trap(*this);
18607           Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf,
18608                                      RefExpr->IgnoreParenImpCasts());
18609         }
18610         if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) &&
18611             !isa<OMPArrayShapingExpr>(SimpleExpr)) {
18612           Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
18613               << (LangOpts.OpenMP >= 50 ? 1 : 0)
18614               << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange();
18615           continue;
18616         }
18617       }
18618     }
18619     Vars.push_back(RefExpr->IgnoreParenImpCasts());
18620   }
18621 
18622   if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
18623       TotalDepCount > VarList.size() &&
18624       DSAStack->getParentOrderedRegionParam().first &&
18625       DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
18626     Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
18627         << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
18628   }
18629   if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
18630       Vars.empty())
18631     return nullptr;
18632 
18633   auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
18634                                     DepModifier, DepKind, DepLoc, ColonLoc,
18635                                     Vars, TotalDepCount.getZExtValue());
18636   if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
18637       DSAStack->isParentOrderedRegion())
18638     DSAStack->addDoacrossDependClause(C, OpsOffs);
18639   return C;
18640 }
18641 
18642 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier,
18643                                          Expr *Device, SourceLocation StartLoc,
18644                                          SourceLocation LParenLoc,
18645                                          SourceLocation ModifierLoc,
18646                                          SourceLocation EndLoc) {
18647   assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) &&
18648          "Unexpected device modifier in OpenMP < 50.");
18649 
18650   bool ErrorFound = false;
18651   if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) {
18652     std::string Values =
18653         getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown);
18654     Diag(ModifierLoc, diag::err_omp_unexpected_clause_value)
18655         << Values << getOpenMPClauseName(OMPC_device);
18656     ErrorFound = true;
18657   }
18658 
18659   Expr *ValExpr = Device;
18660   Stmt *HelperValStmt = nullptr;
18661 
18662   // OpenMP [2.9.1, Restrictions]
18663   // The device expression must evaluate to a non-negative integer value.
18664   ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
18665                                           /*StrictlyPositive=*/false) ||
18666                ErrorFound;
18667   if (ErrorFound)
18668     return nullptr;
18669 
18670   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18671   OpenMPDirectiveKind CaptureRegion =
18672       getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP);
18673   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
18674     ValExpr = MakeFullExpr(ValExpr).get();
18675     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18676     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
18677     HelperValStmt = buildPreInits(Context, Captures);
18678   }
18679 
18680   return new (Context)
18681       OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
18682                       LParenLoc, ModifierLoc, EndLoc);
18683 }
18684 
18685 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
18686                               DSAStackTy *Stack, QualType QTy,
18687                               bool FullCheck = true) {
18688   if (SemaRef.RequireCompleteType(SL, QTy, diag::err_incomplete_type))
18689     return false;
18690   if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
18691       !QTy.isTriviallyCopyableType(SemaRef.Context))
18692     SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
18693   return true;
18694 }
18695 
18696 /// Return true if it can be proven that the provided array expression
18697 /// (array section or array subscript) does NOT specify the whole size of the
18698 /// array whose base type is \a BaseQTy.
18699 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
18700                                                         const Expr *E,
18701                                                         QualType BaseQTy) {
18702   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
18703 
18704   // If this is an array subscript, it refers to the whole size if the size of
18705   // the dimension is constant and equals 1. Also, an array section assumes the
18706   // format of an array subscript if no colon is used.
18707   if (isa<ArraySubscriptExpr>(E) ||
18708       (OASE && OASE->getColonLocFirst().isInvalid())) {
18709     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
18710       return ATy->getSize().getSExtValue() != 1;
18711     // Size can't be evaluated statically.
18712     return false;
18713   }
18714 
18715   assert(OASE && "Expecting array section if not an array subscript.");
18716   const Expr *LowerBound = OASE->getLowerBound();
18717   const Expr *Length = OASE->getLength();
18718 
18719   // If there is a lower bound that does not evaluates to zero, we are not
18720   // covering the whole dimension.
18721   if (LowerBound) {
18722     Expr::EvalResult Result;
18723     if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
18724       return false; // Can't get the integer value as a constant.
18725 
18726     llvm::APSInt ConstLowerBound = Result.Val.getInt();
18727     if (ConstLowerBound.getSExtValue())
18728       return true;
18729   }
18730 
18731   // If we don't have a length we covering the whole dimension.
18732   if (!Length)
18733     return false;
18734 
18735   // If the base is a pointer, we don't have a way to get the size of the
18736   // pointee.
18737   if (BaseQTy->isPointerType())
18738     return false;
18739 
18740   // We can only check if the length is the same as the size of the dimension
18741   // if we have a constant array.
18742   const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
18743   if (!CATy)
18744     return false;
18745 
18746   Expr::EvalResult Result;
18747   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
18748     return false; // Can't get the integer value as a constant.
18749 
18750   llvm::APSInt ConstLength = Result.Val.getInt();
18751   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
18752 }
18753 
18754 // Return true if it can be proven that the provided array expression (array
18755 // section or array subscript) does NOT specify a single element of the array
18756 // whose base type is \a BaseQTy.
18757 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
18758                                                         const Expr *E,
18759                                                         QualType BaseQTy) {
18760   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
18761 
18762   // An array subscript always refer to a single element. Also, an array section
18763   // assumes the format of an array subscript if no colon is used.
18764   if (isa<ArraySubscriptExpr>(E) ||
18765       (OASE && OASE->getColonLocFirst().isInvalid()))
18766     return false;
18767 
18768   assert(OASE && "Expecting array section if not an array subscript.");
18769   const Expr *Length = OASE->getLength();
18770 
18771   // If we don't have a length we have to check if the array has unitary size
18772   // for this dimension. Also, we should always expect a length if the base type
18773   // is pointer.
18774   if (!Length) {
18775     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
18776       return ATy->getSize().getSExtValue() != 1;
18777     // We cannot assume anything.
18778     return false;
18779   }
18780 
18781   // Check if the length evaluates to 1.
18782   Expr::EvalResult Result;
18783   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
18784     return false; // Can't get the integer value as a constant.
18785 
18786   llvm::APSInt ConstLength = Result.Val.getInt();
18787   return ConstLength.getSExtValue() != 1;
18788 }
18789 
18790 // The base of elements of list in a map clause have to be either:
18791 //  - a reference to variable or field.
18792 //  - a member expression.
18793 //  - an array expression.
18794 //
18795 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
18796 // reference to 'r'.
18797 //
18798 // If we have:
18799 //
18800 // struct SS {
18801 //   Bla S;
18802 //   foo() {
18803 //     #pragma omp target map (S.Arr[:12]);
18804 //   }
18805 // }
18806 //
18807 // We want to retrieve the member expression 'this->S';
18808 
18809 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2]
18810 //  If a list item is an array section, it must specify contiguous storage.
18811 //
18812 // For this restriction it is sufficient that we make sure only references
18813 // to variables or fields and array expressions, and that no array sections
18814 // exist except in the rightmost expression (unless they cover the whole
18815 // dimension of the array). E.g. these would be invalid:
18816 //
18817 //   r.ArrS[3:5].Arr[6:7]
18818 //
18819 //   r.ArrS[3:5].x
18820 //
18821 // but these would be valid:
18822 //   r.ArrS[3].Arr[6:7]
18823 //
18824 //   r.ArrS[3].x
18825 namespace {
18826 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> {
18827   Sema &SemaRef;
18828   OpenMPClauseKind CKind = OMPC_unknown;
18829   OpenMPDirectiveKind DKind = OMPD_unknown;
18830   OMPClauseMappableExprCommon::MappableExprComponentList &Components;
18831   bool IsNonContiguous = false;
18832   bool NoDiagnose = false;
18833   const Expr *RelevantExpr = nullptr;
18834   bool AllowUnitySizeArraySection = true;
18835   bool AllowWholeSizeArraySection = true;
18836   bool AllowAnotherPtr = true;
18837   SourceLocation ELoc;
18838   SourceRange ERange;
18839 
18840   void emitErrorMsg() {
18841     // If nothing else worked, this is not a valid map clause expression.
18842     if (SemaRef.getLangOpts().OpenMP < 50) {
18843       SemaRef.Diag(ELoc,
18844                    diag::err_omp_expected_named_var_member_or_array_expression)
18845           << ERange;
18846     } else {
18847       SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses)
18848           << getOpenMPClauseName(CKind) << ERange;
18849     }
18850   }
18851 
18852 public:
18853   bool VisitDeclRefExpr(DeclRefExpr *DRE) {
18854     if (!isa<VarDecl>(DRE->getDecl())) {
18855       emitErrorMsg();
18856       return false;
18857     }
18858     assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
18859     RelevantExpr = DRE;
18860     // Record the component.
18861     Components.emplace_back(DRE, DRE->getDecl(), IsNonContiguous);
18862     return true;
18863   }
18864 
18865   bool VisitMemberExpr(MemberExpr *ME) {
18866     Expr *E = ME;
18867     Expr *BaseE = ME->getBase()->IgnoreParenCasts();
18868 
18869     if (isa<CXXThisExpr>(BaseE)) {
18870       assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
18871       // We found a base expression: this->Val.
18872       RelevantExpr = ME;
18873     } else {
18874       E = BaseE;
18875     }
18876 
18877     if (!isa<FieldDecl>(ME->getMemberDecl())) {
18878       if (!NoDiagnose) {
18879         SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
18880           << ME->getSourceRange();
18881         return false;
18882       }
18883       if (RelevantExpr)
18884         return false;
18885       return Visit(E);
18886     }
18887 
18888     auto *FD = cast<FieldDecl>(ME->getMemberDecl());
18889 
18890     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
18891     //  A bit-field cannot appear in a map clause.
18892     //
18893     if (FD->isBitField()) {
18894       if (!NoDiagnose) {
18895         SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
18896           << ME->getSourceRange() << getOpenMPClauseName(CKind);
18897         return false;
18898       }
18899       if (RelevantExpr)
18900         return false;
18901       return Visit(E);
18902     }
18903 
18904     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
18905     //  If the type of a list item is a reference to a type T then the type
18906     //  will be considered to be T for all purposes of this clause.
18907     QualType CurType = BaseE->getType().getNonReferenceType();
18908 
18909     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
18910     //  A list item cannot be a variable that is a member of a structure with
18911     //  a union type.
18912     //
18913     if (CurType->isUnionType()) {
18914       if (!NoDiagnose) {
18915         SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
18916           << ME->getSourceRange();
18917         return false;
18918       }
18919       return RelevantExpr || Visit(E);
18920     }
18921 
18922     // If we got a member expression, we should not expect any array section
18923     // before that:
18924     //
18925     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
18926     //  If a list item is an element of a structure, only the rightmost symbol
18927     //  of the variable reference can be an array section.
18928     //
18929     AllowUnitySizeArraySection = false;
18930     AllowWholeSizeArraySection = false;
18931 
18932     // Record the component.
18933     Components.emplace_back(ME, FD, IsNonContiguous);
18934     return RelevantExpr || Visit(E);
18935   }
18936 
18937   bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) {
18938     Expr *E = AE->getBase()->IgnoreParenImpCasts();
18939 
18940     if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
18941       if (!NoDiagnose) {
18942         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
18943           << 0 << AE->getSourceRange();
18944         return false;
18945       }
18946       return RelevantExpr || Visit(E);
18947     }
18948 
18949     // If we got an array subscript that express the whole dimension we
18950     // can have any array expressions before. If it only expressing part of
18951     // the dimension, we can only have unitary-size array expressions.
18952     if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE,
18953                                                     E->getType()))
18954       AllowWholeSizeArraySection = false;
18955 
18956     if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) {
18957       Expr::EvalResult Result;
18958       if (!AE->getIdx()->isValueDependent() &&
18959           AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) &&
18960           !Result.Val.getInt().isZero()) {
18961         SemaRef.Diag(AE->getIdx()->getExprLoc(),
18962                      diag::err_omp_invalid_map_this_expr);
18963         SemaRef.Diag(AE->getIdx()->getExprLoc(),
18964                      diag::note_omp_invalid_subscript_on_this_ptr_map);
18965       }
18966       assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
18967       RelevantExpr = TE;
18968     }
18969 
18970     // Record the component - we don't have any declaration associated.
18971     Components.emplace_back(AE, nullptr, IsNonContiguous);
18972 
18973     return RelevantExpr || Visit(E);
18974   }
18975 
18976   bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) {
18977     // After OMP 5.0  Array section in reduction clause will be implicitly
18978     // mapped
18979     assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) &&
18980            "Array sections cannot be implicitly mapped.");
18981     Expr *E = OASE->getBase()->IgnoreParenImpCasts();
18982     QualType CurType =
18983       OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
18984 
18985     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
18986     //  If the type of a list item is a reference to a type T then the type
18987     //  will be considered to be T for all purposes of this clause.
18988     if (CurType->isReferenceType())
18989       CurType = CurType->getPointeeType();
18990 
18991     bool IsPointer = CurType->isAnyPointerType();
18992 
18993     if (!IsPointer && !CurType->isArrayType()) {
18994       SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
18995         << 0 << OASE->getSourceRange();
18996       return false;
18997     }
18998 
18999     bool NotWhole =
19000       checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType);
19001     bool NotUnity =
19002       checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType);
19003 
19004     if (AllowWholeSizeArraySection) {
19005       // Any array section is currently allowed. Allowing a whole size array
19006       // section implies allowing a unity array section as well.
19007       //
19008       // If this array section refers to the whole dimension we can still
19009       // accept other array sections before this one, except if the base is a
19010       // pointer. Otherwise, only unitary sections are accepted.
19011       if (NotWhole || IsPointer)
19012         AllowWholeSizeArraySection = false;
19013     } else if (DKind == OMPD_target_update &&
19014                SemaRef.getLangOpts().OpenMP >= 50) {
19015       if (IsPointer && !AllowAnotherPtr)
19016         SemaRef.Diag(ELoc, diag::err_omp_section_length_undefined)
19017             << /*array of unknown bound */ 1;
19018       else
19019         IsNonContiguous = true;
19020     } else if (AllowUnitySizeArraySection && NotUnity) {
19021       // A unity or whole array section is not allowed and that is not
19022       // compatible with the properties of the current array section.
19023       if (NoDiagnose)
19024         return false;
19025       SemaRef.Diag(
19026         ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
19027         << OASE->getSourceRange();
19028       return false;
19029     }
19030 
19031     if (IsPointer)
19032       AllowAnotherPtr = false;
19033 
19034     if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
19035       Expr::EvalResult ResultR;
19036       Expr::EvalResult ResultL;
19037       if (!OASE->getLength()->isValueDependent() &&
19038           OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) &&
19039           !ResultR.Val.getInt().isOne()) {
19040         SemaRef.Diag(OASE->getLength()->getExprLoc(),
19041                      diag::err_omp_invalid_map_this_expr);
19042         SemaRef.Diag(OASE->getLength()->getExprLoc(),
19043                      diag::note_omp_invalid_length_on_this_ptr_mapping);
19044       }
19045       if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() &&
19046           OASE->getLowerBound()->EvaluateAsInt(ResultL,
19047                                                SemaRef.getASTContext()) &&
19048           !ResultL.Val.getInt().isZero()) {
19049         SemaRef.Diag(OASE->getLowerBound()->getExprLoc(),
19050                      diag::err_omp_invalid_map_this_expr);
19051         SemaRef.Diag(OASE->getLowerBound()->getExprLoc(),
19052                      diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
19053       }
19054       assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
19055       RelevantExpr = TE;
19056     }
19057 
19058     // Record the component - we don't have any declaration associated.
19059     Components.emplace_back(OASE, nullptr, /*IsNonContiguous=*/false);
19060     return RelevantExpr || Visit(E);
19061   }
19062   bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) {
19063     Expr *Base = E->getBase();
19064 
19065     // Record the component - we don't have any declaration associated.
19066     Components.emplace_back(E, nullptr, IsNonContiguous);
19067 
19068     return Visit(Base->IgnoreParenImpCasts());
19069   }
19070 
19071   bool VisitUnaryOperator(UnaryOperator *UO) {
19072     if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() ||
19073         UO->getOpcode() != UO_Deref) {
19074       emitErrorMsg();
19075       return false;
19076     }
19077     if (!RelevantExpr) {
19078       // Record the component if haven't found base decl.
19079       Components.emplace_back(UO, nullptr, /*IsNonContiguous=*/false);
19080     }
19081     return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts());
19082   }
19083   bool VisitBinaryOperator(BinaryOperator *BO) {
19084     if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) {
19085       emitErrorMsg();
19086       return false;
19087     }
19088 
19089     // Pointer arithmetic is the only thing we expect to happen here so after we
19090     // make sure the binary operator is a pointer type, the we only thing need
19091     // to to is to visit the subtree that has the same type as root (so that we
19092     // know the other subtree is just an offset)
19093     Expr *LE = BO->getLHS()->IgnoreParenImpCasts();
19094     Expr *RE = BO->getRHS()->IgnoreParenImpCasts();
19095     Components.emplace_back(BO, nullptr, false);
19096     assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() ||
19097             RE->getType().getTypePtr() == BO->getType().getTypePtr()) &&
19098            "Either LHS or RHS have base decl inside");
19099     if (BO->getType().getTypePtr() == LE->getType().getTypePtr())
19100       return RelevantExpr || Visit(LE);
19101     return RelevantExpr || Visit(RE);
19102   }
19103   bool VisitCXXThisExpr(CXXThisExpr *CTE) {
19104     assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
19105     RelevantExpr = CTE;
19106     Components.emplace_back(CTE, nullptr, IsNonContiguous);
19107     return true;
19108   }
19109   bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) {
19110     assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
19111     Components.emplace_back(COCE, nullptr, IsNonContiguous);
19112     return true;
19113   }
19114   bool VisitOpaqueValueExpr(OpaqueValueExpr *E) {
19115     Expr *Source = E->getSourceExpr();
19116     if (!Source) {
19117       emitErrorMsg();
19118       return false;
19119     }
19120     return Visit(Source);
19121   }
19122   bool VisitStmt(Stmt *) {
19123     emitErrorMsg();
19124     return false;
19125   }
19126   const Expr *getFoundBase() const {
19127     return RelevantExpr;
19128   }
19129   explicit MapBaseChecker(
19130       Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind,
19131       OMPClauseMappableExprCommon::MappableExprComponentList &Components,
19132       bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange)
19133       : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components),
19134         NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {}
19135 };
19136 } // namespace
19137 
19138 /// Return the expression of the base of the mappable expression or null if it
19139 /// cannot be determined and do all the necessary checks to see if the expression
19140 /// is valid as a standalone mappable expression. In the process, record all the
19141 /// components of the expression.
19142 static const Expr *checkMapClauseExpressionBase(
19143     Sema &SemaRef, Expr *E,
19144     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
19145     OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) {
19146   SourceLocation ELoc = E->getExprLoc();
19147   SourceRange ERange = E->getSourceRange();
19148   MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc,
19149                          ERange);
19150   if (Checker.Visit(E->IgnoreParens())) {
19151     // Check if the highest dimension array section has length specified
19152     if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() &&
19153         (CKind == OMPC_to || CKind == OMPC_from)) {
19154       auto CI = CurComponents.rbegin();
19155       auto CE = CurComponents.rend();
19156       for (; CI != CE; ++CI) {
19157         const auto *OASE =
19158             dyn_cast<OMPArraySectionExpr>(CI->getAssociatedExpression());
19159         if (!OASE)
19160           continue;
19161         if (OASE && OASE->getLength())
19162           break;
19163         SemaRef.Diag(ELoc, diag::err_array_section_does_not_specify_length)
19164             << ERange;
19165       }
19166     }
19167     return Checker.getFoundBase();
19168   }
19169   return nullptr;
19170 }
19171 
19172 // Return true if expression E associated with value VD has conflicts with other
19173 // map information.
19174 static bool checkMapConflicts(
19175     Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
19176     bool CurrentRegionOnly,
19177     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
19178     OpenMPClauseKind CKind) {
19179   assert(VD && E);
19180   SourceLocation ELoc = E->getExprLoc();
19181   SourceRange ERange = E->getSourceRange();
19182 
19183   // In order to easily check the conflicts we need to match each component of
19184   // the expression under test with the components of the expressions that are
19185   // already in the stack.
19186 
19187   assert(!CurComponents.empty() && "Map clause expression with no components!");
19188   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
19189          "Map clause expression with unexpected base!");
19190 
19191   // Variables to help detecting enclosing problems in data environment nests.
19192   bool IsEnclosedByDataEnvironmentExpr = false;
19193   const Expr *EnclosingExpr = nullptr;
19194 
19195   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
19196       VD, CurrentRegionOnly,
19197       [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
19198        ERange, CKind, &EnclosingExpr,
19199        CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
19200                           StackComponents,
19201                       OpenMPClauseKind Kind) {
19202         if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50)
19203           return false;
19204         assert(!StackComponents.empty() &&
19205                "Map clause expression with no components!");
19206         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
19207                "Map clause expression with unexpected base!");
19208         (void)VD;
19209 
19210         // The whole expression in the stack.
19211         const Expr *RE = StackComponents.front().getAssociatedExpression();
19212 
19213         // Expressions must start from the same base. Here we detect at which
19214         // point both expressions diverge from each other and see if we can
19215         // detect if the memory referred to both expressions is contiguous and
19216         // do not overlap.
19217         auto CI = CurComponents.rbegin();
19218         auto CE = CurComponents.rend();
19219         auto SI = StackComponents.rbegin();
19220         auto SE = StackComponents.rend();
19221         for (; CI != CE && SI != SE; ++CI, ++SI) {
19222 
19223           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
19224           //  At most one list item can be an array item derived from a given
19225           //  variable in map clauses of the same construct.
19226           if (CurrentRegionOnly &&
19227               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
19228                isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) ||
19229                isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) &&
19230               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
19231                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) ||
19232                isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) {
19233             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
19234                          diag::err_omp_multiple_array_items_in_map_clause)
19235                 << CI->getAssociatedExpression()->getSourceRange();
19236             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
19237                          diag::note_used_here)
19238                 << SI->getAssociatedExpression()->getSourceRange();
19239             return true;
19240           }
19241 
19242           // Do both expressions have the same kind?
19243           if (CI->getAssociatedExpression()->getStmtClass() !=
19244               SI->getAssociatedExpression()->getStmtClass())
19245             break;
19246 
19247           // Are we dealing with different variables/fields?
19248           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
19249             break;
19250         }
19251         // Check if the extra components of the expressions in the enclosing
19252         // data environment are redundant for the current base declaration.
19253         // If they are, the maps completely overlap, which is legal.
19254         for (; SI != SE; ++SI) {
19255           QualType Type;
19256           if (const auto *ASE =
19257                   dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
19258             Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
19259           } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
19260                          SI->getAssociatedExpression())) {
19261             const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
19262             Type =
19263                 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
19264           } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>(
19265                          SI->getAssociatedExpression())) {
19266             Type = OASE->getBase()->getType()->getPointeeType();
19267           }
19268           if (Type.isNull() || Type->isAnyPointerType() ||
19269               checkArrayExpressionDoesNotReferToWholeSize(
19270                   SemaRef, SI->getAssociatedExpression(), Type))
19271             break;
19272         }
19273 
19274         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
19275         //  List items of map clauses in the same construct must not share
19276         //  original storage.
19277         //
19278         // If the expressions are exactly the same or one is a subset of the
19279         // other, it means they are sharing storage.
19280         if (CI == CE && SI == SE) {
19281           if (CurrentRegionOnly) {
19282             if (CKind == OMPC_map) {
19283               SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
19284             } else {
19285               assert(CKind == OMPC_to || CKind == OMPC_from);
19286               SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
19287                   << ERange;
19288             }
19289             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
19290                 << RE->getSourceRange();
19291             return true;
19292           }
19293           // If we find the same expression in the enclosing data environment,
19294           // that is legal.
19295           IsEnclosedByDataEnvironmentExpr = true;
19296           return false;
19297         }
19298 
19299         QualType DerivedType =
19300             std::prev(CI)->getAssociatedDeclaration()->getType();
19301         SourceLocation DerivedLoc =
19302             std::prev(CI)->getAssociatedExpression()->getExprLoc();
19303 
19304         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
19305         //  If the type of a list item is a reference to a type T then the type
19306         //  will be considered to be T for all purposes of this clause.
19307         DerivedType = DerivedType.getNonReferenceType();
19308 
19309         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
19310         //  A variable for which the type is pointer and an array section
19311         //  derived from that variable must not appear as list items of map
19312         //  clauses of the same construct.
19313         //
19314         // Also, cover one of the cases in:
19315         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
19316         //  If any part of the original storage of a list item has corresponding
19317         //  storage in the device data environment, all of the original storage
19318         //  must have corresponding storage in the device data environment.
19319         //
19320         if (DerivedType->isAnyPointerType()) {
19321           if (CI == CE || SI == SE) {
19322             SemaRef.Diag(
19323                 DerivedLoc,
19324                 diag::err_omp_pointer_mapped_along_with_derived_section)
19325                 << DerivedLoc;
19326             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
19327                 << RE->getSourceRange();
19328             return true;
19329           }
19330           if (CI->getAssociatedExpression()->getStmtClass() !=
19331                          SI->getAssociatedExpression()->getStmtClass() ||
19332                      CI->getAssociatedDeclaration()->getCanonicalDecl() ==
19333                          SI->getAssociatedDeclaration()->getCanonicalDecl()) {
19334             assert(CI != CE && SI != SE);
19335             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
19336                 << DerivedLoc;
19337             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
19338                 << RE->getSourceRange();
19339             return true;
19340           }
19341         }
19342 
19343         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
19344         //  List items of map clauses in the same construct must not share
19345         //  original storage.
19346         //
19347         // An expression is a subset of the other.
19348         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
19349           if (CKind == OMPC_map) {
19350             if (CI != CE || SI != SE) {
19351               // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
19352               // a pointer.
19353               auto Begin =
19354                   CI != CE ? CurComponents.begin() : StackComponents.begin();
19355               auto End = CI != CE ? CurComponents.end() : StackComponents.end();
19356               auto It = Begin;
19357               while (It != End && !It->getAssociatedDeclaration())
19358                 std::advance(It, 1);
19359               assert(It != End &&
19360                      "Expected at least one component with the declaration.");
19361               if (It != Begin && It->getAssociatedDeclaration()
19362                                      ->getType()
19363                                      .getCanonicalType()
19364                                      ->isAnyPointerType()) {
19365                 IsEnclosedByDataEnvironmentExpr = false;
19366                 EnclosingExpr = nullptr;
19367                 return false;
19368               }
19369             }
19370             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
19371           } else {
19372             assert(CKind == OMPC_to || CKind == OMPC_from);
19373             SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
19374                 << ERange;
19375           }
19376           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
19377               << RE->getSourceRange();
19378           return true;
19379         }
19380 
19381         // The current expression uses the same base as other expression in the
19382         // data environment but does not contain it completely.
19383         if (!CurrentRegionOnly && SI != SE)
19384           EnclosingExpr = RE;
19385 
19386         // The current expression is a subset of the expression in the data
19387         // environment.
19388         IsEnclosedByDataEnvironmentExpr |=
19389             (!CurrentRegionOnly && CI != CE && SI == SE);
19390 
19391         return false;
19392       });
19393 
19394   if (CurrentRegionOnly)
19395     return FoundError;
19396 
19397   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
19398   //  If any part of the original storage of a list item has corresponding
19399   //  storage in the device data environment, all of the original storage must
19400   //  have corresponding storage in the device data environment.
19401   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
19402   //  If a list item is an element of a structure, and a different element of
19403   //  the structure has a corresponding list item in the device data environment
19404   //  prior to a task encountering the construct associated with the map clause,
19405   //  then the list item must also have a corresponding list item in the device
19406   //  data environment prior to the task encountering the construct.
19407   //
19408   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
19409     SemaRef.Diag(ELoc,
19410                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
19411         << ERange;
19412     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
19413         << EnclosingExpr->getSourceRange();
19414     return true;
19415   }
19416 
19417   return FoundError;
19418 }
19419 
19420 // Look up the user-defined mapper given the mapper name and mapped type, and
19421 // build a reference to it.
19422 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
19423                                             CXXScopeSpec &MapperIdScopeSpec,
19424                                             const DeclarationNameInfo &MapperId,
19425                                             QualType Type,
19426                                             Expr *UnresolvedMapper) {
19427   if (MapperIdScopeSpec.isInvalid())
19428     return ExprError();
19429   // Get the actual type for the array type.
19430   if (Type->isArrayType()) {
19431     assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type");
19432     Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType();
19433   }
19434   // Find all user-defined mappers with the given MapperId.
19435   SmallVector<UnresolvedSet<8>, 4> Lookups;
19436   LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
19437   Lookup.suppressDiagnostics();
19438   if (S) {
19439     while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
19440       NamedDecl *D = Lookup.getRepresentativeDecl();
19441       while (S && !S->isDeclScope(D))
19442         S = S->getParent();
19443       if (S)
19444         S = S->getParent();
19445       Lookups.emplace_back();
19446       Lookups.back().append(Lookup.begin(), Lookup.end());
19447       Lookup.clear();
19448     }
19449   } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
19450     // Extract the user-defined mappers with the given MapperId.
19451     Lookups.push_back(UnresolvedSet<8>());
19452     for (NamedDecl *D : ULE->decls()) {
19453       auto *DMD = cast<OMPDeclareMapperDecl>(D);
19454       assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
19455       Lookups.back().addDecl(DMD);
19456     }
19457   }
19458   // Defer the lookup for dependent types. The results will be passed through
19459   // UnresolvedMapper on instantiation.
19460   if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
19461       Type->isInstantiationDependentType() ||
19462       Type->containsUnexpandedParameterPack() ||
19463       filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
19464         return !D->isInvalidDecl() &&
19465                (D->getType()->isDependentType() ||
19466                 D->getType()->isInstantiationDependentType() ||
19467                 D->getType()->containsUnexpandedParameterPack());
19468       })) {
19469     UnresolvedSet<8> URS;
19470     for (const UnresolvedSet<8> &Set : Lookups) {
19471       if (Set.empty())
19472         continue;
19473       URS.append(Set.begin(), Set.end());
19474     }
19475     return UnresolvedLookupExpr::Create(
19476         SemaRef.Context, /*NamingClass=*/nullptr,
19477         MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
19478         /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
19479   }
19480   SourceLocation Loc = MapperId.getLoc();
19481   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
19482   //  The type must be of struct, union or class type in C and C++
19483   if (!Type->isStructureOrClassType() && !Type->isUnionType() &&
19484       (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) {
19485     SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type);
19486     return ExprError();
19487   }
19488   // Perform argument dependent lookup.
19489   if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
19490     argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
19491   // Return the first user-defined mapper with the desired type.
19492   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
19493           Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
19494             if (!D->isInvalidDecl() &&
19495                 SemaRef.Context.hasSameType(D->getType(), Type))
19496               return D;
19497             return nullptr;
19498           }))
19499     return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
19500   // Find the first user-defined mapper with a type derived from the desired
19501   // type.
19502   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
19503           Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
19504             if (!D->isInvalidDecl() &&
19505                 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
19506                 !Type.isMoreQualifiedThan(D->getType()))
19507               return D;
19508             return nullptr;
19509           })) {
19510     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
19511                        /*DetectVirtual=*/false);
19512     if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
19513       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
19514               VD->getType().getUnqualifiedType()))) {
19515         if (SemaRef.CheckBaseClassAccess(
19516                 Loc, VD->getType(), Type, Paths.front(),
19517                 /*DiagID=*/0) != Sema::AR_inaccessible) {
19518           return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
19519         }
19520       }
19521     }
19522   }
19523   // Report error if a mapper is specified, but cannot be found.
19524   if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
19525     SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
19526         << Type << MapperId.getName();
19527     return ExprError();
19528   }
19529   return ExprEmpty();
19530 }
19531 
19532 namespace {
19533 // Utility struct that gathers all the related lists associated with a mappable
19534 // expression.
19535 struct MappableVarListInfo {
19536   // The list of expressions.
19537   ArrayRef<Expr *> VarList;
19538   // The list of processed expressions.
19539   SmallVector<Expr *, 16> ProcessedVarList;
19540   // The mappble components for each expression.
19541   OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
19542   // The base declaration of the variable.
19543   SmallVector<ValueDecl *, 16> VarBaseDeclarations;
19544   // The reference to the user-defined mapper associated with every expression.
19545   SmallVector<Expr *, 16> UDMapperList;
19546 
19547   MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
19548     // We have a list of components and base declarations for each entry in the
19549     // variable list.
19550     VarComponents.reserve(VarList.size());
19551     VarBaseDeclarations.reserve(VarList.size());
19552   }
19553 };
19554 }
19555 
19556 // Check the validity of the provided variable list for the provided clause kind
19557 // \a CKind. In the check process the valid expressions, mappable expression
19558 // components, variables, and user-defined mappers are extracted and used to
19559 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
19560 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
19561 // and \a MapperId are expected to be valid if the clause kind is 'map'.
19562 static void checkMappableExpressionList(
19563     Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
19564     MappableVarListInfo &MVLI, SourceLocation StartLoc,
19565     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
19566     ArrayRef<Expr *> UnresolvedMappers,
19567     OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
19568     ArrayRef<OpenMPMapModifierKind> Modifiers = None,
19569     bool IsMapTypeImplicit = false, bool NoDiagnose = false) {
19570   // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
19571   assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
19572          "Unexpected clause kind with mappable expressions!");
19573 
19574   // If the identifier of user-defined mapper is not specified, it is "default".
19575   // We do not change the actual name in this clause to distinguish whether a
19576   // mapper is specified explicitly, i.e., it is not explicitly specified when
19577   // MapperId.getName() is empty.
19578   if (!MapperId.getName() || MapperId.getName().isEmpty()) {
19579     auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
19580     MapperId.setName(DeclNames.getIdentifier(
19581         &SemaRef.getASTContext().Idents.get("default")));
19582     MapperId.setLoc(StartLoc);
19583   }
19584 
19585   // Iterators to find the current unresolved mapper expression.
19586   auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
19587   bool UpdateUMIt = false;
19588   Expr *UnresolvedMapper = nullptr;
19589 
19590   bool HasHoldModifier =
19591       llvm::is_contained(Modifiers, OMPC_MAP_MODIFIER_ompx_hold);
19592 
19593   // Keep track of the mappable components and base declarations in this clause.
19594   // Each entry in the list is going to have a list of components associated. We
19595   // record each set of the components so that we can build the clause later on.
19596   // In the end we should have the same amount of declarations and component
19597   // lists.
19598 
19599   for (Expr *RE : MVLI.VarList) {
19600     assert(RE && "Null expr in omp to/from/map clause");
19601     SourceLocation ELoc = RE->getExprLoc();
19602 
19603     // Find the current unresolved mapper expression.
19604     if (UpdateUMIt && UMIt != UMEnd) {
19605       UMIt++;
19606       assert(
19607           UMIt != UMEnd &&
19608           "Expect the size of UnresolvedMappers to match with that of VarList");
19609     }
19610     UpdateUMIt = true;
19611     if (UMIt != UMEnd)
19612       UnresolvedMapper = *UMIt;
19613 
19614     const Expr *VE = RE->IgnoreParenLValueCasts();
19615 
19616     if (VE->isValueDependent() || VE->isTypeDependent() ||
19617         VE->isInstantiationDependent() ||
19618         VE->containsUnexpandedParameterPack()) {
19619       // Try to find the associated user-defined mapper.
19620       ExprResult ER = buildUserDefinedMapperRef(
19621           SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
19622           VE->getType().getCanonicalType(), UnresolvedMapper);
19623       if (ER.isInvalid())
19624         continue;
19625       MVLI.UDMapperList.push_back(ER.get());
19626       // We can only analyze this information once the missing information is
19627       // resolved.
19628       MVLI.ProcessedVarList.push_back(RE);
19629       continue;
19630     }
19631 
19632     Expr *SimpleExpr = RE->IgnoreParenCasts();
19633 
19634     if (!RE->isLValue()) {
19635       if (SemaRef.getLangOpts().OpenMP < 50) {
19636         SemaRef.Diag(
19637             ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
19638             << RE->getSourceRange();
19639       } else {
19640         SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses)
19641             << getOpenMPClauseName(CKind) << RE->getSourceRange();
19642       }
19643       continue;
19644     }
19645 
19646     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
19647     ValueDecl *CurDeclaration = nullptr;
19648 
19649     // Obtain the array or member expression bases if required. Also, fill the
19650     // components array with all the components identified in the process.
19651     const Expr *BE =
19652         checkMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind,
19653                                      DSAS->getCurrentDirective(), NoDiagnose);
19654     if (!BE)
19655       continue;
19656 
19657     assert(!CurComponents.empty() &&
19658            "Invalid mappable expression information.");
19659 
19660     if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
19661       // Add store "this" pointer to class in DSAStackTy for future checking
19662       DSAS->addMappedClassesQualTypes(TE->getType());
19663       // Try to find the associated user-defined mapper.
19664       ExprResult ER = buildUserDefinedMapperRef(
19665           SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
19666           VE->getType().getCanonicalType(), UnresolvedMapper);
19667       if (ER.isInvalid())
19668         continue;
19669       MVLI.UDMapperList.push_back(ER.get());
19670       // Skip restriction checking for variable or field declarations
19671       MVLI.ProcessedVarList.push_back(RE);
19672       MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
19673       MVLI.VarComponents.back().append(CurComponents.begin(),
19674                                        CurComponents.end());
19675       MVLI.VarBaseDeclarations.push_back(nullptr);
19676       continue;
19677     }
19678 
19679     // For the following checks, we rely on the base declaration which is
19680     // expected to be associated with the last component. The declaration is
19681     // expected to be a variable or a field (if 'this' is being mapped).
19682     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
19683     assert(CurDeclaration && "Null decl on map clause.");
19684     assert(
19685         CurDeclaration->isCanonicalDecl() &&
19686         "Expecting components to have associated only canonical declarations.");
19687 
19688     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
19689     const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
19690 
19691     assert((VD || FD) && "Only variables or fields are expected here!");
19692     (void)FD;
19693 
19694     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
19695     // threadprivate variables cannot appear in a map clause.
19696     // OpenMP 4.5 [2.10.5, target update Construct]
19697     // threadprivate variables cannot appear in a from clause.
19698     if (VD && DSAS->isThreadPrivate(VD)) {
19699       if (NoDiagnose)
19700         continue;
19701       DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
19702       SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
19703           << getOpenMPClauseName(CKind);
19704       reportOriginalDsa(SemaRef, DSAS, VD, DVar);
19705       continue;
19706     }
19707 
19708     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
19709     //  A list item cannot appear in both a map clause and a data-sharing
19710     //  attribute clause on the same construct.
19711 
19712     // Check conflicts with other map clause expressions. We check the conflicts
19713     // with the current construct separately from the enclosing data
19714     // environment, because the restrictions are different. We only have to
19715     // check conflicts across regions for the map clauses.
19716     if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
19717                           /*CurrentRegionOnly=*/true, CurComponents, CKind))
19718       break;
19719     if (CKind == OMPC_map &&
19720         (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) &&
19721         checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
19722                           /*CurrentRegionOnly=*/false, CurComponents, CKind))
19723       break;
19724 
19725     // OpenMP 4.5 [2.10.5, target update Construct]
19726     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
19727     //  If the type of a list item is a reference to a type T then the type will
19728     //  be considered to be T for all purposes of this clause.
19729     auto I = llvm::find_if(
19730         CurComponents,
19731         [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
19732           return MC.getAssociatedDeclaration();
19733         });
19734     assert(I != CurComponents.end() && "Null decl on map clause.");
19735     (void)I;
19736     QualType Type;
19737     auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens());
19738     auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens());
19739     auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens());
19740     if (ASE) {
19741       Type = ASE->getType().getNonReferenceType();
19742     } else if (OASE) {
19743       QualType BaseType =
19744           OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
19745       if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
19746         Type = ATy->getElementType();
19747       else
19748         Type = BaseType->getPointeeType();
19749       Type = Type.getNonReferenceType();
19750     } else if (OAShE) {
19751       Type = OAShE->getBase()->getType()->getPointeeType();
19752     } else {
19753       Type = VE->getType();
19754     }
19755 
19756     // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
19757     // A list item in a to or from clause must have a mappable type.
19758     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
19759     //  A list item must have a mappable type.
19760     if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
19761                            DSAS, Type, /*FullCheck=*/true))
19762       continue;
19763 
19764     if (CKind == OMPC_map) {
19765       // target enter data
19766       // OpenMP [2.10.2, Restrictions, p. 99]
19767       // A map-type must be specified in all map clauses and must be either
19768       // to or alloc.
19769       OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
19770       if (DKind == OMPD_target_enter_data &&
19771           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
19772         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
19773             << (IsMapTypeImplicit ? 1 : 0)
19774             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
19775             << getOpenMPDirectiveName(DKind);
19776         continue;
19777       }
19778 
19779       // target exit_data
19780       // OpenMP [2.10.3, Restrictions, p. 102]
19781       // A map-type must be specified in all map clauses and must be either
19782       // from, release, or delete.
19783       if (DKind == OMPD_target_exit_data &&
19784           !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
19785             MapType == OMPC_MAP_delete)) {
19786         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
19787             << (IsMapTypeImplicit ? 1 : 0)
19788             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
19789             << getOpenMPDirectiveName(DKind);
19790         continue;
19791       }
19792 
19793       // The 'ompx_hold' modifier is specifically intended to be used on a
19794       // 'target' or 'target data' directive to prevent data from being unmapped
19795       // during the associated statement.  It is not permitted on a 'target
19796       // enter data' or 'target exit data' directive, which have no associated
19797       // statement.
19798       if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) &&
19799           HasHoldModifier) {
19800         SemaRef.Diag(StartLoc,
19801                      diag::err_omp_invalid_map_type_modifier_for_directive)
19802             << getOpenMPSimpleClauseTypeName(OMPC_map,
19803                                              OMPC_MAP_MODIFIER_ompx_hold)
19804             << getOpenMPDirectiveName(DKind);
19805         continue;
19806       }
19807 
19808       // target, target data
19809       // OpenMP 5.0 [2.12.2, Restrictions, p. 163]
19810       // OpenMP 5.0 [2.12.5, Restrictions, p. 174]
19811       // A map-type in a map clause must be to, from, tofrom or alloc
19812       if ((DKind == OMPD_target_data ||
19813            isOpenMPTargetExecutionDirective(DKind)) &&
19814           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from ||
19815             MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) {
19816         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
19817             << (IsMapTypeImplicit ? 1 : 0)
19818             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
19819             << getOpenMPDirectiveName(DKind);
19820         continue;
19821       }
19822 
19823       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
19824       // A list item cannot appear in both a map clause and a data-sharing
19825       // attribute clause on the same construct
19826       //
19827       // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
19828       // A list item cannot appear in both a map clause and a data-sharing
19829       // attribute clause on the same construct unless the construct is a
19830       // combined construct.
19831       if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
19832                   isOpenMPTargetExecutionDirective(DKind)) ||
19833                  DKind == OMPD_target)) {
19834         DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
19835         if (isOpenMPPrivate(DVar.CKind)) {
19836           SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
19837               << getOpenMPClauseName(DVar.CKind)
19838               << getOpenMPClauseName(OMPC_map)
19839               << getOpenMPDirectiveName(DSAS->getCurrentDirective());
19840           reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
19841           continue;
19842         }
19843       }
19844     }
19845 
19846     // Try to find the associated user-defined mapper.
19847     ExprResult ER = buildUserDefinedMapperRef(
19848         SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
19849         Type.getCanonicalType(), UnresolvedMapper);
19850     if (ER.isInvalid())
19851       continue;
19852     MVLI.UDMapperList.push_back(ER.get());
19853 
19854     // Save the current expression.
19855     MVLI.ProcessedVarList.push_back(RE);
19856 
19857     // Store the components in the stack so that they can be used to check
19858     // against other clauses later on.
19859     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
19860                                           /*WhereFoundClauseKind=*/OMPC_map);
19861 
19862     // Save the components and declaration to create the clause. For purposes of
19863     // the clause creation, any component list that has has base 'this' uses
19864     // null as base declaration.
19865     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
19866     MVLI.VarComponents.back().append(CurComponents.begin(),
19867                                      CurComponents.end());
19868     MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
19869                                                            : CurDeclaration);
19870   }
19871 }
19872 
19873 OMPClause *Sema::ActOnOpenMPMapClause(
19874     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
19875     ArrayRef<SourceLocation> MapTypeModifiersLoc,
19876     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
19877     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
19878     SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
19879     const OMPVarListLocTy &Locs, bool NoDiagnose,
19880     ArrayRef<Expr *> UnresolvedMappers) {
19881   OpenMPMapModifierKind Modifiers[] = {
19882       OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
19883       OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
19884       OMPC_MAP_MODIFIER_unknown};
19885   SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];
19886 
19887   // Process map-type-modifiers, flag errors for duplicate modifiers.
19888   unsigned Count = 0;
19889   for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
19890     if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
19891         llvm::is_contained(Modifiers, MapTypeModifiers[I])) {
19892       Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
19893       continue;
19894     }
19895     assert(Count < NumberOfOMPMapClauseModifiers &&
19896            "Modifiers exceed the allowed number of map type modifiers");
19897     Modifiers[Count] = MapTypeModifiers[I];
19898     ModifiersLoc[Count] = MapTypeModifiersLoc[I];
19899     ++Count;
19900   }
19901 
19902   MappableVarListInfo MVLI(VarList);
19903   checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
19904                               MapperIdScopeSpec, MapperId, UnresolvedMappers,
19905                               MapType, Modifiers, IsMapTypeImplicit,
19906                               NoDiagnose);
19907 
19908   // We need to produce a map clause even if we don't have variables so that
19909   // other diagnostics related with non-existing map clauses are accurate.
19910   return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
19911                               MVLI.VarBaseDeclarations, MVLI.VarComponents,
19912                               MVLI.UDMapperList, Modifiers, ModifiersLoc,
19913                               MapperIdScopeSpec.getWithLocInContext(Context),
19914                               MapperId, MapType, IsMapTypeImplicit, MapLoc);
19915 }
19916 
19917 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
19918                                                TypeResult ParsedType) {
19919   assert(ParsedType.isUsable());
19920 
19921   QualType ReductionType = GetTypeFromParser(ParsedType.get());
19922   if (ReductionType.isNull())
19923     return QualType();
19924 
19925   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
19926   // A type name in a declare reduction directive cannot be a function type, an
19927   // array type, a reference type, or a type qualified with const, volatile or
19928   // restrict.
19929   if (ReductionType.hasQualifiers()) {
19930     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
19931     return QualType();
19932   }
19933 
19934   if (ReductionType->isFunctionType()) {
19935     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
19936     return QualType();
19937   }
19938   if (ReductionType->isReferenceType()) {
19939     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
19940     return QualType();
19941   }
19942   if (ReductionType->isArrayType()) {
19943     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
19944     return QualType();
19945   }
19946   return ReductionType;
19947 }
19948 
19949 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
19950     Scope *S, DeclContext *DC, DeclarationName Name,
19951     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
19952     AccessSpecifier AS, Decl *PrevDeclInScope) {
19953   SmallVector<Decl *, 8> Decls;
19954   Decls.reserve(ReductionTypes.size());
19955 
19956   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
19957                       forRedeclarationInCurContext());
19958   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
19959   // A reduction-identifier may not be re-declared in the current scope for the
19960   // same type or for a type that is compatible according to the base language
19961   // rules.
19962   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
19963   OMPDeclareReductionDecl *PrevDRD = nullptr;
19964   bool InCompoundScope = true;
19965   if (S != nullptr) {
19966     // Find previous declaration with the same name not referenced in other
19967     // declarations.
19968     FunctionScopeInfo *ParentFn = getEnclosingFunction();
19969     InCompoundScope =
19970         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
19971     LookupName(Lookup, S);
19972     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
19973                          /*AllowInlineNamespace=*/false);
19974     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
19975     LookupResult::Filter Filter = Lookup.makeFilter();
19976     while (Filter.hasNext()) {
19977       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
19978       if (InCompoundScope) {
19979         auto I = UsedAsPrevious.find(PrevDecl);
19980         if (I == UsedAsPrevious.end())
19981           UsedAsPrevious[PrevDecl] = false;
19982         if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
19983           UsedAsPrevious[D] = true;
19984       }
19985       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
19986           PrevDecl->getLocation();
19987     }
19988     Filter.done();
19989     if (InCompoundScope) {
19990       for (const auto &PrevData : UsedAsPrevious) {
19991         if (!PrevData.second) {
19992           PrevDRD = PrevData.first;
19993           break;
19994         }
19995       }
19996     }
19997   } else if (PrevDeclInScope != nullptr) {
19998     auto *PrevDRDInScope = PrevDRD =
19999         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
20000     do {
20001       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
20002           PrevDRDInScope->getLocation();
20003       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
20004     } while (PrevDRDInScope != nullptr);
20005   }
20006   for (const auto &TyData : ReductionTypes) {
20007     const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
20008     bool Invalid = false;
20009     if (I != PreviousRedeclTypes.end()) {
20010       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
20011           << TyData.first;
20012       Diag(I->second, diag::note_previous_definition);
20013       Invalid = true;
20014     }
20015     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
20016     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
20017                                                 Name, TyData.first, PrevDRD);
20018     DC->addDecl(DRD);
20019     DRD->setAccess(AS);
20020     Decls.push_back(DRD);
20021     if (Invalid)
20022       DRD->setInvalidDecl();
20023     else
20024       PrevDRD = DRD;
20025   }
20026 
20027   return DeclGroupPtrTy::make(
20028       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
20029 }
20030 
20031 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
20032   auto *DRD = cast<OMPDeclareReductionDecl>(D);
20033 
20034   // Enter new function scope.
20035   PushFunctionScope();
20036   setFunctionHasBranchProtectedScope();
20037   getCurFunction()->setHasOMPDeclareReductionCombiner();
20038 
20039   if (S != nullptr)
20040     PushDeclContext(S, DRD);
20041   else
20042     CurContext = DRD;
20043 
20044   PushExpressionEvaluationContext(
20045       ExpressionEvaluationContext::PotentiallyEvaluated);
20046 
20047   QualType ReductionType = DRD->getType();
20048   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
20049   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
20050   // uses semantics of argument handles by value, but it should be passed by
20051   // reference. C lang does not support references, so pass all parameters as
20052   // pointers.
20053   // Create 'T omp_in;' variable.
20054   VarDecl *OmpInParm =
20055       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
20056   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
20057   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
20058   // uses semantics of argument handles by value, but it should be passed by
20059   // reference. C lang does not support references, so pass all parameters as
20060   // pointers.
20061   // Create 'T omp_out;' variable.
20062   VarDecl *OmpOutParm =
20063       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
20064   if (S != nullptr) {
20065     PushOnScopeChains(OmpInParm, S);
20066     PushOnScopeChains(OmpOutParm, S);
20067   } else {
20068     DRD->addDecl(OmpInParm);
20069     DRD->addDecl(OmpOutParm);
20070   }
20071   Expr *InE =
20072       ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
20073   Expr *OutE =
20074       ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
20075   DRD->setCombinerData(InE, OutE);
20076 }
20077 
20078 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
20079   auto *DRD = cast<OMPDeclareReductionDecl>(D);
20080   DiscardCleanupsInEvaluationContext();
20081   PopExpressionEvaluationContext();
20082 
20083   PopDeclContext();
20084   PopFunctionScopeInfo();
20085 
20086   if (Combiner != nullptr)
20087     DRD->setCombiner(Combiner);
20088   else
20089     DRD->setInvalidDecl();
20090 }
20091 
20092 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
20093   auto *DRD = cast<OMPDeclareReductionDecl>(D);
20094 
20095   // Enter new function scope.
20096   PushFunctionScope();
20097   setFunctionHasBranchProtectedScope();
20098 
20099   if (S != nullptr)
20100     PushDeclContext(S, DRD);
20101   else
20102     CurContext = DRD;
20103 
20104   PushExpressionEvaluationContext(
20105       ExpressionEvaluationContext::PotentiallyEvaluated);
20106 
20107   QualType ReductionType = DRD->getType();
20108   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
20109   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
20110   // uses semantics of argument handles by value, but it should be passed by
20111   // reference. C lang does not support references, so pass all parameters as
20112   // pointers.
20113   // Create 'T omp_priv;' variable.
20114   VarDecl *OmpPrivParm =
20115       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
20116   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
20117   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
20118   // uses semantics of argument handles by value, but it should be passed by
20119   // reference. C lang does not support references, so pass all parameters as
20120   // pointers.
20121   // Create 'T omp_orig;' variable.
20122   VarDecl *OmpOrigParm =
20123       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
20124   if (S != nullptr) {
20125     PushOnScopeChains(OmpPrivParm, S);
20126     PushOnScopeChains(OmpOrigParm, S);
20127   } else {
20128     DRD->addDecl(OmpPrivParm);
20129     DRD->addDecl(OmpOrigParm);
20130   }
20131   Expr *OrigE =
20132       ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
20133   Expr *PrivE =
20134       ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
20135   DRD->setInitializerData(OrigE, PrivE);
20136   return OmpPrivParm;
20137 }
20138 
20139 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
20140                                                      VarDecl *OmpPrivParm) {
20141   auto *DRD = cast<OMPDeclareReductionDecl>(D);
20142   DiscardCleanupsInEvaluationContext();
20143   PopExpressionEvaluationContext();
20144 
20145   PopDeclContext();
20146   PopFunctionScopeInfo();
20147 
20148   if (Initializer != nullptr) {
20149     DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
20150   } else if (OmpPrivParm->hasInit()) {
20151     DRD->setInitializer(OmpPrivParm->getInit(),
20152                         OmpPrivParm->isDirectInit()
20153                             ? OMPDeclareReductionDecl::DirectInit
20154                             : OMPDeclareReductionDecl::CopyInit);
20155   } else {
20156     DRD->setInvalidDecl();
20157   }
20158 }
20159 
20160 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
20161     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
20162   for (Decl *D : DeclReductions.get()) {
20163     if (IsValid) {
20164       if (S)
20165         PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
20166                           /*AddToContext=*/false);
20167     } else {
20168       D->setInvalidDecl();
20169     }
20170   }
20171   return DeclReductions;
20172 }
20173 
20174 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
20175   TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
20176   QualType T = TInfo->getType();
20177   if (D.isInvalidType())
20178     return true;
20179 
20180   if (getLangOpts().CPlusPlus) {
20181     // Check that there are no default arguments (C++ only).
20182     CheckExtraCXXDefaultArguments(D);
20183   }
20184 
20185   return CreateParsedType(T, TInfo);
20186 }
20187 
20188 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
20189                                             TypeResult ParsedType) {
20190   assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
20191 
20192   QualType MapperType = GetTypeFromParser(ParsedType.get());
20193   assert(!MapperType.isNull() && "Expect valid mapper type");
20194 
20195   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
20196   //  The type must be of struct, union or class type in C and C++
20197   if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
20198     Diag(TyLoc, diag::err_omp_mapper_wrong_type);
20199     return QualType();
20200   }
20201   return MapperType;
20202 }
20203 
20204 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective(
20205     Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
20206     SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
20207     Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) {
20208   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
20209                       forRedeclarationInCurContext());
20210   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
20211   //  A mapper-identifier may not be redeclared in the current scope for the
20212   //  same type or for a type that is compatible according to the base language
20213   //  rules.
20214   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
20215   OMPDeclareMapperDecl *PrevDMD = nullptr;
20216   bool InCompoundScope = true;
20217   if (S != nullptr) {
20218     // Find previous declaration with the same name not referenced in other
20219     // declarations.
20220     FunctionScopeInfo *ParentFn = getEnclosingFunction();
20221     InCompoundScope =
20222         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
20223     LookupName(Lookup, S);
20224     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
20225                          /*AllowInlineNamespace=*/false);
20226     llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
20227     LookupResult::Filter Filter = Lookup.makeFilter();
20228     while (Filter.hasNext()) {
20229       auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
20230       if (InCompoundScope) {
20231         auto I = UsedAsPrevious.find(PrevDecl);
20232         if (I == UsedAsPrevious.end())
20233           UsedAsPrevious[PrevDecl] = false;
20234         if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
20235           UsedAsPrevious[D] = true;
20236       }
20237       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
20238           PrevDecl->getLocation();
20239     }
20240     Filter.done();
20241     if (InCompoundScope) {
20242       for (const auto &PrevData : UsedAsPrevious) {
20243         if (!PrevData.second) {
20244           PrevDMD = PrevData.first;
20245           break;
20246         }
20247       }
20248     }
20249   } else if (PrevDeclInScope) {
20250     auto *PrevDMDInScope = PrevDMD =
20251         cast<OMPDeclareMapperDecl>(PrevDeclInScope);
20252     do {
20253       PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
20254           PrevDMDInScope->getLocation();
20255       PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
20256     } while (PrevDMDInScope != nullptr);
20257   }
20258   const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
20259   bool Invalid = false;
20260   if (I != PreviousRedeclTypes.end()) {
20261     Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
20262         << MapperType << Name;
20263     Diag(I->second, diag::note_previous_definition);
20264     Invalid = true;
20265   }
20266   // Build expressions for implicit maps of data members with 'default'
20267   // mappers.
20268   SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses.begin(),
20269                                                   Clauses.end());
20270   if (LangOpts.OpenMP >= 50)
20271     processImplicitMapsWithDefaultMappers(*this, DSAStack, ClausesWithImplicit);
20272   auto *DMD =
20273       OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, MapperType, VN,
20274                                    ClausesWithImplicit, PrevDMD);
20275   if (S)
20276     PushOnScopeChains(DMD, S);
20277   else
20278     DC->addDecl(DMD);
20279   DMD->setAccess(AS);
20280   if (Invalid)
20281     DMD->setInvalidDecl();
20282 
20283   auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl();
20284   VD->setDeclContext(DMD);
20285   VD->setLexicalDeclContext(DMD);
20286   DMD->addDecl(VD);
20287   DMD->setMapperVarRef(MapperVarRef);
20288 
20289   return DeclGroupPtrTy::make(DeclGroupRef(DMD));
20290 }
20291 
20292 ExprResult
20293 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType,
20294                                                SourceLocation StartLoc,
20295                                                DeclarationName VN) {
20296   TypeSourceInfo *TInfo =
20297       Context.getTrivialTypeSourceInfo(MapperType, StartLoc);
20298   auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(),
20299                              StartLoc, StartLoc, VN.getAsIdentifierInfo(),
20300                              MapperType, TInfo, SC_None);
20301   if (S)
20302     PushOnScopeChains(VD, S, /*AddToContext=*/false);
20303   Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
20304   DSAStack->addDeclareMapperVarRef(E);
20305   return E;
20306 }
20307 
20308 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const {
20309   assert(LangOpts.OpenMP && "Expected OpenMP mode.");
20310   const Expr *Ref = DSAStack->getDeclareMapperVarRef();
20311   if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) {
20312     if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl())
20313       return true;
20314     if (VD->isUsableInConstantExpressions(Context))
20315       return true;
20316     return false;
20317   }
20318   return true;
20319 }
20320 
20321 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const {
20322   assert(LangOpts.OpenMP && "Expected OpenMP mode.");
20323   return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl();
20324 }
20325 
20326 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
20327                                            SourceLocation StartLoc,
20328                                            SourceLocation LParenLoc,
20329                                            SourceLocation EndLoc) {
20330   Expr *ValExpr = NumTeams;
20331   Stmt *HelperValStmt = nullptr;
20332 
20333   // OpenMP [teams Constrcut, Restrictions]
20334   // The num_teams expression must evaluate to a positive integer value.
20335   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
20336                                  /*StrictlyPositive=*/true))
20337     return nullptr;
20338 
20339   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
20340   OpenMPDirectiveKind CaptureRegion =
20341       getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP);
20342   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
20343     ValExpr = MakeFullExpr(ValExpr).get();
20344     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
20345     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
20346     HelperValStmt = buildPreInits(Context, Captures);
20347   }
20348 
20349   return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
20350                                          StartLoc, LParenLoc, EndLoc);
20351 }
20352 
20353 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
20354                                               SourceLocation StartLoc,
20355                                               SourceLocation LParenLoc,
20356                                               SourceLocation EndLoc) {
20357   Expr *ValExpr = ThreadLimit;
20358   Stmt *HelperValStmt = nullptr;
20359 
20360   // OpenMP [teams Constrcut, Restrictions]
20361   // The thread_limit expression must evaluate to a positive integer value.
20362   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
20363                                  /*StrictlyPositive=*/true))
20364     return nullptr;
20365 
20366   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
20367   OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
20368       DKind, OMPC_thread_limit, LangOpts.OpenMP);
20369   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
20370     ValExpr = MakeFullExpr(ValExpr).get();
20371     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
20372     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
20373     HelperValStmt = buildPreInits(Context, Captures);
20374   }
20375 
20376   return new (Context) OMPThreadLimitClause(
20377       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
20378 }
20379 
20380 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
20381                                            SourceLocation StartLoc,
20382                                            SourceLocation LParenLoc,
20383                                            SourceLocation EndLoc) {
20384   Expr *ValExpr = Priority;
20385   Stmt *HelperValStmt = nullptr;
20386   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
20387 
20388   // OpenMP [2.9.1, task Constrcut]
20389   // The priority-value is a non-negative numerical scalar expression.
20390   if (!isNonNegativeIntegerValue(
20391           ValExpr, *this, OMPC_priority,
20392           /*StrictlyPositive=*/false, /*BuildCapture=*/true,
20393           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
20394     return nullptr;
20395 
20396   return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion,
20397                                          StartLoc, LParenLoc, EndLoc);
20398 }
20399 
20400 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
20401                                             SourceLocation StartLoc,
20402                                             SourceLocation LParenLoc,
20403                                             SourceLocation EndLoc) {
20404   Expr *ValExpr = Grainsize;
20405   Stmt *HelperValStmt = nullptr;
20406   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
20407 
20408   // OpenMP [2.9.2, taskloop Constrcut]
20409   // The parameter of the grainsize clause must be a positive integer
20410   // expression.
20411   if (!isNonNegativeIntegerValue(
20412           ValExpr, *this, OMPC_grainsize,
20413           /*StrictlyPositive=*/true, /*BuildCapture=*/true,
20414           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
20415     return nullptr;
20416 
20417   return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion,
20418                                           StartLoc, LParenLoc, EndLoc);
20419 }
20420 
20421 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
20422                                            SourceLocation StartLoc,
20423                                            SourceLocation LParenLoc,
20424                                            SourceLocation EndLoc) {
20425   Expr *ValExpr = NumTasks;
20426   Stmt *HelperValStmt = nullptr;
20427   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
20428 
20429   // OpenMP [2.9.2, taskloop Constrcut]
20430   // The parameter of the num_tasks clause must be a positive integer
20431   // expression.
20432   if (!isNonNegativeIntegerValue(
20433           ValExpr, *this, OMPC_num_tasks,
20434           /*StrictlyPositive=*/true, /*BuildCapture=*/true,
20435           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
20436     return nullptr;
20437 
20438   return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion,
20439                                          StartLoc, LParenLoc, EndLoc);
20440 }
20441 
20442 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
20443                                        SourceLocation LParenLoc,
20444                                        SourceLocation EndLoc) {
20445   // OpenMP [2.13.2, critical construct, Description]
20446   // ... where hint-expression is an integer constant expression that evaluates
20447   // to a valid lock hint.
20448   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
20449   if (HintExpr.isInvalid())
20450     return nullptr;
20451   return new (Context)
20452       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
20453 }
20454 
20455 /// Tries to find omp_event_handle_t type.
20456 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc,
20457                                 DSAStackTy *Stack) {
20458   QualType OMPEventHandleT = Stack->getOMPEventHandleT();
20459   if (!OMPEventHandleT.isNull())
20460     return true;
20461   IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t");
20462   ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope());
20463   if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
20464     S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t";
20465     return false;
20466   }
20467   Stack->setOMPEventHandleT(PT.get());
20468   return true;
20469 }
20470 
20471 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc,
20472                                          SourceLocation LParenLoc,
20473                                          SourceLocation EndLoc) {
20474   if (!Evt->isValueDependent() && !Evt->isTypeDependent() &&
20475       !Evt->isInstantiationDependent() &&
20476       !Evt->containsUnexpandedParameterPack()) {
20477     if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack))
20478       return nullptr;
20479     // OpenMP 5.0, 2.10.1 task Construct.
20480     // event-handle is a variable of the omp_event_handle_t type.
20481     auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts());
20482     if (!Ref) {
20483       Diag(Evt->getExprLoc(), diag::err_omp_var_expected)
20484           << "omp_event_handle_t" << 0 << Evt->getSourceRange();
20485       return nullptr;
20486     }
20487     auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl());
20488     if (!VD) {
20489       Diag(Evt->getExprLoc(), diag::err_omp_var_expected)
20490           << "omp_event_handle_t" << 0 << Evt->getSourceRange();
20491       return nullptr;
20492     }
20493     if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(),
20494                                         VD->getType()) ||
20495         VD->getType().isConstant(Context)) {
20496       Diag(Evt->getExprLoc(), diag::err_omp_var_expected)
20497           << "omp_event_handle_t" << 1 << VD->getType()
20498           << Evt->getSourceRange();
20499       return nullptr;
20500     }
20501     // OpenMP 5.0, 2.10.1 task Construct
20502     // [detach clause]... The event-handle will be considered as if it was
20503     // specified on a firstprivate clause.
20504     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false);
20505     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
20506         DVar.RefExpr) {
20507       Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa)
20508           << getOpenMPClauseName(DVar.CKind)
20509           << getOpenMPClauseName(OMPC_firstprivate);
20510       reportOriginalDsa(*this, DSAStack, VD, DVar);
20511       return nullptr;
20512     }
20513   }
20514 
20515   return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc);
20516 }
20517 
20518 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
20519     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
20520     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
20521     SourceLocation EndLoc) {
20522   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
20523     std::string Values;
20524     Values += "'";
20525     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
20526     Values += "'";
20527     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
20528         << Values << getOpenMPClauseName(OMPC_dist_schedule);
20529     return nullptr;
20530   }
20531   Expr *ValExpr = ChunkSize;
20532   Stmt *HelperValStmt = nullptr;
20533   if (ChunkSize) {
20534     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
20535         !ChunkSize->isInstantiationDependent() &&
20536         !ChunkSize->containsUnexpandedParameterPack()) {
20537       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
20538       ExprResult Val =
20539           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
20540       if (Val.isInvalid())
20541         return nullptr;
20542 
20543       ValExpr = Val.get();
20544 
20545       // OpenMP [2.7.1, Restrictions]
20546       //  chunk_size must be a loop invariant integer expression with a positive
20547       //  value.
20548       if (Optional<llvm::APSInt> Result =
20549               ValExpr->getIntegerConstantExpr(Context)) {
20550         if (Result->isSigned() && !Result->isStrictlyPositive()) {
20551           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
20552               << "dist_schedule" << ChunkSize->getSourceRange();
20553           return nullptr;
20554         }
20555       } else if (getOpenMPCaptureRegionForClause(
20556                      DSAStack->getCurrentDirective(), OMPC_dist_schedule,
20557                      LangOpts.OpenMP) != OMPD_unknown &&
20558                  !CurContext->isDependentContext()) {
20559         ValExpr = MakeFullExpr(ValExpr).get();
20560         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
20561         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
20562         HelperValStmt = buildPreInits(Context, Captures);
20563       }
20564     }
20565   }
20566 
20567   return new (Context)
20568       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
20569                             Kind, ValExpr, HelperValStmt);
20570 }
20571 
20572 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
20573     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
20574     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
20575     SourceLocation KindLoc, SourceLocation EndLoc) {
20576   if (getLangOpts().OpenMP < 50) {
20577     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
20578         Kind != OMPC_DEFAULTMAP_scalar) {
20579       std::string Value;
20580       SourceLocation Loc;
20581       Value += "'";
20582       if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
20583         Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
20584                                                OMPC_DEFAULTMAP_MODIFIER_tofrom);
20585         Loc = MLoc;
20586       } else {
20587         Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
20588                                                OMPC_DEFAULTMAP_scalar);
20589         Loc = KindLoc;
20590       }
20591       Value += "'";
20592       Diag(Loc, diag::err_omp_unexpected_clause_value)
20593           << Value << getOpenMPClauseName(OMPC_defaultmap);
20594       return nullptr;
20595     }
20596   } else {
20597     bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown);
20598     bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) ||
20599                             (LangOpts.OpenMP >= 50 && KindLoc.isInvalid());
20600     if (!isDefaultmapKind || !isDefaultmapModifier) {
20601       StringRef KindValue = "'scalar', 'aggregate', 'pointer'";
20602       if (LangOpts.OpenMP == 50) {
20603         StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', "
20604                                   "'firstprivate', 'none', 'default'";
20605         if (!isDefaultmapKind && isDefaultmapModifier) {
20606           Diag(KindLoc, diag::err_omp_unexpected_clause_value)
20607               << KindValue << getOpenMPClauseName(OMPC_defaultmap);
20608         } else if (isDefaultmapKind && !isDefaultmapModifier) {
20609           Diag(MLoc, diag::err_omp_unexpected_clause_value)
20610               << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
20611         } else {
20612           Diag(MLoc, diag::err_omp_unexpected_clause_value)
20613               << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
20614           Diag(KindLoc, diag::err_omp_unexpected_clause_value)
20615               << KindValue << getOpenMPClauseName(OMPC_defaultmap);
20616         }
20617       } else {
20618         StringRef ModifierValue =
20619             "'alloc', 'from', 'to', 'tofrom', "
20620             "'firstprivate', 'none', 'default', 'present'";
20621         if (!isDefaultmapKind && isDefaultmapModifier) {
20622           Diag(KindLoc, diag::err_omp_unexpected_clause_value)
20623               << KindValue << getOpenMPClauseName(OMPC_defaultmap);
20624         } else if (isDefaultmapKind && !isDefaultmapModifier) {
20625           Diag(MLoc, diag::err_omp_unexpected_clause_value)
20626               << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
20627         } else {
20628           Diag(MLoc, diag::err_omp_unexpected_clause_value)
20629               << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
20630           Diag(KindLoc, diag::err_omp_unexpected_clause_value)
20631               << KindValue << getOpenMPClauseName(OMPC_defaultmap);
20632         }
20633       }
20634       return nullptr;
20635     }
20636 
20637     // OpenMP [5.0, 2.12.5, Restrictions, p. 174]
20638     //  At most one defaultmap clause for each category can appear on the
20639     //  directive.
20640     if (DSAStack->checkDefaultmapCategory(Kind)) {
20641       Diag(StartLoc, diag::err_omp_one_defaultmap_each_category);
20642       return nullptr;
20643     }
20644   }
20645   if (Kind == OMPC_DEFAULTMAP_unknown) {
20646     // Variable category is not specified - mark all categories.
20647     DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc);
20648     DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc);
20649     DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc);
20650   } else {
20651     DSAStack->setDefaultDMAAttr(M, Kind, StartLoc);
20652   }
20653 
20654   return new (Context)
20655       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
20656 }
20657 
20658 bool Sema::ActOnStartOpenMPDeclareTargetContext(
20659     DeclareTargetContextInfo &DTCI) {
20660   DeclContext *CurLexicalContext = getCurLexicalContext();
20661   if (!CurLexicalContext->isFileContext() &&
20662       !CurLexicalContext->isExternCContext() &&
20663       !CurLexicalContext->isExternCXXContext() &&
20664       !isa<CXXRecordDecl>(CurLexicalContext) &&
20665       !isa<ClassTemplateDecl>(CurLexicalContext) &&
20666       !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
20667       !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
20668     Diag(DTCI.Loc, diag::err_omp_region_not_file_context);
20669     return false;
20670   }
20671   DeclareTargetNesting.push_back(DTCI);
20672   return true;
20673 }
20674 
20675 const Sema::DeclareTargetContextInfo
20676 Sema::ActOnOpenMPEndDeclareTargetDirective() {
20677   assert(!DeclareTargetNesting.empty() &&
20678          "check isInOpenMPDeclareTargetContext() first!");
20679   return DeclareTargetNesting.pop_back_val();
20680 }
20681 
20682 void Sema::ActOnFinishedOpenMPDeclareTargetContext(
20683     DeclareTargetContextInfo &DTCI) {
20684   for (auto &It : DTCI.ExplicitlyMapped)
20685     ActOnOpenMPDeclareTargetName(It.first, It.second.Loc, It.second.MT,
20686                                  DTCI.DT);
20687 }
20688 
20689 NamedDecl *Sema::lookupOpenMPDeclareTargetName(Scope *CurScope,
20690                                                CXXScopeSpec &ScopeSpec,
20691                                                const DeclarationNameInfo &Id) {
20692   LookupResult Lookup(*this, Id, LookupOrdinaryName);
20693   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
20694 
20695   if (Lookup.isAmbiguous())
20696     return nullptr;
20697   Lookup.suppressDiagnostics();
20698 
20699   if (!Lookup.isSingleResult()) {
20700     VarOrFuncDeclFilterCCC CCC(*this);
20701     if (TypoCorrection Corrected =
20702             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
20703                         CTK_ErrorRecovery)) {
20704       diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
20705                                   << Id.getName());
20706       checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
20707       return nullptr;
20708     }
20709 
20710     Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
20711     return nullptr;
20712   }
20713 
20714   NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
20715   if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) &&
20716       !isa<FunctionTemplateDecl>(ND)) {
20717     Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
20718     return nullptr;
20719   }
20720   return ND;
20721 }
20722 
20723 void Sema::ActOnOpenMPDeclareTargetName(
20724     NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
20725     OMPDeclareTargetDeclAttr::DevTypeTy DT) {
20726   assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
20727           isa<FunctionTemplateDecl>(ND)) &&
20728          "Expected variable, function or function template.");
20729 
20730   // Diagnose marking after use as it may lead to incorrect diagnosis and
20731   // codegen.
20732   if (LangOpts.OpenMP >= 50 &&
20733       (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
20734     Diag(Loc, diag::warn_omp_declare_target_after_first_use);
20735 
20736   // Explicit declare target lists have precedence.
20737   const unsigned Level = -1;
20738 
20739   auto *VD = cast<ValueDecl>(ND);
20740   llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
20741       OMPDeclareTargetDeclAttr::getActiveAttr(VD);
20742   if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getDevType() != DT &&
20743       ActiveAttr.getValue()->getLevel() == Level) {
20744     Diag(Loc, diag::err_omp_device_type_mismatch)
20745         << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT)
20746         << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(
20747                ActiveAttr.getValue()->getDevType());
20748     return;
20749   }
20750   if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getMapType() != MT &&
20751       ActiveAttr.getValue()->getLevel() == Level) {
20752     Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND;
20753     return;
20754   }
20755 
20756   if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() == Level)
20757     return;
20758 
20759   auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT, Level,
20760                                                      SourceRange(Loc, Loc));
20761   ND->addAttr(A);
20762   if (ASTMutationListener *ML = Context.getASTMutationListener())
20763     ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
20764   checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc);
20765 }
20766 
20767 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
20768                                      Sema &SemaRef, Decl *D) {
20769   if (!D || !isa<VarDecl>(D))
20770     return;
20771   auto *VD = cast<VarDecl>(D);
20772   Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
20773       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
20774   if (SemaRef.LangOpts.OpenMP >= 50 &&
20775       (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
20776        SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
20777       VD->hasGlobalStorage()) {
20778     if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) {
20779       // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
20780       // If a lambda declaration and definition appears between a
20781       // declare target directive and the matching end declare target
20782       // directive, all variables that are captured by the lambda
20783       // expression must also appear in a to clause.
20784       SemaRef.Diag(VD->getLocation(),
20785                    diag::err_omp_lambda_capture_in_declare_target_not_to);
20786       SemaRef.Diag(SL, diag::note_var_explicitly_captured_here)
20787           << VD << 0 << SR;
20788       return;
20789     }
20790   }
20791   if (MapTy.hasValue())
20792     return;
20793   SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
20794   SemaRef.Diag(SL, diag::note_used_here) << SR;
20795 }
20796 
20797 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
20798                                    Sema &SemaRef, DSAStackTy *Stack,
20799                                    ValueDecl *VD) {
20800   return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
20801          checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
20802                            /*FullCheck=*/false);
20803 }
20804 
20805 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
20806                                             SourceLocation IdLoc) {
20807   if (!D || D->isInvalidDecl())
20808     return;
20809   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
20810   SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
20811   if (auto *VD = dyn_cast<VarDecl>(D)) {
20812     // Only global variables can be marked as declare target.
20813     if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
20814         !VD->isStaticDataMember())
20815       return;
20816     // 2.10.6: threadprivate variable cannot appear in a declare target
20817     // directive.
20818     if (DSAStack->isThreadPrivate(VD)) {
20819       Diag(SL, diag::err_omp_threadprivate_in_target);
20820       reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
20821       return;
20822     }
20823   }
20824   if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
20825     D = FTD->getTemplatedDecl();
20826   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
20827     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
20828         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
20829     if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
20830       Diag(IdLoc, diag::err_omp_function_in_link_clause);
20831       Diag(FD->getLocation(), diag::note_defined_here) << FD;
20832       return;
20833     }
20834   }
20835   if (auto *VD = dyn_cast<ValueDecl>(D)) {
20836     // Problem if any with var declared with incomplete type will be reported
20837     // as normal, so no need to check it here.
20838     if ((E || !VD->getType()->isIncompleteType()) &&
20839         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
20840       return;
20841     if (!E && isInOpenMPDeclareTargetContext()) {
20842       // Checking declaration inside declare target region.
20843       if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
20844           isa<FunctionTemplateDecl>(D)) {
20845         llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
20846             OMPDeclareTargetDeclAttr::getActiveAttr(VD);
20847         unsigned Level = DeclareTargetNesting.size();
20848         if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() >= Level)
20849           return;
20850         DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back();
20851         auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
20852             Context, OMPDeclareTargetDeclAttr::MT_To, DTCI.DT, Level,
20853             SourceRange(DTCI.Loc, DTCI.Loc));
20854         D->addAttr(A);
20855         if (ASTMutationListener *ML = Context.getASTMutationListener())
20856           ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
20857       }
20858       return;
20859     }
20860   }
20861   if (!E)
20862     return;
20863   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
20864 }
20865 
20866 OMPClause *Sema::ActOnOpenMPToClause(
20867     ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
20868     ArrayRef<SourceLocation> MotionModifiersLoc,
20869     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
20870     SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
20871     const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
20872   OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
20873                                           OMPC_MOTION_MODIFIER_unknown};
20874   SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
20875 
20876   // Process motion-modifiers, flag errors for duplicate modifiers.
20877   unsigned Count = 0;
20878   for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
20879     if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
20880         llvm::is_contained(Modifiers, MotionModifiers[I])) {
20881       Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier);
20882       continue;
20883     }
20884     assert(Count < NumberOfOMPMotionModifiers &&
20885            "Modifiers exceed the allowed number of motion modifiers");
20886     Modifiers[Count] = MotionModifiers[I];
20887     ModifiersLoc[Count] = MotionModifiersLoc[I];
20888     ++Count;
20889   }
20890 
20891   MappableVarListInfo MVLI(VarList);
20892   checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
20893                               MapperIdScopeSpec, MapperId, UnresolvedMappers);
20894   if (MVLI.ProcessedVarList.empty())
20895     return nullptr;
20896 
20897   return OMPToClause::Create(
20898       Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
20899       MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc,
20900       MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
20901 }
20902 
20903 OMPClause *Sema::ActOnOpenMPFromClause(
20904     ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
20905     ArrayRef<SourceLocation> MotionModifiersLoc,
20906     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
20907     SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
20908     const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
20909   OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
20910                                           OMPC_MOTION_MODIFIER_unknown};
20911   SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
20912 
20913   // Process motion-modifiers, flag errors for duplicate modifiers.
20914   unsigned Count = 0;
20915   for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
20916     if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
20917         llvm::is_contained(Modifiers, MotionModifiers[I])) {
20918       Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier);
20919       continue;
20920     }
20921     assert(Count < NumberOfOMPMotionModifiers &&
20922            "Modifiers exceed the allowed number of motion modifiers");
20923     Modifiers[Count] = MotionModifiers[I];
20924     ModifiersLoc[Count] = MotionModifiersLoc[I];
20925     ++Count;
20926   }
20927 
20928   MappableVarListInfo MVLI(VarList);
20929   checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
20930                               MapperIdScopeSpec, MapperId, UnresolvedMappers);
20931   if (MVLI.ProcessedVarList.empty())
20932     return nullptr;
20933 
20934   return OMPFromClause::Create(
20935       Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
20936       MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc,
20937       MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
20938 }
20939 
20940 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
20941                                                const OMPVarListLocTy &Locs) {
20942   MappableVarListInfo MVLI(VarList);
20943   SmallVector<Expr *, 8> PrivateCopies;
20944   SmallVector<Expr *, 8> Inits;
20945 
20946   for (Expr *RefExpr : VarList) {
20947     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
20948     SourceLocation ELoc;
20949     SourceRange ERange;
20950     Expr *SimpleRefExpr = RefExpr;
20951     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
20952     if (Res.second) {
20953       // It will be analyzed later.
20954       MVLI.ProcessedVarList.push_back(RefExpr);
20955       PrivateCopies.push_back(nullptr);
20956       Inits.push_back(nullptr);
20957     }
20958     ValueDecl *D = Res.first;
20959     if (!D)
20960       continue;
20961 
20962     QualType Type = D->getType();
20963     Type = Type.getNonReferenceType().getUnqualifiedType();
20964 
20965     auto *VD = dyn_cast<VarDecl>(D);
20966 
20967     // Item should be a pointer or reference to pointer.
20968     if (!Type->isPointerType()) {
20969       Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
20970           << 0 << RefExpr->getSourceRange();
20971       continue;
20972     }
20973 
20974     // Build the private variable and the expression that refers to it.
20975     auto VDPrivate =
20976         buildVarDecl(*this, ELoc, Type, D->getName(),
20977                      D->hasAttrs() ? &D->getAttrs() : nullptr,
20978                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
20979     if (VDPrivate->isInvalidDecl())
20980       continue;
20981 
20982     CurContext->addDecl(VDPrivate);
20983     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
20984         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
20985 
20986     // Add temporary variable to initialize the private copy of the pointer.
20987     VarDecl *VDInit =
20988         buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
20989     DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
20990         *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
20991     AddInitializerToDecl(VDPrivate,
20992                          DefaultLvalueConversion(VDInitRefExpr).get(),
20993                          /*DirectInit=*/false);
20994 
20995     // If required, build a capture to implement the privatization initialized
20996     // with the current list item value.
20997     DeclRefExpr *Ref = nullptr;
20998     if (!VD)
20999       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
21000     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
21001     PrivateCopies.push_back(VDPrivateRefExpr);
21002     Inits.push_back(VDInitRefExpr);
21003 
21004     // We need to add a data sharing attribute for this variable to make sure it
21005     // is correctly captured. A variable that shows up in a use_device_ptr has
21006     // similar properties of a first private variable.
21007     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
21008 
21009     // Create a mappable component for the list item. List items in this clause
21010     // only need a component.
21011     MVLI.VarBaseDeclarations.push_back(D);
21012     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
21013     MVLI.VarComponents.back().emplace_back(SimpleRefExpr, D,
21014                                            /*IsNonContiguous=*/false);
21015   }
21016 
21017   if (MVLI.ProcessedVarList.empty())
21018     return nullptr;
21019 
21020   return OMPUseDevicePtrClause::Create(
21021       Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
21022       MVLI.VarBaseDeclarations, MVLI.VarComponents);
21023 }
21024 
21025 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList,
21026                                                 const OMPVarListLocTy &Locs) {
21027   MappableVarListInfo MVLI(VarList);
21028 
21029   for (Expr *RefExpr : VarList) {
21030     assert(RefExpr && "NULL expr in OpenMP use_device_addr clause.");
21031     SourceLocation ELoc;
21032     SourceRange ERange;
21033     Expr *SimpleRefExpr = RefExpr;
21034     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
21035                               /*AllowArraySection=*/true);
21036     if (Res.second) {
21037       // It will be analyzed later.
21038       MVLI.ProcessedVarList.push_back(RefExpr);
21039     }
21040     ValueDecl *D = Res.first;
21041     if (!D)
21042       continue;
21043     auto *VD = dyn_cast<VarDecl>(D);
21044 
21045     // If required, build a capture to implement the privatization initialized
21046     // with the current list item value.
21047     DeclRefExpr *Ref = nullptr;
21048     if (!VD)
21049       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
21050     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
21051 
21052     // We need to add a data sharing attribute for this variable to make sure it
21053     // is correctly captured. A variable that shows up in a use_device_addr has
21054     // similar properties of a first private variable.
21055     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
21056 
21057     // Create a mappable component for the list item. List items in this clause
21058     // only need a component.
21059     MVLI.VarBaseDeclarations.push_back(D);
21060     MVLI.VarComponents.emplace_back();
21061     Expr *Component = SimpleRefExpr;
21062     if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) ||
21063                isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts())))
21064       Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get();
21065     MVLI.VarComponents.back().emplace_back(Component, D,
21066                                            /*IsNonContiguous=*/false);
21067   }
21068 
21069   if (MVLI.ProcessedVarList.empty())
21070     return nullptr;
21071 
21072   return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList,
21073                                         MVLI.VarBaseDeclarations,
21074                                         MVLI.VarComponents);
21075 }
21076 
21077 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
21078                                               const OMPVarListLocTy &Locs) {
21079   MappableVarListInfo MVLI(VarList);
21080   for (Expr *RefExpr : VarList) {
21081     assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
21082     SourceLocation ELoc;
21083     SourceRange ERange;
21084     Expr *SimpleRefExpr = RefExpr;
21085     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
21086     if (Res.second) {
21087       // It will be analyzed later.
21088       MVLI.ProcessedVarList.push_back(RefExpr);
21089     }
21090     ValueDecl *D = Res.first;
21091     if (!D)
21092       continue;
21093 
21094     QualType Type = D->getType();
21095     // item should be a pointer or array or reference to pointer or array
21096     if (!Type.getNonReferenceType()->isPointerType() &&
21097         !Type.getNonReferenceType()->isArrayType()) {
21098       Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
21099           << 0 << RefExpr->getSourceRange();
21100       continue;
21101     }
21102 
21103     // Check if the declaration in the clause does not show up in any data
21104     // sharing attribute.
21105     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
21106     if (isOpenMPPrivate(DVar.CKind)) {
21107       Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
21108           << getOpenMPClauseName(DVar.CKind)
21109           << getOpenMPClauseName(OMPC_is_device_ptr)
21110           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
21111       reportOriginalDsa(*this, DSAStack, D, DVar);
21112       continue;
21113     }
21114 
21115     const Expr *ConflictExpr;
21116     if (DSAStack->checkMappableExprComponentListsForDecl(
21117             D, /*CurrentRegionOnly=*/true,
21118             [&ConflictExpr](
21119                 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
21120                 OpenMPClauseKind) -> bool {
21121               ConflictExpr = R.front().getAssociatedExpression();
21122               return true;
21123             })) {
21124       Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
21125       Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
21126           << ConflictExpr->getSourceRange();
21127       continue;
21128     }
21129 
21130     // Store the components in the stack so that they can be used to check
21131     // against other clauses later on.
21132     OMPClauseMappableExprCommon::MappableComponent MC(
21133         SimpleRefExpr, D, /*IsNonContiguous=*/false);
21134     DSAStack->addMappableExpressionComponents(
21135         D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
21136 
21137     // Record the expression we've just processed.
21138     MVLI.ProcessedVarList.push_back(SimpleRefExpr);
21139 
21140     // Create a mappable component for the list item. List items in this clause
21141     // only need a component. We use a null declaration to signal fields in
21142     // 'this'.
21143     assert((isa<DeclRefExpr>(SimpleRefExpr) ||
21144             isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
21145            "Unexpected device pointer expression!");
21146     MVLI.VarBaseDeclarations.push_back(
21147         isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
21148     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
21149     MVLI.VarComponents.back().push_back(MC);
21150   }
21151 
21152   if (MVLI.ProcessedVarList.empty())
21153     return nullptr;
21154 
21155   return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
21156                                       MVLI.VarBaseDeclarations,
21157                                       MVLI.VarComponents);
21158 }
21159 
21160 OMPClause *Sema::ActOnOpenMPAllocateClause(
21161     Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
21162     SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
21163   if (Allocator) {
21164     // OpenMP [2.11.4 allocate Clause, Description]
21165     // allocator is an expression of omp_allocator_handle_t type.
21166     if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack))
21167       return nullptr;
21168 
21169     ExprResult AllocatorRes = DefaultLvalueConversion(Allocator);
21170     if (AllocatorRes.isInvalid())
21171       return nullptr;
21172     AllocatorRes = PerformImplicitConversion(AllocatorRes.get(),
21173                                              DSAStack->getOMPAllocatorHandleT(),
21174                                              Sema::AA_Initializing,
21175                                              /*AllowExplicit=*/true);
21176     if (AllocatorRes.isInvalid())
21177       return nullptr;
21178     Allocator = AllocatorRes.get();
21179   } else {
21180     // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
21181     // allocate clauses that appear on a target construct or on constructs in a
21182     // target region must specify an allocator expression unless a requires
21183     // directive with the dynamic_allocators clause is present in the same
21184     // compilation unit.
21185     if (LangOpts.OpenMPIsDevice &&
21186         !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
21187       targetDiag(StartLoc, diag::err_expected_allocator_expression);
21188   }
21189   // Analyze and build list of variables.
21190   SmallVector<Expr *, 8> Vars;
21191   for (Expr *RefExpr : VarList) {
21192     assert(RefExpr && "NULL expr in OpenMP private clause.");
21193     SourceLocation ELoc;
21194     SourceRange ERange;
21195     Expr *SimpleRefExpr = RefExpr;
21196     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
21197     if (Res.second) {
21198       // It will be analyzed later.
21199       Vars.push_back(RefExpr);
21200     }
21201     ValueDecl *D = Res.first;
21202     if (!D)
21203       continue;
21204 
21205     auto *VD = dyn_cast<VarDecl>(D);
21206     DeclRefExpr *Ref = nullptr;
21207     if (!VD && !CurContext->isDependentContext())
21208       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
21209     Vars.push_back((VD || CurContext->isDependentContext())
21210                        ? RefExpr->IgnoreParens()
21211                        : Ref);
21212   }
21213 
21214   if (Vars.empty())
21215     return nullptr;
21216 
21217   if (Allocator)
21218     DSAStack->addInnerAllocatorExpr(Allocator);
21219   return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator,
21220                                    ColonLoc, EndLoc, Vars);
21221 }
21222 
21223 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList,
21224                                               SourceLocation StartLoc,
21225                                               SourceLocation LParenLoc,
21226                                               SourceLocation EndLoc) {
21227   SmallVector<Expr *, 8> Vars;
21228   for (Expr *RefExpr : VarList) {
21229     assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
21230     SourceLocation ELoc;
21231     SourceRange ERange;
21232     Expr *SimpleRefExpr = RefExpr;
21233     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
21234     if (Res.second)
21235       // It will be analyzed later.
21236       Vars.push_back(RefExpr);
21237     ValueDecl *D = Res.first;
21238     if (!D)
21239       continue;
21240 
21241     // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions.
21242     // A list-item cannot appear in more than one nontemporal clause.
21243     if (const Expr *PrevRef =
21244             DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) {
21245       Diag(ELoc, diag::err_omp_used_in_clause_twice)
21246           << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange;
21247       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
21248           << getOpenMPClauseName(OMPC_nontemporal);
21249       continue;
21250     }
21251 
21252     Vars.push_back(RefExpr);
21253   }
21254 
21255   if (Vars.empty())
21256     return nullptr;
21257 
21258   return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc,
21259                                       Vars);
21260 }
21261 
21262 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList,
21263                                             SourceLocation StartLoc,
21264                                             SourceLocation LParenLoc,
21265                                             SourceLocation EndLoc) {
21266   SmallVector<Expr *, 8> Vars;
21267   for (Expr *RefExpr : VarList) {
21268     assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
21269     SourceLocation ELoc;
21270     SourceRange ERange;
21271     Expr *SimpleRefExpr = RefExpr;
21272     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
21273                               /*AllowArraySection=*/true);
21274     if (Res.second)
21275       // It will be analyzed later.
21276       Vars.push_back(RefExpr);
21277     ValueDecl *D = Res.first;
21278     if (!D)
21279       continue;
21280 
21281     const DSAStackTy::DSAVarData DVar =
21282         DSAStack->getTopDSA(D, /*FromParent=*/true);
21283     // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
21284     // A list item that appears in the inclusive or exclusive clause must appear
21285     // in a reduction clause with the inscan modifier on the enclosing
21286     // worksharing-loop, worksharing-loop SIMD, or simd construct.
21287     if (DVar.CKind != OMPC_reduction ||
21288         DVar.Modifier != OMPC_REDUCTION_inscan)
21289       Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
21290           << RefExpr->getSourceRange();
21291 
21292     if (DSAStack->getParentDirective() != OMPD_unknown)
21293       DSAStack->markDeclAsUsedInScanDirective(D);
21294     Vars.push_back(RefExpr);
21295   }
21296 
21297   if (Vars.empty())
21298     return nullptr;
21299 
21300   return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
21301 }
21302 
21303 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList,
21304                                             SourceLocation StartLoc,
21305                                             SourceLocation LParenLoc,
21306                                             SourceLocation EndLoc) {
21307   SmallVector<Expr *, 8> Vars;
21308   for (Expr *RefExpr : VarList) {
21309     assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
21310     SourceLocation ELoc;
21311     SourceRange ERange;
21312     Expr *SimpleRefExpr = RefExpr;
21313     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
21314                               /*AllowArraySection=*/true);
21315     if (Res.second)
21316       // It will be analyzed later.
21317       Vars.push_back(RefExpr);
21318     ValueDecl *D = Res.first;
21319     if (!D)
21320       continue;
21321 
21322     OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
21323     DSAStackTy::DSAVarData DVar;
21324     if (ParentDirective != OMPD_unknown)
21325       DVar = DSAStack->getTopDSA(D, /*FromParent=*/true);
21326     // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
21327     // A list item that appears in the inclusive or exclusive clause must appear
21328     // in a reduction clause with the inscan modifier on the enclosing
21329     // worksharing-loop, worksharing-loop SIMD, or simd construct.
21330     if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction ||
21331         DVar.Modifier != OMPC_REDUCTION_inscan) {
21332       Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
21333           << RefExpr->getSourceRange();
21334     } else {
21335       DSAStack->markDeclAsUsedInScanDirective(D);
21336     }
21337     Vars.push_back(RefExpr);
21338   }
21339 
21340   if (Vars.empty())
21341     return nullptr;
21342 
21343   return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
21344 }
21345 
21346 /// Tries to find omp_alloctrait_t type.
21347 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) {
21348   QualType OMPAlloctraitT = Stack->getOMPAlloctraitT();
21349   if (!OMPAlloctraitT.isNull())
21350     return true;
21351   IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t");
21352   ParsedType PT = S.getTypeName(II, Loc, S.getCurScope());
21353   if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
21354     S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t";
21355     return false;
21356   }
21357   Stack->setOMPAlloctraitT(PT.get());
21358   return true;
21359 }
21360 
21361 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause(
21362     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc,
21363     ArrayRef<UsesAllocatorsData> Data) {
21364   // OpenMP [2.12.5, target Construct]
21365   // allocator is an identifier of omp_allocator_handle_t type.
21366   if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack))
21367     return nullptr;
21368   // OpenMP [2.12.5, target Construct]
21369   // allocator-traits-array is an identifier of const omp_alloctrait_t * type.
21370   if (llvm::any_of(
21371           Data,
21372           [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) &&
21373       !findOMPAlloctraitT(*this, StartLoc, DSAStack))
21374     return nullptr;
21375   llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators;
21376   for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
21377     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
21378     StringRef Allocator =
21379         OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
21380     DeclarationName AllocatorName = &Context.Idents.get(Allocator);
21381     PredefinedAllocators.insert(LookupSingleName(
21382         TUScope, AllocatorName, StartLoc, Sema::LookupAnyName));
21383   }
21384 
21385   SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData;
21386   for (const UsesAllocatorsData &D : Data) {
21387     Expr *AllocatorExpr = nullptr;
21388     // Check allocator expression.
21389     if (D.Allocator->isTypeDependent()) {
21390       AllocatorExpr = D.Allocator;
21391     } else {
21392       // Traits were specified - need to assign new allocator to the specified
21393       // allocator, so it must be an lvalue.
21394       AllocatorExpr = D.Allocator->IgnoreParenImpCasts();
21395       auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr);
21396       bool IsPredefinedAllocator = false;
21397       if (DRE)
21398         IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl());
21399       if (!DRE ||
21400           !(Context.hasSameUnqualifiedType(
21401                 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) ||
21402             Context.typesAreCompatible(AllocatorExpr->getType(),
21403                                        DSAStack->getOMPAllocatorHandleT(),
21404                                        /*CompareUnqualified=*/true)) ||
21405           (!IsPredefinedAllocator &&
21406            (AllocatorExpr->getType().isConstant(Context) ||
21407             !AllocatorExpr->isLValue()))) {
21408         Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected)
21409             << "omp_allocator_handle_t" << (DRE ? 1 : 0)
21410             << AllocatorExpr->getType() << D.Allocator->getSourceRange();
21411         continue;
21412       }
21413       // OpenMP [2.12.5, target Construct]
21414       // Predefined allocators appearing in a uses_allocators clause cannot have
21415       // traits specified.
21416       if (IsPredefinedAllocator && D.AllocatorTraits) {
21417         Diag(D.AllocatorTraits->getExprLoc(),
21418              diag::err_omp_predefined_allocator_with_traits)
21419             << D.AllocatorTraits->getSourceRange();
21420         Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator)
21421             << cast<NamedDecl>(DRE->getDecl())->getName()
21422             << D.Allocator->getSourceRange();
21423         continue;
21424       }
21425       // OpenMP [2.12.5, target Construct]
21426       // Non-predefined allocators appearing in a uses_allocators clause must
21427       // have traits specified.
21428       if (!IsPredefinedAllocator && !D.AllocatorTraits) {
21429         Diag(D.Allocator->getExprLoc(),
21430              diag::err_omp_nonpredefined_allocator_without_traits);
21431         continue;
21432       }
21433       // No allocator traits - just convert it to rvalue.
21434       if (!D.AllocatorTraits)
21435         AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get();
21436       DSAStack->addUsesAllocatorsDecl(
21437           DRE->getDecl(),
21438           IsPredefinedAllocator
21439               ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator
21440               : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator);
21441     }
21442     Expr *AllocatorTraitsExpr = nullptr;
21443     if (D.AllocatorTraits) {
21444       if (D.AllocatorTraits->isTypeDependent()) {
21445         AllocatorTraitsExpr = D.AllocatorTraits;
21446       } else {
21447         // OpenMP [2.12.5, target Construct]
21448         // Arrays that contain allocator traits that appear in a uses_allocators
21449         // clause must be constant arrays, have constant values and be defined
21450         // in the same scope as the construct in which the clause appears.
21451         AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts();
21452         // Check that traits expr is a constant array.
21453         QualType TraitTy;
21454         if (const ArrayType *Ty =
21455                 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe())
21456           if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty))
21457             TraitTy = ConstArrayTy->getElementType();
21458         if (TraitTy.isNull() ||
21459             !(Context.hasSameUnqualifiedType(TraitTy,
21460                                              DSAStack->getOMPAlloctraitT()) ||
21461               Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(),
21462                                          /*CompareUnqualified=*/true))) {
21463           Diag(D.AllocatorTraits->getExprLoc(),
21464                diag::err_omp_expected_array_alloctraits)
21465               << AllocatorTraitsExpr->getType();
21466           continue;
21467         }
21468         // Do not map by default allocator traits if it is a standalone
21469         // variable.
21470         if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr))
21471           DSAStack->addUsesAllocatorsDecl(
21472               DRE->getDecl(),
21473               DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait);
21474       }
21475     }
21476     OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back();
21477     NewD.Allocator = AllocatorExpr;
21478     NewD.AllocatorTraits = AllocatorTraitsExpr;
21479     NewD.LParenLoc = D.LParenLoc;
21480     NewD.RParenLoc = D.RParenLoc;
21481   }
21482   return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc,
21483                                          NewData);
21484 }
21485 
21486 OMPClause *Sema::ActOnOpenMPAffinityClause(
21487     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
21488     SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) {
21489   SmallVector<Expr *, 8> Vars;
21490   for (Expr *RefExpr : Locators) {
21491     assert(RefExpr && "NULL expr in OpenMP shared clause.");
21492     if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) {
21493       // It will be analyzed later.
21494       Vars.push_back(RefExpr);
21495       continue;
21496     }
21497 
21498     SourceLocation ELoc = RefExpr->getExprLoc();
21499     Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts();
21500 
21501     if (!SimpleExpr->isLValue()) {
21502       Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
21503           << 1 << 0 << RefExpr->getSourceRange();
21504       continue;
21505     }
21506 
21507     ExprResult Res;
21508     {
21509       Sema::TentativeAnalysisScope Trap(*this);
21510       Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr);
21511     }
21512     if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) &&
21513         !isa<OMPArrayShapingExpr>(SimpleExpr)) {
21514       Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
21515           << 1 << 0 << RefExpr->getSourceRange();
21516       continue;
21517     }
21518     Vars.push_back(SimpleExpr);
21519   }
21520 
21521   return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
21522                                    EndLoc, Modifier, Vars);
21523 }
21524